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