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/genhd.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>
22 #include "../internal.h" /* for vfs_path_lookup */
26 #include "connection.h"
28 #include "vfs_cache.h"
34 #include "smb_common.h"
35 #include "mgmt/share_config.h"
36 #include "mgmt/tree_connect.h"
37 #include "mgmt/user_session.h"
38 #include "mgmt/user_config.h"
40 static char *extract_last_component(char *path)
42 char *p = strrchr(path, '/');
44 if (p && p[1] != '\0') {
53 static void ksmbd_vfs_inherit_owner(struct ksmbd_work *work,
54 struct inode *parent_inode,
57 if (!test_share_config_flag(work->tcon->share_conf,
58 KSMBD_SHARE_FLAG_INHERIT_OWNER))
61 i_uid_write(inode, i_uid_read(parent_inode));
65 * ksmbd_vfs_lock_parent() - lock parent dentry if it is stable
67 * the parent dentry got by dget_parent or @parent could be
68 * unstable, we try to lock a parent inode and lookup the
71 * the reference count of @parent isn't incremented.
73 int ksmbd_vfs_lock_parent(struct user_namespace *user_ns, struct dentry *parent,
76 struct dentry *dentry;
79 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
80 dentry = lookup_one(user_ns, child->d_name.name, parent,
83 ret = PTR_ERR(dentry);
87 if (dentry != child) {
96 inode_unlock(d_inode(parent));
100 int ksmbd_vfs_may_delete(struct user_namespace *user_ns,
101 struct dentry *dentry)
103 struct dentry *parent;
106 parent = dget_parent(dentry);
107 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
113 ret = inode_permission(user_ns, d_inode(parent),
114 MAY_EXEC | MAY_WRITE);
116 inode_unlock(d_inode(parent));
121 int ksmbd_vfs_query_maximal_access(struct user_namespace *user_ns,
122 struct dentry *dentry, __le32 *daccess)
124 struct dentry *parent;
127 *daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL);
129 if (!inode_permission(user_ns, 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 |
135 if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_READ))
136 *daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE;
138 if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_EXEC))
139 *daccess |= FILE_EXECUTE_LE;
141 parent = dget_parent(dentry);
142 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
148 if (!inode_permission(user_ns, d_inode(parent), MAY_EXEC | MAY_WRITE))
149 *daccess |= FILE_DELETE_LE;
151 inode_unlock(d_inode(parent));
157 * ksmbd_vfs_create() - vfs helper for smb create file
159 * @name: file name that is relative to share
160 * @mode: file create mode
162 * Return: 0 on success, otherwise error
164 int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode)
167 struct dentry *dentry;
170 dentry = ksmbd_vfs_kern_path_create(work, name,
171 LOOKUP_NO_SYMLINKS, &path);
172 if (IS_ERR(dentry)) {
173 err = PTR_ERR(dentry);
175 pr_err("path create failed for %s, err %d\n",
181 err = vfs_create(mnt_user_ns(path.mnt), d_inode(path.dentry),
184 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry),
187 pr_err("File(%s): creation failed (err:%d)\n", name, err);
189 done_path_create(&path, dentry);
194 * ksmbd_vfs_mkdir() - vfs helper for smb create directory
196 * @name: directory name that is relative to share
197 * @mode: directory create mode
199 * Return: 0 on success, otherwise error
201 int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode)
203 struct user_namespace *user_ns;
205 struct dentry *dentry;
208 dentry = ksmbd_vfs_kern_path_create(work, name,
209 LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY,
211 if (IS_ERR(dentry)) {
212 err = PTR_ERR(dentry);
214 ksmbd_debug(VFS, "path create failed for %s, err %d\n",
219 user_ns = mnt_user_ns(path.mnt);
221 err = vfs_mkdir(user_ns, d_inode(path.dentry), dentry, mode);
224 } else if (d_unhashed(dentry)) {
227 d = lookup_one(user_ns, dentry->d_name.name, dentry->d_parent,
233 if (unlikely(d_is_negative(d))) {
239 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), d_inode(d));
243 done_path_create(&path, dentry);
245 pr_err("mkdir(%s): creation failed (err:%d)\n", name, err);
249 static ssize_t ksmbd_vfs_getcasexattr(struct user_namespace *user_ns,
250 struct dentry *dentry, char *attr_name,
251 int attr_name_len, char **attr_value)
253 char *name, *xattr_list = NULL;
254 ssize_t value_len = -ENOENT, xattr_list_len;
256 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
257 if (xattr_list_len <= 0)
260 for (name = xattr_list; name - xattr_list < xattr_list_len;
261 name += strlen(name) + 1) {
262 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
263 if (strncasecmp(attr_name, name, attr_name_len))
266 value_len = ksmbd_vfs_getxattr(user_ns,
271 pr_err("failed to get xattr in file\n");
280 static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos,
284 char *stream_buf = NULL;
286 ksmbd_debug(VFS, "read stream data pos : %llu, count : %zd\n",
289 v_len = ksmbd_vfs_getcasexattr(file_mnt_user_ns(fp->filp),
290 fp->filp->f_path.dentry,
302 if (v_len - *pos < count)
303 count = v_len - *pos;
305 memcpy(buf, &stream_buf[*pos], count);
313 * check_lock_range() - vfs helper for smb byte range file locking
314 * @filp: the file to apply the lock to
315 * @start: lock start byte offset
316 * @end: lock end byte offset
317 * @type: byte range type read/write
319 * Return: 0 on success, otherwise error
321 static int check_lock_range(struct file *filp, loff_t start, loff_t end,
324 struct file_lock *flock;
325 struct file_lock_context *ctx = file_inode(filp)->i_flctx;
328 if (!ctx || list_empty_careful(&ctx->flc_posix))
331 spin_lock(&ctx->flc_lock);
332 list_for_each_entry(flock, &ctx->flc_posix, fl_list) {
333 /* check conflict locks */
334 if (flock->fl_end >= start && end >= flock->fl_start) {
335 if (flock->fl_type == F_RDLCK) {
337 pr_err("not allow write by shared lock\n");
341 } else if (flock->fl_type == F_WRLCK) {
342 /* check owner in lock */
343 if (flock->fl_file != filp) {
345 pr_err("not allow rw access by exclusive lock from other opens\n");
352 spin_unlock(&ctx->flc_lock);
357 * ksmbd_vfs_read() - vfs helper for smb file read
359 * @fid: file id of open file
360 * @count: read byte count
363 * Return: number of read bytes on success, otherwise error
365 int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count,
368 struct file *filp = fp->filp;
370 char *rbuf = work->aux_payload_buf;
371 struct inode *inode = file_inode(filp);
373 if (S_ISDIR(inode->i_mode))
376 if (unlikely(count == 0))
379 if (work->conn->connection_type) {
380 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
381 pr_err("no right to read(%pd)\n",
382 fp->filp->f_path.dentry);
387 if (ksmbd_stream_fd(fp))
388 return ksmbd_vfs_stream_read(fp, rbuf, pos, count);
390 if (!work->tcon->posix_extensions) {
393 ret = check_lock_range(filp, *pos, *pos + count - 1, READ);
395 pr_err("unable to read due to lock\n");
400 nbytes = kernel_read(filp, rbuf, count, pos);
402 pr_err("smb read failed for (%s), err = %zd\n",
403 fp->filename, nbytes);
411 static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
414 char *stream_buf = NULL, *wbuf;
415 struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
419 ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n",
423 if (size > XATTR_SIZE_MAX) {
424 size = XATTR_SIZE_MAX;
425 count = (*pos + count) - XATTR_SIZE_MAX;
428 v_len = ksmbd_vfs_getcasexattr(user_ns,
429 fp->filp->f_path.dentry,
433 if ((int)v_len < 0) {
434 pr_err("not found stream in xattr : %zd\n", v_len);
440 wbuf = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
447 memcpy(wbuf, stream_buf, v_len);
452 memcpy(&stream_buf[*pos], buf, count);
454 err = ksmbd_vfs_setxattr(user_ns,
455 fp->filp->f_path.dentry,
463 fp->filp->f_pos = *pos;
471 * ksmbd_vfs_write() - vfs helper for smb file write
473 * @fid: file id of open file
474 * @buf: buf containing data for writing
475 * @count: read byte count
477 * @sync: fsync after write
478 * @written: number of bytes written
480 * Return: 0 on success, otherwise error
482 int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp,
483 char *buf, size_t count, loff_t *pos, bool sync,
486 struct ksmbd_session *sess = work->sess;
488 loff_t offset = *pos;
491 if (sess->conn->connection_type) {
492 if (!(fp->daccess & FILE_WRITE_DATA_LE)) {
493 pr_err("no right to write(%pd)\n",
494 fp->filp->f_path.dentry);
502 if (ksmbd_stream_fd(fp)) {
503 err = ksmbd_vfs_stream_write(fp, buf, pos, count);
509 if (!work->tcon->posix_extensions) {
510 err = check_lock_range(filp, *pos, *pos + count - 1, WRITE);
512 pr_err("unable to write due to lock\n");
518 /* Do we need to break any of a levelII oplock? */
519 smb_break_all_levII_oplock(work, fp, 1);
521 err = kernel_write(filp, buf, count, pos);
523 ksmbd_debug(VFS, "smb write failed, err = %d\n", err);
531 err = vfs_fsync_range(filp, offset, offset + *written, 0);
533 pr_err("fsync failed for filename = %pd, err = %d\n",
534 fp->filp->f_path.dentry, err);
542 * ksmbd_vfs_getattr() - vfs helper for smb getattr
544 * @fid: file id of open file
545 * @attrs: inode attributes
547 * Return: 0 on success, otherwise error
549 int ksmbd_vfs_getattr(struct path *path, struct kstat *stat)
553 err = vfs_getattr(path, stat, STATX_BTIME, AT_STATX_SYNC_AS_STAT);
555 pr_err("getattr failed, err %d\n", err);
560 * ksmbd_vfs_fsync() - vfs helper for smb fsync
562 * @fid: file id of open file
564 * Return: 0 on success, otherwise error
566 int ksmbd_vfs_fsync(struct ksmbd_work *work, u64 fid, u64 p_id)
568 struct ksmbd_file *fp;
571 fp = ksmbd_lookup_fd_slow(work, fid, p_id);
573 pr_err("failed to get filp for fid %llu\n", fid);
576 err = vfs_fsync(fp->filp, 0);
578 pr_err("smb fsync failed, err = %d\n", err);
579 ksmbd_fd_put(work, fp);
584 * ksmbd_vfs_remove_file() - vfs helper for smb rmdir or unlink
585 * @name: directory or file name that is relative to share
587 * Return: 0 on success, otherwise error
589 int ksmbd_vfs_remove_file(struct ksmbd_work *work, char *name)
591 struct user_namespace *user_ns;
593 struct dentry *parent;
596 if (ksmbd_override_fsids(work))
599 err = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, false);
601 ksmbd_debug(VFS, "can't get %s, err %d\n", name, err);
602 ksmbd_revert_fsids(work);
606 user_ns = mnt_user_ns(path.mnt);
607 parent = dget_parent(path.dentry);
608 err = ksmbd_vfs_lock_parent(user_ns, parent, path.dentry);
612 ksmbd_revert_fsids(work);
616 if (!d_inode(path.dentry)->i_nlink) {
621 if (S_ISDIR(d_inode(path.dentry)->i_mode)) {
622 err = vfs_rmdir(user_ns, d_inode(parent), path.dentry);
623 if (err && err != -ENOTEMPTY)
624 ksmbd_debug(VFS, "%s: rmdir failed, err %d\n", name,
627 err = vfs_unlink(user_ns, d_inode(parent), path.dentry, NULL);
629 ksmbd_debug(VFS, "%s: unlink failed, err %d\n", name,
634 inode_unlock(d_inode(parent));
637 ksmbd_revert_fsids(work);
642 * ksmbd_vfs_link() - vfs helper for creating smb hardlink
643 * @oldname: source file name
644 * @newname: hardlink name that is relative to share
646 * Return: 0 on success, otherwise error
648 int ksmbd_vfs_link(struct ksmbd_work *work, const char *oldname,
651 struct path oldpath, newpath;
652 struct dentry *dentry;
655 if (ksmbd_override_fsids(work))
658 err = kern_path(oldname, LOOKUP_NO_SYMLINKS, &oldpath);
660 pr_err("cannot get linux path for %s, err = %d\n",
665 dentry = ksmbd_vfs_kern_path_create(work, newname,
666 LOOKUP_NO_SYMLINKS | LOOKUP_REVAL,
668 if (IS_ERR(dentry)) {
669 err = PTR_ERR(dentry);
670 pr_err("path create err for %s, err %d\n", newname, err);
675 if (oldpath.mnt != newpath.mnt) {
676 pr_err("vfs_link failed err %d\n", err);
680 err = vfs_link(oldpath.dentry, mnt_user_ns(newpath.mnt),
681 d_inode(newpath.dentry),
684 ksmbd_debug(VFS, "vfs_link failed err %d\n", err);
687 done_path_create(&newpath, dentry);
691 ksmbd_revert_fsids(work);
695 static int ksmbd_validate_entry_in_use(struct dentry *src_dent)
697 struct dentry *dst_dent;
699 spin_lock(&src_dent->d_lock);
700 list_for_each_entry(dst_dent, &src_dent->d_subdirs, d_child) {
701 struct ksmbd_file *child_fp;
703 if (d_really_is_negative(dst_dent))
706 child_fp = ksmbd_lookup_fd_inode(d_inode(dst_dent));
708 spin_unlock(&src_dent->d_lock);
709 ksmbd_debug(VFS, "Forbid rename, sub file/dir is in use\n");
713 spin_unlock(&src_dent->d_lock);
718 static int __ksmbd_vfs_rename(struct ksmbd_work *work,
719 struct user_namespace *src_user_ns,
720 struct dentry *src_dent_parent,
721 struct dentry *src_dent,
722 struct user_namespace *dst_user_ns,
723 struct dentry *dst_dent_parent,
724 struct dentry *trap_dent,
727 struct dentry *dst_dent;
730 if (!work->tcon->posix_extensions) {
731 err = ksmbd_validate_entry_in_use(src_dent);
736 if (d_really_is_negative(src_dent_parent))
738 if (d_really_is_negative(dst_dent_parent))
740 if (d_really_is_negative(src_dent))
742 if (src_dent == trap_dent)
745 if (ksmbd_override_fsids(work))
748 dst_dent = lookup_one(dst_user_ns, dst_name, dst_dent_parent,
750 err = PTR_ERR(dst_dent);
751 if (IS_ERR(dst_dent)) {
752 pr_err("lookup failed %s [%d]\n", dst_name, err);
757 if (dst_dent != trap_dent && !d_really_is_positive(dst_dent)) {
758 struct renamedata rd = {
759 .old_mnt_userns = src_user_ns,
760 .old_dir = d_inode(src_dent_parent),
761 .old_dentry = src_dent,
762 .new_mnt_userns = dst_user_ns,
763 .new_dir = d_inode(dst_dent_parent),
764 .new_dentry = dst_dent,
766 err = vfs_rename(&rd);
769 pr_err("vfs_rename failed err %d\n", err);
773 ksmbd_revert_fsids(work);
777 int ksmbd_vfs_fp_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
780 struct user_namespace *user_ns;
781 struct path dst_path;
782 struct dentry *src_dent_parent, *dst_dent_parent;
783 struct dentry *src_dent, *trap_dent, *src_child;
787 dst_name = extract_last_component(newname);
793 src_dent_parent = dget_parent(fp->filp->f_path.dentry);
794 src_dent = fp->filp->f_path.dentry;
796 err = ksmbd_vfs_kern_path(work, newname,
797 LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY,
800 ksmbd_debug(VFS, "Cannot get path for %s [%d]\n", newname, err);
803 dst_dent_parent = dst_path.dentry;
805 trap_dent = lock_rename(src_dent_parent, dst_dent_parent);
807 dget(dst_dent_parent);
808 user_ns = file_mnt_user_ns(fp->filp);
809 src_child = lookup_one(user_ns, src_dent->d_name.name, src_dent_parent,
810 src_dent->d_name.len);
811 if (IS_ERR(src_child)) {
812 err = PTR_ERR(src_child);
816 if (src_child != src_dent) {
823 err = __ksmbd_vfs_rename(work,
827 mnt_user_ns(dst_path.mnt),
833 dput(dst_dent_parent);
834 unlock_rename(src_dent_parent, dst_dent_parent);
837 dput(src_dent_parent);
842 * ksmbd_vfs_truncate() - vfs helper for smb file truncate
844 * @fid: file id of old file
845 * @size: truncate to given size
847 * Return: 0 on success, otherwise error
849 int ksmbd_vfs_truncate(struct ksmbd_work *work,
850 struct ksmbd_file *fp, loff_t size)
857 /* Do we need to break any of a levelII oplock? */
858 smb_break_all_levII_oplock(work, fp, 1);
860 if (!work->tcon->posix_extensions) {
861 struct inode *inode = file_inode(filp);
863 if (size < inode->i_size) {
864 err = check_lock_range(filp, size,
865 inode->i_size - 1, WRITE);
867 err = check_lock_range(filp, inode->i_size,
872 pr_err("failed due to lock\n");
877 err = vfs_truncate(&filp->f_path, size);
879 pr_err("truncate failed for filename : %s err %d\n",
885 * ksmbd_vfs_listxattr() - vfs helper for smb list extended attributes
886 * @dentry: dentry of file for listing xattrs
887 * @list: destination buffer
888 * @size: destination buffer length
890 * Return: xattr list length on success, otherwise error
892 ssize_t ksmbd_vfs_listxattr(struct dentry *dentry, char **list)
897 size = vfs_listxattr(dentry, NULL, 0);
901 vlist = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
906 size = vfs_listxattr(dentry, vlist, size);
908 ksmbd_debug(VFS, "listxattr failed\n");
916 static ssize_t ksmbd_vfs_xattr_len(struct user_namespace *user_ns,
917 struct dentry *dentry, char *xattr_name)
919 return vfs_getxattr(user_ns, dentry, xattr_name, NULL, 0);
923 * ksmbd_vfs_getxattr() - vfs helper for smb get extended attributes value
924 * @user_ns: user namespace
925 * @dentry: dentry of file for getting xattrs
926 * @xattr_name: name of xattr name to query
927 * @xattr_buf: destination buffer xattr value
929 * Return: read xattr value length on success, otherwise error
931 ssize_t ksmbd_vfs_getxattr(struct user_namespace *user_ns,
932 struct dentry *dentry,
933 char *xattr_name, char **xattr_buf)
939 xattr_len = ksmbd_vfs_xattr_len(user_ns, dentry, xattr_name);
943 buf = kmalloc(xattr_len + 1, GFP_KERNEL);
947 xattr_len = vfs_getxattr(user_ns, dentry, xattr_name,
948 (void *)buf, xattr_len);
957 * ksmbd_vfs_setxattr() - vfs helper for smb set extended attributes value
958 * @user_ns: user namespace
959 * @dentry: dentry to set XATTR at
960 * @name: xattr name for setxattr
961 * @value: xattr value to set
962 * @size: size of xattr value
963 * @flags: destination buffer length
965 * Return: 0 on success, otherwise error
967 int ksmbd_vfs_setxattr(struct user_namespace *user_ns,
968 struct dentry *dentry, const char *attr_name,
969 const void *attr_value, size_t attr_size, int flags)
973 err = vfs_setxattr(user_ns,
980 ksmbd_debug(VFS, "setxattr failed, err %d\n", err);
985 * ksmbd_vfs_set_fadvise() - convert smb IO caching options to linux options
986 * @filp: file pointer for IO
987 * @options: smb IO options
989 void ksmbd_vfs_set_fadvise(struct file *filp, __le32 option)
991 struct address_space *mapping;
993 mapping = filp->f_mapping;
995 if (!option || !mapping)
998 if (option & FILE_WRITE_THROUGH_LE) {
999 filp->f_flags |= O_SYNC;
1000 } else if (option & FILE_SEQUENTIAL_ONLY_LE) {
1001 filp->f_ra.ra_pages = inode_to_bdi(mapping->host)->ra_pages * 2;
1002 spin_lock(&filp->f_lock);
1003 filp->f_mode &= ~FMODE_RANDOM;
1004 spin_unlock(&filp->f_lock);
1005 } else if (option & FILE_RANDOM_ACCESS_LE) {
1006 spin_lock(&filp->f_lock);
1007 filp->f_mode |= FMODE_RANDOM;
1008 spin_unlock(&filp->f_lock);
1012 int ksmbd_vfs_zero_data(struct ksmbd_work *work, struct ksmbd_file *fp,
1013 loff_t off, loff_t len)
1015 smb_break_all_levII_oplock(work, fp, 1);
1016 if (fp->f_ci->m_fattr & FILE_ATTRIBUTE_SPARSE_FILE_LE)
1017 return vfs_fallocate(fp->filp,
1018 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1021 return vfs_fallocate(fp->filp, FALLOC_FL_ZERO_RANGE, off, len);
1024 int ksmbd_vfs_fqar_lseek(struct ksmbd_file *fp, loff_t start, loff_t length,
1025 struct file_allocated_range_buffer *ranges,
1026 unsigned int in_count, unsigned int *out_count)
1028 struct file *f = fp->filp;
1029 struct inode *inode = file_inode(fp->filp);
1030 loff_t maxbytes = (u64)inode->i_sb->s_maxbytes, end;
1031 loff_t extent_start, extent_end;
1034 if (start > maxbytes)
1041 * Shrink request scope to what the fs can actually handle.
1043 if (length > maxbytes || (maxbytes - length) < start)
1044 length = maxbytes - start;
1046 if (start + length > inode->i_size)
1047 length = inode->i_size - start;
1050 end = start + length;
1051 while (start < end && *out_count < in_count) {
1052 extent_start = f->f_op->llseek(f, start, SEEK_DATA);
1053 if (extent_start < 0) {
1054 if (extent_start != -ENXIO)
1055 ret = (int)extent_start;
1059 if (extent_start >= end)
1062 extent_end = f->f_op->llseek(f, extent_start, SEEK_HOLE);
1063 if (extent_end < 0) {
1064 if (extent_end != -ENXIO)
1065 ret = (int)extent_end;
1067 } else if (extent_start >= extent_end) {
1071 ranges[*out_count].file_offset = cpu_to_le64(extent_start);
1072 ranges[(*out_count)++].length =
1073 cpu_to_le64(min(extent_end, end) - extent_start);
1081 int ksmbd_vfs_remove_xattr(struct user_namespace *user_ns,
1082 struct dentry *dentry, char *attr_name)
1084 return vfs_removexattr(user_ns, dentry, attr_name);
1087 int ksmbd_vfs_unlink(struct user_namespace *user_ns,
1088 struct dentry *dir, struct dentry *dentry)
1092 err = ksmbd_vfs_lock_parent(user_ns, dir, dentry);
1097 if (S_ISDIR(d_inode(dentry)->i_mode))
1098 err = vfs_rmdir(user_ns, d_inode(dir), dentry);
1100 err = vfs_unlink(user_ns, d_inode(dir), dentry, NULL);
1103 inode_unlock(d_inode(dir));
1105 ksmbd_debug(VFS, "failed to delete, err %d\n", err);
1110 static int __dir_empty(struct dir_context *ctx, const char *name, int namlen,
1111 loff_t offset, u64 ino, unsigned int d_type)
1113 struct ksmbd_readdir_data *buf;
1115 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
1116 buf->dirent_count++;
1118 if (buf->dirent_count > 2)
1124 * ksmbd_vfs_empty_dir() - check for empty directory
1125 * @fp: ksmbd file pointer
1127 * Return: true if directory empty, otherwise false
1129 int ksmbd_vfs_empty_dir(struct ksmbd_file *fp)
1132 struct ksmbd_readdir_data readdir_data;
1134 memset(&readdir_data, 0, sizeof(struct ksmbd_readdir_data));
1136 set_ctx_actor(&readdir_data.ctx, __dir_empty);
1137 readdir_data.dirent_count = 0;
1139 err = iterate_dir(fp->filp, &readdir_data.ctx);
1140 if (readdir_data.dirent_count > 2)
1147 static int __caseless_lookup(struct dir_context *ctx, const char *name,
1148 int namlen, loff_t offset, u64 ino,
1149 unsigned int d_type)
1151 struct ksmbd_readdir_data *buf;
1153 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
1155 if (buf->used != namlen)
1157 if (!strncasecmp((char *)buf->private, name, namlen)) {
1158 memcpy((char *)buf->private, name, namlen);
1159 buf->dirent_count = 1;
1166 * ksmbd_vfs_lookup_in_dir() - lookup a file in a directory
1168 * @name: filename to lookup
1169 * @namelen: filename length
1171 * Return: 0 on success, otherwise error
1173 static int ksmbd_vfs_lookup_in_dir(struct path *dir, char *name, size_t namelen)
1177 int flags = O_RDONLY | O_LARGEFILE;
1178 struct ksmbd_readdir_data readdir_data = {
1179 .ctx.actor = __caseless_lookup,
1185 dfilp = dentry_open(dir, flags, current_cred());
1187 return PTR_ERR(dfilp);
1189 ret = iterate_dir(dfilp, &readdir_data.ctx);
1190 if (readdir_data.dirent_count > 0)
1197 * ksmbd_vfs_kern_path() - lookup a file and get path info
1198 * @name: file path that is relative to share
1199 * @flags: lookup flags
1200 * @path: if lookup succeed, return path info
1201 * @caseless: caseless filename lookup
1203 * Return: 0 on success, otherwise error
1205 int ksmbd_vfs_kern_path(struct ksmbd_work *work, char *name,
1206 unsigned int flags, struct path *path, bool caseless)
1208 struct ksmbd_share_config *share_conf = work->tcon->share_conf;
1211 flags |= LOOKUP_BENEATH;
1212 err = vfs_path_lookup(share_conf->vfs_path.dentry,
1213 share_conf->vfs_path.mnt,
1223 size_t path_len, remain_len;
1225 filepath = kstrdup(name, GFP_KERNEL);
1229 path_len = strlen(filepath);
1230 remain_len = path_len;
1232 parent = share_conf->vfs_path;
1235 while (d_can_lookup(parent.dentry)) {
1236 char *filename = filepath + path_len - remain_len;
1237 char *next = strchrnul(filename, '/');
1238 size_t filename_len = next - filename;
1239 bool is_last = !next[0];
1241 if (filename_len == 0)
1244 err = ksmbd_vfs_lookup_in_dir(&parent, filename,
1252 err = vfs_path_lookup(share_conf->vfs_path.dentry,
1253 share_conf->vfs_path.mnt,
1265 remain_len -= filename_len + 1;
1276 struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work,
1282 struct dentry *dent;
1284 abs_name = convert_to_unix_name(work->tcon->share_conf, name);
1286 return ERR_PTR(-ENOMEM);
1288 dent = kern_path_create(AT_FDCWD, abs_name, path, flags);
1293 int ksmbd_vfs_remove_acl_xattrs(struct user_namespace *user_ns,
1294 struct dentry *dentry)
1296 char *name, *xattr_list = NULL;
1297 ssize_t xattr_list_len;
1300 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
1301 if (xattr_list_len < 0) {
1303 } else if (!xattr_list_len) {
1304 ksmbd_debug(SMB, "empty xattr in the file\n");
1308 for (name = xattr_list; name - xattr_list < xattr_list_len;
1309 name += strlen(name) + 1) {
1310 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
1312 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
1313 sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1) ||
1314 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
1315 sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1)) {
1316 err = ksmbd_vfs_remove_xattr(user_ns, dentry, name);
1319 "remove acl xattr failed : %s\n", name);
1327 int ksmbd_vfs_remove_sd_xattrs(struct user_namespace *user_ns,
1328 struct dentry *dentry)
1330 char *name, *xattr_list = NULL;
1331 ssize_t xattr_list_len;
1334 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
1335 if (xattr_list_len < 0) {
1337 } else if (!xattr_list_len) {
1338 ksmbd_debug(SMB, "empty xattr in the file\n");
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));
1346 if (!strncmp(name, XATTR_NAME_SD, XATTR_NAME_SD_LEN)) {
1347 err = ksmbd_vfs_remove_xattr(user_ns, dentry, name);
1349 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
1357 static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct user_namespace *user_ns,
1358 struct inode *inode,
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;
1367 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1370 posix_acls = get_acl(inode, acl_type);
1374 smb_acl = kzalloc(sizeof(struct xattr_smb_acl) +
1375 sizeof(struct xattr_acl_entry) * posix_acls->a_count,
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) {
1386 xa_entry->type = SMB_ACL_USER;
1387 xa_entry->uid = posix_acl_uid_translate(user_ns, pa_entry);
1390 xa_entry->type = SMB_ACL_USER_OBJ;
1393 xa_entry->type = SMB_ACL_GROUP;
1394 xa_entry->gid = posix_acl_gid_translate(user_ns, pa_entry);
1397 xa_entry->type = SMB_ACL_GROUP_OBJ;
1400 xa_entry->type = SMB_ACL_OTHER;
1403 xa_entry->type = SMB_ACL_MASK;
1406 pr_err("unknown type : 0x%x\n", pa_entry->e_tag);
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;
1418 posix_acl_release(posix_acls);
1422 int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
1423 struct user_namespace *user_ns,
1424 struct dentry *dentry,
1425 struct smb_ntsd *pntsd, int len)
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 inode *inode = d_inode(dentry);
1434 acl.hash_type = XATTR_SD_HASH_TYPE_SHA256;
1435 acl.current_time = ksmbd_UnixTimeToNT(current_time(inode));
1437 memcpy(acl.desc, "posix_acl", 9);
1441 cpu_to_le32(le32_to_cpu(pntsd->osidoffset) + NDR_NTSD_OFFSETOF);
1443 cpu_to_le32(le32_to_cpu(pntsd->gsidoffset) + NDR_NTSD_OFFSETOF);
1445 cpu_to_le32(le32_to_cpu(pntsd->dacloffset) + NDR_NTSD_OFFSETOF);
1447 acl.sd_buf = (char *)pntsd;
1450 rc = ksmbd_gen_sd_hash(conn, acl.sd_buf, acl.sd_size, acl.hash);
1452 pr_err("failed to generate hash for ndr acl\n");
1456 smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
1458 if (S_ISDIR(inode->i_mode))
1459 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
1462 rc = ndr_encode_posix_acl(&acl_ndr, user_ns, inode,
1463 smb_acl, def_smb_acl);
1465 pr_err("failed to encode ndr to posix acl\n");
1469 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset,
1470 acl.posix_acl_hash);
1472 pr_err("failed to generate hash for ndr acl\n");
1476 rc = ndr_encode_v4_ntacl(&sd_ndr, &acl);
1478 pr_err("failed to encode ndr to posix acl\n");
1482 rc = ksmbd_vfs_setxattr(user_ns, dentry,
1483 XATTR_NAME_SD, sd_ndr.data,
1486 pr_err("Failed to store XATTR ntacl :%d\n", rc);
1490 kfree(acl_ndr.data);
1496 int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
1497 struct user_namespace *user_ns,
1498 struct dentry *dentry,
1499 struct smb_ntsd **pntsd)
1503 struct inode *inode = d_inode(dentry);
1504 struct ndr acl_ndr = {0};
1505 struct xattr_ntacl acl;
1506 struct xattr_smb_acl *smb_acl = NULL, *def_smb_acl = NULL;
1507 __u8 cmp_hash[XATTR_SD_HASH_SIZE] = {0};
1509 rc = ksmbd_vfs_getxattr(user_ns, dentry, XATTR_NAME_SD, &n.data);
1514 rc = ndr_decode_v4_ntacl(&n, &acl);
1518 smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
1520 if (S_ISDIR(inode->i_mode))
1521 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
1524 rc = ndr_encode_posix_acl(&acl_ndr, user_ns, inode, smb_acl,
1527 pr_err("failed to encode ndr to posix acl\n");
1531 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset, cmp_hash);
1533 pr_err("failed to generate hash for ndr acl\n");
1537 if (memcmp(cmp_hash, acl.posix_acl_hash, XATTR_SD_HASH_SIZE)) {
1538 pr_err("hash value diff\n");
1543 *pntsd = acl.sd_buf;
1544 (*pntsd)->osidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->osidoffset) -
1546 (*pntsd)->gsidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->gsidoffset) -
1548 (*pntsd)->dacloffset = cpu_to_le32(le32_to_cpu((*pntsd)->dacloffset) -
1553 kfree(acl_ndr.data);
1566 int ksmbd_vfs_set_dos_attrib_xattr(struct user_namespace *user_ns,
1567 struct dentry *dentry,
1568 struct xattr_dos_attrib *da)
1573 err = ndr_encode_dos_attr(&n, da);
1577 err = ksmbd_vfs_setxattr(user_ns, dentry, XATTR_NAME_DOS_ATTRIBUTE,
1578 (void *)n.data, n.offset, 0);
1580 ksmbd_debug(SMB, "failed to store dos attribute in xattr\n");
1586 int ksmbd_vfs_get_dos_attrib_xattr(struct user_namespace *user_ns,
1587 struct dentry *dentry,
1588 struct xattr_dos_attrib *da)
1593 err = ksmbd_vfs_getxattr(user_ns, dentry, XATTR_NAME_DOS_ATTRIBUTE,
1597 if (ndr_decode_dos_attr(&n, da))
1601 ksmbd_debug(SMB, "failed to load dos attribute in xattr\n");
1608 * ksmbd_vfs_init_kstat() - convert unix stat information to smb stat format
1609 * @p: destination buffer
1610 * @ksmbd_kstat: ksmbd kstat wrapper
1612 void *ksmbd_vfs_init_kstat(char **p, struct ksmbd_kstat *ksmbd_kstat)
1614 struct file_directory_info *info = (struct file_directory_info *)(*p);
1615 struct kstat *kstat = ksmbd_kstat->kstat;
1618 info->FileIndex = 0;
1619 info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
1620 time = ksmbd_UnixTimeToNT(kstat->atime);
1621 info->LastAccessTime = cpu_to_le64(time);
1622 time = ksmbd_UnixTimeToNT(kstat->mtime);
1623 info->LastWriteTime = cpu_to_le64(time);
1624 time = ksmbd_UnixTimeToNT(kstat->ctime);
1625 info->ChangeTime = cpu_to_le64(time);
1627 if (ksmbd_kstat->file_attributes & FILE_ATTRIBUTE_DIRECTORY_LE) {
1628 info->EndOfFile = 0;
1629 info->AllocationSize = 0;
1631 info->EndOfFile = cpu_to_le64(kstat->size);
1632 info->AllocationSize = cpu_to_le64(kstat->blocks << 9);
1634 info->ExtFileAttributes = ksmbd_kstat->file_attributes;
1639 int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work,
1640 struct user_namespace *user_ns,
1641 struct dentry *dentry,
1642 struct ksmbd_kstat *ksmbd_kstat)
1647 generic_fillattr(user_ns, d_inode(dentry), ksmbd_kstat->kstat);
1649 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
1650 ksmbd_kstat->create_time = time;
1653 * set default value for the case that store dos attributes is not yes
1654 * or that acl is disable in server's filesystem and the config is yes.
1656 if (S_ISDIR(ksmbd_kstat->kstat->mode))
1657 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_DIRECTORY_LE;
1659 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_ARCHIVE_LE;
1661 if (test_share_config_flag(work->tcon->share_conf,
1662 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
1663 struct xattr_dos_attrib da;
1665 rc = ksmbd_vfs_get_dos_attrib_xattr(user_ns, dentry, &da);
1667 ksmbd_kstat->file_attributes = cpu_to_le32(da.attr);
1668 ksmbd_kstat->create_time = da.create_time;
1670 ksmbd_debug(VFS, "fail to load dos attribute.\n");
1677 ssize_t ksmbd_vfs_casexattr_len(struct user_namespace *user_ns,
1678 struct dentry *dentry, char *attr_name,
1681 char *name, *xattr_list = NULL;
1682 ssize_t value_len = -ENOENT, xattr_list_len;
1684 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
1685 if (xattr_list_len <= 0)
1688 for (name = xattr_list; name - xattr_list < xattr_list_len;
1689 name += strlen(name) + 1) {
1690 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
1691 if (strncasecmp(attr_name, name, attr_name_len))
1694 value_len = ksmbd_vfs_xattr_len(user_ns, dentry, name);
1703 int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name,
1704 size_t *xattr_stream_name_size, int s_type)
1708 if (s_type == DIR_STREAM)
1709 type = ":$INDEX_ALLOCATION";
1713 buf = kasprintf(GFP_KERNEL, "%s%s%s",
1714 XATTR_NAME_STREAM, stream_name, type);
1718 *xattr_stream_name = buf;
1719 *xattr_stream_name_size = strlen(buf) + 1;
1724 int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work,
1725 struct ksmbd_file *src_fp,
1726 struct ksmbd_file *dst_fp,
1727 struct srv_copychunk *chunks,
1728 unsigned int chunk_count,
1729 unsigned int *chunk_count_written,
1730 unsigned int *chunk_size_written,
1731 loff_t *total_size_written)
1734 loff_t src_off, dst_off, src_file_size;
1738 *chunk_count_written = 0;
1739 *chunk_size_written = 0;
1740 *total_size_written = 0;
1742 if (!(src_fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
1743 pr_err("no right to read(%pd)\n", src_fp->filp->f_path.dentry);
1746 if (!(dst_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) {
1747 pr_err("no right to write(%pd)\n", dst_fp->filp->f_path.dentry);
1751 if (ksmbd_stream_fd(src_fp) || ksmbd_stream_fd(dst_fp))
1754 smb_break_all_levII_oplock(work, dst_fp, 1);
1756 if (!work->tcon->posix_extensions) {
1757 for (i = 0; i < chunk_count; i++) {
1758 src_off = le64_to_cpu(chunks[i].SourceOffset);
1759 dst_off = le64_to_cpu(chunks[i].TargetOffset);
1760 len = le32_to_cpu(chunks[i].Length);
1762 if (check_lock_range(src_fp->filp, src_off,
1763 src_off + len - 1, READ))
1765 if (check_lock_range(dst_fp->filp, dst_off,
1766 dst_off + len - 1, WRITE))
1771 src_file_size = i_size_read(file_inode(src_fp->filp));
1773 for (i = 0; i < chunk_count; i++) {
1774 src_off = le64_to_cpu(chunks[i].SourceOffset);
1775 dst_off = le64_to_cpu(chunks[i].TargetOffset);
1776 len = le32_to_cpu(chunks[i].Length);
1778 if (src_off + len > src_file_size)
1781 ret = vfs_copy_file_range(src_fp->filp, src_off,
1782 dst_fp->filp, dst_off, len, 0);
1786 *chunk_count_written += 1;
1787 *total_size_written += ret;
1792 void ksmbd_vfs_posix_lock_wait(struct file_lock *flock)
1794 wait_event(flock->fl_wait, !flock->fl_blocker);
1797 int ksmbd_vfs_posix_lock_wait_timeout(struct file_lock *flock, long timeout)
1799 return wait_event_interruptible_timeout(flock->fl_wait,
1804 void ksmbd_vfs_posix_lock_unblock(struct file_lock *flock)
1806 locks_delete_block(flock);
1809 int ksmbd_vfs_set_init_posix_acl(struct user_namespace *user_ns,
1810 struct inode *inode)
1812 struct posix_acl_state acl_state;
1813 struct posix_acl *acls;
1816 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1819 ksmbd_debug(SMB, "Set posix acls\n");
1820 rc = init_acl_state(&acl_state, 1);
1824 /* Set default owner group */
1825 acl_state.owner.allow = (inode->i_mode & 0700) >> 6;
1826 acl_state.group.allow = (inode->i_mode & 0070) >> 3;
1827 acl_state.other.allow = inode->i_mode & 0007;
1828 acl_state.users->aces[acl_state.users->n].uid = inode->i_uid;
1829 acl_state.users->aces[acl_state.users->n++].perms.allow =
1830 acl_state.owner.allow;
1831 acl_state.groups->aces[acl_state.groups->n].gid = inode->i_gid;
1832 acl_state.groups->aces[acl_state.groups->n++].perms.allow =
1833 acl_state.group.allow;
1834 acl_state.mask.allow = 0x07;
1836 acls = posix_acl_alloc(6, GFP_KERNEL);
1838 free_acl_state(&acl_state);
1841 posix_state_to_acl(&acl_state, acls->a_entries);
1842 rc = set_posix_acl(user_ns, inode, ACL_TYPE_ACCESS, acls);
1844 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1846 else if (S_ISDIR(inode->i_mode)) {
1847 posix_state_to_acl(&acl_state, acls->a_entries);
1848 rc = set_posix_acl(user_ns, inode, ACL_TYPE_DEFAULT,
1851 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1854 free_acl_state(&acl_state);
1855 posix_acl_release(acls);
1859 int ksmbd_vfs_inherit_posix_acl(struct user_namespace *user_ns,
1860 struct inode *inode, struct inode *parent_inode)
1862 struct posix_acl *acls;
1863 struct posix_acl_entry *pace;
1866 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1869 acls = get_acl(parent_inode, ACL_TYPE_DEFAULT);
1872 pace = acls->a_entries;
1874 for (i = 0; i < acls->a_count; i++, pace++) {
1875 if (pace->e_tag == ACL_MASK) {
1876 pace->e_perm = 0x07;
1881 rc = set_posix_acl(user_ns, inode, ACL_TYPE_ACCESS, acls);
1883 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1885 if (S_ISDIR(inode->i_mode)) {
1886 rc = set_posix_acl(user_ns, inode, ACL_TYPE_DEFAULT,
1889 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1892 posix_acl_release(acls);