xref: /linux/fs/ecryptfs/main.c (revision 367b8112fe2ea5c39a7bb4d263dcdd9b612fae18)
1 /**
2  * eCryptfs: Linux filesystem encryption layer
3  *
4  * Copyright (C) 1997-2003 Erez Zadok
5  * Copyright (C) 2001-2003 Stony Brook University
6  * Copyright (C) 2004-2007 International Business Machines Corp.
7  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8  *              Michael C. Thompson <mcthomps@us.ibm.com>
9  *              Tyler Hicks <tyhicks@ou.edu>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of the
14  * License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  * 02111-1307, USA.
25  */
26 
27 #include <linux/dcache.h>
28 #include <linux/file.h>
29 #include <linux/module.h>
30 #include <linux/namei.h>
31 #include <linux/skbuff.h>
32 #include <linux/crypto.h>
33 #include <linux/mount.h>
34 #include <linux/pagemap.h>
35 #include <linux/key.h>
36 #include <linux/parser.h>
37 #include <linux/fs_stack.h>
38 #include "ecryptfs_kernel.h"
39 
40 /**
41  * Module parameter that defines the ecryptfs_verbosity level.
42  */
43 int ecryptfs_verbosity = 0;
44 
45 module_param(ecryptfs_verbosity, int, 0);
46 MODULE_PARM_DESC(ecryptfs_verbosity,
47 		 "Initial verbosity level (0 or 1; defaults to "
48 		 "0, which is Quiet)");
49 
50 /**
51  * Module parameter that defines the number of message buffer elements
52  */
53 unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
54 
55 module_param(ecryptfs_message_buf_len, uint, 0);
56 MODULE_PARM_DESC(ecryptfs_message_buf_len,
57 		 "Number of message buffer elements");
58 
59 /**
60  * Module parameter that defines the maximum guaranteed amount of time to wait
61  * for a response from ecryptfsd.  The actual sleep time will be, more than
62  * likely, a small amount greater than this specified value, but only less if
63  * the message successfully arrives.
64  */
65 signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
66 
67 module_param(ecryptfs_message_wait_timeout, long, 0);
68 MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
69 		 "Maximum number of seconds that an operation will "
70 		 "sleep while waiting for a message response from "
71 		 "userspace");
72 
73 /**
74  * Module parameter that is an estimate of the maximum number of users
75  * that will be concurrently using eCryptfs. Set this to the right
76  * value to balance performance and memory use.
77  */
78 unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
79 
80 module_param(ecryptfs_number_of_users, uint, 0);
81 MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
82 		 "concurrent users of eCryptfs");
83 
84 void __ecryptfs_printk(const char *fmt, ...)
85 {
86 	va_list args;
87 	va_start(args, fmt);
88 	if (fmt[1] == '7') { /* KERN_DEBUG */
89 		if (ecryptfs_verbosity >= 1)
90 			vprintk(fmt, args);
91 	} else
92 		vprintk(fmt, args);
93 	va_end(args);
94 }
95 
96 /**
97  * ecryptfs_init_persistent_file
98  * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
99  *                   the lower dentry and the lower mount set
100  *
101  * eCryptfs only ever keeps a single open file for every lower
102  * inode. All I/O operations to the lower inode occur through that
103  * file. When the first eCryptfs dentry that interposes with the first
104  * lower dentry for that inode is created, this function creates the
105  * persistent file struct and associates it with the eCryptfs
106  * inode. When the eCryptfs inode is destroyed, the file is closed.
107  *
108  * The persistent file will be opened with read/write permissions, if
109  * possible. Otherwise, it is opened read-only.
110  *
111  * This function does nothing if a lower persistent file is already
112  * associated with the eCryptfs inode.
113  *
114  * Returns zero on success; non-zero otherwise
115  */
116 int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
117 {
118 	struct ecryptfs_inode_info *inode_info =
119 		ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
120 	int rc = 0;
121 
122 	mutex_lock(&inode_info->lower_file_mutex);
123 	if (!inode_info->lower_file) {
124 		struct dentry *lower_dentry;
125 		struct vfsmount *lower_mnt =
126 			ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
127 
128 		lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
129 		rc = ecryptfs_privileged_open(&inode_info->lower_file,
130 						     lower_dentry, lower_mnt);
131 		if (rc || IS_ERR(inode_info->lower_file)) {
132 			printk(KERN_ERR "Error opening lower persistent file "
133 			       "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
134 			       "rc = [%d]\n", lower_dentry, lower_mnt, rc);
135 			rc = PTR_ERR(inode_info->lower_file);
136 			inode_info->lower_file = NULL;
137 		}
138 	}
139 	mutex_unlock(&inode_info->lower_file_mutex);
140 	return rc;
141 }
142 
143 /**
144  * ecryptfs_interpose
145  * @lower_dentry: Existing dentry in the lower filesystem
146  * @dentry: ecryptfs' dentry
147  * @sb: ecryptfs's super_block
148  * @flags: flags to govern behavior of interpose procedure
149  *
150  * Interposes upper and lower dentries.
151  *
152  * Returns zero on success; non-zero otherwise
153  */
154 int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
155 		       struct super_block *sb, u32 flags)
156 {
157 	struct inode *lower_inode;
158 	struct inode *inode;
159 	int rc = 0;
160 
161 	lower_inode = lower_dentry->d_inode;
162 	if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
163 		rc = -EXDEV;
164 		goto out;
165 	}
166 	if (!igrab(lower_inode)) {
167 		rc = -ESTALE;
168 		goto out;
169 	}
170 	inode = iget5_locked(sb, (unsigned long)lower_inode,
171 			     ecryptfs_inode_test, ecryptfs_inode_set,
172 			     lower_inode);
173 	if (!inode) {
174 		rc = -EACCES;
175 		iput(lower_inode);
176 		goto out;
177 	}
178 	if (inode->i_state & I_NEW)
179 		unlock_new_inode(inode);
180 	else
181 		iput(lower_inode);
182 	if (S_ISLNK(lower_inode->i_mode))
183 		inode->i_op = &ecryptfs_symlink_iops;
184 	else if (S_ISDIR(lower_inode->i_mode))
185 		inode->i_op = &ecryptfs_dir_iops;
186 	if (S_ISDIR(lower_inode->i_mode))
187 		inode->i_fop = &ecryptfs_dir_fops;
188 	if (special_file(lower_inode->i_mode))
189 		init_special_inode(inode, lower_inode->i_mode,
190 				   lower_inode->i_rdev);
191 	dentry->d_op = &ecryptfs_dops;
192 	if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
193 		d_add(dentry, inode);
194 	else
195 		d_instantiate(dentry, inode);
196 	fsstack_copy_attr_all(inode, lower_inode, NULL);
197 	/* This size will be overwritten for real files w/ headers and
198 	 * other metadata */
199 	fsstack_copy_inode_size(inode, lower_inode);
200 out:
201 	return rc;
202 }
203 
204 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
205        ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
206        ecryptfs_opt_ecryptfs_key_bytes,
207        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
208        ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
209 
210 static const match_table_t tokens = {
211 	{ecryptfs_opt_sig, "sig=%s"},
212 	{ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
213 	{ecryptfs_opt_cipher, "cipher=%s"},
214 	{ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
215 	{ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
216 	{ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
217 	{ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
218 	{ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
219 	{ecryptfs_opt_err, NULL}
220 };
221 
222 static int ecryptfs_init_global_auth_toks(
223 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
224 {
225 	struct ecryptfs_global_auth_tok *global_auth_tok;
226 	int rc = 0;
227 
228 	list_for_each_entry(global_auth_tok,
229 			    &mount_crypt_stat->global_auth_tok_list,
230 			    mount_crypt_stat_list) {
231 		rc = ecryptfs_keyring_auth_tok_for_sig(
232 			&global_auth_tok->global_auth_tok_key,
233 			&global_auth_tok->global_auth_tok,
234 			global_auth_tok->sig);
235 		if (rc) {
236 			printk(KERN_ERR "Could not find valid key in user "
237 			       "session keyring for sig specified in mount "
238 			       "option: [%s]\n", global_auth_tok->sig);
239 			global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
240 			goto out;
241 		} else
242 			global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
243 	}
244 out:
245 	return rc;
246 }
247 
248 static void ecryptfs_init_mount_crypt_stat(
249 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
250 {
251 	memset((void *)mount_crypt_stat, 0,
252 	       sizeof(struct ecryptfs_mount_crypt_stat));
253 	INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
254 	mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
255 	mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
256 }
257 
258 /**
259  * ecryptfs_parse_options
260  * @sb: The ecryptfs super block
261  * @options: The options pased to the kernel
262  *
263  * Parse mount options:
264  * debug=N 	   - ecryptfs_verbosity level for debug output
265  * sig=XXX	   - description(signature) of the key to use
266  *
267  * Returns the dentry object of the lower-level (lower/interposed)
268  * directory; We want to mount our stackable file system on top of
269  * that lower directory.
270  *
271  * The signature of the key to use must be the description of a key
272  * already in the keyring. Mounting will fail if the key can not be
273  * found.
274  *
275  * Returns zero on success; non-zero on error
276  */
277 static int ecryptfs_parse_options(struct super_block *sb, char *options)
278 {
279 	char *p;
280 	int rc = 0;
281 	int sig_set = 0;
282 	int cipher_name_set = 0;
283 	int cipher_key_bytes;
284 	int cipher_key_bytes_set = 0;
285 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
286 		&ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
287 	substring_t args[MAX_OPT_ARGS];
288 	int token;
289 	char *sig_src;
290 	char *cipher_name_dst;
291 	char *cipher_name_src;
292 	char *cipher_key_bytes_src;
293 
294 	if (!options) {
295 		rc = -EINVAL;
296 		goto out;
297 	}
298 	ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
299 	while ((p = strsep(&options, ",")) != NULL) {
300 		if (!*p)
301 			continue;
302 		token = match_token(p, tokens, args);
303 		switch (token) {
304 		case ecryptfs_opt_sig:
305 		case ecryptfs_opt_ecryptfs_sig:
306 			sig_src = args[0].from;
307 			rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
308 							  sig_src);
309 			if (rc) {
310 				printk(KERN_ERR "Error attempting to register "
311 				       "global sig; rc = [%d]\n", rc);
312 				goto out;
313 			}
314 			sig_set = 1;
315 			break;
316 		case ecryptfs_opt_cipher:
317 		case ecryptfs_opt_ecryptfs_cipher:
318 			cipher_name_src = args[0].from;
319 			cipher_name_dst =
320 				mount_crypt_stat->
321 				global_default_cipher_name;
322 			strncpy(cipher_name_dst, cipher_name_src,
323 				ECRYPTFS_MAX_CIPHER_NAME_SIZE);
324 			ecryptfs_printk(KERN_DEBUG,
325 					"The mount_crypt_stat "
326 					"global_default_cipher_name set to: "
327 					"[%s]\n", cipher_name_dst);
328 			cipher_name_set = 1;
329 			break;
330 		case ecryptfs_opt_ecryptfs_key_bytes:
331 			cipher_key_bytes_src = args[0].from;
332 			cipher_key_bytes =
333 				(int)simple_strtol(cipher_key_bytes_src,
334 						   &cipher_key_bytes_src, 0);
335 			mount_crypt_stat->global_default_cipher_key_size =
336 				cipher_key_bytes;
337 			ecryptfs_printk(KERN_DEBUG,
338 					"The mount_crypt_stat "
339 					"global_default_cipher_key_size "
340 					"set to: [%d]\n", mount_crypt_stat->
341 					global_default_cipher_key_size);
342 			cipher_key_bytes_set = 1;
343 			break;
344 		case ecryptfs_opt_passthrough:
345 			mount_crypt_stat->flags |=
346 				ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
347 			break;
348 		case ecryptfs_opt_xattr_metadata:
349 			mount_crypt_stat->flags |=
350 				ECRYPTFS_XATTR_METADATA_ENABLED;
351 			break;
352 		case ecryptfs_opt_encrypted_view:
353 			mount_crypt_stat->flags |=
354 				ECRYPTFS_XATTR_METADATA_ENABLED;
355 			mount_crypt_stat->flags |=
356 				ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
357 			break;
358 		case ecryptfs_opt_err:
359 		default:
360 			ecryptfs_printk(KERN_WARNING,
361 					"eCryptfs: unrecognized option '%s'\n",
362 					p);
363 		}
364 	}
365 	if (!sig_set) {
366 		rc = -EINVAL;
367 		ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
368 				"auth tok signature as a mount "
369 				"parameter; see the eCryptfs README\n");
370 		goto out;
371 	}
372 	if (!cipher_name_set) {
373 		int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
374 
375 		BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
376 
377 		strcpy(mount_crypt_stat->global_default_cipher_name,
378 		       ECRYPTFS_DEFAULT_CIPHER);
379 	}
380 	if (!cipher_key_bytes_set) {
381 		mount_crypt_stat->global_default_cipher_key_size = 0;
382 	}
383 	mutex_lock(&key_tfm_list_mutex);
384 	if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
385 				 NULL))
386 		rc = ecryptfs_add_new_key_tfm(
387 			NULL, mount_crypt_stat->global_default_cipher_name,
388 			mount_crypt_stat->global_default_cipher_key_size);
389 	mutex_unlock(&key_tfm_list_mutex);
390 	if (rc) {
391 		printk(KERN_ERR "Error attempting to initialize cipher with "
392 		       "name = [%s] and key size = [%td]; rc = [%d]\n",
393 		       mount_crypt_stat->global_default_cipher_name,
394 		       mount_crypt_stat->global_default_cipher_key_size, rc);
395 		rc = -EINVAL;
396 		goto out;
397 	}
398 	rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
399 	if (rc) {
400 		printk(KERN_WARNING "One or more global auth toks could not "
401 		       "properly register; rc = [%d]\n", rc);
402 	}
403 out:
404 	return rc;
405 }
406 
407 struct kmem_cache *ecryptfs_sb_info_cache;
408 
409 /**
410  * ecryptfs_fill_super
411  * @sb: The ecryptfs super block
412  * @raw_data: The options passed to mount
413  * @silent: Not used but required by function prototype
414  *
415  * Sets up what we can of the sb, rest is done in ecryptfs_read_super
416  *
417  * Returns zero on success; non-zero otherwise
418  */
419 static int
420 ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
421 {
422 	int rc = 0;
423 
424 	/* Released in ecryptfs_put_super() */
425 	ecryptfs_set_superblock_private(sb,
426 					kmem_cache_zalloc(ecryptfs_sb_info_cache,
427 							 GFP_KERNEL));
428 	if (!ecryptfs_superblock_to_private(sb)) {
429 		ecryptfs_printk(KERN_WARNING, "Out of memory\n");
430 		rc = -ENOMEM;
431 		goto out;
432 	}
433 	sb->s_op = &ecryptfs_sops;
434 	/* Released through deactivate_super(sb) from get_sb_nodev */
435 	sb->s_root = d_alloc(NULL, &(const struct qstr) {
436 			     .hash = 0,.name = "/",.len = 1});
437 	if (!sb->s_root) {
438 		ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
439 		rc = -ENOMEM;
440 		goto out;
441 	}
442 	sb->s_root->d_op = &ecryptfs_dops;
443 	sb->s_root->d_sb = sb;
444 	sb->s_root->d_parent = sb->s_root;
445 	/* Released in d_release when dput(sb->s_root) is called */
446 	/* through deactivate_super(sb) from get_sb_nodev() */
447 	ecryptfs_set_dentry_private(sb->s_root,
448 				    kmem_cache_zalloc(ecryptfs_dentry_info_cache,
449 						     GFP_KERNEL));
450 	if (!ecryptfs_dentry_to_private(sb->s_root)) {
451 		ecryptfs_printk(KERN_ERR,
452 				"dentry_info_cache alloc failed\n");
453 		rc = -ENOMEM;
454 		goto out;
455 	}
456 	rc = 0;
457 out:
458 	/* Should be able to rely on deactivate_super called from
459 	 * get_sb_nodev */
460 	return rc;
461 }
462 
463 /**
464  * ecryptfs_read_super
465  * @sb: The ecryptfs super block
466  * @dev_name: The path to mount over
467  *
468  * Read the super block of the lower filesystem, and use
469  * ecryptfs_interpose to create our initial inode and super block
470  * struct.
471  */
472 static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
473 {
474 	struct path path;
475 	int rc;
476 
477 	rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
478 	if (rc) {
479 		ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
480 		goto out;
481 	}
482 	ecryptfs_set_superblock_lower(sb, path.dentry->d_sb);
483 	sb->s_maxbytes = path.dentry->d_sb->s_maxbytes;
484 	sb->s_blocksize = path.dentry->d_sb->s_blocksize;
485 	ecryptfs_set_dentry_lower(sb->s_root, path.dentry);
486 	ecryptfs_set_dentry_lower_mnt(sb->s_root, path.mnt);
487 	rc = ecryptfs_interpose(path.dentry, sb->s_root, sb, 0);
488 	if (rc)
489 		goto out_free;
490 	rc = 0;
491 	goto out;
492 out_free:
493 	path_put(&path);
494 out:
495 	return rc;
496 }
497 
498 /**
499  * ecryptfs_get_sb
500  * @fs_type
501  * @flags
502  * @dev_name: The path to mount over
503  * @raw_data: The options passed into the kernel
504  *
505  * The whole ecryptfs_get_sb process is broken into 4 functions:
506  * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
507  * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
508  *                        with as much information as it can before needing
509  *                        the lower filesystem.
510  * ecryptfs_read_super(): this accesses the lower filesystem and uses
511  *                        ecryptfs_interpolate to perform most of the linking
512  * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
513  */
514 static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
515 			const char *dev_name, void *raw_data,
516 			struct vfsmount *mnt)
517 {
518 	int rc;
519 	struct super_block *sb;
520 
521 	rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
522 	if (rc < 0) {
523 		printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
524 		goto out;
525 	}
526 	sb = mnt->mnt_sb;
527 	rc = ecryptfs_parse_options(sb, raw_data);
528 	if (rc) {
529 		printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
530 		goto out_abort;
531 	}
532 	rc = ecryptfs_read_super(sb, dev_name);
533 	if (rc) {
534 		printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
535 		goto out_abort;
536 	}
537 	goto out;
538 out_abort:
539 	dput(sb->s_root);
540 	up_write(&sb->s_umount);
541 	deactivate_super(sb);
542 out:
543 	return rc;
544 }
545 
546 /**
547  * ecryptfs_kill_block_super
548  * @sb: The ecryptfs super block
549  *
550  * Used to bring the superblock down and free the private data.
551  * Private data is free'd in ecryptfs_put_super()
552  */
553 static void ecryptfs_kill_block_super(struct super_block *sb)
554 {
555 	generic_shutdown_super(sb);
556 }
557 
558 static struct file_system_type ecryptfs_fs_type = {
559 	.owner = THIS_MODULE,
560 	.name = "ecryptfs",
561 	.get_sb = ecryptfs_get_sb,
562 	.kill_sb = ecryptfs_kill_block_super,
563 	.fs_flags = 0
564 };
565 
566 /**
567  * inode_info_init_once
568  *
569  * Initializes the ecryptfs_inode_info_cache when it is created
570  */
571 static void
572 inode_info_init_once(void *vptr)
573 {
574 	struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
575 
576 	inode_init_once(&ei->vfs_inode);
577 }
578 
579 static struct ecryptfs_cache_info {
580 	struct kmem_cache **cache;
581 	const char *name;
582 	size_t size;
583 	void (*ctor)(void *obj);
584 } ecryptfs_cache_infos[] = {
585 	{
586 		.cache = &ecryptfs_auth_tok_list_item_cache,
587 		.name = "ecryptfs_auth_tok_list_item",
588 		.size = sizeof(struct ecryptfs_auth_tok_list_item),
589 	},
590 	{
591 		.cache = &ecryptfs_file_info_cache,
592 		.name = "ecryptfs_file_cache",
593 		.size = sizeof(struct ecryptfs_file_info),
594 	},
595 	{
596 		.cache = &ecryptfs_dentry_info_cache,
597 		.name = "ecryptfs_dentry_info_cache",
598 		.size = sizeof(struct ecryptfs_dentry_info),
599 	},
600 	{
601 		.cache = &ecryptfs_inode_info_cache,
602 		.name = "ecryptfs_inode_cache",
603 		.size = sizeof(struct ecryptfs_inode_info),
604 		.ctor = inode_info_init_once,
605 	},
606 	{
607 		.cache = &ecryptfs_sb_info_cache,
608 		.name = "ecryptfs_sb_cache",
609 		.size = sizeof(struct ecryptfs_sb_info),
610 	},
611 	{
612 		.cache = &ecryptfs_header_cache_1,
613 		.name = "ecryptfs_headers_1",
614 		.size = PAGE_CACHE_SIZE,
615 	},
616 	{
617 		.cache = &ecryptfs_header_cache_2,
618 		.name = "ecryptfs_headers_2",
619 		.size = PAGE_CACHE_SIZE,
620 	},
621 	{
622 		.cache = &ecryptfs_xattr_cache,
623 		.name = "ecryptfs_xattr_cache",
624 		.size = PAGE_CACHE_SIZE,
625 	},
626 	{
627 		.cache = &ecryptfs_key_record_cache,
628 		.name = "ecryptfs_key_record_cache",
629 		.size = sizeof(struct ecryptfs_key_record),
630 	},
631 	{
632 		.cache = &ecryptfs_key_sig_cache,
633 		.name = "ecryptfs_key_sig_cache",
634 		.size = sizeof(struct ecryptfs_key_sig),
635 	},
636 	{
637 		.cache = &ecryptfs_global_auth_tok_cache,
638 		.name = "ecryptfs_global_auth_tok_cache",
639 		.size = sizeof(struct ecryptfs_global_auth_tok),
640 	},
641 	{
642 		.cache = &ecryptfs_key_tfm_cache,
643 		.name = "ecryptfs_key_tfm_cache",
644 		.size = sizeof(struct ecryptfs_key_tfm),
645 	},
646 	{
647 		.cache = &ecryptfs_open_req_cache,
648 		.name = "ecryptfs_open_req_cache",
649 		.size = sizeof(struct ecryptfs_open_req),
650 	},
651 };
652 
653 static void ecryptfs_free_kmem_caches(void)
654 {
655 	int i;
656 
657 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
658 		struct ecryptfs_cache_info *info;
659 
660 		info = &ecryptfs_cache_infos[i];
661 		if (*(info->cache))
662 			kmem_cache_destroy(*(info->cache));
663 	}
664 }
665 
666 /**
667  * ecryptfs_init_kmem_caches
668  *
669  * Returns zero on success; non-zero otherwise
670  */
671 static int ecryptfs_init_kmem_caches(void)
672 {
673 	int i;
674 
675 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
676 		struct ecryptfs_cache_info *info;
677 
678 		info = &ecryptfs_cache_infos[i];
679 		*(info->cache) = kmem_cache_create(info->name, info->size,
680 				0, SLAB_HWCACHE_ALIGN, info->ctor);
681 		if (!*(info->cache)) {
682 			ecryptfs_free_kmem_caches();
683 			ecryptfs_printk(KERN_WARNING, "%s: "
684 					"kmem_cache_create failed\n",
685 					info->name);
686 			return -ENOMEM;
687 		}
688 	}
689 	return 0;
690 }
691 
692 static struct kobject *ecryptfs_kobj;
693 
694 static ssize_t version_show(struct kobject *kobj,
695 			    struct kobj_attribute *attr, char *buff)
696 {
697 	return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
698 }
699 
700 static struct kobj_attribute version_attr = __ATTR_RO(version);
701 
702 static struct attribute *attributes[] = {
703 	&version_attr.attr,
704 	NULL,
705 };
706 
707 static struct attribute_group attr_group = {
708 	.attrs = attributes,
709 };
710 
711 static int do_sysfs_registration(void)
712 {
713 	int rc;
714 
715 	ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
716 	if (!ecryptfs_kobj) {
717 		printk(KERN_ERR "Unable to create ecryptfs kset\n");
718 		rc = -ENOMEM;
719 		goto out;
720 	}
721 	rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
722 	if (rc) {
723 		printk(KERN_ERR
724 		       "Unable to create ecryptfs version attributes\n");
725 		kobject_put(ecryptfs_kobj);
726 	}
727 out:
728 	return rc;
729 }
730 
731 static void do_sysfs_unregistration(void)
732 {
733 	sysfs_remove_group(ecryptfs_kobj, &attr_group);
734 	kobject_put(ecryptfs_kobj);
735 }
736 
737 static int __init ecryptfs_init(void)
738 {
739 	int rc;
740 
741 	if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
742 		rc = -EINVAL;
743 		ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
744 				"larger than the host's page size, and so "
745 				"eCryptfs cannot run on this system. The "
746 				"default eCryptfs extent size is [%d] bytes; "
747 				"the page size is [%d] bytes.\n",
748 				ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
749 		goto out;
750 	}
751 	rc = ecryptfs_init_kmem_caches();
752 	if (rc) {
753 		printk(KERN_ERR
754 		       "Failed to allocate one or more kmem_cache objects\n");
755 		goto out;
756 	}
757 	rc = register_filesystem(&ecryptfs_fs_type);
758 	if (rc) {
759 		printk(KERN_ERR "Failed to register filesystem\n");
760 		goto out_free_kmem_caches;
761 	}
762 	rc = do_sysfs_registration();
763 	if (rc) {
764 		printk(KERN_ERR "sysfs registration failed\n");
765 		goto out_unregister_filesystem;
766 	}
767 	rc = ecryptfs_init_kthread();
768 	if (rc) {
769 		printk(KERN_ERR "%s: kthread initialization failed; "
770 		       "rc = [%d]\n", __func__, rc);
771 		goto out_do_sysfs_unregistration;
772 	}
773 	rc = ecryptfs_init_messaging();
774 	if (rc) {
775 		printk(KERN_ERR "Failure occured while attempting to "
776 				"initialize the communications channel to "
777 				"ecryptfsd\n");
778 		goto out_destroy_kthread;
779 	}
780 	rc = ecryptfs_init_crypto();
781 	if (rc) {
782 		printk(KERN_ERR "Failure whilst attempting to init crypto; "
783 		       "rc = [%d]\n", rc);
784 		goto out_release_messaging;
785 	}
786 	if (ecryptfs_verbosity > 0)
787 		printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
788 			"will be written to the syslog!\n", ecryptfs_verbosity);
789 
790 	goto out;
791 out_release_messaging:
792 	ecryptfs_release_messaging();
793 out_destroy_kthread:
794 	ecryptfs_destroy_kthread();
795 out_do_sysfs_unregistration:
796 	do_sysfs_unregistration();
797 out_unregister_filesystem:
798 	unregister_filesystem(&ecryptfs_fs_type);
799 out_free_kmem_caches:
800 	ecryptfs_free_kmem_caches();
801 out:
802 	return rc;
803 }
804 
805 static void __exit ecryptfs_exit(void)
806 {
807 	int rc;
808 
809 	rc = ecryptfs_destroy_crypto();
810 	if (rc)
811 		printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
812 		       "rc = [%d]\n", rc);
813 	ecryptfs_release_messaging();
814 	ecryptfs_destroy_kthread();
815 	do_sysfs_unregistration();
816 	unregister_filesystem(&ecryptfs_fs_type);
817 	ecryptfs_free_kmem_caches();
818 }
819 
820 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
821 MODULE_DESCRIPTION("eCryptfs");
822 
823 MODULE_LICENSE("GPL");
824 
825 module_init(ecryptfs_init)
826 module_exit(ecryptfs_exit)
827