xref: /linux/fs/ecryptfs/main.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e2401206SLee Jones /*
3237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
4237fead6SMichael Halcrow  *
5237fead6SMichael Halcrow  * Copyright (C) 1997-2003 Erez Zadok
6237fead6SMichael Halcrow  * Copyright (C) 2001-2003 Stony Brook University
7dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
8237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9237fead6SMichael Halcrow  *              Michael C. Thompson <mcthomps@us.ibm.com>
10f8e48a84STyler Hicks  *              Tyler Hicks <code@tyhicks.com>
11237fead6SMichael Halcrow  */
12237fead6SMichael Halcrow 
13237fead6SMichael Halcrow #include <linux/dcache.h>
14237fead6SMichael Halcrow #include <linux/file.h>
15237fead6SMichael Halcrow #include <linux/module.h>
16237fead6SMichael Halcrow #include <linux/namei.h>
17237fead6SMichael Halcrow #include <linux/skbuff.h>
18237fead6SMichael Halcrow #include <linux/mount.h>
19237fead6SMichael Halcrow #include <linux/pagemap.h>
20237fead6SMichael Halcrow #include <linux/key.h>
21237fead6SMichael Halcrow #include <linux/parser.h>
220cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
24070baa51SRoberto Sassu #include <linux/magic.h>
25237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
26237fead6SMichael Halcrow 
27e2401206SLee Jones /*
28237fead6SMichael Halcrow  * Module parameter that defines the ecryptfs_verbosity level.
29237fead6SMichael Halcrow  */
30237fead6SMichael Halcrow int ecryptfs_verbosity = 0;
31237fead6SMichael Halcrow 
32237fead6SMichael Halcrow module_param(ecryptfs_verbosity, int, 0);
33237fead6SMichael Halcrow MODULE_PARM_DESC(ecryptfs_verbosity,
34237fead6SMichael Halcrow 		 "Initial verbosity level (0 or 1; defaults to "
35237fead6SMichael Halcrow 		 "0, which is Quiet)");
36237fead6SMichael Halcrow 
37e2401206SLee Jones /*
38624ae528STyler Hicks  * Module parameter that defines the number of message buffer elements
39dddfa461SMichael Halcrow  */
40dddfa461SMichael Halcrow unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
41dddfa461SMichael Halcrow 
42dddfa461SMichael Halcrow module_param(ecryptfs_message_buf_len, uint, 0);
43dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_message_buf_len,
44dddfa461SMichael Halcrow 		 "Number of message buffer elements");
45dddfa461SMichael Halcrow 
46e2401206SLee Jones /*
47dddfa461SMichael Halcrow  * Module parameter that defines the maximum guaranteed amount of time to wait
48624ae528STyler Hicks  * for a response from ecryptfsd.  The actual sleep time will be, more than
49dddfa461SMichael Halcrow  * likely, a small amount greater than this specified value, but only less if
50624ae528STyler Hicks  * the message successfully arrives.
51dddfa461SMichael Halcrow  */
52dddfa461SMichael Halcrow signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
53dddfa461SMichael Halcrow 
54dddfa461SMichael Halcrow module_param(ecryptfs_message_wait_timeout, long, 0);
55dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
56dddfa461SMichael Halcrow 		 "Maximum number of seconds that an operation will "
57dddfa461SMichael Halcrow 		 "sleep while waiting for a message response from "
58dddfa461SMichael Halcrow 		 "userspace");
59dddfa461SMichael Halcrow 
60e2401206SLee Jones /*
61dddfa461SMichael Halcrow  * Module parameter that is an estimate of the maximum number of users
62dddfa461SMichael Halcrow  * that will be concurrently using eCryptfs. Set this to the right
63dddfa461SMichael Halcrow  * value to balance performance and memory use.
64dddfa461SMichael Halcrow  */
65dddfa461SMichael Halcrow unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
66dddfa461SMichael Halcrow 
67dddfa461SMichael Halcrow module_param(ecryptfs_number_of_users, uint, 0);
68dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
69dddfa461SMichael Halcrow 		 "concurrent users of eCryptfs");
70dddfa461SMichael Halcrow 
__ecryptfs_printk(const char * fmt,...)71237fead6SMichael Halcrow void __ecryptfs_printk(const char *fmt, ...)
72237fead6SMichael Halcrow {
73237fead6SMichael Halcrow 	va_list args;
74237fead6SMichael Halcrow 	va_start(args, fmt);
75237fead6SMichael Halcrow 	if (fmt[1] == '7') { /* KERN_DEBUG */
76237fead6SMichael Halcrow 		if (ecryptfs_verbosity >= 1)
77237fead6SMichael Halcrow 			vprintk(fmt, args);
78237fead6SMichael Halcrow 	} else
79237fead6SMichael Halcrow 		vprintk(fmt, args);
80237fead6SMichael Halcrow 	va_end(args);
81237fead6SMichael Halcrow }
82237fead6SMichael Halcrow 
83e2401206SLee Jones /*
84332ab16fSTyler Hicks  * ecryptfs_init_lower_file
854981e081SMichael Halcrow  * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
864981e081SMichael Halcrow  *                   the lower dentry and the lower mount set
874981e081SMichael Halcrow  *
884981e081SMichael Halcrow  * eCryptfs only ever keeps a single open file for every lower
894981e081SMichael Halcrow  * inode. All I/O operations to the lower inode occur through that
904981e081SMichael Halcrow  * file. When the first eCryptfs dentry that interposes with the first
914981e081SMichael Halcrow  * lower dentry for that inode is created, this function creates the
92332ab16fSTyler Hicks  * lower file struct and associates it with the eCryptfs
93332ab16fSTyler Hicks  * inode. When all eCryptfs files associated with the inode are released, the
94332ab16fSTyler Hicks  * file is closed.
954981e081SMichael Halcrow  *
96332ab16fSTyler Hicks  * The lower file will be opened with read/write permissions, if
974981e081SMichael Halcrow  * possible. Otherwise, it is opened read-only.
984981e081SMichael Halcrow  *
99332ab16fSTyler Hicks  * This function does nothing if a lower file is already
1004981e081SMichael Halcrow  * associated with the eCryptfs inode.
1014981e081SMichael Halcrow  *
1024981e081SMichael Halcrow  * Returns zero on success; non-zero otherwise
1034981e081SMichael Halcrow  */
ecryptfs_init_lower_file(struct dentry * dentry,struct file ** lower_file)104332ab16fSTyler Hicks static int ecryptfs_init_lower_file(struct dentry *dentry,
105332ab16fSTyler Hicks 				    struct file **lower_file)
1064981e081SMichael Halcrow {
107745ca247SDavid Howells 	const struct cred *cred = current_cred();
10888569546SAl Viro 	const struct path *path = ecryptfs_dentry_to_lower_path(dentry);
109332ab16fSTyler Hicks 	int rc;
1104981e081SMichael Halcrow 
111cc18ec3cSMatthew Wilcox 	rc = ecryptfs_privileged_open(lower_file, path->dentry, path->mnt,
112332ab16fSTyler Hicks 				      cred);
113ac22ba23STyler Hicks 	if (rc) {
114332ab16fSTyler Hicks 		printk(KERN_ERR "Error opening lower file "
115746f1e55SMichael Halcrow 		       "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
116cc18ec3cSMatthew Wilcox 		       "rc = [%d]\n", path->dentry, path->mnt, rc);
117332ab16fSTyler Hicks 		(*lower_file) = NULL;
1184981e081SMichael Halcrow 	}
1194981e081SMichael Halcrow 	return rc;
1204981e081SMichael Halcrow }
1214981e081SMichael Halcrow 
ecryptfs_get_lower_file(struct dentry * dentry,struct inode * inode)1223b06b3ebSTyler Hicks int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
123332ab16fSTyler Hicks {
1243b06b3ebSTyler Hicks 	struct ecryptfs_inode_info *inode_info;
125332ab16fSTyler Hicks 	int count, rc = 0;
126332ab16fSTyler Hicks 
1273b06b3ebSTyler Hicks 	inode_info = ecryptfs_inode_to_private(inode);
128332ab16fSTyler Hicks 	mutex_lock(&inode_info->lower_file_mutex);
129332ab16fSTyler Hicks 	count = atomic_inc_return(&inode_info->lower_file_count);
130332ab16fSTyler Hicks 	if (WARN_ON_ONCE(count < 1))
131332ab16fSTyler Hicks 		rc = -EINVAL;
132332ab16fSTyler Hicks 	else if (count == 1) {
133332ab16fSTyler Hicks 		rc = ecryptfs_init_lower_file(dentry,
134332ab16fSTyler Hicks 					      &inode_info->lower_file);
135332ab16fSTyler Hicks 		if (rc)
136332ab16fSTyler Hicks 			atomic_set(&inode_info->lower_file_count, 0);
137332ab16fSTyler Hicks 	}
138332ab16fSTyler Hicks 	mutex_unlock(&inode_info->lower_file_mutex);
139332ab16fSTyler Hicks 	return rc;
140332ab16fSTyler Hicks }
141332ab16fSTyler Hicks 
ecryptfs_put_lower_file(struct inode * inode)142332ab16fSTyler Hicks void ecryptfs_put_lower_file(struct inode *inode)
143332ab16fSTyler Hicks {
144332ab16fSTyler Hicks 	struct ecryptfs_inode_info *inode_info;
145332ab16fSTyler Hicks 
146332ab16fSTyler Hicks 	inode_info = ecryptfs_inode_to_private(inode);
147332ab16fSTyler Hicks 	if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
148332ab16fSTyler Hicks 				      &inode_info->lower_file_mutex)) {
1497149f255STyler Hicks 		filemap_write_and_wait(inode->i_mapping);
150332ab16fSTyler Hicks 		fput(inode_info->lower_file);
151332ab16fSTyler Hicks 		inode_info->lower_file = NULL;
152332ab16fSTyler Hicks 		mutex_unlock(&inode_info->lower_file_mutex);
153332ab16fSTyler Hicks 	}
154332ab16fSTyler Hicks }
155332ab16fSTyler Hicks 
1562830bfd6SEric Sandeen enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
1572830bfd6SEric Sandeen        ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
1582830bfd6SEric Sandeen        ecryptfs_opt_ecryptfs_key_bytes,
15917398957SMichael Halcrow        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
16087c94c4dSMichael Halcrow        ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
16187c94c4dSMichael Halcrow        ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
162f16feb51SRoberto Sassu        ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
16376435548SJohn Johansen        ecryptfs_opt_check_dev_ruid,
164f16feb51SRoberto Sassu        ecryptfs_opt_err };
165237fead6SMichael Halcrow 
166a447c093SSteven Whitehouse static const match_table_t tokens = {
167237fead6SMichael Halcrow 	{ecryptfs_opt_sig, "sig=%s"},
168237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
169237fead6SMichael Halcrow 	{ecryptfs_opt_cipher, "cipher=%s"},
170237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
171237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
172237fead6SMichael Halcrow 	{ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
17317398957SMichael Halcrow 	{ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
17417398957SMichael Halcrow 	{ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
17587c94c4dSMichael Halcrow 	{ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
17687c94c4dSMichael Halcrow 	{ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
17787c94c4dSMichael Halcrow 	{ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
178e77cc8d2STyler Hicks 	{ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
179f16feb51SRoberto Sassu 	{ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
18076435548SJohn Johansen 	{ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
181237fead6SMichael Halcrow 	{ecryptfs_opt_err, NULL}
182237fead6SMichael Halcrow };
183237fead6SMichael Halcrow 
ecryptfs_init_global_auth_toks(struct ecryptfs_mount_crypt_stat * mount_crypt_stat)184f4aad16aSMichael Halcrow static int ecryptfs_init_global_auth_toks(
185f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
186237fead6SMichael Halcrow {
187f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
1880e1fc5efSRoberto Sassu 	struct ecryptfs_auth_tok *auth_tok;
189237fead6SMichael Halcrow 	int rc = 0;
190237fead6SMichael Halcrow 
191f4aad16aSMichael Halcrow 	list_for_each_entry(global_auth_tok,
192f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
193f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
1945dda6992SMichael Halcrow 		rc = ecryptfs_keyring_auth_tok_for_sig(
1950e1fc5efSRoberto Sassu 			&global_auth_tok->global_auth_tok_key, &auth_tok,
1965dda6992SMichael Halcrow 			global_auth_tok->sig);
1975dda6992SMichael Halcrow 		if (rc) {
198f4aad16aSMichael Halcrow 			printk(KERN_ERR "Could not find valid key in user "
199f4aad16aSMichael Halcrow 			       "session keyring for sig specified in mount "
200f4aad16aSMichael Halcrow 			       "option: [%s]\n", global_auth_tok->sig);
201f4aad16aSMichael Halcrow 			global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
202982363c9SEric Sandeen 			goto out;
203b5695d04SRoberto Sassu 		} else {
204f4aad16aSMichael Halcrow 			global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
205b5695d04SRoberto Sassu 			up_write(&(global_auth_tok->global_auth_tok_key)->sem);
206b5695d04SRoberto Sassu 		}
207237fead6SMichael Halcrow 	}
208982363c9SEric Sandeen out:
209237fead6SMichael Halcrow 	return rc;
210237fead6SMichael Halcrow }
211237fead6SMichael Halcrow 
ecryptfs_init_mount_crypt_stat(struct ecryptfs_mount_crypt_stat * mount_crypt_stat)212f4aad16aSMichael Halcrow static void ecryptfs_init_mount_crypt_stat(
213f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
214f4aad16aSMichael Halcrow {
215f4aad16aSMichael Halcrow 	memset((void *)mount_crypt_stat, 0,
216f4aad16aSMichael Halcrow 	       sizeof(struct ecryptfs_mount_crypt_stat));
217f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
218f4aad16aSMichael Halcrow 	mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
219f4aad16aSMichael Halcrow 	mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
220f4aad16aSMichael Halcrow }
221f4aad16aSMichael Halcrow 
222237fead6SMichael Halcrow /**
223237fead6SMichael Halcrow  * ecryptfs_parse_options
224e2401206SLee Jones  * @sbi: The ecryptfs super block
22525985edcSLucas De Marchi  * @options: The options passed to the kernel
22676435548SJohn Johansen  * @check_ruid: set to 1 if device uid should be checked against the ruid
227237fead6SMichael Halcrow  *
228237fead6SMichael Halcrow  * Parse mount options:
229237fead6SMichael Halcrow  * debug=N 	   - ecryptfs_verbosity level for debug output
230237fead6SMichael Halcrow  * sig=XXX	   - description(signature) of the key to use
231237fead6SMichael Halcrow  *
232237fead6SMichael Halcrow  * Returns the dentry object of the lower-level (lower/interposed)
233237fead6SMichael Halcrow  * directory; We want to mount our stackable file system on top of
234237fead6SMichael Halcrow  * that lower directory.
235237fead6SMichael Halcrow  *
236237fead6SMichael Halcrow  * The signature of the key to use must be the description of a key
237237fead6SMichael Halcrow  * already in the keyring. Mounting will fail if the key can not be
238237fead6SMichael Halcrow  * found.
239237fead6SMichael Halcrow  *
240237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
241237fead6SMichael Halcrow  */
ecryptfs_parse_options(struct ecryptfs_sb_info * sbi,char * options,uid_t * check_ruid)24276435548SJohn Johansen static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
24376435548SJohn Johansen 				  uid_t *check_ruid)
244237fead6SMichael Halcrow {
245237fead6SMichael Halcrow 	char *p;
246237fead6SMichael Halcrow 	int rc = 0;
247237fead6SMichael Halcrow 	int sig_set = 0;
248237fead6SMichael Halcrow 	int cipher_name_set = 0;
24987c94c4dSMichael Halcrow 	int fn_cipher_name_set = 0;
250237fead6SMichael Halcrow 	int cipher_key_bytes;
251237fead6SMichael Halcrow 	int cipher_key_bytes_set = 0;
25287c94c4dSMichael Halcrow 	int fn_cipher_key_bytes;
25387c94c4dSMichael Halcrow 	int fn_cipher_key_bytes_set = 0;
254237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2552ccde7c6SAl Viro 		&sbi->mount_crypt_stat;
256237fead6SMichael Halcrow 	substring_t args[MAX_OPT_ARGS];
257237fead6SMichael Halcrow 	int token;
258237fead6SMichael Halcrow 	char *sig_src;
259237fead6SMichael Halcrow 	char *cipher_name_src;
26087c94c4dSMichael Halcrow 	char *fn_cipher_name_src;
26187c94c4dSMichael Halcrow 	char *fnek_src;
262237fead6SMichael Halcrow 	char *cipher_key_bytes_src;
26387c94c4dSMichael Halcrow 	char *fn_cipher_key_bytes_src;
2645f5b331dSTim Sally 	u8 cipher_code;
265237fead6SMichael Halcrow 
26676435548SJohn Johansen 	*check_ruid = 0;
26776435548SJohn Johansen 
268237fead6SMichael Halcrow 	if (!options) {
269237fead6SMichael Halcrow 		rc = -EINVAL;
270237fead6SMichael Halcrow 		goto out;
271237fead6SMichael Halcrow 	}
272956159c3SMichael Halcrow 	ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
273237fead6SMichael Halcrow 	while ((p = strsep(&options, ",")) != NULL) {
274237fead6SMichael Halcrow 		if (!*p)
275237fead6SMichael Halcrow 			continue;
276237fead6SMichael Halcrow 		token = match_token(p, tokens, args);
277237fead6SMichael Halcrow 		switch (token) {
278237fead6SMichael Halcrow 		case ecryptfs_opt_sig:
279237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_sig:
280237fead6SMichael Halcrow 			sig_src = args[0].from;
281f4aad16aSMichael Halcrow 			rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
28284814d64STyler Hicks 							  sig_src, 0);
283f4aad16aSMichael Halcrow 			if (rc) {
284f4aad16aSMichael Halcrow 				printk(KERN_ERR "Error attempting to register "
285f4aad16aSMichael Halcrow 				       "global sig; rc = [%d]\n", rc);
286f4aad16aSMichael Halcrow 				goto out;
287f4aad16aSMichael Halcrow 			}
288237fead6SMichael Halcrow 			sig_set = 1;
289237fead6SMichael Halcrow 			break;
290237fead6SMichael Halcrow 		case ecryptfs_opt_cipher:
291237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_cipher:
292237fead6SMichael Halcrow 			cipher_name_src = args[0].from;
293*f700b719SJustin Stitt 			strscpy(mount_crypt_stat->global_default_cipher_name,
294*f700b719SJustin Stitt 				cipher_name_src);
295237fead6SMichael Halcrow 			cipher_name_set = 1;
296237fead6SMichael Halcrow 			break;
297237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_key_bytes:
298237fead6SMichael Halcrow 			cipher_key_bytes_src = args[0].from;
299237fead6SMichael Halcrow 			cipher_key_bytes =
300237fead6SMichael Halcrow 				(int)simple_strtol(cipher_key_bytes_src,
301237fead6SMichael Halcrow 						   &cipher_key_bytes_src, 0);
302237fead6SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size =
303237fead6SMichael Halcrow 				cipher_key_bytes;
304237fead6SMichael Halcrow 			cipher_key_bytes_set = 1;
305237fead6SMichael Halcrow 			break;
306237fead6SMichael Halcrow 		case ecryptfs_opt_passthrough:
307237fead6SMichael Halcrow 			mount_crypt_stat->flags |=
308237fead6SMichael Halcrow 				ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
309237fead6SMichael Halcrow 			break;
31017398957SMichael Halcrow 		case ecryptfs_opt_xattr_metadata:
31117398957SMichael Halcrow 			mount_crypt_stat->flags |=
31217398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
31317398957SMichael Halcrow 			break;
31417398957SMichael Halcrow 		case ecryptfs_opt_encrypted_view:
31517398957SMichael Halcrow 			mount_crypt_stat->flags |=
31617398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
31717398957SMichael Halcrow 			mount_crypt_stat->flags |=
31817398957SMichael Halcrow 				ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
31917398957SMichael Halcrow 			break;
32087c94c4dSMichael Halcrow 		case ecryptfs_opt_fnek_sig:
32187c94c4dSMichael Halcrow 			fnek_src = args[0].from;
322*f700b719SJustin Stitt 			strscpy(mount_crypt_stat->global_default_fnek_sig,
323*f700b719SJustin Stitt 				fnek_src);
32487c94c4dSMichael Halcrow 			rc = ecryptfs_add_global_auth_tok(
32587c94c4dSMichael Halcrow 				mount_crypt_stat,
32684814d64STyler Hicks 				mount_crypt_stat->global_default_fnek_sig,
32784814d64STyler Hicks 				ECRYPTFS_AUTH_TOK_FNEK);
32887c94c4dSMichael Halcrow 			if (rc) {
32987c94c4dSMichael Halcrow 				printk(KERN_ERR "Error attempting to register "
33087c94c4dSMichael Halcrow 				       "global fnek sig [%s]; rc = [%d]\n",
33187c94c4dSMichael Halcrow 				       mount_crypt_stat->global_default_fnek_sig,
33287c94c4dSMichael Halcrow 				       rc);
33387c94c4dSMichael Halcrow 				goto out;
33487c94c4dSMichael Halcrow 			}
33587c94c4dSMichael Halcrow 			mount_crypt_stat->flags |=
33687c94c4dSMichael Halcrow 				(ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
33787c94c4dSMichael Halcrow 				 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
33887c94c4dSMichael Halcrow 			break;
33987c94c4dSMichael Halcrow 		case ecryptfs_opt_fn_cipher:
34087c94c4dSMichael Halcrow 			fn_cipher_name_src = args[0].from;
341*f700b719SJustin Stitt 			strscpy(mount_crypt_stat->global_default_fn_cipher_name,
342*f700b719SJustin Stitt 				fn_cipher_name_src);
34387c94c4dSMichael Halcrow 			fn_cipher_name_set = 1;
34487c94c4dSMichael Halcrow 			break;
34587c94c4dSMichael Halcrow 		case ecryptfs_opt_fn_cipher_key_bytes:
34687c94c4dSMichael Halcrow 			fn_cipher_key_bytes_src = args[0].from;
34787c94c4dSMichael Halcrow 			fn_cipher_key_bytes =
34887c94c4dSMichael Halcrow 				(int)simple_strtol(fn_cipher_key_bytes_src,
34987c94c4dSMichael Halcrow 						   &fn_cipher_key_bytes_src, 0);
35087c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fn_cipher_key_bytes =
35187c94c4dSMichael Halcrow 				fn_cipher_key_bytes;
35287c94c4dSMichael Halcrow 			fn_cipher_key_bytes_set = 1;
35387c94c4dSMichael Halcrow 			break;
354e77cc8d2STyler Hicks 		case ecryptfs_opt_unlink_sigs:
355e77cc8d2STyler Hicks 			mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
356e77cc8d2STyler Hicks 			break;
357f16feb51SRoberto Sassu 		case ecryptfs_opt_mount_auth_tok_only:
358f16feb51SRoberto Sassu 			mount_crypt_stat->flags |=
359f16feb51SRoberto Sassu 				ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
360f16feb51SRoberto Sassu 			break;
36176435548SJohn Johansen 		case ecryptfs_opt_check_dev_ruid:
36276435548SJohn Johansen 			*check_ruid = 1;
36376435548SJohn Johansen 			break;
364237fead6SMichael Halcrow 		case ecryptfs_opt_err:
365237fead6SMichael Halcrow 		default:
36687c94c4dSMichael Halcrow 			printk(KERN_WARNING
36787c94c4dSMichael Halcrow 			       "%s: eCryptfs: unrecognized option [%s]\n",
36887c94c4dSMichael Halcrow 			       __func__, p);
369237fead6SMichael Halcrow 		}
370237fead6SMichael Halcrow 	}
371237fead6SMichael Halcrow 	if (!sig_set) {
372237fead6SMichael Halcrow 		rc = -EINVAL;
373956159c3SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
374956159c3SMichael Halcrow 				"auth tok signature as a mount "
375237fead6SMichael Halcrow 				"parameter; see the eCryptfs README\n");
376237fead6SMichael Halcrow 		goto out;
377237fead6SMichael Halcrow 	}
378237fead6SMichael Halcrow 	if (!cipher_name_set) {
3798f236809SMiklos Szeredi 		int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
3808f236809SMiklos Szeredi 
3812a559a8bSColin Ian King 		BUG_ON(cipher_name_len > ECRYPTFS_MAX_CIPHER_NAME_SIZE);
3828f236809SMiklos Szeredi 		strcpy(mount_crypt_stat->global_default_cipher_name,
3838f236809SMiklos Szeredi 		       ECRYPTFS_DEFAULT_CIPHER);
384237fead6SMichael Halcrow 	}
38587c94c4dSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
38687c94c4dSMichael Halcrow 	    && !fn_cipher_name_set)
38787c94c4dSMichael Halcrow 		strcpy(mount_crypt_stat->global_default_fn_cipher_name,
38887c94c4dSMichael Halcrow 		       mount_crypt_stat->global_default_cipher_name);
38987c94c4dSMichael Halcrow 	if (!cipher_key_bytes_set)
390e5d9cbdeSMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size = 0;
39187c94c4dSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
39287c94c4dSMichael Halcrow 	    && !fn_cipher_key_bytes_set)
39387c94c4dSMichael Halcrow 		mount_crypt_stat->global_default_fn_cipher_key_bytes =
39487c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size;
3955f5b331dSTim Sally 
3965f5b331dSTim Sally 	cipher_code = ecryptfs_code_for_cipher_string(
3975f5b331dSTim Sally 		mount_crypt_stat->global_default_cipher_name,
3985f5b331dSTim Sally 		mount_crypt_stat->global_default_cipher_key_size);
3995f5b331dSTim Sally 	if (!cipher_code) {
4005f5b331dSTim Sally 		ecryptfs_printk(KERN_ERR,
4010996b67dSColin Ian King 				"eCryptfs doesn't support cipher: %s\n",
4025f5b331dSTim Sally 				mount_crypt_stat->global_default_cipher_name);
4035f5b331dSTim Sally 		rc = -EINVAL;
4045f5b331dSTim Sally 		goto out;
4055f5b331dSTim Sally 	}
4065f5b331dSTim Sally 
407af440f52SEric Sandeen 	mutex_lock(&key_tfm_list_mutex);
408af440f52SEric Sandeen 	if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
40987c94c4dSMichael Halcrow 				 NULL)) {
4105dda6992SMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(
411f4aad16aSMichael Halcrow 			NULL, mount_crypt_stat->global_default_cipher_name,
4125dda6992SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size);
4135dda6992SMichael Halcrow 		if (rc) {
41487c94c4dSMichael Halcrow 			printk(KERN_ERR "Error attempting to initialize "
41587c94c4dSMichael Halcrow 			       "cipher with name = [%s] and key size = [%td]; "
41687c94c4dSMichael Halcrow 			       "rc = [%d]\n",
417237fead6SMichael Halcrow 			       mount_crypt_stat->global_default_cipher_name,
41887c94c4dSMichael Halcrow 			       mount_crypt_stat->global_default_cipher_key_size,
41987c94c4dSMichael Halcrow 			       rc);
420237fead6SMichael Halcrow 			rc = -EINVAL;
42187c94c4dSMichael Halcrow 			mutex_unlock(&key_tfm_list_mutex);
422237fead6SMichael Halcrow 			goto out;
423237fead6SMichael Halcrow 		}
42487c94c4dSMichael Halcrow 	}
42587c94c4dSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
42687c94c4dSMichael Halcrow 	    && !ecryptfs_tfm_exists(
42787c94c4dSMichael Halcrow 		    mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
42887c94c4dSMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(
42987c94c4dSMichael Halcrow 			NULL, mount_crypt_stat->global_default_fn_cipher_name,
43087c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fn_cipher_key_bytes);
4315dda6992SMichael Halcrow 		if (rc) {
43287c94c4dSMichael Halcrow 			printk(KERN_ERR "Error attempting to initialize "
43387c94c4dSMichael Halcrow 			       "cipher with name = [%s] and key size = [%td]; "
43487c94c4dSMichael Halcrow 			       "rc = [%d]\n",
43587c94c4dSMichael Halcrow 			       mount_crypt_stat->global_default_fn_cipher_name,
43687c94c4dSMichael Halcrow 			       mount_crypt_stat->global_default_fn_cipher_key_bytes,
43787c94c4dSMichael Halcrow 			       rc);
43887c94c4dSMichael Halcrow 			rc = -EINVAL;
43987c94c4dSMichael Halcrow 			mutex_unlock(&key_tfm_list_mutex);
44087c94c4dSMichael Halcrow 			goto out;
44187c94c4dSMichael Halcrow 		}
44287c94c4dSMichael Halcrow 	}
44387c94c4dSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
44487c94c4dSMichael Halcrow 	rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
44587c94c4dSMichael Halcrow 	if (rc)
446f4aad16aSMichael Halcrow 		printk(KERN_WARNING "One or more global auth toks could not "
447f4aad16aSMichael Halcrow 		       "properly register; rc = [%d]\n", rc);
448237fead6SMichael Halcrow out:
449237fead6SMichael Halcrow 	return rc;
450237fead6SMichael Halcrow }
451237fead6SMichael Halcrow 
452237fead6SMichael Halcrow struct kmem_cache *ecryptfs_sb_info_cache;
4534403158bSAl Viro static struct file_system_type ecryptfs_fs_type;
454237fead6SMichael Halcrow 
455e2401206SLee Jones /*
456e2401206SLee Jones  * ecryptfs_mount
457e2401206SLee Jones  * @fs_type: The filesystem type that the superblock should belong to
458e2401206SLee Jones  * @flags: The flags associated with the mount
459237fead6SMichael Halcrow  * @dev_name: The path to mount over
460237fead6SMichael Halcrow  * @raw_data: The options passed into the kernel
461237fead6SMichael Halcrow  */
ecryptfs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * raw_data)4624d143bebSAl Viro static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
4634d143bebSAl Viro 			const char *dev_name, void *raw_data)
464237fead6SMichael Halcrow {
4652ccde7c6SAl Viro 	struct super_block *s;
4662ccde7c6SAl Viro 	struct ecryptfs_sb_info *sbi;
467332b122dSTyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
4682ccde7c6SAl Viro 	struct ecryptfs_dentry_info *root_info;
4692ccde7c6SAl Viro 	const char *err = "Getting sb failed";
47066cb7666SAl Viro 	struct inode *inode;
47166cb7666SAl Viro 	struct path path;
47276435548SJohn Johansen 	uid_t check_ruid;
473237fead6SMichael Halcrow 	int rc;
474237fead6SMichael Halcrow 
4752ccde7c6SAl Viro 	sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
4762ccde7c6SAl Viro 	if (!sbi) {
4772ccde7c6SAl Viro 		rc = -ENOMEM;
478237fead6SMichael Halcrow 		goto out;
479237fead6SMichael Halcrow 	}
4802ccde7c6SAl Viro 
48190466255SJeffrey Mitchell 	if (!dev_name) {
48290466255SJeffrey Mitchell 		rc = -EINVAL;
48390466255SJeffrey Mitchell 		err = "Device name cannot be null";
48490466255SJeffrey Mitchell 		goto out;
48590466255SJeffrey Mitchell 	}
48690466255SJeffrey Mitchell 
48776435548SJohn Johansen 	rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
488237fead6SMichael Halcrow 	if (rc) {
4892ccde7c6SAl Viro 		err = "Error parsing options";
490237fead6SMichael Halcrow 		goto out;
4912ccde7c6SAl Viro 	}
492332b122dSTyler Hicks 	mount_crypt_stat = &sbi->mount_crypt_stat;
4932ccde7c6SAl Viro 
4949249e17fSDavid Howells 	s = sget(fs_type, NULL, set_anon_super, flags, NULL);
4952ccde7c6SAl Viro 	if (IS_ERR(s)) {
4962ccde7c6SAl Viro 		rc = PTR_ERR(s);
4972ccde7c6SAl Viro 		goto out;
4982ccde7c6SAl Viro 	}
4992ccde7c6SAl Viro 
500e836818bSJan Kara 	rc = super_setup_bdi(s);
50166cb7666SAl Viro 	if (rc)
50266cb7666SAl Viro 		goto out1;
5032ccde7c6SAl Viro 
5042ccde7c6SAl Viro 	ecryptfs_set_superblock_private(s, sbi);
5052ccde7c6SAl Viro 
5062ccde7c6SAl Viro 	/* ->kill_sb() will take care of sbi after that point */
5072ccde7c6SAl Viro 	sbi = NULL;
5082ccde7c6SAl Viro 	s->s_op = &ecryptfs_sops;
5094b899da5SAndreas Gruenbacher 	s->s_xattr = ecryptfs_xattr_handlers;
51066cb7666SAl Viro 	s->s_d_op = &ecryptfs_dops;
51166cb7666SAl Viro 
51266cb7666SAl Viro 	err = "Reading sb failed";
51366cb7666SAl Viro 	rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
51466cb7666SAl Viro 	if (rc) {
51566cb7666SAl Viro 		ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
51666cb7666SAl Viro 		goto out1;
51766cb7666SAl Viro 	}
51866cb7666SAl Viro 	if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
51966cb7666SAl Viro 		rc = -EINVAL;
52066cb7666SAl Viro 		printk(KERN_ERR "Mount on filesystem of type "
52166cb7666SAl Viro 			"eCryptfs explicitly disallowed due to "
52266cb7666SAl Viro 			"known incompatibilities\n");
52366cb7666SAl Viro 		goto out_free;
52466cb7666SAl Viro 	}
52576435548SJohn Johansen 
526bb49e9e7SChristian Brauner 	if (is_idmapped_mnt(path.mnt)) {
5270f16ff0fSChristian Brauner 		rc = -EINVAL;
5280f16ff0fSChristian Brauner 		printk(KERN_ERR "Mounting on idmapped mounts currently disallowed\n");
5290f16ff0fSChristian Brauner 		goto out_free;
5300f16ff0fSChristian Brauner 	}
5310f16ff0fSChristian Brauner 
5322b0143b5SDavid Howells 	if (check_ruid && !uid_eq(d_inode(path.dentry)->i_uid, current_uid())) {
53376435548SJohn Johansen 		rc = -EPERM;
53476435548SJohn Johansen 		printk(KERN_ERR "Mount of device (uid: %d) not owned by "
53576435548SJohn Johansen 		       "requested user (uid: %d)\n",
5362b0143b5SDavid Howells 			i_uid_read(d_inode(path.dentry)),
537cdf8c58aSEric W. Biederman 			from_kuid(&init_user_ns, current_uid()));
53876435548SJohn Johansen 		goto out_free;
53976435548SJohn Johansen 	}
54076435548SJohn Johansen 
54166cb7666SAl Viro 	ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
542069ddcdaSTyler Hicks 
543069ddcdaSTyler Hicks 	/**
544069ddcdaSTyler Hicks 	 * Set the POSIX ACL flag based on whether they're enabled in the lower
545332b122dSTyler Hicks 	 * mount.
546069ddcdaSTyler Hicks 	 */
5471751e8a6SLinus Torvalds 	s->s_flags = flags & ~SB_POSIXACL;
5481751e8a6SLinus Torvalds 	s->s_flags |= path.dentry->d_sb->s_flags & SB_POSIXACL;
549332b122dSTyler Hicks 
550332b122dSTyler Hicks 	/**
551332b122dSTyler Hicks 	 * Force a read-only eCryptfs mount when:
552332b122dSTyler Hicks 	 *   1) The lower mount is ro
553332b122dSTyler Hicks 	 *   2) The ecryptfs_encrypted_view mount option is specified
554332b122dSTyler Hicks 	 */
555bc98a42cSDavid Howells 	if (sb_rdonly(path.dentry->d_sb) || mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
5561751e8a6SLinus Torvalds 		s->s_flags |= SB_RDONLY;
557069ddcdaSTyler Hicks 
55866cb7666SAl Viro 	s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
55966cb7666SAl Viro 	s->s_blocksize = path.dentry->d_sb->s_blocksize;
560070baa51SRoberto Sassu 	s->s_magic = ECRYPTFS_SUPER_MAGIC;
56169c433edSMiklos Szeredi 	s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
56269c433edSMiklos Szeredi 
56369c433edSMiklos Szeredi 	rc = -EINVAL;
56469c433edSMiklos Szeredi 	if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
56569c433edSMiklos Szeredi 		pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
56669c433edSMiklos Szeredi 		goto out_free;
56769c433edSMiklos Szeredi 	}
56866cb7666SAl Viro 
5692b0143b5SDavid Howells 	inode = ecryptfs_get_inode(d_inode(path.dentry), s);
57066cb7666SAl Viro 	rc = PTR_ERR(inode);
57166cb7666SAl Viro 	if (IS_ERR(inode))
57266cb7666SAl Viro 		goto out_free;
57366cb7666SAl Viro 
57448fde701SAl Viro 	s->s_root = d_make_root(inode);
57566cb7666SAl Viro 	if (!s->s_root) {
57666cb7666SAl Viro 		rc = -ENOMEM;
57766cb7666SAl Viro 		goto out_free;
57866cb7666SAl Viro 	}
5792ccde7c6SAl Viro 
5802ccde7c6SAl Viro 	rc = -ENOMEM;
5812ccde7c6SAl Viro 	root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
58266cb7666SAl Viro 	if (!root_info)
58366cb7666SAl Viro 		goto out_free;
58466cb7666SAl Viro 
5852ccde7c6SAl Viro 	/* ->kill_sb() will take care of root_info */
5862ccde7c6SAl Viro 	ecryptfs_set_dentry_private(s->s_root, root_info);
58792dd1230SAl Viro 	root_info->lower_path = path;
58866cb7666SAl Viro 
5891751e8a6SLinus Torvalds 	s->s_flags |= SB_ACTIVE;
5904d143bebSAl Viro 	return dget(s->s_root);
5912ccde7c6SAl Viro 
59266cb7666SAl Viro out_free:
59366cb7666SAl Viro 	path_put(&path);
59466cb7666SAl Viro out1:
59566cb7666SAl Viro 	deactivate_locked_super(s);
596237fead6SMichael Halcrow out:
5972ccde7c6SAl Viro 	if (sbi) {
5982ccde7c6SAl Viro 		ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
5992ccde7c6SAl Viro 		kmem_cache_free(ecryptfs_sb_info_cache, sbi);
6002ccde7c6SAl Viro 	}
6012ccde7c6SAl Viro 	printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
6024d143bebSAl Viro 	return ERR_PTR(rc);
603237fead6SMichael Halcrow }
604237fead6SMichael Halcrow 
605237fead6SMichael Halcrow /**
606237fead6SMichael Halcrow  * ecryptfs_kill_block_super
607237fead6SMichael Halcrow  * @sb: The ecryptfs super block
608237fead6SMichael Halcrow  *
609237fead6SMichael Halcrow  * Used to bring the superblock down and free the private data.
610237fead6SMichael Halcrow  */
ecryptfs_kill_block_super(struct super_block * sb)611237fead6SMichael Halcrow static void ecryptfs_kill_block_super(struct super_block *sb)
612237fead6SMichael Halcrow {
613decabd66SAl Viro 	struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
614decabd66SAl Viro 	kill_anon_super(sb);
615decabd66SAl Viro 	if (!sb_info)
616decabd66SAl Viro 		return;
617decabd66SAl Viro 	ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
618decabd66SAl Viro 	kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
619237fead6SMichael Halcrow }
620237fead6SMichael Halcrow 
621237fead6SMichael Halcrow static struct file_system_type ecryptfs_fs_type = {
622237fead6SMichael Halcrow 	.owner = THIS_MODULE,
623237fead6SMichael Halcrow 	.name = "ecryptfs",
6244d143bebSAl Viro 	.mount = ecryptfs_mount,
625237fead6SMichael Halcrow 	.kill_sb = ecryptfs_kill_block_super,
626237fead6SMichael Halcrow 	.fs_flags = 0
627237fead6SMichael Halcrow };
6287f78e035SEric W. Biederman MODULE_ALIAS_FS("ecryptfs");
629237fead6SMichael Halcrow 
630e2401206SLee Jones /*
631237fead6SMichael Halcrow  * inode_info_init_once
632237fead6SMichael Halcrow  *
633237fead6SMichael Halcrow  * Initializes the ecryptfs_inode_info_cache when it is created
634237fead6SMichael Halcrow  */
635237fead6SMichael Halcrow static void
inode_info_init_once(void * vptr)63651cc5068SAlexey Dobriyan inode_info_init_once(void *vptr)
637237fead6SMichael Halcrow {
638237fead6SMichael Halcrow 	struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
639237fead6SMichael Halcrow 
640237fead6SMichael Halcrow 	inode_init_once(&ei->vfs_inode);
641237fead6SMichael Halcrow }
642237fead6SMichael Halcrow 
643237fead6SMichael Halcrow static struct ecryptfs_cache_info {
644e18b890bSChristoph Lameter 	struct kmem_cache **cache;
645237fead6SMichael Halcrow 	const char *name;
646237fead6SMichael Halcrow 	size_t size;
647d50112edSAlexey Dobriyan 	slab_flags_t flags;
64851cc5068SAlexey Dobriyan 	void (*ctor)(void *obj);
649237fead6SMichael Halcrow } ecryptfs_cache_infos[] = {
650237fead6SMichael Halcrow 	{
651237fead6SMichael Halcrow 		.cache = &ecryptfs_auth_tok_list_item_cache,
652237fead6SMichael Halcrow 		.name = "ecryptfs_auth_tok_list_item",
653237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_auth_tok_list_item),
654237fead6SMichael Halcrow 	},
655237fead6SMichael Halcrow 	{
656237fead6SMichael Halcrow 		.cache = &ecryptfs_file_info_cache,
657237fead6SMichael Halcrow 		.name = "ecryptfs_file_cache",
658237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_file_info),
659237fead6SMichael Halcrow 	},
660237fead6SMichael Halcrow 	{
661237fead6SMichael Halcrow 		.cache = &ecryptfs_dentry_info_cache,
662237fead6SMichael Halcrow 		.name = "ecryptfs_dentry_info_cache",
663237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_dentry_info),
664237fead6SMichael Halcrow 	},
665237fead6SMichael Halcrow 	{
666237fead6SMichael Halcrow 		.cache = &ecryptfs_inode_info_cache,
667237fead6SMichael Halcrow 		.name = "ecryptfs_inode_cache",
668237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_inode_info),
6695d097056SVladimir Davydov 		.flags = SLAB_ACCOUNT,
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 	{
67830632870STyler Hicks 		.cache = &ecryptfs_header_cache,
67930632870STyler Hicks 		.name = "ecryptfs_headers",
68009cbfeafSKirill A. Shutemov 		.size = PAGE_SIZE,
681237fead6SMichael Halcrow 	},
682237fead6SMichael Halcrow 	{
683dd2a3b7aSMichael Halcrow 		.cache = &ecryptfs_xattr_cache,
684dd2a3b7aSMichael Halcrow 		.name = "ecryptfs_xattr_cache",
68509cbfeafSKirill A. Shutemov 		.size = PAGE_SIZE,
686dd2a3b7aSMichael Halcrow 	},
687dd2a3b7aSMichael Halcrow 	{
688eb95e7ffSMichael Halcrow 		.cache = &ecryptfs_key_record_cache,
689eb95e7ffSMichael Halcrow 		.name = "ecryptfs_key_record_cache",
690eb95e7ffSMichael Halcrow 		.size = sizeof(struct ecryptfs_key_record),
691eb95e7ffSMichael Halcrow 	},
692956159c3SMichael Halcrow 	{
693956159c3SMichael Halcrow 		.cache = &ecryptfs_key_sig_cache,
694956159c3SMichael Halcrow 		.name = "ecryptfs_key_sig_cache",
695956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_key_sig),
696956159c3SMichael Halcrow 	},
697956159c3SMichael Halcrow 	{
698956159c3SMichael Halcrow 		.cache = &ecryptfs_global_auth_tok_cache,
699956159c3SMichael Halcrow 		.name = "ecryptfs_global_auth_tok_cache",
700956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_global_auth_tok),
701956159c3SMichael Halcrow 	},
702956159c3SMichael Halcrow 	{
703956159c3SMichael Halcrow 		.cache = &ecryptfs_key_tfm_cache,
704956159c3SMichael Halcrow 		.name = "ecryptfs_key_tfm_cache",
705956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_key_tfm),
706956159c3SMichael Halcrow 	},
707237fead6SMichael Halcrow };
708237fead6SMichael Halcrow 
ecryptfs_free_kmem_caches(void)709237fead6SMichael Halcrow static void ecryptfs_free_kmem_caches(void)
710237fead6SMichael Halcrow {
711237fead6SMichael Halcrow 	int i;
712237fead6SMichael Halcrow 
7138c0a8537SKirill A. Shutemov 	/*
7148c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
7158c0a8537SKirill A. Shutemov 	 * destroy cache.
7168c0a8537SKirill A. Shutemov 	 */
7178c0a8537SKirill A. Shutemov 	rcu_barrier();
7188c0a8537SKirill A. Shutemov 
719237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
720237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
721237fead6SMichael Halcrow 
722237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
723237fead6SMichael Halcrow 		kmem_cache_destroy(*(info->cache));
724237fead6SMichael Halcrow 	}
725237fead6SMichael Halcrow }
726237fead6SMichael Halcrow 
727237fead6SMichael Halcrow /**
728237fead6SMichael Halcrow  * ecryptfs_init_kmem_caches
729237fead6SMichael Halcrow  *
730237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
731237fead6SMichael Halcrow  */
ecryptfs_init_kmem_caches(void)732237fead6SMichael Halcrow static int ecryptfs_init_kmem_caches(void)
733237fead6SMichael Halcrow {
734237fead6SMichael Halcrow 	int i;
735237fead6SMichael Halcrow 
736237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
737237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
738237fead6SMichael Halcrow 
739237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
7405d097056SVladimir Davydov 		*(info->cache) = kmem_cache_create(info->name, info->size, 0,
7415d097056SVladimir Davydov 				SLAB_HWCACHE_ALIGN | info->flags, info->ctor);
742237fead6SMichael Halcrow 		if (!*(info->cache)) {
743237fead6SMichael Halcrow 			ecryptfs_free_kmem_caches();
744237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "%s: "
745237fead6SMichael Halcrow 					"kmem_cache_create failed\n",
746237fead6SMichael Halcrow 					info->name);
747237fead6SMichael Halcrow 			return -ENOMEM;
748237fead6SMichael Halcrow 		}
749237fead6SMichael Halcrow 	}
750237fead6SMichael Halcrow 	return 0;
751237fead6SMichael Halcrow }
752237fead6SMichael Halcrow 
7536e90aa97SGreg Kroah-Hartman static struct kobject *ecryptfs_kobj;
754237fead6SMichael Halcrow 
version_show(struct kobject * kobj,struct kobj_attribute * attr,char * buff)755386f275fSKay Sievers static ssize_t version_show(struct kobject *kobj,
756386f275fSKay Sievers 			    struct kobj_attribute *attr, char *buff)
757237fead6SMichael Halcrow {
758237fead6SMichael Halcrow 	return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
759237fead6SMichael Halcrow }
760237fead6SMichael Halcrow 
761386f275fSKay Sievers static struct kobj_attribute version_attr = __ATTR_RO(version);
762237fead6SMichael Halcrow 
76330a468b1SGreg Kroah-Hartman static struct attribute *attributes[] = {
76430a468b1SGreg Kroah-Hartman 	&version_attr.attr,
76530a468b1SGreg Kroah-Hartman 	NULL,
76630a468b1SGreg Kroah-Hartman };
76730a468b1SGreg Kroah-Hartman 
7684670269fSArvind Yadav static const struct attribute_group attr_group = {
76930a468b1SGreg Kroah-Hartman 	.attrs = attributes,
77030a468b1SGreg Kroah-Hartman };
771237fead6SMichael Halcrow 
do_sysfs_registration(void)772237fead6SMichael Halcrow static int do_sysfs_registration(void)
773237fead6SMichael Halcrow {
774237fead6SMichael Halcrow 	int rc;
775237fead6SMichael Halcrow 
7766e90aa97SGreg Kroah-Hartman 	ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
7776e90aa97SGreg Kroah-Hartman 	if (!ecryptfs_kobj) {
778917e865dSGreg Kroah-Hartman 		printk(KERN_ERR "Unable to create ecryptfs kset\n");
779917e865dSGreg Kroah-Hartman 		rc = -ENOMEM;
780237fead6SMichael Halcrow 		goto out;
781237fead6SMichael Halcrow 	}
7826e90aa97SGreg Kroah-Hartman 	rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
783237fead6SMichael Halcrow 	if (rc) {
784237fead6SMichael Halcrow 		printk(KERN_ERR
78530a468b1SGreg Kroah-Hartman 		       "Unable to create ecryptfs version attributes\n");
786197b12d6SGreg Kroah-Hartman 		kobject_put(ecryptfs_kobj);
787237fead6SMichael Halcrow 	}
788237fead6SMichael Halcrow out:
789237fead6SMichael Halcrow 	return rc;
790237fead6SMichael Halcrow }
791237fead6SMichael Halcrow 
do_sysfs_unregistration(void)792a75de1b3SRyusuke Konishi static void do_sysfs_unregistration(void)
793a75de1b3SRyusuke Konishi {
7946e90aa97SGreg Kroah-Hartman 	sysfs_remove_group(ecryptfs_kobj, &attr_group);
795197b12d6SGreg Kroah-Hartman 	kobject_put(ecryptfs_kobj);
796a75de1b3SRyusuke Konishi }
797a75de1b3SRyusuke Konishi 
ecryptfs_init(void)798237fead6SMichael Halcrow static int __init ecryptfs_init(void)
799237fead6SMichael Halcrow {
800237fead6SMichael Halcrow 	int rc;
801237fead6SMichael Halcrow 
80209cbfeafSKirill A. Shutemov 	if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_SIZE) {
803237fead6SMichael Halcrow 		rc = -EINVAL;
804237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
805237fead6SMichael Halcrow 				"larger than the host's page size, and so "
806237fead6SMichael Halcrow 				"eCryptfs cannot run on this system. The "
807888d57bbSJoe Perches 				"default eCryptfs extent size is [%u] bytes; "
808888d57bbSJoe Perches 				"the page size is [%lu] bytes.\n",
809888d57bbSJoe Perches 				ECRYPTFS_DEFAULT_EXTENT_SIZE,
81009cbfeafSKirill A. Shutemov 				(unsigned long)PAGE_SIZE);
811237fead6SMichael Halcrow 		goto out;
812237fead6SMichael Halcrow 	}
813237fead6SMichael Halcrow 	rc = ecryptfs_init_kmem_caches();
814237fead6SMichael Halcrow 	if (rc) {
815237fead6SMichael Halcrow 		printk(KERN_ERR
816237fead6SMichael Halcrow 		       "Failed to allocate one or more kmem_cache objects\n");
817237fead6SMichael Halcrow 		goto out;
818237fead6SMichael Halcrow 	}
819237fead6SMichael Halcrow 	rc = do_sysfs_registration();
820237fead6SMichael Halcrow 	if (rc) {
821237fead6SMichael Halcrow 		printk(KERN_ERR "sysfs registration failed\n");
8220794f569SAl Viro 		goto out_free_kmem_caches;
823237fead6SMichael Halcrow 	}
824746f1e55SMichael Halcrow 	rc = ecryptfs_init_kthread();
825746f1e55SMichael Halcrow 	if (rc) {
826746f1e55SMichael Halcrow 		printk(KERN_ERR "%s: kthread initialization failed; "
827746f1e55SMichael Halcrow 		       "rc = [%d]\n", __func__, rc);
828746f1e55SMichael Halcrow 		goto out_do_sysfs_unregistration;
829746f1e55SMichael Halcrow 	}
830624ae528STyler Hicks 	rc = ecryptfs_init_messaging();
831dddfa461SMichael Halcrow 	if (rc) {
83225985edcSLucas De Marchi 		printk(KERN_ERR "Failure occurred while attempting to "
833624ae528STyler Hicks 				"initialize the communications channel to "
834624ae528STyler Hicks 				"ecryptfsd\n");
835746f1e55SMichael Halcrow 		goto out_destroy_kthread;
836956159c3SMichael Halcrow 	}
837956159c3SMichael Halcrow 	rc = ecryptfs_init_crypto();
838956159c3SMichael Halcrow 	if (rc) {
839956159c3SMichael Halcrow 		printk(KERN_ERR "Failure whilst attempting to init crypto; "
840956159c3SMichael Halcrow 		       "rc = [%d]\n", rc);
841cf81f89dSMichael Halcrow 		goto out_release_messaging;
842dddfa461SMichael Halcrow 	}
8430794f569SAl Viro 	rc = register_filesystem(&ecryptfs_fs_type);
8440794f569SAl Viro 	if (rc) {
8450794f569SAl Viro 		printk(KERN_ERR "Failed to register filesystem\n");
8460794f569SAl Viro 		goto out_destroy_crypto;
8470794f569SAl Viro 	}
8482830bfd6SEric Sandeen 	if (ecryptfs_verbosity > 0)
8492830bfd6SEric Sandeen 		printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
8502830bfd6SEric Sandeen 			"will be written to the syslog!\n", ecryptfs_verbosity);
8512830bfd6SEric Sandeen 
852cf81f89dSMichael Halcrow 	goto out;
8530794f569SAl Viro out_destroy_crypto:
8540794f569SAl Viro 	ecryptfs_destroy_crypto();
855cf81f89dSMichael Halcrow out_release_messaging:
856624ae528STyler Hicks 	ecryptfs_release_messaging();
857746f1e55SMichael Halcrow out_destroy_kthread:
858746f1e55SMichael Halcrow 	ecryptfs_destroy_kthread();
859cf81f89dSMichael Halcrow out_do_sysfs_unregistration:
860cf81f89dSMichael Halcrow 	do_sysfs_unregistration();
861cf81f89dSMichael Halcrow out_free_kmem_caches:
862cf81f89dSMichael Halcrow 	ecryptfs_free_kmem_caches();
863237fead6SMichael Halcrow out:
864237fead6SMichael Halcrow 	return rc;
865237fead6SMichael Halcrow }
866237fead6SMichael Halcrow 
ecryptfs_exit(void)867237fead6SMichael Halcrow static void __exit ecryptfs_exit(void)
868237fead6SMichael Halcrow {
869cf81f89dSMichael Halcrow 	int rc;
870cf81f89dSMichael Halcrow 
871cf81f89dSMichael Halcrow 	rc = ecryptfs_destroy_crypto();
872cf81f89dSMichael Halcrow 	if (rc)
873cf81f89dSMichael Halcrow 		printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
874cf81f89dSMichael Halcrow 		       "rc = [%d]\n", rc);
875624ae528STyler Hicks 	ecryptfs_release_messaging();
876746f1e55SMichael Halcrow 	ecryptfs_destroy_kthread();
877cf81f89dSMichael Halcrow 	do_sysfs_unregistration();
878237fead6SMichael Halcrow 	unregister_filesystem(&ecryptfs_fs_type);
879237fead6SMichael Halcrow 	ecryptfs_free_kmem_caches();
880237fead6SMichael Halcrow }
881237fead6SMichael Halcrow 
882237fead6SMichael Halcrow MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
883237fead6SMichael Halcrow MODULE_DESCRIPTION("eCryptfs");
884237fead6SMichael Halcrow 
885237fead6SMichael Halcrow MODULE_LICENSE("GPL");
886237fead6SMichael Halcrow 
887237fead6SMichael Halcrow module_init(ecryptfs_init)
888237fead6SMichael Halcrow module_exit(ecryptfs_exit)
889