mirror of
https://github.com/torvalds/linux
synced 2024-11-05 18:23:50 +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
...
566 lines
14 KiB
C
566 lines
14 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* linux/fs/fat/file.c
|
|
*
|
|
* Written 1992,1993 by Werner Almesberger
|
|
*
|
|
* regular file handling primitives for fat-based filesystems
|
|
*/
|
|
|
|
#include <linux/capability.h>
|
|
#include <linux/module.h>
|
|
#include <linux/compat.h>
|
|
#include <linux/mount.h>
|
|
#include <linux/blkdev.h>
|
|
#include <linux/backing-dev.h>
|
|
#include <linux/fsnotify.h>
|
|
#include <linux/security.h>
|
|
#include <linux/falloc.h>
|
|
#include "fat.h"
|
|
|
|
static long fat_fallocate(struct file *file, int mode,
|
|
loff_t offset, loff_t len);
|
|
|
|
static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
|
|
{
|
|
u32 attr;
|
|
|
|
inode_lock_shared(inode);
|
|
attr = fat_make_attrs(inode);
|
|
inode_unlock_shared(inode);
|
|
|
|
return put_user(attr, user_attr);
|
|
}
|
|
|
|
static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
|
|
{
|
|
struct inode *inode = file_inode(file);
|
|
struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
|
|
int is_dir = S_ISDIR(inode->i_mode);
|
|
u32 attr, oldattr;
|
|
struct iattr ia;
|
|
int err;
|
|
|
|
err = get_user(attr, user_attr);
|
|
if (err)
|
|
goto out;
|
|
|
|
err = mnt_want_write_file(file);
|
|
if (err)
|
|
goto out;
|
|
inode_lock(inode);
|
|
|
|
/*
|
|
* ATTR_VOLUME and ATTR_DIR cannot be changed; this also
|
|
* prevents the user from turning us into a VFAT
|
|
* longname entry. Also, we obviously can't set
|
|
* any of the NTFS attributes in the high 24 bits.
|
|
*/
|
|
attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
|
|
/* Merge in ATTR_VOLUME and ATTR_DIR */
|
|
attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
|
|
(is_dir ? ATTR_DIR : 0);
|
|
oldattr = fat_make_attrs(inode);
|
|
|
|
/* Equivalent to a chmod() */
|
|
ia.ia_valid = ATTR_MODE | ATTR_CTIME;
|
|
ia.ia_ctime = current_time(inode);
|
|
if (is_dir)
|
|
ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
|
|
else {
|
|
ia.ia_mode = fat_make_mode(sbi, attr,
|
|
S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
|
|
}
|
|
|
|
/* The root directory has no attributes */
|
|
if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
|
|
err = -EINVAL;
|
|
goto out_unlock_inode;
|
|
}
|
|
|
|
if (sbi->options.sys_immutable &&
|
|
((attr | oldattr) & ATTR_SYS) &&
|
|
!capable(CAP_LINUX_IMMUTABLE)) {
|
|
err = -EPERM;
|
|
goto out_unlock_inode;
|
|
}
|
|
|
|
/*
|
|
* The security check is questionable... We single
|
|
* out the RO attribute for checking by the security
|
|
* module, just because it maps to a file mode.
|
|
*/
|
|
err = security_inode_setattr(file->f_path.dentry, &ia);
|
|
if (err)
|
|
goto out_unlock_inode;
|
|
|
|
/* This MUST be done before doing anything irreversible... */
|
|
err = fat_setattr(file_mnt_user_ns(file), file->f_path.dentry, &ia);
|
|
if (err)
|
|
goto out_unlock_inode;
|
|
|
|
fsnotify_change(file->f_path.dentry, ia.ia_valid);
|
|
if (sbi->options.sys_immutable) {
|
|
if (attr & ATTR_SYS)
|
|
inode->i_flags |= S_IMMUTABLE;
|
|
else
|
|
inode->i_flags &= ~S_IMMUTABLE;
|
|
}
|
|
|
|
fat_save_attrs(inode, attr);
|
|
mark_inode_dirty(inode);
|
|
out_unlock_inode:
|
|
inode_unlock(inode);
|
|
mnt_drop_write_file(file);
|
|
out:
|
|
return err;
|
|
}
|
|
|
|
static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr)
|
|
{
|
|
struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
|
|
return put_user(sbi->vol_id, user_attr);
|
|
}
|
|
|
|
static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg)
|
|
{
|
|
struct super_block *sb = inode->i_sb;
|
|
struct fstrim_range __user *user_range;
|
|
struct fstrim_range range;
|
|
struct request_queue *q = bdev_get_queue(sb->s_bdev);
|
|
int err;
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
return -EPERM;
|
|
|
|
if (!blk_queue_discard(q))
|
|
return -EOPNOTSUPP;
|
|
|
|
user_range = (struct fstrim_range __user *)arg;
|
|
if (copy_from_user(&range, user_range, sizeof(range)))
|
|
return -EFAULT;
|
|
|
|
range.minlen = max_t(unsigned int, range.minlen,
|
|
q->limits.discard_granularity);
|
|
|
|
err = fat_trim_fs(inode, &range);
|
|
if (err < 0)
|
|
return err;
|
|
|
|
if (copy_to_user(user_range, &range, sizeof(range)))
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
}
|
|
|
|
long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
|
{
|
|
struct inode *inode = file_inode(filp);
|
|
u32 __user *user_attr = (u32 __user *)arg;
|
|
|
|
switch (cmd) {
|
|
case FAT_IOCTL_GET_ATTRIBUTES:
|
|
return fat_ioctl_get_attributes(inode, user_attr);
|
|
case FAT_IOCTL_SET_ATTRIBUTES:
|
|
return fat_ioctl_set_attributes(filp, user_attr);
|
|
case FAT_IOCTL_GET_VOLUME_ID:
|
|
return fat_ioctl_get_volume_id(inode, user_attr);
|
|
case FITRIM:
|
|
return fat_ioctl_fitrim(inode, arg);
|
|
default:
|
|
return -ENOTTY; /* Inappropriate ioctl for device */
|
|
}
|
|
}
|
|
|
|
static int fat_file_release(struct inode *inode, struct file *filp)
|
|
{
|
|
if ((filp->f_mode & FMODE_WRITE) &&
|
|
MSDOS_SB(inode->i_sb)->options.flush) {
|
|
fat_flush_inodes(inode->i_sb, inode, NULL);
|
|
congestion_wait(BLK_RW_ASYNC, HZ/10);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
|
|
{
|
|
struct inode *inode = filp->f_mapping->host;
|
|
int err;
|
|
|
|
err = __generic_file_fsync(filp, start, end, datasync);
|
|
if (err)
|
|
return err;
|
|
|
|
err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
|
|
if (err)
|
|
return err;
|
|
|
|
return blkdev_issue_flush(inode->i_sb->s_bdev);
|
|
}
|
|
|
|
|
|
const struct file_operations fat_file_operations = {
|
|
.llseek = generic_file_llseek,
|
|
.read_iter = generic_file_read_iter,
|
|
.write_iter = generic_file_write_iter,
|
|
.mmap = generic_file_mmap,
|
|
.release = fat_file_release,
|
|
.unlocked_ioctl = fat_generic_ioctl,
|
|
.compat_ioctl = compat_ptr_ioctl,
|
|
.fsync = fat_file_fsync,
|
|
.splice_read = generic_file_splice_read,
|
|
.splice_write = iter_file_splice_write,
|
|
.fallocate = fat_fallocate,
|
|
};
|
|
|
|
static int fat_cont_expand(struct inode *inode, loff_t size)
|
|
{
|
|
struct address_space *mapping = inode->i_mapping;
|
|
loff_t start = inode->i_size, count = size - inode->i_size;
|
|
int err;
|
|
|
|
err = generic_cont_expand_simple(inode, size);
|
|
if (err)
|
|
goto out;
|
|
|
|
fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);
|
|
mark_inode_dirty(inode);
|
|
if (IS_SYNC(inode)) {
|
|
int err2;
|
|
|
|
/*
|
|
* Opencode syncing since we don't have a file open to use
|
|
* standard fsync path.
|
|
*/
|
|
err = filemap_fdatawrite_range(mapping, start,
|
|
start + count - 1);
|
|
err2 = sync_mapping_buffers(mapping);
|
|
if (!err)
|
|
err = err2;
|
|
err2 = write_inode_now(inode, 1);
|
|
if (!err)
|
|
err = err2;
|
|
if (!err) {
|
|
err = filemap_fdatawait_range(mapping, start,
|
|
start + count - 1);
|
|
}
|
|
}
|
|
out:
|
|
return err;
|
|
}
|
|
|
|
/*
|
|
* Preallocate space for a file. This implements fat's fallocate file
|
|
* operation, which gets called from sys_fallocate system call. User
|
|
* space requests len bytes at offset. If FALLOC_FL_KEEP_SIZE is set
|
|
* we just allocate clusters without zeroing them out. Otherwise we
|
|
* allocate and zero out clusters via an expanding truncate.
|
|
*/
|
|
static long fat_fallocate(struct file *file, int mode,
|
|
loff_t offset, loff_t len)
|
|
{
|
|
int nr_cluster; /* Number of clusters to be allocated */
|
|
loff_t mm_bytes; /* Number of bytes to be allocated for file */
|
|
loff_t ondisksize; /* block aligned on-disk size in bytes*/
|
|
struct inode *inode = file->f_mapping->host;
|
|
struct super_block *sb = inode->i_sb;
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
int err = 0;
|
|
|
|
/* No support for hole punch or other fallocate flags. */
|
|
if (mode & ~FALLOC_FL_KEEP_SIZE)
|
|
return -EOPNOTSUPP;
|
|
|
|
/* No support for dir */
|
|
if (!S_ISREG(inode->i_mode))
|
|
return -EOPNOTSUPP;
|
|
|
|
inode_lock(inode);
|
|
if (mode & FALLOC_FL_KEEP_SIZE) {
|
|
ondisksize = inode->i_blocks << 9;
|
|
if ((offset + len) <= ondisksize)
|
|
goto error;
|
|
|
|
/* First compute the number of clusters to be allocated */
|
|
mm_bytes = offset + len - ondisksize;
|
|
nr_cluster = (mm_bytes + (sbi->cluster_size - 1)) >>
|
|
sbi->cluster_bits;
|
|
|
|
/* Start the allocation.We are not zeroing out the clusters */
|
|
while (nr_cluster-- > 0) {
|
|
err = fat_add_cluster(inode);
|
|
if (err)
|
|
goto error;
|
|
}
|
|
} else {
|
|
if ((offset + len) <= i_size_read(inode))
|
|
goto error;
|
|
|
|
/* This is just an expanding truncate */
|
|
err = fat_cont_expand(inode, (offset + len));
|
|
}
|
|
|
|
error:
|
|
inode_unlock(inode);
|
|
return err;
|
|
}
|
|
|
|
/* Free all clusters after the skip'th cluster. */
|
|
static int fat_free(struct inode *inode, int skip)
|
|
{
|
|
struct super_block *sb = inode->i_sb;
|
|
int err, wait, free_start, i_start, i_logstart;
|
|
|
|
if (MSDOS_I(inode)->i_start == 0)
|
|
return 0;
|
|
|
|
fat_cache_inval_inode(inode);
|
|
|
|
wait = IS_DIRSYNC(inode);
|
|
i_start = free_start = MSDOS_I(inode)->i_start;
|
|
i_logstart = MSDOS_I(inode)->i_logstart;
|
|
|
|
/* First, we write the new file size. */
|
|
if (!skip) {
|
|
MSDOS_I(inode)->i_start = 0;
|
|
MSDOS_I(inode)->i_logstart = 0;
|
|
}
|
|
MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
|
|
fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);
|
|
if (wait) {
|
|
err = fat_sync_inode(inode);
|
|
if (err) {
|
|
MSDOS_I(inode)->i_start = i_start;
|
|
MSDOS_I(inode)->i_logstart = i_logstart;
|
|
return err;
|
|
}
|
|
} else
|
|
mark_inode_dirty(inode);
|
|
|
|
/* Write a new EOF, and get the remaining cluster chain for freeing. */
|
|
if (skip) {
|
|
struct fat_entry fatent;
|
|
int ret, fclus, dclus;
|
|
|
|
ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
|
|
if (ret < 0)
|
|
return ret;
|
|
else if (ret == FAT_ENT_EOF)
|
|
return 0;
|
|
|
|
fatent_init(&fatent);
|
|
ret = fat_ent_read(inode, &fatent, dclus);
|
|
if (ret == FAT_ENT_EOF) {
|
|
fatent_brelse(&fatent);
|
|
return 0;
|
|
} else if (ret == FAT_ENT_FREE) {
|
|
fat_fs_error(sb,
|
|
"%s: invalid cluster chain (i_pos %lld)",
|
|
__func__, MSDOS_I(inode)->i_pos);
|
|
ret = -EIO;
|
|
} else if (ret > 0) {
|
|
err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
|
|
if (err)
|
|
ret = err;
|
|
}
|
|
fatent_brelse(&fatent);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
free_start = ret;
|
|
}
|
|
inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
|
|
|
|
/* Freeing the remained cluster chain */
|
|
return fat_free_clusters(inode, free_start);
|
|
}
|
|
|
|
void fat_truncate_blocks(struct inode *inode, loff_t offset)
|
|
{
|
|
struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
|
|
const unsigned int cluster_size = sbi->cluster_size;
|
|
int nr_clusters;
|
|
|
|
/*
|
|
* This protects against truncating a file bigger than it was then
|
|
* trying to write into the hole.
|
|
*/
|
|
if (MSDOS_I(inode)->mmu_private > offset)
|
|
MSDOS_I(inode)->mmu_private = offset;
|
|
|
|
nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits;
|
|
|
|
fat_free(inode, nr_clusters);
|
|
fat_flush_inodes(inode->i_sb, inode, NULL);
|
|
}
|
|
|
|
int fat_getattr(struct user_namespace *mnt_userns, const struct path *path,
|
|
struct kstat *stat, u32 request_mask, unsigned int flags)
|
|
{
|
|
struct inode *inode = d_inode(path->dentry);
|
|
generic_fillattr(mnt_userns, inode, stat);
|
|
stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
|
|
|
|
if (MSDOS_SB(inode->i_sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
|
|
/* Use i_pos for ino. This is used as fileid of nfs. */
|
|
stat->ino = fat_i_pos_read(MSDOS_SB(inode->i_sb), inode);
|
|
}
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(fat_getattr);
|
|
|
|
static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
|
|
struct inode *inode, umode_t *mode_ptr)
|
|
{
|
|
umode_t mask, perm;
|
|
|
|
/*
|
|
* Note, the basic check is already done by a caller of
|
|
* (attr->ia_mode & ~FAT_VALID_MODE)
|
|
*/
|
|
|
|
if (S_ISREG(inode->i_mode))
|
|
mask = sbi->options.fs_fmask;
|
|
else
|
|
mask = sbi->options.fs_dmask;
|
|
|
|
perm = *mode_ptr & ~(S_IFMT | mask);
|
|
|
|
/*
|
|
* Of the r and x bits, all (subject to umask) must be present. Of the
|
|
* w bits, either all (subject to umask) or none must be present.
|
|
*
|
|
* If fat_mode_can_hold_ro(inode) is false, can't change w bits.
|
|
*/
|
|
if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
|
|
return -EPERM;
|
|
if (fat_mode_can_hold_ro(inode)) {
|
|
if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
|
|
return -EPERM;
|
|
} else {
|
|
if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
|
|
return -EPERM;
|
|
}
|
|
|
|
*mode_ptr &= S_IFMT | perm;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int fat_allow_set_time(struct user_namespace *mnt_userns,
|
|
struct msdos_sb_info *sbi, struct inode *inode)
|
|
{
|
|
umode_t allow_utime = sbi->options.allow_utime;
|
|
|
|
if (!uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode))) {
|
|
if (in_group_p(i_gid_into_mnt(mnt_userns, inode)))
|
|
allow_utime >>= 3;
|
|
if (allow_utime & MAY_WRITE)
|
|
return 1;
|
|
}
|
|
|
|
/* use a default check */
|
|
return 0;
|
|
}
|
|
|
|
#define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
|
|
/* valid file mode bits */
|
|
#define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
|
|
|
|
int fat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
|
|
struct iattr *attr)
|
|
{
|
|
struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
|
|
struct inode *inode = d_inode(dentry);
|
|
unsigned int ia_valid;
|
|
int error;
|
|
|
|
/* Check for setting the inode time. */
|
|
ia_valid = attr->ia_valid;
|
|
if (ia_valid & TIMES_SET_FLAGS) {
|
|
if (fat_allow_set_time(mnt_userns, sbi, inode))
|
|
attr->ia_valid &= ~TIMES_SET_FLAGS;
|
|
}
|
|
|
|
error = setattr_prepare(mnt_userns, dentry, attr);
|
|
attr->ia_valid = ia_valid;
|
|
if (error) {
|
|
if (sbi->options.quiet)
|
|
error = 0;
|
|
goto out;
|
|
}
|
|
|
|
/*
|
|
* Expand the file. Since inode_setattr() updates ->i_size
|
|
* before calling the ->truncate(), but FAT needs to fill the
|
|
* hole before it. XXX: this is no longer true with new truncate
|
|
* sequence.
|
|
*/
|
|
if (attr->ia_valid & ATTR_SIZE) {
|
|
inode_dio_wait(inode);
|
|
|
|
if (attr->ia_size > inode->i_size) {
|
|
error = fat_cont_expand(inode, attr->ia_size);
|
|
if (error || attr->ia_valid == ATTR_SIZE)
|
|
goto out;
|
|
attr->ia_valid &= ~ATTR_SIZE;
|
|
}
|
|
}
|
|
|
|
if (((attr->ia_valid & ATTR_UID) &&
|
|
(!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
|
|
((attr->ia_valid & ATTR_GID) &&
|
|
(!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
|
|
((attr->ia_valid & ATTR_MODE) &&
|
|
(attr->ia_mode & ~FAT_VALID_MODE)))
|
|
error = -EPERM;
|
|
|
|
if (error) {
|
|
if (sbi->options.quiet)
|
|
error = 0;
|
|
goto out;
|
|
}
|
|
|
|
/*
|
|
* We don't return -EPERM here. Yes, strange, but this is too
|
|
* old behavior.
|
|
*/
|
|
if (attr->ia_valid & ATTR_MODE) {
|
|
if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
|
|
attr->ia_valid &= ~ATTR_MODE;
|
|
}
|
|
|
|
if (attr->ia_valid & ATTR_SIZE) {
|
|
error = fat_block_truncate_page(inode, attr->ia_size);
|
|
if (error)
|
|
goto out;
|
|
down_write(&MSDOS_I(inode)->truncate_lock);
|
|
truncate_setsize(inode, attr->ia_size);
|
|
fat_truncate_blocks(inode, attr->ia_size);
|
|
up_write(&MSDOS_I(inode)->truncate_lock);
|
|
}
|
|
|
|
/*
|
|
* setattr_copy can't truncate these appropriately, so we'll
|
|
* copy them ourselves
|
|
*/
|
|
if (attr->ia_valid & ATTR_ATIME)
|
|
fat_truncate_time(inode, &attr->ia_atime, S_ATIME);
|
|
if (attr->ia_valid & ATTR_CTIME)
|
|
fat_truncate_time(inode, &attr->ia_ctime, S_CTIME);
|
|
if (attr->ia_valid & ATTR_MTIME)
|
|
fat_truncate_time(inode, &attr->ia_mtime, S_MTIME);
|
|
attr->ia_valid &= ~(ATTR_ATIME|ATTR_CTIME|ATTR_MTIME);
|
|
|
|
setattr_copy(mnt_userns, inode, attr);
|
|
mark_inode_dirty(inode);
|
|
out:
|
|
return error;
|
|
}
|
|
EXPORT_SYMBOL_GPL(fat_setattr);
|
|
|
|
const struct inode_operations fat_file_inode_operations = {
|
|
.setattr = fat_setattr,
|
|
.getattr = fat_getattr,
|
|
.update_time = fat_update_time,
|
|
};
|