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