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