1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 #include <linux/kernel.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>
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 void ksmbd_vfs_inherit_owner(struct ksmbd_work *work,
40 struct inode *parent_inode,
43 if (!test_share_config_flag(work->tcon->share_conf,
44 KSMBD_SHARE_FLAG_INHERIT_OWNER))
47 i_uid_write(inode, i_uid_read(parent_inode));
51 * ksmbd_vfs_lock_parent() - lock parent dentry if it is stable
53 int ksmbd_vfs_lock_parent(struct dentry *parent, struct dentry *child)
55 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
56 if (child->d_parent != parent) {
57 inode_unlock(d_inode(parent));
64 static int ksmbd_vfs_path_lookup_locked(struct ksmbd_share_config *share_conf,
65 char *pathname, unsigned int flags,
69 struct filename *filename;
70 struct path *root_share_path = &share_conf->vfs_path;
72 struct path parent_path;
75 if (pathname[0] == '\0') {
76 pathname = share_conf->path;
77 root_share_path = NULL;
79 flags |= LOOKUP_BENEATH;
82 filename = getname_kernel(pathname);
84 return PTR_ERR(filename);
86 err = vfs_path_parent_lookup(filename, flags,
87 &parent_path, &last, &type,
94 if (unlikely(type != LAST_NORM)) {
95 path_put(&parent_path);
100 inode_lock_nested(parent_path.dentry->d_inode, I_MUTEX_PARENT);
101 d = lookup_one_qstr_excl(&last, parent_path.dentry, 0);
105 if (d_is_negative(d)) {
111 path->mnt = share_conf->vfs_path.mnt;
112 path_put(&parent_path);
118 inode_unlock(parent_path.dentry->d_inode);
119 path_put(&parent_path);
124 int ksmbd_vfs_query_maximal_access(struct mnt_idmap *idmap,
125 struct dentry *dentry, __le32 *daccess)
129 *daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL);
131 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_WRITE))
132 *daccess |= cpu_to_le32(WRITE_DAC | WRITE_OWNER | SYNCHRONIZE |
133 FILE_WRITE_DATA | FILE_APPEND_DATA |
134 FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES |
137 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_READ))
138 *daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE;
140 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_EXEC))
141 *daccess |= FILE_EXECUTE_LE;
143 if (!inode_permission(idmap, d_inode(dentry->d_parent), MAY_EXEC | MAY_WRITE))
144 *daccess |= FILE_DELETE_LE;
150 * ksmbd_vfs_create() - vfs helper for smb create file
152 * @name: file name that is relative to share
153 * @mode: file create mode
155 * Return: 0 on success, otherwise error
157 int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode)
160 struct dentry *dentry;
163 dentry = ksmbd_vfs_kern_path_create(work, name,
164 LOOKUP_NO_SYMLINKS, &path);
165 if (IS_ERR(dentry)) {
166 err = PTR_ERR(dentry);
168 pr_err("path create failed for %s, err %d\n",
174 err = vfs_create(mnt_idmap(path.mnt), d_inode(path.dentry),
177 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry),
180 pr_err("File(%s): creation failed (err:%d)\n", name, err);
182 done_path_create(&path, dentry);
187 * ksmbd_vfs_mkdir() - vfs helper for smb create directory
189 * @name: directory name that is relative to share
190 * @mode: directory create mode
192 * Return: 0 on success, otherwise error
194 int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode)
196 struct mnt_idmap *idmap;
198 struct dentry *dentry;
201 dentry = ksmbd_vfs_kern_path_create(work, name,
202 LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY,
204 if (IS_ERR(dentry)) {
205 err = PTR_ERR(dentry);
207 ksmbd_debug(VFS, "path create failed for %s, err %d\n",
212 idmap = mnt_idmap(path.mnt);
214 err = vfs_mkdir(idmap, d_inode(path.dentry), dentry, mode);
217 } else if (d_unhashed(dentry)) {
220 d = lookup_one(idmap, dentry->d_name.name, dentry->d_parent,
226 if (unlikely(d_is_negative(d))) {
232 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), d_inode(d));
236 done_path_create(&path, dentry);
238 pr_err("mkdir(%s): creation failed (err:%d)\n", name, err);
242 static ssize_t ksmbd_vfs_getcasexattr(struct mnt_idmap *idmap,
243 struct dentry *dentry, char *attr_name,
244 int attr_name_len, char **attr_value)
246 char *name, *xattr_list = NULL;
247 ssize_t value_len = -ENOENT, xattr_list_len;
249 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
250 if (xattr_list_len <= 0)
253 for (name = xattr_list; name - xattr_list < xattr_list_len;
254 name += strlen(name) + 1) {
255 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
256 if (strncasecmp(attr_name, name, attr_name_len))
259 value_len = ksmbd_vfs_getxattr(idmap,
264 pr_err("failed to get xattr in file\n");
273 static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos,
277 char *stream_buf = NULL;
279 ksmbd_debug(VFS, "read stream data pos : %llu, count : %zd\n",
282 v_len = ksmbd_vfs_getcasexattr(file_mnt_idmap(fp->filp),
283 fp->filp->f_path.dentry,
295 if (v_len - *pos < count)
296 count = v_len - *pos;
298 memcpy(buf, &stream_buf[*pos], count);
306 * check_lock_range() - vfs helper for smb byte range file locking
307 * @filp: the file to apply the lock to
308 * @start: lock start byte offset
309 * @end: lock end byte offset
310 * @type: byte range type read/write
312 * Return: 0 on success, otherwise error
314 static int check_lock_range(struct file *filp, loff_t start, loff_t end,
317 struct file_lock *flock;
318 struct file_lock_context *ctx = locks_inode_context(file_inode(filp));
321 if (!ctx || list_empty_careful(&ctx->flc_posix))
324 spin_lock(&ctx->flc_lock);
325 list_for_each_entry(flock, &ctx->flc_posix, fl_list) {
326 /* check conflict locks */
327 if (flock->fl_end >= start && end >= flock->fl_start) {
328 if (flock->fl_type == F_RDLCK) {
330 pr_err("not allow write by shared lock\n");
334 } else if (flock->fl_type == F_WRLCK) {
335 /* check owner in lock */
336 if (flock->fl_file != filp) {
338 pr_err("not allow rw access by exclusive lock from other opens\n");
345 spin_unlock(&ctx->flc_lock);
350 * ksmbd_vfs_read() - vfs helper for smb file read
352 * @fid: file id of open file
353 * @count: read byte count
356 * Return: number of read bytes on success, otherwise error
358 int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count,
361 struct file *filp = fp->filp;
363 char *rbuf = work->aux_payload_buf;
364 struct inode *inode = file_inode(filp);
366 if (S_ISDIR(inode->i_mode))
369 if (unlikely(count == 0))
372 if (work->conn->connection_type) {
373 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
374 pr_err("no right to read(%pD)\n", fp->filp);
379 if (ksmbd_stream_fd(fp))
380 return ksmbd_vfs_stream_read(fp, rbuf, pos, count);
382 if (!work->tcon->posix_extensions) {
385 ret = check_lock_range(filp, *pos, *pos + count - 1, READ);
387 pr_err("unable to read due to lock\n");
392 nbytes = kernel_read(filp, rbuf, count, pos);
394 pr_err("smb read failed, err = %zd\n", nbytes);
402 static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
405 char *stream_buf = NULL, *wbuf;
406 struct mnt_idmap *idmap = file_mnt_idmap(fp->filp);
410 ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n",
414 if (size > XATTR_SIZE_MAX) {
415 size = XATTR_SIZE_MAX;
416 count = (*pos + count) - XATTR_SIZE_MAX;
419 v_len = ksmbd_vfs_getcasexattr(idmap,
420 fp->filp->f_path.dentry,
424 if ((int)v_len < 0) {
425 pr_err("not found stream in xattr : %zd\n", v_len);
431 wbuf = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
438 memcpy(wbuf, stream_buf, v_len);
443 memcpy(&stream_buf[*pos], buf, count);
445 err = ksmbd_vfs_setxattr(idmap,
446 fp->filp->f_path.dentry,
454 fp->filp->f_pos = *pos;
462 * ksmbd_vfs_write() - vfs helper for smb file write
464 * @fid: file id of open file
465 * @buf: buf containing data for writing
466 * @count: read byte count
468 * @sync: fsync after write
469 * @written: number of bytes written
471 * Return: 0 on success, otherwise error
473 int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp,
474 char *buf, size_t count, loff_t *pos, bool sync,
478 loff_t offset = *pos;
481 if (work->conn->connection_type) {
482 if (!(fp->daccess & FILE_WRITE_DATA_LE)) {
483 pr_err("no right to write(%pD)\n", fp->filp);
491 if (ksmbd_stream_fd(fp)) {
492 err = ksmbd_vfs_stream_write(fp, buf, pos, count);
498 if (!work->tcon->posix_extensions) {
499 err = check_lock_range(filp, *pos, *pos + count - 1, WRITE);
501 pr_err("unable to write due to lock\n");
507 /* Do we need to break any of a levelII oplock? */
508 smb_break_all_levII_oplock(work, fp, 1);
510 err = kernel_write(filp, buf, count, pos);
512 ksmbd_debug(VFS, "smb write failed, err = %d\n", err);
520 err = vfs_fsync_range(filp, offset, offset + *written, 0);
522 pr_err("fsync failed for filename = %pD, err = %d\n",
531 * ksmbd_vfs_getattr() - vfs helper for smb getattr
533 * @fid: file id of open file
534 * @attrs: inode attributes
536 * Return: 0 on success, otherwise error
538 int ksmbd_vfs_getattr(const struct path *path, struct kstat *stat)
542 err = vfs_getattr(path, stat, STATX_BTIME, AT_STATX_SYNC_AS_STAT);
544 pr_err("getattr failed, err %d\n", err);
549 * ksmbd_vfs_fsync() - vfs helper for smb fsync
551 * @fid: file id of open file
553 * Return: 0 on success, otherwise error
555 int ksmbd_vfs_fsync(struct ksmbd_work *work, u64 fid, u64 p_id)
557 struct ksmbd_file *fp;
560 fp = ksmbd_lookup_fd_slow(work, fid, p_id);
562 pr_err("failed to get filp for fid %llu\n", fid);
565 err = vfs_fsync(fp->filp, 0);
567 pr_err("smb fsync failed, err = %d\n", err);
568 ksmbd_fd_put(work, fp);
573 * ksmbd_vfs_remove_file() - vfs helper for smb rmdir or unlink
574 * @name: directory or file name that is relative to share
576 * Return: 0 on success, otherwise error
578 int ksmbd_vfs_remove_file(struct ksmbd_work *work, const struct path *path)
580 struct mnt_idmap *idmap;
581 struct dentry *parent = path->dentry->d_parent;
584 if (ksmbd_override_fsids(work))
587 if (!d_inode(path->dentry)->i_nlink) {
592 idmap = mnt_idmap(path->mnt);
593 if (S_ISDIR(d_inode(path->dentry)->i_mode)) {
594 err = vfs_rmdir(idmap, d_inode(parent), path->dentry);
595 if (err && err != -ENOTEMPTY)
596 ksmbd_debug(VFS, "rmdir failed, err %d\n", err);
598 err = vfs_unlink(idmap, d_inode(parent), path->dentry, NULL);
600 ksmbd_debug(VFS, "unlink failed, err %d\n", err);
604 ksmbd_revert_fsids(work);
609 * ksmbd_vfs_link() - vfs helper for creating smb hardlink
610 * @oldname: source file name
611 * @newname: hardlink name that is relative to share
613 * Return: 0 on success, otherwise error
615 int ksmbd_vfs_link(struct ksmbd_work *work, const char *oldname,
618 struct path oldpath, newpath;
619 struct dentry *dentry;
622 if (ksmbd_override_fsids(work))
625 err = kern_path(oldname, LOOKUP_NO_SYMLINKS, &oldpath);
627 pr_err("cannot get linux path for %s, err = %d\n",
632 dentry = ksmbd_vfs_kern_path_create(work, newname,
633 LOOKUP_NO_SYMLINKS | LOOKUP_REVAL,
635 if (IS_ERR(dentry)) {
636 err = PTR_ERR(dentry);
637 pr_err("path create err for %s, err %d\n", newname, err);
642 if (oldpath.mnt != newpath.mnt) {
643 pr_err("vfs_link failed err %d\n", err);
647 err = vfs_link(oldpath.dentry, mnt_idmap(newpath.mnt),
648 d_inode(newpath.dentry),
651 ksmbd_debug(VFS, "vfs_link failed err %d\n", err);
654 done_path_create(&newpath, dentry);
658 ksmbd_revert_fsids(work);
662 int ksmbd_vfs_rename(struct ksmbd_work *work, const struct path *old_path,
663 char *newname, int flags)
665 struct dentry *old_parent, *new_dentry, *trap;
666 struct dentry *old_child = old_path->dentry;
667 struct path new_path;
668 struct qstr new_last;
669 struct renamedata rd;
671 struct ksmbd_share_config *share_conf = work->tcon->share_conf;
672 struct ksmbd_file *parent_fp;
674 int err, lookup_flags = LOOKUP_NO_SYMLINKS;
676 if (ksmbd_override_fsids(work))
679 to = getname_kernel(newname);
686 err = vfs_path_parent_lookup(to, lookup_flags | LOOKUP_BENEATH,
687 &new_path, &new_last, &new_type,
688 &share_conf->vfs_path);
692 if (old_path->mnt != new_path.mnt) {
697 trap = lock_rename_child(old_child, new_path.dentry);
699 old_parent = dget(old_child->d_parent);
700 if (d_unhashed(old_child)) {
705 parent_fp = ksmbd_lookup_fd_inode(d_inode(old_child->d_parent));
707 if (parent_fp->daccess & FILE_DELETE_LE) {
708 pr_err("parent dir is opened with delete access\n");
710 ksmbd_fd_put(work, parent_fp);
713 ksmbd_fd_put(work, parent_fp);
716 new_dentry = lookup_one_qstr_excl(&new_last, new_path.dentry,
717 lookup_flags | LOOKUP_RENAME_TARGET);
718 if (IS_ERR(new_dentry)) {
719 err = PTR_ERR(new_dentry);
723 if (d_is_symlink(new_dentry)) {
728 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) {
733 if (old_child == trap) {
738 if (new_dentry == trap) {
743 rd.old_mnt_idmap = mnt_idmap(old_path->mnt),
744 rd.old_dir = d_inode(old_parent),
745 rd.old_dentry = old_child,
746 rd.new_mnt_idmap = mnt_idmap(new_path.mnt),
747 rd.new_dir = new_path.dentry->d_inode,
748 rd.new_dentry = new_dentry,
750 rd.delegated_inode = NULL,
751 err = vfs_rename(&rd);
753 ksmbd_debug(VFS, "vfs_rename failed err %d\n", err);
759 unlock_rename(old_parent, new_path.dentry);
763 if (retry_estale(err, lookup_flags)) {
764 lookup_flags |= LOOKUP_REVAL;
770 ksmbd_revert_fsids(work);
775 * ksmbd_vfs_truncate() - vfs helper for smb file truncate
777 * @fid: file id of old file
778 * @size: truncate to given size
780 * Return: 0 on success, otherwise error
782 int ksmbd_vfs_truncate(struct ksmbd_work *work,
783 struct ksmbd_file *fp, loff_t size)
790 /* Do we need to break any of a levelII oplock? */
791 smb_break_all_levII_oplock(work, fp, 1);
793 if (!work->tcon->posix_extensions) {
794 struct inode *inode = file_inode(filp);
796 if (size < inode->i_size) {
797 err = check_lock_range(filp, size,
798 inode->i_size - 1, WRITE);
800 err = check_lock_range(filp, inode->i_size,
805 pr_err("failed due to lock\n");
810 err = vfs_truncate(&filp->f_path, size);
812 pr_err("truncate failed, err %d\n", err);
817 * ksmbd_vfs_listxattr() - vfs helper for smb list extended attributes
818 * @dentry: dentry of file for listing xattrs
819 * @list: destination buffer
820 * @size: destination buffer length
822 * Return: xattr list length on success, otherwise error
824 ssize_t ksmbd_vfs_listxattr(struct dentry *dentry, char **list)
829 size = vfs_listxattr(dentry, NULL, 0);
833 vlist = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
838 size = vfs_listxattr(dentry, vlist, size);
840 ksmbd_debug(VFS, "listxattr failed\n");
848 static ssize_t ksmbd_vfs_xattr_len(struct mnt_idmap *idmap,
849 struct dentry *dentry, char *xattr_name)
851 return vfs_getxattr(idmap, dentry, xattr_name, NULL, 0);
855 * ksmbd_vfs_getxattr() - vfs helper for smb get extended attributes value
857 * @dentry: dentry of file for getting xattrs
858 * @xattr_name: name of xattr name to query
859 * @xattr_buf: destination buffer xattr value
861 * Return: read xattr value length on success, otherwise error
863 ssize_t ksmbd_vfs_getxattr(struct mnt_idmap *idmap,
864 struct dentry *dentry,
865 char *xattr_name, char **xattr_buf)
871 xattr_len = ksmbd_vfs_xattr_len(idmap, dentry, xattr_name);
875 buf = kmalloc(xattr_len + 1, GFP_KERNEL);
879 xattr_len = vfs_getxattr(idmap, dentry, xattr_name,
880 (void *)buf, xattr_len);
889 * ksmbd_vfs_setxattr() - vfs helper for smb set extended attributes value
890 * @idmap: idmap of the relevant mount
891 * @dentry: dentry to set XATTR at
892 * @attr_name: xattr name for setxattr
893 * @attr_value: xattr value to set
894 * @attr_size: size of xattr value
895 * @flags: destination buffer length
897 * Return: 0 on success, otherwise error
899 int ksmbd_vfs_setxattr(struct mnt_idmap *idmap,
900 struct dentry *dentry, const char *attr_name,
901 void *attr_value, size_t attr_size, int flags)
905 err = vfs_setxattr(idmap,
912 ksmbd_debug(VFS, "setxattr failed, err %d\n", err);
917 * ksmbd_vfs_set_fadvise() - convert smb IO caching options to linux options
918 * @filp: file pointer for IO
919 * @options: smb IO options
921 void ksmbd_vfs_set_fadvise(struct file *filp, __le32 option)
923 struct address_space *mapping;
925 mapping = filp->f_mapping;
927 if (!option || !mapping)
930 if (option & FILE_WRITE_THROUGH_LE) {
931 filp->f_flags |= O_SYNC;
932 } else if (option & FILE_SEQUENTIAL_ONLY_LE) {
933 filp->f_ra.ra_pages = inode_to_bdi(mapping->host)->ra_pages * 2;
934 spin_lock(&filp->f_lock);
935 filp->f_mode &= ~FMODE_RANDOM;
936 spin_unlock(&filp->f_lock);
937 } else if (option & FILE_RANDOM_ACCESS_LE) {
938 spin_lock(&filp->f_lock);
939 filp->f_mode |= FMODE_RANDOM;
940 spin_unlock(&filp->f_lock);
944 int ksmbd_vfs_zero_data(struct ksmbd_work *work, struct ksmbd_file *fp,
945 loff_t off, loff_t len)
947 smb_break_all_levII_oplock(work, fp, 1);
948 if (fp->f_ci->m_fattr & FILE_ATTRIBUTE_SPARSE_FILE_LE)
949 return vfs_fallocate(fp->filp,
950 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
953 return vfs_fallocate(fp->filp,
954 FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE,
958 int ksmbd_vfs_fqar_lseek(struct ksmbd_file *fp, loff_t start, loff_t length,
959 struct file_allocated_range_buffer *ranges,
960 unsigned int in_count, unsigned int *out_count)
962 struct file *f = fp->filp;
963 struct inode *inode = file_inode(fp->filp);
964 loff_t maxbytes = (u64)inode->i_sb->s_maxbytes, end;
965 loff_t extent_start, extent_end;
968 if (start > maxbytes)
975 * Shrink request scope to what the fs can actually handle.
977 if (length > maxbytes || (maxbytes - length) < start)
978 length = maxbytes - start;
980 if (start + length > inode->i_size)
981 length = inode->i_size - start;
984 end = start + length;
985 while (start < end && *out_count < in_count) {
986 extent_start = vfs_llseek(f, start, SEEK_DATA);
987 if (extent_start < 0) {
988 if (extent_start != -ENXIO)
989 ret = (int)extent_start;
993 if (extent_start >= end)
996 extent_end = vfs_llseek(f, extent_start, SEEK_HOLE);
997 if (extent_end < 0) {
998 if (extent_end != -ENXIO)
999 ret = (int)extent_end;
1001 } else if (extent_start >= extent_end) {
1005 ranges[*out_count].file_offset = cpu_to_le64(extent_start);
1006 ranges[(*out_count)++].length =
1007 cpu_to_le64(min(extent_end, end) - extent_start);
1015 int ksmbd_vfs_remove_xattr(struct mnt_idmap *idmap,
1016 struct dentry *dentry, char *attr_name)
1018 return vfs_removexattr(idmap, dentry, attr_name);
1021 int ksmbd_vfs_unlink(struct file *filp)
1024 struct dentry *dir, *dentry = filp->f_path.dentry;
1025 struct mnt_idmap *idmap = file_mnt_idmap(filp);
1027 dir = dget_parent(dentry);
1028 err = ksmbd_vfs_lock_parent(dir, dentry);
1033 if (S_ISDIR(d_inode(dentry)->i_mode))
1034 err = vfs_rmdir(idmap, d_inode(dir), dentry);
1036 err = vfs_unlink(idmap, d_inode(dir), dentry, NULL);
1039 inode_unlock(d_inode(dir));
1041 ksmbd_debug(VFS, "failed to delete, err %d\n", err);
1048 static bool __dir_empty(struct dir_context *ctx, const char *name, int namlen,
1049 loff_t offset, u64 ino, unsigned int d_type)
1051 struct ksmbd_readdir_data *buf;
1053 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
1054 buf->dirent_count++;
1056 return buf->dirent_count <= 2;
1060 * ksmbd_vfs_empty_dir() - check for empty directory
1061 * @fp: ksmbd file pointer
1063 * Return: true if directory empty, otherwise false
1065 int ksmbd_vfs_empty_dir(struct ksmbd_file *fp)
1068 struct ksmbd_readdir_data readdir_data;
1070 memset(&readdir_data, 0, sizeof(struct ksmbd_readdir_data));
1072 set_ctx_actor(&readdir_data.ctx, __dir_empty);
1073 readdir_data.dirent_count = 0;
1075 err = iterate_dir(fp->filp, &readdir_data.ctx);
1076 if (readdir_data.dirent_count > 2)
1083 static bool __caseless_lookup(struct dir_context *ctx, const char *name,
1084 int namlen, loff_t offset, u64 ino,
1085 unsigned int d_type)
1087 struct ksmbd_readdir_data *buf;
1090 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
1092 if (buf->used != namlen)
1094 if (IS_ENABLED(CONFIG_UNICODE) && buf->um) {
1095 const struct qstr q_buf = {.name = buf->private,
1097 const struct qstr q_name = {.name = name,
1100 cmp = utf8_strncasecmp(buf->um, &q_buf, &q_name);
1103 cmp = strncasecmp((char *)buf->private, name, namlen);
1105 memcpy((char *)buf->private, name, namlen);
1106 buf->dirent_count = 1;
1113 * ksmbd_vfs_lookup_in_dir() - lookup a file in a directory
1115 * @name: filename to lookup
1116 * @namelen: filename length
1118 * Return: 0 on success, otherwise error
1120 static int ksmbd_vfs_lookup_in_dir(const struct path *dir, char *name,
1121 size_t namelen, struct unicode_map *um)
1125 int flags = O_RDONLY | O_LARGEFILE;
1126 struct ksmbd_readdir_data readdir_data = {
1127 .ctx.actor = __caseless_lookup,
1134 dfilp = dentry_open(dir, flags, current_cred());
1136 return PTR_ERR(dfilp);
1138 ret = iterate_dir(dfilp, &readdir_data.ctx);
1139 if (readdir_data.dirent_count > 0)
1146 * ksmbd_vfs_kern_path_locked() - lookup a file and get path info
1147 * @name: file path that is relative to share
1148 * @flags: lookup flags
1149 * @path: if lookup succeed, return path info
1150 * @caseless: caseless filename lookup
1152 * Return: 0 on success, otherwise error
1154 int ksmbd_vfs_kern_path_locked(struct ksmbd_work *work, char *name,
1155 unsigned int flags, struct path *path,
1158 struct ksmbd_share_config *share_conf = work->tcon->share_conf;
1160 struct path parent_path;
1162 err = ksmbd_vfs_path_lookup_locked(share_conf, name, flags, path);
1168 size_t path_len, remain_len;
1170 filepath = kstrdup(name, GFP_KERNEL);
1174 path_len = strlen(filepath);
1175 remain_len = path_len;
1177 parent_path = share_conf->vfs_path;
1178 path_get(&parent_path);
1180 while (d_can_lookup(parent_path.dentry)) {
1181 char *filename = filepath + path_len - remain_len;
1182 char *next = strchrnul(filename, '/');
1183 size_t filename_len = next - filename;
1184 bool is_last = !next[0];
1186 if (filename_len == 0)
1189 err = ksmbd_vfs_lookup_in_dir(&parent_path, filename,
1197 err = vfs_path_lookup(share_conf->vfs_path.dentry,
1198 share_conf->vfs_path.mnt,
1206 path_put(&parent_path);
1207 parent_path = *path;
1210 remain_len -= filename_len + 1;
1215 path_put(&parent_path);
1221 err = ksmbd_vfs_lock_parent(parent_path.dentry, path->dentry);
1224 path_put(&parent_path);
1229 struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work,
1235 struct dentry *dent;
1237 abs_name = convert_to_unix_name(work->tcon->share_conf, name);
1239 return ERR_PTR(-ENOMEM);
1241 dent = kern_path_create(AT_FDCWD, abs_name, path, flags);
1246 int ksmbd_vfs_remove_acl_xattrs(struct mnt_idmap *idmap,
1247 struct dentry *dentry)
1249 char *name, *xattr_list = NULL;
1250 ssize_t xattr_list_len;
1253 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
1254 if (xattr_list_len < 0) {
1256 } else if (!xattr_list_len) {
1257 ksmbd_debug(SMB, "empty xattr in the file\n");
1261 for (name = xattr_list; name - xattr_list < xattr_list_len;
1262 name += strlen(name) + 1) {
1263 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
1265 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
1266 sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1) ||
1267 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
1268 sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1)) {
1269 err = vfs_remove_acl(idmap, dentry, name);
1272 "remove acl xattr failed : %s\n", name);
1280 int ksmbd_vfs_remove_sd_xattrs(struct mnt_idmap *idmap,
1281 struct dentry *dentry)
1283 char *name, *xattr_list = NULL;
1284 ssize_t xattr_list_len;
1287 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
1288 if (xattr_list_len < 0) {
1290 } else if (!xattr_list_len) {
1291 ksmbd_debug(SMB, "empty xattr in the file\n");
1295 for (name = xattr_list; name - xattr_list < xattr_list_len;
1296 name += strlen(name) + 1) {
1297 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
1299 if (!strncmp(name, XATTR_NAME_SD, XATTR_NAME_SD_LEN)) {
1300 err = ksmbd_vfs_remove_xattr(idmap, dentry, name);
1302 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
1310 static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct mnt_idmap *idmap,
1311 struct inode *inode,
1314 struct xattr_smb_acl *smb_acl = NULL;
1315 struct posix_acl *posix_acls;
1316 struct posix_acl_entry *pa_entry;
1317 struct xattr_acl_entry *xa_entry;
1320 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1323 posix_acls = get_inode_acl(inode, acl_type);
1327 smb_acl = kzalloc(sizeof(struct xattr_smb_acl) +
1328 sizeof(struct xattr_acl_entry) * posix_acls->a_count,
1333 smb_acl->count = posix_acls->a_count;
1334 pa_entry = posix_acls->a_entries;
1335 xa_entry = smb_acl->entries;
1336 for (i = 0; i < posix_acls->a_count; i++, pa_entry++, xa_entry++) {
1337 switch (pa_entry->e_tag) {
1339 xa_entry->type = SMB_ACL_USER;
1340 xa_entry->uid = posix_acl_uid_translate(idmap, pa_entry);
1343 xa_entry->type = SMB_ACL_USER_OBJ;
1346 xa_entry->type = SMB_ACL_GROUP;
1347 xa_entry->gid = posix_acl_gid_translate(idmap, pa_entry);
1350 xa_entry->type = SMB_ACL_GROUP_OBJ;
1353 xa_entry->type = SMB_ACL_OTHER;
1356 xa_entry->type = SMB_ACL_MASK;
1359 pr_err("unknown type : 0x%x\n", pa_entry->e_tag);
1363 if (pa_entry->e_perm & ACL_READ)
1364 xa_entry->perm |= SMB_ACL_READ;
1365 if (pa_entry->e_perm & ACL_WRITE)
1366 xa_entry->perm |= SMB_ACL_WRITE;
1367 if (pa_entry->e_perm & ACL_EXECUTE)
1368 xa_entry->perm |= SMB_ACL_EXECUTE;
1371 posix_acl_release(posix_acls);
1375 int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
1376 struct mnt_idmap *idmap,
1377 struct dentry *dentry,
1378 struct smb_ntsd *pntsd, int len)
1381 struct ndr sd_ndr = {0}, acl_ndr = {0};
1382 struct xattr_ntacl acl = {0};
1383 struct xattr_smb_acl *smb_acl, *def_smb_acl = NULL;
1384 struct inode *inode = d_inode(dentry);
1387 acl.hash_type = XATTR_SD_HASH_TYPE_SHA256;
1388 acl.current_time = ksmbd_UnixTimeToNT(current_time(inode));
1390 memcpy(acl.desc, "posix_acl", 9);
1394 cpu_to_le32(le32_to_cpu(pntsd->osidoffset) + NDR_NTSD_OFFSETOF);
1396 cpu_to_le32(le32_to_cpu(pntsd->gsidoffset) + NDR_NTSD_OFFSETOF);
1398 cpu_to_le32(le32_to_cpu(pntsd->dacloffset) + NDR_NTSD_OFFSETOF);
1400 acl.sd_buf = (char *)pntsd;
1403 rc = ksmbd_gen_sd_hash(conn, acl.sd_buf, acl.sd_size, acl.hash);
1405 pr_err("failed to generate hash for ndr acl\n");
1409 smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode,
1411 if (S_ISDIR(inode->i_mode))
1412 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode,
1415 rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode,
1416 smb_acl, def_smb_acl);
1418 pr_err("failed to encode ndr to posix acl\n");
1422 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset,
1423 acl.posix_acl_hash);
1425 pr_err("failed to generate hash for ndr acl\n");
1429 rc = ndr_encode_v4_ntacl(&sd_ndr, &acl);
1431 pr_err("failed to encode ndr to posix acl\n");
1435 rc = ksmbd_vfs_setxattr(idmap, dentry,
1436 XATTR_NAME_SD, sd_ndr.data,
1439 pr_err("Failed to store XATTR ntacl :%d\n", rc);
1443 kfree(acl_ndr.data);
1449 int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
1450 struct mnt_idmap *idmap,
1451 struct dentry *dentry,
1452 struct smb_ntsd **pntsd)
1456 struct inode *inode = d_inode(dentry);
1457 struct ndr acl_ndr = {0};
1458 struct xattr_ntacl acl;
1459 struct xattr_smb_acl *smb_acl = NULL, *def_smb_acl = NULL;
1460 __u8 cmp_hash[XATTR_SD_HASH_SIZE] = {0};
1462 rc = ksmbd_vfs_getxattr(idmap, dentry, XATTR_NAME_SD, &n.data);
1467 rc = ndr_decode_v4_ntacl(&n, &acl);
1471 smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode,
1473 if (S_ISDIR(inode->i_mode))
1474 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode,
1477 rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode, smb_acl,
1480 pr_err("failed to encode ndr to posix acl\n");
1484 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset, cmp_hash);
1486 pr_err("failed to generate hash for ndr acl\n");
1490 if (memcmp(cmp_hash, acl.posix_acl_hash, XATTR_SD_HASH_SIZE)) {
1491 pr_err("hash value diff\n");
1496 *pntsd = acl.sd_buf;
1497 if (acl.sd_size < sizeof(struct smb_ntsd)) {
1498 pr_err("sd size is invalid\n");
1502 (*pntsd)->osidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->osidoffset) -
1504 (*pntsd)->gsidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->gsidoffset) -
1506 (*pntsd)->dacloffset = cpu_to_le32(le32_to_cpu((*pntsd)->dacloffset) -
1511 kfree(acl_ndr.data);
1524 int ksmbd_vfs_set_dos_attrib_xattr(struct mnt_idmap *idmap,
1525 struct dentry *dentry,
1526 struct xattr_dos_attrib *da)
1531 err = ndr_encode_dos_attr(&n, da);
1535 err = ksmbd_vfs_setxattr(idmap, dentry, XATTR_NAME_DOS_ATTRIBUTE,
1536 (void *)n.data, n.offset, 0);
1538 ksmbd_debug(SMB, "failed to store dos attribute in xattr\n");
1544 int ksmbd_vfs_get_dos_attrib_xattr(struct mnt_idmap *idmap,
1545 struct dentry *dentry,
1546 struct xattr_dos_attrib *da)
1551 err = ksmbd_vfs_getxattr(idmap, dentry, XATTR_NAME_DOS_ATTRIBUTE,
1555 if (ndr_decode_dos_attr(&n, da))
1559 ksmbd_debug(SMB, "failed to load dos attribute in xattr\n");
1566 * ksmbd_vfs_init_kstat() - convert unix stat information to smb stat format
1567 * @p: destination buffer
1568 * @ksmbd_kstat: ksmbd kstat wrapper
1570 void *ksmbd_vfs_init_kstat(char **p, struct ksmbd_kstat *ksmbd_kstat)
1572 struct file_directory_info *info = (struct file_directory_info *)(*p);
1573 struct kstat *kstat = ksmbd_kstat->kstat;
1576 info->FileIndex = 0;
1577 info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
1578 time = ksmbd_UnixTimeToNT(kstat->atime);
1579 info->LastAccessTime = cpu_to_le64(time);
1580 time = ksmbd_UnixTimeToNT(kstat->mtime);
1581 info->LastWriteTime = cpu_to_le64(time);
1582 time = ksmbd_UnixTimeToNT(kstat->ctime);
1583 info->ChangeTime = cpu_to_le64(time);
1585 if (ksmbd_kstat->file_attributes & FILE_ATTRIBUTE_DIRECTORY_LE) {
1586 info->EndOfFile = 0;
1587 info->AllocationSize = 0;
1589 info->EndOfFile = cpu_to_le64(kstat->size);
1590 info->AllocationSize = cpu_to_le64(kstat->blocks << 9);
1592 info->ExtFileAttributes = ksmbd_kstat->file_attributes;
1597 int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work,
1598 struct mnt_idmap *idmap,
1599 struct dentry *dentry,
1600 struct ksmbd_kstat *ksmbd_kstat)
1605 generic_fillattr(idmap, d_inode(dentry), ksmbd_kstat->kstat);
1607 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
1608 ksmbd_kstat->create_time = time;
1611 * set default value for the case that store dos attributes is not yes
1612 * or that acl is disable in server's filesystem and the config is yes.
1614 if (S_ISDIR(ksmbd_kstat->kstat->mode))
1615 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_DIRECTORY_LE;
1617 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_ARCHIVE_LE;
1619 if (test_share_config_flag(work->tcon->share_conf,
1620 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
1621 struct xattr_dos_attrib da;
1623 rc = ksmbd_vfs_get_dos_attrib_xattr(idmap, dentry, &da);
1625 ksmbd_kstat->file_attributes = cpu_to_le32(da.attr);
1626 ksmbd_kstat->create_time = da.create_time;
1628 ksmbd_debug(VFS, "fail to load dos attribute.\n");
1635 ssize_t ksmbd_vfs_casexattr_len(struct mnt_idmap *idmap,
1636 struct dentry *dentry, char *attr_name,
1639 char *name, *xattr_list = NULL;
1640 ssize_t value_len = -ENOENT, xattr_list_len;
1642 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
1643 if (xattr_list_len <= 0)
1646 for (name = xattr_list; name - xattr_list < xattr_list_len;
1647 name += strlen(name) + 1) {
1648 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
1649 if (strncasecmp(attr_name, name, attr_name_len))
1652 value_len = ksmbd_vfs_xattr_len(idmap, dentry, name);
1661 int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name,
1662 size_t *xattr_stream_name_size, int s_type)
1666 if (s_type == DIR_STREAM)
1667 type = ":$INDEX_ALLOCATION";
1671 buf = kasprintf(GFP_KERNEL, "%s%s%s",
1672 XATTR_NAME_STREAM, stream_name, type);
1676 *xattr_stream_name = buf;
1677 *xattr_stream_name_size = strlen(buf) + 1;
1682 int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work,
1683 struct ksmbd_file *src_fp,
1684 struct ksmbd_file *dst_fp,
1685 struct srv_copychunk *chunks,
1686 unsigned int chunk_count,
1687 unsigned int *chunk_count_written,
1688 unsigned int *chunk_size_written,
1689 loff_t *total_size_written)
1692 loff_t src_off, dst_off, src_file_size;
1696 *chunk_count_written = 0;
1697 *chunk_size_written = 0;
1698 *total_size_written = 0;
1700 if (!(src_fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
1701 pr_err("no right to read(%pD)\n", src_fp->filp);
1704 if (!(dst_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) {
1705 pr_err("no right to write(%pD)\n", dst_fp->filp);
1709 if (ksmbd_stream_fd(src_fp) || ksmbd_stream_fd(dst_fp))
1712 smb_break_all_levII_oplock(work, dst_fp, 1);
1714 if (!work->tcon->posix_extensions) {
1715 for (i = 0; i < chunk_count; i++) {
1716 src_off = le64_to_cpu(chunks[i].SourceOffset);
1717 dst_off = le64_to_cpu(chunks[i].TargetOffset);
1718 len = le32_to_cpu(chunks[i].Length);
1720 if (check_lock_range(src_fp->filp, src_off,
1721 src_off + len - 1, READ))
1723 if (check_lock_range(dst_fp->filp, dst_off,
1724 dst_off + len - 1, WRITE))
1729 src_file_size = i_size_read(file_inode(src_fp->filp));
1731 for (i = 0; i < chunk_count; i++) {
1732 src_off = le64_to_cpu(chunks[i].SourceOffset);
1733 dst_off = le64_to_cpu(chunks[i].TargetOffset);
1734 len = le32_to_cpu(chunks[i].Length);
1736 if (src_off + len > src_file_size)
1739 ret = vfs_copy_file_range(src_fp->filp, src_off,
1740 dst_fp->filp, dst_off, len, 0);
1741 if (ret == -EOPNOTSUPP || ret == -EXDEV)
1742 ret = vfs_copy_file_range(src_fp->filp, src_off,
1743 dst_fp->filp, dst_off, len,
1748 *chunk_count_written += 1;
1749 *total_size_written += ret;
1754 void ksmbd_vfs_posix_lock_wait(struct file_lock *flock)
1756 wait_event(flock->fl_wait, !flock->fl_blocker);
1759 int ksmbd_vfs_posix_lock_wait_timeout(struct file_lock *flock, long timeout)
1761 return wait_event_interruptible_timeout(flock->fl_wait,
1766 void ksmbd_vfs_posix_lock_unblock(struct file_lock *flock)
1768 locks_delete_block(flock);
1771 int ksmbd_vfs_set_init_posix_acl(struct mnt_idmap *idmap,
1772 struct dentry *dentry)
1774 struct posix_acl_state acl_state;
1775 struct posix_acl *acls;
1776 struct inode *inode = d_inode(dentry);
1779 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1782 ksmbd_debug(SMB, "Set posix acls\n");
1783 rc = init_acl_state(&acl_state, 1);
1787 /* Set default owner group */
1788 acl_state.owner.allow = (inode->i_mode & 0700) >> 6;
1789 acl_state.group.allow = (inode->i_mode & 0070) >> 3;
1790 acl_state.other.allow = inode->i_mode & 0007;
1791 acl_state.users->aces[acl_state.users->n].uid = inode->i_uid;
1792 acl_state.users->aces[acl_state.users->n++].perms.allow =
1793 acl_state.owner.allow;
1794 acl_state.groups->aces[acl_state.groups->n].gid = inode->i_gid;
1795 acl_state.groups->aces[acl_state.groups->n++].perms.allow =
1796 acl_state.group.allow;
1797 acl_state.mask.allow = 0x07;
1799 acls = posix_acl_alloc(6, GFP_KERNEL);
1801 free_acl_state(&acl_state);
1804 posix_state_to_acl(&acl_state, acls->a_entries);
1805 rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls);
1807 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1809 else if (S_ISDIR(inode->i_mode)) {
1810 posix_state_to_acl(&acl_state, acls->a_entries);
1811 rc = set_posix_acl(idmap, dentry, ACL_TYPE_DEFAULT, acls);
1813 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1816 free_acl_state(&acl_state);
1817 posix_acl_release(acls);
1821 int ksmbd_vfs_inherit_posix_acl(struct mnt_idmap *idmap,
1822 struct dentry *dentry, struct inode *parent_inode)
1824 struct posix_acl *acls;
1825 struct posix_acl_entry *pace;
1826 struct inode *inode = d_inode(dentry);
1829 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1832 acls = get_inode_acl(parent_inode, ACL_TYPE_DEFAULT);
1835 pace = acls->a_entries;
1837 for (i = 0; i < acls->a_count; i++, pace++) {
1838 if (pace->e_tag == ACL_MASK) {
1839 pace->e_perm = 0x07;
1844 rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls);
1846 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1848 if (S_ISDIR(inode->i_mode)) {
1849 rc = set_posix_acl(idmap, dentry, ACL_TYPE_DEFAULT,
1852 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1855 posix_acl_release(acls);