2019-06-04 16:11:33 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2016-12-16 18:02:56 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Novell Inc.
|
|
|
|
* Copyright (C) 2016 Red Hat, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/mount.h>
|
|
|
|
#include <linux/slab.h>
|
2017-02-03 00:54:15 +08:00
|
|
|
#include <linux/cred.h>
|
2016-12-16 18:02:56 +08:00
|
|
|
#include <linux/xattr.h>
|
2017-06-21 20:28:36 +08:00
|
|
|
#include <linux/exportfs.h>
|
2021-06-19 17:26:19 +08:00
|
|
|
#include <linux/fileattr.h>
|
2017-06-21 20:28:36 +08:00
|
|
|
#include <linux/uuid.h>
|
2017-06-21 18:46:12 +08:00
|
|
|
#include <linux/namei.h>
|
|
|
|
#include <linux/ratelimit.h>
|
2016-12-16 18:02:56 +08:00
|
|
|
#include "overlayfs.h"
|
|
|
|
|
|
|
|
int ovl_want_write(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
|
2020-06-04 16:48:19 +08:00
|
|
|
return mnt_want_write(ovl_upper_mnt(ofs));
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ovl_drop_write(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
|
2020-06-04 16:48:19 +08:00
|
|
|
mnt_drop_write(ovl_upper_mnt(ofs));
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct dentry *ovl_workdir(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
|
|
|
|
return ofs->workdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct cred *ovl_override_creds(struct super_block *sb)
|
|
|
|
{
|
|
|
|
struct ovl_fs *ofs = sb->s_fs_info;
|
|
|
|
|
|
|
|
return override_creds(ofs->creator_cred);
|
|
|
|
}
|
|
|
|
|
2017-11-07 19:55:04 +08:00
|
|
|
/*
|
|
|
|
* Check if underlying fs supports file handles and try to determine encoding
|
|
|
|
* type, in order to deduce maximum inode number used by fs.
|
|
|
|
*
|
|
|
|
* Return 0 if file handles are not supported.
|
|
|
|
* Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
|
|
|
|
* Return -1 if fs uses a non default encoding with unknown inode size.
|
|
|
|
*/
|
|
|
|
int ovl_can_decode_fh(struct super_block *sb)
|
2017-06-21 20:28:36 +08:00
|
|
|
{
|
2020-12-14 22:26:14 +08:00
|
|
|
if (!capable(CAP_DAC_READ_SEARCH))
|
|
|
|
return 0;
|
|
|
|
|
2018-09-03 14:12:09 +08:00
|
|
|
if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
|
2017-11-07 19:55:04 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
|
2017-06-21 20:28:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct dentry *ovl_indexdir(struct super_block *sb)
|
|
|
|
{
|
|
|
|
struct ovl_fs *ofs = sb->s_fs_info;
|
|
|
|
|
|
|
|
return ofs->indexdir;
|
|
|
|
}
|
|
|
|
|
2018-01-19 17:26:53 +08:00
|
|
|
/* Index all files on copy up. For now only enabled for NFS export */
|
|
|
|
bool ovl_index_all(struct super_block *sb)
|
|
|
|
{
|
|
|
|
struct ovl_fs *ofs = sb->s_fs_info;
|
|
|
|
|
|
|
|
return ofs->config.nfs_export && ofs->config.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify lower origin on lookup. For now only enabled for NFS export */
|
|
|
|
bool ovl_verify_lower(struct super_block *sb)
|
|
|
|
{
|
|
|
|
struct ovl_fs *ofs = sb->s_fs_info;
|
|
|
|
|
|
|
|
return ofs->config.nfs_export && ofs->config.index;
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:02:56 +08:00
|
|
|
struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
|
|
|
|
{
|
|
|
|
size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
|
|
|
|
struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
|
|
|
|
|
|
|
|
if (oe)
|
|
|
|
oe->numlower = numlower;
|
|
|
|
|
|
|
|
return oe;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ovl_dentry_remote(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
return dentry->d_flags &
|
2020-03-17 22:04:22 +08:00
|
|
|
(DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
2020-03-17 22:04:22 +08:00
|
|
|
void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
|
|
|
|
unsigned int mask)
|
|
|
|
{
|
|
|
|
struct ovl_entry *oe = OVL_E(dentry);
|
|
|
|
unsigned int i, flags = 0;
|
|
|
|
|
2020-03-17 22:04:22 +08:00
|
|
|
if (upperdentry)
|
|
|
|
flags |= upperdentry->d_flags;
|
2020-03-17 22:04:22 +08:00
|
|
|
for (i = 0; i < oe->numlower; i++)
|
|
|
|
flags |= oe->lowerstack[i].dentry->d_flags;
|
|
|
|
|
|
|
|
spin_lock(&dentry->d_lock);
|
|
|
|
dentry->d_flags &= ~mask;
|
|
|
|
dentry->d_flags |= flags & mask;
|
|
|
|
spin_unlock(&dentry->d_lock);
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:02:56 +08:00
|
|
|
bool ovl_dentry_weird(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
|
|
|
|
DCACHE_MANAGE_TRANSIT |
|
|
|
|
DCACHE_OP_HASH |
|
|
|
|
DCACHE_OP_COMPARE);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ovl_path_type ovl_path_type(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct ovl_entry *oe = dentry->d_fsdata;
|
|
|
|
enum ovl_path_type type = 0;
|
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
if (ovl_dentry_upper(dentry)) {
|
2016-12-16 18:02:56 +08:00
|
|
|
type = __OVL_PATH_UPPER;
|
|
|
|
|
|
|
|
/*
|
2017-04-24 04:12:34 +08:00
|
|
|
* Non-dir dentry can hold lower dentry of its copy up origin.
|
2016-12-16 18:02:56 +08:00
|
|
|
*/
|
2017-04-24 04:12:34 +08:00
|
|
|
if (oe->numlower) {
|
2018-05-11 23:49:32 +08:00
|
|
|
if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
|
|
|
|
type |= __OVL_PATH_ORIGIN;
|
2018-05-11 23:49:32 +08:00
|
|
|
if (d_is_dir(dentry) ||
|
|
|
|
!ovl_has_upperdata(d_inode(dentry)))
|
2017-04-24 04:12:34 +08:00
|
|
|
type |= __OVL_PATH_MERGE;
|
|
|
|
}
|
2016-12-16 18:02:56 +08:00
|
|
|
} else {
|
|
|
|
if (oe->numlower > 1)
|
|
|
|
type |= __OVL_PATH_MERGE;
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ovl_path_upper(struct dentry *dentry, struct path *path)
|
|
|
|
{
|
|
|
|
struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
|
|
|
|
|
2020-06-04 16:48:19 +08:00
|
|
|
path->mnt = ovl_upper_mnt(ofs);
|
2017-07-05 04:03:16 +08:00
|
|
|
path->dentry = ovl_dentry_upper(dentry);
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ovl_path_lower(struct dentry *dentry, struct path *path)
|
|
|
|
{
|
|
|
|
struct ovl_entry *oe = dentry->d_fsdata;
|
|
|
|
|
2017-07-24 14:57:54 +08:00
|
|
|
if (oe->numlower) {
|
|
|
|
path->mnt = oe->lowerstack[0].layer->mnt;
|
|
|
|
path->dentry = oe->lowerstack[0].dentry;
|
|
|
|
} else {
|
|
|
|
*path = (struct path) { };
|
|
|
|
}
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 23:49:30 +08:00
|
|
|
void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
|
|
|
|
{
|
|
|
|
struct ovl_entry *oe = dentry->d_fsdata;
|
|
|
|
|
|
|
|
if (oe->numlower) {
|
|
|
|
path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
|
|
|
|
path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
|
|
|
|
} else {
|
|
|
|
*path = (struct path) { };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:02:56 +08:00
|
|
|
enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
|
|
|
|
{
|
|
|
|
enum ovl_path_type type = ovl_path_type(dentry);
|
|
|
|
|
|
|
|
if (!OVL_TYPE_UPPER(type))
|
|
|
|
ovl_path_lower(dentry, path);
|
|
|
|
else
|
|
|
|
ovl_path_upper(dentry, path);
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2022-04-04 18:51:47 +08:00
|
|
|
enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
|
|
|
|
{
|
|
|
|
enum ovl_path_type type = ovl_path_type(dentry);
|
|
|
|
|
|
|
|
WARN_ON_ONCE(d_is_dir(dentry));
|
|
|
|
|
|
|
|
if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
|
|
|
|
ovl_path_lowerdata(dentry, path);
|
|
|
|
else
|
|
|
|
ovl_path_upper(dentry, path);
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:02:56 +08:00
|
|
|
struct dentry *ovl_dentry_upper(struct dentry *dentry)
|
|
|
|
{
|
2017-07-05 04:03:16 +08:00
|
|
|
return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct dentry *ovl_dentry_lower(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct ovl_entry *oe = dentry->d_fsdata;
|
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
return oe->numlower ? oe->lowerstack[0].dentry : NULL;
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
2020-01-24 16:46:45 +08:00
|
|
|
const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
|
2017-11-09 01:39:51 +08:00
|
|
|
{
|
|
|
|
struct ovl_entry *oe = dentry->d_fsdata;
|
|
|
|
|
|
|
|
return oe->numlower ? oe->lowerstack[0].layer : NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:49:30 +08:00
|
|
|
/*
|
|
|
|
* ovl_dentry_lower() could return either a data dentry or metacopy dentry
|
2021-03-18 19:30:27 +08:00
|
|
|
* depending on what is stored in lowerstack[0]. At times we need to find
|
2018-05-11 23:49:30 +08:00
|
|
|
* lower dentry which has data (and not metacopy dentry). This helper
|
|
|
|
* returns the lower data dentry.
|
|
|
|
*/
|
|
|
|
struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct ovl_entry *oe = dentry->d_fsdata;
|
|
|
|
|
|
|
|
return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:02:56 +08:00
|
|
|
struct dentry *ovl_dentry_real(struct dentry *dentry)
|
|
|
|
{
|
2017-07-05 04:03:16 +08:00
|
|
|
return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
2017-07-20 17:08:21 +08:00
|
|
|
struct dentry *ovl_i_dentry_upper(struct inode *inode)
|
|
|
|
{
|
|
|
|
return ovl_upperdentry_dereference(OVL_I(inode));
|
|
|
|
}
|
|
|
|
|
2022-04-04 18:51:53 +08:00
|
|
|
void ovl_i_path_real(struct inode *inode, struct path *path)
|
|
|
|
{
|
|
|
|
path->dentry = ovl_i_dentry_upper(inode);
|
|
|
|
if (!path->dentry) {
|
|
|
|
path->dentry = OVL_I(inode)->lowerpath.dentry;
|
|
|
|
path->mnt = OVL_I(inode)->lowerpath.layer->mnt;
|
|
|
|
} else {
|
|
|
|
path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
struct inode *ovl_inode_upper(struct inode *inode)
|
2017-07-05 04:03:16 +08:00
|
|
|
{
|
2017-07-20 17:08:21 +08:00
|
|
|
struct dentry *upperdentry = ovl_i_dentry_upper(inode);
|
2017-07-05 04:03:16 +08:00
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
return upperdentry ? d_inode(upperdentry) : NULL;
|
|
|
|
}
|
2017-07-05 04:03:16 +08:00
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
struct inode *ovl_inode_lower(struct inode *inode)
|
|
|
|
{
|
2022-04-04 18:51:53 +08:00
|
|
|
struct dentry *lowerdentry = OVL_I(inode)->lowerpath.dentry;
|
|
|
|
|
|
|
|
return lowerdentry ? d_inode(lowerdentry) : NULL;
|
2017-07-05 04:03:16 +08:00
|
|
|
}
|
2017-07-05 04:03:16 +08:00
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
struct inode *ovl_inode_real(struct inode *inode)
|
|
|
|
{
|
|
|
|
return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
|
2017-07-05 04:03:16 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 23:49:30 +08:00
|
|
|
/* Return inode which contains lower data. Do not return metacopy */
|
|
|
|
struct inode *ovl_inode_lowerdata(struct inode *inode)
|
|
|
|
{
|
|
|
|
if (WARN_ON(!S_ISREG(inode->i_mode)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
|
|
|
|
}
|
2017-07-05 04:03:16 +08:00
|
|
|
|
2018-05-11 23:49:31 +08:00
|
|
|
/* Return real inode which contains data. Does not return metacopy inode */
|
|
|
|
struct inode *ovl_inode_realdata(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct inode *upperinode;
|
|
|
|
|
|
|
|
upperinode = ovl_inode_upper(inode);
|
|
|
|
if (upperinode && ovl_has_upperdata(inode))
|
|
|
|
return upperinode;
|
|
|
|
|
|
|
|
return ovl_inode_lowerdata(inode);
|
|
|
|
}
|
|
|
|
|
2017-07-28 03:54:06 +08:00
|
|
|
struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
|
2016-12-16 18:02:56 +08:00
|
|
|
{
|
2017-07-28 03:54:06 +08:00
|
|
|
return OVL_I(inode)->cache;
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
2017-07-28 03:54:06 +08:00
|
|
|
void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
|
2016-12-16 18:02:56 +08:00
|
|
|
{
|
2017-07-28 03:54:06 +08:00
|
|
|
OVL_I(inode)->cache = cache;
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
2018-01-15 01:25:31 +08:00
|
|
|
void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
|
|
|
|
{
|
|
|
|
set_bit(flag, &OVL_E(dentry)->flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
|
|
|
|
{
|
|
|
|
clear_bit(flag, &OVL_E(dentry)->flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
|
|
|
|
{
|
|
|
|
return test_bit(flag, &OVL_E(dentry)->flags);
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:02:56 +08:00
|
|
|
bool ovl_dentry_is_opaque(struct dentry *dentry)
|
|
|
|
{
|
2018-01-15 01:25:31 +08:00
|
|
|
return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ovl_dentry_is_whiteout(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:02:57 +08:00
|
|
|
void ovl_dentry_set_opaque(struct dentry *dentry)
|
2016-12-16 18:02:56 +08:00
|
|
|
{
|
2018-01-15 01:25:31 +08:00
|
|
|
ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
2017-07-05 04:03:18 +08:00
|
|
|
/*
|
2017-10-15 23:00:20 +08:00
|
|
|
* For hard links and decoded file handles, it's possible for ovl_dentry_upper()
|
|
|
|
* to return positive, while there's no actual upper alias for the inode.
|
|
|
|
* Copy up code needs to know about the existence of the upper alias, so it
|
|
|
|
* can't use ovl_dentry_upper().
|
2017-07-05 04:03:18 +08:00
|
|
|
*/
|
|
|
|
bool ovl_dentry_has_upper_alias(struct dentry *dentry)
|
|
|
|
{
|
2018-01-15 01:25:31 +08:00
|
|
|
return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
|
2017-07-05 04:03:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ovl_dentry_set_upper_alias(struct dentry *dentry)
|
|
|
|
{
|
2018-01-15 01:25:31 +08:00
|
|
|
ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
|
2017-07-05 04:03:18 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 23:49:28 +08:00
|
|
|
static bool ovl_should_check_upperdata(struct inode *inode)
|
|
|
|
{
|
|
|
|
if (!S_ISREG(inode->i_mode))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!ovl_inode_lower(inode))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ovl_has_upperdata(struct inode *inode)
|
|
|
|
{
|
|
|
|
if (!ovl_should_check_upperdata(inode))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!ovl_test_flag(OVL_UPPERDATA, inode))
|
|
|
|
return false;
|
|
|
|
/*
|
|
|
|
* Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
|
|
|
|
* ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
|
|
|
|
* if setting of OVL_UPPERDATA is visible, then effects of writes
|
|
|
|
* before that are visible too.
|
|
|
|
*/
|
|
|
|
smp_rmb();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ovl_set_upperdata(struct inode *inode)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
|
|
|
|
* if OVL_UPPERDATA flag is visible, then effects of write operations
|
|
|
|
* before it are visible as well.
|
|
|
|
*/
|
|
|
|
smp_wmb();
|
|
|
|
ovl_set_flag(OVL_UPPERDATA, inode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Caller should hold ovl_inode->lock */
|
|
|
|
bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
|
|
|
|
{
|
|
|
|
if (!ovl_open_flags_need_copy_up(flags))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
|
|
|
|
{
|
|
|
|
if (!ovl_open_flags_need_copy_up(flags))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !ovl_has_upperdata(d_inode(dentry));
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:02:56 +08:00
|
|
|
bool ovl_redirect_dir(struct super_block *sb)
|
|
|
|
{
|
|
|
|
struct ovl_fs *ofs = sb->s_fs_info;
|
|
|
|
|
2017-05-17 05:12:41 +08:00
|
|
|
return ofs->config.redirect_dir && !ofs->noxattr;
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *ovl_dentry_get_redirect(struct dentry *dentry)
|
|
|
|
{
|
2017-07-05 04:03:16 +08:00
|
|
|
return OVL_I(d_inode(dentry))->redirect;
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
|
|
|
|
{
|
2017-07-05 04:03:16 +08:00
|
|
|
struct ovl_inode *oi = OVL_I(d_inode(dentry));
|
2016-12-16 18:02:56 +08:00
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
kfree(oi->redirect);
|
|
|
|
oi->redirect = redirect;
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
|
2016-12-16 18:02:56 +08:00
|
|
|
{
|
2017-07-05 04:03:16 +08:00
|
|
|
struct inode *upperinode = d_inode(upperdentry);
|
2017-07-05 04:03:16 +08:00
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
WARN_ON(OVL_I(inode)->__upperdentry);
|
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
/*
|
2017-07-05 04:03:16 +08:00
|
|
|
* Make sure upperdentry is consistent before making it visible
|
2017-07-05 04:03:16 +08:00
|
|
|
*/
|
|
|
|
smp_wmb();
|
2017-07-05 04:03:16 +08:00
|
|
|
OVL_I(inode)->__upperdentry = upperdentry;
|
2018-01-15 00:35:40 +08:00
|
|
|
if (inode_unhashed(inode)) {
|
2017-07-05 04:03:16 +08:00
|
|
|
inode->i_private = upperinode;
|
2016-12-16 18:02:56 +08:00
|
|
|
__insert_inode_hash(inode, (unsigned long) upperinode);
|
2017-07-05 04:03:16 +08:00
|
|
|
}
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:22:23 +08:00
|
|
|
static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
|
2016-12-16 18:02:56 +08:00
|
|
|
{
|
2017-07-05 04:03:16 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
2016-12-16 18:02:56 +08:00
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
WARN_ON(!inode_is_locked(inode));
|
2021-04-11 17:22:23 +08:00
|
|
|
WARN_ON(!d_is_dir(dentry));
|
2017-07-28 03:54:06 +08:00
|
|
|
/*
|
2021-04-11 17:22:23 +08:00
|
|
|
* Version is used by readdir code to keep cache consistent.
|
|
|
|
* For merge dirs (or dirs with origin) all changes need to be noted.
|
|
|
|
* For non-merge dirs, cache contains only impure entries (i.e. ones
|
|
|
|
* which have been copied up and have origins), so only need to note
|
|
|
|
* changes to impure entries.
|
2017-07-28 03:54:06 +08:00
|
|
|
*/
|
2021-04-11 17:22:23 +08:00
|
|
|
if (!ovl_dir_is_real(dentry) || impurity)
|
2017-07-28 03:54:06 +08:00
|
|
|
OVL_I(inode)->version++;
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
2018-07-18 21:44:40 +08:00
|
|
|
void ovl_dir_modified(struct dentry *dentry, bool impurity)
|
|
|
|
{
|
|
|
|
/* Copy mtime/ctime */
|
2022-04-04 18:51:54 +08:00
|
|
|
ovl_copyattr(d_inode(dentry));
|
2018-07-18 21:44:40 +08:00
|
|
|
|
2021-04-11 17:22:23 +08:00
|
|
|
ovl_dir_version_inc(dentry, impurity);
|
2018-07-18 21:44:40 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 18:02:56 +08:00
|
|
|
u64 ovl_dentry_version_get(struct dentry *dentry)
|
|
|
|
{
|
2017-07-05 04:03:16 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
2016-12-16 18:02:56 +08:00
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
WARN_ON(!inode_is_locked(inode));
|
|
|
|
return OVL_I(inode)->version;
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ovl_is_whiteout(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct inode *inode = dentry->d_inode;
|
|
|
|
|
|
|
|
return inode && IS_WHITEOUT(inode);
|
|
|
|
}
|
|
|
|
|
2022-08-05 01:11:15 +08:00
|
|
|
struct file *ovl_path_open(const struct path *path, int flags)
|
2016-12-16 18:02:56 +08:00
|
|
|
{
|
2020-06-03 04:20:26 +08:00
|
|
|
struct inode *inode = d_inode(path->dentry);
|
2022-04-04 18:51:56 +08:00
|
|
|
struct user_namespace *real_mnt_userns = mnt_user_ns(path->mnt);
|
2020-06-03 04:20:26 +08:00
|
|
|
int err, acc_mode;
|
|
|
|
|
|
|
|
if (flags & ~(O_ACCMODE | O_LARGEFILE))
|
|
|
|
BUG();
|
|
|
|
|
|
|
|
switch (flags & O_ACCMODE) {
|
|
|
|
case O_RDONLY:
|
|
|
|
acc_mode = MAY_READ;
|
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
acc_mode = MAY_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
2022-04-04 18:51:56 +08:00
|
|
|
err = inode_permission(real_mnt_userns, inode, acc_mode | MAY_OPEN);
|
2020-06-03 04:20:26 +08:00
|
|
|
if (err)
|
|
|
|
return ERR_PTR(err);
|
|
|
|
|
|
|
|
/* O_NOATIME is an optimization, don't fail if not permitted */
|
2022-04-04 18:51:56 +08:00
|
|
|
if (inode_owner_or_capable(real_mnt_userns, inode))
|
2020-06-03 04:20:26 +08:00
|
|
|
flags |= O_NOATIME;
|
|
|
|
|
|
|
|
return dentry_open(path, flags, current_cred());
|
2016-12-16 18:02:56 +08:00
|
|
|
}
|
2017-01-17 12:34:56 +08:00
|
|
|
|
2018-05-11 23:49:28 +08:00
|
|
|
/* Caller should hold ovl_inode->lock */
|
|
|
|
static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
|
|
|
|
{
|
|
|
|
bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
|
|
|
|
|
|
|
|
if (ovl_dentry_upper(dentry) &&
|
|
|
|
(ovl_dentry_has_upper_alias(dentry) || disconnected) &&
|
|
|
|
!ovl_dentry_needs_data_copy_up_locked(dentry, flags))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ovl_already_copied_up(struct dentry *dentry, int flags)
|
2018-05-11 23:49:28 +08:00
|
|
|
{
|
|
|
|
bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if copy-up has happened as well as for upper alias (in
|
|
|
|
* case of hard links) is there.
|
|
|
|
*
|
|
|
|
* Both checks are lockless:
|
|
|
|
* - false negatives: will recheck under oi->lock
|
|
|
|
* - false positives:
|
|
|
|
* + ovl_dentry_upper() uses memory barriers to ensure the
|
|
|
|
* upper dentry is up-to-date
|
|
|
|
* + ovl_dentry_has_upper_alias() relies on locking of
|
|
|
|
* upper parent i_rwsem to prevent reordering copy-up
|
|
|
|
* with rename.
|
|
|
|
*/
|
|
|
|
if (ovl_dentry_upper(dentry) &&
|
2018-05-11 23:49:28 +08:00
|
|
|
(ovl_dentry_has_upper_alias(dentry) || disconnected) &&
|
|
|
|
!ovl_dentry_needs_data_copy_up(dentry, flags))
|
2018-05-11 23:49:28 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:49:28 +08:00
|
|
|
int ovl_copy_up_start(struct dentry *dentry, int flags)
|
2017-01-17 12:34:56 +08:00
|
|
|
{
|
2018-10-18 23:37:14 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
2017-01-17 12:34:56 +08:00
|
|
|
int err;
|
|
|
|
|
2020-03-02 21:03:35 +08:00
|
|
|
err = ovl_inode_lock_interruptible(inode);
|
2018-05-11 23:49:28 +08:00
|
|
|
if (!err && ovl_already_copied_up_locked(dentry, flags)) {
|
2017-06-21 20:28:51 +08:00
|
|
|
err = 1; /* Already copied up */
|
2018-10-18 23:37:14 +08:00
|
|
|
ovl_inode_unlock(inode);
|
2017-01-17 12:34:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ovl_copy_up_end(struct dentry *dentry)
|
|
|
|
{
|
2018-10-18 23:37:14 +08:00
|
|
|
ovl_inode_unlock(d_inode(dentry));
|
2017-01-17 12:34:56 +08:00
|
|
|
}
|
2017-05-17 05:12:40 +08:00
|
|
|
|
2022-08-05 01:11:15 +08:00
|
|
|
bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
|
2017-06-25 21:37:17 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
2022-04-04 18:51:50 +08:00
|
|
|
res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
|
2017-06-25 21:37:17 +08:00
|
|
|
|
|
|
|
/* Zero size value means "copied up but origin unknown" */
|
|
|
|
if (res >= 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-05 01:11:15 +08:00
|
|
|
bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
|
2022-04-04 18:51:50 +08:00
|
|
|
enum ovl_xattr ox)
|
2017-05-24 20:29:33 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char val;
|
|
|
|
|
2022-04-04 18:51:50 +08:00
|
|
|
if (!d_is_dir(path->dentry))
|
2017-05-24 20:29:33 +08:00
|
|
|
return false;
|
|
|
|
|
2022-04-04 18:51:50 +08:00
|
|
|
res = ovl_path_getxattr(ofs, path, ox, &val, 1);
|
2017-05-24 20:29:33 +08:00
|
|
|
if (res == 1 && val == 'y')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-02 16:58:49 +08:00
|
|
|
#define OVL_XATTR_OPAQUE_POSTFIX "opaque"
|
|
|
|
#define OVL_XATTR_REDIRECT_POSTFIX "redirect"
|
|
|
|
#define OVL_XATTR_ORIGIN_POSTFIX "origin"
|
|
|
|
#define OVL_XATTR_IMPURE_POSTFIX "impure"
|
|
|
|
#define OVL_XATTR_NLINK_POSTFIX "nlink"
|
|
|
|
#define OVL_XATTR_UPPER_POSTFIX "upper"
|
|
|
|
#define OVL_XATTR_METACOPY_POSTFIX "metacopy"
|
2021-06-19 17:26:19 +08:00
|
|
|
#define OVL_XATTR_PROTATTR_POSTFIX "protattr"
|
2020-09-02 16:58:49 +08:00
|
|
|
|
|
|
|
#define OVL_XATTR_TAB_ENTRY(x) \
|
2020-12-14 22:26:14 +08:00
|
|
|
[x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
|
|
|
|
[true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
|
2020-09-02 16:58:49 +08:00
|
|
|
|
2020-12-14 22:26:14 +08:00
|
|
|
const char *const ovl_xattr_table[][2] = {
|
2020-09-02 16:58:49 +08:00
|
|
|
OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
|
|
|
|
OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
|
|
|
|
OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
|
|
|
|
OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
|
|
|
|
OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
|
|
|
|
OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
|
|
|
|
OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
|
2021-06-19 17:26:19 +08:00
|
|
|
OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
|
2020-09-02 16:58:49 +08:00
|
|
|
};
|
|
|
|
|
2021-06-19 17:26:17 +08:00
|
|
|
int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
|
2020-09-02 16:58:49 +08:00
|
|
|
enum ovl_xattr ox, const void *value, size_t size,
|
2017-05-17 05:12:40 +08:00
|
|
|
int xerr)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (ofs->noxattr)
|
|
|
|
return xerr;
|
|
|
|
|
2022-04-04 18:51:42 +08:00
|
|
|
err = ovl_setxattr(ofs, upperdentry, ox, value, size);
|
2017-05-17 05:12:40 +08:00
|
|
|
|
|
|
|
if (err == -EOPNOTSUPP) {
|
2020-09-02 16:58:49 +08:00
|
|
|
pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
|
2017-05-17 05:12:40 +08:00
|
|
|
ofs->noxattr = true;
|
|
|
|
return xerr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2017-05-24 20:29:33 +08:00
|
|
|
|
|
|
|
int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
|
|
|
|
{
|
2021-06-19 17:26:17 +08:00
|
|
|
struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
|
2017-05-24 20:29:33 +08:00
|
|
|
int err;
|
|
|
|
|
2017-07-05 04:03:16 +08:00
|
|
|
if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
|
2017-05-24 20:29:33 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do not fail when upper doesn't support xattrs.
|
|
|
|
* Upper inodes won't have origin nor redirect xattr anyway.
|
|
|
|
*/
|
2021-06-19 17:26:17 +08:00
|
|
|
err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
|
2017-05-24 20:29:33 +08:00
|
|
|
if (!err)
|
2017-07-05 04:03:16 +08:00
|
|
|
ovl_set_flag(OVL_IMPURE, d_inode(dentry));
|
2017-05-24 20:29:33 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2017-07-05 04:03:16 +08:00
|
|
|
|
2021-06-19 17:26:19 +08:00
|
|
|
|
|
|
|
#define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
|
|
|
|
|
|
|
|
void ovl_check_protattr(struct inode *inode, struct dentry *upper)
|
|
|
|
{
|
|
|
|
struct ovl_fs *ofs = OVL_FS(inode->i_sb);
|
|
|
|
u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
|
|
|
|
char buf[OVL_PROTATTR_MAX+1];
|
|
|
|
int res, n;
|
|
|
|
|
2022-04-04 18:51:50 +08:00
|
|
|
res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
|
|
|
|
OVL_PROTATTR_MAX);
|
2021-06-19 17:26:19 +08:00
|
|
|
if (res < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize inode flags from overlay.protattr xattr and upper inode
|
|
|
|
* flags. If upper inode has those fileattr flags set (i.e. from old
|
|
|
|
* kernel), we do not clear them on ovl_get_inode(), but we will clear
|
|
|
|
* them on next fileattr_set().
|
|
|
|
*/
|
|
|
|
for (n = 0; n < res; n++) {
|
|
|
|
if (buf[n] == 'a')
|
|
|
|
iflags |= S_APPEND;
|
|
|
|
else if (buf[n] == 'i')
|
|
|
|
iflags |= S_IMMUTABLE;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!res || n < res) {
|
|
|
|
pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
|
|
|
|
upper, res);
|
|
|
|
} else {
|
|
|
|
inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ovl_set_protattr(struct inode *inode, struct dentry *upper,
|
|
|
|
struct fileattr *fa)
|
|
|
|
{
|
|
|
|
struct ovl_fs *ofs = OVL_FS(inode->i_sb);
|
|
|
|
char buf[OVL_PROTATTR_MAX];
|
|
|
|
int len = 0, err = 0;
|
|
|
|
u32 iflags = 0;
|
|
|
|
|
|
|
|
BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
|
|
|
|
|
|
|
|
if (fa->flags & FS_APPEND_FL) {
|
|
|
|
buf[len++] = 'a';
|
|
|
|
iflags |= S_APPEND;
|
|
|
|
}
|
|
|
|
if (fa->flags & FS_IMMUTABLE_FL) {
|
|
|
|
buf[len++] = 'i';
|
|
|
|
iflags |= S_IMMUTABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do not allow to set protection flags when upper doesn't support
|
|
|
|
* xattrs, because we do not set those fileattr flags on upper inode.
|
|
|
|
* Remove xattr if it exist and all protection flags are cleared.
|
|
|
|
*/
|
|
|
|
if (len) {
|
|
|
|
err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
|
|
|
|
buf, len, -EPERM);
|
|
|
|
} else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
|
2022-04-04 18:51:42 +08:00
|
|
|
err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
|
2021-06-19 17:26:19 +08:00
|
|
|
if (err == -EOPNOTSUPP || err == -ENODATA)
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
|
|
|
|
|
|
|
|
/* Mask out the fileattr flags that should not be set in upper inode */
|
|
|
|
fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
|
|
|
|
fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:28:32 +08:00
|
|
|
/**
|
|
|
|
* Caller must hold a reference to inode to prevent it from being freed while
|
|
|
|
* it is marked inuse.
|
|
|
|
*/
|
|
|
|
bool ovl_inuse_trylock(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct inode *inode = d_inode(dentry);
|
|
|
|
bool locked = false;
|
|
|
|
|
|
|
|
spin_lock(&inode->i_lock);
|
|
|
|
if (!(inode->i_state & I_OVL_INUSE)) {
|
|
|
|
inode->i_state |= I_OVL_INUSE;
|
|
|
|
locked = true;
|
|
|
|
}
|
|
|
|
spin_unlock(&inode->i_lock);
|
|
|
|
|
|
|
|
return locked;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ovl_inuse_unlock(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
if (dentry) {
|
|
|
|
struct inode *inode = d_inode(dentry);
|
|
|
|
|
|
|
|
spin_lock(&inode->i_lock);
|
|
|
|
WARN_ON(!(inode->i_state & I_OVL_INUSE));
|
|
|
|
inode->i_state &= ~I_OVL_INUSE;
|
|
|
|
spin_unlock(&inode->i_lock);
|
|
|
|
}
|
|
|
|
}
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
|
ovl: detect overlapping layers
Overlapping overlay layers are not supported and can cause unexpected
behavior, but overlayfs does not currently check or warn about these
configurations.
User is not supposed to specify the same directory for upper and
lower dirs or for different lower layers and user is not supposed to
specify directories that are descendants of each other for overlay
layers, but that is exactly what this zysbot repro did:
https://syzkaller.appspot.com/x/repro.syz?x=12c7a94f400000
Moving layer root directories into other layers while overlayfs
is mounted could also result in unexpected behavior.
This commit places "traps" in the overlay inode hash table.
Those traps are dummy overlay inodes that are hashed by the layers
root inodes.
On mount, the hash table trap entries are used to verify that overlay
layers are not overlapping. While at it, we also verify that overlay
layers are not overlapping with directories "in-use" by other overlay
instances as upperdir/workdir.
On lookup, the trap entries are used to verify that overlay layers
root inodes have not been moved into other layers after mount.
Some examples:
$ ./run --ov --samefs -s
...
( mkdir -p base/upper/0/u base/upper/0/w base/lower lower upper mnt
mount -o bind base/lower lower
mount -o bind base/upper upper
mount -t overlay none mnt ...
-o lowerdir=lower,upperdir=upper/0/u,workdir=upper/0/w)
$ umount mnt
$ mount -t overlay none mnt ...
-o lowerdir=base,upperdir=upper/0/u,workdir=upper/0/w
[ 94.434900] overlayfs: overlapping upperdir path
mount: mount overlay on mnt failed: Too many levels of symbolic links
$ mount -t overlay none mnt ...
-o lowerdir=upper/0/u,upperdir=upper/0/u,workdir=upper/0/w
[ 151.350132] overlayfs: conflicting lowerdir path
mount: none is already mounted or mnt busy
$ mount -t overlay none mnt ...
-o lowerdir=lower:lower/a,upperdir=upper/0/u,workdir=upper/0/w
[ 201.205045] overlayfs: overlapping lowerdir path
mount: mount overlay on mnt failed: Too many levels of symbolic links
$ mount -t overlay none mnt ...
-o lowerdir=lower,upperdir=upper/0/u,workdir=upper/0/w
$ mv base/upper/0/ base/lower/
$ find mnt/0
mnt/0
mnt/0/w
find: 'mnt/0/w/work': Too many levels of symbolic links
find: 'mnt/0/u': Too many levels of symbolic links
Reported-by: syzbot+9c69c282adc4edd2b540@syzkaller.appspotmail.com
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2019-04-18 22:42:08 +08:00
|
|
|
bool ovl_is_inuse(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct inode *inode = d_inode(dentry);
|
|
|
|
bool inuse;
|
|
|
|
|
|
|
|
spin_lock(&inode->i_lock);
|
|
|
|
inuse = (inode->i_state & I_OVL_INUSE);
|
|
|
|
spin_unlock(&inode->i_lock);
|
|
|
|
|
|
|
|
return inuse;
|
|
|
|
}
|
|
|
|
|
2017-09-26 12:55:26 +08:00
|
|
|
/*
|
|
|
|
* Does this overlay dentry need to be indexed on copy up?
|
|
|
|
*/
|
|
|
|
bool ovl_need_index(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct dentry *lower = ovl_dentry_lower(dentry);
|
|
|
|
|
|
|
|
if (!lower || !ovl_indexdir(dentry->d_sb))
|
|
|
|
return false;
|
|
|
|
|
2017-11-22 06:08:21 +08:00
|
|
|
/* Index all files for NFS export and consistency verification */
|
2018-01-11 20:01:08 +08:00
|
|
|
if (ovl_index_all(dentry->d_sb))
|
2017-11-22 06:08:21 +08:00
|
|
|
return true;
|
|
|
|
|
2017-09-26 12:55:26 +08:00
|
|
|
/* Index only lower hardlinks on copy up */
|
|
|
|
if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-24 22:36:26 +08:00
|
|
|
/* Caller must hold OVL_I(inode)->lock */
|
2017-06-21 18:46:12 +08:00
|
|
|
static void ovl_cleanup_index(struct dentry *dentry)
|
|
|
|
{
|
2020-10-13 22:59:53 +08:00
|
|
|
struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
|
2017-10-24 22:38:33 +08:00
|
|
|
struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
|
|
|
|
struct inode *dir = indexdir->d_inode;
|
2017-06-21 18:46:12 +08:00
|
|
|
struct dentry *lowerdentry = ovl_dentry_lower(dentry);
|
|
|
|
struct dentry *upperdentry = ovl_dentry_upper(dentry);
|
|
|
|
struct dentry *index = NULL;
|
|
|
|
struct inode *inode;
|
2018-09-18 21:34:31 +08:00
|
|
|
struct qstr name = { };
|
2017-06-21 18:46:12 +08:00
|
|
|
int err;
|
|
|
|
|
2020-10-13 22:59:53 +08:00
|
|
|
err = ovl_get_index_name(ofs, lowerdentry, &name);
|
2017-06-21 18:46:12 +08:00
|
|
|
if (err)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
inode = d_inode(upperdentry);
|
2017-09-26 12:40:37 +08:00
|
|
|
if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
|
2019-12-16 19:12:32 +08:00
|
|
|
pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
|
2017-06-21 18:46:12 +08:00
|
|
|
upperdentry, inode->i_ino, inode->i_nlink);
|
|
|
|
/*
|
|
|
|
* We either have a bug with persistent union nlink or a lower
|
|
|
|
* hardlink was added while overlay is mounted. Adding a lower
|
|
|
|
* hardlink and then unlinking all overlay hardlinks would drop
|
|
|
|
* overlay nlink to zero before all upper inodes are unlinked.
|
|
|
|
* As a safety measure, when that situation is detected, set
|
|
|
|
* the overlay nlink to the index inode nlink minus one for the
|
|
|
|
* index entry itself.
|
|
|
|
*/
|
|
|
|
set_nlink(d_inode(dentry), inode->i_nlink - 1);
|
|
|
|
ovl_set_nlink_upper(dentry);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
inode_lock_nested(dir, I_MUTEX_PARENT);
|
2022-04-04 18:51:49 +08:00
|
|
|
index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
|
2017-06-21 18:46:12 +08:00
|
|
|
err = PTR_ERR(index);
|
2017-10-24 22:38:33 +08:00
|
|
|
if (IS_ERR(index)) {
|
2017-09-24 22:36:26 +08:00
|
|
|
index = NULL;
|
2017-10-24 22:38:33 +08:00
|
|
|
} else if (ovl_index_all(dentry->d_sb)) {
|
|
|
|
/* Whiteout orphan index to block future open by handle */
|
2020-04-24 10:55:17 +08:00
|
|
|
err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
|
|
|
|
dir, index);
|
2017-10-24 22:38:33 +08:00
|
|
|
} else {
|
|
|
|
/* Cleanup orphan index entries */
|
2022-04-04 18:51:43 +08:00
|
|
|
err = ovl_cleanup(ofs, dir, index);
|
2017-10-24 22:38:33 +08:00
|
|
|
}
|
2017-09-24 22:36:26 +08:00
|
|
|
|
2017-06-21 18:46:12 +08:00
|
|
|
inode_unlock(dir);
|
|
|
|
if (err)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
out:
|
2018-09-18 21:34:31 +08:00
|
|
|
kfree(name.name);
|
2017-06-21 18:46:12 +08:00
|
|
|
dput(index);
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
2019-12-16 19:12:32 +08:00
|
|
|
pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
|
2017-06-21 18:46:12 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
/*
|
|
|
|
* Operations that change overlay inode and upper inode nlink need to be
|
|
|
|
* synchronized with copy up for persistent nlink accounting.
|
|
|
|
*/
|
2018-10-18 23:37:13 +08:00
|
|
|
int ovl_nlink_start(struct dentry *dentry)
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
{
|
2018-10-18 23:37:14 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
const struct cred *old_cred;
|
|
|
|
int err;
|
|
|
|
|
2018-10-18 23:37:14 +08:00
|
|
|
if (WARN_ON(!inode))
|
2018-10-18 23:37:13 +08:00
|
|
|
return -ENOENT;
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* With inodes index is enabled, we store the union overlay nlink
|
2017-09-26 12:55:26 +08:00
|
|
|
* in an xattr on the index inode. When whiting out an indexed lower,
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
* we need to decrement the overlay persistent nlink, but before the
|
|
|
|
* first copy up, we have no upper index inode to store the xattr.
|
|
|
|
*
|
2017-09-26 12:55:26 +08:00
|
|
|
* As a workaround, before whiteout/rename over an indexed lower,
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
* copy up to create the upper index. Creating the upper index will
|
|
|
|
* initialize the overlay nlink, so it could be dropped if unlink
|
|
|
|
* or rename succeeds.
|
|
|
|
*
|
|
|
|
* TODO: implement metadata only index copy up when called with
|
|
|
|
* ovl_copy_up_flags(dentry, O_PATH).
|
|
|
|
*/
|
2017-09-26 12:55:26 +08:00
|
|
|
if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
err = ovl_copy_up(dentry);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-03-02 21:03:35 +08:00
|
|
|
err = ovl_inode_lock_interruptible(inode);
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2018-10-18 23:37:14 +08:00
|
|
|
if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
old_cred = ovl_override_creds(dentry->d_sb);
|
|
|
|
/*
|
|
|
|
* The overlay inode nlink should be incremented/decremented IFF the
|
|
|
|
* upper operation succeeds, along with nlink change of upper inode.
|
|
|
|
* Therefore, before link/unlink/rename, we store the union nlink
|
|
|
|
* value relative to the upper inode nlink in an upper inode xattr.
|
|
|
|
*/
|
|
|
|
err = ovl_set_nlink_upper(dentry);
|
|
|
|
revert_creds(old_cred);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (err)
|
2018-10-18 23:37:14 +08:00
|
|
|
ovl_inode_unlock(inode);
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-10-18 23:37:13 +08:00
|
|
|
void ovl_nlink_end(struct dentry *dentry)
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
{
|
2018-10-18 23:37:14 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
|
|
|
|
|
|
|
if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
|
2018-10-18 23:37:13 +08:00
|
|
|
const struct cred *old_cred;
|
2017-06-21 18:46:12 +08:00
|
|
|
|
2018-10-18 23:37:13 +08:00
|
|
|
old_cred = ovl_override_creds(dentry->d_sb);
|
|
|
|
ovl_cleanup_index(dentry);
|
|
|
|
revert_creds(old_cred);
|
2017-06-21 18:46:12 +08:00
|
|
|
}
|
2018-10-18 23:37:13 +08:00
|
|
|
|
2018-10-18 23:37:14 +08:00
|
|
|
ovl_inode_unlock(inode);
|
ovl: persistent overlay inode nlink for indexed inodes
With inodes index enabled, an overlay inode nlink counts the union of upper
and non-covered lower hardlinks. During the lifetime of a non-pure upper
inode, the following nlink modifying operations can happen:
1. Lower hardlink copy up
2. Upper hardlink created, unlinked or renamed over
3. Lower hardlink whiteout or renamed over
For the first, copy up case, the union nlink does not change, whether the
operation succeeds or fails, but the upper inode nlink may change.
Therefore, before copy up, we store the union nlink value relative to the
lower inode nlink in the index inode xattr trusted.overlay.nlink.
For the second, upper hardlink case, the union nlink should be incremented
or decremented IFF the operation succeeds, aligned with nlink change of the
upper inode. Therefore, before link/unlink/rename, we store the union nlink
value relative to the upper inode nlink in the index inode.
For the last, lower cover up case, we simplify things by preceding the
whiteout or cover up with copy up. This makes sure that there is an index
upper inode where the nlink xattr can be stored before the copied up upper
entry is unlink.
Return the overlay inode nlinks for indexed upper inodes on stat(2).
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2017-06-20 20:35:14 +08:00
|
|
|
}
|
2017-09-25 21:39:55 +08:00
|
|
|
|
|
|
|
int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
|
|
|
|
{
|
|
|
|
/* Workdir should not be the same as upperdir */
|
|
|
|
if (workdir == upperdir)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Workdir should not be subdir of upperdir and vice versa */
|
|
|
|
if (lock_rename(workdir, upperdir) != NULL)
|
|
|
|
goto err_unlock;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_unlock:
|
|
|
|
unlock_rename(workdir, upperdir);
|
|
|
|
err:
|
2019-12-16 19:12:32 +08:00
|
|
|
pr_err("failed to lock workdir+upperdir\n");
|
2017-09-25 21:39:55 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
2018-05-11 23:49:28 +08:00
|
|
|
|
|
|
|
/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
|
2022-08-05 01:11:15 +08:00
|
|
|
int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path)
|
2018-05-11 23:49:28 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
/* Only regular files can have metacopy xattr */
|
2022-04-04 18:51:50 +08:00
|
|
|
if (!S_ISREG(d_inode(path->dentry)->i_mode))
|
2018-05-11 23:49:28 +08:00
|
|
|
return 0;
|
|
|
|
|
2022-04-04 18:51:50 +08:00
|
|
|
res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY, NULL, 0);
|
2018-05-11 23:49:28 +08:00
|
|
|
if (res < 0) {
|
|
|
|
if (res == -ENODATA || res == -EOPNOTSUPP)
|
|
|
|
return 0;
|
2020-12-14 22:26:14 +08:00
|
|
|
/*
|
|
|
|
* getxattr on user.* may fail with EACCES in case there's no
|
|
|
|
* read permission on the inode. Not much we can do, other than
|
|
|
|
* tell the caller that this is not a metacopy inode.
|
|
|
|
*/
|
|
|
|
if (ofs->config.userxattr && res == -EACCES)
|
|
|
|
return 0;
|
2018-05-11 23:49:28 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
out:
|
2019-12-16 19:12:32 +08:00
|
|
|
pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
|
2018-05-11 23:49:28 +08:00
|
|
|
return res;
|
|
|
|
}
|
2018-05-11 23:49:30 +08:00
|
|
|
|
|
|
|
bool ovl_is_metacopy_dentry(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
struct ovl_entry *oe = dentry->d_fsdata;
|
|
|
|
|
|
|
|
if (!d_is_reg(dentry))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ovl_dentry_upper(dentry)) {
|
|
|
|
if (!ovl_has_upperdata(d_inode(dentry)))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (oe->numlower > 1);
|
|
|
|
}
|
2018-05-11 23:49:32 +08:00
|
|
|
|
2022-08-05 01:11:15 +08:00
|
|
|
char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
|
2019-01-31 03:01:57 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *s, *next, *buf = NULL;
|
|
|
|
|
2022-04-04 18:51:50 +08:00
|
|
|
res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
|
2020-09-02 16:58:48 +08:00
|
|
|
if (res == -ENODATA || res == -EOPNOTSUPP)
|
2019-01-31 03:01:57 +08:00
|
|
|
return NULL;
|
2018-05-11 23:49:32 +08:00
|
|
|
if (res < 0)
|
2020-09-02 16:58:48 +08:00
|
|
|
goto fail;
|
|
|
|
if (res == 0)
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
buf = kzalloc(res + padding + 1, GFP_KERNEL);
|
|
|
|
if (!buf)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2022-04-04 18:51:50 +08:00
|
|
|
res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
|
2020-09-02 16:58:48 +08:00
|
|
|
if (res < 0)
|
|
|
|
goto fail;
|
2018-05-11 23:49:32 +08:00
|
|
|
if (res == 0)
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
if (buf[0] == '/') {
|
|
|
|
for (s = buf; *s++ == '/'; s = next) {
|
|
|
|
next = strchrnul(s, '/');
|
|
|
|
if (s == next)
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strchr(buf, '/') != NULL)
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
invalid:
|
2019-12-16 19:12:32 +08:00
|
|
|
pr_warn_ratelimited("invalid redirect (%s)\n", buf);
|
2018-05-11 23:49:32 +08:00
|
|
|
res = -EINVAL;
|
2020-09-02 16:58:48 +08:00
|
|
|
goto err_free;
|
|
|
|
fail:
|
|
|
|
pr_warn_ratelimited("failed to get redirect (%i)\n", res);
|
|
|
|
err_free:
|
2019-01-31 03:01:57 +08:00
|
|
|
kfree(buf);
|
|
|
|
return ERR_PTR(res);
|
2018-05-11 23:49:32 +08:00
|
|
|
}
|
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-08 08:10:43 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ovl_sync_status() - Check fs sync status for volatile mounts
|
|
|
|
*
|
|
|
|
* Returns 1 if this is not a volatile mount and a real sync is required.
|
|
|
|
*
|
|
|
|
* Returns 0 if syncing can be skipped because mount is volatile, and no errors
|
|
|
|
* have occurred on the upperdir since the mount.
|
|
|
|
*
|
|
|
|
* Returns -errno if it is a volatile mount, and the error that occurred since
|
|
|
|
* the last mount. If the error code changes, it'll return the latest error
|
|
|
|
* code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int ovl_sync_status(struct ovl_fs *ofs)
|
|
|
|
{
|
|
|
|
struct vfsmount *mnt;
|
|
|
|
|
|
|
|
if (ovl_should_sync(ofs))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
mnt = ovl_upper_mnt(ofs);
|
|
|
|
if (!mnt)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
|
|
|
|
}
|
2022-04-04 18:51:54 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ovl_copyattr() - copy inode attributes from layer to ovl inode
|
|
|
|
*
|
|
|
|
* When overlay copies inode information from an upper or lower layer to the
|
|
|
|
* relevant overlay inode it will apply the idmapping of the upper or lower
|
|
|
|
* layer when doing so ensuring that the ovl inode ownership will correctly
|
|
|
|
* reflect the ownership of the idmapped upper or lower layer. For example, an
|
|
|
|
* idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
|
|
|
|
* map any lower or upper inode owned by id 1001 to id 1000. These mapping
|
|
|
|
* helpers are nops when the relevant layer isn't idmapped.
|
|
|
|
*/
|
|
|
|
void ovl_copyattr(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct path realpath;
|
|
|
|
struct inode *realinode;
|
|
|
|
struct user_namespace *real_mnt_userns;
|
|
|
|
|
|
|
|
ovl_i_path_real(inode, &realpath);
|
|
|
|
realinode = d_inode(realpath.dentry);
|
|
|
|
real_mnt_userns = mnt_user_ns(realpath.mnt);
|
|
|
|
|
|
|
|
inode->i_uid = i_uid_into_mnt(real_mnt_userns, realinode);
|
|
|
|
inode->i_gid = i_gid_into_mnt(real_mnt_userns, realinode);
|
|
|
|
inode->i_mode = realinode->i_mode;
|
|
|
|
inode->i_atime = realinode->i_atime;
|
|
|
|
inode->i_mtime = realinode->i_mtime;
|
|
|
|
inode->i_ctime = realinode->i_ctime;
|
|
|
|
i_size_write(inode, i_size_read(realinode));
|
|
|
|
}
|