mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-28 06:34:12 +08: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
...
585 lines
13 KiB
C
585 lines
13 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* linux/fs/affs/namei.c
|
|
*
|
|
* (c) 1996 Hans-Joachim Widmaier - Rewritten
|
|
*
|
|
* (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
|
|
*
|
|
* (C) 1991 Linus Torvalds - minix filesystem
|
|
*/
|
|
|
|
#include "affs.h"
|
|
#include <linux/exportfs.h>
|
|
|
|
typedef int (*toupper_t)(int);
|
|
|
|
/* Simple toupper() for DOS\1 */
|
|
|
|
static int
|
|
affs_toupper(int ch)
|
|
{
|
|
return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch;
|
|
}
|
|
|
|
/* International toupper() for DOS\3 ("international") */
|
|
|
|
static int
|
|
affs_intl_toupper(int ch)
|
|
{
|
|
return (ch >= 'a' && ch <= 'z') || (ch >= 0xE0
|
|
&& ch <= 0xFE && ch != 0xF7) ?
|
|
ch - ('a' - 'A') : ch;
|
|
}
|
|
|
|
static inline toupper_t
|
|
affs_get_toupper(struct super_block *sb)
|
|
{
|
|
return affs_test_opt(AFFS_SB(sb)->s_flags, SF_INTL) ?
|
|
affs_intl_toupper : affs_toupper;
|
|
}
|
|
|
|
/*
|
|
* Note: the dentry argument is the parent dentry.
|
|
*/
|
|
static inline int
|
|
__affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr, toupper_t toupper, bool notruncate)
|
|
{
|
|
const u8 *name = qstr->name;
|
|
unsigned long hash;
|
|
int retval;
|
|
u32 len;
|
|
|
|
retval = affs_check_name(qstr->name, qstr->len, notruncate);
|
|
if (retval)
|
|
return retval;
|
|
|
|
hash = init_name_hash(dentry);
|
|
len = min(qstr->len, AFFSNAMEMAX);
|
|
for (; len > 0; name++, len--)
|
|
hash = partial_name_hash(toupper(*name), hash);
|
|
qstr->hash = end_name_hash(hash);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
|
|
{
|
|
return __affs_hash_dentry(dentry, qstr, affs_toupper,
|
|
affs_nofilenametruncate(dentry));
|
|
|
|
}
|
|
|
|
static int
|
|
affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
|
|
{
|
|
return __affs_hash_dentry(dentry, qstr, affs_intl_toupper,
|
|
affs_nofilenametruncate(dentry));
|
|
|
|
}
|
|
|
|
static inline int __affs_compare_dentry(unsigned int len,
|
|
const char *str, const struct qstr *name, toupper_t toupper,
|
|
bool notruncate)
|
|
{
|
|
const u8 *aname = str;
|
|
const u8 *bname = name->name;
|
|
|
|
/*
|
|
* 'str' is the name of an already existing dentry, so the name
|
|
* must be valid. 'name' must be validated first.
|
|
*/
|
|
|
|
if (affs_check_name(name->name, name->len, notruncate))
|
|
return 1;
|
|
|
|
/*
|
|
* If the names are longer than the allowed 30 chars,
|
|
* the excess is ignored, so their length may differ.
|
|
*/
|
|
if (len >= AFFSNAMEMAX) {
|
|
if (name->len < AFFSNAMEMAX)
|
|
return 1;
|
|
len = AFFSNAMEMAX;
|
|
} else if (len != name->len)
|
|
return 1;
|
|
|
|
for (; len > 0; len--)
|
|
if (toupper(*aname++) != toupper(*bname++))
|
|
return 1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
affs_compare_dentry(const struct dentry *dentry,
|
|
unsigned int len, const char *str, const struct qstr *name)
|
|
{
|
|
|
|
return __affs_compare_dentry(len, str, name, affs_toupper,
|
|
affs_nofilenametruncate(dentry));
|
|
}
|
|
|
|
static int
|
|
affs_intl_compare_dentry(const struct dentry *dentry,
|
|
unsigned int len, const char *str, const struct qstr *name)
|
|
{
|
|
return __affs_compare_dentry(len, str, name, affs_intl_toupper,
|
|
affs_nofilenametruncate(dentry));
|
|
|
|
}
|
|
|
|
/*
|
|
* NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
|
|
*/
|
|
|
|
static inline int
|
|
affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
|
|
{
|
|
const u8 *name = dentry->d_name.name;
|
|
int len = dentry->d_name.len;
|
|
|
|
if (len >= AFFSNAMEMAX) {
|
|
if (*name2 < AFFSNAMEMAX)
|
|
return 0;
|
|
len = AFFSNAMEMAX;
|
|
} else if (len != *name2)
|
|
return 0;
|
|
|
|
for (name2++; len > 0; len--)
|
|
if (toupper(*name++) != toupper(*name2++))
|
|
return 0;
|
|
return 1;
|
|
}
|
|
|
|
int
|
|
affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
|
|
{
|
|
toupper_t toupper = affs_get_toupper(sb);
|
|
u32 hash;
|
|
|
|
hash = len = min(len, AFFSNAMEMAX);
|
|
for (; len > 0; len--)
|
|
hash = (hash * 13 + toupper(*name++)) & 0x7ff;
|
|
|
|
return hash % AFFS_SB(sb)->s_hashsize;
|
|
}
|
|
|
|
static struct buffer_head *
|
|
affs_find_entry(struct inode *dir, struct dentry *dentry)
|
|
{
|
|
struct super_block *sb = dir->i_sb;
|
|
struct buffer_head *bh;
|
|
toupper_t toupper = affs_get_toupper(sb);
|
|
u32 key;
|
|
|
|
pr_debug("%s(\"%pd\")\n", __func__, dentry);
|
|
|
|
bh = affs_bread(sb, dir->i_ino);
|
|
if (!bh)
|
|
return ERR_PTR(-EIO);
|
|
|
|
key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
|
|
|
|
for (;;) {
|
|
affs_brelse(bh);
|
|
if (key == 0)
|
|
return NULL;
|
|
bh = affs_bread(sb, key);
|
|
if (!bh)
|
|
return ERR_PTR(-EIO);
|
|
if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
|
|
return bh;
|
|
key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
|
|
}
|
|
}
|
|
|
|
struct dentry *
|
|
affs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
|
|
{
|
|
struct super_block *sb = dir->i_sb;
|
|
struct buffer_head *bh;
|
|
struct inode *inode = NULL;
|
|
struct dentry *res;
|
|
|
|
pr_debug("%s(\"%pd\")\n", __func__, dentry);
|
|
|
|
affs_lock_dir(dir);
|
|
bh = affs_find_entry(dir, dentry);
|
|
if (IS_ERR(bh)) {
|
|
affs_unlock_dir(dir);
|
|
return ERR_CAST(bh);
|
|
}
|
|
if (bh) {
|
|
u32 ino = bh->b_blocknr;
|
|
|
|
/* store the real header ino in d_fsdata for faster lookups */
|
|
dentry->d_fsdata = (void *)(long)ino;
|
|
switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
|
|
//link to dirs disabled
|
|
//case ST_LINKDIR:
|
|
case ST_LINKFILE:
|
|
ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
|
|
}
|
|
affs_brelse(bh);
|
|
inode = affs_iget(sb, ino);
|
|
}
|
|
res = d_splice_alias(inode, dentry);
|
|
if (!IS_ERR_OR_NULL(res))
|
|
res->d_fsdata = dentry->d_fsdata;
|
|
affs_unlock_dir(dir);
|
|
return res;
|
|
}
|
|
|
|
int
|
|
affs_unlink(struct inode *dir, struct dentry *dentry)
|
|
{
|
|
pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
|
|
d_inode(dentry)->i_ino, dentry);
|
|
|
|
return affs_remove_header(dentry);
|
|
}
|
|
|
|
int
|
|
affs_create(struct user_namespace *mnt_userns, struct inode *dir,
|
|
struct dentry *dentry, umode_t mode, bool excl)
|
|
{
|
|
struct super_block *sb = dir->i_sb;
|
|
struct inode *inode;
|
|
int error;
|
|
|
|
pr_debug("%s(%lu,\"%pd\",0%ho)\n",
|
|
__func__, dir->i_ino, dentry, mode);
|
|
|
|
inode = affs_new_inode(dir);
|
|
if (!inode)
|
|
return -ENOSPC;
|
|
|
|
inode->i_mode = mode;
|
|
affs_mode_to_prot(inode);
|
|
mark_inode_dirty(inode);
|
|
|
|
inode->i_op = &affs_file_inode_operations;
|
|
inode->i_fop = &affs_file_operations;
|
|
inode->i_mapping->a_ops = affs_test_opt(AFFS_SB(sb)->s_flags, SF_OFS) ?
|
|
&affs_aops_ofs : &affs_aops;
|
|
error = affs_add_entry(dir, inode, dentry, ST_FILE);
|
|
if (error) {
|
|
clear_nlink(inode);
|
|
iput(inode);
|
|
return error;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
affs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
|
|
struct dentry *dentry, umode_t mode)
|
|
{
|
|
struct inode *inode;
|
|
int error;
|
|
|
|
pr_debug("%s(%lu,\"%pd\",0%ho)\n",
|
|
__func__, dir->i_ino, dentry, mode);
|
|
|
|
inode = affs_new_inode(dir);
|
|
if (!inode)
|
|
return -ENOSPC;
|
|
|
|
inode->i_mode = S_IFDIR | mode;
|
|
affs_mode_to_prot(inode);
|
|
|
|
inode->i_op = &affs_dir_inode_operations;
|
|
inode->i_fop = &affs_dir_operations;
|
|
|
|
error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
|
|
if (error) {
|
|
clear_nlink(inode);
|
|
mark_inode_dirty(inode);
|
|
iput(inode);
|
|
return error;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
affs_rmdir(struct inode *dir, struct dentry *dentry)
|
|
{
|
|
pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
|
|
d_inode(dentry)->i_ino, dentry);
|
|
|
|
return affs_remove_header(dentry);
|
|
}
|
|
|
|
int
|
|
affs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
|
|
struct dentry *dentry, const char *symname)
|
|
{
|
|
struct super_block *sb = dir->i_sb;
|
|
struct buffer_head *bh;
|
|
struct inode *inode;
|
|
char *p;
|
|
int i, maxlen, error;
|
|
char c, lc;
|
|
|
|
pr_debug("%s(%lu,\"%pd\" -> \"%s\")\n",
|
|
__func__, dir->i_ino, dentry, symname);
|
|
|
|
maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1;
|
|
inode = affs_new_inode(dir);
|
|
if (!inode)
|
|
return -ENOSPC;
|
|
|
|
inode->i_op = &affs_symlink_inode_operations;
|
|
inode_nohighmem(inode);
|
|
inode->i_data.a_ops = &affs_symlink_aops;
|
|
inode->i_mode = S_IFLNK | 0777;
|
|
affs_mode_to_prot(inode);
|
|
|
|
error = -EIO;
|
|
bh = affs_bread(sb, inode->i_ino);
|
|
if (!bh)
|
|
goto err;
|
|
i = 0;
|
|
p = (char *)AFFS_HEAD(bh)->table;
|
|
lc = '/';
|
|
if (*symname == '/') {
|
|
struct affs_sb_info *sbi = AFFS_SB(sb);
|
|
while (*symname == '/')
|
|
symname++;
|
|
spin_lock(&sbi->symlink_lock);
|
|
while (sbi->s_volume[i]) /* Cannot overflow */
|
|
*p++ = sbi->s_volume[i++];
|
|
spin_unlock(&sbi->symlink_lock);
|
|
}
|
|
while (i < maxlen && (c = *symname++)) {
|
|
if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
|
|
*p++ = '/';
|
|
i++;
|
|
symname += 2;
|
|
lc = '/';
|
|
} else if (c == '.' && lc == '/' && *symname == '/') {
|
|
symname++;
|
|
lc = '/';
|
|
} else {
|
|
*p++ = c;
|
|
lc = c;
|
|
i++;
|
|
}
|
|
if (lc == '/')
|
|
while (*symname == '/')
|
|
symname++;
|
|
}
|
|
*p = 0;
|
|
inode->i_size = i + 1;
|
|
mark_buffer_dirty_inode(bh, inode);
|
|
affs_brelse(bh);
|
|
mark_inode_dirty(inode);
|
|
|
|
error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK);
|
|
if (error)
|
|
goto err;
|
|
|
|
return 0;
|
|
|
|
err:
|
|
clear_nlink(inode);
|
|
mark_inode_dirty(inode);
|
|
iput(inode);
|
|
return error;
|
|
}
|
|
|
|
int
|
|
affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
|
|
{
|
|
struct inode *inode = d_inode(old_dentry);
|
|
|
|
pr_debug("%s(%lu, %lu, \"%pd\")\n", __func__, inode->i_ino, dir->i_ino,
|
|
dentry);
|
|
|
|
return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
|
|
}
|
|
|
|
static int
|
|
affs_rename(struct inode *old_dir, struct dentry *old_dentry,
|
|
struct inode *new_dir, struct dentry *new_dentry)
|
|
{
|
|
struct super_block *sb = old_dir->i_sb;
|
|
struct buffer_head *bh = NULL;
|
|
int retval;
|
|
|
|
retval = affs_check_name(new_dentry->d_name.name,
|
|
new_dentry->d_name.len,
|
|
affs_nofilenametruncate(old_dentry));
|
|
|
|
if (retval)
|
|
return retval;
|
|
|
|
/* Unlink destination if it already exists */
|
|
if (d_really_is_positive(new_dentry)) {
|
|
retval = affs_remove_header(new_dentry);
|
|
if (retval)
|
|
return retval;
|
|
}
|
|
|
|
bh = affs_bread(sb, d_inode(old_dentry)->i_ino);
|
|
if (!bh)
|
|
return -EIO;
|
|
|
|
/* Remove header from its parent directory. */
|
|
affs_lock_dir(old_dir);
|
|
retval = affs_remove_hash(old_dir, bh);
|
|
affs_unlock_dir(old_dir);
|
|
if (retval)
|
|
goto done;
|
|
|
|
/* And insert it into the new directory with the new name. */
|
|
affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
|
|
affs_fix_checksum(sb, bh);
|
|
affs_lock_dir(new_dir);
|
|
retval = affs_insert_hash(new_dir, bh);
|
|
affs_unlock_dir(new_dir);
|
|
/* TODO: move it back to old_dir, if error? */
|
|
|
|
done:
|
|
mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
|
|
affs_brelse(bh);
|
|
return retval;
|
|
}
|
|
|
|
static int
|
|
affs_xrename(struct inode *old_dir, struct dentry *old_dentry,
|
|
struct inode *new_dir, struct dentry *new_dentry)
|
|
{
|
|
|
|
struct super_block *sb = old_dir->i_sb;
|
|
struct buffer_head *bh_old = NULL;
|
|
struct buffer_head *bh_new = NULL;
|
|
int retval;
|
|
|
|
bh_old = affs_bread(sb, d_inode(old_dentry)->i_ino);
|
|
if (!bh_old)
|
|
return -EIO;
|
|
|
|
bh_new = affs_bread(sb, d_inode(new_dentry)->i_ino);
|
|
if (!bh_new) {
|
|
affs_brelse(bh_old);
|
|
return -EIO;
|
|
}
|
|
|
|
/* Remove old header from its parent directory. */
|
|
affs_lock_dir(old_dir);
|
|
retval = affs_remove_hash(old_dir, bh_old);
|
|
affs_unlock_dir(old_dir);
|
|
if (retval)
|
|
goto done;
|
|
|
|
/* Remove new header from its parent directory. */
|
|
affs_lock_dir(new_dir);
|
|
retval = affs_remove_hash(new_dir, bh_new);
|
|
affs_unlock_dir(new_dir);
|
|
if (retval)
|
|
goto done;
|
|
|
|
/* Insert old into the new directory with the new name. */
|
|
affs_copy_name(AFFS_TAIL(sb, bh_old)->name, new_dentry);
|
|
affs_fix_checksum(sb, bh_old);
|
|
affs_lock_dir(new_dir);
|
|
retval = affs_insert_hash(new_dir, bh_old);
|
|
affs_unlock_dir(new_dir);
|
|
|
|
/* Insert new into the old directory with the old name. */
|
|
affs_copy_name(AFFS_TAIL(sb, bh_new)->name, old_dentry);
|
|
affs_fix_checksum(sb, bh_new);
|
|
affs_lock_dir(old_dir);
|
|
retval = affs_insert_hash(old_dir, bh_new);
|
|
affs_unlock_dir(old_dir);
|
|
done:
|
|
mark_buffer_dirty_inode(bh_old, new_dir);
|
|
mark_buffer_dirty_inode(bh_new, old_dir);
|
|
affs_brelse(bh_old);
|
|
affs_brelse(bh_new);
|
|
return retval;
|
|
}
|
|
|
|
int affs_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)
|
|
{
|
|
|
|
if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
|
|
return -EINVAL;
|
|
|
|
pr_debug("%s(old=%lu,\"%pd\" to new=%lu,\"%pd\")\n", __func__,
|
|
old_dir->i_ino, old_dentry, new_dir->i_ino, new_dentry);
|
|
|
|
if (flags & RENAME_EXCHANGE)
|
|
return affs_xrename(old_dir, old_dentry, new_dir, new_dentry);
|
|
|
|
return affs_rename(old_dir, old_dentry, new_dir, new_dentry);
|
|
}
|
|
|
|
static struct dentry *affs_get_parent(struct dentry *child)
|
|
{
|
|
struct inode *parent;
|
|
struct buffer_head *bh;
|
|
|
|
bh = affs_bread(child->d_sb, d_inode(child)->i_ino);
|
|
if (!bh)
|
|
return ERR_PTR(-EIO);
|
|
|
|
parent = affs_iget(child->d_sb,
|
|
be32_to_cpu(AFFS_TAIL(child->d_sb, bh)->parent));
|
|
brelse(bh);
|
|
if (IS_ERR(parent))
|
|
return ERR_CAST(parent);
|
|
|
|
return d_obtain_alias(parent);
|
|
}
|
|
|
|
static struct inode *affs_nfs_get_inode(struct super_block *sb, u64 ino,
|
|
u32 generation)
|
|
{
|
|
struct inode *inode;
|
|
|
|
if (!affs_validblock(sb, ino))
|
|
return ERR_PTR(-ESTALE);
|
|
|
|
inode = affs_iget(sb, ino);
|
|
if (IS_ERR(inode))
|
|
return ERR_CAST(inode);
|
|
|
|
return inode;
|
|
}
|
|
|
|
static struct dentry *affs_fh_to_dentry(struct super_block *sb, struct fid *fid,
|
|
int fh_len, int fh_type)
|
|
{
|
|
return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
|
|
affs_nfs_get_inode);
|
|
}
|
|
|
|
static struct dentry *affs_fh_to_parent(struct super_block *sb, struct fid *fid,
|
|
int fh_len, int fh_type)
|
|
{
|
|
return generic_fh_to_parent(sb, fid, fh_len, fh_type,
|
|
affs_nfs_get_inode);
|
|
}
|
|
|
|
const struct export_operations affs_export_ops = {
|
|
.fh_to_dentry = affs_fh_to_dentry,
|
|
.fh_to_parent = affs_fh_to_parent,
|
|
.get_parent = affs_get_parent,
|
|
};
|
|
|
|
const struct dentry_operations affs_dentry_operations = {
|
|
.d_hash = affs_hash_dentry,
|
|
.d_compare = affs_compare_dentry,
|
|
};
|
|
|
|
const struct dentry_operations affs_intl_dentry_operations = {
|
|
.d_hash = affs_intl_hash_dentry,
|
|
.d_compare = affs_intl_compare_dentry,
|
|
};
|