xref: /linux/fs/ecryptfs/main.c (revision 8f2368095e25018838e1bf145041f58270ccd32e)
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
6dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
7237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8237fead6SMichael Halcrow  *              Michael C. Thompson <mcthomps@us.ibm.com>
9dddfa461SMichael Halcrow  *              Tyler Hicks <tyhicks@ou.edu>
10237fead6SMichael Halcrow  *
11237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
12237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
13237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
14237fead6SMichael Halcrow  * License, or (at your option) any later version.
15237fead6SMichael Halcrow  *
16237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
17237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
18237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19237fead6SMichael Halcrow  * General Public License for more details.
20237fead6SMichael Halcrow  *
21237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
22237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
23237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24237fead6SMichael Halcrow  * 02111-1307, USA.
25237fead6SMichael Halcrow  */
26237fead6SMichael Halcrow 
27237fead6SMichael Halcrow #include <linux/dcache.h>
28237fead6SMichael Halcrow #include <linux/file.h>
29237fead6SMichael Halcrow #include <linux/module.h>
30237fead6SMichael Halcrow #include <linux/namei.h>
31237fead6SMichael Halcrow #include <linux/skbuff.h>
32237fead6SMichael Halcrow #include <linux/crypto.h>
33237fead6SMichael Halcrow #include <linux/netlink.h>
34237fead6SMichael Halcrow #include <linux/mount.h>
35237fead6SMichael Halcrow #include <linux/pagemap.h>
36237fead6SMichael Halcrow #include <linux/key.h>
37237fead6SMichael Halcrow #include <linux/parser.h>
380cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
39237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
40237fead6SMichael Halcrow 
41237fead6SMichael Halcrow /**
42237fead6SMichael Halcrow  * Module parameter that defines the ecryptfs_verbosity level.
43237fead6SMichael Halcrow  */
44237fead6SMichael Halcrow int ecryptfs_verbosity = 0;
45237fead6SMichael Halcrow 
46237fead6SMichael Halcrow module_param(ecryptfs_verbosity, int, 0);
47237fead6SMichael Halcrow MODULE_PARM_DESC(ecryptfs_verbosity,
48237fead6SMichael Halcrow 		 "Initial verbosity level (0 or 1; defaults to "
49237fead6SMichael Halcrow 		 "0, which is Quiet)");
50237fead6SMichael Halcrow 
51dddfa461SMichael Halcrow /**
52dddfa461SMichael Halcrow  * Module parameter that defines the number of netlink message buffer
53dddfa461SMichael Halcrow  * elements
54dddfa461SMichael Halcrow  */
55dddfa461SMichael Halcrow unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
56dddfa461SMichael Halcrow 
57dddfa461SMichael Halcrow module_param(ecryptfs_message_buf_len, uint, 0);
58dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_message_buf_len,
59dddfa461SMichael Halcrow 		 "Number of message buffer elements");
60dddfa461SMichael Halcrow 
61dddfa461SMichael Halcrow /**
62dddfa461SMichael Halcrow  * Module parameter that defines the maximum guaranteed amount of time to wait
63dddfa461SMichael Halcrow  * for a response through netlink.  The actual sleep time will be, more than
64dddfa461SMichael Halcrow  * likely, a small amount greater than this specified value, but only less if
65dddfa461SMichael Halcrow  * the netlink message successfully arrives.
66dddfa461SMichael Halcrow  */
67dddfa461SMichael Halcrow signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
68dddfa461SMichael Halcrow 
69dddfa461SMichael Halcrow module_param(ecryptfs_message_wait_timeout, long, 0);
70dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
71dddfa461SMichael Halcrow 		 "Maximum number of seconds that an operation will "
72dddfa461SMichael Halcrow 		 "sleep while waiting for a message response from "
73dddfa461SMichael Halcrow 		 "userspace");
74dddfa461SMichael Halcrow 
75dddfa461SMichael Halcrow /**
76dddfa461SMichael Halcrow  * Module parameter that is an estimate of the maximum number of users
77dddfa461SMichael Halcrow  * that will be concurrently using eCryptfs. Set this to the right
78dddfa461SMichael Halcrow  * value to balance performance and memory use.
79dddfa461SMichael Halcrow  */
80dddfa461SMichael Halcrow unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
81dddfa461SMichael Halcrow 
82dddfa461SMichael Halcrow module_param(ecryptfs_number_of_users, uint, 0);
83dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
84dddfa461SMichael Halcrow 		 "concurrent users of eCryptfs");
85dddfa461SMichael Halcrow 
86dddfa461SMichael Halcrow unsigned int ecryptfs_transport = ECRYPTFS_DEFAULT_TRANSPORT;
87dddfa461SMichael Halcrow 
88237fead6SMichael Halcrow void __ecryptfs_printk(const char *fmt, ...)
89237fead6SMichael Halcrow {
90237fead6SMichael Halcrow 	va_list args;
91237fead6SMichael Halcrow 	va_start(args, fmt);
92237fead6SMichael Halcrow 	if (fmt[1] == '7') { /* KERN_DEBUG */
93237fead6SMichael Halcrow 		if (ecryptfs_verbosity >= 1)
94237fead6SMichael Halcrow 			vprintk(fmt, args);
95237fead6SMichael Halcrow 	} else
96237fead6SMichael Halcrow 		vprintk(fmt, args);
97237fead6SMichael Halcrow 	va_end(args);
98237fead6SMichael Halcrow }
99237fead6SMichael Halcrow 
100237fead6SMichael Halcrow /**
1014981e081SMichael Halcrow  * ecryptfs_init_persistent_file
1024981e081SMichael Halcrow  * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
1034981e081SMichael Halcrow  *                   the lower dentry and the lower mount set
1044981e081SMichael Halcrow  *
1054981e081SMichael Halcrow  * eCryptfs only ever keeps a single open file for every lower
1064981e081SMichael Halcrow  * inode. All I/O operations to the lower inode occur through that
1074981e081SMichael Halcrow  * file. When the first eCryptfs dentry that interposes with the first
1084981e081SMichael Halcrow  * lower dentry for that inode is created, this function creates the
1094981e081SMichael Halcrow  * persistent file struct and associates it with the eCryptfs
1104981e081SMichael Halcrow  * inode. When the eCryptfs inode is destroyed, the file is closed.
1114981e081SMichael Halcrow  *
1124981e081SMichael Halcrow  * The persistent file will be opened with read/write permissions, if
1134981e081SMichael Halcrow  * possible. Otherwise, it is opened read-only.
1144981e081SMichael Halcrow  *
1154981e081SMichael Halcrow  * This function does nothing if a lower persistent file is already
1164981e081SMichael Halcrow  * associated with the eCryptfs inode.
1174981e081SMichael Halcrow  *
1184981e081SMichael Halcrow  * Returns zero on success; non-zero otherwise
1194981e081SMichael Halcrow  */
1207896b631SAdrian Bunk static int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
1214981e081SMichael Halcrow {
1224981e081SMichael Halcrow 	struct ecryptfs_inode_info *inode_info =
1234981e081SMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
1244981e081SMichael Halcrow 	int rc = 0;
1254981e081SMichael Halcrow 
1264981e081SMichael Halcrow 	mutex_lock(&inode_info->lower_file_mutex);
1274981e081SMichael Halcrow 	if (!inode_info->lower_file) {
1284981e081SMichael Halcrow 		struct dentry *lower_dentry;
1294981e081SMichael Halcrow 		struct vfsmount *lower_mnt =
1304981e081SMichael Halcrow 			ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
1314981e081SMichael Halcrow 
1324981e081SMichael Halcrow 		lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
133746f1e55SMichael Halcrow 		rc = ecryptfs_privileged_open(&inode_info->lower_file,
1344981e081SMichael Halcrow 						     lower_dentry, lower_mnt);
135746f1e55SMichael Halcrow 		if (rc || IS_ERR(inode_info->lower_file)) {
136746f1e55SMichael Halcrow 			printk(KERN_ERR "Error opening lower persistent file "
137746f1e55SMichael Halcrow 			       "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
138746f1e55SMichael Halcrow 			       "rc = [%d]\n", lower_dentry, lower_mnt, rc);
1394981e081SMichael Halcrow 			rc = PTR_ERR(inode_info->lower_file);
1404981e081SMichael Halcrow 			inode_info->lower_file = NULL;
1414981e081SMichael Halcrow 		}
1424981e081SMichael Halcrow 	}
1434981e081SMichael Halcrow 	mutex_unlock(&inode_info->lower_file_mutex);
1444981e081SMichael Halcrow 	return rc;
1454981e081SMichael Halcrow }
1464981e081SMichael Halcrow 
1474981e081SMichael Halcrow /**
148237fead6SMichael Halcrow  * ecryptfs_interpose
149237fead6SMichael Halcrow  * @lower_dentry: Existing dentry in the lower filesystem
150237fead6SMichael Halcrow  * @dentry: ecryptfs' dentry
151237fead6SMichael Halcrow  * @sb: ecryptfs's super_block
152237fead6SMichael Halcrow  * @flag: If set to true, then d_add is called, else d_instantiate is called
153237fead6SMichael Halcrow  *
154237fead6SMichael Halcrow  * Interposes upper and lower dentries.
155237fead6SMichael Halcrow  *
156237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
157237fead6SMichael Halcrow  */
158237fead6SMichael Halcrow int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
159237fead6SMichael Halcrow 		       struct super_block *sb, int flag)
160237fead6SMichael Halcrow {
161237fead6SMichael Halcrow 	struct inode *lower_inode;
162237fead6SMichael Halcrow 	struct inode *inode;
163237fead6SMichael Halcrow 	int rc = 0;
164237fead6SMichael Halcrow 
165237fead6SMichael Halcrow 	lower_inode = lower_dentry->d_inode;
166237fead6SMichael Halcrow 	if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
167237fead6SMichael Halcrow 		rc = -EXDEV;
168237fead6SMichael Halcrow 		goto out;
169237fead6SMichael Halcrow 	}
170237fead6SMichael Halcrow 	if (!igrab(lower_inode)) {
171237fead6SMichael Halcrow 		rc = -ESTALE;
172237fead6SMichael Halcrow 		goto out;
173237fead6SMichael Halcrow 	}
174237fead6SMichael Halcrow 	inode = iget5_locked(sb, (unsigned long)lower_inode,
175237fead6SMichael Halcrow 			     ecryptfs_inode_test, ecryptfs_inode_set,
176237fead6SMichael Halcrow 			     lower_inode);
177237fead6SMichael Halcrow 	if (!inode) {
178237fead6SMichael Halcrow 		rc = -EACCES;
179237fead6SMichael Halcrow 		iput(lower_inode);
180237fead6SMichael Halcrow 		goto out;
181237fead6SMichael Halcrow 	}
182237fead6SMichael Halcrow 	if (inode->i_state & I_NEW)
183237fead6SMichael Halcrow 		unlock_new_inode(inode);
184237fead6SMichael Halcrow 	else
185237fead6SMichael Halcrow 		iput(lower_inode);
186237fead6SMichael Halcrow 	if (S_ISLNK(lower_inode->i_mode))
187237fead6SMichael Halcrow 		inode->i_op = &ecryptfs_symlink_iops;
188237fead6SMichael Halcrow 	else if (S_ISDIR(lower_inode->i_mode))
189237fead6SMichael Halcrow 		inode->i_op = &ecryptfs_dir_iops;
190237fead6SMichael Halcrow 	if (S_ISDIR(lower_inode->i_mode))
191237fead6SMichael Halcrow 		inode->i_fop = &ecryptfs_dir_fops;
19226da8205SPekka Enberg 	if (special_file(lower_inode->i_mode))
193237fead6SMichael Halcrow 		init_special_inode(inode, lower_inode->i_mode,
194237fead6SMichael Halcrow 				   lower_inode->i_rdev);
195237fead6SMichael Halcrow 	dentry->d_op = &ecryptfs_dops;
196237fead6SMichael Halcrow 	if (flag)
197237fead6SMichael Halcrow 		d_add(dentry, inode);
198237fead6SMichael Halcrow 	else
199237fead6SMichael Halcrow 		d_instantiate(dentry, inode);
2000cc72dc7SJosef "Jeff" Sipek 	fsstack_copy_attr_all(inode, lower_inode, NULL);
201237fead6SMichael Halcrow 	/* This size will be overwritten for real files w/ headers and
202237fead6SMichael Halcrow 	 * other metadata */
2030cc72dc7SJosef "Jeff" Sipek 	fsstack_copy_inode_size(inode, lower_inode);
2044981e081SMichael Halcrow 	rc = ecryptfs_init_persistent_file(dentry);
2054981e081SMichael Halcrow 	if (rc) {
2064981e081SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize the "
2074981e081SMichael Halcrow 		       "persistent file for the dentry with name [%s]; "
20818d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, dentry->d_name.name, rc);
2094981e081SMichael Halcrow 		goto out;
2104981e081SMichael Halcrow 	}
211237fead6SMichael Halcrow out:
212237fead6SMichael Halcrow 	return rc;
213237fead6SMichael Halcrow }
214237fead6SMichael Halcrow 
2152830bfd6SEric Sandeen enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
2162830bfd6SEric Sandeen        ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
2172830bfd6SEric Sandeen        ecryptfs_opt_ecryptfs_key_bytes,
21817398957SMichael Halcrow        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
21917398957SMichael Halcrow        ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
220237fead6SMichael Halcrow 
221237fead6SMichael Halcrow static match_table_t tokens = {
222237fead6SMichael Halcrow 	{ecryptfs_opt_sig, "sig=%s"},
223237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
224237fead6SMichael Halcrow 	{ecryptfs_opt_cipher, "cipher=%s"},
225237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
226237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
227237fead6SMichael Halcrow 	{ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
22817398957SMichael Halcrow 	{ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
22917398957SMichael Halcrow 	{ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
230237fead6SMichael Halcrow 	{ecryptfs_opt_err, NULL}
231237fead6SMichael Halcrow };
232237fead6SMichael Halcrow 
233f4aad16aSMichael Halcrow static int ecryptfs_init_global_auth_toks(
234f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
235237fead6SMichael Halcrow {
236f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
237237fead6SMichael Halcrow 	int rc = 0;
238237fead6SMichael Halcrow 
239f4aad16aSMichael Halcrow 	list_for_each_entry(global_auth_tok,
240f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
241f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
2425dda6992SMichael Halcrow 		rc = ecryptfs_keyring_auth_tok_for_sig(
243f4aad16aSMichael Halcrow 			&global_auth_tok->global_auth_tok_key,
244f4aad16aSMichael Halcrow 			&global_auth_tok->global_auth_tok,
2455dda6992SMichael Halcrow 			global_auth_tok->sig);
2465dda6992SMichael Halcrow 		if (rc) {
247f4aad16aSMichael Halcrow 			printk(KERN_ERR "Could not find valid key in user "
248f4aad16aSMichael Halcrow 			       "session keyring for sig specified in mount "
249f4aad16aSMichael Halcrow 			       "option: [%s]\n", global_auth_tok->sig);
250f4aad16aSMichael Halcrow 			global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
251982363c9SEric Sandeen 			goto out;
252f4aad16aSMichael Halcrow 		} else
253f4aad16aSMichael Halcrow 			global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
254237fead6SMichael Halcrow 	}
255982363c9SEric Sandeen out:
256237fead6SMichael Halcrow 	return rc;
257237fead6SMichael Halcrow }
258237fead6SMichael Halcrow 
259f4aad16aSMichael Halcrow static void ecryptfs_init_mount_crypt_stat(
260f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
261f4aad16aSMichael Halcrow {
262f4aad16aSMichael Halcrow 	memset((void *)mount_crypt_stat, 0,
263f4aad16aSMichael Halcrow 	       sizeof(struct ecryptfs_mount_crypt_stat));
264f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
265f4aad16aSMichael Halcrow 	mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
266f4aad16aSMichael Halcrow 	mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
267f4aad16aSMichael Halcrow }
268f4aad16aSMichael Halcrow 
269237fead6SMichael Halcrow /**
270237fead6SMichael Halcrow  * ecryptfs_parse_options
271237fead6SMichael Halcrow  * @sb: The ecryptfs super block
272237fead6SMichael Halcrow  * @options: The options pased to the kernel
273237fead6SMichael Halcrow  *
274237fead6SMichael Halcrow  * Parse mount options:
275237fead6SMichael Halcrow  * debug=N 	   - ecryptfs_verbosity level for debug output
276237fead6SMichael Halcrow  * sig=XXX	   - description(signature) of the key to use
277237fead6SMichael Halcrow  *
278237fead6SMichael Halcrow  * Returns the dentry object of the lower-level (lower/interposed)
279237fead6SMichael Halcrow  * directory; We want to mount our stackable file system on top of
280237fead6SMichael Halcrow  * that lower directory.
281237fead6SMichael Halcrow  *
282237fead6SMichael Halcrow  * The signature of the key to use must be the description of a key
283237fead6SMichael Halcrow  * already in the keyring. Mounting will fail if the key can not be
284237fead6SMichael Halcrow  * found.
285237fead6SMichael Halcrow  *
286237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
287237fead6SMichael Halcrow  */
288237fead6SMichael Halcrow static int ecryptfs_parse_options(struct super_block *sb, char *options)
289237fead6SMichael Halcrow {
290237fead6SMichael Halcrow 	char *p;
291237fead6SMichael Halcrow 	int rc = 0;
292237fead6SMichael Halcrow 	int sig_set = 0;
293237fead6SMichael Halcrow 	int cipher_name_set = 0;
294237fead6SMichael Halcrow 	int cipher_key_bytes;
295237fead6SMichael Halcrow 	int cipher_key_bytes_set = 0;
296237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
297237fead6SMichael Halcrow 		&ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
298237fead6SMichael Halcrow 	substring_t args[MAX_OPT_ARGS];
299237fead6SMichael Halcrow 	int token;
300237fead6SMichael Halcrow 	char *sig_src;
301237fead6SMichael Halcrow 	char *cipher_name_dst;
302237fead6SMichael Halcrow 	char *cipher_name_src;
303237fead6SMichael Halcrow 	char *cipher_key_bytes_src;
304237fead6SMichael Halcrow 
305237fead6SMichael Halcrow 	if (!options) {
306237fead6SMichael Halcrow 		rc = -EINVAL;
307237fead6SMichael Halcrow 		goto out;
308237fead6SMichael Halcrow 	}
309956159c3SMichael Halcrow 	ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
310237fead6SMichael Halcrow 	while ((p = strsep(&options, ",")) != NULL) {
311237fead6SMichael Halcrow 		if (!*p)
312237fead6SMichael Halcrow 			continue;
313237fead6SMichael Halcrow 		token = match_token(p, tokens, args);
314237fead6SMichael Halcrow 		switch (token) {
315237fead6SMichael Halcrow 		case ecryptfs_opt_sig:
316237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_sig:
317237fead6SMichael Halcrow 			sig_src = args[0].from;
318f4aad16aSMichael Halcrow 			rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
319f4aad16aSMichael Halcrow 							  sig_src);
320f4aad16aSMichael Halcrow 			if (rc) {
321f4aad16aSMichael Halcrow 				printk(KERN_ERR "Error attempting to register "
322f4aad16aSMichael Halcrow 				       "global sig; rc = [%d]\n", rc);
323f4aad16aSMichael Halcrow 				goto out;
324f4aad16aSMichael Halcrow 			}
325237fead6SMichael Halcrow 			sig_set = 1;
326237fead6SMichael Halcrow 			break;
327237fead6SMichael Halcrow 		case ecryptfs_opt_cipher:
328237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_cipher:
329237fead6SMichael Halcrow 			cipher_name_src = args[0].from;
330237fead6SMichael Halcrow 			cipher_name_dst =
331237fead6SMichael Halcrow 				mount_crypt_stat->
332237fead6SMichael Halcrow 				global_default_cipher_name;
333237fead6SMichael Halcrow 			strncpy(cipher_name_dst, cipher_name_src,
334237fead6SMichael Halcrow 				ECRYPTFS_MAX_CIPHER_NAME_SIZE);
335237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
336237fead6SMichael Halcrow 					"The mount_crypt_stat "
337237fead6SMichael Halcrow 					"global_default_cipher_name set to: "
338237fead6SMichael Halcrow 					"[%s]\n", cipher_name_dst);
339237fead6SMichael Halcrow 			cipher_name_set = 1;
340237fead6SMichael Halcrow 			break;
341237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_key_bytes:
342237fead6SMichael Halcrow 			cipher_key_bytes_src = args[0].from;
343237fead6SMichael Halcrow 			cipher_key_bytes =
344237fead6SMichael Halcrow 				(int)simple_strtol(cipher_key_bytes_src,
345237fead6SMichael Halcrow 						   &cipher_key_bytes_src, 0);
346237fead6SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size =
347237fead6SMichael Halcrow 				cipher_key_bytes;
348237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
349237fead6SMichael Halcrow 					"The mount_crypt_stat "
350237fead6SMichael Halcrow 					"global_default_cipher_key_size "
351237fead6SMichael Halcrow 					"set to: [%d]\n", mount_crypt_stat->
352237fead6SMichael Halcrow 					global_default_cipher_key_size);
353237fead6SMichael Halcrow 			cipher_key_bytes_set = 1;
354237fead6SMichael Halcrow 			break;
355237fead6SMichael Halcrow 		case ecryptfs_opt_passthrough:
356237fead6SMichael Halcrow 			mount_crypt_stat->flags |=
357237fead6SMichael Halcrow 				ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
358237fead6SMichael Halcrow 			break;
35917398957SMichael Halcrow 		case ecryptfs_opt_xattr_metadata:
36017398957SMichael Halcrow 			mount_crypt_stat->flags |=
36117398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
36217398957SMichael Halcrow 			break;
36317398957SMichael Halcrow 		case ecryptfs_opt_encrypted_view:
36417398957SMichael Halcrow 			mount_crypt_stat->flags |=
36517398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
36617398957SMichael Halcrow 			mount_crypt_stat->flags |=
36717398957SMichael Halcrow 				ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
36817398957SMichael Halcrow 			break;
369237fead6SMichael Halcrow 		case ecryptfs_opt_err:
370237fead6SMichael Halcrow 		default:
371237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING,
372237fead6SMichael Halcrow 					"eCryptfs: unrecognized option '%s'\n",
373237fead6SMichael Halcrow 					p);
374237fead6SMichael Halcrow 		}
375237fead6SMichael Halcrow 	}
376237fead6SMichael Halcrow 	if (!sig_set) {
377237fead6SMichael Halcrow 		rc = -EINVAL;
378956159c3SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
379956159c3SMichael Halcrow 				"auth tok signature as a mount "
380237fead6SMichael Halcrow 				"parameter; see the eCryptfs README\n");
381237fead6SMichael Halcrow 		goto out;
382237fead6SMichael Halcrow 	}
383237fead6SMichael Halcrow 	if (!cipher_name_set) {
384*8f236809SMiklos Szeredi 		int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
385*8f236809SMiklos Szeredi 
386*8f236809SMiklos Szeredi 		BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
387*8f236809SMiklos Szeredi 
388*8f236809SMiklos Szeredi 		strcpy(mount_crypt_stat->global_default_cipher_name,
389*8f236809SMiklos Szeredi 		       ECRYPTFS_DEFAULT_CIPHER);
390237fead6SMichael Halcrow 	}
391237fead6SMichael Halcrow 	if (!cipher_key_bytes_set) {
392e5d9cbdeSMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size = 0;
393237fead6SMichael Halcrow 	}
394af440f52SEric Sandeen 	mutex_lock(&key_tfm_list_mutex);
395af440f52SEric Sandeen 	if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
396af440f52SEric Sandeen 				 NULL))
3975dda6992SMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(
398f4aad16aSMichael Halcrow 			NULL, mount_crypt_stat->global_default_cipher_name,
3995dda6992SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size);
400af440f52SEric Sandeen 	mutex_unlock(&key_tfm_list_mutex);
4015dda6992SMichael Halcrow 	if (rc) {
402f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to initialize cipher with "
40381acbcd6SAndrew Morton 		       "name = [%s] and key size = [%td]; rc = [%d]\n",
404237fead6SMichael Halcrow 		       mount_crypt_stat->global_default_cipher_name,
405237fead6SMichael Halcrow 		       mount_crypt_stat->global_default_cipher_key_size, rc);
406237fead6SMichael Halcrow 		rc = -EINVAL;
407237fead6SMichael Halcrow 		goto out;
408237fead6SMichael Halcrow 	}
4095dda6992SMichael Halcrow 	rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
4105dda6992SMichael Halcrow 	if (rc) {
411f4aad16aSMichael Halcrow 		printk(KERN_WARNING "One or more global auth toks could not "
412f4aad16aSMichael Halcrow 		       "properly register; rc = [%d]\n", rc);
413237fead6SMichael Halcrow 	}
414237fead6SMichael Halcrow out:
415237fead6SMichael Halcrow 	return rc;
416237fead6SMichael Halcrow }
417237fead6SMichael Halcrow 
418237fead6SMichael Halcrow struct kmem_cache *ecryptfs_sb_info_cache;
419237fead6SMichael Halcrow 
420237fead6SMichael Halcrow /**
421237fead6SMichael Halcrow  * ecryptfs_fill_super
422237fead6SMichael Halcrow  * @sb: The ecryptfs super block
423237fead6SMichael Halcrow  * @raw_data: The options passed to mount
424237fead6SMichael Halcrow  * @silent: Not used but required by function prototype
425237fead6SMichael Halcrow  *
426237fead6SMichael Halcrow  * Sets up what we can of the sb, rest is done in ecryptfs_read_super
427237fead6SMichael Halcrow  *
428237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
429237fead6SMichael Halcrow  */
430237fead6SMichael Halcrow static int
431237fead6SMichael Halcrow ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
432237fead6SMichael Halcrow {
433237fead6SMichael Halcrow 	int rc = 0;
434237fead6SMichael Halcrow 
435237fead6SMichael Halcrow 	/* Released in ecryptfs_put_super() */
436237fead6SMichael Halcrow 	ecryptfs_set_superblock_private(sb,
437c3762229SRobert P. J. Day 					kmem_cache_zalloc(ecryptfs_sb_info_cache,
438e94b1766SChristoph Lameter 							 GFP_KERNEL));
439237fead6SMichael Halcrow 	if (!ecryptfs_superblock_to_private(sb)) {
440237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Out of memory\n");
441237fead6SMichael Halcrow 		rc = -ENOMEM;
442237fead6SMichael Halcrow 		goto out;
443237fead6SMichael Halcrow 	}
444237fead6SMichael Halcrow 	sb->s_op = &ecryptfs_sops;
445237fead6SMichael Halcrow 	/* Released through deactivate_super(sb) from get_sb_nodev */
446237fead6SMichael Halcrow 	sb->s_root = d_alloc(NULL, &(const struct qstr) {
447237fead6SMichael Halcrow 			     .hash = 0,.name = "/",.len = 1});
448237fead6SMichael Halcrow 	if (!sb->s_root) {
449237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
450237fead6SMichael Halcrow 		rc = -ENOMEM;
451237fead6SMichael Halcrow 		goto out;
452237fead6SMichael Halcrow 	}
453237fead6SMichael Halcrow 	sb->s_root->d_op = &ecryptfs_dops;
454237fead6SMichael Halcrow 	sb->s_root->d_sb = sb;
455237fead6SMichael Halcrow 	sb->s_root->d_parent = sb->s_root;
456237fead6SMichael Halcrow 	/* Released in d_release when dput(sb->s_root) is called */
457237fead6SMichael Halcrow 	/* through deactivate_super(sb) from get_sb_nodev() */
458237fead6SMichael Halcrow 	ecryptfs_set_dentry_private(sb->s_root,
459c3762229SRobert P. J. Day 				    kmem_cache_zalloc(ecryptfs_dentry_info_cache,
460e94b1766SChristoph Lameter 						     GFP_KERNEL));
461237fead6SMichael Halcrow 	if (!ecryptfs_dentry_to_private(sb->s_root)) {
462237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
463237fead6SMichael Halcrow 				"dentry_info_cache alloc failed\n");
464237fead6SMichael Halcrow 		rc = -ENOMEM;
465237fead6SMichael Halcrow 		goto out;
466237fead6SMichael Halcrow 	}
467237fead6SMichael Halcrow 	rc = 0;
468237fead6SMichael Halcrow out:
469237fead6SMichael Halcrow 	/* Should be able to rely on deactivate_super called from
470237fead6SMichael Halcrow 	 * get_sb_nodev */
471237fead6SMichael Halcrow 	return rc;
472237fead6SMichael Halcrow }
473237fead6SMichael Halcrow 
474237fead6SMichael Halcrow /**
475237fead6SMichael Halcrow  * ecryptfs_read_super
476237fead6SMichael Halcrow  * @sb: The ecryptfs super block
477237fead6SMichael Halcrow  * @dev_name: The path to mount over
478237fead6SMichael Halcrow  *
479237fead6SMichael Halcrow  * Read the super block of the lower filesystem, and use
480237fead6SMichael Halcrow  * ecryptfs_interpose to create our initial inode and super block
481237fead6SMichael Halcrow  * struct.
482237fead6SMichael Halcrow  */
483237fead6SMichael Halcrow static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
484237fead6SMichael Halcrow {
485237fead6SMichael Halcrow 	int rc;
486237fead6SMichael Halcrow 	struct nameidata nd;
487237fead6SMichael Halcrow 	struct dentry *lower_root;
488237fead6SMichael Halcrow 	struct vfsmount *lower_mnt;
489237fead6SMichael Halcrow 
490237fead6SMichael Halcrow 	memset(&nd, 0, sizeof(struct nameidata));
49182b16528SDmitriy Monakhov 	rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
492237fead6SMichael Halcrow 	if (rc) {
493237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
49465dc8145SMichael Halcrow 		goto out;
495237fead6SMichael Halcrow 	}
4964ac91378SJan Blunck 	lower_root = nd.path.dentry;
4974ac91378SJan Blunck 	lower_mnt = nd.path.mnt;
498237fead6SMichael Halcrow 	ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
499237fead6SMichael Halcrow 	sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
5007c9e70efSEric Sandeen 	sb->s_blocksize = lower_root->d_sb->s_blocksize;
501237fead6SMichael Halcrow 	ecryptfs_set_dentry_lower(sb->s_root, lower_root);
502237fead6SMichael Halcrow 	ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
5035dda6992SMichael Halcrow 	rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
5045dda6992SMichael Halcrow 	if (rc)
505237fead6SMichael Halcrow 		goto out_free;
506237fead6SMichael Halcrow 	rc = 0;
507237fead6SMichael Halcrow 	goto out;
508237fead6SMichael Halcrow out_free:
5091d957f9bSJan Blunck 	path_put(&nd.path);
510237fead6SMichael Halcrow out:
511237fead6SMichael Halcrow 	return rc;
512237fead6SMichael Halcrow }
513237fead6SMichael Halcrow 
514237fead6SMichael Halcrow /**
515237fead6SMichael Halcrow  * ecryptfs_get_sb
516237fead6SMichael Halcrow  * @fs_type
517237fead6SMichael Halcrow  * @flags
518237fead6SMichael Halcrow  * @dev_name: The path to mount over
519237fead6SMichael Halcrow  * @raw_data: The options passed into the kernel
520237fead6SMichael Halcrow  *
521237fead6SMichael Halcrow  * The whole ecryptfs_get_sb process is broken into 4 functions:
522237fead6SMichael Halcrow  * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
523237fead6SMichael Halcrow  * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
524237fead6SMichael Halcrow  *                        with as much information as it can before needing
525237fead6SMichael Halcrow  *                        the lower filesystem.
526237fead6SMichael Halcrow  * ecryptfs_read_super(): this accesses the lower filesystem and uses
527237fead6SMichael Halcrow  *                        ecryptfs_interpolate to perform most of the linking
528237fead6SMichael Halcrow  * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
529237fead6SMichael Halcrow  */
530237fead6SMichael Halcrow static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
531237fead6SMichael Halcrow 			const char *dev_name, void *raw_data,
532237fead6SMichael Halcrow 			struct vfsmount *mnt)
533237fead6SMichael Halcrow {
534237fead6SMichael Halcrow 	int rc;
535237fead6SMichael Halcrow 	struct super_block *sb;
536237fead6SMichael Halcrow 
537237fead6SMichael Halcrow 	rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
538237fead6SMichael Halcrow 	if (rc < 0) {
539237fead6SMichael Halcrow 		printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
540237fead6SMichael Halcrow 		goto out;
541237fead6SMichael Halcrow 	}
542237fead6SMichael Halcrow 	sb = mnt->mnt_sb;
543237fead6SMichael Halcrow 	rc = ecryptfs_parse_options(sb, raw_data);
544237fead6SMichael Halcrow 	if (rc) {
545237fead6SMichael Halcrow 		printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
546237fead6SMichael Halcrow 		goto out_abort;
547237fead6SMichael Halcrow 	}
548237fead6SMichael Halcrow 	rc = ecryptfs_read_super(sb, dev_name);
549237fead6SMichael Halcrow 	if (rc) {
550237fead6SMichael Halcrow 		printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
551237fead6SMichael Halcrow 		goto out_abort;
552237fead6SMichael Halcrow 	}
553237fead6SMichael Halcrow 	goto out;
554237fead6SMichael Halcrow out_abort:
555237fead6SMichael Halcrow 	dput(sb->s_root);
556237fead6SMichael Halcrow 	up_write(&sb->s_umount);
557237fead6SMichael Halcrow 	deactivate_super(sb);
558237fead6SMichael Halcrow out:
559237fead6SMichael Halcrow 	return rc;
560237fead6SMichael Halcrow }
561237fead6SMichael Halcrow 
562237fead6SMichael Halcrow /**
563237fead6SMichael Halcrow  * ecryptfs_kill_block_super
564237fead6SMichael Halcrow  * @sb: The ecryptfs super block
565237fead6SMichael Halcrow  *
566237fead6SMichael Halcrow  * Used to bring the superblock down and free the private data.
567237fead6SMichael Halcrow  * Private data is free'd in ecryptfs_put_super()
568237fead6SMichael Halcrow  */
569237fead6SMichael Halcrow static void ecryptfs_kill_block_super(struct super_block *sb)
570237fead6SMichael Halcrow {
571237fead6SMichael Halcrow 	generic_shutdown_super(sb);
572237fead6SMichael Halcrow }
573237fead6SMichael Halcrow 
574237fead6SMichael Halcrow static struct file_system_type ecryptfs_fs_type = {
575237fead6SMichael Halcrow 	.owner = THIS_MODULE,
576237fead6SMichael Halcrow 	.name = "ecryptfs",
577237fead6SMichael Halcrow 	.get_sb = ecryptfs_get_sb,
578237fead6SMichael Halcrow 	.kill_sb = ecryptfs_kill_block_super,
579237fead6SMichael Halcrow 	.fs_flags = 0
580237fead6SMichael Halcrow };
581237fead6SMichael Halcrow 
582237fead6SMichael Halcrow /**
583237fead6SMichael Halcrow  * inode_info_init_once
584237fead6SMichael Halcrow  *
585237fead6SMichael Halcrow  * Initializes the ecryptfs_inode_info_cache when it is created
586237fead6SMichael Halcrow  */
587237fead6SMichael Halcrow static void
5884ba9b9d0SChristoph Lameter inode_info_init_once(struct kmem_cache *cachep, void *vptr)
589237fead6SMichael Halcrow {
590237fead6SMichael Halcrow 	struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
591237fead6SMichael Halcrow 
592237fead6SMichael Halcrow 	inode_init_once(&ei->vfs_inode);
593237fead6SMichael Halcrow }
594237fead6SMichael Halcrow 
595237fead6SMichael Halcrow static struct ecryptfs_cache_info {
596e18b890bSChristoph Lameter 	struct kmem_cache **cache;
597237fead6SMichael Halcrow 	const char *name;
598237fead6SMichael Halcrow 	size_t size;
5994ba9b9d0SChristoph Lameter 	void (*ctor)(struct kmem_cache *cache, void *obj);
600237fead6SMichael Halcrow } ecryptfs_cache_infos[] = {
601237fead6SMichael Halcrow 	{
602237fead6SMichael Halcrow 		.cache = &ecryptfs_auth_tok_list_item_cache,
603237fead6SMichael Halcrow 		.name = "ecryptfs_auth_tok_list_item",
604237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_auth_tok_list_item),
605237fead6SMichael Halcrow 	},
606237fead6SMichael Halcrow 	{
607237fead6SMichael Halcrow 		.cache = &ecryptfs_file_info_cache,
608237fead6SMichael Halcrow 		.name = "ecryptfs_file_cache",
609237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_file_info),
610237fead6SMichael Halcrow 	},
611237fead6SMichael Halcrow 	{
612237fead6SMichael Halcrow 		.cache = &ecryptfs_dentry_info_cache,
613237fead6SMichael Halcrow 		.name = "ecryptfs_dentry_info_cache",
614237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_dentry_info),
615237fead6SMichael Halcrow 	},
616237fead6SMichael Halcrow 	{
617237fead6SMichael Halcrow 		.cache = &ecryptfs_inode_info_cache,
618237fead6SMichael Halcrow 		.name = "ecryptfs_inode_cache",
619237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_inode_info),
620237fead6SMichael Halcrow 		.ctor = inode_info_init_once,
621237fead6SMichael Halcrow 	},
622237fead6SMichael Halcrow 	{
623237fead6SMichael Halcrow 		.cache = &ecryptfs_sb_info_cache,
624237fead6SMichael Halcrow 		.name = "ecryptfs_sb_cache",
625237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_sb_info),
626237fead6SMichael Halcrow 	},
627237fead6SMichael Halcrow 	{
628237fead6SMichael Halcrow 		.cache = &ecryptfs_header_cache_1,
629237fead6SMichael Halcrow 		.name = "ecryptfs_headers_1",
630237fead6SMichael Halcrow 		.size = PAGE_CACHE_SIZE,
631237fead6SMichael Halcrow 	},
632237fead6SMichael Halcrow 	{
633237fead6SMichael Halcrow 		.cache = &ecryptfs_header_cache_2,
634237fead6SMichael Halcrow 		.name = "ecryptfs_headers_2",
635237fead6SMichael Halcrow 		.size = PAGE_CACHE_SIZE,
636237fead6SMichael Halcrow 	},
637237fead6SMichael Halcrow 	{
638dd2a3b7aSMichael Halcrow 		.cache = &ecryptfs_xattr_cache,
639dd2a3b7aSMichael Halcrow 		.name = "ecryptfs_xattr_cache",
640dd2a3b7aSMichael Halcrow 		.size = PAGE_CACHE_SIZE,
641dd2a3b7aSMichael Halcrow 	},
642dd2a3b7aSMichael Halcrow 	{
643eb95e7ffSMichael Halcrow 		.cache = &ecryptfs_key_record_cache,
644eb95e7ffSMichael Halcrow 		.name = "ecryptfs_key_record_cache",
645eb95e7ffSMichael Halcrow 		.size = sizeof(struct ecryptfs_key_record),
646eb95e7ffSMichael Halcrow 	},
647956159c3SMichael Halcrow 	{
648956159c3SMichael Halcrow 		.cache = &ecryptfs_key_sig_cache,
649956159c3SMichael Halcrow 		.name = "ecryptfs_key_sig_cache",
650956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_key_sig),
651956159c3SMichael Halcrow 	},
652956159c3SMichael Halcrow 	{
653956159c3SMichael Halcrow 		.cache = &ecryptfs_global_auth_tok_cache,
654956159c3SMichael Halcrow 		.name = "ecryptfs_global_auth_tok_cache",
655956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_global_auth_tok),
656956159c3SMichael Halcrow 	},
657956159c3SMichael Halcrow 	{
658956159c3SMichael Halcrow 		.cache = &ecryptfs_key_tfm_cache,
659956159c3SMichael Halcrow 		.name = "ecryptfs_key_tfm_cache",
660956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_key_tfm),
661956159c3SMichael Halcrow 	},
662746f1e55SMichael Halcrow 	{
663746f1e55SMichael Halcrow 		.cache = &ecryptfs_open_req_cache,
664746f1e55SMichael Halcrow 		.name = "ecryptfs_open_req_cache",
665746f1e55SMichael Halcrow 		.size = sizeof(struct ecryptfs_open_req),
666746f1e55SMichael Halcrow 	},
667237fead6SMichael Halcrow };
668237fead6SMichael Halcrow 
669237fead6SMichael Halcrow static void ecryptfs_free_kmem_caches(void)
670237fead6SMichael Halcrow {
671237fead6SMichael Halcrow 	int i;
672237fead6SMichael Halcrow 
673237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
674237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
675237fead6SMichael Halcrow 
676237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
677237fead6SMichael Halcrow 		if (*(info->cache))
678237fead6SMichael Halcrow 			kmem_cache_destroy(*(info->cache));
679237fead6SMichael Halcrow 	}
680237fead6SMichael Halcrow }
681237fead6SMichael Halcrow 
682237fead6SMichael Halcrow /**
683237fead6SMichael Halcrow  * ecryptfs_init_kmem_caches
684237fead6SMichael Halcrow  *
685237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
686237fead6SMichael Halcrow  */
687237fead6SMichael Halcrow static int ecryptfs_init_kmem_caches(void)
688237fead6SMichael Halcrow {
689237fead6SMichael Halcrow 	int i;
690237fead6SMichael Halcrow 
691237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
692237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
693237fead6SMichael Halcrow 
694237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
695237fead6SMichael Halcrow 		*(info->cache) = kmem_cache_create(info->name, info->size,
69620c2df83SPaul Mundt 				0, SLAB_HWCACHE_ALIGN, info->ctor);
697237fead6SMichael Halcrow 		if (!*(info->cache)) {
698237fead6SMichael Halcrow 			ecryptfs_free_kmem_caches();
699237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "%s: "
700237fead6SMichael Halcrow 					"kmem_cache_create failed\n",
701237fead6SMichael Halcrow 					info->name);
702237fead6SMichael Halcrow 			return -ENOMEM;
703237fead6SMichael Halcrow 		}
704237fead6SMichael Halcrow 	}
705237fead6SMichael Halcrow 	return 0;
706237fead6SMichael Halcrow }
707237fead6SMichael Halcrow 
7086e90aa97SGreg Kroah-Hartman static struct kobject *ecryptfs_kobj;
709237fead6SMichael Halcrow 
710386f275fSKay Sievers static ssize_t version_show(struct kobject *kobj,
711386f275fSKay Sievers 			    struct kobj_attribute *attr, char *buff)
712237fead6SMichael Halcrow {
713237fead6SMichael Halcrow 	return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
714237fead6SMichael Halcrow }
715237fead6SMichael Halcrow 
716386f275fSKay Sievers static struct kobj_attribute version_attr = __ATTR_RO(version);
717237fead6SMichael Halcrow 
71830a468b1SGreg Kroah-Hartman static struct attribute *attributes[] = {
71930a468b1SGreg Kroah-Hartman 	&version_attr.attr,
72030a468b1SGreg Kroah-Hartman 	NULL,
72130a468b1SGreg Kroah-Hartman };
72230a468b1SGreg Kroah-Hartman 
72330a468b1SGreg Kroah-Hartman static struct attribute_group attr_group = {
72430a468b1SGreg Kroah-Hartman 	.attrs = attributes,
72530a468b1SGreg Kroah-Hartman };
726237fead6SMichael Halcrow 
727237fead6SMichael Halcrow static int do_sysfs_registration(void)
728237fead6SMichael Halcrow {
729237fead6SMichael Halcrow 	int rc;
730237fead6SMichael Halcrow 
7316e90aa97SGreg Kroah-Hartman 	ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
7326e90aa97SGreg Kroah-Hartman 	if (!ecryptfs_kobj) {
733917e865dSGreg Kroah-Hartman 		printk(KERN_ERR "Unable to create ecryptfs kset\n");
734917e865dSGreg Kroah-Hartman 		rc = -ENOMEM;
735237fead6SMichael Halcrow 		goto out;
736237fead6SMichael Halcrow 	}
7376e90aa97SGreg Kroah-Hartman 	rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
738237fead6SMichael Halcrow 	if (rc) {
739237fead6SMichael Halcrow 		printk(KERN_ERR
74030a468b1SGreg Kroah-Hartman 		       "Unable to create ecryptfs version attributes\n");
741197b12d6SGreg Kroah-Hartman 		kobject_put(ecryptfs_kobj);
742237fead6SMichael Halcrow 	}
743237fead6SMichael Halcrow out:
744237fead6SMichael Halcrow 	return rc;
745237fead6SMichael Halcrow }
746237fead6SMichael Halcrow 
747a75de1b3SRyusuke Konishi static void do_sysfs_unregistration(void)
748a75de1b3SRyusuke Konishi {
7496e90aa97SGreg Kroah-Hartman 	sysfs_remove_group(ecryptfs_kobj, &attr_group);
750197b12d6SGreg Kroah-Hartman 	kobject_put(ecryptfs_kobj);
751a75de1b3SRyusuke Konishi }
752a75de1b3SRyusuke Konishi 
753237fead6SMichael Halcrow static int __init ecryptfs_init(void)
754237fead6SMichael Halcrow {
755237fead6SMichael Halcrow 	int rc;
756237fead6SMichael Halcrow 
757237fead6SMichael Halcrow 	if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
758237fead6SMichael Halcrow 		rc = -EINVAL;
759237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
760237fead6SMichael Halcrow 				"larger than the host's page size, and so "
761237fead6SMichael Halcrow 				"eCryptfs cannot run on this system. The "
762237fead6SMichael Halcrow 				"default eCryptfs extent size is [%d] bytes; "
763237fead6SMichael Halcrow 				"the page size is [%d] bytes.\n",
764237fead6SMichael Halcrow 				ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
765237fead6SMichael Halcrow 		goto out;
766237fead6SMichael Halcrow 	}
767237fead6SMichael Halcrow 	rc = ecryptfs_init_kmem_caches();
768237fead6SMichael Halcrow 	if (rc) {
769237fead6SMichael Halcrow 		printk(KERN_ERR
770237fead6SMichael Halcrow 		       "Failed to allocate one or more kmem_cache objects\n");
771237fead6SMichael Halcrow 		goto out;
772237fead6SMichael Halcrow 	}
773237fead6SMichael Halcrow 	rc = register_filesystem(&ecryptfs_fs_type);
774237fead6SMichael Halcrow 	if (rc) {
775237fead6SMichael Halcrow 		printk(KERN_ERR "Failed to register filesystem\n");
776cf81f89dSMichael Halcrow 		goto out_free_kmem_caches;
777237fead6SMichael Halcrow 	}
778237fead6SMichael Halcrow 	rc = do_sysfs_registration();
779237fead6SMichael Halcrow 	if (rc) {
780237fead6SMichael Halcrow 		printk(KERN_ERR "sysfs registration failed\n");
781cf81f89dSMichael Halcrow 		goto out_unregister_filesystem;
782237fead6SMichael Halcrow 	}
783746f1e55SMichael Halcrow 	rc = ecryptfs_init_kthread();
784746f1e55SMichael Halcrow 	if (rc) {
785746f1e55SMichael Halcrow 		printk(KERN_ERR "%s: kthread initialization failed; "
786746f1e55SMichael Halcrow 		       "rc = [%d]\n", __func__, rc);
787746f1e55SMichael Halcrow 		goto out_do_sysfs_unregistration;
788746f1e55SMichael Halcrow 	}
789dddfa461SMichael Halcrow 	rc = ecryptfs_init_messaging(ecryptfs_transport);
790dddfa461SMichael Halcrow 	if (rc) {
791746f1e55SMichael Halcrow 		printk(KERN_ERR "Failure occured while attempting to "
792dddfa461SMichael Halcrow 				"initialize the eCryptfs netlink socket\n");
793746f1e55SMichael Halcrow 		goto out_destroy_kthread;
794956159c3SMichael Halcrow 	}
795956159c3SMichael Halcrow 	rc = ecryptfs_init_crypto();
796956159c3SMichael Halcrow 	if (rc) {
797956159c3SMichael Halcrow 		printk(KERN_ERR "Failure whilst attempting to init crypto; "
798956159c3SMichael Halcrow 		       "rc = [%d]\n", rc);
799cf81f89dSMichael Halcrow 		goto out_release_messaging;
800dddfa461SMichael Halcrow 	}
8012830bfd6SEric Sandeen 	if (ecryptfs_verbosity > 0)
8022830bfd6SEric Sandeen 		printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
8032830bfd6SEric Sandeen 			"will be written to the syslog!\n", ecryptfs_verbosity);
8042830bfd6SEric Sandeen 
805cf81f89dSMichael Halcrow 	goto out;
806cf81f89dSMichael Halcrow out_release_messaging:
807cf81f89dSMichael Halcrow 	ecryptfs_release_messaging(ecryptfs_transport);
808746f1e55SMichael Halcrow out_destroy_kthread:
809746f1e55SMichael Halcrow 	ecryptfs_destroy_kthread();
810cf81f89dSMichael Halcrow out_do_sysfs_unregistration:
811cf81f89dSMichael Halcrow 	do_sysfs_unregistration();
812cf81f89dSMichael Halcrow out_unregister_filesystem:
813cf81f89dSMichael Halcrow 	unregister_filesystem(&ecryptfs_fs_type);
814cf81f89dSMichael Halcrow out_free_kmem_caches:
815cf81f89dSMichael Halcrow 	ecryptfs_free_kmem_caches();
816237fead6SMichael Halcrow out:
817237fead6SMichael Halcrow 	return rc;
818237fead6SMichael Halcrow }
819237fead6SMichael Halcrow 
820237fead6SMichael Halcrow static void __exit ecryptfs_exit(void)
821237fead6SMichael Halcrow {
822cf81f89dSMichael Halcrow 	int rc;
823cf81f89dSMichael Halcrow 
824cf81f89dSMichael Halcrow 	rc = ecryptfs_destroy_crypto();
825cf81f89dSMichael Halcrow 	if (rc)
826cf81f89dSMichael Halcrow 		printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
827cf81f89dSMichael Halcrow 		       "rc = [%d]\n", rc);
828dddfa461SMichael Halcrow 	ecryptfs_release_messaging(ecryptfs_transport);
829746f1e55SMichael Halcrow 	ecryptfs_destroy_kthread();
830cf81f89dSMichael Halcrow 	do_sysfs_unregistration();
831237fead6SMichael Halcrow 	unregister_filesystem(&ecryptfs_fs_type);
832237fead6SMichael Halcrow 	ecryptfs_free_kmem_caches();
833237fead6SMichael Halcrow }
834237fead6SMichael Halcrow 
835237fead6SMichael Halcrow MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
836237fead6SMichael Halcrow MODULE_DESCRIPTION("eCryptfs");
837237fead6SMichael Halcrow 
838237fead6SMichael Halcrow MODULE_LICENSE("GPL");
839237fead6SMichael Halcrow 
840237fead6SMichael Halcrow module_init(ecryptfs_init)
841237fead6SMichael Halcrow module_exit(ecryptfs_exit)
842