xref: /openbmc/linux/fs/ecryptfs/dentry.c (revision b593bce5)
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  * @dentry: The ecryptfs dentry
21  * @flags: lookup flags
22  *
23  * Called when the VFS needs to revalidate a dentry. This
24  * is called whenever a name lookup finds a dentry in the
25  * dcache. Most filesystems leave this as NULL, because all their
26  * dentries in the dcache are valid.
27  *
28  * Returns 1 if valid, 0 otherwise.
29  *
30  */
31 static int ecryptfs_d_revalidate(struct dentry *dentry, unsigned int flags)
32 {
33 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
34 	int rc = 1;
35 
36 	if (flags & LOOKUP_RCU)
37 		return -ECHILD;
38 
39 	if (lower_dentry->d_flags & DCACHE_OP_REVALIDATE)
40 		rc = lower_dentry->d_op->d_revalidate(lower_dentry, flags);
41 
42 	if (d_really_is_positive(dentry)) {
43 		struct inode *inode = d_inode(dentry);
44 
45 		fsstack_copy_attr_all(inode, ecryptfs_inode_to_lower(inode));
46 		if (!inode->i_nlink)
47 			return 0;
48 	}
49 	return rc;
50 }
51 
52 struct kmem_cache *ecryptfs_dentry_info_cache;
53 
54 static void ecryptfs_dentry_free_rcu(struct rcu_head *head)
55 {
56 	kmem_cache_free(ecryptfs_dentry_info_cache,
57 		container_of(head, struct ecryptfs_dentry_info, rcu));
58 }
59 
60 /**
61  * ecryptfs_d_release
62  * @dentry: The ecryptfs dentry
63  *
64  * Called when a dentry is really deallocated.
65  */
66 static void ecryptfs_d_release(struct dentry *dentry)
67 {
68 	struct ecryptfs_dentry_info *p = dentry->d_fsdata;
69 	if (p) {
70 		path_put(&p->lower_path);
71 		call_rcu(&p->rcu, ecryptfs_dentry_free_rcu);
72 	}
73 }
74 
75 const struct dentry_operations ecryptfs_dops = {
76 	.d_revalidate = ecryptfs_d_revalidate,
77 	.d_release = ecryptfs_d_release,
78 };
79