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