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