ecryptfs: propagate key errors up at mount time
[linux-2.6/openmoko-kernel.git] / fs / ecryptfs / main.c
blob8876fe7c76e28e2be30f87d5f478ff0de885785e
1 /**
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
24 * 02111-1307, USA.
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/netlink.h>
34 #include <linux/mount.h>
35 #include <linux/pagemap.h>
36 #include <linux/key.h>
37 #include <linux/parser.h>
38 #include <linux/fs_stack.h>
39 #include "ecryptfs_kernel.h"
41 /**
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)");
51 /**
52 * Module parameter that defines the number of netlink message buffer
53 * 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");
61 /**
62 * Module parameter that defines the maximum guaranteed amount of time to wait
63 * for a response through netlink. The actual sleep time will be, more than
64 * likely, a small amount greater than this specified value, but only less if
65 * the netlink 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 "
73 "userspace");
75 /**
76 * Module parameter that is an estimate of the maximum number of users
77 * that will be concurrently using eCryptfs. Set this to the right
78 * value to balance performance and memory use.
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 unsigned int ecryptfs_transport = ECRYPTFS_DEFAULT_TRANSPORT;
88 void __ecryptfs_printk(const char *fmt, ...)
90 va_list args;
91 va_start(args, fmt);
92 if (fmt[1] == '7') { /* KERN_DEBUG */
93 if (ecryptfs_verbosity >= 1)
94 vprintk(fmt, args);
95 } else
96 vprintk(fmt, args);
97 va_end(args);
101 * ecryptfs_init_persistent_file
102 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
103 * the lower dentry and the lower mount set
105 * eCryptfs only ever keeps a single open file for every lower
106 * inode. All I/O operations to the lower inode occur through that
107 * file. When the first eCryptfs dentry that interposes with the first
108 * lower dentry for that inode is created, this function creates the
109 * persistent file struct and associates it with the eCryptfs
110 * inode. When the eCryptfs inode is destroyed, the file is closed.
112 * The persistent file will be opened with read/write permissions, if
113 * possible. Otherwise, it is opened read-only.
115 * This function does nothing if a lower persistent file is already
116 * associated with the eCryptfs inode.
118 * Returns zero on success; non-zero otherwise
120 static int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
122 struct ecryptfs_inode_info *inode_info =
123 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
124 int rc = 0;
126 mutex_lock(&inode_info->lower_file_mutex);
127 if (!inode_info->lower_file) {
128 struct dentry *lower_dentry;
129 struct vfsmount *lower_mnt =
130 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
132 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
133 rc = ecryptfs_privileged_open(&inode_info->lower_file,
134 lower_dentry, lower_mnt);
135 if (rc || IS_ERR(inode_info->lower_file)) {
136 printk(KERN_ERR "Error opening lower persistent file "
137 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
138 "rc = [%d]\n", lower_dentry, lower_mnt, rc);
139 rc = PTR_ERR(inode_info->lower_file);
140 inode_info->lower_file = NULL;
143 mutex_unlock(&inode_info->lower_file_mutex);
144 return rc;
148 * ecryptfs_interpose
149 * @lower_dentry: Existing dentry in the lower filesystem
150 * @dentry: ecryptfs' dentry
151 * @sb: ecryptfs's super_block
152 * @flag: If set to true, then d_add is called, else d_instantiate is called
154 * Interposes upper and lower dentries.
156 * Returns zero on success; non-zero otherwise
158 int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
159 struct super_block *sb, int flag)
161 struct inode *lower_inode;
162 struct inode *inode;
163 int rc = 0;
165 lower_inode = lower_dentry->d_inode;
166 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
167 rc = -EXDEV;
168 goto out;
170 if (!igrab(lower_inode)) {
171 rc = -ESTALE;
172 goto out;
174 inode = iget5_locked(sb, (unsigned long)lower_inode,
175 ecryptfs_inode_test, ecryptfs_inode_set,
176 lower_inode);
177 if (!inode) {
178 rc = -EACCES;
179 iput(lower_inode);
180 goto out;
182 if (inode->i_state & I_NEW)
183 unlock_new_inode(inode);
184 else
185 iput(lower_inode);
186 if (S_ISLNK(lower_inode->i_mode))
187 inode->i_op = &ecryptfs_symlink_iops;
188 else if (S_ISDIR(lower_inode->i_mode))
189 inode->i_op = &ecryptfs_dir_iops;
190 if (S_ISDIR(lower_inode->i_mode))
191 inode->i_fop = &ecryptfs_dir_fops;
192 if (special_file(lower_inode->i_mode))
193 init_special_inode(inode, lower_inode->i_mode,
194 lower_inode->i_rdev);
195 dentry->d_op = &ecryptfs_dops;
196 if (flag)
197 d_add(dentry, inode);
198 else
199 d_instantiate(dentry, inode);
200 fsstack_copy_attr_all(inode, lower_inode, NULL);
201 /* This size will be overwritten for real files w/ headers and
202 * other metadata */
203 fsstack_copy_inode_size(inode, lower_inode);
204 rc = ecryptfs_init_persistent_file(dentry);
205 if (rc) {
206 printk(KERN_ERR "%s: Error attempting to initialize the "
207 "persistent file for the dentry with name [%s]; "
208 "rc = [%d]\n", __func__, dentry->d_name.name, rc);
209 goto out;
211 out:
212 return rc;
215 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
216 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
217 ecryptfs_opt_ecryptfs_key_bytes,
218 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
219 ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
221 static match_table_t tokens = {
222 {ecryptfs_opt_sig, "sig=%s"},
223 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
224 {ecryptfs_opt_cipher, "cipher=%s"},
225 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
226 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
227 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
228 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
229 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
230 {ecryptfs_opt_err, NULL}
233 static int ecryptfs_init_global_auth_toks(
234 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
236 struct ecryptfs_global_auth_tok *global_auth_tok;
237 int rc = 0;
239 list_for_each_entry(global_auth_tok,
240 &mount_crypt_stat->global_auth_tok_list,
241 mount_crypt_stat_list) {
242 rc = ecryptfs_keyring_auth_tok_for_sig(
243 &global_auth_tok->global_auth_tok_key,
244 &global_auth_tok->global_auth_tok,
245 global_auth_tok->sig);
246 if (rc) {
247 printk(KERN_ERR "Could not find valid key in user "
248 "session keyring for sig specified in mount "
249 "option: [%s]\n", global_auth_tok->sig);
250 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
251 goto out;
252 } else
253 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
255 out:
256 return rc;
259 static void ecryptfs_init_mount_crypt_stat(
260 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
262 memset((void *)mount_crypt_stat, 0,
263 sizeof(struct ecryptfs_mount_crypt_stat));
264 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
265 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
266 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
270 * ecryptfs_parse_options
271 * @sb: The ecryptfs super block
272 * @options: The options pased to the kernel
274 * Parse mount options:
275 * debug=N - ecryptfs_verbosity level for debug output
276 * sig=XXX - description(signature) of the key to use
278 * Returns the dentry object of the lower-level (lower/interposed)
279 * directory; We want to mount our stackable file system on top of
280 * that lower directory.
282 * The signature of the key to use must be the description of a key
283 * already in the keyring. Mounting will fail if the key can not be
284 * found.
286 * Returns zero on success; non-zero on error
288 static int ecryptfs_parse_options(struct super_block *sb, char *options)
290 char *p;
291 int rc = 0;
292 int sig_set = 0;
293 int cipher_name_set = 0;
294 int cipher_key_bytes;
295 int cipher_key_bytes_set = 0;
296 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
297 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
298 substring_t args[MAX_OPT_ARGS];
299 int token;
300 char *sig_src;
301 char *cipher_name_dst;
302 char *cipher_name_src;
303 char *cipher_key_bytes_src;
304 int cipher_name_len;
306 if (!options) {
307 rc = -EINVAL;
308 goto out;
310 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
311 while ((p = strsep(&options, ",")) != NULL) {
312 if (!*p)
313 continue;
314 token = match_token(p, tokens, args);
315 switch (token) {
316 case ecryptfs_opt_sig:
317 case ecryptfs_opt_ecryptfs_sig:
318 sig_src = args[0].from;
319 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
320 sig_src);
321 if (rc) {
322 printk(KERN_ERR "Error attempting to register "
323 "global sig; rc = [%d]\n", rc);
324 goto out;
326 sig_set = 1;
327 break;
328 case ecryptfs_opt_cipher:
329 case ecryptfs_opt_ecryptfs_cipher:
330 cipher_name_src = args[0].from;
331 cipher_name_dst =
332 mount_crypt_stat->
333 global_default_cipher_name;
334 strncpy(cipher_name_dst, cipher_name_src,
335 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
336 ecryptfs_printk(KERN_DEBUG,
337 "The mount_crypt_stat "
338 "global_default_cipher_name set to: "
339 "[%s]\n", cipher_name_dst);
340 cipher_name_set = 1;
341 break;
342 case ecryptfs_opt_ecryptfs_key_bytes:
343 cipher_key_bytes_src = args[0].from;
344 cipher_key_bytes =
345 (int)simple_strtol(cipher_key_bytes_src,
346 &cipher_key_bytes_src, 0);
347 mount_crypt_stat->global_default_cipher_key_size =
348 cipher_key_bytes;
349 ecryptfs_printk(KERN_DEBUG,
350 "The mount_crypt_stat "
351 "global_default_cipher_key_size "
352 "set to: [%d]\n", mount_crypt_stat->
353 global_default_cipher_key_size);
354 cipher_key_bytes_set = 1;
355 break;
356 case ecryptfs_opt_passthrough:
357 mount_crypt_stat->flags |=
358 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
359 break;
360 case ecryptfs_opt_xattr_metadata:
361 mount_crypt_stat->flags |=
362 ECRYPTFS_XATTR_METADATA_ENABLED;
363 break;
364 case ecryptfs_opt_encrypted_view:
365 mount_crypt_stat->flags |=
366 ECRYPTFS_XATTR_METADATA_ENABLED;
367 mount_crypt_stat->flags |=
368 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
369 break;
370 case ecryptfs_opt_err:
371 default:
372 ecryptfs_printk(KERN_WARNING,
373 "eCryptfs: unrecognized option '%s'\n",
377 if (!sig_set) {
378 rc = -EINVAL;
379 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
380 "auth tok signature as a mount "
381 "parameter; see the eCryptfs README\n");
382 goto out;
384 if (!cipher_name_set) {
385 cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
386 if (unlikely(cipher_name_len
387 >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
388 rc = -EINVAL;
389 BUG();
390 goto out;
392 memcpy(mount_crypt_stat->global_default_cipher_name,
393 ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
394 mount_crypt_stat->global_default_cipher_name[cipher_name_len]
395 = '\0';
397 if (!cipher_key_bytes_set) {
398 mount_crypt_stat->global_default_cipher_key_size = 0;
400 mutex_lock(&key_tfm_list_mutex);
401 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
402 NULL))
403 rc = ecryptfs_add_new_key_tfm(
404 NULL, mount_crypt_stat->global_default_cipher_name,
405 mount_crypt_stat->global_default_cipher_key_size);
406 mutex_unlock(&key_tfm_list_mutex);
407 if (rc) {
408 printk(KERN_ERR "Error attempting to initialize cipher with "
409 "name = [%s] and key size = [%td]; rc = [%d]\n",
410 mount_crypt_stat->global_default_cipher_name,
411 mount_crypt_stat->global_default_cipher_key_size, rc);
412 rc = -EINVAL;
413 goto out;
415 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
416 if (rc) {
417 printk(KERN_WARNING "One or more global auth toks could not "
418 "properly register; rc = [%d]\n", rc);
420 out:
421 return rc;
424 struct kmem_cache *ecryptfs_sb_info_cache;
427 * ecryptfs_fill_super
428 * @sb: The ecryptfs super block
429 * @raw_data: The options passed to mount
430 * @silent: Not used but required by function prototype
432 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
434 * Returns zero on success; non-zero otherwise
436 static int
437 ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
439 int rc = 0;
441 /* Released in ecryptfs_put_super() */
442 ecryptfs_set_superblock_private(sb,
443 kmem_cache_zalloc(ecryptfs_sb_info_cache,
444 GFP_KERNEL));
445 if (!ecryptfs_superblock_to_private(sb)) {
446 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
447 rc = -ENOMEM;
448 goto out;
450 sb->s_op = &ecryptfs_sops;
451 /* Released through deactivate_super(sb) from get_sb_nodev */
452 sb->s_root = d_alloc(NULL, &(const struct qstr) {
453 .hash = 0,.name = "/",.len = 1});
454 if (!sb->s_root) {
455 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
456 rc = -ENOMEM;
457 goto out;
459 sb->s_root->d_op = &ecryptfs_dops;
460 sb->s_root->d_sb = sb;
461 sb->s_root->d_parent = sb->s_root;
462 /* Released in d_release when dput(sb->s_root) is called */
463 /* through deactivate_super(sb) from get_sb_nodev() */
464 ecryptfs_set_dentry_private(sb->s_root,
465 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
466 GFP_KERNEL));
467 if (!ecryptfs_dentry_to_private(sb->s_root)) {
468 ecryptfs_printk(KERN_ERR,
469 "dentry_info_cache alloc failed\n");
470 rc = -ENOMEM;
471 goto out;
473 rc = 0;
474 out:
475 /* Should be able to rely on deactivate_super called from
476 * get_sb_nodev */
477 return rc;
481 * ecryptfs_read_super
482 * @sb: The ecryptfs super block
483 * @dev_name: The path to mount over
485 * Read the super block of the lower filesystem, and use
486 * ecryptfs_interpose to create our initial inode and super block
487 * struct.
489 static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
491 int rc;
492 struct nameidata nd;
493 struct dentry *lower_root;
494 struct vfsmount *lower_mnt;
496 memset(&nd, 0, sizeof(struct nameidata));
497 rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
498 if (rc) {
499 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
500 goto out;
502 lower_root = nd.path.dentry;
503 lower_mnt = nd.path.mnt;
504 ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
505 sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
506 sb->s_blocksize = lower_root->d_sb->s_blocksize;
507 ecryptfs_set_dentry_lower(sb->s_root, lower_root);
508 ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
509 rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
510 if (rc)
511 goto out_free;
512 rc = 0;
513 goto out;
514 out_free:
515 path_put(&nd.path);
516 out:
517 return rc;
521 * ecryptfs_get_sb
522 * @fs_type
523 * @flags
524 * @dev_name: The path to mount over
525 * @raw_data: The options passed into the kernel
527 * The whole ecryptfs_get_sb process is broken into 4 functions:
528 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
529 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
530 * with as much information as it can before needing
531 * the lower filesystem.
532 * ecryptfs_read_super(): this accesses the lower filesystem and uses
533 * ecryptfs_interpolate to perform most of the linking
534 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
536 static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
537 const char *dev_name, void *raw_data,
538 struct vfsmount *mnt)
540 int rc;
541 struct super_block *sb;
543 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
544 if (rc < 0) {
545 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
546 goto out;
548 sb = mnt->mnt_sb;
549 rc = ecryptfs_parse_options(sb, raw_data);
550 if (rc) {
551 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
552 goto out_abort;
554 rc = ecryptfs_read_super(sb, dev_name);
555 if (rc) {
556 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
557 goto out_abort;
559 goto out;
560 out_abort:
561 dput(sb->s_root);
562 up_write(&sb->s_umount);
563 deactivate_super(sb);
564 out:
565 return rc;
569 * ecryptfs_kill_block_super
570 * @sb: The ecryptfs super block
572 * Used to bring the superblock down and free the private data.
573 * Private data is free'd in ecryptfs_put_super()
575 static void ecryptfs_kill_block_super(struct super_block *sb)
577 generic_shutdown_super(sb);
580 static struct file_system_type ecryptfs_fs_type = {
581 .owner = THIS_MODULE,
582 .name = "ecryptfs",
583 .get_sb = ecryptfs_get_sb,
584 .kill_sb = ecryptfs_kill_block_super,
585 .fs_flags = 0
589 * inode_info_init_once
591 * Initializes the ecryptfs_inode_info_cache when it is created
593 static void
594 inode_info_init_once(struct kmem_cache *cachep, void *vptr)
596 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
598 inode_init_once(&ei->vfs_inode);
601 static struct ecryptfs_cache_info {
602 struct kmem_cache **cache;
603 const char *name;
604 size_t size;
605 void (*ctor)(struct kmem_cache *cache, void *obj);
606 } ecryptfs_cache_infos[] = {
608 .cache = &ecryptfs_auth_tok_list_item_cache,
609 .name = "ecryptfs_auth_tok_list_item",
610 .size = sizeof(struct ecryptfs_auth_tok_list_item),
613 .cache = &ecryptfs_file_info_cache,
614 .name = "ecryptfs_file_cache",
615 .size = sizeof(struct ecryptfs_file_info),
618 .cache = &ecryptfs_dentry_info_cache,
619 .name = "ecryptfs_dentry_info_cache",
620 .size = sizeof(struct ecryptfs_dentry_info),
623 .cache = &ecryptfs_inode_info_cache,
624 .name = "ecryptfs_inode_cache",
625 .size = sizeof(struct ecryptfs_inode_info),
626 .ctor = inode_info_init_once,
629 .cache = &ecryptfs_sb_info_cache,
630 .name = "ecryptfs_sb_cache",
631 .size = sizeof(struct ecryptfs_sb_info),
634 .cache = &ecryptfs_header_cache_1,
635 .name = "ecryptfs_headers_1",
636 .size = PAGE_CACHE_SIZE,
639 .cache = &ecryptfs_header_cache_2,
640 .name = "ecryptfs_headers_2",
641 .size = PAGE_CACHE_SIZE,
644 .cache = &ecryptfs_xattr_cache,
645 .name = "ecryptfs_xattr_cache",
646 .size = PAGE_CACHE_SIZE,
649 .cache = &ecryptfs_key_record_cache,
650 .name = "ecryptfs_key_record_cache",
651 .size = sizeof(struct ecryptfs_key_record),
654 .cache = &ecryptfs_key_sig_cache,
655 .name = "ecryptfs_key_sig_cache",
656 .size = sizeof(struct ecryptfs_key_sig),
659 .cache = &ecryptfs_global_auth_tok_cache,
660 .name = "ecryptfs_global_auth_tok_cache",
661 .size = sizeof(struct ecryptfs_global_auth_tok),
664 .cache = &ecryptfs_key_tfm_cache,
665 .name = "ecryptfs_key_tfm_cache",
666 .size = sizeof(struct ecryptfs_key_tfm),
669 .cache = &ecryptfs_open_req_cache,
670 .name = "ecryptfs_open_req_cache",
671 .size = sizeof(struct ecryptfs_open_req),
675 static void ecryptfs_free_kmem_caches(void)
677 int i;
679 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
680 struct ecryptfs_cache_info *info;
682 info = &ecryptfs_cache_infos[i];
683 if (*(info->cache))
684 kmem_cache_destroy(*(info->cache));
689 * ecryptfs_init_kmem_caches
691 * Returns zero on success; non-zero otherwise
693 static int ecryptfs_init_kmem_caches(void)
695 int i;
697 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
698 struct ecryptfs_cache_info *info;
700 info = &ecryptfs_cache_infos[i];
701 *(info->cache) = kmem_cache_create(info->name, info->size,
702 0, SLAB_HWCACHE_ALIGN, info->ctor);
703 if (!*(info->cache)) {
704 ecryptfs_free_kmem_caches();
705 ecryptfs_printk(KERN_WARNING, "%s: "
706 "kmem_cache_create failed\n",
707 info->name);
708 return -ENOMEM;
711 return 0;
714 static struct kobject *ecryptfs_kobj;
716 static ssize_t version_show(struct kobject *kobj,
717 struct kobj_attribute *attr, char *buff)
719 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
722 static struct kobj_attribute version_attr = __ATTR_RO(version);
724 static struct attribute *attributes[] = {
725 &version_attr.attr,
726 NULL,
729 static struct attribute_group attr_group = {
730 .attrs = attributes,
733 static int do_sysfs_registration(void)
735 int rc;
737 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
738 if (!ecryptfs_kobj) {
739 printk(KERN_ERR "Unable to create ecryptfs kset\n");
740 rc = -ENOMEM;
741 goto out;
743 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
744 if (rc) {
745 printk(KERN_ERR
746 "Unable to create ecryptfs version attributes\n");
747 kobject_put(ecryptfs_kobj);
749 out:
750 return rc;
753 static void do_sysfs_unregistration(void)
755 sysfs_remove_group(ecryptfs_kobj, &attr_group);
756 kobject_put(ecryptfs_kobj);
759 static int __init ecryptfs_init(void)
761 int rc;
763 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
764 rc = -EINVAL;
765 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
766 "larger than the host's page size, and so "
767 "eCryptfs cannot run on this system. The "
768 "default eCryptfs extent size is [%d] bytes; "
769 "the page size is [%d] bytes.\n",
770 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
771 goto out;
773 rc = ecryptfs_init_kmem_caches();
774 if (rc) {
775 printk(KERN_ERR
776 "Failed to allocate one or more kmem_cache objects\n");
777 goto out;
779 rc = register_filesystem(&ecryptfs_fs_type);
780 if (rc) {
781 printk(KERN_ERR "Failed to register filesystem\n");
782 goto out_free_kmem_caches;
784 rc = do_sysfs_registration();
785 if (rc) {
786 printk(KERN_ERR "sysfs registration failed\n");
787 goto out_unregister_filesystem;
789 rc = ecryptfs_init_kthread();
790 if (rc) {
791 printk(KERN_ERR "%s: kthread initialization failed; "
792 "rc = [%d]\n", __func__, rc);
793 goto out_do_sysfs_unregistration;
795 rc = ecryptfs_init_messaging(ecryptfs_transport);
796 if (rc) {
797 printk(KERN_ERR "Failure occured while attempting to "
798 "initialize the eCryptfs netlink socket\n");
799 goto out_destroy_kthread;
801 rc = ecryptfs_init_crypto();
802 if (rc) {
803 printk(KERN_ERR "Failure whilst attempting to init crypto; "
804 "rc = [%d]\n", rc);
805 goto out_release_messaging;
807 if (ecryptfs_verbosity > 0)
808 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
809 "will be written to the syslog!\n", ecryptfs_verbosity);
811 goto out;
812 out_release_messaging:
813 ecryptfs_release_messaging(ecryptfs_transport);
814 out_destroy_kthread:
815 ecryptfs_destroy_kthread();
816 out_do_sysfs_unregistration:
817 do_sysfs_unregistration();
818 out_unregister_filesystem:
819 unregister_filesystem(&ecryptfs_fs_type);
820 out_free_kmem_caches:
821 ecryptfs_free_kmem_caches();
822 out:
823 return rc;
826 static void __exit ecryptfs_exit(void)
828 int rc;
830 rc = ecryptfs_destroy_crypto();
831 if (rc)
832 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
833 "rc = [%d]\n", rc);
834 ecryptfs_release_messaging(ecryptfs_transport);
835 ecryptfs_destroy_kthread();
836 do_sysfs_unregistration();
837 unregister_filesystem(&ecryptfs_fs_type);
838 ecryptfs_free_kmem_caches();
841 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
842 MODULE_DESCRIPTION("eCryptfs");
844 MODULE_LICENSE("GPL");
846 module_init(ecryptfs_init)
847 module_exit(ecryptfs_exit)