// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2011 Novell Inc.
* Copyright (C) 2016 Red Hat, Inc.
*/
#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/slab.h>
#include <linux/cred.h>
#include <linux/xattr.h>
#include <linux/exportfs.h>
#include <linux/file.h>
#include <linux/fileattr.h>
#include <linux/uuid.h>
#include <linux/namei.h>
#include <linux/ratelimit.h>
#include "overlayfs.h"
/* Get write access to upper mnt - may fail if upper sb was remounted ro */
int ovl_get_write_access(struct dentry *dentry)
{
struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
return mnt_get_write_access(ovl_upper_mnt(ofs));
}
/* Get write access to upper sb - may block if upper sb is frozen */
void ovl_start_write(struct dentry *dentry)
{
struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
sb_start_write(ovl_upper_mnt(ofs)->mnt_sb);
}
int ovl_want_write(struct dentry *dentry)
{
struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
return mnt_want_write(ovl_upper_mnt(ofs));
}
void ovl_put_write_access(struct dentry *dentry)
{
struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
mnt_put_write_access(ovl_upper_mnt(ofs));
}
void ovl_end_write(struct dentry *dentry)
{
struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
sb_end_write(ovl_upper_mnt(ofs)->mnt_sb);
}
void ovl_drop_write(struct dentry *dentry)
{
struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
mnt_drop_write(ovl_upper_mnt(ofs));
}
struct dentry *ovl_workdir(struct dentry *dentry)
{
struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
return ofs->workdir;
}
const struct cred *ovl_override_creds(struct super_block *sb)
{
struct ovl_fs *ofs = OVL_FS(sb);
return override_creds(ofs->creator_cred);
}
/*
* 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)
{
if (!capable(CAP_DAC_READ_SEARCH))
return 0;
if (!exportfs_can_decode_fh(sb->s_export_op))
return 0;
return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
}
struct dentry *ovl_indexdir(struct super_block *sb)
{
struct ovl_fs *ofs = OVL_FS(sb);
return ofs->config.index ? ofs->workdir : NULL;
}
/* 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 = OVL_FS(sb);
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 = OVL_FS(sb);
return ofs->config.nfs_export && ofs->config.index;
}
struct ovl_path *ovl_stack_alloc(unsigned int n)
{
return kcalloc(n, sizeof(struct ovl_path), GFP_KERNEL);
}
void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n)
{
unsigned int i;
memcpy(dst, src, sizeof(struct ovl_path) * n);
for (i = 0; i < n; i++)
dget(src[i].dentry);
}
void ovl_stack_put(struct ovl_path *stack, unsigned int n)
{
unsigned int i;
for (i = 0; stack && i < n; i++)
dput(stack[i].dentry);
}
void ovl_stack_free(struct ovl_path *stack, unsigned int n)
{
ovl_stack_put(stack, n);
kfree(stack);
}
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;
}
void ovl_free_entry(struct ovl_entry *oe)
{
ovl_stack_put(ovl_lowerstack(oe), ovl_numlower(oe));
kfree(oe);
}
#define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
bool ovl_dentry_remote(struct dentry *dentry)
{
return dentry->d_flags & OVL_D_REVALIDATE;
}
void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
{
if (!ovl_dentry_remote(realdentry))
return;
spin_lock(&dentry->d_lock);
dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
spin_unlock(&dentry->d_lock);
}
void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
struct ovl_entry *oe)
{
return ovl_dentry_init_flags(dentry, upperdentry, oe, OVL_D_REVALIDATE);
}
void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
struct ovl_entry *oe, unsigned int mask)
{
struct ovl_path *lowerstack = ovl_lowerstack(oe);
unsigned int i, flags = 0;
if (upperdentry)
flags |= upperdentry->d_flags;
for (i = 0; i < ovl_numlower(oe) && lowerstack[i].dentry; i++)
flags |= lowerstack[i].dentry->d_flags;
spin_lock(&dentry->d_lock);
dentry->d_flags &= ~mask;
dentry->d_flags |= flags & mask;
spin_unlock(&dentry->d_lock);
}
bool ovl_dentry_weird(struct dentry *dentry)
{
if (!d_can_lookup(dentry) && !d_is_file(dentry) && !d_is_symlink(dentry))
return true;
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 = OVL_E(dentry);
enum ovl_path_type type = 0;
if (ovl_dentry_upper(dentry)) {
type = __OVL_PATH_UPPER;
/*
* Non-dir dentry can hold lower dentry of its copy up origin.
*/
if (ovl_numlower(oe)) {
if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
type |= __OVL_PATH_ORIGIN;
if (d_is_dir(dentry) ||
!ovl_has_upperdata(d_inode(dentry)))
type |= __OVL_PATH_MERGE;
}
} else {
if (ovl_numlower(oe) > 1)
type |= __OVL_PATH_MERGE;
}
return type;
}
void ovl_path_upper(struct dentry *dentry, struct path *path)
{
struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
path->mnt = ovl_upper_mnt(ofs);
path->dentry = ovl_dentry_upper(dentry);
}
void ovl_path_lower(struct dentry *dentry, struct path *path)
{
struct ovl_entry *oe = OVL_E(dentry);
struct ovl_path *lowerpath = ovl_lowerstack(oe);
if (ovl_numlower(oe)) {
path->mnt = lowerpath->layer->mnt;
path->dentry = lowerpath->dentry;
} else {
*path = (struct path) { };
}
}
void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
{
struct ovl_entry *oe = OVL_E(dentry);
struct ovl_path *lowerdata = ovl_lowerdata(oe);
struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe);
if (lowerdata_dentry) {
path->dentry = lowerdata_dentry;
/*
* Pairs with smp_wmb() in ovl_dentry_set_lowerdata().
* Make sure that if lowerdata->dentry is visible, then
* datapath->layer is visible as well.
*/
smp_rmb();
path->mnt = READ_ONCE(lowerdata->layer)->mnt;
} else {
*path = (struct path) { };
}
}
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;
}
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;
}
struct dentry *ovl_dentry_upper(struct dentry *dentry)
{
struct inode *inode = d_inode(dentry);
return inode ? ovl_upperdentry_dereference(OVL_I(inode)) : NULL;
}
struct dentry *ovl_dentry_lower(struct dentry *dentry)
{
struct ovl_e
|