xref: /linux/fs/ecryptfs/main.c (revision 09cbfeaf1a5a67bfb3201e0c83c810cecb2efa5a)
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/mount.h>
33237fead6SMichael Halcrow #include <linux/pagemap.h>
34237fead6SMichael Halcrow #include <linux/key.h>
35237fead6SMichael Halcrow #include <linux/parser.h>
360cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
375a0e3ad6STejun Heo #include <linux/slab.h>
38070baa51SRoberto Sassu #include <linux/magic.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 /**
98332ab16fSTyler Hicks  * ecryptfs_init_lower_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
106332ab16fSTyler Hicks  * lower file struct and associates it with the eCryptfs
107332ab16fSTyler Hicks  * inode. When all eCryptfs files associated with the inode are released, the
108332ab16fSTyler Hicks  * file is closed.
1094981e081SMichael Halcrow  *
110332ab16fSTyler Hicks  * The lower file will be opened with read/write permissions, if
1114981e081SMichael Halcrow  * possible. Otherwise, it is opened read-only.
1124981e081SMichael Halcrow  *
113332ab16fSTyler Hicks  * This function does nothing if a lower file is already
1144981e081SMichael Halcrow  * associated with the eCryptfs inode.
1154981e081SMichael Halcrow  *
1164981e081SMichael Halcrow  * Returns zero on success; non-zero otherwise
1174981e081SMichael Halcrow  */
118332ab16fSTyler Hicks static int ecryptfs_init_lower_file(struct dentry *dentry,
119332ab16fSTyler Hicks 				    struct file **lower_file)
1204981e081SMichael Halcrow {
121745ca247SDavid Howells 	const struct cred *cred = current_cred();
122cc18ec3cSMatthew Wilcox 	struct path *path = ecryptfs_dentry_to_lower_path(dentry);
123332ab16fSTyler Hicks 	int rc;
1244981e081SMichael Halcrow 
125cc18ec3cSMatthew Wilcox 	rc = ecryptfs_privileged_open(lower_file, path->dentry, path->mnt,
126332ab16fSTyler Hicks 				      cred);
127ac22ba23STyler Hicks 	if (rc) {
128332ab16fSTyler Hicks 		printk(KERN_ERR "Error opening lower file "
129746f1e55SMichael Halcrow 		       "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
130cc18ec3cSMatthew Wilcox 		       "rc = [%d]\n", path->dentry, path->mnt, rc);
131332ab16fSTyler Hicks 		(*lower_file) = NULL;
1324981e081SMichael Halcrow 	}
1334981e081SMichael Halcrow 	return rc;
1344981e081SMichael Halcrow }
1354981e081SMichael Halcrow 
1363b06b3ebSTyler Hicks int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
137332ab16fSTyler Hicks {
1383b06b3ebSTyler Hicks 	struct ecryptfs_inode_info *inode_info;
139332ab16fSTyler Hicks 	int count, rc = 0;
140332ab16fSTyler Hicks 
1413b06b3ebSTyler Hicks 	inode_info = ecryptfs_inode_to_private(inode);
142332ab16fSTyler Hicks 	mutex_lock(&inode_info->lower_file_mutex);
143332ab16fSTyler Hicks 	count = atomic_inc_return(&inode_info->lower_file_count);
144332ab16fSTyler Hicks 	if (WARN_ON_ONCE(count < 1))
145332ab16fSTyler Hicks 		rc = -EINVAL;
146332ab16fSTyler Hicks 	else if (count == 1) {
147332ab16fSTyler Hicks 		rc = ecryptfs_init_lower_file(dentry,
148332ab16fSTyler Hicks 					      &inode_info->lower_file);
149332ab16fSTyler Hicks 		if (rc)
150332ab16fSTyler Hicks 			atomic_set(&inode_info->lower_file_count, 0);
151332ab16fSTyler Hicks 	}
152332ab16fSTyler Hicks 	mutex_unlock(&inode_info->lower_file_mutex);
153332ab16fSTyler Hicks 	return rc;
154332ab16fSTyler Hicks }
155332ab16fSTyler Hicks 
156332ab16fSTyler Hicks void ecryptfs_put_lower_file(struct inode *inode)
157332ab16fSTyler Hicks {
158332ab16fSTyler Hicks 	struct ecryptfs_inode_info *inode_info;
159332ab16fSTyler Hicks 
160332ab16fSTyler Hicks 	inode_info = ecryptfs_inode_to_private(inode);
161332ab16fSTyler Hicks 	if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
162332ab16fSTyler Hicks 				      &inode_info->lower_file_mutex)) {
1637149f255STyler Hicks 		filemap_write_and_wait(inode->i_mapping);
164332ab16fSTyler Hicks 		fput(inode_info->lower_file);
165332ab16fSTyler Hicks 		inode_info->lower_file = NULL;
166332ab16fSTyler Hicks 		mutex_unlock(&inode_info->lower_file_mutex);
167332ab16fSTyler Hicks 	}
168332ab16fSTyler Hicks }
169332ab16fSTyler Hicks 
1702830bfd6SEric Sandeen enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
1712830bfd6SEric Sandeen        ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
1722830bfd6SEric Sandeen        ecryptfs_opt_ecryptfs_key_bytes,
17317398957SMichael Halcrow        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
17487c94c4dSMichael Halcrow        ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
17587c94c4dSMichael Halcrow        ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
176f16feb51SRoberto Sassu        ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
17776435548SJohn Johansen        ecryptfs_opt_check_dev_ruid,
178f16feb51SRoberto Sassu        ecryptfs_opt_err };
179237fead6SMichael Halcrow 
180a447c093SSteven Whitehouse static const match_table_t tokens = {
181237fead6SMichael Halcrow 	{ecryptfs_opt_sig, "sig=%s"},
182237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
183237fead6SMichael Halcrow 	{ecryptfs_opt_cipher, "cipher=%s"},
184237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
185237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
186237fead6SMichael Halcrow 	{ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
18717398957SMichael Halcrow 	{ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
18817398957SMichael Halcrow 	{ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
18987c94c4dSMichael Halcrow 	{ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
19087c94c4dSMichael Halcrow 	{ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
19187c94c4dSMichael Halcrow 	{ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
192e77cc8d2STyler Hicks 	{ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
193f16feb51SRoberto Sassu 	{ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
19476435548SJohn Johansen 	{ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
195237fead6SMichael Halcrow 	{ecryptfs_opt_err, NULL}
196237fead6SMichael Halcrow };
197237fead6SMichael Halcrow 
198f4aad16aSMichael Halcrow static int ecryptfs_init_global_auth_toks(
199f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
200237fead6SMichael Halcrow {
201f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
2020e1fc5efSRoberto Sassu 	struct ecryptfs_auth_tok *auth_tok;
203237fead6SMichael Halcrow 	int rc = 0;
204237fead6SMichael Halcrow 
205f4aad16aSMichael Halcrow 	list_for_each_entry(global_auth_tok,
206f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
207f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
2085dda6992SMichael Halcrow 		rc = ecryptfs_keyring_auth_tok_for_sig(
2090e1fc5efSRoberto Sassu 			&global_auth_tok->global_auth_tok_key, &auth_tok,
2105dda6992SMichael Halcrow 			global_auth_tok->sig);
2115dda6992SMichael Halcrow 		if (rc) {
212f4aad16aSMichael Halcrow 			printk(KERN_ERR "Could not find valid key in user "
213f4aad16aSMichael Halcrow 			       "session keyring for sig specified in mount "
214f4aad16aSMichael Halcrow 			       "option: [%s]\n", global_auth_tok->sig);
215f4aad16aSMichael Halcrow 			global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
216982363c9SEric Sandeen 			goto out;
217b5695d04SRoberto Sassu 		} else {
218f4aad16aSMichael Halcrow 			global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
219b5695d04SRoberto Sassu 			up_write(&(global_auth_tok->global_auth_tok_key)->sem);
220b5695d04SRoberto Sassu 		}
221237fead6SMichael Halcrow 	}
222982363c9SEric Sandeen out:
223237fead6SMichael Halcrow 	return rc;
224237fead6SMichael Halcrow }
225237fead6SMichael Halcrow 
226f4aad16aSMichael Halcrow static void ecryptfs_init_mount_crypt_stat(
227f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
228f4aad16aSMichael Halcrow {
229f4aad16aSMichael Halcrow 	memset((void *)mount_crypt_stat, 0,
230f4aad16aSMichael Halcrow 	       sizeof(struct ecryptfs_mount_crypt_stat));
231f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
232f4aad16aSMichael Halcrow 	mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
233f4aad16aSMichael Halcrow 	mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
234f4aad16aSMichael Halcrow }
235f4aad16aSMichael Halcrow 
236237fead6SMichael Halcrow /**
237237fead6SMichael Halcrow  * ecryptfs_parse_options
238237fead6SMichael Halcrow  * @sb: The ecryptfs super block
23925985edcSLucas De Marchi  * @options: The options passed to the kernel
24076435548SJohn Johansen  * @check_ruid: set to 1 if device uid should be checked against the ruid
241237fead6SMichael Halcrow  *
242237fead6SMichael Halcrow  * Parse mount options:
243237fead6SMichael Halcrow  * debug=N 	   - ecryptfs_verbosity level for debug output
244237fead6SMichael Halcrow  * sig=XXX	   - description(signature) of the key to use
245237fead6SMichael Halcrow  *
246237fead6SMichael Halcrow  * Returns the dentry object of the lower-level (lower/interposed)
247237fead6SMichael Halcrow  * directory; We want to mount our stackable file system on top of
248237fead6SMichael Halcrow  * that lower directory.
249237fead6SMichael Halcrow  *
250237fead6SMichael Halcrow  * The signature of the key to use must be the description of a key
251237fead6SMichael Halcrow  * already in the keyring. Mounting will fail if the key can not be
252237fead6SMichael Halcrow  * found.
253237fead6SMichael Halcrow  *
254237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
255237fead6SMichael Halcrow  */
25676435548SJohn Johansen static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
25776435548SJohn Johansen 				  uid_t *check_ruid)
258237fead6SMichael Halcrow {
259237fead6SMichael Halcrow 	char *p;
260237fead6SMichael Halcrow 	int rc = 0;
261237fead6SMichael Halcrow 	int sig_set = 0;
262237fead6SMichael Halcrow 	int cipher_name_set = 0;
26387c94c4dSMichael Halcrow 	int fn_cipher_name_set = 0;
264237fead6SMichael Halcrow 	int cipher_key_bytes;
265237fead6SMichael Halcrow 	int cipher_key_bytes_set = 0;
26687c94c4dSMichael Halcrow 	int fn_cipher_key_bytes;
26787c94c4dSMichael Halcrow 	int fn_cipher_key_bytes_set = 0;
268237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2692ccde7c6SAl Viro 		&sbi->mount_crypt_stat;
270237fead6SMichael Halcrow 	substring_t args[MAX_OPT_ARGS];
271237fead6SMichael Halcrow 	int token;
272237fead6SMichael Halcrow 	char *sig_src;
273237fead6SMichael Halcrow 	char *cipher_name_dst;
274237fead6SMichael Halcrow 	char *cipher_name_src;
27587c94c4dSMichael Halcrow 	char *fn_cipher_name_dst;
27687c94c4dSMichael Halcrow 	char *fn_cipher_name_src;
27787c94c4dSMichael Halcrow 	char *fnek_dst;
27887c94c4dSMichael Halcrow 	char *fnek_src;
279237fead6SMichael Halcrow 	char *cipher_key_bytes_src;
28087c94c4dSMichael Halcrow 	char *fn_cipher_key_bytes_src;
2815f5b331dSTim Sally 	u8 cipher_code;
282237fead6SMichael Halcrow 
28376435548SJohn Johansen 	*check_ruid = 0;
28476435548SJohn Johansen 
285237fead6SMichael Halcrow 	if (!options) {
286237fead6SMichael Halcrow 		rc = -EINVAL;
287237fead6SMichael Halcrow 		goto out;
288237fead6SMichael Halcrow 	}
289956159c3SMichael Halcrow 	ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
290237fead6SMichael Halcrow 	while ((p = strsep(&options, ",")) != NULL) {
291237fead6SMichael Halcrow 		if (!*p)
292237fead6SMichael Halcrow 			continue;
293237fead6SMichael Halcrow 		token = match_token(p, tokens, args);
294237fead6SMichael Halcrow 		switch (token) {
295237fead6SMichael Halcrow 		case ecryptfs_opt_sig:
296237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_sig:
297237fead6SMichael Halcrow 			sig_src = args[0].from;
298f4aad16aSMichael Halcrow 			rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
29984814d64STyler Hicks 							  sig_src, 0);
300f4aad16aSMichael Halcrow 			if (rc) {
301f4aad16aSMichael Halcrow 				printk(KERN_ERR "Error attempting to register "
302f4aad16aSMichael Halcrow 				       "global sig; rc = [%d]\n", rc);
303f4aad16aSMichael Halcrow 				goto out;
304f4aad16aSMichael Halcrow 			}
305237fead6SMichael Halcrow 			sig_set = 1;
306237fead6SMichael Halcrow 			break;
307237fead6SMichael Halcrow 		case ecryptfs_opt_cipher:
308237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_cipher:
309237fead6SMichael Halcrow 			cipher_name_src = args[0].from;
310237fead6SMichael Halcrow 			cipher_name_dst =
311237fead6SMichael Halcrow 				mount_crypt_stat->
312237fead6SMichael Halcrow 				global_default_cipher_name;
313237fead6SMichael Halcrow 			strncpy(cipher_name_dst, cipher_name_src,
314237fead6SMichael Halcrow 				ECRYPTFS_MAX_CIPHER_NAME_SIZE);
31587c94c4dSMichael Halcrow 			cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
316237fead6SMichael Halcrow 			cipher_name_set = 1;
317237fead6SMichael Halcrow 			break;
318237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_key_bytes:
319237fead6SMichael Halcrow 			cipher_key_bytes_src = args[0].from;
320237fead6SMichael Halcrow 			cipher_key_bytes =
321237fead6SMichael Halcrow 				(int)simple_strtol(cipher_key_bytes_src,
322237fead6SMichael Halcrow 						   &cipher_key_bytes_src, 0);
323237fead6SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size =
324237fead6SMichael Halcrow 				cipher_key_bytes;
325237fead6SMichael Halcrow 			cipher_key_bytes_set = 1;
326237fead6SMichael Halcrow 			break;
327237fead6SMichael Halcrow 		case ecryptfs_opt_passthrough:
328237fead6SMichael Halcrow 			mount_crypt_stat->flags |=
329237fead6SMichael Halcrow 				ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
330237fead6SMichael Halcrow 			break;
33117398957SMichael Halcrow 		case ecryptfs_opt_xattr_metadata:
33217398957SMichael Halcrow 			mount_crypt_stat->flags |=
33317398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
33417398957SMichael Halcrow 			break;
33517398957SMichael Halcrow 		case ecryptfs_opt_encrypted_view:
33617398957SMichael Halcrow 			mount_crypt_stat->flags |=
33717398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
33817398957SMichael Halcrow 			mount_crypt_stat->flags |=
33917398957SMichael Halcrow 				ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
34017398957SMichael Halcrow 			break;
34187c94c4dSMichael Halcrow 		case ecryptfs_opt_fnek_sig:
34287c94c4dSMichael Halcrow 			fnek_src = args[0].from;
34387c94c4dSMichael Halcrow 			fnek_dst =
34487c94c4dSMichael Halcrow 				mount_crypt_stat->global_default_fnek_sig;
34587c94c4dSMichael Halcrow 			strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
34687c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fnek_sig[
34787c94c4dSMichael Halcrow 				ECRYPTFS_SIG_SIZE_HEX] = '\0';
34887c94c4dSMichael Halcrow 			rc = ecryptfs_add_global_auth_tok(
34987c94c4dSMichael Halcrow 				mount_crypt_stat,
35084814d64STyler Hicks 				mount_crypt_stat->global_default_fnek_sig,
35184814d64STyler Hicks 				ECRYPTFS_AUTH_TOK_FNEK);
35287c94c4dSMichael Halcrow 			if (rc) {
35387c94c4dSMichael Halcrow 				printk(KERN_ERR "Error attempting to register "
35487c94c4dSMichael Halcrow 				       "global fnek sig [%s]; rc = [%d]\n",
35587c94c4dSMichael Halcrow 				       mount_crypt_stat->global_default_fnek_sig,
35687c94c4dSMichael Halcrow 				       rc);
35787c94c4dSMichael Halcrow 				goto out;
35887c94c4dSMichael Halcrow 			}
35987c94c4dSMichael Halcrow 			mount_crypt_stat->flags |=
36087c94c4dSMichael Halcrow 				(ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
36187c94c4dSMichael Halcrow 				 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
36287c94c4dSMichael Halcrow 			break;
36387c94c4dSMichael Halcrow 		case ecryptfs_opt_fn_cipher:
36487c94c4dSMichael Halcrow 			fn_cipher_name_src = args[0].from;
36587c94c4dSMichael Halcrow 			fn_cipher_name_dst =
36687c94c4dSMichael Halcrow 				mount_crypt_stat->global_default_fn_cipher_name;
36787c94c4dSMichael Halcrow 			strncpy(fn_cipher_name_dst, fn_cipher_name_src,
36887c94c4dSMichael Halcrow 				ECRYPTFS_MAX_CIPHER_NAME_SIZE);
36987c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fn_cipher_name[
37087c94c4dSMichael Halcrow 				ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
37187c94c4dSMichael Halcrow 			fn_cipher_name_set = 1;
37287c94c4dSMichael Halcrow 			break;
37387c94c4dSMichael Halcrow 		case ecryptfs_opt_fn_cipher_key_bytes:
37487c94c4dSMichael Halcrow 			fn_cipher_key_bytes_src = args[0].from;
37587c94c4dSMichael Halcrow 			fn_cipher_key_bytes =
37687c94c4dSMichael Halcrow 				(int)simple_strtol(fn_cipher_key_bytes_src,
37787c94c4dSMichael Halcrow 						   &fn_cipher_key_bytes_src, 0);
37887c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fn_cipher_key_bytes =
37987c94c4dSMichael Halcrow 				fn_cipher_key_bytes;
38087c94c4dSMichael Halcrow 			fn_cipher_key_bytes_set = 1;
38187c94c4dSMichael Halcrow 			break;
382e77cc8d2STyler Hicks 		case ecryptfs_opt_unlink_sigs:
383e77cc8d2STyler Hicks 			mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
384e77cc8d2STyler Hicks 			break;
385f16feb51SRoberto Sassu 		case ecryptfs_opt_mount_auth_tok_only:
386f16feb51SRoberto Sassu 			mount_crypt_stat->flags |=
387f16feb51SRoberto Sassu 				ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
388f16feb51SRoberto Sassu 			break;
38976435548SJohn Johansen 		case ecryptfs_opt_check_dev_ruid:
39076435548SJohn Johansen 			*check_ruid = 1;
39176435548SJohn Johansen 			break;
392237fead6SMichael Halcrow 		case ecryptfs_opt_err:
393237fead6SMichael Halcrow 		default:
39487c94c4dSMichael Halcrow 			printk(KERN_WARNING
39587c94c4dSMichael Halcrow 			       "%s: eCryptfs: unrecognized option [%s]\n",
39687c94c4dSMichael Halcrow 			       __func__, p);
397237fead6SMichael Halcrow 		}
398237fead6SMichael Halcrow 	}
399237fead6SMichael Halcrow 	if (!sig_set) {
400237fead6SMichael Halcrow 		rc = -EINVAL;
401956159c3SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
402956159c3SMichael Halcrow 				"auth tok signature as a mount "
403237fead6SMichael Halcrow 				"parameter; see the eCryptfs README\n");
404237fead6SMichael Halcrow 		goto out;
405237fead6SMichael Halcrow 	}
406237fead6SMichael Halcrow 	if (!cipher_name_set) {
4078f236809SMiklos Szeredi 		int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
4088f236809SMiklos Szeredi 
4092a559a8bSColin Ian King 		BUG_ON(cipher_name_len > ECRYPTFS_MAX_CIPHER_NAME_SIZE);
4108f236809SMiklos Szeredi 		strcpy(mount_crypt_stat->global_default_cipher_name,
4118f236809SMiklos Szeredi 		       ECRYPTFS_DEFAULT_CIPHER);
412237fead6SMichael Halcrow 	}
41387c94c4dSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
41487c94c4dSMichael Halcrow 	    && !fn_cipher_name_set)
41587c94c4dSMichael Halcrow 		strcpy(mount_crypt_stat->global_default_fn_cipher_name,
41687c94c4dSMichael Halcrow 		       mount_crypt_stat->global_default_cipher_name);
41787c94c4dSMichael Halcrow 	if (!cipher_key_bytes_set)
418e5d9cbdeSMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size = 0;
41987c94c4dSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
42087c94c4dSMichael Halcrow 	    && !fn_cipher_key_bytes_set)
42187c94c4dSMichael Halcrow 		mount_crypt_stat->global_default_fn_cipher_key_bytes =
42287c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size;
4235f5b331dSTim Sally 
4245f5b331dSTim Sally 	cipher_code = ecryptfs_code_for_cipher_string(
4255f5b331dSTim Sally 		mount_crypt_stat->global_default_cipher_name,
4265f5b331dSTim Sally 		mount_crypt_stat->global_default_cipher_key_size);
4275f5b331dSTim Sally 	if (!cipher_code) {
4285f5b331dSTim Sally 		ecryptfs_printk(KERN_ERR,
4295f5b331dSTim Sally 				"eCryptfs doesn't support cipher: %s",
4305f5b331dSTim Sally 				mount_crypt_stat->global_default_cipher_name);
4315f5b331dSTim Sally 		rc = -EINVAL;
4325f5b331dSTim Sally 		goto out;
4335f5b331dSTim Sally 	}
4345f5b331dSTim Sally 
435af440f52SEric Sandeen 	mutex_lock(&key_tfm_list_mutex);
436af440f52SEric Sandeen 	if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
43787c94c4dSMichael Halcrow 				 NULL)) {
4385dda6992SMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(
439f4aad16aSMichael Halcrow 			NULL, mount_crypt_stat->global_default_cipher_name,
4405dda6992SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size);
4415dda6992SMichael Halcrow 		if (rc) {
44287c94c4dSMichael Halcrow 			printk(KERN_ERR "Error attempting to initialize "
44387c94c4dSMichael Halcrow 			       "cipher with name = [%s] and key size = [%td]; "
44487c94c4dSMichael Halcrow 			       "rc = [%d]\n",
445237fead6SMichael Halcrow 			       mount_crypt_stat->global_default_cipher_name,
44687c94c4dSMichael Halcrow 			       mount_crypt_stat->global_default_cipher_key_size,
44787c94c4dSMichael Halcrow 			       rc);
448237fead6SMichael Halcrow 			rc = -EINVAL;
44987c94c4dSMichael Halcrow 			mutex_unlock(&key_tfm_list_mutex);
450237fead6SMichael Halcrow 			goto out;
451237fead6SMichael Halcrow 		}
45287c94c4dSMichael Halcrow 	}
45387c94c4dSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
45487c94c4dSMichael Halcrow 	    && !ecryptfs_tfm_exists(
45587c94c4dSMichael Halcrow 		    mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
45687c94c4dSMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(
45787c94c4dSMichael Halcrow 			NULL, mount_crypt_stat->global_default_fn_cipher_name,
45887c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fn_cipher_key_bytes);
4595dda6992SMichael Halcrow 		if (rc) {
46087c94c4dSMichael Halcrow 			printk(KERN_ERR "Error attempting to initialize "
46187c94c4dSMichael Halcrow 			       "cipher with name = [%s] and key size = [%td]; "
46287c94c4dSMichael Halcrow 			       "rc = [%d]\n",
46387c94c4dSMichael Halcrow 			       mount_crypt_stat->global_default_fn_cipher_name,
46487c94c4dSMichael Halcrow 			       mount_crypt_stat->global_default_fn_cipher_key_bytes,
46587c94c4dSMichael Halcrow 			       rc);
46687c94c4dSMichael Halcrow 			rc = -EINVAL;
46787c94c4dSMichael Halcrow 			mutex_unlock(&key_tfm_list_mutex);
46887c94c4dSMichael Halcrow 			goto out;
46987c94c4dSMichael Halcrow 		}
47087c94c4dSMichael Halcrow 	}
47187c94c4dSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
47287c94c4dSMichael Halcrow 	rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
47387c94c4dSMichael Halcrow 	if (rc)
474f4aad16aSMichael Halcrow 		printk(KERN_WARNING "One or more global auth toks could not "
475f4aad16aSMichael Halcrow 		       "properly register; rc = [%d]\n", rc);
476237fead6SMichael Halcrow out:
477237fead6SMichael Halcrow 	return rc;
478237fead6SMichael Halcrow }
479237fead6SMichael Halcrow 
480237fead6SMichael Halcrow struct kmem_cache *ecryptfs_sb_info_cache;
4814403158bSAl Viro static struct file_system_type ecryptfs_fs_type;
482237fead6SMichael Halcrow 
483237fead6SMichael Halcrow /**
484237fead6SMichael Halcrow  * ecryptfs_get_sb
485237fead6SMichael Halcrow  * @fs_type
486237fead6SMichael Halcrow  * @flags
487237fead6SMichael Halcrow  * @dev_name: The path to mount over
488237fead6SMichael Halcrow  * @raw_data: The options passed into the kernel
489237fead6SMichael Halcrow  */
4904d143bebSAl Viro static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
4914d143bebSAl Viro 			const char *dev_name, void *raw_data)
492237fead6SMichael Halcrow {
4932ccde7c6SAl Viro 	struct super_block *s;
4942ccde7c6SAl Viro 	struct ecryptfs_sb_info *sbi;
495332b122dSTyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
4962ccde7c6SAl Viro 	struct ecryptfs_dentry_info *root_info;
4972ccde7c6SAl Viro 	const char *err = "Getting sb failed";
49866cb7666SAl Viro 	struct inode *inode;
49966cb7666SAl Viro 	struct path path;
50076435548SJohn Johansen 	uid_t check_ruid;
501237fead6SMichael Halcrow 	int rc;
502237fead6SMichael Halcrow 
5032ccde7c6SAl Viro 	sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
5042ccde7c6SAl Viro 	if (!sbi) {
5052ccde7c6SAl Viro 		rc = -ENOMEM;
506237fead6SMichael Halcrow 		goto out;
507237fead6SMichael Halcrow 	}
5082ccde7c6SAl Viro 
50976435548SJohn Johansen 	rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
510237fead6SMichael Halcrow 	if (rc) {
5112ccde7c6SAl Viro 		err = "Error parsing options";
512237fead6SMichael Halcrow 		goto out;
5132ccde7c6SAl Viro 	}
514332b122dSTyler Hicks 	mount_crypt_stat = &sbi->mount_crypt_stat;
5152ccde7c6SAl Viro 
5169249e17fSDavid Howells 	s = sget(fs_type, NULL, set_anon_super, flags, NULL);
5172ccde7c6SAl Viro 	if (IS_ERR(s)) {
5182ccde7c6SAl Viro 		rc = PTR_ERR(s);
5192ccde7c6SAl Viro 		goto out;
5202ccde7c6SAl Viro 	}
5212ccde7c6SAl Viro 
522b4caecd4SChristoph Hellwig 	rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs");
52366cb7666SAl Viro 	if (rc)
52466cb7666SAl Viro 		goto out1;
5252ccde7c6SAl Viro 
5262ccde7c6SAl Viro 	ecryptfs_set_superblock_private(s, sbi);
5272ccde7c6SAl Viro 	s->s_bdi = &sbi->bdi;
5282ccde7c6SAl Viro 
5292ccde7c6SAl Viro 	/* ->kill_sb() will take care of sbi after that point */
5302ccde7c6SAl Viro 	sbi = NULL;
5312ccde7c6SAl Viro 	s->s_op = &ecryptfs_sops;
53266cb7666SAl Viro 	s->s_d_op = &ecryptfs_dops;
53366cb7666SAl Viro 
53466cb7666SAl Viro 	err = "Reading sb failed";
53566cb7666SAl Viro 	rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
53666cb7666SAl Viro 	if (rc) {
53766cb7666SAl Viro 		ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
53866cb7666SAl Viro 		goto out1;
53966cb7666SAl Viro 	}
54066cb7666SAl Viro 	if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
54166cb7666SAl Viro 		rc = -EINVAL;
54266cb7666SAl Viro 		printk(KERN_ERR "Mount on filesystem of type "
54366cb7666SAl Viro 			"eCryptfs explicitly disallowed due to "
54466cb7666SAl Viro 			"known incompatibilities\n");
54566cb7666SAl Viro 		goto out_free;
54666cb7666SAl Viro 	}
54776435548SJohn Johansen 
5482b0143b5SDavid Howells 	if (check_ruid && !uid_eq(d_inode(path.dentry)->i_uid, current_uid())) {
54976435548SJohn Johansen 		rc = -EPERM;
55076435548SJohn Johansen 		printk(KERN_ERR "Mount of device (uid: %d) not owned by "
55176435548SJohn Johansen 		       "requested user (uid: %d)\n",
5522b0143b5SDavid Howells 			i_uid_read(d_inode(path.dentry)),
553cdf8c58aSEric W. Biederman 			from_kuid(&init_user_ns, current_uid()));
55476435548SJohn Johansen 		goto out_free;
55576435548SJohn Johansen 	}
55676435548SJohn Johansen 
55766cb7666SAl Viro 	ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
558069ddcdaSTyler Hicks 
559069ddcdaSTyler Hicks 	/**
560069ddcdaSTyler Hicks 	 * Set the POSIX ACL flag based on whether they're enabled in the lower
561332b122dSTyler Hicks 	 * mount.
562069ddcdaSTyler Hicks 	 */
563069ddcdaSTyler Hicks 	s->s_flags = flags & ~MS_POSIXACL;
564332b122dSTyler Hicks 	s->s_flags |= path.dentry->d_sb->s_flags & MS_POSIXACL;
565332b122dSTyler Hicks 
566332b122dSTyler Hicks 	/**
567332b122dSTyler Hicks 	 * Force a read-only eCryptfs mount when:
568332b122dSTyler Hicks 	 *   1) The lower mount is ro
569332b122dSTyler Hicks 	 *   2) The ecryptfs_encrypted_view mount option is specified
570332b122dSTyler Hicks 	 */
571332b122dSTyler Hicks 	if (path.dentry->d_sb->s_flags & MS_RDONLY ||
572332b122dSTyler Hicks 	    mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
573332b122dSTyler Hicks 		s->s_flags |= MS_RDONLY;
574069ddcdaSTyler Hicks 
57566cb7666SAl Viro 	s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
57666cb7666SAl Viro 	s->s_blocksize = path.dentry->d_sb->s_blocksize;
577070baa51SRoberto Sassu 	s->s_magic = ECRYPTFS_SUPER_MAGIC;
57869c433edSMiklos Szeredi 	s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
57969c433edSMiklos Szeredi 
58069c433edSMiklos Szeredi 	rc = -EINVAL;
58169c433edSMiklos Szeredi 	if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
58269c433edSMiklos Szeredi 		pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
58369c433edSMiklos Szeredi 		goto out_free;
58469c433edSMiklos Szeredi 	}
58566cb7666SAl Viro 
5862b0143b5SDavid Howells 	inode = ecryptfs_get_inode(d_inode(path.dentry), s);
58766cb7666SAl Viro 	rc = PTR_ERR(inode);
58866cb7666SAl Viro 	if (IS_ERR(inode))
58966cb7666SAl Viro 		goto out_free;
59066cb7666SAl Viro 
59148fde701SAl Viro 	s->s_root = d_make_root(inode);
59266cb7666SAl Viro 	if (!s->s_root) {
59366cb7666SAl Viro 		rc = -ENOMEM;
59466cb7666SAl Viro 		goto out_free;
59566cb7666SAl Viro 	}
5962ccde7c6SAl Viro 
5972ccde7c6SAl Viro 	rc = -ENOMEM;
5982ccde7c6SAl Viro 	root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
59966cb7666SAl Viro 	if (!root_info)
60066cb7666SAl Viro 		goto out_free;
60166cb7666SAl Viro 
6022ccde7c6SAl Viro 	/* ->kill_sb() will take care of root_info */
6032ccde7c6SAl Viro 	ecryptfs_set_dentry_private(s->s_root, root_info);
60492dd1230SAl Viro 	root_info->lower_path = path;
60566cb7666SAl Viro 
6062ccde7c6SAl Viro 	s->s_flags |= MS_ACTIVE;
6074d143bebSAl Viro 	return dget(s->s_root);
6082ccde7c6SAl Viro 
60966cb7666SAl Viro out_free:
61066cb7666SAl Viro 	path_put(&path);
61166cb7666SAl Viro out1:
61266cb7666SAl Viro 	deactivate_locked_super(s);
613237fead6SMichael Halcrow out:
6142ccde7c6SAl Viro 	if (sbi) {
6152ccde7c6SAl Viro 		ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
6162ccde7c6SAl Viro 		kmem_cache_free(ecryptfs_sb_info_cache, sbi);
6172ccde7c6SAl Viro 	}
6182ccde7c6SAl Viro 	printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
6194d143bebSAl Viro 	return ERR_PTR(rc);
620237fead6SMichael Halcrow }
621237fead6SMichael Halcrow 
622237fead6SMichael Halcrow /**
623237fead6SMichael Halcrow  * ecryptfs_kill_block_super
624237fead6SMichael Halcrow  * @sb: The ecryptfs super block
625237fead6SMichael Halcrow  *
626237fead6SMichael Halcrow  * Used to bring the superblock down and free the private data.
627237fead6SMichael Halcrow  */
628237fead6SMichael Halcrow static void ecryptfs_kill_block_super(struct super_block *sb)
629237fead6SMichael Halcrow {
630decabd66SAl Viro 	struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
631decabd66SAl Viro 	kill_anon_super(sb);
632decabd66SAl Viro 	if (!sb_info)
633decabd66SAl Viro 		return;
634decabd66SAl Viro 	ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
635decabd66SAl Viro 	bdi_destroy(&sb_info->bdi);
636decabd66SAl Viro 	kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
637237fead6SMichael Halcrow }
638237fead6SMichael Halcrow 
639237fead6SMichael Halcrow static struct file_system_type ecryptfs_fs_type = {
640237fead6SMichael Halcrow 	.owner = THIS_MODULE,
641237fead6SMichael Halcrow 	.name = "ecryptfs",
6424d143bebSAl Viro 	.mount = ecryptfs_mount,
643237fead6SMichael Halcrow 	.kill_sb = ecryptfs_kill_block_super,
644237fead6SMichael Halcrow 	.fs_flags = 0
645237fead6SMichael Halcrow };
6467f78e035SEric W. Biederman MODULE_ALIAS_FS("ecryptfs");
647237fead6SMichael Halcrow 
648237fead6SMichael Halcrow /**
649237fead6SMichael Halcrow  * inode_info_init_once
650237fead6SMichael Halcrow  *
651237fead6SMichael Halcrow  * Initializes the ecryptfs_inode_info_cache when it is created
652237fead6SMichael Halcrow  */
653237fead6SMichael Halcrow static void
65451cc5068SAlexey Dobriyan inode_info_init_once(void *vptr)
655237fead6SMichael Halcrow {
656237fead6SMichael Halcrow 	struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
657237fead6SMichael Halcrow 
658237fead6SMichael Halcrow 	inode_init_once(&ei->vfs_inode);
659237fead6SMichael Halcrow }
660237fead6SMichael Halcrow 
661237fead6SMichael Halcrow static struct ecryptfs_cache_info {
662e18b890bSChristoph Lameter 	struct kmem_cache **cache;
663237fead6SMichael Halcrow 	const char *name;
664237fead6SMichael Halcrow 	size_t size;
6655d097056SVladimir Davydov 	unsigned long flags;
66651cc5068SAlexey Dobriyan 	void (*ctor)(void *obj);
667237fead6SMichael Halcrow } ecryptfs_cache_infos[] = {
668237fead6SMichael Halcrow 	{
669237fead6SMichael Halcrow 		.cache = &ecryptfs_auth_tok_list_item_cache,
670237fead6SMichael Halcrow 		.name = "ecryptfs_auth_tok_list_item",
671237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_auth_tok_list_item),
672237fead6SMichael Halcrow 	},
673237fead6SMichael Halcrow 	{
674237fead6SMichael Halcrow 		.cache = &ecryptfs_file_info_cache,
675237fead6SMichael Halcrow 		.name = "ecryptfs_file_cache",
676237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_file_info),
677237fead6SMichael Halcrow 	},
678237fead6SMichael Halcrow 	{
679237fead6SMichael Halcrow 		.cache = &ecryptfs_dentry_info_cache,
680237fead6SMichael Halcrow 		.name = "ecryptfs_dentry_info_cache",
681237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_dentry_info),
682237fead6SMichael Halcrow 	},
683237fead6SMichael Halcrow 	{
684237fead6SMichael Halcrow 		.cache = &ecryptfs_inode_info_cache,
685237fead6SMichael Halcrow 		.name = "ecryptfs_inode_cache",
686237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_inode_info),
6875d097056SVladimir Davydov 		.flags = SLAB_ACCOUNT,
688237fead6SMichael Halcrow 		.ctor = inode_info_init_once,
689237fead6SMichael Halcrow 	},
690237fead6SMichael Halcrow 	{
691237fead6SMichael Halcrow 		.cache = &ecryptfs_sb_info_cache,
692237fead6SMichael Halcrow 		.name = "ecryptfs_sb_cache",
693237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_sb_info),
694237fead6SMichael Halcrow 	},
695237fead6SMichael Halcrow 	{
69630632870STyler Hicks 		.cache = &ecryptfs_header_cache,
69730632870STyler Hicks 		.name = "ecryptfs_headers",
698*09cbfeafSKirill A. Shutemov 		.size = PAGE_SIZE,
699237fead6SMichael Halcrow 	},
700237fead6SMichael Halcrow 	{
701dd2a3b7aSMichael Halcrow 		.cache = &ecryptfs_xattr_cache,
702dd2a3b7aSMichael Halcrow 		.name = "ecryptfs_xattr_cache",
703*09cbfeafSKirill A. Shutemov 		.size = PAGE_SIZE,
704dd2a3b7aSMichael Halcrow 	},
705dd2a3b7aSMichael Halcrow 	{
706eb95e7ffSMichael Halcrow 		.cache = &ecryptfs_key_record_cache,
707eb95e7ffSMichael Halcrow 		.name = "ecryptfs_key_record_cache",
708eb95e7ffSMichael Halcrow 		.size = sizeof(struct ecryptfs_key_record),
709eb95e7ffSMichael Halcrow 	},
710956159c3SMichael Halcrow 	{
711956159c3SMichael Halcrow 		.cache = &ecryptfs_key_sig_cache,
712956159c3SMichael Halcrow 		.name = "ecryptfs_key_sig_cache",
713956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_key_sig),
714956159c3SMichael Halcrow 	},
715956159c3SMichael Halcrow 	{
716956159c3SMichael Halcrow 		.cache = &ecryptfs_global_auth_tok_cache,
717956159c3SMichael Halcrow 		.name = "ecryptfs_global_auth_tok_cache",
718956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_global_auth_tok),
719956159c3SMichael Halcrow 	},
720956159c3SMichael Halcrow 	{
721956159c3SMichael Halcrow 		.cache = &ecryptfs_key_tfm_cache,
722956159c3SMichael Halcrow 		.name = "ecryptfs_key_tfm_cache",
723956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_key_tfm),
724956159c3SMichael Halcrow 	},
725237fead6SMichael Halcrow };
726237fead6SMichael Halcrow 
727237fead6SMichael Halcrow static void ecryptfs_free_kmem_caches(void)
728237fead6SMichael Halcrow {
729237fead6SMichael Halcrow 	int i;
730237fead6SMichael Halcrow 
7318c0a8537SKirill A. Shutemov 	/*
7328c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
7338c0a8537SKirill A. Shutemov 	 * destroy cache.
7348c0a8537SKirill A. Shutemov 	 */
7358c0a8537SKirill A. Shutemov 	rcu_barrier();
7368c0a8537SKirill A. Shutemov 
737237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
738237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
739237fead6SMichael Halcrow 
740237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
741237fead6SMichael Halcrow 		if (*(info->cache))
742237fead6SMichael Halcrow 			kmem_cache_destroy(*(info->cache));
743237fead6SMichael Halcrow 	}
744237fead6SMichael Halcrow }
745237fead6SMichael Halcrow 
746237fead6SMichael Halcrow /**
747237fead6SMichael Halcrow  * ecryptfs_init_kmem_caches
748237fead6SMichael Halcrow  *
749237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
750237fead6SMichael Halcrow  */
751237fead6SMichael Halcrow static int ecryptfs_init_kmem_caches(void)
752237fead6SMichael Halcrow {
753237fead6SMichael Halcrow 	int i;
754237fead6SMichael Halcrow 
755237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
756237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
757237fead6SMichael Halcrow 
758237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
7595d097056SVladimir Davydov 		*(info->cache) = kmem_cache_create(info->name, info->size, 0,
7605d097056SVladimir Davydov 				SLAB_HWCACHE_ALIGN | info->flags, info->ctor);
761237fead6SMichael Halcrow 		if (!*(info->cache)) {
762237fead6SMichael Halcrow 			ecryptfs_free_kmem_caches();
763237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "%s: "
764237fead6SMichael Halcrow 					"kmem_cache_create failed\n",
765237fead6SMichael Halcrow 					info->name);
766237fead6SMichael Halcrow 			return -ENOMEM;
767237fead6SMichael Halcrow 		}
768237fead6SMichael Halcrow 	}
769237fead6SMichael Halcrow 	return 0;
770237fead6SMichael Halcrow }
771237fead6SMichael Halcrow 
7726e90aa97SGreg Kroah-Hartman static struct kobject *ecryptfs_kobj;
773237fead6SMichael Halcrow 
774386f275fSKay Sievers static ssize_t version_show(struct kobject *kobj,
775386f275fSKay Sievers 			    struct kobj_attribute *attr, char *buff)
776237fead6SMichael Halcrow {
777237fead6SMichael Halcrow 	return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
778237fead6SMichael Halcrow }
779237fead6SMichael Halcrow 
780386f275fSKay Sievers static struct kobj_attribute version_attr = __ATTR_RO(version);
781237fead6SMichael Halcrow 
78230a468b1SGreg Kroah-Hartman static struct attribute *attributes[] = {
78330a468b1SGreg Kroah-Hartman 	&version_attr.attr,
78430a468b1SGreg Kroah-Hartman 	NULL,
78530a468b1SGreg Kroah-Hartman };
78630a468b1SGreg Kroah-Hartman 
78730a468b1SGreg Kroah-Hartman static struct attribute_group attr_group = {
78830a468b1SGreg Kroah-Hartman 	.attrs = attributes,
78930a468b1SGreg Kroah-Hartman };
790237fead6SMichael Halcrow 
791237fead6SMichael Halcrow static int do_sysfs_registration(void)
792237fead6SMichael Halcrow {
793237fead6SMichael Halcrow 	int rc;
794237fead6SMichael Halcrow 
7956e90aa97SGreg Kroah-Hartman 	ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
7966e90aa97SGreg Kroah-Hartman 	if (!ecryptfs_kobj) {
797917e865dSGreg Kroah-Hartman 		printk(KERN_ERR "Unable to create ecryptfs kset\n");
798917e865dSGreg Kroah-Hartman 		rc = -ENOMEM;
799237fead6SMichael Halcrow 		goto out;
800237fead6SMichael Halcrow 	}
8016e90aa97SGreg Kroah-Hartman 	rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
802237fead6SMichael Halcrow 	if (rc) {
803237fead6SMichael Halcrow 		printk(KERN_ERR
80430a468b1SGreg Kroah-Hartman 		       "Unable to create ecryptfs version attributes\n");
805197b12d6SGreg Kroah-Hartman 		kobject_put(ecryptfs_kobj);
806237fead6SMichael Halcrow 	}
807237fead6SMichael Halcrow out:
808237fead6SMichael Halcrow 	return rc;
809237fead6SMichael Halcrow }
810237fead6SMichael Halcrow 
811a75de1b3SRyusuke Konishi static void do_sysfs_unregistration(void)
812a75de1b3SRyusuke Konishi {
8136e90aa97SGreg Kroah-Hartman 	sysfs_remove_group(ecryptfs_kobj, &attr_group);
814197b12d6SGreg Kroah-Hartman 	kobject_put(ecryptfs_kobj);
815a75de1b3SRyusuke Konishi }
816a75de1b3SRyusuke Konishi 
817237fead6SMichael Halcrow static int __init ecryptfs_init(void)
818237fead6SMichael Halcrow {
819237fead6SMichael Halcrow 	int rc;
820237fead6SMichael Halcrow 
821*09cbfeafSKirill A. Shutemov 	if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_SIZE) {
822237fead6SMichael Halcrow 		rc = -EINVAL;
823237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
824237fead6SMichael Halcrow 				"larger than the host's page size, and so "
825237fead6SMichael Halcrow 				"eCryptfs cannot run on this system. The "
826888d57bbSJoe Perches 				"default eCryptfs extent size is [%u] bytes; "
827888d57bbSJoe Perches 				"the page size is [%lu] bytes.\n",
828888d57bbSJoe Perches 				ECRYPTFS_DEFAULT_EXTENT_SIZE,
829*09cbfeafSKirill A. Shutemov 				(unsigned long)PAGE_SIZE);
830237fead6SMichael Halcrow 		goto out;
831237fead6SMichael Halcrow 	}
832237fead6SMichael Halcrow 	rc = ecryptfs_init_kmem_caches();
833237fead6SMichael Halcrow 	if (rc) {
834237fead6SMichael Halcrow 		printk(KERN_ERR
835237fead6SMichael Halcrow 		       "Failed to allocate one or more kmem_cache objects\n");
836237fead6SMichael Halcrow 		goto out;
837237fead6SMichael Halcrow 	}
838237fead6SMichael Halcrow 	rc = do_sysfs_registration();
839237fead6SMichael Halcrow 	if (rc) {
840237fead6SMichael Halcrow 		printk(KERN_ERR "sysfs registration failed\n");
8410794f569SAl Viro 		goto out_free_kmem_caches;
842237fead6SMichael Halcrow 	}
843746f1e55SMichael Halcrow 	rc = ecryptfs_init_kthread();
844746f1e55SMichael Halcrow 	if (rc) {
845746f1e55SMichael Halcrow 		printk(KERN_ERR "%s: kthread initialization failed; "
846746f1e55SMichael Halcrow 		       "rc = [%d]\n", __func__, rc);
847746f1e55SMichael Halcrow 		goto out_do_sysfs_unregistration;
848746f1e55SMichael Halcrow 	}
849624ae528STyler Hicks 	rc = ecryptfs_init_messaging();
850dddfa461SMichael Halcrow 	if (rc) {
85125985edcSLucas De Marchi 		printk(KERN_ERR "Failure occurred while attempting to "
852624ae528STyler Hicks 				"initialize the communications channel to "
853624ae528STyler Hicks 				"ecryptfsd\n");
854746f1e55SMichael Halcrow 		goto out_destroy_kthread;
855956159c3SMichael Halcrow 	}
856956159c3SMichael Halcrow 	rc = ecryptfs_init_crypto();
857956159c3SMichael Halcrow 	if (rc) {
858956159c3SMichael Halcrow 		printk(KERN_ERR "Failure whilst attempting to init crypto; "
859956159c3SMichael Halcrow 		       "rc = [%d]\n", rc);
860cf81f89dSMichael Halcrow 		goto out_release_messaging;
861dddfa461SMichael Halcrow 	}
8620794f569SAl Viro 	rc = register_filesystem(&ecryptfs_fs_type);
8630794f569SAl Viro 	if (rc) {
8640794f569SAl Viro 		printk(KERN_ERR "Failed to register filesystem\n");
8650794f569SAl Viro 		goto out_destroy_crypto;
8660794f569SAl Viro 	}
8672830bfd6SEric Sandeen 	if (ecryptfs_verbosity > 0)
8682830bfd6SEric Sandeen 		printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
8692830bfd6SEric Sandeen 			"will be written to the syslog!\n", ecryptfs_verbosity);
8702830bfd6SEric Sandeen 
871cf81f89dSMichael Halcrow 	goto out;
8720794f569SAl Viro out_destroy_crypto:
8730794f569SAl Viro 	ecryptfs_destroy_crypto();
874cf81f89dSMichael Halcrow out_release_messaging:
875624ae528STyler Hicks 	ecryptfs_release_messaging();
876746f1e55SMichael Halcrow out_destroy_kthread:
877746f1e55SMichael Halcrow 	ecryptfs_destroy_kthread();
878cf81f89dSMichael Halcrow out_do_sysfs_unregistration:
879cf81f89dSMichael Halcrow 	do_sysfs_unregistration();
880cf81f89dSMichael Halcrow out_free_kmem_caches:
881cf81f89dSMichael Halcrow 	ecryptfs_free_kmem_caches();
882237fead6SMichael Halcrow out:
883237fead6SMichael Halcrow 	return rc;
884237fead6SMichael Halcrow }
885237fead6SMichael Halcrow 
886237fead6SMichael Halcrow static void __exit ecryptfs_exit(void)
887237fead6SMichael Halcrow {
888cf81f89dSMichael Halcrow 	int rc;
889cf81f89dSMichael Halcrow 
890cf81f89dSMichael Halcrow 	rc = ecryptfs_destroy_crypto();
891cf81f89dSMichael Halcrow 	if (rc)
892cf81f89dSMichael Halcrow 		printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
893cf81f89dSMichael Halcrow 		       "rc = [%d]\n", rc);
894624ae528STyler Hicks 	ecryptfs_release_messaging();
895746f1e55SMichael Halcrow 	ecryptfs_destroy_kthread();
896cf81f89dSMichael Halcrow 	do_sysfs_unregistration();
897237fead6SMichael Halcrow 	unregister_filesystem(&ecryptfs_fs_type);
898237fead6SMichael Halcrow 	ecryptfs_free_kmem_caches();
899237fead6SMichael Halcrow }
900237fead6SMichael Halcrow 
901237fead6SMichael Halcrow MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
902237fead6SMichael Halcrow MODULE_DESCRIPTION("eCryptfs");
903237fead6SMichael Halcrow 
904237fead6SMichael Halcrow MODULE_LICENSE("GPL");
905237fead6SMichael Halcrow 
906237fead6SMichael Halcrow module_init(ecryptfs_init)
907237fead6SMichael Halcrow module_exit(ecryptfs_exit)
908