1 /**
2  * eCryptfs: Linux filesystem encryption layer
3  *
4  * Copyright (C) 1997-2003 Erez Zadok
5  * Copyright (C) 2001-2003 Stony Brook University
6  * Copyright (C) 2004-2006 International Business Machines Corp.
7  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  * 02111-1307, USA.
23  */
24 
25 #include <linux/dcache.h>
26 #include <linux/namei.h>
27 #include <linux/mount.h>
28 #include <linux/fs_stack.h>
29 #include <linux/slab.h>
30 #include "ecryptfs_kernel.h"
31 
32 /**
33  * ecryptfs_d_revalidate - revalidate an ecryptfs dentry
34  * @dentry: The ecryptfs dentry
35  * @nd: The associated nameidata
36  *
37  * Called when the VFS needs to revalidate a dentry. This
38  * is called whenever a name lookup finds a dentry in the
39  * dcache. Most filesystems leave this as NULL, because all their
40  * dentries in the dcache are valid.
41  *
42  * Returns 1 if valid, 0 otherwise.
43  *
44  */
ecryptfs_d_revalidate(struct dentry * dentry,struct nameidata * nd)45 static int ecryptfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
46 {
47 	struct dentry *lower_dentry;
48 	struct vfsmount *lower_mnt;
49 	struct dentry *dentry_save = NULL;
50 	struct vfsmount *vfsmount_save = NULL;
51 	int rc = 1;
52 
53 	if (nd && nd->flags & LOOKUP_RCU)
54 		return -ECHILD;
55 
56 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
57 	lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
58 	if (!lower_dentry->d_op || !lower_dentry->d_op->d_revalidate)
59 		goto out;
60 	if (nd) {
61 		dentry_save = nd->path.dentry;
62 		vfsmount_save = nd->path.mnt;
63 		nd->path.dentry = lower_dentry;
64 		nd->path.mnt = lower_mnt;
65 	}
66 	rc = lower_dentry->d_op->d_revalidate(lower_dentry, nd);
67 	if (nd) {
68 		nd->path.dentry = dentry_save;
69 		nd->path.mnt = vfsmount_save;
70 	}
71 	if (dentry->d_inode) {
72 		struct inode *lower_inode =
73 			ecryptfs_inode_to_lower(dentry->d_inode);
74 
75 		fsstack_copy_attr_all(dentry->d_inode, lower_inode);
76 	}
77 out:
78 	return rc;
79 }
80 
81 struct kmem_cache *ecryptfs_dentry_info_cache;
82 
83 /**
84  * ecryptfs_d_release
85  * @dentry: The ecryptfs dentry
86  *
87  * Called when a dentry is really deallocated.
88  */
ecryptfs_d_release(struct dentry * dentry)89 static void ecryptfs_d_release(struct dentry *dentry)
90 {
91 	if (ecryptfs_dentry_to_private(dentry)) {
92 		if (ecryptfs_dentry_to_lower(dentry)) {
93 			dput(ecryptfs_dentry_to_lower(dentry));
94 			mntput(ecryptfs_dentry_to_lower_mnt(dentry));
95 		}
96 		kmem_cache_free(ecryptfs_dentry_info_cache,
97 				ecryptfs_dentry_to_private(dentry));
98 	}
99 	return;
100 }
101 
102 const struct dentry_operations ecryptfs_dops = {
103 	.d_revalidate = ecryptfs_d_revalidate,
104 	.d_release = ecryptfs_d_release,
105 };
106