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