^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * eCryptfs: Linux filesystem encryption layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1997-2003 Erez Zadok
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2001-2003 Stony Brook University
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2004-2006 International Business Machines Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/dcache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/fs_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "ecryptfs_kernel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * ecryptfs_d_revalidate - revalidate an ecryptfs dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @dentry: The ecryptfs dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @flags: lookup flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Called when the VFS needs to revalidate a dentry. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * is called whenever a name lookup finds a dentry in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * dcache. Most filesystems leave this as NULL, because all their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * dentries in the dcache are valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * Returns 1 if valid, 0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static int ecryptfs_d_revalidate(struct dentry *dentry, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int rc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (flags & LOOKUP_RCU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return -ECHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (lower_dentry->d_flags & DCACHE_OP_REVALIDATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) rc = lower_dentry->d_op->d_revalidate(lower_dentry, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (d_really_is_positive(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) fsstack_copy_attr_all(inode, ecryptfs_inode_to_lower(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (!inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct kmem_cache *ecryptfs_dentry_info_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static void ecryptfs_dentry_free_rcu(struct rcu_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) kmem_cache_free(ecryptfs_dentry_info_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) container_of(head, struct ecryptfs_dentry_info, rcu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * ecryptfs_d_release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * @dentry: The ecryptfs dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * Called when a dentry is really deallocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static void ecryptfs_d_release(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct ecryptfs_dentry_info *p = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) path_put(&p->lower_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) call_rcu(&p->rcu, ecryptfs_dentry_free_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) const struct dentry_operations ecryptfs_dops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .d_revalidate = ecryptfs_d_revalidate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .d_release = ecryptfs_d_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };