2021-03-16 09:50:04 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
|
|
|
|
* Copyright (C) 2018 Samsung Electronics Co., Ltd.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/backing-dev.h>
|
|
|
|
#include <linux/writeback.h>
|
|
|
|
#include <linux/xattr.h>
|
|
|
|
#include <linux/falloc.h>
|
|
|
|
#include <linux/fsnotify.h>
|
|
|
|
#include <linux/dcache.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/sched/xacct.h>
|
|
|
|
#include <linux/crc32c.h>
|
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
#include "../internal.h" /* for vfs_path_lookup */
|
|
|
|
|
2021-03-16 09:50:04 +08:00
|
|
|
#include "glob.h"
|
|
|
|
#include "oplock.h"
|
|
|
|
#include "connection.h"
|
|
|
|
#include "vfs.h"
|
|
|
|
#include "vfs_cache.h"
|
|
|
|
#include "smbacl.h"
|
|
|
|
#include "ndr.h"
|
|
|
|
#include "auth.h"
|
2021-04-14 08:24:11 +08:00
|
|
|
#include "misc.h"
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
#include "smb_common.h"
|
|
|
|
#include "mgmt/share_config.h"
|
|
|
|
#include "mgmt/tree_connect.h"
|
|
|
|
#include "mgmt/user_session.h"
|
|
|
|
#include "mgmt/user_config.h"
|
|
|
|
|
|
|
|
static char *extract_last_component(char *path)
|
|
|
|
{
|
|
|
|
char *p = strrchr(path, '/');
|
|
|
|
|
|
|
|
if (p && p[1] != '\0') {
|
|
|
|
*p = '\0';
|
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ksmbd_vfs_inherit_owner(struct ksmbd_work *work,
|
2021-05-26 16:57:12 +08:00
|
|
|
struct inode *parent_inode,
|
|
|
|
struct inode *inode)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
if (!test_share_config_flag(work->tcon->share_conf,
|
2021-03-30 13:25:35 +08:00
|
|
|
KSMBD_SHARE_FLAG_INHERIT_OWNER))
|
2021-03-16 09:50:04 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
i_uid_write(inode, i_uid_read(parent_inode));
|
|
|
|
}
|
|
|
|
|
2021-06-23 10:07:43 +08:00
|
|
|
/**
|
|
|
|
* ksmbd_vfs_lock_parent() - lock parent dentry if it is stable
|
|
|
|
*
|
|
|
|
* the parent dentry got by dget_parent or @parent could be
|
|
|
|
* unstable, we try to lock a parent inode and lookup the
|
|
|
|
* child dentry again.
|
|
|
|
*
|
|
|
|
* the reference count of @parent isn't incremented.
|
|
|
|
*/
|
2021-08-23 23:13:47 +08:00
|
|
|
int ksmbd_vfs_lock_parent(struct user_namespace *user_ns, struct dentry *parent,
|
|
|
|
struct dentry *child)
|
2021-06-23 10:07:43 +08:00
|
|
|
{
|
|
|
|
struct dentry *dentry;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
|
2021-08-23 23:13:47 +08:00
|
|
|
dentry = lookup_one(user_ns, child->d_name.name, parent,
|
|
|
|
child->d_name.len);
|
2021-06-23 10:07:43 +08:00
|
|
|
if (IS_ERR(dentry)) {
|
|
|
|
ret = PTR_ERR(dentry);
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dentry != child) {
|
|
|
|
ret = -ESTALE;
|
|
|
|
dput(dentry);
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
dput(dentry);
|
|
|
|
return 0;
|
|
|
|
out_err:
|
|
|
|
inode_unlock(d_inode(parent));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_may_delete(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
2021-06-23 12:48:24 +08:00
|
|
|
struct dentry *parent;
|
|
|
|
int ret;
|
2021-06-23 10:07:43 +08:00
|
|
|
|
2021-06-23 12:48:24 +08:00
|
|
|
parent = dget_parent(dentry);
|
2021-08-23 23:13:47 +08:00
|
|
|
ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
|
2021-06-23 12:48:24 +08:00
|
|
|
if (ret) {
|
2021-03-16 09:50:04 +08:00
|
|
|
dput(parent);
|
2021-06-23 12:48:24 +08:00
|
|
|
return ret;
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
2021-06-23 12:48:24 +08:00
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
ret = inode_permission(user_ns, d_inode(parent),
|
2021-06-23 12:48:24 +08:00
|
|
|
MAY_EXEC | MAY_WRITE);
|
|
|
|
|
|
|
|
inode_unlock(d_inode(parent));
|
|
|
|
dput(parent);
|
2021-04-13 12:18:10 +08:00
|
|
|
return ret;
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_query_maximal_access(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry, __le32 *daccess)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
2021-06-23 10:07:43 +08:00
|
|
|
struct dentry *parent;
|
2021-04-13 12:18:10 +08:00
|
|
|
int ret = 0;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
*daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL);
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_WRITE))
|
2021-03-16 09:50:04 +08:00
|
|
|
*daccess |= cpu_to_le32(WRITE_DAC | WRITE_OWNER | SYNCHRONIZE |
|
|
|
|
FILE_WRITE_DATA | FILE_APPEND_DATA |
|
|
|
|
FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES |
|
|
|
|
FILE_DELETE_CHILD);
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_READ))
|
2021-03-16 09:50:04 +08:00
|
|
|
*daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE;
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_EXEC))
|
2021-03-16 09:50:04 +08:00
|
|
|
*daccess |= FILE_EXECUTE_LE;
|
|
|
|
|
|
|
|
parent = dget_parent(dentry);
|
2021-08-23 23:13:47 +08:00
|
|
|
ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
|
2021-06-23 10:07:43 +08:00
|
|
|
if (ret) {
|
|
|
|
dput(parent);
|
|
|
|
return ret;
|
2021-04-13 12:18:10 +08:00
|
|
|
}
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
if (!inode_permission(user_ns, d_inode(parent), MAY_EXEC | MAY_WRITE))
|
2021-03-16 09:50:04 +08:00
|
|
|
*daccess |= FILE_DELETE_LE;
|
2021-04-13 12:18:10 +08:00
|
|
|
|
|
|
|
inode_unlock(d_inode(parent));
|
2021-03-16 09:50:04 +08:00
|
|
|
dput(parent);
|
2021-04-13 12:18:10 +08:00
|
|
|
return ret;
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_create() - vfs helper for smb create file
|
|
|
|
* @work: work
|
2021-09-24 23:06:16 +08:00
|
|
|
* @name: file name that is relative to share
|
2021-03-16 09:50:04 +08:00
|
|
|
* @mode: file create mode
|
|
|
|
*
|
|
|
|
* Return: 0 on success, otherwise error
|
|
|
|
*/
|
2021-03-30 13:25:35 +08:00
|
|
|
int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct path path;
|
|
|
|
struct dentry *dentry;
|
|
|
|
int err;
|
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
dentry = ksmbd_vfs_kern_path_create(work, name,
|
|
|
|
LOOKUP_NO_SYMLINKS, &path);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (IS_ERR(dentry)) {
|
|
|
|
err = PTR_ERR(dentry);
|
|
|
|
if (err != -ENOENT)
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("path create failed for %s, err %d\n",
|
|
|
|
name, err);
|
2021-03-16 09:50:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
mode |= S_IFREG;
|
2021-06-30 17:25:53 +08:00
|
|
|
err = vfs_create(mnt_user_ns(path.mnt), d_inode(path.dentry),
|
|
|
|
dentry, mode, true);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (!err) {
|
|
|
|
ksmbd_vfs_inherit_owner(work, d_inode(path.dentry),
|
2021-05-26 16:57:12 +08:00
|
|
|
d_inode(dentry));
|
2021-03-16 09:50:04 +08:00
|
|
|
} else {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("File(%s): creation failed (err:%d)\n", name, err);
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
done_path_create(&path, dentry);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_mkdir() - vfs helper for smb create directory
|
|
|
|
* @work: work
|
2021-09-24 23:06:16 +08:00
|
|
|
* @name: directory name that is relative to share
|
2021-03-16 09:50:04 +08:00
|
|
|
* @mode: directory create mode
|
|
|
|
*
|
|
|
|
* Return: 0 on success, otherwise error
|
|
|
|
*/
|
2021-03-30 13:25:35 +08:00
|
|
|
int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
2021-08-23 23:13:47 +08:00
|
|
|
struct user_namespace *user_ns;
|
2021-03-16 09:50:04 +08:00
|
|
|
struct path path;
|
|
|
|
struct dentry *dentry;
|
|
|
|
int err;
|
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
dentry = ksmbd_vfs_kern_path_create(work, name,
|
|
|
|
LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY,
|
|
|
|
&path);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (IS_ERR(dentry)) {
|
|
|
|
err = PTR_ERR(dentry);
|
|
|
|
if (err != -EEXIST)
|
|
|
|
ksmbd_debug(VFS, "path create failed for %s, err %d\n",
|
2021-05-26 16:57:12 +08:00
|
|
|
name, err);
|
2021-03-16 09:50:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-08-23 23:13:47 +08:00
|
|
|
user_ns = mnt_user_ns(path.mnt);
|
2021-03-16 09:50:04 +08:00
|
|
|
mode |= S_IFDIR;
|
2021-08-23 23:13:47 +08:00
|
|
|
err = vfs_mkdir(user_ns, d_inode(path.dentry), dentry, mode);
|
2021-05-26 15:40:39 +08:00
|
|
|
if (err) {
|
2021-04-01 16:32:24 +08:00
|
|
|
goto out;
|
2021-05-26 15:40:39 +08:00
|
|
|
} else if (d_unhashed(dentry)) {
|
2021-04-01 16:32:24 +08:00
|
|
|
struct dentry *d;
|
|
|
|
|
2021-08-23 23:13:47 +08:00
|
|
|
d = lookup_one(user_ns, dentry->d_name.name, dentry->d_parent,
|
|
|
|
dentry->d_name.len);
|
2021-04-01 16:32:24 +08:00
|
|
|
if (IS_ERR(d)) {
|
|
|
|
err = PTR_ERR(d);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (unlikely(d_is_negative(d))) {
|
|
|
|
dput(d);
|
|
|
|
err = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-05-26 16:57:12 +08:00
|
|
|
ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), d_inode(d));
|
2021-04-01 16:32:24 +08:00
|
|
|
dput(d);
|
2021-03-30 13:25:35 +08:00
|
|
|
}
|
2021-04-01 16:32:24 +08:00
|
|
|
out:
|
2021-03-16 09:50:04 +08:00
|
|
|
done_path_create(&path, dentry);
|
2021-04-01 16:32:24 +08:00
|
|
|
if (err)
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("mkdir(%s): creation failed (err:%d)\n", name, err);
|
2021-03-16 09:50:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
static ssize_t ksmbd_vfs_getcasexattr(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry, char *attr_name,
|
2021-05-26 16:57:12 +08:00
|
|
|
int attr_name_len, char **attr_value)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
char *name, *xattr_list = NULL;
|
|
|
|
ssize_t value_len = -ENOENT, xattr_list_len;
|
|
|
|
|
|
|
|
xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
|
|
|
|
if (xattr_list_len <= 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
for (name = xattr_list; name - xattr_list < xattr_list_len;
|
|
|
|
name += strlen(name) + 1) {
|
|
|
|
ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
|
|
|
|
if (strncasecmp(attr_name, name, attr_name_len))
|
|
|
|
continue;
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
value_len = ksmbd_vfs_getxattr(user_ns,
|
|
|
|
dentry,
|
2021-03-16 09:50:04 +08:00
|
|
|
name,
|
|
|
|
attr_value);
|
|
|
|
if (value_len < 0)
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("failed to get xattr in file\n");
|
2021-03-16 09:50:04 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2021-04-02 11:47:14 +08:00
|
|
|
kvfree(xattr_list);
|
2021-03-16 09:50:04 +08:00
|
|
|
return value_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos,
|
2021-05-26 16:57:12 +08:00
|
|
|
size_t count)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
ssize_t v_len;
|
|
|
|
char *stream_buf = NULL;
|
|
|
|
|
|
|
|
ksmbd_debug(VFS, "read stream data pos : %llu, count : %zd\n",
|
2021-05-26 16:57:12 +08:00
|
|
|
*pos, count);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
v_len = ksmbd_vfs_getcasexattr(file_mnt_user_ns(fp->filp),
|
|
|
|
fp->filp->f_path.dentry,
|
2021-03-16 09:50:04 +08:00
|
|
|
fp->stream.name,
|
|
|
|
fp->stream.size,
|
|
|
|
&stream_buf);
|
2021-05-31 16:25:05 +08:00
|
|
|
if ((int)v_len <= 0)
|
|
|
|
return (int)v_len;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-05-31 16:26:43 +08:00
|
|
|
if (v_len <= *pos) {
|
|
|
|
count = -EINVAL;
|
|
|
|
goto free_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v_len - *pos < count)
|
|
|
|
count = v_len - *pos;
|
|
|
|
|
2021-03-16 09:50:04 +08:00
|
|
|
memcpy(buf, &stream_buf[*pos], count);
|
2021-05-31 16:26:43 +08:00
|
|
|
|
|
|
|
free_buf:
|
2021-05-29 16:20:57 +08:00
|
|
|
kvfree(stream_buf);
|
2021-05-31 16:26:43 +08:00
|
|
|
return count;
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check_lock_range() - vfs helper for smb byte range file locking
|
|
|
|
* @filp: the file to apply the lock to
|
|
|
|
* @start: lock start byte offset
|
|
|
|
* @end: lock end byte offset
|
|
|
|
* @type: byte range type read/write
|
|
|
|
*
|
|
|
|
* Return: 0 on success, otherwise error
|
|
|
|
*/
|
2021-03-30 13:25:35 +08:00
|
|
|
static int check_lock_range(struct file *filp, loff_t start, loff_t end,
|
2021-05-26 16:57:12 +08:00
|
|
|
unsigned char type)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct file_lock *flock;
|
2022-11-16 21:50:52 +08:00
|
|
|
struct file_lock_context *ctx = locks_inode_context(file_inode(filp));
|
2021-03-16 09:50:04 +08:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
if (!ctx || list_empty_careful(&ctx->flc_posix))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
spin_lock(&ctx->flc_lock);
|
|
|
|
list_for_each_entry(flock, &ctx->flc_posix, fl_list) {
|
|
|
|
/* check conflict locks */
|
|
|
|
if (flock->fl_end >= start && end >= flock->fl_start) {
|
|
|
|
if (flock->fl_type == F_RDLCK) {
|
|
|
|
if (type == WRITE) {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("not allow write by shared lock\n");
|
2021-03-16 09:50:04 +08:00
|
|
|
error = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else if (flock->fl_type == F_WRLCK) {
|
|
|
|
/* check owner in lock */
|
|
|
|
if (flock->fl_file != filp) {
|
|
|
|
error = 1;
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("not allow rw access by exclusive lock from other opens\n");
|
2021-03-16 09:50:04 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
spin_unlock(&ctx->flc_lock);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_read() - vfs helper for smb file read
|
|
|
|
* @work: smb work
|
|
|
|
* @fid: file id of open file
|
|
|
|
* @count: read byte count
|
|
|
|
* @pos: file pos
|
|
|
|
*
|
|
|
|
* Return: number of read bytes on success, otherwise error
|
|
|
|
*/
|
2021-03-30 13:25:35 +08:00
|
|
|
int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count,
|
2021-05-26 16:57:12 +08:00
|
|
|
loff_t *pos)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
2021-06-18 09:18:34 +08:00
|
|
|
struct file *filp = fp->filp;
|
2021-03-16 09:50:04 +08:00
|
|
|
ssize_t nbytes = 0;
|
2021-06-18 09:18:34 +08:00
|
|
|
char *rbuf = work->aux_payload_buf;
|
|
|
|
struct inode *inode = file_inode(filp);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
if (S_ISDIR(inode->i_mode))
|
|
|
|
return -EISDIR;
|
|
|
|
|
|
|
|
if (unlikely(count == 0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (work->conn->connection_type) {
|
|
|
|
if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
|
2022-01-31 10:11:02 +08:00
|
|
|
pr_err("no right to read(%pD)\n", fp->filp);
|
2021-03-16 09:50:04 +08:00
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ksmbd_stream_fd(fp))
|
|
|
|
return ksmbd_vfs_stream_read(fp, rbuf, pos, count);
|
|
|
|
|
2021-03-30 11:43:18 +08:00
|
|
|
if (!work->tcon->posix_extensions) {
|
|
|
|
int ret;
|
|
|
|
|
2021-05-26 16:57:12 +08:00
|
|
|
ret = check_lock_range(filp, *pos, *pos + count - 1, READ);
|
2021-03-30 11:43:18 +08:00
|
|
|
if (ret) {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("unable to read due to lock\n");
|
2021-03-30 11:43:18 +08:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
nbytes = kernel_read(filp, rbuf, count, pos);
|
|
|
|
if (nbytes < 0) {
|
2022-02-24 10:03:41 +08:00
|
|
|
pr_err("smb read failed, err = %zd\n", nbytes);
|
2021-03-16 09:50:04 +08:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
filp->f_pos = *pos;
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
|
2021-05-26 16:57:12 +08:00
|
|
|
size_t count)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
char *stream_buf = NULL, *wbuf;
|
2021-07-03 11:10:36 +08:00
|
|
|
struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
|
2021-03-16 09:50:04 +08:00
|
|
|
size_t size, v_len;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n",
|
2021-05-26 16:57:12 +08:00
|
|
|
*pos, count);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
size = *pos + count;
|
|
|
|
if (size > XATTR_SIZE_MAX) {
|
|
|
|
size = XATTR_SIZE_MAX;
|
|
|
|
count = (*pos + count) - XATTR_SIZE_MAX;
|
|
|
|
}
|
|
|
|
|
2021-07-03 11:10:36 +08:00
|
|
|
v_len = ksmbd_vfs_getcasexattr(user_ns,
|
2021-06-30 17:25:53 +08:00
|
|
|
fp->filp->f_path.dentry,
|
2021-03-16 09:50:04 +08:00
|
|
|
fp->stream.name,
|
|
|
|
fp->stream.size,
|
|
|
|
&stream_buf);
|
2021-05-31 16:25:05 +08:00
|
|
|
if ((int)v_len < 0) {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("not found stream in xattr : %zd\n", v_len);
|
2021-05-31 16:25:05 +08:00
|
|
|
err = (int)v_len;
|
2021-03-16 09:50:04 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v_len < size) {
|
2021-04-02 11:47:14 +08:00
|
|
|
wbuf = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (!wbuf) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v_len > 0)
|
|
|
|
memcpy(wbuf, stream_buf, v_len);
|
2021-05-29 16:20:56 +08:00
|
|
|
kvfree(stream_buf);
|
2021-03-16 09:50:04 +08:00
|
|
|
stream_buf = wbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&stream_buf[*pos], buf, count);
|
|
|
|
|
2021-07-03 11:10:36 +08:00
|
|
|
err = ksmbd_vfs_setxattr(user_ns,
|
2021-06-30 17:25:53 +08:00
|
|
|
fp->filp->f_path.dentry,
|
2021-03-16 09:50:04 +08:00
|
|
|
fp->stream.name,
|
|
|
|
(void *)stream_buf,
|
|
|
|
size,
|
|
|
|
0);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
fp->filp->f_pos = *pos;
|
|
|
|
err = 0;
|
|
|
|
out:
|
2021-04-02 11:47:14 +08:00
|
|
|
kvfree(stream_buf);
|
2021-03-16 09:50:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_write() - vfs helper for smb file write
|
|
|
|
* @work: work
|
|
|
|
* @fid: file id of open file
|
|
|
|
* @buf: buf containing data for writing
|
|
|
|
* @count: read byte count
|
|
|
|
* @pos: file pos
|
|
|
|
* @sync: fsync after write
|
|
|
|
* @written: number of bytes written
|
|
|
|
*
|
|
|
|
* Return: 0 on success, otherwise error
|
|
|
|
*/
|
|
|
|
int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp,
|
2021-05-26 16:57:12 +08:00
|
|
|
char *buf, size_t count, loff_t *pos, bool sync,
|
|
|
|
ssize_t *written)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct file *filp;
|
|
|
|
loff_t offset = *pos;
|
|
|
|
int err = 0;
|
|
|
|
|
2022-07-25 12:36:52 +08:00
|
|
|
if (work->conn->connection_type) {
|
2021-03-16 09:50:04 +08:00
|
|
|
if (!(fp->daccess & FILE_WRITE_DATA_LE)) {
|
2022-01-31 10:11:02 +08:00
|
|
|
pr_err("no right to write(%pD)\n", fp->filp);
|
2021-03-16 09:50:04 +08:00
|
|
|
err = -EACCES;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filp = fp->filp;
|
|
|
|
|
|
|
|
if (ksmbd_stream_fd(fp)) {
|
|
|
|
err = ksmbd_vfs_stream_write(fp, buf, pos, count);
|
|
|
|
if (!err)
|
|
|
|
*written = count;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-03-30 11:43:18 +08:00
|
|
|
if (!work->tcon->posix_extensions) {
|
|
|
|
err = check_lock_range(filp, *pos, *pos + count - 1, WRITE);
|
|
|
|
if (err) {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("unable to write due to lock\n");
|
2021-03-30 11:43:18 +08:00
|
|
|
err = -EAGAIN;
|
|
|
|
goto out;
|
|
|
|
}
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do we need to break any of a levelII oplock? */
|
|
|
|
smb_break_all_levII_oplock(work, fp, 1);
|
|
|
|
|
|
|
|
err = kernel_write(filp, buf, count, pos);
|
|
|
|
if (err < 0) {
|
|
|
|
ksmbd_debug(VFS, "smb write failed, err = %d\n", err);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
filp->f_pos = *pos;
|
|
|
|
*written = err;
|
|
|
|
err = 0;
|
|
|
|
if (sync) {
|
|
|
|
err = vfs_fsync_range(filp, offset, offset + *written, 0);
|
|
|
|
if (err < 0)
|
2022-01-31 10:11:02 +08:00
|
|
|
pr_err("fsync failed for filename = %pD, err = %d\n",
|
|
|
|
fp->filp, err);
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_getattr() - vfs helper for smb getattr
|
|
|
|
* @work: work
|
|
|
|
* @fid: file id of open file
|
|
|
|
* @attrs: inode attributes
|
|
|
|
*
|
|
|
|
* Return: 0 on success, otherwise error
|
|
|
|
*/
|
2022-08-05 00:51:14 +08:00
|
|
|
int ksmbd_vfs_getattr(const struct path *path, struct kstat *stat)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = vfs_getattr(path, stat, STATX_BTIME, AT_STATX_SYNC_AS_STAT);
|
|
|
|
if (err)
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("getattr failed, err %d\n", err);
|
2021-03-16 09:50:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_fsync() - vfs helper for smb fsync
|
|
|
|
* @work: work
|
|
|
|
* @fid: file id of open file
|
|
|
|
*
|
|
|
|
* Return: 0 on success, otherwise error
|
|
|
|
*/
|
2021-03-30 13:25:35 +08:00
|
|
|
int ksmbd_vfs_fsync(struct ksmbd_work *work, u64 fid, u64 p_id)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct ksmbd_file *fp;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
fp = ksmbd_lookup_fd_slow(work, fid, p_id);
|
|
|
|
if (!fp) {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("failed to get filp for fid %llu\n", fid);
|
2021-03-16 09:50:04 +08:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
err = vfs_fsync(fp->filp, 0);
|
|
|
|
if (err < 0)
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("smb fsync failed, err = %d\n", err);
|
2021-03-16 09:50:04 +08:00
|
|
|
ksmbd_fd_put(work, fp);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_remove_file() - vfs helper for smb rmdir or unlink
|
2021-09-24 23:06:16 +08:00
|
|
|
* @name: directory or file name that is relative to share
|
2021-03-16 09:50:04 +08:00
|
|
|
*
|
|
|
|
* Return: 0 on success, otherwise error
|
|
|
|
*/
|
|
|
|
int ksmbd_vfs_remove_file(struct ksmbd_work *work, char *name)
|
|
|
|
{
|
2021-08-23 23:13:47 +08:00
|
|
|
struct user_namespace *user_ns;
|
2021-04-13 12:20:52 +08:00
|
|
|
struct path path;
|
2021-06-23 10:07:43 +08:00
|
|
|
struct dentry *parent;
|
2021-03-26 01:35:38 +08:00
|
|
|
int err;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
if (ksmbd_override_fsids(work))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
err = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, false);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (err) {
|
|
|
|
ksmbd_debug(VFS, "can't get %s, err %d\n", name, err);
|
|
|
|
ksmbd_revert_fsids(work);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-08-23 23:13:47 +08:00
|
|
|
user_ns = mnt_user_ns(path.mnt);
|
2021-04-13 12:20:52 +08:00
|
|
|
parent = dget_parent(path.dentry);
|
2021-08-23 23:13:47 +08:00
|
|
|
err = ksmbd_vfs_lock_parent(user_ns, parent, path.dentry);
|
2021-06-23 10:07:43 +08:00
|
|
|
if (err) {
|
|
|
|
dput(parent);
|
|
|
|
path_put(&path);
|
|
|
|
ksmbd_revert_fsids(work);
|
|
|
|
return err;
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
2021-06-23 10:07:43 +08:00
|
|
|
if (!d_inode(path.dentry)->i_nlink) {
|
2021-03-16 09:50:04 +08:00
|
|
|
err = -ENOENT;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
2021-06-23 10:07:43 +08:00
|
|
|
if (S_ISDIR(d_inode(path.dentry)->i_mode)) {
|
2021-08-23 23:13:47 +08:00
|
|
|
err = vfs_rmdir(user_ns, d_inode(parent), path.dentry);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (err && err != -ENOTEMPTY)
|
|
|
|
ksmbd_debug(VFS, "%s: rmdir failed, err %d\n", name,
|
2021-05-26 16:57:12 +08:00
|
|
|
err);
|
2021-03-16 09:50:04 +08:00
|
|
|
} else {
|
2021-08-23 23:13:47 +08:00
|
|
|
err = vfs_unlink(user_ns, d_inode(parent), path.dentry, NULL);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (err)
|
|
|
|
ksmbd_debug(VFS, "%s: unlink failed, err %d\n", name,
|
2021-05-26 16:57:12 +08:00
|
|
|
err);
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
out_err:
|
2021-04-13 12:20:52 +08:00
|
|
|
inode_unlock(d_inode(parent));
|
|
|
|
dput(parent);
|
|
|
|
path_put(&path);
|
2021-03-16 09:50:04 +08:00
|
|
|
ksmbd_revert_fsids(work);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_link() - vfs helper for creating smb hardlink
|
|
|
|
* @oldname: source file name
|
2021-09-24 23:06:16 +08:00
|
|
|
* @newname: hardlink name that is relative to share
|
2021-03-16 09:50:04 +08:00
|
|
|
*
|
|
|
|
* Return: 0 on success, otherwise error
|
|
|
|
*/
|
2021-03-30 13:25:35 +08:00
|
|
|
int ksmbd_vfs_link(struct ksmbd_work *work, const char *oldname,
|
2021-05-26 16:57:12 +08:00
|
|
|
const char *newname)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct path oldpath, newpath;
|
|
|
|
struct dentry *dentry;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (ksmbd_override_fsids(work))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2021-09-21 13:19:33 +08:00
|
|
|
err = kern_path(oldname, LOOKUP_NO_SYMLINKS, &oldpath);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (err) {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("cannot get linux path for %s, err = %d\n",
|
|
|
|
oldname, err);
|
2021-03-16 09:50:04 +08:00
|
|
|
goto out1;
|
|
|
|
}
|
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
dentry = ksmbd_vfs_kern_path_create(work, newname,
|
|
|
|
LOOKUP_NO_SYMLINKS | LOOKUP_REVAL,
|
|
|
|
&newpath);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (IS_ERR(dentry)) {
|
|
|
|
err = PTR_ERR(dentry);
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("path create err for %s, err %d\n", newname, err);
|
2021-03-16 09:50:04 +08:00
|
|
|
goto out2;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = -EXDEV;
|
|
|
|
if (oldpath.mnt != newpath.mnt) {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("vfs_link failed err %d\n", err);
|
2021-03-16 09:50:04 +08:00
|
|
|
goto out3;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
err = vfs_link(oldpath.dentry, mnt_user_ns(newpath.mnt),
|
|
|
|
d_inode(newpath.dentry),
|
2021-05-26 16:57:12 +08:00
|
|
|
dentry, NULL);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (err)
|
|
|
|
ksmbd_debug(VFS, "vfs_link failed err %d\n", err);
|
|
|
|
|
|
|
|
out3:
|
|
|
|
done_path_create(&newpath, dentry);
|
|
|
|
out2:
|
|
|
|
path_put(&oldpath);
|
|
|
|
out1:
|
|
|
|
ksmbd_revert_fsids(work);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-06-18 09:20:24 +08:00
|
|
|
static int ksmbd_validate_entry_in_use(struct dentry *src_dent)
|
|
|
|
{
|
|
|
|
struct dentry *dst_dent;
|
|
|
|
|
|
|
|
spin_lock(&src_dent->d_lock);
|
|
|
|
list_for_each_entry(dst_dent, &src_dent->d_subdirs, d_child) {
|
|
|
|
struct ksmbd_file *child_fp;
|
|
|
|
|
|
|
|
if (d_really_is_negative(dst_dent))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
child_fp = ksmbd_lookup_fd_inode(d_inode(dst_dent));
|
|
|
|
if (child_fp) {
|
|
|
|
spin_unlock(&src_dent->d_lock);
|
|
|
|
ksmbd_debug(VFS, "Forbid rename, sub file/dir is in use\n");
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock(&src_dent->d_lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:50:04 +08:00
|
|
|
static int __ksmbd_vfs_rename(struct ksmbd_work *work,
|
2021-06-30 17:25:53 +08:00
|
|
|
struct user_namespace *src_user_ns,
|
2021-05-26 16:57:12 +08:00
|
|
|
struct dentry *src_dent_parent,
|
|
|
|
struct dentry *src_dent,
|
2021-06-30 17:25:53 +08:00
|
|
|
struct user_namespace *dst_user_ns,
|
2021-05-26 16:57:12 +08:00
|
|
|
struct dentry *dst_dent_parent,
|
|
|
|
struct dentry *trap_dent,
|
|
|
|
char *dst_name)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct dentry *dst_dent;
|
|
|
|
int err;
|
|
|
|
|
2021-03-30 11:43:18 +08:00
|
|
|
if (!work->tcon->posix_extensions) {
|
2021-06-18 09:20:24 +08:00
|
|
|
err = ksmbd_validate_entry_in_use(src_dent);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d_really_is_negative(src_dent_parent))
|
|
|
|
return -ENOENT;
|
|
|
|
if (d_really_is_negative(dst_dent_parent))
|
|
|
|
return -ENOENT;
|
|
|
|
if (d_really_is_negative(src_dent))
|
|
|
|
return -ENOENT;
|
|
|
|
if (src_dent == trap_dent)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (ksmbd_override_fsids(work))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2021-08-23 23:13:47 +08:00
|
|
|
dst_dent = lookup_one(dst_user_ns, dst_name, dst_dent_parent,
|
|
|
|
strlen(dst_name));
|
2021-03-16 09:50:04 +08:00
|
|
|
err = PTR_ERR(dst_dent);
|
|
|
|
if (IS_ERR(dst_dent)) {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("lookup failed %s [%d]\n", dst_name, err);
|
2021-03-16 09:50:04 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = -ENOTEMPTY;
|
|
|
|
if (dst_dent != trap_dent && !d_really_is_positive(dst_dent)) {
|
|
|
|
struct renamedata rd = {
|
2021-06-30 17:25:53 +08:00
|
|
|
.old_mnt_userns = src_user_ns,
|
2021-03-16 09:50:04 +08:00
|
|
|
.old_dir = d_inode(src_dent_parent),
|
|
|
|
.old_dentry = src_dent,
|
2021-06-30 17:25:53 +08:00
|
|
|
.new_mnt_userns = dst_user_ns,
|
2021-03-16 09:50:04 +08:00
|
|
|
.new_dir = d_inode(dst_dent_parent),
|
|
|
|
.new_dentry = dst_dent,
|
|
|
|
};
|
|
|
|
err = vfs_rename(&rd);
|
|
|
|
}
|
|
|
|
if (err)
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("vfs_rename failed err %d\n", err);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (dst_dent)
|
|
|
|
dput(dst_dent);
|
|
|
|
out:
|
|
|
|
ksmbd_revert_fsids(work);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ksmbd_vfs_fp_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
|
2021-05-26 16:57:12 +08:00
|
|
|
char *newname)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
2021-08-23 23:13:47 +08:00
|
|
|
struct user_namespace *user_ns;
|
2021-03-16 09:50:04 +08:00
|
|
|
struct path dst_path;
|
|
|
|
struct dentry *src_dent_parent, *dst_dent_parent;
|
2021-04-13 12:18:10 +08:00
|
|
|
struct dentry *src_dent, *trap_dent, *src_child;
|
2021-03-16 09:50:04 +08:00
|
|
|
char *dst_name;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
dst_name = extract_last_component(newname);
|
2021-09-24 23:06:16 +08:00
|
|
|
if (!dst_name) {
|
|
|
|
dst_name = newname;
|
|
|
|
newname = "";
|
|
|
|
}
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
src_dent_parent = dget_parent(fp->filp->f_path.dentry);
|
|
|
|
src_dent = fp->filp->f_path.dentry;
|
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
err = ksmbd_vfs_kern_path(work, newname,
|
|
|
|
LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY,
|
|
|
|
&dst_path, false);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (err) {
|
|
|
|
ksmbd_debug(VFS, "Cannot get path for %s [%d]\n", newname, err);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
dst_dent_parent = dst_path.dentry;
|
|
|
|
|
|
|
|
trap_dent = lock_rename(src_dent_parent, dst_dent_parent);
|
2021-04-13 12:18:10 +08:00
|
|
|
dget(src_dent);
|
|
|
|
dget(dst_dent_parent);
|
2021-08-23 23:13:47 +08:00
|
|
|
user_ns = file_mnt_user_ns(fp->filp);
|
|
|
|
src_child = lookup_one(user_ns, src_dent->d_name.name, src_dent_parent,
|
|
|
|
src_dent->d_name.len);
|
2021-04-13 12:18:10 +08:00
|
|
|
if (IS_ERR(src_child)) {
|
|
|
|
err = PTR_ERR(src_child);
|
|
|
|
goto out_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src_child != src_dent) {
|
|
|
|
err = -ESTALE;
|
|
|
|
dput(src_child);
|
|
|
|
goto out_lock;
|
|
|
|
}
|
|
|
|
dput(src_child);
|
|
|
|
|
2021-03-16 09:50:04 +08:00
|
|
|
err = __ksmbd_vfs_rename(work,
|
2021-08-23 23:13:47 +08:00
|
|
|
user_ns,
|
2021-03-16 09:50:04 +08:00
|
|
|
src_dent_parent,
|
|
|
|
src_dent,
|
2021-06-30 17:25:53 +08:00
|
|
|
mnt_user_ns(dst_path.mnt),
|
2021-03-16 09:50:04 +08:00
|
|
|
dst_dent_parent,
|
|
|
|
trap_dent,
|
|
|
|
dst_name);
|
2021-04-13 12:18:10 +08:00
|
|
|
out_lock:
|
|
|
|
dput(src_dent);
|
2021-03-16 09:50:04 +08:00
|
|
|
dput(dst_dent_parent);
|
2021-04-13 12:18:10 +08:00
|
|
|
unlock_rename(src_dent_parent, dst_dent_parent);
|
2021-03-16 09:50:04 +08:00
|
|
|
path_put(&dst_path);
|
|
|
|
out:
|
|
|
|
dput(src_dent_parent);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_truncate() - vfs helper for smb file truncate
|
|
|
|
* @work: work
|
|
|
|
* @fid: file id of old file
|
|
|
|
* @size: truncate to given size
|
|
|
|
*
|
|
|
|
* Return: 0 on success, otherwise error
|
|
|
|
*/
|
2021-09-24 23:06:16 +08:00
|
|
|
int ksmbd_vfs_truncate(struct ksmbd_work *work,
|
2021-05-26 16:57:12 +08:00
|
|
|
struct ksmbd_file *fp, loff_t size)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
int err = 0;
|
2021-09-24 23:06:16 +08:00
|
|
|
struct file *filp;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
filp = fp->filp;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
/* Do we need to break any of a levelII oplock? */
|
|
|
|
smb_break_all_levII_oplock(work, fp, 1);
|
2021-03-30 11:43:18 +08:00
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
if (!work->tcon->posix_extensions) {
|
|
|
|
struct inode *inode = file_inode(filp);
|
|
|
|
|
|
|
|
if (size < inode->i_size) {
|
|
|
|
err = check_lock_range(filp, size,
|
|
|
|
inode->i_size - 1, WRITE);
|
|
|
|
} else {
|
|
|
|
err = check_lock_range(filp, inode->i_size,
|
|
|
|
size - 1, WRITE);
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
if (err) {
|
|
|
|
pr_err("failed due to lock\n");
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
err = vfs_truncate(&filp->f_path, size);
|
|
|
|
if (err)
|
2022-02-24 10:03:41 +08:00
|
|
|
pr_err("truncate failed, err %d\n", err);
|
2021-03-16 09:50:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_listxattr() - vfs helper for smb list extended attributes
|
|
|
|
* @dentry: dentry of file for listing xattrs
|
|
|
|
* @list: destination buffer
|
|
|
|
* @size: destination buffer length
|
|
|
|
*
|
|
|
|
* Return: xattr list length on success, otherwise error
|
|
|
|
*/
|
|
|
|
ssize_t ksmbd_vfs_listxattr(struct dentry *dentry, char **list)
|
|
|
|
{
|
|
|
|
ssize_t size;
|
|
|
|
char *vlist = NULL;
|
|
|
|
|
|
|
|
size = vfs_listxattr(dentry, NULL, 0);
|
|
|
|
if (size <= 0)
|
|
|
|
return size;
|
|
|
|
|
2021-04-02 11:47:14 +08:00
|
|
|
vlist = kvmalloc(size, GFP_KERNEL | __GFP_ZERO);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (!vlist)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
*list = vlist;
|
|
|
|
size = vfs_listxattr(dentry, vlist, size);
|
|
|
|
if (size < 0) {
|
|
|
|
ksmbd_debug(VFS, "listxattr failed\n");
|
2021-04-02 11:47:14 +08:00
|
|
|
kvfree(vlist);
|
2021-03-16 09:50:04 +08:00
|
|
|
*list = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
static ssize_t ksmbd_vfs_xattr_len(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry, char *xattr_name)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
2021-06-30 17:25:53 +08:00
|
|
|
return vfs_getxattr(user_ns, dentry, xattr_name, NULL, 0);
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_getxattr() - vfs helper for smb get extended attributes value
|
2021-06-30 17:25:53 +08:00
|
|
|
* @user_ns: user namespace
|
2021-03-16 09:50:04 +08:00
|
|
|
* @dentry: dentry of file for getting xattrs
|
|
|
|
* @xattr_name: name of xattr name to query
|
|
|
|
* @xattr_buf: destination buffer xattr value
|
|
|
|
*
|
|
|
|
* Return: read xattr value length on success, otherwise error
|
|
|
|
*/
|
2021-06-30 17:25:53 +08:00
|
|
|
ssize_t ksmbd_vfs_getxattr(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry,
|
|
|
|
char *xattr_name, char **xattr_buf)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
ssize_t xattr_len;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
*xattr_buf = NULL;
|
2021-06-30 17:25:53 +08:00
|
|
|
xattr_len = ksmbd_vfs_xattr_len(user_ns, dentry, xattr_name);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (xattr_len < 0)
|
|
|
|
return xattr_len;
|
|
|
|
|
|
|
|
buf = kmalloc(xattr_len + 1, GFP_KERNEL);
|
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
xattr_len = vfs_getxattr(user_ns, dentry, xattr_name,
|
2021-05-29 08:59:59 +08:00
|
|
|
(void *)buf, xattr_len);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (xattr_len > 0)
|
|
|
|
*xattr_buf = buf;
|
|
|
|
else
|
|
|
|
kfree(buf);
|
|
|
|
return xattr_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_setxattr() - vfs helper for smb set extended attributes value
|
2021-06-30 17:25:53 +08:00
|
|
|
* @user_ns: user namespace
|
2021-03-16 09:50:04 +08:00
|
|
|
* @dentry: dentry to set XATTR at
|
|
|
|
* @name: xattr name for setxattr
|
|
|
|
* @value: xattr value to set
|
|
|
|
* @size: size of xattr value
|
|
|
|
* @flags: destination buffer length
|
|
|
|
*
|
|
|
|
* Return: 0 on success, otherwise error
|
|
|
|
*/
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_setxattr(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry, const char *attr_name,
|
acl: move idmapped mount fixup into vfs_{g,s}etxattr()
This cycle we added support for mounting overlayfs on top of idmapped mounts.
Recently I've started looking into potential corner cases when trying to add
additional tests and I noticed that reporting for POSIX ACLs is currently wrong
when using idmapped layers with overlayfs mounted on top of it.
I'm going to give a rather detailed explanation to both the origin of the
problem and the solution.
Let's assume the user creates the following directory layout and they have a
rootfs /var/lib/lxc/c1/rootfs. The files in this rootfs are owned as you would
expect files on your host system to be owned. For example, ~/.bashrc for your
regular user would be owned by 1000:1000 and /root/.bashrc would be owned by
0:0. IOW, this is just regular boring filesystem tree on an ext4 or xfs
filesystem.
The user chooses to set POSIX ACLs using the setfacl binary granting the user
with uid 4 read, write, and execute permissions for their .bashrc file:
setfacl -m u:4:rwx /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
Now they to expose the whole rootfs to a container using an idmapped mount. So
they first create:
mkdir -pv /vol/contpool/{ctrover,merge,lowermap,overmap}
mkdir -pv /vol/contpool/ctrover/{over,work}
chown 10000000:10000000 /vol/contpool/ctrover/{over,work}
The user now creates an idmapped mount for the rootfs:
mount-idmapped/mount-idmapped --map-mount=b:0:10000000:65536 \
/var/lib/lxc/c2/rootfs \
/vol/contpool/lowermap
This for example makes it so that /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
which is owned by uid and gid 1000 as being owned by uid and gid 10001000 at
/vol/contpool/lowermap/home/ubuntu/.bashrc.
Assume the user wants to expose these idmapped mounts through an overlayfs
mount to a container.
mount -t overlay overlay \
-o lowerdir=/vol/contpool/lowermap, \
upperdir=/vol/contpool/overmap/over, \
workdir=/vol/contpool/overmap/work \
/vol/contpool/merge
The user can do this in two ways:
(1) Mount overlayfs in the initial user namespace and expose it to the
container.
(2) Mount overlayfs on top of the idmapped mounts inside of the container's
user namespace.
Let's assume the user chooses the (1) option and mounts overlayfs on the host
and then changes into a container which uses the idmapping 0:10000000:65536
which is the same used for the two idmapped mounts.
Now the user tries to retrieve the POSIX ACLs using the getfacl command
getfacl -n /vol/contpool/lowermap/home/ubuntu/.bashrc
and to their surprise they see:
# file: vol/contpool/merge/home/ubuntu/.bashrc
# owner: 1000
# group: 1000
user::rw-
user:4294967295:rwx
group::r--
mask::rwx
other::r--
indicating the the uid wasn't correctly translated according to the idmapped
mount. The problem is how we currently translate POSIX ACLs. Let's inspect the
callchain in this example:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get == ovl_posix_acl_xattr_get()
| -> ovl_xattr_get()
| -> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get() /* lower filesystem callback */
|> posix_acl_fix_xattr_to_user()
{
4 = make_kuid(&init_user_ns, 4);
4 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 4);
/* FAILURE */
-1 = from_kuid(0:10000000:65536 /* caller's idmapping */, 4);
}
If the user chooses to use option (2) and mounts overlayfs on top of idmapped
mounts inside the container things don't look that much better:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get == ovl_posix_acl_xattr_get()
| -> ovl_xattr_get()
| -> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get() /* lower filesystem callback */
|> posix_acl_fix_xattr_to_user()
{
4 = make_kuid(&init_user_ns, 4);
4 = mapped_kuid_fs(&init_user_ns, 4);
/* FAILURE */
-1 = from_kuid(0:10000000:65536 /* caller's idmapping */, 4);
}
As is easily seen the problem arises because the idmapping of the lower mount
isn't taken into account as all of this happens in do_gexattr(). But
do_getxattr() is always called on an overlayfs mount and inode and thus cannot
possible take the idmapping of the lower layers into account.
This problem is similar for fscaps but there the translation happens as part of
vfs_getxattr() already. Let's walk through an fscaps overlayfs callchain:
setcap 'cap_net_raw+ep' /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
The expected outcome here is that we'll receive the cap_net_raw capability as
we are able to map the uid associated with the fscap to 0 within our container.
IOW, we want to see 0 as the result of the idmapping translations.
If the user chooses option (1) we get the following callchain for fscaps:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
-> vfs_getxattr()
-> xattr_getsecurity()
-> security_inode_getsecurity() ________________________________
-> cap_inode_getsecurity() | |
{ V |
10000000 = make_kuid(0:0:4k /* overlayfs idmapping */, 10000000); |
10000000 = mapped_kuid_fs(0:0:4k /* no idmapped mount */, 10000000); |
/* Expected result is 0 and thus that we own the fscap. */ |
0 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000000); |
} |
-> vfs_getxattr_alloc() |
-> handler->get == ovl_other_xattr_get() |
-> vfs_getxattr() |
-> xattr_getsecurity() |
-> security_inode_getsecurity() |
-> cap_inode_getsecurity() |
{ |
0 = make_kuid(0:0:4k /* lower s_user_ns */, 0); |
10000000 = mapped_kuid_fs(0:10000000:65536 /* idmapped mount */, 0); |
10000000 = from_kuid(0:0:4k /* overlayfs idmapping */, 10000000); |
|____________________________________________________________________|
}
-> vfs_getxattr_alloc()
-> handler->get == /* lower filesystem callback */
And if the user chooses option (2) we get:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
-> vfs_getxattr()
-> xattr_getsecurity()
-> security_inode_getsecurity() _______________________________
-> cap_inode_getsecurity() | |
{ V |
10000000 = make_kuid(0:10000000:65536 /* overlayfs idmapping */, 0); |
10000000 = mapped_kuid_fs(0:0:4k /* no idmapped mount */, 10000000); |
/* Expected result is 0 and thus that we own the fscap. */ |
0 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000000); |
} |
-> vfs_getxattr_alloc() |
-> handler->get == ovl_other_xattr_get() |
|-> vfs_getxattr() |
-> xattr_getsecurity() |
-> security_inode_getsecurity() |
-> cap_inode_getsecurity() |
{ |
0 = make_kuid(0:0:4k /* lower s_user_ns */, 0); |
10000000 = mapped_kuid_fs(0:10000000:65536 /* idmapped mount */, 0); |
0 = from_kuid(0:10000000:65536 /* overlayfs idmapping */, 10000000); |
|____________________________________________________________________|
}
-> vfs_getxattr_alloc()
-> handler->get == /* lower filesystem callback */
We can see how the translation happens correctly in those cases as the
conversion happens within the vfs_getxattr() helper.
For POSIX ACLs we need to do something similar. However, in contrast to fscaps
we cannot apply the fix directly to the kernel internal posix acl data
structure as this would alter the cached values and would also require a rework
of how we currently deal with POSIX ACLs in general which almost never take the
filesystem idmapping into account (the noteable exception being FUSE but even
there the implementation is special) and instead retrieve the raw values based
on the initial idmapping.
The correct values are then generated right before returning to userspace. The
fix for this is to move taking the mount's idmapping into account directly in
vfs_getxattr() instead of having it be part of posix_acl_fix_xattr_to_user().
To this end we split out two small and unexported helpers
posix_acl_getxattr_idmapped_mnt() and posix_acl_setxattr_idmapped_mnt(). The
former to be called in vfs_getxattr() and the latter to be called in
vfs_setxattr().
Let's go back to the original example. Assume the user chose option (1) and
mounted overlayfs on top of idmapped mounts on the host:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| |> __vfs_getxattr()
| | -> handler->get == ovl_posix_acl_xattr_get()
| | -> ovl_xattr_get()
| | -> vfs_getxattr()
| | |> __vfs_getxattr()
| | | -> handler->get() /* lower filesystem callback */
| | |> posix_acl_getxattr_idmapped_mnt()
| | {
| | 4 = make_kuid(&init_user_ns, 4);
| | 10000004 = mapped_kuid_fs(0:10000000:65536 /* lower idmapped mount */, 4);
| | 10000004 = from_kuid(&init_user_ns, 10000004);
| | |_______________________
| | } |
| | |
| |> posix_acl_getxattr_idmapped_mnt() |
| { |
| V
| 10000004 = make_kuid(&init_user_ns, 10000004);
| 10000004 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 10000004);
| 10000004 = from_kuid(&init_user_ns, 10000004);
| } |_________________________________________________
| |
| |
|> posix_acl_fix_xattr_to_user() |
{ V
10000004 = make_kuid(0:0:4k /* init_user_ns */, 10000004);
/* SUCCESS */
4 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000004);
}
And similarly if the user chooses option (1) and mounted overayfs on top of
idmapped mounts inside the container:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| |> __vfs_getxattr()
| | -> handler->get == ovl_posix_acl_xattr_get()
| | -> ovl_xattr_get()
| | -> vfs_getxattr()
| | |> __vfs_getxattr()
| | | -> handler->get() /* lower filesystem callback */
| | |> posix_acl_getxattr_idmapped_mnt()
| | {
| | 4 = make_kuid(&init_user_ns, 4);
| | 10000004 = mapped_kuid_fs(0:10000000:65536 /* lower idmapped mount */, 4);
| | 10000004 = from_kuid(&init_user_ns, 10000004);
| | |_______________________
| | } |
| | |
| |> posix_acl_getxattr_idmapped_mnt() |
| { V
| 10000004 = make_kuid(&init_user_ns, 10000004);
| 10000004 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 10000004);
| 10000004 = from_kuid(0(&init_user_ns, 10000004);
| |_________________________________________________
| } |
| |
|> posix_acl_fix_xattr_to_user() |
{ V
10000004 = make_kuid(0:0:4k /* init_user_ns */, 10000004);
/* SUCCESS */
4 = from_kuid(0:10000000:65536 /* caller's idmappings */, 10000004);
}
The last remaining problem we need to fix here is ovl_get_acl(). During
ovl_permission() overlayfs will call:
ovl_permission()
-> generic_permission()
-> acl_permission_check()
-> check_acl()
-> get_acl()
-> inode->i_op->get_acl() == ovl_get_acl()
> get_acl() /* on the underlying filesystem)
->inode->i_op->get_acl() == /*lower filesystem callback */
-> posix_acl_permission()
passing through the get_acl request to the underlying filesystem. This will
retrieve the acls stored in the lower filesystem without taking the idmapping
of the underlying mount into account as this would mean altering the cached
values for the lower filesystem. So we block using ACLs for now until we
decided on a nice way to fix this. Note this limitation both in the
documentation and in the code.
The most straightforward solution would be to have ovl_get_acl() simply
duplicate the ACLs, update the values according to the idmapped mount and
return it to acl_permission_check() so it can be used in posix_acl_permission()
forgetting them afterwards. This is a bit heavy handed but fairly
straightforward otherwise.
Link: https://github.com/brauner/mount-idmapped/issues/9
Link: https://lore.kernel.org/r/20220708090134.385160-2-brauner@kernel.org
Cc: Seth Forshee <sforshee@digitalocean.com>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Aleksa Sarai <cyphar@cyphar.com>
Cc: Miklos Szeredi <mszeredi@redhat.com>
Cc: linux-unionfs@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org
Reviewed-by: Seth Forshee <sforshee@digitalocean.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
2022-07-07 00:30:59 +08:00
|
|
|
void *attr_value, size_t attr_size, int flags)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
err = vfs_setxattr(user_ns,
|
|
|
|
dentry,
|
2021-03-16 09:50:04 +08:00
|
|
|
attr_name,
|
|
|
|
attr_value,
|
|
|
|
attr_size,
|
|
|
|
flags);
|
|
|
|
if (err)
|
|
|
|
ksmbd_debug(VFS, "setxattr failed, err %d\n", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_set_fadvise() - convert smb IO caching options to linux options
|
|
|
|
* @filp: file pointer for IO
|
|
|
|
* @options: smb IO options
|
|
|
|
*/
|
|
|
|
void ksmbd_vfs_set_fadvise(struct file *filp, __le32 option)
|
|
|
|
{
|
|
|
|
struct address_space *mapping;
|
|
|
|
|
|
|
|
mapping = filp->f_mapping;
|
|
|
|
|
|
|
|
if (!option || !mapping)
|
|
|
|
return;
|
|
|
|
|
2021-03-30 13:25:35 +08:00
|
|
|
if (option & FILE_WRITE_THROUGH_LE) {
|
2021-03-16 09:50:04 +08:00
|
|
|
filp->f_flags |= O_SYNC;
|
2021-03-30 13:25:35 +08:00
|
|
|
} else if (option & FILE_SEQUENTIAL_ONLY_LE) {
|
2021-03-16 09:50:04 +08:00
|
|
|
filp->f_ra.ra_pages = inode_to_bdi(mapping->host)->ra_pages * 2;
|
|
|
|
spin_lock(&filp->f_lock);
|
|
|
|
filp->f_mode &= ~FMODE_RANDOM;
|
|
|
|
spin_unlock(&filp->f_lock);
|
|
|
|
} else if (option & FILE_RANDOM_ACCESS_LE) {
|
|
|
|
spin_lock(&filp->f_lock);
|
|
|
|
filp->f_mode |= FMODE_RANDOM;
|
|
|
|
spin_unlock(&filp->f_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 13:25:35 +08:00
|
|
|
int ksmbd_vfs_zero_data(struct ksmbd_work *work, struct ksmbd_file *fp,
|
2021-05-26 16:57:12 +08:00
|
|
|
loff_t off, loff_t len)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
smb_break_all_levII_oplock(work, fp, 1);
|
2021-11-03 07:45:52 +08:00
|
|
|
if (fp->f_ci->m_fattr & FILE_ATTRIBUTE_SPARSE_FILE_LE)
|
2021-03-16 09:50:04 +08:00
|
|
|
return vfs_fallocate(fp->filp,
|
2021-05-26 16:57:12 +08:00
|
|
|
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
|
|
|
|
off, len);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2022-06-19 21:35:48 +08:00
|
|
|
return vfs_fallocate(fp->filp,
|
|
|
|
FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE,
|
|
|
|
off, len);
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ksmbd_vfs_fqar_lseek(struct ksmbd_file *fp, loff_t start, loff_t length,
|
2021-05-26 16:57:12 +08:00
|
|
|
struct file_allocated_range_buffer *ranges,
|
2021-10-08 06:31:03 +08:00
|
|
|
unsigned int in_count, unsigned int *out_count)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct file *f = fp->filp;
|
2021-06-29 08:20:13 +08:00
|
|
|
struct inode *inode = file_inode(fp->filp);
|
2021-03-16 09:50:04 +08:00
|
|
|
loff_t maxbytes = (u64)inode->i_sb->s_maxbytes, end;
|
|
|
|
loff_t extent_start, extent_end;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (start > maxbytes)
|
|
|
|
return -EFBIG;
|
|
|
|
|
|
|
|
if (!in_count)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shrink request scope to what the fs can actually handle.
|
|
|
|
*/
|
|
|
|
if (length > maxbytes || (maxbytes - length) < start)
|
|
|
|
length = maxbytes - start;
|
|
|
|
|
|
|
|
if (start + length > inode->i_size)
|
|
|
|
length = inode->i_size - start;
|
|
|
|
|
|
|
|
*out_count = 0;
|
|
|
|
end = start + length;
|
|
|
|
while (start < end && *out_count < in_count) {
|
2022-06-25 19:01:08 +08:00
|
|
|
extent_start = vfs_llseek(f, start, SEEK_DATA);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (extent_start < 0) {
|
|
|
|
if (extent_start != -ENXIO)
|
|
|
|
ret = (int)extent_start;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extent_start >= end)
|
|
|
|
break;
|
|
|
|
|
2022-06-25 19:01:08 +08:00
|
|
|
extent_end = vfs_llseek(f, extent_start, SEEK_HOLE);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (extent_end < 0) {
|
|
|
|
if (extent_end != -ENXIO)
|
|
|
|
ret = (int)extent_end;
|
|
|
|
break;
|
2021-03-30 13:25:35 +08:00
|
|
|
} else if (extent_start >= extent_end) {
|
2021-03-16 09:50:04 +08:00
|
|
|
break;
|
2021-03-30 13:25:35 +08:00
|
|
|
}
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
ranges[*out_count].file_offset = cpu_to_le64(extent_start);
|
|
|
|
ranges[(*out_count)++].length =
|
|
|
|
cpu_to_le64(min(extent_end, end) - extent_start);
|
|
|
|
|
|
|
|
start = extent_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_remove_xattr(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry, char *attr_name)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
2021-06-30 17:25:53 +08:00
|
|
|
return vfs_removexattr(user_ns, dentry, attr_name);
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_unlink(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dir, struct dentry *dentry)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
2021-08-23 23:13:47 +08:00
|
|
|
err = ksmbd_vfs_lock_parent(user_ns, dir, dentry);
|
2021-06-23 10:07:43 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
2021-04-13 12:18:10 +08:00
|
|
|
dget(dentry);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
if (S_ISDIR(d_inode(dentry)->i_mode))
|
2021-06-30 17:25:53 +08:00
|
|
|
err = vfs_rmdir(user_ns, d_inode(dir), dentry);
|
2021-03-16 09:50:04 +08:00
|
|
|
else
|
2021-06-30 17:25:53 +08:00
|
|
|
err = vfs_unlink(user_ns, d_inode(dir), dentry, NULL);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
dput(dentry);
|
2021-04-13 12:18:10 +08:00
|
|
|
inode_unlock(d_inode(dir));
|
2021-03-16 09:50:04 +08:00
|
|
|
if (err)
|
|
|
|
ksmbd_debug(VFS, "failed to delete, err %d\n", err);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-08-16 23:57:56 +08:00
|
|
|
static bool __dir_empty(struct dir_context *ctx, const char *name, int namlen,
|
2021-05-26 16:57:12 +08:00
|
|
|
loff_t offset, u64 ino, unsigned int d_type)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct ksmbd_readdir_data *buf;
|
|
|
|
|
|
|
|
buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
|
|
|
|
buf->dirent_count++;
|
|
|
|
|
2022-08-16 23:57:56 +08:00
|
|
|
return buf->dirent_count <= 2;
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_empty_dir() - check for empty directory
|
|
|
|
* @fp: ksmbd file pointer
|
|
|
|
*
|
|
|
|
* Return: true if directory empty, otherwise false
|
|
|
|
*/
|
|
|
|
int ksmbd_vfs_empty_dir(struct ksmbd_file *fp)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct ksmbd_readdir_data readdir_data;
|
|
|
|
|
|
|
|
memset(&readdir_data, 0, sizeof(struct ksmbd_readdir_data));
|
|
|
|
|
|
|
|
set_ctx_actor(&readdir_data.ctx, __dir_empty);
|
|
|
|
readdir_data.dirent_count = 0;
|
|
|
|
|
2021-06-22 10:06:11 +08:00
|
|
|
err = iterate_dir(fp->filp, &readdir_data.ctx);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (readdir_data.dirent_count > 2)
|
|
|
|
err = -ENOTEMPTY;
|
|
|
|
else
|
|
|
|
err = 0;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-08-16 23:57:56 +08:00
|
|
|
static bool __caseless_lookup(struct dir_context *ctx, const char *name,
|
2021-05-26 16:57:12 +08:00
|
|
|
int namlen, loff_t offset, u64 ino,
|
|
|
|
unsigned int d_type)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct ksmbd_readdir_data *buf;
|
2022-09-28 05:57:21 +08:00
|
|
|
int cmp = -EINVAL;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
|
|
|
|
|
|
|
|
if (buf->used != namlen)
|
2022-08-16 23:57:56 +08:00
|
|
|
return true;
|
2022-09-28 05:57:21 +08:00
|
|
|
if (IS_ENABLED(CONFIG_UNICODE) && buf->um) {
|
|
|
|
const struct qstr q_buf = {.name = buf->private,
|
|
|
|
.len = buf->used};
|
|
|
|
const struct qstr q_name = {.name = name,
|
|
|
|
.len = namlen};
|
|
|
|
|
|
|
|
cmp = utf8_strncasecmp(buf->um, &q_buf, &q_name);
|
|
|
|
}
|
|
|
|
if (cmp < 0)
|
|
|
|
cmp = strncasecmp((char *)buf->private, name, namlen);
|
|
|
|
if (!cmp) {
|
2021-03-16 09:50:04 +08:00
|
|
|
memcpy((char *)buf->private, name, namlen);
|
|
|
|
buf->dirent_count = 1;
|
2022-08-16 23:57:56 +08:00
|
|
|
return false;
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
2022-08-16 23:57:56 +08:00
|
|
|
return true;
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_lookup_in_dir() - lookup a file in a directory
|
2021-04-13 12:22:31 +08:00
|
|
|
* @dir: path info
|
|
|
|
* @name: filename to lookup
|
|
|
|
* @namelen: filename length
|
2021-03-16 09:50:04 +08:00
|
|
|
*
|
|
|
|
* Return: 0 on success, otherwise error
|
|
|
|
*/
|
2022-09-28 05:57:21 +08:00
|
|
|
static int ksmbd_vfs_lookup_in_dir(const struct path *dir, char *name,
|
|
|
|
size_t namelen, struct unicode_map *um)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct file *dfilp;
|
2021-03-30 13:25:35 +08:00
|
|
|
int flags = O_RDONLY | O_LARGEFILE;
|
2021-03-16 09:50:04 +08:00
|
|
|
struct ksmbd_readdir_data readdir_data = {
|
|
|
|
.ctx.actor = __caseless_lookup,
|
2021-04-13 12:22:31 +08:00
|
|
|
.private = name,
|
|
|
|
.used = namelen,
|
|
|
|
.dirent_count = 0,
|
2022-09-28 05:57:21 +08:00
|
|
|
.um = um,
|
2021-03-16 09:50:04 +08:00
|
|
|
};
|
|
|
|
|
2021-04-13 12:22:31 +08:00
|
|
|
dfilp = dentry_open(dir, flags, current_cred());
|
|
|
|
if (IS_ERR(dfilp))
|
|
|
|
return PTR_ERR(dfilp);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-06-22 10:06:11 +08:00
|
|
|
ret = iterate_dir(dfilp, &readdir_data.ctx);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (readdir_data.dirent_count > 0)
|
|
|
|
ret = 0;
|
|
|
|
fput(dfilp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_kern_path() - lookup a file and get path info
|
2021-09-24 23:06:16 +08:00
|
|
|
* @name: file path that is relative to share
|
2021-03-16 09:50:04 +08:00
|
|
|
* @flags: lookup flags
|
|
|
|
* @path: if lookup succeed, return path info
|
|
|
|
* @caseless: caseless filename lookup
|
|
|
|
*
|
|
|
|
* Return: 0 on success, otherwise error
|
|
|
|
*/
|
2021-09-24 23:06:16 +08:00
|
|
|
int ksmbd_vfs_kern_path(struct ksmbd_work *work, char *name,
|
|
|
|
unsigned int flags, struct path *path, bool caseless)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
2021-09-24 23:06:16 +08:00
|
|
|
struct ksmbd_share_config *share_conf = work->tcon->share_conf;
|
2021-03-16 09:50:04 +08:00
|
|
|
int err;
|
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
flags |= LOOKUP_BENEATH;
|
|
|
|
err = vfs_path_lookup(share_conf->vfs_path.dentry,
|
|
|
|
share_conf->vfs_path.mnt,
|
|
|
|
name,
|
|
|
|
flags,
|
|
|
|
path);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (!err)
|
2021-04-13 12:22:31 +08:00
|
|
|
return 0;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
if (caseless) {
|
2021-04-13 12:22:31 +08:00
|
|
|
char *filepath;
|
|
|
|
struct path parent;
|
|
|
|
size_t path_len, remain_len;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-04-13 12:22:31 +08:00
|
|
|
filepath = kstrdup(name, GFP_KERNEL);
|
|
|
|
if (!filepath)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
path_len = strlen(filepath);
|
2021-09-24 23:06:16 +08:00
|
|
|
remain_len = path_len;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
parent = share_conf->vfs_path;
|
|
|
|
path_get(&parent);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-04-13 12:22:31 +08:00
|
|
|
while (d_can_lookup(parent.dentry)) {
|
|
|
|
char *filename = filepath + path_len - remain_len;
|
|
|
|
char *next = strchrnul(filename, '/');
|
|
|
|
size_t filename_len = next - filename;
|
|
|
|
bool is_last = !next[0];
|
|
|
|
|
|
|
|
if (filename_len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
err = ksmbd_vfs_lookup_in_dir(&parent, filename,
|
2022-09-28 05:57:21 +08:00
|
|
|
filename_len,
|
|
|
|
work->conn->um);
|
2021-09-24 23:06:16 +08:00
|
|
|
path_put(&parent);
|
|
|
|
if (err)
|
2021-04-13 12:22:31 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
next[0] = '\0';
|
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
err = vfs_path_lookup(share_conf->vfs_path.dentry,
|
|
|
|
share_conf->vfs_path.mnt,
|
|
|
|
filepath,
|
|
|
|
flags,
|
|
|
|
&parent);
|
2021-04-13 12:22:31 +08:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2021-09-24 23:06:16 +08:00
|
|
|
else if (is_last) {
|
|
|
|
*path = parent;
|
2021-04-13 12:22:31 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
next[0] = '/';
|
|
|
|
remain_len -= filename_len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
path_put(&parent);
|
|
|
|
err = -EINVAL;
|
2021-03-16 09:50:04 +08:00
|
|
|
out:
|
2021-04-13 12:22:31 +08:00
|
|
|
kfree(filepath);
|
|
|
|
}
|
2021-03-16 09:50:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-09-24 23:06:16 +08:00
|
|
|
struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work,
|
|
|
|
const char *name,
|
|
|
|
unsigned int flags,
|
|
|
|
struct path *path)
|
|
|
|
{
|
|
|
|
char *abs_name;
|
|
|
|
struct dentry *dent;
|
|
|
|
|
|
|
|
abs_name = convert_to_unix_name(work->tcon->share_conf, name);
|
|
|
|
if (!abs_name)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
dent = kern_path_create(AT_FDCWD, abs_name, path, flags);
|
|
|
|
kfree(abs_name);
|
|
|
|
return dent;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_remove_acl_xattrs(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
char *name, *xattr_list = NULL;
|
|
|
|
ssize_t xattr_list_len;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
|
|
|
|
if (xattr_list_len < 0) {
|
|
|
|
goto out;
|
|
|
|
} else if (!xattr_list_len) {
|
|
|
|
ksmbd_debug(SMB, "empty xattr in the file\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (name = xattr_list; name - xattr_list < xattr_list_len;
|
2021-05-26 16:57:12 +08:00
|
|
|
name += strlen(name) + 1) {
|
2021-03-16 09:50:04 +08:00
|
|
|
ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
|
|
|
|
|
|
|
|
if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
|
2021-03-30 13:25:35 +08:00
|
|
|
sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1) ||
|
2021-03-16 09:50:04 +08:00
|
|
|
!strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
|
2021-03-30 13:25:35 +08:00
|
|
|
sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1)) {
|
2022-09-22 23:17:16 +08:00
|
|
|
err = vfs_remove_acl(user_ns, dentry, name);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (err)
|
|
|
|
ksmbd_debug(SMB,
|
2021-05-26 16:57:12 +08:00
|
|
|
"remove acl xattr failed : %s\n", name);
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
2021-04-02 11:47:14 +08:00
|
|
|
kvfree(xattr_list);
|
2021-03-16 09:50:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_remove_sd_xattrs(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
char *name, *xattr_list = NULL;
|
|
|
|
ssize_t xattr_list_len;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
|
|
|
|
if (xattr_list_len < 0) {
|
|
|
|
goto out;
|
|
|
|
} else if (!xattr_list_len) {
|
|
|
|
ksmbd_debug(SMB, "empty xattr in the file\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (name = xattr_list; name - xattr_list < xattr_list_len;
|
|
|
|
name += strlen(name) + 1) {
|
|
|
|
ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
|
|
|
|
|
|
|
|
if (!strncmp(name, XATTR_NAME_SD, XATTR_NAME_SD_LEN)) {
|
2021-06-30 17:25:53 +08:00
|
|
|
err = ksmbd_vfs_remove_xattr(user_ns, dentry, name);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (err)
|
|
|
|
ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
2021-04-02 11:47:14 +08:00
|
|
|
kvfree(xattr_list);
|
2021-03-16 09:50:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct user_namespace *user_ns,
|
|
|
|
struct inode *inode,
|
2021-05-26 16:57:12 +08:00
|
|
|
int acl_type)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct xattr_smb_acl *smb_acl = NULL;
|
|
|
|
struct posix_acl *posix_acls;
|
|
|
|
struct posix_acl_entry *pa_entry;
|
|
|
|
struct xattr_acl_entry *xa_entry;
|
|
|
|
int i;
|
|
|
|
|
2021-08-13 07:15:33 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
|
|
|
|
return NULL;
|
|
|
|
|
2022-09-22 23:17:00 +08:00
|
|
|
posix_acls = get_inode_acl(inode, acl_type);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (!posix_acls)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
smb_acl = kzalloc(sizeof(struct xattr_smb_acl) +
|
|
|
|
sizeof(struct xattr_acl_entry) * posix_acls->a_count,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!smb_acl)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
smb_acl->count = posix_acls->a_count;
|
|
|
|
pa_entry = posix_acls->a_entries;
|
|
|
|
xa_entry = smb_acl->entries;
|
|
|
|
for (i = 0; i < posix_acls->a_count; i++, pa_entry++, xa_entry++) {
|
|
|
|
switch (pa_entry->e_tag) {
|
|
|
|
case ACL_USER:
|
|
|
|
xa_entry->type = SMB_ACL_USER;
|
2021-08-23 23:13:51 +08:00
|
|
|
xa_entry->uid = posix_acl_uid_translate(user_ns, pa_entry);
|
2021-03-16 09:50:04 +08:00
|
|
|
break;
|
|
|
|
case ACL_USER_OBJ:
|
|
|
|
xa_entry->type = SMB_ACL_USER_OBJ;
|
|
|
|
break;
|
|
|
|
case ACL_GROUP:
|
|
|
|
xa_entry->type = SMB_ACL_GROUP;
|
2021-08-23 23:13:51 +08:00
|
|
|
xa_entry->gid = posix_acl_gid_translate(user_ns, pa_entry);
|
2021-03-16 09:50:04 +08:00
|
|
|
break;
|
|
|
|
case ACL_GROUP_OBJ:
|
|
|
|
xa_entry->type = SMB_ACL_GROUP_OBJ;
|
|
|
|
break;
|
|
|
|
case ACL_OTHER:
|
|
|
|
xa_entry->type = SMB_ACL_OTHER;
|
|
|
|
break;
|
|
|
|
case ACL_MASK:
|
|
|
|
xa_entry->type = SMB_ACL_MASK;
|
|
|
|
break;
|
|
|
|
default:
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("unknown type : 0x%x\n", pa_entry->e_tag);
|
2021-03-16 09:50:04 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pa_entry->e_perm & ACL_READ)
|
|
|
|
xa_entry->perm |= SMB_ACL_READ;
|
|
|
|
if (pa_entry->e_perm & ACL_WRITE)
|
|
|
|
xa_entry->perm |= SMB_ACL_WRITE;
|
|
|
|
if (pa_entry->e_perm & ACL_EXECUTE)
|
|
|
|
xa_entry->perm |= SMB_ACL_EXECUTE;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
posix_acl_release(posix_acls);
|
|
|
|
return smb_acl;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
|
|
|
|
struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry,
|
2021-05-26 16:57:12 +08:00
|
|
|
struct smb_ntsd *pntsd, int len)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct ndr sd_ndr = {0}, acl_ndr = {0};
|
|
|
|
struct xattr_ntacl acl = {0};
|
|
|
|
struct xattr_smb_acl *smb_acl, *def_smb_acl = NULL;
|
2021-04-15 09:29:39 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
acl.version = 4;
|
|
|
|
acl.hash_type = XATTR_SD_HASH_TYPE_SHA256;
|
2021-04-15 09:29:39 +08:00
|
|
|
acl.current_time = ksmbd_UnixTimeToNT(current_time(inode));
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
memcpy(acl.desc, "posix_acl", 9);
|
|
|
|
acl.desc_len = 10;
|
|
|
|
|
|
|
|
pntsd->osidoffset =
|
|
|
|
cpu_to_le32(le32_to_cpu(pntsd->osidoffset) + NDR_NTSD_OFFSETOF);
|
|
|
|
pntsd->gsidoffset =
|
|
|
|
cpu_to_le32(le32_to_cpu(pntsd->gsidoffset) + NDR_NTSD_OFFSETOF);
|
|
|
|
pntsd->dacloffset =
|
|
|
|
cpu_to_le32(le32_to_cpu(pntsd->dacloffset) + NDR_NTSD_OFFSETOF);
|
|
|
|
|
|
|
|
acl.sd_buf = (char *)pntsd;
|
|
|
|
acl.sd_size = len;
|
|
|
|
|
|
|
|
rc = ksmbd_gen_sd_hash(conn, acl.sd_buf, acl.sd_size, acl.hash);
|
|
|
|
if (rc) {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("failed to generate hash for ndr acl\n");
|
2021-03-16 09:50:04 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
|
|
|
|
ACL_TYPE_ACCESS);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (S_ISDIR(inode->i_mode))
|
2021-06-30 17:25:53 +08:00
|
|
|
def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
|
2021-05-26 16:57:12 +08:00
|
|
|
ACL_TYPE_DEFAULT);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
rc = ndr_encode_posix_acl(&acl_ndr, user_ns, inode,
|
|
|
|
smb_acl, def_smb_acl);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (rc) {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("failed to encode ndr to posix acl\n");
|
2021-03-16 09:50:04 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset,
|
2021-05-26 16:57:12 +08:00
|
|
|
acl.posix_acl_hash);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (rc) {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("failed to generate hash for ndr acl\n");
|
2021-03-16 09:50:04 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ndr_encode_v4_ntacl(&sd_ndr, &acl);
|
|
|
|
if (rc) {
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("failed to encode ndr to posix acl\n");
|
2021-03-16 09:50:04 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
rc = ksmbd_vfs_setxattr(user_ns, dentry,
|
|
|
|
XATTR_NAME_SD, sd_ndr.data,
|
2021-05-26 16:57:12 +08:00
|
|
|
sd_ndr.offset, 0);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (rc < 0)
|
2021-06-28 14:23:19 +08:00
|
|
|
pr_err("Failed to store XATTR ntacl :%d\n", rc);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
kfree(sd_ndr.data);
|
|
|
|
out:
|
|
|
|
kfree(acl_ndr.data);
|
|
|
|
kfree(smb_acl);
|
|
|
|
kfree(def_smb_acl);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
|
|
|
|
struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry,
|
2021-05-26 16:57:12 +08:00
|
|
|
struct smb_ntsd **pntsd)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct ndr n;
|
2021-07-13 16:12:41 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
|
|
|
struct ndr acl_ndr = {0};
|
|
|
|
struct xattr_ntacl acl;
|
|
|
|
struct xattr_smb_acl *smb_acl = NULL, *def_smb_acl = NULL;
|
|
|
|
__u8 cmp_hash[XATTR_SD_HASH_SIZE] = {0};
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
rc = ksmbd_vfs_getxattr(user_ns, dentry, XATTR_NAME_SD, &n.data);
|
2021-07-13 16:12:41 +08:00
|
|
|
if (rc <= 0)
|
|
|
|
return rc;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-07-13 16:12:41 +08:00
|
|
|
n.length = rc;
|
|
|
|
rc = ndr_decode_v4_ntacl(&n, &acl);
|
|
|
|
if (rc)
|
|
|
|
goto free_n_data;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-07-13 16:12:41 +08:00
|
|
|
smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
|
|
|
|
ACL_TYPE_ACCESS);
|
|
|
|
if (S_ISDIR(inode->i_mode))
|
|
|
|
def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(user_ns, inode,
|
|
|
|
ACL_TYPE_DEFAULT);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-07-13 16:12:41 +08:00
|
|
|
rc = ndr_encode_posix_acl(&acl_ndr, user_ns, inode, smb_acl,
|
|
|
|
def_smb_acl);
|
|
|
|
if (rc) {
|
|
|
|
pr_err("failed to encode ndr to posix acl\n");
|
|
|
|
goto out_free;
|
|
|
|
}
|
2021-03-16 09:50:04 +08:00
|
|
|
|
2021-07-13 16:12:41 +08:00
|
|
|
rc = ksmbd_gen_sd_hash(conn, acl_ndr.data, acl_ndr.offset, cmp_hash);
|
|
|
|
if (rc) {
|
|
|
|
pr_err("failed to generate hash for ndr acl\n");
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(cmp_hash, acl.posix_acl_hash, XATTR_SD_HASH_SIZE)) {
|
|
|
|
pr_err("hash value diff\n");
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pntsd = acl.sd_buf;
|
2022-08-02 06:28:51 +08:00
|
|
|
if (acl.sd_size < sizeof(struct smb_ntsd)) {
|
|
|
|
pr_err("sd size is invalid\n");
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
2021-07-13 16:12:41 +08:00
|
|
|
(*pntsd)->osidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->osidoffset) -
|
|
|
|
NDR_NTSD_OFFSETOF);
|
|
|
|
(*pntsd)->gsidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->gsidoffset) -
|
|
|
|
NDR_NTSD_OFFSETOF);
|
|
|
|
(*pntsd)->dacloffset = cpu_to_le32(le32_to_cpu((*pntsd)->dacloffset) -
|
|
|
|
NDR_NTSD_OFFSETOF);
|
|
|
|
|
|
|
|
rc = acl.sd_size;
|
|
|
|
out_free:
|
|
|
|
kfree(acl_ndr.data);
|
|
|
|
kfree(smb_acl);
|
|
|
|
kfree(def_smb_acl);
|
|
|
|
if (rc < 0) {
|
|
|
|
kfree(acl.sd_buf);
|
|
|
|
*pntsd = NULL;
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
2021-07-13 16:12:41 +08:00
|
|
|
free_n_data:
|
|
|
|
kfree(n.data);
|
2021-03-16 09:50:04 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_set_dos_attrib_xattr(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry,
|
2021-05-26 16:57:12 +08:00
|
|
|
struct xattr_dos_attrib *da)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct ndr n;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = ndr_encode_dos_attr(&n, da);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
err = ksmbd_vfs_setxattr(user_ns, dentry, XATTR_NAME_DOS_ATTRIBUTE,
|
2021-05-26 16:57:12 +08:00
|
|
|
(void *)n.data, n.offset, 0);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (err)
|
|
|
|
ksmbd_debug(SMB, "failed to store dos attribute in xattr\n");
|
|
|
|
kfree(n.data);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_get_dos_attrib_xattr(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry,
|
2021-05-26 16:57:12 +08:00
|
|
|
struct xattr_dos_attrib *da)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct ndr n;
|
|
|
|
int err;
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
err = ksmbd_vfs_getxattr(user_ns, dentry, XATTR_NAME_DOS_ATTRIBUTE,
|
2021-05-26 16:57:12 +08:00
|
|
|
(char **)&n.data);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (err > 0) {
|
|
|
|
n.length = err;
|
|
|
|
if (ndr_decode_dos_attr(&n, da))
|
|
|
|
err = -EINVAL;
|
2021-04-02 11:47:14 +08:00
|
|
|
kfree(n.data);
|
2021-03-30 13:25:35 +08:00
|
|
|
} else {
|
2021-03-16 09:50:04 +08:00
|
|
|
ksmbd_debug(SMB, "failed to load dos attribute in xattr\n");
|
2021-03-30 13:25:35 +08:00
|
|
|
}
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ksmbd_vfs_init_kstat() - convert unix stat information to smb stat format
|
|
|
|
* @p: destination buffer
|
|
|
|
* @ksmbd_kstat: ksmbd kstat wrapper
|
|
|
|
*/
|
|
|
|
void *ksmbd_vfs_init_kstat(char **p, struct ksmbd_kstat *ksmbd_kstat)
|
|
|
|
{
|
|
|
|
struct file_directory_info *info = (struct file_directory_info *)(*p);
|
|
|
|
struct kstat *kstat = ksmbd_kstat->kstat;
|
|
|
|
u64 time;
|
|
|
|
|
|
|
|
info->FileIndex = 0;
|
|
|
|
info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
|
|
|
|
time = ksmbd_UnixTimeToNT(kstat->atime);
|
|
|
|
info->LastAccessTime = cpu_to_le64(time);
|
|
|
|
time = ksmbd_UnixTimeToNT(kstat->mtime);
|
|
|
|
info->LastWriteTime = cpu_to_le64(time);
|
|
|
|
time = ksmbd_UnixTimeToNT(kstat->ctime);
|
|
|
|
info->ChangeTime = cpu_to_le64(time);
|
|
|
|
|
2021-11-03 07:45:52 +08:00
|
|
|
if (ksmbd_kstat->file_attributes & FILE_ATTRIBUTE_DIRECTORY_LE) {
|
2021-03-16 09:50:04 +08:00
|
|
|
info->EndOfFile = 0;
|
|
|
|
info->AllocationSize = 0;
|
|
|
|
} else {
|
|
|
|
info->EndOfFile = cpu_to_le64(kstat->size);
|
|
|
|
info->AllocationSize = cpu_to_le64(kstat->blocks << 9);
|
|
|
|
}
|
|
|
|
info->ExtFileAttributes = ksmbd_kstat->file_attributes;
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work,
|
|
|
|
struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry,
|
2021-05-26 16:57:12 +08:00
|
|
|
struct ksmbd_kstat *ksmbd_kstat)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
u64 time;
|
|
|
|
int rc;
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
generic_fillattr(user_ns, d_inode(dentry), ksmbd_kstat->kstat);
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
|
|
|
|
ksmbd_kstat->create_time = time;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set default value for the case that store dos attributes is not yes
|
|
|
|
* or that acl is disable in server's filesystem and the config is yes.
|
|
|
|
*/
|
|
|
|
if (S_ISDIR(ksmbd_kstat->kstat->mode))
|
2021-11-03 07:45:52 +08:00
|
|
|
ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_DIRECTORY_LE;
|
2021-03-16 09:50:04 +08:00
|
|
|
else
|
2021-11-03 07:45:52 +08:00
|
|
|
ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_ARCHIVE_LE;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
if (test_share_config_flag(work->tcon->share_conf,
|
2021-03-30 13:25:35 +08:00
|
|
|
KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
|
2021-03-16 09:50:04 +08:00
|
|
|
struct xattr_dos_attrib da;
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
rc = ksmbd_vfs_get_dos_attrib_xattr(user_ns, dentry, &da);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (rc > 0) {
|
|
|
|
ksmbd_kstat->file_attributes = cpu_to_le32(da.attr);
|
|
|
|
ksmbd_kstat->create_time = da.create_time;
|
2021-03-30 13:25:35 +08:00
|
|
|
} else {
|
2021-03-16 09:50:04 +08:00
|
|
|
ksmbd_debug(VFS, "fail to load dos attribute.\n");
|
2021-03-30 13:25:35 +08:00
|
|
|
}
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
ssize_t ksmbd_vfs_casexattr_len(struct user_namespace *user_ns,
|
|
|
|
struct dentry *dentry, char *attr_name,
|
2021-05-26 16:57:12 +08:00
|
|
|
int attr_name_len)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
char *name, *xattr_list = NULL;
|
|
|
|
ssize_t value_len = -ENOENT, xattr_list_len;
|
|
|
|
|
|
|
|
xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
|
|
|
|
if (xattr_list_len <= 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
for (name = xattr_list; name - xattr_list < xattr_list_len;
|
|
|
|
name += strlen(name) + 1) {
|
|
|
|
ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
|
|
|
|
if (strncasecmp(attr_name, name, attr_name_len))
|
|
|
|
continue;
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
value_len = ksmbd_vfs_xattr_len(user_ns, dentry, name);
|
2021-03-16 09:50:04 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2021-04-02 11:47:14 +08:00
|
|
|
kvfree(xattr_list);
|
2021-03-16 09:50:04 +08:00
|
|
|
return value_len;
|
|
|
|
}
|
|
|
|
|
2021-03-30 13:25:35 +08:00
|
|
|
int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name,
|
2021-05-26 16:57:12 +08:00
|
|
|
size_t *xattr_stream_name_size, int s_type)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
2021-07-07 18:15:32 +08:00
|
|
|
char *type, *buf;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
if (s_type == DIR_STREAM)
|
|
|
|
type = ":$INDEX_ALLOCATION";
|
|
|
|
else
|
|
|
|
type = ":$DATA";
|
|
|
|
|
2021-07-07 18:15:32 +08:00
|
|
|
buf = kasprintf(GFP_KERNEL, "%s%s%s",
|
|
|
|
XATTR_NAME_STREAM, stream_name, type);
|
|
|
|
if (!buf)
|
2021-03-16 09:50:04 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2021-07-07 18:15:32 +08:00
|
|
|
*xattr_stream_name = buf;
|
|
|
|
*xattr_stream_name_size = strlen(buf) + 1;
|
2021-03-16 09:50:04 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work,
|
2021-05-26 16:57:12 +08:00
|
|
|
struct ksmbd_file *src_fp,
|
|
|
|
struct ksmbd_file *dst_fp,
|
|
|
|
struct srv_copychunk *chunks,
|
|
|
|
unsigned int chunk_count,
|
|
|
|
unsigned int *chunk_count_written,
|
|
|
|
unsigned int *chunk_size_written,
|
|
|
|
loff_t *total_size_written)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
loff_t src_off, dst_off, src_file_size;
|
|
|
|
size_t len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*chunk_count_written = 0;
|
|
|
|
*chunk_size_written = 0;
|
|
|
|
*total_size_written = 0;
|
|
|
|
|
|
|
|
if (!(src_fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
|
2022-01-31 10:11:02 +08:00
|
|
|
pr_err("no right to read(%pD)\n", src_fp->filp);
|
2021-03-16 09:50:04 +08:00
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
if (!(dst_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) {
|
2022-01-31 10:11:02 +08:00
|
|
|
pr_err("no right to write(%pD)\n", dst_fp->filp);
|
2021-03-16 09:50:04 +08:00
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ksmbd_stream_fd(src_fp) || ksmbd_stream_fd(dst_fp))
|
|
|
|
return -EBADF;
|
|
|
|
|
|
|
|
smb_break_all_levII_oplock(work, dst_fp, 1);
|
|
|
|
|
2021-03-30 11:43:18 +08:00
|
|
|
if (!work->tcon->posix_extensions) {
|
|
|
|
for (i = 0; i < chunk_count; i++) {
|
|
|
|
src_off = le64_to_cpu(chunks[i].SourceOffset);
|
|
|
|
dst_off = le64_to_cpu(chunks[i].TargetOffset);
|
|
|
|
len = le32_to_cpu(chunks[i].Length);
|
|
|
|
|
|
|
|
if (check_lock_range(src_fp->filp, src_off,
|
2021-03-30 13:25:35 +08:00
|
|
|
src_off + len - 1, READ))
|
2021-03-30 11:43:18 +08:00
|
|
|
return -EAGAIN;
|
|
|
|
if (check_lock_range(dst_fp->filp, dst_off,
|
2021-03-30 13:25:35 +08:00
|
|
|
dst_off + len - 1, WRITE))
|
2021-03-30 11:43:18 +08:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
src_file_size = i_size_read(file_inode(src_fp->filp));
|
|
|
|
|
|
|
|
for (i = 0; i < chunk_count; i++) {
|
|
|
|
src_off = le64_to_cpu(chunks[i].SourceOffset);
|
|
|
|
dst_off = le64_to_cpu(chunks[i].TargetOffset);
|
|
|
|
len = le32_to_cpu(chunks[i].Length);
|
|
|
|
|
|
|
|
if (src_off + len > src_file_size)
|
|
|
|
return -E2BIG;
|
|
|
|
|
2021-06-18 09:28:00 +08:00
|
|
|
ret = vfs_copy_file_range(src_fp->filp, src_off,
|
|
|
|
dst_fp->filp, dst_off, len, 0);
|
2022-07-01 03:58:49 +08:00
|
|
|
if (ret == -EOPNOTSUPP || ret == -EXDEV)
|
2022-11-18 04:52:49 +08:00
|
|
|
ret = vfs_copy_file_range(src_fp->filp, src_off,
|
|
|
|
dst_fp->filp, dst_off, len,
|
|
|
|
COPY_FILE_SPLICE);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
*chunk_count_written += 1;
|
|
|
|
*total_size_written += ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-10 08:34:20 +08:00
|
|
|
void ksmbd_vfs_posix_lock_wait(struct file_lock *flock)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
2021-07-10 08:34:20 +08:00
|
|
|
wait_event(flock->fl_wait, !flock->fl_blocker);
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ksmbd_vfs_posix_lock_wait_timeout(struct file_lock *flock, long timeout)
|
|
|
|
{
|
|
|
|
return wait_event_interruptible_timeout(flock->fl_wait,
|
|
|
|
!flock->fl_blocker,
|
|
|
|
timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ksmbd_vfs_posix_lock_unblock(struct file_lock *flock)
|
|
|
|
{
|
|
|
|
locks_delete_block(flock);
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_set_init_posix_acl(struct user_namespace *user_ns,
|
2022-09-23 16:29:39 +08:00
|
|
|
struct dentry *dentry)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct posix_acl_state acl_state;
|
|
|
|
struct posix_acl *acls;
|
2022-09-23 16:29:39 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
2021-03-16 09:50:04 +08:00
|
|
|
int rc;
|
|
|
|
|
2021-08-13 07:15:33 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2021-03-16 09:50:04 +08:00
|
|
|
ksmbd_debug(SMB, "Set posix acls\n");
|
|
|
|
rc = init_acl_state(&acl_state, 1);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Set default owner group */
|
|
|
|
acl_state.owner.allow = (inode->i_mode & 0700) >> 6;
|
|
|
|
acl_state.group.allow = (inode->i_mode & 0070) >> 3;
|
|
|
|
acl_state.other.allow = inode->i_mode & 0007;
|
|
|
|
acl_state.users->aces[acl_state.users->n].uid = inode->i_uid;
|
|
|
|
acl_state.users->aces[acl_state.users->n++].perms.allow =
|
|
|
|
acl_state.owner.allow;
|
|
|
|
acl_state.groups->aces[acl_state.groups->n].gid = inode->i_gid;
|
|
|
|
acl_state.groups->aces[acl_state.groups->n++].perms.allow =
|
|
|
|
acl_state.group.allow;
|
|
|
|
acl_state.mask.allow = 0x07;
|
|
|
|
|
2021-06-22 10:42:29 +08:00
|
|
|
acls = posix_acl_alloc(6, GFP_KERNEL);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (!acls) {
|
|
|
|
free_acl_state(&acl_state);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
posix_state_to_acl(&acl_state, acls->a_entries);
|
2022-09-23 16:29:39 +08:00
|
|
|
rc = set_posix_acl(user_ns, dentry, ACL_TYPE_ACCESS, acls);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (rc < 0)
|
|
|
|
ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
|
2021-05-26 16:57:12 +08:00
|
|
|
rc);
|
2021-03-16 09:50:04 +08:00
|
|
|
else if (S_ISDIR(inode->i_mode)) {
|
|
|
|
posix_state_to_acl(&acl_state, acls->a_entries);
|
2022-09-23 16:29:39 +08:00
|
|
|
rc = set_posix_acl(user_ns, dentry, ACL_TYPE_DEFAULT, acls);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (rc < 0)
|
|
|
|
ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
|
2021-05-26 16:57:12 +08:00
|
|
|
rc);
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
free_acl_state(&acl_state);
|
|
|
|
posix_acl_release(acls);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-06-30 17:25:53 +08:00
|
|
|
int ksmbd_vfs_inherit_posix_acl(struct user_namespace *user_ns,
|
2022-09-23 16:29:39 +08:00
|
|
|
struct dentry *dentry, struct inode *parent_inode)
|
2021-03-16 09:50:04 +08:00
|
|
|
{
|
|
|
|
struct posix_acl *acls;
|
|
|
|
struct posix_acl_entry *pace;
|
2022-09-23 16:29:39 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
2021-03-16 09:50:04 +08:00
|
|
|
int rc, i;
|
|
|
|
|
2021-08-13 07:15:33 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_FS_POSIX_ACL))
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2022-09-22 23:17:00 +08:00
|
|
|
acls = get_inode_acl(parent_inode, ACL_TYPE_DEFAULT);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (!acls)
|
|
|
|
return -ENOENT;
|
|
|
|
pace = acls->a_entries;
|
|
|
|
|
|
|
|
for (i = 0; i < acls->a_count; i++, pace++) {
|
|
|
|
if (pace->e_tag == ACL_MASK) {
|
|
|
|
pace->e_perm = 0x07;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 16:29:39 +08:00
|
|
|
rc = set_posix_acl(user_ns, dentry, ACL_TYPE_ACCESS, acls);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (rc < 0)
|
|
|
|
ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
|
2021-05-26 16:57:12 +08:00
|
|
|
rc);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (S_ISDIR(inode->i_mode)) {
|
2022-09-23 16:29:39 +08:00
|
|
|
rc = set_posix_acl(user_ns, dentry, ACL_TYPE_DEFAULT,
|
2021-06-22 10:42:29 +08:00
|
|
|
acls);
|
2021-03-16 09:50:04 +08:00
|
|
|
if (rc < 0)
|
|
|
|
ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
|
2021-05-26 16:57:12 +08:00
|
|
|
rc);
|
2021-03-16 09:50:04 +08:00
|
|
|
}
|
|
|
|
posix_acl_release(acls);
|
|
|
|
return rc;
|
|
|
|
}
|