]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/cifs/smb2inode.c
smb3: fix redundant opens on root
[linux.git] / fs / cifs / smb2inode.c
1 /*
2  *   fs/cifs/smb2inode.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002, 2011
5  *                 Etersoft, 2012
6  *   Author(s): Pavel Shilovsky (pshilovsky@samba.org),
7  *              Steve French (sfrench@us.ibm.com)
8  *
9  *   This library is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU Lesser General Public License as published
11  *   by the Free Software Foundation; either version 2.1 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This library is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17  *   the GNU Lesser General Public License for more details.
18  *
19  *   You should have received a copy of the GNU Lesser General Public License
20  *   along with this library; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 #include <linux/fs.h>
24 #include <linux/stat.h>
25 #include <linux/slab.h>
26 #include <linux/pagemap.h>
27 #include <asm/div64.h>
28 #include "cifsfs.h"
29 #include "cifspdu.h"
30 #include "cifsglob.h"
31 #include "cifsproto.h"
32 #include "cifs_debug.h"
33 #include "cifs_fs_sb.h"
34 #include "cifs_unicode.h"
35 #include "fscache.h"
36 #include "smb2glob.h"
37 #include "smb2pdu.h"
38 #include "smb2proto.h"
39
40 static int
41 smb2_open_op_close(const unsigned int xid, struct cifs_tcon *tcon,
42                    struct cifs_sb_info *cifs_sb, const char *full_path,
43                    __u32 desired_access, __u32 create_disposition,
44                    __u32 create_options, void *data, int command)
45 {
46         int rc, tmprc = 0;
47         __le16 *utf16_path = NULL;
48         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
49         struct cifs_open_parms oparms;
50         struct cifs_fid fid;
51         bool use_cached_root_handle = false;
52
53         if ((strcmp(full_path, "") == 0) && (create_options == 0) &&
54             (desired_access == FILE_READ_ATTRIBUTES) &&
55             (create_disposition == FILE_OPEN) &&
56             (tcon->nohandlecache == false)) {
57                 rc = open_shroot(xid, tcon, &fid);
58                 if (rc == 0)
59                         use_cached_root_handle = true;
60         }
61
62         if (use_cached_root_handle == false) {
63                 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
64                 if (!utf16_path)
65                         return -ENOMEM;
66
67                 oparms.tcon = tcon;
68                 oparms.desired_access = desired_access;
69                 oparms.disposition = create_disposition;
70                 oparms.create_options = create_options;
71                 oparms.fid = &fid;
72                 oparms.reconnect = false;
73
74                 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
75                 if (rc) {
76                         kfree(utf16_path);
77                         return rc;
78                 }
79         }
80
81         switch (command) {
82         case SMB2_OP_DELETE:
83                 break;
84         case SMB2_OP_QUERY_INFO:
85                 tmprc = SMB2_query_info(xid, tcon, fid.persistent_fid,
86                                         fid.volatile_fid,
87                                         (struct smb2_file_all_info *)data);
88                 break;
89         case SMB2_OP_MKDIR:
90                 /*
91                  * Directories are created through parameters in the
92                  * SMB2_open() call.
93                  */
94                 break;
95         case SMB2_OP_RMDIR:
96                 tmprc = SMB2_rmdir(xid, tcon, fid.persistent_fid,
97                                    fid.volatile_fid);
98                 break;
99         case SMB2_OP_RENAME:
100                 tmprc = SMB2_rename(xid, tcon, fid.persistent_fid,
101                                     fid.volatile_fid, (__le16 *)data);
102                 break;
103         case SMB2_OP_HARDLINK:
104                 tmprc = SMB2_set_hardlink(xid, tcon, fid.persistent_fid,
105                                           fid.volatile_fid, (__le16 *)data);
106                 break;
107         case SMB2_OP_SET_EOF:
108                 tmprc = SMB2_set_eof(xid, tcon, fid.persistent_fid,
109                                      fid.volatile_fid, current->tgid,
110                                      (__le64 *)data, false);
111                 break;
112         case SMB2_OP_SET_INFO:
113                 tmprc = SMB2_set_info(xid, tcon, fid.persistent_fid,
114                                       fid.volatile_fid,
115                                       (FILE_BASIC_INFO *)data);
116                 break;
117         default:
118                 cifs_dbg(VFS, "Invalid command\n");
119                 break;
120         }
121
122         if (use_cached_root_handle == false)
123                 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
124         if (tmprc)
125                 rc = tmprc;
126         kfree(utf16_path);
127         return rc;
128 }
129
130 void
131 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
132 {
133         memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
134         dst->CurrentByteOffset = src->CurrentByteOffset;
135         dst->Mode = src->Mode;
136         dst->AlignmentRequirement = src->AlignmentRequirement;
137         dst->IndexNumber1 = 0; /* we don't use it */
138 }
139
140 int
141 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
142                      struct cifs_sb_info *cifs_sb, const char *full_path,
143                      FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink)
144 {
145         int rc;
146         struct smb2_file_all_info *smb2_data;
147
148         *adjust_tz = false;
149         *symlink = false;
150
151         smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
152                             GFP_KERNEL);
153         if (smb2_data == NULL)
154                 return -ENOMEM;
155
156         rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path,
157                                 FILE_READ_ATTRIBUTES, FILE_OPEN, 0,
158                                 smb2_data, SMB2_OP_QUERY_INFO);
159         if (rc == -EOPNOTSUPP) {
160                 *symlink = true;
161                 /* Failed on a symbolic link - query a reparse point info */
162                 rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path,
163                                         FILE_READ_ATTRIBUTES, FILE_OPEN,
164                                         OPEN_REPARSE_POINT, smb2_data,
165                                         SMB2_OP_QUERY_INFO);
166         }
167         if (rc)
168                 goto out;
169
170         move_smb2_info_to_cifs(data, smb2_data);
171 out:
172         kfree(smb2_data);
173         return rc;
174 }
175
176 int
177 smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
178            struct cifs_sb_info *cifs_sb)
179 {
180         return smb2_open_op_close(xid, tcon, cifs_sb, name,
181                                   FILE_WRITE_ATTRIBUTES, FILE_CREATE,
182                                   CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR);
183 }
184
185 void
186 smb2_mkdir_setinfo(struct inode *inode, const char *name,
187                    struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
188                    const unsigned int xid)
189 {
190         FILE_BASIC_INFO data;
191         struct cifsInodeInfo *cifs_i;
192         u32 dosattrs;
193         int tmprc;
194
195         memset(&data, 0, sizeof(data));
196         cifs_i = CIFS_I(inode);
197         dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
198         data.Attributes = cpu_to_le32(dosattrs);
199         tmprc = smb2_open_op_close(xid, tcon, cifs_sb, name,
200                                    FILE_WRITE_ATTRIBUTES, FILE_CREATE,
201                                    CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO);
202         if (tmprc == 0)
203                 cifs_i->cifsAttrs = dosattrs;
204 }
205
206 int
207 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
208            struct cifs_sb_info *cifs_sb)
209 {
210         return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
211                                   CREATE_NOT_FILE,
212                                   NULL, SMB2_OP_RMDIR);
213 }
214
215 int
216 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
217             struct cifs_sb_info *cifs_sb)
218 {
219         return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
220                                   CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
221                                   NULL, SMB2_OP_DELETE);
222 }
223
224 static int
225 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
226                    const char *from_name, const char *to_name,
227                    struct cifs_sb_info *cifs_sb, __u32 access, int command)
228 {
229         __le16 *smb2_to_name = NULL;
230         int rc;
231
232         smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
233         if (smb2_to_name == NULL) {
234                 rc = -ENOMEM;
235                 goto smb2_rename_path;
236         }
237
238         rc = smb2_open_op_close(xid, tcon, cifs_sb, from_name, access,
239                                 FILE_OPEN, 0, smb2_to_name, command);
240 smb2_rename_path:
241         kfree(smb2_to_name);
242         return rc;
243 }
244
245 int
246 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
247                  const char *from_name, const char *to_name,
248                  struct cifs_sb_info *cifs_sb)
249 {
250         return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
251                                   DELETE, SMB2_OP_RENAME);
252 }
253
254 int
255 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
256                      const char *from_name, const char *to_name,
257                      struct cifs_sb_info *cifs_sb)
258 {
259         return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
260                                   FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK);
261 }
262
263 int
264 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
265                    const char *full_path, __u64 size,
266                    struct cifs_sb_info *cifs_sb, bool set_alloc)
267 {
268         __le64 eof = cpu_to_le64(size);
269         return smb2_open_op_close(xid, tcon, cifs_sb, full_path,
270                                   FILE_WRITE_DATA, FILE_OPEN, 0, &eof,
271                                   SMB2_OP_SET_EOF);
272 }
273
274 int
275 smb2_set_file_info(struct inode *inode, const char *full_path,
276                    FILE_BASIC_INFO *buf, const unsigned int xid)
277 {
278         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
279         struct tcon_link *tlink;
280         int rc;
281
282         if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
283             (buf->LastWriteTime == 0) && (buf->ChangeTime) &&
284             (buf->Attributes == 0))
285                 return 0; /* would be a no op, no sense sending this */
286
287         tlink = cifs_sb_tlink(cifs_sb);
288         if (IS_ERR(tlink))
289                 return PTR_ERR(tlink);
290
291         rc = smb2_open_op_close(xid, tlink_tcon(tlink), cifs_sb, full_path,
292                                 FILE_WRITE_ATTRIBUTES, FILE_OPEN, 0, buf,
293                                 SMB2_OP_SET_INFO);
294         cifs_put_tlink(tlink);
295         return rc;
296 }