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 "ecryptfs_kernel.h"
42 * Module parameter that defines the ecryptfs_verbosity level.
44 int ecryptfs_verbosity
= 0;
46 module_param(ecryptfs_verbosity
, int, 0);
47 MODULE_PARM_DESC(ecryptfs_verbosity
,
48 "Initial verbosity level (0 or 1; defaults to "
49 "0, which is Quiet)");
52 * Module parameter that defines the number of message buffer elements
54 unsigned int ecryptfs_message_buf_len
= ECRYPTFS_DEFAULT_MSG_CTX_ELEMS
;
56 module_param(ecryptfs_message_buf_len
, uint
, 0);
57 MODULE_PARM_DESC(ecryptfs_message_buf_len
,
58 "Number of message buffer elements");
61 * Module parameter that defines the maximum guaranteed amount of time to wait
62 * for a response from ecryptfsd. The actual sleep time will be, more than
63 * likely, a small amount greater than this specified value, but only less if
64 * the message successfully arrives.
66 signed long ecryptfs_message_wait_timeout
= ECRYPTFS_MAX_MSG_CTX_TTL
/ HZ
;
68 module_param(ecryptfs_message_wait_timeout
, long, 0);
69 MODULE_PARM_DESC(ecryptfs_message_wait_timeout
,
70 "Maximum number of seconds that an operation will "
71 "sleep while waiting for a message response from "
75 * Module parameter that is an estimate of the maximum number of users
76 * that will be concurrently using eCryptfs. Set this to the right
77 * value to balance performance and memory use.
79 unsigned int ecryptfs_number_of_users
= ECRYPTFS_DEFAULT_NUM_USERS
;
81 module_param(ecryptfs_number_of_users
, uint
, 0);
82 MODULE_PARM_DESC(ecryptfs_number_of_users
, "An estimate of the number of "
83 "concurrent users of eCryptfs");
85 void __ecryptfs_printk(const char *fmt
, ...)
89 if (fmt
[1] == '7') { /* KERN_DEBUG */
90 if (ecryptfs_verbosity
>= 1)
98 * ecryptfs_init_persistent_file
99 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
100 * the lower dentry and the lower mount set
102 * eCryptfs only ever keeps a single open file for every lower
103 * inode. All I/O operations to the lower inode occur through that
104 * file. When the first eCryptfs dentry that interposes with the first
105 * lower dentry for that inode is created, this function creates the
106 * persistent file struct and associates it with the eCryptfs
107 * inode. When the eCryptfs inode is destroyed, the file is closed.
109 * The persistent file will be opened with read/write permissions, if
110 * possible. Otherwise, it is opened read-only.
112 * This function does nothing if a lower persistent file is already
113 * associated with the eCryptfs inode.
115 * Returns zero on success; non-zero otherwise
117 int ecryptfs_init_persistent_file(struct dentry
*ecryptfs_dentry
)
119 const struct cred
*cred
= current_cred();
120 struct ecryptfs_inode_info
*inode_info
=
121 ecryptfs_inode_to_private(ecryptfs_dentry
->d_inode
);
124 mutex_lock(&inode_info
->lower_file_mutex
);
125 if (!inode_info
->lower_file
) {
126 struct dentry
*lower_dentry
;
127 struct vfsmount
*lower_mnt
=
128 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry
);
130 lower_dentry
= ecryptfs_dentry_to_lower(ecryptfs_dentry
);
131 rc
= ecryptfs_privileged_open(&inode_info
->lower_file
,
132 lower_dentry
, lower_mnt
, cred
);
134 printk(KERN_ERR
"Error opening lower persistent file "
135 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
136 "rc = [%d]\n", lower_dentry
, lower_mnt
, rc
);
137 inode_info
->lower_file
= NULL
;
140 mutex_unlock(&inode_info
->lower_file_mutex
);
146 * @lower_dentry: Existing dentry in the lower filesystem
147 * @dentry: ecryptfs' dentry
148 * @sb: ecryptfs's super_block
149 * @flags: flags to govern behavior of interpose procedure
151 * Interposes upper and lower dentries.
153 * Returns zero on success; non-zero otherwise
155 int ecryptfs_interpose(struct dentry
*lower_dentry
, struct dentry
*dentry
,
156 struct super_block
*sb
, u32 flags
)
158 struct inode
*lower_inode
;
162 lower_inode
= lower_dentry
->d_inode
;
163 if (lower_inode
->i_sb
!= ecryptfs_superblock_to_lower(sb
)) {
167 if (!igrab(lower_inode
)) {
171 inode
= iget5_locked(sb
, (unsigned long)lower_inode
,
172 ecryptfs_inode_test
, ecryptfs_inode_set
,
179 if (inode
->i_state
& I_NEW
)
180 unlock_new_inode(inode
);
183 if (S_ISLNK(lower_inode
->i_mode
))
184 inode
->i_op
= &ecryptfs_symlink_iops
;
185 else if (S_ISDIR(lower_inode
->i_mode
))
186 inode
->i_op
= &ecryptfs_dir_iops
;
187 if (S_ISDIR(lower_inode
->i_mode
))
188 inode
->i_fop
= &ecryptfs_dir_fops
;
189 if (special_file(lower_inode
->i_mode
))
190 init_special_inode(inode
, lower_inode
->i_mode
,
191 lower_inode
->i_rdev
);
192 dentry
->d_op
= &ecryptfs_dops
;
193 fsstack_copy_attr_all(inode
, lower_inode
);
194 /* This size will be overwritten for real files w/ headers and
196 fsstack_copy_inode_size(inode
, lower_inode
);
197 if (flags
& ECRYPTFS_INTERPOSE_FLAG_D_ADD
)
198 d_add(dentry
, inode
);
200 d_instantiate(dentry
, inode
);
205 enum { ecryptfs_opt_sig
, ecryptfs_opt_ecryptfs_sig
,
206 ecryptfs_opt_cipher
, ecryptfs_opt_ecryptfs_cipher
,
207 ecryptfs_opt_ecryptfs_key_bytes
,
208 ecryptfs_opt_passthrough
, ecryptfs_opt_xattr_metadata
,
209 ecryptfs_opt_encrypted_view
, ecryptfs_opt_fnek_sig
,
210 ecryptfs_opt_fn_cipher
, ecryptfs_opt_fn_cipher_key_bytes
,
211 ecryptfs_opt_unlink_sigs
, ecryptfs_opt_err
};
213 static const match_table_t tokens
= {
214 {ecryptfs_opt_sig
, "sig=%s"},
215 {ecryptfs_opt_ecryptfs_sig
, "ecryptfs_sig=%s"},
216 {ecryptfs_opt_cipher
, "cipher=%s"},
217 {ecryptfs_opt_ecryptfs_cipher
, "ecryptfs_cipher=%s"},
218 {ecryptfs_opt_ecryptfs_key_bytes
, "ecryptfs_key_bytes=%u"},
219 {ecryptfs_opt_passthrough
, "ecryptfs_passthrough"},
220 {ecryptfs_opt_xattr_metadata
, "ecryptfs_xattr_metadata"},
221 {ecryptfs_opt_encrypted_view
, "ecryptfs_encrypted_view"},
222 {ecryptfs_opt_fnek_sig
, "ecryptfs_fnek_sig=%s"},
223 {ecryptfs_opt_fn_cipher
, "ecryptfs_fn_cipher=%s"},
224 {ecryptfs_opt_fn_cipher_key_bytes
, "ecryptfs_fn_key_bytes=%u"},
225 {ecryptfs_opt_unlink_sigs
, "ecryptfs_unlink_sigs"},
226 {ecryptfs_opt_err
, NULL
}
229 static int ecryptfs_init_global_auth_toks(
230 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
)
232 struct ecryptfs_global_auth_tok
*global_auth_tok
;
235 list_for_each_entry(global_auth_tok
,
236 &mount_crypt_stat
->global_auth_tok_list
,
237 mount_crypt_stat_list
) {
238 rc
= ecryptfs_keyring_auth_tok_for_sig(
239 &global_auth_tok
->global_auth_tok_key
,
240 &global_auth_tok
->global_auth_tok
,
241 global_auth_tok
->sig
);
243 printk(KERN_ERR
"Could not find valid key in user "
244 "session keyring for sig specified in mount "
245 "option: [%s]\n", global_auth_tok
->sig
);
246 global_auth_tok
->flags
|= ECRYPTFS_AUTH_TOK_INVALID
;
249 global_auth_tok
->flags
&= ~ECRYPTFS_AUTH_TOK_INVALID
;
255 static void ecryptfs_init_mount_crypt_stat(
256 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
)
258 memset((void *)mount_crypt_stat
, 0,
259 sizeof(struct ecryptfs_mount_crypt_stat
));
260 INIT_LIST_HEAD(&mount_crypt_stat
->global_auth_tok_list
);
261 mutex_init(&mount_crypt_stat
->global_auth_tok_list_mutex
);
262 mount_crypt_stat
->flags
|= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED
;
265 static int ecryptfs_parse_options(struct ecryptfs_sb_info
*sbi
, char *options
)
270 int cipher_name_set
= 0;
271 int fn_cipher_name_set
= 0;
272 int cipher_key_bytes
;
273 int cipher_key_bytes_set
= 0;
274 int fn_cipher_key_bytes
;
275 int fn_cipher_key_bytes_set
= 0;
276 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
=
277 &sbi
->mount_crypt_stat
;
278 substring_t args
[MAX_OPT_ARGS
];
281 char *cipher_name_dst
;
282 char *cipher_name_src
;
283 char *fn_cipher_name_dst
;
284 char *fn_cipher_name_src
;
287 char *cipher_key_bytes_src
;
288 char *fn_cipher_key_bytes_src
;
294 ecryptfs_init_mount_crypt_stat(mount_crypt_stat
);
295 while ((p
= strsep(&options
, ",")) != NULL
) {
298 token
= match_token(p
, tokens
, args
);
300 case ecryptfs_opt_sig
:
301 case ecryptfs_opt_ecryptfs_sig
:
302 sig_src
= args
[0].from
;
303 rc
= ecryptfs_add_global_auth_tok(mount_crypt_stat
,
306 printk(KERN_ERR
"Error attempting to register "
307 "global sig; rc = [%d]\n", rc
);
312 case ecryptfs_opt_cipher
:
313 case ecryptfs_opt_ecryptfs_cipher
:
314 cipher_name_src
= args
[0].from
;
317 global_default_cipher_name
;
318 strncpy(cipher_name_dst
, cipher_name_src
,
319 ECRYPTFS_MAX_CIPHER_NAME_SIZE
);
320 cipher_name_dst
[ECRYPTFS_MAX_CIPHER_NAME_SIZE
] = '\0';
323 case ecryptfs_opt_ecryptfs_key_bytes
:
324 cipher_key_bytes_src
= args
[0].from
;
326 (int)simple_strtol(cipher_key_bytes_src
,
327 &cipher_key_bytes_src
, 0);
328 mount_crypt_stat
->global_default_cipher_key_size
=
330 cipher_key_bytes_set
= 1;
332 case ecryptfs_opt_passthrough
:
333 mount_crypt_stat
->flags
|=
334 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED
;
336 case ecryptfs_opt_xattr_metadata
:
337 mount_crypt_stat
->flags
|=
338 ECRYPTFS_XATTR_METADATA_ENABLED
;
340 case ecryptfs_opt_encrypted_view
:
341 mount_crypt_stat
->flags
|=
342 ECRYPTFS_XATTR_METADATA_ENABLED
;
343 mount_crypt_stat
->flags
|=
344 ECRYPTFS_ENCRYPTED_VIEW_ENABLED
;
346 case ecryptfs_opt_fnek_sig
:
347 fnek_src
= args
[0].from
;
349 mount_crypt_stat
->global_default_fnek_sig
;
350 strncpy(fnek_dst
, fnek_src
, ECRYPTFS_SIG_SIZE_HEX
);
351 mount_crypt_stat
->global_default_fnek_sig
[
352 ECRYPTFS_SIG_SIZE_HEX
] = '\0';
353 rc
= ecryptfs_add_global_auth_tok(
355 mount_crypt_stat
->global_default_fnek_sig
,
356 ECRYPTFS_AUTH_TOK_FNEK
);
358 printk(KERN_ERR
"Error attempting to register "
359 "global fnek sig [%s]; rc = [%d]\n",
360 mount_crypt_stat
->global_default_fnek_sig
,
364 mount_crypt_stat
->flags
|=
365 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
366 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK
);
368 case ecryptfs_opt_fn_cipher
:
369 fn_cipher_name_src
= args
[0].from
;
371 mount_crypt_stat
->global_default_fn_cipher_name
;
372 strncpy(fn_cipher_name_dst
, fn_cipher_name_src
,
373 ECRYPTFS_MAX_CIPHER_NAME_SIZE
);
374 mount_crypt_stat
->global_default_fn_cipher_name
[
375 ECRYPTFS_MAX_CIPHER_NAME_SIZE
] = '\0';
376 fn_cipher_name_set
= 1;
378 case ecryptfs_opt_fn_cipher_key_bytes
:
379 fn_cipher_key_bytes_src
= args
[0].from
;
380 fn_cipher_key_bytes
=
381 (int)simple_strtol(fn_cipher_key_bytes_src
,
382 &fn_cipher_key_bytes_src
, 0);
383 mount_crypt_stat
->global_default_fn_cipher_key_bytes
=
385 fn_cipher_key_bytes_set
= 1;
387 case ecryptfs_opt_unlink_sigs
:
388 mount_crypt_stat
->flags
|= ECRYPTFS_UNLINK_SIGS
;
390 case ecryptfs_opt_err
:
393 "%s: eCryptfs: unrecognized option [%s]\n",
399 ecryptfs_printk(KERN_ERR
, "You must supply at least one valid "
400 "auth tok signature as a mount "
401 "parameter; see the eCryptfs README\n");
404 if (!cipher_name_set
) {
405 int cipher_name_len
= strlen(ECRYPTFS_DEFAULT_CIPHER
);
407 BUG_ON(cipher_name_len
>= ECRYPTFS_MAX_CIPHER_NAME_SIZE
);
408 strcpy(mount_crypt_stat
->global_default_cipher_name
,
409 ECRYPTFS_DEFAULT_CIPHER
);
411 if ((mount_crypt_stat
->flags
& ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
)
412 && !fn_cipher_name_set
)
413 strcpy(mount_crypt_stat
->global_default_fn_cipher_name
,
414 mount_crypt_stat
->global_default_cipher_name
);
415 if (!cipher_key_bytes_set
)
416 mount_crypt_stat
->global_default_cipher_key_size
= 0;
417 if ((mount_crypt_stat
->flags
& ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
)
418 && !fn_cipher_key_bytes_set
)
419 mount_crypt_stat
->global_default_fn_cipher_key_bytes
=
420 mount_crypt_stat
->global_default_cipher_key_size
;
421 mutex_lock(&key_tfm_list_mutex
);
422 if (!ecryptfs_tfm_exists(mount_crypt_stat
->global_default_cipher_name
,
424 rc
= ecryptfs_add_new_key_tfm(
425 NULL
, mount_crypt_stat
->global_default_cipher_name
,
426 mount_crypt_stat
->global_default_cipher_key_size
);
428 printk(KERN_ERR
"Error attempting to initialize "
429 "cipher with name = [%s] and key size = [%td]; "
431 mount_crypt_stat
->global_default_cipher_name
,
432 mount_crypt_stat
->global_default_cipher_key_size
,
435 mutex_unlock(&key_tfm_list_mutex
);
439 if ((mount_crypt_stat
->flags
& ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
)
440 && !ecryptfs_tfm_exists(
441 mount_crypt_stat
->global_default_fn_cipher_name
, NULL
)) {
442 rc
= ecryptfs_add_new_key_tfm(
443 NULL
, mount_crypt_stat
->global_default_fn_cipher_name
,
444 mount_crypt_stat
->global_default_fn_cipher_key_bytes
);
446 printk(KERN_ERR
"Error attempting to initialize "
447 "cipher with name = [%s] and key size = [%td]; "
449 mount_crypt_stat
->global_default_fn_cipher_name
,
450 mount_crypt_stat
->global_default_fn_cipher_key_bytes
,
453 mutex_unlock(&key_tfm_list_mutex
);
457 mutex_unlock(&key_tfm_list_mutex
);
458 rc
= ecryptfs_init_global_auth_toks(mount_crypt_stat
);
460 printk(KERN_WARNING
"One or more global auth toks could not "
461 "properly register; rc = [%d]\n", rc
);
466 struct kmem_cache
*ecryptfs_sb_info_cache
;
467 static struct file_system_type ecryptfs_fs_type
;
470 * ecryptfs_read_super
471 * @sb: The ecryptfs super block
472 * @dev_name: The path to mount over
474 * Read the super block of the lower filesystem, and use
475 * ecryptfs_interpose to create our initial inode and super block
478 static int ecryptfs_read_super(struct super_block
*sb
, const char *dev_name
)
483 rc
= kern_path(dev_name
, LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
, &path
);
485 ecryptfs_printk(KERN_WARNING
, "path_lookup() failed\n");
488 if (path
.dentry
->d_sb
->s_type
== &ecryptfs_fs_type
) {
490 printk(KERN_ERR
"Mount on filesystem of type "
491 "eCryptfs explicitly disallowed due to "
492 "known incompatibilities\n");
495 ecryptfs_set_superblock_lower(sb
, path
.dentry
->d_sb
);
496 sb
->s_maxbytes
= path
.dentry
->d_sb
->s_maxbytes
;
497 sb
->s_blocksize
= path
.dentry
->d_sb
->s_blocksize
;
498 ecryptfs_set_dentry_lower(sb
->s_root
, path
.dentry
);
499 ecryptfs_set_dentry_lower_mnt(sb
->s_root
, path
.mnt
);
500 rc
= ecryptfs_interpose(path
.dentry
, sb
->s_root
, sb
, 0);
515 * @dev_name: The path to mount over
516 * @raw_data: The options passed into the kernel
518 * The whole ecryptfs_get_sb process is broken into 3 functions:
519 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
520 * ecryptfs_read_super(): this accesses the lower filesystem and uses
521 * ecryptfs_interpose to perform most of the linking
522 * ecryptfs_interpose(): links the lower filesystem into ecryptfs (inode.c)
524 static int ecryptfs_get_sb(struct file_system_type
*fs_type
, int flags
,
525 const char *dev_name
, void *raw_data
,
526 struct vfsmount
*mnt
)
528 struct super_block
*s
;
529 struct ecryptfs_sb_info
*sbi
;
530 struct ecryptfs_dentry_info
*root_info
;
531 const char *err
= "Getting sb failed";
534 sbi
= kmem_cache_zalloc(ecryptfs_sb_info_cache
, GFP_KERNEL
);
540 rc
= ecryptfs_parse_options(sbi
, raw_data
);
542 err
= "Error parsing options";
546 s
= sget(fs_type
, NULL
, set_anon_super
, NULL
);
553 rc
= bdi_setup_and_register(&sbi
->bdi
, "ecryptfs", BDI_CAP_MAP_COPY
);
555 deactivate_locked_super(s
);
559 ecryptfs_set_superblock_private(s
, sbi
);
560 s
->s_bdi
= &sbi
->bdi
;
562 /* ->kill_sb() will take care of sbi after that point */
564 s
->s_op
= &ecryptfs_sops
;
567 s
->s_root
= d_alloc(NULL
, &(const struct qstr
) {
568 .hash
= 0,.name
= "/",.len
= 1});
570 deactivate_locked_super(s
);
573 s
->s_root
->d_op
= &ecryptfs_dops
;
575 s
->s_root
->d_parent
= s
->s_root
;
577 root_info
= kmem_cache_zalloc(ecryptfs_dentry_info_cache
, GFP_KERNEL
);
579 deactivate_locked_super(s
);
582 /* ->kill_sb() will take care of root_info */
583 ecryptfs_set_dentry_private(s
->s_root
, root_info
);
584 s
->s_flags
|= MS_ACTIVE
;
585 rc
= ecryptfs_read_super(s
, dev_name
);
587 deactivate_locked_super(s
);
588 err
= "Reading sb failed";
591 simple_set_mnt(mnt
, s
);
596 ecryptfs_destroy_mount_crypt_stat(&sbi
->mount_crypt_stat
);
597 kmem_cache_free(ecryptfs_sb_info_cache
, sbi
);
599 printk(KERN_ERR
"%s; rc = [%d]\n", err
, rc
);
604 * ecryptfs_kill_block_super
605 * @sb: The ecryptfs super block
607 * Used to bring the superblock down and free the private data.
609 static void ecryptfs_kill_block_super(struct super_block
*sb
)
611 struct ecryptfs_sb_info
*sb_info
= ecryptfs_superblock_to_private(sb
);
615 ecryptfs_destroy_mount_crypt_stat(&sb_info
->mount_crypt_stat
);
616 bdi_destroy(&sb_info
->bdi
);
617 kmem_cache_free(ecryptfs_sb_info_cache
, sb_info
);
620 static struct file_system_type ecryptfs_fs_type
= {
621 .owner
= THIS_MODULE
,
623 .get_sb
= ecryptfs_get_sb
,
624 .kill_sb
= ecryptfs_kill_block_super
,
629 * inode_info_init_once
631 * Initializes the ecryptfs_inode_info_cache when it is created
634 inode_info_init_once(void *vptr
)
636 struct ecryptfs_inode_info
*ei
= (struct ecryptfs_inode_info
*)vptr
;
638 inode_init_once(&ei
->vfs_inode
);
641 static struct ecryptfs_cache_info
{
642 struct kmem_cache
**cache
;
645 void (*ctor
)(void *obj
);
646 } ecryptfs_cache_infos
[] = {
648 .cache
= &ecryptfs_auth_tok_list_item_cache
,
649 .name
= "ecryptfs_auth_tok_list_item",
650 .size
= sizeof(struct ecryptfs_auth_tok_list_item
),
653 .cache
= &ecryptfs_file_info_cache
,
654 .name
= "ecryptfs_file_cache",
655 .size
= sizeof(struct ecryptfs_file_info
),
658 .cache
= &ecryptfs_dentry_info_cache
,
659 .name
= "ecryptfs_dentry_info_cache",
660 .size
= sizeof(struct ecryptfs_dentry_info
),
663 .cache
= &ecryptfs_inode_info_cache
,
664 .name
= "ecryptfs_inode_cache",
665 .size
= sizeof(struct ecryptfs_inode_info
),
666 .ctor
= inode_info_init_once
,
669 .cache
= &ecryptfs_sb_info_cache
,
670 .name
= "ecryptfs_sb_cache",
671 .size
= sizeof(struct ecryptfs_sb_info
),
674 .cache
= &ecryptfs_header_cache_1
,
675 .name
= "ecryptfs_headers_1",
676 .size
= PAGE_CACHE_SIZE
,
679 .cache
= &ecryptfs_header_cache_2
,
680 .name
= "ecryptfs_headers_2",
681 .size
= PAGE_CACHE_SIZE
,
684 .cache
= &ecryptfs_xattr_cache
,
685 .name
= "ecryptfs_xattr_cache",
686 .size
= PAGE_CACHE_SIZE
,
689 .cache
= &ecryptfs_key_record_cache
,
690 .name
= "ecryptfs_key_record_cache",
691 .size
= sizeof(struct ecryptfs_key_record
),
694 .cache
= &ecryptfs_key_sig_cache
,
695 .name
= "ecryptfs_key_sig_cache",
696 .size
= sizeof(struct ecryptfs_key_sig
),
699 .cache
= &ecryptfs_global_auth_tok_cache
,
700 .name
= "ecryptfs_global_auth_tok_cache",
701 .size
= sizeof(struct ecryptfs_global_auth_tok
),
704 .cache
= &ecryptfs_key_tfm_cache
,
705 .name
= "ecryptfs_key_tfm_cache",
706 .size
= sizeof(struct ecryptfs_key_tfm
),
709 .cache
= &ecryptfs_open_req_cache
,
710 .name
= "ecryptfs_open_req_cache",
711 .size
= sizeof(struct ecryptfs_open_req
),
715 static void ecryptfs_free_kmem_caches(void)
719 for (i
= 0; i
< ARRAY_SIZE(ecryptfs_cache_infos
); i
++) {
720 struct ecryptfs_cache_info
*info
;
722 info
= &ecryptfs_cache_infos
[i
];
724 kmem_cache_destroy(*(info
->cache
));
729 * ecryptfs_init_kmem_caches
731 * Returns zero on success; non-zero otherwise
733 static int ecryptfs_init_kmem_caches(void)
737 for (i
= 0; i
< ARRAY_SIZE(ecryptfs_cache_infos
); i
++) {
738 struct ecryptfs_cache_info
*info
;
740 info
= &ecryptfs_cache_infos
[i
];
741 *(info
->cache
) = kmem_cache_create(info
->name
, info
->size
,
742 0, SLAB_HWCACHE_ALIGN
, info
->ctor
);
743 if (!*(info
->cache
)) {
744 ecryptfs_free_kmem_caches();
745 ecryptfs_printk(KERN_WARNING
, "%s: "
746 "kmem_cache_create failed\n",
754 static struct kobject
*ecryptfs_kobj
;
756 static ssize_t
version_show(struct kobject
*kobj
,
757 struct kobj_attribute
*attr
, char *buff
)
759 return snprintf(buff
, PAGE_SIZE
, "%d\n", ECRYPTFS_VERSIONING_MASK
);
762 static struct kobj_attribute version_attr
= __ATTR_RO(version
);
764 static struct attribute
*attributes
[] = {
769 static struct attribute_group attr_group
= {
773 static int do_sysfs_registration(void)
777 ecryptfs_kobj
= kobject_create_and_add("ecryptfs", fs_kobj
);
778 if (!ecryptfs_kobj
) {
779 printk(KERN_ERR
"Unable to create ecryptfs kset\n");
783 rc
= sysfs_create_group(ecryptfs_kobj
, &attr_group
);
786 "Unable to create ecryptfs version attributes\n");
787 kobject_put(ecryptfs_kobj
);
793 static void do_sysfs_unregistration(void)
795 sysfs_remove_group(ecryptfs_kobj
, &attr_group
);
796 kobject_put(ecryptfs_kobj
);
799 static int __init
ecryptfs_init(void)
803 if (ECRYPTFS_DEFAULT_EXTENT_SIZE
> PAGE_CACHE_SIZE
) {
805 ecryptfs_printk(KERN_ERR
, "The eCryptfs extent size is "
806 "larger than the host's page size, and so "
807 "eCryptfs cannot run on this system. The "
808 "default eCryptfs extent size is [%d] bytes; "
809 "the page size is [%d] bytes.\n",
810 ECRYPTFS_DEFAULT_EXTENT_SIZE
, PAGE_CACHE_SIZE
);
813 rc
= ecryptfs_init_kmem_caches();
816 "Failed to allocate one or more kmem_cache objects\n");
819 rc
= register_filesystem(&ecryptfs_fs_type
);
821 printk(KERN_ERR
"Failed to register filesystem\n");
822 goto out_free_kmem_caches
;
824 rc
= do_sysfs_registration();
826 printk(KERN_ERR
"sysfs registration failed\n");
827 goto out_unregister_filesystem
;
829 rc
= ecryptfs_init_kthread();
831 printk(KERN_ERR
"%s: kthread initialization failed; "
832 "rc = [%d]\n", __func__
, rc
);
833 goto out_do_sysfs_unregistration
;
835 rc
= ecryptfs_init_messaging();
837 printk(KERN_ERR
"Failure occured while attempting to "
838 "initialize the communications channel to "
840 goto out_destroy_kthread
;
842 rc
= ecryptfs_init_crypto();
844 printk(KERN_ERR
"Failure whilst attempting to init crypto; "
846 goto out_release_messaging
;
848 if (ecryptfs_verbosity
> 0)
849 printk(KERN_CRIT
"eCryptfs verbosity set to %d. Secret values "
850 "will be written to the syslog!\n", ecryptfs_verbosity
);
853 out_release_messaging
:
854 ecryptfs_release_messaging();
856 ecryptfs_destroy_kthread();
857 out_do_sysfs_unregistration
:
858 do_sysfs_unregistration();
859 out_unregister_filesystem
:
860 unregister_filesystem(&ecryptfs_fs_type
);
861 out_free_kmem_caches
:
862 ecryptfs_free_kmem_caches();
867 static void __exit
ecryptfs_exit(void)
871 rc
= ecryptfs_destroy_crypto();
873 printk(KERN_ERR
"Failure whilst attempting to destroy crypto; "
875 ecryptfs_release_messaging();
876 ecryptfs_destroy_kthread();
877 do_sysfs_unregistration();
878 unregister_filesystem(&ecryptfs_fs_type
);
879 ecryptfs_free_kmem_caches();
882 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
883 MODULE_DESCRIPTION("eCryptfs");
885 MODULE_LICENSE("GPL");
887 module_init(ecryptfs_init
)
888 module_exit(ecryptfs_exit
)