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