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