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