1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 #include <linux/kernel.h>
9 #include <linux/uaccess.h>
10 #include <linux/backing-dev.h>
11 #include <linux/writeback.h>
12 #include <linux/xattr.h>
13 #include <linux/falloc.h>
14 #include <linux/fsnotify.h>
15 #include <linux/dcache.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/sched/xacct.h>
19 #include <linux/crc32c.h>
21 #include "../internal.h" /* for vfs_path_lookup */
25 #include "connection.h"
27 #include "vfs_cache.h"
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"
39 static char *extract_last_component(char *path)
41 char *p = strrchr(path, '/');
43 if (p && p[1] != '\0') {
52 static void ksmbd_vfs_inherit_owner(struct ksmbd_work *work,
53 struct inode *parent_inode,
56 if (!test_share_config_flag(work->tcon->share_conf,
57 KSMBD_SHARE_FLAG_INHERIT_OWNER))
60 i_uid_write(inode, i_uid_read(parent_inode));
64 * ksmbd_vfs_lock_parent() - lock parent dentry if it is stable
66 * the parent dentry got by dget_parent or @parent could be
67 * unstable, we try to lock a parent inode and lookup the
70 * the reference count of @parent isn't incremented.
72 int ksmbd_vfs_lock_parent(struct user_namespace *user_ns, struct dentry *parent,
75 struct dentry *dentry;
78 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
79 dentry = lookup_one(user_ns, child->d_name.name, parent,
82 ret = PTR_ERR(dentry);
86 if (dentry != child) {
95 inode_unlock(d_inode(parent));
99 int ksmbd_vfs_may_delete(struct user_namespace *user_ns,
100 struct dentry *dentry)
102 struct dentry *parent;
105 parent = dget_parent(dentry);
106 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
112 ret = inode_permission(user_ns, d_inode(parent),
113 MAY_EXEC | MAY_WRITE);
115 inode_unlock(d_inode(parent));
120 int ksmbd_vfs_query_maximal_access(struct user_namespace *user_ns,
121 struct dentry *dentry, __le32 *daccess)
123 struct dentry *parent;
126 *daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL);
128 if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_WRITE))
129 *daccess |= cpu_to_le32(WRITE_DAC | WRITE_OWNER | SYNCHRONIZE |
130 FILE_WRITE_DATA | FILE_APPEND_DATA |
131 FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES |
134 if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_READ))
135 *daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE;
137 if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_EXEC))
138 *daccess |= FILE_EXECUTE_LE;
140 parent = dget_parent(dentry);
141 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
147 if (!inode_permission(user_ns, d_inode(parent), MAY_EXEC | MAY_WRITE))
148 *daccess |= FILE_DELETE_LE;
150 inode_unlock(d_inode(parent));
156 * ksmbd_vfs_create() - vfs helper for smb create file
158 * @name: file name that is relative to share
159 * @mode: file create mode
161 * Return: 0 on success, otherwise error
163 int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode)
166 struct dentry *dentry;
169 dentry = ksmbd_vfs_kern_path_create(work, name,
170 LOOKUP_NO_SYMLINKS, &path);
171 if (IS_ERR(dentry)) {
172 err = PTR_ERR(dentry);
174 pr_err("path create failed for %s, err %d\n",
180 err = vfs_create(mnt_user_ns(path.mnt), d_inode(path.dentry),
183 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry),
186 pr_err("File(%s): creation failed (err:%d)\n", name, err);
188 done_path_create(&path, dentry);
193 * ksmbd_vfs_mkdir() - vfs helper for smb create directory
195 * @name: directory name that is relative to share
196 * @mode: directory create mode
198 * Return: 0 on success, otherwise error
200 int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode)
202 struct user_namespace *user_ns;
204 struct dentry *dentry;
207 dentry = ksmbd_vfs_kern_path_create(work, name,
208 LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY,
210 if (IS_ERR(dentry)) {
211 err = PTR_ERR(dentry);
213 ksmbd_debug(VFS, "path create failed for %s, err %d\n",
218 user_ns = mnt_user_ns(path.mnt);
220 err = vfs_mkdir(user_ns, d_inode(path.dentry), dentry, mode);
223 } else if (d_unhashed(dentry)) {
226 d = lookup_one(user_ns, dentry->d_name.name, dentry->d_parent,
232 if (unlikely(d_is_negative(d))) {
238 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), d_inode(d));
242 done_path_create(&path, dentry);
244 pr_err("mkdir(%s): creation failed (err:%d)\n", name, err);
248 static ssize_t ksmbd_vfs_getcasexattr(struct user_namespace *user_ns,
249 struct dentry *dentry, char *attr_name,
250 int attr_name_len, char **attr_value)
252 char *name, *xattr_list = NULL;
253 ssize_t value_len = -ENOENT, xattr_list_len;
255 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
256 if (xattr_list_len <= 0)
259 for (name = xattr_list; name - xattr_list < xattr_list_len;
260 name += strlen(name) + 1) {
261 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
262 if (strncasecmp(attr_name, name, attr_name_len))
265 value_len = ksmbd_vfs_getxattr(user_ns,
270 pr_err("failed to get xattr in file\n");
279 static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos,
283 char *stream_buf = NULL;
285 ksmbd_debug(VFS, "read stream data pos : %llu, count : %zd\n",
288 v_len = ksmbd_vfs_getcasexattr(file_mnt_user_ns(fp->filp),
289 fp->filp->f_path.dentry,
301 if (v_len - *pos < count)
302 count = v_len - *pos;
304 memcpy(buf, &stream_buf[*pos], count);
312 * check_lock_range() - vfs helper for smb byte range file locking
313 * @filp: the file to apply the lock to
314 * @start: lock start byte offset
315 * @end: lock end byte offset
316 * @type: byte range type read/write
318 * Return: 0 on success, otherwise error
320 static int check_lock_range(struct file *filp, loff_t start, loff_t end,
323 struct file_lock *flock;
324 struct file_lock_context *ctx = file_inode(filp)->i_flctx;
327 if (!ctx || list_empty_careful(&ctx->flc_posix))
330 spin_lock(&ctx->flc_lock);
331 list_for_each_entry(flock, &ctx->flc_posix, fl_list) {
332 /* check conflict locks */
333 if (flock->fl_end >= start && end >= flock->fl_start) {
334 if (flock->fl_type == F_RDLCK) {
336 pr_err("not allow write by shared lock\n");
340 } else if (flock->fl_type == F_WRLCK) {
341 /* check owner in lock */
342 if (flock->fl_file != filp) {
344 pr_err("not allow rw access by exclusive lock from other opens\n");
351 spin_unlock(&ctx->flc_lock);
356 * ksmbd_vfs_read() - vfs helper for smb file read
358 * @fid: file id of open file
359 * @count: read byte count
362 * Return: number of read bytes on success, otherwise error
364 int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count,
367 struct file *filp = fp->filp;
369 char *rbuf = work->aux_payload_buf;
370 struct inode *inode = file_inode(filp);
372 if (S_ISDIR(inode->i_mode))
375 if (unlikely(count == 0))
378 if (work->conn->connection_type) {
379 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
380 pr_err("no right to read(%pd)\n",
381 fp->filp->f_path.dentry);
386 if (ksmbd_stream_fd(fp))
387 return ksmbd_vfs_stream_read(fp, rbuf, pos, count);
389 if (!work->tcon->posix_extensions) {
392 ret = check_lock_range(filp, *pos, *pos + count - 1, READ);
394 pr_err("unable to read due to lock\n");
399 nbytes = kernel_read(filp, rbuf, count, pos);
401 pr_err("smb read failed, err = %zd\n", nbytes);
409 static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
412 char *stream_buf = NULL, *wbuf;
413 struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
417 ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n",
421 if (size > XATTR_SIZE_MAX) {
422 size = XATTR_SIZE_MAX;
423 count = (*pos + count) - XATTR_SIZE_MAX;
426 v_len = ksmbd_vfs_getcasexattr(user_ns,
427 fp->filp->f_path.dentry,
431 if ((int)v_len < 0) {
432 pr_err("not found stream in xattr : %zd\n", v_len);
438 wbuf = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
445 memcpy(wbuf, stream_buf, v_len);
450 memcpy(&stream_buf[*pos], buf, count);
452 err = ksmbd_vfs_setxattr(user_ns,
453 fp->filp->f_path.dentry,
461 fp->filp->f_pos = *pos;
469 * ksmbd_vfs_write() - vfs helper for smb file write
471 * @fid: file id of open file
472 * @buf: buf containing data for writing
473 * @count: read byte count
475 * @sync: fsync after write
476 * @written: number of bytes written
478 * Return: 0 on success, otherwise error
480 int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp,
481 char *buf, size_t count, loff_t *pos, bool sync,
485 loff_t offset = *pos;
488 if (work->conn->connection_type) {
489 if (!(fp->daccess & FILE_WRITE_DATA_LE)) {
490 pr_err("no right to write(%pd)\n",
491 fp->filp->f_path.dentry);
499 if (ksmbd_stream_fd(fp)) {
500 err = ksmbd_vfs_stream_write(fp, buf, pos, count);
506 if (!work->tcon->posix_extensions) {
507 err = check_lock_range(filp, *pos, *pos + count - 1, WRITE);
509 pr_err("unable to write due to lock\n");
515 /* Do we need to break any of a levelII oplock? */
516 smb_break_all_levII_oplock(work, fp, 1);
518 err = kernel_write(filp, buf, count, pos);
520 ksmbd_debug(VFS, "smb write failed, err = %d\n", err);
528 err = vfs_fsync_range(filp, offset, offset + *written, 0);
530 pr_err("fsync failed for filename = %pd, err = %d\n",
531 fp->filp->f_path.dentry, err);
539 * ksmbd_vfs_getattr() - vfs helper for smb getattr
541 * @fid: file id of open file
542 * @attrs: inode attributes
544 * Return: 0 on success, otherwise error
546 int ksmbd_vfs_getattr(struct path *path, struct kstat *stat)
550 err = vfs_getattr(path, stat, STATX_BTIME, AT_STATX_SYNC_AS_STAT);
552 pr_err("getattr failed, err %d\n", err);
557 * ksmbd_vfs_fsync() - vfs helper for smb fsync
559 * @fid: file id of open file
561 * Return: 0 on success, otherwise error
563 int ksmbd_vfs_fsync(struct ksmbd_work *work, u64 fid, u64 p_id)
565 struct ksmbd_file *fp;
568 fp = ksmbd_lookup_fd_slow(work, fid, p_id);
570 pr_err("failed to get filp for fid %llu\n", fid);
573 err = vfs_fsync(fp->filp, 0);
575 pr_err("smb fsync failed, err = %d\n", err);
576 ksmbd_fd_put(work, fp);
581 * ksmbd_vfs_remove_file() - vfs helper for smb rmdir or unlink
582 * @name: directory or file name that is relative to share
584 * Return: 0 on success, otherwise error
586 int ksmbd_vfs_remove_file(struct ksmbd_work *work, char *name)
588 struct user_namespace *user_ns;
590 struct dentry *parent;
593 if (ksmbd_override_fsids(work))
596 err = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, false);
598 ksmbd_debug(VFS, "can't get %s, err %d\n", name, err);
599 ksmbd_revert_fsids(work);
603 user_ns = mnt_user_ns(path.mnt);
604 parent = dget_parent(path.dentry);
605 err = ksmbd_vfs_lock_parent(user_ns, parent, path.dentry);
609 ksmbd_revert_fsids(work);
613 if (!d_inode(path.dentry)->i_nlink) {
618 if (S_ISDIR(d_inode(path.dentry)->i_mode)) {
619 err = vfs_rmdir(user_ns, d_inode(parent), path.dentry);
620 if (err && err != -ENOTEMPTY)
621 ksmbd_debug(VFS, "%s: rmdir failed, err %d\n", name,
624 err = vfs_unlink(user_ns, d_inode(parent), path.dentry, NULL);
626 ksmbd_debug(VFS, "%s: unlink failed, err %d\n", name,
631 inode_unlock(d_inode(parent));
634 ksmbd_revert_fsids(work);
639 * ksmbd_vfs_link() - vfs helper for creating smb hardlink
640 * @oldname: source file name
641 * @newname: hardlink name that is relative to share
643 * Return: 0 on success, otherwise error
645 int ksmbd_vfs_link(struct ksmbd_work *work, const char *oldname,
648 struct path oldpath, newpath;
649 struct dentry *dentry;
652 if (ksmbd_override_fsids(work))
655 err = kern_path(oldname, LOOKUP_NO_SYMLINKS, &oldpath);
657 pr_err("cannot get linux path for %s, err = %d\n",
662 dentry = ksmbd_vfs_kern_path_create(work, newname,
663 LOOKUP_NO_SYMLINKS | LOOKUP_REVAL,
665 if (IS_ERR(dentry)) {
666 err = PTR_ERR(dentry);
667 pr_err("path create err for %s, err %d\n", newname, err);
672 if (oldpath.mnt != newpath.mnt) {
673 pr_err("vfs_link failed err %d\n", err);
677 err = vfs_link(oldpath.dentry, mnt_user_ns(newpath.mnt),
678 d_inode(newpath.dentry),
681 ksmbd_debug(VFS, "vfs_link failed err %d\n", err);
684 done_path_create(&newpath, dentry);
688 ksmbd_revert_fsids(work);
692 static int ksmbd_validate_entry_in_use(struct dentry *src_dent)
694 struct dentry *dst_dent;
696 spin_lock(&src_dent->d_lock);
697 list_for_each_entry(dst_dent, &src_dent->d_subdirs, d_child) {
698 struct ksmbd_file *child_fp;
700 if (d_really_is_negative(dst_dent))
703 child_fp = ksmbd_lookup_fd_inode(d_inode(dst_dent));
705 spin_unlock(&src_dent->d_lock);
706 ksmbd_debug(VFS, "Forbid rename, sub file/dir is in use\n");
710 spin_unlock(&src_dent->d_lock);
715 static int __ksmbd_vfs_rename(struct ksmbd_work *work,
716 struct user_namespace *src_user_ns,
717 struct dentry *src_dent_parent,
718 struct dentry *src_dent,
719 struct user_namespace *dst_user_ns,
720 struct dentry *dst_dent_parent,
721 struct dentry *trap_dent,
724 struct dentry *dst_dent;
727 if (!work->tcon->posix_extensions) {
728 err = ksmbd_validate_entry_in_use(src_dent);
733 if (d_really_is_negative(src_dent_parent))
735 if (d_really_is_negative(dst_dent_parent))
737 if (d_really_is_negative(src_dent))
739 if (src_dent == trap_dent)
742 if (ksmbd_override_fsids(work))
745 dst_dent = lookup_one(dst_user_ns, dst_name, dst_dent_parent,
747 err = PTR_ERR(dst_dent);
748 if (IS_ERR(dst_dent)) {
749 pr_err("lookup failed %s [%d]\n", dst_name, err);
754 if (dst_dent != trap_dent && !d_really_is_positive(dst_dent)) {
755 struct renamedata rd = {
756 .old_mnt_userns = src_user_ns,
757 .old_dir = d_inode(src_dent_parent),
758 .old_dentry = src_dent,
759 .new_mnt_userns = dst_user_ns,
760 .new_dir = d_inode(dst_dent_parent),
761 .new_dentry = dst_dent,
763 err = vfs_rename(&rd);
766 pr_err("vfs_rename failed err %d\n", err);
770 ksmbd_revert_fsids(work);
774 int ksmbd_vfs_fp_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
777 struct user_namespace *user_ns;
778 struct path dst_path;
779 struct dentry *src_dent_parent, *dst_dent_parent;
780 struct dentry *src_dent, *trap_dent, *src_child;
784 dst_name = extract_last_component(newname);
790 src_dent_parent = dget_parent(fp->filp->f_path.dentry);
791 src_dent = fp->filp->f_path.dentry;
793 err = ksmbd_vfs_kern_path(work, newname,
794 LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY,
797 ksmbd_debug(VFS, "Cannot get path for %s [%d]\n", newname, err);
800 dst_dent_parent = dst_path.dentry;
802 trap_dent = lock_rename(src_dent_parent, dst_dent_parent);
804 dget(dst_dent_parent);
805 user_ns = file_mnt_user_ns(fp->filp);
806 src_child = lookup_one(user_ns, src_dent->d_name.name, src_dent_parent,
807 src_dent->d_name.len);
808 if (IS_ERR(src_child)) {
809 err = PTR_ERR(src_child);
813 if (src_child != src_dent) {
820 err = __ksmbd_vfs_rename(work,
824 mnt_user_ns(dst_path.mnt),
830 dput(dst_dent_parent);
831 unlock_rename(src_dent_parent, dst_dent_parent);
834 dput(src_dent_parent);
839 * ksmbd_vfs_truncate() - vfs helper for smb file truncate
841 * @fid: file id of old file
842 * @size: truncate to given size
844 * Return: 0 on success, otherwise error
846 int ksmbd_vfs_truncate(struct ksmbd_work *work,
847 struct ksmbd_file *fp, loff_t size)
854 /* Do we need to break any of a levelII oplock? */
855 smb_break_all_levII_oplock(work, fp, 1);
857 if (!work->tcon->posix_extensions) {
858 struct inode *inode = file_inode(filp);
860 if (size < inode->i_size) {
861 err = check_lock_range(filp, size,
862 inode->i_size - 1, WRITE);
864 err = check_lock_range(filp, inode->i_size,
869 pr_err("failed due to lock\n");
874 err = vfs_truncate(&filp->f_path, size);
876 pr_err("truncate failed, err %d\n", err);
881 * ksmbd_vfs_listxattr() - vfs helper for smb list extended attributes
882 * @dentry: dentry of file for listing xattrs
883 * @list: destination buffer
884 * @size: destination buffer length
886 * Return: xattr list length on success, otherwise error
888 ssize_t ksmbd_vfs_listxattr(struct dentry *dentry, char **list)
893 size = vfs_listxattr(dentry, NULL, 0);
897 vlist = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
902 size = vfs_listxattr(dentry, vlist, size);
904 ksmbd_debug(VFS, "listxattr failed\n");
912 static ssize_t ksmbd_vfs_xattr_len(struct user_namespace *user_ns,
913 struct dentry *dentry, char *xattr_name)
915 return vfs_getxattr(user_ns, dentry, xattr_name, NULL, 0);
919 * ksmbd_vfs_getxattr() - vfs helper for smb get extended attributes value
920 * @user_ns: user namespace
921 * @dentry: dentry of file for getting xattrs
922 * @xattr_name: name of xattr name to query
923 * @xattr_buf: destination buffer xattr value
925 * Return: read xattr value length on success, otherwise error
927 ssize_t ksmbd_vfs_getxattr(struct user_namespace *user_ns,
928 struct dentry *dentry,
929 char *xattr_name, char **xattr_buf)
935 xattr_len = ksmbd_vfs_xattr_len(user_ns, dentry, xattr_name);
939 buf = kmalloc(xattr_len + 1, GFP_KERNEL);
943 xattr_len = vfs_getxattr(user_ns, dentry, xattr_name,
944 (void *)buf, xattr_len);
953 * ksmbd_vfs_setxattr() - vfs helper for smb set extended attributes value
954 * @user_ns: user namespace
955 * @dentry: dentry to set XATTR at
956 * @name: xattr name for setxattr
957 * @value: xattr value to set
958 * @size: size of xattr value
959 * @flags: destination buffer length
961 * Return: 0 on success, otherwise error
963 int ksmbd_vfs_setxattr(struct user_namespace *user_ns,
964 struct dentry *dentry, const char *attr_name,
965 void *attr_value, size_t attr_size, int flags)
969 err = vfs_setxattr(user_ns,
976 ksmbd_debug(VFS, "setxattr failed, err %d\n", err);
981 * ksmbd_vfs_set_fadvise() - convert smb IO caching options to linux options
982 * @filp: file pointer for IO
983 * @options: smb IO options
985 void ksmbd_vfs_set_fadvise(struct file *filp, __le32 option)
987 struct address_space *mapping;
989 mapping = filp->f_mapping;
991 if (!option || !mapping)
994 if (option & FILE_WRITE_THROUGH_LE) {
995 filp->f_flags |= O_SYNC;
996 } else if (option & FILE_SEQUENTIAL_ONLY_LE) {
997 filp->f_ra.ra_pages = inode_to_bdi(mapping->host)->ra_pages * 2;
998 spin_lock(&filp->f_lock);
999 filp->f_mode &= ~FMODE_RANDOM;
1000 spin_unlock(&filp->f_lock);
1001 } else if (option & FILE_RANDOM_ACCESS_LE) {
1002 spin_lock(&filp->f_lock);
1003 filp->f_mode |= FMODE_RANDOM;
1004 spin_unlock(&filp->f_lock);
1008 int ksmbd_vfs_zero_data(struct ksmbd_work *work, struct ksmbd_file *fp,
1009 loff_t off, loff_t len)
1011 smb_break_all_levII_oplock(work, fp, 1);
1012 if (fp->f_ci->m_fattr & FILE_ATTRIBUTE_SPARSE_FILE_LE)
1013 return vfs_fallocate(fp->filp,
1014 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1017 return vfs_fallocate(fp->filp,
1018 FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE,
1022 int ksmbd_vfs_fqar_lseek(struct ksmbd_file *fp, loff_t start, loff_t length,
1023 struct file_allocated_range_buffer *ranges,
1024 unsigned int in_count, unsigned int *out_count)
1026 struct file *f = fp->filp;
1027 struct inode *inode = file_inode(fp->filp);
1028 loff_t maxbytes = (u64)inode->i_sb->s_maxbytes, end;
1029 loff_t extent_start, extent_end;
1032 if (start > maxbytes)
1039 * Shrink request scope to what the fs can actually handle.
1041 if (length > maxbytes || (maxbytes - length) < start)
1042 length = maxbytes - start;
1044 if (start + length > inode->i_size)
1045 length = inode->i_size - start;
1048 end = start + length;
1049 while (start < end && *out_count < in_count) {
1050 extent_start = vfs_llseek(f, start, SEEK_DATA);
1051 if (extent_start < 0) {
1052 if (extent_start != -ENXIO)
1053 ret = (int)extent_start;
1057 if (extent_start >= end)
1060 extent_end = vfs_llseek(f, extent_start, SEEK_HOLE);
1061 if (extent_end < 0) {
1062 if (extent_end != -ENXIO)
1063 ret = (int)extent_end;
1065 } else if (extent_start >= extent_end) {
1069 ranges[*out_count].file_offset = cpu_to_le64(extent_start);
1070 ranges[(*out_count)++].length =
1071 cpu_to_le64(min(extent_end, end) - extent_start);
1079 int ksmbd_vfs_remove_xattr(struct user_namespace *user_ns,
1080 struct dentry *dentry, char *attr_name)
1082 return vfs_removexattr(user_ns, dentry, attr_name);
1085 int ksmbd_vfs_unlink(struct user_namespace *user_ns,
1086 struct dentry *dir, struct dentry *dentry)
1090 err = ksmbd_vfs_lock_parent(user_ns, dir, dentry);
1095 if (S_ISDIR(d_inode(dentry)->i_mode))
1096 err = vfs_rmdir(user_ns, d_inode(dir), dentry);
1098 err = vfs_unlink(user_ns, d_inode(dir), dentry, NULL);
1101 inode_unlock(d_inode(dir));
1103 ksmbd_debug(VFS, "failed to delete, err %d\n", err);
1108 static int __dir_empty(struct dir_context *ctx, const char *name, int namlen,
1109 loff_t offset, u64 ino, unsigned int d_type)
1111 struct ksmbd_readdir_data *buf;
1113 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
1114 buf->dirent_count++;
1116 if (buf->dirent_count > 2)
1122 * ksmbd_vfs_empty_dir() - check for empty directory
1123 * @fp: ksmbd file pointer
1125 * Return: true if directory empty, otherwise false
1127 int ksmbd_vfs_empty_dir(struct ksmbd_file *fp)
1130 struct ksmbd_readdir_data readdir_data;
1132 memset(&readdir_data, 0, sizeof(struct ksmbd_readdir_data));
1134 set_ctx_actor(&readdir_data.ctx, __dir_empty);
1135 readdir_data.dirent_count = 0;
1137 err = iterate_dir(fp->filp, &readdir_data.ctx);
1138 if (readdir_data.dirent_count > 2)
1145 static int __caseless_lookup(struct dir_context *ctx, const char *name,
1146 int namlen, loff_t offset, u64 ino,
1147 unsigned int d_type)
1149 struct ksmbd_readdir_data *buf;
1151 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
1153 if (buf->used != namlen)
1155 if (!strncasecmp((char *)buf->private, name, namlen)) {
1156 memcpy((char *)buf->private, name, namlen);
1157 buf->dirent_count = 1;
1164 * ksmbd_vfs_lookup_in_dir() - lookup a file in a directory
1166 * @name: filename to lookup
1167 * @namelen: filename length
1169 * Return: 0 on success, otherwise error
1171 static int ksmbd_vfs_lookup_in_dir(struct path *dir, char *name, size_t namelen)
1175 int flags = O_RDONLY | O_LARGEFILE;
1176 struct ksmbd_readdir_data readdir_data = {
1177 .ctx.actor = __caseless_lookup,
1183 dfilp = dentry_open(dir, flags, current_cred());
1185 return PTR_ERR(dfilp);
1187 ret = iterate_dir(dfilp, &readdir_data.ctx);
1188 if (readdir_data.dirent_count > 0)
1195 * ksmbd_vfs_kern_path() - lookup a file and get path info
1196 * @name: file path that is relative to share
1197 * @flags: lookup flags
1198 * @path: if lookup succeed, return path info
1199 * @caseless: caseless filename lookup
1201 * Return: 0 on success, otherwise error
1203 int ksmbd_vfs_kern_path(struct ksmbd_work *work, char *name,
1204 unsigned int flags, struct path *path, bool caseless)
1206 struct ksmbd_share_config *share_conf = work->tcon->share_conf;
1209 flags |= LOOKUP_BENEATH;
1210 err = vfs_path_lookup(share_conf->vfs_path.dentry,
1211 share_conf->vfs_path.mnt,
1221 size_t path_len, remain_len;
1223 filepath = kstrdup(name, GFP_KERNEL);
1227 path_len = strlen(filepath);
1228 remain_len = path_len;
1230 parent = share_conf->vfs_path;
1233 while (d_can_lookup(parent.dentry)) {
1234 char *filename = filepath + path_len - remain_len;
1235 char *next = strchrnul(filename, '/');
1236 size_t filename_len = next - filename;
1237 bool is_last = !next[0];
1239 if (filename_len == 0)
1242 err = ksmbd_vfs_lookup_in_dir(&parent, filename,
1250 err = vfs_path_lookup(share_conf->vfs_path.dentry,
1251 share_conf->vfs_path.mnt,
1263 remain_len -= filename_len + 1;
1274 struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work,
1280 struct dentry *dent;
1282 abs_name = convert_to_unix_name(work->tcon->share_conf, name);
1284 return ERR_PTR(-ENOMEM);
1286 dent = kern_path_create(AT_FDCWD, abs_name, path, flags);
1291 int ksmbd_vfs_remove_acl_xattrs(struct user_namespace *user_ns,
1292 struct dentry *dentry)
1294 char *name, *xattr_list = NULL;
1295 ssize_t xattr_list_len;
1298 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
1299 if (xattr_list_len < 0) {
1301 } else if (!xattr_list_len) {
1302 ksmbd_debug(SMB, "empty xattr in the file\n");
1306 for (name = xattr_list; name - xattr_list < xattr_list_len;
1307 name += strlen(name) + 1) {
1308 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
1310 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
1311 sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1) ||
1312 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
1313 sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1)) {
1314 err = ksmbd_vfs_remove_xattr(user_ns, dentry, name);
1317 "remove acl xattr failed : %s\n", name);
1325 int ksmbd_vfs_remove_sd_xattrs(struct user_namespace *user_ns,
1326 struct dentry *dentry)
1328 char *name, *xattr_list = NULL;
1329 ssize_t xattr_list_len;
1332 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
1333 if (xattr_list_len < 0) {
1335 } else if (!xattr_list_len) {
1336 ksmbd_debug(SMB, "empty xattr in the file\n");
1340 for (name = xattr_list; name - xattr_list < xattr_list_len;
1341 name += strlen(name) + 1) {
1342 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
1344 if (!strncmp(name, XATTR_NAME_SD, XATTR_NAME_SD_LEN)) {
1345 err = ksmbd_vfs_remove_xattr(user_ns, dentry, name);
1347 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
1355 static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct user_namespace *user_ns,
1356 struct inode *inode,
1359 struct xattr_smb_acl *smb_acl = NULL;
1360 struct posix_acl *posix_acls;
1361 struct posix_acl_entry *pa_entry;
1362 struct xattr_acl_entry *xa_entry;
1365 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1368 posix_acls = get_acl(inode, acl_type);
1372 smb_acl = kzalloc(sizeof(struct xattr_smb_acl) +
1373 sizeof(struct xattr_acl_entry) * posix_acls->a_count,
1378 smb_acl->count = posix_acls->a_count;
1379 pa_entry = posix_acls->a_entries;
1380 xa_entry = smb_acl->entries;
1381 for (i = 0; i < posix_acls->a_count; i++, pa_entry++, xa_entry++) {
1382 switch (pa_entry->e_tag) {
1384 xa_entry->type = SMB_ACL_USER;
1385 xa_entry->uid = posix_acl_uid_translate(user_ns, pa_entry);
1388 xa_entry->type = SMB_ACL_USER_OBJ;
1391 xa_entry->type = SMB_ACL_GROUP;
1392 xa_entry->gid = posix_acl_gid_translate(user_ns, pa_entry);
1395 xa_entry->type = SMB_ACL_GROUP_OBJ;
1398 xa_entry->type = SMB_ACL_OTHER;
1401 xa_entry->type = SMB_ACL_MASK;
1404 pr_err("unknown type : 0x%x\n", pa_entry->e_tag);
1408 if (pa_entry->e_perm & ACL_READ)
1409 xa_entry->perm |= SMB_ACL_READ;
1410 if (pa_entry->e_perm & ACL_WRITE)
1411 xa_entry->perm |= SMB_ACL_WRITE;
1412 if (pa_entry->e_perm & ACL_EXECUTE)
1413 xa_entry->perm |= SMB_ACL_EXECUTE;
1416 posix_acl_release(posix_acls);
1420 int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
1421 struct user_namespace *user_ns,
1422 struct dentry *dentry,
1423 struct smb_ntsd *pntsd, int len)
1426 struct ndr sd_ndr = {0}, acl_ndr = {0};
1427 struct xattr_ntacl acl = {0};
1428 struct xattr_smb_acl *smb_acl, *def_smb_acl = NULL;
1429 struct inode *inode = d_inode(dentry);
1432 acl.hash_type = XATTR_SD_HASH_TYPE_SHA256;
1433 acl.current_time = ksmbd_UnixTimeToNT(current_time(inode));
1435 memcpy(acl.desc, "posix_acl", 9);
1439 cpu_to_le32(le32_to_cpu(pntsd->osidoffset) + NDR_NTSD_OFFSETOF);
1441 cpu_to_le32(le32_to_cpu(pntsd->gsidoffset) + NDR_NTSD_OFFSETOF);
1443 cpu_to_le32(le32_to_cpu(pntsd->dacloffset) + NDR_NTSD_OFFSETOF);
1445 acl.sd_buf = (char *)pntsd;
1448 rc = ksmbd_gen_sd_hash(conn, acl.sd_buf, acl.sd_size, acl.hash);
1450 pr_err("failed to generate hash for ndr acl\n");
1454 smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
1456 if (S_ISDIR(inode->i_mode))
1457 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
1460 rc = ndr_encode_posix_acl(&acl_ndr, user_ns, inode,
1461 smb_acl, def_smb_acl);
1463 pr_err("failed to encode ndr to posix acl\n");
1467 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset,
1468 acl.posix_acl_hash);
1470 pr_err("failed to generate hash for ndr acl\n");
1474 rc = ndr_encode_v4_ntacl(&sd_ndr, &acl);
1476 pr_err("failed to encode ndr to posix acl\n");
1480 rc = ksmbd_vfs_setxattr(user_ns, dentry,
1481 XATTR_NAME_SD, sd_ndr.data,
1484 pr_err("Failed to store XATTR ntacl :%d\n", rc);
1488 kfree(acl_ndr.data);
1494 int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
1495 struct user_namespace *user_ns,
1496 struct dentry *dentry,
1497 struct smb_ntsd **pntsd)
1501 struct inode *inode = d_inode(dentry);
1502 struct ndr acl_ndr = {0};
1503 struct xattr_ntacl acl;
1504 struct xattr_smb_acl *smb_acl = NULL, *def_smb_acl = NULL;
1505 __u8 cmp_hash[XATTR_SD_HASH_SIZE] = {0};
1507 rc = ksmbd_vfs_getxattr(user_ns, dentry, XATTR_NAME_SD, &n.data);
1512 rc = ndr_decode_v4_ntacl(&n, &acl);
1516 smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
1518 if (S_ISDIR(inode->i_mode))
1519 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
1522 rc = ndr_encode_posix_acl(&acl_ndr, user_ns, inode, smb_acl,
1525 pr_err("failed to encode ndr to posix acl\n");
1529 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset, cmp_hash);
1531 pr_err("failed to generate hash for ndr acl\n");
1535 if (memcmp(cmp_hash, acl.posix_acl_hash, XATTR_SD_HASH_SIZE)) {
1536 pr_err("hash value diff\n");
1541 *pntsd = acl.sd_buf;
1542 if (acl.sd_size < sizeof(struct smb_ntsd)) {
1543 pr_err("sd size is invalid\n");
1547 (*pntsd)->osidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->osidoffset) -
1549 (*pntsd)->gsidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->gsidoffset) -
1551 (*pntsd)->dacloffset = cpu_to_le32(le32_to_cpu((*pntsd)->dacloffset) -
1556 kfree(acl_ndr.data);
1569 int ksmbd_vfs_set_dos_attrib_xattr(struct user_namespace *user_ns,
1570 struct dentry *dentry,
1571 struct xattr_dos_attrib *da)
1576 err = ndr_encode_dos_attr(&n, da);
1580 err = ksmbd_vfs_setxattr(user_ns, dentry, XATTR_NAME_DOS_ATTRIBUTE,
1581 (void *)n.data, n.offset, 0);
1583 ksmbd_debug(SMB, "failed to store dos attribute in xattr\n");
1589 int ksmbd_vfs_get_dos_attrib_xattr(struct user_namespace *user_ns,
1590 struct dentry *dentry,
1591 struct xattr_dos_attrib *da)
1596 err = ksmbd_vfs_getxattr(user_ns, dentry, XATTR_NAME_DOS_ATTRIBUTE,
1600 if (ndr_decode_dos_attr(&n, da))
1604 ksmbd_debug(SMB, "failed to load dos attribute in xattr\n");
1611 * ksmbd_vfs_init_kstat() - convert unix stat information to smb stat format
1612 * @p: destination buffer
1613 * @ksmbd_kstat: ksmbd kstat wrapper
1615 void *ksmbd_vfs_init_kstat(char **p, struct ksmbd_kstat *ksmbd_kstat)
1617 struct file_directory_info *info = (struct file_directory_info *)(*p);
1618 struct kstat *kstat = ksmbd_kstat->kstat;
1621 info->FileIndex = 0;
1622 info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
1623 time = ksmbd_UnixTimeToNT(kstat->atime);
1624 info->LastAccessTime = cpu_to_le64(time);
1625 time = ksmbd_UnixTimeToNT(kstat->mtime);
1626 info->LastWriteTime = cpu_to_le64(time);
1627 time = ksmbd_UnixTimeToNT(kstat->ctime);
1628 info->ChangeTime = cpu_to_le64(time);
1630 if (ksmbd_kstat->file_attributes & FILE_ATTRIBUTE_DIRECTORY_LE) {
1631 info->EndOfFile = 0;
1632 info->AllocationSize = 0;
1634 info->EndOfFile = cpu_to_le64(kstat->size);
1635 info->AllocationSize = cpu_to_le64(kstat->blocks << 9);
1637 info->ExtFileAttributes = ksmbd_kstat->file_attributes;
1642 int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work,
1643 struct user_namespace *user_ns,
1644 struct dentry *dentry,
1645 struct ksmbd_kstat *ksmbd_kstat)
1650 generic_fillattr(user_ns, d_inode(dentry), ksmbd_kstat->kstat);
1652 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
1653 ksmbd_kstat->create_time = time;
1656 * set default value for the case that store dos attributes is not yes
1657 * or that acl is disable in server's filesystem and the config is yes.
1659 if (S_ISDIR(ksmbd_kstat->kstat->mode))
1660 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_DIRECTORY_LE;
1662 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_ARCHIVE_LE;
1664 if (test_share_config_flag(work->tcon->share_conf,
1665 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
1666 struct xattr_dos_attrib da;
1668 rc = ksmbd_vfs_get_dos_attrib_xattr(user_ns, dentry, &da);
1670 ksmbd_kstat->file_attributes = cpu_to_le32(da.attr);
1671 ksmbd_kstat->create_time = da.create_time;
1673 ksmbd_debug(VFS, "fail to load dos attribute.\n");
1680 ssize_t ksmbd_vfs_casexattr_len(struct user_namespace *user_ns,
1681 struct dentry *dentry, char *attr_name,
1684 char *name, *xattr_list = NULL;
1685 ssize_t value_len = -ENOENT, xattr_list_len;
1687 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
1688 if (xattr_list_len <= 0)
1691 for (name = xattr_list; name - xattr_list < xattr_list_len;
1692 name += strlen(name) + 1) {
1693 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
1694 if (strncasecmp(attr_name, name, attr_name_len))
1697 value_len = ksmbd_vfs_xattr_len(user_ns, dentry, name);
1706 int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name,
1707 size_t *xattr_stream_name_size, int s_type)
1711 if (s_type == DIR_STREAM)
1712 type = ":$INDEX_ALLOCATION";
1716 buf = kasprintf(GFP_KERNEL, "%s%s%s",
1717 XATTR_NAME_STREAM, stream_name, type);
1721 *xattr_stream_name = buf;
1722 *xattr_stream_name_size = strlen(buf) + 1;
1727 int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work,
1728 struct ksmbd_file *src_fp,
1729 struct ksmbd_file *dst_fp,
1730 struct srv_copychunk *chunks,
1731 unsigned int chunk_count,
1732 unsigned int *chunk_count_written,
1733 unsigned int *chunk_size_written,
1734 loff_t *total_size_written)
1737 loff_t src_off, dst_off, src_file_size;
1741 *chunk_count_written = 0;
1742 *chunk_size_written = 0;
1743 *total_size_written = 0;
1745 if (!(src_fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
1746 pr_err("no right to read(%pd)\n", src_fp->filp->f_path.dentry);
1749 if (!(dst_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) {
1750 pr_err("no right to write(%pd)\n", dst_fp->filp->f_path.dentry);
1754 if (ksmbd_stream_fd(src_fp) || ksmbd_stream_fd(dst_fp))
1757 smb_break_all_levII_oplock(work, dst_fp, 1);
1759 if (!work->tcon->posix_extensions) {
1760 for (i = 0; i < chunk_count; i++) {
1761 src_off = le64_to_cpu(chunks[i].SourceOffset);
1762 dst_off = le64_to_cpu(chunks[i].TargetOffset);
1763 len = le32_to_cpu(chunks[i].Length);
1765 if (check_lock_range(src_fp->filp, src_off,
1766 src_off + len - 1, READ))
1768 if (check_lock_range(dst_fp->filp, dst_off,
1769 dst_off + len - 1, WRITE))
1774 src_file_size = i_size_read(file_inode(src_fp->filp));
1776 for (i = 0; i < chunk_count; i++) {
1777 src_off = le64_to_cpu(chunks[i].SourceOffset);
1778 dst_off = le64_to_cpu(chunks[i].TargetOffset);
1779 len = le32_to_cpu(chunks[i].Length);
1781 if (src_off + len > src_file_size)
1784 ret = vfs_copy_file_range(src_fp->filp, src_off,
1785 dst_fp->filp, dst_off, len, 0);
1786 if (ret == -EOPNOTSUPP || ret == -EXDEV)
1787 ret = generic_copy_file_range(src_fp->filp, src_off,
1788 dst_fp->filp, dst_off,
1793 *chunk_count_written += 1;
1794 *total_size_written += ret;
1799 void ksmbd_vfs_posix_lock_wait(struct file_lock *flock)
1801 wait_event(flock->fl_wait, !flock->fl_blocker);
1804 int ksmbd_vfs_posix_lock_wait_timeout(struct file_lock *flock, long timeout)
1806 return wait_event_interruptible_timeout(flock->fl_wait,
1811 void ksmbd_vfs_posix_lock_unblock(struct file_lock *flock)
1813 locks_delete_block(flock);
1816 int ksmbd_vfs_set_init_posix_acl(struct user_namespace *user_ns,
1817 struct inode *inode)
1819 struct posix_acl_state acl_state;
1820 struct posix_acl *acls;
1823 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1826 ksmbd_debug(SMB, "Set posix acls\n");
1827 rc = init_acl_state(&acl_state, 1);
1831 /* Set default owner group */
1832 acl_state.owner.allow = (inode->i_mode & 0700) >> 6;
1833 acl_state.group.allow = (inode->i_mode & 0070) >> 3;
1834 acl_state.other.allow = inode->i_mode & 0007;
1835 acl_state.users->aces[acl_state.users->n].uid = inode->i_uid;
1836 acl_state.users->aces[acl_state.users->n++].perms.allow =
1837 acl_state.owner.allow;
1838 acl_state.groups->aces[acl_state.groups->n].gid = inode->i_gid;
1839 acl_state.groups->aces[acl_state.groups->n++].perms.allow =
1840 acl_state.group.allow;
1841 acl_state.mask.allow = 0x07;
1843 acls = posix_acl_alloc(6, GFP_KERNEL);
1845 free_acl_state(&acl_state);
1848 posix_state_to_acl(&acl_state, acls->a_entries);
1849 rc = set_posix_acl(user_ns, inode, ACL_TYPE_ACCESS, acls);
1851 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1853 else if (S_ISDIR(inode->i_mode)) {
1854 posix_state_to_acl(&acl_state, acls->a_entries);
1855 rc = set_posix_acl(user_ns, inode, ACL_TYPE_DEFAULT,
1858 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1861 free_acl_state(&acl_state);
1862 posix_acl_release(acls);
1866 int ksmbd_vfs_inherit_posix_acl(struct user_namespace *user_ns,
1867 struct inode *inode, struct inode *parent_inode)
1869 struct posix_acl *acls;
1870 struct posix_acl_entry *pace;
1873 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1876 acls = get_acl(parent_inode, ACL_TYPE_DEFAULT);
1879 pace = acls->a_entries;
1881 for (i = 0; i < acls->a_count; i++, pace++) {
1882 if (pace->e_tag == ACL_MASK) {
1883 pace->e_perm = 0x07;
1888 rc = set_posix_acl(user_ns, inode, ACL_TYPE_ACCESS, acls);
1890 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1892 if (S_ISDIR(inode->i_mode)) {
1893 rc = set_posix_acl(user_ns, inode, ACL_TYPE_DEFAULT,
1896 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1899 posix_acl_release(acls);