xref: /openbmc/linux/fs/smb/client/smb2inode.c (revision a325f174)
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_STOPPED_ON_SYMLINK:
543 		rc = smb2_parse_symlink_response(cifs_sb, iov,
544 						 &data->symlink_target);
545 		if (rc)
546 			return rc;
547 		tag = IO_REPARSE_TAG_SYMLINK;
548 		reparse_point = true;
549 		break;
550 	case STATUS_SUCCESS:
551 		reparse_point = !!(rsp->Flags & SMB2_CREATE_FLAG_REPARSEPOINT);
552 		break;
553 	}
554 	data->reparse_point = reparse_point;
555 	data->reparse_tag = tag;
556 	return rc;
557 }
558 
559 int smb2_query_path_info(const unsigned int xid,
560 			 struct cifs_tcon *tcon,
561 			 struct cifs_sb_info *cifs_sb,
562 			 const char *full_path,
563 			 struct cifs_open_info_data *data)
564 {
565 	__u32 create_options = 0;
566 	struct cifsFileInfo *cfile;
567 	struct cached_fid *cfid = NULL;
568 	struct smb2_hdr *hdr;
569 	struct kvec out_iov[3] = {};
570 	int out_buftype[3] = {};
571 	bool islink;
572 	int rc, rc2;
573 
574 	data->adjust_tz = false;
575 	data->reparse_point = false;
576 
577 	if (strcmp(full_path, ""))
578 		rc = -ENOENT;
579 	else
580 		rc = open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
581 	/* If it is a root and its handle is cached then use it */
582 	if (!rc) {
583 		if (cfid->file_all_info_is_valid) {
584 			memcpy(&data->fi, &cfid->file_all_info, sizeof(data->fi));
585 		} else {
586 			rc = SMB2_query_info(xid, tcon, cfid->fid.persistent_fid,
587 					     cfid->fid.volatile_fid, &data->fi);
588 		}
589 		close_cached_dir(cfid);
590 		return rc;
591 	}
592 
593 	cifs_get_readable_path(tcon, full_path, &cfile);
594 	rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES, FILE_OPEN,
595 			      create_options, ACL_NO_MODE, data, SMB2_OP_QUERY_INFO, cfile,
596 			      NULL, NULL, out_iov, out_buftype);
597 	hdr = out_iov[0].iov_base;
598 	/*
599 	 * If first iov is unset, then SMB session was dropped or we've got a
600 	 * cached open file (@cfile).
601 	 */
602 	if (!hdr || out_buftype[0] == CIFS_NO_BUFFER)
603 		goto out;
604 
605 	switch (rc) {
606 	case 0:
607 	case -EOPNOTSUPP:
608 		rc = parse_create_response(data, cifs_sb, &out_iov[0]);
609 		if (rc || !data->reparse_point)
610 			goto out;
611 
612 		create_options |= OPEN_REPARSE_POINT;
613 		/* Failed on a symbolic link - query a reparse point info */
614 		cifs_get_readable_path(tcon, full_path, &cfile);
615 		rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
616 				      FILE_READ_ATTRIBUTES, FILE_OPEN,
617 				      create_options, ACL_NO_MODE, data,
618 				      SMB2_OP_QUERY_INFO, cfile, NULL, NULL,
619 				      NULL, NULL);
620 		break;
621 	case -EREMOTE:
622 		break;
623 	default:
624 		if (hdr->Status != STATUS_OBJECT_NAME_INVALID)
625 			break;
626 		rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb,
627 						     full_path, &islink);
628 		if (rc2) {
629 			rc = rc2;
630 			goto out;
631 		}
632 		if (islink)
633 			rc = -EREMOTE;
634 	}
635 
636 out:
637 	free_rsp_buf(out_buftype[0], out_iov[0].iov_base);
638 	free_rsp_buf(out_buftype[1], out_iov[1].iov_base);
639 	free_rsp_buf(out_buftype[2], out_iov[2].iov_base);
640 	return rc;
641 }
642 
643 int smb311_posix_query_path_info(const unsigned int xid,
644 				 struct cifs_tcon *tcon,
645 				 struct cifs_sb_info *cifs_sb,
646 				 const char *full_path,
647 				 struct cifs_open_info_data *data,
648 				 struct cifs_sid *owner,
649 				 struct cifs_sid *group)
650 {
651 	int rc;
652 	__u32 create_options = 0;
653 	struct cifsFileInfo *cfile;
654 	struct kvec out_iov[3] = {};
655 	int out_buftype[3] = {};
656 	__u8 *sidsbuf = NULL;
657 	__u8 *sidsbuf_end = NULL;
658 	size_t sidsbuflen = 0;
659 	size_t owner_len, group_len;
660 
661 	data->adjust_tz = false;
662 	data->reparse_point = false;
663 
664 	/*
665 	 * BB TODO: Add support for using the cached root handle.
666 	 * Create SMB2_query_posix_info worker function to do non-compounded query
667 	 * when we already have an open file handle for this. For now this is fast enough
668 	 * (always using the compounded version).
669 	 */
670 
671 	cifs_get_readable_path(tcon, full_path, &cfile);
672 	rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES, FILE_OPEN,
673 			      create_options, ACL_NO_MODE, data, SMB2_OP_POSIX_QUERY_INFO, cfile,
674 			      &sidsbuf, &sidsbuflen, out_iov, out_buftype);
675 	/*
676 	 * If first iov is unset, then SMB session was dropped or we've got a
677 	 * cached open file (@cfile).
678 	 */
679 	if (!out_iov[0].iov_base || out_buftype[0] == CIFS_NO_BUFFER)
680 		goto out;
681 
682 	switch (rc) {
683 	case 0:
684 	case -EOPNOTSUPP:
685 		/* BB TODO: When support for special files added to Samba re-verify this path */
686 		rc = parse_create_response(data, cifs_sb, &out_iov[0]);
687 		if (rc || !data->reparse_point)
688 			goto out;
689 
690 		create_options |= OPEN_REPARSE_POINT;
691 		/* Failed on a symbolic link - query a reparse point info */
692 		cifs_get_readable_path(tcon, full_path, &cfile);
693 		rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES,
694 				      FILE_OPEN, create_options, ACL_NO_MODE, data,
695 				      SMB2_OP_POSIX_QUERY_INFO, cfile,
696 				      &sidsbuf, &sidsbuflen, NULL, NULL);
697 		break;
698 	}
699 
700 out:
701 	if (rc == 0) {
702 		sidsbuf_end = sidsbuf + sidsbuflen;
703 
704 		owner_len = posix_info_sid_size(sidsbuf, sidsbuf_end);
705 		if (owner_len == -1) {
706 			rc = -EINVAL;
707 			goto out;
708 		}
709 		memcpy(owner, sidsbuf, owner_len);
710 
711 		group_len = posix_info_sid_size(
712 			sidsbuf + owner_len, sidsbuf_end);
713 		if (group_len == -1) {
714 			rc = -EINVAL;
715 			goto out;
716 		}
717 		memcpy(group, sidsbuf + owner_len, group_len);
718 	}
719 
720 	kfree(sidsbuf);
721 	free_rsp_buf(out_buftype[0], out_iov[0].iov_base);
722 	free_rsp_buf(out_buftype[1], out_iov[1].iov_base);
723 	free_rsp_buf(out_buftype[2], out_iov[2].iov_base);
724 	return rc;
725 }
726 
727 int
728 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
729 	   struct cifs_tcon *tcon, const char *name,
730 	   struct cifs_sb_info *cifs_sb)
731 {
732 	return smb2_compound_op(xid, tcon, cifs_sb, name,
733 				FILE_WRITE_ATTRIBUTES, FILE_CREATE,
734 				CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
735 				NULL, NULL, NULL, NULL, NULL);
736 }
737 
738 void
739 smb2_mkdir_setinfo(struct inode *inode, const char *name,
740 		   struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
741 		   const unsigned int xid)
742 {
743 	FILE_BASIC_INFO data;
744 	struct cifsInodeInfo *cifs_i;
745 	struct cifsFileInfo *cfile;
746 	u32 dosattrs;
747 	int tmprc;
748 
749 	memset(&data, 0, sizeof(data));
750 	cifs_i = CIFS_I(inode);
751 	dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
752 	data.Attributes = cpu_to_le32(dosattrs);
753 	cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
754 	tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
755 				 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
756 				 CREATE_NOT_FILE, ACL_NO_MODE,
757 				 &data, SMB2_OP_SET_INFO, cfile, NULL, NULL, NULL, NULL);
758 	if (tmprc == 0)
759 		cifs_i->cifsAttrs = dosattrs;
760 }
761 
762 int
763 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
764 	   struct cifs_sb_info *cifs_sb)
765 {
766 	drop_cached_dir_by_name(xid, tcon, name, cifs_sb);
767 	return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
768 				CREATE_NOT_FILE, ACL_NO_MODE,
769 				NULL, SMB2_OP_RMDIR, NULL, NULL, NULL, NULL, NULL);
770 }
771 
772 int
773 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
774 	    struct cifs_sb_info *cifs_sb)
775 {
776 	return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
777 				CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
778 				ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL, NULL, NULL, NULL, NULL);
779 }
780 
781 static int
782 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
783 		   const char *from_name, const char *to_name,
784 		   struct cifs_sb_info *cifs_sb, __u32 access, int command,
785 		   struct cifsFileInfo *cfile)
786 {
787 	__le16 *smb2_to_name = NULL;
788 	int rc;
789 
790 	smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
791 	if (smb2_to_name == NULL) {
792 		rc = -ENOMEM;
793 		goto smb2_rename_path;
794 	}
795 	rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
796 			      FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
797 			      command, cfile, NULL, NULL, NULL, NULL);
798 smb2_rename_path:
799 	kfree(smb2_to_name);
800 	return rc;
801 }
802 
803 int
804 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
805 		 const char *from_name, const char *to_name,
806 		 struct cifs_sb_info *cifs_sb)
807 {
808 	struct cifsFileInfo *cfile;
809 
810 	drop_cached_dir_by_name(xid, tcon, from_name, cifs_sb);
811 	cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
812 
813 	return smb2_set_path_attr(xid, tcon, from_name, to_name,
814 				  cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
815 }
816 
817 int
818 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
819 		     const char *from_name, const char *to_name,
820 		     struct cifs_sb_info *cifs_sb)
821 {
822 	return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
823 				  FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
824 				  NULL);
825 }
826 
827 int
828 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
829 		   const char *full_path, __u64 size,
830 		   struct cifs_sb_info *cifs_sb, bool set_alloc)
831 {
832 	__le64 eof = cpu_to_le64(size);
833 	struct cifsFileInfo *cfile;
834 
835 	cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
836 	return smb2_compound_op(xid, tcon, cifs_sb, full_path,
837 				FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
838 				&eof, SMB2_OP_SET_EOF, cfile, NULL, NULL, NULL, NULL);
839 }
840 
841 int
842 smb2_set_file_info(struct inode *inode, const char *full_path,
843 		   FILE_BASIC_INFO *buf, const unsigned int xid)
844 {
845 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
846 	struct tcon_link *tlink;
847 	struct cifs_tcon *tcon;
848 	struct cifsFileInfo *cfile;
849 	int rc;
850 
851 	if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
852 	    (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
853 	    (buf->Attributes == 0))
854 		return 0; /* would be a no op, no sense sending this */
855 
856 	tlink = cifs_sb_tlink(cifs_sb);
857 	if (IS_ERR(tlink))
858 		return PTR_ERR(tlink);
859 	tcon = tlink_tcon(tlink);
860 
861 	cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
862 	rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
863 			      FILE_WRITE_ATTRIBUTES, FILE_OPEN,
864 			      0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, cfile,
865 			      NULL, NULL, NULL, NULL);
866 	cifs_put_tlink(tlink);
867 	return rc;
868 }
869