xref: /linux/fs/ecryptfs/main.c (revision 746f1e558bc52b9693c1a1ecdab60f8392e5ff18)
1237fead6SMichael Halcrow /**
2237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3237fead6SMichael Halcrow  *
4237fead6SMichael Halcrow  * Copyright (C) 1997-2003 Erez Zadok
5237fead6SMichael Halcrow  * Copyright (C) 2001-2003 Stony Brook University
6dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
7237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8237fead6SMichael Halcrow  *              Michael C. Thompson <mcthomps@us.ibm.com>
9dddfa461SMichael Halcrow  *              Tyler Hicks <tyhicks@ou.edu>
10237fead6SMichael Halcrow  *
11237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
12237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
13237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
14237fead6SMichael Halcrow  * License, or (at your option) any later version.
15237fead6SMichael Halcrow  *
16237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
17237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
18237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19237fead6SMichael Halcrow  * General Public License for more details.
20237fead6SMichael Halcrow  *
21237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
22237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
23237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24237fead6SMichael Halcrow  * 02111-1307, USA.
25237fead6SMichael Halcrow  */
26237fead6SMichael Halcrow 
27237fead6SMichael Halcrow #include <linux/dcache.h>
28237fead6SMichael Halcrow #include <linux/file.h>
29237fead6SMichael Halcrow #include <linux/module.h>
30237fead6SMichael Halcrow #include <linux/namei.h>
31237fead6SMichael Halcrow #include <linux/skbuff.h>
32237fead6SMichael Halcrow #include <linux/crypto.h>
33237fead6SMichael Halcrow #include <linux/netlink.h>
34237fead6SMichael Halcrow #include <linux/mount.h>
35237fead6SMichael Halcrow #include <linux/pagemap.h>
36237fead6SMichael Halcrow #include <linux/key.h>
37237fead6SMichael Halcrow #include <linux/parser.h>
380cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
39237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
40237fead6SMichael Halcrow 
41237fead6SMichael Halcrow /**
42237fead6SMichael Halcrow  * Module parameter that defines the ecryptfs_verbosity level.
43237fead6SMichael Halcrow  */
44237fead6SMichael Halcrow int ecryptfs_verbosity = 0;
45237fead6SMichael Halcrow 
46237fead6SMichael Halcrow module_param(ecryptfs_verbosity, int, 0);
47237fead6SMichael Halcrow MODULE_PARM_DESC(ecryptfs_verbosity,
48237fead6SMichael Halcrow 		 "Initial verbosity level (0 or 1; defaults to "
49237fead6SMichael Halcrow 		 "0, which is Quiet)");
50237fead6SMichael Halcrow 
51dddfa461SMichael Halcrow /**
52dddfa461SMichael Halcrow  * Module parameter that defines the number of netlink message buffer
53dddfa461SMichael Halcrow  * 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
63dddfa461SMichael Halcrow  * for a response through netlink.  The actual sleep time will be, more than
64dddfa461SMichael Halcrow  * likely, a small amount greater than this specified value, but only less if
65dddfa461SMichael Halcrow  * the netlink 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 
86dddfa461SMichael Halcrow unsigned int ecryptfs_transport = ECRYPTFS_DEFAULT_TRANSPORT;
87dddfa461SMichael Halcrow 
88237fead6SMichael Halcrow void __ecryptfs_printk(const char *fmt, ...)
89237fead6SMichael Halcrow {
90237fead6SMichael Halcrow 	va_list args;
91237fead6SMichael Halcrow 	va_start(args, fmt);
92237fead6SMichael Halcrow 	if (fmt[1] == '7') { /* KERN_DEBUG */
93237fead6SMichael Halcrow 		if (ecryptfs_verbosity >= 1)
94237fead6SMichael Halcrow 			vprintk(fmt, args);
95237fead6SMichael Halcrow 	} else
96237fead6SMichael Halcrow 		vprintk(fmt, args);
97237fead6SMichael Halcrow 	va_end(args);
98237fead6SMichael Halcrow }
99237fead6SMichael Halcrow 
100237fead6SMichael Halcrow /**
1014981e081SMichael Halcrow  * ecryptfs_init_persistent_file
1024981e081SMichael Halcrow  * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
1034981e081SMichael Halcrow  *                   the lower dentry and the lower mount set
1044981e081SMichael Halcrow  *
1054981e081SMichael Halcrow  * eCryptfs only ever keeps a single open file for every lower
1064981e081SMichael Halcrow  * inode. All I/O operations to the lower inode occur through that
1074981e081SMichael Halcrow  * file. When the first eCryptfs dentry that interposes with the first
1084981e081SMichael Halcrow  * lower dentry for that inode is created, this function creates the
1094981e081SMichael Halcrow  * persistent file struct and associates it with the eCryptfs
1104981e081SMichael Halcrow  * inode. When the eCryptfs inode is destroyed, the file is closed.
1114981e081SMichael Halcrow  *
1124981e081SMichael Halcrow  * The persistent file will be opened with read/write permissions, if
1134981e081SMichael Halcrow  * possible. Otherwise, it is opened read-only.
1144981e081SMichael Halcrow  *
1154981e081SMichael Halcrow  * This function does nothing if a lower persistent file is already
1164981e081SMichael Halcrow  * associated with the eCryptfs inode.
1174981e081SMichael Halcrow  *
1184981e081SMichael Halcrow  * Returns zero on success; non-zero otherwise
1194981e081SMichael Halcrow  */
1207896b631SAdrian Bunk static int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
1214981e081SMichael Halcrow {
1224981e081SMichael Halcrow 	struct ecryptfs_inode_info *inode_info =
1234981e081SMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
1244981e081SMichael Halcrow 	int rc = 0;
1254981e081SMichael Halcrow 
1264981e081SMichael Halcrow 	mutex_lock(&inode_info->lower_file_mutex);
1274981e081SMichael Halcrow 	if (!inode_info->lower_file) {
1284981e081SMichael Halcrow 		struct dentry *lower_dentry;
1294981e081SMichael Halcrow 		struct vfsmount *lower_mnt =
1304981e081SMichael Halcrow 			ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
1314981e081SMichael Halcrow 
1324981e081SMichael Halcrow 		lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
133*746f1e55SMichael Halcrow 		rc = ecryptfs_privileged_open(&inode_info->lower_file,
1344981e081SMichael Halcrow 						     lower_dentry, lower_mnt);
135*746f1e55SMichael Halcrow 		if (rc || IS_ERR(inode_info->lower_file)) {
136*746f1e55SMichael Halcrow 			printk(KERN_ERR "Error opening lower persistent file "
137*746f1e55SMichael Halcrow 			       "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
138*746f1e55SMichael Halcrow 			       "rc = [%d]\n", lower_dentry, lower_mnt, rc);
1394981e081SMichael Halcrow 			rc = PTR_ERR(inode_info->lower_file);
1404981e081SMichael Halcrow 			inode_info->lower_file = NULL;
1414981e081SMichael Halcrow 		}
1424981e081SMichael Halcrow 	}
1434981e081SMichael Halcrow 	mutex_unlock(&inode_info->lower_file_mutex);
1444981e081SMichael Halcrow 	return rc;
1454981e081SMichael Halcrow }
1464981e081SMichael Halcrow 
1474981e081SMichael Halcrow /**
148237fead6SMichael Halcrow  * ecryptfs_interpose
149237fead6SMichael Halcrow  * @lower_dentry: Existing dentry in the lower filesystem
150237fead6SMichael Halcrow  * @dentry: ecryptfs' dentry
151237fead6SMichael Halcrow  * @sb: ecryptfs's super_block
152237fead6SMichael Halcrow  * @flag: If set to true, then d_add is called, else d_instantiate is called
153237fead6SMichael Halcrow  *
154237fead6SMichael Halcrow  * Interposes upper and lower dentries.
155237fead6SMichael Halcrow  *
156237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
157237fead6SMichael Halcrow  */
158237fead6SMichael Halcrow int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
159237fead6SMichael Halcrow 		       struct super_block *sb, int flag)
160237fead6SMichael Halcrow {
161237fead6SMichael Halcrow 	struct inode *lower_inode;
162237fead6SMichael Halcrow 	struct inode *inode;
163237fead6SMichael Halcrow 	int rc = 0;
164237fead6SMichael Halcrow 
165237fead6SMichael Halcrow 	lower_inode = lower_dentry->d_inode;
166237fead6SMichael Halcrow 	if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
167237fead6SMichael Halcrow 		rc = -EXDEV;
168237fead6SMichael Halcrow 		goto out;
169237fead6SMichael Halcrow 	}
170237fead6SMichael Halcrow 	if (!igrab(lower_inode)) {
171237fead6SMichael Halcrow 		rc = -ESTALE;
172237fead6SMichael Halcrow 		goto out;
173237fead6SMichael Halcrow 	}
174237fead6SMichael Halcrow 	inode = iget5_locked(sb, (unsigned long)lower_inode,
175237fead6SMichael Halcrow 			     ecryptfs_inode_test, ecryptfs_inode_set,
176237fead6SMichael Halcrow 			     lower_inode);
177237fead6SMichael Halcrow 	if (!inode) {
178237fead6SMichael Halcrow 		rc = -EACCES;
179237fead6SMichael Halcrow 		iput(lower_inode);
180237fead6SMichael Halcrow 		goto out;
181237fead6SMichael Halcrow 	}
182237fead6SMichael Halcrow 	if (inode->i_state & I_NEW)
183237fead6SMichael Halcrow 		unlock_new_inode(inode);
184237fead6SMichael Halcrow 	else
185237fead6SMichael Halcrow 		iput(lower_inode);
186237fead6SMichael Halcrow 	if (S_ISLNK(lower_inode->i_mode))
187237fead6SMichael Halcrow 		inode->i_op = &ecryptfs_symlink_iops;
188237fead6SMichael Halcrow 	else if (S_ISDIR(lower_inode->i_mode))
189237fead6SMichael Halcrow 		inode->i_op = &ecryptfs_dir_iops;
190237fead6SMichael Halcrow 	if (S_ISDIR(lower_inode->i_mode))
191237fead6SMichael Halcrow 		inode->i_fop = &ecryptfs_dir_fops;
19226da8205SPekka Enberg 	if (special_file(lower_inode->i_mode))
193237fead6SMichael Halcrow 		init_special_inode(inode, lower_inode->i_mode,
194237fead6SMichael Halcrow 				   lower_inode->i_rdev);
195237fead6SMichael Halcrow 	dentry->d_op = &ecryptfs_dops;
196237fead6SMichael Halcrow 	if (flag)
197237fead6SMichael Halcrow 		d_add(dentry, inode);
198237fead6SMichael Halcrow 	else
199237fead6SMichael Halcrow 		d_instantiate(dentry, inode);
2000cc72dc7SJosef "Jeff" Sipek 	fsstack_copy_attr_all(inode, lower_inode, NULL);
201237fead6SMichael Halcrow 	/* This size will be overwritten for real files w/ headers and
202237fead6SMichael Halcrow 	 * other metadata */
2030cc72dc7SJosef "Jeff" Sipek 	fsstack_copy_inode_size(inode, lower_inode);
2044981e081SMichael Halcrow 	rc = ecryptfs_init_persistent_file(dentry);
2054981e081SMichael Halcrow 	if (rc) {
2064981e081SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize the "
2074981e081SMichael Halcrow 		       "persistent file for the dentry with name [%s]; "
20818d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, dentry->d_name.name, rc);
2094981e081SMichael Halcrow 		goto out;
2104981e081SMichael Halcrow 	}
211237fead6SMichael Halcrow out:
212237fead6SMichael Halcrow 	return rc;
213237fead6SMichael Halcrow }
214237fead6SMichael Halcrow 
2152830bfd6SEric Sandeen enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
2162830bfd6SEric Sandeen        ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
2172830bfd6SEric Sandeen        ecryptfs_opt_ecryptfs_key_bytes,
21817398957SMichael Halcrow        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
21917398957SMichael Halcrow        ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
220237fead6SMichael Halcrow 
221237fead6SMichael Halcrow static match_table_t tokens = {
222237fead6SMichael Halcrow 	{ecryptfs_opt_sig, "sig=%s"},
223237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
224237fead6SMichael Halcrow 	{ecryptfs_opt_cipher, "cipher=%s"},
225237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
226237fead6SMichael Halcrow 	{ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
227237fead6SMichael Halcrow 	{ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
22817398957SMichael Halcrow 	{ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
22917398957SMichael Halcrow 	{ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
230237fead6SMichael Halcrow 	{ecryptfs_opt_err, NULL}
231237fead6SMichael Halcrow };
232237fead6SMichael Halcrow 
233f4aad16aSMichael Halcrow static int ecryptfs_init_global_auth_toks(
234f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
235237fead6SMichael Halcrow {
236f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
237237fead6SMichael Halcrow 	int rc = 0;
238237fead6SMichael Halcrow 
239f4aad16aSMichael Halcrow 	list_for_each_entry(global_auth_tok,
240f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
241f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
2425dda6992SMichael Halcrow 		rc = ecryptfs_keyring_auth_tok_for_sig(
243f4aad16aSMichael Halcrow 			&global_auth_tok->global_auth_tok_key,
244f4aad16aSMichael Halcrow 			&global_auth_tok->global_auth_tok,
2455dda6992SMichael Halcrow 			global_auth_tok->sig);
2465dda6992SMichael Halcrow 		if (rc) {
247f4aad16aSMichael Halcrow 			printk(KERN_ERR "Could not find valid key in user "
248f4aad16aSMichael Halcrow 			       "session keyring for sig specified in mount "
249f4aad16aSMichael Halcrow 			       "option: [%s]\n", global_auth_tok->sig);
250f4aad16aSMichael Halcrow 			global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
251f4aad16aSMichael Halcrow 			rc = 0;
252f4aad16aSMichael Halcrow 		} else
253f4aad16aSMichael Halcrow 			global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
254237fead6SMichael Halcrow 	}
255237fead6SMichael Halcrow 	return rc;
256237fead6SMichael Halcrow }
257237fead6SMichael Halcrow 
258f4aad16aSMichael Halcrow static void ecryptfs_init_mount_crypt_stat(
259f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
260f4aad16aSMichael Halcrow {
261f4aad16aSMichael Halcrow 	memset((void *)mount_crypt_stat, 0,
262f4aad16aSMichael Halcrow 	       sizeof(struct ecryptfs_mount_crypt_stat));
263f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
264f4aad16aSMichael Halcrow 	mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
265f4aad16aSMichael Halcrow 	mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
266f4aad16aSMichael Halcrow }
267f4aad16aSMichael Halcrow 
268237fead6SMichael Halcrow /**
269237fead6SMichael Halcrow  * ecryptfs_parse_options
270237fead6SMichael Halcrow  * @sb: The ecryptfs super block
271237fead6SMichael Halcrow  * @options: The options pased to the kernel
272237fead6SMichael Halcrow  *
273237fead6SMichael Halcrow  * Parse mount options:
274237fead6SMichael Halcrow  * debug=N 	   - ecryptfs_verbosity level for debug output
275237fead6SMichael Halcrow  * sig=XXX	   - description(signature) of the key to use
276237fead6SMichael Halcrow  *
277237fead6SMichael Halcrow  * Returns the dentry object of the lower-level (lower/interposed)
278237fead6SMichael Halcrow  * directory; We want to mount our stackable file system on top of
279237fead6SMichael Halcrow  * that lower directory.
280237fead6SMichael Halcrow  *
281237fead6SMichael Halcrow  * The signature of the key to use must be the description of a key
282237fead6SMichael Halcrow  * already in the keyring. Mounting will fail if the key can not be
283237fead6SMichael Halcrow  * found.
284237fead6SMichael Halcrow  *
285237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
286237fead6SMichael Halcrow  */
287237fead6SMichael Halcrow static int ecryptfs_parse_options(struct super_block *sb, char *options)
288237fead6SMichael Halcrow {
289237fead6SMichael Halcrow 	char *p;
290237fead6SMichael Halcrow 	int rc = 0;
291237fead6SMichael Halcrow 	int sig_set = 0;
292237fead6SMichael Halcrow 	int cipher_name_set = 0;
293237fead6SMichael Halcrow 	int cipher_key_bytes;
294237fead6SMichael Halcrow 	int cipher_key_bytes_set = 0;
295237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
296237fead6SMichael Halcrow 		&ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
297237fead6SMichael Halcrow 	substring_t args[MAX_OPT_ARGS];
298237fead6SMichael Halcrow 	int token;
299237fead6SMichael Halcrow 	char *sig_src;
300237fead6SMichael Halcrow 	char *cipher_name_dst;
301237fead6SMichael Halcrow 	char *cipher_name_src;
302237fead6SMichael Halcrow 	char *cipher_key_bytes_src;
303237fead6SMichael Halcrow 	int cipher_name_len;
304237fead6SMichael Halcrow 
305237fead6SMichael Halcrow 	if (!options) {
306237fead6SMichael Halcrow 		rc = -EINVAL;
307237fead6SMichael Halcrow 		goto out;
308237fead6SMichael Halcrow 	}
309956159c3SMichael Halcrow 	ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
310237fead6SMichael Halcrow 	while ((p = strsep(&options, ",")) != NULL) {
311237fead6SMichael Halcrow 		if (!*p)
312237fead6SMichael Halcrow 			continue;
313237fead6SMichael Halcrow 		token = match_token(p, tokens, args);
314237fead6SMichael Halcrow 		switch (token) {
315237fead6SMichael Halcrow 		case ecryptfs_opt_sig:
316237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_sig:
317237fead6SMichael Halcrow 			sig_src = args[0].from;
318f4aad16aSMichael Halcrow 			rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
319f4aad16aSMichael Halcrow 							  sig_src);
320f4aad16aSMichael Halcrow 			if (rc) {
321f4aad16aSMichael Halcrow 				printk(KERN_ERR "Error attempting to register "
322f4aad16aSMichael Halcrow 				       "global sig; rc = [%d]\n", rc);
323f4aad16aSMichael Halcrow 				goto out;
324f4aad16aSMichael Halcrow 			}
325237fead6SMichael Halcrow 			sig_set = 1;
326237fead6SMichael Halcrow 			break;
327237fead6SMichael Halcrow 		case ecryptfs_opt_cipher:
328237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_cipher:
329237fead6SMichael Halcrow 			cipher_name_src = args[0].from;
330237fead6SMichael Halcrow 			cipher_name_dst =
331237fead6SMichael Halcrow 				mount_crypt_stat->
332237fead6SMichael Halcrow 				global_default_cipher_name;
333237fead6SMichael Halcrow 			strncpy(cipher_name_dst, cipher_name_src,
334237fead6SMichael Halcrow 				ECRYPTFS_MAX_CIPHER_NAME_SIZE);
335237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
336237fead6SMichael Halcrow 					"The mount_crypt_stat "
337237fead6SMichael Halcrow 					"global_default_cipher_name set to: "
338237fead6SMichael Halcrow 					"[%s]\n", cipher_name_dst);
339237fead6SMichael Halcrow 			cipher_name_set = 1;
340237fead6SMichael Halcrow 			break;
341237fead6SMichael Halcrow 		case ecryptfs_opt_ecryptfs_key_bytes:
342237fead6SMichael Halcrow 			cipher_key_bytes_src = args[0].from;
343237fead6SMichael Halcrow 			cipher_key_bytes =
344237fead6SMichael Halcrow 				(int)simple_strtol(cipher_key_bytes_src,
345237fead6SMichael Halcrow 						   &cipher_key_bytes_src, 0);
346237fead6SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size =
347237fead6SMichael Halcrow 				cipher_key_bytes;
348237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
349237fead6SMichael Halcrow 					"The mount_crypt_stat "
350237fead6SMichael Halcrow 					"global_default_cipher_key_size "
351237fead6SMichael Halcrow 					"set to: [%d]\n", mount_crypt_stat->
352237fead6SMichael Halcrow 					global_default_cipher_key_size);
353237fead6SMichael Halcrow 			cipher_key_bytes_set = 1;
354237fead6SMichael Halcrow 			break;
355237fead6SMichael Halcrow 		case ecryptfs_opt_passthrough:
356237fead6SMichael Halcrow 			mount_crypt_stat->flags |=
357237fead6SMichael Halcrow 				ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
358237fead6SMichael Halcrow 			break;
35917398957SMichael Halcrow 		case ecryptfs_opt_xattr_metadata:
36017398957SMichael Halcrow 			mount_crypt_stat->flags |=
36117398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
36217398957SMichael Halcrow 			break;
36317398957SMichael Halcrow 		case ecryptfs_opt_encrypted_view:
36417398957SMichael Halcrow 			mount_crypt_stat->flags |=
36517398957SMichael Halcrow 				ECRYPTFS_XATTR_METADATA_ENABLED;
36617398957SMichael Halcrow 			mount_crypt_stat->flags |=
36717398957SMichael Halcrow 				ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
36817398957SMichael Halcrow 			break;
369237fead6SMichael Halcrow 		case ecryptfs_opt_err:
370237fead6SMichael Halcrow 		default:
371237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING,
372237fead6SMichael Halcrow 					"eCryptfs: unrecognized option '%s'\n",
373237fead6SMichael Halcrow 					p);
374237fead6SMichael Halcrow 		}
375237fead6SMichael Halcrow 	}
376237fead6SMichael Halcrow 	if (!sig_set) {
377237fead6SMichael Halcrow 		rc = -EINVAL;
378956159c3SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
379956159c3SMichael Halcrow 				"auth tok signature as a mount "
380237fead6SMichael Halcrow 				"parameter; see the eCryptfs README\n");
381237fead6SMichael Halcrow 		goto out;
382237fead6SMichael Halcrow 	}
383237fead6SMichael Halcrow 	if (!cipher_name_set) {
384237fead6SMichael Halcrow 		cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
385237fead6SMichael Halcrow 		if (unlikely(cipher_name_len
386237fead6SMichael Halcrow 			     >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
387237fead6SMichael Halcrow 			rc = -EINVAL;
388237fead6SMichael Halcrow 			BUG();
389237fead6SMichael Halcrow 			goto out;
390237fead6SMichael Halcrow 		}
391237fead6SMichael Halcrow 		memcpy(mount_crypt_stat->global_default_cipher_name,
392237fead6SMichael Halcrow 		       ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
393237fead6SMichael Halcrow 		mount_crypt_stat->global_default_cipher_name[cipher_name_len]
394237fead6SMichael Halcrow 		    = '\0';
395237fead6SMichael Halcrow 	}
396237fead6SMichael Halcrow 	if (!cipher_key_bytes_set) {
397e5d9cbdeSMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size = 0;
398237fead6SMichael Halcrow 	}
399af440f52SEric Sandeen 	mutex_lock(&key_tfm_list_mutex);
400af440f52SEric Sandeen 	if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
401af440f52SEric Sandeen 				 NULL))
4025dda6992SMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(
403f4aad16aSMichael Halcrow 			NULL, mount_crypt_stat->global_default_cipher_name,
4045dda6992SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size);
405af440f52SEric Sandeen 	mutex_unlock(&key_tfm_list_mutex);
4065dda6992SMichael Halcrow 	if (rc) {
407f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to initialize cipher with "
40881acbcd6SAndrew Morton 		       "name = [%s] and key size = [%td]; rc = [%d]\n",
409237fead6SMichael Halcrow 		       mount_crypt_stat->global_default_cipher_name,
410237fead6SMichael Halcrow 		       mount_crypt_stat->global_default_cipher_key_size, rc);
411237fead6SMichael Halcrow 		rc = -EINVAL;
412237fead6SMichael Halcrow 		goto out;
413237fead6SMichael Halcrow 	}
4145dda6992SMichael Halcrow 	rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
4155dda6992SMichael Halcrow 	if (rc) {
416f4aad16aSMichael Halcrow 		printk(KERN_WARNING "One or more global auth toks could not "
417f4aad16aSMichael Halcrow 		       "properly register; rc = [%d]\n", rc);
418237fead6SMichael Halcrow 	}
419f4aad16aSMichael Halcrow 	rc = 0;
420237fead6SMichael Halcrow out:
421237fead6SMichael Halcrow 	return rc;
422237fead6SMichael Halcrow }
423237fead6SMichael Halcrow 
424237fead6SMichael Halcrow struct kmem_cache *ecryptfs_sb_info_cache;
425237fead6SMichael Halcrow 
426237fead6SMichael Halcrow /**
427237fead6SMichael Halcrow  * ecryptfs_fill_super
428237fead6SMichael Halcrow  * @sb: The ecryptfs super block
429237fead6SMichael Halcrow  * @raw_data: The options passed to mount
430237fead6SMichael Halcrow  * @silent: Not used but required by function prototype
431237fead6SMichael Halcrow  *
432237fead6SMichael Halcrow  * Sets up what we can of the sb, rest is done in ecryptfs_read_super
433237fead6SMichael Halcrow  *
434237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
435237fead6SMichael Halcrow  */
436237fead6SMichael Halcrow static int
437237fead6SMichael Halcrow ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
438237fead6SMichael Halcrow {
439237fead6SMichael Halcrow 	int rc = 0;
440237fead6SMichael Halcrow 
441237fead6SMichael Halcrow 	/* Released in ecryptfs_put_super() */
442237fead6SMichael Halcrow 	ecryptfs_set_superblock_private(sb,
443c3762229SRobert P. J. Day 					kmem_cache_zalloc(ecryptfs_sb_info_cache,
444e94b1766SChristoph Lameter 							 GFP_KERNEL));
445237fead6SMichael Halcrow 	if (!ecryptfs_superblock_to_private(sb)) {
446237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Out of memory\n");
447237fead6SMichael Halcrow 		rc = -ENOMEM;
448237fead6SMichael Halcrow 		goto out;
449237fead6SMichael Halcrow 	}
450237fead6SMichael Halcrow 	sb->s_op = &ecryptfs_sops;
451237fead6SMichael Halcrow 	/* Released through deactivate_super(sb) from get_sb_nodev */
452237fead6SMichael Halcrow 	sb->s_root = d_alloc(NULL, &(const struct qstr) {
453237fead6SMichael Halcrow 			     .hash = 0,.name = "/",.len = 1});
454237fead6SMichael Halcrow 	if (!sb->s_root) {
455237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
456237fead6SMichael Halcrow 		rc = -ENOMEM;
457237fead6SMichael Halcrow 		goto out;
458237fead6SMichael Halcrow 	}
459237fead6SMichael Halcrow 	sb->s_root->d_op = &ecryptfs_dops;
460237fead6SMichael Halcrow 	sb->s_root->d_sb = sb;
461237fead6SMichael Halcrow 	sb->s_root->d_parent = sb->s_root;
462237fead6SMichael Halcrow 	/* Released in d_release when dput(sb->s_root) is called */
463237fead6SMichael Halcrow 	/* through deactivate_super(sb) from get_sb_nodev() */
464237fead6SMichael Halcrow 	ecryptfs_set_dentry_private(sb->s_root,
465c3762229SRobert P. J. Day 				    kmem_cache_zalloc(ecryptfs_dentry_info_cache,
466e94b1766SChristoph Lameter 						     GFP_KERNEL));
467237fead6SMichael Halcrow 	if (!ecryptfs_dentry_to_private(sb->s_root)) {
468237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
469237fead6SMichael Halcrow 				"dentry_info_cache alloc failed\n");
470237fead6SMichael Halcrow 		rc = -ENOMEM;
471237fead6SMichael Halcrow 		goto out;
472237fead6SMichael Halcrow 	}
473237fead6SMichael Halcrow 	rc = 0;
474237fead6SMichael Halcrow out:
475237fead6SMichael Halcrow 	/* Should be able to rely on deactivate_super called from
476237fead6SMichael Halcrow 	 * get_sb_nodev */
477237fead6SMichael Halcrow 	return rc;
478237fead6SMichael Halcrow }
479237fead6SMichael Halcrow 
480237fead6SMichael Halcrow /**
481237fead6SMichael Halcrow  * ecryptfs_read_super
482237fead6SMichael Halcrow  * @sb: The ecryptfs super block
483237fead6SMichael Halcrow  * @dev_name: The path to mount over
484237fead6SMichael Halcrow  *
485237fead6SMichael Halcrow  * Read the super block of the lower filesystem, and use
486237fead6SMichael Halcrow  * ecryptfs_interpose to create our initial inode and super block
487237fead6SMichael Halcrow  * struct.
488237fead6SMichael Halcrow  */
489237fead6SMichael Halcrow static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
490237fead6SMichael Halcrow {
491237fead6SMichael Halcrow 	int rc;
492237fead6SMichael Halcrow 	struct nameidata nd;
493237fead6SMichael Halcrow 	struct dentry *lower_root;
494237fead6SMichael Halcrow 	struct vfsmount *lower_mnt;
495237fead6SMichael Halcrow 
496237fead6SMichael Halcrow 	memset(&nd, 0, sizeof(struct nameidata));
49782b16528SDmitriy Monakhov 	rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
498237fead6SMichael Halcrow 	if (rc) {
499237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
50065dc8145SMichael Halcrow 		goto out;
501237fead6SMichael Halcrow 	}
5024ac91378SJan Blunck 	lower_root = nd.path.dentry;
5034ac91378SJan Blunck 	lower_mnt = nd.path.mnt;
504237fead6SMichael Halcrow 	ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
505237fead6SMichael Halcrow 	sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
5067c9e70efSEric Sandeen 	sb->s_blocksize = lower_root->d_sb->s_blocksize;
507237fead6SMichael Halcrow 	ecryptfs_set_dentry_lower(sb->s_root, lower_root);
508237fead6SMichael Halcrow 	ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
5095dda6992SMichael Halcrow 	rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
5105dda6992SMichael Halcrow 	if (rc)
511237fead6SMichael Halcrow 		goto out_free;
512237fead6SMichael Halcrow 	rc = 0;
513237fead6SMichael Halcrow 	goto out;
514237fead6SMichael Halcrow out_free:
5151d957f9bSJan Blunck 	path_put(&nd.path);
516237fead6SMichael Halcrow out:
517237fead6SMichael Halcrow 	return rc;
518237fead6SMichael Halcrow }
519237fead6SMichael Halcrow 
520237fead6SMichael Halcrow /**
521237fead6SMichael Halcrow  * ecryptfs_get_sb
522237fead6SMichael Halcrow  * @fs_type
523237fead6SMichael Halcrow  * @flags
524237fead6SMichael Halcrow  * @dev_name: The path to mount over
525237fead6SMichael Halcrow  * @raw_data: The options passed into the kernel
526237fead6SMichael Halcrow  *
527237fead6SMichael Halcrow  * The whole ecryptfs_get_sb process is broken into 4 functions:
528237fead6SMichael Halcrow  * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
529237fead6SMichael Halcrow  * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
530237fead6SMichael Halcrow  *                        with as much information as it can before needing
531237fead6SMichael Halcrow  *                        the lower filesystem.
532237fead6SMichael Halcrow  * ecryptfs_read_super(): this accesses the lower filesystem and uses
533237fead6SMichael Halcrow  *                        ecryptfs_interpolate to perform most of the linking
534237fead6SMichael Halcrow  * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
535237fead6SMichael Halcrow  */
536237fead6SMichael Halcrow static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
537237fead6SMichael Halcrow 			const char *dev_name, void *raw_data,
538237fead6SMichael Halcrow 			struct vfsmount *mnt)
539237fead6SMichael Halcrow {
540237fead6SMichael Halcrow 	int rc;
541237fead6SMichael Halcrow 	struct super_block *sb;
542237fead6SMichael Halcrow 
543237fead6SMichael Halcrow 	rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
544237fead6SMichael Halcrow 	if (rc < 0) {
545237fead6SMichael Halcrow 		printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
546237fead6SMichael Halcrow 		goto out;
547237fead6SMichael Halcrow 	}
548237fead6SMichael Halcrow 	sb = mnt->mnt_sb;
549237fead6SMichael Halcrow 	rc = ecryptfs_parse_options(sb, raw_data);
550237fead6SMichael Halcrow 	if (rc) {
551237fead6SMichael Halcrow 		printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
552237fead6SMichael Halcrow 		goto out_abort;
553237fead6SMichael Halcrow 	}
554237fead6SMichael Halcrow 	rc = ecryptfs_read_super(sb, dev_name);
555237fead6SMichael Halcrow 	if (rc) {
556237fead6SMichael Halcrow 		printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
557237fead6SMichael Halcrow 		goto out_abort;
558237fead6SMichael Halcrow 	}
559237fead6SMichael Halcrow 	goto out;
560237fead6SMichael Halcrow out_abort:
561237fead6SMichael Halcrow 	dput(sb->s_root);
562237fead6SMichael Halcrow 	up_write(&sb->s_umount);
563237fead6SMichael Halcrow 	deactivate_super(sb);
564237fead6SMichael Halcrow out:
565237fead6SMichael Halcrow 	return rc;
566237fead6SMichael Halcrow }
567237fead6SMichael Halcrow 
568237fead6SMichael Halcrow /**
569237fead6SMichael Halcrow  * ecryptfs_kill_block_super
570237fead6SMichael Halcrow  * @sb: The ecryptfs super block
571237fead6SMichael Halcrow  *
572237fead6SMichael Halcrow  * Used to bring the superblock down and free the private data.
573237fead6SMichael Halcrow  * Private data is free'd in ecryptfs_put_super()
574237fead6SMichael Halcrow  */
575237fead6SMichael Halcrow static void ecryptfs_kill_block_super(struct super_block *sb)
576237fead6SMichael Halcrow {
577237fead6SMichael Halcrow 	generic_shutdown_super(sb);
578237fead6SMichael Halcrow }
579237fead6SMichael Halcrow 
580237fead6SMichael Halcrow static struct file_system_type ecryptfs_fs_type = {
581237fead6SMichael Halcrow 	.owner = THIS_MODULE,
582237fead6SMichael Halcrow 	.name = "ecryptfs",
583237fead6SMichael Halcrow 	.get_sb = ecryptfs_get_sb,
584237fead6SMichael Halcrow 	.kill_sb = ecryptfs_kill_block_super,
585237fead6SMichael Halcrow 	.fs_flags = 0
586237fead6SMichael Halcrow };
587237fead6SMichael Halcrow 
588237fead6SMichael Halcrow /**
589237fead6SMichael Halcrow  * inode_info_init_once
590237fead6SMichael Halcrow  *
591237fead6SMichael Halcrow  * Initializes the ecryptfs_inode_info_cache when it is created
592237fead6SMichael Halcrow  */
593237fead6SMichael Halcrow static void
5944ba9b9d0SChristoph Lameter inode_info_init_once(struct kmem_cache *cachep, void *vptr)
595237fead6SMichael Halcrow {
596237fead6SMichael Halcrow 	struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
597237fead6SMichael Halcrow 
598237fead6SMichael Halcrow 	inode_init_once(&ei->vfs_inode);
599237fead6SMichael Halcrow }
600237fead6SMichael Halcrow 
601237fead6SMichael Halcrow static struct ecryptfs_cache_info {
602e18b890bSChristoph Lameter 	struct kmem_cache **cache;
603237fead6SMichael Halcrow 	const char *name;
604237fead6SMichael Halcrow 	size_t size;
6054ba9b9d0SChristoph Lameter 	void (*ctor)(struct kmem_cache *cache, void *obj);
606237fead6SMichael Halcrow } ecryptfs_cache_infos[] = {
607237fead6SMichael Halcrow 	{
608237fead6SMichael Halcrow 		.cache = &ecryptfs_auth_tok_list_item_cache,
609237fead6SMichael Halcrow 		.name = "ecryptfs_auth_tok_list_item",
610237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_auth_tok_list_item),
611237fead6SMichael Halcrow 	},
612237fead6SMichael Halcrow 	{
613237fead6SMichael Halcrow 		.cache = &ecryptfs_file_info_cache,
614237fead6SMichael Halcrow 		.name = "ecryptfs_file_cache",
615237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_file_info),
616237fead6SMichael Halcrow 	},
617237fead6SMichael Halcrow 	{
618237fead6SMichael Halcrow 		.cache = &ecryptfs_dentry_info_cache,
619237fead6SMichael Halcrow 		.name = "ecryptfs_dentry_info_cache",
620237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_dentry_info),
621237fead6SMichael Halcrow 	},
622237fead6SMichael Halcrow 	{
623237fead6SMichael Halcrow 		.cache = &ecryptfs_inode_info_cache,
624237fead6SMichael Halcrow 		.name = "ecryptfs_inode_cache",
625237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_inode_info),
626237fead6SMichael Halcrow 		.ctor = inode_info_init_once,
627237fead6SMichael Halcrow 	},
628237fead6SMichael Halcrow 	{
629237fead6SMichael Halcrow 		.cache = &ecryptfs_sb_info_cache,
630237fead6SMichael Halcrow 		.name = "ecryptfs_sb_cache",
631237fead6SMichael Halcrow 		.size = sizeof(struct ecryptfs_sb_info),
632237fead6SMichael Halcrow 	},
633237fead6SMichael Halcrow 	{
634237fead6SMichael Halcrow 		.cache = &ecryptfs_header_cache_1,
635237fead6SMichael Halcrow 		.name = "ecryptfs_headers_1",
636237fead6SMichael Halcrow 		.size = PAGE_CACHE_SIZE,
637237fead6SMichael Halcrow 	},
638237fead6SMichael Halcrow 	{
639237fead6SMichael Halcrow 		.cache = &ecryptfs_header_cache_2,
640237fead6SMichael Halcrow 		.name = "ecryptfs_headers_2",
641237fead6SMichael Halcrow 		.size = PAGE_CACHE_SIZE,
642237fead6SMichael Halcrow 	},
643237fead6SMichael Halcrow 	{
644dd2a3b7aSMichael Halcrow 		.cache = &ecryptfs_xattr_cache,
645dd2a3b7aSMichael Halcrow 		.name = "ecryptfs_xattr_cache",
646dd2a3b7aSMichael Halcrow 		.size = PAGE_CACHE_SIZE,
647dd2a3b7aSMichael Halcrow 	},
648dd2a3b7aSMichael Halcrow 	{
649eb95e7ffSMichael Halcrow 		.cache = &ecryptfs_key_record_cache,
650eb95e7ffSMichael Halcrow 		.name = "ecryptfs_key_record_cache",
651eb95e7ffSMichael Halcrow 		.size = sizeof(struct ecryptfs_key_record),
652eb95e7ffSMichael Halcrow 	},
653956159c3SMichael Halcrow 	{
654956159c3SMichael Halcrow 		.cache = &ecryptfs_key_sig_cache,
655956159c3SMichael Halcrow 		.name = "ecryptfs_key_sig_cache",
656956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_key_sig),
657956159c3SMichael Halcrow 	},
658956159c3SMichael Halcrow 	{
659956159c3SMichael Halcrow 		.cache = &ecryptfs_global_auth_tok_cache,
660956159c3SMichael Halcrow 		.name = "ecryptfs_global_auth_tok_cache",
661956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_global_auth_tok),
662956159c3SMichael Halcrow 	},
663956159c3SMichael Halcrow 	{
664956159c3SMichael Halcrow 		.cache = &ecryptfs_key_tfm_cache,
665956159c3SMichael Halcrow 		.name = "ecryptfs_key_tfm_cache",
666956159c3SMichael Halcrow 		.size = sizeof(struct ecryptfs_key_tfm),
667956159c3SMichael Halcrow 	},
668*746f1e55SMichael Halcrow 	{
669*746f1e55SMichael Halcrow 		.cache = &ecryptfs_open_req_cache,
670*746f1e55SMichael Halcrow 		.name = "ecryptfs_open_req_cache",
671*746f1e55SMichael Halcrow 		.size = sizeof(struct ecryptfs_open_req),
672*746f1e55SMichael Halcrow 	},
673237fead6SMichael Halcrow };
674237fead6SMichael Halcrow 
675237fead6SMichael Halcrow static void ecryptfs_free_kmem_caches(void)
676237fead6SMichael Halcrow {
677237fead6SMichael Halcrow 	int i;
678237fead6SMichael Halcrow 
679237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
680237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
681237fead6SMichael Halcrow 
682237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
683237fead6SMichael Halcrow 		if (*(info->cache))
684237fead6SMichael Halcrow 			kmem_cache_destroy(*(info->cache));
685237fead6SMichael Halcrow 	}
686237fead6SMichael Halcrow }
687237fead6SMichael Halcrow 
688237fead6SMichael Halcrow /**
689237fead6SMichael Halcrow  * ecryptfs_init_kmem_caches
690237fead6SMichael Halcrow  *
691237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
692237fead6SMichael Halcrow  */
693237fead6SMichael Halcrow static int ecryptfs_init_kmem_caches(void)
694237fead6SMichael Halcrow {
695237fead6SMichael Halcrow 	int i;
696237fead6SMichael Halcrow 
697237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
698237fead6SMichael Halcrow 		struct ecryptfs_cache_info *info;
699237fead6SMichael Halcrow 
700237fead6SMichael Halcrow 		info = &ecryptfs_cache_infos[i];
701237fead6SMichael Halcrow 		*(info->cache) = kmem_cache_create(info->name, info->size,
70220c2df83SPaul Mundt 				0, SLAB_HWCACHE_ALIGN, info->ctor);
703237fead6SMichael Halcrow 		if (!*(info->cache)) {
704237fead6SMichael Halcrow 			ecryptfs_free_kmem_caches();
705237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "%s: "
706237fead6SMichael Halcrow 					"kmem_cache_create failed\n",
707237fead6SMichael Halcrow 					info->name);
708237fead6SMichael Halcrow 			return -ENOMEM;
709237fead6SMichael Halcrow 		}
710237fead6SMichael Halcrow 	}
711237fead6SMichael Halcrow 	return 0;
712237fead6SMichael Halcrow }
713237fead6SMichael Halcrow 
7146e90aa97SGreg Kroah-Hartman static struct kobject *ecryptfs_kobj;
715237fead6SMichael Halcrow 
716386f275fSKay Sievers static ssize_t version_show(struct kobject *kobj,
717386f275fSKay Sievers 			    struct kobj_attribute *attr, char *buff)
718237fead6SMichael Halcrow {
719237fead6SMichael Halcrow 	return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
720237fead6SMichael Halcrow }
721237fead6SMichael Halcrow 
722386f275fSKay Sievers static struct kobj_attribute version_attr = __ATTR_RO(version);
723237fead6SMichael Halcrow 
72430a468b1SGreg Kroah-Hartman static struct attribute *attributes[] = {
72530a468b1SGreg Kroah-Hartman 	&version_attr.attr,
72630a468b1SGreg Kroah-Hartman 	NULL,
72730a468b1SGreg Kroah-Hartman };
72830a468b1SGreg Kroah-Hartman 
72930a468b1SGreg Kroah-Hartman static struct attribute_group attr_group = {
73030a468b1SGreg Kroah-Hartman 	.attrs = attributes,
73130a468b1SGreg Kroah-Hartman };
732237fead6SMichael Halcrow 
733237fead6SMichael Halcrow static int do_sysfs_registration(void)
734237fead6SMichael Halcrow {
735237fead6SMichael Halcrow 	int rc;
736237fead6SMichael Halcrow 
7376e90aa97SGreg Kroah-Hartman 	ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
7386e90aa97SGreg Kroah-Hartman 	if (!ecryptfs_kobj) {
739917e865dSGreg Kroah-Hartman 		printk(KERN_ERR "Unable to create ecryptfs kset\n");
740917e865dSGreg Kroah-Hartman 		rc = -ENOMEM;
741237fead6SMichael Halcrow 		goto out;
742237fead6SMichael Halcrow 	}
7436e90aa97SGreg Kroah-Hartman 	rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
744237fead6SMichael Halcrow 	if (rc) {
745237fead6SMichael Halcrow 		printk(KERN_ERR
74630a468b1SGreg Kroah-Hartman 		       "Unable to create ecryptfs version attributes\n");
747197b12d6SGreg Kroah-Hartman 		kobject_put(ecryptfs_kobj);
748237fead6SMichael Halcrow 	}
749237fead6SMichael Halcrow out:
750237fead6SMichael Halcrow 	return rc;
751237fead6SMichael Halcrow }
752237fead6SMichael Halcrow 
753a75de1b3SRyusuke Konishi static void do_sysfs_unregistration(void)
754a75de1b3SRyusuke Konishi {
7556e90aa97SGreg Kroah-Hartman 	sysfs_remove_group(ecryptfs_kobj, &attr_group);
756197b12d6SGreg Kroah-Hartman 	kobject_put(ecryptfs_kobj);
757a75de1b3SRyusuke Konishi }
758a75de1b3SRyusuke Konishi 
759237fead6SMichael Halcrow static int __init ecryptfs_init(void)
760237fead6SMichael Halcrow {
761237fead6SMichael Halcrow 	int rc;
762237fead6SMichael Halcrow 
763237fead6SMichael Halcrow 	if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
764237fead6SMichael Halcrow 		rc = -EINVAL;
765237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
766237fead6SMichael Halcrow 				"larger than the host's page size, and so "
767237fead6SMichael Halcrow 				"eCryptfs cannot run on this system. The "
768237fead6SMichael Halcrow 				"default eCryptfs extent size is [%d] bytes; "
769237fead6SMichael Halcrow 				"the page size is [%d] bytes.\n",
770237fead6SMichael Halcrow 				ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
771237fead6SMichael Halcrow 		goto out;
772237fead6SMichael Halcrow 	}
773237fead6SMichael Halcrow 	rc = ecryptfs_init_kmem_caches();
774237fead6SMichael Halcrow 	if (rc) {
775237fead6SMichael Halcrow 		printk(KERN_ERR
776237fead6SMichael Halcrow 		       "Failed to allocate one or more kmem_cache objects\n");
777237fead6SMichael Halcrow 		goto out;
778237fead6SMichael Halcrow 	}
779237fead6SMichael Halcrow 	rc = register_filesystem(&ecryptfs_fs_type);
780237fead6SMichael Halcrow 	if (rc) {
781237fead6SMichael Halcrow 		printk(KERN_ERR "Failed to register filesystem\n");
782cf81f89dSMichael Halcrow 		goto out_free_kmem_caches;
783237fead6SMichael Halcrow 	}
784237fead6SMichael Halcrow 	rc = do_sysfs_registration();
785237fead6SMichael Halcrow 	if (rc) {
786237fead6SMichael Halcrow 		printk(KERN_ERR "sysfs registration failed\n");
787cf81f89dSMichael Halcrow 		goto out_unregister_filesystem;
788237fead6SMichael Halcrow 	}
789*746f1e55SMichael Halcrow 	rc = ecryptfs_init_kthread();
790*746f1e55SMichael Halcrow 	if (rc) {
791*746f1e55SMichael Halcrow 		printk(KERN_ERR "%s: kthread initialization failed; "
792*746f1e55SMichael Halcrow 		       "rc = [%d]\n", __func__, rc);
793*746f1e55SMichael Halcrow 		goto out_do_sysfs_unregistration;
794*746f1e55SMichael Halcrow 	}
795dddfa461SMichael Halcrow 	rc = ecryptfs_init_messaging(ecryptfs_transport);
796dddfa461SMichael Halcrow 	if (rc) {
797*746f1e55SMichael Halcrow 		printk(KERN_ERR "Failure occured while attempting to "
798dddfa461SMichael Halcrow 				"initialize the eCryptfs netlink socket\n");
799*746f1e55SMichael Halcrow 		goto out_destroy_kthread;
800956159c3SMichael Halcrow 	}
801956159c3SMichael Halcrow 	rc = ecryptfs_init_crypto();
802956159c3SMichael Halcrow 	if (rc) {
803956159c3SMichael Halcrow 		printk(KERN_ERR "Failure whilst attempting to init crypto; "
804956159c3SMichael Halcrow 		       "rc = [%d]\n", rc);
805cf81f89dSMichael Halcrow 		goto out_release_messaging;
806dddfa461SMichael Halcrow 	}
8072830bfd6SEric Sandeen 	if (ecryptfs_verbosity > 0)
8082830bfd6SEric Sandeen 		printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
8092830bfd6SEric Sandeen 			"will be written to the syslog!\n", ecryptfs_verbosity);
8102830bfd6SEric Sandeen 
811cf81f89dSMichael Halcrow 	goto out;
812cf81f89dSMichael Halcrow out_release_messaging:
813cf81f89dSMichael Halcrow 	ecryptfs_release_messaging(ecryptfs_transport);
814*746f1e55SMichael Halcrow out_destroy_kthread:
815*746f1e55SMichael Halcrow 	ecryptfs_destroy_kthread();
816cf81f89dSMichael Halcrow out_do_sysfs_unregistration:
817cf81f89dSMichael Halcrow 	do_sysfs_unregistration();
818cf81f89dSMichael Halcrow out_unregister_filesystem:
819cf81f89dSMichael Halcrow 	unregister_filesystem(&ecryptfs_fs_type);
820cf81f89dSMichael Halcrow out_free_kmem_caches:
821cf81f89dSMichael Halcrow 	ecryptfs_free_kmem_caches();
822237fead6SMichael Halcrow out:
823237fead6SMichael Halcrow 	return rc;
824237fead6SMichael Halcrow }
825237fead6SMichael Halcrow 
826237fead6SMichael Halcrow static void __exit ecryptfs_exit(void)
827237fead6SMichael Halcrow {
828cf81f89dSMichael Halcrow 	int rc;
829cf81f89dSMichael Halcrow 
830cf81f89dSMichael Halcrow 	rc = ecryptfs_destroy_crypto();
831cf81f89dSMichael Halcrow 	if (rc)
832cf81f89dSMichael Halcrow 		printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
833cf81f89dSMichael Halcrow 		       "rc = [%d]\n", rc);
834dddfa461SMichael Halcrow 	ecryptfs_release_messaging(ecryptfs_transport);
835*746f1e55SMichael Halcrow 	ecryptfs_destroy_kthread();
836cf81f89dSMichael Halcrow 	do_sysfs_unregistration();
837237fead6SMichael Halcrow 	unregister_filesystem(&ecryptfs_fs_type);
838237fead6SMichael Halcrow 	ecryptfs_free_kmem_caches();
839237fead6SMichael Halcrow }
840237fead6SMichael Halcrow 
841237fead6SMichael Halcrow MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
842237fead6SMichael Halcrow MODULE_DESCRIPTION("eCryptfs");
843237fead6SMichael Halcrow 
844237fead6SMichael Halcrow MODULE_LICENSE("GPL");
845237fead6SMichael Halcrow 
846237fead6SMichael Halcrow module_init(ecryptfs_init)
847237fead6SMichael Halcrow module_exit(ecryptfs_exit)
848