2019-06-04 10:11:33 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2018-07-18 15:44:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Red Hat, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/cred.h>
|
|
|
|
#include <linux/file.h>
|
2018-07-18 15:44:42 +02:00
|
|
|
#include <linux/mount.h>
|
2018-07-18 15:44:41 +02:00
|
|
|
#include <linux/xattr.h>
|
2018-07-18 15:44:41 +02:00
|
|
|
#include <linux/uio.h>
|
2019-05-06 15:41:02 +08:00
|
|
|
#include <linux/uaccess.h>
|
2020-06-02 22:20:26 +02:00
|
|
|
#include <linux/security.h>
|
2020-01-17 20:49:29 +08:00
|
|
|
#include <linux/fs.h>
|
2023-10-02 17:19:46 +03:00
|
|
|
#include <linux/backing-file.h>
|
2018-07-18 15:44:41 +02:00
|
|
|
#include "overlayfs.h"
|
|
|
|
|
2018-05-11 11:49:31 -04:00
|
|
|
static char ovl_whatisit(struct inode *inode, struct inode *realinode)
|
|
|
|
{
|
|
|
|
if (realinode != ovl_inode_upper(inode))
|
|
|
|
return 'l';
|
|
|
|
if (ovl_has_upperdata(inode))
|
|
|
|
return 'u';
|
|
|
|
else
|
|
|
|
return 'm';
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct file *ovl_open_realfile(const struct file *file,
|
2022-08-04 13:11:15 -04:00
|
|
|
const struct path *realpath)
|
2018-07-18 15:44:41 +02:00
|
|
|
{
|
2022-04-04 12:51:47 +02:00
|
|
|
struct inode *realinode = d_inode(realpath->dentry);
|
2018-07-18 15:44:41 +02:00
|
|
|
struct inode *inode = file_inode(file);
|
2023-01-13 12:49:22 +01:00
|
|
|
struct mnt_idmap *real_idmap;
|
2018-07-18 15:44:41 +02:00
|
|
|
struct file *realfile;
|
|
|
|
const struct cred *old_cred;
|
2020-06-18 18:43:53 +03:00
|
|
|
int flags = file->f_flags | OVL_OPEN_FLAGS;
|
2020-06-02 22:20:26 +02:00
|
|
|
int acc_mode = ACC_MODE(flags);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (flags & O_APPEND)
|
|
|
|
acc_mode |= MAY_APPEND;
|
2018-07-18 15:44:41 +02:00
|
|
|
|
|
|
|
old_cred = ovl_override_creds(inode->i_sb);
|
2023-01-13 12:49:22 +01:00
|
|
|
real_idmap = mnt_idmap(realpath->mnt);
|
|
|
|
err = inode_permission(real_idmap, realinode, MAY_OPEN | acc_mode);
|
2020-06-02 22:20:26 +02:00
|
|
|
if (err) {
|
|
|
|
realfile = ERR_PTR(err);
|
|
|
|
} else {
|
2023-01-13 12:49:26 +01:00
|
|
|
if (!inode_owner_or_capable(real_idmap, realinode))
|
2020-12-14 15:26:14 +01:00
|
|
|
flags &= ~O_NOATIME;
|
|
|
|
|
2023-06-15 14:22:28 +03:00
|
|
|
realfile = backing_file_open(&file->f_path, flags, realpath,
|
|
|
|
current_cred());
|
2020-06-02 22:20:26 +02:00
|
|
|
}
|
2024-11-05 11:35:13 -08:00
|
|
|
ovl_revert_creds(old_cred);
|
2018-07-18 15:44:41 +02:00
|
|
|
|
|
|
|
pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
|
2018-05-11 11:49:31 -04:00
|
|
|
file, file, ovl_whatisit(inode, realinode), file->f_flags,
|
2018-07-18 15:44:41 +02:00
|
|
|
realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
|
|
|
|
|
|
|
|
return realfile;
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:44:41 +02:00
|
|
|
#define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
|
|
|
|
|
|
|
|
static int ovl_change_flags(struct file *file, unsigned int flags)
|
|
|
|
{
|
|
|
|
struct inode *inode = file_inode(file);
|
|
|
|
int err;
|
|
|
|
|
|
|
|
flags &= OVL_SETFL_MASK;
|
|
|
|
|
|
|
|
if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
|
|
|
|
return -EPERM;
|
|
|
|
|
2022-05-09 18:20:49 -07:00
|
|
|
if ((flags & O_DIRECT) && !(file->f_mode & FMODE_CAN_ODIRECT))
|
|
|
|
return -EINVAL;
|
2018-07-18 15:44:41 +02:00
|
|
|
|
|
|
|
if (file->f_op->check_flags) {
|
|
|
|
err = file->f_op->check_flags(flags);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock(&file->f_lock);
|
|
|
|
file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
|
2022-11-24 17:03:11 +00:00
|
|
|
file->f_iocb_flags = iocb_flags(file);
|
2018-07-18 15:44:41 +02:00
|
|
|
spin_unlock(&file->f_lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-10-07 15:22:29 +02:00
|
|
|
struct ovl_file {
|
|
|
|
struct file *realfile;
|
2024-10-14 17:25:26 +02:00
|
|
|
struct file *upperfile;
|
2024-10-07 15:22:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ovl_file *ovl_file_alloc(struct file *realfile)
|
|
|
|
{
|
|
|
|
struct ovl_file *of = kzalloc(sizeof(struct ovl_file), GFP_KERNEL);
|
|
|
|
|
|
|
|
if (unlikely(!of))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
of->realfile = realfile;
|
|
|
|
return of;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ovl_file_free(struct ovl_file *of)
|
|
|
|
{
|
|
|
|
fput(of->realfile);
|
2024-10-14 17:25:26 +02:00
|
|
|
if (of->upperfile)
|
|
|
|
fput(of->upperfile);
|
2024-10-07 15:22:29 +02:00
|
|
|
kfree(of);
|
|
|
|
}
|
|
|
|
|
2024-10-14 17:25:26 +02:00
|
|
|
static bool ovl_is_real_file(const struct file *realfile,
|
|
|
|
const struct path *realpath)
|
|
|
|
{
|
|
|
|
return file_inode(realfile) == d_inode(realpath->dentry);
|
|
|
|
}
|
|
|
|
|
2024-11-05 21:28:49 +01:00
|
|
|
static struct file *ovl_real_file_path(const struct file *file,
|
|
|
|
struct path *realpath)
|
2018-07-18 15:44:41 +02:00
|
|
|
{
|
2024-10-07 15:22:29 +02:00
|
|
|
struct ovl_file *of = file->private_data;
|
|
|
|
struct file *realfile = of->realfile;
|
2018-07-18 15:44:41 +02:00
|
|
|
|
2024-11-05 21:28:06 +01:00
|
|
|
if (WARN_ON_ONCE(!realpath->dentry))
|
2024-11-05 21:28:49 +01:00
|
|
|
return ERR_PTR(-EIO);
|
2018-05-11 11:49:31 -04:00
|
|
|
|
2024-10-14 17:25:26 +02:00
|
|
|
/*
|
|
|
|
* If the realfile that we want is not where the data used to be at
|
|
|
|
* open time, either we'd been copied up, or it's an fsync of a
|
|
|
|
* metacopied file. We need the upperfile either way, so see if it
|
|
|
|
* is already opened and if it is not then open and store it.
|
|
|
|
*/
|
|
|
|
if (unlikely(!ovl_is_real_file(realfile, realpath))) {
|
|
|
|
struct file *upperfile = READ_ONCE(of->upperfile);
|
|
|
|
struct file *old;
|
|
|
|
|
|
|
|
if (!upperfile) { /* Nobody opened upperfile yet */
|
|
|
|
upperfile = ovl_open_realfile(file, realpath);
|
|
|
|
if (IS_ERR(upperfile))
|
2024-11-05 21:28:49 +01:00
|
|
|
return upperfile;
|
2024-10-14 17:25:26 +02:00
|
|
|
|
|
|
|
/* Store the upperfile for later */
|
|
|
|
old = cmpxchg_release(&of->upperfile, NULL, upperfile);
|
|
|
|
if (old) { /* Someone opened upperfile before us */
|
|
|
|
fput(upperfile);
|
|
|
|
upperfile = old;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Stored file must be from the right inode, unless someone's
|
|
|
|
* been corrupting the upper layer.
|
|
|
|
*/
|
|
|
|
if (WARN_ON_ONCE(!ovl_is_real_file(upperfile, realpath)))
|
2024-11-05 21:28:49 +01:00
|
|
|
return ERR_PTR(-EIO);
|
2024-10-14 17:25:26 +02:00
|
|
|
|
|
|
|
realfile = upperfile;
|
2018-07-18 15:44:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Did the flags change since open? */
|
2024-11-05 21:28:49 +01:00
|
|
|
if (unlikely((file->f_flags ^ realfile->f_flags) & ~OVL_OPEN_FLAGS)) {
|
|
|
|
int err = ovl_change_flags(realfile, file->f_flags);
|
2018-07-18 15:44:41 +02:00
|
|
|
|
2024-11-05 21:28:49 +01:00
|
|
|
if (err)
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return realfile;
|
2018-07-18 15:44:41 +02:00
|
|
|
}
|
|
|
|
|
2024-11-05 21:28:49 +01:00
|
|
|
static struct file *ovl_real_file(const struct file *file)
|
2018-05-11 11:49:31 -04:00
|
|
|
{
|
2024-11-05 21:28:06 +01:00
|
|
|
struct dentry *dentry = file_dentry(file);
|
|
|
|
struct path realpath;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (d_is_dir(dentry)) {
|
struct fd: representation change
We want the compiler to see that fdput() on empty instance
is a no-op. The emptiness check is that file reference is NULL,
while fdput() is "fput() if FDPUT_FPUT is present in flags".
The reason why fdput() on empty instance is a no-op is something
compiler can't see - it's that we never generate instances with
NULL file reference combined with non-zero flags.
It's not that hard to deal with - the real primitives behind
fdget() et.al. are returning an unsigned long value, unpacked by (inlined)
__to_fd() into the current struct file * + int. The lower bits are
used to store flags, while the rest encodes the pointer. Linus suggested
that keeping this unsigned long around with the extractions done by inlined
accessors should generate a sane code and that turns out to be the case.
Namely, turning struct fd into a struct-wrapped unsinged long, with
fd_empty(f) => unlikely(f.word == 0)
fd_file(f) => (struct file *)(f.word & ~3)
fdput(f) => if (f.word & 1) fput(fd_file(f))
ends up with compiler doing the right thing. The cost is the patch
footprint, of course - we need to switch f.file to fd_file(f) all over
the tree, and it's not doable with simple search and replace; there are
false positives, etc.
Note that the sole member of that structure is an opaque
unsigned long - all accesses should be done via wrappers and I don't
want to use a name that would invite manual casts to file pointers,
etc. The value of that member is equal either to (unsigned long)p | flags,
p being an address of some struct file instance, or to 0 for an empty fd.
For now the new predicate (fd_empty(f)) has no users; all the
existing checks have form (!fd_file(f)). We will convert to fd_empty()
use later; here we only define it (and tell the compiler that it's
unlikely to return true).
This commit only deals with representation change; there will
be followups.
Reviewed-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2024-05-31 15:45:12 -04:00
|
|
|
struct file *f = ovl_dir_real_file(file, false);
|
2024-11-05 21:28:49 +01:00
|
|
|
|
|
|
|
if (WARN_ON_ONCE(!f))
|
|
|
|
return ERR_PTR(-EIO);
|
|
|
|
return f;
|
2020-09-29 15:28:47 +08:00
|
|
|
}
|
|
|
|
|
2024-11-05 21:28:06 +01:00
|
|
|
/* lazy lookup and verify of lowerdata */
|
|
|
|
err = ovl_verify_lowerdata(dentry);
|
|
|
|
if (err)
|
2024-11-05 21:28:49 +01:00
|
|
|
return ERR_PTR(err);
|
2024-11-05 21:28:06 +01:00
|
|
|
|
|
|
|
ovl_path_realdata(dentry, &realpath);
|
|
|
|
|
2024-11-05 21:28:49 +01:00
|
|
|
return ovl_real_file_path(file, &realpath);
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:44:41 +02:00
|
|
|
static int ovl_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2022-04-04 12:51:47 +02:00
|
|
|
struct dentry *dentry = file_dentry(file);
|
2018-07-18 15:44:41 +02:00
|
|
|
struct file *realfile;
|
2022-04-04 12:51:47 +02:00
|
|
|
struct path realpath;
|
2024-10-07 15:22:29 +02:00
|
|
|
struct ovl_file *of;
|
2018-07-18 15:44:41 +02:00
|
|
|
int err;
|
|
|
|
|
2023-06-21 10:44:27 +02:00
|
|
|
/* lazy lookup and verify lowerdata */
|
|
|
|
err = ovl_verify_lowerdata(dentry);
|
2023-04-27 13:39:09 +03:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2022-04-04 12:51:47 +02:00
|
|
|
err = ovl_maybe_copy_up(dentry, file->f_flags);
|
2018-07-18 15:44:41 +02:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* No longer need these flags, so don't pass them on to underlying fs */
|
|
|
|
file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
|
|
|
|
|
2022-04-04 12:51:47 +02:00
|
|
|
ovl_path_realdata(dentry, &realpath);
|
2023-04-02 21:56:49 +03:00
|
|
|
if (!realpath.dentry)
|
|
|
|
return -EIO;
|
|
|
|
|
2022-04-04 12:51:47 +02:00
|
|
|
realfile = ovl_open_realfile(file, &realpath);
|
2018-07-18 15:44:41 +02:00
|
|
|
if (IS_ERR(realfile))
|
|
|
|
return PTR_ERR(realfile);
|
|
|
|
|
2024-10-07 15:22:29 +02:00
|
|
|
of = ovl_file_alloc(realfile);
|
|
|
|
if (!of) {
|
|
|
|
fput(realfile);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
file->private_data = of;
|
2018-07-18 15:44:41 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovl_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
2024-10-07 15:22:29 +02:00
|
|
|
ovl_file_free(file->private_data);
|
2018-07-18 15:44:41 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
|
|
|
|
{
|
2019-02-27 13:32:11 +02:00
|
|
|
struct inode *inode = file_inode(file);
|
2024-11-05 21:29:36 +01:00
|
|
|
struct file *realfile;
|
2019-02-27 13:32:11 +02:00
|
|
|
const struct cred *old_cred;
|
2020-02-03 11:41:53 +01:00
|
|
|
loff_t ret;
|
2019-02-27 13:32:11 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The two special cases below do not need to involve real fs,
|
|
|
|
* so we can optimizing concurrent callers.
|
|
|
|
*/
|
|
|
|
if (offset == 0) {
|
|
|
|
if (whence == SEEK_CUR)
|
|
|
|
return file->f_pos;
|
|
|
|
|
|
|
|
if (whence == SEEK_SET)
|
|
|
|
return vfs_setpos(file, 0, 0);
|
|
|
|
}
|
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
realfile = ovl_real_file(file);
|
|
|
|
if (IS_ERR(realfile))
|
|
|
|
return PTR_ERR(realfile);
|
2019-02-27 13:32:11 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Overlay file f_pos is the master copy that is preserved
|
|
|
|
* through copy up and modified on read/write, but only real
|
|
|
|
* fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
|
|
|
|
* limitations that are more strict than ->s_maxbytes for specific
|
|
|
|
* files, so we use the real file to perform seeks.
|
|
|
|
*/
|
2019-12-21 11:42:29 +02:00
|
|
|
ovl_inode_lock(inode);
|
2024-11-05 21:29:36 +01:00
|
|
|
realfile->f_pos = file->f_pos;
|
2019-02-27 13:32:11 +02:00
|
|
|
|
|
|
|
old_cred = ovl_override_creds(inode->i_sb);
|
2024-11-05 21:29:36 +01:00
|
|
|
ret = vfs_llseek(realfile, offset, whence);
|
2024-11-05 11:35:13 -08:00
|
|
|
ovl_revert_creds(old_cred);
|
2019-02-27 13:32:11 +02:00
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
file->f_pos = realfile->f_pos;
|
2019-12-21 11:42:29 +02:00
|
|
|
ovl_inode_unlock(inode);
|
2019-02-27 13:32:11 +02:00
|
|
|
|
|
|
|
return ret;
|
2018-07-18 15:44:41 +02:00
|
|
|
}
|
|
|
|
|
2023-09-27 13:43:44 +03:00
|
|
|
static void ovl_file_modified(struct file *file)
|
|
|
|
{
|
|
|
|
/* Update size/mtime */
|
|
|
|
ovl_copyattr(file_inode(file));
|
|
|
|
}
|
|
|
|
|
2024-10-21 12:33:38 +02:00
|
|
|
static void ovl_file_end_write(struct kiocb *iocb, ssize_t ret)
|
2024-10-14 21:27:58 +02:00
|
|
|
{
|
2024-10-21 12:33:38 +02:00
|
|
|
ovl_file_modified(iocb->ki_filp);
|
2024-10-14 21:27:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 15:44:41 +02:00
|
|
|
static void ovl_file_accessed(struct file *file)
|
|
|
|
{
|
|
|
|
struct inode *inode, *upperinode;
|
2023-07-05 15:01:31 -04:00
|
|
|
struct timespec64 ctime, uctime;
|
2023-10-04 14:52:45 -04:00
|
|
|
struct timespec64 mtime, umtime;
|
2018-07-18 15:44:41 +02:00
|
|
|
|
|
|
|
if (file->f_flags & O_NOATIME)
|
|
|
|
return;
|
|
|
|
|
|
|
|
inode = file_inode(file);
|
|
|
|
upperinode = ovl_inode_upper(inode);
|
|
|
|
|
|
|
|
if (!upperinode)
|
|
|
|
return;
|
|
|
|
|
2023-07-05 15:01:31 -04:00
|
|
|
ctime = inode_get_ctime(inode);
|
|
|
|
uctime = inode_get_ctime(upperinode);
|
2023-10-04 14:52:45 -04:00
|
|
|
mtime = inode_get_mtime(inode);
|
|
|
|
umtime = inode_get_mtime(upperinode);
|
|
|
|
if ((!timespec64_equal(&mtime, &umtime)) ||
|
|
|
|
!timespec64_equal(&ctime, &uctime)) {
|
|
|
|
inode_set_mtime_to_ts(inode, inode_get_mtime(upperinode));
|
2023-07-05 15:01:31 -04:00
|
|
|
inode_set_ctime_to_ts(inode, uctime);
|
2018-07-18 15:44:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
touch_atime(&file->f_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
|
|
|
|
{
|
|
|
|
struct file *file = iocb->ki_filp;
|
2024-11-05 21:29:36 +01:00
|
|
|
struct file *realfile;
|
2023-11-22 17:48:52 +02:00
|
|
|
struct backing_file_ctx ctx = {
|
|
|
|
.cred = ovl_creds(file_inode(file)->i_sb),
|
|
|
|
.accessed = ovl_file_accessed,
|
|
|
|
};
|
2018-07-18 15:44:41 +02:00
|
|
|
|
|
|
|
if (!iov_iter_count(iter))
|
|
|
|
return 0;
|
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
realfile = ovl_real_file(file);
|
|
|
|
if (IS_ERR(realfile))
|
|
|
|
return PTR_ERR(realfile);
|
2018-07-18 15:44:41 +02:00
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
return backing_file_read_iter(realfile, iter, iocb, iocb->ki_flags,
|
|
|
|
&ctx);
|
2018-07-18 15:44:41 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 15:44:41 +02:00
|
|
|
static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
|
|
|
|
{
|
|
|
|
struct file *file = iocb->ki_filp;
|
|
|
|
struct inode *inode = file_inode(file);
|
2024-11-05 21:29:36 +01:00
|
|
|
struct file *realfile;
|
2018-07-18 15:44:41 +02:00
|
|
|
ssize_t ret;
|
2020-08-31 14:15:29 -04:00
|
|
|
int ifl = iocb->ki_flags;
|
2023-11-22 17:48:52 +02:00
|
|
|
struct backing_file_ctx ctx = {
|
|
|
|
.cred = ovl_creds(inode->i_sb),
|
2024-10-14 21:27:58 +02:00
|
|
|
.end_write = ovl_file_end_write,
|
2023-11-22 17:48:52 +02:00
|
|
|
};
|
2018-07-18 15:44:41 +02:00
|
|
|
|
|
|
|
if (!iov_iter_count(iter))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
inode_lock(inode);
|
|
|
|
/* Update mode */
|
2022-04-04 12:51:54 +02:00
|
|
|
ovl_copyattr(inode);
|
2018-07-18 15:44:41 +02:00
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
realfile = ovl_real_file(file);
|
|
|
|
ret = PTR_ERR(realfile);
|
|
|
|
if (IS_ERR(realfile))
|
2018-07-18 15:44:41 +02:00
|
|
|
goto out_unlock;
|
|
|
|
|
2020-08-31 14:15:29 -04:00
|
|
|
if (!ovl_should_sync(OVL_FS(inode->i_sb)))
|
|
|
|
ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
|
|
|
|
|
2023-09-25 00:21:35 -06:00
|
|
|
/*
|
|
|
|
* Overlayfs doesn't support deferred completions, don't copy
|
|
|
|
* this property in case it is set by the issuer.
|
|
|
|
*/
|
|
|
|
ifl &= ~IOCB_DIO_CALLER_COMP;
|
2024-11-05 21:29:36 +01:00
|
|
|
ret = backing_file_write_iter(realfile, iter, iocb, ifl, &ctx);
|
2018-07-18 15:44:41 +02:00
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
inode_unlock(inode);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-05-22 14:49:57 +01:00
|
|
|
static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
|
|
|
|
struct pipe_inode_info *pipe, size_t len,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
2024-11-05 21:29:36 +01:00
|
|
|
struct file *realfile;
|
2023-05-22 14:49:57 +01:00
|
|
|
ssize_t ret;
|
2023-10-13 12:13:12 +03:00
|
|
|
struct backing_file_ctx ctx = {
|
|
|
|
.cred = ovl_creds(file_inode(in)->i_sb),
|
|
|
|
.accessed = ovl_file_accessed,
|
|
|
|
};
|
2024-10-21 12:33:38 +02:00
|
|
|
struct kiocb iocb;
|
2023-05-22 14:49:57 +01:00
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
realfile = ovl_real_file(in);
|
|
|
|
if (IS_ERR(realfile))
|
|
|
|
return PTR_ERR(realfile);
|
2023-05-22 14:49:57 +01:00
|
|
|
|
2024-10-21 12:33:38 +02:00
|
|
|
init_sync_kiocb(&iocb, in);
|
|
|
|
iocb.ki_pos = *ppos;
|
2024-11-05 21:29:36 +01:00
|
|
|
ret = backing_file_splice_read(realfile, &iocb, pipe, len, flags, &ctx);
|
2024-10-21 12:33:38 +02:00
|
|
|
*ppos = iocb.ki_pos;
|
2023-10-13 12:13:12 +03:00
|
|
|
|
2023-05-22 14:49:57 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-07-28 10:38:43 +02:00
|
|
|
/*
|
|
|
|
* Calling iter_file_splice_write() directly from overlay's f_op may deadlock
|
|
|
|
* due to lock order inversion between pipe->mutex in iter_file_splice_write()
|
2024-11-05 21:29:36 +01:00
|
|
|
* and file_start_write(realfile) in ovl_write_iter().
|
2021-07-28 10:38:43 +02:00
|
|
|
*
|
|
|
|
* So do everything ovl_write_iter() does and call iter_file_splice_write() on
|
|
|
|
* the real file.
|
|
|
|
*/
|
|
|
|
static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
|
|
|
|
loff_t *ppos, size_t len, unsigned int flags)
|
|
|
|
{
|
2024-11-05 21:29:36 +01:00
|
|
|
struct file *realfile;
|
2021-07-28 10:38:43 +02:00
|
|
|
struct inode *inode = file_inode(out);
|
|
|
|
ssize_t ret;
|
2023-10-13 12:13:12 +03:00
|
|
|
struct backing_file_ctx ctx = {
|
|
|
|
.cred = ovl_creds(inode->i_sb),
|
2024-10-14 21:27:58 +02:00
|
|
|
.end_write = ovl_file_end_write,
|
2023-10-13 12:13:12 +03:00
|
|
|
};
|
2024-10-21 12:33:38 +02:00
|
|
|
struct kiocb iocb;
|
2021-07-28 10:38:43 +02:00
|
|
|
|
|
|
|
inode_lock(inode);
|
|
|
|
/* Update mode */
|
2022-04-04 12:51:54 +02:00
|
|
|
ovl_copyattr(inode);
|
2021-07-28 10:38:43 +02:00
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
realfile = ovl_real_file(out);
|
|
|
|
ret = PTR_ERR(realfile);
|
|
|
|
if (IS_ERR(realfile))
|
2021-07-28 10:38:43 +02:00
|
|
|
goto out_unlock;
|
|
|
|
|
2024-10-21 12:33:38 +02:00
|
|
|
init_sync_kiocb(&iocb, out);
|
|
|
|
iocb.ki_pos = *ppos;
|
2024-11-05 21:29:36 +01:00
|
|
|
ret = backing_file_splice_write(pipe, realfile, &iocb, len, flags, &ctx);
|
2024-10-21 12:33:38 +02:00
|
|
|
*ppos = iocb.ki_pos;
|
|
|
|
|
2021-07-28 10:38:43 +02:00
|
|
|
out_unlock:
|
|
|
|
inode_unlock(inode);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:44:42 +02:00
|
|
|
static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
|
|
|
|
{
|
2024-11-05 21:28:06 +01:00
|
|
|
struct dentry *dentry = file_dentry(file);
|
|
|
|
enum ovl_path_type type;
|
|
|
|
struct path upperpath;
|
2024-11-05 21:28:49 +01:00
|
|
|
struct file *upperfile;
|
2018-07-18 15:44:42 +02:00
|
|
|
const struct cred *old_cred;
|
|
|
|
int ret;
|
|
|
|
|
ovl: implement volatile-specific fsync error behaviour
Overlayfs's volatile option allows the user to bypass all forced sync calls
to the upperdir filesystem. This comes at the cost of safety. We can never
ensure that the user's data is intact, but we can make a best effort to
expose whether or not the data is likely to be in a bad state.
The best way to handle this in the time being is that if an overlayfs's
upperdir experiences an error after a volatile mount occurs, that error
will be returned on fsync, fdatasync, sync, and syncfs. This is
contradictory to the traditional behaviour of VFS which fails the call
once, and only raises an error if a subsequent fsync error has occurred,
and been raised by the filesystem.
One awkward aspect of the patch is that we have to manually set the
superblock's errseq_t after the sync_fs callback as opposed to just
returning an error from syncfs. This is because the call chain looks
something like this:
sys_syncfs ->
sync_filesystem ->
__sync_filesystem ->
/* The return value is ignored here
sb->s_op->sync_fs(sb)
_sync_blockdev
/* Where the VFS fetches the error to raise to userspace */
errseq_check_and_advance
Because of this we call errseq_set every time the sync_fs callback occurs.
Due to the nature of this seen / unseen dichotomy, if the upperdir is an
inconsistent state at the initial mount time, overlayfs will refuse to
mount, as overlayfs cannot get a snapshot of the upperdir's errseq that
will increment on error until the user calls syncfs.
Signed-off-by: Sargun Dhillon <sargun@sargun.me>
Suggested-by: Amir Goldstein <amir73il@gmail.com>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Fixes: c86243b090bc ("ovl: provide a mount option "volatile"")
Cc: stable@vger.kernel.org
Reviewed-by: Vivek Goyal <vgoyal@redhat.com>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2021-01-07 16:10:43 -08:00
|
|
|
ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
|
|
|
|
if (ret <= 0)
|
|
|
|
return ret;
|
2020-08-31 14:15:29 -04:00
|
|
|
|
2024-11-05 21:28:06 +01:00
|
|
|
/* Don't sync lower file for fear of receiving EROFS error */
|
|
|
|
type = ovl_path_type(dentry);
|
|
|
|
if (!OVL_TYPE_UPPER(type) || (datasync && OVL_TYPE_MERGE(type)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ovl_path_upper(dentry, &upperpath);
|
2024-11-05 21:28:49 +01:00
|
|
|
upperfile = ovl_real_file_path(file, &upperpath);
|
|
|
|
if (IS_ERR(upperfile))
|
|
|
|
return PTR_ERR(upperfile);
|
2018-07-18 15:44:42 +02:00
|
|
|
|
2024-11-05 21:28:06 +01:00
|
|
|
old_cred = ovl_override_creds(file_inode(file)->i_sb);
|
2024-11-05 21:28:49 +01:00
|
|
|
ret = vfs_fsync_range(upperfile, start, end, datasync);
|
2024-11-05 21:28:06 +01:00
|
|
|
ovl_revert_creds(old_cred);
|
2018-07-18 15:44:42 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:44:42 +02:00
|
|
|
static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
|
|
|
|
{
|
2024-10-07 15:22:29 +02:00
|
|
|
struct ovl_file *of = file->private_data;
|
2023-10-13 12:49:37 +03:00
|
|
|
struct backing_file_ctx ctx = {
|
|
|
|
.cred = ovl_creds(file_inode(file)->i_sb),
|
|
|
|
.accessed = ovl_file_accessed,
|
|
|
|
};
|
2018-07-18 15:44:42 +02:00
|
|
|
|
2024-10-07 15:22:29 +02:00
|
|
|
return backing_file_mmap(of->realfile, vma, &ctx);
|
2018-07-18 15:44:42 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 15:44:42 +02:00
|
|
|
static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
|
|
|
|
{
|
|
|
|
struct inode *inode = file_inode(file);
|
2024-11-05 21:29:36 +01:00
|
|
|
struct file *realfile;
|
2018-07-18 15:44:42 +02:00
|
|
|
const struct cred *old_cred;
|
|
|
|
int ret;
|
|
|
|
|
2022-10-17 17:06:39 +02:00
|
|
|
inode_lock(inode);
|
|
|
|
/* Update mode */
|
|
|
|
ovl_copyattr(inode);
|
|
|
|
ret = file_remove_privs(file);
|
|
|
|
if (ret)
|
|
|
|
goto out_unlock;
|
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
realfile = ovl_real_file(file);
|
|
|
|
ret = PTR_ERR(realfile);
|
|
|
|
if (IS_ERR(realfile))
|
2022-10-17 17:06:39 +02:00
|
|
|
goto out_unlock;
|
2018-07-18 15:44:42 +02:00
|
|
|
|
|
|
|
old_cred = ovl_override_creds(file_inode(file)->i_sb);
|
2024-11-05 21:29:36 +01:00
|
|
|
ret = vfs_fallocate(realfile, mode, offset, len);
|
2024-11-05 11:35:13 -08:00
|
|
|
ovl_revert_creds(old_cred);
|
2018-07-18 15:44:42 +02:00
|
|
|
|
|
|
|
/* Update size */
|
2023-09-27 13:43:44 +03:00
|
|
|
ovl_file_modified(file);
|
2018-07-18 15:44:42 +02:00
|
|
|
|
2022-10-17 17:06:39 +02:00
|
|
|
out_unlock:
|
|
|
|
inode_unlock(inode);
|
|
|
|
|
2018-07-18 15:44:42 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-08-28 10:58:41 +03:00
|
|
|
static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
|
|
|
|
{
|
2024-11-05 21:29:36 +01:00
|
|
|
struct file *realfile;
|
2018-08-28 10:58:41 +03:00
|
|
|
const struct cred *old_cred;
|
|
|
|
int ret;
|
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
realfile = ovl_real_file(file);
|
|
|
|
if (IS_ERR(realfile))
|
|
|
|
return PTR_ERR(realfile);
|
2018-08-28 10:58:41 +03:00
|
|
|
|
|
|
|
old_cred = ovl_override_creds(file_inode(file)->i_sb);
|
2024-11-05 21:29:36 +01:00
|
|
|
ret = vfs_fadvise(realfile, offset, len, advice);
|
2024-11-05 11:35:13 -08:00
|
|
|
ovl_revert_creds(old_cred);
|
2018-08-28 10:58:41 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:44:42 +02:00
|
|
|
enum ovl_copyop {
|
|
|
|
OVL_COPY,
|
|
|
|
OVL_CLONE,
|
|
|
|
OVL_DEDUPE,
|
|
|
|
};
|
|
|
|
|
2018-10-30 10:41:49 +11:00
|
|
|
static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
|
2018-07-18 15:44:42 +02:00
|
|
|
struct file *file_out, loff_t pos_out,
|
2018-10-30 10:41:49 +11:00
|
|
|
loff_t len, unsigned int flags, enum ovl_copyop op)
|
2018-07-18 15:44:42 +02:00
|
|
|
{
|
|
|
|
struct inode *inode_out = file_inode(file_out);
|
2024-11-05 21:29:36 +01:00
|
|
|
struct file *realfile_in, *realfile_out;
|
2018-07-18 15:44:42 +02:00
|
|
|
const struct cred *old_cred;
|
2018-10-30 10:41:49 +11:00
|
|
|
loff_t ret;
|
2018-07-18 15:44:42 +02:00
|
|
|
|
2022-10-17 17:06:38 +02:00
|
|
|
inode_lock(inode_out);
|
|
|
|
if (op != OVL_DEDUPE) {
|
|
|
|
/* Update mode */
|
|
|
|
ovl_copyattr(inode_out);
|
|
|
|
ret = file_remove_privs(file_out);
|
|
|
|
if (ret)
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
realfile_out = ovl_real_file(file_out);
|
|
|
|
ret = PTR_ERR(realfile_out);
|
|
|
|
if (IS_ERR(realfile_out))
|
2022-10-17 17:06:38 +02:00
|
|
|
goto out_unlock;
|
2018-07-18 15:44:42 +02:00
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
realfile_in = ovl_real_file(file_in);
|
|
|
|
ret = PTR_ERR(realfile_in);
|
|
|
|
if (IS_ERR(realfile_in))
|
2022-10-17 17:06:38 +02:00
|
|
|
goto out_unlock;
|
2018-07-18 15:44:42 +02:00
|
|
|
|
|
|
|
old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
|
|
|
|
switch (op) {
|
|
|
|
case OVL_COPY:
|
2024-11-05 21:29:36 +01:00
|
|
|
ret = vfs_copy_file_range(realfile_in, pos_in,
|
|
|
|
realfile_out, pos_out, len, flags);
|
2018-07-18 15:44:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OVL_CLONE:
|
2024-11-05 21:29:36 +01:00
|
|
|
ret = vfs_clone_file_range(realfile_in, pos_in,
|
|
|
|
realfile_out, pos_out, len, flags);
|
2018-07-18 15:44:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OVL_DEDUPE:
|
2024-11-05 21:29:36 +01:00
|
|
|
ret = vfs_dedupe_file_range_one(realfile_in, pos_in,
|
|
|
|
realfile_out, pos_out, len,
|
2018-10-30 10:42:03 +11:00
|
|
|
flags);
|
2018-07-18 15:44:42 +02:00
|
|
|
break;
|
|
|
|
}
|
2024-11-05 11:35:13 -08:00
|
|
|
ovl_revert_creds(old_cred);
|
2018-07-18 15:44:42 +02:00
|
|
|
|
|
|
|
/* Update size */
|
2023-09-27 13:43:44 +03:00
|
|
|
ovl_file_modified(file_out);
|
2018-07-18 15:44:42 +02:00
|
|
|
|
2022-10-17 17:06:38 +02:00
|
|
|
out_unlock:
|
|
|
|
inode_unlock(inode_out);
|
|
|
|
|
2018-07-18 15:44:42 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
|
|
|
|
struct file *file_out, loff_t pos_out,
|
|
|
|
size_t len, unsigned int flags)
|
|
|
|
{
|
|
|
|
return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
|
|
|
|
OVL_COPY);
|
|
|
|
}
|
|
|
|
|
2018-10-30 10:41:49 +11:00
|
|
|
static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
|
|
|
|
struct file *file_out, loff_t pos_out,
|
|
|
|
loff_t len, unsigned int remap_flags)
|
2018-07-18 15:44:42 +02:00
|
|
|
{
|
2018-10-30 10:41:21 +11:00
|
|
|
enum ovl_copyop op;
|
|
|
|
|
|
|
|
if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (remap_flags & REMAP_FILE_DEDUP)
|
|
|
|
op = OVL_DEDUPE;
|
|
|
|
else
|
|
|
|
op = OVL_CLONE;
|
2018-07-18 15:44:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't copy up because of a dedupe request, this wouldn't make sense
|
|
|
|
* most of the time (data would be duplicated instead of deduplicated).
|
|
|
|
*/
|
2018-10-30 10:41:21 +11:00
|
|
|
if (op == OVL_DEDUPE &&
|
|
|
|
(!ovl_inode_upper(file_inode(file_in)) ||
|
|
|
|
!ovl_inode_upper(file_inode(file_out))))
|
2018-07-18 15:44:42 +02:00
|
|
|
return -EPERM;
|
|
|
|
|
2018-10-30 10:41:56 +11:00
|
|
|
return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
|
|
|
|
remap_flags, op);
|
2018-07-18 15:44:42 +02:00
|
|
|
}
|
|
|
|
|
2020-11-29 19:00:39 -08:00
|
|
|
static int ovl_flush(struct file *file, fl_owner_t id)
|
|
|
|
{
|
2024-11-05 21:29:36 +01:00
|
|
|
struct file *realfile;
|
2020-11-29 19:00:39 -08:00
|
|
|
const struct cred *old_cred;
|
2024-11-05 21:29:36 +01:00
|
|
|
int err = 0;
|
2020-11-29 19:00:39 -08:00
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
realfile = ovl_real_file(file);
|
|
|
|
if (IS_ERR(realfile))
|
|
|
|
return PTR_ERR(realfile);
|
2020-11-29 19:00:39 -08:00
|
|
|
|
2024-11-05 21:29:36 +01:00
|
|
|
if (realfile->f_op->flush) {
|
2020-11-29 19:00:39 -08:00
|
|
|
old_cred = ovl_override_creds(file_inode(file)->i_sb);
|
2024-11-05 21:29:36 +01:00
|
|
|
err = realfile->f_op->flush(realfile, id);
|
2024-11-05 11:35:13 -08:00
|
|
|
ovl_revert_creds(old_cred);
|
2020-11-29 19:00:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:44:41 +02:00
|
|
|
const struct file_operations ovl_file_operations = {
|
|
|
|
.open = ovl_open,
|
|
|
|
.release = ovl_release,
|
|
|
|
.llseek = ovl_llseek,
|
2018-07-18 15:44:41 +02:00
|
|
|
.read_iter = ovl_read_iter,
|
2018-07-18 15:44:41 +02:00
|
|
|
.write_iter = ovl_write_iter,
|
2018-07-18 15:44:42 +02:00
|
|
|
.fsync = ovl_fsync,
|
2018-07-18 15:44:42 +02:00
|
|
|
.mmap = ovl_mmap,
|
2018-07-18 15:44:42 +02:00
|
|
|
.fallocate = ovl_fallocate,
|
2018-08-28 10:58:41 +03:00
|
|
|
.fadvise = ovl_fadvise,
|
2020-11-29 19:00:39 -08:00
|
|
|
.flush = ovl_flush,
|
2023-05-22 14:49:57 +01:00
|
|
|
.splice_read = ovl_splice_read,
|
2021-07-28 10:38:43 +02:00
|
|
|
.splice_write = ovl_splice_write,
|
2018-07-18 15:44:42 +02:00
|
|
|
|
|
|
|
.copy_file_range = ovl_copy_file_range,
|
2018-10-30 10:41:21 +11:00
|
|
|
.remap_file_range = ovl_remap_file_range,
|
2018-07-18 15:44:41 +02:00
|
|
|
};
|