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