blob: 19d36393974ca95a30590921f98ad649e11014c2 [file] [log] [blame]
Namjae Jeonf4415842021-03-16 10:50:04 +09001// 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 Jeonf4415842021-03-16 10:50:04 +090012#include <linux/xattr.h>
13#include <linux/falloc.h>
14#include <linux/genhd.h>
Namjae Jeonf4415842021-03-16 10:50:04 +090015#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 Lee265fd192021-09-25 00:06:16 +090022#include "../internal.h" /* for vfs_path_lookup */
23
Namjae Jeonf4415842021-03-16 10:50:04 +090024#include "glob.h"
25#include "oplock.h"
26#include "connection.h"
Namjae Jeonf4415842021-03-16 10:50:04 +090027#include "vfs.h"
28#include "vfs_cache.h"
29#include "smbacl.h"
30#include "ndr.h"
31#include "auth.h"
Namjae Jeon56265182021-04-14 09:24:11 +090032#include "misc.h"
Namjae Jeonf4415842021-03-16 10:50:04 +090033
Namjae Jeonf4415842021-03-16 10:50:04 +090034#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
40static 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 Jeonf4415842021-03-16 10:50:04 +090049 }
50 return p;
51}
52
Namjae Jeonf4415842021-03-16 10:50:04 +090053static void ksmbd_vfs_inherit_owner(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +090054 struct inode *parent_inode,
55 struct inode *inode)
Namjae Jeonf4415842021-03-16 10:50:04 +090056{
57 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +090058 KSMBD_SHARE_FLAG_INHERIT_OWNER))
Namjae Jeonf4415842021-03-16 10:50:04 +090059 return;
60
61 i_uid_write(inode, i_uid_read(parent_inode));
62}
63
Hyunchul Lee333111a2021-06-23 11:07:43 +090064/**
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 Braunerda1e7ad2021-08-23 17:13:47 +020073int ksmbd_vfs_lock_parent(struct user_namespace *user_ns, struct dentry *parent,
74 struct dentry *child)
Hyunchul Lee333111a2021-06-23 11:07:43 +090075{
76 struct dentry *dentry;
77 int ret = 0;
78
79 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
Christian Braunerda1e7ad2021-08-23 17:13:47 +020080 dentry = lookup_one(user_ns, child->d_name.name, parent,
81 child->d_name.len);
Hyunchul Lee333111a2021-06-23 11:07:43 +090082 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;
95out_err:
96 inode_unlock(d_inode(parent));
97 return ret;
98}
99
Hyunchul Leeaf349832021-06-30 18:25:53 +0900100int ksmbd_vfs_may_delete(struct user_namespace *user_ns,
101 struct dentry *dentry)
Namjae Jeonf4415842021-03-16 10:50:04 +0900102{
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +0900103 struct dentry *parent;
104 int ret;
Namjae Jeonf4415842021-03-16 10:50:04 +0900105
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +0900106 parent = dget_parent(dentry);
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200107 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +0900108 if (ret) {
Namjae Jeonf4415842021-03-16 10:50:04 +0900109 dput(parent);
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +0900110 return ret;
Namjae Jeonf4415842021-03-16 10:50:04 +0900111 }
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +0900112
Hyunchul Leeaf349832021-06-30 18:25:53 +0900113 ret = inode_permission(user_ns, d_inode(parent),
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +0900114 MAY_EXEC | MAY_WRITE);
115
116 inode_unlock(d_inode(parent));
117 dput(parent);
Namjae Jeonff1d5722021-04-13 13:18:10 +0900118 return ret;
Namjae Jeonf4415842021-03-16 10:50:04 +0900119}
120
Hyunchul Leeaf349832021-06-30 18:25:53 +0900121int ksmbd_vfs_query_maximal_access(struct user_namespace *user_ns,
122 struct dentry *dentry, __le32 *daccess)
Namjae Jeonf4415842021-03-16 10:50:04 +0900123{
Hyunchul Lee333111a2021-06-23 11:07:43 +0900124 struct dentry *parent;
Namjae Jeonff1d5722021-04-13 13:18:10 +0900125 int ret = 0;
Namjae Jeonf4415842021-03-16 10:50:04 +0900126
127 *daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL);
128
Hyunchul Leeaf349832021-06-30 18:25:53 +0900129 if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_WRITE))
Namjae Jeonf4415842021-03-16 10:50:04 +0900130 *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 Leeaf349832021-06-30 18:25:53 +0900135 if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_READ))
Namjae Jeonf4415842021-03-16 10:50:04 +0900136 *daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE;
137
Hyunchul Leeaf349832021-06-30 18:25:53 +0900138 if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_EXEC))
Namjae Jeonf4415842021-03-16 10:50:04 +0900139 *daccess |= FILE_EXECUTE_LE;
140
141 parent = dget_parent(dentry);
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200142 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
Hyunchul Lee333111a2021-06-23 11:07:43 +0900143 if (ret) {
144 dput(parent);
145 return ret;
Namjae Jeonff1d5722021-04-13 13:18:10 +0900146 }
147
Hyunchul Leeaf349832021-06-30 18:25:53 +0900148 if (!inode_permission(user_ns, d_inode(parent), MAY_EXEC | MAY_WRITE))
Namjae Jeonf4415842021-03-16 10:50:04 +0900149 *daccess |= FILE_DELETE_LE;
Namjae Jeonff1d5722021-04-13 13:18:10 +0900150
Namjae Jeonff1d5722021-04-13 13:18:10 +0900151 inode_unlock(d_inode(parent));
Namjae Jeonf4415842021-03-16 10:50:04 +0900152 dput(parent);
Namjae Jeonff1d5722021-04-13 13:18:10 +0900153 return ret;
Namjae Jeonf4415842021-03-16 10:50:04 +0900154}
155
Namjae Jeonf4415842021-03-16 10:50:04 +0900156/**
157 * ksmbd_vfs_create() - vfs helper for smb create file
158 * @work: work
Hyunchul Lee265fd192021-09-25 00:06:16 +0900159 * @name: file name that is relative to share
Namjae Jeonf4415842021-03-16 10:50:04 +0900160 * @mode: file create mode
161 *
162 * Return: 0 on success, otherwise error
163 */
Namjae Jeon64b39f42021-03-30 14:25:35 +0900164int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode)
Namjae Jeonf4415842021-03-16 10:50:04 +0900165{
166 struct path path;
167 struct dentry *dentry;
168 int err;
169
Hyunchul Lee265fd192021-09-25 00:06:16 +0900170 dentry = ksmbd_vfs_kern_path_create(work, name,
171 LOOKUP_NO_SYMLINKS, &path);
Namjae Jeonf4415842021-03-16 10:50:04 +0900172 if (IS_ERR(dentry)) {
173 err = PTR_ERR(dentry);
174 if (err != -ENOENT)
Namjae Jeonbde16942021-06-28 15:23:19 +0900175 pr_err("path create failed for %s, err %d\n",
176 name, err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900177 return err;
178 }
179
180 mode |= S_IFREG;
Hyunchul Leeaf349832021-06-30 18:25:53 +0900181 err = vfs_create(mnt_user_ns(path.mnt), d_inode(path.dentry),
182 dentry, mode, true);
Namjae Jeonf4415842021-03-16 10:50:04 +0900183 if (!err) {
184 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry),
Namjae Jeon070fb212021-05-26 17:57:12 +0900185 d_inode(dentry));
Namjae Jeonf4415842021-03-16 10:50:04 +0900186 } else {
Namjae Jeonbde16942021-06-28 15:23:19 +0900187 pr_err("File(%s): creation failed (err:%d)\n", name, err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900188 }
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 Lee265fd192021-09-25 00:06:16 +0900196 * @name: directory name that is relative to share
Namjae Jeonf4415842021-03-16 10:50:04 +0900197 * @mode: directory create mode
198 *
199 * Return: 0 on success, otherwise error
200 */
Namjae Jeon64b39f42021-03-30 14:25:35 +0900201int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode)
Namjae Jeonf4415842021-03-16 10:50:04 +0900202{
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200203 struct user_namespace *user_ns;
Namjae Jeonf4415842021-03-16 10:50:04 +0900204 struct path path;
205 struct dentry *dentry;
206 int err;
207
Hyunchul Lee265fd192021-09-25 00:06:16 +0900208 dentry = ksmbd_vfs_kern_path_create(work, name,
209 LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY,
210 &path);
Namjae Jeonf4415842021-03-16 10:50:04 +0900211 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 Jeon070fb212021-05-26 17:57:12 +0900215 name, err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900216 return err;
217 }
218
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200219 user_ns = mnt_user_ns(path.mnt);
Namjae Jeonf4415842021-03-16 10:50:04 +0900220 mode |= S_IFDIR;
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200221 err = vfs_mkdir(user_ns, d_inode(path.dentry), dentry, mode);
Namjae Jeona2d63212021-05-26 16:40:39 +0900222 if (err) {
Hyunchul Lee16370232021-04-01 17:32:24 +0900223 goto out;
Namjae Jeona2d63212021-05-26 16:40:39 +0900224 } else if (d_unhashed(dentry)) {
Hyunchul Lee16370232021-04-01 17:32:24 +0900225 struct dentry *d;
Namjae Jeonf4415842021-03-16 10:50:04 +0900226
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200227 d = lookup_one(user_ns, dentry->d_name.name, dentry->d_parent,
228 dentry->d_name.len);
Hyunchul Lee16370232021-04-01 17:32:24 +0900229 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 Jeon070fb212021-05-26 17:57:12 +0900239 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), d_inode(d));
Hyunchul Lee16370232021-04-01 17:32:24 +0900240 dput(d);
241 }
242out:
Namjae Jeonf4415842021-03-16 10:50:04 +0900243 done_path_create(&path, dentry);
Hyunchul Lee16370232021-04-01 17:32:24 +0900244 if (err)
Namjae Jeonbde16942021-06-28 15:23:19 +0900245 pr_err("mkdir(%s): creation failed (err:%d)\n", name, err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900246 return err;
247}
248
Hyunchul Leeaf349832021-06-30 18:25:53 +0900249static ssize_t ksmbd_vfs_getcasexattr(struct user_namespace *user_ns,
250 struct dentry *dentry, char *attr_name,
Namjae Jeon070fb212021-05-26 17:57:12 +0900251 int attr_name_len, char **attr_value)
Namjae Jeonf4415842021-03-16 10:50:04 +0900252{
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 Leeaf349832021-06-30 18:25:53 +0900266 value_len = ksmbd_vfs_getxattr(user_ns,
267 dentry,
Namjae Jeonf4415842021-03-16 10:50:04 +0900268 name,
269 attr_value);
270 if (value_len < 0)
Namjae Jeonbde16942021-06-28 15:23:19 +0900271 pr_err("failed to get xattr in file\n");
Namjae Jeonf4415842021-03-16 10:50:04 +0900272 break;
273 }
274
275out:
Namjae Jeon79f6b112021-04-02 12:47:14 +0900276 kvfree(xattr_list);
Namjae Jeonf4415842021-03-16 10:50:04 +0900277 return value_len;
278}
279
280static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos,
Namjae Jeon070fb212021-05-26 17:57:12 +0900281 size_t count)
Namjae Jeonf4415842021-03-16 10:50:04 +0900282{
283 ssize_t v_len;
284 char *stream_buf = NULL;
Namjae Jeonf4415842021-03-16 10:50:04 +0900285
286 ksmbd_debug(VFS, "read stream data pos : %llu, count : %zd\n",
Namjae Jeon070fb212021-05-26 17:57:12 +0900287 *pos, count);
Namjae Jeonf4415842021-03-16 10:50:04 +0900288
Hyunchul Leeaf349832021-06-30 18:25:53 +0900289 v_len = ksmbd_vfs_getcasexattr(file_mnt_user_ns(fp->filp),
290 fp->filp->f_path.dentry,
Namjae Jeonf4415842021-03-16 10:50:04 +0900291 fp->stream.name,
292 fp->stream.size,
293 &stream_buf);
Yang Yingliangfd6de092021-05-31 17:25:05 +0900294 if ((int)v_len <= 0)
295 return (int)v_len;
Namjae Jeonf4415842021-03-16 10:50:04 +0900296
Namjae Jeon2ae1a6c2021-05-31 17:26:43 +0900297 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 Jeonf4415842021-03-16 10:50:04 +0900305 memcpy(buf, &stream_buf[*pos], count);
Namjae Jeon2ae1a6c2021-05-31 17:26:43 +0900306
307free_buf:
Yang Yingliang673b9ba2021-05-29 16:20:57 +0800308 kvfree(stream_buf);
Namjae Jeon2ae1a6c2021-05-31 17:26:43 +0900309 return count;
Namjae Jeonf4415842021-03-16 10:50:04 +0900310}
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 Jeon64b39f42021-03-30 14:25:35 +0900321static int check_lock_range(struct file *filp, loff_t start, loff_t end,
Namjae Jeon070fb212021-05-26 17:57:12 +0900322 unsigned char type)
Namjae Jeonf4415842021-03-16 10:50:04 +0900323{
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 Jeonbde16942021-06-28 15:23:19 +0900337 pr_err("not allow write by shared lock\n");
Namjae Jeonf4415842021-03-16 10:50:04 +0900338 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 Jeonbde16942021-06-28 15:23:19 +0900345 pr_err("not allow rw access by exclusive lock from other opens\n");
Namjae Jeonf4415842021-03-16 10:50:04 +0900346 goto out;
347 }
348 }
349 }
350 }
351out:
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 Jeon64b39f42021-03-30 14:25:35 +0900365int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count,
Namjae Jeon070fb212021-05-26 17:57:12 +0900366 loff_t *pos)
Namjae Jeonf4415842021-03-16 10:50:04 +0900367{
Namjae Jeonafa8f012021-06-18 10:18:34 +0900368 struct file *filp = fp->filp;
Namjae Jeonf4415842021-03-16 10:50:04 +0900369 ssize_t nbytes = 0;
Namjae Jeonafa8f012021-06-18 10:18:34 +0900370 char *rbuf = work->aux_payload_buf;
371 struct inode *inode = file_inode(filp);
Namjae Jeonf4415842021-03-16 10:50:04 +0900372
Namjae Jeonf4415842021-03-16 10:50:04 +0900373 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 Jeon493fa2f2021-06-29 09:22:16 +0900381 pr_err("no right to read(%pd)\n",
382 fp->filp->f_path.dentry);
Namjae Jeonf4415842021-03-16 10:50:04 +0900383 return -EACCES;
384 }
385 }
386
387 if (ksmbd_stream_fd(fp))
388 return ksmbd_vfs_stream_read(fp, rbuf, pos, count);
389
Namjae Jeonc36fca82021-03-30 12:43:18 +0900390 if (!work->tcon->posix_extensions) {
391 int ret;
392
Namjae Jeon070fb212021-05-26 17:57:12 +0900393 ret = check_lock_range(filp, *pos, *pos + count - 1, READ);
Namjae Jeonc36fca82021-03-30 12:43:18 +0900394 if (ret) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900395 pr_err("unable to read due to lock\n");
Namjae Jeonc36fca82021-03-30 12:43:18 +0900396 return -EAGAIN;
397 }
Namjae Jeonf4415842021-03-16 10:50:04 +0900398 }
399
400 nbytes = kernel_read(filp, rbuf, count, pos);
401 if (nbytes < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900402 pr_err("smb read failed for (%s), err = %zd\n",
403 fp->filename, nbytes);
Namjae Jeonf4415842021-03-16 10:50:04 +0900404 return nbytes;
405 }
406
407 filp->f_pos = *pos;
408 return nbytes;
409}
410
411static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
Namjae Jeon070fb212021-05-26 17:57:12 +0900412 size_t count)
Namjae Jeonf4415842021-03-16 10:50:04 +0900413{
414 char *stream_buf = NULL, *wbuf;
Hyunchul Lee465d7202021-07-03 12:10:36 +0900415 struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeonf4415842021-03-16 10:50:04 +0900416 size_t size, v_len;
417 int err = 0;
418
419 ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n",
Namjae Jeon070fb212021-05-26 17:57:12 +0900420 *pos, count);
Namjae Jeonf4415842021-03-16 10:50:04 +0900421
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 Lee465d7202021-07-03 12:10:36 +0900428 v_len = ksmbd_vfs_getcasexattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +0900429 fp->filp->f_path.dentry,
Namjae Jeonf4415842021-03-16 10:50:04 +0900430 fp->stream.name,
431 fp->stream.size,
432 &stream_buf);
Yang Yingliangfd6de092021-05-31 17:25:05 +0900433 if ((int)v_len < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900434 pr_err("not found stream in xattr : %zd\n", v_len);
Yang Yingliangfd6de092021-05-31 17:25:05 +0900435 err = (int)v_len;
Namjae Jeonf4415842021-03-16 10:50:04 +0900436 goto out;
437 }
438
439 if (v_len < size) {
Namjae Jeon79f6b112021-04-02 12:47:14 +0900440 wbuf = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
Namjae Jeonf4415842021-03-16 10:50:04 +0900441 if (!wbuf) {
442 err = -ENOMEM;
443 goto out;
444 }
445
446 if (v_len > 0)
447 memcpy(wbuf, stream_buf, v_len);
Yang Yingliang113ef682021-05-29 16:20:56 +0800448 kvfree(stream_buf);
Namjae Jeonf4415842021-03-16 10:50:04 +0900449 stream_buf = wbuf;
450 }
451
452 memcpy(&stream_buf[*pos], buf, count);
453
Hyunchul Lee465d7202021-07-03 12:10:36 +0900454 err = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +0900455 fp->filp->f_path.dentry,
Namjae Jeonf4415842021-03-16 10:50:04 +0900456 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;
465out:
Namjae Jeon79f6b112021-04-02 12:47:14 +0900466 kvfree(stream_buf);
Namjae Jeonf4415842021-03-16 10:50:04 +0900467 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 */
482int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +0900483 char *buf, size_t count, loff_t *pos, bool sync,
484 ssize_t *written)
Namjae Jeonf4415842021-03-16 10:50:04 +0900485{
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 Jeon493fa2f2021-06-29 09:22:16 +0900493 pr_err("no right to write(%pd)\n",
494 fp->filp->f_path.dentry);
Namjae Jeonf4415842021-03-16 10:50:04 +0900495 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 Jeonc36fca82021-03-30 12:43:18 +0900509 if (!work->tcon->posix_extensions) {
510 err = check_lock_range(filp, *pos, *pos + count - 1, WRITE);
511 if (err) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900512 pr_err("unable to write due to lock\n");
Namjae Jeonc36fca82021-03-30 12:43:18 +0900513 err = -EAGAIN;
514 goto out;
515 }
Namjae Jeonf4415842021-03-16 10:50:04 +0900516 }
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 Jeon493fa2f2021-06-29 09:22:16 +0900533 pr_err("fsync failed for filename = %pd, err = %d\n",
534 fp->filp->f_path.dentry, err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900535 }
536
537out:
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 */
549int 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 Jeonbde16942021-06-28 15:23:19 +0900555 pr_err("getattr failed, err %d\n", err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900556 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 Jeon64b39f42021-03-30 14:25:35 +0900566int ksmbd_vfs_fsync(struct ksmbd_work *work, u64 fid, u64 p_id)
Namjae Jeonf4415842021-03-16 10:50:04 +0900567{
568 struct ksmbd_file *fp;
569 int err;
570
571 fp = ksmbd_lookup_fd_slow(work, fid, p_id);
572 if (!fp) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900573 pr_err("failed to get filp for fid %llu\n", fid);
Namjae Jeonf4415842021-03-16 10:50:04 +0900574 return -ENOENT;
575 }
576 err = vfs_fsync(fp->filp, 0);
577 if (err < 0)
Namjae Jeonbde16942021-06-28 15:23:19 +0900578 pr_err("smb fsync failed, err = %d\n", err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900579 ksmbd_fd_put(work, fp);
580 return err;
581}
582
583/**
584 * ksmbd_vfs_remove_file() - vfs helper for smb rmdir or unlink
Hyunchul Lee265fd192021-09-25 00:06:16 +0900585 * @name: directory or file name that is relative to share
Namjae Jeonf4415842021-03-16 10:50:04 +0900586 *
587 * Return: 0 on success, otherwise error
588 */
589int ksmbd_vfs_remove_file(struct ksmbd_work *work, char *name)
590{
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200591 struct user_namespace *user_ns;
Namjae Jeon7c3d3e92021-04-13 13:20:52 +0900592 struct path path;
Hyunchul Lee333111a2021-06-23 11:07:43 +0900593 struct dentry *parent;
Colin Ian King3161ad32021-03-25 17:35:38 +0000594 int err;
Namjae Jeonf4415842021-03-16 10:50:04 +0900595
Namjae Jeonf4415842021-03-16 10:50:04 +0900596 if (ksmbd_override_fsids(work))
597 return -ENOMEM;
598
Hyunchul Lee265fd192021-09-25 00:06:16 +0900599 err = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, false);
Namjae Jeonf4415842021-03-16 10:50:04 +0900600 if (err) {
601 ksmbd_debug(VFS, "can't get %s, err %d\n", name, err);
602 ksmbd_revert_fsids(work);
Namjae Jeonf4415842021-03-16 10:50:04 +0900603 return err;
604 }
605
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200606 user_ns = mnt_user_ns(path.mnt);
Namjae Jeon7c3d3e92021-04-13 13:20:52 +0900607 parent = dget_parent(path.dentry);
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200608 err = ksmbd_vfs_lock_parent(user_ns, parent, path.dentry);
Hyunchul Lee333111a2021-06-23 11:07:43 +0900609 if (err) {
610 dput(parent);
611 path_put(&path);
612 ksmbd_revert_fsids(work);
613 return err;
Namjae Jeonf4415842021-03-16 10:50:04 +0900614 }
615
Hyunchul Lee333111a2021-06-23 11:07:43 +0900616 if (!d_inode(path.dentry)->i_nlink) {
Namjae Jeonf4415842021-03-16 10:50:04 +0900617 err = -ENOENT;
618 goto out_err;
619 }
620
Hyunchul Lee333111a2021-06-23 11:07:43 +0900621 if (S_ISDIR(d_inode(path.dentry)->i_mode)) {
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200622 err = vfs_rmdir(user_ns, d_inode(parent), path.dentry);
Namjae Jeonf4415842021-03-16 10:50:04 +0900623 if (err && err != -ENOTEMPTY)
624 ksmbd_debug(VFS, "%s: rmdir failed, err %d\n", name,
Namjae Jeon070fb212021-05-26 17:57:12 +0900625 err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900626 } else {
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200627 err = vfs_unlink(user_ns, d_inode(parent), path.dentry, NULL);
Namjae Jeonf4415842021-03-16 10:50:04 +0900628 if (err)
629 ksmbd_debug(VFS, "%s: unlink failed, err %d\n", name,
Namjae Jeon070fb212021-05-26 17:57:12 +0900630 err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900631 }
632
Namjae Jeonf4415842021-03-16 10:50:04 +0900633out_err:
Namjae Jeon7c3d3e92021-04-13 13:20:52 +0900634 inode_unlock(d_inode(parent));
635 dput(parent);
636 path_put(&path);
Namjae Jeonf4415842021-03-16 10:50:04 +0900637 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 Lee265fd192021-09-25 00:06:16 +0900644 * @newname: hardlink name that is relative to share
Namjae Jeonf4415842021-03-16 10:50:04 +0900645 *
646 * Return: 0 on success, otherwise error
647 */
Namjae Jeon64b39f42021-03-30 14:25:35 +0900648int ksmbd_vfs_link(struct ksmbd_work *work, const char *oldname,
Namjae Jeon070fb212021-05-26 17:57:12 +0900649 const char *newname)
Namjae Jeonf4415842021-03-16 10:50:04 +0900650{
651 struct path oldpath, newpath;
652 struct dentry *dentry;
653 int err;
654
655 if (ksmbd_override_fsids(work))
656 return -ENOMEM;
657
Namjae Jeon4ea47792021-09-21 14:19:33 +0900658 err = kern_path(oldname, LOOKUP_NO_SYMLINKS, &oldpath);
Namjae Jeonf4415842021-03-16 10:50:04 +0900659 if (err) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900660 pr_err("cannot get linux path for %s, err = %d\n",
661 oldname, err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900662 goto out1;
663 }
664
Hyunchul Lee265fd192021-09-25 00:06:16 +0900665 dentry = ksmbd_vfs_kern_path_create(work, newname,
666 LOOKUP_NO_SYMLINKS | LOOKUP_REVAL,
667 &newpath);
Namjae Jeonf4415842021-03-16 10:50:04 +0900668 if (IS_ERR(dentry)) {
669 err = PTR_ERR(dentry);
Namjae Jeonbde16942021-06-28 15:23:19 +0900670 pr_err("path create err for %s, err %d\n", newname, err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900671 goto out2;
672 }
673
674 err = -EXDEV;
675 if (oldpath.mnt != newpath.mnt) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900676 pr_err("vfs_link failed err %d\n", err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900677 goto out3;
678 }
679
Hyunchul Leeaf349832021-06-30 18:25:53 +0900680 err = vfs_link(oldpath.dentry, mnt_user_ns(newpath.mnt),
681 d_inode(newpath.dentry),
Namjae Jeon070fb212021-05-26 17:57:12 +0900682 dentry, NULL);
Namjae Jeonf4415842021-03-16 10:50:04 +0900683 if (err)
684 ksmbd_debug(VFS, "vfs_link failed err %d\n", err);
685
686out3:
687 done_path_create(&newpath, dentry);
688out2:
689 path_put(&oldpath);
690out1:
691 ksmbd_revert_fsids(work);
692 return err;
693}
694
Namjae Jeon4b637fc2021-06-18 10:20:24 +0900695static 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 Jeonf4415842021-03-16 10:50:04 +0900718static int __ksmbd_vfs_rename(struct ksmbd_work *work,
Hyunchul Leeaf349832021-06-30 18:25:53 +0900719 struct user_namespace *src_user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +0900720 struct dentry *src_dent_parent,
721 struct dentry *src_dent,
Hyunchul Leeaf349832021-06-30 18:25:53 +0900722 struct user_namespace *dst_user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +0900723 struct dentry *dst_dent_parent,
724 struct dentry *trap_dent,
725 char *dst_name)
Namjae Jeonf4415842021-03-16 10:50:04 +0900726{
727 struct dentry *dst_dent;
728 int err;
729
Namjae Jeonc36fca82021-03-30 12:43:18 +0900730 if (!work->tcon->posix_extensions) {
Namjae Jeon4b637fc2021-06-18 10:20:24 +0900731 err = ksmbd_validate_entry_in_use(src_dent);
732 if (err)
733 return err;
Namjae Jeonf4415842021-03-16 10:50:04 +0900734 }
Namjae Jeonf4415842021-03-16 10:50:04 +0900735
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 Braunerda1e7ad2021-08-23 17:13:47 +0200748 dst_dent = lookup_one(dst_user_ns, dst_name, dst_dent_parent,
749 strlen(dst_name));
Namjae Jeonf4415842021-03-16 10:50:04 +0900750 err = PTR_ERR(dst_dent);
751 if (IS_ERR(dst_dent)) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900752 pr_err("lookup failed %s [%d]\n", dst_name, err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900753 goto out;
754 }
755
756 err = -ENOTEMPTY;
757 if (dst_dent != trap_dent && !d_really_is_positive(dst_dent)) {
758 struct renamedata rd = {
Hyunchul Leeaf349832021-06-30 18:25:53 +0900759 .old_mnt_userns = src_user_ns,
Namjae Jeonf4415842021-03-16 10:50:04 +0900760 .old_dir = d_inode(src_dent_parent),
761 .old_dentry = src_dent,
Hyunchul Leeaf349832021-06-30 18:25:53 +0900762 .new_mnt_userns = dst_user_ns,
Namjae Jeonf4415842021-03-16 10:50:04 +0900763 .new_dir = d_inode(dst_dent_parent),
764 .new_dentry = dst_dent,
765 };
766 err = vfs_rename(&rd);
767 }
768 if (err)
Namjae Jeonbde16942021-06-28 15:23:19 +0900769 pr_err("vfs_rename failed err %d\n", err);
Namjae Jeonf4415842021-03-16 10:50:04 +0900770 if (dst_dent)
771 dput(dst_dent);
772out:
773 ksmbd_revert_fsids(work);
774 return err;
775}
776
777int ksmbd_vfs_fp_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +0900778 char *newname)
Namjae Jeonf4415842021-03-16 10:50:04 +0900779{
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200780 struct user_namespace *user_ns;
Namjae Jeonf4415842021-03-16 10:50:04 +0900781 struct path dst_path;
782 struct dentry *src_dent_parent, *dst_dent_parent;
Namjae Jeonff1d5722021-04-13 13:18:10 +0900783 struct dentry *src_dent, *trap_dent, *src_child;
Namjae Jeonf4415842021-03-16 10:50:04 +0900784 char *dst_name;
785 int err;
786
787 dst_name = extract_last_component(newname);
Hyunchul Lee265fd192021-09-25 00:06:16 +0900788 if (!dst_name) {
789 dst_name = newname;
790 newname = "";
791 }
Namjae Jeonf4415842021-03-16 10:50:04 +0900792
793 src_dent_parent = dget_parent(fp->filp->f_path.dentry);
Namjae Jeonf4415842021-03-16 10:50:04 +0900794 src_dent = fp->filp->f_path.dentry;
Namjae Jeonf4415842021-03-16 10:50:04 +0900795
Hyunchul Lee265fd192021-09-25 00:06:16 +0900796 err = ksmbd_vfs_kern_path(work, newname,
797 LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY,
798 &dst_path, false);
Namjae Jeonf4415842021-03-16 10:50:04 +0900799 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 Jeonf4415842021-03-16 10:50:04 +0900804
805 trap_dent = lock_rename(src_dent_parent, dst_dent_parent);
Namjae Jeonff1d5722021-04-13 13:18:10 +0900806 dget(src_dent);
807 dget(dst_dent_parent);
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200808 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 Jeonff1d5722021-04-13 13:18:10 +0900811 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 Jeonf4415842021-03-16 10:50:04 +0900823 err = __ksmbd_vfs_rename(work,
Christian Braunerda1e7ad2021-08-23 17:13:47 +0200824 user_ns,
Namjae Jeonf4415842021-03-16 10:50:04 +0900825 src_dent_parent,
826 src_dent,
Hyunchul Leeaf349832021-06-30 18:25:53 +0900827 mnt_user_ns(dst_path.mnt),
Namjae Jeonf4415842021-03-16 10:50:04 +0900828 dst_dent_parent,
829 trap_dent,
830 dst_name);
Namjae Jeonff1d5722021-04-13 13:18:10 +0900831out_lock:
832 dput(src_dent);
Namjae Jeonf4415842021-03-16 10:50:04 +0900833 dput(dst_dent_parent);
Namjae Jeonff1d5722021-04-13 13:18:10 +0900834 unlock_rename(src_dent_parent, dst_dent_parent);
Namjae Jeonf4415842021-03-16 10:50:04 +0900835 path_put(&dst_path);
836out:
Namjae Jeonf4415842021-03-16 10:50:04 +0900837 dput(src_dent_parent);
838 return err;
839}
840
841/**
842 * ksmbd_vfs_truncate() - vfs helper for smb file truncate
843 * @work: work
Namjae Jeonf4415842021-03-16 10:50:04 +0900844 * @fid: file id of old file
845 * @size: truncate to given size
846 *
847 * Return: 0 on success, otherwise error
848 */
Hyunchul Lee265fd192021-09-25 00:06:16 +0900849int ksmbd_vfs_truncate(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +0900850 struct ksmbd_file *fp, loff_t size)
Namjae Jeonf4415842021-03-16 10:50:04 +0900851{
Namjae Jeonf4415842021-03-16 10:50:04 +0900852 int err = 0;
Hyunchul Lee265fd192021-09-25 00:06:16 +0900853 struct file *filp;
Namjae Jeonf4415842021-03-16 10:50:04 +0900854
Hyunchul Lee265fd192021-09-25 00:06:16 +0900855 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 Jeonf4415842021-03-16 10:50:04 +0900871 if (err) {
Hyunchul Lee265fd192021-09-25 00:06:16 +0900872 pr_err("failed due to lock\n");
873 return -EAGAIN;
Namjae Jeonf4415842021-03-16 10:50:04 +0900874 }
Namjae Jeonf4415842021-03-16 10:50:04 +0900875 }
876
Hyunchul Lee265fd192021-09-25 00:06:16 +0900877 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 Jeonf4415842021-03-16 10:50:04 +0900881 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 */
892ssize_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 Jeon79f6b112021-04-02 12:47:14 +0900901 vlist = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
Namjae Jeonf4415842021-03-16 10:50:04 +0900902 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 Jeon79f6b112021-04-02 12:47:14 +0900909 kvfree(vlist);
Namjae Jeonf4415842021-03-16 10:50:04 +0900910 *list = NULL;
911 }
912
913 return size;
914}
915
Hyunchul Leeaf349832021-06-30 18:25:53 +0900916static ssize_t ksmbd_vfs_xattr_len(struct user_namespace *user_ns,
917 struct dentry *dentry, char *xattr_name)
Namjae Jeonf4415842021-03-16 10:50:04 +0900918{
Hyunchul Leeaf349832021-06-30 18:25:53 +0900919 return vfs_getxattr(user_ns, dentry, xattr_name, NULL, 0);
Namjae Jeonf4415842021-03-16 10:50:04 +0900920}
921
922/**
923 * ksmbd_vfs_getxattr() - vfs helper for smb get extended attributes value
Hyunchul Leeaf349832021-06-30 18:25:53 +0900924 * @user_ns: user namespace
Namjae Jeonf4415842021-03-16 10:50:04 +0900925 * @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 Leeaf349832021-06-30 18:25:53 +0900931ssize_t ksmbd_vfs_getxattr(struct user_namespace *user_ns,
932 struct dentry *dentry,
933 char *xattr_name, char **xattr_buf)
Namjae Jeonf4415842021-03-16 10:50:04 +0900934{
935 ssize_t xattr_len;
936 char *buf;
937
938 *xattr_buf = NULL;
Hyunchul Leeaf349832021-06-30 18:25:53 +0900939 xattr_len = ksmbd_vfs_xattr_len(user_ns, dentry, xattr_name);
Namjae Jeonf4415842021-03-16 10:50:04 +0900940 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 Leeaf349832021-06-30 18:25:53 +0900947 xattr_len = vfs_getxattr(user_ns, dentry, xattr_name,
Hyunchul Leed7e58522021-05-29 09:59:59 +0900948 (void *)buf, xattr_len);
Namjae Jeonf4415842021-03-16 10:50:04 +0900949 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 Leeaf349832021-06-30 18:25:53 +0900958 * @user_ns: user namespace
Namjae Jeonf4415842021-03-16 10:50:04 +0900959 * @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 Leeaf349832021-06-30 18:25:53 +0900967int ksmbd_vfs_setxattr(struct user_namespace *user_ns,
968 struct dentry *dentry, const char *attr_name,
Namjae Jeon070fb212021-05-26 17:57:12 +0900969 const void *attr_value, size_t attr_size, int flags)
Namjae Jeonf4415842021-03-16 10:50:04 +0900970{
971 int err;
972
Hyunchul Leeaf349832021-06-30 18:25:53 +0900973 err = vfs_setxattr(user_ns,
974 dentry,
Namjae Jeonf4415842021-03-16 10:50:04 +0900975 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 */
989void 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 Jeon64b39f42021-03-30 14:25:35 +0900998 if (option & FILE_WRITE_THROUGH_LE) {
Namjae Jeonf4415842021-03-16 10:50:04 +0900999 filp->f_flags |= O_SYNC;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001000 } else if (option & FILE_SEQUENTIAL_ONLY_LE) {
Namjae Jeonf4415842021-03-16 10:50:04 +09001001 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 Jeon64b39f42021-03-30 14:25:35 +09001012int ksmbd_vfs_zero_data(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09001013 loff_t off, loff_t len)
Namjae Jeonf4415842021-03-16 10:50:04 +09001014{
1015 smb_break_all_levII_oplock(work, fp, 1);
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09001016 if (fp->f_ci->m_fattr & FILE_ATTRIBUTE_SPARSE_FILE_LE)
Namjae Jeonf4415842021-03-16 10:50:04 +09001017 return vfs_fallocate(fp->filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09001018 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1019 off, len);
Namjae Jeonf4415842021-03-16 10:50:04 +09001020
1021 return vfs_fallocate(fp->filp, FALLOC_FL_ZERO_RANGE, off, len);
1022}
1023
1024int ksmbd_vfs_fqar_lseek(struct ksmbd_file *fp, loff_t start, loff_t length,
Namjae Jeon070fb212021-05-26 17:57:12 +09001025 struct file_allocated_range_buffer *ranges,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09001026 unsigned int in_count, unsigned int *out_count)
Namjae Jeonf4415842021-03-16 10:50:04 +09001027{
1028 struct file *f = fp->filp;
Namjae Jeonab0b2632021-06-29 09:20:13 +09001029 struct inode *inode = file_inode(fp->filp);
Namjae Jeonf4415842021-03-16 10:50:04 +09001030 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 Jeon64b39f42021-03-30 14:25:35 +09001067 } else if (extent_start >= extent_end) {
Namjae Jeonf4415842021-03-16 10:50:04 +09001068 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001069 }
Namjae Jeonf4415842021-03-16 10:50:04 +09001070
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 Leeaf349832021-06-30 18:25:53 +09001081int ksmbd_vfs_remove_xattr(struct user_namespace *user_ns,
1082 struct dentry *dentry, char *attr_name)
Namjae Jeonf4415842021-03-16 10:50:04 +09001083{
Hyunchul Leeaf349832021-06-30 18:25:53 +09001084 return vfs_removexattr(user_ns, dentry, attr_name);
Namjae Jeonf4415842021-03-16 10:50:04 +09001085}
1086
Hyunchul Leeaf349832021-06-30 18:25:53 +09001087int ksmbd_vfs_unlink(struct user_namespace *user_ns,
1088 struct dentry *dir, struct dentry *dentry)
Namjae Jeonf4415842021-03-16 10:50:04 +09001089{
1090 int err = 0;
1091
Christian Braunerda1e7ad2021-08-23 17:13:47 +02001092 err = ksmbd_vfs_lock_parent(user_ns, dir, dentry);
Hyunchul Lee333111a2021-06-23 11:07:43 +09001093 if (err)
1094 return err;
Namjae Jeonff1d5722021-04-13 13:18:10 +09001095 dget(dentry);
Namjae Jeonff1d5722021-04-13 13:18:10 +09001096
Namjae Jeonf4415842021-03-16 10:50:04 +09001097 if (S_ISDIR(d_inode(dentry)->i_mode))
Hyunchul Leeaf349832021-06-30 18:25:53 +09001098 err = vfs_rmdir(user_ns, d_inode(dir), dentry);
Namjae Jeonf4415842021-03-16 10:50:04 +09001099 else
Hyunchul Leeaf349832021-06-30 18:25:53 +09001100 err = vfs_unlink(user_ns, d_inode(dir), dentry, NULL);
Namjae Jeonf4415842021-03-16 10:50:04 +09001101
Namjae Jeonf4415842021-03-16 10:50:04 +09001102 dput(dentry);
Namjae Jeonff1d5722021-04-13 13:18:10 +09001103 inode_unlock(d_inode(dir));
Namjae Jeonf4415842021-03-16 10:50:04 +09001104 if (err)
1105 ksmbd_debug(VFS, "failed to delete, err %d\n", err);
1106
1107 return err;
1108}
1109
Namjae Jeon64b39f42021-03-30 14:25:35 +09001110static int __dir_empty(struct dir_context *ctx, const char *name, int namlen,
Namjae Jeon070fb212021-05-26 17:57:12 +09001111 loff_t offset, u64 ino, unsigned int d_type)
Namjae Jeonf4415842021-03-16 10:50:04 +09001112{
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 */
1129int 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 Jeone8c06192021-06-22 11:06:11 +09001139 err = iterate_dir(fp->filp, &readdir_data.ctx);
Namjae Jeonf4415842021-03-16 10:50:04 +09001140 if (readdir_data.dirent_count > 2)
1141 err = -ENOTEMPTY;
1142 else
1143 err = 0;
1144 return err;
1145}
1146
Namjae Jeon64b39f42021-03-30 14:25:35 +09001147static int __caseless_lookup(struct dir_context *ctx, const char *name,
Namjae Jeon070fb212021-05-26 17:57:12 +09001148 int namlen, loff_t offset, u64 ino,
1149 unsigned int d_type)
Namjae Jeonf4415842021-03-16 10:50:04 +09001150{
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 Lee3c203782021-04-13 13:22:31 +09001167 * @dir: path info
1168 * @name: filename to lookup
1169 * @namelen: filename length
Namjae Jeonf4415842021-03-16 10:50:04 +09001170 *
1171 * Return: 0 on success, otherwise error
1172 */
Hyunchul Lee3c203782021-04-13 13:22:31 +09001173static int ksmbd_vfs_lookup_in_dir(struct path *dir, char *name, size_t namelen)
Namjae Jeonf4415842021-03-16 10:50:04 +09001174{
Namjae Jeonf4415842021-03-16 10:50:04 +09001175 int ret;
1176 struct file *dfilp;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001177 int flags = O_RDONLY | O_LARGEFILE;
Namjae Jeonf4415842021-03-16 10:50:04 +09001178 struct ksmbd_readdir_data readdir_data = {
1179 .ctx.actor = __caseless_lookup,
Hyunchul Lee3c203782021-04-13 13:22:31 +09001180 .private = name,
1181 .used = namelen,
1182 .dirent_count = 0,
Namjae Jeonf4415842021-03-16 10:50:04 +09001183 };
1184
Hyunchul Lee3c203782021-04-13 13:22:31 +09001185 dfilp = dentry_open(dir, flags, current_cred());
1186 if (IS_ERR(dfilp))
1187 return PTR_ERR(dfilp);
Namjae Jeonf4415842021-03-16 10:50:04 +09001188
Namjae Jeone8c06192021-06-22 11:06:11 +09001189 ret = iterate_dir(dfilp, &readdir_data.ctx);
Namjae Jeonf4415842021-03-16 10:50:04 +09001190 if (readdir_data.dirent_count > 0)
1191 ret = 0;
Namjae Jeonf4415842021-03-16 10:50:04 +09001192 fput(dfilp);
Namjae Jeonf4415842021-03-16 10:50:04 +09001193 return ret;
1194}
1195
1196/**
1197 * ksmbd_vfs_kern_path() - lookup a file and get path info
Hyunchul Lee265fd192021-09-25 00:06:16 +09001198 * @name: file path that is relative to share
Namjae Jeonf4415842021-03-16 10:50:04 +09001199 * @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 Lee265fd192021-09-25 00:06:16 +09001205int ksmbd_vfs_kern_path(struct ksmbd_work *work, char *name,
1206 unsigned int flags, struct path *path, bool caseless)
Namjae Jeonf4415842021-03-16 10:50:04 +09001207{
Hyunchul Lee265fd192021-09-25 00:06:16 +09001208 struct ksmbd_share_config *share_conf = work->tcon->share_conf;
Namjae Jeonf4415842021-03-16 10:50:04 +09001209 int err;
1210
Hyunchul Lee265fd192021-09-25 00:06:16 +09001211 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 Jeonf4415842021-03-16 10:50:04 +09001217 if (!err)
Hyunchul Lee3c203782021-04-13 13:22:31 +09001218 return 0;
Namjae Jeonf4415842021-03-16 10:50:04 +09001219
1220 if (caseless) {
Hyunchul Lee3c203782021-04-13 13:22:31 +09001221 char *filepath;
1222 struct path parent;
1223 size_t path_len, remain_len;
Namjae Jeonf4415842021-03-16 10:50:04 +09001224
Hyunchul Lee3c203782021-04-13 13:22:31 +09001225 filepath = kstrdup(name, GFP_KERNEL);
1226 if (!filepath)
1227 return -ENOMEM;
Namjae Jeonf4415842021-03-16 10:50:04 +09001228
Hyunchul Lee3c203782021-04-13 13:22:31 +09001229 path_len = strlen(filepath);
Hyunchul Lee265fd192021-09-25 00:06:16 +09001230 remain_len = path_len;
Hyunchul Lee3c203782021-04-13 13:22:31 +09001231
Hyunchul Lee265fd192021-09-25 00:06:16 +09001232 parent = share_conf->vfs_path;
1233 path_get(&parent);
Namjae Jeonf4415842021-03-16 10:50:04 +09001234
Hyunchul Lee3c203782021-04-13 13:22:31 +09001235 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 Lee3c203782021-04-13 13:22:31 +09001246 path_put(&parent);
Hyunchul Lee3c203782021-04-13 13:22:31 +09001247 if (err)
1248 goto out;
1249
Hyunchul Lee265fd192021-09-25 00:06:16 +09001250 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 Lee3c203782021-04-13 13:22:31 +09001261 goto out;
1262 }
1263
1264 next[0] = '/';
1265 remain_len -= filename_len + 1;
1266 }
1267
1268 path_put(&parent);
1269 err = -EINVAL;
Namjae Jeonf4415842021-03-16 10:50:04 +09001270out:
Hyunchul Lee3c203782021-04-13 13:22:31 +09001271 kfree(filepath);
1272 }
Namjae Jeonf4415842021-03-16 10:50:04 +09001273 return err;
1274}
1275
Hyunchul Lee265fd192021-09-25 00:06:16 +09001276struct 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 Leeaf349832021-06-30 18:25:53 +09001293int ksmbd_vfs_remove_acl_xattrs(struct user_namespace *user_ns,
1294 struct dentry *dentry)
Namjae Jeonf4415842021-03-16 10:50:04 +09001295{
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 Jeon070fb212021-05-26 17:57:12 +09001309 name += strlen(name) + 1) {
Namjae Jeonf4415842021-03-16 10:50:04 +09001310 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
1311
1312 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001313 sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1) ||
Namjae Jeonf4415842021-03-16 10:50:04 +09001314 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001315 sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1)) {
Hyunchul Leeaf349832021-06-30 18:25:53 +09001316 err = ksmbd_vfs_remove_xattr(user_ns, dentry, name);
Namjae Jeonf4415842021-03-16 10:50:04 +09001317 if (err)
1318 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001319 "remove acl xattr failed : %s\n", name);
Namjae Jeonf4415842021-03-16 10:50:04 +09001320 }
1321 }
1322out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09001323 kvfree(xattr_list);
Namjae Jeonf4415842021-03-16 10:50:04 +09001324 return err;
1325}
1326
Hyunchul Leeaf349832021-06-30 18:25:53 +09001327int ksmbd_vfs_remove_sd_xattrs(struct user_namespace *user_ns,
1328 struct dentry *dentry)
Namjae Jeonf4415842021-03-16 10:50:04 +09001329{
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 Leeaf349832021-06-30 18:25:53 +09001347 err = ksmbd_vfs_remove_xattr(user_ns, dentry, name);
Namjae Jeonf4415842021-03-16 10:50:04 +09001348 if (err)
1349 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
1350 }
1351 }
1352out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09001353 kvfree(xattr_list);
Namjae Jeonf4415842021-03-16 10:50:04 +09001354 return err;
1355}
1356
Hyunchul Leeaf349832021-06-30 18:25:53 +09001357static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct user_namespace *user_ns,
1358 struct inode *inode,
Namjae Jeon070fb212021-05-26 17:57:12 +09001359 int acl_type)
Namjae Jeonf4415842021-03-16 10:50:04 +09001360{
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 Jeon777cad12021-08-13 08:15:33 +09001367 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1368 return NULL;
1369
Namjae Jeon67d1c432021-06-22 11:42:29 +09001370 posix_acls = get_acl(inode, acl_type);
Namjae Jeonf4415842021-03-16 10:50:04 +09001371 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 Brauner0e844ef2021-08-23 17:13:51 +02001387 xa_entry->uid = posix_acl_uid_translate(user_ns, pa_entry);
Namjae Jeonf4415842021-03-16 10:50:04 +09001388 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 Brauner0e844ef2021-08-23 17:13:51 +02001394 xa_entry->gid = posix_acl_gid_translate(user_ns, pa_entry);
Namjae Jeonf4415842021-03-16 10:50:04 +09001395 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 Jeonbde16942021-06-28 15:23:19 +09001406 pr_err("unknown type : 0x%x\n", pa_entry->e_tag);
Namjae Jeonf4415842021-03-16 10:50:04 +09001407 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 }
1417out:
1418 posix_acl_release(posix_acls);
1419 return smb_acl;
1420}
1421
Hyunchul Leeaf349832021-06-30 18:25:53 +09001422int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
1423 struct user_namespace *user_ns,
1424 struct dentry *dentry,
Namjae Jeon070fb212021-05-26 17:57:12 +09001425 struct smb_ntsd *pntsd, int len)
Namjae Jeonf4415842021-03-16 10:50:04 +09001426{
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 Jeonfba08fa2021-04-15 10:29:39 +09001431 struct inode *inode = d_inode(dentry);
Namjae Jeonf4415842021-03-16 10:50:04 +09001432
1433 acl.version = 4;
1434 acl.hash_type = XATTR_SD_HASH_TYPE_SHA256;
Namjae Jeonfba08fa2021-04-15 10:29:39 +09001435 acl.current_time = ksmbd_UnixTimeToNT(current_time(inode));
Namjae Jeonf4415842021-03-16 10:50:04 +09001436
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 Jeonbde16942021-06-28 15:23:19 +09001452 pr_err("failed to generate hash for ndr acl\n");
Namjae Jeonf4415842021-03-16 10:50:04 +09001453 return rc;
1454 }
1455
Hyunchul Leeaf349832021-06-30 18:25:53 +09001456 smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
1457 ACL_TYPE_ACCESS);
Namjae Jeonf4415842021-03-16 10:50:04 +09001458 if (S_ISDIR(inode->i_mode))
Hyunchul Leeaf349832021-06-30 18:25:53 +09001459 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
Namjae Jeon070fb212021-05-26 17:57:12 +09001460 ACL_TYPE_DEFAULT);
Namjae Jeonf4415842021-03-16 10:50:04 +09001461
Hyunchul Leeaf349832021-06-30 18:25:53 +09001462 rc = ndr_encode_posix_acl(&acl_ndr, user_ns, inode,
1463 smb_acl, def_smb_acl);
Namjae Jeonf4415842021-03-16 10:50:04 +09001464 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001465 pr_err("failed to encode ndr to posix acl\n");
Namjae Jeonf4415842021-03-16 10:50:04 +09001466 goto out;
1467 }
1468
1469 rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset,
Namjae Jeon070fb212021-05-26 17:57:12 +09001470 acl.posix_acl_hash);
Namjae Jeonf4415842021-03-16 10:50:04 +09001471 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001472 pr_err("failed to generate hash for ndr acl\n");
Namjae Jeonf4415842021-03-16 10:50:04 +09001473 goto out;
1474 }
1475
1476 rc = ndr_encode_v4_ntacl(&sd_ndr, &acl);
1477 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001478 pr_err("failed to encode ndr to posix acl\n");
Namjae Jeonf4415842021-03-16 10:50:04 +09001479 goto out;
1480 }
1481
Hyunchul Leeaf349832021-06-30 18:25:53 +09001482 rc = ksmbd_vfs_setxattr(user_ns, dentry,
1483 XATTR_NAME_SD, sd_ndr.data,
Namjae Jeon070fb212021-05-26 17:57:12 +09001484 sd_ndr.offset, 0);
Namjae Jeonf4415842021-03-16 10:50:04 +09001485 if (rc < 0)
Namjae Jeonbde16942021-06-28 15:23:19 +09001486 pr_err("Failed to store XATTR ntacl :%d\n", rc);
Namjae Jeonf4415842021-03-16 10:50:04 +09001487
1488 kfree(sd_ndr.data);
1489out:
1490 kfree(acl_ndr.data);
1491 kfree(smb_acl);
1492 kfree(def_smb_acl);
1493 return rc;
1494}
1495
Hyunchul Leeaf349832021-06-30 18:25:53 +09001496int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
1497 struct user_namespace *user_ns,
1498 struct dentry *dentry,
Namjae Jeon070fb212021-05-26 17:57:12 +09001499 struct smb_ntsd **pntsd)
Namjae Jeonf4415842021-03-16 10:50:04 +09001500{
1501 int rc;
1502 struct ndr n;
Namjae Jeon78ad2c22021-07-13 17:12:41 +09001503 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 Jeonf4415842021-03-16 10:50:04 +09001508
Hyunchul Leeaf349832021-06-30 18:25:53 +09001509 rc = ksmbd_vfs_getxattr(user_ns, dentry, XATTR_NAME_SD, &n.data);
Namjae Jeon78ad2c22021-07-13 17:12:41 +09001510 if (rc <= 0)
1511 return rc;
Namjae Jeonf4415842021-03-16 10:50:04 +09001512
Namjae Jeon78ad2c22021-07-13 17:12:41 +09001513 n.length = rc;
1514 rc = ndr_decode_v4_ntacl(&n, &acl);
1515 if (rc)
1516 goto free_n_data;
Namjae Jeonf4415842021-03-16 10:50:04 +09001517
Namjae Jeon78ad2c22021-07-13 17:12:41 +09001518 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 Jeonf4415842021-03-16 10:50:04 +09001523
Namjae Jeon78ad2c22021-07-13 17:12:41 +09001524 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 Jeonf4415842021-03-16 10:50:04 +09001529 }
1530
Namjae Jeon78ad2c22021-07-13 17:12:41 +09001531 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;
1552out_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
1561free_n_data:
1562 kfree(n.data);
Namjae Jeonf4415842021-03-16 10:50:04 +09001563 return rc;
1564}
1565
Hyunchul Leeaf349832021-06-30 18:25:53 +09001566int ksmbd_vfs_set_dos_attrib_xattr(struct user_namespace *user_ns,
1567 struct dentry *dentry,
Namjae Jeon070fb212021-05-26 17:57:12 +09001568 struct xattr_dos_attrib *da)
Namjae Jeonf4415842021-03-16 10:50:04 +09001569{
1570 struct ndr n;
1571 int err;
1572
1573 err = ndr_encode_dos_attr(&n, da);
1574 if (err)
1575 return err;
1576
Hyunchul Leeaf349832021-06-30 18:25:53 +09001577 err = ksmbd_vfs_setxattr(user_ns, dentry, XATTR_NAME_DOS_ATTRIBUTE,
Namjae Jeon070fb212021-05-26 17:57:12 +09001578 (void *)n.data, n.offset, 0);
Namjae Jeonf4415842021-03-16 10:50:04 +09001579 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 Leeaf349832021-06-30 18:25:53 +09001586int ksmbd_vfs_get_dos_attrib_xattr(struct user_namespace *user_ns,
1587 struct dentry *dentry,
Namjae Jeon070fb212021-05-26 17:57:12 +09001588 struct xattr_dos_attrib *da)
Namjae Jeonf4415842021-03-16 10:50:04 +09001589{
1590 struct ndr n;
1591 int err;
1592
Hyunchul Leeaf349832021-06-30 18:25:53 +09001593 err = ksmbd_vfs_getxattr(user_ns, dentry, XATTR_NAME_DOS_ATTRIBUTE,
Namjae Jeon070fb212021-05-26 17:57:12 +09001594 (char **)&n.data);
Namjae Jeonf4415842021-03-16 10:50:04 +09001595 if (err > 0) {
1596 n.length = err;
1597 if (ndr_decode_dos_attr(&n, da))
1598 err = -EINVAL;
Namjae Jeon79f6b112021-04-02 12:47:14 +09001599 kfree(n.data);
Namjae Jeon64b39f42021-03-30 14:25:35 +09001600 } else {
Namjae Jeonf4415842021-03-16 10:50:04 +09001601 ksmbd_debug(SMB, "failed to load dos attribute in xattr\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +09001602 }
Namjae Jeonf4415842021-03-16 10:50:04 +09001603
1604 return err;
1605}
1606
Namjae Jeonf4415842021-03-16 10:50:04 +09001607/**
1608 * ksmbd_vfs_init_kstat() - convert unix stat information to smb stat format
1609 * @p: destination buffer
1610 * @ksmbd_kstat: ksmbd kstat wrapper
1611 */
1612void *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 Sahlberg26a27872021-11-03 08:45:52 +09001627 if (ksmbd_kstat->file_attributes & FILE_ATTRIBUTE_DIRECTORY_LE) {
Namjae Jeonf4415842021-03-16 10:50:04 +09001628 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 Leeaf349832021-06-30 18:25:53 +09001639int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work,
1640 struct user_namespace *user_ns,
1641 struct dentry *dentry,
Namjae Jeon070fb212021-05-26 17:57:12 +09001642 struct ksmbd_kstat *ksmbd_kstat)
Namjae Jeonf4415842021-03-16 10:50:04 +09001643{
1644 u64 time;
1645 int rc;
1646
Hyunchul Leeaf349832021-06-30 18:25:53 +09001647 generic_fillattr(user_ns, d_inode(dentry), ksmbd_kstat->kstat);
Namjae Jeonf4415842021-03-16 10:50:04 +09001648
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 Sahlberg26a27872021-11-03 08:45:52 +09001657 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_DIRECTORY_LE;
Namjae Jeonf4415842021-03-16 10:50:04 +09001658 else
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09001659 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_ARCHIVE_LE;
Namjae Jeonf4415842021-03-16 10:50:04 +09001660
1661 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001662 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
Namjae Jeonf4415842021-03-16 10:50:04 +09001663 struct xattr_dos_attrib da;
1664
Hyunchul Leeaf349832021-06-30 18:25:53 +09001665 rc = ksmbd_vfs_get_dos_attrib_xattr(user_ns, dentry, &da);
Namjae Jeonf4415842021-03-16 10:50:04 +09001666 if (rc > 0) {
1667 ksmbd_kstat->file_attributes = cpu_to_le32(da.attr);
1668 ksmbd_kstat->create_time = da.create_time;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001669 } else {
Namjae Jeonf4415842021-03-16 10:50:04 +09001670 ksmbd_debug(VFS, "fail to load dos attribute.\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +09001671 }
Namjae Jeonf4415842021-03-16 10:50:04 +09001672 }
1673
1674 return 0;
1675}
1676
Hyunchul Leeaf349832021-06-30 18:25:53 +09001677ssize_t ksmbd_vfs_casexattr_len(struct user_namespace *user_ns,
1678 struct dentry *dentry, char *attr_name,
Namjae Jeon070fb212021-05-26 17:57:12 +09001679 int attr_name_len)
Namjae Jeonf4415842021-03-16 10:50:04 +09001680{
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 Leeaf349832021-06-30 18:25:53 +09001694 value_len = ksmbd_vfs_xattr_len(user_ns, dentry, name);
Namjae Jeonf4415842021-03-16 10:50:04 +09001695 break;
1696 }
1697
1698out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09001699 kvfree(xattr_list);
Namjae Jeonf4415842021-03-16 10:50:04 +09001700 return value_len;
1701}
1702
Namjae Jeon64b39f42021-03-30 14:25:35 +09001703int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name,
Namjae Jeon070fb212021-05-26 17:57:12 +09001704 size_t *xattr_stream_name_size, int s_type)
Namjae Jeonf4415842021-03-16 10:50:04 +09001705{
Dan Carpenter07781de2021-07-07 13:15:32 +03001706 char *type, *buf;
Namjae Jeonf4415842021-03-16 10:50:04 +09001707
1708 if (s_type == DIR_STREAM)
1709 type = ":$INDEX_ALLOCATION";
1710 else
1711 type = ":$DATA";
1712
Dan Carpenter07781de2021-07-07 13:15:32 +03001713 buf = kasprintf(GFP_KERNEL, "%s%s%s",
1714 XATTR_NAME_STREAM, stream_name, type);
1715 if (!buf)
Namjae Jeonf4415842021-03-16 10:50:04 +09001716 return -ENOMEM;
1717
Dan Carpenter07781de2021-07-07 13:15:32 +03001718 *xattr_stream_name = buf;
1719 *xattr_stream_name_size = strlen(buf) + 1;
Namjae Jeonf4415842021-03-16 10:50:04 +09001720
1721 return 0;
1722}
1723
Namjae Jeonf4415842021-03-16 10:50:04 +09001724int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09001725 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 Jeonf4415842021-03-16 10:50:04 +09001732{
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 Jeon493fa2f2021-06-29 09:22:16 +09001743 pr_err("no right to read(%pd)\n", src_fp->filp->f_path.dentry);
Namjae Jeonf4415842021-03-16 10:50:04 +09001744 return -EACCES;
1745 }
1746 if (!(dst_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) {
Namjae Jeon493fa2f2021-06-29 09:22:16 +09001747 pr_err("no right to write(%pd)\n", dst_fp->filp->f_path.dentry);
Namjae Jeonf4415842021-03-16 10:50:04 +09001748 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 Jeonc36fca82021-03-30 12:43:18 +09001756 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 Jeonf4415842021-03-16 10:50:04 +09001761
Namjae Jeonc36fca82021-03-30 12:43:18 +09001762 if (check_lock_range(src_fp->filp, src_off,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001763 src_off + len - 1, READ))
Namjae Jeonc36fca82021-03-30 12:43:18 +09001764 return -EAGAIN;
1765 if (check_lock_range(dst_fp->filp, dst_off,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001766 dst_off + len - 1, WRITE))
Namjae Jeonc36fca82021-03-30 12:43:18 +09001767 return -EAGAIN;
1768 }
Namjae Jeonf4415842021-03-16 10:50:04 +09001769 }
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 Jeonf8524772021-06-18 10:28:00 +09001781 ret = vfs_copy_file_range(src_fp->filp, src_off,
1782 dst_fp->filp, dst_off, len, 0);
Namjae Jeonf4415842021-03-16 10:50:04 +09001783 if (ret < 0)
1784 return ret;
1785
1786 *chunk_count_written += 1;
1787 *total_size_written += ret;
1788 }
1789 return 0;
1790}
1791
Hyunchul Lee45a64e82021-07-10 09:34:20 +09001792void ksmbd_vfs_posix_lock_wait(struct file_lock *flock)
Namjae Jeonf4415842021-03-16 10:50:04 +09001793{
Hyunchul Lee45a64e82021-07-10 09:34:20 +09001794 wait_event(flock->fl_wait, !flock->fl_blocker);
Namjae Jeonf4415842021-03-16 10:50:04 +09001795}
1796
1797int 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
1804void ksmbd_vfs_posix_lock_unblock(struct file_lock *flock)
1805{
1806 locks_delete_block(flock);
1807}
1808
Hyunchul Leeaf349832021-06-30 18:25:53 +09001809int ksmbd_vfs_set_init_posix_acl(struct user_namespace *user_ns,
1810 struct inode *inode)
Namjae Jeonf4415842021-03-16 10:50:04 +09001811{
1812 struct posix_acl_state acl_state;
1813 struct posix_acl *acls;
1814 int rc;
1815
Namjae Jeon777cad12021-08-13 08:15:33 +09001816 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1817 return -EOPNOTSUPP;
1818
Namjae Jeonf4415842021-03-16 10:50:04 +09001819 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 Jeon67d1c432021-06-22 11:42:29 +09001836 acls = posix_acl_alloc(6, GFP_KERNEL);
Namjae Jeonf4415842021-03-16 10:50:04 +09001837 if (!acls) {
1838 free_acl_state(&acl_state);
1839 return -ENOMEM;
1840 }
1841 posix_state_to_acl(&acl_state, acls->a_entries);
Hyunchul Leeaf349832021-06-30 18:25:53 +09001842 rc = set_posix_acl(user_ns, inode, ACL_TYPE_ACCESS, acls);
Namjae Jeonf4415842021-03-16 10:50:04 +09001843 if (rc < 0)
1844 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001845 rc);
Namjae Jeonf4415842021-03-16 10:50:04 +09001846 else if (S_ISDIR(inode->i_mode)) {
1847 posix_state_to_acl(&acl_state, acls->a_entries);
Hyunchul Leeaf349832021-06-30 18:25:53 +09001848 rc = set_posix_acl(user_ns, inode, ACL_TYPE_DEFAULT,
Namjae Jeon67d1c432021-06-22 11:42:29 +09001849 acls);
Namjae Jeonf4415842021-03-16 10:50:04 +09001850 if (rc < 0)
1851 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001852 rc);
Namjae Jeonf4415842021-03-16 10:50:04 +09001853 }
1854 free_acl_state(&acl_state);
1855 posix_acl_release(acls);
1856 return rc;
1857}
1858
Hyunchul Leeaf349832021-06-30 18:25:53 +09001859int ksmbd_vfs_inherit_posix_acl(struct user_namespace *user_ns,
1860 struct inode *inode, struct inode *parent_inode)
Namjae Jeonf4415842021-03-16 10:50:04 +09001861{
1862 struct posix_acl *acls;
1863 struct posix_acl_entry *pace;
1864 int rc, i;
1865
Namjae Jeon777cad12021-08-13 08:15:33 +09001866 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
1867 return -EOPNOTSUPP;
1868
Namjae Jeon67d1c432021-06-22 11:42:29 +09001869 acls = get_acl(parent_inode, ACL_TYPE_DEFAULT);
Namjae Jeonf4415842021-03-16 10:50:04 +09001870 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 Leeaf349832021-06-30 18:25:53 +09001881 rc = set_posix_acl(user_ns, inode, ACL_TYPE_ACCESS, acls);
Namjae Jeonf4415842021-03-16 10:50:04 +09001882 if (rc < 0)
1883 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001884 rc);
Namjae Jeonf4415842021-03-16 10:50:04 +09001885 if (S_ISDIR(inode->i_mode)) {
Hyunchul Leeaf349832021-06-30 18:25:53 +09001886 rc = set_posix_acl(user_ns, inode, ACL_TYPE_DEFAULT,
Namjae Jeon67d1c432021-06-22 11:42:29 +09001887 acls);
Namjae Jeonf4415842021-03-16 10:50:04 +09001888 if (rc < 0)
1889 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001890 rc);
Namjae Jeonf4415842021-03-16 10:50:04 +09001891 }
1892 posix_acl_release(acls);
1893 return rc;
1894}