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