ecryptfs: remove version_str file from sysfs
[linux-2.6/zen-sources.git] / fs / ecryptfs / main.c
blobd984eac9a7f57889adca591654c17d8b4f2e5054
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 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 /* Corresponding dput() and mntput() are done when the
134 * persistent file is fput() when the eCryptfs inode
135 * is destroyed. */
136 dget(lower_dentry);
137 mntget(lower_mnt);
138 inode_info->lower_file = dentry_open(lower_dentry,
139 lower_mnt,
140 (O_RDWR | O_LARGEFILE));
141 if (IS_ERR(inode_info->lower_file)) {
142 dget(lower_dentry);
143 mntget(lower_mnt);
144 inode_info->lower_file = dentry_open(lower_dentry,
145 lower_mnt,
146 (O_RDONLY
147 | O_LARGEFILE));
149 if (IS_ERR(inode_info->lower_file)) {
150 printk(KERN_ERR "Error opening lower persistent file "
151 "for lower_dentry [0x%p] and lower_mnt [0x%p]\n",
152 lower_dentry, lower_mnt);
153 rc = PTR_ERR(inode_info->lower_file);
154 inode_info->lower_file = NULL;
157 mutex_unlock(&inode_info->lower_file_mutex);
158 return rc;
162 * ecryptfs_interpose
163 * @lower_dentry: Existing dentry in the lower filesystem
164 * @dentry: ecryptfs' dentry
165 * @sb: ecryptfs's super_block
166 * @flag: If set to true, then d_add is called, else d_instantiate is called
168 * Interposes upper and lower dentries.
170 * Returns zero on success; non-zero otherwise
172 int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
173 struct super_block *sb, int flag)
175 struct inode *lower_inode;
176 struct inode *inode;
177 int rc = 0;
179 lower_inode = lower_dentry->d_inode;
180 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
181 rc = -EXDEV;
182 goto out;
184 if (!igrab(lower_inode)) {
185 rc = -ESTALE;
186 goto out;
188 inode = iget5_locked(sb, (unsigned long)lower_inode,
189 ecryptfs_inode_test, ecryptfs_inode_set,
190 lower_inode);
191 if (!inode) {
192 rc = -EACCES;
193 iput(lower_inode);
194 goto out;
196 if (inode->i_state & I_NEW)
197 unlock_new_inode(inode);
198 else
199 iput(lower_inode);
200 if (S_ISLNK(lower_inode->i_mode))
201 inode->i_op = &ecryptfs_symlink_iops;
202 else if (S_ISDIR(lower_inode->i_mode))
203 inode->i_op = &ecryptfs_dir_iops;
204 if (S_ISDIR(lower_inode->i_mode))
205 inode->i_fop = &ecryptfs_dir_fops;
206 if (special_file(lower_inode->i_mode))
207 init_special_inode(inode, lower_inode->i_mode,
208 lower_inode->i_rdev);
209 dentry->d_op = &ecryptfs_dops;
210 if (flag)
211 d_add(dentry, inode);
212 else
213 d_instantiate(dentry, inode);
214 fsstack_copy_attr_all(inode, lower_inode, NULL);
215 /* This size will be overwritten for real files w/ headers and
216 * other metadata */
217 fsstack_copy_inode_size(inode, lower_inode);
218 rc = ecryptfs_init_persistent_file(dentry);
219 if (rc) {
220 printk(KERN_ERR "%s: Error attempting to initialize the "
221 "persistent file for the dentry with name [%s]; "
222 "rc = [%d]\n", __FUNCTION__, dentry->d_name.name, rc);
223 goto out;
225 out:
226 return rc;
229 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, ecryptfs_opt_debug,
230 ecryptfs_opt_ecryptfs_debug, ecryptfs_opt_cipher,
231 ecryptfs_opt_ecryptfs_cipher, ecryptfs_opt_ecryptfs_key_bytes,
232 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
233 ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
235 static match_table_t tokens = {
236 {ecryptfs_opt_sig, "sig=%s"},
237 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
238 {ecryptfs_opt_debug, "debug=%u"},
239 {ecryptfs_opt_ecryptfs_debug, "ecryptfs_debug=%u"},
240 {ecryptfs_opt_cipher, "cipher=%s"},
241 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
242 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
243 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
244 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
245 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
246 {ecryptfs_opt_err, NULL}
249 static int ecryptfs_init_global_auth_toks(
250 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
252 struct ecryptfs_global_auth_tok *global_auth_tok;
253 int rc = 0;
255 list_for_each_entry(global_auth_tok,
256 &mount_crypt_stat->global_auth_tok_list,
257 mount_crypt_stat_list) {
258 rc = ecryptfs_keyring_auth_tok_for_sig(
259 &global_auth_tok->global_auth_tok_key,
260 &global_auth_tok->global_auth_tok,
261 global_auth_tok->sig);
262 if (rc) {
263 printk(KERN_ERR "Could not find valid key in user "
264 "session keyring for sig specified in mount "
265 "option: [%s]\n", global_auth_tok->sig);
266 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
267 rc = 0;
268 } else
269 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
271 return rc;
274 static void ecryptfs_init_mount_crypt_stat(
275 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
277 memset((void *)mount_crypt_stat, 0,
278 sizeof(struct ecryptfs_mount_crypt_stat));
279 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
280 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
281 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
285 * ecryptfs_parse_options
286 * @sb: The ecryptfs super block
287 * @options: The options pased to the kernel
289 * Parse mount options:
290 * debug=N - ecryptfs_verbosity level for debug output
291 * sig=XXX - description(signature) of the key to use
293 * Returns the dentry object of the lower-level (lower/interposed)
294 * directory; We want to mount our stackable file system on top of
295 * that lower directory.
297 * The signature of the key to use must be the description of a key
298 * already in the keyring. Mounting will fail if the key can not be
299 * found.
301 * Returns zero on success; non-zero on error
303 static int ecryptfs_parse_options(struct super_block *sb, char *options)
305 char *p;
306 int rc = 0;
307 int sig_set = 0;
308 int cipher_name_set = 0;
309 int cipher_key_bytes;
310 int cipher_key_bytes_set = 0;
311 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
312 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
313 substring_t args[MAX_OPT_ARGS];
314 int token;
315 char *sig_src;
316 char *debug_src;
317 char *cipher_name_dst;
318 char *cipher_name_src;
319 char *cipher_key_bytes_src;
320 int cipher_name_len;
322 if (!options) {
323 rc = -EINVAL;
324 goto out;
326 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
327 while ((p = strsep(&options, ",")) != NULL) {
328 if (!*p)
329 continue;
330 token = match_token(p, tokens, args);
331 switch (token) {
332 case ecryptfs_opt_sig:
333 case ecryptfs_opt_ecryptfs_sig:
334 sig_src = args[0].from;
335 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
336 sig_src);
337 if (rc) {
338 printk(KERN_ERR "Error attempting to register "
339 "global sig; rc = [%d]\n", rc);
340 goto out;
342 sig_set = 1;
343 break;
344 case ecryptfs_opt_debug:
345 case ecryptfs_opt_ecryptfs_debug:
346 debug_src = args[0].from;
347 ecryptfs_verbosity =
348 (int)simple_strtol(debug_src, &debug_src,
350 ecryptfs_printk(KERN_DEBUG,
351 "Verbosity set to [%d]" "\n",
352 ecryptfs_verbosity);
353 break;
354 case ecryptfs_opt_cipher:
355 case ecryptfs_opt_ecryptfs_cipher:
356 cipher_name_src = args[0].from;
357 cipher_name_dst =
358 mount_crypt_stat->
359 global_default_cipher_name;
360 strncpy(cipher_name_dst, cipher_name_src,
361 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
362 ecryptfs_printk(KERN_DEBUG,
363 "The mount_crypt_stat "
364 "global_default_cipher_name set to: "
365 "[%s]\n", cipher_name_dst);
366 cipher_name_set = 1;
367 break;
368 case ecryptfs_opt_ecryptfs_key_bytes:
369 cipher_key_bytes_src = args[0].from;
370 cipher_key_bytes =
371 (int)simple_strtol(cipher_key_bytes_src,
372 &cipher_key_bytes_src, 0);
373 mount_crypt_stat->global_default_cipher_key_size =
374 cipher_key_bytes;
375 ecryptfs_printk(KERN_DEBUG,
376 "The mount_crypt_stat "
377 "global_default_cipher_key_size "
378 "set to: [%d]\n", mount_crypt_stat->
379 global_default_cipher_key_size);
380 cipher_key_bytes_set = 1;
381 break;
382 case ecryptfs_opt_passthrough:
383 mount_crypt_stat->flags |=
384 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
385 break;
386 case ecryptfs_opt_xattr_metadata:
387 mount_crypt_stat->flags |=
388 ECRYPTFS_XATTR_METADATA_ENABLED;
389 break;
390 case ecryptfs_opt_encrypted_view:
391 mount_crypt_stat->flags |=
392 ECRYPTFS_XATTR_METADATA_ENABLED;
393 mount_crypt_stat->flags |=
394 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
395 break;
396 case ecryptfs_opt_err:
397 default:
398 ecryptfs_printk(KERN_WARNING,
399 "eCryptfs: unrecognized option '%s'\n",
403 if (!sig_set) {
404 rc = -EINVAL;
405 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
406 "auth tok signature as a mount "
407 "parameter; see the eCryptfs README\n");
408 goto out;
410 if (!cipher_name_set) {
411 cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
412 if (unlikely(cipher_name_len
413 >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
414 rc = -EINVAL;
415 BUG();
416 goto out;
418 memcpy(mount_crypt_stat->global_default_cipher_name,
419 ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
420 mount_crypt_stat->global_default_cipher_name[cipher_name_len]
421 = '\0';
423 if (!cipher_key_bytes_set) {
424 mount_crypt_stat->global_default_cipher_key_size = 0;
426 rc = ecryptfs_add_new_key_tfm(
427 NULL, mount_crypt_stat->global_default_cipher_name,
428 mount_crypt_stat->global_default_cipher_key_size);
429 if (rc) {
430 printk(KERN_ERR "Error attempting to initialize cipher with "
431 "name = [%s] and key size = [%td]; rc = [%d]\n",
432 mount_crypt_stat->global_default_cipher_name,
433 mount_crypt_stat->global_default_cipher_key_size, rc);
434 rc = -EINVAL;
435 goto out;
437 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
438 if (rc) {
439 printk(KERN_WARNING "One or more global auth toks could not "
440 "properly register; rc = [%d]\n", rc);
442 rc = 0;
443 out:
444 return rc;
447 struct kmem_cache *ecryptfs_sb_info_cache;
450 * ecryptfs_fill_super
451 * @sb: The ecryptfs super block
452 * @raw_data: The options passed to mount
453 * @silent: Not used but required by function prototype
455 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
457 * Returns zero on success; non-zero otherwise
459 static int
460 ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
462 int rc = 0;
464 /* Released in ecryptfs_put_super() */
465 ecryptfs_set_superblock_private(sb,
466 kmem_cache_zalloc(ecryptfs_sb_info_cache,
467 GFP_KERNEL));
468 if (!ecryptfs_superblock_to_private(sb)) {
469 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
470 rc = -ENOMEM;
471 goto out;
473 sb->s_op = &ecryptfs_sops;
474 /* Released through deactivate_super(sb) from get_sb_nodev */
475 sb->s_root = d_alloc(NULL, &(const struct qstr) {
476 .hash = 0,.name = "/",.len = 1});
477 if (!sb->s_root) {
478 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
479 rc = -ENOMEM;
480 goto out;
482 sb->s_root->d_op = &ecryptfs_dops;
483 sb->s_root->d_sb = sb;
484 sb->s_root->d_parent = sb->s_root;
485 /* Released in d_release when dput(sb->s_root) is called */
486 /* through deactivate_super(sb) from get_sb_nodev() */
487 ecryptfs_set_dentry_private(sb->s_root,
488 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
489 GFP_KERNEL));
490 if (!ecryptfs_dentry_to_private(sb->s_root)) {
491 ecryptfs_printk(KERN_ERR,
492 "dentry_info_cache alloc failed\n");
493 rc = -ENOMEM;
494 goto out;
496 rc = 0;
497 out:
498 /* Should be able to rely on deactivate_super called from
499 * get_sb_nodev */
500 return rc;
504 * ecryptfs_read_super
505 * @sb: The ecryptfs super block
506 * @dev_name: The path to mount over
508 * Read the super block of the lower filesystem, and use
509 * ecryptfs_interpose to create our initial inode and super block
510 * struct.
512 static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
514 int rc;
515 struct nameidata nd;
516 struct dentry *lower_root;
517 struct vfsmount *lower_mnt;
519 memset(&nd, 0, sizeof(struct nameidata));
520 rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
521 if (rc) {
522 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
523 goto out;
525 lower_root = nd.dentry;
526 lower_mnt = nd.mnt;
527 ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
528 sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
529 sb->s_blocksize = lower_root->d_sb->s_blocksize;
530 ecryptfs_set_dentry_lower(sb->s_root, lower_root);
531 ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
532 rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
533 if (rc)
534 goto out_free;
535 rc = 0;
536 goto out;
537 out_free:
538 path_release(&nd);
539 out:
540 return rc;
544 * ecryptfs_get_sb
545 * @fs_type
546 * @flags
547 * @dev_name: The path to mount over
548 * @raw_data: The options passed into the kernel
550 * The whole ecryptfs_get_sb process is broken into 4 functions:
551 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
552 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
553 * with as much information as it can before needing
554 * the lower filesystem.
555 * ecryptfs_read_super(): this accesses the lower filesystem and uses
556 * ecryptfs_interpolate to perform most of the linking
557 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
559 static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
560 const char *dev_name, void *raw_data,
561 struct vfsmount *mnt)
563 int rc;
564 struct super_block *sb;
566 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
567 if (rc < 0) {
568 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
569 goto out;
571 sb = mnt->mnt_sb;
572 rc = ecryptfs_parse_options(sb, raw_data);
573 if (rc) {
574 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
575 goto out_abort;
577 rc = ecryptfs_read_super(sb, dev_name);
578 if (rc) {
579 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
580 goto out_abort;
582 goto out;
583 out_abort:
584 dput(sb->s_root);
585 up_write(&sb->s_umount);
586 deactivate_super(sb);
587 out:
588 return rc;
592 * ecryptfs_kill_block_super
593 * @sb: The ecryptfs super block
595 * Used to bring the superblock down and free the private data.
596 * Private data is free'd in ecryptfs_put_super()
598 static void ecryptfs_kill_block_super(struct super_block *sb)
600 generic_shutdown_super(sb);
603 static struct file_system_type ecryptfs_fs_type = {
604 .owner = THIS_MODULE,
605 .name = "ecryptfs",
606 .get_sb = ecryptfs_get_sb,
607 .kill_sb = ecryptfs_kill_block_super,
608 .fs_flags = 0
612 * inode_info_init_once
614 * Initializes the ecryptfs_inode_info_cache when it is created
616 static void
617 inode_info_init_once(struct kmem_cache *cachep, void *vptr)
619 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
621 inode_init_once(&ei->vfs_inode);
624 static struct ecryptfs_cache_info {
625 struct kmem_cache **cache;
626 const char *name;
627 size_t size;
628 void (*ctor)(struct kmem_cache *cache, void *obj);
629 } ecryptfs_cache_infos[] = {
631 .cache = &ecryptfs_auth_tok_list_item_cache,
632 .name = "ecryptfs_auth_tok_list_item",
633 .size = sizeof(struct ecryptfs_auth_tok_list_item),
636 .cache = &ecryptfs_file_info_cache,
637 .name = "ecryptfs_file_cache",
638 .size = sizeof(struct ecryptfs_file_info),
641 .cache = &ecryptfs_dentry_info_cache,
642 .name = "ecryptfs_dentry_info_cache",
643 .size = sizeof(struct ecryptfs_dentry_info),
646 .cache = &ecryptfs_inode_info_cache,
647 .name = "ecryptfs_inode_cache",
648 .size = sizeof(struct ecryptfs_inode_info),
649 .ctor = inode_info_init_once,
652 .cache = &ecryptfs_sb_info_cache,
653 .name = "ecryptfs_sb_cache",
654 .size = sizeof(struct ecryptfs_sb_info),
657 .cache = &ecryptfs_header_cache_0,
658 .name = "ecryptfs_headers_0",
659 .size = PAGE_CACHE_SIZE,
662 .cache = &ecryptfs_header_cache_1,
663 .name = "ecryptfs_headers_1",
664 .size = PAGE_CACHE_SIZE,
667 .cache = &ecryptfs_header_cache_2,
668 .name = "ecryptfs_headers_2",
669 .size = PAGE_CACHE_SIZE,
672 .cache = &ecryptfs_xattr_cache,
673 .name = "ecryptfs_xattr_cache",
674 .size = PAGE_CACHE_SIZE,
677 .cache = &ecryptfs_key_record_cache,
678 .name = "ecryptfs_key_record_cache",
679 .size = sizeof(struct ecryptfs_key_record),
682 .cache = &ecryptfs_key_sig_cache,
683 .name = "ecryptfs_key_sig_cache",
684 .size = sizeof(struct ecryptfs_key_sig),
687 .cache = &ecryptfs_global_auth_tok_cache,
688 .name = "ecryptfs_global_auth_tok_cache",
689 .size = sizeof(struct ecryptfs_global_auth_tok),
692 .cache = &ecryptfs_key_tfm_cache,
693 .name = "ecryptfs_key_tfm_cache",
694 .size = sizeof(struct ecryptfs_key_tfm),
698 static void ecryptfs_free_kmem_caches(void)
700 int i;
702 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
703 struct ecryptfs_cache_info *info;
705 info = &ecryptfs_cache_infos[i];
706 if (*(info->cache))
707 kmem_cache_destroy(*(info->cache));
712 * ecryptfs_init_kmem_caches
714 * Returns zero on success; non-zero otherwise
716 static int ecryptfs_init_kmem_caches(void)
718 int i;
720 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
721 struct ecryptfs_cache_info *info;
723 info = &ecryptfs_cache_infos[i];
724 *(info->cache) = kmem_cache_create(info->name, info->size,
725 0, SLAB_HWCACHE_ALIGN, info->ctor);
726 if (!*(info->cache)) {
727 ecryptfs_free_kmem_caches();
728 ecryptfs_printk(KERN_WARNING, "%s: "
729 "kmem_cache_create failed\n",
730 info->name);
731 return -ENOMEM;
734 return 0;
737 static struct kset *ecryptfs_kset;
739 static ssize_t version_show(struct kobject *kobj,
740 struct kobj_attribute *attr, char *buff)
742 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
745 static struct kobj_attribute version_attr = __ATTR_RO(version);
747 static struct attribute *attributes[] = {
748 &version_attr.attr,
749 NULL,
752 static struct attribute_group attr_group = {
753 .attrs = attributes,
756 static int do_sysfs_registration(void)
758 int rc;
760 ecryptfs_kset = kset_create_and_add("ecryptfs", NULL, fs_kobj);
761 if (!ecryptfs_kset) {
762 printk(KERN_ERR "Unable to create ecryptfs kset\n");
763 rc = -ENOMEM;
764 goto out;
766 rc = sysfs_create_group(&ecryptfs_kset->kobj, &attr_group);
767 if (rc) {
768 printk(KERN_ERR
769 "Unable to create ecryptfs version attributes\n");
770 kset_unregister(ecryptfs_kset);
772 out:
773 return rc;
776 static void do_sysfs_unregistration(void)
778 sysfs_remove_group(&ecryptfs_kset->kobj, &attr_group);
779 kset_unregister(ecryptfs_kset);
782 static int __init ecryptfs_init(void)
784 int rc;
786 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
787 rc = -EINVAL;
788 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
789 "larger than the host's page size, and so "
790 "eCryptfs cannot run on this system. The "
791 "default eCryptfs extent size is [%d] bytes; "
792 "the page size is [%d] bytes.\n",
793 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
794 goto out;
796 rc = ecryptfs_init_kmem_caches();
797 if (rc) {
798 printk(KERN_ERR
799 "Failed to allocate one or more kmem_cache objects\n");
800 goto out;
802 rc = register_filesystem(&ecryptfs_fs_type);
803 if (rc) {
804 printk(KERN_ERR "Failed to register filesystem\n");
805 goto out_free_kmem_caches;
807 rc = do_sysfs_registration();
808 if (rc) {
809 printk(KERN_ERR "sysfs registration failed\n");
810 goto out_unregister_filesystem;
812 rc = ecryptfs_init_messaging(ecryptfs_transport);
813 if (rc) {
814 ecryptfs_printk(KERN_ERR, "Failure occured while attempting to "
815 "initialize the eCryptfs netlink socket\n");
816 goto out_do_sysfs_unregistration;
818 rc = ecryptfs_init_crypto();
819 if (rc) {
820 printk(KERN_ERR "Failure whilst attempting to init crypto; "
821 "rc = [%d]\n", rc);
822 goto out_release_messaging;
824 goto out;
825 out_release_messaging:
826 ecryptfs_release_messaging(ecryptfs_transport);
827 out_do_sysfs_unregistration:
828 do_sysfs_unregistration();
829 out_unregister_filesystem:
830 unregister_filesystem(&ecryptfs_fs_type);
831 out_free_kmem_caches:
832 ecryptfs_free_kmem_caches();
833 out:
834 return rc;
837 static void __exit ecryptfs_exit(void)
839 int rc;
841 rc = ecryptfs_destroy_crypto();
842 if (rc)
843 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
844 "rc = [%d]\n", rc);
845 ecryptfs_release_messaging(ecryptfs_transport);
846 do_sysfs_unregistration();
847 unregister_filesystem(&ecryptfs_fs_type);
848 ecryptfs_free_kmem_caches();
851 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
852 MODULE_DESCRIPTION("eCryptfs");
854 MODULE_LICENSE("GPL");
856 module_init(ecryptfs_init)
857 module_exit(ecryptfs_exit)