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 "ecryptfs_kernel.h"
41 * Module parameter that defines the ecryptfs_verbosity level.
43 int ecryptfs_verbosity
= 0;
45 module_param(ecryptfs_verbosity
, int, 0);
46 MODULE_PARM_DESC(ecryptfs_verbosity
,
47 "Initial verbosity level (0 or 1; defaults to "
48 "0, which is Quiet)");
51 * Module parameter that defines the number of message buffer elements
53 unsigned int ecryptfs_message_buf_len
= ECRYPTFS_DEFAULT_MSG_CTX_ELEMS
;
55 module_param(ecryptfs_message_buf_len
, uint
, 0);
56 MODULE_PARM_DESC(ecryptfs_message_buf_len
,
57 "Number of message buffer elements");
60 * Module parameter that defines the maximum guaranteed amount of time to wait
61 * for a response from ecryptfsd. The actual sleep time will be, more than
62 * likely, a small amount greater than this specified value, but only less if
63 * the message successfully arrives.
65 signed long ecryptfs_message_wait_timeout
= ECRYPTFS_MAX_MSG_CTX_TTL
/ HZ
;
67 module_param(ecryptfs_message_wait_timeout
, long, 0);
68 MODULE_PARM_DESC(ecryptfs_message_wait_timeout
,
69 "Maximum number of seconds that an operation will "
70 "sleep while waiting for a message response from "
74 * Module parameter that is an estimate of the maximum number of users
75 * that will be concurrently using eCryptfs. Set this to the right
76 * value to balance performance and memory use.
78 unsigned int ecryptfs_number_of_users
= ECRYPTFS_DEFAULT_NUM_USERS
;
80 module_param(ecryptfs_number_of_users
, uint
, 0);
81 MODULE_PARM_DESC(ecryptfs_number_of_users
, "An estimate of the number of "
82 "concurrent users of eCryptfs");
84 void __ecryptfs_printk(const char *fmt
, ...)
88 if (fmt
[1] == '7') { /* KERN_DEBUG */
89 if (ecryptfs_verbosity
>= 1)
97 * ecryptfs_init_persistent_file
98 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
99 * the lower dentry and the lower mount set
101 * eCryptfs only ever keeps a single open file for every lower
102 * inode. All I/O operations to the lower inode occur through that
103 * file. When the first eCryptfs dentry that interposes with the first
104 * lower dentry for that inode is created, this function creates the
105 * persistent file struct and associates it with the eCryptfs
106 * inode. When the eCryptfs inode is destroyed, the file is closed.
108 * The persistent file will be opened with read/write permissions, if
109 * possible. Otherwise, it is opened read-only.
111 * This function does nothing if a lower persistent file is already
112 * associated with the eCryptfs inode.
114 * Returns zero on success; non-zero otherwise
116 int ecryptfs_init_persistent_file(struct dentry
*ecryptfs_dentry
)
118 const struct cred
*cred
= current_cred();
119 struct ecryptfs_inode_info
*inode_info
=
120 ecryptfs_inode_to_private(ecryptfs_dentry
->d_inode
);
123 mutex_lock(&inode_info
->lower_file_mutex
);
124 if (!inode_info
->lower_file
) {
125 struct dentry
*lower_dentry
;
126 struct vfsmount
*lower_mnt
=
127 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry
);
129 lower_dentry
= ecryptfs_dentry_to_lower(ecryptfs_dentry
);
130 rc
= ecryptfs_privileged_open(&inode_info
->lower_file
,
131 lower_dentry
, lower_mnt
, cred
);
132 if (rc
|| IS_ERR(inode_info
->lower_file
)) {
133 printk(KERN_ERR
"Error opening lower persistent file "
134 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
135 "rc = [%d]\n", lower_dentry
, lower_mnt
, rc
);
136 rc
= PTR_ERR(inode_info
->lower_file
);
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
, NULL
);
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
;
266 * ecryptfs_parse_options
267 * @sb: The ecryptfs super block
268 * @options: The options pased to the kernel
270 * Parse mount options:
271 * debug=N - ecryptfs_verbosity level for debug output
272 * sig=XXX - description(signature) of the key to use
274 * Returns the dentry object of the lower-level (lower/interposed)
275 * directory; We want to mount our stackable file system on top of
276 * that lower directory.
278 * The signature of the key to use must be the description of a key
279 * already in the keyring. Mounting will fail if the key can not be
282 * Returns zero on success; non-zero on error
284 static int ecryptfs_parse_options(struct super_block
*sb
, char *options
)
289 int cipher_name_set
= 0;
290 int fn_cipher_name_set
= 0;
291 int cipher_key_bytes
;
292 int cipher_key_bytes_set
= 0;
293 int fn_cipher_key_bytes
;
294 int fn_cipher_key_bytes_set
= 0;
295 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
=
296 &ecryptfs_superblock_to_private(sb
)->mount_crypt_stat
;
297 substring_t args
[MAX_OPT_ARGS
];
300 char *cipher_name_dst
;
301 char *cipher_name_src
;
302 char *fn_cipher_name_dst
;
303 char *fn_cipher_name_src
;
306 char *cipher_key_bytes_src
;
307 char *fn_cipher_key_bytes_src
;
313 ecryptfs_init_mount_crypt_stat(mount_crypt_stat
);
314 while ((p
= strsep(&options
, ",")) != NULL
) {
317 token
= match_token(p
, tokens
, args
);
319 case ecryptfs_opt_sig
:
320 case ecryptfs_opt_ecryptfs_sig
:
321 sig_src
= args
[0].from
;
322 rc
= ecryptfs_add_global_auth_tok(mount_crypt_stat
,
325 printk(KERN_ERR
"Error attempting to register "
326 "global sig; rc = [%d]\n", rc
);
331 case ecryptfs_opt_cipher
:
332 case ecryptfs_opt_ecryptfs_cipher
:
333 cipher_name_src
= args
[0].from
;
336 global_default_cipher_name
;
337 strncpy(cipher_name_dst
, cipher_name_src
,
338 ECRYPTFS_MAX_CIPHER_NAME_SIZE
);
339 cipher_name_dst
[ECRYPTFS_MAX_CIPHER_NAME_SIZE
] = '\0';
342 case ecryptfs_opt_ecryptfs_key_bytes
:
343 cipher_key_bytes_src
= args
[0].from
;
345 (int)simple_strtol(cipher_key_bytes_src
,
346 &cipher_key_bytes_src
, 0);
347 mount_crypt_stat
->global_default_cipher_key_size
=
349 cipher_key_bytes_set
= 1;
351 case ecryptfs_opt_passthrough
:
352 mount_crypt_stat
->flags
|=
353 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED
;
355 case ecryptfs_opt_xattr_metadata
:
356 mount_crypt_stat
->flags
|=
357 ECRYPTFS_XATTR_METADATA_ENABLED
;
359 case ecryptfs_opt_encrypted_view
:
360 mount_crypt_stat
->flags
|=
361 ECRYPTFS_XATTR_METADATA_ENABLED
;
362 mount_crypt_stat
->flags
|=
363 ECRYPTFS_ENCRYPTED_VIEW_ENABLED
;
365 case ecryptfs_opt_fnek_sig
:
366 fnek_src
= args
[0].from
;
368 mount_crypt_stat
->global_default_fnek_sig
;
369 strncpy(fnek_dst
, fnek_src
, ECRYPTFS_SIG_SIZE_HEX
);
370 mount_crypt_stat
->global_default_fnek_sig
[
371 ECRYPTFS_SIG_SIZE_HEX
] = '\0';
372 rc
= ecryptfs_add_global_auth_tok(
374 mount_crypt_stat
->global_default_fnek_sig
,
375 ECRYPTFS_AUTH_TOK_FNEK
);
377 printk(KERN_ERR
"Error attempting to register "
378 "global fnek sig [%s]; rc = [%d]\n",
379 mount_crypt_stat
->global_default_fnek_sig
,
383 mount_crypt_stat
->flags
|=
384 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
385 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK
);
387 case ecryptfs_opt_fn_cipher
:
388 fn_cipher_name_src
= args
[0].from
;
390 mount_crypt_stat
->global_default_fn_cipher_name
;
391 strncpy(fn_cipher_name_dst
, fn_cipher_name_src
,
392 ECRYPTFS_MAX_CIPHER_NAME_SIZE
);
393 mount_crypt_stat
->global_default_fn_cipher_name
[
394 ECRYPTFS_MAX_CIPHER_NAME_SIZE
] = '\0';
395 fn_cipher_name_set
= 1;
397 case ecryptfs_opt_fn_cipher_key_bytes
:
398 fn_cipher_key_bytes_src
= args
[0].from
;
399 fn_cipher_key_bytes
=
400 (int)simple_strtol(fn_cipher_key_bytes_src
,
401 &fn_cipher_key_bytes_src
, 0);
402 mount_crypt_stat
->global_default_fn_cipher_key_bytes
=
404 fn_cipher_key_bytes_set
= 1;
406 case ecryptfs_opt_unlink_sigs
:
407 mount_crypt_stat
->flags
|= ECRYPTFS_UNLINK_SIGS
;
409 case ecryptfs_opt_err
:
412 "%s: eCryptfs: unrecognized option [%s]\n",
418 ecryptfs_printk(KERN_ERR
, "You must supply at least one valid "
419 "auth tok signature as a mount "
420 "parameter; see the eCryptfs README\n");
423 if (!cipher_name_set
) {
424 int cipher_name_len
= strlen(ECRYPTFS_DEFAULT_CIPHER
);
426 BUG_ON(cipher_name_len
>= ECRYPTFS_MAX_CIPHER_NAME_SIZE
);
427 strcpy(mount_crypt_stat
->global_default_cipher_name
,
428 ECRYPTFS_DEFAULT_CIPHER
);
430 if ((mount_crypt_stat
->flags
& ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
)
431 && !fn_cipher_name_set
)
432 strcpy(mount_crypt_stat
->global_default_fn_cipher_name
,
433 mount_crypt_stat
->global_default_cipher_name
);
434 if (!cipher_key_bytes_set
)
435 mount_crypt_stat
->global_default_cipher_key_size
= 0;
436 if ((mount_crypt_stat
->flags
& ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
)
437 && !fn_cipher_key_bytes_set
)
438 mount_crypt_stat
->global_default_fn_cipher_key_bytes
=
439 mount_crypt_stat
->global_default_cipher_key_size
;
440 mutex_lock(&key_tfm_list_mutex
);
441 if (!ecryptfs_tfm_exists(mount_crypt_stat
->global_default_cipher_name
,
443 rc
= ecryptfs_add_new_key_tfm(
444 NULL
, mount_crypt_stat
->global_default_cipher_name
,
445 mount_crypt_stat
->global_default_cipher_key_size
);
447 printk(KERN_ERR
"Error attempting to initialize "
448 "cipher with name = [%s] and key size = [%td]; "
450 mount_crypt_stat
->global_default_cipher_name
,
451 mount_crypt_stat
->global_default_cipher_key_size
,
454 mutex_unlock(&key_tfm_list_mutex
);
458 if ((mount_crypt_stat
->flags
& ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
)
459 && !ecryptfs_tfm_exists(
460 mount_crypt_stat
->global_default_fn_cipher_name
, NULL
)) {
461 rc
= ecryptfs_add_new_key_tfm(
462 NULL
, mount_crypt_stat
->global_default_fn_cipher_name
,
463 mount_crypt_stat
->global_default_fn_cipher_key_bytes
);
465 printk(KERN_ERR
"Error attempting to initialize "
466 "cipher with name = [%s] and key size = [%td]; "
468 mount_crypt_stat
->global_default_fn_cipher_name
,
469 mount_crypt_stat
->global_default_fn_cipher_key_bytes
,
472 mutex_unlock(&key_tfm_list_mutex
);
476 mutex_unlock(&key_tfm_list_mutex
);
477 rc
= ecryptfs_init_global_auth_toks(mount_crypt_stat
);
479 printk(KERN_WARNING
"One or more global auth toks could not "
480 "properly register; rc = [%d]\n", rc
);
485 struct kmem_cache
*ecryptfs_sb_info_cache
;
488 * ecryptfs_fill_super
489 * @sb: The ecryptfs super block
490 * @raw_data: The options passed to mount
491 * @silent: Not used but required by function prototype
493 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
495 * Returns zero on success; non-zero otherwise
498 ecryptfs_fill_super(struct super_block
*sb
, void *raw_data
, int silent
)
502 /* Released in ecryptfs_put_super() */
503 ecryptfs_set_superblock_private(sb
,
504 kmem_cache_zalloc(ecryptfs_sb_info_cache
,
506 if (!ecryptfs_superblock_to_private(sb
)) {
507 ecryptfs_printk(KERN_WARNING
, "Out of memory\n");
511 sb
->s_op
= &ecryptfs_sops
;
512 /* Released through deactivate_super(sb) from get_sb_nodev */
513 sb
->s_root
= d_alloc(NULL
, &(const struct qstr
) {
514 .hash
= 0,.name
= "/",.len
= 1});
516 ecryptfs_printk(KERN_ERR
, "d_alloc failed\n");
520 sb
->s_root
->d_op
= &ecryptfs_dops
;
521 sb
->s_root
->d_sb
= sb
;
522 sb
->s_root
->d_parent
= sb
->s_root
;
523 /* Released in d_release when dput(sb->s_root) is called */
524 /* through deactivate_super(sb) from get_sb_nodev() */
525 ecryptfs_set_dentry_private(sb
->s_root
,
526 kmem_cache_zalloc(ecryptfs_dentry_info_cache
,
528 if (!ecryptfs_dentry_to_private(sb
->s_root
)) {
529 ecryptfs_printk(KERN_ERR
,
530 "dentry_info_cache alloc failed\n");
536 /* Should be able to rely on deactivate_super called from
542 * ecryptfs_read_super
543 * @sb: The ecryptfs super block
544 * @dev_name: The path to mount over
546 * Read the super block of the lower filesystem, and use
547 * ecryptfs_interpose to create our initial inode and super block
550 static int ecryptfs_read_super(struct super_block
*sb
, const char *dev_name
)
555 rc
= kern_path(dev_name
, LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
, &path
);
557 ecryptfs_printk(KERN_WARNING
, "path_lookup() failed\n");
560 ecryptfs_set_superblock_lower(sb
, path
.dentry
->d_sb
);
561 sb
->s_maxbytes
= path
.dentry
->d_sb
->s_maxbytes
;
562 sb
->s_blocksize
= path
.dentry
->d_sb
->s_blocksize
;
563 ecryptfs_set_dentry_lower(sb
->s_root
, path
.dentry
);
564 ecryptfs_set_dentry_lower_mnt(sb
->s_root
, path
.mnt
);
565 rc
= ecryptfs_interpose(path
.dentry
, sb
->s_root
, sb
, 0);
580 * @dev_name: The path to mount over
581 * @raw_data: The options passed into the kernel
583 * The whole ecryptfs_get_sb process is broken into 4 functions:
584 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
585 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
586 * with as much information as it can before needing
587 * the lower filesystem.
588 * ecryptfs_read_super(): this accesses the lower filesystem and uses
589 * ecryptfs_interpolate to perform most of the linking
590 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
592 static int ecryptfs_get_sb(struct file_system_type
*fs_type
, int flags
,
593 const char *dev_name
, void *raw_data
,
594 struct vfsmount
*mnt
)
597 struct super_block
*sb
;
599 rc
= get_sb_nodev(fs_type
, flags
, raw_data
, ecryptfs_fill_super
, mnt
);
601 printk(KERN_ERR
"Getting sb failed; rc = [%d]\n", rc
);
605 rc
= ecryptfs_parse_options(sb
, raw_data
);
607 printk(KERN_ERR
"Error parsing options; rc = [%d]\n", rc
);
610 rc
= ecryptfs_read_super(sb
, dev_name
);
612 printk(KERN_ERR
"Reading sb failed; rc = [%d]\n", rc
);
617 dput(sb
->s_root
); /* aka mnt->mnt_root, as set by get_sb_nodev() */
618 deactivate_locked_super(sb
);
624 * ecryptfs_kill_block_super
625 * @sb: The ecryptfs super block
627 * Used to bring the superblock down and free the private data.
628 * Private data is free'd in ecryptfs_put_super()
630 static void ecryptfs_kill_block_super(struct super_block
*sb
)
632 generic_shutdown_super(sb
);
635 static struct file_system_type ecryptfs_fs_type
= {
636 .owner
= THIS_MODULE
,
638 .get_sb
= ecryptfs_get_sb
,
639 .kill_sb
= ecryptfs_kill_block_super
,
644 * inode_info_init_once
646 * Initializes the ecryptfs_inode_info_cache when it is created
649 inode_info_init_once(void *vptr
)
651 struct ecryptfs_inode_info
*ei
= (struct ecryptfs_inode_info
*)vptr
;
653 inode_init_once(&ei
->vfs_inode
);
656 static struct ecryptfs_cache_info
{
657 struct kmem_cache
**cache
;
660 void (*ctor
)(void *obj
);
661 } ecryptfs_cache_infos
[] = {
663 .cache
= &ecryptfs_auth_tok_list_item_cache
,
664 .name
= "ecryptfs_auth_tok_list_item",
665 .size
= sizeof(struct ecryptfs_auth_tok_list_item
),
668 .cache
= &ecryptfs_file_info_cache
,
669 .name
= "ecryptfs_file_cache",
670 .size
= sizeof(struct ecryptfs_file_info
),
673 .cache
= &ecryptfs_dentry_info_cache
,
674 .name
= "ecryptfs_dentry_info_cache",
675 .size
= sizeof(struct ecryptfs_dentry_info
),
678 .cache
= &ecryptfs_inode_info_cache
,
679 .name
= "ecryptfs_inode_cache",
680 .size
= sizeof(struct ecryptfs_inode_info
),
681 .ctor
= inode_info_init_once
,
684 .cache
= &ecryptfs_sb_info_cache
,
685 .name
= "ecryptfs_sb_cache",
686 .size
= sizeof(struct ecryptfs_sb_info
),
689 .cache
= &ecryptfs_header_cache_1
,
690 .name
= "ecryptfs_headers_1",
691 .size
= PAGE_CACHE_SIZE
,
694 .cache
= &ecryptfs_header_cache_2
,
695 .name
= "ecryptfs_headers_2",
696 .size
= PAGE_CACHE_SIZE
,
699 .cache
= &ecryptfs_xattr_cache
,
700 .name
= "ecryptfs_xattr_cache",
701 .size
= PAGE_CACHE_SIZE
,
704 .cache
= &ecryptfs_key_record_cache
,
705 .name
= "ecryptfs_key_record_cache",
706 .size
= sizeof(struct ecryptfs_key_record
),
709 .cache
= &ecryptfs_key_sig_cache
,
710 .name
= "ecryptfs_key_sig_cache",
711 .size
= sizeof(struct ecryptfs_key_sig
),
714 .cache
= &ecryptfs_global_auth_tok_cache
,
715 .name
= "ecryptfs_global_auth_tok_cache",
716 .size
= sizeof(struct ecryptfs_global_auth_tok
),
719 .cache
= &ecryptfs_key_tfm_cache
,
720 .name
= "ecryptfs_key_tfm_cache",
721 .size
= sizeof(struct ecryptfs_key_tfm
),
724 .cache
= &ecryptfs_open_req_cache
,
725 .name
= "ecryptfs_open_req_cache",
726 .size
= sizeof(struct ecryptfs_open_req
),
730 static void ecryptfs_free_kmem_caches(void)
734 for (i
= 0; i
< ARRAY_SIZE(ecryptfs_cache_infos
); i
++) {
735 struct ecryptfs_cache_info
*info
;
737 info
= &ecryptfs_cache_infos
[i
];
739 kmem_cache_destroy(*(info
->cache
));
744 * ecryptfs_init_kmem_caches
746 * Returns zero on success; non-zero otherwise
748 static int ecryptfs_init_kmem_caches(void)
752 for (i
= 0; i
< ARRAY_SIZE(ecryptfs_cache_infos
); i
++) {
753 struct ecryptfs_cache_info
*info
;
755 info
= &ecryptfs_cache_infos
[i
];
756 *(info
->cache
) = kmem_cache_create(info
->name
, info
->size
,
757 0, SLAB_HWCACHE_ALIGN
, info
->ctor
);
758 if (!*(info
->cache
)) {
759 ecryptfs_free_kmem_caches();
760 ecryptfs_printk(KERN_WARNING
, "%s: "
761 "kmem_cache_create failed\n",
769 static struct kobject
*ecryptfs_kobj
;
771 static ssize_t
version_show(struct kobject
*kobj
,
772 struct kobj_attribute
*attr
, char *buff
)
774 return snprintf(buff
, PAGE_SIZE
, "%d\n", ECRYPTFS_VERSIONING_MASK
);
777 static struct kobj_attribute version_attr
= __ATTR_RO(version
);
779 static struct attribute
*attributes
[] = {
784 static struct attribute_group attr_group
= {
788 static int do_sysfs_registration(void)
792 ecryptfs_kobj
= kobject_create_and_add("ecryptfs", fs_kobj
);
793 if (!ecryptfs_kobj
) {
794 printk(KERN_ERR
"Unable to create ecryptfs kset\n");
798 rc
= sysfs_create_group(ecryptfs_kobj
, &attr_group
);
801 "Unable to create ecryptfs version attributes\n");
802 kobject_put(ecryptfs_kobj
);
808 static void do_sysfs_unregistration(void)
810 sysfs_remove_group(ecryptfs_kobj
, &attr_group
);
811 kobject_put(ecryptfs_kobj
);
814 static int __init
ecryptfs_init(void)
818 if (ECRYPTFS_DEFAULT_EXTENT_SIZE
> PAGE_CACHE_SIZE
) {
820 ecryptfs_printk(KERN_ERR
, "The eCryptfs extent size is "
821 "larger than the host's page size, and so "
822 "eCryptfs cannot run on this system. The "
823 "default eCryptfs extent size is [%d] bytes; "
824 "the page size is [%d] bytes.\n",
825 ECRYPTFS_DEFAULT_EXTENT_SIZE
, PAGE_CACHE_SIZE
);
828 rc
= ecryptfs_init_kmem_caches();
831 "Failed to allocate one or more kmem_cache objects\n");
834 rc
= register_filesystem(&ecryptfs_fs_type
);
836 printk(KERN_ERR
"Failed to register filesystem\n");
837 goto out_free_kmem_caches
;
839 rc
= do_sysfs_registration();
841 printk(KERN_ERR
"sysfs registration failed\n");
842 goto out_unregister_filesystem
;
844 rc
= ecryptfs_init_kthread();
846 printk(KERN_ERR
"%s: kthread initialization failed; "
847 "rc = [%d]\n", __func__
, rc
);
848 goto out_do_sysfs_unregistration
;
850 rc
= ecryptfs_init_messaging();
852 printk(KERN_ERR
"Failure occured while attempting to "
853 "initialize the communications channel to "
855 goto out_destroy_kthread
;
857 rc
= ecryptfs_init_crypto();
859 printk(KERN_ERR
"Failure whilst attempting to init crypto; "
861 goto out_release_messaging
;
863 if (ecryptfs_verbosity
> 0)
864 printk(KERN_CRIT
"eCryptfs verbosity set to %d. Secret values "
865 "will be written to the syslog!\n", ecryptfs_verbosity
);
868 out_release_messaging
:
869 ecryptfs_release_messaging();
871 ecryptfs_destroy_kthread();
872 out_do_sysfs_unregistration
:
873 do_sysfs_unregistration();
874 out_unregister_filesystem
:
875 unregister_filesystem(&ecryptfs_fs_type
);
876 out_free_kmem_caches
:
877 ecryptfs_free_kmem_caches();
882 static void __exit
ecryptfs_exit(void)
886 rc
= ecryptfs_destroy_crypto();
888 printk(KERN_ERR
"Failure whilst attempting to destroy crypto; "
890 ecryptfs_release_messaging();
891 ecryptfs_destroy_kthread();
892 do_sysfs_unregistration();
893 unregister_filesystem(&ecryptfs_fs_type
);
894 ecryptfs_free_kmem_caches();
897 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
898 MODULE_DESCRIPTION("eCryptfs");
900 MODULE_LICENSE("GPL");
902 module_init(ecryptfs_init
)
903 module_exit(ecryptfs_exit
)