xref: /linux/fs/ecryptfs/dentry.c (revision 84a73014d86fd660822a20c032625e3afe99ca58)
1237fead6SMichael Halcrow /**
2237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3237fead6SMichael Halcrow  *
4237fead6SMichael Halcrow  * Copyright (C) 1997-2003 Erez Zadok
5237fead6SMichael Halcrow  * Copyright (C) 2001-2003 Stony Brook University
6237fead6SMichael Halcrow  * Copyright (C) 2004-2006 International Business Machines Corp.
7237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8237fead6SMichael Halcrow  *
9237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
10237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
11237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
12237fead6SMichael Halcrow  * License, or (at your option) any later version.
13237fead6SMichael Halcrow  *
14237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
15237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
16237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17237fead6SMichael Halcrow  * General Public License for more details.
18237fead6SMichael Halcrow  *
19237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
20237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
21237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22237fead6SMichael Halcrow  * 02111-1307, USA.
23237fead6SMichael Halcrow  */
24237fead6SMichael Halcrow 
25237fead6SMichael Halcrow #include <linux/dcache.h>
26237fead6SMichael Halcrow #include <linux/namei.h>
2745ec4abaSMichael Halcrow #include <linux/mount.h>
280cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
30237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
31237fead6SMichael Halcrow 
32237fead6SMichael Halcrow /**
33237fead6SMichael Halcrow  * ecryptfs_d_revalidate - revalidate an ecryptfs dentry
34237fead6SMichael Halcrow  * @dentry: The ecryptfs dentry
350b728e19SAl Viro  * @flags: lookup flags
36237fead6SMichael Halcrow  *
37237fead6SMichael Halcrow  * Called when the VFS needs to revalidate a dentry. This
38237fead6SMichael Halcrow  * is called whenever a name lookup finds a dentry in the
39237fead6SMichael Halcrow  * dcache. Most filesystems leave this as NULL, because all their
40237fead6SMichael Halcrow  * dentries in the dcache are valid.
41237fead6SMichael Halcrow  *
42237fead6SMichael Halcrow  * Returns 1 if valid, 0 otherwise.
43237fead6SMichael Halcrow  *
44237fead6SMichael Halcrow  */
450b728e19SAl Viro static int ecryptfs_d_revalidate(struct dentry *dentry, unsigned int flags)
46237fead6SMichael Halcrow {
472edbfbf1SAl Viro 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
485556e7e6STyler Hicks 	int rc = 1;
49237fead6SMichael Halcrow 
500b728e19SAl Viro 	if (flags & LOOKUP_RCU)
5134286d66SNick Piggin 		return -ECHILD;
5234286d66SNick Piggin 
535556e7e6STyler Hicks 	if (lower_dentry->d_flags & DCACHE_OP_REVALIDATE)
540b728e19SAl Viro 		rc = lower_dentry->d_op->d_revalidate(lower_dentry, flags);
55ae56fb16SMichael Halcrow 
565556e7e6STyler Hicks 	if (d_really_is_positive(dentry)) {
575556e7e6STyler Hicks 		struct inode *inode = d_inode(dentry);
585556e7e6STyler Hicks 
595556e7e6STyler Hicks 		fsstack_copy_attr_all(inode, ecryptfs_inode_to_lower(inode));
605556e7e6STyler Hicks 		if (!inode->i_nlink)
615556e7e6STyler Hicks 			return 0;
62ae56fb16SMichael Halcrow 	}
63237fead6SMichael Halcrow 	return rc;
64237fead6SMichael Halcrow }
65237fead6SMichael Halcrow 
66237fead6SMichael Halcrow struct kmem_cache *ecryptfs_dentry_info_cache;
67237fead6SMichael Halcrow 
682edbfbf1SAl Viro static void ecryptfs_dentry_free_rcu(struct rcu_head *head)
692edbfbf1SAl Viro {
702edbfbf1SAl Viro 	kmem_cache_free(ecryptfs_dentry_info_cache,
712edbfbf1SAl Viro 		container_of(head, struct ecryptfs_dentry_info, rcu));
722edbfbf1SAl Viro }
732edbfbf1SAl Viro 
74237fead6SMichael Halcrow /**
75237fead6SMichael Halcrow  * ecryptfs_d_release
76237fead6SMichael Halcrow  * @dentry: The ecryptfs dentry
77237fead6SMichael Halcrow  *
78237fead6SMichael Halcrow  * Called when a dentry is really deallocated.
79237fead6SMichael Halcrow  */
80237fead6SMichael Halcrow static void ecryptfs_d_release(struct dentry *dentry)
81237fead6SMichael Halcrow {
822edbfbf1SAl Viro 	struct ecryptfs_dentry_info *p = dentry->d_fsdata;
832edbfbf1SAl Viro 	if (p) {
842edbfbf1SAl Viro 		path_put(&p->lower_path);
852edbfbf1SAl Viro 		call_rcu(&p->rcu, ecryptfs_dentry_free_rcu);
86b228b8e5SMichael Halcrow 	}
87237fead6SMichael Halcrow }
88237fead6SMichael Halcrow 
895a3fd05aSAl Viro const struct dentry_operations ecryptfs_dops = {
90237fead6SMichael Halcrow 	.d_revalidate = ecryptfs_d_revalidate,
91237fead6SMichael Halcrow 	.d_release = ecryptfs_d_release,
92237fead6SMichael Halcrow };
93