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 "cifsglob.h"
17 #include "cifsproto.h"
18 #include "cifs_debug.h"
19 #include "cifs_fs_sb.h"
20 #include "cifs_unicode.h"
21 #include "fscache.h"
22 #include "smb2glob.h"
23 #include "smb2proto.h"
24 #include "cached_dir.h"
25 #include "../common/smb2status.h"
26 #include "../common/smbfsctl.h"
27
reparse_buf_ptr(struct kvec * iov)28 static struct reparse_data_buffer *reparse_buf_ptr(struct kvec *iov)
29 {
30 struct reparse_data_buffer *buf;
31 struct smb2_ioctl_rsp *io = iov->iov_base;
32 u32 off, count, len;
33 u16 rdlen;
34
35 count = le32_to_cpu(io->OutputCount);
36 off = le32_to_cpu(io->OutputOffset);
37 if (check_add_overflow(off, count, &len) || len > iov->iov_len)
38 return ERR_PTR(smb_EIO2(smb_eio_trace_reparse_overlong,
39 off, count));
40
41 buf = (struct reparse_data_buffer *)((u8 *)io + off);
42 len = sizeof(*buf);
43 rdlen = le16_to_cpu(buf->ReparseDataLength);
44
45 if (count < len || count < rdlen + len)
46 return ERR_PTR(smb_EIO2(smb_eio_trace_reparse_rdlen, count, rdlen));
47 return buf;
48 }
49
file_create_options(struct dentry * dentry)50 static inline __u32 file_create_options(struct dentry *dentry)
51 {
52 struct cifsInodeInfo *ci;
53
54 if (dentry) {
55 ci = CIFS_I(d_inode(dentry));
56 if (ci->cifsAttrs & ATTR_REPARSE_POINT)
57 return OPEN_REPARSE_POINT;
58 }
59 return 0;
60 }
61
62 /* Parse owner and group from SMB3.1.1 POSIX query info */
parse_posix_sids(struct cifs_open_info_data * data,struct kvec * rsp_iov)63 static int parse_posix_sids(struct cifs_open_info_data *data,
64 struct kvec *rsp_iov)
65 {
66 struct smb2_query_info_rsp *qi = rsp_iov->iov_base;
67 unsigned int out_len = le32_to_cpu(qi->OutputBufferLength);
68 unsigned int qi_len = sizeof(data->posix_fi);
69 int owner_len, group_len;
70 u8 *sidsbuf, *sidsbuf_end;
71
72 if (out_len <= qi_len)
73 return -EINVAL;
74
75 sidsbuf = (u8 *)qi + le16_to_cpu(qi->OutputBufferOffset) + qi_len;
76 sidsbuf_end = sidsbuf + out_len - qi_len;
77
78 owner_len = posix_info_sid_size(sidsbuf, sidsbuf_end);
79 if (owner_len == -1)
80 return -EINVAL;
81
82 memcpy(&data->posix_owner, sidsbuf, owner_len);
83 group_len = posix_info_sid_size(sidsbuf + owner_len, sidsbuf_end);
84 if (group_len == -1)
85 return -EINVAL;
86
87 memcpy(&data->posix_group, sidsbuf + owner_len, group_len);
88 return 0;
89 }
90
91 struct wsl_query_ea {
92 __le32 next;
93 __u8 name_len;
94 __u8 name[SMB2_WSL_XATTR_NAME_LEN + 1];
95 } __packed;
96
97 #define NEXT_OFF cpu_to_le32(sizeof(struct wsl_query_ea))
98
99 static const struct wsl_query_ea wsl_query_eas[] = {
100 { .next = NEXT_OFF, .name_len = SMB2_WSL_XATTR_NAME_LEN, .name = SMB2_WSL_XATTR_UID, },
101 { .next = NEXT_OFF, .name_len = SMB2_WSL_XATTR_NAME_LEN, .name = SMB2_WSL_XATTR_GID, },
102 { .next = NEXT_OFF, .name_len = SMB2_WSL_XATTR_NAME_LEN, .name = SMB2_WSL_XATTR_MODE, },
103 { .next = 0, .name_len = SMB2_WSL_XATTR_NAME_LEN, .name = SMB2_WSL_XATTR_DEV, },
104 };
105
check_wsl_eas(struct kvec * rsp_iov)106 static int check_wsl_eas(struct kvec *rsp_iov)
107 {
108 struct smb2_file_full_ea_info *ea;
109 struct smb2_query_info_rsp *rsp = rsp_iov->iov_base;
110 unsigned long addr;
111 u32 outlen, next;
112 u16 vlen;
113 u8 nlen;
114 u8 *end;
115
116 outlen = le32_to_cpu(rsp->OutputBufferLength);
117 if (outlen < SMB2_WSL_MIN_QUERY_EA_RESP_SIZE ||
118 outlen > SMB2_WSL_MAX_QUERY_EA_RESP_SIZE)
119 return -EINVAL;
120
121 ea = (void *)((u8 *)rsp_iov->iov_base +
122 le16_to_cpu(rsp->OutputBufferOffset));
123 end = (u8 *)rsp_iov->iov_base + rsp_iov->iov_len;
124 for (;;) {
125 if ((u8 *)ea > end - sizeof(*ea))
126 return -EINVAL;
127
128 nlen = ea->ea_name_length;
129 vlen = le16_to_cpu(ea->ea_value_length);
130 if (nlen != SMB2_WSL_XATTR_NAME_LEN ||
131 (u8 *)ea + nlen + 1 + vlen > end)
132 return -EINVAL;
133
134 switch (vlen) {
135 case 4:
136 if (strncmp(ea->ea_data, SMB2_WSL_XATTR_UID, nlen) &&
137 strncmp(ea->ea_data, SMB2_WSL_XATTR_GID, nlen) &&
138 strncmp(ea->ea_data, SMB2_WSL_XATTR_MODE, nlen))
139 return -EINVAL;
140 break;
141 case 8:
142 if (strncmp(ea->ea_data, SMB2_WSL_XATTR_DEV, nlen))
143 return -EINVAL;
144 break;
145 case 0:
146 if (!strncmp(ea->ea_data, SMB2_WSL_XATTR_UID, nlen) ||
147 !strncmp(ea->ea_data, SMB2_WSL_XATTR_GID, nlen) ||
148 !strncmp(ea->ea_data, SMB2_WSL_XATTR_MODE, nlen) ||
149 !strncmp(ea->ea_data, SMB2_WSL_XATTR_DEV, nlen))
150 break;
151 fallthrough;
152 default:
153 return -EINVAL;
154 }
155
156 next = le32_to_cpu(ea->next_entry_offset);
157 if (!next)
158 break;
159 if (!IS_ALIGNED(next, 4) ||
160 check_add_overflow((unsigned long)ea, next, &addr))
161 return -EINVAL;
162 ea = (void *)addr;
163 }
164 return 0;
165 }
166
167 /*
168 * note: If cfile is passed, the reference to it is dropped here.
169 * So make sure that you do not reuse cfile after return from this func.
170 *
171 * If passing @out_iov and @out_buftype, ensure to make them both large enough
172 * (>= 3) to hold all compounded responses. Caller is also responsible for
173 * freeing them up with free_rsp_buf().
174 */
smb2_compound_op(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,struct cifs_open_parms * oparms,struct kvec * in_iov,int * cmds,int num_cmds,struct cifsFileInfo * cfile,struct kvec * out_iov,int * out_buftype,struct dentry * dentry)175 static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
176 struct cifs_sb_info *cifs_sb, const char *full_path,
177 struct cifs_open_parms *oparms, struct kvec *in_iov,
178 int *cmds, int num_cmds, struct cifsFileInfo *cfile,
179 struct kvec *out_iov, int *out_buftype, struct dentry *dentry)
180 {
181
182 struct smb2_create_rsp *create_rsp = NULL;
183 struct smb2_query_info_rsp *qi_rsp = NULL;
184 struct smb2_compound_vars *vars = NULL;
185 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
186 struct cifs_open_info_data *idata;
187 struct cifs_ses *ses = tcon->ses;
188 struct reparse_data_buffer *rbuf;
189 struct TCP_Server_Info *server;
190 int resp_buftype[MAX_COMPOUND];
191 int retries = 0, cur_sleep = 0;
192 __u8 delete_pending[8] = {1,};
193 struct kvec *rsp_iov, *iov;
194 struct inode *inode = NULL;
195 __le16 *utf16_path = NULL;
196 struct smb_rqst *rqst;
197 unsigned int size[2];
198 struct cifs_fid fid;
199 int num_rqst = 0, i;
200 unsigned int len;
201 int tmp_rc, rc;
202 int flags = 0;
203 void *data[2];
204
205 replay_again:
206 /* reinitialize for possible replay */
207 flags = 0;
208 oplock = SMB2_OPLOCK_LEVEL_NONE;
209 num_rqst = 0;
210 server = cifs_pick_channel(ses);
211
212 vars = kzalloc_obj(*vars, GFP_ATOMIC);
213 if (vars == NULL) {
214 rc = -ENOMEM;
215 goto out;
216 }
217 rqst = &vars->rqst[0];
218 rsp_iov = &vars->rsp_iov[0];
219
220 if (smb3_encryption_required(tcon))
221 flags |= CIFS_TRANSFORM_REQ;
222
223 for (i = 0; i < ARRAY_SIZE(resp_buftype); i++)
224 resp_buftype[i] = CIFS_NO_BUFFER;
225
226 /* We already have a handle so we can skip the open */
227 if (cfile)
228 goto after_open;
229
230 /* Open */
231 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
232 if (!utf16_path) {
233 rc = -ENOMEM;
234 goto finished;
235 }
236
237 /* if there is an existing lease, reuse it */
238
239 /*
240 * note: files with hardlinks cause unexpected behaviour. As per MS-SMB2,
241 * lease keys are associated with the filepath. We are maintaining lease keys
242 * with the inode on the client. If the file has hardlinks, it is possible
243 * that the lease for a file be reused for an operation on its hardlink or
244 * vice versa.
245 * As a workaround, send request using an existing lease key and if the server
246 * returns STATUS_INVALID_PARAMETER, which maps to EINVAL, send the request
247 * again without the lease.
248 */
249 if (dentry) {
250 inode = d_inode(dentry);
251 if (CIFS_I(inode)->lease_granted && server->ops->get_lease_key) {
252 oplock = SMB2_OPLOCK_LEVEL_LEASE;
253 server->ops->get_lease_key(inode, &fid);
254 }
255 }
256
257 vars->oparms = *oparms;
258 vars->oparms.fid = &fid;
259
260 rqst[num_rqst].rq_iov = &vars->open_iov[0];
261 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
262 rc = SMB2_open_init(tcon, server,
263 &rqst[num_rqst], &oplock, &vars->oparms,
264 utf16_path);
265 kfree(utf16_path);
266 if (rc)
267 goto finished;
268
269 smb2_set_next_command(tcon, &rqst[num_rqst]);
270 after_open:
271 num_rqst++;
272 rc = 0;
273
274 i = 0;
275
276 /* Skip the leading explicit OPEN operation */
277 if (num_cmds > 0 && cmds[0] == SMB2_OP_OPEN_QUERY)
278 i++;
279
280 for (; i < num_cmds; i++) {
281 /* Operation */
282 switch (cmds[i]) {
283 case SMB2_OP_QUERY_INFO:
284 rqst[num_rqst].rq_iov = &vars->qi_iov;
285 rqst[num_rqst].rq_nvec = 1;
286
287 if (cfile) {
288 rc = SMB2_query_info_init(tcon, server,
289 &rqst[num_rqst],
290 cfile->fid.persistent_fid,
291 cfile->fid.volatile_fid,
292 FILE_ALL_INFORMATION,
293 SMB2_O_INFO_FILE, 0,
294 sizeof(struct smb2_file_all_info) +
295 PATH_MAX * 2, 0, NULL);
296 } else {
297 rc = SMB2_query_info_init(tcon, server,
298 &rqst[num_rqst],
299 COMPOUND_FID,
300 COMPOUND_FID,
301 FILE_ALL_INFORMATION,
302 SMB2_O_INFO_FILE, 0,
303 sizeof(struct smb2_file_all_info) +
304 PATH_MAX * 2, 0, NULL);
305 }
306 if (!rc && (!cfile || num_rqst > 1)) {
307 smb2_set_next_command(tcon, &rqst[num_rqst]);
308 smb2_set_related(&rqst[num_rqst]);
309 } else if (rc) {
310 goto finished;
311 }
312 num_rqst++;
313 trace_smb3_query_info_compound_enter(xid, tcon->tid,
314 ses->Suid, full_path);
315 break;
316 case SMB2_OP_POSIX_QUERY_INFO:
317 rqst[num_rqst].rq_iov = &vars->qi_iov;
318 rqst[num_rqst].rq_nvec = 1;
319
320 if (cfile) {
321 /* TBD: fix following to allow for longer SIDs */
322 rc = SMB2_query_info_init(tcon, server,
323 &rqst[num_rqst],
324 cfile->fid.persistent_fid,
325 cfile->fid.volatile_fid,
326 SMB_FIND_FILE_POSIX_INFO,
327 SMB2_O_INFO_FILE, 0,
328 sizeof(struct smb311_posix_qinfo *) +
329 (PATH_MAX * 2) +
330 (sizeof(struct smb_sid) * 2), 0, NULL);
331 } else {
332 rc = SMB2_query_info_init(tcon, server,
333 &rqst[num_rqst],
334 COMPOUND_FID,
335 COMPOUND_FID,
336 SMB_FIND_FILE_POSIX_INFO,
337 SMB2_O_INFO_FILE, 0,
338 sizeof(struct smb311_posix_qinfo *) +
339 (PATH_MAX * 2) +
340 (sizeof(struct smb_sid) * 2), 0, NULL);
341 }
342 if (!rc && (!cfile || num_rqst > 1)) {
343 smb2_set_next_command(tcon, &rqst[num_rqst]);
344 smb2_set_related(&rqst[num_rqst]);
345 } else if (rc) {
346 goto finished;
347 }
348 num_rqst++;
349 trace_smb3_posix_query_info_compound_enter(xid, tcon->tid,
350 ses->Suid, full_path);
351 break;
352 case SMB2_OP_MKDIR:
353 /*
354 * Directories are created through parameters in the
355 * SMB2_open() call.
356 */
357 trace_smb3_mkdir_enter(xid, tcon->tid, ses->Suid, full_path);
358 break;
359 case SMB2_OP_UNLINK:
360 rqst[num_rqst].rq_iov = vars->unlink_iov;
361 rqst[num_rqst].rq_nvec = 1;
362
363 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
364 data[0] = &delete_pending[0];
365
366 if (cfile) {
367 rc = SMB2_set_info_init(tcon, server,
368 &rqst[num_rqst],
369 cfile->fid.persistent_fid,
370 cfile->fid.volatile_fid,
371 current->tgid,
372 FILE_DISPOSITION_INFORMATION,
373 SMB2_O_INFO_FILE, 0,
374 data, size);
375 } else {
376 rc = SMB2_set_info_init(tcon, server,
377 &rqst[num_rqst],
378 COMPOUND_FID,
379 COMPOUND_FID,
380 current->tgid,
381 FILE_DISPOSITION_INFORMATION,
382 SMB2_O_INFO_FILE, 0,
383 data, size);
384 }
385 if (!rc && (!cfile || num_rqst > 1)) {
386 smb2_set_next_command(tcon, &rqst[num_rqst]);
387 smb2_set_related(&rqst[num_rqst]);
388 } else if (rc) {
389 goto finished;
390 }
391 num_rqst++;
392 trace_smb3_unlink_enter(xid, tcon->tid, ses->Suid, full_path);
393 break;
394 case SMB2_OP_SET_EOF:
395 rqst[num_rqst].rq_iov = &vars->si_iov[0];
396 rqst[num_rqst].rq_nvec = 1;
397
398 size[0] = in_iov[i].iov_len;
399 data[0] = in_iov[i].iov_base;
400
401 if (cfile) {
402 rc = SMB2_set_info_init(tcon, server,
403 &rqst[num_rqst],
404 cfile->fid.persistent_fid,
405 cfile->fid.volatile_fid,
406 current->tgid,
407 FILE_END_OF_FILE_INFORMATION,
408 SMB2_O_INFO_FILE, 0,
409 data, size);
410 } else {
411 rc = SMB2_set_info_init(tcon, server,
412 &rqst[num_rqst],
413 COMPOUND_FID,
414 COMPOUND_FID,
415 current->tgid,
416 FILE_END_OF_FILE_INFORMATION,
417 SMB2_O_INFO_FILE, 0,
418 data, size);
419 }
420 if (!rc && (!cfile || num_rqst > 1)) {
421 smb2_set_next_command(tcon, &rqst[num_rqst]);
422 smb2_set_related(&rqst[num_rqst]);
423 } else if (rc) {
424 goto finished;
425 }
426 num_rqst++;
427 trace_smb3_set_eof_enter(xid, tcon->tid, ses->Suid, full_path);
428 break;
429 case SMB2_OP_SET_INFO:
430 rqst[num_rqst].rq_iov = &vars->si_iov[0];
431 rqst[num_rqst].rq_nvec = 1;
432
433 size[0] = in_iov[i].iov_len;
434 data[0] = in_iov[i].iov_base;
435
436 if (cfile) {
437 rc = SMB2_set_info_init(tcon, server,
438 &rqst[num_rqst],
439 cfile->fid.persistent_fid,
440 cfile->fid.volatile_fid, current->tgid,
441 FILE_BASIC_INFORMATION,
442 SMB2_O_INFO_FILE, 0, data, size);
443 } else {
444 rc = SMB2_set_info_init(tcon, server,
445 &rqst[num_rqst],
446 COMPOUND_FID,
447 COMPOUND_FID, current->tgid,
448 FILE_BASIC_INFORMATION,
449 SMB2_O_INFO_FILE, 0, data, size);
450 }
451 if (!rc && (!cfile || num_rqst > 1)) {
452 smb2_set_next_command(tcon, &rqst[num_rqst]);
453 smb2_set_related(&rqst[num_rqst]);
454 } else if (rc) {
455 goto finished;
456 }
457 num_rqst++;
458 trace_smb3_set_info_compound_enter(xid, tcon->tid,
459 ses->Suid, full_path);
460 break;
461 case SMB2_OP_RENAME:
462 rqst[num_rqst].rq_iov = vars->rename_iov;
463 rqst[num_rqst].rq_nvec = 2;
464
465 len = in_iov[i].iov_len;
466
467 vars->rename_info.ReplaceIfExists = 1;
468 vars->rename_info.RootDirectory = 0;
469 vars->rename_info.FileNameLength = cpu_to_le32(len);
470
471 size[0] = sizeof(struct smb2_file_rename_info);
472 data[0] = &vars->rename_info;
473
474 size[1] = len + 2 /* null */;
475 data[1] = in_iov[i].iov_base;
476
477 if (cfile) {
478 rc = SMB2_set_info_init(tcon, server,
479 &rqst[num_rqst],
480 cfile->fid.persistent_fid,
481 cfile->fid.volatile_fid,
482 current->tgid, FILE_RENAME_INFORMATION,
483 SMB2_O_INFO_FILE, 0, data, size);
484 } else {
485 rc = SMB2_set_info_init(tcon, server,
486 &rqst[num_rqst],
487 COMPOUND_FID, COMPOUND_FID,
488 current->tgid, FILE_RENAME_INFORMATION,
489 SMB2_O_INFO_FILE, 0, data, size);
490 }
491 if (!rc && (!cfile || num_rqst > 1)) {
492 smb2_set_next_command(tcon, &rqst[num_rqst]);
493 smb2_set_related(&rqst[num_rqst]);
494 } else if (rc) {
495 goto finished;
496 }
497 num_rqst++;
498 trace_smb3_rename_enter(xid, tcon->tid, ses->Suid, full_path);
499 break;
500 case SMB2_OP_HARDLINK:
501 rqst[num_rqst].rq_iov = &vars->si_iov[0];
502 rqst[num_rqst].rq_nvec = 2;
503
504 len = in_iov[i].iov_len;
505
506 vars->link_info.ReplaceIfExists = 0;
507 vars->link_info.RootDirectory = 0;
508 vars->link_info.FileNameLength = cpu_to_le32(len);
509
510 size[0] = sizeof(struct smb2_file_link_info);
511 data[0] = &vars->link_info;
512
513 size[1] = len + 2 /* null */;
514 data[1] = in_iov[i].iov_base;
515
516 rc = SMB2_set_info_init(tcon, server,
517 &rqst[num_rqst], COMPOUND_FID,
518 COMPOUND_FID, current->tgid,
519 FILE_LINK_INFORMATION,
520 SMB2_O_INFO_FILE, 0, data, size);
521 if (rc)
522 goto finished;
523 smb2_set_next_command(tcon, &rqst[num_rqst]);
524 smb2_set_related(&rqst[num_rqst++]);
525 trace_smb3_hardlink_enter(xid, tcon->tid, ses->Suid, full_path);
526 break;
527 case SMB2_OP_SET_REPARSE:
528 rqst[num_rqst].rq_iov = vars->io_iov;
529 rqst[num_rqst].rq_nvec = ARRAY_SIZE(vars->io_iov);
530
531 if (cfile) {
532 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst],
533 cfile->fid.persistent_fid,
534 cfile->fid.volatile_fid,
535 FSCTL_SET_REPARSE_POINT,
536 in_iov[i].iov_base,
537 in_iov[i].iov_len, 0);
538 } else {
539 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst],
540 COMPOUND_FID, COMPOUND_FID,
541 FSCTL_SET_REPARSE_POINT,
542 in_iov[i].iov_base,
543 in_iov[i].iov_len, 0);
544 }
545 if (!rc && (!cfile || num_rqst > 1)) {
546 smb2_set_next_command(tcon, &rqst[num_rqst]);
547 smb2_set_related(&rqst[num_rqst]);
548 } else if (rc) {
549 goto finished;
550 }
551 num_rqst++;
552 trace_smb3_set_reparse_compound_enter(xid, tcon->tid,
553 ses->Suid, full_path);
554 break;
555 case SMB2_OP_GET_REPARSE:
556 rqst[num_rqst].rq_iov = vars->io_iov;
557 rqst[num_rqst].rq_nvec = ARRAY_SIZE(vars->io_iov);
558
559 if (cfile) {
560 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst],
561 cfile->fid.persistent_fid,
562 cfile->fid.volatile_fid,
563 FSCTL_GET_REPARSE_POINT,
564 NULL, 0, CIFSMaxBufSize);
565 } else {
566 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst],
567 COMPOUND_FID, COMPOUND_FID,
568 FSCTL_GET_REPARSE_POINT,
569 NULL, 0, CIFSMaxBufSize);
570 }
571 if (!rc && (!cfile || num_rqst > 1)) {
572 smb2_set_next_command(tcon, &rqst[num_rqst]);
573 smb2_set_related(&rqst[num_rqst]);
574 } else if (rc) {
575 goto finished;
576 }
577 num_rqst++;
578 trace_smb3_get_reparse_compound_enter(xid, tcon->tid,
579 ses->Suid, full_path);
580 break;
581 case SMB2_OP_QUERY_WSL_EA:
582 rqst[num_rqst].rq_iov = &vars->ea_iov;
583 rqst[num_rqst].rq_nvec = 1;
584
585 if (cfile) {
586 rc = SMB2_query_info_init(tcon, server,
587 &rqst[num_rqst],
588 cfile->fid.persistent_fid,
589 cfile->fid.volatile_fid,
590 FILE_FULL_EA_INFORMATION,
591 SMB2_O_INFO_FILE, 0,
592 SMB2_WSL_MAX_QUERY_EA_RESP_SIZE,
593 sizeof(wsl_query_eas),
594 (void *)wsl_query_eas);
595 } else {
596 rc = SMB2_query_info_init(tcon, server,
597 &rqst[num_rqst],
598 COMPOUND_FID,
599 COMPOUND_FID,
600 FILE_FULL_EA_INFORMATION,
601 SMB2_O_INFO_FILE, 0,
602 SMB2_WSL_MAX_QUERY_EA_RESP_SIZE,
603 sizeof(wsl_query_eas),
604 (void *)wsl_query_eas);
605 }
606 if (!rc && (!cfile || num_rqst > 1)) {
607 smb2_set_next_command(tcon, &rqst[num_rqst]);
608 smb2_set_related(&rqst[num_rqst]);
609 } else if (rc) {
610 goto finished;
611 }
612 num_rqst++;
613 trace_smb3_query_wsl_ea_compound_enter(xid, tcon->tid,
614 ses->Suid, full_path);
615 break;
616 default:
617 cifs_dbg(VFS, "Invalid command\n");
618 rc = -EINVAL;
619 }
620 }
621 if (rc)
622 goto finished;
623
624 /* We already have a handle so we can skip the close */
625 if (cfile)
626 goto after_close;
627 /* Close */
628 flags |= CIFS_CP_CREATE_CLOSE_OP;
629 rqst[num_rqst].rq_iov = &vars->close_iov;
630 rqst[num_rqst].rq_nvec = 1;
631 rc = SMB2_close_init(tcon, server,
632 &rqst[num_rqst], COMPOUND_FID,
633 COMPOUND_FID, false);
634 smb2_set_related(&rqst[num_rqst]);
635 if (rc)
636 goto finished;
637 after_close:
638 num_rqst++;
639
640 if (cfile) {
641 if (retries) {
642 /* Back-off before retry */
643 if (cur_sleep)
644 msleep(cur_sleep);
645 for (i = 1; i < num_rqst - 2; i++)
646 smb2_set_replay(server, &rqst[i]);
647 }
648
649 rc = compound_send_recv(xid, ses, server,
650 flags, num_rqst - 2,
651 &rqst[1], &resp_buftype[1],
652 &rsp_iov[1]);
653 } else {
654 if (retries) {
655 /* Back-off before retry */
656 if (cur_sleep)
657 msleep(cur_sleep);
658 for (i = 0; i < num_rqst; i++)
659 smb2_set_replay(server, &rqst[i]);
660 }
661
662 rc = compound_send_recv(xid, ses, server,
663 flags, num_rqst,
664 rqst, resp_buftype,
665 rsp_iov);
666 }
667
668 finished:
669 num_rqst = 0;
670 SMB2_open_free(&rqst[num_rqst++]);
671 if (rc == -EREMCHG) {
672 pr_warn_once("server share %s deleted\n", tcon->tree_name);
673 tcon->need_reconnect = true;
674 }
675
676 tmp_rc = rc;
677
678 if (rc == 0 && num_cmds > 0 && cmds[0] == SMB2_OP_OPEN_QUERY) {
679 create_rsp = rsp_iov[0].iov_base;
680 idata = in_iov[0].iov_base;
681 idata->fi.CreationTime = create_rsp->CreationTime;
682 idata->fi.LastAccessTime = create_rsp->LastAccessTime;
683 idata->fi.LastWriteTime = create_rsp->LastWriteTime;
684 idata->fi.ChangeTime = create_rsp->ChangeTime;
685 idata->fi.Attributes = create_rsp->FileAttributes;
686 idata->fi.AllocationSize = create_rsp->AllocationSize;
687 idata->fi.EndOfFile = create_rsp->EndofFile;
688 if (le32_to_cpu(idata->fi.NumberOfLinks) == 0)
689 idata->fi.NumberOfLinks = cpu_to_le32(1); /* dummy value */
690 idata->fi.DeletePending = 0; /* successful open = not delete pending */
691 idata->fi.Directory = !!(le32_to_cpu(create_rsp->FileAttributes) & ATTR_DIRECTORY);
692
693 /* smb2_parse_contexts() fills idata->fi.IndexNumber */
694 rc = smb2_parse_contexts(server, &rsp_iov[0], &oparms->fid->epoch,
695 oparms->fid->lease_key, &oplock, &idata->fi, NULL);
696 if (rc)
697 cifs_dbg(VFS, "rc: %d parsing context of compound op\n", rc);
698 }
699
700 for (i = 0; i < num_cmds; i++) {
701 char *buf = rsp_iov[i + 1].iov_base;
702
703 if (buf && resp_buftype[i + 1] != CIFS_NO_BUFFER)
704 rc = server->ops->map_error(buf, false);
705 else
706 rc = tmp_rc;
707 switch (cmds[i]) {
708 case SMB2_OP_QUERY_INFO:
709 idata = in_iov[i].iov_base;
710 idata->contains_posix_file_info = false;
711 if (rc == 0 && cfile && cfile->symlink_target) {
712 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
713 if (!idata->symlink_target)
714 rc = -ENOMEM;
715 }
716 if (rc == 0) {
717 qi_rsp = (struct smb2_query_info_rsp *)
718 rsp_iov[i + 1].iov_base;
719 rc = smb2_validate_and_copy_iov(
720 le16_to_cpu(qi_rsp->OutputBufferOffset),
721 le32_to_cpu(qi_rsp->OutputBufferLength),
722 &rsp_iov[i + 1], sizeof(idata->fi), (char *)&idata->fi);
723 }
724 SMB2_query_info_free(&rqst[num_rqst++]);
725 if (rc)
726 trace_smb3_query_info_compound_err(xid, tcon->tid,
727 ses->Suid, rc);
728 else
729 trace_smb3_query_info_compound_done(xid, tcon->tid,
730 ses->Suid);
731 break;
732 case SMB2_OP_POSIX_QUERY_INFO:
733 idata = in_iov[i].iov_base;
734 idata->contains_posix_file_info = true;
735 if (rc == 0 && cfile && cfile->symlink_target) {
736 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
737 if (!idata->symlink_target)
738 rc = -ENOMEM;
739 }
740 if (rc == 0) {
741 qi_rsp = (struct smb2_query_info_rsp *)
742 rsp_iov[i + 1].iov_base;
743 rc = smb2_validate_and_copy_iov(
744 le16_to_cpu(qi_rsp->OutputBufferOffset),
745 le32_to_cpu(qi_rsp->OutputBufferLength),
746 &rsp_iov[i + 1], sizeof(idata->posix_fi) /* add SIDs */,
747 (char *)&idata->posix_fi);
748 }
749 if (rc == 0)
750 rc = parse_posix_sids(idata, &rsp_iov[i + 1]);
751
752 SMB2_query_info_free(&rqst[num_rqst++]);
753 if (rc)
754 trace_smb3_posix_query_info_compound_err(xid, tcon->tid,
755 ses->Suid, rc);
756 else
757 trace_smb3_posix_query_info_compound_done(xid, tcon->tid,
758 ses->Suid);
759 break;
760 case SMB2_OP_MKDIR:
761 if (rc)
762 trace_smb3_mkdir_err(xid, tcon->tid, ses->Suid, rc);
763 else
764 trace_smb3_mkdir_done(xid, tcon->tid, ses->Suid);
765 break;
766 case SMB2_OP_HARDLINK:
767 if (rc)
768 trace_smb3_hardlink_err(xid, tcon->tid, ses->Suid, rc);
769 else
770 trace_smb3_hardlink_done(xid, tcon->tid, ses->Suid);
771 SMB2_set_info_free(&rqst[num_rqst++]);
772 break;
773 case SMB2_OP_RENAME:
774 if (rc)
775 trace_smb3_rename_err(xid, tcon->tid, ses->Suid, rc);
776 else
777 trace_smb3_rename_done(xid, tcon->tid, ses->Suid);
778 SMB2_set_info_free(&rqst[num_rqst++]);
779 break;
780 case SMB2_OP_UNLINK:
781 if (!rc)
782 trace_smb3_unlink_done(xid, tcon->tid, ses->Suid);
783 else
784 trace_smb3_unlink_err(xid, tcon->tid, ses->Suid, rc);
785 SMB2_set_info_free(&rqst[num_rqst++]);
786 break;
787 case SMB2_OP_SET_EOF:
788 if (rc)
789 trace_smb3_set_eof_err(xid, tcon->tid, ses->Suid, rc);
790 else
791 trace_smb3_set_eof_done(xid, tcon->tid, ses->Suid);
792 SMB2_set_info_free(&rqst[num_rqst++]);
793 break;
794 case SMB2_OP_SET_INFO:
795 if (rc)
796 trace_smb3_set_info_compound_err(xid, tcon->tid,
797 ses->Suid, rc);
798 else
799 trace_smb3_set_info_compound_done(xid, tcon->tid,
800 ses->Suid);
801 SMB2_set_info_free(&rqst[num_rqst++]);
802 break;
803 case SMB2_OP_SET_REPARSE:
804 if (rc) {
805 trace_smb3_set_reparse_compound_err(xid, tcon->tid,
806 ses->Suid, rc);
807 } else {
808 trace_smb3_set_reparse_compound_done(xid, tcon->tid,
809 ses->Suid);
810 }
811 SMB2_ioctl_free(&rqst[num_rqst++]);
812 break;
813 case SMB2_OP_GET_REPARSE:
814 if (!rc) {
815 iov = &rsp_iov[i + 1];
816 idata = in_iov[i].iov_base;
817 idata->reparse.io.iov = *iov;
818 idata->reparse.io.buftype = resp_buftype[i + 1];
819 idata->contains_posix_file_info = false; /* BB VERIFY */
820 rbuf = reparse_buf_ptr(iov);
821 if (IS_ERR(rbuf)) {
822 rc = PTR_ERR(rbuf);
823 trace_smb3_get_reparse_compound_err(xid, tcon->tid,
824 ses->Suid, rc);
825 } else {
826 idata->reparse.tag = le32_to_cpu(rbuf->ReparseTag);
827 trace_smb3_get_reparse_compound_done(xid, tcon->tid,
828 ses->Suid);
829 }
830 memset(iov, 0, sizeof(*iov));
831 resp_buftype[i + 1] = CIFS_NO_BUFFER;
832 } else {
833 trace_smb3_get_reparse_compound_err(xid, tcon->tid,
834 ses->Suid, rc);
835 }
836 SMB2_ioctl_free(&rqst[num_rqst++]);
837 break;
838 case SMB2_OP_QUERY_WSL_EA:
839 if (!rc) {
840 idata = in_iov[i].iov_base;
841 idata->contains_posix_file_info = false;
842 qi_rsp = rsp_iov[i + 1].iov_base;
843 data[0] = (u8 *)qi_rsp + le16_to_cpu(qi_rsp->OutputBufferOffset);
844 size[0] = le32_to_cpu(qi_rsp->OutputBufferLength);
845 rc = check_wsl_eas(&rsp_iov[i + 1]);
846 if (!rc) {
847 memcpy(idata->wsl.eas, data[0], size[0]);
848 idata->wsl.eas_len = size[0];
849 }
850 }
851 if (!rc) {
852 trace_smb3_query_wsl_ea_compound_done(xid, tcon->tid,
853 ses->Suid);
854 } else {
855 trace_smb3_query_wsl_ea_compound_err(xid, tcon->tid,
856 ses->Suid, rc);
857 }
858 SMB2_query_info_free(&rqst[num_rqst++]);
859 break;
860 }
861 }
862 SMB2_close_free(&rqst[num_rqst]);
863 rc = tmp_rc;
864
865 num_cmds += 2;
866 if (out_iov && out_buftype) {
867 memcpy(out_iov, rsp_iov, num_cmds * sizeof(*out_iov));
868 memcpy(out_buftype, resp_buftype,
869 num_cmds * sizeof(*out_buftype));
870 } else {
871 for (i = 0; i < num_cmds; i++)
872 free_rsp_buf(resp_buftype[i], rsp_iov[i].iov_base);
873 }
874 num_cmds -= 2; /* correct num_cmds as there could be a retry */
875 kfree(vars);
876
877 if (is_replayable_error(rc) &&
878 smb2_should_replay(tcon, &retries, &cur_sleep))
879 goto replay_again;
880
881 out:
882 if (cfile)
883 cifsFileInfo_put(cfile);
884
885 return rc;
886 }
887
parse_create_response(struct cifs_open_info_data * data,struct cifs_sb_info * cifs_sb,const char * full_path,const struct kvec * iov)888 static int parse_create_response(struct cifs_open_info_data *data,
889 struct cifs_sb_info *cifs_sb,
890 const char *full_path,
891 const struct kvec *iov)
892 {
893 struct smb2_create_rsp *rsp = iov->iov_base;
894 bool reparse_point = false;
895 u32 tag = 0;
896 int rc = 0;
897
898 switch (rsp->hdr.Status) {
899 case STATUS_IO_REPARSE_TAG_NOT_HANDLED:
900 reparse_point = true;
901 break;
902 case STATUS_STOPPED_ON_SYMLINK:
903 rc = smb2_parse_symlink_response(cifs_sb, iov,
904 full_path,
905 &data->symlink_target);
906 if (rc)
907 return rc;
908 tag = IO_REPARSE_TAG_SYMLINK;
909 reparse_point = true;
910 break;
911 case STATUS_SUCCESS:
912 reparse_point = !!(rsp->Flags & SMB2_CREATE_FLAG_REPARSEPOINT);
913 break;
914 }
915 data->reparse_point = reparse_point;
916 data->reparse.tag = tag;
917 return rc;
918 }
919
920 /* Check only if SMB2_OP_QUERY_WSL_EA command failed in the compound chain */
ea_unsupported(int * cmds,int num_cmds,struct kvec * out_iov,int * out_buftype)921 static bool ea_unsupported(int *cmds, int num_cmds,
922 struct kvec *out_iov, int *out_buftype)
923 {
924 int i;
925
926 if (cmds[num_cmds - 1] != SMB2_OP_QUERY_WSL_EA)
927 return false;
928
929 for (i = 1; i < num_cmds - 1; i++) {
930 struct smb2_hdr *hdr = out_iov[i].iov_base;
931
932 if (out_buftype[i] == CIFS_NO_BUFFER || !hdr ||
933 hdr->Status != STATUS_SUCCESS)
934 return false;
935 }
936 return true;
937 }
938
free_rsp_iov(struct kvec * iovs,int * buftype,int count)939 static inline void free_rsp_iov(struct kvec *iovs, int *buftype, int count)
940 {
941 int i;
942
943 for (i = 0; i < count; i++) {
944 free_rsp_buf(buftype[i], iovs[i].iov_base);
945 memset(&iovs[i], 0, sizeof(*iovs));
946 buftype[i] = CIFS_NO_BUFFER;
947 }
948 }
949
smb2_query_path_info(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,struct cifs_open_info_data * data)950 int smb2_query_path_info(const unsigned int xid,
951 struct cifs_tcon *tcon,
952 struct cifs_sb_info *cifs_sb,
953 const char *full_path,
954 struct cifs_open_info_data *data)
955 {
956 struct kvec in_iov[3], out_iov[5] = {};
957 struct cached_fid *cfid = NULL;
958 struct cifs_open_parms oparms;
959 struct cifsFileInfo *cfile;
960 __u32 create_options = 0;
961 int out_buftype[5] = {};
962 struct smb2_hdr *hdr;
963 int num_cmds = 0;
964 int cmds[3];
965 bool islink;
966 int rc, rc2;
967
968 data->adjust_tz = false;
969 data->reparse_point = false;
970
971 /*
972 * BB TODO: Add support for using cached root handle in SMB3.1.1 POSIX.
973 * Create SMB2_query_posix_info worker function to do non-compounded
974 * query when we already have an open file handle for this. For now this
975 * is fast enough (always using the compounded version).
976 */
977 if (!tcon->posix_extensions) {
978 if (*full_path) {
979 rc = -ENOENT;
980 } else {
981 rc = open_cached_dir(xid, tcon, full_path,
982 cifs_sb, false, &cfid);
983 }
984 /* If it is a root and its handle is cached then use it */
985 if (!rc) {
986 if (cfid->file_all_info_is_valid) {
987 memcpy(&data->fi, &cfid->file_all_info,
988 sizeof(data->fi));
989 } else {
990 rc = SMB2_query_info(xid, tcon,
991 cfid->fid.persistent_fid,
992 cfid->fid.volatile_fid,
993 &data->fi);
994 }
995 close_cached_dir(cfid);
996 return rc;
997 }
998 cmds[num_cmds++] = SMB2_OP_QUERY_INFO;
999 } else {
1000 cmds[num_cmds++] = SMB2_OP_POSIX_QUERY_INFO;
1001 }
1002
1003 in_iov[0].iov_base = data;
1004 in_iov[0].iov_len = sizeof(*data);
1005 in_iov[1] = in_iov[0];
1006 in_iov[2] = in_iov[0];
1007
1008 cifs_get_readable_path(tcon, full_path, &cfile);
1009 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, FILE_READ_ATTRIBUTES,
1010 FILE_OPEN, create_options, ACL_NO_MODE);
1011 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
1012 &oparms, in_iov, cmds, num_cmds,
1013 cfile, out_iov, out_buftype, NULL);
1014 hdr = out_iov[0].iov_base;
1015 /*
1016 * If first iov is unset, then SMB session was dropped or we've got a
1017 * cached open file (@cfile).
1018 */
1019 if (!hdr || out_buftype[0] == CIFS_NO_BUFFER)
1020 goto out;
1021
1022 switch (rc) {
1023 case 0:
1024 rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]);
1025 break;
1026 case -EACCES:
1027 /*
1028 * If SMB2_OP_QUERY_INFO (called when POSIX extensions are not used) failed with
1029 * STATUS_ACCESS_DENIED then it means that caller does not have permission to
1030 * open the path with FILE_READ_ATTRIBUTES access and therefore cannot issue
1031 * SMB2_OP_QUERY_INFO command.
1032 *
1033 * There is an alternative way how to query limited information about path but still
1034 * suitable for stat() syscall. SMB2 OPEN/CREATE operation returns in its successful
1035 * response subset of query information.
1036 *
1037 * So try to open the path without FILE_READ_ATTRIBUTES but with MAXIMUM_ALLOWED
1038 * access which will grant the maximum possible access to the file and the response
1039 * will contain required query information for stat() syscall.
1040 */
1041
1042 if (tcon->posix_extensions)
1043 break;
1044
1045 num_cmds = 1;
1046 cmds[0] = SMB2_OP_OPEN_QUERY;
1047 in_iov[0].iov_base = data;
1048 in_iov[0].iov_len = sizeof(*data);
1049 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, MAXIMUM_ALLOWED,
1050 FILE_OPEN, create_options, ACL_NO_MODE);
1051 free_rsp_iov(out_iov, out_buftype, ARRAY_SIZE(out_iov));
1052 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
1053 &oparms, in_iov, cmds, num_cmds,
1054 cfile, out_iov, out_buftype, NULL);
1055
1056 hdr = out_iov[0].iov_base;
1057 if (!hdr || out_buftype[0] == CIFS_NO_BUFFER)
1058 goto out;
1059
1060 if (!rc)
1061 rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]);
1062 break;
1063 case -EOPNOTSUPP:
1064 /*
1065 * BB TODO: When support for special files added to Samba
1066 * re-verify this path.
1067 */
1068 rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]);
1069 if (rc || !data->reparse_point)
1070 goto out;
1071
1072 /*
1073 * Skip SMB2_OP_GET_REPARSE if symlink already parsed in create
1074 * response.
1075 */
1076 if (data->reparse.tag != IO_REPARSE_TAG_SYMLINK) {
1077 cmds[num_cmds++] = SMB2_OP_GET_REPARSE;
1078 if (!tcon->posix_extensions)
1079 cmds[num_cmds++] = SMB2_OP_QUERY_WSL_EA;
1080 }
1081
1082 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path,
1083 FILE_READ_ATTRIBUTES |
1084 FILE_READ_EA | SYNCHRONIZE,
1085 FILE_OPEN, create_options |
1086 OPEN_REPARSE_POINT, ACL_NO_MODE);
1087 cifs_get_readable_path(tcon, full_path, &cfile);
1088 free_rsp_iov(out_iov, out_buftype, ARRAY_SIZE(out_iov));
1089 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
1090 &oparms, in_iov, cmds, num_cmds,
1091 cfile, out_iov, out_buftype, NULL);
1092 if (rc && ea_unsupported(cmds, num_cmds,
1093 out_iov, out_buftype)) {
1094 if (data->reparse.tag != IO_REPARSE_TAG_LX_BLK &&
1095 data->reparse.tag != IO_REPARSE_TAG_LX_CHR)
1096 rc = 0;
1097 else
1098 rc = -EOPNOTSUPP;
1099 }
1100
1101 if (data->reparse.tag == IO_REPARSE_TAG_SYMLINK && !rc) {
1102 bool directory = le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY;
1103 rc = smb2_fix_symlink_target_type(&data->symlink_target, directory, cifs_sb);
1104 }
1105 break;
1106 case -EREMOTE:
1107 break;
1108 default:
1109 if (hdr->Status != STATUS_OBJECT_NAME_INVALID)
1110 break;
1111 rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb,
1112 full_path, &islink);
1113 if (rc2) {
1114 rc = rc2;
1115 goto out;
1116 }
1117 if (islink)
1118 rc = -EREMOTE;
1119 }
1120
1121 out:
1122 free_rsp_iov(out_iov, out_buftype, ARRAY_SIZE(out_iov));
1123 return rc;
1124 }
1125
1126 int
smb2_mkdir(const unsigned int xid,struct inode * parent_inode,umode_t mode,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb)1127 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
1128 struct cifs_tcon *tcon, const char *name,
1129 struct cifs_sb_info *cifs_sb)
1130 {
1131 struct cifs_open_parms oparms;
1132
1133 oparms = CIFS_OPARMS(cifs_sb, tcon, name, FILE_WRITE_ATTRIBUTES,
1134 FILE_CREATE, CREATE_NOT_FILE, mode);
1135 return smb2_compound_op(xid, tcon, cifs_sb,
1136 name, &oparms, NULL,
1137 &(int){SMB2_OP_MKDIR}, 1,
1138 NULL, NULL, NULL, NULL);
1139 }
1140
1141 void
smb2_mkdir_setinfo(struct inode * inode,const char * name,struct cifs_sb_info * cifs_sb,struct cifs_tcon * tcon,const unsigned int xid)1142 smb2_mkdir_setinfo(struct inode *inode, const char *name,
1143 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
1144 const unsigned int xid)
1145 {
1146 struct cifs_open_parms oparms;
1147 FILE_BASIC_INFO data = {};
1148 struct cifsInodeInfo *cifs_i;
1149 struct cifsFileInfo *cfile;
1150 struct kvec in_iov;
1151 u32 dosattrs;
1152 int tmprc;
1153
1154 in_iov.iov_base = &data;
1155 in_iov.iov_len = sizeof(data);
1156 cifs_i = CIFS_I(inode);
1157 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
1158 data.Attributes = cpu_to_le32(dosattrs);
1159 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
1160 oparms = CIFS_OPARMS(cifs_sb, tcon, name, FILE_WRITE_ATTRIBUTES,
1161 FILE_CREATE, CREATE_NOT_FILE, ACL_NO_MODE);
1162 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
1163 &oparms, &in_iov,
1164 &(int){SMB2_OP_SET_INFO}, 1,
1165 cfile, NULL, NULL, NULL);
1166 if (tmprc == 0)
1167 cifs_i->cifsAttrs = dosattrs;
1168 }
1169
1170 int
smb2_rmdir(const unsigned int xid,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb)1171 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1172 struct cifs_sb_info *cifs_sb)
1173 {
1174 struct cifs_open_parms oparms;
1175
1176 drop_cached_dir_by_name(xid, tcon, name, cifs_sb);
1177 oparms = CIFS_OPARMS(cifs_sb, tcon, name, DELETE,
1178 FILE_OPEN, CREATE_NOT_FILE, ACL_NO_MODE);
1179 return smb2_compound_op(xid, tcon, cifs_sb,
1180 name, &oparms, NULL,
1181 &(int){SMB2_OP_UNLINK}, 1,
1182 NULL, NULL, NULL, NULL);
1183 }
1184
1185 int
smb2_unlink(const unsigned int xid,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb,struct dentry * dentry)1186 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1187 struct cifs_sb_info *cifs_sb, struct dentry *dentry)
1188 {
1189 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1190 __le16 *utf16_path __free(kfree) = NULL;
1191 int retries = 0, cur_sleep = 0;
1192 struct TCP_Server_Info *server;
1193 struct cifs_open_parms oparms;
1194 struct smb2_create_req *creq;
1195 struct inode *inode = NULL;
1196 struct smb_rqst rqst[2];
1197 struct kvec rsp_iov[2];
1198 struct kvec close_iov;
1199 int resp_buftype[2];
1200 struct cifs_fid fid;
1201 int flags = 0;
1202 __u8 oplock;
1203 int rc;
1204
1205 utf16_path = cifs_convert_path_to_utf16(name, cifs_sb);
1206 if (!utf16_path)
1207 return -ENOMEM;
1208
1209 if (smb3_encryption_required(tcon))
1210 flags |= CIFS_TRANSFORM_REQ;
1211 again:
1212 oplock = SMB2_OPLOCK_LEVEL_NONE;
1213 server = cifs_pick_channel(tcon->ses);
1214
1215 memset(rqst, 0, sizeof(rqst));
1216 memset(resp_buftype, 0, sizeof(resp_buftype));
1217 memset(rsp_iov, 0, sizeof(rsp_iov));
1218
1219 rqst[0].rq_iov = open_iov;
1220 rqst[0].rq_nvec = ARRAY_SIZE(open_iov);
1221
1222 oparms = CIFS_OPARMS(cifs_sb, tcon, name, DELETE | FILE_READ_ATTRIBUTES,
1223 FILE_OPEN, CREATE_DELETE_ON_CLOSE |
1224 OPEN_REPARSE_POINT, ACL_NO_MODE);
1225 oparms.fid = &fid;
1226
1227 if (dentry) {
1228 inode = d_inode(dentry);
1229 if (CIFS_I(inode)->lease_granted && server->ops->get_lease_key) {
1230 oplock = SMB2_OPLOCK_LEVEL_LEASE;
1231 server->ops->get_lease_key(inode, &fid);
1232 }
1233 }
1234
1235 rc = SMB2_open_init(tcon, server,
1236 &rqst[0], &oplock, &oparms, utf16_path);
1237 if (rc)
1238 goto err_free;
1239 smb2_set_next_command(tcon, &rqst[0]);
1240 creq = rqst[0].rq_iov[0].iov_base;
1241 creq->ShareAccess = FILE_SHARE_DELETE_LE;
1242
1243 rqst[1].rq_iov = &close_iov;
1244 rqst[1].rq_nvec = 1;
1245
1246 rc = SMB2_close_init(tcon, server, &rqst[1],
1247 COMPOUND_FID, COMPOUND_FID, false);
1248 smb2_set_related(&rqst[1]);
1249 if (rc)
1250 goto err_free;
1251
1252 if (retries) {
1253 /* Back-off before retry */
1254 if (cur_sleep)
1255 msleep(cur_sleep);
1256 for (int i = 0; i < ARRAY_SIZE(rqst); i++)
1257 smb2_set_replay(server, &rqst[i]);
1258 }
1259
1260 rc = compound_send_recv(xid, tcon->ses, server, flags,
1261 ARRAY_SIZE(rqst), rqst,
1262 resp_buftype, rsp_iov);
1263 SMB2_open_free(&rqst[0]);
1264 SMB2_close_free(&rqst[1]);
1265 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1266 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1267
1268 if (is_replayable_error(rc) &&
1269 smb2_should_replay(tcon, &retries, &cur_sleep))
1270 goto again;
1271
1272 /* Retry compound request without lease */
1273 if (rc == -EINVAL && dentry) {
1274 dentry = NULL;
1275 retries = 0;
1276 cur_sleep = 0;
1277 goto again;
1278 }
1279 /*
1280 * If dentry (hence, inode) is NULL, lease break is going to
1281 * take care of degrading leases on handles for deleted files.
1282 */
1283 if (!rc && inode)
1284 cifs_mark_open_handles_for_deleted_file(inode, name);
1285
1286 return rc;
1287
1288 err_free:
1289 SMB2_open_free(&rqst[0]);
1290 SMB2_close_free(&rqst[1]);
1291 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1292 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1293 return rc;
1294 }
1295
smb2_set_path_attr(const unsigned int xid,struct cifs_tcon * tcon,const char * from_name,const char * to_name,struct cifs_sb_info * cifs_sb,__u32 create_options,__u32 access,int command,struct cifsFileInfo * cfile,struct dentry * dentry)1296 static int smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
1297 const char *from_name, const char *to_name,
1298 struct cifs_sb_info *cifs_sb,
1299 __u32 create_options, __u32 access,
1300 int command, struct cifsFileInfo *cfile,
1301 struct dentry *dentry)
1302 {
1303 struct cifs_open_parms oparms;
1304 struct kvec in_iov;
1305 __le16 *smb2_to_name = NULL;
1306 int rc;
1307
1308 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
1309 if (smb2_to_name == NULL) {
1310 rc = -ENOMEM;
1311 if (cfile)
1312 cifsFileInfo_put(cfile);
1313 goto smb2_rename_path;
1314 }
1315 in_iov.iov_base = smb2_to_name;
1316 in_iov.iov_len = 2 * UniStrnlen((wchar_t *)smb2_to_name, PATH_MAX);
1317 oparms = CIFS_OPARMS(cifs_sb, tcon, from_name, access, FILE_OPEN,
1318 create_options, ACL_NO_MODE);
1319 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name,
1320 &oparms, &in_iov, &command, 1,
1321 cfile, NULL, NULL, dentry);
1322 smb2_rename_path:
1323 kfree(smb2_to_name);
1324 return rc;
1325 }
1326
smb2_rename_path(const unsigned int xid,struct cifs_tcon * tcon,struct dentry * source_dentry,const char * from_name,const char * to_name,struct cifs_sb_info * cifs_sb)1327 int smb2_rename_path(const unsigned int xid,
1328 struct cifs_tcon *tcon,
1329 struct dentry *source_dentry,
1330 const char *from_name, const char *to_name,
1331 struct cifs_sb_info *cifs_sb)
1332 {
1333 struct cifsFileInfo *cfile;
1334 __u32 co = file_create_options(source_dentry);
1335
1336 drop_cached_dir_by_name(xid, tcon, from_name, cifs_sb);
1337 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
1338
1339 int rc = smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
1340 co, DELETE, SMB2_OP_RENAME, cfile, source_dentry);
1341 if (rc == -EINVAL) {
1342 cifs_dbg(FYI, "invalid lease key, resending request without lease");
1343 cifs_get_writable_path(tcon, from_name,
1344 FIND_WR_WITH_DELETE, &cfile);
1345 rc = smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
1346 co, DELETE, SMB2_OP_RENAME, cfile, NULL);
1347 }
1348 return rc;
1349 }
1350
smb2_create_hardlink(const unsigned int xid,struct cifs_tcon * tcon,struct dentry * source_dentry,const char * from_name,const char * to_name,struct cifs_sb_info * cifs_sb)1351 int smb2_create_hardlink(const unsigned int xid,
1352 struct cifs_tcon *tcon,
1353 struct dentry *source_dentry,
1354 const char *from_name, const char *to_name,
1355 struct cifs_sb_info *cifs_sb)
1356 {
1357 __u32 co = file_create_options(source_dentry);
1358
1359 return smb2_set_path_attr(xid, tcon, from_name, to_name,
1360 cifs_sb, co, FILE_READ_ATTRIBUTES,
1361 SMB2_OP_HARDLINK, NULL, NULL);
1362 }
1363
1364 int
smb2_set_path_size(const unsigned int xid,struct cifs_tcon * tcon,const char * full_path,__u64 size,struct cifs_sb_info * cifs_sb,bool set_alloc,struct dentry * dentry)1365 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
1366 const char *full_path, __u64 size,
1367 struct cifs_sb_info *cifs_sb, bool set_alloc,
1368 struct dentry *dentry)
1369 {
1370 struct cifs_open_parms oparms;
1371 struct cifsFileInfo *cfile;
1372 struct kvec in_iov;
1373 __le64 eof = cpu_to_le64(size);
1374 int rc;
1375
1376 in_iov.iov_base = &eof;
1377 in_iov.iov_len = sizeof(eof);
1378 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
1379
1380 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, FILE_WRITE_DATA,
1381 FILE_OPEN, 0, ACL_NO_MODE);
1382 rc = smb2_compound_op(xid, tcon, cifs_sb,
1383 full_path, &oparms, &in_iov,
1384 &(int){SMB2_OP_SET_EOF}, 1,
1385 cfile, NULL, NULL, dentry);
1386 if (rc == -EINVAL) {
1387 cifs_dbg(FYI, "invalid lease key, resending request without lease");
1388 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
1389 rc = smb2_compound_op(xid, tcon, cifs_sb,
1390 full_path, &oparms, &in_iov,
1391 &(int){SMB2_OP_SET_EOF}, 1,
1392 cfile, NULL, NULL, NULL);
1393 }
1394 return rc;
1395 }
1396
1397 int
smb2_set_file_info(struct inode * inode,const char * full_path,FILE_BASIC_INFO * buf,const unsigned int xid)1398 smb2_set_file_info(struct inode *inode, const char *full_path,
1399 FILE_BASIC_INFO *buf, const unsigned int xid)
1400 {
1401 struct kvec in_iov = { .iov_base = buf, .iov_len = sizeof(*buf), };
1402 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1403 struct cifsFileInfo *cfile = NULL;
1404 struct cifs_open_parms oparms;
1405 struct tcon_link *tlink;
1406 struct cifs_tcon *tcon;
1407 int rc = 0;
1408
1409 tlink = cifs_sb_tlink(cifs_sb);
1410 if (IS_ERR(tlink))
1411 return PTR_ERR(tlink);
1412 tcon = tlink_tcon(tlink);
1413
1414 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
1415 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0)) {
1416 if (buf->Attributes == 0)
1417 goto out; /* would be a no op, no sense sending this */
1418 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
1419 }
1420
1421 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, FILE_WRITE_ATTRIBUTES,
1422 FILE_OPEN, 0, ACL_NO_MODE);
1423 rc = smb2_compound_op(xid, tcon, cifs_sb,
1424 full_path, &oparms, &in_iov,
1425 &(int){SMB2_OP_SET_INFO}, 1,
1426 cfile, NULL, NULL, NULL);
1427 out:
1428 cifs_put_tlink(tlink);
1429 return rc;
1430 }
1431
smb2_create_reparse_inode(struct cifs_open_info_data * data,struct super_block * sb,const unsigned int xid,struct cifs_tcon * tcon,const char * full_path,bool directory,struct kvec * reparse_iov,struct kvec * xattr_iov)1432 struct inode *smb2_create_reparse_inode(struct cifs_open_info_data *data,
1433 struct super_block *sb,
1434 const unsigned int xid,
1435 struct cifs_tcon *tcon,
1436 const char *full_path,
1437 bool directory,
1438 struct kvec *reparse_iov,
1439 struct kvec *xattr_iov)
1440 {
1441 struct cifs_open_parms oparms;
1442 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1443 struct cifsFileInfo *cfile;
1444 struct inode *new = NULL;
1445 int out_buftype[4] = {};
1446 struct kvec out_iov[4] = {};
1447 struct kvec in_iov[2];
1448 int cmds[2];
1449 int rc;
1450 int i;
1451
1452 /*
1453 * If server filesystem does not support reparse points then do not
1454 * attempt to create reparse point. This will prevent creating unusable
1455 * empty object on the server.
1456 */
1457 if (!CIFS_REPARSE_SUPPORT(tcon))
1458 return ERR_PTR(-EOPNOTSUPP);
1459
1460 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path,
1461 SYNCHRONIZE | DELETE |
1462 FILE_READ_ATTRIBUTES |
1463 FILE_WRITE_ATTRIBUTES,
1464 FILE_CREATE,
1465 (directory ? CREATE_NOT_FILE : CREATE_NOT_DIR) | OPEN_REPARSE_POINT,
1466 ACL_NO_MODE);
1467 if (xattr_iov)
1468 oparms.ea_cctx = xattr_iov;
1469
1470 cmds[0] = SMB2_OP_SET_REPARSE;
1471 in_iov[0] = *reparse_iov;
1472 in_iov[1].iov_base = data;
1473 in_iov[1].iov_len = sizeof(*data);
1474
1475 if (tcon->posix_extensions) {
1476 cmds[1] = SMB2_OP_POSIX_QUERY_INFO;
1477 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
1478 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, &oparms,
1479 in_iov, cmds, 2, cfile, out_iov, out_buftype, NULL);
1480 if (!rc) {
1481 rc = smb311_posix_get_inode_info(&new, full_path,
1482 data, sb, xid);
1483 }
1484 } else {
1485 cmds[1] = SMB2_OP_QUERY_INFO;
1486 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
1487 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, &oparms,
1488 in_iov, cmds, 2, cfile, out_iov, out_buftype, NULL);
1489 if (!rc) {
1490 rc = cifs_get_inode_info(&new, full_path,
1491 data, sb, xid, NULL);
1492 }
1493 }
1494
1495
1496 /*
1497 * If CREATE was successful but SMB2_OP_SET_REPARSE failed then
1498 * remove the intermediate object created by CREATE. Otherwise
1499 * empty object stay on the server when reparse call failed.
1500 */
1501 if (rc &&
1502 out_iov[0].iov_base != NULL && out_buftype[0] != CIFS_NO_BUFFER &&
1503 ((struct smb2_hdr *)out_iov[0].iov_base)->Status == STATUS_SUCCESS &&
1504 (out_iov[1].iov_base == NULL || out_buftype[1] == CIFS_NO_BUFFER ||
1505 ((struct smb2_hdr *)out_iov[1].iov_base)->Status != STATUS_SUCCESS))
1506 smb2_unlink(xid, tcon, full_path, cifs_sb, NULL);
1507
1508 for (i = 0; i < ARRAY_SIZE(out_buftype); i++)
1509 free_rsp_buf(out_buftype[i], out_iov[i].iov_base);
1510
1511 return rc ? ERR_PTR(rc) : new;
1512 }
1513
smb2_query_reparse_point(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,u32 * tag,struct kvec * rsp,int * rsp_buftype)1514 int smb2_query_reparse_point(const unsigned int xid,
1515 struct cifs_tcon *tcon,
1516 struct cifs_sb_info *cifs_sb,
1517 const char *full_path,
1518 u32 *tag, struct kvec *rsp,
1519 int *rsp_buftype)
1520 {
1521 struct cifs_open_parms oparms;
1522 struct cifs_open_info_data data = {};
1523 struct cifsFileInfo *cfile;
1524 struct kvec in_iov = { .iov_base = &data, .iov_len = sizeof(data), };
1525 int rc;
1526
1527 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1528
1529 cifs_get_readable_path(tcon, full_path, &cfile);
1530 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path,
1531 FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE,
1532 FILE_OPEN, OPEN_REPARSE_POINT, ACL_NO_MODE);
1533 rc = smb2_compound_op(xid, tcon, cifs_sb,
1534 full_path, &oparms, &in_iov,
1535 &(int){SMB2_OP_GET_REPARSE}, 1,
1536 cfile, NULL, NULL, NULL);
1537 if (rc)
1538 goto out;
1539
1540 *tag = data.reparse.tag;
1541 *rsp = data.reparse.io.iov;
1542 *rsp_buftype = data.reparse.io.buftype;
1543 memset(&data.reparse.io.iov, 0, sizeof(data.reparse.io.iov));
1544 data.reparse.io.buftype = CIFS_NO_BUFFER;
1545 out:
1546 cifs_free_open_info(&data);
1547 return rc;
1548 }
1549
utf16_smb2_path(struct cifs_sb_info * cifs_sb,const char * name,size_t namelen)1550 static inline __le16 *utf16_smb2_path(struct cifs_sb_info *cifs_sb,
1551 const char *name, size_t namelen)
1552 {
1553 int len;
1554
1555 if (*name == '\\' ||
1556 (cifs_sb_master_tlink(cifs_sb) &&
1557 cifs_sb_master_tcon(cifs_sb)->posix_extensions && *name == '/'))
1558 name++;
1559 return cifs_strndup_to_utf16(name, namelen, &len,
1560 cifs_sb->local_nls,
1561 cifs_remap(cifs_sb));
1562 }
1563
smb2_rename_pending_delete(const char * full_path,struct dentry * dentry,const unsigned int xid)1564 int smb2_rename_pending_delete(const char *full_path,
1565 struct dentry *dentry,
1566 const unsigned int xid)
1567 {
1568 struct cifs_sb_info *cifs_sb = CIFS_SB(d_inode(dentry)->i_sb);
1569 struct cifsInodeInfo *cinode = CIFS_I(d_inode(dentry));
1570 __le16 *utf16_path __free(kfree) = NULL;
1571 __u32 co = file_create_options(dentry);
1572 int cmds[] = {
1573 SMB2_OP_SET_INFO,
1574 SMB2_OP_RENAME,
1575 SMB2_OP_UNLINK,
1576 };
1577 const int num_cmds = ARRAY_SIZE(cmds);
1578 char *to_name __free(kfree) = NULL;
1579 __u32 attrs = cinode->cifsAttrs;
1580 struct cifs_open_parms oparms;
1581 static atomic_t sillycounter;
1582 struct cifsFileInfo *cfile;
1583 struct tcon_link *tlink;
1584 struct cifs_tcon *tcon;
1585 struct kvec iov[2];
1586 const char *ppath;
1587 void *page;
1588 size_t len;
1589 int rc;
1590
1591 tlink = cifs_sb_tlink(cifs_sb);
1592 if (IS_ERR(tlink))
1593 return PTR_ERR(tlink);
1594 tcon = tlink_tcon(tlink);
1595
1596 page = alloc_dentry_path();
1597
1598 ppath = build_path_from_dentry(dentry->d_parent, page);
1599 if (IS_ERR(ppath)) {
1600 rc = PTR_ERR(ppath);
1601 goto out;
1602 }
1603
1604 len = strlen(ppath) + strlen("/.__smb1234") + 1;
1605 to_name = kmalloc(len, GFP_KERNEL);
1606 if (!to_name) {
1607 rc = -ENOMEM;
1608 goto out;
1609 }
1610
1611 scnprintf(to_name, len, "%s%c.__smb%04X", ppath, CIFS_DIR_SEP(cifs_sb),
1612 atomic_inc_return(&sillycounter) & 0xffff);
1613
1614 utf16_path = utf16_smb2_path(cifs_sb, to_name, len);
1615 if (!utf16_path) {
1616 rc = -ENOMEM;
1617 goto out;
1618 }
1619
1620 drop_cached_dir_by_name(xid, tcon, full_path, cifs_sb);
1621 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path,
1622 DELETE | FILE_WRITE_ATTRIBUTES,
1623 FILE_OPEN, co, ACL_NO_MODE);
1624
1625 attrs &= ~ATTR_READONLY;
1626 if (!attrs)
1627 attrs = ATTR_NORMAL;
1628 if (d_inode(dentry)->i_nlink <= 1)
1629 attrs |= ATTR_HIDDEN;
1630 iov[0].iov_base = &(FILE_BASIC_INFO) {
1631 .Attributes = cpu_to_le32(attrs),
1632 };
1633 iov[0].iov_len = sizeof(FILE_BASIC_INFO);
1634 iov[1].iov_base = utf16_path;
1635 iov[1].iov_len = sizeof(*utf16_path) * UniStrlen((wchar_t *)utf16_path);
1636
1637 cifs_get_writable_path(tcon, full_path, FIND_WR_WITH_DELETE, &cfile);
1638 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, &oparms, iov,
1639 cmds, num_cmds, cfile, NULL, NULL, dentry);
1640 if (rc == -EINVAL) {
1641 cifs_dbg(FYI, "invalid lease key, resending request without lease\n");
1642 cifs_get_writable_path(tcon, full_path,
1643 FIND_WR_WITH_DELETE, &cfile);
1644 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, &oparms, iov,
1645 cmds, num_cmds, cfile, NULL, NULL, NULL);
1646 }
1647 if (!rc) {
1648 set_bit(CIFS_INO_DELETE_PENDING, &cinode->flags);
1649 } else {
1650 cifs_tcon_dbg(FYI, "%s: failed to rename '%s' to '%s': %d\n",
1651 __func__, full_path, to_name, rc);
1652 rc = smb_EIO1(smb_eio_trace_pend_del_fail, rc);
1653 }
1654 out:
1655 cifs_put_tlink(tlink);
1656 free_dentry_path(page);
1657 return rc;
1658 }
1659