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