1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 *
4 * Copyright (C) International Business Machines Corp., 2002, 2011
5 * Etersoft, 2012
6 * Author(s): Pavel Shilovsky (pshilovsky@samba.org),
7 * Steve French (sfrench@us.ibm.com)
8 *
9 */
10 #include <linux/fs.h>
11 #include <linux/stat.h>
12 #include <linux/slab.h>
13 #include <linux/pagemap.h>
14 #include <asm/div64.h>
15 #include "cifsfs.h"
16 #include "cifspdu.h"
17 #include "cifsglob.h"
18 #include "cifsproto.h"
19 #include "cifs_debug.h"
20 #include "cifs_fs_sb.h"
21 #include "cifs_unicode.h"
22 #include "fscache.h"
23 #include "smb2glob.h"
24 #include "smb2pdu.h"
25 #include "smb2proto.h"
26 #include "cached_dir.h"
27 #include "../common/smb2status.h"
28
reparse_buf_ptr(struct kvec * iov)29 static struct reparse_data_buffer *reparse_buf_ptr(struct kvec *iov)
30 {
31 struct reparse_data_buffer *buf;
32 struct smb2_ioctl_rsp *io = iov->iov_base;
33 u32 off, count, len;
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(-EIO);
39
40 buf = (struct reparse_data_buffer *)((u8 *)io + off);
41 len = sizeof(*buf);
42 if (count < len || count < le16_to_cpu(buf->ReparseDataLength) + len)
43 return ERR_PTR(-EIO);
44 return buf;
45 }
46
file_create_options(struct dentry * dentry)47 static inline __u32 file_create_options(struct dentry *dentry)
48 {
49 struct cifsInodeInfo *ci;
50
51 if (dentry) {
52 ci = CIFS_I(d_inode(dentry));
53 if (ci->cifsAttrs & ATTR_REPARSE)
54 return OPEN_REPARSE_POINT;
55 }
56 return 0;
57 }
58
59 /* Parse owner and group from SMB3.1.1 POSIX query info */
parse_posix_sids(struct cifs_open_info_data * data,struct kvec * rsp_iov)60 static int parse_posix_sids(struct cifs_open_info_data *data,
61 struct kvec *rsp_iov)
62 {
63 struct smb2_query_info_rsp *qi = rsp_iov->iov_base;
64 unsigned int out_len = le32_to_cpu(qi->OutputBufferLength);
65 unsigned int qi_len = sizeof(data->posix_fi);
66 int owner_len, group_len;
67 u8 *sidsbuf, *sidsbuf_end;
68
69 if (out_len <= qi_len)
70 return -EINVAL;
71
72 sidsbuf = (u8 *)qi + le16_to_cpu(qi->OutputBufferOffset) + qi_len;
73 sidsbuf_end = sidsbuf + out_len - qi_len;
74
75 owner_len = posix_info_sid_size(sidsbuf, sidsbuf_end);
76 if (owner_len == -1)
77 return -EINVAL;
78
79 memcpy(&data->posix_owner, sidsbuf, owner_len);
80 group_len = posix_info_sid_size(sidsbuf + owner_len, sidsbuf_end);
81 if (group_len == -1)
82 return -EINVAL;
83
84 memcpy(&data->posix_group, sidsbuf + owner_len, group_len);
85 return 0;
86 }
87
88 struct wsl_query_ea {
89 __le32 next;
90 __u8 name_len;
91 __u8 name[SMB2_WSL_XATTR_NAME_LEN + 1];
92 } __packed;
93
94 #define NEXT_OFF cpu_to_le32(sizeof(struct wsl_query_ea))
95
96 static const struct wsl_query_ea wsl_query_eas[] = {
97 { .next = NEXT_OFF, .name_len = SMB2_WSL_XATTR_NAME_LEN, .name = SMB2_WSL_XATTR_UID, },
98 { .next = NEXT_OFF, .name_len = SMB2_WSL_XATTR_NAME_LEN, .name = SMB2_WSL_XATTR_GID, },
99 { .next = NEXT_OFF, .name_len = SMB2_WSL_XATTR_NAME_LEN, .name = SMB2_WSL_XATTR_MODE, },
100 { .next = 0, .name_len = SMB2_WSL_XATTR_NAME_LEN, .name = SMB2_WSL_XATTR_DEV, },
101 };
102
check_wsl_eas(struct kvec * rsp_iov)103 static int check_wsl_eas(struct kvec *rsp_iov)
104 {
105 struct smb2_file_full_ea_info *ea;
106 struct smb2_query_info_rsp *rsp = rsp_iov->iov_base;
107 unsigned long addr;
108 u32 outlen, next;
109 u16 vlen;
110 u8 nlen;
111 u8 *end;
112
113 outlen = le32_to_cpu(rsp->OutputBufferLength);
114 if (outlen < SMB2_WSL_MIN_QUERY_EA_RESP_SIZE ||
115 outlen > SMB2_WSL_MAX_QUERY_EA_RESP_SIZE)
116 return -EINVAL;
117
118 ea = (void *)((u8 *)rsp_iov->iov_base +
119 le16_to_cpu(rsp->OutputBufferOffset));
120 end = (u8 *)rsp_iov->iov_base + rsp_iov->iov_len;
121 for (;;) {
122 if ((u8 *)ea > end - sizeof(*ea))
123 return -EINVAL;
124
125 nlen = ea->ea_name_length;
126 vlen = le16_to_cpu(ea->ea_value_length);
127 if (nlen != SMB2_WSL_XATTR_NAME_LEN ||
128 (u8 *)ea + nlen + 1 + vlen > end)
129 return -EINVAL;
130
131 switch (vlen) {
132 case 4:
133 if (strncmp(ea->ea_data, SMB2_WSL_XATTR_UID, nlen) &&
134 strncmp(ea->ea_data, SMB2_WSL_XATTR_GID, nlen) &&
135 strncmp(ea->ea_data, SMB2_WSL_XATTR_MODE, nlen))
136 return -EINVAL;
137 break;
138 case 8:
139 if (strncmp(ea->ea_data, SMB2_WSL_XATTR_DEV, nlen))
140 return -EINVAL;
141 break;
142 case 0:
143 if (!strncmp(ea->ea_data, SMB2_WSL_XATTR_UID, nlen) ||
144 !strncmp(ea->ea_data, SMB2_WSL_XATTR_GID, nlen) ||
145 !strncmp(ea->ea_data, SMB2_WSL_XATTR_MODE, nlen) ||
146 !strncmp(ea->ea_data, SMB2_WSL_XATTR_DEV, nlen))
147 break;
148 fallthrough;
149 default:
150 return -EINVAL;
151 }
152
153 next = le32_to_cpu(ea->next_entry_offset);
154 if (!next)
155 break;
156 if (!IS_ALIGNED(next, 4) ||
157 check_add_overflow((unsigned long)ea, next, &addr))
158 return -EINVAL;
159 ea = (void *)addr;
160 }
161 return 0;
162 }
163
164 /*
165 * note: If cfile is passed, the reference to it is dropped here.
166 * So make sure that you do not reuse cfile after return from this func.
167 *
168 * If passing @out_iov and @out_buftype, ensure to make them both large enough
169 * (>= 3) to hold all compounded responses. Caller is also responsible for
170 * freeing them up with free_rsp_buf().
171 */
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)172 static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
173 struct cifs_sb_info *cifs_sb, const char *full_path,
174 struct cifs_open_parms *oparms, struct kvec *in_iov,
175 int *cmds, int num_cmds, struct cifsFileInfo *cfile,
176 struct kvec *out_iov, int *out_buftype, struct dentry *dentry)
177 {
178
179 struct smb2_query_info_rsp *qi_rsp = NULL;
180 struct smb2_compound_vars *vars = NULL;
181 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
182 struct cifs_open_info_data *idata;
183 struct cifs_ses *ses = tcon->ses;
184 struct reparse_data_buffer *rbuf;
185 struct TCP_Server_Info *server;
186 int resp_buftype[MAX_COMPOUND];
187 int retries = 0, cur_sleep = 1;
188 __u8 delete_pending[8] = {1,};
189 struct kvec *rsp_iov, *iov;
190 struct inode *inode = NULL;
191 __le16 *utf16_path = NULL;
192 struct smb_rqst *rqst;
193 unsigned int size[2];
194 struct cifs_fid fid;
195 int num_rqst = 0, i;
196 unsigned int len;
197 int tmp_rc, rc;
198 int flags = 0;
199 void *data[2];
200
201 replay_again:
202 /* reinitialize for possible replay */
203 flags = 0;
204 oplock = SMB2_OPLOCK_LEVEL_NONE;
205 num_rqst = 0;
206 server = cifs_pick_channel(ses);
207
208 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
209 if (vars == NULL)
210 return -ENOMEM;
211 rqst = &vars->rqst[0];
212 rsp_iov = &vars->rsp_iov[0];
213
214 if (smb3_encryption_required(tcon))
215 flags |= CIFS_TRANSFORM_REQ;
216
217 for (i = 0; i < ARRAY_SIZE(resp_buftype); i++)
218 resp_buftype[i] = CIFS_NO_BUFFER;
219
220 /* We already have a handle so we can skip the open */
221 if (cfile)
222 goto after_open;
223
224 /* Open */
225 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
226 if (!utf16_path) {
227 rc = -ENOMEM;
228 goto finished;
229 }
230
231 /* if there is an existing lease, reuse it */
232
233 /*
234 * note: files with hardlinks cause unexpected behaviour. As per MS-SMB2,
235 * lease keys are associated with the filepath. We are maintaining lease keys
236 * with the inode on the client. If the file has hardlinks, it is possible
237 * that the lease for a file be reused for an operation on its hardlink or
238 * vice versa.
239 * As a workaround, send request using an existing lease key and if the server
240 * returns STATUS_INVALID_PARAMETER, which maps to EINVAL, send the request
241 * again without the lease.
242 */
243 if (dentry) {
244 inode = d_inode(dentry);
245 if (CIFS_I(inode)->lease_granted && server->ops->get_lease_key) {
246 oplock = SMB2_OPLOCK_LEVEL_LEASE;
247 server->ops->get_lease_key(inode, &fid);
248 }
249 }
250
251 vars->oparms = *oparms;
252 vars->oparms.fid = &fid;
253
254 rqst[num_rqst].rq_iov = &vars->open_iov[0];
255 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
256 rc = SMB2_open_init(tcon, server,
257 &rqst[num_rqst], &oplock, &vars->oparms,
258 utf16_path);
259 kfree(utf16_path);
260 if (rc)
261 goto finished;
262
263 smb2_set_next_command(tcon, &rqst[num_rqst]);
264 after_open:
265 num_rqst++;
266 rc = 0;
267
268 for (i = 0; i < num_cmds; i++) {
269 /* Operation */
270 switch (cmds[i]) {
271 case SMB2_OP_QUERY_INFO:
272 rqst[num_rqst].rq_iov = &vars->qi_iov;
273 rqst[num_rqst].rq_nvec = 1;
274
275 if (cfile) {
276 rc = SMB2_query_info_init(tcon, server,
277 &rqst[num_rqst],
278 cfile->fid.persistent_fid,
279 cfile->fid.volatile_fid,
280 FILE_ALL_INFORMATION,
281 SMB2_O_INFO_FILE, 0,
282 sizeof(struct smb2_file_all_info) +
283 PATH_MAX * 2, 0, NULL);
284 } else {
285 rc = SMB2_query_info_init(tcon, server,
286 &rqst[num_rqst],
287 COMPOUND_FID,
288 COMPOUND_FID,
289 FILE_ALL_INFORMATION,
290 SMB2_O_INFO_FILE, 0,
291 sizeof(struct smb2_file_all_info) +
292 PATH_MAX * 2, 0, NULL);
293 }
294 if (!rc && (!cfile || num_rqst > 1)) {
295 smb2_set_next_command(tcon, &rqst[num_rqst]);
296 smb2_set_related(&rqst[num_rqst]);
297 } else if (rc) {
298 goto finished;
299 }
300 num_rqst++;
301 trace_smb3_query_info_compound_enter(xid, tcon->tid,
302 ses->Suid, full_path);
303 break;
304 case SMB2_OP_POSIX_QUERY_INFO:
305 rqst[num_rqst].rq_iov = &vars->qi_iov;
306 rqst[num_rqst].rq_nvec = 1;
307
308 if (cfile) {
309 /* TBD: fix following to allow for longer SIDs */
310 rc = SMB2_query_info_init(tcon, server,
311 &rqst[num_rqst],
312 cfile->fid.persistent_fid,
313 cfile->fid.volatile_fid,
314 SMB_FIND_FILE_POSIX_INFO,
315 SMB2_O_INFO_FILE, 0,
316 sizeof(struct smb311_posix_qinfo *) +
317 (PATH_MAX * 2) +
318 (sizeof(struct smb_sid) * 2), 0, NULL);
319 } else {
320 rc = SMB2_query_info_init(tcon, server,
321 &rqst[num_rqst],
322 COMPOUND_FID,
323 COMPOUND_FID,
324 SMB_FIND_FILE_POSIX_INFO,
325 SMB2_O_INFO_FILE, 0,
326 sizeof(struct smb311_posix_qinfo *) +
327 (PATH_MAX * 2) +
328 (sizeof(struct smb_sid) * 2), 0, NULL);
329 }
330 if (!rc && (!cfile || num_rqst > 1)) {
331 smb2_set_next_command(tcon, &rqst[num_rqst]);
332 smb2_set_related(&rqst[num_rqst]);
333 } else if (rc) {
334 goto finished;
335 }
336 num_rqst++;
337 trace_smb3_posix_query_info_compound_enter(xid, tcon->tid,
338 ses->Suid, full_path);
339 break;
340 case SMB2_OP_DELETE:
341 trace_smb3_delete_enter(xid, tcon->tid, ses->Suid, full_path);
342 break;
343 case SMB2_OP_MKDIR:
344 /*
345 * Directories are created through parameters in the
346 * SMB2_open() call.
347 */
348 trace_smb3_mkdir_enter(xid, tcon->tid, ses->Suid, full_path);
349 break;
350 case SMB2_OP_RMDIR:
351 rqst[num_rqst].rq_iov = &vars->si_iov[0];
352 rqst[num_rqst].rq_nvec = 1;
353
354 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
355 data[0] = &delete_pending[0];
356
357 rc = SMB2_set_info_init(tcon, server,
358 &rqst[num_rqst], COMPOUND_FID,
359 COMPOUND_FID, current->tgid,
360 FILE_DISPOSITION_INFORMATION,
361 SMB2_O_INFO_FILE, 0, data, size);
362 if (rc)
363 goto finished;
364 smb2_set_next_command(tcon, &rqst[num_rqst]);
365 smb2_set_related(&rqst[num_rqst++]);
366 trace_smb3_rmdir_enter(xid, tcon->tid, ses->Suid, full_path);
367 break;
368 case SMB2_OP_SET_EOF:
369 rqst[num_rqst].rq_iov = &vars->si_iov[0];
370 rqst[num_rqst].rq_nvec = 1;
371
372 size[0] = in_iov[i].iov_len;
373 data[0] = in_iov[i].iov_base;
374
375 if (cfile) {
376 rc = SMB2_set_info_init(tcon, server,
377 &rqst[num_rqst],
378 cfile->fid.persistent_fid,
379 cfile->fid.volatile_fid,
380 current->tgid,
381 FILE_END_OF_FILE_INFORMATION,
382 SMB2_O_INFO_FILE, 0,
383 data, size);
384 } else {
385 rc = SMB2_set_info_init(tcon, server,
386 &rqst[num_rqst],
387 COMPOUND_FID,
388 COMPOUND_FID,
389 current->tgid,
390 FILE_END_OF_FILE_INFORMATION,
391 SMB2_O_INFO_FILE, 0,
392 data, size);
393 }
394 if (!rc && (!cfile || num_rqst > 1)) {
395 smb2_set_next_command(tcon, &rqst[num_rqst]);
396 smb2_set_related(&rqst[num_rqst]);
397 } else if (rc) {
398 goto finished;
399 }
400 num_rqst++;
401 trace_smb3_set_eof_enter(xid, tcon->tid, ses->Suid, full_path);
402 break;
403 case SMB2_OP_SET_INFO:
404 rqst[num_rqst].rq_iov = &vars->si_iov[0];
405 rqst[num_rqst].rq_nvec = 1;
406
407 size[0] = in_iov[i].iov_len;
408 data[0] = in_iov[i].iov_base;
409
410 if (cfile) {
411 rc = SMB2_set_info_init(tcon, server,
412 &rqst[num_rqst],
413 cfile->fid.persistent_fid,
414 cfile->fid.volatile_fid, current->tgid,
415 FILE_BASIC_INFORMATION,
416 SMB2_O_INFO_FILE, 0, data, size);
417 } else {
418 rc = SMB2_set_info_init(tcon, server,
419 &rqst[num_rqst],
420 COMPOUND_FID,
421 COMPOUND_FID, current->tgid,
422 FILE_BASIC_INFORMATION,
423 SMB2_O_INFO_FILE, 0, data, size);
424 }
425 if (!rc && (!cfile || num_rqst > 1)) {
426 smb2_set_next_command(tcon, &rqst[num_rqst]);
427 smb2_set_related(&rqst[num_rqst]);
428 } else if (rc) {
429 goto finished;
430 }
431 num_rqst++;
432 trace_smb3_set_info_compound_enter(xid, tcon->tid,
433 ses->Suid, full_path);
434 break;
435 case SMB2_OP_RENAME:
436 rqst[num_rqst].rq_iov = &vars->si_iov[0];
437 rqst[num_rqst].rq_nvec = 2;
438
439 len = in_iov[i].iov_len;
440
441 vars->rename_info.ReplaceIfExists = 1;
442 vars->rename_info.RootDirectory = 0;
443 vars->rename_info.FileNameLength = cpu_to_le32(len);
444
445 size[0] = sizeof(struct smb2_file_rename_info);
446 data[0] = &vars->rename_info;
447
448 size[1] = len + 2 /* null */;
449 data[1] = in_iov[i].iov_base;
450
451 if (cfile) {
452 rc = SMB2_set_info_init(tcon, server,
453 &rqst[num_rqst],
454 cfile->fid.persistent_fid,
455 cfile->fid.volatile_fid,
456 current->tgid, FILE_RENAME_INFORMATION,
457 SMB2_O_INFO_FILE, 0, data, size);
458 } else {
459 rc = SMB2_set_info_init(tcon, server,
460 &rqst[num_rqst],
461 COMPOUND_FID, COMPOUND_FID,
462 current->tgid, FILE_RENAME_INFORMATION,
463 SMB2_O_INFO_FILE, 0, data, size);
464 }
465 if (!rc && (!cfile || num_rqst > 1)) {
466 smb2_set_next_command(tcon, &rqst[num_rqst]);
467 smb2_set_related(&rqst[num_rqst]);
468 } else if (rc) {
469 goto finished;
470 }
471 num_rqst++;
472 trace_smb3_rename_enter(xid, tcon->tid, ses->Suid, full_path);
473 break;
474 case SMB2_OP_HARDLINK:
475 rqst[num_rqst].rq_iov = &vars->si_iov[0];
476 rqst[num_rqst].rq_nvec = 2;
477
478 len = in_iov[i].iov_len;
479
480 vars->link_info.ReplaceIfExists = 0;
481 vars->link_info.RootDirectory = 0;
482 vars->link_info.FileNameLength = cpu_to_le32(len);
483
484 size[0] = sizeof(struct smb2_file_link_info);
485 data[0] = &vars->link_info;
486
487 size[1] = len + 2 /* null */;
488 data[1] = in_iov[i].iov_base;
489
490 rc = SMB2_set_info_init(tcon, server,
491 &rqst[num_rqst], COMPOUND_FID,
492 COMPOUND_FID, current->tgid,
493 FILE_LINK_INFORMATION,
494 SMB2_O_INFO_FILE, 0, data, size);
495 if (rc)
496 goto finished;
497 smb2_set_next_command(tcon, &rqst[num_rqst]);
498 smb2_set_related(&rqst[num_rqst++]);
499 trace_smb3_hardlink_enter(xid, tcon->tid, ses->Suid, full_path);
500 break;
501 case SMB2_OP_SET_REPARSE:
502 rqst[num_rqst].rq_iov = vars->io_iov;
503 rqst[num_rqst].rq_nvec = ARRAY_SIZE(vars->io_iov);
504
505 if (cfile) {
506 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst],
507 cfile->fid.persistent_fid,
508 cfile->fid.volatile_fid,
509 FSCTL_SET_REPARSE_POINT,
510 in_iov[i].iov_base,
511 in_iov[i].iov_len, 0);
512 } else {
513 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst],
514 COMPOUND_FID, COMPOUND_FID,
515 FSCTL_SET_REPARSE_POINT,
516 in_iov[i].iov_base,
517 in_iov[i].iov_len, 0);
518 }
519 if (!rc && (!cfile || num_rqst > 1)) {
520 smb2_set_next_command(tcon, &rqst[num_rqst]);
521 smb2_set_related(&rqst[num_rqst]);
522 } else if (rc) {
523 goto finished;
524 }
525 num_rqst++;
526 trace_smb3_set_reparse_compound_enter(xid, tcon->tid,
527 ses->Suid, full_path);
528 break;
529 case SMB2_OP_GET_REPARSE:
530 rqst[num_rqst].rq_iov = vars->io_iov;
531 rqst[num_rqst].rq_nvec = ARRAY_SIZE(vars->io_iov);
532
533 if (cfile) {
534 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst],
535 cfile->fid.persistent_fid,
536 cfile->fid.volatile_fid,
537 FSCTL_GET_REPARSE_POINT,
538 NULL, 0, CIFSMaxBufSize);
539 } else {
540 rc = SMB2_ioctl_init(tcon, server, &rqst[num_rqst],
541 COMPOUND_FID, COMPOUND_FID,
542 FSCTL_GET_REPARSE_POINT,
543 NULL, 0, CIFSMaxBufSize);
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_get_reparse_compound_enter(xid, tcon->tid,
553 ses->Suid, full_path);
554 break;
555 case SMB2_OP_QUERY_WSL_EA:
556 rqst[num_rqst].rq_iov = &vars->ea_iov;
557 rqst[num_rqst].rq_nvec = 1;
558
559 if (cfile) {
560 rc = SMB2_query_info_init(tcon, server,
561 &rqst[num_rqst],
562 cfile->fid.persistent_fid,
563 cfile->fid.volatile_fid,
564 FILE_FULL_EA_INFORMATION,
565 SMB2_O_INFO_FILE, 0,
566 SMB2_WSL_MAX_QUERY_EA_RESP_SIZE,
567 sizeof(wsl_query_eas),
568 (void *)wsl_query_eas);
569 } else {
570 rc = SMB2_query_info_init(tcon, server,
571 &rqst[num_rqst],
572 COMPOUND_FID,
573 COMPOUND_FID,
574 FILE_FULL_EA_INFORMATION,
575 SMB2_O_INFO_FILE, 0,
576 SMB2_WSL_MAX_QUERY_EA_RESP_SIZE,
577 sizeof(wsl_query_eas),
578 (void *)wsl_query_eas);
579 }
580 if (!rc && (!cfile || num_rqst > 1)) {
581 smb2_set_next_command(tcon, &rqst[num_rqst]);
582 smb2_set_related(&rqst[num_rqst]);
583 } else if (rc) {
584 goto finished;
585 }
586 num_rqst++;
587 trace_smb3_query_wsl_ea_compound_enter(xid, tcon->tid,
588 ses->Suid, full_path);
589 break;
590 default:
591 cifs_dbg(VFS, "Invalid command\n");
592 rc = -EINVAL;
593 }
594 }
595 if (rc)
596 goto finished;
597
598 /* We already have a handle so we can skip the close */
599 if (cfile)
600 goto after_close;
601 /* Close */
602 flags |= CIFS_CP_CREATE_CLOSE_OP;
603 rqst[num_rqst].rq_iov = &vars->close_iov;
604 rqst[num_rqst].rq_nvec = 1;
605 rc = SMB2_close_init(tcon, server,
606 &rqst[num_rqst], COMPOUND_FID,
607 COMPOUND_FID, false);
608 smb2_set_related(&rqst[num_rqst]);
609 if (rc)
610 goto finished;
611 after_close:
612 num_rqst++;
613
614 if (cfile) {
615 if (retries)
616 for (i = 1; i < num_rqst - 2; i++)
617 smb2_set_replay(server, &rqst[i]);
618
619 rc = compound_send_recv(xid, ses, server,
620 flags, num_rqst - 2,
621 &rqst[1], &resp_buftype[1],
622 &rsp_iov[1]);
623 } else {
624 if (retries)
625 for (i = 0; i < num_rqst; i++)
626 smb2_set_replay(server, &rqst[i]);
627
628 rc = compound_send_recv(xid, ses, server,
629 flags, num_rqst,
630 rqst, resp_buftype,
631 rsp_iov);
632 }
633
634 finished:
635 num_rqst = 0;
636 SMB2_open_free(&rqst[num_rqst++]);
637 if (rc == -EREMCHG) {
638 pr_warn_once("server share %s deleted\n", tcon->tree_name);
639 tcon->need_reconnect = true;
640 }
641
642 tmp_rc = rc;
643 for (i = 0; i < num_cmds; i++) {
644 char *buf = rsp_iov[i + i].iov_base;
645
646 if (buf && resp_buftype[i + 1] != CIFS_NO_BUFFER)
647 rc = server->ops->map_error(buf, false);
648 else
649 rc = tmp_rc;
650 switch (cmds[i]) {
651 case SMB2_OP_QUERY_INFO:
652 idata = in_iov[i].iov_base;
653 idata->contains_posix_file_info = false;
654 if (rc == 0 && cfile && cfile->symlink_target) {
655 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
656 if (!idata->symlink_target)
657 rc = -ENOMEM;
658 }
659 if (rc == 0) {
660 qi_rsp = (struct smb2_query_info_rsp *)
661 rsp_iov[i + 1].iov_base;
662 rc = smb2_validate_and_copy_iov(
663 le16_to_cpu(qi_rsp->OutputBufferOffset),
664 le32_to_cpu(qi_rsp->OutputBufferLength),
665 &rsp_iov[i + 1], sizeof(idata->fi), (char *)&idata->fi);
666 }
667 SMB2_query_info_free(&rqst[num_rqst++]);
668 if (rc)
669 trace_smb3_query_info_compound_err(xid, tcon->tid,
670 ses->Suid, rc);
671 else
672 trace_smb3_query_info_compound_done(xid, tcon->tid,
673 ses->Suid);
674 break;
675 case SMB2_OP_POSIX_QUERY_INFO:
676 idata = in_iov[i].iov_base;
677 idata->contains_posix_file_info = true;
678 if (rc == 0 && cfile && cfile->symlink_target) {
679 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
680 if (!idata->symlink_target)
681 rc = -ENOMEM;
682 }
683 if (rc == 0) {
684 qi_rsp = (struct smb2_query_info_rsp *)
685 rsp_iov[i + 1].iov_base;
686 rc = smb2_validate_and_copy_iov(
687 le16_to_cpu(qi_rsp->OutputBufferOffset),
688 le32_to_cpu(qi_rsp->OutputBufferLength),
689 &rsp_iov[i + 1], sizeof(idata->posix_fi) /* add SIDs */,
690 (char *)&idata->posix_fi);
691 }
692 if (rc == 0)
693 rc = parse_posix_sids(idata, &rsp_iov[i + 1]);
694
695 SMB2_query_info_free(&rqst[num_rqst++]);
696 if (rc)
697 trace_smb3_posix_query_info_compound_err(xid, tcon->tid,
698 ses->Suid, rc);
699 else
700 trace_smb3_posix_query_info_compound_done(xid, tcon->tid,
701 ses->Suid);
702 break;
703 case SMB2_OP_DELETE:
704 if (rc)
705 trace_smb3_delete_err(xid, tcon->tid, ses->Suid, rc);
706 else {
707 /*
708 * If dentry (hence, inode) is NULL, lease break is going to
709 * take care of degrading leases on handles for deleted files.
710 */
711 if (inode)
712 cifs_mark_open_handles_for_deleted_file(inode, full_path);
713 trace_smb3_delete_done(xid, tcon->tid, ses->Suid);
714 }
715 break;
716 case SMB2_OP_MKDIR:
717 if (rc)
718 trace_smb3_mkdir_err(xid, tcon->tid, ses->Suid, rc);
719 else
720 trace_smb3_mkdir_done(xid, tcon->tid, ses->Suid);
721 break;
722 case SMB2_OP_HARDLINK:
723 if (rc)
724 trace_smb3_hardlink_err(xid, tcon->tid, ses->Suid, rc);
725 else
726 trace_smb3_hardlink_done(xid, tcon->tid, ses->Suid);
727 SMB2_set_info_free(&rqst[num_rqst++]);
728 break;
729 case SMB2_OP_RENAME:
730 if (rc)
731 trace_smb3_rename_err(xid, tcon->tid, ses->Suid, rc);
732 else
733 trace_smb3_rename_done(xid, tcon->tid, ses->Suid);
734 SMB2_set_info_free(&rqst[num_rqst++]);
735 break;
736 case SMB2_OP_RMDIR:
737 if (rc)
738 trace_smb3_rmdir_err(xid, tcon->tid, ses->Suid, rc);
739 else
740 trace_smb3_rmdir_done(xid, tcon->tid, ses->Suid);
741 SMB2_set_info_free(&rqst[num_rqst++]);
742 break;
743 case SMB2_OP_SET_EOF:
744 if (rc)
745 trace_smb3_set_eof_err(xid, tcon->tid, ses->Suid, rc);
746 else
747 trace_smb3_set_eof_done(xid, tcon->tid, ses->Suid);
748 SMB2_set_info_free(&rqst[num_rqst++]);
749 break;
750 case SMB2_OP_SET_INFO:
751 if (rc)
752 trace_smb3_set_info_compound_err(xid, tcon->tid,
753 ses->Suid, rc);
754 else
755 trace_smb3_set_info_compound_done(xid, tcon->tid,
756 ses->Suid);
757 SMB2_set_info_free(&rqst[num_rqst++]);
758 break;
759 case SMB2_OP_SET_REPARSE:
760 if (rc) {
761 trace_smb3_set_reparse_compound_err(xid, tcon->tid,
762 ses->Suid, rc);
763 } else {
764 trace_smb3_set_reparse_compound_done(xid, tcon->tid,
765 ses->Suid);
766 }
767 SMB2_ioctl_free(&rqst[num_rqst++]);
768 break;
769 case SMB2_OP_GET_REPARSE:
770 if (!rc) {
771 iov = &rsp_iov[i + 1];
772 idata = in_iov[i].iov_base;
773 idata->reparse.io.iov = *iov;
774 idata->reparse.io.buftype = resp_buftype[i + 1];
775 idata->contains_posix_file_info = false; /* BB VERIFY */
776 rbuf = reparse_buf_ptr(iov);
777 if (IS_ERR(rbuf)) {
778 rc = PTR_ERR(rbuf);
779 trace_smb3_get_reparse_compound_err(xid, tcon->tid,
780 ses->Suid, rc);
781 } else {
782 idata->reparse.tag = le32_to_cpu(rbuf->ReparseTag);
783 trace_smb3_get_reparse_compound_done(xid, tcon->tid,
784 ses->Suid);
785 }
786 memset(iov, 0, sizeof(*iov));
787 resp_buftype[i + 1] = CIFS_NO_BUFFER;
788 } else {
789 trace_smb3_get_reparse_compound_err(xid, tcon->tid,
790 ses->Suid, rc);
791 }
792 SMB2_ioctl_free(&rqst[num_rqst++]);
793 break;
794 case SMB2_OP_QUERY_WSL_EA:
795 if (!rc) {
796 idata = in_iov[i].iov_base;
797 idata->contains_posix_file_info = false;
798 qi_rsp = rsp_iov[i + 1].iov_base;
799 data[0] = (u8 *)qi_rsp + le16_to_cpu(qi_rsp->OutputBufferOffset);
800 size[0] = le32_to_cpu(qi_rsp->OutputBufferLength);
801 rc = check_wsl_eas(&rsp_iov[i + 1]);
802 if (!rc) {
803 memcpy(idata->wsl.eas, data[0], size[0]);
804 idata->wsl.eas_len = size[0];
805 }
806 }
807 if (!rc) {
808 trace_smb3_query_wsl_ea_compound_done(xid, tcon->tid,
809 ses->Suid);
810 } else {
811 trace_smb3_query_wsl_ea_compound_err(xid, tcon->tid,
812 ses->Suid, rc);
813 }
814 SMB2_query_info_free(&rqst[num_rqst++]);
815 break;
816 }
817 }
818 SMB2_close_free(&rqst[num_rqst]);
819 rc = tmp_rc;
820
821 num_cmds += 2;
822 if (out_iov && out_buftype) {
823 memcpy(out_iov, rsp_iov, num_cmds * sizeof(*out_iov));
824 memcpy(out_buftype, resp_buftype,
825 num_cmds * sizeof(*out_buftype));
826 } else {
827 for (i = 0; i < num_cmds; i++)
828 free_rsp_buf(resp_buftype[i], rsp_iov[i].iov_base);
829 }
830 num_cmds -= 2; /* correct num_cmds as there could be a retry */
831 kfree(vars);
832
833 if (is_replayable_error(rc) &&
834 smb2_should_replay(tcon, &retries, &cur_sleep))
835 goto replay_again;
836
837 if (cfile)
838 cifsFileInfo_put(cfile);
839
840 return rc;
841 }
842
parse_create_response(struct cifs_open_info_data * data,struct cifs_sb_info * cifs_sb,const char * full_path,const struct kvec * iov)843 static int parse_create_response(struct cifs_open_info_data *data,
844 struct cifs_sb_info *cifs_sb,
845 const char *full_path,
846 const struct kvec *iov)
847 {
848 struct smb2_create_rsp *rsp = iov->iov_base;
849 bool reparse_point = false;
850 u32 tag = 0;
851 int rc = 0;
852
853 switch (rsp->hdr.Status) {
854 case STATUS_IO_REPARSE_TAG_NOT_HANDLED:
855 reparse_point = true;
856 break;
857 case STATUS_STOPPED_ON_SYMLINK:
858 rc = smb2_parse_symlink_response(cifs_sb, iov,
859 full_path,
860 &data->symlink_target);
861 if (rc)
862 return rc;
863 tag = IO_REPARSE_TAG_SYMLINK;
864 reparse_point = true;
865 break;
866 case STATUS_SUCCESS:
867 reparse_point = !!(rsp->Flags & SMB2_CREATE_FLAG_REPARSEPOINT);
868 break;
869 }
870 data->reparse_point = reparse_point;
871 data->reparse.tag = tag;
872 return rc;
873 }
874
875 /* 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)876 static bool ea_unsupported(int *cmds, int num_cmds,
877 struct kvec *out_iov, int *out_buftype)
878 {
879 int i;
880
881 if (cmds[num_cmds - 1] != SMB2_OP_QUERY_WSL_EA)
882 return false;
883
884 for (i = 1; i < num_cmds - 1; i++) {
885 struct smb2_hdr *hdr = out_iov[i].iov_base;
886
887 if (out_buftype[i] == CIFS_NO_BUFFER || !hdr ||
888 hdr->Status != STATUS_SUCCESS)
889 return false;
890 }
891 return true;
892 }
893
free_rsp_iov(struct kvec * iovs,int * buftype,int count)894 static inline void free_rsp_iov(struct kvec *iovs, int *buftype, int count)
895 {
896 int i;
897
898 for (i = 0; i < count; i++) {
899 free_rsp_buf(buftype[i], iovs[i].iov_base);
900 memset(&iovs[i], 0, sizeof(*iovs));
901 buftype[i] = CIFS_NO_BUFFER;
902 }
903 }
904
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)905 int smb2_query_path_info(const unsigned int xid,
906 struct cifs_tcon *tcon,
907 struct cifs_sb_info *cifs_sb,
908 const char *full_path,
909 struct cifs_open_info_data *data)
910 {
911 struct kvec in_iov[3], out_iov[5] = {};
912 struct cached_fid *cfid = NULL;
913 struct cifs_open_parms oparms;
914 struct cifsFileInfo *cfile;
915 __u32 create_options = 0;
916 int out_buftype[5] = {};
917 struct smb2_hdr *hdr;
918 int num_cmds = 0;
919 int cmds[3];
920 bool islink;
921 int rc, rc2;
922
923 data->adjust_tz = false;
924 data->reparse_point = false;
925
926 /*
927 * BB TODO: Add support for using cached root handle in SMB3.1.1 POSIX.
928 * Create SMB2_query_posix_info worker function to do non-compounded
929 * query when we already have an open file handle for this. For now this
930 * is fast enough (always using the compounded version).
931 */
932 if (!tcon->posix_extensions) {
933 if (*full_path) {
934 rc = -ENOENT;
935 } else {
936 rc = open_cached_dir(xid, tcon, full_path,
937 cifs_sb, false, &cfid);
938 }
939 /* If it is a root and its handle is cached then use it */
940 if (!rc) {
941 if (cfid->file_all_info_is_valid) {
942 memcpy(&data->fi, &cfid->file_all_info,
943 sizeof(data->fi));
944 } else {
945 rc = SMB2_query_info(xid, tcon,
946 cfid->fid.persistent_fid,
947 cfid->fid.volatile_fid,
948 &data->fi);
949 }
950 close_cached_dir(cfid);
951 return rc;
952 }
953 cmds[num_cmds++] = SMB2_OP_QUERY_INFO;
954 } else {
955 cmds[num_cmds++] = SMB2_OP_POSIX_QUERY_INFO;
956 }
957
958 in_iov[0].iov_base = data;
959 in_iov[0].iov_len = sizeof(*data);
960 in_iov[1] = in_iov[0];
961 in_iov[2] = in_iov[0];
962
963 cifs_get_readable_path(tcon, full_path, &cfile);
964 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, FILE_READ_ATTRIBUTES,
965 FILE_OPEN, create_options, ACL_NO_MODE);
966 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
967 &oparms, in_iov, cmds, num_cmds,
968 cfile, out_iov, out_buftype, NULL);
969 hdr = out_iov[0].iov_base;
970 /*
971 * If first iov is unset, then SMB session was dropped or we've got a
972 * cached open file (@cfile).
973 */
974 if (!hdr || out_buftype[0] == CIFS_NO_BUFFER)
975 goto out;
976
977 switch (rc) {
978 case 0:
979 rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]);
980 break;
981 case -EOPNOTSUPP:
982 /*
983 * BB TODO: When support for special files added to Samba
984 * re-verify this path.
985 */
986 rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]);
987 if (rc || !data->reparse_point)
988 goto out;
989
990 /*
991 * Skip SMB2_OP_GET_REPARSE if symlink already parsed in create
992 * response.
993 */
994 if (data->reparse.tag != IO_REPARSE_TAG_SYMLINK)
995 cmds[num_cmds++] = SMB2_OP_GET_REPARSE;
996 if (!tcon->posix_extensions)
997 cmds[num_cmds++] = SMB2_OP_QUERY_WSL_EA;
998
999 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path,
1000 FILE_READ_ATTRIBUTES |
1001 FILE_READ_EA | SYNCHRONIZE,
1002 FILE_OPEN, create_options |
1003 OPEN_REPARSE_POINT, ACL_NO_MODE);
1004 cifs_get_readable_path(tcon, full_path, &cfile);
1005 free_rsp_iov(out_iov, out_buftype, ARRAY_SIZE(out_iov));
1006 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
1007 &oparms, in_iov, cmds, num_cmds,
1008 cfile, out_iov, out_buftype, NULL);
1009 if (rc && ea_unsupported(cmds, num_cmds,
1010 out_iov, out_buftype)) {
1011 if (data->reparse.tag != IO_REPARSE_TAG_LX_BLK &&
1012 data->reparse.tag != IO_REPARSE_TAG_LX_CHR)
1013 rc = 0;
1014 else
1015 rc = -EOPNOTSUPP;
1016 }
1017
1018 if (data->reparse.tag == IO_REPARSE_TAG_SYMLINK && !rc) {
1019 bool directory = le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY;
1020 rc = smb2_fix_symlink_target_type(&data->symlink_target, directory, cifs_sb);
1021 }
1022 break;
1023 case -EREMOTE:
1024 break;
1025 default:
1026 if (hdr->Status != STATUS_OBJECT_NAME_INVALID)
1027 break;
1028 rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb,
1029 full_path, &islink);
1030 if (rc2) {
1031 rc = rc2;
1032 goto out;
1033 }
1034 if (islink)
1035 rc = -EREMOTE;
1036 }
1037
1038 out:
1039 free_rsp_iov(out_iov, out_buftype, ARRAY_SIZE(out_iov));
1040 return rc;
1041 }
1042
1043 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)1044 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
1045 struct cifs_tcon *tcon, const char *name,
1046 struct cifs_sb_info *cifs_sb)
1047 {
1048 struct cifs_open_parms oparms;
1049
1050 oparms = CIFS_OPARMS(cifs_sb, tcon, name, FILE_WRITE_ATTRIBUTES,
1051 FILE_CREATE, CREATE_NOT_FILE, mode);
1052 return smb2_compound_op(xid, tcon, cifs_sb,
1053 name, &oparms, NULL,
1054 &(int){SMB2_OP_MKDIR}, 1,
1055 NULL, NULL, NULL, NULL);
1056 }
1057
1058 void
smb2_mkdir_setinfo(struct inode * inode,const char * name,struct cifs_sb_info * cifs_sb,struct cifs_tcon * tcon,const unsigned int xid)1059 smb2_mkdir_setinfo(struct inode *inode, const char *name,
1060 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
1061 const unsigned int xid)
1062 {
1063 struct cifs_open_parms oparms;
1064 FILE_BASIC_INFO data = {};
1065 struct cifsInodeInfo *cifs_i;
1066 struct cifsFileInfo *cfile;
1067 struct kvec in_iov;
1068 u32 dosattrs;
1069 int tmprc;
1070
1071 in_iov.iov_base = &data;
1072 in_iov.iov_len = sizeof(data);
1073 cifs_i = CIFS_I(inode);
1074 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
1075 data.Attributes = cpu_to_le32(dosattrs);
1076 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
1077 oparms = CIFS_OPARMS(cifs_sb, tcon, name, FILE_WRITE_ATTRIBUTES,
1078 FILE_CREATE, CREATE_NOT_FILE, ACL_NO_MODE);
1079 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
1080 &oparms, &in_iov,
1081 &(int){SMB2_OP_SET_INFO}, 1,
1082 cfile, NULL, NULL, NULL);
1083 if (tmprc == 0)
1084 cifs_i->cifsAttrs = dosattrs;
1085 }
1086
1087 int
smb2_rmdir(const unsigned int xid,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb)1088 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1089 struct cifs_sb_info *cifs_sb)
1090 {
1091 struct cifs_open_parms oparms;
1092
1093 drop_cached_dir_by_name(xid, tcon, name, cifs_sb);
1094 oparms = CIFS_OPARMS(cifs_sb, tcon, name, DELETE,
1095 FILE_OPEN, CREATE_NOT_FILE, ACL_NO_MODE);
1096 return smb2_compound_op(xid, tcon, cifs_sb,
1097 name, &oparms, NULL,
1098 &(int){SMB2_OP_RMDIR}, 1,
1099 NULL, NULL, NULL, NULL);
1100 }
1101
1102 int
smb2_unlink(const unsigned int xid,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb,struct dentry * dentry)1103 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1104 struct cifs_sb_info *cifs_sb, struct dentry *dentry)
1105 {
1106 struct cifs_open_parms oparms;
1107
1108 oparms = CIFS_OPARMS(cifs_sb, tcon, name,
1109 DELETE, FILE_OPEN,
1110 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
1111 ACL_NO_MODE);
1112 int rc = smb2_compound_op(xid, tcon, cifs_sb, name, &oparms,
1113 NULL, &(int){SMB2_OP_DELETE}, 1,
1114 NULL, NULL, NULL, dentry);
1115 if (rc == -EINVAL) {
1116 cifs_dbg(FYI, "invalid lease key, resending request without lease");
1117 rc = smb2_compound_op(xid, tcon, cifs_sb, name, &oparms,
1118 NULL, &(int){SMB2_OP_DELETE}, 1,
1119 NULL, NULL, NULL, NULL);
1120 }
1121 return rc;
1122 }
1123
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)1124 static int smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
1125 const char *from_name, const char *to_name,
1126 struct cifs_sb_info *cifs_sb,
1127 __u32 create_options, __u32 access,
1128 int command, struct cifsFileInfo *cfile,
1129 struct dentry *dentry)
1130 {
1131 struct cifs_open_parms oparms;
1132 struct kvec in_iov;
1133 __le16 *smb2_to_name = NULL;
1134 int rc;
1135
1136 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
1137 if (smb2_to_name == NULL) {
1138 rc = -ENOMEM;
1139 goto smb2_rename_path;
1140 }
1141 in_iov.iov_base = smb2_to_name;
1142 in_iov.iov_len = 2 * UniStrnlen((wchar_t *)smb2_to_name, PATH_MAX);
1143 oparms = CIFS_OPARMS(cifs_sb, tcon, from_name, access, FILE_OPEN,
1144 create_options, ACL_NO_MODE);
1145 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name,
1146 &oparms, &in_iov, &command, 1,
1147 cfile, NULL, NULL, dentry);
1148 smb2_rename_path:
1149 kfree(smb2_to_name);
1150 return rc;
1151 }
1152
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)1153 int smb2_rename_path(const unsigned int xid,
1154 struct cifs_tcon *tcon,
1155 struct dentry *source_dentry,
1156 const char *from_name, const char *to_name,
1157 struct cifs_sb_info *cifs_sb)
1158 {
1159 struct cifsFileInfo *cfile;
1160 __u32 co = file_create_options(source_dentry);
1161
1162 drop_cached_dir_by_name(xid, tcon, from_name, cifs_sb);
1163 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
1164
1165 int rc = smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
1166 co, DELETE, SMB2_OP_RENAME, cfile, source_dentry);
1167 if (rc == -EINVAL) {
1168 cifs_dbg(FYI, "invalid lease key, resending request without lease");
1169 cifs_get_writable_path(tcon, from_name,
1170 FIND_WR_WITH_DELETE, &cfile);
1171 rc = smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
1172 co, DELETE, SMB2_OP_RENAME, cfile, NULL);
1173 }
1174 return rc;
1175 }
1176
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)1177 int smb2_create_hardlink(const unsigned int xid,
1178 struct cifs_tcon *tcon,
1179 struct dentry *source_dentry,
1180 const char *from_name, const char *to_name,
1181 struct cifs_sb_info *cifs_sb)
1182 {
1183 __u32 co = file_create_options(source_dentry);
1184
1185 return smb2_set_path_attr(xid, tcon, from_name, to_name,
1186 cifs_sb, co, FILE_READ_ATTRIBUTES,
1187 SMB2_OP_HARDLINK, NULL, NULL);
1188 }
1189
1190 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)1191 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
1192 const char *full_path, __u64 size,
1193 struct cifs_sb_info *cifs_sb, bool set_alloc,
1194 struct dentry *dentry)
1195 {
1196 struct cifs_open_parms oparms;
1197 struct cifsFileInfo *cfile;
1198 struct kvec in_iov;
1199 __le64 eof = cpu_to_le64(size);
1200 int rc;
1201
1202 in_iov.iov_base = &eof;
1203 in_iov.iov_len = sizeof(eof);
1204 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
1205
1206 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, FILE_WRITE_DATA,
1207 FILE_OPEN, 0, ACL_NO_MODE);
1208 rc = smb2_compound_op(xid, tcon, cifs_sb,
1209 full_path, &oparms, &in_iov,
1210 &(int){SMB2_OP_SET_EOF}, 1,
1211 cfile, NULL, NULL, dentry);
1212 if (rc == -EINVAL) {
1213 cifs_dbg(FYI, "invalid lease key, resending request without lease");
1214 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
1215 rc = smb2_compound_op(xid, tcon, cifs_sb,
1216 full_path, &oparms, &in_iov,
1217 &(int){SMB2_OP_SET_EOF}, 1,
1218 cfile, NULL, NULL, NULL);
1219 }
1220 return rc;
1221 }
1222
1223 int
smb2_set_file_info(struct inode * inode,const char * full_path,FILE_BASIC_INFO * buf,const unsigned int xid)1224 smb2_set_file_info(struct inode *inode, const char *full_path,
1225 FILE_BASIC_INFO *buf, const unsigned int xid)
1226 {
1227 struct cifs_open_parms oparms;
1228 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1229 struct tcon_link *tlink;
1230 struct cifs_tcon *tcon;
1231 struct cifsFileInfo *cfile;
1232 struct kvec in_iov = { .iov_base = buf, .iov_len = sizeof(*buf), };
1233 int rc;
1234
1235 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
1236 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
1237 (buf->Attributes == 0))
1238 return 0; /* would be a no op, no sense sending this */
1239
1240 tlink = cifs_sb_tlink(cifs_sb);
1241 if (IS_ERR(tlink))
1242 return PTR_ERR(tlink);
1243 tcon = tlink_tcon(tlink);
1244
1245 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
1246 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, FILE_WRITE_ATTRIBUTES,
1247 FILE_OPEN, 0, ACL_NO_MODE);
1248 rc = smb2_compound_op(xid, tcon, cifs_sb,
1249 full_path, &oparms, &in_iov,
1250 &(int){SMB2_OP_SET_INFO}, 1,
1251 cfile, NULL, NULL, NULL);
1252 cifs_put_tlink(tlink);
1253 return rc;
1254 }
1255
smb2_get_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)1256 struct inode *smb2_get_reparse_inode(struct cifs_open_info_data *data,
1257 struct super_block *sb,
1258 const unsigned int xid,
1259 struct cifs_tcon *tcon,
1260 const char *full_path,
1261 bool directory,
1262 struct kvec *reparse_iov,
1263 struct kvec *xattr_iov)
1264 {
1265 struct cifs_open_parms oparms;
1266 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1267 struct cifsFileInfo *cfile;
1268 struct inode *new = NULL;
1269 int out_buftype[4] = {};
1270 struct kvec out_iov[4] = {};
1271 struct kvec in_iov[2];
1272 int cmds[2];
1273 int rc;
1274 int i;
1275
1276 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path,
1277 SYNCHRONIZE | DELETE |
1278 FILE_READ_ATTRIBUTES |
1279 FILE_WRITE_ATTRIBUTES,
1280 FILE_CREATE,
1281 (directory ? CREATE_NOT_FILE : CREATE_NOT_DIR) | OPEN_REPARSE_POINT,
1282 ACL_NO_MODE);
1283 if (xattr_iov)
1284 oparms.ea_cctx = xattr_iov;
1285
1286 cmds[0] = SMB2_OP_SET_REPARSE;
1287 in_iov[0] = *reparse_iov;
1288 in_iov[1].iov_base = data;
1289 in_iov[1].iov_len = sizeof(*data);
1290
1291 if (tcon->posix_extensions) {
1292 cmds[1] = SMB2_OP_POSIX_QUERY_INFO;
1293 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
1294 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, &oparms,
1295 in_iov, cmds, 2, cfile, out_iov, out_buftype, NULL);
1296 if (!rc) {
1297 rc = smb311_posix_get_inode_info(&new, full_path,
1298 data, sb, xid);
1299 }
1300 } else {
1301 cmds[1] = SMB2_OP_QUERY_INFO;
1302 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
1303 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, &oparms,
1304 in_iov, cmds, 2, cfile, out_iov, out_buftype, NULL);
1305 if (!rc) {
1306 rc = cifs_get_inode_info(&new, full_path,
1307 data, sb, xid, NULL);
1308 }
1309 }
1310
1311
1312 /*
1313 * If CREATE was successful but SMB2_OP_SET_REPARSE failed then
1314 * remove the intermediate object created by CREATE. Otherwise
1315 * empty object stay on the server when reparse call failed.
1316 */
1317 if (rc &&
1318 out_iov[0].iov_base != NULL && out_buftype[0] != CIFS_NO_BUFFER &&
1319 ((struct smb2_hdr *)out_iov[0].iov_base)->Status == STATUS_SUCCESS &&
1320 (out_iov[1].iov_base == NULL || out_buftype[1] == CIFS_NO_BUFFER ||
1321 ((struct smb2_hdr *)out_iov[1].iov_base)->Status != STATUS_SUCCESS))
1322 smb2_unlink(xid, tcon, full_path, cifs_sb, NULL);
1323
1324 for (i = 0; i < ARRAY_SIZE(out_buftype); i++)
1325 free_rsp_buf(out_buftype[i], out_iov[i].iov_base);
1326
1327 return rc ? ERR_PTR(rc) : new;
1328 }
1329
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)1330 int smb2_query_reparse_point(const unsigned int xid,
1331 struct cifs_tcon *tcon,
1332 struct cifs_sb_info *cifs_sb,
1333 const char *full_path,
1334 u32 *tag, struct kvec *rsp,
1335 int *rsp_buftype)
1336 {
1337 struct cifs_open_parms oparms;
1338 struct cifs_open_info_data data = {};
1339 struct cifsFileInfo *cfile;
1340 struct kvec in_iov = { .iov_base = &data, .iov_len = sizeof(data), };
1341 int rc;
1342
1343 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1344
1345 cifs_get_readable_path(tcon, full_path, &cfile);
1346 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path,
1347 FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE,
1348 FILE_OPEN, OPEN_REPARSE_POINT, ACL_NO_MODE);
1349 rc = smb2_compound_op(xid, tcon, cifs_sb,
1350 full_path, &oparms, &in_iov,
1351 &(int){SMB2_OP_GET_REPARSE}, 1,
1352 cfile, NULL, NULL, NULL);
1353 if (rc)
1354 goto out;
1355
1356 *tag = data.reparse.tag;
1357 *rsp = data.reparse.io.iov;
1358 *rsp_buftype = data.reparse.io.buftype;
1359 memset(&data.reparse.io.iov, 0, sizeof(data.reparse.io.iov));
1360 data.reparse.io.buftype = CIFS_NO_BUFFER;
1361 out:
1362 cifs_free_open_info(&data);
1363 return rc;
1364 }
1365