xref: /linux/fs/ecryptfs/main.c (revision a75de1b3799f8933d6d2b64bdf31194368ec98ab)
1237fead6SMichael Halcrow /**
2237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3237fead6SMichael Halcrow  *
4237fead6SMichael Halcrow  * Copyright (C) 1997-2003 Erez Zadok
5237fead6SMichael Halcrow  * Copyright (C) 2001-2003 Stony Brook University
6dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
7237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8237fead6SMichael Halcrow  *              Michael C. Thompson <mcthomps@us.ibm.com>
9dddfa461SMichael Halcrow  *              Tyler Hicks <tyhicks@ou.edu>
10237fead6SMichael Halcrow  *
11237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
12237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
13237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
14237fead6SMichael Halcrow  * License, or (at your option) any later version.
15237fead6SMichael Halcrow  *
16237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
17237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
18237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19237fead6SMichael Halcrow  * General Public License for more details.
20237fead6SMichael Halcrow  *
21237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
22237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
23237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24237fead6SMichael Halcrow  * 02111-1307, USA.
25237fead6SMichael Halcrow  */
26237fead6SMichael Halcrow 
27237fead6SMichael Halcrow #include <linux/dcache.h>
28237fead6SMichael Halcrow #include <linux/file.h>
29237fead6SMichael Halcrow #include <linux/module.h>
30237fead6SMichael Halcrow #include <linux/namei.h>
31237fead6SMichael Halcrow #include <linux/skbuff.h>
32237fead6SMichael Halcrow #include <linux/crypto.h>
33237fead6SMichael Halcrow #include <linux/netlink.h>
34237fead6SMichael Halcrow #include <linux/mount.h>
35237fead6SMichael Halcrow #include <linux/dcache.h>
36237fead6SMichael Halcrow #include <linux/pagemap.h>
37237fead6SMichael Halcrow #include <linux/key.h>
38237fead6SMichael Halcrow #include <linux/parser.h>
390cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
40237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
41237fead6SMichael Halcrow 
42237fead6SMichael Halcrow /**
43237fead6SMichael Halcrow  * Module parameter that defines the ecryptfs_verbosity level.
44237fead6SMichael Halcrow  */
45237fead6SMichael Halcrow int ecryptfs_verbosity = 0;
46237fead6SMichael Halcrow 
47237fead6SMichael Halcrow module_param(ecryptfs_verbosity, int, 0);
48237fead6SMichael Halcrow MODULE_PARM_DESC(ecryptfs_verbosity,
49237fead6SMichael Halcrow 		 "Initial verbosity level (0 or 1; defaults to "
50237fead6SMichael Halcrow 		 "0, which is Quiet)");
51237fead6SMichael Halcrow 
52dddfa461SMichael Halcrow /**
53dddfa461SMichael Halcrow  * Module parameter that defines the number of netlink message buffer
54dddfa461SMichael Halcrow  * elements
55dddfa461SMichael Halcrow  */
56dddfa461SMichael Halcrow unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
57dddfa461SMichael Halcrow 
58dddfa461SMichael Halcrow module_param(ecryptfs_message_buf_len, uint, 0);
59dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_message_buf_len,
60dddfa461SMichael Halcrow 		 "Number of message buffer elements");
61dddfa461SMichael Halcrow 
62dddfa461SMichael Halcrow /**
63dddfa461SMichael Halcrow  * Module parameter that defines the maximum guaranteed amount of time to wait
64dddfa461SMichael Halcrow  * for a response through netlink.  The actual sleep time will be, more than
65dddfa461SMichael Halcrow  * likely, a small amount greater than this specified value, but only less if
66dddfa461SMichael Halcrow  * the netlink message successfully arrives.
67dddfa461SMichael Halcrow  */
68dddfa461SMichael Halcrow signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
69dddfa461SMichael Halcrow 
70dddfa461SMichael Halcrow module_param(ecryptfs_message_wait_timeout, long, 0);
71dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
72dddfa461SMichael Halcrow 		 "Maximum number of seconds that an operation will "
73dddfa461SMichael Halcrow 		 "sleep while waiting for a message response from "
74dddfa461SMichael Halcrow 		 "userspace");
75dddfa461SMichael Halcrow 
76dddfa461SMichael Halcrow /**
77dddfa461SMichael Halcrow  * Module parameter that is an estimate of the maximum number of users
78dddfa461SMichael Halcrow  * that will be concurrently using eCryptfs. Set this to the right
79dddfa461SMichael Halcrow  * value to balance performance and memory use.
80dddfa461SMichael Halcrow  */
81dddfa461SMichael Halcrow unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
82dddfa461SMichael Halcrow 
83dddfa461SMichael Halcrow module_param(ecryptfs_number_of_users, uint, 0);
84dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
85dddfa461SMichael Halcrow 		 "concurrent users of eCryptfs");
86dddfa461SMichael Halcrow 
87dddfa461SMichael Halcrow unsigned int ecryptfs_transport = ECRYPTFS_DEFAULT_TRANSPORT;
88dddfa461SMichael Halcrow 
89237fead6SMichael Halcrow void __ecryptfs_printk(const char *fmt, ...)
90237fead6SMichael Halcrow {
91237fead6SMichael Halcrow 	va_list args;
92237fead6SMichael Halcrow 	va_start(args, fmt);
93237fead6SMichael Halcrow 	if (fmt[1] == '7') { /* KERN_DEBUG */
94237fead6SMichael Halcrow 		if (ecryptfs_verbosity >= 1)
95237fead6SMichael Halcrow 			vprintk(fmt, args);
96237fead6SMichael Halcrow 	} else
97237fead6SMichael Halcrow 		vprintk(fmt, args);
98237fead6SMichael Halcrow 	va_end(args);
99237fead6SMichael Halcrow }
100237fead6SMichael Halcrow 
101237fead6SMichael Halcrow /**
102237fead6SMichael Halcrow  * ecryptfs_interpose
103237fead6SMichael Halcrow  * @lower_dentry: Existing dentry in the lower filesystem
104237fead6SMichael Halcrow  * @dentry: ecryptfs' dentry
105237fead6SMichael Halcrow  * @sb: ecryptfs's super_block
106237fead6SMichael Halcrow  * @flag: If set to true, then d_add is called, else d_instantiate is called
107237fead6SMichael Halcrow  *
108237fead6SMichael Halcrow  * Interposes upper and lower dentries.
109237fead6SMichael Halcrow  *
110237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
111237fead6SMichael Halcrow  */
112237fead6SMichael Halcrow int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
113237fead6SMichael Halcrow 		       struct super_block *sb, int flag)
114237fead6SMichael Halcrow {
115237fead6SMichael Halcrow 	struct inode *lower_inode;
116237fead6SMichael Halcrow 	struct inode *inode;
117237fead6SMichael Halcrow 	int rc = 0;
118237fead6SMichael Halcrow 
119237fead6SMichael Halcrow 	lower_inode = lower_dentry->d_inode;
120237fead6SMichael Halcrow 	if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
121237fead6SMichael Halcrow 		rc = -EXDEV;
122237fead6SMichael Halcrow 		goto out;
123237fead6SMichael Halcrow 	}
124237fead6SMichael Halcrow 	if (!igrab(lower_inode)) {
125237fead6SMichael Halcrow 		rc = -ESTALE;
126237fead6SMichael Halcrow 		goto out;
127237fead6SMichael Halcrow 	}
128237fead6SMichael Halcrow 	inode = iget5_locked(sb, (unsigned long)lower_inode,
129237fead6SMichael Halcrow 			     ecryptfs_inode_test, ecryptfs_inode_set,
130237fead6SMichael Halcrow 			     lower_inode);
131237fead6SMichael Halcrow 	if (!inode) {
132237fead6SMichael Halcrow 		rc = -EACCES;
133237fead6SMichael Halcrow 		iput(lower_inode);
134237fead6SMichael Halcrow 		goto out;
135237fead6SMichael Halcrow 	}
136237fead6SMichael Halcrow 	if (inode->i_state & I_NEW)
137237fead6SMichael Halcrow 		unlock_new_inode(inode);
138237fead6SMichael Halcrow 	else
139237fead6SMichael Halcrow 		iput(lower_inode);
140237fead6SMichael Halcrow 	if (S_ISLNK(lower_inode->i_mode))
141237fead6SMichael Halcrow 		inode->i_op = &ecryptfs_symlink_iops;
142237fead6SMichael Halcrow 	else if (S_ISDIR(lower_inode->i_mode))
143237fead6SMichael Halcrow 		inode->i_op = &ecryptfs_dir_iops;
144237fead6SMichael Halcrow 	if (S_ISDIR(lower_inode->i_mode))
145237fead6SMichael Halcrow 		inode->i_fop = &ecryptfs_dir_fops;
14626da8205SPekka Enberg 	if (special_file(lower_inode->i_mode))
147237fead6SMichael Halcrow 		init_special_inode(inode, lower_inode->i_mode,
148237fead6SMichael Halcrow 				   lower_inode->i_rdev);
149237fead6SMichael Halcrow 	dentry->d_op = &ecryptfs_dops;
150237fead6SMichael Halcrow 	if (flag)
151237fead6SMichael Halcrow 		d_add(dentry, inode);
152237fead6SMichael Halcrow 	else
153237fead6SMichael Halcrow 		d_instantiate(dentry, inode);
1540cc72dc7SJosef "Jeff" Sipek 	fsstack_copy_attr_all(inode, lower_inode, NULL);
155237fead6SMichael Halcrow 	/* This size will be overwritten for real files w/ headers and
156237fead6SMichael Halcrow 	 * other metadata */
1570cc72dc7SJosef "Jeff" Sipek 	fsstack_copy_inode_size(inode, lower_inode);
158237fead6SMichael Halcrow out:
159237fead6SMichael Halcrow 	return rc;
160237fead6SMichael Halcrow }
161237fead6SMichael Halcrow 
162237fead6SMichael Halcrow enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, ecryptfs_opt_debug,
163237fead6SMichael Halcrow        ecryptfs_opt_ecryptfs_debug, ecryptfs_opt_cipher,
164237fead6SMichael Halcrow        ecryptfs_opt_ecryptfs_cipher, ecryptfs_opt_ecryptfs_key_bytes,
16517398957SMichael Halcrow        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
16617398957SMichael Halcrow        ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
167237fead6SMichael Halcrow 
168237fead6SMichael Halcrow static match_table_t tokens = {
169237fead6SMichael Halcrow 	{ecryptfs_opt_sig, "sig=%s"},
170237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
171237fead6SMichael Halcrow 	{ecryptfs_opt_debug, "debug=%u"},
172237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_debug, "ecryptfs_debug=%u"},
173237fead6SMichael Halcrow 	{ecryptfs_opt_cipher, "cipher=%s"},
174237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
175237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
176237fead6SMichael Halcrow 	{ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
17717398957SMichael Halcrow 	{ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
17817398957SMichael Halcrow 	{ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
179237fead6SMichael Halcrow 	{ecryptfs_opt_err, NULL}
180237fead6SMichael Halcrow };
181237fead6SMichael Halcrow 
182237fead6SMichael Halcrow /**
183237fead6SMichael Halcrow  * ecryptfs_verify_version
184237fead6SMichael Halcrow  * @version: The version number to confirm
185237fead6SMichael Halcrow  *
186237fead6SMichael Halcrow  * Returns zero on good version; non-zero otherwise
187237fead6SMichael Halcrow  */
188237fead6SMichael Halcrow static int ecryptfs_verify_version(u16 version)
189237fead6SMichael Halcrow {
190237fead6SMichael Halcrow 	int rc = 0;
191237fead6SMichael Halcrow 	unsigned char major;
192237fead6SMichael Halcrow 	unsigned char minor;
193237fead6SMichael Halcrow 
194237fead6SMichael Halcrow 	major = ((version >> 8) & 0xFF);
195237fead6SMichael Halcrow 	minor = (version & 0xFF);
196237fead6SMichael Halcrow 	if (major != ECRYPTFS_VERSION_MAJOR) {
197237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
198237fead6SMichael Halcrow 				"Expected [%d]; got [%d]\n",
199237fead6SMichael Halcrow 				ECRYPTFS_VERSION_MAJOR, major);
200237fead6SMichael Halcrow 		rc = -EINVAL;
201237fead6SMichael Halcrow 		goto out;
202237fead6SMichael Halcrow 	}
203237fead6SMichael Halcrow 	if (minor != ECRYPTFS_VERSION_MINOR) {
204237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
205237fead6SMichael Halcrow 				"Expected [%d]; got [%d]\n",
206237fead6SMichael Halcrow 				ECRYPTFS_VERSION_MINOR, minor);
207237fead6SMichael Halcrow 		rc = -EINVAL;
208237fead6SMichael Halcrow 		goto out;
209237fead6SMichael Halcrow 	}
210237fead6SMichael Halcrow out:
211237fead6SMichael Halcrow 	return rc;
212237fead6SMichael Halcrow }
213237fead6SMichael Halcrow 
214237fead6SMichael Halcrow /**
215237fead6SMichael Halcrow  * ecryptfs_parse_options
216237fead6SMichael Halcrow  * @sb: The ecryptfs super block
217237fead6SMichael Halcrow  * @options: The options pased to the kernel
218237fead6SMichael Halcrow  *
219237fead6SMichael Halcrow  * Parse mount options:
220237fead6SMichael Halcrow  * debug=N 	   - ecryptfs_verbosity level for debug output
221237fead6SMichael Halcrow  * sig=XXX	   - description(signature) of the key to use
222237fead6SMichael Halcrow  *
223237fead6SMichael Halcrow  * Returns the dentry object of the lower-level (lower/interposed)
224237fead6SMichael Halcrow  * directory; We want to mount our stackable file system on top of
225237fead6SMichael Halcrow  * that lower directory.
226237fead6SMichael Halcrow  *
227237fead6SMichael Halcrow  * The signature of the key to use must be the description of a key
228237fead6SMichael Halcrow  * already in the keyring. Mounting will fail if the key can not be
229237fead6SMichael Halcrow  * found.
230237fead6SMichael Halcrow  *
231237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
232237fead6SMichael Halcrow  */
233237fead6SMichael Halcrow static int ecryptfs_parse_options(struct super_block *sb, char *options)
234237fead6SMichael Halcrow {
235237fead6SMichael Halcrow 	char *p;
236237fead6SMichael Halcrow 	int rc = 0;
237237fead6SMichael Halcrow 	int sig_set = 0;
238237fead6SMichael Halcrow 	int cipher_name_set = 0;
239237fead6SMichael Halcrow 	int cipher_key_bytes;
240237fead6SMichael Halcrow 	int cipher_key_bytes_set = 0;
241237fead6SMichael Halcrow 	struct key *auth_tok_key = NULL;
242237fead6SMichael Halcrow 	struct ecryptfs_auth_tok *auth_tok = NULL;
243237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
244237fead6SMichael Halcrow 		&ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
245237fead6SMichael Halcrow 	substring_t args[MAX_OPT_ARGS];
246237fead6SMichael Halcrow 	int token;
247237fead6SMichael Halcrow 	char *sig_src;
248237fead6SMichael Halcrow 	char *sig_dst;
249237fead6SMichael Halcrow 	char *debug_src;
250237fead6SMichael Halcrow 	char *cipher_name_dst;
251237fead6SMichael Halcrow 	char *cipher_name_src;
252237fead6SMichael Halcrow 	char *cipher_key_bytes_src;
253237fead6SMichael Halcrow 	int cipher_name_len;
254237fead6SMichael Halcrow 
255237fead6SMichael Halcrow 	if (!options) {
256237fead6SMichael Halcrow 		rc = -EINVAL;
257237fead6SMichael Halcrow 		goto out;
258237fead6SMichael Halcrow 	}
259237fead6SMichael Halcrow 	while ((p = strsep(&options, ",")) != NULL) {
260237fead6SMichael Halcrow 		if (!*p)
261237fead6SMichael Halcrow 			continue;
262237fead6SMichael Halcrow 		token = match_token(p, tokens, args);
263237fead6SMichael Halcrow 		switch (token) {
264237fead6SMichael Halcrow 		case ecryptfs_opt_sig:
265237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_sig:
266237fead6SMichael Halcrow 			sig_src = args[0].from;
267237fead6SMichael Halcrow 			sig_dst =
268237fead6SMichael Halcrow 				mount_crypt_stat->global_auth_tok_sig;
269237fead6SMichael Halcrow 			memcpy(sig_dst, sig_src, ECRYPTFS_SIG_SIZE_HEX);
270237fead6SMichael Halcrow 			sig_dst[ECRYPTFS_SIG_SIZE_HEX] = '\0';
271237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
272237fead6SMichael Halcrow 					"The mount_crypt_stat "
273237fead6SMichael Halcrow 					"global_auth_tok_sig set to: "
274237fead6SMichael Halcrow 					"[%s]\n", sig_dst);
275237fead6SMichael Halcrow 			sig_set = 1;
276237fead6SMichael Halcrow 			break;
277237fead6SMichael Halcrow 		case ecryptfs_opt_debug:
278237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_debug:
279237fead6SMichael Halcrow 			debug_src = args[0].from;
280237fead6SMichael Halcrow 			ecryptfs_verbosity =
281237fead6SMichael Halcrow 				(int)simple_strtol(debug_src, &debug_src,
282237fead6SMichael Halcrow 						   0);
283237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
284237fead6SMichael Halcrow 					"Verbosity set to [%d]" "\n",
285237fead6SMichael Halcrow 					ecryptfs_verbosity);
286237fead6SMichael Halcrow 			break;
287237fead6SMichael Halcrow 		case ecryptfs_opt_cipher:
288237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_cipher:
289237fead6SMichael Halcrow 			cipher_name_src = args[0].from;
290237fead6SMichael Halcrow 			cipher_name_dst =
291237fead6SMichael Halcrow 				mount_crypt_stat->
292237fead6SMichael Halcrow 				global_default_cipher_name;
293237fead6SMichael Halcrow 			strncpy(cipher_name_dst, cipher_name_src,
294237fead6SMichael Halcrow 				ECRYPTFS_MAX_CIPHER_NAME_SIZE);
295237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
296237fead6SMichael Halcrow 					"The mount_crypt_stat "
297237fead6SMichael Halcrow 					"global_default_cipher_name set to: "
298237fead6SMichael Halcrow 					"[%s]\n", cipher_name_dst);
299237fead6SMichael Halcrow 			cipher_name_set = 1;
300237fead6SMichael Halcrow 			break;
301237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_key_bytes:
302237fead6SMichael Halcrow 			cipher_key_bytes_src = args[0].from;
303237fead6SMichael Halcrow 			cipher_key_bytes =
304237fead6SMichael Halcrow 				(int)simple_strtol(cipher_key_bytes_src,
305237fead6SMichael Halcrow 						   &cipher_key_bytes_src, 0);
306237fead6SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size =
307237fead6SMichael Halcrow 				cipher_key_bytes;
308237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
309237fead6SMichael Halcrow 					"The mount_crypt_stat "
310237fead6SMichael Halcrow 					"global_default_cipher_key_size "
311237fead6SMichael Halcrow 					"set to: [%d]\n", mount_crypt_stat->
312237fead6SMichael Halcrow 					global_default_cipher_key_size);
313237fead6SMichael Halcrow 			cipher_key_bytes_set = 1;
314237fead6SMichael Halcrow 			break;
315237fead6SMichael Halcrow 		case ecryptfs_opt_passthrough:
316237fead6SMichael Halcrow 			mount_crypt_stat->flags |=
317237fead6SMichael Halcrow 				ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
318237fead6SMichael Halcrow 			break;
31917398957SMichael Halcrow 		case ecryptfs_opt_xattr_metadata:
32017398957SMichael Halcrow 			mount_crypt_stat->flags |=
32117398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
32217398957SMichael Halcrow 			break;
32317398957SMichael Halcrow 		case ecryptfs_opt_encrypted_view:
32417398957SMichael Halcrow 			mount_crypt_stat->flags |=
32517398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
32617398957SMichael Halcrow 			mount_crypt_stat->flags |=
32717398957SMichael Halcrow 				ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
32817398957SMichael Halcrow 			break;
329237fead6SMichael Halcrow 		case ecryptfs_opt_err:
330237fead6SMichael Halcrow 		default:
331237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING,
332237fead6SMichael Halcrow 					"eCryptfs: unrecognized option '%s'\n",
333237fead6SMichael Halcrow 					p);
334237fead6SMichael Halcrow 		}
335237fead6SMichael Halcrow 	}
336237fead6SMichael Halcrow 	/* Do not support lack of mount-wide signature in 0.1
337237fead6SMichael Halcrow 	 * release */
338237fead6SMichael Halcrow 	if (!sig_set) {
339237fead6SMichael Halcrow 		rc = -EINVAL;
340237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "You must supply a valid "
341237fead6SMichael Halcrow 				"passphrase auth tok signature as a mount "
342237fead6SMichael Halcrow 				"parameter; see the eCryptfs README\n");
343237fead6SMichael Halcrow 		goto out;
344237fead6SMichael Halcrow 	}
345237fead6SMichael Halcrow 	if (!cipher_name_set) {
346237fead6SMichael Halcrow 		cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
347237fead6SMichael Halcrow 		if (unlikely(cipher_name_len
348237fead6SMichael Halcrow 			     >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
349237fead6SMichael Halcrow 			rc = -EINVAL;
350237fead6SMichael Halcrow 			BUG();
351237fead6SMichael Halcrow 			goto out;
352237fead6SMichael Halcrow 		}
353237fead6SMichael Halcrow 		memcpy(mount_crypt_stat->global_default_cipher_name,
354237fead6SMichael Halcrow 		       ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
355237fead6SMichael Halcrow 		mount_crypt_stat->global_default_cipher_name[cipher_name_len]
356237fead6SMichael Halcrow 		    = '\0';
357237fead6SMichael Halcrow 	}
358237fead6SMichael Halcrow 	if (!cipher_key_bytes_set) {
359e5d9cbdeSMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size = 0;
360237fead6SMichael Halcrow 	}
361237fead6SMichael Halcrow 	rc = ecryptfs_process_cipher(
362237fead6SMichael Halcrow 		&mount_crypt_stat->global_key_tfm,
363237fead6SMichael Halcrow 		mount_crypt_stat->global_default_cipher_name,
364e5d9cbdeSMichael Halcrow 		&mount_crypt_stat->global_default_cipher_key_size);
365237fead6SMichael Halcrow 	if (rc) {
366237fead6SMichael Halcrow 		printk(KERN_ERR "Error attempting to initialize cipher [%s] "
367237fead6SMichael Halcrow 		       "with key size [%Zd] bytes; rc = [%d]\n",
368237fead6SMichael Halcrow 		       mount_crypt_stat->global_default_cipher_name,
369237fead6SMichael Halcrow 		       mount_crypt_stat->global_default_cipher_key_size, rc);
3708bba066fSMichael Halcrow 		mount_crypt_stat->global_key_tfm = NULL;
3718bba066fSMichael Halcrow 		mount_crypt_stat->global_auth_tok_key = NULL;
372237fead6SMichael Halcrow 		rc = -EINVAL;
373237fead6SMichael Halcrow 		goto out;
374237fead6SMichael Halcrow 	}
375237fead6SMichael Halcrow 	mutex_init(&mount_crypt_stat->global_key_tfm_mutex);
376237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Requesting the key with description: "
377237fead6SMichael Halcrow 			"[%s]\n", mount_crypt_stat->global_auth_tok_sig);
378237fead6SMichael Halcrow 	/* The reference to this key is held until umount is done The
379237fead6SMichael Halcrow 	 * call to key_put is done in ecryptfs_put_super() */
380237fead6SMichael Halcrow 	auth_tok_key = request_key(&key_type_user,
381237fead6SMichael Halcrow 				   mount_crypt_stat->global_auth_tok_sig,
382237fead6SMichael Halcrow 				   NULL);
383237fead6SMichael Halcrow 	if (!auth_tok_key || IS_ERR(auth_tok_key)) {
384237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Could not find key with "
385237fead6SMichael Halcrow 				"description: [%s]\n",
386237fead6SMichael Halcrow 				mount_crypt_stat->global_auth_tok_sig);
387237fead6SMichael Halcrow 		process_request_key_err(PTR_ERR(auth_tok_key));
388237fead6SMichael Halcrow 		rc = -EINVAL;
389237fead6SMichael Halcrow 		goto out;
390237fead6SMichael Halcrow 	}
391237fead6SMichael Halcrow 	auth_tok = ecryptfs_get_key_payload_data(auth_tok_key);
392237fead6SMichael Halcrow 	if (ecryptfs_verify_version(auth_tok->version)) {
393237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Data structure version mismatch. "
394237fead6SMichael Halcrow 				"Userspace tools must match eCryptfs kernel "
395237fead6SMichael Halcrow 				"module with major version [%d] and minor "
396237fead6SMichael Halcrow 				"version [%d]\n", ECRYPTFS_VERSION_MAJOR,
397237fead6SMichael Halcrow 				ECRYPTFS_VERSION_MINOR);
398237fead6SMichael Halcrow 		rc = -EINVAL;
399237fead6SMichael Halcrow 		goto out;
400237fead6SMichael Halcrow 	}
401dddfa461SMichael Halcrow 	if (auth_tok->token_type != ECRYPTFS_PASSWORD
402dddfa461SMichael Halcrow 	    && auth_tok->token_type != ECRYPTFS_PRIVATE_KEY) {
403237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Invalid auth_tok structure "
404dddfa461SMichael Halcrow 				"returned from key query\n");
405237fead6SMichael Halcrow 		rc = -EINVAL;
406237fead6SMichael Halcrow 		goto out;
407237fead6SMichael Halcrow 	}
408237fead6SMichael Halcrow 	mount_crypt_stat->global_auth_tok_key = auth_tok_key;
409237fead6SMichael Halcrow 	mount_crypt_stat->global_auth_tok = auth_tok;
410237fead6SMichael Halcrow out:
411237fead6SMichael Halcrow 	return rc;
412237fead6SMichael Halcrow }
413237fead6SMichael Halcrow 
414237fead6SMichael Halcrow struct kmem_cache *ecryptfs_sb_info_cache;
415237fead6SMichael Halcrow 
416237fead6SMichael Halcrow /**
417237fead6SMichael Halcrow  * ecryptfs_fill_super
418237fead6SMichael Halcrow  * @sb: The ecryptfs super block
419237fead6SMichael Halcrow  * @raw_data: The options passed to mount
420237fead6SMichael Halcrow  * @silent: Not used but required by function prototype
421237fead6SMichael Halcrow  *
422237fead6SMichael Halcrow  * Sets up what we can of the sb, rest is done in ecryptfs_read_super
423237fead6SMichael Halcrow  *
424237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
425237fead6SMichael Halcrow  */
426237fead6SMichael Halcrow static int
427237fead6SMichael Halcrow ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
428237fead6SMichael Halcrow {
429237fead6SMichael Halcrow 	int rc = 0;
430237fead6SMichael Halcrow 
431237fead6SMichael Halcrow 	/* Released in ecryptfs_put_super() */
432237fead6SMichael Halcrow 	ecryptfs_set_superblock_private(sb,
433c3762229SRobert P. J. Day 					kmem_cache_zalloc(ecryptfs_sb_info_cache,
434e94b1766SChristoph Lameter 							 GFP_KERNEL));
435237fead6SMichael Halcrow 	if (!ecryptfs_superblock_to_private(sb)) {
436237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Out of memory\n");
437237fead6SMichael Halcrow 		rc = -ENOMEM;
438237fead6SMichael Halcrow 		goto out;
439237fead6SMichael Halcrow 	}
440237fead6SMichael Halcrow 	sb->s_op = &ecryptfs_sops;
441237fead6SMichael Halcrow 	/* Released through deactivate_super(sb) from get_sb_nodev */
442237fead6SMichael Halcrow 	sb->s_root = d_alloc(NULL, &(const struct qstr) {
443237fead6SMichael Halcrow 			     .hash = 0,.name = "/",.len = 1});
444237fead6SMichael Halcrow 	if (!sb->s_root) {
445237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
446237fead6SMichael Halcrow 		rc = -ENOMEM;
447237fead6SMichael Halcrow 		goto out;
448237fead6SMichael Halcrow 	}
449237fead6SMichael Halcrow 	sb->s_root->d_op = &ecryptfs_dops;
450237fead6SMichael Halcrow 	sb->s_root->d_sb = sb;
451237fead6SMichael Halcrow 	sb->s_root->d_parent = sb->s_root;
452237fead6SMichael Halcrow 	/* Released in d_release when dput(sb->s_root) is called */
453237fead6SMichael Halcrow 	/* through deactivate_super(sb) from get_sb_nodev() */
454237fead6SMichael Halcrow 	ecryptfs_set_dentry_private(sb->s_root,
455c3762229SRobert P. J. Day 				    kmem_cache_zalloc(ecryptfs_dentry_info_cache,
456e94b1766SChristoph Lameter 						     GFP_KERNEL));
457237fead6SMichael Halcrow 	if (!ecryptfs_dentry_to_private(sb->s_root)) {
458237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
459237fead6SMichael Halcrow 				"dentry_info_cache alloc failed\n");
460237fead6SMichael Halcrow 		rc = -ENOMEM;
461237fead6SMichael Halcrow 		goto out;
462237fead6SMichael Halcrow 	}
463237fead6SMichael Halcrow 	rc = 0;
464237fead6SMichael Halcrow out:
465237fead6SMichael Halcrow 	/* Should be able to rely on deactivate_super called from
466237fead6SMichael Halcrow 	 * get_sb_nodev */
467237fead6SMichael Halcrow 	return rc;
468237fead6SMichael Halcrow }
469237fead6SMichael Halcrow 
470237fead6SMichael Halcrow /**
471237fead6SMichael Halcrow  * ecryptfs_read_super
472237fead6SMichael Halcrow  * @sb: The ecryptfs super block
473237fead6SMichael Halcrow  * @dev_name: The path to mount over
474237fead6SMichael Halcrow  *
475237fead6SMichael Halcrow  * Read the super block of the lower filesystem, and use
476237fead6SMichael Halcrow  * ecryptfs_interpose to create our initial inode and super block
477237fead6SMichael Halcrow  * struct.
478237fead6SMichael Halcrow  */
479237fead6SMichael Halcrow static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
480237fead6SMichael Halcrow {
481237fead6SMichael Halcrow 	int rc;
482237fead6SMichael Halcrow 	struct nameidata nd;
483237fead6SMichael Halcrow 	struct dentry *lower_root;
484237fead6SMichael Halcrow 	struct vfsmount *lower_mnt;
485237fead6SMichael Halcrow 
486237fead6SMichael Halcrow 	memset(&nd, 0, sizeof(struct nameidata));
48782b16528SDmitriy Monakhov 	rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
488237fead6SMichael Halcrow 	if (rc) {
489237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
49065dc8145SMichael Halcrow 		goto out;
491237fead6SMichael Halcrow 	}
492237fead6SMichael Halcrow 	lower_root = nd.dentry;
493237fead6SMichael Halcrow 	lower_mnt = nd.mnt;
494237fead6SMichael Halcrow 	ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
495237fead6SMichael Halcrow 	sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
496237fead6SMichael Halcrow 	ecryptfs_set_dentry_lower(sb->s_root, lower_root);
497237fead6SMichael Halcrow 	ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
498237fead6SMichael Halcrow 	if ((rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0)))
499237fead6SMichael Halcrow 		goto out_free;
500237fead6SMichael Halcrow 	rc = 0;
501237fead6SMichael Halcrow 	goto out;
502237fead6SMichael Halcrow out_free:
503237fead6SMichael Halcrow 	path_release(&nd);
504237fead6SMichael Halcrow out:
505237fead6SMichael Halcrow 	return rc;
506237fead6SMichael Halcrow }
507237fead6SMichael Halcrow 
508237fead6SMichael Halcrow /**
509237fead6SMichael Halcrow  * ecryptfs_get_sb
510237fead6SMichael Halcrow  * @fs_type
511237fead6SMichael Halcrow  * @flags
512237fead6SMichael Halcrow  * @dev_name: The path to mount over
513237fead6SMichael Halcrow  * @raw_data: The options passed into the kernel
514237fead6SMichael Halcrow  *
515237fead6SMichael Halcrow  * The whole ecryptfs_get_sb process is broken into 4 functions:
516237fead6SMichael Halcrow  * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
517237fead6SMichael Halcrow  * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
518237fead6SMichael Halcrow  *                        with as much information as it can before needing
519237fead6SMichael Halcrow  *                        the lower filesystem.
520237fead6SMichael Halcrow  * ecryptfs_read_super(): this accesses the lower filesystem and uses
521237fead6SMichael Halcrow  *                        ecryptfs_interpolate to perform most of the linking
522237fead6SMichael Halcrow  * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
523237fead6SMichael Halcrow  */
524237fead6SMichael Halcrow static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
525237fead6SMichael Halcrow 			const char *dev_name, void *raw_data,
526237fead6SMichael Halcrow 			struct vfsmount *mnt)
527237fead6SMichael Halcrow {
528237fead6SMichael Halcrow 	int rc;
529237fead6SMichael Halcrow 	struct super_block *sb;
530237fead6SMichael Halcrow 
531237fead6SMichael Halcrow 	rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
532237fead6SMichael Halcrow 	if (rc < 0) {
533237fead6SMichael Halcrow 		printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
534237fead6SMichael Halcrow 		goto out;
535237fead6SMichael Halcrow 	}
536237fead6SMichael Halcrow 	sb = mnt->mnt_sb;
537237fead6SMichael Halcrow 	rc = ecryptfs_parse_options(sb, raw_data);
538237fead6SMichael Halcrow 	if (rc) {
539237fead6SMichael Halcrow 		printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
540237fead6SMichael Halcrow 		goto out_abort;
541237fead6SMichael Halcrow 	}
542237fead6SMichael Halcrow 	rc = ecryptfs_read_super(sb, dev_name);
543237fead6SMichael Halcrow 	if (rc) {
544237fead6SMichael Halcrow 		printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
545237fead6SMichael Halcrow 		goto out_abort;
546237fead6SMichael Halcrow 	}
547237fead6SMichael Halcrow 	goto out;
548237fead6SMichael Halcrow out_abort:
549237fead6SMichael Halcrow 	dput(sb->s_root);
550237fead6SMichael Halcrow 	up_write(&sb->s_umount);
551237fead6SMichael Halcrow 	deactivate_super(sb);
552237fead6SMichael Halcrow out:
553237fead6SMichael Halcrow 	return rc;
554237fead6SMichael Halcrow }
555237fead6SMichael Halcrow 
556237fead6SMichael Halcrow /**
557237fead6SMichael Halcrow  * ecryptfs_kill_block_super
558237fead6SMichael Halcrow  * @sb: The ecryptfs super block
559237fead6SMichael Halcrow  *
560237fead6SMichael Halcrow  * Used to bring the superblock down and free the private data.
561237fead6SMichael Halcrow  * Private data is free'd in ecryptfs_put_super()
562237fead6SMichael Halcrow  */
563237fead6SMichael Halcrow static void ecryptfs_kill_block_super(struct super_block *sb)
564237fead6SMichael Halcrow {
565237fead6SMichael Halcrow 	generic_shutdown_super(sb);
566237fead6SMichael Halcrow }
567237fead6SMichael Halcrow 
568237fead6SMichael Halcrow static struct file_system_type ecryptfs_fs_type = {
569237fead6SMichael Halcrow 	.owner = THIS_MODULE,
570237fead6SMichael Halcrow 	.name = "ecryptfs",
571237fead6SMichael Halcrow 	.get_sb = ecryptfs_get_sb,
572237fead6SMichael Halcrow 	.kill_sb = ecryptfs_kill_block_super,
573237fead6SMichael Halcrow 	.fs_flags = 0
574237fead6SMichael Halcrow };
575237fead6SMichael Halcrow 
576237fead6SMichael Halcrow /**
577237fead6SMichael Halcrow  * inode_info_init_once
578237fead6SMichael Halcrow  *
579237fead6SMichael Halcrow  * Initializes the ecryptfs_inode_info_cache when it is created
580237fead6SMichael Halcrow  */
581237fead6SMichael Halcrow static void
582237fead6SMichael Halcrow inode_info_init_once(void *vptr, struct kmem_cache *cachep, unsigned long flags)
583237fead6SMichael Halcrow {
584237fead6SMichael Halcrow 	struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
585237fead6SMichael Halcrow 
586237fead6SMichael Halcrow 	inode_init_once(&ei->vfs_inode);
587237fead6SMichael Halcrow }
588237fead6SMichael Halcrow 
589237fead6SMichael Halcrow static struct ecryptfs_cache_info {
590e18b890bSChristoph Lameter 	struct kmem_cache **cache;
591237fead6SMichael Halcrow 	const char *name;
592237fead6SMichael Halcrow 	size_t size;
593237fead6SMichael Halcrow 	void (*ctor)(void*, struct kmem_cache *, unsigned long);
594237fead6SMichael Halcrow } ecryptfs_cache_infos[] = {
595237fead6SMichael Halcrow 	{
596237fead6SMichael Halcrow 		.cache = &ecryptfs_auth_tok_list_item_cache,
597237fead6SMichael Halcrow 		.name = "ecryptfs_auth_tok_list_item",
598237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_auth_tok_list_item),
599237fead6SMichael Halcrow 	},
600237fead6SMichael Halcrow 	{
601237fead6SMichael Halcrow 		.cache = &ecryptfs_file_info_cache,
602237fead6SMichael Halcrow 		.name = "ecryptfs_file_cache",
603237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_file_info),
604237fead6SMichael Halcrow 	},
605237fead6SMichael Halcrow 	{
606237fead6SMichael Halcrow 		.cache = &ecryptfs_dentry_info_cache,
607237fead6SMichael Halcrow 		.name = "ecryptfs_dentry_info_cache",
608237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_dentry_info),
609237fead6SMichael Halcrow 	},
610237fead6SMichael Halcrow 	{
611237fead6SMichael Halcrow 		.cache = &ecryptfs_inode_info_cache,
612237fead6SMichael Halcrow 		.name = "ecryptfs_inode_cache",
613237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_inode_info),
614237fead6SMichael Halcrow 		.ctor = inode_info_init_once,
615237fead6SMichael Halcrow 	},
616237fead6SMichael Halcrow 	{
617237fead6SMichael Halcrow 		.cache = &ecryptfs_sb_info_cache,
618237fead6SMichael Halcrow 		.name = "ecryptfs_sb_cache",
619237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_sb_info),
620237fead6SMichael Halcrow 	},
621237fead6SMichael Halcrow 	{
622237fead6SMichael Halcrow 		.cache = &ecryptfs_header_cache_0,
623237fead6SMichael Halcrow 		.name = "ecryptfs_headers_0",
624237fead6SMichael Halcrow 		.size = PAGE_CACHE_SIZE,
625237fead6SMichael Halcrow 	},
626237fead6SMichael Halcrow 	{
627237fead6SMichael Halcrow 		.cache = &ecryptfs_header_cache_1,
628237fead6SMichael Halcrow 		.name = "ecryptfs_headers_1",
629237fead6SMichael Halcrow 		.size = PAGE_CACHE_SIZE,
630237fead6SMichael Halcrow 	},
631237fead6SMichael Halcrow 	{
632237fead6SMichael Halcrow 		.cache = &ecryptfs_header_cache_2,
633237fead6SMichael Halcrow 		.name = "ecryptfs_headers_2",
634237fead6SMichael Halcrow 		.size = PAGE_CACHE_SIZE,
635237fead6SMichael Halcrow 	},
636237fead6SMichael Halcrow 	{
637dd2a3b7aSMichael Halcrow 		.cache = &ecryptfs_xattr_cache,
638dd2a3b7aSMichael Halcrow 		.name = "ecryptfs_xattr_cache",
639dd2a3b7aSMichael Halcrow 		.size = PAGE_CACHE_SIZE,
640dd2a3b7aSMichael Halcrow 	},
641dd2a3b7aSMichael Halcrow 	{
642237fead6SMichael Halcrow 		.cache = &ecryptfs_lower_page_cache,
643237fead6SMichael Halcrow 		.name = "ecryptfs_lower_page_cache",
644237fead6SMichael Halcrow 		.size = PAGE_CACHE_SIZE,
645237fead6SMichael Halcrow 	},
646eb95e7ffSMichael Halcrow 	{
647eb95e7ffSMichael Halcrow 		.cache = &ecryptfs_key_record_cache,
648eb95e7ffSMichael Halcrow 		.name = "ecryptfs_key_record_cache",
649eb95e7ffSMichael Halcrow 		.size = sizeof(struct ecryptfs_key_record),
650eb95e7ffSMichael Halcrow 	},
651237fead6SMichael Halcrow };
652237fead6SMichael Halcrow 
653237fead6SMichael Halcrow static void ecryptfs_free_kmem_caches(void)
654237fead6SMichael Halcrow {
655237fead6SMichael Halcrow 	int i;
656237fead6SMichael Halcrow 
657237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
658237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
659237fead6SMichael Halcrow 
660237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
661237fead6SMichael Halcrow 		if (*(info->cache))
662237fead6SMichael Halcrow 			kmem_cache_destroy(*(info->cache));
663237fead6SMichael Halcrow 	}
664237fead6SMichael Halcrow }
665237fead6SMichael Halcrow 
666237fead6SMichael Halcrow /**
667237fead6SMichael Halcrow  * ecryptfs_init_kmem_caches
668237fead6SMichael Halcrow  *
669237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
670237fead6SMichael Halcrow  */
671237fead6SMichael Halcrow static int ecryptfs_init_kmem_caches(void)
672237fead6SMichael Halcrow {
673237fead6SMichael Halcrow 	int i;
674237fead6SMichael Halcrow 
675237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
676237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
677237fead6SMichael Halcrow 
678237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
679237fead6SMichael Halcrow 		*(info->cache) = kmem_cache_create(info->name, info->size,
68020c2df83SPaul Mundt 				0, SLAB_HWCACHE_ALIGN, info->ctor);
681237fead6SMichael Halcrow 		if (!*(info->cache)) {
682237fead6SMichael Halcrow 			ecryptfs_free_kmem_caches();
683237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "%s: "
684237fead6SMichael Halcrow 					"kmem_cache_create failed\n",
685237fead6SMichael Halcrow 					info->name);
686237fead6SMichael Halcrow 			return -ENOMEM;
687237fead6SMichael Halcrow 		}
688237fead6SMichael Halcrow 	}
689237fead6SMichael Halcrow 	return 0;
690237fead6SMichael Halcrow }
691237fead6SMichael Halcrow 
692237fead6SMichael Halcrow struct ecryptfs_obj {
693237fead6SMichael Halcrow 	char *name;
694237fead6SMichael Halcrow 	struct list_head slot_list;
695237fead6SMichael Halcrow 	struct kobject kobj;
696237fead6SMichael Halcrow };
697237fead6SMichael Halcrow 
698237fead6SMichael Halcrow struct ecryptfs_attribute {
699237fead6SMichael Halcrow 	struct attribute attr;
700237fead6SMichael Halcrow 	ssize_t(*show) (struct ecryptfs_obj *, char *);
701237fead6SMichael Halcrow 	ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t);
702237fead6SMichael Halcrow };
703237fead6SMichael Halcrow 
704237fead6SMichael Halcrow static ssize_t
705237fead6SMichael Halcrow ecryptfs_attr_store(struct kobject *kobj,
706237fead6SMichael Halcrow 		    struct attribute *attr, const char *buf, size_t len)
707237fead6SMichael Halcrow {
708237fead6SMichael Halcrow 	struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
709237fead6SMichael Halcrow 						kobj);
710237fead6SMichael Halcrow 	struct ecryptfs_attribute *attribute =
711237fead6SMichael Halcrow 		container_of(attr, struct ecryptfs_attribute, attr);
712237fead6SMichael Halcrow 
713237fead6SMichael Halcrow 	return (attribute->store ? attribute->store(obj, buf, len) : 0);
714237fead6SMichael Halcrow }
715237fead6SMichael Halcrow 
716237fead6SMichael Halcrow static ssize_t
717237fead6SMichael Halcrow ecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
718237fead6SMichael Halcrow {
719237fead6SMichael Halcrow 	struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
720237fead6SMichael Halcrow 						kobj);
721237fead6SMichael Halcrow 	struct ecryptfs_attribute *attribute =
722237fead6SMichael Halcrow 		container_of(attr, struct ecryptfs_attribute, attr);
723237fead6SMichael Halcrow 
724237fead6SMichael Halcrow 	return (attribute->show ? attribute->show(obj, buf) : 0);
725237fead6SMichael Halcrow }
726237fead6SMichael Halcrow 
727237fead6SMichael Halcrow static struct sysfs_ops ecryptfs_sysfs_ops = {
728237fead6SMichael Halcrow 	.show = ecryptfs_attr_show,
729237fead6SMichael Halcrow 	.store = ecryptfs_attr_store
730237fead6SMichael Halcrow };
731237fead6SMichael Halcrow 
732237fead6SMichael Halcrow static struct kobj_type ecryptfs_ktype = {
733237fead6SMichael Halcrow 	.sysfs_ops = &ecryptfs_sysfs_ops
734237fead6SMichael Halcrow };
735237fead6SMichael Halcrow 
736237fead6SMichael Halcrow static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL);
737237fead6SMichael Halcrow 
738237fead6SMichael Halcrow static ssize_t version_show(struct ecryptfs_obj *obj, char *buff)
739237fead6SMichael Halcrow {
740237fead6SMichael Halcrow 	return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
741237fead6SMichael Halcrow }
742237fead6SMichael Halcrow 
743237fead6SMichael Halcrow static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version);
744237fead6SMichael Halcrow 
7458487f2e4SAdrian Bunk static struct ecryptfs_version_str_map_elem {
746237fead6SMichael Halcrow 	u32 flag;
747237fead6SMichael Halcrow 	char *str;
748237fead6SMichael Halcrow } ecryptfs_version_str_map[] = {
749237fead6SMichael Halcrow 	{ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"},
750237fead6SMichael Halcrow 	{ECRYPTFS_VERSIONING_PUBKEY, "pubkey"},
751237fead6SMichael Halcrow 	{ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"},
75217398957SMichael Halcrow 	{ECRYPTFS_VERSIONING_POLICY, "policy"},
75317398957SMichael Halcrow 	{ECRYPTFS_VERSIONING_XATTR, "metadata in extended attribute"}
754237fead6SMichael Halcrow };
755237fead6SMichael Halcrow 
756237fead6SMichael Halcrow static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff)
757237fead6SMichael Halcrow {
758237fead6SMichael Halcrow 	int i;
759237fead6SMichael Halcrow 	int remaining = PAGE_SIZE;
760237fead6SMichael Halcrow 	int total_written = 0;
761237fead6SMichael Halcrow 
762237fead6SMichael Halcrow 	buff[0] = '\0';
763237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) {
764237fead6SMichael Halcrow 		int entry_size;
765237fead6SMichael Halcrow 
766237fead6SMichael Halcrow 		if (!(ECRYPTFS_VERSIONING_MASK
767237fead6SMichael Halcrow 		      & ecryptfs_version_str_map[i].flag))
768237fead6SMichael Halcrow 			continue;
769237fead6SMichael Halcrow 		entry_size = strlen(ecryptfs_version_str_map[i].str);
770237fead6SMichael Halcrow 		if ((entry_size + 2) > remaining)
771237fead6SMichael Halcrow 			goto out;
772237fead6SMichael Halcrow 		memcpy(buff, ecryptfs_version_str_map[i].str, entry_size);
773237fead6SMichael Halcrow 		buff[entry_size++] = '\n';
774237fead6SMichael Halcrow 		buff[entry_size] = '\0';
775237fead6SMichael Halcrow 		buff += entry_size;
776237fead6SMichael Halcrow 		total_written += entry_size;
777237fead6SMichael Halcrow 		remaining -= entry_size;
778237fead6SMichael Halcrow 	}
779237fead6SMichael Halcrow out:
780237fead6SMichael Halcrow 	return total_written;
781237fead6SMichael Halcrow }
782237fead6SMichael Halcrow 
783237fead6SMichael Halcrow static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str);
784237fead6SMichael Halcrow 
785237fead6SMichael Halcrow static int do_sysfs_registration(void)
786237fead6SMichael Halcrow {
787237fead6SMichael Halcrow 	int rc;
788237fead6SMichael Halcrow 
789237fead6SMichael Halcrow 	if ((rc = subsystem_register(&ecryptfs_subsys))) {
790237fead6SMichael Halcrow 		printk(KERN_ERR
791237fead6SMichael Halcrow 		       "Unable to register ecryptfs sysfs subsystem\n");
792237fead6SMichael Halcrow 		goto out;
793237fead6SMichael Halcrow 	}
794823bccfcSGreg Kroah-Hartman 	rc = sysfs_create_file(&ecryptfs_subsys.kobj,
795237fead6SMichael Halcrow 			       &sysfs_attr_version.attr);
796237fead6SMichael Halcrow 	if (rc) {
797237fead6SMichael Halcrow 		printk(KERN_ERR
798237fead6SMichael Halcrow 		       "Unable to create ecryptfs version attribute\n");
799237fead6SMichael Halcrow 		subsystem_unregister(&ecryptfs_subsys);
800237fead6SMichael Halcrow 		goto out;
801237fead6SMichael Halcrow 	}
802823bccfcSGreg Kroah-Hartman 	rc = sysfs_create_file(&ecryptfs_subsys.kobj,
803237fead6SMichael Halcrow 			       &sysfs_attr_version_str.attr);
804237fead6SMichael Halcrow 	if (rc) {
805237fead6SMichael Halcrow 		printk(KERN_ERR
806237fead6SMichael Halcrow 		       "Unable to create ecryptfs version_str attribute\n");
807823bccfcSGreg Kroah-Hartman 		sysfs_remove_file(&ecryptfs_subsys.kobj,
808237fead6SMichael Halcrow 				  &sysfs_attr_version.attr);
809237fead6SMichael Halcrow 		subsystem_unregister(&ecryptfs_subsys);
810237fead6SMichael Halcrow 		goto out;
811237fead6SMichael Halcrow 	}
812237fead6SMichael Halcrow out:
813237fead6SMichael Halcrow 	return rc;
814237fead6SMichael Halcrow }
815237fead6SMichael Halcrow 
816*a75de1b3SRyusuke Konishi static void do_sysfs_unregistration(void)
817*a75de1b3SRyusuke Konishi {
818*a75de1b3SRyusuke Konishi 	sysfs_remove_file(&ecryptfs_subsys.kobj,
819*a75de1b3SRyusuke Konishi 			  &sysfs_attr_version.attr);
820*a75de1b3SRyusuke Konishi 	sysfs_remove_file(&ecryptfs_subsys.kobj,
821*a75de1b3SRyusuke Konishi 			  &sysfs_attr_version_str.attr);
822*a75de1b3SRyusuke Konishi 	subsystem_unregister(&ecryptfs_subsys);
823*a75de1b3SRyusuke Konishi }
824*a75de1b3SRyusuke Konishi 
825237fead6SMichael Halcrow static int __init ecryptfs_init(void)
826237fead6SMichael Halcrow {
827237fead6SMichael Halcrow 	int rc;
828237fead6SMichael Halcrow 
829237fead6SMichael Halcrow 	if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
830237fead6SMichael Halcrow 		rc = -EINVAL;
831237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
832237fead6SMichael Halcrow 				"larger than the host's page size, and so "
833237fead6SMichael Halcrow 				"eCryptfs cannot run on this system. The "
834237fead6SMichael Halcrow 				"default eCryptfs extent size is [%d] bytes; "
835237fead6SMichael Halcrow 				"the page size is [%d] bytes.\n",
836237fead6SMichael Halcrow 				ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
837237fead6SMichael Halcrow 		goto out;
838237fead6SMichael Halcrow 	}
839237fead6SMichael Halcrow 	rc = ecryptfs_init_kmem_caches();
840237fead6SMichael Halcrow 	if (rc) {
841237fead6SMichael Halcrow 		printk(KERN_ERR
842237fead6SMichael Halcrow 		       "Failed to allocate one or more kmem_cache objects\n");
843237fead6SMichael Halcrow 		goto out;
844237fead6SMichael Halcrow 	}
845237fead6SMichael Halcrow 	rc = register_filesystem(&ecryptfs_fs_type);
846237fead6SMichael Halcrow 	if (rc) {
847237fead6SMichael Halcrow 		printk(KERN_ERR "Failed to register filesystem\n");
848237fead6SMichael Halcrow 		ecryptfs_free_kmem_caches();
849237fead6SMichael Halcrow 		goto out;
850237fead6SMichael Halcrow 	}
851823bccfcSGreg Kroah-Hartman 	kobj_set_kset_s(&ecryptfs_subsys, fs_subsys);
852237fead6SMichael Halcrow 	rc = do_sysfs_registration();
853237fead6SMichael Halcrow 	if (rc) {
854237fead6SMichael Halcrow 		printk(KERN_ERR "sysfs registration failed\n");
855237fead6SMichael Halcrow 		unregister_filesystem(&ecryptfs_fs_type);
856237fead6SMichael Halcrow 		ecryptfs_free_kmem_caches();
857237fead6SMichael Halcrow 		goto out;
858237fead6SMichael Halcrow 	}
859dddfa461SMichael Halcrow 	rc = ecryptfs_init_messaging(ecryptfs_transport);
860dddfa461SMichael Halcrow 	if (rc) {
861dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Failure occured while attempting to "
862dddfa461SMichael Halcrow 				"initialize the eCryptfs netlink socket\n");
863*a75de1b3SRyusuke Konishi 		do_sysfs_unregistration();
864*a75de1b3SRyusuke Konishi 		unregister_filesystem(&ecryptfs_fs_type);
865*a75de1b3SRyusuke Konishi 		ecryptfs_free_kmem_caches();
866dddfa461SMichael Halcrow 	}
867237fead6SMichael Halcrow out:
868237fead6SMichael Halcrow 	return rc;
869237fead6SMichael Halcrow }
870237fead6SMichael Halcrow 
871237fead6SMichael Halcrow static void __exit ecryptfs_exit(void)
872237fead6SMichael Halcrow {
873*a75de1b3SRyusuke Konishi 	do_sysfs_unregistration();
874dddfa461SMichael Halcrow 	ecryptfs_release_messaging(ecryptfs_transport);
875237fead6SMichael Halcrow 	unregister_filesystem(&ecryptfs_fs_type);
876237fead6SMichael Halcrow 	ecryptfs_free_kmem_caches();
877237fead6SMichael Halcrow }
878237fead6SMichael Halcrow 
879237fead6SMichael Halcrow MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
880237fead6SMichael Halcrow MODULE_DESCRIPTION("eCryptfs");
881237fead6SMichael Halcrow 
882237fead6SMichael Halcrow MODULE_LICENSE("GPL");
883237fead6SMichael Halcrow 
884237fead6SMichael Halcrow module_init(ecryptfs_init)
885237fead6SMichael Halcrow module_exit(ecryptfs_exit)
886