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