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 		rc = SMB2_set_info_init(tcon, server,
227 					&rqst[num_rqst], COMPOUND_FID,
228 					COMPOUND_FID, current->tgid,
229 					FILE_END_OF_FILE_INFORMATION,
230 					SMB2_O_INFO_FILE, 0, data, size);
231 		if (rc)
232 			goto finished;
233 		smb2_set_next_command(tcon, &rqst[num_rqst]);
234 		smb2_set_related(&rqst[num_rqst++]);
235 		trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
236 		break;
237 	case SMB2_OP_SET_INFO:
238 		rqst[num_rqst].rq_iov = &vars->si_iov[0];
239 		rqst[num_rqst].rq_nvec = 1;
240 
241 
242 		size[0] = sizeof(FILE_BASIC_INFO);
243 		data[0] = ptr;
244 
245 		if (cfile)
246 			rc = SMB2_set_info_init(tcon, server,
247 				&rqst[num_rqst],
248 				cfile->fid.persistent_fid,
249 				cfile->fid.volatile_fid, current->tgid,
250 				FILE_BASIC_INFORMATION,
251 				SMB2_O_INFO_FILE, 0, data, size);
252 		else {
253 			rc = SMB2_set_info_init(tcon, server,
254 				&rqst[num_rqst],
255 				COMPOUND_FID,
256 				COMPOUND_FID, current->tgid,
257 				FILE_BASIC_INFORMATION,
258 				SMB2_O_INFO_FILE, 0, data, size);
259 			if (!rc) {
260 				smb2_set_next_command(tcon, &rqst[num_rqst]);
261 				smb2_set_related(&rqst[num_rqst]);
262 			}
263 		}
264 
265 		if (rc)
266 			goto finished;
267 		num_rqst++;
268 		trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
269 						   full_path);
270 		break;
271 	case SMB2_OP_RENAME:
272 		rqst[num_rqst].rq_iov = &vars->si_iov[0];
273 		rqst[num_rqst].rq_nvec = 2;
274 
275 		len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
276 
277 		vars->rename_info.ReplaceIfExists = 1;
278 		vars->rename_info.RootDirectory = 0;
279 		vars->rename_info.FileNameLength = cpu_to_le32(len);
280 
281 		size[0] = sizeof(struct smb2_file_rename_info);
282 		data[0] = &vars->rename_info;
283 
284 		size[1] = len + 2 /* null */;
285 		data[1] = (__le16 *)ptr;
286 
287 		if (cfile)
288 			rc = SMB2_set_info_init(tcon, server,
289 						&rqst[num_rqst],
290 						cfile->fid.persistent_fid,
291 						cfile->fid.volatile_fid,
292 					current->tgid, FILE_RENAME_INFORMATION,
293 					SMB2_O_INFO_FILE, 0, data, size);
294 		else {
295 			rc = SMB2_set_info_init(tcon, server,
296 					&rqst[num_rqst],
297 					COMPOUND_FID, COMPOUND_FID,
298 					current->tgid, FILE_RENAME_INFORMATION,
299 					SMB2_O_INFO_FILE, 0, data, size);
300 			if (!rc) {
301 				smb2_set_next_command(tcon, &rqst[num_rqst]);
302 				smb2_set_related(&rqst[num_rqst]);
303 			}
304 		}
305 		if (rc)
306 			goto finished;
307 		num_rqst++;
308 		trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
309 		break;
310 	case SMB2_OP_HARDLINK:
311 		rqst[num_rqst].rq_iov = &vars->si_iov[0];
312 		rqst[num_rqst].rq_nvec = 2;
313 
314 		len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
315 
316 		vars->link_info.ReplaceIfExists = 0;
317 		vars->link_info.RootDirectory = 0;
318 		vars->link_info.FileNameLength = cpu_to_le32(len);
319 
320 		size[0] = sizeof(struct smb2_file_link_info);
321 		data[0] = &vars->link_info;
322 
323 		size[1] = len + 2 /* null */;
324 		data[1] = (__le16 *)ptr;
325 
326 		rc = SMB2_set_info_init(tcon, server,
327 					&rqst[num_rqst], COMPOUND_FID,
328 					COMPOUND_FID, current->tgid,
329 					FILE_LINK_INFORMATION,
330 					SMB2_O_INFO_FILE, 0, data, size);
331 		if (rc)
332 			goto finished;
333 		smb2_set_next_command(tcon, &rqst[num_rqst]);
334 		smb2_set_related(&rqst[num_rqst++]);
335 		trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
336 		break;
337 	default:
338 		cifs_dbg(VFS, "Invalid command\n");
339 		rc = -EINVAL;
340 	}
341 	if (rc)
342 		goto finished;
343 
344 	/* We already have a handle so we can skip the close */
345 	if (cfile)
346 		goto after_close;
347 	/* Close */
348 	flags |= CIFS_CP_CREATE_CLOSE_OP;
349 	rqst[num_rqst].rq_iov = &vars->close_iov[0];
350 	rqst[num_rqst].rq_nvec = 1;
351 	rc = SMB2_close_init(tcon, server,
352 			     &rqst[num_rqst], COMPOUND_FID,
353 			     COMPOUND_FID, false);
354 	smb2_set_related(&rqst[num_rqst]);
355 	if (rc)
356 		goto finished;
357  after_close:
358 	num_rqst++;
359 
360 	if (cfile) {
361 		cifsFileInfo_put(cfile);
362 		cfile = NULL;
363 		rc = compound_send_recv(xid, ses, server,
364 					flags, num_rqst - 2,
365 					&rqst[1], &resp_buftype[1],
366 					&rsp_iov[1]);
367 	} else
368 		rc = compound_send_recv(xid, ses, server,
369 					flags, num_rqst,
370 					rqst, resp_buftype,
371 					rsp_iov);
372 
373  finished:
374 	if (cfile)
375 		cifsFileInfo_put(cfile);
376 
377 	SMB2_open_free(&rqst[0]);
378 	if (rc == -EREMCHG) {
379 		pr_warn_once("server share %s deleted\n", tcon->treeName);
380 		tcon->need_reconnect = true;
381 	}
382 
383 	switch (command) {
384 	case SMB2_OP_QUERY_INFO:
385 		if (rc == 0) {
386 			qi_rsp = (struct smb2_query_info_rsp *)
387 				rsp_iov[1].iov_base;
388 			rc = smb2_validate_and_copy_iov(
389 				le16_to_cpu(qi_rsp->OutputBufferOffset),
390 				le32_to_cpu(qi_rsp->OutputBufferLength),
391 				&rsp_iov[1], sizeof(struct smb2_file_all_info),
392 				ptr);
393 		}
394 		if (rqst[1].rq_iov)
395 			SMB2_query_info_free(&rqst[1]);
396 		if (rqst[2].rq_iov)
397 			SMB2_close_free(&rqst[2]);
398 		if (rc)
399 			trace_smb3_query_info_compound_err(xid,  ses->Suid,
400 						tcon->tid, rc);
401 		else
402 			trace_smb3_query_info_compound_done(xid, ses->Suid,
403 						tcon->tid);
404 		break;
405 	case SMB2_OP_POSIX_QUERY_INFO:
406 		if (rc == 0) {
407 			qi_rsp = (struct smb2_query_info_rsp *)
408 				rsp_iov[1].iov_base;
409 			rc = smb2_validate_and_copy_iov(
410 				le16_to_cpu(qi_rsp->OutputBufferOffset),
411 				le32_to_cpu(qi_rsp->OutputBufferLength),
412 				&rsp_iov[1], sizeof(struct smb311_posix_qinfo) /* add SIDs */, ptr);
413 		}
414 		if (rqst[1].rq_iov)
415 			SMB2_query_info_free(&rqst[1]);
416 		if (rqst[2].rq_iov)
417 			SMB2_close_free(&rqst[2]);
418 		if (rc)
419 			trace_smb3_posix_query_info_compound_err(xid,  ses->Suid, tcon->tid, rc);
420 		else
421 			trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
422 		break;
423 	case SMB2_OP_DELETE:
424 		if (rc)
425 			trace_smb3_delete_err(xid,  ses->Suid, tcon->tid, rc);
426 		else
427 			trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
428 		if (rqst[1].rq_iov)
429 			SMB2_close_free(&rqst[1]);
430 		break;
431 	case SMB2_OP_MKDIR:
432 		if (rc)
433 			trace_smb3_mkdir_err(xid,  ses->Suid, tcon->tid, rc);
434 		else
435 			trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
436 		if (rqst[1].rq_iov)
437 			SMB2_close_free(&rqst[1]);
438 		break;
439 	case SMB2_OP_HARDLINK:
440 		if (rc)
441 			trace_smb3_hardlink_err(xid,  ses->Suid, tcon->tid, rc);
442 		else
443 			trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
444 		free_set_inf_compound(rqst);
445 		break;
446 	case SMB2_OP_RENAME:
447 		if (rc)
448 			trace_smb3_rename_err(xid,  ses->Suid, tcon->tid, rc);
449 		else
450 			trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
451 		free_set_inf_compound(rqst);
452 		break;
453 	case SMB2_OP_RMDIR:
454 		if (rc)
455 			trace_smb3_rmdir_err(xid,  ses->Suid, tcon->tid, rc);
456 		else
457 			trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
458 		free_set_inf_compound(rqst);
459 		break;
460 	case SMB2_OP_SET_EOF:
461 		if (rc)
462 			trace_smb3_set_eof_err(xid,  ses->Suid, tcon->tid, rc);
463 		else
464 			trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
465 		free_set_inf_compound(rqst);
466 		break;
467 	case SMB2_OP_SET_INFO:
468 		if (rc)
469 			trace_smb3_set_info_compound_err(xid,  ses->Suid,
470 						tcon->tid, rc);
471 		else
472 			trace_smb3_set_info_compound_done(xid, ses->Suid,
473 						tcon->tid);
474 		free_set_inf_compound(rqst);
475 		break;
476 	}
477 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
478 	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
479 	free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
480 	kfree(vars);
481 	return rc;
482 }
483 
484 void
move_smb2_info_to_cifs(FILE_ALL_INFO * dst,struct smb2_file_all_info * src)485 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
486 {
487 	memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
488 	dst->CurrentByteOffset = src->CurrentByteOffset;
489 	dst->Mode = src->Mode;
490 	dst->AlignmentRequirement = src->AlignmentRequirement;
491 	dst->IndexNumber1 = 0; /* we don't use it */
492 }
493 
494 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)495 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
496 		     struct cifs_sb_info *cifs_sb, const char *full_path,
497 		     FILE_ALL_INFO *data, bool *adjust_tz, bool *reparse)
498 {
499 	int rc;
500 	struct smb2_file_all_info *smb2_data;
501 	__u32 create_options = 0;
502 	struct cifsFileInfo *cfile;
503 	struct cached_fid *cfid = NULL;
504 
505 	*adjust_tz = false;
506 	*reparse = false;
507 
508 	smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
509 			    GFP_KERNEL);
510 	if (smb2_data == NULL)
511 		return -ENOMEM;
512 
513 	/* If it is a root and its handle is cached then use it */
514 	rc = open_cached_dir(xid, tcon, full_path, cifs_sb, &cfid);
515 	if (!rc) {
516 		if (tcon->crfid.file_all_info_is_valid) {
517 			move_smb2_info_to_cifs(data,
518 					       &tcon->crfid.file_all_info);
519 		} else {
520 			rc = SMB2_query_info(xid, tcon,
521 					     cfid->fid->persistent_fid,
522 					     cfid->fid->volatile_fid, smb2_data);
523 			if (!rc)
524 				move_smb2_info_to_cifs(data, smb2_data);
525 		}
526 		close_cached_dir(cfid);
527 		goto out;
528 	}
529 
530 	cifs_get_readable_path(tcon, full_path, &cfile);
531 	rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
532 			      FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
533 			      ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
534 	if (rc == -EOPNOTSUPP) {
535 		*reparse = true;
536 		create_options |= OPEN_REPARSE_POINT;
537 
538 		/* Failed on a symbolic link - query a reparse point info */
539 		rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
540 				      FILE_READ_ATTRIBUTES, FILE_OPEN,
541 				      create_options, ACL_NO_MODE,
542 				      smb2_data, SMB2_OP_QUERY_INFO, NULL);
543 	}
544 	if (rc)
545 		goto out;
546 
547 	move_smb2_info_to_cifs(data, smb2_data);
548 out:
549 	kfree(smb2_data);
550 	return rc;
551 }
552 
553 
554 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)555 smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
556 		     struct cifs_sb_info *cifs_sb, const char *full_path,
557 		     struct smb311_posix_qinfo *data, bool *adjust_tz, bool *reparse)
558 {
559 	int rc;
560 	__u32 create_options = 0;
561 	struct cifsFileInfo *cfile;
562 	struct smb311_posix_qinfo *smb2_data;
563 
564 	*adjust_tz = false;
565 	*reparse = false;
566 
567 	/* BB TODO: Make struct larger when add support for parsing owner SIDs */
568 	smb2_data = kzalloc(sizeof(struct smb311_posix_qinfo),
569 			    GFP_KERNEL);
570 	if (smb2_data == NULL)
571 		return -ENOMEM;
572 
573 	/*
574 	 * BB TODO: Add support for using the cached root handle.
575 	 * Create SMB2_query_posix_info worker function to do non-compounded query
576 	 * when we already have an open file handle for this. For now this is fast enough
577 	 * (always using the compounded version).
578 	 */
579 
580 	cifs_get_readable_path(tcon, full_path, &cfile);
581 	rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
582 			      FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
583 			      ACL_NO_MODE, smb2_data, SMB2_OP_POSIX_QUERY_INFO, cfile);
584 	if (rc == -EOPNOTSUPP) {
585 		/* BB TODO: When support for special files added to Samba re-verify this path */
586 		*reparse = true;
587 		create_options |= OPEN_REPARSE_POINT;
588 
589 		/* Failed on a symbolic link - query a reparse point info */
590 		rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
591 				      FILE_READ_ATTRIBUTES, FILE_OPEN,
592 				      create_options, ACL_NO_MODE,
593 				      smb2_data, SMB2_OP_POSIX_QUERY_INFO, NULL);
594 	}
595 	if (rc)
596 		goto out;
597 
598 	 /* TODO: will need to allow for the 2 SIDs when add support for getting owner UID/GID */
599 	memcpy(data, smb2_data, sizeof(struct smb311_posix_qinfo));
600 
601 out:
602 	kfree(smb2_data);
603 	return rc;
604 }
605 
606 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)607 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
608 	   struct cifs_tcon *tcon, const char *name,
609 	   struct cifs_sb_info *cifs_sb)
610 {
611 	return smb2_compound_op(xid, tcon, cifs_sb, name,
612 				FILE_WRITE_ATTRIBUTES, FILE_CREATE,
613 				CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
614 				NULL);
615 }
616 
617 void
smb2_mkdir_setinfo(struct inode * inode,const char * name,struct cifs_sb_info * cifs_sb,struct cifs_tcon * tcon,const unsigned int xid)618 smb2_mkdir_setinfo(struct inode *inode, const char *name,
619 		   struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
620 		   const unsigned int xid)
621 {
622 	FILE_BASIC_INFO data;
623 	struct cifsInodeInfo *cifs_i;
624 	struct cifsFileInfo *cfile;
625 	u32 dosattrs;
626 	int tmprc;
627 
628 	memset(&data, 0, sizeof(data));
629 	cifs_i = CIFS_I(inode);
630 	dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
631 	data.Attributes = cpu_to_le32(dosattrs);
632 	cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
633 	tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
634 				 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
635 				 CREATE_NOT_FILE, ACL_NO_MODE,
636 				 &data, SMB2_OP_SET_INFO, cfile);
637 	if (tmprc == 0)
638 		cifs_i->cifsAttrs = dosattrs;
639 }
640 
641 int
smb2_rmdir(const unsigned int xid,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb)642 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
643 	   struct cifs_sb_info *cifs_sb)
644 {
645 	return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
646 				CREATE_NOT_FILE, ACL_NO_MODE,
647 				NULL, SMB2_OP_RMDIR, NULL);
648 }
649 
650 int
smb2_unlink(const unsigned int xid,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb)651 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
652 	    struct cifs_sb_info *cifs_sb)
653 {
654 	return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
655 				CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
656 				ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
657 }
658 
659 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)660 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
661 		   const char *from_name, const char *to_name,
662 		   struct cifs_sb_info *cifs_sb, __u32 access, int command,
663 		   struct cifsFileInfo *cfile)
664 {
665 	__le16 *smb2_to_name = NULL;
666 	int rc;
667 
668 	smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
669 	if (smb2_to_name == NULL) {
670 		rc = -ENOMEM;
671 		goto smb2_rename_path;
672 	}
673 	rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
674 			      FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
675 			      command, cfile);
676 smb2_rename_path:
677 	kfree(smb2_to_name);
678 	return rc;
679 }
680 
681 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)682 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
683 		 const char *from_name, const char *to_name,
684 		 struct cifs_sb_info *cifs_sb)
685 {
686 	struct cifsFileInfo *cfile;
687 
688 	cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
689 
690 	return smb2_set_path_attr(xid, tcon, from_name, to_name,
691 				  cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
692 }
693 
694 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)695 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
696 		     const char *from_name, const char *to_name,
697 		     struct cifs_sb_info *cifs_sb)
698 {
699 	return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
700 				  FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
701 				  NULL);
702 }
703 
704 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)705 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
706 		   const char *full_path, __u64 size,
707 		   struct cifs_sb_info *cifs_sb, bool set_alloc)
708 {
709 	__le64 eof = cpu_to_le64(size);
710 
711 	return smb2_compound_op(xid, tcon, cifs_sb, full_path,
712 				FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
713 				&eof, SMB2_OP_SET_EOF, NULL);
714 }
715 
716 int
smb2_set_file_info(struct inode * inode,const char * full_path,FILE_BASIC_INFO * buf,const unsigned int xid)717 smb2_set_file_info(struct inode *inode, const char *full_path,
718 		   FILE_BASIC_INFO *buf, const unsigned int xid)
719 {
720 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
721 	struct tcon_link *tlink;
722 	int rc;
723 
724 	if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
725 	    (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
726 	    (buf->Attributes == 0))
727 		return 0; /* would be a no op, no sense sending this */
728 
729 	tlink = cifs_sb_tlink(cifs_sb);
730 	if (IS_ERR(tlink))
731 		return PTR_ERR(tlink);
732 
733 	rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
734 			      FILE_WRITE_ATTRIBUTES, FILE_OPEN,
735 			      0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, NULL);
736 	cifs_put_tlink(tlink);
737 	return rc;
738 }
739