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/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
52 * @parent: parent dentry
53 * @child: child dentry
55 * Returns: %0 on success, %-ENOENT if the parent dentry is not stable
57 int ksmbd_vfs_lock_parent(struct dentry *parent, struct dentry *child)
59 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
60 if (child->d_parent != parent) {
61 inode_unlock(d_inode(parent));
68 static int ksmbd_vfs_path_lookup_locked(struct ksmbd_share_config *share_conf,
69 char *pathname, unsigned int flags,
70 struct path *parent_path,
74 struct filename *filename;
75 struct path *root_share_path = &share_conf->vfs_path;
79 if (pathname[0] == '\0') {
80 pathname = share_conf->path;
81 root_share_path = NULL;
83 flags |= LOOKUP_BENEATH;
86 filename = getname_kernel(pathname);
88 return PTR_ERR(filename);
90 err = vfs_path_parent_lookup(filename, flags,
91 parent_path, &last, &type,
98 if (unlikely(type != LAST_NORM)) {
99 path_put(parent_path);
104 err = mnt_want_write(parent_path->mnt);
106 path_put(parent_path);
111 inode_lock_nested(parent_path->dentry->d_inode, I_MUTEX_PARENT);
112 d = lookup_one_qstr_excl(&last, parent_path->dentry, 0);
116 if (d_is_negative(d)) {
122 path->mnt = mntget(parent_path->mnt);
124 if (test_share_config_flag(share_conf, KSMBD_SHARE_FLAG_CROSSMNT)) {
125 err = follow_down(path, 0);
136 inode_unlock(d_inode(parent_path->dentry));
137 mnt_drop_write(parent_path->mnt);
138 path_put(parent_path);
143 void ksmbd_vfs_query_maximal_access(struct mnt_idmap *idmap,
144 struct dentry *dentry, __le32 *daccess)
146 *daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL);
148 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_WRITE))
149 *daccess |= cpu_to_le32(WRITE_DAC | WRITE_OWNER | SYNCHRONIZE |
150 FILE_WRITE_DATA | FILE_APPEND_DATA |
151 FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES |
154 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_READ))
155 *daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE;
157 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_EXEC))
158 *daccess |= FILE_EXECUTE_LE;
160 if (!inode_permission(idmap, d_inode(dentry->d_parent), MAY_EXEC | MAY_WRITE))
161 *daccess |= FILE_DELETE_LE;
165 * ksmbd_vfs_create() - vfs helper for smb create file
167 * @name: file name that is relative to share
168 * @mode: file create mode
170 * Return: 0 on success, otherwise error
172 int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode)
175 struct dentry *dentry;
178 dentry = ksmbd_vfs_kern_path_create(work, name,
179 LOOKUP_NO_SYMLINKS, &path);
180 if (IS_ERR(dentry)) {
181 err = PTR_ERR(dentry);
183 pr_err("path create failed for %s, err %d\n",
189 err = vfs_create(mnt_idmap(path.mnt), d_inode(path.dentry),
192 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry),
195 pr_err("File(%s): creation failed (err:%d)\n", name, err);
198 done_path_create(&path, dentry);
203 * ksmbd_vfs_mkdir() - vfs helper for smb create directory
205 * @name: directory name that is relative to share
206 * @mode: directory create mode
208 * Return: 0 on success, otherwise error
210 int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode)
212 struct mnt_idmap *idmap;
214 struct dentry *dentry;
217 dentry = ksmbd_vfs_kern_path_create(work, name,
218 LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY,
220 if (IS_ERR(dentry)) {
221 err = PTR_ERR(dentry);
223 ksmbd_debug(VFS, "path create failed for %s, err %d\n",
228 idmap = mnt_idmap(path.mnt);
230 err = vfs_mkdir(idmap, d_inode(path.dentry), dentry, mode);
231 if (!err && d_unhashed(dentry)) {
234 d = lookup_one(idmap, dentry->d_name.name, dentry->d_parent,
240 if (unlikely(d_is_negative(d))) {
246 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), d_inode(d));
251 done_path_create(&path, dentry);
253 pr_err("mkdir(%s): creation failed (err:%d)\n", name, err);
257 static ssize_t ksmbd_vfs_getcasexattr(struct mnt_idmap *idmap,
258 struct dentry *dentry, char *attr_name,
259 int attr_name_len, char **attr_value)
261 char *name, *xattr_list = NULL;
262 ssize_t value_len = -ENOENT, xattr_list_len;
264 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
265 if (xattr_list_len <= 0)
268 for (name = xattr_list; name - xattr_list < xattr_list_len;
269 name += strlen(name) + 1) {
270 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
271 if (strncasecmp(attr_name, name, attr_name_len))
274 value_len = ksmbd_vfs_getxattr(idmap,
279 pr_err("failed to get xattr in file\n");
288 static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos,
292 char *stream_buf = NULL;
294 ksmbd_debug(VFS, "read stream data pos : %llu, count : %zd\n",
297 v_len = ksmbd_vfs_getcasexattr(file_mnt_idmap(fp->filp),
298 fp->filp->f_path.dentry,
310 if (v_len - *pos < count)
311 count = v_len - *pos;
313 memcpy(buf, &stream_buf[*pos], count);
321 * check_lock_range() - vfs helper for smb byte range file locking
322 * @filp: the file to apply the lock to
323 * @start: lock start byte offset
324 * @end: lock end byte offset
325 * @type: byte range type read/write
327 * Return: 0 on success, otherwise error
329 static int check_lock_range(struct file *filp, loff_t start, loff_t end,
332 struct file_lock *flock;
333 struct file_lock_context *ctx = locks_inode_context(file_inode(filp));
336 if (!ctx || list_empty_careful(&ctx->flc_posix))
339 spin_lock(&ctx->flc_lock);
340 for_each_file_lock(flock, &ctx->flc_posix) {
341 /* check conflict locks */
342 if (flock->fl_end >= start && end >= flock->fl_start) {
343 if (lock_is_read(flock)) {
345 pr_err("not allow write by shared lock\n");
349 } else if (lock_is_write(flock)) {
350 /* check owner in lock */
351 if (flock->c.flc_file != filp) {
353 pr_err("not allow rw access by exclusive lock from other opens\n");
360 spin_unlock(&ctx->flc_lock);
365 * ksmbd_vfs_read() - vfs helper for smb file read
367 * @fp: ksmbd file pointer
368 * @count: read byte count
370 * @rbuf: read data buffer
372 * Return: number of read bytes on success, otherwise error
374 int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count,
375 loff_t *pos, char *rbuf)
377 struct file *filp = fp->filp;
379 struct inode *inode = file_inode(filp);
381 if (S_ISDIR(inode->i_mode))
384 if (unlikely(count == 0))
387 if (work->conn->connection_type) {
388 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
389 pr_err("no right to read(%pD)\n", fp->filp);
394 if (ksmbd_stream_fd(fp))
395 return ksmbd_vfs_stream_read(fp, rbuf, pos, count);
397 if (!work->tcon->posix_extensions) {
400 ret = check_lock_range(filp, *pos, *pos + count - 1, READ);
402 pr_err("unable to read due to lock\n");
407 nbytes = kernel_read(filp, rbuf, count, pos);
409 pr_err("smb read failed, err = %zd\n", nbytes);
417 static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
420 char *stream_buf = NULL, *wbuf;
421 struct mnt_idmap *idmap = file_mnt_idmap(fp->filp);
426 ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n",
430 if (size > XATTR_SIZE_MAX) {
431 size = XATTR_SIZE_MAX;
432 count = (*pos + count) - XATTR_SIZE_MAX;
435 v_len = ksmbd_vfs_getcasexattr(idmap,
436 fp->filp->f_path.dentry,
441 pr_err("not found stream in xattr : %zd\n", v_len);
447 wbuf = kvzalloc(size, KSMBD_DEFAULT_GFP);
454 memcpy(wbuf, stream_buf, v_len);
459 memcpy(&stream_buf[*pos], buf, count);
461 err = ksmbd_vfs_setxattr(idmap,
471 fp->filp->f_pos = *pos;
479 * ksmbd_vfs_write() - vfs helper for smb file write
481 * @fp: ksmbd file pointer
482 * @buf: buf containing data for writing
483 * @count: read byte count
485 * @sync: fsync after write
486 * @written: number of bytes written
488 * Return: 0 on success, otherwise error
490 int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp,
491 char *buf, size_t count, loff_t *pos, bool sync,
495 loff_t offset = *pos;
498 if (work->conn->connection_type) {
499 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) {
500 pr_err("no right to write(%pD)\n", fp->filp);
508 if (ksmbd_stream_fd(fp)) {
509 err = ksmbd_vfs_stream_write(fp, buf, pos, count);
515 if (!work->tcon->posix_extensions) {
516 err = check_lock_range(filp, *pos, *pos + count - 1, WRITE);
518 pr_err("unable to write due to lock\n");
524 /* Reserve lease break for parent dir at closing time */
525 fp->reserve_lease_break = true;
527 /* Do we need to break any of a levelII oplock? */
528 smb_break_all_levII_oplock(work, fp, 1);
530 err = kernel_write(filp, buf, count, pos);
532 ksmbd_debug(VFS, "smb write failed, err = %d\n", err);
540 err = vfs_fsync_range(filp, offset, offset + *written, 0);
542 pr_err("fsync failed for filename = %pD, err = %d\n",
551 * ksmbd_vfs_getattr() - vfs helper for smb getattr
552 * @path: path of dentry
553 * @stat: pointer to returned kernel stat structure
554 * Return: 0 on success, otherwise error
556 int ksmbd_vfs_getattr(const struct path *path, struct kstat *stat)
560 err = vfs_getattr(path, stat, STATX_BTIME, AT_STATX_SYNC_AS_STAT);
562 pr_err("getattr failed, err %d\n", err);
567 * ksmbd_vfs_fsync() - vfs helper for smb fsync
569 * @fid: file id of open file
570 * @p_id: persistent file id
572 * Return: 0 on success, otherwise error
574 int ksmbd_vfs_fsync(struct ksmbd_work *work, u64 fid, u64 p_id)
576 struct ksmbd_file *fp;
579 fp = ksmbd_lookup_fd_slow(work, fid, p_id);
581 pr_err("failed to get filp for fid %llu\n", fid);
584 err = vfs_fsync(fp->filp, 0);
586 pr_err("smb fsync failed, err = %d\n", err);
587 ksmbd_fd_put(work, fp);
592 * ksmbd_vfs_remove_file() - vfs helper for smb rmdir or unlink
594 * @path: path of dentry
596 * Return: 0 on success, otherwise error
598 int ksmbd_vfs_remove_file(struct ksmbd_work *work, const struct path *path)
600 struct mnt_idmap *idmap;
601 struct dentry *parent = path->dentry->d_parent;
604 if (ksmbd_override_fsids(work))
607 if (!d_inode(path->dentry)->i_nlink) {
612 idmap = mnt_idmap(path->mnt);
613 if (S_ISDIR(d_inode(path->dentry)->i_mode)) {
614 err = vfs_rmdir(idmap, d_inode(parent), path->dentry);
615 if (err && err != -ENOTEMPTY)
616 ksmbd_debug(VFS, "rmdir failed, err %d\n", err);
618 err = vfs_unlink(idmap, d_inode(parent), path->dentry, NULL);
620 ksmbd_debug(VFS, "unlink failed, err %d\n", err);
624 ksmbd_revert_fsids(work);
629 * ksmbd_vfs_link() - vfs helper for creating smb hardlink
631 * @oldname: source file name
632 * @newname: hardlink name that is relative to share
634 * Return: 0 on success, otherwise error
636 int ksmbd_vfs_link(struct ksmbd_work *work, const char *oldname,
639 struct path oldpath, newpath;
640 struct dentry *dentry;
643 if (ksmbd_override_fsids(work))
646 err = kern_path(oldname, LOOKUP_NO_SYMLINKS, &oldpath);
648 pr_err("cannot get linux path for %s, err = %d\n",
653 dentry = ksmbd_vfs_kern_path_create(work, newname,
654 LOOKUP_NO_SYMLINKS | LOOKUP_REVAL,
656 if (IS_ERR(dentry)) {
657 err = PTR_ERR(dentry);
658 pr_err("path create err for %s, err %d\n", newname, err);
663 if (oldpath.mnt != newpath.mnt) {
664 pr_err("vfs_link failed err %d\n", err);
668 err = vfs_link(oldpath.dentry, mnt_idmap(newpath.mnt),
669 d_inode(newpath.dentry),
672 ksmbd_debug(VFS, "vfs_link failed err %d\n", err);
675 done_path_create(&newpath, dentry);
679 ksmbd_revert_fsids(work);
683 int ksmbd_vfs_rename(struct ksmbd_work *work, const struct path *old_path,
684 char *newname, int flags)
686 struct dentry *old_parent, *new_dentry, *trap;
687 struct dentry *old_child = old_path->dentry;
688 struct path new_path;
689 struct qstr new_last;
690 struct renamedata rd;
692 struct ksmbd_share_config *share_conf = work->tcon->share_conf;
693 struct ksmbd_file *parent_fp;
695 int err, lookup_flags = LOOKUP_NO_SYMLINKS;
697 if (ksmbd_override_fsids(work))
700 to = getname_kernel(newname);
707 err = vfs_path_parent_lookup(to, lookup_flags | LOOKUP_BENEATH,
708 &new_path, &new_last, &new_type,
709 &share_conf->vfs_path);
713 if (old_path->mnt != new_path.mnt) {
718 err = mnt_want_write(old_path->mnt);
722 trap = lock_rename_child(old_child, new_path.dentry);
728 old_parent = dget(old_child->d_parent);
729 if (d_unhashed(old_child)) {
734 parent_fp = ksmbd_lookup_fd_inode(old_child->d_parent);
736 if (parent_fp->daccess & FILE_DELETE_LE) {
737 pr_err("parent dir is opened with delete access\n");
739 ksmbd_fd_put(work, parent_fp);
742 ksmbd_fd_put(work, parent_fp);
745 new_dentry = lookup_one_qstr_excl(&new_last, new_path.dentry,
746 lookup_flags | LOOKUP_RENAME_TARGET);
747 if (IS_ERR(new_dentry)) {
748 err = PTR_ERR(new_dentry);
752 if (d_is_symlink(new_dentry)) {
758 * explicitly handle file overwrite case, for compatibility with
759 * filesystems that may not support rename flags (e.g: fuse)
761 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) {
765 flags &= ~(RENAME_NOREPLACE);
767 if (old_child == trap) {
772 if (new_dentry == trap) {
777 rd.old_mnt_idmap = mnt_idmap(old_path->mnt),
778 rd.old_dir = d_inode(old_parent),
779 rd.old_dentry = old_child,
780 rd.new_mnt_idmap = mnt_idmap(new_path.mnt),
781 rd.new_dir = new_path.dentry->d_inode,
782 rd.new_dentry = new_dentry,
784 rd.delegated_inode = NULL,
785 err = vfs_rename(&rd);
787 ksmbd_debug(VFS, "vfs_rename failed err %d\n", err);
793 unlock_rename(old_parent, new_path.dentry);
795 mnt_drop_write(old_path->mnt);
799 if (retry_estale(err, lookup_flags)) {
800 lookup_flags |= LOOKUP_REVAL;
806 ksmbd_revert_fsids(work);
811 * ksmbd_vfs_truncate() - vfs helper for smb file truncate
813 * @fp: ksmbd file pointer
814 * @size: truncate to given size
816 * Return: 0 on success, otherwise error
818 int ksmbd_vfs_truncate(struct ksmbd_work *work,
819 struct ksmbd_file *fp, loff_t size)
826 /* Do we need to break any of a levelII oplock? */
827 smb_break_all_levII_oplock(work, fp, 1);
829 if (!work->tcon->posix_extensions) {
830 struct inode *inode = file_inode(filp);
832 if (size < inode->i_size) {
833 err = check_lock_range(filp, size,
834 inode->i_size - 1, WRITE);
836 err = check_lock_range(filp, inode->i_size,
841 pr_err("failed due to lock\n");
846 err = vfs_truncate(&filp->f_path, size);
848 pr_err("truncate failed, err %d\n", err);
853 * ksmbd_vfs_listxattr() - vfs helper for smb list extended attributes
854 * @dentry: dentry of file for listing xattrs
855 * @list: destination buffer
857 * Return: xattr list length on success, otherwise error
859 ssize_t ksmbd_vfs_listxattr(struct dentry *dentry, char **list)
864 size = vfs_listxattr(dentry, NULL, 0);
868 vlist = kvzalloc(size, KSMBD_DEFAULT_GFP);
873 size = vfs_listxattr(dentry, vlist, size);
875 ksmbd_debug(VFS, "listxattr failed\n");
883 static ssize_t ksmbd_vfs_xattr_len(struct mnt_idmap *idmap,
884 struct dentry *dentry, char *xattr_name)
886 return vfs_getxattr(idmap, dentry, xattr_name, NULL, 0);
890 * ksmbd_vfs_getxattr() - vfs helper for smb get extended attributes value
892 * @dentry: dentry of file for getting xattrs
893 * @xattr_name: name of xattr name to query
894 * @xattr_buf: destination buffer xattr value
896 * Return: read xattr value length on success, otherwise error
898 ssize_t ksmbd_vfs_getxattr(struct mnt_idmap *idmap,
899 struct dentry *dentry,
900 char *xattr_name, char **xattr_buf)
906 xattr_len = ksmbd_vfs_xattr_len(idmap, dentry, xattr_name);
910 buf = kmalloc(xattr_len + 1, KSMBD_DEFAULT_GFP);
914 xattr_len = vfs_getxattr(idmap, dentry, xattr_name,
915 (void *)buf, xattr_len);
924 * ksmbd_vfs_setxattr() - vfs helper for smb set extended attributes value
925 * @idmap: idmap of the relevant mount
926 * @path: path of dentry to set XATTR at
927 * @attr_name: xattr name for setxattr
928 * @attr_value: xattr value to set
929 * @attr_size: size of xattr value
930 * @flags: destination buffer length
931 * @get_write: get write access to a mount
933 * Return: 0 on success, otherwise error
935 int ksmbd_vfs_setxattr(struct mnt_idmap *idmap,
936 const struct path *path, const char *attr_name,
937 void *attr_value, size_t attr_size, int flags,
942 if (get_write == true) {
943 err = mnt_want_write(path->mnt);
948 err = vfs_setxattr(idmap,
955 ksmbd_debug(VFS, "setxattr failed, err %d\n", err);
956 if (get_write == true)
957 mnt_drop_write(path->mnt);
962 * ksmbd_vfs_set_fadvise() - convert smb IO caching options to linux options
963 * @filp: file pointer for IO
964 * @option: smb IO options
966 void ksmbd_vfs_set_fadvise(struct file *filp, __le32 option)
968 struct address_space *mapping;
970 mapping = filp->f_mapping;
972 if (!option || !mapping)
975 if (option & FILE_WRITE_THROUGH_LE) {
976 filp->f_flags |= O_SYNC;
977 } else if (option & FILE_SEQUENTIAL_ONLY_LE) {
978 filp->f_ra.ra_pages = inode_to_bdi(mapping->host)->ra_pages * 2;
979 spin_lock(&filp->f_lock);
980 filp->f_mode &= ~FMODE_RANDOM;
981 spin_unlock(&filp->f_lock);
982 } else if (option & FILE_RANDOM_ACCESS_LE) {
983 spin_lock(&filp->f_lock);
984 filp->f_mode |= FMODE_RANDOM;
985 spin_unlock(&filp->f_lock);
989 int ksmbd_vfs_zero_data(struct ksmbd_work *work, struct ksmbd_file *fp,
990 loff_t off, loff_t len)
992 smb_break_all_levII_oplock(work, fp, 1);
993 if (fp->f_ci->m_fattr & FILE_ATTRIBUTE_SPARSE_FILE_LE)
994 return vfs_fallocate(fp->filp,
995 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
998 return vfs_fallocate(fp->filp,
999 FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE,
1003 int ksmbd_vfs_fqar_lseek(struct ksmbd_file *fp, loff_t start, loff_t length,
1004 struct file_allocated_range_buffer *ranges,
1005 unsigned int in_count, unsigned int *out_count)
1007 struct file *f = fp->filp;
1008 struct inode *inode = file_inode(fp->filp);
1009 loff_t maxbytes = (u64)inode->i_sb->s_maxbytes, end;
1010 loff_t extent_start, extent_end;
1013 if (start > maxbytes)
1020 * Shrink request scope to what the fs can actually handle.
1022 if (length > maxbytes || (maxbytes - length) < start)
1023 length = maxbytes - start;
1025 if (start + length > inode->i_size)
1026 length = inode->i_size - start;
1029 end = start + length;
1030 while (start < end && *out_count < in_count) {
1031 extent_start = vfs_llseek(f, start, SEEK_DATA);
1032 if (extent_start < 0) {
1033 if (extent_start != -ENXIO)
1034 ret = (int)extent_start;
1038 if (extent_start >= end)
1041 extent_end = vfs_llseek(f, extent_start, SEEK_HOLE);
1042 if (extent_end < 0) {
1043 if (extent_end != -ENXIO)
1044 ret = (int)extent_end;
1046 } else if (extent_start >= extent_end) {
1050 ranges[*out_count].file_offset = cpu_to_le64(extent_start);
1051 ranges[(*out_count)++].length =
1052 cpu_to_le64(min(extent_end, end) - extent_start);
1060 int ksmbd_vfs_remove_xattr(struct mnt_idmap *idmap,
1061 const struct path *path, char *attr_name,
1066 if (get_write == true) {
1067 err = mnt_want_write(path->mnt);
1072 err = vfs_removexattr(idmap, path->dentry, attr_name);
1074 if (get_write == true)
1075 mnt_drop_write(path->mnt);
1080 int ksmbd_vfs_unlink(struct file *filp)
1083 struct dentry *dir, *dentry = filp->f_path.dentry;
1084 struct mnt_idmap *idmap = file_mnt_idmap(filp);
1086 err = mnt_want_write(filp->f_path.mnt);
1090 dir = dget_parent(dentry);
1091 err = ksmbd_vfs_lock_parent(dir, dentry);
1096 if (S_ISDIR(d_inode(dentry)->i_mode))
1097 err = vfs_rmdir(idmap, d_inode(dir), dentry);
1099 err = vfs_unlink(idmap, d_inode(dir), dentry, NULL);
1102 inode_unlock(d_inode(dir));
1104 ksmbd_debug(VFS, "failed to delete, err %d\n", err);
1107 mnt_drop_write(filp->f_path.mnt);
1112 static bool __dir_empty(struct dir_context *ctx, const char *name, int namlen,
1113 loff_t offset, u64 ino, unsigned int d_type)
1115 struct ksmbd_readdir_data *buf;
1117 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
1118 if (!is_dot_dotdot(name, namlen))
1119 buf->dirent_count++;
1121 return !buf->dirent_count;
1125 * ksmbd_vfs_empty_dir() - check for empty directory
1126 * @fp: ksmbd file pointer
1128 * Return: true if directory empty, otherwise false
1130 int ksmbd_vfs_empty_dir(struct ksmbd_file *fp)
1133 struct ksmbd_readdir_data readdir_data;
1135 memset(&readdir_data, 0, sizeof(struct ksmbd_readdir_data));
1137 set_ctx_actor(&readdir_data.ctx, __dir_empty);
1138 readdir_data.dirent_count = 0;
1140 err = iterate_dir(fp->filp, &readdir_data.ctx);
1141 if (readdir_data.dirent_count)
1148 static bool __caseless_lookup(struct dir_context *ctx, const char *name,
1149 int namlen, loff_t offset, u64 ino,
1150 unsigned int d_type)
1152 struct ksmbd_readdir_data *buf;
1155 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
1157 if (buf->used != namlen)
1159 if (IS_ENABLED(CONFIG_UNICODE) && buf->um) {
1160 const struct qstr q_buf = {.name = buf->private,
1162 const struct qstr q_name = {.name = name,
1165 cmp = utf8_strncasecmp(buf->um, &q_buf, &q_name);
1168 cmp = strncasecmp((char *)buf->private, name, namlen);
1170 memcpy((char *)buf->private, name, buf->used);
1171 buf->dirent_count = 1;
1178 * ksmbd_vfs_lookup_in_dir() - lookup a file in a directory
1180 * @name: filename to lookup
1181 * @namelen: filename length
1182 * @um: &struct unicode_map to use
1184 * Return: 0 on success, otherwise error
1186 static int ksmbd_vfs_lookup_in_dir(const struct path *dir, char *name,
1187 size_t namelen, struct unicode_map *um)
1191 int flags = O_RDONLY | O_LARGEFILE;
1192 struct ksmbd_readdir_data readdir_data = {
1193 .ctx.actor = __caseless_lookup,
1200 dfilp = dentry_open(dir, flags, current_cred());
1202 return PTR_ERR(dfilp);
1204 ret = iterate_dir(dfilp, &readdir_data.ctx);
1205 if (readdir_data.dirent_count > 0)
1212 * ksmbd_vfs_kern_path_locked() - lookup a file and get path info
1214 * @name: file path that is relative to share
1215 * @flags: lookup flags
1216 * @parent_path: if lookup succeed, return parent_path info
1217 * @path: if lookup succeed, return path info
1218 * @caseless: caseless filename lookup
1220 * Return: 0 on success, otherwise error
1222 int ksmbd_vfs_kern_path_locked(struct ksmbd_work *work, char *name,
1223 unsigned int flags, struct path *parent_path,
1224 struct path *path, bool caseless)
1226 struct ksmbd_share_config *share_conf = work->tcon->share_conf;
1229 err = ksmbd_vfs_path_lookup_locked(share_conf, name, flags, parent_path,
1236 size_t path_len, remain_len;
1239 path_len = strlen(filepath);
1240 remain_len = path_len;
1242 *parent_path = share_conf->vfs_path;
1243 path_get(parent_path);
1245 while (d_can_lookup(parent_path->dentry)) {
1246 char *filename = filepath + path_len - remain_len;
1247 char *next = strchrnul(filename, '/');
1248 size_t filename_len = next - filename;
1249 bool is_last = !next[0];
1251 if (filename_len == 0)
1254 err = ksmbd_vfs_lookup_in_dir(parent_path, filename,
1262 err = vfs_path_lookup(share_conf->vfs_path.dentry,
1263 share_conf->vfs_path.mnt,
1271 path_put(parent_path);
1272 *parent_path = *path;
1275 remain_len -= filename_len + 1;
1280 path_put(parent_path);
1285 err = mnt_want_write(parent_path->mnt);
1288 path_put(parent_path);
1292 err = ksmbd_vfs_lock_parent(parent_path->dentry, path->dentry);
1295 path_put(parent_path);
1301 void ksmbd_vfs_kern_path_unlock(struct path *parent_path, struct path *path)
1303 inode_unlock(d_inode(parent_path->dentry));
1304 mnt_drop_write(parent_path->mnt);
1306 path_put(parent_path);
1309 struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work,
1315 struct dentry *dent;
1317 abs_name = convert_to_unix_name(work->tcon->share_conf, name);
1319 return ERR_PTR(-ENOMEM);
1321 dent = kern_path_create(AT_FDCWD, abs_name, path, flags);
1326 int ksmbd_vfs_remove_acl_xattrs(struct mnt_idmap *idmap,
1327 const struct path *path)
1329 char *name, *xattr_list = NULL;
1330 ssize_t xattr_list_len;
1333 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
1334 if (xattr_list_len < 0) {
1336 } else if (!xattr_list_len) {
1337 ksmbd_debug(SMB, "empty xattr in the file\n");
1341 err = mnt_want_write(path->mnt);
1345 for (name = xattr_list; name - xattr_list < xattr_list_len;
1346 name += strlen(name) + 1) {
1347 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
1349 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
1350 sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1) ||
1351 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
1352 sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1)) {
1353 err = vfs_remove_acl(idmap, path->dentry, name);
1356 "remove acl xattr failed : %s\n", name);
1359 mnt_drop_write(path->mnt);
1366 int ksmbd_vfs_remove_sd_xattrs(struct mnt_idmap *idmap, const struct path *path)
1368 char *name, *xattr_list = NULL;
1369 ssize_t xattr_list_len;
1372 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
1373 if (xattr_list_len < 0) {
1375 } else if (!xattr_list_len) {
1376 ksmbd_debug(SMB, "empty xattr in the file\n");
1380 for (name = xattr_list; name - xattr_list < xattr_list_len;
1381 name += strlen(name) + 1) {
1382 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
1384 if (!strncmp(name, XATTR_NAME_SD, XATTR_NAME_SD_LEN)) {
1385 err = ksmbd_vfs_remove_xattr(idmap, path, name, true);
1387 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
1395 static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct mnt_idmap *idmap,
1396 struct inode *inode,
1399 struct xattr_smb_acl *smb_acl = NULL;
1400 struct posix_acl *posix_acls;
1401 struct posix_acl_entry *pa_entry;
1402 struct xattr_acl_entry *xa_entry;
1405 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1408 posix_acls = get_inode_acl(inode, acl_type);
1409 if (IS_ERR_OR_NULL(posix_acls))
1412 smb_acl = kzalloc(sizeof(struct xattr_smb_acl) +
1413 sizeof(struct xattr_acl_entry) * posix_acls->a_count,
1418 smb_acl->count = posix_acls->a_count;
1419 pa_entry = posix_acls->a_entries;
1420 xa_entry = smb_acl->entries;
1421 for (i = 0; i < posix_acls->a_count; i++, pa_entry++, xa_entry++) {
1422 switch (pa_entry->e_tag) {
1424 xa_entry->type = SMB_ACL_USER;
1425 xa_entry->uid = posix_acl_uid_translate(idmap, pa_entry);
1428 xa_entry->type = SMB_ACL_USER_OBJ;
1431 xa_entry->type = SMB_ACL_GROUP;
1432 xa_entry->gid = posix_acl_gid_translate(idmap, pa_entry);
1435 xa_entry->type = SMB_ACL_GROUP_OBJ;
1438 xa_entry->type = SMB_ACL_OTHER;
1441 xa_entry->type = SMB_ACL_MASK;
1444 pr_err("unknown type : 0x%x\n", pa_entry->e_tag);
1448 if (pa_entry->e_perm & ACL_READ)
1449 xa_entry->perm |= SMB_ACL_READ;
1450 if (pa_entry->e_perm & ACL_WRITE)
1451 xa_entry->perm |= SMB_ACL_WRITE;
1452 if (pa_entry->e_perm & ACL_EXECUTE)
1453 xa_entry->perm |= SMB_ACL_EXECUTE;
1456 posix_acl_release(posix_acls);
1460 int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
1461 struct mnt_idmap *idmap,
1462 const struct path *path,
1463 struct smb_ntsd *pntsd, int len,
1467 struct ndr sd_ndr = {0}, acl_ndr = {0};
1468 struct xattr_ntacl acl = {0};
1469 struct xattr_smb_acl *smb_acl, *def_smb_acl = NULL;
1470 struct dentry *dentry = path->dentry;
1471 struct inode *inode = d_inode(dentry);
1474 acl.hash_type = XATTR_SD_HASH_TYPE_SHA256;
1475 acl.current_time = ksmbd_UnixTimeToNT(current_time(inode));
1477 memcpy(acl.desc, "posix_acl", 9);
1481 cpu_to_le32(le32_to_cpu(pntsd->osidoffset) + NDR_NTSD_OFFSETOF);
1483 cpu_to_le32(le32_to_cpu(pntsd->gsidoffset) + NDR_NTSD_OFFSETOF);
1485 cpu_to_le32(le32_to_cpu(pntsd->dacloffset) + NDR_NTSD_OFFSETOF);
1487 acl.sd_buf = (char *)pntsd;
1490 rc = ksmbd_gen_sd_hash(conn, acl.sd_buf, acl.sd_size, acl.hash);
1492 pr_err("failed to generate hash for ndr acl\n");
1496 smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode,
1498 if (S_ISDIR(inode->i_mode))
1499 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode,
1502 rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode,
1503 smb_acl, def_smb_acl);
1505 pr_err("failed to encode ndr to posix acl\n");
1509 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset,
1510 acl.posix_acl_hash);
1512 pr_err("failed to generate hash for ndr acl\n");
1516 rc = ndr_encode_v4_ntacl(&sd_ndr, &acl);
1518 pr_err("failed to encode ndr to posix acl\n");
1522 rc = ksmbd_vfs_setxattr(idmap, path,
1523 XATTR_NAME_SD, sd_ndr.data,
1524 sd_ndr.offset, 0, get_write);
1526 pr_err("Failed to store XATTR ntacl :%d\n", rc);
1530 kfree(acl_ndr.data);
1536 int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
1537 struct mnt_idmap *idmap,
1538 struct dentry *dentry,
1539 struct smb_ntsd **pntsd)
1543 struct inode *inode = d_inode(dentry);
1544 struct ndr acl_ndr = {0};
1545 struct xattr_ntacl acl;
1546 struct xattr_smb_acl *smb_acl = NULL, *def_smb_acl = NULL;
1547 __u8 cmp_hash[XATTR_SD_HASH_SIZE] = {0};
1549 rc = ksmbd_vfs_getxattr(idmap, dentry, XATTR_NAME_SD, &n.data);
1554 rc = ndr_decode_v4_ntacl(&n, &acl);
1558 smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode,
1560 if (S_ISDIR(inode->i_mode))
1561 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode,
1564 rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode, smb_acl,
1567 pr_err("failed to encode ndr to posix acl\n");
1571 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset, cmp_hash);
1573 pr_err("failed to generate hash for ndr acl\n");
1577 if (memcmp(cmp_hash, acl.posix_acl_hash, XATTR_SD_HASH_SIZE)) {
1578 pr_err("hash value diff\n");
1583 *pntsd = acl.sd_buf;
1584 if (acl.sd_size < sizeof(struct smb_ntsd)) {
1585 pr_err("sd size is invalid\n");
1589 (*pntsd)->osidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->osidoffset) -
1591 (*pntsd)->gsidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->gsidoffset) -
1593 (*pntsd)->dacloffset = cpu_to_le32(le32_to_cpu((*pntsd)->dacloffset) -
1598 kfree(acl_ndr.data);
1611 int ksmbd_vfs_set_dos_attrib_xattr(struct mnt_idmap *idmap,
1612 const struct path *path,
1613 struct xattr_dos_attrib *da,
1619 err = ndr_encode_dos_attr(&n, da);
1623 err = ksmbd_vfs_setxattr(idmap, path, XATTR_NAME_DOS_ATTRIBUTE,
1624 (void *)n.data, n.offset, 0, get_write);
1626 ksmbd_debug(SMB, "failed to store dos attribute in xattr\n");
1632 int ksmbd_vfs_get_dos_attrib_xattr(struct mnt_idmap *idmap,
1633 struct dentry *dentry,
1634 struct xattr_dos_attrib *da)
1639 err = ksmbd_vfs_getxattr(idmap, dentry, XATTR_NAME_DOS_ATTRIBUTE,
1643 if (ndr_decode_dos_attr(&n, da))
1647 ksmbd_debug(SMB, "failed to load dos attribute in xattr\n");
1654 * ksmbd_vfs_init_kstat() - convert unix stat information to smb stat format
1655 * @p: destination buffer
1656 * @ksmbd_kstat: ksmbd kstat wrapper
1658 * Returns: pointer to the converted &struct file_directory_info
1660 void *ksmbd_vfs_init_kstat(char **p, struct ksmbd_kstat *ksmbd_kstat)
1662 struct file_directory_info *info = (struct file_directory_info *)(*p);
1663 struct kstat *kstat = ksmbd_kstat->kstat;
1666 info->FileIndex = 0;
1667 info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
1668 time = ksmbd_UnixTimeToNT(kstat->atime);
1669 info->LastAccessTime = cpu_to_le64(time);
1670 time = ksmbd_UnixTimeToNT(kstat->mtime);
1671 info->LastWriteTime = cpu_to_le64(time);
1672 time = ksmbd_UnixTimeToNT(kstat->ctime);
1673 info->ChangeTime = cpu_to_le64(time);
1675 if (ksmbd_kstat->file_attributes & FILE_ATTRIBUTE_DIRECTORY_LE) {
1676 info->EndOfFile = 0;
1677 info->AllocationSize = 0;
1679 info->EndOfFile = cpu_to_le64(kstat->size);
1680 info->AllocationSize = cpu_to_le64(kstat->blocks << 9);
1682 info->ExtFileAttributes = ksmbd_kstat->file_attributes;
1687 int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work,
1688 struct mnt_idmap *idmap,
1689 struct dentry *dentry,
1690 struct ksmbd_kstat *ksmbd_kstat)
1692 struct ksmbd_share_config *share_conf = work->tcon->share_conf;
1695 struct path path = {
1696 .mnt = share_conf->vfs_path.mnt,
1700 rc = vfs_getattr(&path, ksmbd_kstat->kstat,
1701 STATX_BASIC_STATS | STATX_BTIME,
1702 AT_STATX_SYNC_AS_STAT);
1706 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
1707 ksmbd_kstat->create_time = time;
1710 * set default value for the case that store dos attributes is not yes
1711 * or that acl is disable in server's filesystem and the config is yes.
1713 if (S_ISDIR(ksmbd_kstat->kstat->mode))
1714 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_DIRECTORY_LE;
1716 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_ARCHIVE_LE;
1718 if (test_share_config_flag(work->tcon->share_conf,
1719 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
1720 struct xattr_dos_attrib da;
1722 rc = ksmbd_vfs_get_dos_attrib_xattr(idmap, dentry, &da);
1724 ksmbd_kstat->file_attributes = cpu_to_le32(da.attr);
1725 ksmbd_kstat->create_time = da.create_time;
1727 ksmbd_debug(VFS, "fail to load dos attribute.\n");
1734 ssize_t ksmbd_vfs_casexattr_len(struct mnt_idmap *idmap,
1735 struct dentry *dentry, char *attr_name,
1738 char *name, *xattr_list = NULL;
1739 ssize_t value_len = -ENOENT, xattr_list_len;
1741 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
1742 if (xattr_list_len <= 0)
1745 for (name = xattr_list; name - xattr_list < xattr_list_len;
1746 name += strlen(name) + 1) {
1747 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
1748 if (strncasecmp(attr_name, name, attr_name_len))
1751 value_len = ksmbd_vfs_xattr_len(idmap, dentry, name);
1760 int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name,
1761 size_t *xattr_stream_name_size, int s_type)
1765 if (s_type == DIR_STREAM)
1766 type = ":$INDEX_ALLOCATION";
1770 buf = kasprintf(KSMBD_DEFAULT_GFP, "%s%s%s",
1771 XATTR_NAME_STREAM, stream_name, type);
1775 *xattr_stream_name = buf;
1776 *xattr_stream_name_size = strlen(buf) + 1;
1781 int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work,
1782 struct ksmbd_file *src_fp,
1783 struct ksmbd_file *dst_fp,
1784 struct srv_copychunk *chunks,
1785 unsigned int chunk_count,
1786 unsigned int *chunk_count_written,
1787 unsigned int *chunk_size_written,
1788 loff_t *total_size_written)
1791 loff_t src_off, dst_off, src_file_size;
1795 *chunk_count_written = 0;
1796 *chunk_size_written = 0;
1797 *total_size_written = 0;
1799 if (!(src_fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
1800 pr_err("no right to read(%pD)\n", src_fp->filp);
1803 if (!(dst_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) {
1804 pr_err("no right to write(%pD)\n", dst_fp->filp);
1808 if (ksmbd_stream_fd(src_fp) || ksmbd_stream_fd(dst_fp))
1811 smb_break_all_levII_oplock(work, dst_fp, 1);
1813 if (!work->tcon->posix_extensions) {
1814 for (i = 0; i < chunk_count; i++) {
1815 src_off = le64_to_cpu(chunks[i].SourceOffset);
1816 dst_off = le64_to_cpu(chunks[i].TargetOffset);
1817 len = le32_to_cpu(chunks[i].Length);
1819 if (check_lock_range(src_fp->filp, src_off,
1820 src_off + len - 1, READ))
1822 if (check_lock_range(dst_fp->filp, dst_off,
1823 dst_off + len - 1, WRITE))
1828 src_file_size = i_size_read(file_inode(src_fp->filp));
1830 for (i = 0; i < chunk_count; i++) {
1831 src_off = le64_to_cpu(chunks[i].SourceOffset);
1832 dst_off = le64_to_cpu(chunks[i].TargetOffset);
1833 len = le32_to_cpu(chunks[i].Length);
1835 if (src_off + len > src_file_size)
1838 ret = vfs_copy_file_range(src_fp->filp, src_off,
1839 dst_fp->filp, dst_off, len, 0);
1840 if (ret == -EOPNOTSUPP || ret == -EXDEV)
1841 ret = vfs_copy_file_range(src_fp->filp, src_off,
1842 dst_fp->filp, dst_off, len,
1847 *chunk_count_written += 1;
1848 *total_size_written += ret;
1853 void ksmbd_vfs_posix_lock_wait(struct file_lock *flock)
1855 wait_event(flock->c.flc_wait, !flock->c.flc_blocker);
1858 int ksmbd_vfs_posix_lock_wait_timeout(struct file_lock *flock, long timeout)
1860 return wait_event_interruptible_timeout(flock->c.flc_wait,
1861 !flock->c.flc_blocker,
1865 void ksmbd_vfs_posix_lock_unblock(struct file_lock *flock)
1867 locks_delete_block(flock);
1870 int ksmbd_vfs_set_init_posix_acl(struct mnt_idmap *idmap,
1873 struct posix_acl_state acl_state;
1874 struct posix_acl *acls;
1875 struct dentry *dentry = path->dentry;
1876 struct inode *inode = d_inode(dentry);
1879 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1882 ksmbd_debug(SMB, "Set posix acls\n");
1883 rc = init_acl_state(&acl_state, 1);
1887 /* Set default owner group */
1888 acl_state.owner.allow = (inode->i_mode & 0700) >> 6;
1889 acl_state.group.allow = (inode->i_mode & 0070) >> 3;
1890 acl_state.other.allow = inode->i_mode & 0007;
1891 acl_state.users->aces[acl_state.users->n].uid = inode->i_uid;
1892 acl_state.users->aces[acl_state.users->n++].perms.allow =
1893 acl_state.owner.allow;
1894 acl_state.groups->aces[acl_state.groups->n].gid = inode->i_gid;
1895 acl_state.groups->aces[acl_state.groups->n++].perms.allow =
1896 acl_state.group.allow;
1897 acl_state.mask.allow = 0x07;
1899 acls = posix_acl_alloc(6, KSMBD_DEFAULT_GFP);
1901 free_acl_state(&acl_state);
1904 posix_state_to_acl(&acl_state, acls->a_entries);
1906 rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls);
1908 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1910 else if (S_ISDIR(inode->i_mode)) {
1911 posix_state_to_acl(&acl_state, acls->a_entries);
1912 rc = set_posix_acl(idmap, dentry, ACL_TYPE_DEFAULT, acls);
1914 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1918 free_acl_state(&acl_state);
1919 posix_acl_release(acls);
1923 int ksmbd_vfs_inherit_posix_acl(struct mnt_idmap *idmap,
1924 struct path *path, struct inode *parent_inode)
1926 struct posix_acl *acls;
1927 struct posix_acl_entry *pace;
1928 struct dentry *dentry = path->dentry;
1929 struct inode *inode = d_inode(dentry);
1932 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1935 acls = get_inode_acl(parent_inode, ACL_TYPE_DEFAULT);
1936 if (IS_ERR_OR_NULL(acls))
1938 pace = acls->a_entries;
1940 for (i = 0; i < acls->a_count; i++, pace++) {
1941 if (pace->e_tag == ACL_MASK) {
1942 pace->e_perm = 0x07;
1947 rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls);
1949 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1951 if (S_ISDIR(inode->i_mode)) {
1952 rc = set_posix_acl(idmap, dentry, ACL_TYPE_DEFAULT,
1955 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1959 posix_acl_release(acls);