1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * eCryptfs: Linux filesystem encryption layer
5 * Copyright (C) 1997-2003 Erez Zadok
6 * Copyright (C) 2001-2003 Stony Brook University
7 * Copyright (C) 2004-2007 International Business Machines Corp.
8 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9 * Michael C. Thompson <mcthomps@us.ibm.com>
10 * Tyler Hicks <code@tyhicks.com>
13 #include <linux/dcache.h>
14 #include <linux/file.h>
15 #include <linux/module.h>
16 #include <linux/namei.h>
17 #include <linux/skbuff.h>
18 #include <linux/mount.h>
19 #include <linux/pagemap.h>
20 #include <linux/key.h>
21 #include <linux/parser.h>
22 #include <linux/fs_stack.h>
23 #include <linux/slab.h>
24 #include <linux/magic.h>
25 #include "ecryptfs_kernel.h"
28 * Module parameter that defines the ecryptfs_verbosity level.
30 int ecryptfs_verbosity
= 0;
32 module_param(ecryptfs_verbosity
, int, 0);
33 MODULE_PARM_DESC(ecryptfs_verbosity
,
34 "Initial verbosity level (0 or 1; defaults to "
35 "0, which is Quiet)");
38 * Module parameter that defines the number of message buffer elements
40 unsigned int ecryptfs_message_buf_len
= ECRYPTFS_DEFAULT_MSG_CTX_ELEMS
;
42 module_param(ecryptfs_message_buf_len
, uint
, 0);
43 MODULE_PARM_DESC(ecryptfs_message_buf_len
,
44 "Number of message buffer elements");
47 * Module parameter that defines the maximum guaranteed amount of time to wait
48 * for a response from ecryptfsd. The actual sleep time will be, more than
49 * likely, a small amount greater than this specified value, but only less if
50 * the message successfully arrives.
52 signed long ecryptfs_message_wait_timeout
= ECRYPTFS_MAX_MSG_CTX_TTL
/ HZ
;
54 module_param(ecryptfs_message_wait_timeout
, long, 0);
55 MODULE_PARM_DESC(ecryptfs_message_wait_timeout
,
56 "Maximum number of seconds that an operation will "
57 "sleep while waiting for a message response from "
61 * Module parameter that is an estimate of the maximum number of users
62 * that will be concurrently using eCryptfs. Set this to the right
63 * value to balance performance and memory use.
65 unsigned int ecryptfs_number_of_users
= ECRYPTFS_DEFAULT_NUM_USERS
;
67 module_param(ecryptfs_number_of_users
, uint
, 0);
68 MODULE_PARM_DESC(ecryptfs_number_of_users
, "An estimate of the number of "
69 "concurrent users of eCryptfs");
71 void __ecryptfs_printk(const char *fmt
, ...)
75 if (fmt
[1] == '7') { /* KERN_DEBUG */
76 if (ecryptfs_verbosity
>= 1)
84 * ecryptfs_init_lower_file
85 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
86 * the lower dentry and the lower mount set
88 * eCryptfs only ever keeps a single open file for every lower
89 * inode. All I/O operations to the lower inode occur through that
90 * file. When the first eCryptfs dentry that interposes with the first
91 * lower dentry for that inode is created, this function creates the
92 * lower file struct and associates it with the eCryptfs
93 * inode. When all eCryptfs files associated with the inode are released, the
96 * The lower file will be opened with read/write permissions, if
97 * possible. Otherwise, it is opened read-only.
99 * This function does nothing if a lower file is already
100 * associated with the eCryptfs inode.
102 * Returns zero on success; non-zero otherwise
104 static int ecryptfs_init_lower_file(struct dentry
*dentry
,
105 struct file
**lower_file
)
107 const struct cred
*cred
= current_cred();
108 const struct path
*path
= ecryptfs_dentry_to_lower_path(dentry
);
111 rc
= ecryptfs_privileged_open(lower_file
, path
->dentry
, path
->mnt
,
114 printk(KERN_ERR
"Error opening lower file "
115 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
116 "rc = [%d]\n", path
->dentry
, path
->mnt
, rc
);
117 (*lower_file
) = NULL
;
122 int ecryptfs_get_lower_file(struct dentry
*dentry
, struct inode
*inode
)
124 struct ecryptfs_inode_info
*inode_info
;
127 inode_info
= ecryptfs_inode_to_private(inode
);
128 mutex_lock(&inode_info
->lower_file_mutex
);
129 count
= atomic_inc_return(&inode_info
->lower_file_count
);
130 if (WARN_ON_ONCE(count
< 1))
132 else if (count
== 1) {
133 rc
= ecryptfs_init_lower_file(dentry
,
134 &inode_info
->lower_file
);
136 atomic_set(&inode_info
->lower_file_count
, 0);
138 mutex_unlock(&inode_info
->lower_file_mutex
);
142 void ecryptfs_put_lower_file(struct inode
*inode
)
144 struct ecryptfs_inode_info
*inode_info
;
146 inode_info
= ecryptfs_inode_to_private(inode
);
147 if (atomic_dec_and_mutex_lock(&inode_info
->lower_file_count
,
148 &inode_info
->lower_file_mutex
)) {
149 filemap_write_and_wait(inode
->i_mapping
);
150 fput(inode_info
->lower_file
);
151 inode_info
->lower_file
= NULL
;
152 mutex_unlock(&inode_info
->lower_file_mutex
);
156 enum { ecryptfs_opt_sig
, ecryptfs_opt_ecryptfs_sig
,
157 ecryptfs_opt_cipher
, ecryptfs_opt_ecryptfs_cipher
,
158 ecryptfs_opt_ecryptfs_key_bytes
,
159 ecryptfs_opt_passthrough
, ecryptfs_opt_xattr_metadata
,
160 ecryptfs_opt_encrypted_view
, ecryptfs_opt_fnek_sig
,
161 ecryptfs_opt_fn_cipher
, ecryptfs_opt_fn_cipher_key_bytes
,
162 ecryptfs_opt_unlink_sigs
, ecryptfs_opt_mount_auth_tok_only
,
163 ecryptfs_opt_check_dev_ruid
,
166 static const match_table_t tokens
= {
167 {ecryptfs_opt_sig
, "sig=%s"},
168 {ecryptfs_opt_ecryptfs_sig
, "ecryptfs_sig=%s"},
169 {ecryptfs_opt_cipher
, "cipher=%s"},
170 {ecryptfs_opt_ecryptfs_cipher
, "ecryptfs_cipher=%s"},
171 {ecryptfs_opt_ecryptfs_key_bytes
, "ecryptfs_key_bytes=%u"},
172 {ecryptfs_opt_passthrough
, "ecryptfs_passthrough"},
173 {ecryptfs_opt_xattr_metadata
, "ecryptfs_xattr_metadata"},
174 {ecryptfs_opt_encrypted_view
, "ecryptfs_encrypted_view"},
175 {ecryptfs_opt_fnek_sig
, "ecryptfs_fnek_sig=%s"},
176 {ecryptfs_opt_fn_cipher
, "ecryptfs_fn_cipher=%s"},
177 {ecryptfs_opt_fn_cipher_key_bytes
, "ecryptfs_fn_key_bytes=%u"},
178 {ecryptfs_opt_unlink_sigs
, "ecryptfs_unlink_sigs"},
179 {ecryptfs_opt_mount_auth_tok_only
, "ecryptfs_mount_auth_tok_only"},
180 {ecryptfs_opt_check_dev_ruid
, "ecryptfs_check_dev_ruid"},
181 {ecryptfs_opt_err
, NULL
}
184 static int ecryptfs_init_global_auth_toks(
185 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
)
187 struct ecryptfs_global_auth_tok
*global_auth_tok
;
188 struct ecryptfs_auth_tok
*auth_tok
;
191 list_for_each_entry(global_auth_tok
,
192 &mount_crypt_stat
->global_auth_tok_list
,
193 mount_crypt_stat_list
) {
194 rc
= ecryptfs_keyring_auth_tok_for_sig(
195 &global_auth_tok
->global_auth_tok_key
, &auth_tok
,
196 global_auth_tok
->sig
);
198 printk(KERN_ERR
"Could not find valid key in user "
199 "session keyring for sig specified in mount "
200 "option: [%s]\n", global_auth_tok
->sig
);
201 global_auth_tok
->flags
|= ECRYPTFS_AUTH_TOK_INVALID
;
204 global_auth_tok
->flags
&= ~ECRYPTFS_AUTH_TOK_INVALID
;
205 up_write(&(global_auth_tok
->global_auth_tok_key
)->sem
);
212 static void ecryptfs_init_mount_crypt_stat(
213 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
)
215 memset((void *)mount_crypt_stat
, 0,
216 sizeof(struct ecryptfs_mount_crypt_stat
));
217 INIT_LIST_HEAD(&mount_crypt_stat
->global_auth_tok_list
);
218 mutex_init(&mount_crypt_stat
->global_auth_tok_list_mutex
);
219 mount_crypt_stat
->flags
|= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED
;
223 * ecryptfs_parse_options
224 * @sbi: The ecryptfs super block
225 * @options: The options passed to the kernel
226 * @check_ruid: set to 1 if device uid should be checked against the ruid
228 * Parse mount options:
229 * debug=N - ecryptfs_verbosity level for debug output
230 * sig=XXX - description(signature) of the key to use
232 * Returns the dentry object of the lower-level (lower/interposed)
233 * directory; We want to mount our stackable file system on top of
234 * that lower directory.
236 * The signature of the key to use must be the description of a key
237 * already in the keyring. Mounting will fail if the key can not be
240 * Returns zero on success; non-zero on error
242 static int ecryptfs_parse_options(struct ecryptfs_sb_info
*sbi
, char *options
,
248 int cipher_name_set
= 0;
249 int fn_cipher_name_set
= 0;
250 int cipher_key_bytes
;
251 int cipher_key_bytes_set
= 0;
252 int fn_cipher_key_bytes
;
253 int fn_cipher_key_bytes_set
= 0;
254 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
=
255 &sbi
->mount_crypt_stat
;
256 substring_t args
[MAX_OPT_ARGS
];
259 char *cipher_name_src
;
260 char *fn_cipher_name_src
;
262 char *cipher_key_bytes_src
;
263 char *fn_cipher_key_bytes_src
;
272 ecryptfs_init_mount_crypt_stat(mount_crypt_stat
);
273 while ((p
= strsep(&options
, ",")) != NULL
) {
276 token
= match_token(p
, tokens
, args
);
278 case ecryptfs_opt_sig
:
279 case ecryptfs_opt_ecryptfs_sig
:
280 sig_src
= args
[0].from
;
281 rc
= ecryptfs_add_global_auth_tok(mount_crypt_stat
,
284 printk(KERN_ERR
"Error attempting to register "
285 "global sig; rc = [%d]\n", rc
);
290 case ecryptfs_opt_cipher
:
291 case ecryptfs_opt_ecryptfs_cipher
:
292 cipher_name_src
= args
[0].from
;
293 strscpy(mount_crypt_stat
->global_default_cipher_name
,
297 case ecryptfs_opt_ecryptfs_key_bytes
:
298 cipher_key_bytes_src
= args
[0].from
;
300 (int)simple_strtol(cipher_key_bytes_src
,
301 &cipher_key_bytes_src
, 0);
302 mount_crypt_stat
->global_default_cipher_key_size
=
304 cipher_key_bytes_set
= 1;
306 case ecryptfs_opt_passthrough
:
307 mount_crypt_stat
->flags
|=
308 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED
;
310 case ecryptfs_opt_xattr_metadata
:
311 mount_crypt_stat
->flags
|=
312 ECRYPTFS_XATTR_METADATA_ENABLED
;
314 case ecryptfs_opt_encrypted_view
:
315 mount_crypt_stat
->flags
|=
316 ECRYPTFS_XATTR_METADATA_ENABLED
;
317 mount_crypt_stat
->flags
|=
318 ECRYPTFS_ENCRYPTED_VIEW_ENABLED
;
320 case ecryptfs_opt_fnek_sig
:
321 fnek_src
= args
[0].from
;
322 strscpy(mount_crypt_stat
->global_default_fnek_sig
,
324 rc
= ecryptfs_add_global_auth_tok(
326 mount_crypt_stat
->global_default_fnek_sig
,
327 ECRYPTFS_AUTH_TOK_FNEK
);
329 printk(KERN_ERR
"Error attempting to register "
330 "global fnek sig [%s]; rc = [%d]\n",
331 mount_crypt_stat
->global_default_fnek_sig
,
335 mount_crypt_stat
->flags
|=
336 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
337 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK
);
339 case ecryptfs_opt_fn_cipher
:
340 fn_cipher_name_src
= args
[0].from
;
341 strscpy(mount_crypt_stat
->global_default_fn_cipher_name
,
343 fn_cipher_name_set
= 1;
345 case ecryptfs_opt_fn_cipher_key_bytes
:
346 fn_cipher_key_bytes_src
= args
[0].from
;
347 fn_cipher_key_bytes
=
348 (int)simple_strtol(fn_cipher_key_bytes_src
,
349 &fn_cipher_key_bytes_src
, 0);
350 mount_crypt_stat
->global_default_fn_cipher_key_bytes
=
352 fn_cipher_key_bytes_set
= 1;
354 case ecryptfs_opt_unlink_sigs
:
355 mount_crypt_stat
->flags
|= ECRYPTFS_UNLINK_SIGS
;
357 case ecryptfs_opt_mount_auth_tok_only
:
358 mount_crypt_stat
->flags
|=
359 ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY
;
361 case ecryptfs_opt_check_dev_ruid
:
364 case ecryptfs_opt_err
:
367 "%s: eCryptfs: unrecognized option [%s]\n",
373 ecryptfs_printk(KERN_ERR
, "You must supply at least one valid "
374 "auth tok signature as a mount "
375 "parameter; see the eCryptfs README\n");
378 if (!cipher_name_set
) {
379 int cipher_name_len
= strlen(ECRYPTFS_DEFAULT_CIPHER
);
381 BUG_ON(cipher_name_len
> ECRYPTFS_MAX_CIPHER_NAME_SIZE
);
382 strcpy(mount_crypt_stat
->global_default_cipher_name
,
383 ECRYPTFS_DEFAULT_CIPHER
);
385 if ((mount_crypt_stat
->flags
& ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
)
386 && !fn_cipher_name_set
)
387 strcpy(mount_crypt_stat
->global_default_fn_cipher_name
,
388 mount_crypt_stat
->global_default_cipher_name
);
389 if (!cipher_key_bytes_set
)
390 mount_crypt_stat
->global_default_cipher_key_size
= 0;
391 if ((mount_crypt_stat
->flags
& ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
)
392 && !fn_cipher_key_bytes_set
)
393 mount_crypt_stat
->global_default_fn_cipher_key_bytes
=
394 mount_crypt_stat
->global_default_cipher_key_size
;
396 cipher_code
= ecryptfs_code_for_cipher_string(
397 mount_crypt_stat
->global_default_cipher_name
,
398 mount_crypt_stat
->global_default_cipher_key_size
);
400 ecryptfs_printk(KERN_ERR
,
401 "eCryptfs doesn't support cipher: %s\n",
402 mount_crypt_stat
->global_default_cipher_name
);
407 mutex_lock(&key_tfm_list_mutex
);
408 if (!ecryptfs_tfm_exists(mount_crypt_stat
->global_default_cipher_name
,
410 rc
= ecryptfs_add_new_key_tfm(
411 NULL
, mount_crypt_stat
->global_default_cipher_name
,
412 mount_crypt_stat
->global_default_cipher_key_size
);
414 printk(KERN_ERR
"Error attempting to initialize "
415 "cipher with name = [%s] and key size = [%td]; "
417 mount_crypt_stat
->global_default_cipher_name
,
418 mount_crypt_stat
->global_default_cipher_key_size
,
421 mutex_unlock(&key_tfm_list_mutex
);
425 if ((mount_crypt_stat
->flags
& ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
)
426 && !ecryptfs_tfm_exists(
427 mount_crypt_stat
->global_default_fn_cipher_name
, NULL
)) {
428 rc
= ecryptfs_add_new_key_tfm(
429 NULL
, mount_crypt_stat
->global_default_fn_cipher_name
,
430 mount_crypt_stat
->global_default_fn_cipher_key_bytes
);
432 printk(KERN_ERR
"Error attempting to initialize "
433 "cipher with name = [%s] and key size = [%td]; "
435 mount_crypt_stat
->global_default_fn_cipher_name
,
436 mount_crypt_stat
->global_default_fn_cipher_key_bytes
,
439 mutex_unlock(&key_tfm_list_mutex
);
443 mutex_unlock(&key_tfm_list_mutex
);
444 rc
= ecryptfs_init_global_auth_toks(mount_crypt_stat
);
446 printk(KERN_WARNING
"One or more global auth toks could not "
447 "properly register; rc = [%d]\n", rc
);
452 struct kmem_cache
*ecryptfs_sb_info_cache
;
453 static struct file_system_type ecryptfs_fs_type
;
457 * @fs_type: The filesystem type that the superblock should belong to
458 * @flags: The flags associated with the mount
459 * @dev_name: The path to mount over
460 * @raw_data: The options passed into the kernel
462 static struct dentry
*ecryptfs_mount(struct file_system_type
*fs_type
, int flags
,
463 const char *dev_name
, void *raw_data
)
465 struct super_block
*s
;
466 struct ecryptfs_sb_info
*sbi
;
467 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
;
468 struct ecryptfs_dentry_info
*root_info
;
469 const char *err
= "Getting sb failed";
475 sbi
= kmem_cache_zalloc(ecryptfs_sb_info_cache
, GFP_KERNEL
);
483 err
= "Device name cannot be null";
487 rc
= ecryptfs_parse_options(sbi
, raw_data
, &check_ruid
);
489 err
= "Error parsing options";
492 mount_crypt_stat
= &sbi
->mount_crypt_stat
;
494 s
= sget(fs_type
, NULL
, set_anon_super
, flags
, NULL
);
500 rc
= super_setup_bdi(s
);
504 ecryptfs_set_superblock_private(s
, sbi
);
506 /* ->kill_sb() will take care of sbi after that point */
508 s
->s_op
= &ecryptfs_sops
;
509 s
->s_xattr
= ecryptfs_xattr_handlers
;
510 s
->s_d_op
= &ecryptfs_dops
;
512 err
= "Reading sb failed";
513 rc
= kern_path(dev_name
, LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
, &path
);
515 ecryptfs_printk(KERN_WARNING
, "kern_path() failed\n");
518 if (path
.dentry
->d_sb
->s_type
== &ecryptfs_fs_type
) {
520 printk(KERN_ERR
"Mount on filesystem of type "
521 "eCryptfs explicitly disallowed due to "
522 "known incompatibilities\n");
526 if (is_idmapped_mnt(path
.mnt
)) {
528 printk(KERN_ERR
"Mounting on idmapped mounts currently disallowed\n");
532 if (check_ruid
&& !uid_eq(d_inode(path
.dentry
)->i_uid
, current_uid())) {
534 printk(KERN_ERR
"Mount of device (uid: %d) not owned by "
535 "requested user (uid: %d)\n",
536 i_uid_read(d_inode(path
.dentry
)),
537 from_kuid(&init_user_ns
, current_uid()));
541 ecryptfs_set_superblock_lower(s
, path
.dentry
->d_sb
);
544 * Set the POSIX ACL flag based on whether they're enabled in the lower
547 s
->s_flags
= flags
& ~SB_POSIXACL
;
548 s
->s_flags
|= path
.dentry
->d_sb
->s_flags
& SB_POSIXACL
;
551 * Force a read-only eCryptfs mount when:
552 * 1) The lower mount is ro
553 * 2) The ecryptfs_encrypted_view mount option is specified
555 if (sb_rdonly(path
.dentry
->d_sb
) || mount_crypt_stat
->flags
& ECRYPTFS_ENCRYPTED_VIEW_ENABLED
)
556 s
->s_flags
|= SB_RDONLY
;
558 s
->s_maxbytes
= path
.dentry
->d_sb
->s_maxbytes
;
559 s
->s_blocksize
= path
.dentry
->d_sb
->s_blocksize
;
560 s
->s_magic
= ECRYPTFS_SUPER_MAGIC
;
561 s
->s_stack_depth
= path
.dentry
->d_sb
->s_stack_depth
+ 1;
564 if (s
->s_stack_depth
> FILESYSTEM_MAX_STACK_DEPTH
) {
565 pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
569 inode
= ecryptfs_get_inode(d_inode(path
.dentry
), s
);
574 s
->s_root
= d_make_root(inode
);
581 root_info
= kmem_cache_zalloc(ecryptfs_dentry_info_cache
, GFP_KERNEL
);
585 /* ->kill_sb() will take care of root_info */
586 ecryptfs_set_dentry_private(s
->s_root
, root_info
);
587 root_info
->lower_path
= path
;
589 s
->s_flags
|= SB_ACTIVE
;
590 return dget(s
->s_root
);
595 deactivate_locked_super(s
);
598 ecryptfs_destroy_mount_crypt_stat(&sbi
->mount_crypt_stat
);
599 kmem_cache_free(ecryptfs_sb_info_cache
, sbi
);
601 printk(KERN_ERR
"%s; rc = [%d]\n", err
, rc
);
606 * ecryptfs_kill_block_super
607 * @sb: The ecryptfs super block
609 * Used to bring the superblock down and free the private data.
611 static void ecryptfs_kill_block_super(struct super_block
*sb
)
613 struct ecryptfs_sb_info
*sb_info
= ecryptfs_superblock_to_private(sb
);
617 ecryptfs_destroy_mount_crypt_stat(&sb_info
->mount_crypt_stat
);
618 kmem_cache_free(ecryptfs_sb_info_cache
, sb_info
);
621 static struct file_system_type ecryptfs_fs_type
= {
622 .owner
= THIS_MODULE
,
624 .mount
= ecryptfs_mount
,
625 .kill_sb
= ecryptfs_kill_block_super
,
628 MODULE_ALIAS_FS("ecryptfs");
631 * inode_info_init_once
633 * Initializes the ecryptfs_inode_info_cache when it is created
636 inode_info_init_once(void *vptr
)
638 struct ecryptfs_inode_info
*ei
= (struct ecryptfs_inode_info
*)vptr
;
640 inode_init_once(&ei
->vfs_inode
);
643 static struct ecryptfs_cache_info
{
644 struct kmem_cache
**cache
;
648 void (*ctor
)(void *obj
);
649 } ecryptfs_cache_infos
[] = {
651 .cache
= &ecryptfs_auth_tok_list_item_cache
,
652 .name
= "ecryptfs_auth_tok_list_item",
653 .size
= sizeof(struct ecryptfs_auth_tok_list_item
),
656 .cache
= &ecryptfs_file_info_cache
,
657 .name
= "ecryptfs_file_cache",
658 .size
= sizeof(struct ecryptfs_file_info
),
661 .cache
= &ecryptfs_dentry_info_cache
,
662 .name
= "ecryptfs_dentry_info_cache",
663 .size
= sizeof(struct ecryptfs_dentry_info
),
666 .cache
= &ecryptfs_inode_info_cache
,
667 .name
= "ecryptfs_inode_cache",
668 .size
= sizeof(struct ecryptfs_inode_info
),
669 .flags
= SLAB_ACCOUNT
,
670 .ctor
= inode_info_init_once
,
673 .cache
= &ecryptfs_sb_info_cache
,
674 .name
= "ecryptfs_sb_cache",
675 .size
= sizeof(struct ecryptfs_sb_info
),
678 .cache
= &ecryptfs_header_cache
,
679 .name
= "ecryptfs_headers",
683 .cache
= &ecryptfs_xattr_cache
,
684 .name
= "ecryptfs_xattr_cache",
688 .cache
= &ecryptfs_key_record_cache
,
689 .name
= "ecryptfs_key_record_cache",
690 .size
= sizeof(struct ecryptfs_key_record
),
693 .cache
= &ecryptfs_key_sig_cache
,
694 .name
= "ecryptfs_key_sig_cache",
695 .size
= sizeof(struct ecryptfs_key_sig
),
698 .cache
= &ecryptfs_global_auth_tok_cache
,
699 .name
= "ecryptfs_global_auth_tok_cache",
700 .size
= sizeof(struct ecryptfs_global_auth_tok
),
703 .cache
= &ecryptfs_key_tfm_cache
,
704 .name
= "ecryptfs_key_tfm_cache",
705 .size
= sizeof(struct ecryptfs_key_tfm
),
709 static void ecryptfs_free_kmem_caches(void)
714 * Make sure all delayed rcu free inodes are flushed before we
719 for (i
= 0; i
< ARRAY_SIZE(ecryptfs_cache_infos
); i
++) {
720 struct ecryptfs_cache_info
*info
;
722 info
= &ecryptfs_cache_infos
[i
];
723 kmem_cache_destroy(*(info
->cache
));
728 * ecryptfs_init_kmem_caches
730 * Returns zero on success; non-zero otherwise
732 static int ecryptfs_init_kmem_caches(void)
736 for (i
= 0; i
< ARRAY_SIZE(ecryptfs_cache_infos
); i
++) {
737 struct ecryptfs_cache_info
*info
;
739 info
= &ecryptfs_cache_infos
[i
];
740 *(info
->cache
) = kmem_cache_create(info
->name
, info
->size
, 0,
741 SLAB_HWCACHE_ALIGN
| info
->flags
, info
->ctor
);
742 if (!*(info
->cache
)) {
743 ecryptfs_free_kmem_caches();
744 ecryptfs_printk(KERN_WARNING
, "%s: "
745 "kmem_cache_create failed\n",
753 static struct kobject
*ecryptfs_kobj
;
755 static ssize_t
version_show(struct kobject
*kobj
,
756 struct kobj_attribute
*attr
, char *buff
)
758 return snprintf(buff
, PAGE_SIZE
, "%d\n", ECRYPTFS_VERSIONING_MASK
);
761 static struct kobj_attribute version_attr
= __ATTR_RO(version
);
763 static struct attribute
*attributes
[] = {
768 static const struct attribute_group attr_group
= {
772 static int do_sysfs_registration(void)
776 ecryptfs_kobj
= kobject_create_and_add("ecryptfs", fs_kobj
);
777 if (!ecryptfs_kobj
) {
778 printk(KERN_ERR
"Unable to create ecryptfs kset\n");
782 rc
= sysfs_create_group(ecryptfs_kobj
, &attr_group
);
785 "Unable to create ecryptfs version attributes\n");
786 kobject_put(ecryptfs_kobj
);
792 static void do_sysfs_unregistration(void)
794 sysfs_remove_group(ecryptfs_kobj
, &attr_group
);
795 kobject_put(ecryptfs_kobj
);
798 static int __init
ecryptfs_init(void)
802 if (ECRYPTFS_DEFAULT_EXTENT_SIZE
> PAGE_SIZE
) {
804 ecryptfs_printk(KERN_ERR
, "The eCryptfs extent size is "
805 "larger than the host's page size, and so "
806 "eCryptfs cannot run on this system. The "
807 "default eCryptfs extent size is [%u] bytes; "
808 "the page size is [%lu] bytes.\n",
809 ECRYPTFS_DEFAULT_EXTENT_SIZE
,
810 (unsigned long)PAGE_SIZE
);
813 rc
= ecryptfs_init_kmem_caches();
816 "Failed to allocate one or more kmem_cache objects\n");
819 rc
= do_sysfs_registration();
821 printk(KERN_ERR
"sysfs registration failed\n");
822 goto out_free_kmem_caches
;
824 rc
= ecryptfs_init_kthread();
826 printk(KERN_ERR
"%s: kthread initialization failed; "
827 "rc = [%d]\n", __func__
, rc
);
828 goto out_do_sysfs_unregistration
;
830 rc
= ecryptfs_init_messaging();
832 printk(KERN_ERR
"Failure occurred while attempting to "
833 "initialize the communications channel to "
835 goto out_destroy_kthread
;
837 rc
= ecryptfs_init_crypto();
839 printk(KERN_ERR
"Failure whilst attempting to init crypto; "
841 goto out_release_messaging
;
843 rc
= register_filesystem(&ecryptfs_fs_type
);
845 printk(KERN_ERR
"Failed to register filesystem\n");
846 goto out_destroy_crypto
;
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
);
854 ecryptfs_destroy_crypto();
855 out_release_messaging
:
856 ecryptfs_release_messaging();
858 ecryptfs_destroy_kthread();
859 out_do_sysfs_unregistration
:
860 do_sysfs_unregistration();
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
)