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