mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-10-29 23:53:32 +00:00
7d6beb71da
-----BEGIN PGP SIGNATURE-----
iHUEABYKAB0WIQRAhzRXHqcMeLMyaSiRxhvAZXjcogUCYCegywAKCRCRxhvAZXjc
ouJ6AQDlf+7jCQlQdeKKoN9QDFfMzG1ooemat36EpRRTONaGuAD8D9A4sUsG4+5f
4IU5Lj9oY4DEmF8HenbWK2ZHsesL2Qg=
=yPaw
-----END PGP SIGNATURE-----
Merge tag 'idmapped-mounts-v5.12' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux
Pull idmapped mounts from Christian Brauner:
"This introduces idmapped mounts which has been in the making for some
time. Simply put, different mounts can expose the same file or
directory with different ownership. This initial implementation comes
with ports for fat, ext4 and with Christoph's port for xfs with more
filesystems being actively worked on by independent people and
maintainers.
Idmapping mounts handle a wide range of long standing use-cases. Here
are just a few:
- Idmapped mounts make it possible to easily share files between
multiple users or multiple machines especially in complex
scenarios. For example, idmapped mounts will be used in the
implementation of portable home directories in
systemd-homed.service(8) where they allow users to move their home
directory to an external storage device and use it on multiple
computers where they are assigned different uids and gids. This
effectively makes it possible to assign random uids and gids at
login time.
- It is possible to share files from the host with unprivileged
containers without having to change ownership permanently through
chown(2).
- It is possible to idmap a container's rootfs and without having to
mangle every file. For example, Chromebooks use it to share the
user's Download folder with their unprivileged containers in their
Linux subsystem.
- It is possible to share files between containers with
non-overlapping idmappings.
- Filesystem that lack a proper concept of ownership such as fat can
use idmapped mounts to implement discretionary access (DAC)
permission checking.
- They allow users to efficiently changing ownership on a per-mount
basis without having to (recursively) chown(2) all files. In
contrast to chown (2) changing ownership of large sets of files is
instantenous with idmapped mounts. This is especially useful when
ownership of a whole root filesystem of a virtual machine or
container is changed. With idmapped mounts a single syscall
mount_setattr syscall will be sufficient to change the ownership of
all files.
- Idmapped mounts always take the current ownership into account as
idmappings specify what a given uid or gid is supposed to be mapped
to. This contrasts with the chown(2) syscall which cannot by itself
take the current ownership of the files it changes into account. It
simply changes the ownership to the specified uid and gid. This is
especially problematic when recursively chown(2)ing a large set of
files which is commong with the aforementioned portable home
directory and container and vm scenario.
- Idmapped mounts allow to change ownership locally, restricting it
to specific mounts, and temporarily as the ownership changes only
apply as long as the mount exists.
Several userspace projects have either already put up patches and
pull-requests for this feature or will do so should you decide to pull
this:
- systemd: In a wide variety of scenarios but especially right away
in their implementation of portable home directories.
https://systemd.io/HOME_DIRECTORY/
- container runtimes: containerd, runC, LXD:To share data between
host and unprivileged containers, unprivileged and privileged
containers, etc. The pull request for idmapped mounts support in
containerd, the default Kubernetes runtime is already up for quite
a while now: https://github.com/containerd/containerd/pull/4734
- The virtio-fs developers and several users have expressed interest
in using this feature with virtual machines once virtio-fs is
ported.
- ChromeOS: Sharing host-directories with unprivileged containers.
I've tightly synced with all those projects and all of those listed
here have also expressed their need/desire for this feature on the
mailing list. For more info on how people use this there's a bunch of
talks about this too. Here's just two recent ones:
https://www.cncf.io/wp-content/uploads/2020/12/Rootless-Containers-in-Gitpod.pdf
https://fosdem.org/2021/schedule/event/containers_idmap/
This comes with an extensive xfstests suite covering both ext4 and
xfs:
https://git.kernel.org/brauner/xfstests-dev/h/idmapped_mounts
It covers truncation, creation, opening, xattrs, vfscaps, setid
execution, setgid inheritance and more both with idmapped and
non-idmapped mounts. It already helped to discover an unrelated xfs
setgid inheritance bug which has since been fixed in mainline. It will
be sent for inclusion with the xfstests project should you decide to
merge this.
In order to support per-mount idmappings vfsmounts are marked with
user namespaces. The idmapping of the user namespace will be used to
map the ids of vfs objects when they are accessed through that mount.
By default all vfsmounts are marked with the initial user namespace.
The initial user namespace is used to indicate that a mount is not
idmapped. All operations behave as before and this is verified in the
testsuite.
Based on prior discussions we want to attach the whole user namespace
and not just a dedicated idmapping struct. This allows us to reuse all
the helpers that already exist for dealing with idmappings instead of
introducing a whole new range of helpers. In addition, if we decide in
the future that we are confident enough to enable unprivileged users
to setup idmapped mounts the permission checking can take into account
whether the caller is privileged in the user namespace the mount is
currently marked with.
The user namespace the mount will be marked with can be specified by
passing a file descriptor refering to the user namespace as an
argument to the new mount_setattr() syscall together with the new
MOUNT_ATTR_IDMAP flag. The system call follows the openat2() pattern
of extensibility.
The following conditions must be met in order to create an idmapped
mount:
- The caller must currently have the CAP_SYS_ADMIN capability in the
user namespace the underlying filesystem has been mounted in.
- The underlying filesystem must support idmapped mounts.
- The mount must not already be idmapped. This also implies that the
idmapping of a mount cannot be altered once it has been idmapped.
- The mount must be a detached/anonymous mount, i.e. it must have
been created by calling open_tree() with the OPEN_TREE_CLONE flag
and it must not already have been visible in the filesystem.
The last two points guarantee easier semantics for userspace and the
kernel and make the implementation significantly simpler.
By default vfsmounts are marked with the initial user namespace and no
behavioral or performance changes are observed.
The manpage with a detailed description can be found here:
1d7b902e28
In order to support idmapped mounts, filesystems need to be changed
and mark themselves with the FS_ALLOW_IDMAP flag in fs_flags. The
patches to convert individual filesystem are not very large or
complicated overall as can be seen from the included fat, ext4, and
xfs ports. Patches for other filesystems are actively worked on and
will be sent out separately. The xfstestsuite can be used to verify
that port has been done correctly.
The mount_setattr() syscall is motivated independent of the idmapped
mounts patches and it's been around since July 2019. One of the most
valuable features of the new mount api is the ability to perform
mounts based on file descriptors only.
Together with the lookup restrictions available in the openat2()
RESOLVE_* flag namespace which we added in v5.6 this is the first time
we are close to hardened and race-free (e.g. symlinks) mounting and
path resolution.
While userspace has started porting to the new mount api to mount
proper filesystems and create new bind-mounts it is currently not
possible to change mount options of an already existing bind mount in
the new mount api since the mount_setattr() syscall is missing.
With the addition of the mount_setattr() syscall we remove this last
restriction and userspace can now fully port to the new mount api,
covering every use-case the old mount api could. We also add the
crucial ability to recursively change mount options for a whole mount
tree, both removing and adding mount options at the same time. This
syscall has been requested multiple times by various people and
projects.
There is a simple tool available at
https://github.com/brauner/mount-idmapped
that allows to create idmapped mounts so people can play with this
patch series. I'll add support for the regular mount binary should you
decide to pull this in the following weeks:
Here's an example to a simple idmapped mount of another user's home
directory:
u1001@f2-vm:/$ sudo ./mount --idmap both:1000:1001:1 /home/ubuntu/ /mnt
u1001@f2-vm:/$ ls -al /home/ubuntu/
total 28
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 28 22:07 .
drwxr-xr-x 4 root root 4096 Oct 28 04:00 ..
-rw------- 1 ubuntu ubuntu 3154 Oct 28 22:12 .bash_history
-rw-r--r-- 1 ubuntu ubuntu 220 Feb 25 2020 .bash_logout
-rw-r--r-- 1 ubuntu ubuntu 3771 Feb 25 2020 .bashrc
-rw-r--r-- 1 ubuntu ubuntu 807 Feb 25 2020 .profile
-rw-r--r-- 1 ubuntu ubuntu 0 Oct 16 16:11 .sudo_as_admin_successful
-rw------- 1 ubuntu ubuntu 1144 Oct 28 00:43 .viminfo
u1001@f2-vm:/$ ls -al /mnt/
total 28
drwxr-xr-x 2 u1001 u1001 4096 Oct 28 22:07 .
drwxr-xr-x 29 root root 4096 Oct 28 22:01 ..
-rw------- 1 u1001 u1001 3154 Oct 28 22:12 .bash_history
-rw-r--r-- 1 u1001 u1001 220 Feb 25 2020 .bash_logout
-rw-r--r-- 1 u1001 u1001 3771 Feb 25 2020 .bashrc
-rw-r--r-- 1 u1001 u1001 807 Feb 25 2020 .profile
-rw-r--r-- 1 u1001 u1001 0 Oct 16 16:11 .sudo_as_admin_successful
-rw------- 1 u1001 u1001 1144 Oct 28 00:43 .viminfo
u1001@f2-vm:/$ touch /mnt/my-file
u1001@f2-vm:/$ setfacl -m u:1001:rwx /mnt/my-file
u1001@f2-vm:/$ sudo setcap -n 1001 cap_net_raw+ep /mnt/my-file
u1001@f2-vm:/$ ls -al /mnt/my-file
-rw-rwxr--+ 1 u1001 u1001 0 Oct 28 22:14 /mnt/my-file
u1001@f2-vm:/$ ls -al /home/ubuntu/my-file
-rw-rwxr--+ 1 ubuntu ubuntu 0 Oct 28 22:14 /home/ubuntu/my-file
u1001@f2-vm:/$ getfacl /mnt/my-file
getfacl: Removing leading '/' from absolute path names
# file: mnt/my-file
# owner: u1001
# group: u1001
user::rw-
user:u1001:rwx
group::rw-
mask::rwx
other::r--
u1001@f2-vm:/$ getfacl /home/ubuntu/my-file
getfacl: Removing leading '/' from absolute path names
# file: home/ubuntu/my-file
# owner: ubuntu
# group: ubuntu
user::rw-
user:ubuntu:rwx
group::rw-
mask::rwx
other::r--"
* tag 'idmapped-mounts-v5.12' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux: (41 commits)
xfs: remove the possibly unused mp variable in xfs_file_compat_ioctl
xfs: support idmapped mounts
ext4: support idmapped mounts
fat: handle idmapped mounts
tests: add mount_setattr() selftests
fs: introduce MOUNT_ATTR_IDMAP
fs: add mount_setattr()
fs: add attr_flags_to_mnt_flags helper
fs: split out functions to hold writers
namespace: only take read lock in do_reconfigure_mnt()
mount: make {lock,unlock}_mount_hash() static
namespace: take lock_mount_hash() directly when changing flags
nfs: do not export idmapped mounts
overlayfs: do not mount on top of idmapped mounts
ecryptfs: do not mount on top of idmapped mounts
ima: handle idmapped mounts
apparmor: handle idmapped mounts
fs: make helpers idmap mount aware
exec: handle idmapped mounts
would_dump: handle idmapped mounts
...
1010 lines
21 KiB
C
1010 lines
21 KiB
C
/*
|
|
* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
|
* Licensed under the GPL
|
|
*
|
|
* Ported the filesystem routines to 2.5.
|
|
* 2003-02-10 Petr Baudis <pasky@ucw.cz>
|
|
*/
|
|
|
|
#include <linux/fs.h>
|
|
#include <linux/magic.h>
|
|
#include <linux/module.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/pagemap.h>
|
|
#include <linux/statfs.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/seq_file.h>
|
|
#include <linux/mount.h>
|
|
#include <linux/namei.h>
|
|
#include "hostfs.h"
|
|
#include <init.h>
|
|
#include <kern.h>
|
|
|
|
struct hostfs_inode_info {
|
|
int fd;
|
|
fmode_t mode;
|
|
struct inode vfs_inode;
|
|
struct mutex open_mutex;
|
|
};
|
|
|
|
static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
|
|
{
|
|
return list_entry(inode, struct hostfs_inode_info, vfs_inode);
|
|
}
|
|
|
|
#define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
|
|
|
|
static struct kmem_cache *hostfs_inode_cache;
|
|
|
|
/* Changed in hostfs_args before the kernel starts running */
|
|
static char *root_ino = "";
|
|
static int append = 0;
|
|
|
|
static const struct inode_operations hostfs_iops;
|
|
static const struct inode_operations hostfs_dir_iops;
|
|
static const struct inode_operations hostfs_link_iops;
|
|
|
|
#ifndef MODULE
|
|
static int __init hostfs_args(char *options, int *add)
|
|
{
|
|
char *ptr;
|
|
|
|
ptr = strchr(options, ',');
|
|
if (ptr != NULL)
|
|
*ptr++ = '\0';
|
|
if (*options != '\0')
|
|
root_ino = options;
|
|
|
|
options = ptr;
|
|
while (options) {
|
|
ptr = strchr(options, ',');
|
|
if (ptr != NULL)
|
|
*ptr++ = '\0';
|
|
if (*options != '\0') {
|
|
if (!strcmp(options, "append"))
|
|
append = 1;
|
|
else printf("hostfs_args - unsupported option - %s\n",
|
|
options);
|
|
}
|
|
options = ptr;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
__uml_setup("hostfs=", hostfs_args,
|
|
"hostfs=<root dir>,<flags>,...\n"
|
|
" This is used to set hostfs parameters. The root directory argument\n"
|
|
" is used to confine all hostfs mounts to within the specified directory\n"
|
|
" tree on the host. If this isn't specified, then a user inside UML can\n"
|
|
" mount anything on the host that's accessible to the user that's running\n"
|
|
" it.\n"
|
|
" The only flag currently supported is 'append', which specifies that all\n"
|
|
" files opened by hostfs will be opened in append mode.\n\n"
|
|
);
|
|
#endif
|
|
|
|
static char *__dentry_name(struct dentry *dentry, char *name)
|
|
{
|
|
char *p = dentry_path_raw(dentry, name, PATH_MAX);
|
|
char *root;
|
|
size_t len;
|
|
|
|
root = dentry->d_sb->s_fs_info;
|
|
len = strlen(root);
|
|
if (IS_ERR(p)) {
|
|
__putname(name);
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
* This function relies on the fact that dentry_path_raw() will place
|
|
* the path name at the end of the provided buffer.
|
|
*/
|
|
BUG_ON(p + strlen(p) + 1 != name + PATH_MAX);
|
|
|
|
strlcpy(name, root, PATH_MAX);
|
|
if (len > p - name) {
|
|
__putname(name);
|
|
return NULL;
|
|
}
|
|
|
|
if (p > name + len)
|
|
strcpy(name + len, p);
|
|
|
|
return name;
|
|
}
|
|
|
|
static char *dentry_name(struct dentry *dentry)
|
|
{
|
|
char *name = __getname();
|
|
if (!name)
|
|
return NULL;
|
|
|
|
return __dentry_name(dentry, name);
|
|
}
|
|
|
|
static char *inode_name(struct inode *ino)
|
|
{
|
|
struct dentry *dentry;
|
|
char *name;
|
|
|
|
dentry = d_find_alias(ino);
|
|
if (!dentry)
|
|
return NULL;
|
|
|
|
name = dentry_name(dentry);
|
|
|
|
dput(dentry);
|
|
|
|
return name;
|
|
}
|
|
|
|
static char *follow_link(char *link)
|
|
{
|
|
char *name, *resolved, *end;
|
|
int n;
|
|
|
|
name = __getname();
|
|
if (!name) {
|
|
n = -ENOMEM;
|
|
goto out_free;
|
|
}
|
|
|
|
n = hostfs_do_readlink(link, name, PATH_MAX);
|
|
if (n < 0)
|
|
goto out_free;
|
|
else if (n == PATH_MAX) {
|
|
n = -E2BIG;
|
|
goto out_free;
|
|
}
|
|
|
|
if (*name == '/')
|
|
return name;
|
|
|
|
end = strrchr(link, '/');
|
|
if (end == NULL)
|
|
return name;
|
|
|
|
*(end + 1) = '\0';
|
|
|
|
resolved = kasprintf(GFP_KERNEL, "%s%s", link, name);
|
|
if (resolved == NULL) {
|
|
n = -ENOMEM;
|
|
goto out_free;
|
|
}
|
|
|
|
__putname(name);
|
|
kfree(link);
|
|
return resolved;
|
|
|
|
out_free:
|
|
__putname(name);
|
|
return ERR_PTR(n);
|
|
}
|
|
|
|
static struct inode *hostfs_iget(struct super_block *sb)
|
|
{
|
|
struct inode *inode = new_inode(sb);
|
|
if (!inode)
|
|
return ERR_PTR(-ENOMEM);
|
|
return inode;
|
|
}
|
|
|
|
static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
|
|
{
|
|
/*
|
|
* do_statfs uses struct statfs64 internally, but the linux kernel
|
|
* struct statfs still has 32-bit versions for most of these fields,
|
|
* so we convert them here
|
|
*/
|
|
int err;
|
|
long long f_blocks;
|
|
long long f_bfree;
|
|
long long f_bavail;
|
|
long long f_files;
|
|
long long f_ffree;
|
|
|
|
err = do_statfs(dentry->d_sb->s_fs_info,
|
|
&sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
|
|
&f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
|
|
&sf->f_namelen);
|
|
if (err)
|
|
return err;
|
|
sf->f_blocks = f_blocks;
|
|
sf->f_bfree = f_bfree;
|
|
sf->f_bavail = f_bavail;
|
|
sf->f_files = f_files;
|
|
sf->f_ffree = f_ffree;
|
|
sf->f_type = HOSTFS_SUPER_MAGIC;
|
|
return 0;
|
|
}
|
|
|
|
static struct inode *hostfs_alloc_inode(struct super_block *sb)
|
|
{
|
|
struct hostfs_inode_info *hi;
|
|
|
|
hi = kmem_cache_alloc(hostfs_inode_cache, GFP_KERNEL_ACCOUNT);
|
|
if (hi == NULL)
|
|
return NULL;
|
|
hi->fd = -1;
|
|
hi->mode = 0;
|
|
inode_init_once(&hi->vfs_inode);
|
|
mutex_init(&hi->open_mutex);
|
|
return &hi->vfs_inode;
|
|
}
|
|
|
|
static void hostfs_evict_inode(struct inode *inode)
|
|
{
|
|
truncate_inode_pages_final(&inode->i_data);
|
|
clear_inode(inode);
|
|
if (HOSTFS_I(inode)->fd != -1) {
|
|
close_file(&HOSTFS_I(inode)->fd);
|
|
HOSTFS_I(inode)->fd = -1;
|
|
}
|
|
}
|
|
|
|
static void hostfs_free_inode(struct inode *inode)
|
|
{
|
|
kmem_cache_free(hostfs_inode_cache, HOSTFS_I(inode));
|
|
}
|
|
|
|
static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
|
|
{
|
|
const char *root_path = root->d_sb->s_fs_info;
|
|
size_t offset = strlen(root_ino) + 1;
|
|
|
|
if (strlen(root_path) > offset)
|
|
seq_show_option(seq, root_path + offset, NULL);
|
|
|
|
if (append)
|
|
seq_puts(seq, ",append");
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct super_operations hostfs_sbops = {
|
|
.alloc_inode = hostfs_alloc_inode,
|
|
.free_inode = hostfs_free_inode,
|
|
.evict_inode = hostfs_evict_inode,
|
|
.statfs = hostfs_statfs,
|
|
.show_options = hostfs_show_options,
|
|
};
|
|
|
|
static int hostfs_readdir(struct file *file, struct dir_context *ctx)
|
|
{
|
|
void *dir;
|
|
char *name;
|
|
unsigned long long next, ino;
|
|
int error, len;
|
|
unsigned int type;
|
|
|
|
name = dentry_name(file->f_path.dentry);
|
|
if (name == NULL)
|
|
return -ENOMEM;
|
|
dir = open_dir(name, &error);
|
|
__putname(name);
|
|
if (dir == NULL)
|
|
return -error;
|
|
next = ctx->pos;
|
|
seek_dir(dir, next);
|
|
while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
|
|
if (!dir_emit(ctx, name, len, ino, type))
|
|
break;
|
|
ctx->pos = next;
|
|
}
|
|
close_dir(dir);
|
|
return 0;
|
|
}
|
|
|
|
static int hostfs_open(struct inode *ino, struct file *file)
|
|
{
|
|
char *name;
|
|
fmode_t mode;
|
|
int err;
|
|
int r, w, fd;
|
|
|
|
mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
|
|
if ((mode & HOSTFS_I(ino)->mode) == mode)
|
|
return 0;
|
|
|
|
mode |= HOSTFS_I(ino)->mode;
|
|
|
|
retry:
|
|
r = w = 0;
|
|
|
|
if (mode & FMODE_READ)
|
|
r = 1;
|
|
if (mode & FMODE_WRITE)
|
|
r = w = 1;
|
|
|
|
name = dentry_name(d_real(file->f_path.dentry, file->f_inode));
|
|
if (name == NULL)
|
|
return -ENOMEM;
|
|
|
|
fd = open_file(name, r, w, append);
|
|
__putname(name);
|
|
if (fd < 0)
|
|
return fd;
|
|
|
|
mutex_lock(&HOSTFS_I(ino)->open_mutex);
|
|
/* somebody else had handled it first? */
|
|
if ((mode & HOSTFS_I(ino)->mode) == mode) {
|
|
mutex_unlock(&HOSTFS_I(ino)->open_mutex);
|
|
close_file(&fd);
|
|
return 0;
|
|
}
|
|
if ((mode | HOSTFS_I(ino)->mode) != mode) {
|
|
mode |= HOSTFS_I(ino)->mode;
|
|
mutex_unlock(&HOSTFS_I(ino)->open_mutex);
|
|
close_file(&fd);
|
|
goto retry;
|
|
}
|
|
if (HOSTFS_I(ino)->fd == -1) {
|
|
HOSTFS_I(ino)->fd = fd;
|
|
} else {
|
|
err = replace_file(fd, HOSTFS_I(ino)->fd);
|
|
close_file(&fd);
|
|
if (err < 0) {
|
|
mutex_unlock(&HOSTFS_I(ino)->open_mutex);
|
|
return err;
|
|
}
|
|
}
|
|
HOSTFS_I(ino)->mode = mode;
|
|
mutex_unlock(&HOSTFS_I(ino)->open_mutex);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int hostfs_file_release(struct inode *inode, struct file *file)
|
|
{
|
|
filemap_write_and_wait(inode->i_mapping);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
|
|
int datasync)
|
|
{
|
|
struct inode *inode = file->f_mapping->host;
|
|
int ret;
|
|
|
|
ret = file_write_and_wait_range(file, start, end);
|
|
if (ret)
|
|
return ret;
|
|
|
|
inode_lock(inode);
|
|
ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
|
|
inode_unlock(inode);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static const struct file_operations hostfs_file_fops = {
|
|
.llseek = generic_file_llseek,
|
|
.splice_read = generic_file_splice_read,
|
|
.read_iter = generic_file_read_iter,
|
|
.write_iter = generic_file_write_iter,
|
|
.mmap = generic_file_mmap,
|
|
.open = hostfs_open,
|
|
.release = hostfs_file_release,
|
|
.fsync = hostfs_fsync,
|
|
};
|
|
|
|
static const struct file_operations hostfs_dir_fops = {
|
|
.llseek = generic_file_llseek,
|
|
.iterate_shared = hostfs_readdir,
|
|
.read = generic_read_dir,
|
|
.open = hostfs_open,
|
|
.fsync = hostfs_fsync,
|
|
};
|
|
|
|
static int hostfs_writepage(struct page *page, struct writeback_control *wbc)
|
|
{
|
|
struct address_space *mapping = page->mapping;
|
|
struct inode *inode = mapping->host;
|
|
char *buffer;
|
|
loff_t base = page_offset(page);
|
|
int count = PAGE_SIZE;
|
|
int end_index = inode->i_size >> PAGE_SHIFT;
|
|
int err;
|
|
|
|
if (page->index >= end_index)
|
|
count = inode->i_size & (PAGE_SIZE-1);
|
|
|
|
buffer = kmap(page);
|
|
|
|
err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
|
|
if (err != count) {
|
|
ClearPageUptodate(page);
|
|
goto out;
|
|
}
|
|
|
|
if (base > inode->i_size)
|
|
inode->i_size = base;
|
|
|
|
if (PageError(page))
|
|
ClearPageError(page);
|
|
err = 0;
|
|
|
|
out:
|
|
kunmap(page);
|
|
|
|
unlock_page(page);
|
|
return err;
|
|
}
|
|
|
|
static int hostfs_readpage(struct file *file, struct page *page)
|
|
{
|
|
char *buffer;
|
|
loff_t start = page_offset(page);
|
|
int bytes_read, ret = 0;
|
|
|
|
buffer = kmap(page);
|
|
bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
|
|
PAGE_SIZE);
|
|
if (bytes_read < 0) {
|
|
ClearPageUptodate(page);
|
|
SetPageError(page);
|
|
ret = bytes_read;
|
|
goto out;
|
|
}
|
|
|
|
memset(buffer + bytes_read, 0, PAGE_SIZE - bytes_read);
|
|
|
|
ClearPageError(page);
|
|
SetPageUptodate(page);
|
|
|
|
out:
|
|
flush_dcache_page(page);
|
|
kunmap(page);
|
|
unlock_page(page);
|
|
return ret;
|
|
}
|
|
|
|
static int hostfs_write_begin(struct file *file, struct address_space *mapping,
|
|
loff_t pos, unsigned len, unsigned flags,
|
|
struct page **pagep, void **fsdata)
|
|
{
|
|
pgoff_t index = pos >> PAGE_SHIFT;
|
|
|
|
*pagep = grab_cache_page_write_begin(mapping, index, flags);
|
|
if (!*pagep)
|
|
return -ENOMEM;
|
|
return 0;
|
|
}
|
|
|
|
static int hostfs_write_end(struct file *file, struct address_space *mapping,
|
|
loff_t pos, unsigned len, unsigned copied,
|
|
struct page *page, void *fsdata)
|
|
{
|
|
struct inode *inode = mapping->host;
|
|
void *buffer;
|
|
unsigned from = pos & (PAGE_SIZE - 1);
|
|
int err;
|
|
|
|
buffer = kmap(page);
|
|
err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
|
|
kunmap(page);
|
|
|
|
if (!PageUptodate(page) && err == PAGE_SIZE)
|
|
SetPageUptodate(page);
|
|
|
|
/*
|
|
* If err > 0, write_file has added err to pos, so we are comparing
|
|
* i_size against the last byte written.
|
|
*/
|
|
if (err > 0 && (pos > inode->i_size))
|
|
inode->i_size = pos;
|
|
unlock_page(page);
|
|
put_page(page);
|
|
|
|
return err;
|
|
}
|
|
|
|
static const struct address_space_operations hostfs_aops = {
|
|
.writepage = hostfs_writepage,
|
|
.readpage = hostfs_readpage,
|
|
.set_page_dirty = __set_page_dirty_nobuffers,
|
|
.write_begin = hostfs_write_begin,
|
|
.write_end = hostfs_write_end,
|
|
};
|
|
|
|
static int read_name(struct inode *ino, char *name)
|
|
{
|
|
dev_t rdev;
|
|
struct hostfs_stat st;
|
|
int err = stat_file(name, &st, -1);
|
|
if (err)
|
|
return err;
|
|
|
|
/* Reencode maj and min with the kernel encoding.*/
|
|
rdev = MKDEV(st.maj, st.min);
|
|
|
|
switch (st.mode & S_IFMT) {
|
|
case S_IFLNK:
|
|
ino->i_op = &hostfs_link_iops;
|
|
break;
|
|
case S_IFDIR:
|
|
ino->i_op = &hostfs_dir_iops;
|
|
ino->i_fop = &hostfs_dir_fops;
|
|
break;
|
|
case S_IFCHR:
|
|
case S_IFBLK:
|
|
case S_IFIFO:
|
|
case S_IFSOCK:
|
|
init_special_inode(ino, st.mode & S_IFMT, rdev);
|
|
ino->i_op = &hostfs_iops;
|
|
break;
|
|
case S_IFREG:
|
|
ino->i_op = &hostfs_iops;
|
|
ino->i_fop = &hostfs_file_fops;
|
|
ino->i_mapping->a_ops = &hostfs_aops;
|
|
break;
|
|
default:
|
|
return -EIO;
|
|
}
|
|
|
|
ino->i_ino = st.ino;
|
|
ino->i_mode = st.mode;
|
|
set_nlink(ino, st.nlink);
|
|
i_uid_write(ino, st.uid);
|
|
i_gid_write(ino, st.gid);
|
|
ino->i_atime = (struct timespec64){ st.atime.tv_sec, st.atime.tv_nsec };
|
|
ino->i_mtime = (struct timespec64){ st.mtime.tv_sec, st.mtime.tv_nsec };
|
|
ino->i_ctime = (struct timespec64){ st.ctime.tv_sec, st.ctime.tv_nsec };
|
|
ino->i_size = st.size;
|
|
ino->i_blocks = st.blocks;
|
|
return 0;
|
|
}
|
|
|
|
static int hostfs_create(struct user_namespace *mnt_userns, struct inode *dir,
|
|
struct dentry *dentry, umode_t mode, bool excl)
|
|
{
|
|
struct inode *inode;
|
|
char *name;
|
|
int error, fd;
|
|
|
|
inode = hostfs_iget(dir->i_sb);
|
|
if (IS_ERR(inode)) {
|
|
error = PTR_ERR(inode);
|
|
goto out;
|
|
}
|
|
|
|
error = -ENOMEM;
|
|
name = dentry_name(dentry);
|
|
if (name == NULL)
|
|
goto out_put;
|
|
|
|
fd = file_create(name, mode & 0777);
|
|
if (fd < 0)
|
|
error = fd;
|
|
else
|
|
error = read_name(inode, name);
|
|
|
|
__putname(name);
|
|
if (error)
|
|
goto out_put;
|
|
|
|
HOSTFS_I(inode)->fd = fd;
|
|
HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
|
|
d_instantiate(dentry, inode);
|
|
return 0;
|
|
|
|
out_put:
|
|
iput(inode);
|
|
out:
|
|
return error;
|
|
}
|
|
|
|
static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
|
|
unsigned int flags)
|
|
{
|
|
struct inode *inode;
|
|
char *name;
|
|
int err;
|
|
|
|
inode = hostfs_iget(ino->i_sb);
|
|
if (IS_ERR(inode))
|
|
goto out;
|
|
|
|
err = -ENOMEM;
|
|
name = dentry_name(dentry);
|
|
if (name) {
|
|
err = read_name(inode, name);
|
|
__putname(name);
|
|
}
|
|
if (err) {
|
|
iput(inode);
|
|
inode = (err == -ENOENT) ? NULL : ERR_PTR(err);
|
|
}
|
|
out:
|
|
return d_splice_alias(inode, dentry);
|
|
}
|
|
|
|
static int hostfs_link(struct dentry *to, struct inode *ino,
|
|
struct dentry *from)
|
|
{
|
|
char *from_name, *to_name;
|
|
int err;
|
|
|
|
if ((from_name = dentry_name(from)) == NULL)
|
|
return -ENOMEM;
|
|
to_name = dentry_name(to);
|
|
if (to_name == NULL) {
|
|
__putname(from_name);
|
|
return -ENOMEM;
|
|
}
|
|
err = link_file(to_name, from_name);
|
|
__putname(from_name);
|
|
__putname(to_name);
|
|
return err;
|
|
}
|
|
|
|
static int hostfs_unlink(struct inode *ino, struct dentry *dentry)
|
|
{
|
|
char *file;
|
|
int err;
|
|
|
|
if (append)
|
|
return -EPERM;
|
|
|
|
if ((file = dentry_name(dentry)) == NULL)
|
|
return -ENOMEM;
|
|
|
|
err = unlink_file(file);
|
|
__putname(file);
|
|
return err;
|
|
}
|
|
|
|
static int hostfs_symlink(struct user_namespace *mnt_userns, struct inode *ino,
|
|
struct dentry *dentry, const char *to)
|
|
{
|
|
char *file;
|
|
int err;
|
|
|
|
if ((file = dentry_name(dentry)) == NULL)
|
|
return -ENOMEM;
|
|
err = make_symlink(file, to);
|
|
__putname(file);
|
|
return err;
|
|
}
|
|
|
|
static int hostfs_mkdir(struct user_namespace *mnt_userns, struct inode *ino,
|
|
struct dentry *dentry, umode_t mode)
|
|
{
|
|
char *file;
|
|
int err;
|
|
|
|
if ((file = dentry_name(dentry)) == NULL)
|
|
return -ENOMEM;
|
|
err = do_mkdir(file, mode);
|
|
__putname(file);
|
|
return err;
|
|
}
|
|
|
|
static int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
|
|
{
|
|
char *file;
|
|
int err;
|
|
|
|
if ((file = dentry_name(dentry)) == NULL)
|
|
return -ENOMEM;
|
|
err = hostfs_do_rmdir(file);
|
|
__putname(file);
|
|
return err;
|
|
}
|
|
|
|
static int hostfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
|
|
struct dentry *dentry, umode_t mode, dev_t dev)
|
|
{
|
|
struct inode *inode;
|
|
char *name;
|
|
int err;
|
|
|
|
inode = hostfs_iget(dir->i_sb);
|
|
if (IS_ERR(inode)) {
|
|
err = PTR_ERR(inode);
|
|
goto out;
|
|
}
|
|
|
|
err = -ENOMEM;
|
|
name = dentry_name(dentry);
|
|
if (name == NULL)
|
|
goto out_put;
|
|
|
|
init_special_inode(inode, mode, dev);
|
|
err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
|
|
if (err)
|
|
goto out_free;
|
|
|
|
err = read_name(inode, name);
|
|
__putname(name);
|
|
if (err)
|
|
goto out_put;
|
|
|
|
d_instantiate(dentry, inode);
|
|
return 0;
|
|
|
|
out_free:
|
|
__putname(name);
|
|
out_put:
|
|
iput(inode);
|
|
out:
|
|
return err;
|
|
}
|
|
|
|
static int hostfs_rename2(struct user_namespace *mnt_userns,
|
|
struct inode *old_dir, struct dentry *old_dentry,
|
|
struct inode *new_dir, struct dentry *new_dentry,
|
|
unsigned int flags)
|
|
{
|
|
char *old_name, *new_name;
|
|
int err;
|
|
|
|
if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
|
|
return -EINVAL;
|
|
|
|
old_name = dentry_name(old_dentry);
|
|
if (old_name == NULL)
|
|
return -ENOMEM;
|
|
new_name = dentry_name(new_dentry);
|
|
if (new_name == NULL) {
|
|
__putname(old_name);
|
|
return -ENOMEM;
|
|
}
|
|
if (!flags)
|
|
err = rename_file(old_name, new_name);
|
|
else
|
|
err = rename2_file(old_name, new_name, flags);
|
|
|
|
__putname(old_name);
|
|
__putname(new_name);
|
|
return err;
|
|
}
|
|
|
|
static int hostfs_permission(struct user_namespace *mnt_userns,
|
|
struct inode *ino, int desired)
|
|
{
|
|
char *name;
|
|
int r = 0, w = 0, x = 0, err;
|
|
|
|
if (desired & MAY_NOT_BLOCK)
|
|
return -ECHILD;
|
|
|
|
if (desired & MAY_READ) r = 1;
|
|
if (desired & MAY_WRITE) w = 1;
|
|
if (desired & MAY_EXEC) x = 1;
|
|
name = inode_name(ino);
|
|
if (name == NULL)
|
|
return -ENOMEM;
|
|
|
|
if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
|
|
S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
|
|
err = 0;
|
|
else
|
|
err = access_file(name, r, w, x);
|
|
__putname(name);
|
|
if (!err)
|
|
err = generic_permission(&init_user_ns, ino, desired);
|
|
return err;
|
|
}
|
|
|
|
static int hostfs_setattr(struct user_namespace *mnt_userns,
|
|
struct dentry *dentry, struct iattr *attr)
|
|
{
|
|
struct inode *inode = d_inode(dentry);
|
|
struct hostfs_iattr attrs;
|
|
char *name;
|
|
int err;
|
|
|
|
int fd = HOSTFS_I(inode)->fd;
|
|
|
|
err = setattr_prepare(&init_user_ns, dentry, attr);
|
|
if (err)
|
|
return err;
|
|
|
|
if (append)
|
|
attr->ia_valid &= ~ATTR_SIZE;
|
|
|
|
attrs.ia_valid = 0;
|
|
if (attr->ia_valid & ATTR_MODE) {
|
|
attrs.ia_valid |= HOSTFS_ATTR_MODE;
|
|
attrs.ia_mode = attr->ia_mode;
|
|
}
|
|
if (attr->ia_valid & ATTR_UID) {
|
|
attrs.ia_valid |= HOSTFS_ATTR_UID;
|
|
attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
|
|
}
|
|
if (attr->ia_valid & ATTR_GID) {
|
|
attrs.ia_valid |= HOSTFS_ATTR_GID;
|
|
attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
|
|
}
|
|
if (attr->ia_valid & ATTR_SIZE) {
|
|
attrs.ia_valid |= HOSTFS_ATTR_SIZE;
|
|
attrs.ia_size = attr->ia_size;
|
|
}
|
|
if (attr->ia_valid & ATTR_ATIME) {
|
|
attrs.ia_valid |= HOSTFS_ATTR_ATIME;
|
|
attrs.ia_atime = (struct hostfs_timespec)
|
|
{ attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec };
|
|
}
|
|
if (attr->ia_valid & ATTR_MTIME) {
|
|
attrs.ia_valid |= HOSTFS_ATTR_MTIME;
|
|
attrs.ia_mtime = (struct hostfs_timespec)
|
|
{ attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec };
|
|
}
|
|
if (attr->ia_valid & ATTR_CTIME) {
|
|
attrs.ia_valid |= HOSTFS_ATTR_CTIME;
|
|
attrs.ia_ctime = (struct hostfs_timespec)
|
|
{ attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec };
|
|
}
|
|
if (attr->ia_valid & ATTR_ATIME_SET) {
|
|
attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
|
|
}
|
|
if (attr->ia_valid & ATTR_MTIME_SET) {
|
|
attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
|
|
}
|
|
name = dentry_name(dentry);
|
|
if (name == NULL)
|
|
return -ENOMEM;
|
|
err = set_attr(name, &attrs, fd);
|
|
__putname(name);
|
|
if (err)
|
|
return err;
|
|
|
|
if ((attr->ia_valid & ATTR_SIZE) &&
|
|
attr->ia_size != i_size_read(inode))
|
|
truncate_setsize(inode, attr->ia_size);
|
|
|
|
setattr_copy(&init_user_ns, inode, attr);
|
|
mark_inode_dirty(inode);
|
|
return 0;
|
|
}
|
|
|
|
static const struct inode_operations hostfs_iops = {
|
|
.permission = hostfs_permission,
|
|
.setattr = hostfs_setattr,
|
|
};
|
|
|
|
static const struct inode_operations hostfs_dir_iops = {
|
|
.create = hostfs_create,
|
|
.lookup = hostfs_lookup,
|
|
.link = hostfs_link,
|
|
.unlink = hostfs_unlink,
|
|
.symlink = hostfs_symlink,
|
|
.mkdir = hostfs_mkdir,
|
|
.rmdir = hostfs_rmdir,
|
|
.mknod = hostfs_mknod,
|
|
.rename = hostfs_rename2,
|
|
.permission = hostfs_permission,
|
|
.setattr = hostfs_setattr,
|
|
};
|
|
|
|
static const char *hostfs_get_link(struct dentry *dentry,
|
|
struct inode *inode,
|
|
struct delayed_call *done)
|
|
{
|
|
char *link;
|
|
if (!dentry)
|
|
return ERR_PTR(-ECHILD);
|
|
link = kmalloc(PATH_MAX, GFP_KERNEL);
|
|
if (link) {
|
|
char *path = dentry_name(dentry);
|
|
int err = -ENOMEM;
|
|
if (path) {
|
|
err = hostfs_do_readlink(path, link, PATH_MAX);
|
|
if (err == PATH_MAX)
|
|
err = -E2BIG;
|
|
__putname(path);
|
|
}
|
|
if (err < 0) {
|
|
kfree(link);
|
|
return ERR_PTR(err);
|
|
}
|
|
} else {
|
|
return ERR_PTR(-ENOMEM);
|
|
}
|
|
|
|
set_delayed_call(done, kfree_link, link);
|
|
return link;
|
|
}
|
|
|
|
static const struct inode_operations hostfs_link_iops = {
|
|
.get_link = hostfs_get_link,
|
|
};
|
|
|
|
static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
|
|
{
|
|
struct inode *root_inode;
|
|
char *host_root_path, *req_root = d;
|
|
int err;
|
|
|
|
sb->s_blocksize = 1024;
|
|
sb->s_blocksize_bits = 10;
|
|
sb->s_magic = HOSTFS_SUPER_MAGIC;
|
|
sb->s_op = &hostfs_sbops;
|
|
sb->s_d_op = &simple_dentry_operations;
|
|
sb->s_maxbytes = MAX_LFS_FILESIZE;
|
|
|
|
/* NULL is printed as '(null)' by printf(): avoid that. */
|
|
if (req_root == NULL)
|
|
req_root = "";
|
|
|
|
err = -ENOMEM;
|
|
sb->s_fs_info = host_root_path =
|
|
kasprintf(GFP_KERNEL, "%s/%s", root_ino, req_root);
|
|
if (host_root_path == NULL)
|
|
goto out;
|
|
|
|
root_inode = new_inode(sb);
|
|
if (!root_inode)
|
|
goto out;
|
|
|
|
err = read_name(root_inode, host_root_path);
|
|
if (err)
|
|
goto out_put;
|
|
|
|
if (S_ISLNK(root_inode->i_mode)) {
|
|
char *name = follow_link(host_root_path);
|
|
if (IS_ERR(name)) {
|
|
err = PTR_ERR(name);
|
|
goto out_put;
|
|
}
|
|
err = read_name(root_inode, name);
|
|
kfree(name);
|
|
if (err)
|
|
goto out_put;
|
|
}
|
|
|
|
err = -ENOMEM;
|
|
sb->s_root = d_make_root(root_inode);
|
|
if (sb->s_root == NULL)
|
|
goto out;
|
|
|
|
return 0;
|
|
|
|
out_put:
|
|
iput(root_inode);
|
|
out:
|
|
return err;
|
|
}
|
|
|
|
static struct dentry *hostfs_read_sb(struct file_system_type *type,
|
|
int flags, const char *dev_name,
|
|
void *data)
|
|
{
|
|
return mount_nodev(type, flags, data, hostfs_fill_sb_common);
|
|
}
|
|
|
|
static void hostfs_kill_sb(struct super_block *s)
|
|
{
|
|
kill_anon_super(s);
|
|
kfree(s->s_fs_info);
|
|
}
|
|
|
|
static struct file_system_type hostfs_type = {
|
|
.owner = THIS_MODULE,
|
|
.name = "hostfs",
|
|
.mount = hostfs_read_sb,
|
|
.kill_sb = hostfs_kill_sb,
|
|
.fs_flags = 0,
|
|
};
|
|
MODULE_ALIAS_FS("hostfs");
|
|
|
|
static int __init init_hostfs(void)
|
|
{
|
|
hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0);
|
|
if (!hostfs_inode_cache)
|
|
return -ENOMEM;
|
|
return register_filesystem(&hostfs_type);
|
|
}
|
|
|
|
static void __exit exit_hostfs(void)
|
|
{
|
|
unregister_filesystem(&hostfs_type);
|
|
kmem_cache_destroy(hostfs_inode_cache);
|
|
}
|
|
|
|
module_init(init_hostfs)
|
|
module_exit(exit_hostfs)
|
|
MODULE_LICENSE("GPL");
|