• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 static void
free_set_inf_compound(struct smb_rqst * rqst)28 free_set_inf_compound(struct smb_rqst *rqst)
29 {
30 	if (rqst[1].rq_iov)
31 		SMB2_set_info_free(&rqst[1]);
32 	if (rqst[2].rq_iov)
33 		SMB2_close_free(&rqst[2]);
34 }
35 
36 
37 struct cop_vars {
38 	struct cifs_open_parms oparms;
39 	struct kvec rsp_iov[3];
40 	struct smb_rqst rqst[3];
41 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
42 	struct kvec qi_iov[1];
43 	struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
44 	struct kvec close_iov[1];
45 	struct smb2_file_rename_info rename_info;
46 	struct smb2_file_link_info link_info;
47 };
48 
49 static int
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)50 smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
51 		 struct cifs_sb_info *cifs_sb, const char *full_path,
52 		 __u32 desired_access, __u32 create_disposition,
53 		 __u32 create_options, umode_t mode, void *ptr, int command,
54 		 struct cifsFileInfo *cfile)
55 {
56 	struct cop_vars *vars = NULL;
57 	struct kvec *rsp_iov;
58 	struct smb_rqst *rqst;
59 	int rc;
60 	__le16 *utf16_path = NULL;
61 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
62 	struct cifs_fid fid;
63 	struct cifs_ses *ses = tcon->ses;
64 	struct TCP_Server_Info *server;
65 	int num_rqst = 0;
66 	int resp_buftype[3];
67 	struct smb2_query_info_rsp *qi_rsp = NULL;
68 	int flags = 0;
69 	__u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
70 	unsigned int size[2];
71 	void *data[2];
72 	int len;
73 
74 	vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
75 	if (vars == NULL)
76 		return -ENOMEM;
77 	rqst = &vars->rqst[0];
78 	rsp_iov = &vars->rsp_iov[0];
79 
80 	server = cifs_pick_channel(ses);
81 
82 	if (smb3_encryption_required(tcon))
83 		flags |= CIFS_TRANSFORM_REQ;
84 
85 	resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
86 
87 	/* We already have a handle so we can skip the open */
88 	if (cfile)
89 		goto after_open;
90 
91 	/* Open */
92 	utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
93 	if (!utf16_path) {
94 		rc = -ENOMEM;
95 		goto finished;
96 	}
97 
98 	vars->oparms.tcon = tcon;
99 	vars->oparms.desired_access = desired_access;
100 	vars->oparms.disposition = create_disposition;
101 	vars->oparms.create_options = cifs_create_options(cifs_sb, create_options);
102 	vars->oparms.fid = &fid;
103 	vars->oparms.reconnect = false;
104 	vars->oparms.mode = mode;
105 	vars->oparms.cifs_sb = cifs_sb;
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[0];
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[0];
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[0];
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 	if (cfile)
390 		cifsFileInfo_put(cfile);
391 
392 	SMB2_open_free(&rqst[0]);
393 	if (rc == -EREMCHG) {
394 		pr_warn_once("server share %s deleted\n", tcon->treeName);
395 		tcon->need_reconnect = true;
396 	}
397 
398 	switch (command) {
399 	case SMB2_OP_QUERY_INFO:
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(struct smb2_file_all_info),
407 				ptr);
408 		}
409 		if (rqst[1].rq_iov)
410 			SMB2_query_info_free(&rqst[1]);
411 		if (rqst[2].rq_iov)
412 			SMB2_close_free(&rqst[2]);
413 		if (rc)
414 			trace_smb3_query_info_compound_err(xid,  ses->Suid,
415 						tcon->tid, rc);
416 		else
417 			trace_smb3_query_info_compound_done(xid, ses->Suid,
418 						tcon->tid);
419 		break;
420 	case SMB2_OP_POSIX_QUERY_INFO:
421 		if (rc == 0) {
422 			qi_rsp = (struct smb2_query_info_rsp *)
423 				rsp_iov[1].iov_base;
424 			rc = smb2_validate_and_copy_iov(
425 				le16_to_cpu(qi_rsp->OutputBufferOffset),
426 				le32_to_cpu(qi_rsp->OutputBufferLength),
427 				&rsp_iov[1], sizeof(struct smb311_posix_qinfo) /* add SIDs */, ptr);
428 		}
429 		if (rqst[1].rq_iov)
430 			SMB2_query_info_free(&rqst[1]);
431 		if (rqst[2].rq_iov)
432 			SMB2_close_free(&rqst[2]);
433 		if (rc)
434 			trace_smb3_posix_query_info_compound_err(xid,  ses->Suid, tcon->tid, rc);
435 		else
436 			trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
437 		break;
438 	case SMB2_OP_DELETE:
439 		if (rc)
440 			trace_smb3_delete_err(xid,  ses->Suid, tcon->tid, rc);
441 		else
442 			trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
443 		if (rqst[1].rq_iov)
444 			SMB2_close_free(&rqst[1]);
445 		break;
446 	case SMB2_OP_MKDIR:
447 		if (rc)
448 			trace_smb3_mkdir_err(xid,  ses->Suid, tcon->tid, rc);
449 		else
450 			trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
451 		if (rqst[1].rq_iov)
452 			SMB2_close_free(&rqst[1]);
453 		break;
454 	case SMB2_OP_HARDLINK:
455 		if (rc)
456 			trace_smb3_hardlink_err(xid,  ses->Suid, tcon->tid, rc);
457 		else
458 			trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
459 		free_set_inf_compound(rqst);
460 		break;
461 	case SMB2_OP_RENAME:
462 		if (rc)
463 			trace_smb3_rename_err(xid,  ses->Suid, tcon->tid, rc);
464 		else
465 			trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
466 		free_set_inf_compound(rqst);
467 		break;
468 	case SMB2_OP_RMDIR:
469 		if (rc)
470 			trace_smb3_rmdir_err(xid,  ses->Suid, tcon->tid, rc);
471 		else
472 			trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
473 		free_set_inf_compound(rqst);
474 		break;
475 	case SMB2_OP_SET_EOF:
476 		if (rc)
477 			trace_smb3_set_eof_err(xid,  ses->Suid, tcon->tid, rc);
478 		else
479 			trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
480 		free_set_inf_compound(rqst);
481 		break;
482 	case SMB2_OP_SET_INFO:
483 		if (rc)
484 			trace_smb3_set_info_compound_err(xid,  ses->Suid,
485 						tcon->tid, rc);
486 		else
487 			trace_smb3_set_info_compound_done(xid, ses->Suid,
488 						tcon->tid);
489 		free_set_inf_compound(rqst);
490 		break;
491 	}
492 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
493 	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
494 	free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
495 	kfree(vars);
496 	return rc;
497 }
498 
499 void
move_smb2_info_to_cifs(FILE_ALL_INFO * dst,struct smb2_file_all_info * src)500 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
501 {
502 	memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
503 	dst->CurrentByteOffset = src->CurrentByteOffset;
504 	dst->Mode = src->Mode;
505 	dst->AlignmentRequirement = src->AlignmentRequirement;
506 	dst->IndexNumber1 = 0; /* we don't use it */
507 }
508 
509 int
smb2_query_path_info(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,FILE_ALL_INFO * data,bool * adjust_tz,bool * reparse)510 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
511 		     struct cifs_sb_info *cifs_sb, const char *full_path,
512 		     FILE_ALL_INFO *data, bool *adjust_tz, bool *reparse)
513 {
514 	int rc;
515 	struct smb2_file_all_info *smb2_data;
516 	__u32 create_options = 0;
517 	struct cifsFileInfo *cfile;
518 	struct cached_fid *cfid = NULL;
519 
520 	*adjust_tz = false;
521 	*reparse = false;
522 
523 	smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
524 			    GFP_KERNEL);
525 	if (smb2_data == NULL)
526 		return -ENOMEM;
527 
528 	/* If it is a root and its handle is cached then use it */
529 	rc = open_cached_dir(xid, tcon, full_path, cifs_sb, &cfid);
530 	if (!rc) {
531 		if (tcon->crfid.file_all_info_is_valid) {
532 			move_smb2_info_to_cifs(data,
533 					       &tcon->crfid.file_all_info);
534 		} else {
535 			rc = SMB2_query_info(xid, tcon,
536 					     cfid->fid->persistent_fid,
537 					     cfid->fid->volatile_fid, smb2_data);
538 			if (!rc)
539 				move_smb2_info_to_cifs(data, smb2_data);
540 		}
541 		close_cached_dir(cfid);
542 		goto out;
543 	}
544 
545 	cifs_get_readable_path(tcon, full_path, &cfile);
546 	rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
547 			      FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
548 			      ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
549 	if (rc == -EOPNOTSUPP) {
550 		*reparse = true;
551 		create_options |= OPEN_REPARSE_POINT;
552 
553 		/* Failed on a symbolic link - query a reparse point info */
554 		rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
555 				      FILE_READ_ATTRIBUTES, FILE_OPEN,
556 				      create_options, ACL_NO_MODE,
557 				      smb2_data, SMB2_OP_QUERY_INFO, NULL);
558 	}
559 	if (rc)
560 		goto out;
561 
562 	move_smb2_info_to_cifs(data, smb2_data);
563 out:
564 	kfree(smb2_data);
565 	return rc;
566 }
567 
568 
569 int
smb311_posix_query_path_info(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,struct smb311_posix_qinfo * data,bool * adjust_tz,bool * reparse)570 smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
571 		     struct cifs_sb_info *cifs_sb, const char *full_path,
572 		     struct smb311_posix_qinfo *data, bool *adjust_tz, bool *reparse)
573 {
574 	int rc;
575 	__u32 create_options = 0;
576 	struct cifsFileInfo *cfile;
577 	struct smb311_posix_qinfo *smb2_data;
578 
579 	*adjust_tz = false;
580 	*reparse = false;
581 
582 	/* BB TODO: Make struct larger when add support for parsing owner SIDs */
583 	smb2_data = kzalloc(sizeof(struct smb311_posix_qinfo),
584 			    GFP_KERNEL);
585 	if (smb2_data == NULL)
586 		return -ENOMEM;
587 
588 	/*
589 	 * BB TODO: Add support for using the cached root handle.
590 	 * Create SMB2_query_posix_info worker function to do non-compounded query
591 	 * when we already have an open file handle for this. For now this is fast enough
592 	 * (always using the compounded version).
593 	 */
594 
595 	cifs_get_readable_path(tcon, full_path, &cfile);
596 	rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
597 			      FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
598 			      ACL_NO_MODE, smb2_data, SMB2_OP_POSIX_QUERY_INFO, cfile);
599 	if (rc == -EOPNOTSUPP) {
600 		/* BB TODO: When support for special files added to Samba re-verify this path */
601 		*reparse = true;
602 		create_options |= OPEN_REPARSE_POINT;
603 
604 		/* Failed on a symbolic link - query a reparse point info */
605 		rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
606 				      FILE_READ_ATTRIBUTES, FILE_OPEN,
607 				      create_options, ACL_NO_MODE,
608 				      smb2_data, SMB2_OP_POSIX_QUERY_INFO, NULL);
609 	}
610 	if (rc)
611 		goto out;
612 
613 	 /* TODO: will need to allow for the 2 SIDs when add support for getting owner UID/GID */
614 	memcpy(data, smb2_data, sizeof(struct smb311_posix_qinfo));
615 
616 out:
617 	kfree(smb2_data);
618 	return rc;
619 }
620 
621 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)622 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
623 	   struct cifs_tcon *tcon, const char *name,
624 	   struct cifs_sb_info *cifs_sb)
625 {
626 	return smb2_compound_op(xid, tcon, cifs_sb, name,
627 				FILE_WRITE_ATTRIBUTES, FILE_CREATE,
628 				CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
629 				NULL);
630 }
631 
632 void
smb2_mkdir_setinfo(struct inode * inode,const char * name,struct cifs_sb_info * cifs_sb,struct cifs_tcon * tcon,const unsigned int xid)633 smb2_mkdir_setinfo(struct inode *inode, const char *name,
634 		   struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
635 		   const unsigned int xid)
636 {
637 	FILE_BASIC_INFO data;
638 	struct cifsInodeInfo *cifs_i;
639 	struct cifsFileInfo *cfile;
640 	u32 dosattrs;
641 	int tmprc;
642 
643 	memset(&data, 0, sizeof(data));
644 	cifs_i = CIFS_I(inode);
645 	dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
646 	data.Attributes = cpu_to_le32(dosattrs);
647 	cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
648 	tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
649 				 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
650 				 CREATE_NOT_FILE, ACL_NO_MODE,
651 				 &data, SMB2_OP_SET_INFO, cfile);
652 	if (tmprc == 0)
653 		cifs_i->cifsAttrs = dosattrs;
654 }
655 
656 int
smb2_rmdir(const unsigned int xid,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb)657 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
658 	   struct cifs_sb_info *cifs_sb)
659 {
660 	return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
661 				CREATE_NOT_FILE, ACL_NO_MODE,
662 				NULL, SMB2_OP_RMDIR, NULL);
663 }
664 
665 int
smb2_unlink(const unsigned int xid,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb)666 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
667 	    struct cifs_sb_info *cifs_sb)
668 {
669 	return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
670 				CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
671 				ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
672 }
673 
674 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)675 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
676 		   const char *from_name, const char *to_name,
677 		   struct cifs_sb_info *cifs_sb, __u32 access, int command,
678 		   struct cifsFileInfo *cfile)
679 {
680 	__le16 *smb2_to_name = NULL;
681 	int rc;
682 
683 	smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
684 	if (smb2_to_name == NULL) {
685 		rc = -ENOMEM;
686 		goto smb2_rename_path;
687 	}
688 	rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
689 			      FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
690 			      command, cfile);
691 smb2_rename_path:
692 	kfree(smb2_to_name);
693 	return rc;
694 }
695 
696 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)697 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
698 		 const char *from_name, const char *to_name,
699 		 struct cifs_sb_info *cifs_sb)
700 {
701 	struct cifsFileInfo *cfile;
702 
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 
726 	return smb2_compound_op(xid, tcon, cifs_sb, full_path,
727 				FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
728 				&eof, SMB2_OP_SET_EOF, NULL);
729 }
730 
731 int
smb2_set_file_info(struct inode * inode,const char * full_path,FILE_BASIC_INFO * buf,const unsigned int xid)732 smb2_set_file_info(struct inode *inode, const char *full_path,
733 		   FILE_BASIC_INFO *buf, const unsigned int xid)
734 {
735 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
736 	struct tcon_link *tlink;
737 	int rc;
738 
739 	if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
740 	    (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
741 	    (buf->Attributes == 0))
742 		return 0; /* would be a no op, no sense sending this */
743 
744 	tlink = cifs_sb_tlink(cifs_sb);
745 	if (IS_ERR(tlink))
746 		return PTR_ERR(tlink);
747 
748 	rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
749 			      FILE_WRITE_ATTRIBUTES, FILE_OPEN,
750 			      0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, NULL);
751 	cifs_put_tlink(tlink);
752 	return rc;
753 }
754