xref: /linux/fs/smb/server/vfs.c (revision 5dfe7a7e52ccdf60dfd11ccbe509e4365ea721ca)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/fs.h>
9 #include <linux/filelock.h>
10 #include <linux/uaccess.h>
11 #include <linux/backing-dev.h>
12 #include <linux/writeback.h>
13 #include <linux/xattr.h>
14 #include <linux/falloc.h>
15 #include <linux/fsnotify.h>
16 #include <linux/dcache.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/sched/xacct.h>
20 #include <linux/crc32c.h>
21 #include <linux/namei.h>
22 
23 #include "glob.h"
24 #include "oplock.h"
25 #include "connection.h"
26 #include "vfs.h"
27 #include "vfs_cache.h"
28 #include "smbacl.h"
29 #include "ndr.h"
30 #include "auth.h"
31 #include "misc.h"
32 
33 #include "smb_common.h"
34 #include "mgmt/share_config.h"
35 #include "mgmt/tree_connect.h"
36 #include "mgmt/user_session.h"
37 #include "mgmt/user_config.h"
38 
39 static void ksmbd_vfs_inherit_owner(struct ksmbd_work *work,
40 				    struct inode *parent_inode,
41 				    struct inode *inode)
42 {
43 	if (!test_share_config_flag(work->tcon->share_conf,
44 				    KSMBD_SHARE_FLAG_INHERIT_OWNER))
45 		return;
46 
47 	i_uid_write(inode, i_uid_read(parent_inode));
48 }
49 
50 /**
51  * ksmbd_vfs_lock_parent() - lock parent dentry if it is stable
52  */
53 int ksmbd_vfs_lock_parent(struct dentry *parent, struct dentry *child)
54 {
55 	inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
56 	if (child->d_parent != parent) {
57 		inode_unlock(d_inode(parent));
58 		return -ENOENT;
59 	}
60 
61 	return 0;
62 }
63 
64 static int ksmbd_vfs_path_lookup_locked(struct ksmbd_share_config *share_conf,
65 					char *pathname, unsigned int flags,
66 					struct path *path)
67 {
68 	struct qstr last;
69 	struct filename *filename;
70 	struct path *root_share_path = &share_conf->vfs_path;
71 	int err, type;
72 	struct path parent_path;
73 	struct dentry *d;
74 
75 	if (pathname[0] == '\0') {
76 		pathname = share_conf->path;
77 		root_share_path = NULL;
78 	} else {
79 		flags |= LOOKUP_BENEATH;
80 	}
81 
82 	filename = getname_kernel(pathname);
83 	if (IS_ERR(filename))
84 		return PTR_ERR(filename);
85 
86 	err = vfs_path_parent_lookup(filename, flags,
87 				     &parent_path, &last, &type,
88 				     root_share_path);
89 	if (err) {
90 		putname(filename);
91 		return err;
92 	}
93 
94 	if (unlikely(type != LAST_NORM)) {
95 		path_put(&parent_path);
96 		putname(filename);
97 		return -ENOENT;
98 	}
99 
100 	inode_lock_nested(parent_path.dentry->d_inode, I_MUTEX_PARENT);
101 	d = lookup_one_qstr_excl(&last, parent_path.dentry, 0);
102 	if (IS_ERR(d))
103 		goto err_out;
104 
105 	if (d_is_negative(d)) {
106 		dput(d);
107 		goto err_out;
108 	}
109 
110 	path->dentry = d;
111 	path->mnt = share_conf->vfs_path.mnt;
112 	path_put(&parent_path);
113 	putname(filename);
114 
115 	return 0;
116 
117 err_out:
118 	inode_unlock(parent_path.dentry->d_inode);
119 	path_put(&parent_path);
120 	putname(filename);
121 	return -ENOENT;
122 }
123 
124 int ksmbd_vfs_query_maximal_access(struct mnt_idmap *idmap,
125 				   struct dentry *dentry, __le32 *daccess)
126 {
127 	int ret = 0;
128 
129 	*daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL);
130 
131 	if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_WRITE))
132 		*daccess |= cpu_to_le32(WRITE_DAC | WRITE_OWNER | SYNCHRONIZE |
133 				FILE_WRITE_DATA | FILE_APPEND_DATA |
134 				FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES |
135 				FILE_DELETE_CHILD);
136 
137 	if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_READ))
138 		*daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE;
139 
140 	if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_EXEC))
141 		*daccess |= FILE_EXECUTE_LE;
142 
143 	if (!inode_permission(idmap, d_inode(dentry->d_parent), MAY_EXEC | MAY_WRITE))
144 		*daccess |= FILE_DELETE_LE;
145 
146 	return ret;
147 }
148 
149 /**
150  * ksmbd_vfs_create() - vfs helper for smb create file
151  * @work:	work
152  * @name:	file name that is relative to share
153  * @mode:	file create mode
154  *
155  * Return:	0 on success, otherwise error
156  */
157 int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode)
158 {
159 	struct path path;
160 	struct dentry *dentry;
161 	int err;
162 
163 	dentry = ksmbd_vfs_kern_path_create(work, name,
164 					    LOOKUP_NO_SYMLINKS, &path);
165 	if (IS_ERR(dentry)) {
166 		err = PTR_ERR(dentry);
167 		if (err != -ENOENT)
168 			pr_err("path create failed for %s, err %d\n",
169 			       name, err);
170 		return err;
171 	}
172 
173 	err = mnt_want_write(path.mnt);
174 	if (err)
175 		goto out_err;
176 
177 	mode |= S_IFREG;
178 	err = vfs_create(mnt_idmap(path.mnt), d_inode(path.dentry),
179 			 dentry, mode, true);
180 	if (!err) {
181 		ksmbd_vfs_inherit_owner(work, d_inode(path.dentry),
182 					d_inode(dentry));
183 	} else {
184 		pr_err("File(%s): creation failed (err:%d)\n", name, err);
185 	}
186 	mnt_drop_write(path.mnt);
187 
188 out_err:
189 	done_path_create(&path, dentry);
190 	return err;
191 }
192 
193 /**
194  * ksmbd_vfs_mkdir() - vfs helper for smb create directory
195  * @work:	work
196  * @name:	directory name that is relative to share
197  * @mode:	directory create mode
198  *
199  * Return:	0 on success, otherwise error
200  */
201 int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode)
202 {
203 	struct mnt_idmap *idmap;
204 	struct path path;
205 	struct dentry *dentry;
206 	int err;
207 
208 	dentry = ksmbd_vfs_kern_path_create(work, name,
209 					    LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY,
210 					    &path);
211 	if (IS_ERR(dentry)) {
212 		err = PTR_ERR(dentry);
213 		if (err != -EEXIST)
214 			ksmbd_debug(VFS, "path create failed for %s, err %d\n",
215 				    name, err);
216 		return err;
217 	}
218 
219 	err = mnt_want_write(path.mnt);
220 	if (err)
221 		goto out_err2;
222 
223 	idmap = mnt_idmap(path.mnt);
224 	mode |= S_IFDIR;
225 	err = vfs_mkdir(idmap, d_inode(path.dentry), dentry, mode);
226 	if (!err && d_unhashed(dentry)) {
227 		struct dentry *d;
228 
229 		d = lookup_one(idmap, dentry->d_name.name, dentry->d_parent,
230 			       dentry->d_name.len);
231 		if (IS_ERR(d)) {
232 			err = PTR_ERR(d);
233 			goto out_err1;
234 		}
235 		if (unlikely(d_is_negative(d))) {
236 			dput(d);
237 			err = -ENOENT;
238 			goto out_err1;
239 		}
240 
241 		ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), d_inode(d));
242 		dput(d);
243 	}
244 
245 out_err1:
246 	mnt_drop_write(path.mnt);
247 out_err2:
248 	done_path_create(&path, dentry);
249 	if (err)
250 		pr_err("mkdir(%s): creation failed (err:%d)\n", name, err);
251 	return err;
252 }
253 
254 static ssize_t ksmbd_vfs_getcasexattr(struct mnt_idmap *idmap,
255 				      struct dentry *dentry, char *attr_name,
256 				      int attr_name_len, char **attr_value)
257 {
258 	char *name, *xattr_list = NULL;
259 	ssize_t value_len = -ENOENT, xattr_list_len;
260 
261 	xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
262 	if (xattr_list_len <= 0)
263 		goto out;
264 
265 	for (name = xattr_list; name - xattr_list < xattr_list_len;
266 			name += strlen(name) + 1) {
267 		ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
268 		if (strncasecmp(attr_name, name, attr_name_len))
269 			continue;
270 
271 		value_len = ksmbd_vfs_getxattr(idmap,
272 					       dentry,
273 					       name,
274 					       attr_value);
275 		if (value_len < 0)
276 			pr_err("failed to get xattr in file\n");
277 		break;
278 	}
279 
280 out:
281 	kvfree(xattr_list);
282 	return value_len;
283 }
284 
285 static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos,
286 				 size_t count)
287 {
288 	ssize_t v_len;
289 	char *stream_buf = NULL;
290 
291 	ksmbd_debug(VFS, "read stream data pos : %llu, count : %zd\n",
292 		    *pos, count);
293 
294 	v_len = ksmbd_vfs_getcasexattr(file_mnt_idmap(fp->filp),
295 				       fp->filp->f_path.dentry,
296 				       fp->stream.name,
297 				       fp->stream.size,
298 				       &stream_buf);
299 	if ((int)v_len <= 0)
300 		return (int)v_len;
301 
302 	if (v_len <= *pos) {
303 		count = -EINVAL;
304 		goto free_buf;
305 	}
306 
307 	if (v_len - *pos < count)
308 		count = v_len - *pos;
309 
310 	memcpy(buf, &stream_buf[*pos], count);
311 
312 free_buf:
313 	kvfree(stream_buf);
314 	return count;
315 }
316 
317 /**
318  * check_lock_range() - vfs helper for smb byte range file locking
319  * @filp:	the file to apply the lock to
320  * @start:	lock start byte offset
321  * @end:	lock end byte offset
322  * @type:	byte range type read/write
323  *
324  * Return:	0 on success, otherwise error
325  */
326 static int check_lock_range(struct file *filp, loff_t start, loff_t end,
327 			    unsigned char type)
328 {
329 	struct file_lock *flock;
330 	struct file_lock_context *ctx = locks_inode_context(file_inode(filp));
331 	int error = 0;
332 
333 	if (!ctx || list_empty_careful(&ctx->flc_posix))
334 		return 0;
335 
336 	spin_lock(&ctx->flc_lock);
337 	list_for_each_entry(flock, &ctx->flc_posix, fl_list) {
338 		/* check conflict locks */
339 		if (flock->fl_end >= start && end >= flock->fl_start) {
340 			if (flock->fl_type == F_RDLCK) {
341 				if (type == WRITE) {
342 					pr_err("not allow write by shared lock\n");
343 					error = 1;
344 					goto out;
345 				}
346 			} else if (flock->fl_type == F_WRLCK) {
347 				/* check owner in lock */
348 				if (flock->fl_file != filp) {
349 					error = 1;
350 					pr_err("not allow rw access by exclusive lock from other opens\n");
351 					goto out;
352 				}
353 			}
354 		}
355 	}
356 out:
357 	spin_unlock(&ctx->flc_lock);
358 	return error;
359 }
360 
361 /**
362  * ksmbd_vfs_read() - vfs helper for smb file read
363  * @work:	smb work
364  * @fid:	file id of open file
365  * @count:	read byte count
366  * @pos:	file pos
367  *
368  * Return:	number of read bytes on success, otherwise error
369  */
370 int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count,
371 		   loff_t *pos)
372 {
373 	struct file *filp = fp->filp;
374 	ssize_t nbytes = 0;
375 	char *rbuf = work->aux_payload_buf;
376 	struct inode *inode = file_inode(filp);
377 
378 	if (S_ISDIR(inode->i_mode))
379 		return -EISDIR;
380 
381 	if (unlikely(count == 0))
382 		return 0;
383 
384 	if (work->conn->connection_type) {
385 		if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
386 			pr_err("no right to read(%pD)\n", fp->filp);
387 			return -EACCES;
388 		}
389 	}
390 
391 	if (ksmbd_stream_fd(fp))
392 		return ksmbd_vfs_stream_read(fp, rbuf, pos, count);
393 
394 	if (!work->tcon->posix_extensions) {
395 		int ret;
396 
397 		ret = check_lock_range(filp, *pos, *pos + count - 1, READ);
398 		if (ret) {
399 			pr_err("unable to read due to lock\n");
400 			return -EAGAIN;
401 		}
402 	}
403 
404 	nbytes = kernel_read(filp, rbuf, count, pos);
405 	if (nbytes < 0) {
406 		pr_err("smb read failed, err = %zd\n", nbytes);
407 		return nbytes;
408 	}
409 
410 	filp->f_pos = *pos;
411 	return nbytes;
412 }
413 
414 static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
415 				  size_t count)
416 {
417 	char *stream_buf = NULL, *wbuf;
418 	struct mnt_idmap *idmap = file_mnt_idmap(fp->filp);
419 	size_t size, v_len;
420 	int err = 0;
421 
422 	ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n",
423 		    *pos, count);
424 
425 	size = *pos + count;
426 	if (size > XATTR_SIZE_MAX) {
427 		size = XATTR_SIZE_MAX;
428 		count = (*pos + count) - XATTR_SIZE_MAX;
429 	}
430 
431 	v_len = ksmbd_vfs_getcasexattr(idmap,
432 				       fp->filp->f_path.dentry,
433 				       fp->stream.name,
434 				       fp->stream.size,
435 				       &stream_buf);
436 	if ((int)v_len < 0) {
437 		pr_err("not found stream in xattr : %zd\n", v_len);
438 		err = (int)v_len;
439 		goto out;
440 	}
441 
442 	if (v_len < size) {
443 		wbuf = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
444 		if (!wbuf) {
445 			err = -ENOMEM;
446 			goto out;
447 		}
448 
449 		if (v_len > 0)
450 			memcpy(wbuf, stream_buf, v_len);
451 		kvfree(stream_buf);
452 		stream_buf = wbuf;
453 	}
454 
455 	memcpy(&stream_buf[*pos], buf, count);
456 
457 	err = ksmbd_vfs_setxattr(idmap,
458 				 &fp->filp->f_path,
459 				 fp->stream.name,
460 				 (void *)stream_buf,
461 				 size,
462 				 0);
463 	if (err < 0)
464 		goto out;
465 
466 	fp->filp->f_pos = *pos;
467 	err = 0;
468 out:
469 	kvfree(stream_buf);
470 	return err;
471 }
472 
473 /**
474  * ksmbd_vfs_write() - vfs helper for smb file write
475  * @work:	work
476  * @fid:	file id of open file
477  * @buf:	buf containing data for writing
478  * @count:	read byte count
479  * @pos:	file pos
480  * @sync:	fsync after write
481  * @written:	number of bytes written
482  *
483  * Return:	0 on success, otherwise error
484  */
485 int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp,
486 		    char *buf, size_t count, loff_t *pos, bool sync,
487 		    ssize_t *written)
488 {
489 	struct file *filp;
490 	loff_t	offset = *pos;
491 	int err = 0;
492 
493 	if (work->conn->connection_type) {
494 		if (!(fp->daccess & FILE_WRITE_DATA_LE)) {
495 			pr_err("no right to write(%pD)\n", fp->filp);
496 			err = -EACCES;
497 			goto out;
498 		}
499 	}
500 
501 	filp = fp->filp;
502 
503 	if (ksmbd_stream_fd(fp)) {
504 		err = ksmbd_vfs_stream_write(fp, buf, pos, count);
505 		if (!err)
506 			*written = count;
507 		goto out;
508 	}
509 
510 	if (!work->tcon->posix_extensions) {
511 		err = check_lock_range(filp, *pos, *pos + count - 1, WRITE);
512 		if (err) {
513 			pr_err("unable to write due to lock\n");
514 			err = -EAGAIN;
515 			goto out;
516 		}
517 	}
518 
519 	/* Do we need to break any of a levelII oplock? */
520 	smb_break_all_levII_oplock(work, fp, 1);
521 
522 	err = kernel_write(filp, buf, count, pos);
523 	if (err < 0) {
524 		ksmbd_debug(VFS, "smb write failed, err = %d\n", err);
525 		goto out;
526 	}
527 
528 	filp->f_pos = *pos;
529 	*written = err;
530 	err = 0;
531 	if (sync) {
532 		err = vfs_fsync_range(filp, offset, offset + *written, 0);
533 		if (err < 0)
534 			pr_err("fsync failed for filename = %pD, err = %d\n",
535 			       fp->filp, err);
536 	}
537 
538 out:
539 	return err;
540 }
541 
542 /**
543  * ksmbd_vfs_getattr() - vfs helper for smb getattr
544  * @work:	work
545  * @fid:	file id of open file
546  * @attrs:	inode attributes
547  *
548  * Return:	0 on success, otherwise error
549  */
550 int ksmbd_vfs_getattr(const struct path *path, struct kstat *stat)
551 {
552 	int err;
553 
554 	err = vfs_getattr(path, stat, STATX_BTIME, AT_STATX_SYNC_AS_STAT);
555 	if (err)
556 		pr_err("getattr failed, err %d\n", err);
557 	return err;
558 }
559 
560 /**
561  * ksmbd_vfs_fsync() - vfs helper for smb fsync
562  * @work:	work
563  * @fid:	file id of open file
564  *
565  * Return:	0 on success, otherwise error
566  */
567 int ksmbd_vfs_fsync(struct ksmbd_work *work, u64 fid, u64 p_id)
568 {
569 	struct ksmbd_file *fp;
570 	int err;
571 
572 	fp = ksmbd_lookup_fd_slow(work, fid, p_id);
573 	if (!fp) {
574 		pr_err("failed to get filp for fid %llu\n", fid);
575 		return -ENOENT;
576 	}
577 	err = vfs_fsync(fp->filp, 0);
578 	if (err < 0)
579 		pr_err("smb fsync failed, err = %d\n", err);
580 	ksmbd_fd_put(work, fp);
581 	return err;
582 }
583 
584 /**
585  * ksmbd_vfs_remove_file() - vfs helper for smb rmdir or unlink
586  * @name:	directory or file name that is relative to share
587  *
588  * Return:	0 on success, otherwise error
589  */
590 int ksmbd_vfs_remove_file(struct ksmbd_work *work, const struct path *path)
591 {
592 	struct mnt_idmap *idmap;
593 	struct dentry *parent = path->dentry->d_parent;
594 	int err;
595 
596 	if (ksmbd_override_fsids(work))
597 		return -ENOMEM;
598 
599 	if (!d_inode(path->dentry)->i_nlink) {
600 		err = -ENOENT;
601 		goto out_err;
602 	}
603 
604 	err = mnt_want_write(path->mnt);
605 	if (err)
606 		goto out_err;
607 
608 	idmap = mnt_idmap(path->mnt);
609 	if (S_ISDIR(d_inode(path->dentry)->i_mode)) {
610 		err = vfs_rmdir(idmap, d_inode(parent), path->dentry);
611 		if (err && err != -ENOTEMPTY)
612 			ksmbd_debug(VFS, "rmdir failed, err %d\n", err);
613 	} else {
614 		err = vfs_unlink(idmap, d_inode(parent), path->dentry, NULL);
615 		if (err)
616 			ksmbd_debug(VFS, "unlink failed, err %d\n", err);
617 	}
618 	mnt_drop_write(path->mnt);
619 
620 out_err:
621 	ksmbd_revert_fsids(work);
622 	return err;
623 }
624 
625 /**
626  * ksmbd_vfs_link() - vfs helper for creating smb hardlink
627  * @oldname:	source file name
628  * @newname:	hardlink name that is relative to share
629  *
630  * Return:	0 on success, otherwise error
631  */
632 int ksmbd_vfs_link(struct ksmbd_work *work, const char *oldname,
633 		   const char *newname)
634 {
635 	struct path oldpath, newpath;
636 	struct dentry *dentry;
637 	int err;
638 
639 	if (ksmbd_override_fsids(work))
640 		return -ENOMEM;
641 
642 	err = kern_path(oldname, LOOKUP_NO_SYMLINKS, &oldpath);
643 	if (err) {
644 		pr_err("cannot get linux path for %s, err = %d\n",
645 		       oldname, err);
646 		goto out1;
647 	}
648 
649 	dentry = ksmbd_vfs_kern_path_create(work, newname,
650 					    LOOKUP_NO_SYMLINKS | LOOKUP_REVAL,
651 					    &newpath);
652 	if (IS_ERR(dentry)) {
653 		err = PTR_ERR(dentry);
654 		pr_err("path create err for %s, err %d\n", newname, err);
655 		goto out2;
656 	}
657 
658 	err = -EXDEV;
659 	if (oldpath.mnt != newpath.mnt) {
660 		pr_err("vfs_link failed err %d\n", err);
661 		goto out3;
662 	}
663 
664 	err = mnt_want_write(newpath.mnt);
665 	if (err)
666 		goto out3;
667 
668 	err = vfs_link(oldpath.dentry, mnt_idmap(newpath.mnt),
669 		       d_inode(newpath.dentry),
670 		       dentry, NULL);
671 	if (err)
672 		ksmbd_debug(VFS, "vfs_link failed err %d\n", err);
673 	mnt_drop_write(newpath.mnt);
674 
675 out3:
676 	done_path_create(&newpath, dentry);
677 out2:
678 	path_put(&oldpath);
679 out1:
680 	ksmbd_revert_fsids(work);
681 	return err;
682 }
683 
684 int ksmbd_vfs_rename(struct ksmbd_work *work, const struct path *old_path,
685 		     char *newname, int flags)
686 {
687 	struct dentry *old_parent, *new_dentry, *trap;
688 	struct dentry *old_child = old_path->dentry;
689 	struct path new_path;
690 	struct qstr new_last;
691 	struct renamedata rd;
692 	struct filename *to;
693 	struct ksmbd_share_config *share_conf = work->tcon->share_conf;
694 	struct ksmbd_file *parent_fp;
695 	int new_type;
696 	int err, lookup_flags = LOOKUP_NO_SYMLINKS;
697 
698 	if (ksmbd_override_fsids(work))
699 		return -ENOMEM;
700 
701 	to = getname_kernel(newname);
702 	if (IS_ERR(to)) {
703 		err = PTR_ERR(to);
704 		goto revert_fsids;
705 	}
706 
707 retry:
708 	err = vfs_path_parent_lookup(to, lookup_flags | LOOKUP_BENEATH,
709 				     &new_path, &new_last, &new_type,
710 				     &share_conf->vfs_path);
711 	if (err)
712 		goto out1;
713 
714 	if (old_path->mnt != new_path.mnt) {
715 		err = -EXDEV;
716 		goto out2;
717 	}
718 
719 	err = mnt_want_write(old_path->mnt);
720 	if (err)
721 		goto out2;
722 
723 	trap = lock_rename_child(old_child, new_path.dentry);
724 
725 	old_parent = dget(old_child->d_parent);
726 	if (d_unhashed(old_child)) {
727 		err = -EINVAL;
728 		goto out3;
729 	}
730 
731 	parent_fp = ksmbd_lookup_fd_inode(d_inode(old_child->d_parent));
732 	if (parent_fp) {
733 		if (parent_fp->daccess & FILE_DELETE_LE) {
734 			pr_err("parent dir is opened with delete access\n");
735 			err = -ESHARE;
736 			ksmbd_fd_put(work, parent_fp);
737 			goto out3;
738 		}
739 		ksmbd_fd_put(work, parent_fp);
740 	}
741 
742 	new_dentry = lookup_one_qstr_excl(&new_last, new_path.dentry,
743 					  lookup_flags | LOOKUP_RENAME_TARGET);
744 	if (IS_ERR(new_dentry)) {
745 		err = PTR_ERR(new_dentry);
746 		goto out3;
747 	}
748 
749 	if (d_is_symlink(new_dentry)) {
750 		err = -EACCES;
751 		goto out4;
752 	}
753 
754 	if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) {
755 		err = -EEXIST;
756 		goto out4;
757 	}
758 
759 	if (old_child == trap) {
760 		err = -EINVAL;
761 		goto out4;
762 	}
763 
764 	if (new_dentry == trap) {
765 		err = -ENOTEMPTY;
766 		goto out4;
767 	}
768 
769 	rd.old_mnt_idmap	= mnt_idmap(old_path->mnt),
770 	rd.old_dir		= d_inode(old_parent),
771 	rd.old_dentry		= old_child,
772 	rd.new_mnt_idmap	= mnt_idmap(new_path.mnt),
773 	rd.new_dir		= new_path.dentry->d_inode,
774 	rd.new_dentry		= new_dentry,
775 	rd.flags		= flags,
776 	rd.delegated_inode	= NULL,
777 	err = vfs_rename(&rd);
778 	if (err)
779 		ksmbd_debug(VFS, "vfs_rename failed err %d\n", err);
780 
781 out4:
782 	dput(new_dentry);
783 out3:
784 	dput(old_parent);
785 	unlock_rename(old_parent, new_path.dentry);
786 	mnt_drop_write(old_path->mnt);
787 out2:
788 	path_put(&new_path);
789 
790 	if (retry_estale(err, lookup_flags)) {
791 		lookup_flags |= LOOKUP_REVAL;
792 		goto retry;
793 	}
794 out1:
795 	putname(to);
796 revert_fsids:
797 	ksmbd_revert_fsids(work);
798 	return err;
799 }
800 
801 /**
802  * ksmbd_vfs_truncate() - vfs helper for smb file truncate
803  * @work:	work
804  * @fid:	file id of old file
805  * @size:	truncate to given size
806  *
807  * Return:	0 on success, otherwise error
808  */
809 int ksmbd_vfs_truncate(struct ksmbd_work *work,
810 		       struct ksmbd_file *fp, loff_t size)
811 {
812 	int err = 0;
813 	struct file *filp;
814 
815 	filp = fp->filp;
816 
817 	/* Do we need to break any of a levelII oplock? */
818 	smb_break_all_levII_oplock(work, fp, 1);
819 
820 	if (!work->tcon->posix_extensions) {
821 		struct inode *inode = file_inode(filp);
822 
823 		if (size < inode->i_size) {
824 			err = check_lock_range(filp, size,
825 					       inode->i_size - 1, WRITE);
826 		} else {
827 			err = check_lock_range(filp, inode->i_size,
828 					       size - 1, WRITE);
829 		}
830 
831 		if (err) {
832 			pr_err("failed due to lock\n");
833 			return -EAGAIN;
834 		}
835 	}
836 
837 	err = vfs_truncate(&filp->f_path, size);
838 	if (err)
839 		pr_err("truncate failed, err %d\n", err);
840 	return err;
841 }
842 
843 /**
844  * ksmbd_vfs_listxattr() - vfs helper for smb list extended attributes
845  * @dentry:	dentry of file for listing xattrs
846  * @list:	destination buffer
847  * @size:	destination buffer length
848  *
849  * Return:	xattr list length on success, otherwise error
850  */
851 ssize_t ksmbd_vfs_listxattr(struct dentry *dentry, char **list)
852 {
853 	ssize_t size;
854 	char *vlist = NULL;
855 
856 	size = vfs_listxattr(dentry, NULL, 0);
857 	if (size <= 0)
858 		return size;
859 
860 	vlist = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
861 	if (!vlist)
862 		return -ENOMEM;
863 
864 	*list = vlist;
865 	size = vfs_listxattr(dentry, vlist, size);
866 	if (size < 0) {
867 		ksmbd_debug(VFS, "listxattr failed\n");
868 		kvfree(vlist);
869 		*list = NULL;
870 	}
871 
872 	return size;
873 }
874 
875 static ssize_t ksmbd_vfs_xattr_len(struct mnt_idmap *idmap,
876 				   struct dentry *dentry, char *xattr_name)
877 {
878 	return vfs_getxattr(idmap, dentry, xattr_name, NULL, 0);
879 }
880 
881 /**
882  * ksmbd_vfs_getxattr() - vfs helper for smb get extended attributes value
883  * @idmap:	idmap
884  * @dentry:	dentry of file for getting xattrs
885  * @xattr_name:	name of xattr name to query
886  * @xattr_buf:	destination buffer xattr value
887  *
888  * Return:	read xattr value length on success, otherwise error
889  */
890 ssize_t ksmbd_vfs_getxattr(struct mnt_idmap *idmap,
891 			   struct dentry *dentry,
892 			   char *xattr_name, char **xattr_buf)
893 {
894 	ssize_t xattr_len;
895 	char *buf;
896 
897 	*xattr_buf = NULL;
898 	xattr_len = ksmbd_vfs_xattr_len(idmap, dentry, xattr_name);
899 	if (xattr_len < 0)
900 		return xattr_len;
901 
902 	buf = kmalloc(xattr_len + 1, GFP_KERNEL);
903 	if (!buf)
904 		return -ENOMEM;
905 
906 	xattr_len = vfs_getxattr(idmap, dentry, xattr_name,
907 				 (void *)buf, xattr_len);
908 	if (xattr_len > 0)
909 		*xattr_buf = buf;
910 	else
911 		kfree(buf);
912 	return xattr_len;
913 }
914 
915 /**
916  * ksmbd_vfs_setxattr() - vfs helper for smb set extended attributes value
917  * @idmap:	idmap of the relevant mount
918  * @dentry:	dentry to set XATTR at
919  * @attr_name:	xattr name for setxattr
920  * @attr_value:	xattr value to set
921  * @attr_size:	size of xattr value
922  * @flags:	destination buffer length
923  *
924  * Return:	0 on success, otherwise error
925  */
926 int ksmbd_vfs_setxattr(struct mnt_idmap *idmap,
927 		       const struct path *path, const char *attr_name,
928 		       void *attr_value, size_t attr_size, int flags)
929 {
930 	int err;
931 
932 	err = mnt_want_write(path->mnt);
933 	if (err)
934 		return err;
935 
936 	err = vfs_setxattr(idmap,
937 			   path->dentry,
938 			   attr_name,
939 			   attr_value,
940 			   attr_size,
941 			   flags);
942 	if (err)
943 		ksmbd_debug(VFS, "setxattr failed, err %d\n", err);
944 	mnt_drop_write(path->mnt);
945 	return err;
946 }
947 
948 /**
949  * ksmbd_vfs_set_fadvise() - convert smb IO caching options to linux options
950  * @filp:	file pointer for IO
951  * @options:	smb IO options
952  */
953 void ksmbd_vfs_set_fadvise(struct file *filp, __le32 option)
954 {
955 	struct address_space *mapping;
956 
957 	mapping = filp->f_mapping;
958 
959 	if (!option || !mapping)
960 		return;
961 
962 	if (option & FILE_WRITE_THROUGH_LE) {
963 		filp->f_flags |= O_SYNC;
964 	} else if (option & FILE_SEQUENTIAL_ONLY_LE) {
965 		filp->f_ra.ra_pages = inode_to_bdi(mapping->host)->ra_pages * 2;
966 		spin_lock(&filp->f_lock);
967 		filp->f_mode &= ~FMODE_RANDOM;
968 		spin_unlock(&filp->f_lock);
969 	} else if (option & FILE_RANDOM_ACCESS_LE) {
970 		spin_lock(&filp->f_lock);
971 		filp->f_mode |= FMODE_RANDOM;
972 		spin_unlock(&filp->f_lock);
973 	}
974 }
975 
976 int ksmbd_vfs_zero_data(struct ksmbd_work *work, struct ksmbd_file *fp,
977 			loff_t off, loff_t len)
978 {
979 	smb_break_all_levII_oplock(work, fp, 1);
980 	if (fp->f_ci->m_fattr & FILE_ATTRIBUTE_SPARSE_FILE_LE)
981 		return vfs_fallocate(fp->filp,
982 				     FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
983 				     off, len);
984 
985 	return vfs_fallocate(fp->filp,
986 			     FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE,
987 			     off, len);
988 }
989 
990 int ksmbd_vfs_fqar_lseek(struct ksmbd_file *fp, loff_t start, loff_t length,
991 			 struct file_allocated_range_buffer *ranges,
992 			 unsigned int in_count, unsigned int *out_count)
993 {
994 	struct file *f = fp->filp;
995 	struct inode *inode = file_inode(fp->filp);
996 	loff_t maxbytes = (u64)inode->i_sb->s_maxbytes, end;
997 	loff_t extent_start, extent_end;
998 	int ret = 0;
999 
1000 	if (start > maxbytes)
1001 		return -EFBIG;
1002 
1003 	if (!in_count)
1004 		return 0;
1005 
1006 	/*
1007 	 * Shrink request scope to what the fs can actually handle.
1008 	 */
1009 	if (length > maxbytes || (maxbytes - length) < start)
1010 		length = maxbytes - start;
1011 
1012 	if (start + length > inode->i_size)
1013 		length = inode->i_size - start;
1014 
1015 	*out_count = 0;
1016 	end = start + length;
1017 	while (start < end && *out_count < in_count) {
1018 		extent_start = vfs_llseek(f, start, SEEK_DATA);
1019 		if (extent_start < 0) {
1020 			if (extent_start != -ENXIO)
1021 				ret = (int)extent_start;
1022 			break;
1023 		}
1024 
1025 		if (extent_start >= end)
1026 			break;
1027 
1028 		extent_end = vfs_llseek(f, extent_start, SEEK_HOLE);
1029 		if (extent_end < 0) {
1030 			if (extent_end != -ENXIO)
1031 				ret = (int)extent_end;
1032 			break;
1033 		} else if (extent_start >= extent_end) {
1034 			break;
1035 		}
1036 
1037 		ranges[*out_count].file_offset = cpu_to_le64(extent_start);
1038 		ranges[(*out_count)++].length =
1039 			cpu_to_le64(min(extent_end, end) - extent_start);
1040 
1041 		start = extent_end;
1042 	}
1043 
1044 	return ret;
1045 }
1046 
1047 int ksmbd_vfs_remove_xattr(struct mnt_idmap *idmap,
1048 			   const struct path *path, char *attr_name)
1049 {
1050 	int err;
1051 
1052 	err = mnt_want_write(path->mnt);
1053 	if (err)
1054 		return err;
1055 
1056 	err = vfs_removexattr(idmap, path->dentry, attr_name);
1057 	mnt_drop_write(path->mnt);
1058 
1059 	return err;
1060 }
1061 
1062 int ksmbd_vfs_unlink(struct file *filp)
1063 {
1064 	int err = 0;
1065 	struct dentry *dir, *dentry = filp->f_path.dentry;
1066 	struct mnt_idmap *idmap = file_mnt_idmap(filp);
1067 
1068 	err = mnt_want_write(filp->f_path.mnt);
1069 	if (err)
1070 		return err;
1071 
1072 	dir = dget_parent(dentry);
1073 	err = ksmbd_vfs_lock_parent(dir, dentry);
1074 	if (err)
1075 		goto out;
1076 	dget(dentry);
1077 
1078 	if (S_ISDIR(d_inode(dentry)->i_mode))
1079 		err = vfs_rmdir(idmap, d_inode(dir), dentry);
1080 	else
1081 		err = vfs_unlink(idmap, d_inode(dir), dentry, NULL);
1082 
1083 	dput(dentry);
1084 	inode_unlock(d_inode(dir));
1085 	if (err)
1086 		ksmbd_debug(VFS, "failed to delete, err %d\n", err);
1087 out:
1088 	dput(dir);
1089 	mnt_drop_write(filp->f_path.mnt);
1090 
1091 	return err;
1092 }
1093 
1094 static bool __dir_empty(struct dir_context *ctx, const char *name, int namlen,
1095 		       loff_t offset, u64 ino, unsigned int d_type)
1096 {
1097 	struct ksmbd_readdir_data *buf;
1098 
1099 	buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
1100 	buf->dirent_count++;
1101 
1102 	return buf->dirent_count <= 2;
1103 }
1104 
1105 /**
1106  * ksmbd_vfs_empty_dir() - check for empty directory
1107  * @fp:	ksmbd file pointer
1108  *
1109  * Return:	true if directory empty, otherwise false
1110  */
1111 int ksmbd_vfs_empty_dir(struct ksmbd_file *fp)
1112 {
1113 	int err;
1114 	struct ksmbd_readdir_data readdir_data;
1115 
1116 	memset(&readdir_data, 0, sizeof(struct ksmbd_readdir_data));
1117 
1118 	set_ctx_actor(&readdir_data.ctx, __dir_empty);
1119 	readdir_data.dirent_count = 0;
1120 
1121 	err = iterate_dir(fp->filp, &readdir_data.ctx);
1122 	if (readdir_data.dirent_count > 2)
1123 		err = -ENOTEMPTY;
1124 	else
1125 		err = 0;
1126 	return err;
1127 }
1128 
1129 static bool __caseless_lookup(struct dir_context *ctx, const char *name,
1130 			     int namlen, loff_t offset, u64 ino,
1131 			     unsigned int d_type)
1132 {
1133 	struct ksmbd_readdir_data *buf;
1134 	int cmp = -EINVAL;
1135 
1136 	buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
1137 
1138 	if (buf->used != namlen)
1139 		return true;
1140 	if (IS_ENABLED(CONFIG_UNICODE) && buf->um) {
1141 		const struct qstr q_buf = {.name = buf->private,
1142 					   .len = buf->used};
1143 		const struct qstr q_name = {.name = name,
1144 					    .len = namlen};
1145 
1146 		cmp = utf8_strncasecmp(buf->um, &q_buf, &q_name);
1147 	}
1148 	if (cmp < 0)
1149 		cmp = strncasecmp((char *)buf->private, name, namlen);
1150 	if (!cmp) {
1151 		memcpy((char *)buf->private, name, namlen);
1152 		buf->dirent_count = 1;
1153 		return false;
1154 	}
1155 	return true;
1156 }
1157 
1158 /**
1159  * ksmbd_vfs_lookup_in_dir() - lookup a file in a directory
1160  * @dir:	path info
1161  * @name:	filename to lookup
1162  * @namelen:	filename length
1163  *
1164  * Return:	0 on success, otherwise error
1165  */
1166 static int ksmbd_vfs_lookup_in_dir(const struct path *dir, char *name,
1167 				   size_t namelen, struct unicode_map *um)
1168 {
1169 	int ret;
1170 	struct file *dfilp;
1171 	int flags = O_RDONLY | O_LARGEFILE;
1172 	struct ksmbd_readdir_data readdir_data = {
1173 		.ctx.actor	= __caseless_lookup,
1174 		.private	= name,
1175 		.used		= namelen,
1176 		.dirent_count	= 0,
1177 		.um		= um,
1178 	};
1179 
1180 	dfilp = dentry_open(dir, flags, current_cred());
1181 	if (IS_ERR(dfilp))
1182 		return PTR_ERR(dfilp);
1183 
1184 	ret = iterate_dir(dfilp, &readdir_data.ctx);
1185 	if (readdir_data.dirent_count > 0)
1186 		ret = 0;
1187 	fput(dfilp);
1188 	return ret;
1189 }
1190 
1191 /**
1192  * ksmbd_vfs_kern_path_locked() - lookup a file and get path info
1193  * @name:	file path that is relative to share
1194  * @flags:	lookup flags
1195  * @path:	if lookup succeed, return path info
1196  * @caseless:	caseless filename lookup
1197  *
1198  * Return:	0 on success, otherwise error
1199  */
1200 int ksmbd_vfs_kern_path_locked(struct ksmbd_work *work, char *name,
1201 			       unsigned int flags, struct path *path,
1202 			       bool caseless)
1203 {
1204 	struct ksmbd_share_config *share_conf = work->tcon->share_conf;
1205 	int err;
1206 	struct path parent_path;
1207 
1208 	err = ksmbd_vfs_path_lookup_locked(share_conf, name, flags, path);
1209 	if (!err)
1210 		return err;
1211 
1212 	if (caseless) {
1213 		char *filepath;
1214 		size_t path_len, remain_len;
1215 
1216 		filepath = kstrdup(name, GFP_KERNEL);
1217 		if (!filepath)
1218 			return -ENOMEM;
1219 
1220 		path_len = strlen(filepath);
1221 		remain_len = path_len;
1222 
1223 		parent_path = share_conf->vfs_path;
1224 		path_get(&parent_path);
1225 
1226 		while (d_can_lookup(parent_path.dentry)) {
1227 			char *filename = filepath + path_len - remain_len;
1228 			char *next = strchrnul(filename, '/');
1229 			size_t filename_len = next - filename;
1230 			bool is_last = !next[0];
1231 
1232 			if (filename_len == 0)
1233 				break;
1234 
1235 			err = ksmbd_vfs_lookup_in_dir(&parent_path, filename,
1236 						      filename_len,
1237 						      work->conn->um);
1238 			if (err)
1239 				goto out2;
1240 
1241 			next[0] = '\0';
1242 
1243 			err = vfs_path_lookup(share_conf->vfs_path.dentry,
1244 					      share_conf->vfs_path.mnt,
1245 					      filepath,
1246 					      flags,
1247 					      path);
1248 			if (err)
1249 				goto out2;
1250 			else if (is_last)
1251 				goto out1;
1252 			path_put(&parent_path);
1253 			parent_path = *path;
1254 
1255 			next[0] = '/';
1256 			remain_len -= filename_len + 1;
1257 		}
1258 
1259 		err = -EINVAL;
1260 out2:
1261 		path_put(&parent_path);
1262 out1:
1263 		kfree(filepath);
1264 	}
1265 
1266 	if (!err) {
1267 		err = ksmbd_vfs_lock_parent(parent_path.dentry, path->dentry);
1268 		if (err)
1269 			dput(path->dentry);
1270 		path_put(&parent_path);
1271 	}
1272 	return err;
1273 }
1274 
1275 struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work,
1276 					  const char *name,
1277 					  unsigned int flags,
1278 					  struct path *path)
1279 {
1280 	char *abs_name;
1281 	struct dentry *dent;
1282 
1283 	abs_name = convert_to_unix_name(work->tcon->share_conf, name);
1284 	if (!abs_name)
1285 		return ERR_PTR(-ENOMEM);
1286 
1287 	dent = kern_path_create(AT_FDCWD, abs_name, path, flags);
1288 	kfree(abs_name);
1289 	return dent;
1290 }
1291 
1292 int ksmbd_vfs_remove_acl_xattrs(struct mnt_idmap *idmap,
1293 				const struct path *path)
1294 {
1295 	char *name, *xattr_list = NULL;
1296 	ssize_t xattr_list_len;
1297 	int err = 0;
1298 
1299 	xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
1300 	if (xattr_list_len < 0) {
1301 		goto out;
1302 	} else if (!xattr_list_len) {
1303 		ksmbd_debug(SMB, "empty xattr in the file\n");
1304 		goto out;
1305 	}
1306 
1307 	err = mnt_want_write(path->mnt);
1308 	if (err)
1309 		goto out;
1310 
1311 	for (name = xattr_list; name - xattr_list < xattr_list_len;
1312 	     name += strlen(name) + 1) {
1313 		ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
1314 
1315 		if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
1316 			     sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1) ||
1317 		    !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
1318 			     sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1)) {
1319 			err = vfs_remove_acl(idmap, path->dentry, name);
1320 			if (err)
1321 				ksmbd_debug(SMB,
1322 					    "remove acl xattr failed : %s\n", name);
1323 		}
1324 	}
1325 	mnt_drop_write(path->mnt);
1326 
1327 out:
1328 	kvfree(xattr_list);
1329 	return err;
1330 }
1331 
1332 int ksmbd_vfs_remove_sd_xattrs(struct mnt_idmap *idmap, const struct path *path)
1333 {
1334 	char *name, *xattr_list = NULL;
1335 	ssize_t xattr_list_len;
1336 	int err = 0;
1337 
1338 	xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
1339 	if (xattr_list_len < 0) {
1340 		goto out;
1341 	} else if (!xattr_list_len) {
1342 		ksmbd_debug(SMB, "empty xattr in the file\n");
1343 		goto out;
1344 	}
1345 
1346 	for (name = xattr_list; name - xattr_list < xattr_list_len;
1347 			name += strlen(name) + 1) {
1348 		ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
1349 
1350 		if (!strncmp(name, XATTR_NAME_SD, XATTR_NAME_SD_LEN)) {
1351 			err = ksmbd_vfs_remove_xattr(idmap, path, name);
1352 			if (err)
1353 				ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
1354 		}
1355 	}
1356 out:
1357 	kvfree(xattr_list);
1358 	return err;
1359 }
1360 
1361 static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct mnt_idmap *idmap,
1362 							    struct inode *inode,
1363 							    int acl_type)
1364 {
1365 	struct xattr_smb_acl *smb_acl = NULL;
1366 	struct posix_acl *posix_acls;
1367 	struct posix_acl_entry *pa_entry;
1368 	struct xattr_acl_entry *xa_entry;
1369 	int i;
1370 
1371 	if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1372 		return NULL;
1373 
1374 	posix_acls = get_inode_acl(inode, acl_type);
1375 	if (IS_ERR_OR_NULL(posix_acls))
1376 		return NULL;
1377 
1378 	smb_acl = kzalloc(sizeof(struct xattr_smb_acl) +
1379 			  sizeof(struct xattr_acl_entry) * posix_acls->a_count,
1380 			  GFP_KERNEL);
1381 	if (!smb_acl)
1382 		goto out;
1383 
1384 	smb_acl->count = posix_acls->a_count;
1385 	pa_entry = posix_acls->a_entries;
1386 	xa_entry = smb_acl->entries;
1387 	for (i = 0; i < posix_acls->a_count; i++, pa_entry++, xa_entry++) {
1388 		switch (pa_entry->e_tag) {
1389 		case ACL_USER:
1390 			xa_entry->type = SMB_ACL_USER;
1391 			xa_entry->uid = posix_acl_uid_translate(idmap, pa_entry);
1392 			break;
1393 		case ACL_USER_OBJ:
1394 			xa_entry->type = SMB_ACL_USER_OBJ;
1395 			break;
1396 		case ACL_GROUP:
1397 			xa_entry->type = SMB_ACL_GROUP;
1398 			xa_entry->gid = posix_acl_gid_translate(idmap, pa_entry);
1399 			break;
1400 		case ACL_GROUP_OBJ:
1401 			xa_entry->type = SMB_ACL_GROUP_OBJ;
1402 			break;
1403 		case ACL_OTHER:
1404 			xa_entry->type = SMB_ACL_OTHER;
1405 			break;
1406 		case ACL_MASK:
1407 			xa_entry->type = SMB_ACL_MASK;
1408 			break;
1409 		default:
1410 			pr_err("unknown type : 0x%x\n", pa_entry->e_tag);
1411 			goto out;
1412 		}
1413 
1414 		if (pa_entry->e_perm & ACL_READ)
1415 			xa_entry->perm |= SMB_ACL_READ;
1416 		if (pa_entry->e_perm & ACL_WRITE)
1417 			xa_entry->perm |= SMB_ACL_WRITE;
1418 		if (pa_entry->e_perm & ACL_EXECUTE)
1419 			xa_entry->perm |= SMB_ACL_EXECUTE;
1420 	}
1421 out:
1422 	posix_acl_release(posix_acls);
1423 	return smb_acl;
1424 }
1425 
1426 int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
1427 			   struct mnt_idmap *idmap,
1428 			   const struct path *path,
1429 			   struct smb_ntsd *pntsd, int len)
1430 {
1431 	int rc;
1432 	struct ndr sd_ndr = {0}, acl_ndr = {0};
1433 	struct xattr_ntacl acl = {0};
1434 	struct xattr_smb_acl *smb_acl, *def_smb_acl = NULL;
1435 	struct dentry *dentry = path->dentry;
1436 	struct inode *inode = d_inode(dentry);
1437 
1438 	acl.version = 4;
1439 	acl.hash_type = XATTR_SD_HASH_TYPE_SHA256;
1440 	acl.current_time = ksmbd_UnixTimeToNT(current_time(inode));
1441 
1442 	memcpy(acl.desc, "posix_acl", 9);
1443 	acl.desc_len = 10;
1444 
1445 	pntsd->osidoffset =
1446 		cpu_to_le32(le32_to_cpu(pntsd->osidoffset) + NDR_NTSD_OFFSETOF);
1447 	pntsd->gsidoffset =
1448 		cpu_to_le32(le32_to_cpu(pntsd->gsidoffset) + NDR_NTSD_OFFSETOF);
1449 	pntsd->dacloffset =
1450 		cpu_to_le32(le32_to_cpu(pntsd->dacloffset) + NDR_NTSD_OFFSETOF);
1451 
1452 	acl.sd_buf = (char *)pntsd;
1453 	acl.sd_size = len;
1454 
1455 	rc = ksmbd_gen_sd_hash(conn, acl.sd_buf, acl.sd_size, acl.hash);
1456 	if (rc) {
1457 		pr_err("failed to generate hash for ndr acl\n");
1458 		return rc;
1459 	}
1460 
1461 	smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode,
1462 						 ACL_TYPE_ACCESS);
1463 	if (S_ISDIR(inode->i_mode))
1464 		def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode,
1465 							     ACL_TYPE_DEFAULT);
1466 
1467 	rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode,
1468 				  smb_acl, def_smb_acl);
1469 	if (rc) {
1470 		pr_err("failed to encode ndr to posix acl\n");
1471 		goto out;
1472 	}
1473 
1474 	rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset,
1475 			       acl.posix_acl_hash);
1476 	if (rc) {
1477 		pr_err("failed to generate hash for ndr acl\n");
1478 		goto out;
1479 	}
1480 
1481 	rc = ndr_encode_v4_ntacl(&sd_ndr, &acl);
1482 	if (rc) {
1483 		pr_err("failed to encode ndr to posix acl\n");
1484 		goto out;
1485 	}
1486 
1487 	rc = ksmbd_vfs_setxattr(idmap, path,
1488 				XATTR_NAME_SD, sd_ndr.data,
1489 				sd_ndr.offset, 0);
1490 	if (rc < 0)
1491 		pr_err("Failed to store XATTR ntacl :%d\n", rc);
1492 
1493 	kfree(sd_ndr.data);
1494 out:
1495 	kfree(acl_ndr.data);
1496 	kfree(smb_acl);
1497 	kfree(def_smb_acl);
1498 	return rc;
1499 }
1500 
1501 int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
1502 			   struct mnt_idmap *idmap,
1503 			   struct dentry *dentry,
1504 			   struct smb_ntsd **pntsd)
1505 {
1506 	int rc;
1507 	struct ndr n;
1508 	struct inode *inode = d_inode(dentry);
1509 	struct ndr acl_ndr = {0};
1510 	struct xattr_ntacl acl;
1511 	struct xattr_smb_acl *smb_acl = NULL, *def_smb_acl = NULL;
1512 	__u8 cmp_hash[XATTR_SD_HASH_SIZE] = {0};
1513 
1514 	rc = ksmbd_vfs_getxattr(idmap, dentry, XATTR_NAME_SD, &n.data);
1515 	if (rc <= 0)
1516 		return rc;
1517 
1518 	n.length = rc;
1519 	rc = ndr_decode_v4_ntacl(&n, &acl);
1520 	if (rc)
1521 		goto free_n_data;
1522 
1523 	smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode,
1524 						 ACL_TYPE_ACCESS);
1525 	if (S_ISDIR(inode->i_mode))
1526 		def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode,
1527 							     ACL_TYPE_DEFAULT);
1528 
1529 	rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode, smb_acl,
1530 				  def_smb_acl);
1531 	if (rc) {
1532 		pr_err("failed to encode ndr to posix acl\n");
1533 		goto out_free;
1534 	}
1535 
1536 	rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset, cmp_hash);
1537 	if (rc) {
1538 		pr_err("failed to generate hash for ndr acl\n");
1539 		goto out_free;
1540 	}
1541 
1542 	if (memcmp(cmp_hash, acl.posix_acl_hash, XATTR_SD_HASH_SIZE)) {
1543 		pr_err("hash value diff\n");
1544 		rc = -EINVAL;
1545 		goto out_free;
1546 	}
1547 
1548 	*pntsd = acl.sd_buf;
1549 	if (acl.sd_size < sizeof(struct smb_ntsd)) {
1550 		pr_err("sd size is invalid\n");
1551 		goto out_free;
1552 	}
1553 
1554 	(*pntsd)->osidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->osidoffset) -
1555 					   NDR_NTSD_OFFSETOF);
1556 	(*pntsd)->gsidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->gsidoffset) -
1557 					   NDR_NTSD_OFFSETOF);
1558 	(*pntsd)->dacloffset = cpu_to_le32(le32_to_cpu((*pntsd)->dacloffset) -
1559 					   NDR_NTSD_OFFSETOF);
1560 
1561 	rc = acl.sd_size;
1562 out_free:
1563 	kfree(acl_ndr.data);
1564 	kfree(smb_acl);
1565 	kfree(def_smb_acl);
1566 	if (rc < 0) {
1567 		kfree(acl.sd_buf);
1568 		*pntsd = NULL;
1569 	}
1570 
1571 free_n_data:
1572 	kfree(n.data);
1573 	return rc;
1574 }
1575 
1576 int ksmbd_vfs_set_dos_attrib_xattr(struct mnt_idmap *idmap,
1577 				   const struct path *path,
1578 				   struct xattr_dos_attrib *da)
1579 {
1580 	struct ndr n;
1581 	int err;
1582 
1583 	err = ndr_encode_dos_attr(&n, da);
1584 	if (err)
1585 		return err;
1586 
1587 	err = ksmbd_vfs_setxattr(idmap, path, XATTR_NAME_DOS_ATTRIBUTE,
1588 				 (void *)n.data, n.offset, 0);
1589 	if (err)
1590 		ksmbd_debug(SMB, "failed to store dos attribute in xattr\n");
1591 	kfree(n.data);
1592 
1593 	return err;
1594 }
1595 
1596 int ksmbd_vfs_get_dos_attrib_xattr(struct mnt_idmap *idmap,
1597 				   struct dentry *dentry,
1598 				   struct xattr_dos_attrib *da)
1599 {
1600 	struct ndr n;
1601 	int err;
1602 
1603 	err = ksmbd_vfs_getxattr(idmap, dentry, XATTR_NAME_DOS_ATTRIBUTE,
1604 				 (char **)&n.data);
1605 	if (err > 0) {
1606 		n.length = err;
1607 		if (ndr_decode_dos_attr(&n, da))
1608 			err = -EINVAL;
1609 		kfree(n.data);
1610 	} else {
1611 		ksmbd_debug(SMB, "failed to load dos attribute in xattr\n");
1612 	}
1613 
1614 	return err;
1615 }
1616 
1617 /**
1618  * ksmbd_vfs_init_kstat() - convert unix stat information to smb stat format
1619  * @p:          destination buffer
1620  * @ksmbd_kstat:      ksmbd kstat wrapper
1621  */
1622 void *ksmbd_vfs_init_kstat(char **p, struct ksmbd_kstat *ksmbd_kstat)
1623 {
1624 	struct file_directory_info *info = (struct file_directory_info *)(*p);
1625 	struct kstat *kstat = ksmbd_kstat->kstat;
1626 	u64 time;
1627 
1628 	info->FileIndex = 0;
1629 	info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
1630 	time = ksmbd_UnixTimeToNT(kstat->atime);
1631 	info->LastAccessTime = cpu_to_le64(time);
1632 	time = ksmbd_UnixTimeToNT(kstat->mtime);
1633 	info->LastWriteTime = cpu_to_le64(time);
1634 	time = ksmbd_UnixTimeToNT(kstat->ctime);
1635 	info->ChangeTime = cpu_to_le64(time);
1636 
1637 	if (ksmbd_kstat->file_attributes & FILE_ATTRIBUTE_DIRECTORY_LE) {
1638 		info->EndOfFile = 0;
1639 		info->AllocationSize = 0;
1640 	} else {
1641 		info->EndOfFile = cpu_to_le64(kstat->size);
1642 		info->AllocationSize = cpu_to_le64(kstat->blocks << 9);
1643 	}
1644 	info->ExtFileAttributes = ksmbd_kstat->file_attributes;
1645 
1646 	return info;
1647 }
1648 
1649 int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work,
1650 				struct mnt_idmap *idmap,
1651 				struct dentry *dentry,
1652 				struct ksmbd_kstat *ksmbd_kstat)
1653 {
1654 	u64 time;
1655 	int rc;
1656 
1657 	generic_fillattr(idmap, d_inode(dentry), ksmbd_kstat->kstat);
1658 
1659 	time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
1660 	ksmbd_kstat->create_time = time;
1661 
1662 	/*
1663 	 * set default value for the case that store dos attributes is not yes
1664 	 * or that acl is disable in server's filesystem and the config is yes.
1665 	 */
1666 	if (S_ISDIR(ksmbd_kstat->kstat->mode))
1667 		ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_DIRECTORY_LE;
1668 	else
1669 		ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_ARCHIVE_LE;
1670 
1671 	if (test_share_config_flag(work->tcon->share_conf,
1672 				   KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
1673 		struct xattr_dos_attrib da;
1674 
1675 		rc = ksmbd_vfs_get_dos_attrib_xattr(idmap, dentry, &da);
1676 		if (rc > 0) {
1677 			ksmbd_kstat->file_attributes = cpu_to_le32(da.attr);
1678 			ksmbd_kstat->create_time = da.create_time;
1679 		} else {
1680 			ksmbd_debug(VFS, "fail to load dos attribute.\n");
1681 		}
1682 	}
1683 
1684 	return 0;
1685 }
1686 
1687 ssize_t ksmbd_vfs_casexattr_len(struct mnt_idmap *idmap,
1688 				struct dentry *dentry, char *attr_name,
1689 				int attr_name_len)
1690 {
1691 	char *name, *xattr_list = NULL;
1692 	ssize_t value_len = -ENOENT, xattr_list_len;
1693 
1694 	xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
1695 	if (xattr_list_len <= 0)
1696 		goto out;
1697 
1698 	for (name = xattr_list; name - xattr_list < xattr_list_len;
1699 			name += strlen(name) + 1) {
1700 		ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
1701 		if (strncasecmp(attr_name, name, attr_name_len))
1702 			continue;
1703 
1704 		value_len = ksmbd_vfs_xattr_len(idmap, dentry, name);
1705 		break;
1706 	}
1707 
1708 out:
1709 	kvfree(xattr_list);
1710 	return value_len;
1711 }
1712 
1713 int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name,
1714 				size_t *xattr_stream_name_size, int s_type)
1715 {
1716 	char *type, *buf;
1717 
1718 	if (s_type == DIR_STREAM)
1719 		type = ":$INDEX_ALLOCATION";
1720 	else
1721 		type = ":$DATA";
1722 
1723 	buf = kasprintf(GFP_KERNEL, "%s%s%s",
1724 			XATTR_NAME_STREAM, stream_name,	type);
1725 	if (!buf)
1726 		return -ENOMEM;
1727 
1728 	*xattr_stream_name = buf;
1729 	*xattr_stream_name_size = strlen(buf) + 1;
1730 
1731 	return 0;
1732 }
1733 
1734 int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work,
1735 			       struct ksmbd_file *src_fp,
1736 			       struct ksmbd_file *dst_fp,
1737 			       struct srv_copychunk *chunks,
1738 			       unsigned int chunk_count,
1739 			       unsigned int *chunk_count_written,
1740 			       unsigned int *chunk_size_written,
1741 			       loff_t *total_size_written)
1742 {
1743 	unsigned int i;
1744 	loff_t src_off, dst_off, src_file_size;
1745 	size_t len;
1746 	int ret;
1747 
1748 	*chunk_count_written = 0;
1749 	*chunk_size_written = 0;
1750 	*total_size_written = 0;
1751 
1752 	if (!(src_fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
1753 		pr_err("no right to read(%pD)\n", src_fp->filp);
1754 		return -EACCES;
1755 	}
1756 	if (!(dst_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) {
1757 		pr_err("no right to write(%pD)\n", dst_fp->filp);
1758 		return -EACCES;
1759 	}
1760 
1761 	if (ksmbd_stream_fd(src_fp) || ksmbd_stream_fd(dst_fp))
1762 		return -EBADF;
1763 
1764 	smb_break_all_levII_oplock(work, dst_fp, 1);
1765 
1766 	if (!work->tcon->posix_extensions) {
1767 		for (i = 0; i < chunk_count; i++) {
1768 			src_off = le64_to_cpu(chunks[i].SourceOffset);
1769 			dst_off = le64_to_cpu(chunks[i].TargetOffset);
1770 			len = le32_to_cpu(chunks[i].Length);
1771 
1772 			if (check_lock_range(src_fp->filp, src_off,
1773 					     src_off + len - 1, READ))
1774 				return -EAGAIN;
1775 			if (check_lock_range(dst_fp->filp, dst_off,
1776 					     dst_off + len - 1, WRITE))
1777 				return -EAGAIN;
1778 		}
1779 	}
1780 
1781 	src_file_size = i_size_read(file_inode(src_fp->filp));
1782 
1783 	for (i = 0; i < chunk_count; i++) {
1784 		src_off = le64_to_cpu(chunks[i].SourceOffset);
1785 		dst_off = le64_to_cpu(chunks[i].TargetOffset);
1786 		len = le32_to_cpu(chunks[i].Length);
1787 
1788 		if (src_off + len > src_file_size)
1789 			return -E2BIG;
1790 
1791 		ret = vfs_copy_file_range(src_fp->filp, src_off,
1792 					  dst_fp->filp, dst_off, len, 0);
1793 		if (ret == -EOPNOTSUPP || ret == -EXDEV)
1794 			ret = vfs_copy_file_range(src_fp->filp, src_off,
1795 						  dst_fp->filp, dst_off, len,
1796 						  COPY_FILE_SPLICE);
1797 		if (ret < 0)
1798 			return ret;
1799 
1800 		*chunk_count_written += 1;
1801 		*total_size_written += ret;
1802 	}
1803 	return 0;
1804 }
1805 
1806 void ksmbd_vfs_posix_lock_wait(struct file_lock *flock)
1807 {
1808 	wait_event(flock->fl_wait, !flock->fl_blocker);
1809 }
1810 
1811 int ksmbd_vfs_posix_lock_wait_timeout(struct file_lock *flock, long timeout)
1812 {
1813 	return wait_event_interruptible_timeout(flock->fl_wait,
1814 						!flock->fl_blocker,
1815 						timeout);
1816 }
1817 
1818 void ksmbd_vfs_posix_lock_unblock(struct file_lock *flock)
1819 {
1820 	locks_delete_block(flock);
1821 }
1822 
1823 int ksmbd_vfs_set_init_posix_acl(struct mnt_idmap *idmap,
1824 				 struct path *path)
1825 {
1826 	struct posix_acl_state acl_state;
1827 	struct posix_acl *acls;
1828 	struct dentry *dentry = path->dentry;
1829 	struct inode *inode = d_inode(dentry);
1830 	int rc;
1831 
1832 	if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1833 		return -EOPNOTSUPP;
1834 
1835 	ksmbd_debug(SMB, "Set posix acls\n");
1836 	rc = init_acl_state(&acl_state, 1);
1837 	if (rc)
1838 		return rc;
1839 
1840 	/* Set default owner group */
1841 	acl_state.owner.allow = (inode->i_mode & 0700) >> 6;
1842 	acl_state.group.allow = (inode->i_mode & 0070) >> 3;
1843 	acl_state.other.allow = inode->i_mode & 0007;
1844 	acl_state.users->aces[acl_state.users->n].uid = inode->i_uid;
1845 	acl_state.users->aces[acl_state.users->n++].perms.allow =
1846 		acl_state.owner.allow;
1847 	acl_state.groups->aces[acl_state.groups->n].gid = inode->i_gid;
1848 	acl_state.groups->aces[acl_state.groups->n++].perms.allow =
1849 		acl_state.group.allow;
1850 	acl_state.mask.allow = 0x07;
1851 
1852 	acls = posix_acl_alloc(6, GFP_KERNEL);
1853 	if (!acls) {
1854 		free_acl_state(&acl_state);
1855 		return -ENOMEM;
1856 	}
1857 	posix_state_to_acl(&acl_state, acls->a_entries);
1858 
1859 	rc = mnt_want_write(path->mnt);
1860 	if (rc)
1861 		goto out_err;
1862 
1863 	rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls);
1864 	if (rc < 0)
1865 		ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1866 			    rc);
1867 	else if (S_ISDIR(inode->i_mode)) {
1868 		posix_state_to_acl(&acl_state, acls->a_entries);
1869 		rc = set_posix_acl(idmap, dentry, ACL_TYPE_DEFAULT, acls);
1870 		if (rc < 0)
1871 			ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1872 				    rc);
1873 	}
1874 	mnt_drop_write(path->mnt);
1875 
1876 out_err:
1877 	free_acl_state(&acl_state);
1878 	posix_acl_release(acls);
1879 	return rc;
1880 }
1881 
1882 int ksmbd_vfs_inherit_posix_acl(struct mnt_idmap *idmap,
1883 				struct path *path, struct inode *parent_inode)
1884 {
1885 	struct posix_acl *acls;
1886 	struct posix_acl_entry *pace;
1887 	struct dentry *dentry = path->dentry;
1888 	struct inode *inode = d_inode(dentry);
1889 	int rc, i;
1890 
1891 	if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1892 		return -EOPNOTSUPP;
1893 
1894 	acls = get_inode_acl(parent_inode, ACL_TYPE_DEFAULT);
1895 	if (IS_ERR_OR_NULL(acls))
1896 		return -ENOENT;
1897 	pace = acls->a_entries;
1898 
1899 	for (i = 0; i < acls->a_count; i++, pace++) {
1900 		if (pace->e_tag == ACL_MASK) {
1901 			pace->e_perm = 0x07;
1902 			break;
1903 		}
1904 	}
1905 
1906 	rc = mnt_want_write(path->mnt);
1907 	if (rc)
1908 		goto out_err;
1909 
1910 	rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls);
1911 	if (rc < 0)
1912 		ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1913 			    rc);
1914 	if (S_ISDIR(inode->i_mode)) {
1915 		rc = set_posix_acl(idmap, dentry, ACL_TYPE_DEFAULT,
1916 				   acls);
1917 		if (rc < 0)
1918 			ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1919 				    rc);
1920 	}
1921 	mnt_drop_write(path->mnt);
1922 
1923 out_err:
1924 	posix_acl_release(acls);
1925 	return rc;
1926 }
1927