cifs: add a smb2_compound_op and change QUERY_INFO to use it
[platform/kernel/linux-rpi.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_compound_op(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;
47         __le16 *utf16_path = NULL;
48         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
49         struct cifs_open_parms oparms;
50         struct cifs_fid fid;
51         struct cifs_ses *ses = tcon->ses;
52         struct TCP_Server_Info *server = ses->server;
53         int num_rqst = 0;
54         struct smb_rqst rqst[3];
55         int resp_buftype[3];
56         struct kvec rsp_iov[3];
57         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
58         struct kvec qi_iov[1];
59         struct kvec close_iov[1];
60         struct smb2_query_info_rsp *rsp = NULL;
61         int flags = 0;
62
63         if (smb3_encryption_required(tcon))
64                 flags |= CIFS_TRANSFORM_REQ;
65
66         memset(rqst, 0, sizeof(rqst));
67         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
68         memset(rsp_iov, 0, sizeof(rsp_iov));
69
70         /* Open */
71         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
72         if (!utf16_path)
73                 return -ENOMEM;
74
75         oparms.tcon = tcon;
76         oparms.desired_access = desired_access;
77         oparms.disposition = create_disposition;
78         oparms.create_options = create_options;
79         oparms.fid = &fid;
80         oparms.reconnect = false;
81
82         memset(&open_iov, 0, sizeof(open_iov));
83         rqst[num_rqst].rq_iov = open_iov;
84         rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
85         rc = SMB2_open_init(tcon, &rqst[num_rqst], &oplock, &oparms,
86                             utf16_path);
87         kfree(utf16_path);
88         if (rc)
89                 goto finished;
90
91         smb2_set_next_command(server, &rqst[num_rqst++]);
92
93         /* Operation */
94         switch (command) {
95         case SMB2_OP_QUERY_INFO:
96                 memset(&qi_iov, 0, sizeof(qi_iov));
97                 rqst[num_rqst].rq_iov = qi_iov;
98                 rqst[num_rqst].rq_nvec = 1;
99
100                 rc = SMB2_query_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
101                                 COMPOUND_FID, FILE_ALL_INFORMATION,
102                                 SMB2_O_INFO_FILE, 0,
103                                 sizeof(struct smb2_file_all_info) +
104                                           PATH_MAX * 2);
105                 smb2_set_next_command(server, &rqst[num_rqst]);
106                 smb2_set_related(&rqst[num_rqst++]);
107                 break;
108         default:
109                 cifs_dbg(VFS, "Invalid command\n");
110                 rc = -EINVAL;
111         }
112         if (rc)
113                 goto finished;
114
115         /* Close */
116         memset(&close_iov, 0, sizeof(close_iov));
117         rqst[num_rqst].rq_iov = close_iov;
118         rqst[num_rqst].rq_nvec = 1;
119         rc = SMB2_close_init(tcon, &rqst[num_rqst], COMPOUND_FID,
120                              COMPOUND_FID);
121         smb2_set_related(&rqst[num_rqst++]);
122         if (rc)
123                 goto finished;
124
125         rc = compound_send_recv(xid, ses, flags, num_rqst, rqst,
126                                 resp_buftype, rsp_iov);
127
128  finished:
129         SMB2_open_free(&rqst[0]);
130         switch (command) {
131         case SMB2_OP_QUERY_INFO:
132                 if (rc == 0) {
133                         rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
134                         rc = smb2_validate_and_copy_iov(
135                                 le16_to_cpu(rsp->OutputBufferOffset),
136                                 le32_to_cpu(rsp->OutputBufferLength),
137                                 &rsp_iov[1], sizeof(struct smb2_file_all_info),
138                                 data);
139                 }
140                 if (rqst[1].rq_iov)
141                         SMB2_query_info_free(&rqst[1]);
142                 if (rqst[2].rq_iov)
143                         SMB2_close_free(&rqst[2]);
144                 break;
145         }
146         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
147         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
148         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
149         return rc;
150 }
151
152 static int
153 smb2_open_op_close(const unsigned int xid, struct cifs_tcon *tcon,
154                    struct cifs_sb_info *cifs_sb, const char *full_path,
155                    __u32 desired_access, __u32 create_disposition,
156                    __u32 create_options, void *data, int command)
157 {
158         int rc, tmprc = 0;
159         __le16 *utf16_path = NULL;
160         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
161         struct cifs_open_parms oparms;
162         struct cifs_fid fid;
163         bool use_cached_root_handle = false;
164
165         if ((strcmp(full_path, "") == 0) && (create_options == 0) &&
166             (desired_access == FILE_READ_ATTRIBUTES) &&
167             (create_disposition == FILE_OPEN) &&
168             (tcon->nohandlecache == false)) {
169                 rc = open_shroot(xid, tcon, &fid);
170                 if (rc == 0)
171                         use_cached_root_handle = true;
172         }
173
174         if (use_cached_root_handle == false) {
175                 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
176                 if (!utf16_path)
177                         return -ENOMEM;
178
179                 oparms.tcon = tcon;
180                 oparms.desired_access = desired_access;
181                 oparms.disposition = create_disposition;
182                 oparms.create_options = create_options;
183                 oparms.fid = &fid;
184                 oparms.reconnect = false;
185
186                 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
187                                NULL);
188                 if (rc) {
189                         kfree(utf16_path);
190                         return rc;
191                 }
192         }
193
194         switch (command) {
195         case SMB2_OP_DELETE:
196                 break;
197         case SMB2_OP_MKDIR:
198                 /*
199                  * Directories are created through parameters in the
200                  * SMB2_open() call.
201                  */
202                 break;
203         case SMB2_OP_RMDIR:
204                 tmprc = SMB2_rmdir(xid, tcon, fid.persistent_fid,
205                                    fid.volatile_fid);
206                 break;
207         case SMB2_OP_RENAME:
208                 tmprc = SMB2_rename(xid, tcon, fid.persistent_fid,
209                                     fid.volatile_fid, (__le16 *)data);
210                 break;
211         case SMB2_OP_HARDLINK:
212                 tmprc = SMB2_set_hardlink(xid, tcon, fid.persistent_fid,
213                                           fid.volatile_fid, (__le16 *)data);
214                 break;
215         case SMB2_OP_SET_EOF:
216                 tmprc = SMB2_set_eof(xid, tcon, fid.persistent_fid,
217                                      fid.volatile_fid, current->tgid,
218                                      (__le64 *)data, false);
219                 break;
220         case SMB2_OP_SET_INFO:
221                 tmprc = SMB2_set_info(xid, tcon, fid.persistent_fid,
222                                       fid.volatile_fid,
223                                       (FILE_BASIC_INFO *)data);
224                 break;
225         default:
226                 cifs_dbg(VFS, "Invalid command\n");
227                 break;
228         }
229
230         if (use_cached_root_handle)
231                 close_shroot(&tcon->crfid);
232         else
233                 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
234         if (tmprc)
235                 rc = tmprc;
236         kfree(utf16_path);
237         return rc;
238 }
239
240 void
241 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
242 {
243         memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
244         dst->CurrentByteOffset = src->CurrentByteOffset;
245         dst->Mode = src->Mode;
246         dst->AlignmentRequirement = src->AlignmentRequirement;
247         dst->IndexNumber1 = 0; /* we don't use it */
248 }
249
250 int
251 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
252                      struct cifs_sb_info *cifs_sb, const char *full_path,
253                      FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink)
254 {
255         int rc;
256         struct smb2_file_all_info *smb2_data;
257
258         *adjust_tz = false;
259         *symlink = false;
260
261         smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
262                             GFP_KERNEL);
263         if (smb2_data == NULL)
264                 return -ENOMEM;
265
266         rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
267                               FILE_READ_ATTRIBUTES, FILE_OPEN, 0,
268                               smb2_data, SMB2_OP_QUERY_INFO);
269         if (rc == -EOPNOTSUPP) {
270                 *symlink = true;
271                 /* Failed on a symbolic link - query a reparse point info */
272                 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
273                                       FILE_READ_ATTRIBUTES, FILE_OPEN,
274                                       OPEN_REPARSE_POINT, smb2_data,
275                                       SMB2_OP_QUERY_INFO);
276         }
277         if (rc)
278                 goto out;
279
280         move_smb2_info_to_cifs(data, smb2_data);
281 out:
282         kfree(smb2_data);
283         return rc;
284 }
285
286 int
287 smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
288            struct cifs_sb_info *cifs_sb)
289 {
290         return smb2_open_op_close(xid, tcon, cifs_sb, name,
291                                   FILE_WRITE_ATTRIBUTES, FILE_CREATE,
292                                   CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR);
293 }
294
295 void
296 smb2_mkdir_setinfo(struct inode *inode, const char *name,
297                    struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
298                    const unsigned int xid)
299 {
300         FILE_BASIC_INFO data;
301         struct cifsInodeInfo *cifs_i;
302         u32 dosattrs;
303         int tmprc;
304
305         memset(&data, 0, sizeof(data));
306         cifs_i = CIFS_I(inode);
307         dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
308         data.Attributes = cpu_to_le32(dosattrs);
309         tmprc = smb2_open_op_close(xid, tcon, cifs_sb, name,
310                                    FILE_WRITE_ATTRIBUTES, FILE_CREATE,
311                                    CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO);
312         if (tmprc == 0)
313                 cifs_i->cifsAttrs = dosattrs;
314 }
315
316 int
317 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
318            struct cifs_sb_info *cifs_sb)
319 {
320         return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
321                                   CREATE_NOT_FILE,
322                                   NULL, SMB2_OP_RMDIR);
323 }
324
325 int
326 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
327             struct cifs_sb_info *cifs_sb)
328 {
329         return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
330                                   CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
331                                   NULL, SMB2_OP_DELETE);
332 }
333
334 static int
335 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
336                    const char *from_name, const char *to_name,
337                    struct cifs_sb_info *cifs_sb, __u32 access, int command)
338 {
339         __le16 *smb2_to_name = NULL;
340         int rc;
341
342         smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
343         if (smb2_to_name == NULL) {
344                 rc = -ENOMEM;
345                 goto smb2_rename_path;
346         }
347
348         rc = smb2_open_op_close(xid, tcon, cifs_sb, from_name, access,
349                                 FILE_OPEN, 0, smb2_to_name, command);
350 smb2_rename_path:
351         kfree(smb2_to_name);
352         return rc;
353 }
354
355 int
356 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
357                  const char *from_name, const char *to_name,
358                  struct cifs_sb_info *cifs_sb)
359 {
360         return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
361                                   DELETE, SMB2_OP_RENAME);
362 }
363
364 int
365 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
366                      const char *from_name, const char *to_name,
367                      struct cifs_sb_info *cifs_sb)
368 {
369         return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
370                                   FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK);
371 }
372
373 int
374 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
375                    const char *full_path, __u64 size,
376                    struct cifs_sb_info *cifs_sb, bool set_alloc)
377 {
378         __le64 eof = cpu_to_le64(size);
379         return smb2_open_op_close(xid, tcon, cifs_sb, full_path,
380                                   FILE_WRITE_DATA, FILE_OPEN, 0, &eof,
381                                   SMB2_OP_SET_EOF);
382 }
383
384 int
385 smb2_set_file_info(struct inode *inode, const char *full_path,
386                    FILE_BASIC_INFO *buf, const unsigned int xid)
387 {
388         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
389         struct tcon_link *tlink;
390         int rc;
391
392         if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
393             (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
394             (buf->Attributes == 0))
395                 return 0; /* would be a no op, no sense sending this */
396
397         tlink = cifs_sb_tlink(cifs_sb);
398         if (IS_ERR(tlink))
399                 return PTR_ERR(tlink);
400
401         rc = smb2_open_op_close(xid, tlink_tcon(tlink), cifs_sb, full_path,
402                                 FILE_WRITE_ATTRIBUTES, FILE_OPEN, 0, buf,
403                                 SMB2_OP_SET_INFO);
404         cifs_put_tlink(tlink);
405         return rc;
406 }