Mention branches and keyring.
[releases.git] / smb / client / smb2inode.c
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
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  */
10 #include <linux/fs.h>
11 #include <linux/stat.h>
12 #include <linux/slab.h>
13 #include <linux/pagemap.h>
14 #include <asm/div64.h>
15 #include "cifsfs.h"
16 #include "cifspdu.h"
17 #include "cifsglob.h"
18 #include "cifsproto.h"
19 #include "cifs_debug.h"
20 #include "cifs_fs_sb.h"
21 #include "cifs_unicode.h"
22 #include "fscache.h"
23 #include "smb2glob.h"
24 #include "smb2pdu.h"
25 #include "smb2proto.h"
26 #include "cached_dir.h"
27 #include "smb2status.h"
28
29 static void
30 free_set_inf_compound(struct smb_rqst *rqst)
31 {
32         if (rqst[1].rq_iov)
33                 SMB2_set_info_free(&rqst[1]);
34         if (rqst[2].rq_iov)
35                 SMB2_close_free(&rqst[2]);
36 }
37
38
39 struct cop_vars {
40         struct cifs_open_parms oparms;
41         struct kvec rsp_iov[3];
42         struct smb_rqst rqst[3];
43         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
44         struct kvec qi_iov[1];
45         struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
46         struct kvec close_iov[1];
47         struct smb2_file_rename_info rename_info;
48         struct smb2_file_link_info link_info;
49 };
50
51 /*
52  * note: If cfile is passed, the reference to it is dropped here.
53  * So make sure that you do not reuse cfile after return from this func.
54  *
55  * If passing @err_iov and @err_buftype, ensure to make them both large enough (>= 3) to hold all
56  * error responses.  Caller is also responsible for freeing them up.
57  */
58 static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
59                             struct cifs_sb_info *cifs_sb, const char *full_path,
60                             __u32 desired_access, __u32 create_disposition, __u32 create_options,
61                             umode_t mode, void *ptr, int command, struct cifsFileInfo *cfile,
62                             struct kvec *err_iov, int *err_buftype)
63 {
64         struct cop_vars *vars = NULL;
65         struct kvec *rsp_iov;
66         struct smb_rqst *rqst;
67         int rc;
68         __le16 *utf16_path = NULL;
69         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
70         struct cifs_fid fid;
71         struct cifs_ses *ses = tcon->ses;
72         struct TCP_Server_Info *server;
73         int num_rqst = 0;
74         int resp_buftype[3];
75         struct smb2_query_info_rsp *qi_rsp = NULL;
76         struct cifs_open_info_data *idata;
77         int flags = 0;
78         __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
79         unsigned int size[2];
80         void *data[2];
81         int len;
82
83         vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
84         if (vars == NULL)
85                 return -ENOMEM;
86         rqst = &vars->rqst[0];
87         rsp_iov = &vars->rsp_iov[0];
88
89         server = cifs_pick_channel(ses);
90
91         if (smb3_encryption_required(tcon))
92                 flags |= CIFS_TRANSFORM_REQ;
93
94         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
95
96         /* We already have a handle so we can skip the open */
97         if (cfile)
98                 goto after_open;
99
100         /* Open */
101         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
102         if (!utf16_path) {
103                 rc = -ENOMEM;
104                 goto finished;
105         }
106
107         vars->oparms = (struct cifs_open_parms) {
108                 .tcon = tcon,
109                 .path = full_path,
110                 .desired_access = desired_access,
111                 .disposition = create_disposition,
112                 .create_options = cifs_create_options(cifs_sb, create_options),
113                 .fid = &fid,
114                 .mode = mode,
115                 .cifs_sb = cifs_sb,
116         };
117
118         rqst[num_rqst].rq_iov = &vars->open_iov[0];
119         rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
120         rc = SMB2_open_init(tcon, server,
121                             &rqst[num_rqst], &oplock, &vars->oparms,
122                             utf16_path);
123         kfree(utf16_path);
124         if (rc)
125                 goto finished;
126
127         smb2_set_next_command(tcon, &rqst[num_rqst]);
128  after_open:
129         num_rqst++;
130         rc = 0;
131
132         /* Operation */
133         switch (command) {
134         case SMB2_OP_QUERY_INFO:
135                 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
136                 rqst[num_rqst].rq_nvec = 1;
137
138                 if (cfile)
139                         rc = SMB2_query_info_init(tcon, server,
140                                 &rqst[num_rqst],
141                                 cfile->fid.persistent_fid,
142                                 cfile->fid.volatile_fid,
143                                 FILE_ALL_INFORMATION,
144                                 SMB2_O_INFO_FILE, 0,
145                                 sizeof(struct smb2_file_all_info) +
146                                           PATH_MAX * 2, 0, NULL);
147                 else {
148                         rc = SMB2_query_info_init(tcon, server,
149                                 &rqst[num_rqst],
150                                 COMPOUND_FID,
151                                 COMPOUND_FID,
152                                 FILE_ALL_INFORMATION,
153                                 SMB2_O_INFO_FILE, 0,
154                                 sizeof(struct smb2_file_all_info) +
155                                           PATH_MAX * 2, 0, NULL);
156                         if (!rc) {
157                                 smb2_set_next_command(tcon, &rqst[num_rqst]);
158                                 smb2_set_related(&rqst[num_rqst]);
159                         }
160                 }
161
162                 if (rc)
163                         goto finished;
164                 num_rqst++;
165                 trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
166                                                      full_path);
167                 break;
168         case SMB2_OP_POSIX_QUERY_INFO:
169                 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
170                 rqst[num_rqst].rq_nvec = 1;
171
172                 if (cfile)
173                         rc = SMB2_query_info_init(tcon, server,
174                                 &rqst[num_rqst],
175                                 cfile->fid.persistent_fid,
176                                 cfile->fid.volatile_fid,
177                                 SMB_FIND_FILE_POSIX_INFO,
178                                 SMB2_O_INFO_FILE, 0,
179                                 /* TBD: fix following to allow for longer SIDs */
180                                 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
181                                 (sizeof(struct cifs_sid) * 2), 0, NULL);
182                 else {
183                         rc = SMB2_query_info_init(tcon, server,
184                                 &rqst[num_rqst],
185                                 COMPOUND_FID,
186                                 COMPOUND_FID,
187                                 SMB_FIND_FILE_POSIX_INFO,
188                                 SMB2_O_INFO_FILE, 0,
189                                 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
190                                 (sizeof(struct cifs_sid) * 2), 0, NULL);
191                         if (!rc) {
192                                 smb2_set_next_command(tcon, &rqst[num_rqst]);
193                                 smb2_set_related(&rqst[num_rqst]);
194                         }
195                 }
196
197                 if (rc)
198                         goto finished;
199                 num_rqst++;
200                 trace_smb3_posix_query_info_compound_enter(xid, ses->Suid, tcon->tid, full_path);
201                 break;
202         case SMB2_OP_DELETE:
203                 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
204                 break;
205         case SMB2_OP_MKDIR:
206                 /*
207                  * Directories are created through parameters in the
208                  * SMB2_open() call.
209                  */
210                 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
211                 break;
212         case SMB2_OP_RMDIR:
213                 rqst[num_rqst].rq_iov = &vars->si_iov[0];
214                 rqst[num_rqst].rq_nvec = 1;
215
216                 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
217                 data[0] = &delete_pending[0];
218
219                 rc = SMB2_set_info_init(tcon, server,
220                                         &rqst[num_rqst], COMPOUND_FID,
221                                         COMPOUND_FID, current->tgid,
222                                         FILE_DISPOSITION_INFORMATION,
223                                         SMB2_O_INFO_FILE, 0, data, size);
224                 if (rc)
225                         goto finished;
226                 smb2_set_next_command(tcon, &rqst[num_rqst]);
227                 smb2_set_related(&rqst[num_rqst++]);
228                 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
229                 break;
230         case SMB2_OP_SET_EOF:
231                 rqst[num_rqst].rq_iov = &vars->si_iov[0];
232                 rqst[num_rqst].rq_nvec = 1;
233
234                 size[0] = 8; /* sizeof __le64 */
235                 data[0] = ptr;
236
237                 if (cfile) {
238                         rc = SMB2_set_info_init(tcon, server,
239                                                 &rqst[num_rqst],
240                                                 cfile->fid.persistent_fid,
241                                                 cfile->fid.volatile_fid,
242                                                 current->tgid,
243                                                 FILE_END_OF_FILE_INFORMATION,
244                                                 SMB2_O_INFO_FILE, 0,
245                                                 data, size);
246                 } else {
247                         rc = SMB2_set_info_init(tcon, server,
248                                                 &rqst[num_rqst],
249                                                 COMPOUND_FID,
250                                                 COMPOUND_FID,
251                                                 current->tgid,
252                                                 FILE_END_OF_FILE_INFORMATION,
253                                                 SMB2_O_INFO_FILE, 0,
254                                                 data, size);
255                         if (!rc) {
256                                 smb2_set_next_command(tcon, &rqst[num_rqst]);
257                                 smb2_set_related(&rqst[num_rqst]);
258                         }
259                 }
260                 if (rc)
261                         goto finished;
262                 num_rqst++;
263                 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
264                 break;
265         case SMB2_OP_SET_INFO:
266                 rqst[num_rqst].rq_iov = &vars->si_iov[0];
267                 rqst[num_rqst].rq_nvec = 1;
268
269
270                 size[0] = sizeof(FILE_BASIC_INFO);
271                 data[0] = ptr;
272
273                 if (cfile)
274                         rc = SMB2_set_info_init(tcon, server,
275                                 &rqst[num_rqst],
276                                 cfile->fid.persistent_fid,
277                                 cfile->fid.volatile_fid, current->tgid,
278                                 FILE_BASIC_INFORMATION,
279                                 SMB2_O_INFO_FILE, 0, data, size);
280                 else {
281                         rc = SMB2_set_info_init(tcon, server,
282                                 &rqst[num_rqst],
283                                 COMPOUND_FID,
284                                 COMPOUND_FID, current->tgid,
285                                 FILE_BASIC_INFORMATION,
286                                 SMB2_O_INFO_FILE, 0, data, size);
287                         if (!rc) {
288                                 smb2_set_next_command(tcon, &rqst[num_rqst]);
289                                 smb2_set_related(&rqst[num_rqst]);
290                         }
291                 }
292
293                 if (rc)
294                         goto finished;
295                 num_rqst++;
296                 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
297                                                    full_path);
298                 break;
299         case SMB2_OP_RENAME:
300                 rqst[num_rqst].rq_iov = &vars->si_iov[0];
301                 rqst[num_rqst].rq_nvec = 2;
302
303                 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
304
305                 vars->rename_info.ReplaceIfExists = 1;
306                 vars->rename_info.RootDirectory = 0;
307                 vars->rename_info.FileNameLength = cpu_to_le32(len);
308
309                 size[0] = sizeof(struct smb2_file_rename_info);
310                 data[0] = &vars->rename_info;
311
312                 size[1] = len + 2 /* null */;
313                 data[1] = (__le16 *)ptr;
314
315                 if (cfile)
316                         rc = SMB2_set_info_init(tcon, server,
317                                                 &rqst[num_rqst],
318                                                 cfile->fid.persistent_fid,
319                                                 cfile->fid.volatile_fid,
320                                         current->tgid, FILE_RENAME_INFORMATION,
321                                         SMB2_O_INFO_FILE, 0, data, size);
322                 else {
323                         rc = SMB2_set_info_init(tcon, server,
324                                         &rqst[num_rqst],
325                                         COMPOUND_FID, COMPOUND_FID,
326                                         current->tgid, FILE_RENAME_INFORMATION,
327                                         SMB2_O_INFO_FILE, 0, data, size);
328                         if (!rc) {
329                                 smb2_set_next_command(tcon, &rqst[num_rqst]);
330                                 smb2_set_related(&rqst[num_rqst]);
331                         }
332                 }
333                 if (rc)
334                         goto finished;
335                 num_rqst++;
336                 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
337                 break;
338         case SMB2_OP_HARDLINK:
339                 rqst[num_rqst].rq_iov = &vars->si_iov[0];
340                 rqst[num_rqst].rq_nvec = 2;
341
342                 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
343
344                 vars->link_info.ReplaceIfExists = 0;
345                 vars->link_info.RootDirectory = 0;
346                 vars->link_info.FileNameLength = cpu_to_le32(len);
347
348                 size[0] = sizeof(struct smb2_file_link_info);
349                 data[0] = &vars->link_info;
350
351                 size[1] = len + 2 /* null */;
352                 data[1] = (__le16 *)ptr;
353
354                 rc = SMB2_set_info_init(tcon, server,
355                                         &rqst[num_rqst], COMPOUND_FID,
356                                         COMPOUND_FID, current->tgid,
357                                         FILE_LINK_INFORMATION,
358                                         SMB2_O_INFO_FILE, 0, data, size);
359                 if (rc)
360                         goto finished;
361                 smb2_set_next_command(tcon, &rqst[num_rqst]);
362                 smb2_set_related(&rqst[num_rqst++]);
363                 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
364                 break;
365         default:
366                 cifs_dbg(VFS, "Invalid command\n");
367                 rc = -EINVAL;
368         }
369         if (rc)
370                 goto finished;
371
372         /* We already have a handle so we can skip the close */
373         if (cfile)
374                 goto after_close;
375         /* Close */
376         flags |= CIFS_CP_CREATE_CLOSE_OP;
377         rqst[num_rqst].rq_iov = &vars->close_iov[0];
378         rqst[num_rqst].rq_nvec = 1;
379         rc = SMB2_close_init(tcon, server,
380                              &rqst[num_rqst], COMPOUND_FID,
381                              COMPOUND_FID, false);
382         smb2_set_related(&rqst[num_rqst]);
383         if (rc)
384                 goto finished;
385  after_close:
386         num_rqst++;
387
388         if (cfile) {
389                 rc = compound_send_recv(xid, ses, server,
390                                         flags, num_rqst - 2,
391                                         &rqst[1], &resp_buftype[1],
392                                         &rsp_iov[1]);
393         } else
394                 rc = compound_send_recv(xid, ses, server,
395                                         flags, num_rqst,
396                                         rqst, resp_buftype,
397                                         rsp_iov);
398
399  finished:
400         SMB2_open_free(&rqst[0]);
401         if (rc == -EREMCHG) {
402                 pr_warn_once("server share %s deleted\n", tcon->tree_name);
403                 tcon->need_reconnect = true;
404         }
405
406         switch (command) {
407         case SMB2_OP_QUERY_INFO:
408                 idata = ptr;
409                 if (rc == 0 && cfile && cfile->symlink_target) {
410                         idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
411                         if (!idata->symlink_target)
412                                 rc = -ENOMEM;
413                 }
414                 if (rc == 0) {
415                         qi_rsp = (struct smb2_query_info_rsp *)
416                                 rsp_iov[1].iov_base;
417                         rc = smb2_validate_and_copy_iov(
418                                 le16_to_cpu(qi_rsp->OutputBufferOffset),
419                                 le32_to_cpu(qi_rsp->OutputBufferLength),
420                                 &rsp_iov[1], sizeof(idata->fi), (char *)&idata->fi);
421                 }
422                 if (rqst[1].rq_iov)
423                         SMB2_query_info_free(&rqst[1]);
424                 if (rqst[2].rq_iov)
425                         SMB2_close_free(&rqst[2]);
426                 if (rc)
427                         trace_smb3_query_info_compound_err(xid,  ses->Suid,
428                                                 tcon->tid, rc);
429                 else
430                         trace_smb3_query_info_compound_done(xid, ses->Suid,
431                                                 tcon->tid);
432                 break;
433         case SMB2_OP_POSIX_QUERY_INFO:
434                 idata = ptr;
435                 if (rc == 0 && cfile && cfile->symlink_target) {
436                         idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
437                         if (!idata->symlink_target)
438                                 rc = -ENOMEM;
439                 }
440                 if (rc == 0) {
441                         qi_rsp = (struct smb2_query_info_rsp *)
442                                 rsp_iov[1].iov_base;
443                         rc = smb2_validate_and_copy_iov(
444                                 le16_to_cpu(qi_rsp->OutputBufferOffset),
445                                 le32_to_cpu(qi_rsp->OutputBufferLength),
446                                 &rsp_iov[1], sizeof(idata->posix_fi) /* add SIDs */,
447                                 (char *)&idata->posix_fi);
448                 }
449                 if (rqst[1].rq_iov)
450                         SMB2_query_info_free(&rqst[1]);
451                 if (rqst[2].rq_iov)
452                         SMB2_close_free(&rqst[2]);
453                 if (rc)
454                         trace_smb3_posix_query_info_compound_err(xid,  ses->Suid, tcon->tid, rc);
455                 else
456                         trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
457                 break;
458         case SMB2_OP_DELETE:
459                 if (rc)
460                         trace_smb3_delete_err(xid,  ses->Suid, tcon->tid, rc);
461                 else
462                         trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
463                 if (rqst[1].rq_iov)
464                         SMB2_close_free(&rqst[1]);
465                 break;
466         case SMB2_OP_MKDIR:
467                 if (rc)
468                         trace_smb3_mkdir_err(xid,  ses->Suid, tcon->tid, rc);
469                 else
470                         trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
471                 if (rqst[1].rq_iov)
472                         SMB2_close_free(&rqst[1]);
473                 break;
474         case SMB2_OP_HARDLINK:
475                 if (rc)
476                         trace_smb3_hardlink_err(xid,  ses->Suid, tcon->tid, rc);
477                 else
478                         trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
479                 free_set_inf_compound(rqst);
480                 break;
481         case SMB2_OP_RENAME:
482                 if (rc)
483                         trace_smb3_rename_err(xid,  ses->Suid, tcon->tid, rc);
484                 else
485                         trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
486                 free_set_inf_compound(rqst);
487                 break;
488         case SMB2_OP_RMDIR:
489                 if (rc)
490                         trace_smb3_rmdir_err(xid,  ses->Suid, tcon->tid, rc);
491                 else
492                         trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
493                 free_set_inf_compound(rqst);
494                 break;
495         case SMB2_OP_SET_EOF:
496                 if (rc)
497                         trace_smb3_set_eof_err(xid,  ses->Suid, tcon->tid, rc);
498                 else
499                         trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
500                 free_set_inf_compound(rqst);
501                 break;
502         case SMB2_OP_SET_INFO:
503                 if (rc)
504                         trace_smb3_set_info_compound_err(xid,  ses->Suid,
505                                                 tcon->tid, rc);
506                 else
507                         trace_smb3_set_info_compound_done(xid, ses->Suid,
508                                                 tcon->tid);
509                 free_set_inf_compound(rqst);
510                 break;
511         }
512
513         if (cfile)
514                 cifsFileInfo_put(cfile);
515
516         if (rc && err_iov && err_buftype) {
517                 memcpy(err_iov, rsp_iov, 3 * sizeof(*err_iov));
518                 memcpy(err_buftype, resp_buftype, 3 * sizeof(*err_buftype));
519         } else {
520                 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
521                 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
522                 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
523         }
524         kfree(vars);
525         return rc;
526 }
527
528 int smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
529                          struct cifs_sb_info *cifs_sb, const char *full_path,
530                          struct cifs_open_info_data *data, bool *adjust_tz, bool *reparse)
531 {
532         __u32 create_options = 0;
533         struct cifsFileInfo *cfile;
534         struct cached_fid *cfid = NULL;
535         struct kvec err_iov[3] = {};
536         int err_buftype[3] = {};
537         bool islink;
538         int rc, rc2;
539
540         *adjust_tz = false;
541         *reparse = false;
542
543         if (strcmp(full_path, ""))
544                 rc = -ENOENT;
545         else
546                 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
547         /* If it is a root and its handle is cached then use it */
548         if (!rc) {
549                 if (cfid->file_all_info_is_valid) {
550                         memcpy(&data->fi, &cfid->file_all_info, sizeof(data->fi));
551                 } else {
552                         rc = SMB2_query_info(xid, tcon, cfid->fid.persistent_fid,
553                                              cfid->fid.volatile_fid, &data->fi);
554                 }
555                 close_cached_dir(cfid);
556                 return rc;
557         }
558
559         cifs_get_readable_path(tcon, full_path, &cfile);
560         rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES, FILE_OPEN,
561                               create_options, ACL_NO_MODE, data, SMB2_OP_QUERY_INFO, cfile,
562                               err_iov, err_buftype);
563         if (rc) {
564                 struct smb2_hdr *hdr = err_iov[0].iov_base;
565
566                 if (unlikely(!hdr || err_buftype[0] == CIFS_NO_BUFFER))
567                         goto out;
568                 if (rc == -EOPNOTSUPP && hdr->Command == SMB2_CREATE &&
569                     hdr->Status == STATUS_STOPPED_ON_SYMLINK) {
570                         rc = smb2_parse_symlink_response(cifs_sb, err_iov,
571                                                          &data->symlink_target);
572                         if (rc)
573                                 goto out;
574
575                         *reparse = true;
576                         create_options |= OPEN_REPARSE_POINT;
577
578                         /* Failed on a symbolic link - query a reparse point info */
579                         cifs_get_readable_path(tcon, full_path, &cfile);
580                         rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
581                                               FILE_READ_ATTRIBUTES, FILE_OPEN,
582                                               create_options, ACL_NO_MODE, data,
583                                               SMB2_OP_QUERY_INFO, cfile, NULL, NULL);
584                         goto out;
585                 } else if (rc != -EREMOTE && hdr->Status == STATUS_OBJECT_NAME_INVALID) {
586                         rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb,
587                                                              full_path, &islink);
588                         if (rc2) {
589                                 rc = rc2;
590                                 goto out;
591                         }
592                         if (islink)
593                                 rc = -EREMOTE;
594                 }
595         }
596
597 out:
598         free_rsp_buf(err_buftype[0], err_iov[0].iov_base);
599         free_rsp_buf(err_buftype[1], err_iov[1].iov_base);
600         free_rsp_buf(err_buftype[2], err_iov[2].iov_base);
601         return rc;
602 }
603
604
605 int smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
606                                  struct cifs_sb_info *cifs_sb, const char *full_path,
607                                  struct cifs_open_info_data *data, bool *adjust_tz, bool *reparse)
608 {
609         int rc;
610         __u32 create_options = 0;
611         struct cifsFileInfo *cfile;
612         struct kvec err_iov[3] = {};
613         int err_buftype[3] = {};
614
615         *adjust_tz = false;
616         *reparse = false;
617
618         /*
619          * BB TODO: Add support for using the cached root handle.
620          * Create SMB2_query_posix_info worker function to do non-compounded query
621          * when we already have an open file handle for this. For now this is fast enough
622          * (always using the compounded version).
623          */
624
625         cifs_get_readable_path(tcon, full_path, &cfile);
626         rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES, FILE_OPEN,
627                               create_options, ACL_NO_MODE, data, SMB2_OP_POSIX_QUERY_INFO, cfile,
628                               err_iov, err_buftype);
629         if (rc == -EOPNOTSUPP) {
630                 /* BB TODO: When support for special files added to Samba re-verify this path */
631                 if (err_iov[0].iov_base && err_buftype[0] != CIFS_NO_BUFFER &&
632                     ((struct smb2_hdr *)err_iov[0].iov_base)->Command == SMB2_CREATE &&
633                     ((struct smb2_hdr *)err_iov[0].iov_base)->Status == STATUS_STOPPED_ON_SYMLINK) {
634                         rc = smb2_parse_symlink_response(cifs_sb, err_iov, &data->symlink_target);
635                         if (rc)
636                                 goto out;
637                 }
638                 *reparse = true;
639                 create_options |= OPEN_REPARSE_POINT;
640
641                 /* Failed on a symbolic link - query a reparse point info */
642                 cifs_get_readable_path(tcon, full_path, &cfile);
643                 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES,
644                                       FILE_OPEN, create_options, ACL_NO_MODE, data,
645                                       SMB2_OP_POSIX_QUERY_INFO, cfile, NULL, NULL);
646         }
647
648 out:
649         free_rsp_buf(err_buftype[0], err_iov[0].iov_base);
650         free_rsp_buf(err_buftype[1], err_iov[1].iov_base);
651         free_rsp_buf(err_buftype[2], err_iov[2].iov_base);
652         return rc;
653 }
654
655 int
656 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
657            struct cifs_tcon *tcon, const char *name,
658            struct cifs_sb_info *cifs_sb)
659 {
660         return smb2_compound_op(xid, tcon, cifs_sb, name,
661                                 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
662                                 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
663                                 NULL, NULL, NULL);
664 }
665
666 void
667 smb2_mkdir_setinfo(struct inode *inode, const char *name,
668                    struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
669                    const unsigned int xid)
670 {
671         FILE_BASIC_INFO data;
672         struct cifsInodeInfo *cifs_i;
673         struct cifsFileInfo *cfile;
674         u32 dosattrs;
675         int tmprc;
676
677         memset(&data, 0, sizeof(data));
678         cifs_i = CIFS_I(inode);
679         dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
680         data.Attributes = cpu_to_le32(dosattrs);
681         cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
682         tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
683                                  FILE_WRITE_ATTRIBUTES, FILE_CREATE,
684                                  CREATE_NOT_FILE, ACL_NO_MODE,
685                                  &data, SMB2_OP_SET_INFO, cfile, NULL, NULL);
686         if (tmprc == 0)
687                 cifs_i->cifsAttrs = dosattrs;
688 }
689
690 int
691 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
692            struct cifs_sb_info *cifs_sb)
693 {
694         drop_cached_dir_by_name(xid, tcon, name, cifs_sb);
695         return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
696                                 CREATE_NOT_FILE, ACL_NO_MODE,
697                                 NULL, SMB2_OP_RMDIR, NULL, NULL, NULL);
698 }
699
700 int
701 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
702             struct cifs_sb_info *cifs_sb)
703 {
704         return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
705                                 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
706                                 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL, NULL, NULL);
707 }
708
709 static int
710 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
711                    const char *from_name, const char *to_name,
712                    struct cifs_sb_info *cifs_sb, __u32 access, int command,
713                    struct cifsFileInfo *cfile)
714 {
715         __le16 *smb2_to_name = NULL;
716         int rc;
717
718         smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
719         if (smb2_to_name == NULL) {
720                 rc = -ENOMEM;
721                 goto smb2_rename_path;
722         }
723         rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
724                               FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
725                               command, cfile, NULL, NULL);
726 smb2_rename_path:
727         kfree(smb2_to_name);
728         return rc;
729 }
730
731 int
732 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
733                  const char *from_name, const char *to_name,
734                  struct cifs_sb_info *cifs_sb)
735 {
736         struct cifsFileInfo *cfile;
737
738         drop_cached_dir_by_name(xid, tcon, from_name, cifs_sb);
739         cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
740
741         return smb2_set_path_attr(xid, tcon, from_name, to_name,
742                                   cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
743 }
744
745 int
746 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
747                      const char *from_name, const char *to_name,
748                      struct cifs_sb_info *cifs_sb)
749 {
750         return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
751                                   FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
752                                   NULL);
753 }
754
755 int
756 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
757                    const char *full_path, __u64 size,
758                    struct cifs_sb_info *cifs_sb, bool set_alloc)
759 {
760         __le64 eof = cpu_to_le64(size);
761         struct cifsFileInfo *cfile;
762
763         cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
764         return smb2_compound_op(xid, tcon, cifs_sb, full_path,
765                                 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
766                                 &eof, SMB2_OP_SET_EOF, cfile, NULL, NULL);
767 }
768
769 int
770 smb2_set_file_info(struct inode *inode, const char *full_path,
771                    FILE_BASIC_INFO *buf, const unsigned int xid)
772 {
773         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
774         struct tcon_link *tlink;
775         struct cifs_tcon *tcon;
776         struct cifsFileInfo *cfile;
777         int rc;
778
779         if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
780             (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
781             (buf->Attributes == 0))
782                 return 0; /* would be a no op, no sense sending this */
783
784         tlink = cifs_sb_tlink(cifs_sb);
785         if (IS_ERR(tlink))
786                 return PTR_ERR(tlink);
787         tcon = tlink_tcon(tlink);
788
789         cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
790         rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
791                               FILE_WRITE_ATTRIBUTES, FILE_OPEN,
792                               0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, cfile,
793                               NULL, NULL);
794         cifs_put_tlink(tlink);
795         return rc;
796 }