xref: /linux/fs/ecryptfs/main.c (revision 66cb76666d69502fe982990b2cff5b6d607fd3b1)
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/mount.h>
34237fead6SMichael Halcrow #include <linux/pagemap.h>
35237fead6SMichael Halcrow #include <linux/key.h>
36237fead6SMichael Halcrow #include <linux/parser.h>
370cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
385a0e3ad6STejun Heo #include <linux/slab.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 /**
52624ae528STyler Hicks  * Module parameter that defines the number of message buffer elements
53dddfa461SMichael Halcrow  */
54dddfa461SMichael Halcrow unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
55dddfa461SMichael Halcrow 
56dddfa461SMichael Halcrow module_param(ecryptfs_message_buf_len, uint, 0);
57dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_message_buf_len,
58dddfa461SMichael Halcrow 		 "Number of message buffer elements");
59dddfa461SMichael Halcrow 
60dddfa461SMichael Halcrow /**
61dddfa461SMichael Halcrow  * Module parameter that defines the maximum guaranteed amount of time to wait
62624ae528STyler Hicks  * for a response from ecryptfsd.  The actual sleep time will be, more than
63dddfa461SMichael Halcrow  * likely, a small amount greater than this specified value, but only less if
64624ae528STyler Hicks  * the message successfully arrives.
65dddfa461SMichael Halcrow  */
66dddfa461SMichael Halcrow signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
67dddfa461SMichael Halcrow 
68dddfa461SMichael Halcrow module_param(ecryptfs_message_wait_timeout, long, 0);
69dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
70dddfa461SMichael Halcrow 		 "Maximum number of seconds that an operation will "
71dddfa461SMichael Halcrow 		 "sleep while waiting for a message response from "
72dddfa461SMichael Halcrow 		 "userspace");
73dddfa461SMichael Halcrow 
74dddfa461SMichael Halcrow /**
75dddfa461SMichael Halcrow  * Module parameter that is an estimate of the maximum number of users
76dddfa461SMichael Halcrow  * that will be concurrently using eCryptfs. Set this to the right
77dddfa461SMichael Halcrow  * value to balance performance and memory use.
78dddfa461SMichael Halcrow  */
79dddfa461SMichael Halcrow unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
80dddfa461SMichael Halcrow 
81dddfa461SMichael Halcrow module_param(ecryptfs_number_of_users, uint, 0);
82dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
83dddfa461SMichael Halcrow 		 "concurrent users of eCryptfs");
84dddfa461SMichael Halcrow 
85237fead6SMichael Halcrow void __ecryptfs_printk(const char *fmt, ...)
86237fead6SMichael Halcrow {
87237fead6SMichael Halcrow 	va_list args;
88237fead6SMichael Halcrow 	va_start(args, fmt);
89237fead6SMichael Halcrow 	if (fmt[1] == '7') { /* KERN_DEBUG */
90237fead6SMichael Halcrow 		if (ecryptfs_verbosity >= 1)
91237fead6SMichael Halcrow 			vprintk(fmt, args);
92237fead6SMichael Halcrow 	} else
93237fead6SMichael Halcrow 		vprintk(fmt, args);
94237fead6SMichael Halcrow 	va_end(args);
95237fead6SMichael Halcrow }
96237fead6SMichael Halcrow 
97237fead6SMichael Halcrow /**
984981e081SMichael Halcrow  * ecryptfs_init_persistent_file
994981e081SMichael Halcrow  * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
1004981e081SMichael Halcrow  *                   the lower dentry and the lower mount set
1014981e081SMichael Halcrow  *
1024981e081SMichael Halcrow  * eCryptfs only ever keeps a single open file for every lower
1034981e081SMichael Halcrow  * inode. All I/O operations to the lower inode occur through that
1044981e081SMichael Halcrow  * file. When the first eCryptfs dentry that interposes with the first
1054981e081SMichael Halcrow  * lower dentry for that inode is created, this function creates the
1064981e081SMichael Halcrow  * persistent file struct and associates it with the eCryptfs
1074981e081SMichael Halcrow  * inode. When the eCryptfs inode is destroyed, the file is closed.
1084981e081SMichael Halcrow  *
1094981e081SMichael Halcrow  * The persistent file will be opened with read/write permissions, if
1104981e081SMichael Halcrow  * possible. Otherwise, it is opened read-only.
1114981e081SMichael Halcrow  *
1124981e081SMichael Halcrow  * This function does nothing if a lower persistent file is already
1134981e081SMichael Halcrow  * associated with the eCryptfs inode.
1144981e081SMichael Halcrow  *
1154981e081SMichael Halcrow  * Returns zero on success; non-zero otherwise
1164981e081SMichael Halcrow  */
11772b55fffSMichael Halcrow int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
1184981e081SMichael Halcrow {
119745ca247SDavid Howells 	const struct cred *cred = current_cred();
1204981e081SMichael Halcrow 	struct ecryptfs_inode_info *inode_info =
1214981e081SMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
1224981e081SMichael Halcrow 	int rc = 0;
1234981e081SMichael Halcrow 
1244981e081SMichael Halcrow 	mutex_lock(&inode_info->lower_file_mutex);
1254981e081SMichael Halcrow 	if (!inode_info->lower_file) {
1264981e081SMichael Halcrow 		struct dentry *lower_dentry;
1274981e081SMichael Halcrow 		struct vfsmount *lower_mnt =
1284981e081SMichael Halcrow 			ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
1294981e081SMichael Halcrow 
1304981e081SMichael Halcrow 		lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
131746f1e55SMichael Halcrow 		rc = ecryptfs_privileged_open(&inode_info->lower_file,
132745ca247SDavid Howells 					      lower_dentry, lower_mnt, cred);
133ac22ba23STyler Hicks 		if (rc) {
134746f1e55SMichael Halcrow 			printk(KERN_ERR "Error opening lower persistent file "
135746f1e55SMichael Halcrow 			       "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
136746f1e55SMichael Halcrow 			       "rc = [%d]\n", lower_dentry, lower_mnt, rc);
1374981e081SMichael Halcrow 			inode_info->lower_file = NULL;
138b65a9cfcSAl Viro 		}
1394981e081SMichael Halcrow 	}
1404981e081SMichael Halcrow 	mutex_unlock(&inode_info->lower_file_mutex);
1414981e081SMichael Halcrow 	return rc;
1424981e081SMichael Halcrow }
1434981e081SMichael Halcrow 
144*66cb7666SAl Viro static inode *ecryptfs_get_inode(struct inode *lower_inode,
145*66cb7666SAl Viro 		       struct super_block *sb)
146237fead6SMichael Halcrow {
147237fead6SMichael Halcrow 	struct inode *inode;
148237fead6SMichael Halcrow 	int rc = 0;
149237fead6SMichael Halcrow 
150237fead6SMichael Halcrow 	lower_inode = lower_dentry->d_inode;
151237fead6SMichael Halcrow 	if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
152237fead6SMichael Halcrow 		rc = -EXDEV;
153237fead6SMichael Halcrow 		goto out;
154237fead6SMichael Halcrow 	}
155237fead6SMichael Halcrow 	if (!igrab(lower_inode)) {
156237fead6SMichael Halcrow 		rc = -ESTALE;
157237fead6SMichael Halcrow 		goto out;
158237fead6SMichael Halcrow 	}
159237fead6SMichael Halcrow 	inode = iget5_locked(sb, (unsigned long)lower_inode,
160237fead6SMichael Halcrow 			     ecryptfs_inode_test, ecryptfs_inode_set,
161237fead6SMichael Halcrow 			     lower_inode);
162237fead6SMichael Halcrow 	if (!inode) {
163237fead6SMichael Halcrow 		rc = -EACCES;
164237fead6SMichael Halcrow 		iput(lower_inode);
165237fead6SMichael Halcrow 		goto out;
166237fead6SMichael Halcrow 	}
167237fead6SMichael Halcrow 	if (inode->i_state & I_NEW)
168237fead6SMichael Halcrow 		unlock_new_inode(inode);
169237fead6SMichael Halcrow 	else
170237fead6SMichael Halcrow 		iput(lower_inode);
171237fead6SMichael Halcrow 	if (S_ISLNK(lower_inode->i_mode))
172237fead6SMichael Halcrow 		inode->i_op = &ecryptfs_symlink_iops;
173237fead6SMichael Halcrow 	else if (S_ISDIR(lower_inode->i_mode))
174237fead6SMichael Halcrow 		inode->i_op = &ecryptfs_dir_iops;
175237fead6SMichael Halcrow 	if (S_ISDIR(lower_inode->i_mode))
176237fead6SMichael Halcrow 		inode->i_fop = &ecryptfs_dir_fops;
17726da8205SPekka Enberg 	if (special_file(lower_inode->i_mode))
178237fead6SMichael Halcrow 		init_special_inode(inode, lower_inode->i_mode,
179237fead6SMichael Halcrow 				   lower_inode->i_rdev);
1809afa2fb6SErez Zadok 	fsstack_copy_attr_all(inode, lower_inode);
181237fead6SMichael Halcrow 	/* This size will be overwritten for real files w/ headers and
182237fead6SMichael Halcrow 	 * other metadata */
1830cc72dc7SJosef "Jeff" Sipek 	fsstack_copy_inode_size(inode, lower_inode);
184*66cb7666SAl Viro 	return inode;
185*66cb7666SAl Viro out:
186*66cb7666SAl Viro 	return ERR_PTR(rc);
187*66cb7666SAl Viro }
188*66cb7666SAl Viro 
189*66cb7666SAl Viro /**
190*66cb7666SAl Viro  * ecryptfs_interpose
191*66cb7666SAl Viro  * @lower_dentry: Existing dentry in the lower filesystem
192*66cb7666SAl Viro  * @dentry: ecryptfs' dentry
193*66cb7666SAl Viro  * @sb: ecryptfs's super_block
194*66cb7666SAl Viro  * @flags: flags to govern behavior of interpose procedure
195*66cb7666SAl Viro  *
196*66cb7666SAl Viro  * Interposes upper and lower dentries.
197*66cb7666SAl Viro  *
198*66cb7666SAl Viro  * Returns zero on success; non-zero otherwise
199*66cb7666SAl Viro  */
200*66cb7666SAl Viro int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
201*66cb7666SAl Viro 		       struct super_block *sb, u32 flags)
202*66cb7666SAl Viro {
203*66cb7666SAl Viro 	struct inode *lower_inode = lower_dentry->d_inode;
204*66cb7666SAl Viro 	struct inode *inode = ecryptfs_get_inode(lower_inode, sb);
205*66cb7666SAl Viro 	if (IS_ERR(inode)
206*66cb7666SAl Viro 		return PTR_ERR(inode);
207ae6e8459STyler Hicks 	if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
208ae6e8459STyler Hicks 		d_add(dentry, inode);
209ae6e8459STyler Hicks 	else
210ae6e8459STyler Hicks 		d_instantiate(dentry, inode);
211*66cb7666SAl Viro 	return 0;
212237fead6SMichael Halcrow }
213237fead6SMichael Halcrow 
2142830bfd6SEric Sandeen enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
2152830bfd6SEric Sandeen        ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
2162830bfd6SEric Sandeen        ecryptfs_opt_ecryptfs_key_bytes,
21717398957SMichael Halcrow        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
21887c94c4dSMichael Halcrow        ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
21987c94c4dSMichael Halcrow        ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
220f16feb51SRoberto Sassu        ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
221f16feb51SRoberto Sassu        ecryptfs_opt_err };
222237fead6SMichael Halcrow 
223a447c093SSteven Whitehouse static const match_table_t tokens = {
224237fead6SMichael Halcrow 	{ecryptfs_opt_sig, "sig=%s"},
225237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
226237fead6SMichael Halcrow 	{ecryptfs_opt_cipher, "cipher=%s"},
227237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
228237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
229237fead6SMichael Halcrow 	{ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
23017398957SMichael Halcrow 	{ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
23117398957SMichael Halcrow 	{ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
23287c94c4dSMichael Halcrow 	{ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
23387c94c4dSMichael Halcrow 	{ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
23487c94c4dSMichael Halcrow 	{ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
235e77cc8d2STyler Hicks 	{ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
236f16feb51SRoberto Sassu 	{ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
237237fead6SMichael Halcrow 	{ecryptfs_opt_err, NULL}
238237fead6SMichael Halcrow };
239237fead6SMichael Halcrow 
240f4aad16aSMichael Halcrow static int ecryptfs_init_global_auth_toks(
241f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
242237fead6SMichael Halcrow {
243f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
244237fead6SMichael Halcrow 	int rc = 0;
245237fead6SMichael Halcrow 
246f4aad16aSMichael Halcrow 	list_for_each_entry(global_auth_tok,
247f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
248f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
2495dda6992SMichael Halcrow 		rc = ecryptfs_keyring_auth_tok_for_sig(
250f4aad16aSMichael Halcrow 			&global_auth_tok->global_auth_tok_key,
251f4aad16aSMichael Halcrow 			&global_auth_tok->global_auth_tok,
2525dda6992SMichael Halcrow 			global_auth_tok->sig);
2535dda6992SMichael Halcrow 		if (rc) {
254f4aad16aSMichael Halcrow 			printk(KERN_ERR "Could not find valid key in user "
255f4aad16aSMichael Halcrow 			       "session keyring for sig specified in mount "
256f4aad16aSMichael Halcrow 			       "option: [%s]\n", global_auth_tok->sig);
257f4aad16aSMichael Halcrow 			global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
258982363c9SEric Sandeen 			goto out;
259f4aad16aSMichael Halcrow 		} else
260f4aad16aSMichael Halcrow 			global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
261237fead6SMichael Halcrow 	}
262982363c9SEric Sandeen out:
263237fead6SMichael Halcrow 	return rc;
264237fead6SMichael Halcrow }
265237fead6SMichael Halcrow 
266f4aad16aSMichael Halcrow static void ecryptfs_init_mount_crypt_stat(
267f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
268f4aad16aSMichael Halcrow {
269f4aad16aSMichael Halcrow 	memset((void *)mount_crypt_stat, 0,
270f4aad16aSMichael Halcrow 	       sizeof(struct ecryptfs_mount_crypt_stat));
271f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
272f4aad16aSMichael Halcrow 	mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
273f4aad16aSMichael Halcrow 	mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
274f4aad16aSMichael Halcrow }
275f4aad16aSMichael Halcrow 
276237fead6SMichael Halcrow /**
277237fead6SMichael Halcrow  * ecryptfs_parse_options
278237fead6SMichael Halcrow  * @sb: The ecryptfs super block
279237fead6SMichael Halcrow  * @options: The options pased to the kernel
280237fead6SMichael Halcrow  *
281237fead6SMichael Halcrow  * Parse mount options:
282237fead6SMichael Halcrow  * debug=N 	   - ecryptfs_verbosity level for debug output
283237fead6SMichael Halcrow  * sig=XXX	   - description(signature) of the key to use
284237fead6SMichael Halcrow  *
285237fead6SMichael Halcrow  * Returns the dentry object of the lower-level (lower/interposed)
286237fead6SMichael Halcrow  * directory; We want to mount our stackable file system on top of
287237fead6SMichael Halcrow  * that lower directory.
288237fead6SMichael Halcrow  *
289237fead6SMichael Halcrow  * The signature of the key to use must be the description of a key
290237fead6SMichael Halcrow  * already in the keyring. Mounting will fail if the key can not be
291237fead6SMichael Halcrow  * found.
292237fead6SMichael Halcrow  *
293237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
294237fead6SMichael Halcrow  */
2952ccde7c6SAl Viro static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options)
296237fead6SMichael Halcrow {
297237fead6SMichael Halcrow 	char *p;
298237fead6SMichael Halcrow 	int rc = 0;
299237fead6SMichael Halcrow 	int sig_set = 0;
300237fead6SMichael Halcrow 	int cipher_name_set = 0;
30187c94c4dSMichael Halcrow 	int fn_cipher_name_set = 0;
302237fead6SMichael Halcrow 	int cipher_key_bytes;
303237fead6SMichael Halcrow 	int cipher_key_bytes_set = 0;
30487c94c4dSMichael Halcrow 	int fn_cipher_key_bytes;
30587c94c4dSMichael Halcrow 	int fn_cipher_key_bytes_set = 0;
306237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
3072ccde7c6SAl Viro 		&sbi->mount_crypt_stat;
308237fead6SMichael Halcrow 	substring_t args[MAX_OPT_ARGS];
309237fead6SMichael Halcrow 	int token;
310237fead6SMichael Halcrow 	char *sig_src;
311237fead6SMichael Halcrow 	char *cipher_name_dst;
312237fead6SMichael Halcrow 	char *cipher_name_src;
31387c94c4dSMichael Halcrow 	char *fn_cipher_name_dst;
31487c94c4dSMichael Halcrow 	char *fn_cipher_name_src;
31587c94c4dSMichael Halcrow 	char *fnek_dst;
31687c94c4dSMichael Halcrow 	char *fnek_src;
317237fead6SMichael Halcrow 	char *cipher_key_bytes_src;
31887c94c4dSMichael Halcrow 	char *fn_cipher_key_bytes_src;
319237fead6SMichael Halcrow 
320237fead6SMichael Halcrow 	if (!options) {
321237fead6SMichael Halcrow 		rc = -EINVAL;
322237fead6SMichael Halcrow 		goto out;
323237fead6SMichael Halcrow 	}
324956159c3SMichael Halcrow 	ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
325237fead6SMichael Halcrow 	while ((p = strsep(&options, ",")) != NULL) {
326237fead6SMichael Halcrow 		if (!*p)
327237fead6SMichael Halcrow 			continue;
328237fead6SMichael Halcrow 		token = match_token(p, tokens, args);
329237fead6SMichael Halcrow 		switch (token) {
330237fead6SMichael Halcrow 		case ecryptfs_opt_sig:
331237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_sig:
332237fead6SMichael Halcrow 			sig_src = args[0].from;
333f4aad16aSMichael Halcrow 			rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
33484814d64STyler Hicks 							  sig_src, 0);
335f4aad16aSMichael Halcrow 			if (rc) {
336f4aad16aSMichael Halcrow 				printk(KERN_ERR "Error attempting to register "
337f4aad16aSMichael Halcrow 				       "global sig; rc = [%d]\n", rc);
338f4aad16aSMichael Halcrow 				goto out;
339f4aad16aSMichael Halcrow 			}
340237fead6SMichael Halcrow 			sig_set = 1;
341237fead6SMichael Halcrow 			break;
342237fead6SMichael Halcrow 		case ecryptfs_opt_cipher:
343237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_cipher:
344237fead6SMichael Halcrow 			cipher_name_src = args[0].from;
345237fead6SMichael Halcrow 			cipher_name_dst =
346237fead6SMichael Halcrow 				mount_crypt_stat->
347237fead6SMichael Halcrow 				global_default_cipher_name;
348237fead6SMichael Halcrow 			strncpy(cipher_name_dst, cipher_name_src,
349237fead6SMichael Halcrow 				ECRYPTFS_MAX_CIPHER_NAME_SIZE);
35087c94c4dSMichael Halcrow 			cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
351237fead6SMichael Halcrow 			cipher_name_set = 1;
352237fead6SMichael Halcrow 			break;
353237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_key_bytes:
354237fead6SMichael Halcrow 			cipher_key_bytes_src = args[0].from;
355237fead6SMichael Halcrow 			cipher_key_bytes =
356237fead6SMichael Halcrow 				(int)simple_strtol(cipher_key_bytes_src,
357237fead6SMichael Halcrow 						   &cipher_key_bytes_src, 0);
358237fead6SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size =
359237fead6SMichael Halcrow 				cipher_key_bytes;
360237fead6SMichael Halcrow 			cipher_key_bytes_set = 1;
361237fead6SMichael Halcrow 			break;
362237fead6SMichael Halcrow 		case ecryptfs_opt_passthrough:
363237fead6SMichael Halcrow 			mount_crypt_stat->flags |=
364237fead6SMichael Halcrow 				ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
365237fead6SMichael Halcrow 			break;
36617398957SMichael Halcrow 		case ecryptfs_opt_xattr_metadata:
36717398957SMichael Halcrow 			mount_crypt_stat->flags |=
36817398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
36917398957SMichael Halcrow 			break;
37017398957SMichael Halcrow 		case ecryptfs_opt_encrypted_view:
37117398957SMichael Halcrow 			mount_crypt_stat->flags |=
37217398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
37317398957SMichael Halcrow 			mount_crypt_stat->flags |=
37417398957SMichael Halcrow 				ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
37517398957SMichael Halcrow 			break;
37687c94c4dSMichael Halcrow 		case ecryptfs_opt_fnek_sig:
37787c94c4dSMichael Halcrow 			fnek_src = args[0].from;
37887c94c4dSMichael Halcrow 			fnek_dst =
37987c94c4dSMichael Halcrow 				mount_crypt_stat->global_default_fnek_sig;
38087c94c4dSMichael Halcrow 			strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
38187c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fnek_sig[
38287c94c4dSMichael Halcrow 				ECRYPTFS_SIG_SIZE_HEX] = '\0';
38387c94c4dSMichael Halcrow 			rc = ecryptfs_add_global_auth_tok(
38487c94c4dSMichael Halcrow 				mount_crypt_stat,
38584814d64STyler Hicks 				mount_crypt_stat->global_default_fnek_sig,
38684814d64STyler Hicks 				ECRYPTFS_AUTH_TOK_FNEK);
38787c94c4dSMichael Halcrow 			if (rc) {
38887c94c4dSMichael Halcrow 				printk(KERN_ERR "Error attempting to register "
38987c94c4dSMichael Halcrow 				       "global fnek sig [%s]; rc = [%d]\n",
39087c94c4dSMichael Halcrow 				       mount_crypt_stat->global_default_fnek_sig,
39187c94c4dSMichael Halcrow 				       rc);
39287c94c4dSMichael Halcrow 				goto out;
39387c94c4dSMichael Halcrow 			}
39487c94c4dSMichael Halcrow 			mount_crypt_stat->flags |=
39587c94c4dSMichael Halcrow 				(ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
39687c94c4dSMichael Halcrow 				 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
39787c94c4dSMichael Halcrow 			break;
39887c94c4dSMichael Halcrow 		case ecryptfs_opt_fn_cipher:
39987c94c4dSMichael Halcrow 			fn_cipher_name_src = args[0].from;
40087c94c4dSMichael Halcrow 			fn_cipher_name_dst =
40187c94c4dSMichael Halcrow 				mount_crypt_stat->global_default_fn_cipher_name;
40287c94c4dSMichael Halcrow 			strncpy(fn_cipher_name_dst, fn_cipher_name_src,
40387c94c4dSMichael Halcrow 				ECRYPTFS_MAX_CIPHER_NAME_SIZE);
40487c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fn_cipher_name[
40587c94c4dSMichael Halcrow 				ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
40687c94c4dSMichael Halcrow 			fn_cipher_name_set = 1;
40787c94c4dSMichael Halcrow 			break;
40887c94c4dSMichael Halcrow 		case ecryptfs_opt_fn_cipher_key_bytes:
40987c94c4dSMichael Halcrow 			fn_cipher_key_bytes_src = args[0].from;
41087c94c4dSMichael Halcrow 			fn_cipher_key_bytes =
41187c94c4dSMichael Halcrow 				(int)simple_strtol(fn_cipher_key_bytes_src,
41287c94c4dSMichael Halcrow 						   &fn_cipher_key_bytes_src, 0);
41387c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fn_cipher_key_bytes =
41487c94c4dSMichael Halcrow 				fn_cipher_key_bytes;
41587c94c4dSMichael Halcrow 			fn_cipher_key_bytes_set = 1;
41687c94c4dSMichael Halcrow 			break;
417e77cc8d2STyler Hicks 		case ecryptfs_opt_unlink_sigs:
418e77cc8d2STyler Hicks 			mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
419e77cc8d2STyler Hicks 			break;
420f16feb51SRoberto Sassu 		case ecryptfs_opt_mount_auth_tok_only:
421f16feb51SRoberto Sassu 			mount_crypt_stat->flags |=
422f16feb51SRoberto Sassu 				ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
423f16feb51SRoberto Sassu 			break;
424237fead6SMichael Halcrow 		case ecryptfs_opt_err:
425237fead6SMichael Halcrow 		default:
42687c94c4dSMichael Halcrow 			printk(KERN_WARNING
42787c94c4dSMichael Halcrow 			       "%s: eCryptfs: unrecognized option [%s]\n",
42887c94c4dSMichael Halcrow 			       __func__, p);
429237fead6SMichael Halcrow 		}
430237fead6SMichael Halcrow 	}
431237fead6SMichael Halcrow 	if (!sig_set) {
432237fead6SMichael Halcrow 		rc = -EINVAL;
433956159c3SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
434956159c3SMichael Halcrow 				"auth tok signature as a mount "
435237fead6SMichael Halcrow 				"parameter; see the eCryptfs README\n");
436237fead6SMichael Halcrow 		goto out;
437237fead6SMichael Halcrow 	}
438237fead6SMichael Halcrow 	if (!cipher_name_set) {
4398f236809SMiklos Szeredi 		int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
4408f236809SMiklos Szeredi 
4418f236809SMiklos Szeredi 		BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
4428f236809SMiklos Szeredi 		strcpy(mount_crypt_stat->global_default_cipher_name,
4438f236809SMiklos Szeredi 		       ECRYPTFS_DEFAULT_CIPHER);
444237fead6SMichael Halcrow 	}
44587c94c4dSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
44687c94c4dSMichael Halcrow 	    && !fn_cipher_name_set)
44787c94c4dSMichael Halcrow 		strcpy(mount_crypt_stat->global_default_fn_cipher_name,
44887c94c4dSMichael Halcrow 		       mount_crypt_stat->global_default_cipher_name);
44987c94c4dSMichael Halcrow 	if (!cipher_key_bytes_set)
450e5d9cbdeSMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size = 0;
45187c94c4dSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
45287c94c4dSMichael Halcrow 	    && !fn_cipher_key_bytes_set)
45387c94c4dSMichael Halcrow 		mount_crypt_stat->global_default_fn_cipher_key_bytes =
45487c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size;
455af440f52SEric Sandeen 	mutex_lock(&key_tfm_list_mutex);
456af440f52SEric Sandeen 	if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
45787c94c4dSMichael Halcrow 				 NULL)) {
4585dda6992SMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(
459f4aad16aSMichael Halcrow 			NULL, mount_crypt_stat->global_default_cipher_name,
4605dda6992SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size);
4615dda6992SMichael Halcrow 		if (rc) {
46287c94c4dSMichael Halcrow 			printk(KERN_ERR "Error attempting to initialize "
46387c94c4dSMichael Halcrow 			       "cipher with name = [%s] and key size = [%td]; "
46487c94c4dSMichael Halcrow 			       "rc = [%d]\n",
465237fead6SMichael Halcrow 			       mount_crypt_stat->global_default_cipher_name,
46687c94c4dSMichael Halcrow 			       mount_crypt_stat->global_default_cipher_key_size,
46787c94c4dSMichael Halcrow 			       rc);
468237fead6SMichael Halcrow 			rc = -EINVAL;
46987c94c4dSMichael Halcrow 			mutex_unlock(&key_tfm_list_mutex);
470237fead6SMichael Halcrow 			goto out;
471237fead6SMichael Halcrow 		}
47287c94c4dSMichael Halcrow 	}
47387c94c4dSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
47487c94c4dSMichael Halcrow 	    && !ecryptfs_tfm_exists(
47587c94c4dSMichael Halcrow 		    mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
47687c94c4dSMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(
47787c94c4dSMichael Halcrow 			NULL, mount_crypt_stat->global_default_fn_cipher_name,
47887c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fn_cipher_key_bytes);
4795dda6992SMichael Halcrow 		if (rc) {
48087c94c4dSMichael Halcrow 			printk(KERN_ERR "Error attempting to initialize "
48187c94c4dSMichael Halcrow 			       "cipher with name = [%s] and key size = [%td]; "
48287c94c4dSMichael Halcrow 			       "rc = [%d]\n",
48387c94c4dSMichael Halcrow 			       mount_crypt_stat->global_default_fn_cipher_name,
48487c94c4dSMichael Halcrow 			       mount_crypt_stat->global_default_fn_cipher_key_bytes,
48587c94c4dSMichael Halcrow 			       rc);
48687c94c4dSMichael Halcrow 			rc = -EINVAL;
48787c94c4dSMichael Halcrow 			mutex_unlock(&key_tfm_list_mutex);
48887c94c4dSMichael Halcrow 			goto out;
48987c94c4dSMichael Halcrow 		}
49087c94c4dSMichael Halcrow 	}
49187c94c4dSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
49287c94c4dSMichael Halcrow 	rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
49387c94c4dSMichael Halcrow 	if (rc)
494f4aad16aSMichael Halcrow 		printk(KERN_WARNING "One or more global auth toks could not "
495f4aad16aSMichael Halcrow 		       "properly register; rc = [%d]\n", rc);
496237fead6SMichael Halcrow out:
497237fead6SMichael Halcrow 	return rc;
498237fead6SMichael Halcrow }
499237fead6SMichael Halcrow 
500237fead6SMichael Halcrow struct kmem_cache *ecryptfs_sb_info_cache;
5014403158bSAl Viro static struct file_system_type ecryptfs_fs_type;
502237fead6SMichael Halcrow 
503237fead6SMichael Halcrow /**
504237fead6SMichael Halcrow  * ecryptfs_get_sb
505237fead6SMichael Halcrow  * @fs_type
506237fead6SMichael Halcrow  * @flags
507237fead6SMichael Halcrow  * @dev_name: The path to mount over
508237fead6SMichael Halcrow  * @raw_data: The options passed into the kernel
509237fead6SMichael Halcrow  */
5104d143bebSAl Viro static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
5114d143bebSAl Viro 			const char *dev_name, void *raw_data)
512237fead6SMichael Halcrow {
5132ccde7c6SAl Viro 	struct super_block *s;
5142ccde7c6SAl Viro 	struct ecryptfs_sb_info *sbi;
5152ccde7c6SAl Viro 	struct ecryptfs_dentry_info *root_info;
5162ccde7c6SAl Viro 	const char *err = "Getting sb failed";
517*66cb7666SAl Viro 	struct inode *inode;
518*66cb7666SAl Viro 	struct path path;
519237fead6SMichael Halcrow 	int rc;
520237fead6SMichael Halcrow 
5212ccde7c6SAl Viro 	sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
5222ccde7c6SAl Viro 	if (!sbi) {
5232ccde7c6SAl Viro 		rc = -ENOMEM;
524237fead6SMichael Halcrow 		goto out;
525237fead6SMichael Halcrow 	}
5262ccde7c6SAl Viro 
5272ccde7c6SAl Viro 	rc = ecryptfs_parse_options(sbi, raw_data);
528237fead6SMichael Halcrow 	if (rc) {
5292ccde7c6SAl Viro 		err = "Error parsing options";
530237fead6SMichael Halcrow 		goto out;
5312ccde7c6SAl Viro 	}
5322ccde7c6SAl Viro 
5332ccde7c6SAl Viro 	s = sget(fs_type, NULL, set_anon_super, NULL);
5342ccde7c6SAl Viro 	if (IS_ERR(s)) {
5352ccde7c6SAl Viro 		rc = PTR_ERR(s);
5362ccde7c6SAl Viro 		goto out;
5372ccde7c6SAl Viro 	}
5382ccde7c6SAl Viro 
5392ccde7c6SAl Viro 	s->s_flags = flags;
5402ccde7c6SAl Viro 	rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs", BDI_CAP_MAP_COPY);
541*66cb7666SAl Viro 	if (rc)
542*66cb7666SAl Viro 		goto out1;
5432ccde7c6SAl Viro 
5442ccde7c6SAl Viro 	ecryptfs_set_superblock_private(s, sbi);
5452ccde7c6SAl Viro 	s->s_bdi = &sbi->bdi;
5462ccde7c6SAl Viro 
5472ccde7c6SAl Viro 	/* ->kill_sb() will take care of sbi after that point */
5482ccde7c6SAl Viro 	sbi = NULL;
5492ccde7c6SAl Viro 	s->s_op = &ecryptfs_sops;
550*66cb7666SAl Viro 	s->s_d_op = &ecryptfs_dops;
551*66cb7666SAl Viro 
552*66cb7666SAl Viro 	err = "Reading sb failed";
553*66cb7666SAl Viro 	rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
554*66cb7666SAl Viro 	if (rc) {
555*66cb7666SAl Viro 		ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
556*66cb7666SAl Viro 		goto out1;
557*66cb7666SAl Viro 	}
558*66cb7666SAl Viro 	if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
559*66cb7666SAl Viro 		rc = -EINVAL;
560*66cb7666SAl Viro 		printk(KERN_ERR "Mount on filesystem of type "
561*66cb7666SAl Viro 			"eCryptfs explicitly disallowed due to "
562*66cb7666SAl Viro 			"known incompatibilities\n");
563*66cb7666SAl Viro 		goto out_free;
564*66cb7666SAl Viro 	}
565*66cb7666SAl Viro 	ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
566*66cb7666SAl Viro 	s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
567*66cb7666SAl Viro 	s->s_blocksize = path.dentry->d_sb->s_blocksize;
568*66cb7666SAl Viro 
569*66cb7666SAl Viro 	inode = ecryptfs_get_inode(path.dentry->d_inode, s);
570*66cb7666SAl Viro 	rc = PTR_ERR(inode);
571*66cb7666SAl Viro 	if (IS_ERR(inode))
572*66cb7666SAl Viro 		goto out_free;
573*66cb7666SAl Viro 
574*66cb7666SAl Viro 	s->s_root = d_alloc_root(inode);
575*66cb7666SAl Viro 	if (!s->s_root) {
576*66cb7666SAl Viro 		iput(inode);
577*66cb7666SAl Viro 		rc = -ENOMEM;
578*66cb7666SAl Viro 		goto out_free;
579*66cb7666SAl Viro 	}
5802ccde7c6SAl Viro 
5812ccde7c6SAl Viro 	rc = -ENOMEM;
5822ccde7c6SAl Viro 	root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
583*66cb7666SAl Viro 	if (!root_info)
584*66cb7666SAl Viro 		goto out_free;
585*66cb7666SAl Viro 
5862ccde7c6SAl Viro 	/* ->kill_sb() will take care of root_info */
5872ccde7c6SAl Viro 	ecryptfs_set_dentry_private(s->s_root, root_info);
588*66cb7666SAl Viro 	ecryptfs_set_dentry_lower(s->s_root, path.dentry);
589*66cb7666SAl Viro 	ecryptfs_set_dentry_lower_mnt(s->s_root, path.mnt);
590*66cb7666SAl Viro 
5912ccde7c6SAl Viro 	s->s_flags |= MS_ACTIVE;
5924d143bebSAl Viro 	return dget(s->s_root);
5932ccde7c6SAl Viro 
594*66cb7666SAl Viro out_free:
595*66cb7666SAl Viro 	path_put(&path);
596*66cb7666SAl Viro out1:
597*66cb7666SAl Viro 	deactivate_locked_super(s);
598237fead6SMichael Halcrow out:
5992ccde7c6SAl Viro 	if (sbi) {
6002ccde7c6SAl Viro 		ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
6012ccde7c6SAl Viro 		kmem_cache_free(ecryptfs_sb_info_cache, sbi);
6022ccde7c6SAl Viro 	}
6032ccde7c6SAl Viro 	printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
6044d143bebSAl Viro 	return ERR_PTR(rc);
605237fead6SMichael Halcrow }
606237fead6SMichael Halcrow 
607237fead6SMichael Halcrow /**
608237fead6SMichael Halcrow  * ecryptfs_kill_block_super
609237fead6SMichael Halcrow  * @sb: The ecryptfs super block
610237fead6SMichael Halcrow  *
611237fead6SMichael Halcrow  * Used to bring the superblock down and free the private data.
612237fead6SMichael Halcrow  */
613237fead6SMichael Halcrow static void ecryptfs_kill_block_super(struct super_block *sb)
614237fead6SMichael Halcrow {
615decabd66SAl Viro 	struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
616decabd66SAl Viro 	kill_anon_super(sb);
617decabd66SAl Viro 	if (!sb_info)
618decabd66SAl Viro 		return;
619decabd66SAl Viro 	ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
620decabd66SAl Viro 	bdi_destroy(&sb_info->bdi);
621decabd66SAl Viro 	kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
622237fead6SMichael Halcrow }
623237fead6SMichael Halcrow 
624237fead6SMichael Halcrow static struct file_system_type ecryptfs_fs_type = {
625237fead6SMichael Halcrow 	.owner = THIS_MODULE,
626237fead6SMichael Halcrow 	.name = "ecryptfs",
6274d143bebSAl Viro 	.mount = ecryptfs_mount,
628237fead6SMichael Halcrow 	.kill_sb = ecryptfs_kill_block_super,
629237fead6SMichael Halcrow 	.fs_flags = 0
630237fead6SMichael Halcrow };
631237fead6SMichael Halcrow 
632237fead6SMichael Halcrow /**
633237fead6SMichael Halcrow  * inode_info_init_once
634237fead6SMichael Halcrow  *
635237fead6SMichael Halcrow  * Initializes the ecryptfs_inode_info_cache when it is created
636237fead6SMichael Halcrow  */
637237fead6SMichael Halcrow static void
63851cc5068SAlexey Dobriyan inode_info_init_once(void *vptr)
639237fead6SMichael Halcrow {
640237fead6SMichael Halcrow 	struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
641237fead6SMichael Halcrow 
642237fead6SMichael Halcrow 	inode_init_once(&ei->vfs_inode);
643237fead6SMichael Halcrow }
644237fead6SMichael Halcrow 
645237fead6SMichael Halcrow static struct ecryptfs_cache_info {
646e18b890bSChristoph Lameter 	struct kmem_cache **cache;
647237fead6SMichael Halcrow 	const char *name;
648237fead6SMichael Halcrow 	size_t size;
64951cc5068SAlexey Dobriyan 	void (*ctor)(void *obj);
650237fead6SMichael Halcrow } ecryptfs_cache_infos[] = {
651237fead6SMichael Halcrow 	{
652237fead6SMichael Halcrow 		.cache = &ecryptfs_auth_tok_list_item_cache,
653237fead6SMichael Halcrow 		.name = "ecryptfs_auth_tok_list_item",
654237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_auth_tok_list_item),
655237fead6SMichael Halcrow 	},
656237fead6SMichael Halcrow 	{
657237fead6SMichael Halcrow 		.cache = &ecryptfs_file_info_cache,
658237fead6SMichael Halcrow 		.name = "ecryptfs_file_cache",
659237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_file_info),
660237fead6SMichael Halcrow 	},
661237fead6SMichael Halcrow 	{
662237fead6SMichael Halcrow 		.cache = &ecryptfs_dentry_info_cache,
663237fead6SMichael Halcrow 		.name = "ecryptfs_dentry_info_cache",
664237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_dentry_info),
665237fead6SMichael Halcrow 	},
666237fead6SMichael Halcrow 	{
667237fead6SMichael Halcrow 		.cache = &ecryptfs_inode_info_cache,
668237fead6SMichael Halcrow 		.name = "ecryptfs_inode_cache",
669237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_inode_info),
670237fead6SMichael Halcrow 		.ctor = inode_info_init_once,
671237fead6SMichael Halcrow 	},
672237fead6SMichael Halcrow 	{
673237fead6SMichael Halcrow 		.cache = &ecryptfs_sb_info_cache,
674237fead6SMichael Halcrow 		.name = "ecryptfs_sb_cache",
675237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_sb_info),
676237fead6SMichael Halcrow 	},
677237fead6SMichael Halcrow 	{
678237fead6SMichael Halcrow 		.cache = &ecryptfs_header_cache_1,
679237fead6SMichael Halcrow 		.name = "ecryptfs_headers_1",
680237fead6SMichael Halcrow 		.size = PAGE_CACHE_SIZE,
681237fead6SMichael Halcrow 	},
682237fead6SMichael Halcrow 	{
683237fead6SMichael Halcrow 		.cache = &ecryptfs_header_cache_2,
684237fead6SMichael Halcrow 		.name = "ecryptfs_headers_2",
685237fead6SMichael Halcrow 		.size = PAGE_CACHE_SIZE,
686237fead6SMichael Halcrow 	},
687237fead6SMichael Halcrow 	{
688dd2a3b7aSMichael Halcrow 		.cache = &ecryptfs_xattr_cache,
689dd2a3b7aSMichael Halcrow 		.name = "ecryptfs_xattr_cache",
690dd2a3b7aSMichael Halcrow 		.size = PAGE_CACHE_SIZE,
691dd2a3b7aSMichael Halcrow 	},
692dd2a3b7aSMichael Halcrow 	{
693eb95e7ffSMichael Halcrow 		.cache = &ecryptfs_key_record_cache,
694eb95e7ffSMichael Halcrow 		.name = "ecryptfs_key_record_cache",
695eb95e7ffSMichael Halcrow 		.size = sizeof(struct ecryptfs_key_record),
696eb95e7ffSMichael Halcrow 	},
697956159c3SMichael Halcrow 	{
698956159c3SMichael Halcrow 		.cache = &ecryptfs_key_sig_cache,
699956159c3SMichael Halcrow 		.name = "ecryptfs_key_sig_cache",
700956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_key_sig),
701956159c3SMichael Halcrow 	},
702956159c3SMichael Halcrow 	{
703956159c3SMichael Halcrow 		.cache = &ecryptfs_global_auth_tok_cache,
704956159c3SMichael Halcrow 		.name = "ecryptfs_global_auth_tok_cache",
705956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_global_auth_tok),
706956159c3SMichael Halcrow 	},
707956159c3SMichael Halcrow 	{
708956159c3SMichael Halcrow 		.cache = &ecryptfs_key_tfm_cache,
709956159c3SMichael Halcrow 		.name = "ecryptfs_key_tfm_cache",
710956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_key_tfm),
711956159c3SMichael Halcrow 	},
712746f1e55SMichael Halcrow 	{
713746f1e55SMichael Halcrow 		.cache = &ecryptfs_open_req_cache,
714746f1e55SMichael Halcrow 		.name = "ecryptfs_open_req_cache",
715746f1e55SMichael Halcrow 		.size = sizeof(struct ecryptfs_open_req),
716746f1e55SMichael Halcrow 	},
717237fead6SMichael Halcrow };
718237fead6SMichael Halcrow 
719237fead6SMichael Halcrow static void ecryptfs_free_kmem_caches(void)
720237fead6SMichael Halcrow {
721237fead6SMichael Halcrow 	int i;
722237fead6SMichael Halcrow 
723237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
724237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
725237fead6SMichael Halcrow 
726237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
727237fead6SMichael Halcrow 		if (*(info->cache))
728237fead6SMichael Halcrow 			kmem_cache_destroy(*(info->cache));
729237fead6SMichael Halcrow 	}
730237fead6SMichael Halcrow }
731237fead6SMichael Halcrow 
732237fead6SMichael Halcrow /**
733237fead6SMichael Halcrow  * ecryptfs_init_kmem_caches
734237fead6SMichael Halcrow  *
735237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
736237fead6SMichael Halcrow  */
737237fead6SMichael Halcrow static int ecryptfs_init_kmem_caches(void)
738237fead6SMichael Halcrow {
739237fead6SMichael Halcrow 	int i;
740237fead6SMichael Halcrow 
741237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
742237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
743237fead6SMichael Halcrow 
744237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
745237fead6SMichael Halcrow 		*(info->cache) = kmem_cache_create(info->name, info->size,
74620c2df83SPaul Mundt 				0, SLAB_HWCACHE_ALIGN, info->ctor);
747237fead6SMichael Halcrow 		if (!*(info->cache)) {
748237fead6SMichael Halcrow 			ecryptfs_free_kmem_caches();
749237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "%s: "
750237fead6SMichael Halcrow 					"kmem_cache_create failed\n",
751237fead6SMichael Halcrow 					info->name);
752237fead6SMichael Halcrow 			return -ENOMEM;
753237fead6SMichael Halcrow 		}
754237fead6SMichael Halcrow 	}
755237fead6SMichael Halcrow 	return 0;
756237fead6SMichael Halcrow }
757237fead6SMichael Halcrow 
7586e90aa97SGreg Kroah-Hartman static struct kobject *ecryptfs_kobj;
759237fead6SMichael Halcrow 
760386f275fSKay Sievers static ssize_t version_show(struct kobject *kobj,
761386f275fSKay Sievers 			    struct kobj_attribute *attr, char *buff)
762237fead6SMichael Halcrow {
763237fead6SMichael Halcrow 	return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
764237fead6SMichael Halcrow }
765237fead6SMichael Halcrow 
766386f275fSKay Sievers static struct kobj_attribute version_attr = __ATTR_RO(version);
767237fead6SMichael Halcrow 
76830a468b1SGreg Kroah-Hartman static struct attribute *attributes[] = {
76930a468b1SGreg Kroah-Hartman 	&version_attr.attr,
77030a468b1SGreg Kroah-Hartman 	NULL,
77130a468b1SGreg Kroah-Hartman };
77230a468b1SGreg Kroah-Hartman 
77330a468b1SGreg Kroah-Hartman static struct attribute_group attr_group = {
77430a468b1SGreg Kroah-Hartman 	.attrs = attributes,
77530a468b1SGreg Kroah-Hartman };
776237fead6SMichael Halcrow 
777237fead6SMichael Halcrow static int do_sysfs_registration(void)
778237fead6SMichael Halcrow {
779237fead6SMichael Halcrow 	int rc;
780237fead6SMichael Halcrow 
7816e90aa97SGreg Kroah-Hartman 	ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
7826e90aa97SGreg Kroah-Hartman 	if (!ecryptfs_kobj) {
783917e865dSGreg Kroah-Hartman 		printk(KERN_ERR "Unable to create ecryptfs kset\n");
784917e865dSGreg Kroah-Hartman 		rc = -ENOMEM;
785237fead6SMichael Halcrow 		goto out;
786237fead6SMichael Halcrow 	}
7876e90aa97SGreg Kroah-Hartman 	rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
788237fead6SMichael Halcrow 	if (rc) {
789237fead6SMichael Halcrow 		printk(KERN_ERR
79030a468b1SGreg Kroah-Hartman 		       "Unable to create ecryptfs version attributes\n");
791197b12d6SGreg Kroah-Hartman 		kobject_put(ecryptfs_kobj);
792237fead6SMichael Halcrow 	}
793237fead6SMichael Halcrow out:
794237fead6SMichael Halcrow 	return rc;
795237fead6SMichael Halcrow }
796237fead6SMichael Halcrow 
797a75de1b3SRyusuke Konishi static void do_sysfs_unregistration(void)
798a75de1b3SRyusuke Konishi {
7996e90aa97SGreg Kroah-Hartman 	sysfs_remove_group(ecryptfs_kobj, &attr_group);
800197b12d6SGreg Kroah-Hartman 	kobject_put(ecryptfs_kobj);
801a75de1b3SRyusuke Konishi }
802a75de1b3SRyusuke Konishi 
803237fead6SMichael Halcrow static int __init ecryptfs_init(void)
804237fead6SMichael Halcrow {
805237fead6SMichael Halcrow 	int rc;
806237fead6SMichael Halcrow 
807237fead6SMichael Halcrow 	if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
808237fead6SMichael Halcrow 		rc = -EINVAL;
809237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
810237fead6SMichael Halcrow 				"larger than the host's page size, and so "
811237fead6SMichael Halcrow 				"eCryptfs cannot run on this system. The "
812237fead6SMichael Halcrow 				"default eCryptfs extent size is [%d] bytes; "
813237fead6SMichael Halcrow 				"the page size is [%d] bytes.\n",
814237fead6SMichael Halcrow 				ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
815237fead6SMichael Halcrow 		goto out;
816237fead6SMichael Halcrow 	}
817237fead6SMichael Halcrow 	rc = ecryptfs_init_kmem_caches();
818237fead6SMichael Halcrow 	if (rc) {
819237fead6SMichael Halcrow 		printk(KERN_ERR
820237fead6SMichael Halcrow 		       "Failed to allocate one or more kmem_cache objects\n");
821237fead6SMichael Halcrow 		goto out;
822237fead6SMichael Halcrow 	}
823237fead6SMichael Halcrow 	rc = register_filesystem(&ecryptfs_fs_type);
824237fead6SMichael Halcrow 	if (rc) {
825237fead6SMichael Halcrow 		printk(KERN_ERR "Failed to register filesystem\n");
826cf81f89dSMichael Halcrow 		goto out_free_kmem_caches;
827237fead6SMichael Halcrow 	}
828237fead6SMichael Halcrow 	rc = do_sysfs_registration();
829237fead6SMichael Halcrow 	if (rc) {
830237fead6SMichael Halcrow 		printk(KERN_ERR "sysfs registration failed\n");
831cf81f89dSMichael Halcrow 		goto out_unregister_filesystem;
832237fead6SMichael Halcrow 	}
833746f1e55SMichael Halcrow 	rc = ecryptfs_init_kthread();
834746f1e55SMichael Halcrow 	if (rc) {
835746f1e55SMichael Halcrow 		printk(KERN_ERR "%s: kthread initialization failed; "
836746f1e55SMichael Halcrow 		       "rc = [%d]\n", __func__, rc);
837746f1e55SMichael Halcrow 		goto out_do_sysfs_unregistration;
838746f1e55SMichael Halcrow 	}
839624ae528STyler Hicks 	rc = ecryptfs_init_messaging();
840dddfa461SMichael Halcrow 	if (rc) {
841746f1e55SMichael Halcrow 		printk(KERN_ERR "Failure occured while attempting to "
842624ae528STyler Hicks 				"initialize the communications channel to "
843624ae528STyler Hicks 				"ecryptfsd\n");
844746f1e55SMichael Halcrow 		goto out_destroy_kthread;
845956159c3SMichael Halcrow 	}
846956159c3SMichael Halcrow 	rc = ecryptfs_init_crypto();
847956159c3SMichael Halcrow 	if (rc) {
848956159c3SMichael Halcrow 		printk(KERN_ERR "Failure whilst attempting to init crypto; "
849956159c3SMichael Halcrow 		       "rc = [%d]\n", rc);
850cf81f89dSMichael Halcrow 		goto out_release_messaging;
851dddfa461SMichael Halcrow 	}
8522830bfd6SEric Sandeen 	if (ecryptfs_verbosity > 0)
8532830bfd6SEric Sandeen 		printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
8542830bfd6SEric Sandeen 			"will be written to the syslog!\n", ecryptfs_verbosity);
8552830bfd6SEric Sandeen 
856cf81f89dSMichael Halcrow 	goto out;
857cf81f89dSMichael Halcrow out_release_messaging:
858624ae528STyler Hicks 	ecryptfs_release_messaging();
859746f1e55SMichael Halcrow out_destroy_kthread:
860746f1e55SMichael Halcrow 	ecryptfs_destroy_kthread();
861cf81f89dSMichael Halcrow out_do_sysfs_unregistration:
862cf81f89dSMichael Halcrow 	do_sysfs_unregistration();
863cf81f89dSMichael Halcrow out_unregister_filesystem:
864cf81f89dSMichael Halcrow 	unregister_filesystem(&ecryptfs_fs_type);
865cf81f89dSMichael Halcrow out_free_kmem_caches:
866cf81f89dSMichael Halcrow 	ecryptfs_free_kmem_caches();
867237fead6SMichael Halcrow out:
868237fead6SMichael Halcrow 	return rc;
869237fead6SMichael Halcrow }
870237fead6SMichael Halcrow 
871237fead6SMichael Halcrow static void __exit ecryptfs_exit(void)
872237fead6SMichael Halcrow {
873cf81f89dSMichael Halcrow 	int rc;
874cf81f89dSMichael Halcrow 
875cf81f89dSMichael Halcrow 	rc = ecryptfs_destroy_crypto();
876cf81f89dSMichael Halcrow 	if (rc)
877cf81f89dSMichael Halcrow 		printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
878cf81f89dSMichael Halcrow 		       "rc = [%d]\n", rc);
879624ae528STyler Hicks 	ecryptfs_release_messaging();
880746f1e55SMichael Halcrow 	ecryptfs_destroy_kthread();
881cf81f89dSMichael Halcrow 	do_sysfs_unregistration();
882237fead6SMichael Halcrow 	unregister_filesystem(&ecryptfs_fs_type);
883237fead6SMichael Halcrow 	ecryptfs_free_kmem_caches();
884237fead6SMichael Halcrow }
885237fead6SMichael Halcrow 
886237fead6SMichael Halcrow MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
887237fead6SMichael Halcrow MODULE_DESCRIPTION("eCryptfs");
888237fead6SMichael Halcrow 
889237fead6SMichael Halcrow MODULE_LICENSE("GPL");
890237fead6SMichael Halcrow 
891237fead6SMichael Halcrow module_init(ecryptfs_init)
892237fead6SMichael Halcrow module_exit(ecryptfs_exit)
893