xref: /linux/fs/ecryptfs/main.c (revision 332ab16f830f59e7621ae8eb2c353dc135a316f6)
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/mount.h>
34237fead6SMichael Halcrow #include <linux/pagemap.h>
35237fead6SMichael Halcrow #include <linux/key.h>
36237fead6SMichael Halcrow #include <linux/parser.h>
370cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
385a0e3ad6STejun Heo #include <linux/slab.h>
39070baa51SRoberto Sassu #include <linux/magic.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 /**
53624ae528STyler Hicks  * Module parameter that defines the number of message buffer elements
54dddfa461SMichael Halcrow  */
55dddfa461SMichael Halcrow unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
56dddfa461SMichael Halcrow 
57dddfa461SMichael Halcrow module_param(ecryptfs_message_buf_len, uint, 0);
58dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_message_buf_len,
59dddfa461SMichael Halcrow 		 "Number of message buffer elements");
60dddfa461SMichael Halcrow 
61dddfa461SMichael Halcrow /**
62dddfa461SMichael Halcrow  * Module parameter that defines the maximum guaranteed amount of time to wait
63624ae528STyler Hicks  * for a response from ecryptfsd.  The actual sleep time will be, more than
64dddfa461SMichael Halcrow  * likely, a small amount greater than this specified value, but only less if
65624ae528STyler Hicks  * the message successfully arrives.
66dddfa461SMichael Halcrow  */
67dddfa461SMichael Halcrow signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
68dddfa461SMichael Halcrow 
69dddfa461SMichael Halcrow module_param(ecryptfs_message_wait_timeout, long, 0);
70dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
71dddfa461SMichael Halcrow 		 "Maximum number of seconds that an operation will "
72dddfa461SMichael Halcrow 		 "sleep while waiting for a message response from "
73dddfa461SMichael Halcrow 		 "userspace");
74dddfa461SMichael Halcrow 
75dddfa461SMichael Halcrow /**
76dddfa461SMichael Halcrow  * Module parameter that is an estimate of the maximum number of users
77dddfa461SMichael Halcrow  * that will be concurrently using eCryptfs. Set this to the right
78dddfa461SMichael Halcrow  * value to balance performance and memory use.
79dddfa461SMichael Halcrow  */
80dddfa461SMichael Halcrow unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
81dddfa461SMichael Halcrow 
82dddfa461SMichael Halcrow module_param(ecryptfs_number_of_users, uint, 0);
83dddfa461SMichael Halcrow MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
84dddfa461SMichael Halcrow 		 "concurrent users of eCryptfs");
85dddfa461SMichael Halcrow 
86237fead6SMichael Halcrow void __ecryptfs_printk(const char *fmt, ...)
87237fead6SMichael Halcrow {
88237fead6SMichael Halcrow 	va_list args;
89237fead6SMichael Halcrow 	va_start(args, fmt);
90237fead6SMichael Halcrow 	if (fmt[1] == '7') { /* KERN_DEBUG */
91237fead6SMichael Halcrow 		if (ecryptfs_verbosity >= 1)
92237fead6SMichael Halcrow 			vprintk(fmt, args);
93237fead6SMichael Halcrow 	} else
94237fead6SMichael Halcrow 		vprintk(fmt, args);
95237fead6SMichael Halcrow 	va_end(args);
96237fead6SMichael Halcrow }
97237fead6SMichael Halcrow 
98237fead6SMichael Halcrow /**
99*332ab16fSTyler Hicks  * ecryptfs_init_lower_file
1004981e081SMichael Halcrow  * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
1014981e081SMichael Halcrow  *                   the lower dentry and the lower mount set
1024981e081SMichael Halcrow  *
1034981e081SMichael Halcrow  * eCryptfs only ever keeps a single open file for every lower
1044981e081SMichael Halcrow  * inode. All I/O operations to the lower inode occur through that
1054981e081SMichael Halcrow  * file. When the first eCryptfs dentry that interposes with the first
1064981e081SMichael Halcrow  * lower dentry for that inode is created, this function creates the
107*332ab16fSTyler Hicks  * lower file struct and associates it with the eCryptfs
108*332ab16fSTyler Hicks  * inode. When all eCryptfs files associated with the inode are released, the
109*332ab16fSTyler Hicks  * file is closed.
1104981e081SMichael Halcrow  *
111*332ab16fSTyler Hicks  * The lower file will be opened with read/write permissions, if
1124981e081SMichael Halcrow  * possible. Otherwise, it is opened read-only.
1134981e081SMichael Halcrow  *
114*332ab16fSTyler Hicks  * This function does nothing if a lower file is already
1154981e081SMichael Halcrow  * associated with the eCryptfs inode.
1164981e081SMichael Halcrow  *
1174981e081SMichael Halcrow  * Returns zero on success; non-zero otherwise
1184981e081SMichael Halcrow  */
119*332ab16fSTyler Hicks static int ecryptfs_init_lower_file(struct dentry *dentry,
120*332ab16fSTyler Hicks 				    struct file **lower_file)
1214981e081SMichael Halcrow {
122745ca247SDavid Howells 	const struct cred *cred = current_cred();
123*332ab16fSTyler Hicks 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
124*332ab16fSTyler Hicks 	struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
125*332ab16fSTyler Hicks 	int rc;
1264981e081SMichael Halcrow 
127*332ab16fSTyler Hicks 	rc = ecryptfs_privileged_open(lower_file, lower_dentry, lower_mnt,
128*332ab16fSTyler Hicks 				      cred);
129ac22ba23STyler Hicks 	if (rc) {
130*332ab16fSTyler Hicks 		printk(KERN_ERR "Error opening lower file "
131746f1e55SMichael Halcrow 		       "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
132746f1e55SMichael Halcrow 		       "rc = [%d]\n", lower_dentry, lower_mnt, rc);
133*332ab16fSTyler Hicks 		(*lower_file) = NULL;
1344981e081SMichael Halcrow 	}
1354981e081SMichael Halcrow 	return rc;
1364981e081SMichael Halcrow }
1374981e081SMichael Halcrow 
138*332ab16fSTyler Hicks int ecryptfs_get_lower_file(struct dentry *dentry)
139*332ab16fSTyler Hicks {
140*332ab16fSTyler Hicks 	struct ecryptfs_inode_info *inode_info =
141*332ab16fSTyler Hicks 		ecryptfs_inode_to_private(dentry->d_inode);
142*332ab16fSTyler Hicks 	int count, rc = 0;
143*332ab16fSTyler Hicks 
144*332ab16fSTyler Hicks 	mutex_lock(&inode_info->lower_file_mutex);
145*332ab16fSTyler Hicks 	count = atomic_inc_return(&inode_info->lower_file_count);
146*332ab16fSTyler Hicks 	if (WARN_ON_ONCE(count < 1))
147*332ab16fSTyler Hicks 		rc = -EINVAL;
148*332ab16fSTyler Hicks 	else if (count == 1) {
149*332ab16fSTyler Hicks 		rc = ecryptfs_init_lower_file(dentry,
150*332ab16fSTyler Hicks 					      &inode_info->lower_file);
151*332ab16fSTyler Hicks 		if (rc)
152*332ab16fSTyler Hicks 			atomic_set(&inode_info->lower_file_count, 0);
153*332ab16fSTyler Hicks 	}
154*332ab16fSTyler Hicks 	mutex_unlock(&inode_info->lower_file_mutex);
155*332ab16fSTyler Hicks 	return rc;
156*332ab16fSTyler Hicks }
157*332ab16fSTyler Hicks 
158*332ab16fSTyler Hicks void ecryptfs_put_lower_file(struct inode *inode)
159*332ab16fSTyler Hicks {
160*332ab16fSTyler Hicks 	struct ecryptfs_inode_info *inode_info;
161*332ab16fSTyler Hicks 
162*332ab16fSTyler Hicks 	inode_info = ecryptfs_inode_to_private(inode);
163*332ab16fSTyler Hicks 	if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
164*332ab16fSTyler Hicks 				      &inode_info->lower_file_mutex)) {
165*332ab16fSTyler Hicks 		fput(inode_info->lower_file);
166*332ab16fSTyler Hicks 		inode_info->lower_file = NULL;
167*332ab16fSTyler Hicks 		mutex_unlock(&inode_info->lower_file_mutex);
168*332ab16fSTyler Hicks 	}
169*332ab16fSTyler Hicks }
170*332ab16fSTyler Hicks 
1716254b32bSLinus Torvalds static struct inode *ecryptfs_get_inode(struct inode *lower_inode,
17266cb7666SAl Viro 		       struct super_block *sb)
173237fead6SMichael Halcrow {
174237fead6SMichael Halcrow 	struct inode *inode;
175237fead6SMichael Halcrow 	int rc = 0;
176237fead6SMichael Halcrow 
177237fead6SMichael Halcrow 	if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
178237fead6SMichael Halcrow 		rc = -EXDEV;
179237fead6SMichael Halcrow 		goto out;
180237fead6SMichael Halcrow 	}
181237fead6SMichael Halcrow 	if (!igrab(lower_inode)) {
182237fead6SMichael Halcrow 		rc = -ESTALE;
183237fead6SMichael Halcrow 		goto out;
184237fead6SMichael Halcrow 	}
185237fead6SMichael Halcrow 	inode = iget5_locked(sb, (unsigned long)lower_inode,
186237fead6SMichael Halcrow 			     ecryptfs_inode_test, ecryptfs_inode_set,
187237fead6SMichael Halcrow 			     lower_inode);
188237fead6SMichael Halcrow 	if (!inode) {
189237fead6SMichael Halcrow 		rc = -EACCES;
190237fead6SMichael Halcrow 		iput(lower_inode);
191237fead6SMichael Halcrow 		goto out;
192237fead6SMichael Halcrow 	}
193237fead6SMichael Halcrow 	if (inode->i_state & I_NEW)
194237fead6SMichael Halcrow 		unlock_new_inode(inode);
195237fead6SMichael Halcrow 	else
196237fead6SMichael Halcrow 		iput(lower_inode);
197237fead6SMichael Halcrow 	if (S_ISLNK(lower_inode->i_mode))
198237fead6SMichael Halcrow 		inode->i_op = &ecryptfs_symlink_iops;
199237fead6SMichael Halcrow 	else if (S_ISDIR(lower_inode->i_mode))
200237fead6SMichael Halcrow 		inode->i_op = &ecryptfs_dir_iops;
201237fead6SMichael Halcrow 	if (S_ISDIR(lower_inode->i_mode))
202237fead6SMichael Halcrow 		inode->i_fop = &ecryptfs_dir_fops;
20326da8205SPekka Enberg 	if (special_file(lower_inode->i_mode))
204237fead6SMichael Halcrow 		init_special_inode(inode, lower_inode->i_mode,
205237fead6SMichael Halcrow 				   lower_inode->i_rdev);
2069afa2fb6SErez Zadok 	fsstack_copy_attr_all(inode, lower_inode);
207237fead6SMichael Halcrow 	/* This size will be overwritten for real files w/ headers and
208237fead6SMichael Halcrow 	 * other metadata */
2090cc72dc7SJosef "Jeff" Sipek 	fsstack_copy_inode_size(inode, lower_inode);
21066cb7666SAl Viro 	return inode;
21166cb7666SAl Viro out:
21266cb7666SAl Viro 	return ERR_PTR(rc);
21366cb7666SAl Viro }
21466cb7666SAl Viro 
21566cb7666SAl Viro /**
21666cb7666SAl Viro  * ecryptfs_interpose
21766cb7666SAl Viro  * @lower_dentry: Existing dentry in the lower filesystem
21866cb7666SAl Viro  * @dentry: ecryptfs' dentry
21966cb7666SAl Viro  * @sb: ecryptfs's super_block
22066cb7666SAl Viro  * @flags: flags to govern behavior of interpose procedure
22166cb7666SAl Viro  *
22266cb7666SAl Viro  * Interposes upper and lower dentries.
22366cb7666SAl Viro  *
22466cb7666SAl Viro  * Returns zero on success; non-zero otherwise
22566cb7666SAl Viro  */
22666cb7666SAl Viro int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
22766cb7666SAl Viro 		       struct super_block *sb, u32 flags)
22866cb7666SAl Viro {
22966cb7666SAl Viro 	struct inode *lower_inode = lower_dentry->d_inode;
23066cb7666SAl Viro 	struct inode *inode = ecryptfs_get_inode(lower_inode, sb);
2316254b32bSLinus Torvalds 	if (IS_ERR(inode))
23266cb7666SAl Viro 		return PTR_ERR(inode);
233ae6e8459STyler Hicks 	if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
234ae6e8459STyler Hicks 		d_add(dentry, inode);
235ae6e8459STyler Hicks 	else
236ae6e8459STyler Hicks 		d_instantiate(dentry, inode);
23766cb7666SAl Viro 	return 0;
238237fead6SMichael Halcrow }
239237fead6SMichael Halcrow 
2402830bfd6SEric Sandeen enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
2412830bfd6SEric Sandeen        ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
2422830bfd6SEric Sandeen        ecryptfs_opt_ecryptfs_key_bytes,
24317398957SMichael Halcrow        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
24487c94c4dSMichael Halcrow        ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
24587c94c4dSMichael Halcrow        ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
246f16feb51SRoberto Sassu        ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
247f16feb51SRoberto Sassu        ecryptfs_opt_err };
248237fead6SMichael Halcrow 
249a447c093SSteven Whitehouse static const match_table_t tokens = {
250237fead6SMichael Halcrow 	{ecryptfs_opt_sig, "sig=%s"},
251237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
252237fead6SMichael Halcrow 	{ecryptfs_opt_cipher, "cipher=%s"},
253237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
254237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
255237fead6SMichael Halcrow 	{ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
25617398957SMichael Halcrow 	{ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
25717398957SMichael Halcrow 	{ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
25887c94c4dSMichael Halcrow 	{ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
25987c94c4dSMichael Halcrow 	{ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
26087c94c4dSMichael Halcrow 	{ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
261e77cc8d2STyler Hicks 	{ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
262f16feb51SRoberto Sassu 	{ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
263237fead6SMichael Halcrow 	{ecryptfs_opt_err, NULL}
264237fead6SMichael Halcrow };
265237fead6SMichael Halcrow 
266f4aad16aSMichael Halcrow static int ecryptfs_init_global_auth_toks(
267f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
268237fead6SMichael Halcrow {
269f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
2700e1fc5efSRoberto Sassu 	struct ecryptfs_auth_tok *auth_tok;
271237fead6SMichael Halcrow 	int rc = 0;
272237fead6SMichael Halcrow 
273f4aad16aSMichael Halcrow 	list_for_each_entry(global_auth_tok,
274f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
275f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
2765dda6992SMichael Halcrow 		rc = ecryptfs_keyring_auth_tok_for_sig(
2770e1fc5efSRoberto Sassu 			&global_auth_tok->global_auth_tok_key, &auth_tok,
2785dda6992SMichael Halcrow 			global_auth_tok->sig);
2795dda6992SMichael Halcrow 		if (rc) {
280f4aad16aSMichael Halcrow 			printk(KERN_ERR "Could not find valid key in user "
281f4aad16aSMichael Halcrow 			       "session keyring for sig specified in mount "
282f4aad16aSMichael Halcrow 			       "option: [%s]\n", global_auth_tok->sig);
283f4aad16aSMichael Halcrow 			global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
284982363c9SEric Sandeen 			goto out;
285b5695d04SRoberto Sassu 		} else {
286f4aad16aSMichael Halcrow 			global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
287b5695d04SRoberto Sassu 			up_write(&(global_auth_tok->global_auth_tok_key)->sem);
288b5695d04SRoberto Sassu 		}
289237fead6SMichael Halcrow 	}
290982363c9SEric Sandeen out:
291237fead6SMichael Halcrow 	return rc;
292237fead6SMichael Halcrow }
293237fead6SMichael Halcrow 
294f4aad16aSMichael Halcrow static void ecryptfs_init_mount_crypt_stat(
295f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
296f4aad16aSMichael Halcrow {
297f4aad16aSMichael Halcrow 	memset((void *)mount_crypt_stat, 0,
298f4aad16aSMichael Halcrow 	       sizeof(struct ecryptfs_mount_crypt_stat));
299f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
300f4aad16aSMichael Halcrow 	mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
301f4aad16aSMichael Halcrow 	mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
302f4aad16aSMichael Halcrow }
303f4aad16aSMichael Halcrow 
304237fead6SMichael Halcrow /**
305237fead6SMichael Halcrow  * ecryptfs_parse_options
306237fead6SMichael Halcrow  * @sb: The ecryptfs super block
30725985edcSLucas De Marchi  * @options: The options passed to the kernel
308237fead6SMichael Halcrow  *
309237fead6SMichael Halcrow  * Parse mount options:
310237fead6SMichael Halcrow  * debug=N 	   - ecryptfs_verbosity level for debug output
311237fead6SMichael Halcrow  * sig=XXX	   - description(signature) of the key to use
312237fead6SMichael Halcrow  *
313237fead6SMichael Halcrow  * Returns the dentry object of the lower-level (lower/interposed)
314237fead6SMichael Halcrow  * directory; We want to mount our stackable file system on top of
315237fead6SMichael Halcrow  * that lower directory.
316237fead6SMichael Halcrow  *
317237fead6SMichael Halcrow  * The signature of the key to use must be the description of a key
318237fead6SMichael Halcrow  * already in the keyring. Mounting will fail if the key can not be
319237fead6SMichael Halcrow  * found.
320237fead6SMichael Halcrow  *
321237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
322237fead6SMichael Halcrow  */
3232ccde7c6SAl Viro static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options)
324237fead6SMichael Halcrow {
325237fead6SMichael Halcrow 	char *p;
326237fead6SMichael Halcrow 	int rc = 0;
327237fead6SMichael Halcrow 	int sig_set = 0;
328237fead6SMichael Halcrow 	int cipher_name_set = 0;
32987c94c4dSMichael Halcrow 	int fn_cipher_name_set = 0;
330237fead6SMichael Halcrow 	int cipher_key_bytes;
331237fead6SMichael Halcrow 	int cipher_key_bytes_set = 0;
33287c94c4dSMichael Halcrow 	int fn_cipher_key_bytes;
33387c94c4dSMichael Halcrow 	int fn_cipher_key_bytes_set = 0;
334237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
3352ccde7c6SAl Viro 		&sbi->mount_crypt_stat;
336237fead6SMichael Halcrow 	substring_t args[MAX_OPT_ARGS];
337237fead6SMichael Halcrow 	int token;
338237fead6SMichael Halcrow 	char *sig_src;
339237fead6SMichael Halcrow 	char *cipher_name_dst;
340237fead6SMichael Halcrow 	char *cipher_name_src;
34187c94c4dSMichael Halcrow 	char *fn_cipher_name_dst;
34287c94c4dSMichael Halcrow 	char *fn_cipher_name_src;
34387c94c4dSMichael Halcrow 	char *fnek_dst;
34487c94c4dSMichael Halcrow 	char *fnek_src;
345237fead6SMichael Halcrow 	char *cipher_key_bytes_src;
34687c94c4dSMichael Halcrow 	char *fn_cipher_key_bytes_src;
347237fead6SMichael Halcrow 
348237fead6SMichael Halcrow 	if (!options) {
349237fead6SMichael Halcrow 		rc = -EINVAL;
350237fead6SMichael Halcrow 		goto out;
351237fead6SMichael Halcrow 	}
352956159c3SMichael Halcrow 	ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
353237fead6SMichael Halcrow 	while ((p = strsep(&options, ",")) != NULL) {
354237fead6SMichael Halcrow 		if (!*p)
355237fead6SMichael Halcrow 			continue;
356237fead6SMichael Halcrow 		token = match_token(p, tokens, args);
357237fead6SMichael Halcrow 		switch (token) {
358237fead6SMichael Halcrow 		case ecryptfs_opt_sig:
359237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_sig:
360237fead6SMichael Halcrow 			sig_src = args[0].from;
361f4aad16aSMichael Halcrow 			rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
36284814d64STyler Hicks 							  sig_src, 0);
363f4aad16aSMichael Halcrow 			if (rc) {
364f4aad16aSMichael Halcrow 				printk(KERN_ERR "Error attempting to register "
365f4aad16aSMichael Halcrow 				       "global sig; rc = [%d]\n", rc);
366f4aad16aSMichael Halcrow 				goto out;
367f4aad16aSMichael Halcrow 			}
368237fead6SMichael Halcrow 			sig_set = 1;
369237fead6SMichael Halcrow 			break;
370237fead6SMichael Halcrow 		case ecryptfs_opt_cipher:
371237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_cipher:
372237fead6SMichael Halcrow 			cipher_name_src = args[0].from;
373237fead6SMichael Halcrow 			cipher_name_dst =
374237fead6SMichael Halcrow 				mount_crypt_stat->
375237fead6SMichael Halcrow 				global_default_cipher_name;
376237fead6SMichael Halcrow 			strncpy(cipher_name_dst, cipher_name_src,
377237fead6SMichael Halcrow 				ECRYPTFS_MAX_CIPHER_NAME_SIZE);
37887c94c4dSMichael Halcrow 			cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
379237fead6SMichael Halcrow 			cipher_name_set = 1;
380237fead6SMichael Halcrow 			break;
381237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_key_bytes:
382237fead6SMichael Halcrow 			cipher_key_bytes_src = args[0].from;
383237fead6SMichael Halcrow 			cipher_key_bytes =
384237fead6SMichael Halcrow 				(int)simple_strtol(cipher_key_bytes_src,
385237fead6SMichael Halcrow 						   &cipher_key_bytes_src, 0);
386237fead6SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size =
387237fead6SMichael Halcrow 				cipher_key_bytes;
388237fead6SMichael Halcrow 			cipher_key_bytes_set = 1;
389237fead6SMichael Halcrow 			break;
390237fead6SMichael Halcrow 		case ecryptfs_opt_passthrough:
391237fead6SMichael Halcrow 			mount_crypt_stat->flags |=
392237fead6SMichael Halcrow 				ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
393237fead6SMichael Halcrow 			break;
39417398957SMichael Halcrow 		case ecryptfs_opt_xattr_metadata:
39517398957SMichael Halcrow 			mount_crypt_stat->flags |=
39617398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
39717398957SMichael Halcrow 			break;
39817398957SMichael Halcrow 		case ecryptfs_opt_encrypted_view:
39917398957SMichael Halcrow 			mount_crypt_stat->flags |=
40017398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
40117398957SMichael Halcrow 			mount_crypt_stat->flags |=
40217398957SMichael Halcrow 				ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
40317398957SMichael Halcrow 			break;
40487c94c4dSMichael Halcrow 		case ecryptfs_opt_fnek_sig:
40587c94c4dSMichael Halcrow 			fnek_src = args[0].from;
40687c94c4dSMichael Halcrow 			fnek_dst =
40787c94c4dSMichael Halcrow 				mount_crypt_stat->global_default_fnek_sig;
40887c94c4dSMichael Halcrow 			strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
40987c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fnek_sig[
41087c94c4dSMichael Halcrow 				ECRYPTFS_SIG_SIZE_HEX] = '\0';
41187c94c4dSMichael Halcrow 			rc = ecryptfs_add_global_auth_tok(
41287c94c4dSMichael Halcrow 				mount_crypt_stat,
41384814d64STyler Hicks 				mount_crypt_stat->global_default_fnek_sig,
41484814d64STyler Hicks 				ECRYPTFS_AUTH_TOK_FNEK);
41587c94c4dSMichael Halcrow 			if (rc) {
41687c94c4dSMichael Halcrow 				printk(KERN_ERR "Error attempting to register "
41787c94c4dSMichael Halcrow 				       "global fnek sig [%s]; rc = [%d]\n",
41887c94c4dSMichael Halcrow 				       mount_crypt_stat->global_default_fnek_sig,
41987c94c4dSMichael Halcrow 				       rc);
42087c94c4dSMichael Halcrow 				goto out;
42187c94c4dSMichael Halcrow 			}
42287c94c4dSMichael Halcrow 			mount_crypt_stat->flags |=
42387c94c4dSMichael Halcrow 				(ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
42487c94c4dSMichael Halcrow 				 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
42587c94c4dSMichael Halcrow 			break;
42687c94c4dSMichael Halcrow 		case ecryptfs_opt_fn_cipher:
42787c94c4dSMichael Halcrow 			fn_cipher_name_src = args[0].from;
42887c94c4dSMichael Halcrow 			fn_cipher_name_dst =
42987c94c4dSMichael Halcrow 				mount_crypt_stat->global_default_fn_cipher_name;
43087c94c4dSMichael Halcrow 			strncpy(fn_cipher_name_dst, fn_cipher_name_src,
43187c94c4dSMichael Halcrow 				ECRYPTFS_MAX_CIPHER_NAME_SIZE);
43287c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fn_cipher_name[
43387c94c4dSMichael Halcrow 				ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
43487c94c4dSMichael Halcrow 			fn_cipher_name_set = 1;
43587c94c4dSMichael Halcrow 			break;
43687c94c4dSMichael Halcrow 		case ecryptfs_opt_fn_cipher_key_bytes:
43787c94c4dSMichael Halcrow 			fn_cipher_key_bytes_src = args[0].from;
43887c94c4dSMichael Halcrow 			fn_cipher_key_bytes =
43987c94c4dSMichael Halcrow 				(int)simple_strtol(fn_cipher_key_bytes_src,
44087c94c4dSMichael Halcrow 						   &fn_cipher_key_bytes_src, 0);
44187c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fn_cipher_key_bytes =
44287c94c4dSMichael Halcrow 				fn_cipher_key_bytes;
44387c94c4dSMichael Halcrow 			fn_cipher_key_bytes_set = 1;
44487c94c4dSMichael Halcrow 			break;
445e77cc8d2STyler Hicks 		case ecryptfs_opt_unlink_sigs:
446e77cc8d2STyler Hicks 			mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
447e77cc8d2STyler Hicks 			break;
448f16feb51SRoberto Sassu 		case ecryptfs_opt_mount_auth_tok_only:
449f16feb51SRoberto Sassu 			mount_crypt_stat->flags |=
450f16feb51SRoberto Sassu 				ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
451f16feb51SRoberto Sassu 			break;
452237fead6SMichael Halcrow 		case ecryptfs_opt_err:
453237fead6SMichael Halcrow 		default:
45487c94c4dSMichael Halcrow 			printk(KERN_WARNING
45587c94c4dSMichael Halcrow 			       "%s: eCryptfs: unrecognized option [%s]\n",
45687c94c4dSMichael Halcrow 			       __func__, p);
457237fead6SMichael Halcrow 		}
458237fead6SMichael Halcrow 	}
459237fead6SMichael Halcrow 	if (!sig_set) {
460237fead6SMichael Halcrow 		rc = -EINVAL;
461956159c3SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
462956159c3SMichael Halcrow 				"auth tok signature as a mount "
463237fead6SMichael Halcrow 				"parameter; see the eCryptfs README\n");
464237fead6SMichael Halcrow 		goto out;
465237fead6SMichael Halcrow 	}
466237fead6SMichael Halcrow 	if (!cipher_name_set) {
4678f236809SMiklos Szeredi 		int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
4688f236809SMiklos Szeredi 
4698f236809SMiklos Szeredi 		BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
4708f236809SMiklos Szeredi 		strcpy(mount_crypt_stat->global_default_cipher_name,
4718f236809SMiklos Szeredi 		       ECRYPTFS_DEFAULT_CIPHER);
472237fead6SMichael Halcrow 	}
47387c94c4dSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
47487c94c4dSMichael Halcrow 	    && !fn_cipher_name_set)
47587c94c4dSMichael Halcrow 		strcpy(mount_crypt_stat->global_default_fn_cipher_name,
47687c94c4dSMichael Halcrow 		       mount_crypt_stat->global_default_cipher_name);
47787c94c4dSMichael Halcrow 	if (!cipher_key_bytes_set)
478e5d9cbdeSMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size = 0;
47987c94c4dSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
48087c94c4dSMichael Halcrow 	    && !fn_cipher_key_bytes_set)
48187c94c4dSMichael Halcrow 		mount_crypt_stat->global_default_fn_cipher_key_bytes =
48287c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size;
483af440f52SEric Sandeen 	mutex_lock(&key_tfm_list_mutex);
484af440f52SEric Sandeen 	if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
48587c94c4dSMichael Halcrow 				 NULL)) {
4865dda6992SMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(
487f4aad16aSMichael Halcrow 			NULL, mount_crypt_stat->global_default_cipher_name,
4885dda6992SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size);
4895dda6992SMichael Halcrow 		if (rc) {
49087c94c4dSMichael Halcrow 			printk(KERN_ERR "Error attempting to initialize "
49187c94c4dSMichael Halcrow 			       "cipher with name = [%s] and key size = [%td]; "
49287c94c4dSMichael Halcrow 			       "rc = [%d]\n",
493237fead6SMichael Halcrow 			       mount_crypt_stat->global_default_cipher_name,
49487c94c4dSMichael Halcrow 			       mount_crypt_stat->global_default_cipher_key_size,
49587c94c4dSMichael Halcrow 			       rc);
496237fead6SMichael Halcrow 			rc = -EINVAL;
49787c94c4dSMichael Halcrow 			mutex_unlock(&key_tfm_list_mutex);
498237fead6SMichael Halcrow 			goto out;
499237fead6SMichael Halcrow 		}
50087c94c4dSMichael Halcrow 	}
50187c94c4dSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
50287c94c4dSMichael Halcrow 	    && !ecryptfs_tfm_exists(
50387c94c4dSMichael Halcrow 		    mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
50487c94c4dSMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(
50587c94c4dSMichael Halcrow 			NULL, mount_crypt_stat->global_default_fn_cipher_name,
50687c94c4dSMichael Halcrow 			mount_crypt_stat->global_default_fn_cipher_key_bytes);
5075dda6992SMichael Halcrow 		if (rc) {
50887c94c4dSMichael Halcrow 			printk(KERN_ERR "Error attempting to initialize "
50987c94c4dSMichael Halcrow 			       "cipher with name = [%s] and key size = [%td]; "
51087c94c4dSMichael Halcrow 			       "rc = [%d]\n",
51187c94c4dSMichael Halcrow 			       mount_crypt_stat->global_default_fn_cipher_name,
51287c94c4dSMichael Halcrow 			       mount_crypt_stat->global_default_fn_cipher_key_bytes,
51387c94c4dSMichael Halcrow 			       rc);
51487c94c4dSMichael Halcrow 			rc = -EINVAL;
51587c94c4dSMichael Halcrow 			mutex_unlock(&key_tfm_list_mutex);
51687c94c4dSMichael Halcrow 			goto out;
51787c94c4dSMichael Halcrow 		}
51887c94c4dSMichael Halcrow 	}
51987c94c4dSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
52087c94c4dSMichael Halcrow 	rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
52187c94c4dSMichael Halcrow 	if (rc)
522f4aad16aSMichael Halcrow 		printk(KERN_WARNING "One or more global auth toks could not "
523f4aad16aSMichael Halcrow 		       "properly register; rc = [%d]\n", rc);
524237fead6SMichael Halcrow out:
525237fead6SMichael Halcrow 	return rc;
526237fead6SMichael Halcrow }
527237fead6SMichael Halcrow 
528237fead6SMichael Halcrow struct kmem_cache *ecryptfs_sb_info_cache;
5294403158bSAl Viro static struct file_system_type ecryptfs_fs_type;
530237fead6SMichael Halcrow 
531237fead6SMichael Halcrow /**
532237fead6SMichael Halcrow  * ecryptfs_get_sb
533237fead6SMichael Halcrow  * @fs_type
534237fead6SMichael Halcrow  * @flags
535237fead6SMichael Halcrow  * @dev_name: The path to mount over
536237fead6SMichael Halcrow  * @raw_data: The options passed into the kernel
537237fead6SMichael Halcrow  */
5384d143bebSAl Viro static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
5394d143bebSAl Viro 			const char *dev_name, void *raw_data)
540237fead6SMichael Halcrow {
5412ccde7c6SAl Viro 	struct super_block *s;
5422ccde7c6SAl Viro 	struct ecryptfs_sb_info *sbi;
5432ccde7c6SAl Viro 	struct ecryptfs_dentry_info *root_info;
5442ccde7c6SAl Viro 	const char *err = "Getting sb failed";
54566cb7666SAl Viro 	struct inode *inode;
54666cb7666SAl Viro 	struct path path;
547237fead6SMichael Halcrow 	int rc;
548237fead6SMichael Halcrow 
5492ccde7c6SAl Viro 	sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
5502ccde7c6SAl Viro 	if (!sbi) {
5512ccde7c6SAl Viro 		rc = -ENOMEM;
552237fead6SMichael Halcrow 		goto out;
553237fead6SMichael Halcrow 	}
5542ccde7c6SAl Viro 
5552ccde7c6SAl Viro 	rc = ecryptfs_parse_options(sbi, raw_data);
556237fead6SMichael Halcrow 	if (rc) {
5572ccde7c6SAl Viro 		err = "Error parsing options";
558237fead6SMichael Halcrow 		goto out;
5592ccde7c6SAl Viro 	}
5602ccde7c6SAl Viro 
5612ccde7c6SAl Viro 	s = sget(fs_type, NULL, set_anon_super, NULL);
5622ccde7c6SAl Viro 	if (IS_ERR(s)) {
5632ccde7c6SAl Viro 		rc = PTR_ERR(s);
5642ccde7c6SAl Viro 		goto out;
5652ccde7c6SAl Viro 	}
5662ccde7c6SAl Viro 
5672ccde7c6SAl Viro 	s->s_flags = flags;
5682ccde7c6SAl Viro 	rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs", BDI_CAP_MAP_COPY);
56966cb7666SAl Viro 	if (rc)
57066cb7666SAl Viro 		goto out1;
5712ccde7c6SAl Viro 
5722ccde7c6SAl Viro 	ecryptfs_set_superblock_private(s, sbi);
5732ccde7c6SAl Viro 	s->s_bdi = &sbi->bdi;
5742ccde7c6SAl Viro 
5752ccde7c6SAl Viro 	/* ->kill_sb() will take care of sbi after that point */
5762ccde7c6SAl Viro 	sbi = NULL;
5772ccde7c6SAl Viro 	s->s_op = &ecryptfs_sops;
57866cb7666SAl Viro 	s->s_d_op = &ecryptfs_dops;
57966cb7666SAl Viro 
58066cb7666SAl Viro 	err = "Reading sb failed";
58166cb7666SAl Viro 	rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
58266cb7666SAl Viro 	if (rc) {
58366cb7666SAl Viro 		ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
58466cb7666SAl Viro 		goto out1;
58566cb7666SAl Viro 	}
58666cb7666SAl Viro 	if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
58766cb7666SAl Viro 		rc = -EINVAL;
58866cb7666SAl Viro 		printk(KERN_ERR "Mount on filesystem of type "
58966cb7666SAl Viro 			"eCryptfs explicitly disallowed due to "
59066cb7666SAl Viro 			"known incompatibilities\n");
59166cb7666SAl Viro 		goto out_free;
59266cb7666SAl Viro 	}
59366cb7666SAl Viro 	ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
59466cb7666SAl Viro 	s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
59566cb7666SAl Viro 	s->s_blocksize = path.dentry->d_sb->s_blocksize;
596070baa51SRoberto Sassu 	s->s_magic = ECRYPTFS_SUPER_MAGIC;
59766cb7666SAl Viro 
59866cb7666SAl Viro 	inode = ecryptfs_get_inode(path.dentry->d_inode, s);
59966cb7666SAl Viro 	rc = PTR_ERR(inode);
60066cb7666SAl Viro 	if (IS_ERR(inode))
60166cb7666SAl Viro 		goto out_free;
60266cb7666SAl Viro 
60366cb7666SAl Viro 	s->s_root = d_alloc_root(inode);
60466cb7666SAl Viro 	if (!s->s_root) {
60566cb7666SAl Viro 		iput(inode);
60666cb7666SAl Viro 		rc = -ENOMEM;
60766cb7666SAl Viro 		goto out_free;
60866cb7666SAl Viro 	}
6092ccde7c6SAl Viro 
6102ccde7c6SAl Viro 	rc = -ENOMEM;
6112ccde7c6SAl Viro 	root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
61266cb7666SAl Viro 	if (!root_info)
61366cb7666SAl Viro 		goto out_free;
61466cb7666SAl Viro 
6152ccde7c6SAl Viro 	/* ->kill_sb() will take care of root_info */
6162ccde7c6SAl Viro 	ecryptfs_set_dentry_private(s->s_root, root_info);
61766cb7666SAl Viro 	ecryptfs_set_dentry_lower(s->s_root, path.dentry);
61866cb7666SAl Viro 	ecryptfs_set_dentry_lower_mnt(s->s_root, path.mnt);
61966cb7666SAl Viro 
6202ccde7c6SAl Viro 	s->s_flags |= MS_ACTIVE;
6214d143bebSAl Viro 	return dget(s->s_root);
6222ccde7c6SAl Viro 
62366cb7666SAl Viro out_free:
62466cb7666SAl Viro 	path_put(&path);
62566cb7666SAl Viro out1:
62666cb7666SAl Viro 	deactivate_locked_super(s);
627237fead6SMichael Halcrow out:
6282ccde7c6SAl Viro 	if (sbi) {
6292ccde7c6SAl Viro 		ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
6302ccde7c6SAl Viro 		kmem_cache_free(ecryptfs_sb_info_cache, sbi);
6312ccde7c6SAl Viro 	}
6322ccde7c6SAl Viro 	printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
6334d143bebSAl Viro 	return ERR_PTR(rc);
634237fead6SMichael Halcrow }
635237fead6SMichael Halcrow 
636237fead6SMichael Halcrow /**
637237fead6SMichael Halcrow  * ecryptfs_kill_block_super
638237fead6SMichael Halcrow  * @sb: The ecryptfs super block
639237fead6SMichael Halcrow  *
640237fead6SMichael Halcrow  * Used to bring the superblock down and free the private data.
641237fead6SMichael Halcrow  */
642237fead6SMichael Halcrow static void ecryptfs_kill_block_super(struct super_block *sb)
643237fead6SMichael Halcrow {
644decabd66SAl Viro 	struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
645decabd66SAl Viro 	kill_anon_super(sb);
646decabd66SAl Viro 	if (!sb_info)
647decabd66SAl Viro 		return;
648decabd66SAl Viro 	ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
649decabd66SAl Viro 	bdi_destroy(&sb_info->bdi);
650decabd66SAl Viro 	kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
651237fead6SMichael Halcrow }
652237fead6SMichael Halcrow 
653237fead6SMichael Halcrow static struct file_system_type ecryptfs_fs_type = {
654237fead6SMichael Halcrow 	.owner = THIS_MODULE,
655237fead6SMichael Halcrow 	.name = "ecryptfs",
6564d143bebSAl Viro 	.mount = ecryptfs_mount,
657237fead6SMichael Halcrow 	.kill_sb = ecryptfs_kill_block_super,
658237fead6SMichael Halcrow 	.fs_flags = 0
659237fead6SMichael Halcrow };
660237fead6SMichael Halcrow 
661237fead6SMichael Halcrow /**
662237fead6SMichael Halcrow  * inode_info_init_once
663237fead6SMichael Halcrow  *
664237fead6SMichael Halcrow  * Initializes the ecryptfs_inode_info_cache when it is created
665237fead6SMichael Halcrow  */
666237fead6SMichael Halcrow static void
66751cc5068SAlexey Dobriyan inode_info_init_once(void *vptr)
668237fead6SMichael Halcrow {
669237fead6SMichael Halcrow 	struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
670237fead6SMichael Halcrow 
671237fead6SMichael Halcrow 	inode_init_once(&ei->vfs_inode);
672237fead6SMichael Halcrow }
673237fead6SMichael Halcrow 
674237fead6SMichael Halcrow static struct ecryptfs_cache_info {
675e18b890bSChristoph Lameter 	struct kmem_cache **cache;
676237fead6SMichael Halcrow 	const char *name;
677237fead6SMichael Halcrow 	size_t size;
67851cc5068SAlexey Dobriyan 	void (*ctor)(void *obj);
679237fead6SMichael Halcrow } ecryptfs_cache_infos[] = {
680237fead6SMichael Halcrow 	{
681237fead6SMichael Halcrow 		.cache = &ecryptfs_auth_tok_list_item_cache,
682237fead6SMichael Halcrow 		.name = "ecryptfs_auth_tok_list_item",
683237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_auth_tok_list_item),
684237fead6SMichael Halcrow 	},
685237fead6SMichael Halcrow 	{
686237fead6SMichael Halcrow 		.cache = &ecryptfs_file_info_cache,
687237fead6SMichael Halcrow 		.name = "ecryptfs_file_cache",
688237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_file_info),
689237fead6SMichael Halcrow 	},
690237fead6SMichael Halcrow 	{
691237fead6SMichael Halcrow 		.cache = &ecryptfs_dentry_info_cache,
692237fead6SMichael Halcrow 		.name = "ecryptfs_dentry_info_cache",
693237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_dentry_info),
694237fead6SMichael Halcrow 	},
695237fead6SMichael Halcrow 	{
696237fead6SMichael Halcrow 		.cache = &ecryptfs_inode_info_cache,
697237fead6SMichael Halcrow 		.name = "ecryptfs_inode_cache",
698237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_inode_info),
699237fead6SMichael Halcrow 		.ctor = inode_info_init_once,
700237fead6SMichael Halcrow 	},
701237fead6SMichael Halcrow 	{
702237fead6SMichael Halcrow 		.cache = &ecryptfs_sb_info_cache,
703237fead6SMichael Halcrow 		.name = "ecryptfs_sb_cache",
704237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_sb_info),
705237fead6SMichael Halcrow 	},
706237fead6SMichael Halcrow 	{
707237fead6SMichael Halcrow 		.cache = &ecryptfs_header_cache_1,
708237fead6SMichael Halcrow 		.name = "ecryptfs_headers_1",
709237fead6SMichael Halcrow 		.size = PAGE_CACHE_SIZE,
710237fead6SMichael Halcrow 	},
711237fead6SMichael Halcrow 	{
712237fead6SMichael Halcrow 		.cache = &ecryptfs_header_cache_2,
713237fead6SMichael Halcrow 		.name = "ecryptfs_headers_2",
714237fead6SMichael Halcrow 		.size = PAGE_CACHE_SIZE,
715237fead6SMichael Halcrow 	},
716237fead6SMichael Halcrow 	{
717dd2a3b7aSMichael Halcrow 		.cache = &ecryptfs_xattr_cache,
718dd2a3b7aSMichael Halcrow 		.name = "ecryptfs_xattr_cache",
719dd2a3b7aSMichael Halcrow 		.size = PAGE_CACHE_SIZE,
720dd2a3b7aSMichael Halcrow 	},
721dd2a3b7aSMichael Halcrow 	{
722eb95e7ffSMichael Halcrow 		.cache = &ecryptfs_key_record_cache,
723eb95e7ffSMichael Halcrow 		.name = "ecryptfs_key_record_cache",
724eb95e7ffSMichael Halcrow 		.size = sizeof(struct ecryptfs_key_record),
725eb95e7ffSMichael Halcrow 	},
726956159c3SMichael Halcrow 	{
727956159c3SMichael Halcrow 		.cache = &ecryptfs_key_sig_cache,
728956159c3SMichael Halcrow 		.name = "ecryptfs_key_sig_cache",
729956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_key_sig),
730956159c3SMichael Halcrow 	},
731956159c3SMichael Halcrow 	{
732956159c3SMichael Halcrow 		.cache = &ecryptfs_global_auth_tok_cache,
733956159c3SMichael Halcrow 		.name = "ecryptfs_global_auth_tok_cache",
734956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_global_auth_tok),
735956159c3SMichael Halcrow 	},
736956159c3SMichael Halcrow 	{
737956159c3SMichael Halcrow 		.cache = &ecryptfs_key_tfm_cache,
738956159c3SMichael Halcrow 		.name = "ecryptfs_key_tfm_cache",
739956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_key_tfm),
740956159c3SMichael Halcrow 	},
741746f1e55SMichael Halcrow 	{
742746f1e55SMichael Halcrow 		.cache = &ecryptfs_open_req_cache,
743746f1e55SMichael Halcrow 		.name = "ecryptfs_open_req_cache",
744746f1e55SMichael Halcrow 		.size = sizeof(struct ecryptfs_open_req),
745746f1e55SMichael Halcrow 	},
746237fead6SMichael Halcrow };
747237fead6SMichael Halcrow 
748237fead6SMichael Halcrow static void ecryptfs_free_kmem_caches(void)
749237fead6SMichael Halcrow {
750237fead6SMichael Halcrow 	int i;
751237fead6SMichael Halcrow 
752237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
753237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
754237fead6SMichael Halcrow 
755237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
756237fead6SMichael Halcrow 		if (*(info->cache))
757237fead6SMichael Halcrow 			kmem_cache_destroy(*(info->cache));
758237fead6SMichael Halcrow 	}
759237fead6SMichael Halcrow }
760237fead6SMichael Halcrow 
761237fead6SMichael Halcrow /**
762237fead6SMichael Halcrow  * ecryptfs_init_kmem_caches
763237fead6SMichael Halcrow  *
764237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
765237fead6SMichael Halcrow  */
766237fead6SMichael Halcrow static int ecryptfs_init_kmem_caches(void)
767237fead6SMichael Halcrow {
768237fead6SMichael Halcrow 	int i;
769237fead6SMichael Halcrow 
770237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
771237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
772237fead6SMichael Halcrow 
773237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
774237fead6SMichael Halcrow 		*(info->cache) = kmem_cache_create(info->name, info->size,
77520c2df83SPaul Mundt 				0, SLAB_HWCACHE_ALIGN, info->ctor);
776237fead6SMichael Halcrow 		if (!*(info->cache)) {
777237fead6SMichael Halcrow 			ecryptfs_free_kmem_caches();
778237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "%s: "
779237fead6SMichael Halcrow 					"kmem_cache_create failed\n",
780237fead6SMichael Halcrow 					info->name);
781237fead6SMichael Halcrow 			return -ENOMEM;
782237fead6SMichael Halcrow 		}
783237fead6SMichael Halcrow 	}
784237fead6SMichael Halcrow 	return 0;
785237fead6SMichael Halcrow }
786237fead6SMichael Halcrow 
7876e90aa97SGreg Kroah-Hartman static struct kobject *ecryptfs_kobj;
788237fead6SMichael Halcrow 
789386f275fSKay Sievers static ssize_t version_show(struct kobject *kobj,
790386f275fSKay Sievers 			    struct kobj_attribute *attr, char *buff)
791237fead6SMichael Halcrow {
792237fead6SMichael Halcrow 	return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
793237fead6SMichael Halcrow }
794237fead6SMichael Halcrow 
795386f275fSKay Sievers static struct kobj_attribute version_attr = __ATTR_RO(version);
796237fead6SMichael Halcrow 
79730a468b1SGreg Kroah-Hartman static struct attribute *attributes[] = {
79830a468b1SGreg Kroah-Hartman 	&version_attr.attr,
79930a468b1SGreg Kroah-Hartman 	NULL,
80030a468b1SGreg Kroah-Hartman };
80130a468b1SGreg Kroah-Hartman 
80230a468b1SGreg Kroah-Hartman static struct attribute_group attr_group = {
80330a468b1SGreg Kroah-Hartman 	.attrs = attributes,
80430a468b1SGreg Kroah-Hartman };
805237fead6SMichael Halcrow 
806237fead6SMichael Halcrow static int do_sysfs_registration(void)
807237fead6SMichael Halcrow {
808237fead6SMichael Halcrow 	int rc;
809237fead6SMichael Halcrow 
8106e90aa97SGreg Kroah-Hartman 	ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
8116e90aa97SGreg Kroah-Hartman 	if (!ecryptfs_kobj) {
812917e865dSGreg Kroah-Hartman 		printk(KERN_ERR "Unable to create ecryptfs kset\n");
813917e865dSGreg Kroah-Hartman 		rc = -ENOMEM;
814237fead6SMichael Halcrow 		goto out;
815237fead6SMichael Halcrow 	}
8166e90aa97SGreg Kroah-Hartman 	rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
817237fead6SMichael Halcrow 	if (rc) {
818237fead6SMichael Halcrow 		printk(KERN_ERR
81930a468b1SGreg Kroah-Hartman 		       "Unable to create ecryptfs version attributes\n");
820197b12d6SGreg Kroah-Hartman 		kobject_put(ecryptfs_kobj);
821237fead6SMichael Halcrow 	}
822237fead6SMichael Halcrow out:
823237fead6SMichael Halcrow 	return rc;
824237fead6SMichael Halcrow }
825237fead6SMichael Halcrow 
826a75de1b3SRyusuke Konishi static void do_sysfs_unregistration(void)
827a75de1b3SRyusuke Konishi {
8286e90aa97SGreg Kroah-Hartman 	sysfs_remove_group(ecryptfs_kobj, &attr_group);
829197b12d6SGreg Kroah-Hartman 	kobject_put(ecryptfs_kobj);
830a75de1b3SRyusuke Konishi }
831a75de1b3SRyusuke Konishi 
832237fead6SMichael Halcrow static int __init ecryptfs_init(void)
833237fead6SMichael Halcrow {
834237fead6SMichael Halcrow 	int rc;
835237fead6SMichael Halcrow 
836237fead6SMichael Halcrow 	if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
837237fead6SMichael Halcrow 		rc = -EINVAL;
838237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
839237fead6SMichael Halcrow 				"larger than the host's page size, and so "
840237fead6SMichael Halcrow 				"eCryptfs cannot run on this system. The "
841888d57bbSJoe Perches 				"default eCryptfs extent size is [%u] bytes; "
842888d57bbSJoe Perches 				"the page size is [%lu] bytes.\n",
843888d57bbSJoe Perches 				ECRYPTFS_DEFAULT_EXTENT_SIZE,
844888d57bbSJoe Perches 				(unsigned long)PAGE_CACHE_SIZE);
845237fead6SMichael Halcrow 		goto out;
846237fead6SMichael Halcrow 	}
847237fead6SMichael Halcrow 	rc = ecryptfs_init_kmem_caches();
848237fead6SMichael Halcrow 	if (rc) {
849237fead6SMichael Halcrow 		printk(KERN_ERR
850237fead6SMichael Halcrow 		       "Failed to allocate one or more kmem_cache objects\n");
851237fead6SMichael Halcrow 		goto out;
852237fead6SMichael Halcrow 	}
853237fead6SMichael Halcrow 	rc = register_filesystem(&ecryptfs_fs_type);
854237fead6SMichael Halcrow 	if (rc) {
855237fead6SMichael Halcrow 		printk(KERN_ERR "Failed to register filesystem\n");
856cf81f89dSMichael Halcrow 		goto out_free_kmem_caches;
857237fead6SMichael Halcrow 	}
858237fead6SMichael Halcrow 	rc = do_sysfs_registration();
859237fead6SMichael Halcrow 	if (rc) {
860237fead6SMichael Halcrow 		printk(KERN_ERR "sysfs registration failed\n");
861cf81f89dSMichael Halcrow 		goto out_unregister_filesystem;
862237fead6SMichael Halcrow 	}
863746f1e55SMichael Halcrow 	rc = ecryptfs_init_kthread();
864746f1e55SMichael Halcrow 	if (rc) {
865746f1e55SMichael Halcrow 		printk(KERN_ERR "%s: kthread initialization failed; "
866746f1e55SMichael Halcrow 		       "rc = [%d]\n", __func__, rc);
867746f1e55SMichael Halcrow 		goto out_do_sysfs_unregistration;
868746f1e55SMichael Halcrow 	}
869624ae528STyler Hicks 	rc = ecryptfs_init_messaging();
870dddfa461SMichael Halcrow 	if (rc) {
87125985edcSLucas De Marchi 		printk(KERN_ERR "Failure occurred while attempting to "
872624ae528STyler Hicks 				"initialize the communications channel to "
873624ae528STyler Hicks 				"ecryptfsd\n");
874746f1e55SMichael Halcrow 		goto out_destroy_kthread;
875956159c3SMichael Halcrow 	}
876956159c3SMichael Halcrow 	rc = ecryptfs_init_crypto();
877956159c3SMichael Halcrow 	if (rc) {
878956159c3SMichael Halcrow 		printk(KERN_ERR "Failure whilst attempting to init crypto; "
879956159c3SMichael Halcrow 		       "rc = [%d]\n", rc);
880cf81f89dSMichael Halcrow 		goto out_release_messaging;
881dddfa461SMichael Halcrow 	}
8822830bfd6SEric Sandeen 	if (ecryptfs_verbosity > 0)
8832830bfd6SEric Sandeen 		printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
8842830bfd6SEric Sandeen 			"will be written to the syslog!\n", ecryptfs_verbosity);
8852830bfd6SEric Sandeen 
886cf81f89dSMichael Halcrow 	goto out;
887cf81f89dSMichael Halcrow out_release_messaging:
888624ae528STyler Hicks 	ecryptfs_release_messaging();
889746f1e55SMichael Halcrow out_destroy_kthread:
890746f1e55SMichael Halcrow 	ecryptfs_destroy_kthread();
891cf81f89dSMichael Halcrow out_do_sysfs_unregistration:
892cf81f89dSMichael Halcrow 	do_sysfs_unregistration();
893cf81f89dSMichael Halcrow out_unregister_filesystem:
894cf81f89dSMichael Halcrow 	unregister_filesystem(&ecryptfs_fs_type);
895cf81f89dSMichael Halcrow out_free_kmem_caches:
896cf81f89dSMichael Halcrow 	ecryptfs_free_kmem_caches();
897237fead6SMichael Halcrow out:
898237fead6SMichael Halcrow 	return rc;
899237fead6SMichael Halcrow }
900237fead6SMichael Halcrow 
901237fead6SMichael Halcrow static void __exit ecryptfs_exit(void)
902237fead6SMichael Halcrow {
903cf81f89dSMichael Halcrow 	int rc;
904cf81f89dSMichael Halcrow 
905cf81f89dSMichael Halcrow 	rc = ecryptfs_destroy_crypto();
906cf81f89dSMichael Halcrow 	if (rc)
907cf81f89dSMichael Halcrow 		printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
908cf81f89dSMichael Halcrow 		       "rc = [%d]\n", rc);
909624ae528STyler Hicks 	ecryptfs_release_messaging();
910746f1e55SMichael Halcrow 	ecryptfs_destroy_kthread();
911cf81f89dSMichael Halcrow 	do_sysfs_unregistration();
912237fead6SMichael Halcrow 	unregister_filesystem(&ecryptfs_fs_type);
913237fead6SMichael Halcrow 	ecryptfs_free_kmem_caches();
914237fead6SMichael Halcrow }
915237fead6SMichael Halcrow 
916237fead6SMichael Halcrow MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
917237fead6SMichael Halcrow MODULE_DESCRIPTION("eCryptfs");
918237fead6SMichael Halcrow 
919237fead6SMichael Halcrow MODULE_LICENSE("GPL");
920237fead6SMichael Halcrow 
921237fead6SMichael Halcrow module_init(ecryptfs_init)
922237fead6SMichael Halcrow module_exit(ecryptfs_exit)
923