Debian Codegraph @

fs/smb/client/smb2inode.c

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