Staging: usbip: fix build warning on 64bit kernels
[linux/fpc-iii.git] / fs / ecryptfs / main.c
blob046e027a4cb192705a49caf27048ac79cf35badb
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/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"
40 /**
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)");
50 /**
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");
59 /**
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 "
71 "userspace");
73 /**
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, ...)
86 va_list args;
87 va_start(args, fmt);
88 if (fmt[1] == '7') { /* KERN_DEBUG */
89 if (ecryptfs_verbosity >= 1)
90 vprintk(fmt, args);
91 } else
92 vprintk(fmt, args);
93 va_end(args);
96 /**
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 struct ecryptfs_inode_info *inode_info =
119 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
120 int rc = 0;
122 mutex_lock(&inode_info->lower_file_mutex);
123 if (!inode_info->lower_file) {
124 struct dentry *lower_dentry;
125 struct vfsmount *lower_mnt =
126 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
128 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
129 rc = ecryptfs_privileged_open(&inode_info->lower_file,
130 lower_dentry, lower_mnt);
131 if (rc || IS_ERR(inode_info->lower_file)) {
132 printk(KERN_ERR "Error opening lower persistent file "
133 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
134 "rc = [%d]\n", lower_dentry, lower_mnt, rc);
135 rc = PTR_ERR(inode_info->lower_file);
136 inode_info->lower_file = NULL;
139 mutex_unlock(&inode_info->lower_file_mutex);
140 return rc;
144 * ecryptfs_interpose
145 * @lower_dentry: Existing dentry in the lower filesystem
146 * @dentry: ecryptfs' dentry
147 * @sb: ecryptfs's super_block
148 * @flags: flags to govern behavior of interpose procedure
150 * Interposes upper and lower dentries.
152 * Returns zero on success; non-zero otherwise
154 int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
155 struct super_block *sb, u32 flags)
157 struct inode *lower_inode;
158 struct inode *inode;
159 int rc = 0;
161 lower_inode = lower_dentry->d_inode;
162 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
163 rc = -EXDEV;
164 goto out;
166 if (!igrab(lower_inode)) {
167 rc = -ESTALE;
168 goto out;
170 inode = iget5_locked(sb, (unsigned long)lower_inode,
171 ecryptfs_inode_test, ecryptfs_inode_set,
172 lower_inode);
173 if (!inode) {
174 rc = -EACCES;
175 iput(lower_inode);
176 goto out;
178 if (inode->i_state & I_NEW)
179 unlock_new_inode(inode);
180 else
181 iput(lower_inode);
182 if (S_ISLNK(lower_inode->i_mode))
183 inode->i_op = &ecryptfs_symlink_iops;
184 else if (S_ISDIR(lower_inode->i_mode))
185 inode->i_op = &ecryptfs_dir_iops;
186 if (S_ISDIR(lower_inode->i_mode))
187 inode->i_fop = &ecryptfs_dir_fops;
188 if (special_file(lower_inode->i_mode))
189 init_special_inode(inode, lower_inode->i_mode,
190 lower_inode->i_rdev);
191 dentry->d_op = &ecryptfs_dops;
192 if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
193 d_add(dentry, inode);
194 else
195 d_instantiate(dentry, inode);
196 fsstack_copy_attr_all(inode, lower_inode, NULL);
197 /* This size will be overwritten for real files w/ headers and
198 * other metadata */
199 fsstack_copy_inode_size(inode, lower_inode);
200 out:
201 return rc;
204 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
205 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
206 ecryptfs_opt_ecryptfs_key_bytes,
207 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
208 ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
210 static const match_table_t tokens = {
211 {ecryptfs_opt_sig, "sig=%s"},
212 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
213 {ecryptfs_opt_cipher, "cipher=%s"},
214 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
215 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
216 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
217 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
218 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
219 {ecryptfs_opt_err, NULL}
222 static int ecryptfs_init_global_auth_toks(
223 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
225 struct ecryptfs_global_auth_tok *global_auth_tok;
226 int rc = 0;
228 list_for_each_entry(global_auth_tok,
229 &mount_crypt_stat->global_auth_tok_list,
230 mount_crypt_stat_list) {
231 rc = ecryptfs_keyring_auth_tok_for_sig(
232 &global_auth_tok->global_auth_tok_key,
233 &global_auth_tok->global_auth_tok,
234 global_auth_tok->sig);
235 if (rc) {
236 printk(KERN_ERR "Could not find valid key in user "
237 "session keyring for sig specified in mount "
238 "option: [%s]\n", global_auth_tok->sig);
239 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
240 goto out;
241 } else
242 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
244 out:
245 return rc;
248 static void ecryptfs_init_mount_crypt_stat(
249 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
251 memset((void *)mount_crypt_stat, 0,
252 sizeof(struct ecryptfs_mount_crypt_stat));
253 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
254 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
255 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
259 * ecryptfs_parse_options
260 * @sb: The ecryptfs super block
261 * @options: The options pased to the kernel
263 * Parse mount options:
264 * debug=N - ecryptfs_verbosity level for debug output
265 * sig=XXX - description(signature) of the key to use
267 * Returns the dentry object of the lower-level (lower/interposed)
268 * directory; We want to mount our stackable file system on top of
269 * that lower directory.
271 * The signature of the key to use must be the description of a key
272 * already in the keyring. Mounting will fail if the key can not be
273 * found.
275 * Returns zero on success; non-zero on error
277 static int ecryptfs_parse_options(struct super_block *sb, char *options)
279 char *p;
280 int rc = 0;
281 int sig_set = 0;
282 int cipher_name_set = 0;
283 int cipher_key_bytes;
284 int cipher_key_bytes_set = 0;
285 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
286 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
287 substring_t args[MAX_OPT_ARGS];
288 int token;
289 char *sig_src;
290 char *cipher_name_dst;
291 char *cipher_name_src;
292 char *cipher_key_bytes_src;
294 if (!options) {
295 rc = -EINVAL;
296 goto out;
298 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
299 while ((p = strsep(&options, ",")) != NULL) {
300 if (!*p)
301 continue;
302 token = match_token(p, tokens, args);
303 switch (token) {
304 case ecryptfs_opt_sig:
305 case ecryptfs_opt_ecryptfs_sig:
306 sig_src = args[0].from;
307 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
308 sig_src);
309 if (rc) {
310 printk(KERN_ERR "Error attempting to register "
311 "global sig; rc = [%d]\n", rc);
312 goto out;
314 sig_set = 1;
315 break;
316 case ecryptfs_opt_cipher:
317 case ecryptfs_opt_ecryptfs_cipher:
318 cipher_name_src = args[0].from;
319 cipher_name_dst =
320 mount_crypt_stat->
321 global_default_cipher_name;
322 strncpy(cipher_name_dst, cipher_name_src,
323 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
324 ecryptfs_printk(KERN_DEBUG,
325 "The mount_crypt_stat "
326 "global_default_cipher_name set to: "
327 "[%s]\n", cipher_name_dst);
328 cipher_name_set = 1;
329 break;
330 case ecryptfs_opt_ecryptfs_key_bytes:
331 cipher_key_bytes_src = args[0].from;
332 cipher_key_bytes =
333 (int)simple_strtol(cipher_key_bytes_src,
334 &cipher_key_bytes_src, 0);
335 mount_crypt_stat->global_default_cipher_key_size =
336 cipher_key_bytes;
337 ecryptfs_printk(KERN_DEBUG,
338 "The mount_crypt_stat "
339 "global_default_cipher_key_size "
340 "set to: [%d]\n", mount_crypt_stat->
341 global_default_cipher_key_size);
342 cipher_key_bytes_set = 1;
343 break;
344 case ecryptfs_opt_passthrough:
345 mount_crypt_stat->flags |=
346 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
347 break;
348 case ecryptfs_opt_xattr_metadata:
349 mount_crypt_stat->flags |=
350 ECRYPTFS_XATTR_METADATA_ENABLED;
351 break;
352 case ecryptfs_opt_encrypted_view:
353 mount_crypt_stat->flags |=
354 ECRYPTFS_XATTR_METADATA_ENABLED;
355 mount_crypt_stat->flags |=
356 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
357 break;
358 case ecryptfs_opt_err:
359 default:
360 ecryptfs_printk(KERN_WARNING,
361 "eCryptfs: unrecognized option '%s'\n",
365 if (!sig_set) {
366 rc = -EINVAL;
367 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
368 "auth tok signature as a mount "
369 "parameter; see the eCryptfs README\n");
370 goto out;
372 if (!cipher_name_set) {
373 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
375 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
377 strcpy(mount_crypt_stat->global_default_cipher_name,
378 ECRYPTFS_DEFAULT_CIPHER);
380 if (!cipher_key_bytes_set) {
381 mount_crypt_stat->global_default_cipher_key_size = 0;
383 mutex_lock(&key_tfm_list_mutex);
384 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
385 NULL))
386 rc = ecryptfs_add_new_key_tfm(
387 NULL, mount_crypt_stat->global_default_cipher_name,
388 mount_crypt_stat->global_default_cipher_key_size);
389 mutex_unlock(&key_tfm_list_mutex);
390 if (rc) {
391 printk(KERN_ERR "Error attempting to initialize cipher with "
392 "name = [%s] and key size = [%td]; rc = [%d]\n",
393 mount_crypt_stat->global_default_cipher_name,
394 mount_crypt_stat->global_default_cipher_key_size, rc);
395 rc = -EINVAL;
396 goto out;
398 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
399 if (rc) {
400 printk(KERN_WARNING "One or more global auth toks could not "
401 "properly register; rc = [%d]\n", rc);
403 out:
404 return rc;
407 struct kmem_cache *ecryptfs_sb_info_cache;
410 * ecryptfs_fill_super
411 * @sb: The ecryptfs super block
412 * @raw_data: The options passed to mount
413 * @silent: Not used but required by function prototype
415 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
417 * Returns zero on success; non-zero otherwise
419 static int
420 ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
422 int rc = 0;
424 /* Released in ecryptfs_put_super() */
425 ecryptfs_set_superblock_private(sb,
426 kmem_cache_zalloc(ecryptfs_sb_info_cache,
427 GFP_KERNEL));
428 if (!ecryptfs_superblock_to_private(sb)) {
429 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
430 rc = -ENOMEM;
431 goto out;
433 sb->s_op = &ecryptfs_sops;
434 /* Released through deactivate_super(sb) from get_sb_nodev */
435 sb->s_root = d_alloc(NULL, &(const struct qstr) {
436 .hash = 0,.name = "/",.len = 1});
437 if (!sb->s_root) {
438 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
439 rc = -ENOMEM;
440 goto out;
442 sb->s_root->d_op = &ecryptfs_dops;
443 sb->s_root->d_sb = sb;
444 sb->s_root->d_parent = sb->s_root;
445 /* Released in d_release when dput(sb->s_root) is called */
446 /* through deactivate_super(sb) from get_sb_nodev() */
447 ecryptfs_set_dentry_private(sb->s_root,
448 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
449 GFP_KERNEL));
450 if (!ecryptfs_dentry_to_private(sb->s_root)) {
451 ecryptfs_printk(KERN_ERR,
452 "dentry_info_cache alloc failed\n");
453 rc = -ENOMEM;
454 goto out;
456 rc = 0;
457 out:
458 /* Should be able to rely on deactivate_super called from
459 * get_sb_nodev */
460 return rc;
464 * ecryptfs_read_super
465 * @sb: The ecryptfs super block
466 * @dev_name: The path to mount over
468 * Read the super block of the lower filesystem, and use
469 * ecryptfs_interpose to create our initial inode and super block
470 * struct.
472 static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
474 int rc;
475 struct nameidata nd;
476 struct dentry *lower_root;
477 struct vfsmount *lower_mnt;
479 memset(&nd, 0, sizeof(struct nameidata));
480 rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
481 if (rc) {
482 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
483 goto out;
485 lower_root = nd.path.dentry;
486 lower_mnt = nd.path.mnt;
487 ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
488 sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
489 sb->s_blocksize = lower_root->d_sb->s_blocksize;
490 ecryptfs_set_dentry_lower(sb->s_root, lower_root);
491 ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
492 rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
493 if (rc)
494 goto out_free;
495 rc = 0;
496 goto out;
497 out_free:
498 path_put(&nd.path);
499 out:
500 return rc;
504 * ecryptfs_get_sb
505 * @fs_type
506 * @flags
507 * @dev_name: The path to mount over
508 * @raw_data: The options passed into the kernel
510 * The whole ecryptfs_get_sb process is broken into 4 functions:
511 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
512 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
513 * with as much information as it can before needing
514 * the lower filesystem.
515 * ecryptfs_read_super(): this accesses the lower filesystem and uses
516 * ecryptfs_interpolate to perform most of the linking
517 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
519 static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
520 const char *dev_name, void *raw_data,
521 struct vfsmount *mnt)
523 int rc;
524 struct super_block *sb;
526 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
527 if (rc < 0) {
528 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
529 goto out;
531 sb = mnt->mnt_sb;
532 rc = ecryptfs_parse_options(sb, raw_data);
533 if (rc) {
534 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
535 goto out_abort;
537 rc = ecryptfs_read_super(sb, dev_name);
538 if (rc) {
539 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
540 goto out_abort;
542 goto out;
543 out_abort:
544 dput(sb->s_root);
545 up_write(&sb->s_umount);
546 deactivate_super(sb);
547 out:
548 return rc;
552 * ecryptfs_kill_block_super
553 * @sb: The ecryptfs super block
555 * Used to bring the superblock down and free the private data.
556 * Private data is free'd in ecryptfs_put_super()
558 static void ecryptfs_kill_block_super(struct super_block *sb)
560 generic_shutdown_super(sb);
563 static struct file_system_type ecryptfs_fs_type = {
564 .owner = THIS_MODULE,
565 .name = "ecryptfs",
566 .get_sb = ecryptfs_get_sb,
567 .kill_sb = ecryptfs_kill_block_super,
568 .fs_flags = 0
572 * inode_info_init_once
574 * Initializes the ecryptfs_inode_info_cache when it is created
576 static void
577 inode_info_init_once(void *vptr)
579 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
581 inode_init_once(&ei->vfs_inode);
584 static struct ecryptfs_cache_info {
585 struct kmem_cache **cache;
586 const char *name;
587 size_t size;
588 void (*ctor)(void *obj);
589 } ecryptfs_cache_infos[] = {
591 .cache = &ecryptfs_auth_tok_list_item_cache,
592 .name = "ecryptfs_auth_tok_list_item",
593 .size = sizeof(struct ecryptfs_auth_tok_list_item),
596 .cache = &ecryptfs_file_info_cache,
597 .name = "ecryptfs_file_cache",
598 .size = sizeof(struct ecryptfs_file_info),
601 .cache = &ecryptfs_dentry_info_cache,
602 .name = "ecryptfs_dentry_info_cache",
603 .size = sizeof(struct ecryptfs_dentry_info),
606 .cache = &ecryptfs_inode_info_cache,
607 .name = "ecryptfs_inode_cache",
608 .size = sizeof(struct ecryptfs_inode_info),
609 .ctor = inode_info_init_once,
612 .cache = &ecryptfs_sb_info_cache,
613 .name = "ecryptfs_sb_cache",
614 .size = sizeof(struct ecryptfs_sb_info),
617 .cache = &ecryptfs_header_cache_1,
618 .name = "ecryptfs_headers_1",
619 .size = PAGE_CACHE_SIZE,
622 .cache = &ecryptfs_header_cache_2,
623 .name = "ecryptfs_headers_2",
624 .size = PAGE_CACHE_SIZE,
627 .cache = &ecryptfs_xattr_cache,
628 .name = "ecryptfs_xattr_cache",
629 .size = PAGE_CACHE_SIZE,
632 .cache = &ecryptfs_key_record_cache,
633 .name = "ecryptfs_key_record_cache",
634 .size = sizeof(struct ecryptfs_key_record),
637 .cache = &ecryptfs_key_sig_cache,
638 .name = "ecryptfs_key_sig_cache",
639 .size = sizeof(struct ecryptfs_key_sig),
642 .cache = &ecryptfs_global_auth_tok_cache,
643 .name = "ecryptfs_global_auth_tok_cache",
644 .size = sizeof(struct ecryptfs_global_auth_tok),
647 .cache = &ecryptfs_key_tfm_cache,
648 .name = "ecryptfs_key_tfm_cache",
649 .size = sizeof(struct ecryptfs_key_tfm),
652 .cache = &ecryptfs_open_req_cache,
653 .name = "ecryptfs_open_req_cache",
654 .size = sizeof(struct ecryptfs_open_req),
658 static void ecryptfs_free_kmem_caches(void)
660 int i;
662 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
663 struct ecryptfs_cache_info *info;
665 info = &ecryptfs_cache_infos[i];
666 if (*(info->cache))
667 kmem_cache_destroy(*(info->cache));
672 * ecryptfs_init_kmem_caches
674 * Returns zero on success; non-zero otherwise
676 static int ecryptfs_init_kmem_caches(void)
678 int i;
680 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
681 struct ecryptfs_cache_info *info;
683 info = &ecryptfs_cache_infos[i];
684 *(info->cache) = kmem_cache_create(info->name, info->size,
685 0, SLAB_HWCACHE_ALIGN, info->ctor);
686 if (!*(info->cache)) {
687 ecryptfs_free_kmem_caches();
688 ecryptfs_printk(KERN_WARNING, "%s: "
689 "kmem_cache_create failed\n",
690 info->name);
691 return -ENOMEM;
694 return 0;
697 static struct kobject *ecryptfs_kobj;
699 static ssize_t version_show(struct kobject *kobj,
700 struct kobj_attribute *attr, char *buff)
702 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
705 static struct kobj_attribute version_attr = __ATTR_RO(version);
707 static struct attribute *attributes[] = {
708 &version_attr.attr,
709 NULL,
712 static struct attribute_group attr_group = {
713 .attrs = attributes,
716 static int do_sysfs_registration(void)
718 int rc;
720 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
721 if (!ecryptfs_kobj) {
722 printk(KERN_ERR "Unable to create ecryptfs kset\n");
723 rc = -ENOMEM;
724 goto out;
726 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
727 if (rc) {
728 printk(KERN_ERR
729 "Unable to create ecryptfs version attributes\n");
730 kobject_put(ecryptfs_kobj);
732 out:
733 return rc;
736 static void do_sysfs_unregistration(void)
738 sysfs_remove_group(ecryptfs_kobj, &attr_group);
739 kobject_put(ecryptfs_kobj);
742 static int __init ecryptfs_init(void)
744 int rc;
746 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
747 rc = -EINVAL;
748 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
749 "larger than the host's page size, and so "
750 "eCryptfs cannot run on this system. The "
751 "default eCryptfs extent size is [%d] bytes; "
752 "the page size is [%d] bytes.\n",
753 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
754 goto out;
756 rc = ecryptfs_init_kmem_caches();
757 if (rc) {
758 printk(KERN_ERR
759 "Failed to allocate one or more kmem_cache objects\n");
760 goto out;
762 rc = register_filesystem(&ecryptfs_fs_type);
763 if (rc) {
764 printk(KERN_ERR "Failed to register filesystem\n");
765 goto out_free_kmem_caches;
767 rc = do_sysfs_registration();
768 if (rc) {
769 printk(KERN_ERR "sysfs registration failed\n");
770 goto out_unregister_filesystem;
772 rc = ecryptfs_init_kthread();
773 if (rc) {
774 printk(KERN_ERR "%s: kthread initialization failed; "
775 "rc = [%d]\n", __func__, rc);
776 goto out_do_sysfs_unregistration;
778 rc = ecryptfs_init_messaging();
779 if (rc) {
780 printk(KERN_ERR "Failure occured while attempting to "
781 "initialize the communications channel to "
782 "ecryptfsd\n");
783 goto out_destroy_kthread;
785 rc = ecryptfs_init_crypto();
786 if (rc) {
787 printk(KERN_ERR "Failure whilst attempting to init crypto; "
788 "rc = [%d]\n", rc);
789 goto out_release_messaging;
791 if (ecryptfs_verbosity > 0)
792 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
793 "will be written to the syslog!\n", ecryptfs_verbosity);
795 goto out;
796 out_release_messaging:
797 ecryptfs_release_messaging();
798 out_destroy_kthread:
799 ecryptfs_destroy_kthread();
800 out_do_sysfs_unregistration:
801 do_sysfs_unregistration();
802 out_unregister_filesystem:
803 unregister_filesystem(&ecryptfs_fs_type);
804 out_free_kmem_caches:
805 ecryptfs_free_kmem_caches();
806 out:
807 return rc;
810 static void __exit ecryptfs_exit(void)
812 int rc;
814 rc = ecryptfs_destroy_crypto();
815 if (rc)
816 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
817 "rc = [%d]\n", rc);
818 ecryptfs_release_messaging();
819 ecryptfs_destroy_kthread();
820 do_sysfs_unregistration();
821 unregister_filesystem(&ecryptfs_fs_type);
822 ecryptfs_free_kmem_caches();
825 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
826 MODULE_DESCRIPTION("eCryptfs");
828 MODULE_LICENSE("GPL");
830 module_init(ecryptfs_init)
831 module_exit(ecryptfs_exit)