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