2 * eCryptfs: Linux filesystem encryption layer
4 * Copyright (C) 1997-2003 Erez Zadok
5 * Copyright (C) 2001-2003 Stony Brook University
6 * Copyright (C) 2004-2007 International Business Machines Corp.
7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com>
9 * Tyler Hicks <tyhicks@ou.edu>
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.
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.
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
27 #include <linux/dcache.h>
28 #include <linux/file.h>
29 #include <linux/module.h>
30 #include <linux/namei.h>
31 #include <linux/skbuff.h>
32 #include <linux/crypto.h>
33 #include <linux/mount.h>
34 #include <linux/pagemap.h>
35 #include <linux/key.h>
36 #include <linux/parser.h>
37 #include <linux/fs_stack.h>
38 #include <linux/slab.h>
39 #include <linux/magic.h>
40 #include "ecryptfs_kernel.h"
43 * Module parameter that defines the ecryptfs_verbosity level.
45 int ecryptfs_verbosity = 0;
47 module_param(ecryptfs_verbosity, int, 0);
48 MODULE_PARM_DESC(ecryptfs_verbosity,
49 "Initial verbosity level (0 or 1; defaults to "
50 "0, which is Quiet)");
53 * Module parameter that defines the number of message buffer elements
55 unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
57 module_param(ecryptfs_message_buf_len, uint, 0);
58 MODULE_PARM_DESC(ecryptfs_message_buf_len,
59 "Number of message buffer elements");
62 * Module parameter that defines the maximum guaranteed amount of time to wait
63 * for a response from ecryptfsd. The actual sleep time will be, more than
64 * likely, a small amount greater than this specified value, but only less if
65 * the message successfully arrives.
67 signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
69 module_param(ecryptfs_message_wait_timeout, long, 0);
70 MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
71 "Maximum number of seconds that an operation will "
72 "sleep while waiting for a message response from "
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.
80 unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
82 module_param(ecryptfs_number_of_users, uint, 0);
83 MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
84 "concurrent users of eCryptfs");
86 void __ecryptfs_printk(const char *fmt, ...)
90 if (fmt[1] == '7') { /* KERN_DEBUG */
91 if (ecryptfs_verbosity >= 1)
99 * ecryptfs_init_persistent_file
100 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
101 * the lower dentry and the lower mount set
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
107 * persistent file struct and associates it with the eCryptfs
108 * inode. When the eCryptfs inode is destroyed, the file is closed.
110 * The persistent file will be opened with read/write permissions, if
111 * possible. Otherwise, it is opened read-only.
113 * This function does nothing if a lower persistent file is already
114 * associated with the eCryptfs inode.
116 * Returns zero on success; non-zero otherwise
118 int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
120 const struct cred *cred = current_cred();
121 struct ecryptfs_inode_info *inode_info =
122 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
125 mutex_lock(&inode_info->lower_file_mutex);
126 if (!inode_info->lower_file) {
127 struct dentry *lower_dentry;
128 struct vfsmount *lower_mnt =
129 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
131 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
132 rc = ecryptfs_privileged_open(&inode_info->lower_file,
133 lower_dentry, lower_mnt, cred);
135 printk(KERN_ERR "Error opening lower persistent file "
136 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
137 "rc = [%d]\n", lower_dentry, lower_mnt, rc);
138 inode_info->lower_file = NULL;
141 mutex_unlock(&inode_info->lower_file_mutex);
145 static struct inode *ecryptfs_get_inode(struct inode *lower_inode,
146 struct super_block *sb)
151 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
155 if (!igrab(lower_inode)) {
159 inode = iget5_locked(sb, (unsigned long)lower_inode,
160 ecryptfs_inode_test, ecryptfs_inode_set,
167 if (inode->i_state & I_NEW)
168 unlock_new_inode(inode);
171 if (S_ISLNK(lower_inode->i_mode))
172 inode->i_op = &ecryptfs_symlink_iops;
173 else if (S_ISDIR(lower_inode->i_mode))
174 inode->i_op = &ecryptfs_dir_iops;
175 if (S_ISDIR(lower_inode->i_mode))
176 inode->i_fop = &ecryptfs_dir_fops;
177 if (special_file(lower_inode->i_mode))
178 init_special_inode(inode, lower_inode->i_mode,
179 lower_inode->i_rdev);
180 fsstack_copy_attr_all(inode, lower_inode);
181 /* This size will be overwritten for real files w/ headers and
183 fsstack_copy_inode_size(inode, lower_inode);
191 * @lower_dentry: Existing dentry in the lower filesystem
192 * @dentry: ecryptfs' dentry
193 * @sb: ecryptfs's super_block
194 * @flags: flags to govern behavior of interpose procedure
196 * Interposes upper and lower dentries.
198 * Returns zero on success; non-zero otherwise
200 int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
201 struct super_block *sb, u32 flags)
203 struct inode *lower_inode = lower_dentry->d_inode;
204 struct inode *inode = ecryptfs_get_inode(lower_inode, sb);
206 return PTR_ERR(inode);
207 if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
208 d_add(dentry, inode);
210 d_instantiate(dentry, inode);
214 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
215 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
216 ecryptfs_opt_ecryptfs_key_bytes,
217 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
218 ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
219 ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
220 ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
221 ecryptfs_opt_check_dev_ruid,
224 static const match_table_t tokens = {
225 {ecryptfs_opt_sig, "sig=%s"},
226 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
227 {ecryptfs_opt_cipher, "cipher=%s"},
228 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
229 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
230 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
231 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
232 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
233 {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
234 {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
235 {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
236 {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
237 {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
238 {ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
239 {ecryptfs_opt_err, NULL}
242 static int ecryptfs_init_global_auth_toks(
243 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
245 struct ecryptfs_global_auth_tok *global_auth_tok;
248 list_for_each_entry(global_auth_tok,
249 &mount_crypt_stat->global_auth_tok_list,
250 mount_crypt_stat_list) {
251 rc = ecryptfs_keyring_auth_tok_for_sig(
252 &global_auth_tok->global_auth_tok_key,
253 &global_auth_tok->global_auth_tok,
254 global_auth_tok->sig);
256 printk(KERN_ERR "Could not find valid key in user "
257 "session keyring for sig specified in mount "
258 "option: [%s]\n", global_auth_tok->sig);
259 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
262 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
268 static void ecryptfs_init_mount_crypt_stat(
269 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
271 memset((void *)mount_crypt_stat, 0,
272 sizeof(struct ecryptfs_mount_crypt_stat));
273 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
274 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
275 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
279 * ecryptfs_parse_options
280 * @sb: The ecryptfs super block
281 * @options: The options pased to the kernel
282 * @check_ruid: set to 1 if device uid should be checked against the ruid
284 * Parse mount options:
285 * debug=N - ecryptfs_verbosity level for debug output
286 * sig=XXX - description(signature) of the key to use
288 * Returns the dentry object of the lower-level (lower/interposed)
289 * directory; We want to mount our stackable file system on top of
290 * that lower directory.
292 * The signature of the key to use must be the description of a key
293 * already in the keyring. Mounting will fail if the key can not be
296 * Returns zero on success; non-zero on error
298 static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
304 int cipher_name_set = 0;
305 int fn_cipher_name_set = 0;
306 int cipher_key_bytes;
307 int cipher_key_bytes_set = 0;
308 int fn_cipher_key_bytes;
309 int fn_cipher_key_bytes_set = 0;
310 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
311 &sbi->mount_crypt_stat;
312 substring_t args[MAX_OPT_ARGS];
315 char *cipher_name_dst;
316 char *cipher_name_src;
317 char *fn_cipher_name_dst;
318 char *fn_cipher_name_src;
321 char *cipher_key_bytes_src;
322 char *fn_cipher_key_bytes_src;
330 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
331 while ((p = strsep(&options, ",")) != NULL) {
334 token = match_token(p, tokens, args);
336 case ecryptfs_opt_sig:
337 case ecryptfs_opt_ecryptfs_sig:
338 sig_src = args[0].from;
339 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
342 printk(KERN_ERR "Error attempting to register "
343 "global sig; rc = [%d]\n", rc);
348 case ecryptfs_opt_cipher:
349 case ecryptfs_opt_ecryptfs_cipher:
350 cipher_name_src = args[0].from;
353 global_default_cipher_name;
354 strncpy(cipher_name_dst, cipher_name_src,
355 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
356 cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
359 case ecryptfs_opt_ecryptfs_key_bytes:
360 cipher_key_bytes_src = args[0].from;
362 (int)simple_strtol(cipher_key_bytes_src,
363 &cipher_key_bytes_src, 0);
364 mount_crypt_stat->global_default_cipher_key_size =
366 cipher_key_bytes_set = 1;
368 case ecryptfs_opt_passthrough:
369 mount_crypt_stat->flags |=
370 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
372 case ecryptfs_opt_xattr_metadata:
373 mount_crypt_stat->flags |=
374 ECRYPTFS_XATTR_METADATA_ENABLED;
376 case ecryptfs_opt_encrypted_view:
377 mount_crypt_stat->flags |=
378 ECRYPTFS_XATTR_METADATA_ENABLED;
379 mount_crypt_stat->flags |=
380 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
382 case ecryptfs_opt_fnek_sig:
383 fnek_src = args[0].from;
385 mount_crypt_stat->global_default_fnek_sig;
386 strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
387 mount_crypt_stat->global_default_fnek_sig[
388 ECRYPTFS_SIG_SIZE_HEX] = '\0';
389 rc = ecryptfs_add_global_auth_tok(
391 mount_crypt_stat->global_default_fnek_sig,
392 ECRYPTFS_AUTH_TOK_FNEK);
394 printk(KERN_ERR "Error attempting to register "
395 "global fnek sig [%s]; rc = [%d]\n",
396 mount_crypt_stat->global_default_fnek_sig,
400 mount_crypt_stat->flags |=
401 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
402 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
404 case ecryptfs_opt_fn_cipher:
405 fn_cipher_name_src = args[0].from;
407 mount_crypt_stat->global_default_fn_cipher_name;
408 strncpy(fn_cipher_name_dst, fn_cipher_name_src,
409 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
410 mount_crypt_stat->global_default_fn_cipher_name[
411 ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
412 fn_cipher_name_set = 1;
414 case ecryptfs_opt_fn_cipher_key_bytes:
415 fn_cipher_key_bytes_src = args[0].from;
416 fn_cipher_key_bytes =
417 (int)simple_strtol(fn_cipher_key_bytes_src,
418 &fn_cipher_key_bytes_src, 0);
419 mount_crypt_stat->global_default_fn_cipher_key_bytes =
421 fn_cipher_key_bytes_set = 1;
423 case ecryptfs_opt_unlink_sigs:
424 mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
426 case ecryptfs_opt_mount_auth_tok_only:
427 mount_crypt_stat->flags |=
428 ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
430 case ecryptfs_opt_check_dev_ruid:
433 case ecryptfs_opt_err:
436 "%s: eCryptfs: unrecognized option [%s]\n",
442 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
443 "auth tok signature as a mount "
444 "parameter; see the eCryptfs README\n");
447 if (!cipher_name_set) {
448 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
450 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
451 strcpy(mount_crypt_stat->global_default_cipher_name,
452 ECRYPTFS_DEFAULT_CIPHER);
454 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
455 && !fn_cipher_name_set)
456 strcpy(mount_crypt_stat->global_default_fn_cipher_name,
457 mount_crypt_stat->global_default_cipher_name);
458 if (!cipher_key_bytes_set)
459 mount_crypt_stat->global_default_cipher_key_size = 0;
460 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
461 && !fn_cipher_key_bytes_set)
462 mount_crypt_stat->global_default_fn_cipher_key_bytes =
463 mount_crypt_stat->global_default_cipher_key_size;
464 mutex_lock(&key_tfm_list_mutex);
465 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
467 rc = ecryptfs_add_new_key_tfm(
468 NULL, mount_crypt_stat->global_default_cipher_name,
469 mount_crypt_stat->global_default_cipher_key_size);
471 printk(KERN_ERR "Error attempting to initialize "
472 "cipher with name = [%s] and key size = [%td]; "
474 mount_crypt_stat->global_default_cipher_name,
475 mount_crypt_stat->global_default_cipher_key_size,
478 mutex_unlock(&key_tfm_list_mutex);
482 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
483 && !ecryptfs_tfm_exists(
484 mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
485 rc = ecryptfs_add_new_key_tfm(
486 NULL, mount_crypt_stat->global_default_fn_cipher_name,
487 mount_crypt_stat->global_default_fn_cipher_key_bytes);
489 printk(KERN_ERR "Error attempting to initialize "
490 "cipher with name = [%s] and key size = [%td]; "
492 mount_crypt_stat->global_default_fn_cipher_name,
493 mount_crypt_stat->global_default_fn_cipher_key_bytes,
496 mutex_unlock(&key_tfm_list_mutex);
500 mutex_unlock(&key_tfm_list_mutex);
501 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
503 printk(KERN_WARNING "One or more global auth toks could not "
504 "properly register; rc = [%d]\n", rc);
509 struct kmem_cache *ecryptfs_sb_info_cache;
510 static struct file_system_type ecryptfs_fs_type;
516 * @dev_name: The path to mount over
517 * @raw_data: The options passed into the kernel
519 static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
520 const char *dev_name, void *raw_data)
522 struct super_block *s;
523 struct ecryptfs_sb_info *sbi;
524 struct ecryptfs_dentry_info *root_info;
525 const char *err = "Getting sb failed";
531 sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
537 rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
539 err = "Error parsing options";
543 s = sget(fs_type, NULL, set_anon_super, NULL);
550 rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs", BDI_CAP_MAP_COPY);
554 ecryptfs_set_superblock_private(s, sbi);
555 s->s_bdi = &sbi->bdi;
557 /* ->kill_sb() will take care of sbi after that point */
559 s->s_op = &ecryptfs_sops;
560 s->s_d_op = &ecryptfs_dops;
562 err = "Reading sb failed";
563 rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
565 ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
568 if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
570 printk(KERN_ERR "Mount on filesystem of type "
571 "eCryptfs explicitly disallowed due to "
572 "known incompatibilities\n");
576 if (check_ruid && path.dentry->d_inode->i_uid != current_uid()) {
578 printk(KERN_ERR "Mount of device (uid: %d) not owned by "
579 "requested user (uid: %d)\n",
580 path.dentry->d_inode->i_uid, current_uid());
584 ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
585 s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
586 s->s_blocksize = path.dentry->d_sb->s_blocksize;
587 s->s_magic = ECRYPTFS_SUPER_MAGIC;
589 inode = ecryptfs_get_inode(path.dentry->d_inode, s);
594 s->s_root = d_alloc_root(inode);
602 root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
606 /* ->kill_sb() will take care of root_info */
607 ecryptfs_set_dentry_private(s->s_root, root_info);
608 ecryptfs_set_dentry_lower(s->s_root, path.dentry);
609 ecryptfs_set_dentry_lower_mnt(s->s_root, path.mnt);
611 s->s_flags |= MS_ACTIVE;
612 return dget(s->s_root);
617 deactivate_locked_super(s);
620 ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
621 kmem_cache_free(ecryptfs_sb_info_cache, sbi);
623 printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
628 * ecryptfs_kill_block_super
629 * @sb: The ecryptfs super block
631 * Used to bring the superblock down and free the private data.
633 static void ecryptfs_kill_block_super(struct super_block *sb)
635 struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
639 ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
640 bdi_destroy(&sb_info->bdi);
641 kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
644 static struct file_system_type ecryptfs_fs_type = {
645 .owner = THIS_MODULE,
647 .mount = ecryptfs_mount,
648 .kill_sb = ecryptfs_kill_block_super,
653 * inode_info_init_once
655 * Initializes the ecryptfs_inode_info_cache when it is created
658 inode_info_init_once(void *vptr)
660 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
662 inode_init_once(&ei->vfs_inode);
665 static struct ecryptfs_cache_info {
666 struct kmem_cache **cache;
669 void (*ctor)(void *obj);
670 } ecryptfs_cache_infos[] = {
672 .cache = &ecryptfs_auth_tok_list_item_cache,
673 .name = "ecryptfs_auth_tok_list_item",
674 .size = sizeof(struct ecryptfs_auth_tok_list_item),
677 .cache = &ecryptfs_file_info_cache,
678 .name = "ecryptfs_file_cache",
679 .size = sizeof(struct ecryptfs_file_info),
682 .cache = &ecryptfs_dentry_info_cache,
683 .name = "ecryptfs_dentry_info_cache",
684 .size = sizeof(struct ecryptfs_dentry_info),
687 .cache = &ecryptfs_inode_info_cache,
688 .name = "ecryptfs_inode_cache",
689 .size = sizeof(struct ecryptfs_inode_info),
690 .ctor = inode_info_init_once,
693 .cache = &ecryptfs_sb_info_cache,
694 .name = "ecryptfs_sb_cache",
695 .size = sizeof(struct ecryptfs_sb_info),
698 .cache = &ecryptfs_header_cache_1,
699 .name = "ecryptfs_headers_1",
700 .size = PAGE_CACHE_SIZE,
703 .cache = &ecryptfs_header_cache_2,
704 .name = "ecryptfs_headers_2",
705 .size = PAGE_CACHE_SIZE,
708 .cache = &ecryptfs_xattr_cache,
709 .name = "ecryptfs_xattr_cache",
710 .size = PAGE_CACHE_SIZE,
713 .cache = &ecryptfs_key_record_cache,
714 .name = "ecryptfs_key_record_cache",
715 .size = sizeof(struct ecryptfs_key_record),
718 .cache = &ecryptfs_key_sig_cache,
719 .name = "ecryptfs_key_sig_cache",
720 .size = sizeof(struct ecryptfs_key_sig),
723 .cache = &ecryptfs_global_auth_tok_cache,
724 .name = "ecryptfs_global_auth_tok_cache",
725 .size = sizeof(struct ecryptfs_global_auth_tok),
728 .cache = &ecryptfs_key_tfm_cache,
729 .name = "ecryptfs_key_tfm_cache",
730 .size = sizeof(struct ecryptfs_key_tfm),
733 .cache = &ecryptfs_open_req_cache,
734 .name = "ecryptfs_open_req_cache",
735 .size = sizeof(struct ecryptfs_open_req),
739 static void ecryptfs_free_kmem_caches(void)
743 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
744 struct ecryptfs_cache_info *info;
746 info = &ecryptfs_cache_infos[i];
748 kmem_cache_destroy(*(info->cache));
753 * ecryptfs_init_kmem_caches
755 * Returns zero on success; non-zero otherwise
757 static int ecryptfs_init_kmem_caches(void)
761 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
762 struct ecryptfs_cache_info *info;
764 info = &ecryptfs_cache_infos[i];
765 *(info->cache) = kmem_cache_create(info->name, info->size,
766 0, SLAB_HWCACHE_ALIGN, info->ctor);
767 if (!*(info->cache)) {
768 ecryptfs_free_kmem_caches();
769 ecryptfs_printk(KERN_WARNING, "%s: "
770 "kmem_cache_create failed\n",
778 static struct kobject *ecryptfs_kobj;
780 static ssize_t version_show(struct kobject *kobj,
781 struct kobj_attribute *attr, char *buff)
783 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
786 static struct kobj_attribute version_attr = __ATTR_RO(version);
788 static struct attribute *attributes[] = {
793 static struct attribute_group attr_group = {
797 static int do_sysfs_registration(void)
801 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
802 if (!ecryptfs_kobj) {
803 printk(KERN_ERR "Unable to create ecryptfs kset\n");
807 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
810 "Unable to create ecryptfs version attributes\n");
811 kobject_put(ecryptfs_kobj);
817 static void do_sysfs_unregistration(void)
819 sysfs_remove_group(ecryptfs_kobj, &attr_group);
820 kobject_put(ecryptfs_kobj);
823 static int __init ecryptfs_init(void)
827 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
829 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
830 "larger than the host's page size, and so "
831 "eCryptfs cannot run on this system. The "
832 "default eCryptfs extent size is [%u] bytes; "
833 "the page size is [%lu] bytes.\n",
834 ECRYPTFS_DEFAULT_EXTENT_SIZE,
835 (unsigned long)PAGE_CACHE_SIZE);
838 rc = ecryptfs_init_kmem_caches();
841 "Failed to allocate one or more kmem_cache objects\n");
844 rc = register_filesystem(&ecryptfs_fs_type);
846 printk(KERN_ERR "Failed to register filesystem\n");
847 goto out_free_kmem_caches;
849 rc = do_sysfs_registration();
851 printk(KERN_ERR "sysfs registration failed\n");
852 goto out_unregister_filesystem;
854 rc = ecryptfs_init_kthread();
856 printk(KERN_ERR "%s: kthread initialization failed; "
857 "rc = [%d]\n", __func__, rc);
858 goto out_do_sysfs_unregistration;
860 rc = ecryptfs_init_messaging();
862 printk(KERN_ERR "Failure occured while attempting to "
863 "initialize the communications channel to "
865 goto out_destroy_kthread;
867 rc = ecryptfs_init_crypto();
869 printk(KERN_ERR "Failure whilst attempting to init crypto; "
871 goto out_release_messaging;
873 if (ecryptfs_verbosity > 0)
874 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
875 "will be written to the syslog!\n", ecryptfs_verbosity);
878 out_release_messaging:
879 ecryptfs_release_messaging();
881 ecryptfs_destroy_kthread();
882 out_do_sysfs_unregistration:
883 do_sysfs_unregistration();
884 out_unregister_filesystem:
885 unregister_filesystem(&ecryptfs_fs_type);
886 out_free_kmem_caches:
887 ecryptfs_free_kmem_caches();
892 static void __exit ecryptfs_exit(void)
896 rc = ecryptfs_destroy_crypto();
898 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
900 ecryptfs_release_messaging();
901 ecryptfs_destroy_kthread();
902 do_sysfs_unregistration();
903 unregister_filesystem(&ecryptfs_fs_type);
904 ecryptfs_free_kmem_caches();
907 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
908 MODULE_DESCRIPTION("eCryptfs");
910 MODULE_LICENSE("GPL");
912 module_init(ecryptfs_init)
913 module_exit(ecryptfs_exit)