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 == -EOPNOTSUPP) {
544 		if (err_iov[0].iov_base && err_buftype[0] != CIFS_NO_BUFFER &&
545 		    ((struct smb2_hdr *)err_iov[0].iov_base)->Command == SMB2_CREATE &&
546 		    ((struct smb2_hdr *)err_iov[0].iov_base)->Status == STATUS_STOPPED_ON_SYMLINK) {
547 			rc = smb2_parse_symlink_response(cifs_sb, err_iov, &data->symlink_target);
548 			if (rc)
549 				goto out;
550 		}
551 		*reparse = true;
552 		create_options |= OPEN_REPARSE_POINT;
553 
554 		/* Failed on a symbolic link - query a reparse point info */
555 		cifs_get_readable_path(tcon, full_path, &cfile);
556 		rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES,
557 				      FILE_OPEN, create_options, ACL_NO_MODE, data,
558 				      SMB2_OP_QUERY_INFO, cfile, NULL, NULL);
559 	}
560 
561 out:
562 	free_rsp_buf(err_buftype[0], err_iov[0].iov_base);
563 	free_rsp_buf(err_buftype[1], err_iov[1].iov_base);
564 	free_rsp_buf(err_buftype[2], err_iov[2].iov_base);
565 	return rc;
566 }
567 
568 
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)569 int smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
570 				 struct cifs_sb_info *cifs_sb, const char *full_path,
571 				 struct cifs_open_info_data *data, bool *adjust_tz, bool *reparse)
572 {
573 	int rc;
574 	__u32 create_options = 0;
575 	struct cifsFileInfo *cfile;
576 	struct kvec err_iov[3] = {};
577 	int err_buftype[3] = {};
578 
579 	*adjust_tz = false;
580 	*reparse = false;
581 
582 	/*
583 	 * BB TODO: Add support for using the cached root handle.
584 	 * Create SMB2_query_posix_info worker function to do non-compounded query
585 	 * when we already have an open file handle for this. For now this is fast enough
586 	 * (always using the compounded version).
587 	 */
588 
589 	cifs_get_readable_path(tcon, full_path, &cfile);
590 	rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES, FILE_OPEN,
591 			      create_options, ACL_NO_MODE, data, SMB2_OP_POSIX_QUERY_INFO, cfile,
592 			      err_iov, err_buftype);
593 	if (rc == -EOPNOTSUPP) {
594 		/* BB TODO: When support for special files added to Samba re-verify this path */
595 		if (err_iov[0].iov_base && err_buftype[0] != CIFS_NO_BUFFER &&
596 		    ((struct smb2_hdr *)err_iov[0].iov_base)->Command == SMB2_CREATE &&
597 		    ((struct smb2_hdr *)err_iov[0].iov_base)->Status == STATUS_STOPPED_ON_SYMLINK) {
598 			rc = smb2_parse_symlink_response(cifs_sb, err_iov, &data->symlink_target);
599 			if (rc)
600 				goto out;
601 		}
602 		*reparse = true;
603 		create_options |= OPEN_REPARSE_POINT;
604 
605 		/* Failed on a symbolic link - query a reparse point info */
606 		cifs_get_readable_path(tcon, full_path, &cfile);
607 		rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES,
608 				      FILE_OPEN, create_options, ACL_NO_MODE, data,
609 				      SMB2_OP_POSIX_QUERY_INFO, cfile, NULL, NULL);
610 	}
611 
612 out:
613 	free_rsp_buf(err_buftype[0], err_iov[0].iov_base);
614 	free_rsp_buf(err_buftype[1], err_iov[1].iov_base);
615 	free_rsp_buf(err_buftype[2], err_iov[2].iov_base);
616 	return rc;
617 }
618 
619 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)620 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
621 	   struct cifs_tcon *tcon, const char *name,
622 	   struct cifs_sb_info *cifs_sb)
623 {
624 	return smb2_compound_op(xid, tcon, cifs_sb, name,
625 				FILE_WRITE_ATTRIBUTES, FILE_CREATE,
626 				CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
627 				NULL, NULL, NULL);
628 }
629 
630 void
smb2_mkdir_setinfo(struct inode * inode,const char * name,struct cifs_sb_info * cifs_sb,struct cifs_tcon * tcon,const unsigned int xid)631 smb2_mkdir_setinfo(struct inode *inode, const char *name,
632 		   struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
633 		   const unsigned int xid)
634 {
635 	FILE_BASIC_INFO data;
636 	struct cifsInodeInfo *cifs_i;
637 	struct cifsFileInfo *cfile;
638 	u32 dosattrs;
639 	int tmprc;
640 
641 	memset(&data, 0, sizeof(data));
642 	cifs_i = CIFS_I(inode);
643 	dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
644 	data.Attributes = cpu_to_le32(dosattrs);
645 	cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
646 	tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
647 				 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
648 				 CREATE_NOT_FILE, ACL_NO_MODE,
649 				 &data, SMB2_OP_SET_INFO, cfile, NULL, NULL);
650 	if (tmprc == 0)
651 		cifs_i->cifsAttrs = dosattrs;
652 }
653 
654 int
smb2_rmdir(const unsigned int xid,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb)655 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
656 	   struct cifs_sb_info *cifs_sb)
657 {
658 	drop_cached_dir_by_name(xid, tcon, name, cifs_sb);
659 	return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
660 				CREATE_NOT_FILE, ACL_NO_MODE,
661 				NULL, SMB2_OP_RMDIR, NULL, NULL, NULL);
662 }
663 
664 int
smb2_unlink(const unsigned int xid,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb)665 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
666 	    struct cifs_sb_info *cifs_sb)
667 {
668 	return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
669 				CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
670 				ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL, NULL, NULL);
671 }
672 
673 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)674 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
675 		   const char *from_name, const char *to_name,
676 		   struct cifs_sb_info *cifs_sb, __u32 access, int command,
677 		   struct cifsFileInfo *cfile)
678 {
679 	__le16 *smb2_to_name = NULL;
680 	int rc;
681 
682 	smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
683 	if (smb2_to_name == NULL) {
684 		rc = -ENOMEM;
685 		goto smb2_rename_path;
686 	}
687 	rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
688 			      FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
689 			      command, cfile, NULL, NULL);
690 smb2_rename_path:
691 	kfree(smb2_to_name);
692 	return rc;
693 }
694 
695 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)696 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
697 		 const char *from_name, const char *to_name,
698 		 struct cifs_sb_info *cifs_sb)
699 {
700 	struct cifsFileInfo *cfile;
701 
702 	drop_cached_dir_by_name(xid, tcon, from_name, cifs_sb);
703 	cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
704 
705 	return smb2_set_path_attr(xid, tcon, from_name, to_name,
706 				  cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
707 }
708 
709 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)710 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
711 		     const char *from_name, const char *to_name,
712 		     struct cifs_sb_info *cifs_sb)
713 {
714 	return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
715 				  FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
716 				  NULL);
717 }
718 
719 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)720 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
721 		   const char *full_path, __u64 size,
722 		   struct cifs_sb_info *cifs_sb, bool set_alloc)
723 {
724 	__le64 eof = cpu_to_le64(size);
725 	struct cifsFileInfo *cfile;
726 
727 	cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
728 	return smb2_compound_op(xid, tcon, cifs_sb, full_path,
729 				FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
730 				&eof, SMB2_OP_SET_EOF, cfile, NULL, NULL);
731 }
732 
733 int
smb2_set_file_info(struct inode * inode,const char * full_path,FILE_BASIC_INFO * buf,const unsigned int xid)734 smb2_set_file_info(struct inode *inode, const char *full_path,
735 		   FILE_BASIC_INFO *buf, const unsigned int xid)
736 {
737 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
738 	struct tcon_link *tlink;
739 	struct cifs_tcon *tcon;
740 	struct cifsFileInfo *cfile;
741 	int rc;
742 
743 	if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
744 	    (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
745 	    (buf->Attributes == 0))
746 		return 0; /* would be a no op, no sense sending this */
747 
748 	tlink = cifs_sb_tlink(cifs_sb);
749 	if (IS_ERR(tlink))
750 		return PTR_ERR(tlink);
751 	tcon = tlink_tcon(tlink);
752 
753 	cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
754 	rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
755 			      FILE_WRITE_ATTRIBUTES, FILE_OPEN,
756 			      0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, cfile,
757 			      NULL, NULL);
758 	cifs_put_tlink(tlink);
759 	return rc;
760 }
761