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