1a59d1b8e05ea (Thomas Gleixner 2019-05-27 08:55:05 +0200 1) // SPDX-License-Identifier: GPL-2.0-or-later
64cbb654ed779 (Lee Jones 2021-03-30 17:44:48 +0100 2) /*
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 3) * eCryptfs: Linux filesystem encryption layer
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 4) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 5) * Copyright (C) 1997-2003 Erez Zadok
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 6) * Copyright (C) 2001-2003 Stony Brook University
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 7) * Copyright (C) 2004-2006 International Business Machines Corp.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 8) * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 9) */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 10)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 11) #include <linux/dcache.h>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 12) #include <linux/namei.h>
45ec4ababe999 (Michael Halcrow 2006-10-30 22:07:20 -0800 13) #include <linux/mount.h>
0cc72dc7f0501 (Josef "Jeff" Sipek 2006-12-08 02:36:31 -0800 14) #include <linux/fs_stack.h>
5a0e3ad6af866 (Tejun Heo 2010-03-24 17:04:11 +0900 15) #include <linux/slab.h>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 16) #include "ecryptfs_kernel.h"
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 17)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 18) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 19) * ecryptfs_d_revalidate - revalidate an ecryptfs dentry
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 20) * @dentry: The ecryptfs dentry
0b728e1911cbe (Al Viro 2012-06-10 16:03:43 -0400 21) * @flags: lookup flags
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 22) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 23) * Called when the VFS needs to revalidate a dentry. This
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 24) * is called whenever a name lookup finds a dentry in the
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 25) * dcache. Most filesystems leave this as NULL, because all their
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 26) * dentries in the dcache are valid.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 27) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 28) * Returns 1 if valid, 0 otherwise.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 29) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 30) */
0b728e1911cbe (Al Viro 2012-06-10 16:03:43 -0400 31) static int ecryptfs_d_revalidate(struct dentry *dentry, unsigned int flags)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 32) {
2edbfbf1c1ab0 (Al Viro 2013-09-15 20:45:11 -0400 33) struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
5556e7e6d30e8 (Tyler Hicks 2015-08-05 11:26:36 -0500 34) int rc = 1;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 35)
0b728e1911cbe (Al Viro 2012-06-10 16:03:43 -0400 36) if (flags & LOOKUP_RCU)
34286d6662308 (Nicholas Piggin 2011-01-07 17:49:57 +1100 37) return -ECHILD;
34286d6662308 (Nicholas Piggin 2011-01-07 17:49:57 +1100 38)
5556e7e6d30e8 (Tyler Hicks 2015-08-05 11:26:36 -0500 39) if (lower_dentry->d_flags & DCACHE_OP_REVALIDATE)
5556e7e6d30e8 (Tyler Hicks 2015-08-05 11:26:36 -0500 40) rc = lower_dentry->d_op->d_revalidate(lower_dentry, flags);
5556e7e6d30e8 (Tyler Hicks 2015-08-05 11:26:36 -0500 41)
2b0143b5c986b (David Howells 2015-03-17 22:25:59 +0000 42) if (d_really_is_positive(dentry)) {
5556e7e6d30e8 (Tyler Hicks 2015-08-05 11:26:36 -0500 43) struct inode *inode = d_inode(dentry);
ae56fb16337c8 (Michael Halcrow 2006-11-16 01:19:30 -0800 44)
5556e7e6d30e8 (Tyler Hicks 2015-08-05 11:26:36 -0500 45) fsstack_copy_attr_all(inode, ecryptfs_inode_to_lower(inode));
5556e7e6d30e8 (Tyler Hicks 2015-08-05 11:26:36 -0500 46) if (!inode->i_nlink)
5556e7e6d30e8 (Tyler Hicks 2015-08-05 11:26:36 -0500 47) return 0;
ae56fb16337c8 (Michael Halcrow 2006-11-16 01:19:30 -0800 48) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 49) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 50) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 51)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 52) struct kmem_cache *ecryptfs_dentry_info_cache;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 53)
2edbfbf1c1ab0 (Al Viro 2013-09-15 20:45:11 -0400 54) static void ecryptfs_dentry_free_rcu(struct rcu_head *head)
2edbfbf1c1ab0 (Al Viro 2013-09-15 20:45:11 -0400 55) {
2edbfbf1c1ab0 (Al Viro 2013-09-15 20:45:11 -0400 56) kmem_cache_free(ecryptfs_dentry_info_cache,
2edbfbf1c1ab0 (Al Viro 2013-09-15 20:45:11 -0400 57) container_of(head, struct ecryptfs_dentry_info, rcu));
2edbfbf1c1ab0 (Al Viro 2013-09-15 20:45:11 -0400 58) }
2edbfbf1c1ab0 (Al Viro 2013-09-15 20:45:11 -0400 59)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 60) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 61) * ecryptfs_d_release
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 62) * @dentry: The ecryptfs dentry
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 63) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 64) * Called when a dentry is really deallocated.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 65) */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 66) static void ecryptfs_d_release(struct dentry *dentry)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 67) {
2edbfbf1c1ab0 (Al Viro 2013-09-15 20:45:11 -0400 68) struct ecryptfs_dentry_info *p = dentry->d_fsdata;
2edbfbf1c1ab0 (Al Viro 2013-09-15 20:45:11 -0400 69) if (p) {
cbe9c08524a68 (Al Viro 2013-09-15 20:54:18 -0400 70) path_put(&p->lower_path);
2edbfbf1c1ab0 (Al Viro 2013-09-15 20:45:11 -0400 71) call_rcu(&p->rcu, ecryptfs_dentry_free_rcu);
45ec4ababe999 (Michael Halcrow 2006-10-30 22:07:20 -0800 72) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 73) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 74)
5a3fd05a9bb2f (Al Viro 2009-02-20 05:57:52 +0000 75) const struct dentry_operations ecryptfs_dops = {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 76) .d_revalidate = ecryptfs_d_revalidate,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 77) .d_release = ecryptfs_d_release,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 78) };