xref: /openbmc/linux/fs/smb/client/smb2inode.c (revision 657c45b3)
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 "smb2status.h"
28 
29 static void
30 free_set_inf_compound(struct smb_rqst *rqst)
31 {
32 	if (rqst[1].rq_iov)
33 		SMB2_set_info_free(&rqst[1]);
34 	if (rqst[2].rq_iov)
35 		SMB2_close_free(&rqst[2]);
36 }
37 
38 
39 struct cop_vars {
40 	struct cifs_open_parms oparms;
41 	struct kvec rsp_iov[3];
42 	struct smb_rqst rqst[3];
43 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
44 	struct kvec qi_iov[1];
45 	struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
46 	struct kvec close_iov[1];
47 	struct smb2_file_rename_info rename_info;
48 	struct smb2_file_link_info link_info;
49 };
50 
51 /*
52  * note: If cfile is passed, the reference to it is dropped here.
53  * So make sure that you do not reuse cfile after return from this func.
54  *
55  * If passing @err_iov and @err_buftype, ensure to make them both large enough (>= 3) to hold all
56  * error responses.  Caller is also responsible for freeing them up.
57  */
58 static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
59 			    struct cifs_sb_info *cifs_sb, const char *full_path,
60 			    __u32 desired_access, __u32 create_disposition, __u32 create_options,
61 			    umode_t mode, void *ptr, int command, struct cifsFileInfo *cfile,
62 			    __u8 **extbuf, size_t *extbuflen,
63 			    struct kvec *err_iov, int *err_buftype)
64 {
65 	struct cop_vars *vars = NULL;
66 	struct kvec *rsp_iov;
67 	struct smb_rqst *rqst;
68 	int rc;
69 	__le16 *utf16_path = NULL;
70 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
71 	struct cifs_fid fid;
72 	struct cifs_ses *ses = tcon->ses;
73 	struct TCP_Server_Info *server;
74 	int num_rqst = 0;
75 	int resp_buftype[3];
76 	struct smb2_query_info_rsp *qi_rsp = NULL;
77 	struct cifs_open_info_data *idata;
78 	int flags = 0;
79 	__u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
80 	unsigned int size[2];
81 	void *data[2];
82 	int len;
83 
84 	vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
85 	if (vars == NULL)
86 		return -ENOMEM;
87 	rqst = &vars->rqst[0];
88 	rsp_iov = &vars->rsp_iov[0];
89 
90 	server = cifs_pick_channel(ses);
91 
92 	if (smb3_encryption_required(tcon))
93 		flags |= CIFS_TRANSFORM_REQ;
94 
95 	resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
96 
97 	/* We already have a handle so we can skip the open */
98 	if (cfile)
99 		goto after_open;
100 
101 	/* Open */
102 	utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
103 	if (!utf16_path) {
104 		rc = -ENOMEM;
105 		goto finished;
106 	}
107 
108 	vars->oparms = (struct cifs_open_parms) {
109 		.tcon = tcon,
110 		.path = full_path,
111 		.desired_access = desired_access,
112 		.disposition = create_disposition,
113 		.create_options = cifs_create_options(cifs_sb, create_options),
114 		.fid = &fid,
115 		.mode = mode,
116 		.cifs_sb = cifs_sb,
117 	};
118 
119 	rqst[num_rqst].rq_iov = &vars->open_iov[0];
120 	rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
121 	rc = SMB2_open_init(tcon, server,
122 			    &rqst[num_rqst], &oplock, &vars->oparms,
123 			    utf16_path);
124 	kfree(utf16_path);
125 	if (rc)
126 		goto finished;
127 
128 	smb2_set_next_command(tcon, &rqst[num_rqst]);
129  after_open:
130 	num_rqst++;
131 	rc = 0;
132 
133 	/* Operation */
134 	switch (command) {
135 	case SMB2_OP_QUERY_INFO:
136 		rqst[num_rqst].rq_iov = &vars->qi_iov[0];
137 		rqst[num_rqst].rq_nvec = 1;
138 
139 		if (cfile)
140 			rc = SMB2_query_info_init(tcon, server,
141 				&rqst[num_rqst],
142 				cfile->fid.persistent_fid,
143 				cfile->fid.volatile_fid,
144 				FILE_ALL_INFORMATION,
145 				SMB2_O_INFO_FILE, 0,
146 				sizeof(struct smb2_file_all_info) +
147 					  PATH_MAX * 2, 0, NULL);
148 		else {
149 			rc = SMB2_query_info_init(tcon, server,
150 				&rqst[num_rqst],
151 				COMPOUND_FID,
152 				COMPOUND_FID,
153 				FILE_ALL_INFORMATION,
154 				SMB2_O_INFO_FILE, 0,
155 				sizeof(struct smb2_file_all_info) +
156 					  PATH_MAX * 2, 0, NULL);
157 			if (!rc) {
158 				smb2_set_next_command(tcon, &rqst[num_rqst]);
159 				smb2_set_related(&rqst[num_rqst]);
160 			}
161 		}
162 
163 		if (rc)
164 			goto finished;
165 		num_rqst++;
166 		trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
167 						     full_path);
168 		break;
169 	case SMB2_OP_POSIX_QUERY_INFO:
170 		rqst[num_rqst].rq_iov = &vars->qi_iov[0];
171 		rqst[num_rqst].rq_nvec = 1;
172 
173 		if (cfile)
174 			rc = SMB2_query_info_init(tcon, server,
175 				&rqst[num_rqst],
176 				cfile->fid.persistent_fid,
177 				cfile->fid.volatile_fid,
178 				SMB_FIND_FILE_POSIX_INFO,
179 				SMB2_O_INFO_FILE, 0,
180 				/* TBD: fix following to allow for longer SIDs */
181 				sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
182 				(sizeof(struct cifs_sid) * 2), 0, NULL);
183 		else {
184 			rc = SMB2_query_info_init(tcon, server,
185 				&rqst[num_rqst],
186 				COMPOUND_FID,
187 				COMPOUND_FID,
188 				SMB_FIND_FILE_POSIX_INFO,
189 				SMB2_O_INFO_FILE, 0,
190 				sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
191 				(sizeof(struct cifs_sid) * 2), 0, NULL);
192 			if (!rc) {
193 				smb2_set_next_command(tcon, &rqst[num_rqst]);
194 				smb2_set_related(&rqst[num_rqst]);
195 			}
196 		}
197 
198 		if (rc)
199 			goto finished;
200 		num_rqst++;
201 		trace_smb3_posix_query_info_compound_enter(xid, ses->Suid, tcon->tid, full_path);
202 		break;
203 	case SMB2_OP_DELETE:
204 		trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
205 		break;
206 	case SMB2_OP_MKDIR:
207 		/*
208 		 * Directories are created through parameters in the
209 		 * SMB2_open() call.
210 		 */
211 		trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
212 		break;
213 	case SMB2_OP_RMDIR:
214 		rqst[num_rqst].rq_iov = &vars->si_iov[0];
215 		rqst[num_rqst].rq_nvec = 1;
216 
217 		size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
218 		data[0] = &delete_pending[0];
219 
220 		rc = SMB2_set_info_init(tcon, server,
221 					&rqst[num_rqst], COMPOUND_FID,
222 					COMPOUND_FID, current->tgid,
223 					FILE_DISPOSITION_INFORMATION,
224 					SMB2_O_INFO_FILE, 0, data, size);
225 		if (rc)
226 			goto finished;
227 		smb2_set_next_command(tcon, &rqst[num_rqst]);
228 		smb2_set_related(&rqst[num_rqst++]);
229 		trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
230 		break;
231 	case SMB2_OP_SET_EOF:
232 		rqst[num_rqst].rq_iov = &vars->si_iov[0];
233 		rqst[num_rqst].rq_nvec = 1;
234 
235 		size[0] = 8; /* sizeof __le64 */
236 		data[0] = ptr;
237 
238 		if (cfile) {
239 			rc = SMB2_set_info_init(tcon, server,
240 						&rqst[num_rqst],
241 						cfile->fid.persistent_fid,
242 						cfile->fid.volatile_fid,
243 						current->tgid,
244 						FILE_END_OF_FILE_INFORMATION,
245 						SMB2_O_INFO_FILE, 0,
246 						data, size);
247 		} else {
248 			rc = SMB2_set_info_init(tcon, server,
249 						&rqst[num_rqst],
250 						COMPOUND_FID,
251 						COMPOUND_FID,
252 						current->tgid,
253 						FILE_END_OF_FILE_INFORMATION,
254 						SMB2_O_INFO_FILE, 0,
255 						data, size);
256 			if (!rc) {
257 				smb2_set_next_command(tcon, &rqst[num_rqst]);
258 				smb2_set_related(&rqst[num_rqst]);
259 			}
260 		}
261 		if (rc)
262 			goto finished;
263 		num_rqst++;
264 		trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
265 		break;
266 	case SMB2_OP_SET_INFO:
267 		rqst[num_rqst].rq_iov = &vars->si_iov[0];
268 		rqst[num_rqst].rq_nvec = 1;
269 
270 
271 		size[0] = sizeof(FILE_BASIC_INFO);
272 		data[0] = ptr;
273 
274 		if (cfile)
275 			rc = SMB2_set_info_init(tcon, server,
276 				&rqst[num_rqst],
277 				cfile->fid.persistent_fid,
278 				cfile->fid.volatile_fid, current->tgid,
279 				FILE_BASIC_INFORMATION,
280 				SMB2_O_INFO_FILE, 0, data, size);
281 		else {
282 			rc = SMB2_set_info_init(tcon, server,
283 				&rqst[num_rqst],
284 				COMPOUND_FID,
285 				COMPOUND_FID, current->tgid,
286 				FILE_BASIC_INFORMATION,
287 				SMB2_O_INFO_FILE, 0, data, size);
288 			if (!rc) {
289 				smb2_set_next_command(tcon, &rqst[num_rqst]);
290 				smb2_set_related(&rqst[num_rqst]);
291 			}
292 		}
293 
294 		if (rc)
295 			goto finished;
296 		num_rqst++;
297 		trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
298 						   full_path);
299 		break;
300 	case SMB2_OP_RENAME:
301 		rqst[num_rqst].rq_iov = &vars->si_iov[0];
302 		rqst[num_rqst].rq_nvec = 2;
303 
304 		len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
305 
306 		vars->rename_info.ReplaceIfExists = 1;
307 		vars->rename_info.RootDirectory = 0;
308 		vars->rename_info.FileNameLength = cpu_to_le32(len);
309 
310 		size[0] = sizeof(struct smb2_file_rename_info);
311 		data[0] = &vars->rename_info;
312 
313 		size[1] = len + 2 /* null */;
314 		data[1] = (__le16 *)ptr;
315 
316 		if (cfile)
317 			rc = SMB2_set_info_init(tcon, server,
318 						&rqst[num_rqst],
319 						cfile->fid.persistent_fid,
320 						cfile->fid.volatile_fid,
321 					current->tgid, FILE_RENAME_INFORMATION,
322 					SMB2_O_INFO_FILE, 0, data, size);
323 		else {
324 			rc = SMB2_set_info_init(tcon, server,
325 					&rqst[num_rqst],
326 					COMPOUND_FID, COMPOUND_FID,
327 					current->tgid, FILE_RENAME_INFORMATION,
328 					SMB2_O_INFO_FILE, 0, data, size);
329 			if (!rc) {
330 				smb2_set_next_command(tcon, &rqst[num_rqst]);
331 				smb2_set_related(&rqst[num_rqst]);
332 			}
333 		}
334 		if (rc)
335 			goto finished;
336 		num_rqst++;
337 		trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
338 		break;
339 	case SMB2_OP_HARDLINK:
340 		rqst[num_rqst].rq_iov = &vars->si_iov[0];
341 		rqst[num_rqst].rq_nvec = 2;
342 
343 		len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
344 
345 		vars->link_info.ReplaceIfExists = 0;
346 		vars->link_info.RootDirectory = 0;
347 		vars->link_info.FileNameLength = cpu_to_le32(len);
348 
349 		size[0] = sizeof(struct smb2_file_link_info);
350 		data[0] = &vars->link_info;
351 
352 		size[1] = len + 2 /* null */;
353 		data[1] = (__le16 *)ptr;
354 
355 		rc = SMB2_set_info_init(tcon, server,
356 					&rqst[num_rqst], COMPOUND_FID,
357 					COMPOUND_FID, current->tgid,
358 					FILE_LINK_INFORMATION,
359 					SMB2_O_INFO_FILE, 0, data, size);
360 		if (rc)
361 			goto finished;
362 		smb2_set_next_command(tcon, &rqst[num_rqst]);
363 		smb2_set_related(&rqst[num_rqst++]);
364 		trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
365 		break;
366 	default:
367 		cifs_dbg(VFS, "Invalid command\n");
368 		rc = -EINVAL;
369 	}
370 	if (rc)
371 		goto finished;
372 
373 	/* We already have a handle so we can skip the close */
374 	if (cfile)
375 		goto after_close;
376 	/* Close */
377 	flags |= CIFS_CP_CREATE_CLOSE_OP;
378 	rqst[num_rqst].rq_iov = &vars->close_iov[0];
379 	rqst[num_rqst].rq_nvec = 1;
380 	rc = SMB2_close_init(tcon, server,
381 			     &rqst[num_rqst], COMPOUND_FID,
382 			     COMPOUND_FID, false);
383 	smb2_set_related(&rqst[num_rqst]);
384 	if (rc)
385 		goto finished;
386  after_close:
387 	num_rqst++;
388 
389 	if (cfile) {
390 		rc = compound_send_recv(xid, ses, server,
391 					flags, num_rqst - 2,
392 					&rqst[1], &resp_buftype[1],
393 					&rsp_iov[1]);
394 	} else
395 		rc = compound_send_recv(xid, ses, server,
396 					flags, num_rqst,
397 					rqst, resp_buftype,
398 					rsp_iov);
399 
400  finished:
401 	SMB2_open_free(&rqst[0]);
402 	if (rc == -EREMCHG) {
403 		pr_warn_once("server share %s deleted\n", tcon->tree_name);
404 		tcon->need_reconnect = true;
405 	}
406 
407 	switch (command) {
408 	case SMB2_OP_QUERY_INFO:
409 		idata = ptr;
410 		if (rc == 0 && cfile && cfile->symlink_target) {
411 			idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
412 			if (!idata->symlink_target)
413 				rc = -ENOMEM;
414 		}
415 		if (rc == 0) {
416 			qi_rsp = (struct smb2_query_info_rsp *)
417 				rsp_iov[1].iov_base;
418 			rc = smb2_validate_and_copy_iov(
419 				le16_to_cpu(qi_rsp->OutputBufferOffset),
420 				le32_to_cpu(qi_rsp->OutputBufferLength),
421 				&rsp_iov[1], sizeof(idata->fi), (char *)&idata->fi);
422 		}
423 		if (rqst[1].rq_iov)
424 			SMB2_query_info_free(&rqst[1]);
425 		if (rqst[2].rq_iov)
426 			SMB2_close_free(&rqst[2]);
427 		if (rc)
428 			trace_smb3_query_info_compound_err(xid,  ses->Suid,
429 						tcon->tid, rc);
430 		else
431 			trace_smb3_query_info_compound_done(xid, ses->Suid,
432 						tcon->tid);
433 		break;
434 	case SMB2_OP_POSIX_QUERY_INFO:
435 		idata = ptr;
436 		if (rc == 0 && cfile && cfile->symlink_target) {
437 			idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
438 			if (!idata->symlink_target)
439 				rc = -ENOMEM;
440 		}
441 		if (rc == 0) {
442 			qi_rsp = (struct smb2_query_info_rsp *)
443 				rsp_iov[1].iov_base;
444 			rc = smb2_validate_and_copy_iov(
445 				le16_to_cpu(qi_rsp->OutputBufferOffset),
446 				le32_to_cpu(qi_rsp->OutputBufferLength),
447 				&rsp_iov[1], sizeof(idata->posix_fi) /* add SIDs */,
448 				(char *)&idata->posix_fi);
449 		}
450 		if (rc == 0) {
451 			unsigned int length = le32_to_cpu(qi_rsp->OutputBufferLength);
452 
453 			if (length > sizeof(idata->posix_fi)) {
454 				char *base = (char *)rsp_iov[1].iov_base +
455 					le16_to_cpu(qi_rsp->OutputBufferOffset) +
456 					sizeof(idata->posix_fi);
457 				*extbuflen = length - sizeof(idata->posix_fi);
458 				*extbuf = kmemdup(base, *extbuflen, GFP_KERNEL);
459 				if (!*extbuf)
460 					rc = -ENOMEM;
461 			} else {
462 				rc = -EINVAL;
463 			}
464 		}
465 		if (rqst[1].rq_iov)
466 			SMB2_query_info_free(&rqst[1]);
467 		if (rqst[2].rq_iov)
468 			SMB2_close_free(&rqst[2]);
469 		if (rc)
470 			trace_smb3_posix_query_info_compound_err(xid,  ses->Suid, tcon->tid, rc);
471 		else
472 			trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
473 		break;
474 	case SMB2_OP_DELETE:
475 		if (rc)
476 			trace_smb3_delete_err(xid,  ses->Suid, tcon->tid, rc);
477 		else
478 			trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
479 		if (rqst[1].rq_iov)
480 			SMB2_close_free(&rqst[1]);
481 		break;
482 	case SMB2_OP_MKDIR:
483 		if (rc)
484 			trace_smb3_mkdir_err(xid,  ses->Suid, tcon->tid, rc);
485 		else
486 			trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
487 		if (rqst[1].rq_iov)
488 			SMB2_close_free(&rqst[1]);
489 		break;
490 	case SMB2_OP_HARDLINK:
491 		if (rc)
492 			trace_smb3_hardlink_err(xid,  ses->Suid, tcon->tid, rc);
493 		else
494 			trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
495 		free_set_inf_compound(rqst);
496 		break;
497 	case SMB2_OP_RENAME:
498 		if (rc)
499 			trace_smb3_rename_err(xid,  ses->Suid, tcon->tid, rc);
500 		else
501 			trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
502 		free_set_inf_compound(rqst);
503 		break;
504 	case SMB2_OP_RMDIR:
505 		if (rc)
506 			trace_smb3_rmdir_err(xid,  ses->Suid, tcon->tid, rc);
507 		else
508 			trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
509 		free_set_inf_compound(rqst);
510 		break;
511 	case SMB2_OP_SET_EOF:
512 		if (rc)
513 			trace_smb3_set_eof_err(xid,  ses->Suid, tcon->tid, rc);
514 		else
515 			trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
516 		free_set_inf_compound(rqst);
517 		break;
518 	case SMB2_OP_SET_INFO:
519 		if (rc)
520 			trace_smb3_set_info_compound_err(xid,  ses->Suid,
521 						tcon->tid, rc);
522 		else
523 			trace_smb3_set_info_compound_done(xid, ses->Suid,
524 						tcon->tid);
525 		free_set_inf_compound(rqst);
526 		break;
527 	}
528 
529 	if (cfile)
530 		cifsFileInfo_put(cfile);
531 
532 	if (rc && err_iov && err_buftype) {
533 		memcpy(err_iov, rsp_iov, 3 * sizeof(*err_iov));
534 		memcpy(err_buftype, resp_buftype, 3 * sizeof(*err_buftype));
535 	} else {
536 		free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
537 		free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
538 		free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
539 	}
540 	kfree(vars);
541 	return rc;
542 }
543 
544 int smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
545 			 struct cifs_sb_info *cifs_sb, const char *full_path,
546 			 struct cifs_open_info_data *data, bool *adjust_tz, bool *reparse)
547 {
548 	__u32 create_options = 0;
549 	struct cifsFileInfo *cfile;
550 	struct cached_fid *cfid = NULL;
551 	struct kvec err_iov[3] = {};
552 	int err_buftype[3] = {};
553 	bool islink;
554 	int rc, rc2;
555 
556 	*adjust_tz = false;
557 	*reparse = false;
558 
559 	if (strcmp(full_path, ""))
560 		rc = -ENOENT;
561 	else
562 		rc = open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
563 	/* If it is a root and its handle is cached then use it */
564 	if (!rc) {
565 		if (cfid->file_all_info_is_valid) {
566 			memcpy(&data->fi, &cfid->file_all_info, sizeof(data->fi));
567 		} else {
568 			rc = SMB2_query_info(xid, tcon, cfid->fid.persistent_fid,
569 					     cfid->fid.volatile_fid, &data->fi);
570 		}
571 		close_cached_dir(cfid);
572 		return rc;
573 	}
574 
575 	cifs_get_readable_path(tcon, full_path, &cfile);
576 	rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES, FILE_OPEN,
577 			      create_options, ACL_NO_MODE, data, SMB2_OP_QUERY_INFO, cfile,
578 			      NULL, NULL, err_iov, err_buftype);
579 	if (rc) {
580 		struct smb2_hdr *hdr = err_iov[0].iov_base;
581 
582 		if (unlikely(!hdr || err_buftype[0] == CIFS_NO_BUFFER))
583 			goto out;
584 		if (rc == -EOPNOTSUPP && hdr->Command == SMB2_CREATE &&
585 		    hdr->Status == STATUS_STOPPED_ON_SYMLINK) {
586 			rc = smb2_parse_symlink_response(cifs_sb, err_iov,
587 							 &data->symlink_target);
588 			if (rc)
589 				goto out;
590 
591 			*reparse = true;
592 			create_options |= OPEN_REPARSE_POINT;
593 
594 			/* Failed on a symbolic link - query a reparse point info */
595 			cifs_get_readable_path(tcon, full_path, &cfile);
596 			rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
597 					      FILE_READ_ATTRIBUTES, FILE_OPEN,
598 					      create_options, ACL_NO_MODE, data,
599 					      SMB2_OP_QUERY_INFO, cfile, NULL, NULL,
600 					      NULL, NULL);
601 			goto out;
602 		} else if (rc != -EREMOTE && hdr->Status == STATUS_OBJECT_NAME_INVALID) {
603 			rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb,
604 							     full_path, &islink);
605 			if (rc2) {
606 				rc = rc2;
607 				goto out;
608 			}
609 			if (islink)
610 				rc = -EREMOTE;
611 		}
612 	}
613 
614 out:
615 	free_rsp_buf(err_buftype[0], err_iov[0].iov_base);
616 	free_rsp_buf(err_buftype[1], err_iov[1].iov_base);
617 	free_rsp_buf(err_buftype[2], err_iov[2].iov_base);
618 	return rc;
619 }
620 
621 
622 int smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
623 				 struct cifs_sb_info *cifs_sb, const char *full_path,
624 				 struct cifs_open_info_data *data,
625 				 struct cifs_sid *owner,
626 				 struct cifs_sid *group,
627 				 bool *adjust_tz, bool *reparse)
628 {
629 	int rc;
630 	__u32 create_options = 0;
631 	struct cifsFileInfo *cfile;
632 	struct kvec err_iov[3] = {};
633 	int err_buftype[3] = {};
634 	__u8 *sidsbuf = NULL;
635 	__u8 *sidsbuf_end = NULL;
636 	size_t sidsbuflen = 0;
637 	size_t owner_len, group_len;
638 
639 	*adjust_tz = false;
640 	*reparse = false;
641 
642 	/*
643 	 * BB TODO: Add support for using the cached root handle.
644 	 * Create SMB2_query_posix_info worker function to do non-compounded query
645 	 * when we already have an open file handle for this. For now this is fast enough
646 	 * (always using the compounded version).
647 	 */
648 
649 	cifs_get_readable_path(tcon, full_path, &cfile);
650 	rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES, FILE_OPEN,
651 			      create_options, ACL_NO_MODE, data, SMB2_OP_POSIX_QUERY_INFO, cfile,
652 			      &sidsbuf, &sidsbuflen, err_iov, err_buftype);
653 	if (rc == -EOPNOTSUPP) {
654 		/* BB TODO: When support for special files added to Samba re-verify this path */
655 		if (err_iov[0].iov_base && err_buftype[0] != CIFS_NO_BUFFER &&
656 		    ((struct smb2_hdr *)err_iov[0].iov_base)->Command == SMB2_CREATE &&
657 		    ((struct smb2_hdr *)err_iov[0].iov_base)->Status == STATUS_STOPPED_ON_SYMLINK) {
658 			rc = smb2_parse_symlink_response(cifs_sb, err_iov, &data->symlink_target);
659 			if (rc)
660 				goto out;
661 		}
662 		*reparse = true;
663 		create_options |= OPEN_REPARSE_POINT;
664 
665 		/* Failed on a symbolic link - query a reparse point info */
666 		cifs_get_readable_path(tcon, full_path, &cfile);
667 		rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES,
668 				      FILE_OPEN, create_options, ACL_NO_MODE, data,
669 				      SMB2_OP_POSIX_QUERY_INFO, cfile,
670 				      &sidsbuf, &sidsbuflen, NULL, NULL);
671 	}
672 
673 	if (rc == 0) {
674 		sidsbuf_end = sidsbuf + sidsbuflen;
675 
676 		owner_len = posix_info_sid_size(sidsbuf, sidsbuf_end);
677 		if (owner_len == -1) {
678 			rc = -EINVAL;
679 			goto out;
680 		}
681 		memcpy(owner, sidsbuf, owner_len);
682 
683 		group_len = posix_info_sid_size(
684 			sidsbuf + owner_len, sidsbuf_end);
685 		if (group_len == -1) {
686 			rc = -EINVAL;
687 			goto out;
688 		}
689 		memcpy(group, sidsbuf + owner_len, group_len);
690 	}
691 
692 out:
693 	kfree(sidsbuf);
694 	free_rsp_buf(err_buftype[0], err_iov[0].iov_base);
695 	free_rsp_buf(err_buftype[1], err_iov[1].iov_base);
696 	free_rsp_buf(err_buftype[2], err_iov[2].iov_base);
697 	return rc;
698 }
699 
700 int
701 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
702 	   struct cifs_tcon *tcon, const char *name,
703 	   struct cifs_sb_info *cifs_sb)
704 {
705 	return smb2_compound_op(xid, tcon, cifs_sb, name,
706 				FILE_WRITE_ATTRIBUTES, FILE_CREATE,
707 				CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
708 				NULL, NULL, NULL, NULL, NULL);
709 }
710 
711 void
712 smb2_mkdir_setinfo(struct inode *inode, const char *name,
713 		   struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
714 		   const unsigned int xid)
715 {
716 	FILE_BASIC_INFO data;
717 	struct cifsInodeInfo *cifs_i;
718 	struct cifsFileInfo *cfile;
719 	u32 dosattrs;
720 	int tmprc;
721 
722 	memset(&data, 0, sizeof(data));
723 	cifs_i = CIFS_I(inode);
724 	dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
725 	data.Attributes = cpu_to_le32(dosattrs);
726 	cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
727 	tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
728 				 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
729 				 CREATE_NOT_FILE, ACL_NO_MODE,
730 				 &data, SMB2_OP_SET_INFO, cfile, NULL, NULL, NULL, NULL);
731 	if (tmprc == 0)
732 		cifs_i->cifsAttrs = dosattrs;
733 }
734 
735 int
736 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
737 	   struct cifs_sb_info *cifs_sb)
738 {
739 	drop_cached_dir_by_name(xid, tcon, name, cifs_sb);
740 	return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
741 				CREATE_NOT_FILE, ACL_NO_MODE,
742 				NULL, SMB2_OP_RMDIR, NULL, NULL, NULL, NULL, NULL);
743 }
744 
745 int
746 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
747 	    struct cifs_sb_info *cifs_sb)
748 {
749 	return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
750 				CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
751 				ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL, NULL, NULL, NULL, NULL);
752 }
753 
754 static int
755 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
756 		   const char *from_name, const char *to_name,
757 		   struct cifs_sb_info *cifs_sb, __u32 access, int command,
758 		   struct cifsFileInfo *cfile)
759 {
760 	__le16 *smb2_to_name = NULL;
761 	int rc;
762 
763 	smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
764 	if (smb2_to_name == NULL) {
765 		rc = -ENOMEM;
766 		goto smb2_rename_path;
767 	}
768 	rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
769 			      FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
770 			      command, cfile, NULL, NULL, NULL, NULL);
771 smb2_rename_path:
772 	kfree(smb2_to_name);
773 	return rc;
774 }
775 
776 int
777 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
778 		 const char *from_name, const char *to_name,
779 		 struct cifs_sb_info *cifs_sb)
780 {
781 	struct cifsFileInfo *cfile;
782 
783 	drop_cached_dir_by_name(xid, tcon, from_name, cifs_sb);
784 	cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
785 
786 	return smb2_set_path_attr(xid, tcon, from_name, to_name,
787 				  cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
788 }
789 
790 int
791 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
792 		     const char *from_name, const char *to_name,
793 		     struct cifs_sb_info *cifs_sb)
794 {
795 	return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
796 				  FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
797 				  NULL);
798 }
799 
800 int
801 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
802 		   const char *full_path, __u64 size,
803 		   struct cifs_sb_info *cifs_sb, bool set_alloc)
804 {
805 	__le64 eof = cpu_to_le64(size);
806 	struct cifsFileInfo *cfile;
807 
808 	cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
809 	return smb2_compound_op(xid, tcon, cifs_sb, full_path,
810 				FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
811 				&eof, SMB2_OP_SET_EOF, cfile, NULL, NULL, NULL, NULL);
812 }
813 
814 int
815 smb2_set_file_info(struct inode *inode, const char *full_path,
816 		   FILE_BASIC_INFO *buf, const unsigned int xid)
817 {
818 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
819 	struct tcon_link *tlink;
820 	struct cifs_tcon *tcon;
821 	struct cifsFileInfo *cfile;
822 	int rc;
823 
824 	if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
825 	    (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
826 	    (buf->Attributes == 0))
827 		return 0; /* would be a no op, no sense sending this */
828 
829 	tlink = cifs_sb_tlink(cifs_sb);
830 	if (IS_ERR(tlink))
831 		return PTR_ERR(tlink);
832 	tcon = tlink_tcon(tlink);
833 
834 	cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
835 	rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
836 			      FILE_WRITE_ATTRIBUTES, FILE_OPEN,
837 			      0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, cfile,
838 			      NULL, NULL, NULL, NULL);
839 	cifs_put_tlink(tlink);
840 	return rc;
841 }
842