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