xref: /openbmc/linux/fs/ecryptfs/super.c (revision 4981e081)
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  *              Michael C. Thompson <mcthomps@us.ibm.com>
9237fead6SMichael Halcrow  *
10237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
11237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
12237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
13237fead6SMichael Halcrow  * License, or (at your option) any later version.
14237fead6SMichael Halcrow  *
15237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
16237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
17237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18237fead6SMichael Halcrow  * General Public License for more details.
19237fead6SMichael Halcrow  *
20237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
21237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
22237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23237fead6SMichael Halcrow  * 02111-1307, USA.
24237fead6SMichael Halcrow  */
25237fead6SMichael Halcrow 
26237fead6SMichael Halcrow #include <linux/fs.h>
27237fead6SMichael Halcrow #include <linux/mount.h>
28237fead6SMichael Halcrow #include <linux/key.h>
29237fead6SMichael Halcrow #include <linux/seq_file.h>
304981e081SMichael Halcrow #include <linux/file.h>
31237fead6SMichael Halcrow #include <linux/crypto.h>
32237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
33237fead6SMichael Halcrow 
34237fead6SMichael Halcrow struct kmem_cache *ecryptfs_inode_info_cache;
35237fead6SMichael Halcrow 
36237fead6SMichael Halcrow /**
37237fead6SMichael Halcrow  * ecryptfs_alloc_inode - allocate an ecryptfs inode
38237fead6SMichael Halcrow  * @sb: Pointer to the ecryptfs super block
39237fead6SMichael Halcrow  *
40237fead6SMichael Halcrow  * Called to bring an inode into existence.
41237fead6SMichael Halcrow  *
42237fead6SMichael Halcrow  * Only handle allocation, setting up structures should be done in
43237fead6SMichael Halcrow  * ecryptfs_read_inode. This is because the kernel, between now and
44237fead6SMichael Halcrow  * then, will 0 out the private data pointer.
45237fead6SMichael Halcrow  *
46237fead6SMichael Halcrow  * Returns a pointer to a newly allocated inode, NULL otherwise
47237fead6SMichael Halcrow  */
48237fead6SMichael Halcrow static struct inode *ecryptfs_alloc_inode(struct super_block *sb)
49237fead6SMichael Halcrow {
50237fead6SMichael Halcrow 	struct ecryptfs_inode_info *ecryptfs_inode;
51237fead6SMichael Halcrow 	struct inode *inode = NULL;
52237fead6SMichael Halcrow 
53237fead6SMichael Halcrow 	ecryptfs_inode = kmem_cache_alloc(ecryptfs_inode_info_cache,
54e94b1766SChristoph Lameter 					  GFP_KERNEL);
55237fead6SMichael Halcrow 	if (unlikely(!ecryptfs_inode))
56237fead6SMichael Halcrow 		goto out;
57237fead6SMichael Halcrow 	ecryptfs_init_crypt_stat(&ecryptfs_inode->crypt_stat);
58237fead6SMichael Halcrow 	inode = &ecryptfs_inode->vfs_inode;
59237fead6SMichael Halcrow out:
60237fead6SMichael Halcrow 	return inode;
61237fead6SMichael Halcrow }
62237fead6SMichael Halcrow 
63237fead6SMichael Halcrow /**
64237fead6SMichael Halcrow  * ecryptfs_destroy_inode
65237fead6SMichael Halcrow  * @inode: The ecryptfs inode
66237fead6SMichael Halcrow  *
674981e081SMichael Halcrow  * This is used during the final destruction of the inode.  All
684981e081SMichael Halcrow  * allocation of memory related to the inode, including allocated
694981e081SMichael Halcrow  * memory in the crypt_stat struct, will be released here. This
704981e081SMichael Halcrow  * function also fput()'s the persistent file for the lower inode.
71237fead6SMichael Halcrow  * There should be no chance that this deallocation will be missed.
72237fead6SMichael Halcrow  */
73237fead6SMichael Halcrow static void ecryptfs_destroy_inode(struct inode *inode)
74237fead6SMichael Halcrow {
75237fead6SMichael Halcrow 	struct ecryptfs_inode_info *inode_info;
76237fead6SMichael Halcrow 
77237fead6SMichael Halcrow 	inode_info = ecryptfs_inode_to_private(inode);
784981e081SMichael Halcrow 	mutex_lock(&inode_info->lower_file_mutex);
794981e081SMichael Halcrow 	if (inode_info->lower_file) {
804981e081SMichael Halcrow 		struct dentry *lower_dentry =
814981e081SMichael Halcrow 			inode_info->lower_file->f_dentry;
824981e081SMichael Halcrow 
834981e081SMichael Halcrow 		BUG_ON(!lower_dentry);
844981e081SMichael Halcrow 		if (lower_dentry->d_inode) {
854981e081SMichael Halcrow 			fput(inode_info->lower_file);
864981e081SMichael Halcrow 			inode_info->lower_file = NULL;
874981e081SMichael Halcrow 			d_drop(lower_dentry);
884981e081SMichael Halcrow 			d_delete(lower_dentry);
894981e081SMichael Halcrow 		}
904981e081SMichael Halcrow 	}
914981e081SMichael Halcrow 	mutex_unlock(&inode_info->lower_file_mutex);
92fcd12835SMichael Halcrow 	ecryptfs_destroy_crypt_stat(&inode_info->crypt_stat);
93237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_inode_info_cache, inode_info);
94237fead6SMichael Halcrow }
95237fead6SMichael Halcrow 
96237fead6SMichael Halcrow /**
97237fead6SMichael Halcrow  * ecryptfs_init_inode
98237fead6SMichael Halcrow  * @inode: The ecryptfs inode
99237fead6SMichael Halcrow  *
100237fead6SMichael Halcrow  * Set up the ecryptfs inode.
101237fead6SMichael Halcrow  */
102237fead6SMichael Halcrow void ecryptfs_init_inode(struct inode *inode, struct inode *lower_inode)
103237fead6SMichael Halcrow {
104237fead6SMichael Halcrow 	ecryptfs_set_inode_lower(inode, lower_inode);
105237fead6SMichael Halcrow 	inode->i_ino = lower_inode->i_ino;
106237fead6SMichael Halcrow 	inode->i_version++;
107237fead6SMichael Halcrow 	inode->i_op = &ecryptfs_main_iops;
108237fead6SMichael Halcrow 	inode->i_fop = &ecryptfs_main_fops;
109237fead6SMichael Halcrow 	inode->i_mapping->a_ops = &ecryptfs_aops;
110237fead6SMichael Halcrow }
111237fead6SMichael Halcrow 
112237fead6SMichael Halcrow /**
113237fead6SMichael Halcrow  * ecryptfs_put_super
114237fead6SMichael Halcrow  * @sb: Pointer to the ecryptfs super block
115237fead6SMichael Halcrow  *
116237fead6SMichael Halcrow  * Final actions when unmounting a file system.
117237fead6SMichael Halcrow  * This will handle deallocation and release of our private data.
118237fead6SMichael Halcrow  */
119237fead6SMichael Halcrow static void ecryptfs_put_super(struct super_block *sb)
120237fead6SMichael Halcrow {
121237fead6SMichael Halcrow 	struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
122237fead6SMichael Halcrow 
123fcd12835SMichael Halcrow 	ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
124237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
125237fead6SMichael Halcrow 	ecryptfs_set_superblock_private(sb, NULL);
126237fead6SMichael Halcrow }
127237fead6SMichael Halcrow 
128237fead6SMichael Halcrow /**
129237fead6SMichael Halcrow  * ecryptfs_statfs
130237fead6SMichael Halcrow  * @sb: The ecryptfs super block
131237fead6SMichael Halcrow  * @buf: The struct kstatfs to fill in with stats
132237fead6SMichael Halcrow  *
133237fead6SMichael Halcrow  * Get the filesystem statistics. Currently, we let this pass right through
134237fead6SMichael Halcrow  * to the lower filesystem and take no action ourselves.
135237fead6SMichael Halcrow  */
136237fead6SMichael Halcrow static int ecryptfs_statfs(struct dentry *dentry, struct kstatfs *buf)
137237fead6SMichael Halcrow {
138237fead6SMichael Halcrow 	return vfs_statfs(ecryptfs_dentry_to_lower(dentry), buf);
139237fead6SMichael Halcrow }
140237fead6SMichael Halcrow 
141237fead6SMichael Halcrow /**
142237fead6SMichael Halcrow  * ecryptfs_clear_inode
143237fead6SMichael Halcrow  * @inode - The ecryptfs inode
144237fead6SMichael Halcrow  *
145237fead6SMichael Halcrow  * Called by iput() when the inode reference count reached zero
146237fead6SMichael Halcrow  * and the inode is not hashed anywhere.  Used to clear anything
147237fead6SMichael Halcrow  * that needs to be, before the inode is completely destroyed and put
148237fead6SMichael Halcrow  * on the inode free list. We use this to drop out reference to the
149237fead6SMichael Halcrow  * lower inode.
150237fead6SMichael Halcrow  */
151237fead6SMichael Halcrow static void ecryptfs_clear_inode(struct inode *inode)
152237fead6SMichael Halcrow {
153237fead6SMichael Halcrow 	iput(ecryptfs_inode_to_lower(inode));
154237fead6SMichael Halcrow }
155237fead6SMichael Halcrow 
156237fead6SMichael Halcrow /**
157237fead6SMichael Halcrow  * ecryptfs_show_options
158237fead6SMichael Halcrow  *
159237fead6SMichael Halcrow  * Prints the directory we are currently mounted over.
160237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
161237fead6SMichael Halcrow  */
162237fead6SMichael Halcrow static int ecryptfs_show_options(struct seq_file *m, struct vfsmount *mnt)
163237fead6SMichael Halcrow {
164237fead6SMichael Halcrow 	struct super_block *sb = mnt->mnt_sb;
165237fead6SMichael Halcrow 	struct dentry *lower_root_dentry = ecryptfs_dentry_to_lower(sb->s_root);
166237fead6SMichael Halcrow 	struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(sb->s_root);
167237fead6SMichael Halcrow 	char *tmp_page;
168237fead6SMichael Halcrow 	char *path;
169237fead6SMichael Halcrow 	int rc = 0;
170237fead6SMichael Halcrow 
171237fead6SMichael Halcrow 	tmp_page = (char *)__get_free_page(GFP_KERNEL);
172237fead6SMichael Halcrow 	if (!tmp_page) {
173237fead6SMichael Halcrow 		rc = -ENOMEM;
174237fead6SMichael Halcrow 		goto out;
175237fead6SMichael Halcrow 	}
176237fead6SMichael Halcrow 	path = d_path(lower_root_dentry, lower_mnt, tmp_page, PAGE_SIZE);
177237fead6SMichael Halcrow 	if (IS_ERR(path)) {
178237fead6SMichael Halcrow 		rc = PTR_ERR(path);
179237fead6SMichael Halcrow 		goto out;
180237fead6SMichael Halcrow 	}
181237fead6SMichael Halcrow 	seq_printf(m, ",dir=%s", path);
182237fead6SMichael Halcrow 	free_page((unsigned long)tmp_page);
183237fead6SMichael Halcrow out:
184237fead6SMichael Halcrow 	return rc;
185237fead6SMichael Halcrow }
186237fead6SMichael Halcrow 
187ee9b6d61SJosef 'Jeff' Sipek const struct super_operations ecryptfs_sops = {
188237fead6SMichael Halcrow 	.alloc_inode = ecryptfs_alloc_inode,
189237fead6SMichael Halcrow 	.destroy_inode = ecryptfs_destroy_inode,
190237fead6SMichael Halcrow 	.drop_inode = generic_delete_inode,
191237fead6SMichael Halcrow 	.put_super = ecryptfs_put_super,
192237fead6SMichael Halcrow 	.statfs = ecryptfs_statfs,
193237fead6SMichael Halcrow 	.remount_fs = NULL,
194237fead6SMichael Halcrow 	.clear_inode = ecryptfs_clear_inode,
195237fead6SMichael Halcrow 	.show_options = ecryptfs_show_options
196237fead6SMichael Halcrow };
197