initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / fs / ecryptfs / main.c
blobe2f18ada2ec289e22b481e60379cbcaa56b31f50
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 <linux/ima.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 message buffer elements
54 unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
56 module_param(ecryptfs_message_buf_len, uint, 0);
57 MODULE_PARM_DESC(ecryptfs_message_buf_len,
58 "Number of message buffer elements");
60 /**
61 * Module parameter that defines the maximum guaranteed amount of time to wait
62 * for a response from ecryptfsd. The actual sleep time will be, more than
63 * likely, a small amount greater than this specified value, but only less if
64 * the message successfully arrives.
66 signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
68 module_param(ecryptfs_message_wait_timeout, long, 0);
69 MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
70 "Maximum number of seconds that an operation will "
71 "sleep while waiting for a message response from "
72 "userspace");
74 /**
75 * Module parameter that is an estimate of the maximum number of users
76 * that will be concurrently using eCryptfs. Set this to the right
77 * value to balance performance and memory use.
79 unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
81 module_param(ecryptfs_number_of_users, uint, 0);
82 MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
83 "concurrent users of eCryptfs");
85 void __ecryptfs_printk(const char *fmt, ...)
87 va_list args;
88 va_start(args, fmt);
89 if (fmt[1] == '7') { /* KERN_DEBUG */
90 if (ecryptfs_verbosity >= 1)
91 vprintk(fmt, args);
92 } else
93 vprintk(fmt, args);
94 va_end(args);
97 /**
98 * ecryptfs_init_persistent_file
99 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
100 * the lower dentry and the lower mount set
102 * eCryptfs only ever keeps a single open file for every lower
103 * inode. All I/O operations to the lower inode occur through that
104 * file. When the first eCryptfs dentry that interposes with the first
105 * lower dentry for that inode is created, this function creates the
106 * persistent file struct and associates it with the eCryptfs
107 * inode. When the eCryptfs inode is destroyed, the file is closed.
109 * The persistent file will be opened with read/write permissions, if
110 * possible. Otherwise, it is opened read-only.
112 * This function does nothing if a lower persistent file is already
113 * associated with the eCryptfs inode.
115 * Returns zero on success; non-zero otherwise
117 int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
119 const struct cred *cred = current_cred();
120 struct ecryptfs_inode_info *inode_info =
121 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
122 int opened_lower_file = 0;
123 int rc = 0;
125 mutex_lock(&inode_info->lower_file_mutex);
126 if (!inode_info->lower_file) {
127 struct dentry *lower_dentry;
128 struct vfsmount *lower_mnt =
129 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
131 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
132 rc = ecryptfs_privileged_open(&inode_info->lower_file,
133 lower_dentry, lower_mnt, cred);
134 if (rc) {
135 printk(KERN_ERR "Error opening lower persistent file "
136 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
137 "rc = [%d]\n", lower_dentry, lower_mnt, rc);
138 inode_info->lower_file = NULL;
139 } else
140 opened_lower_file = 1;
142 mutex_unlock(&inode_info->lower_file_mutex);
143 if (opened_lower_file)
144 ima_counts_get(inode_info->lower_file);
145 return rc;
149 * ecryptfs_interpose
150 * @lower_dentry: Existing dentry in the lower filesystem
151 * @dentry: ecryptfs' dentry
152 * @sb: ecryptfs's super_block
153 * @flags: flags to govern behavior of interpose procedure
155 * Interposes upper and lower dentries.
157 * Returns zero on success; non-zero otherwise
159 int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
160 struct super_block *sb, u32 flags)
162 struct inode *lower_inode;
163 struct inode *inode;
164 int rc = 0;
166 lower_inode = lower_dentry->d_inode;
167 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
168 rc = -EXDEV;
169 goto out;
171 if (!igrab(lower_inode)) {
172 rc = -ESTALE;
173 goto out;
175 inode = iget5_locked(sb, (unsigned long)lower_inode,
176 ecryptfs_inode_test, ecryptfs_inode_set,
177 lower_inode);
178 if (!inode) {
179 rc = -EACCES;
180 iput(lower_inode);
181 goto out;
183 if (inode->i_state & I_NEW)
184 unlock_new_inode(inode);
185 else
186 iput(lower_inode);
187 if (S_ISLNK(lower_inode->i_mode))
188 inode->i_op = &ecryptfs_symlink_iops;
189 else if (S_ISDIR(lower_inode->i_mode))
190 inode->i_op = &ecryptfs_dir_iops;
191 if (S_ISDIR(lower_inode->i_mode))
192 inode->i_fop = &ecryptfs_dir_fops;
193 if (special_file(lower_inode->i_mode))
194 init_special_inode(inode, lower_inode->i_mode,
195 lower_inode->i_rdev);
196 dentry->d_op = &ecryptfs_dops;
197 fsstack_copy_attr_all(inode, lower_inode, NULL);
198 /* This size will be overwritten for real files w/ headers and
199 * other metadata */
200 fsstack_copy_inode_size(inode, lower_inode);
201 if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
202 d_add(dentry, inode);
203 else
204 d_instantiate(dentry, inode);
205 out:
206 return rc;
209 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
210 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
211 ecryptfs_opt_ecryptfs_key_bytes,
212 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
213 ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
214 ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
215 ecryptfs_opt_unlink_sigs, ecryptfs_opt_check_dev_ruid,
216 ecryptfs_opt_err };
218 static const match_table_t tokens = {
219 {ecryptfs_opt_sig, "sig=%s"},
220 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
221 {ecryptfs_opt_cipher, "cipher=%s"},
222 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
223 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
224 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
225 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
226 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
227 {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
228 {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
229 {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
230 {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
231 {ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
232 {ecryptfs_opt_err, NULL}
235 static int ecryptfs_init_global_auth_toks(
236 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
238 struct ecryptfs_global_auth_tok *global_auth_tok;
239 int rc = 0;
241 list_for_each_entry(global_auth_tok,
242 &mount_crypt_stat->global_auth_tok_list,
243 mount_crypt_stat_list) {
244 rc = ecryptfs_keyring_auth_tok_for_sig(
245 &global_auth_tok->global_auth_tok_key,
246 &global_auth_tok->global_auth_tok,
247 global_auth_tok->sig);
248 if (rc) {
249 printk(KERN_ERR "Could not find valid key in user "
250 "session keyring for sig specified in mount "
251 "option: [%s]\n", global_auth_tok->sig);
252 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
253 goto out;
254 } else
255 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
257 out:
258 return rc;
261 static void ecryptfs_init_mount_crypt_stat(
262 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
264 memset((void *)mount_crypt_stat, 0,
265 sizeof(struct ecryptfs_mount_crypt_stat));
266 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
267 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
268 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
272 * ecryptfs_parse_options
273 * @sb: The ecryptfs super block
274 * @options: The options pased to the kernel
275 * @check_ruid: set to 1 if device uid should be checked against the ruid
277 * Parse mount options:
278 * debug=N - ecryptfs_verbosity level for debug output
279 * sig=XXX - description(signature) of the key to use
281 * Returns the dentry object of the lower-level (lower/interposed)
282 * directory; We want to mount our stackable file system on top of
283 * that lower directory.
285 * The signature of the key to use must be the description of a key
286 * already in the keyring. Mounting will fail if the key can not be
287 * found.
289 * Returns zero on success; non-zero on error
291 static int ecryptfs_parse_options(struct super_block *sb, char *options,
292 uid_t *check_ruid)
294 char *p;
295 int rc = 0;
296 int sig_set = 0;
297 int cipher_name_set = 0;
298 int fn_cipher_name_set = 0;
299 int cipher_key_bytes;
300 int cipher_key_bytes_set = 0;
301 int fn_cipher_key_bytes;
302 int fn_cipher_key_bytes_set = 0;
303 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
304 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
305 substring_t args[MAX_OPT_ARGS];
306 int token;
307 char *sig_src;
308 char *cipher_name_dst;
309 char *cipher_name_src;
310 char *fn_cipher_name_dst;
311 char *fn_cipher_name_src;
312 char *fnek_dst;
313 char *fnek_src;
314 char *cipher_key_bytes_src;
315 char *fn_cipher_key_bytes_src;
317 *check_ruid = 0;
319 if (!options) {
320 rc = -EINVAL;
321 goto out;
323 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
324 while ((p = strsep(&options, ",")) != NULL) {
325 if (!*p)
326 continue;
327 token = match_token(p, tokens, args);
328 switch (token) {
329 case ecryptfs_opt_sig:
330 case ecryptfs_opt_ecryptfs_sig:
331 sig_src = args[0].from;
332 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
333 sig_src, 0);
334 if (rc) {
335 printk(KERN_ERR "Error attempting to register "
336 "global sig; rc = [%d]\n", rc);
337 goto out;
339 sig_set = 1;
340 break;
341 case ecryptfs_opt_cipher:
342 case ecryptfs_opt_ecryptfs_cipher:
343 cipher_name_src = args[0].from;
344 cipher_name_dst =
345 mount_crypt_stat->
346 global_default_cipher_name;
347 strncpy(cipher_name_dst, cipher_name_src,
348 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
349 cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
350 cipher_name_set = 1;
351 break;
352 case ecryptfs_opt_ecryptfs_key_bytes:
353 cipher_key_bytes_src = args[0].from;
354 cipher_key_bytes =
355 (int)simple_strtol(cipher_key_bytes_src,
356 &cipher_key_bytes_src, 0);
357 mount_crypt_stat->global_default_cipher_key_size =
358 cipher_key_bytes;
359 cipher_key_bytes_set = 1;
360 break;
361 case ecryptfs_opt_passthrough:
362 mount_crypt_stat->flags |=
363 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
364 break;
365 case ecryptfs_opt_xattr_metadata:
366 mount_crypt_stat->flags |=
367 ECRYPTFS_XATTR_METADATA_ENABLED;
368 break;
369 case ecryptfs_opt_encrypted_view:
370 mount_crypt_stat->flags |=
371 ECRYPTFS_XATTR_METADATA_ENABLED;
372 mount_crypt_stat->flags |=
373 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
374 break;
375 case ecryptfs_opt_fnek_sig:
376 fnek_src = args[0].from;
377 fnek_dst =
378 mount_crypt_stat->global_default_fnek_sig;
379 strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
380 mount_crypt_stat->global_default_fnek_sig[
381 ECRYPTFS_SIG_SIZE_HEX] = '\0';
382 rc = ecryptfs_add_global_auth_tok(
383 mount_crypt_stat,
384 mount_crypt_stat->global_default_fnek_sig,
385 ECRYPTFS_AUTH_TOK_FNEK);
386 if (rc) {
387 printk(KERN_ERR "Error attempting to register "
388 "global fnek sig [%s]; rc = [%d]\n",
389 mount_crypt_stat->global_default_fnek_sig,
390 rc);
391 goto out;
393 mount_crypt_stat->flags |=
394 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
395 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
396 break;
397 case ecryptfs_opt_fn_cipher:
398 fn_cipher_name_src = args[0].from;
399 fn_cipher_name_dst =
400 mount_crypt_stat->global_default_fn_cipher_name;
401 strncpy(fn_cipher_name_dst, fn_cipher_name_src,
402 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
403 mount_crypt_stat->global_default_fn_cipher_name[
404 ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
405 fn_cipher_name_set = 1;
406 break;
407 case ecryptfs_opt_fn_cipher_key_bytes:
408 fn_cipher_key_bytes_src = args[0].from;
409 fn_cipher_key_bytes =
410 (int)simple_strtol(fn_cipher_key_bytes_src,
411 &fn_cipher_key_bytes_src, 0);
412 mount_crypt_stat->global_default_fn_cipher_key_bytes =
413 fn_cipher_key_bytes;
414 fn_cipher_key_bytes_set = 1;
415 break;
416 case ecryptfs_opt_unlink_sigs:
417 mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
418 break;
419 case ecryptfs_opt_check_dev_ruid:
420 *check_ruid = 1;
421 break;
422 case ecryptfs_opt_err:
423 default:
424 printk(KERN_WARNING
425 "%s: eCryptfs: unrecognized option [%s]\n",
426 __func__, p);
429 if (!sig_set) {
430 rc = -EINVAL;
431 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
432 "auth tok signature as a mount "
433 "parameter; see the eCryptfs README\n");
434 goto out;
436 if (!cipher_name_set) {
437 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
439 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
440 strcpy(mount_crypt_stat->global_default_cipher_name,
441 ECRYPTFS_DEFAULT_CIPHER);
443 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
444 && !fn_cipher_name_set)
445 strcpy(mount_crypt_stat->global_default_fn_cipher_name,
446 mount_crypt_stat->global_default_cipher_name);
447 if (!cipher_key_bytes_set)
448 mount_crypt_stat->global_default_cipher_key_size = 0;
449 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
450 && !fn_cipher_key_bytes_set)
451 mount_crypt_stat->global_default_fn_cipher_key_bytes =
452 mount_crypt_stat->global_default_cipher_key_size;
453 mutex_lock(&key_tfm_list_mutex);
454 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
455 NULL)) {
456 rc = ecryptfs_add_new_key_tfm(
457 NULL, mount_crypt_stat->global_default_cipher_name,
458 mount_crypt_stat->global_default_cipher_key_size);
459 if (rc) {
460 printk(KERN_ERR "Error attempting to initialize "
461 "cipher with name = [%s] and key size = [%td]; "
462 "rc = [%d]\n",
463 mount_crypt_stat->global_default_cipher_name,
464 mount_crypt_stat->global_default_cipher_key_size,
465 rc);
466 rc = -EINVAL;
467 mutex_unlock(&key_tfm_list_mutex);
468 goto out;
471 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
472 && !ecryptfs_tfm_exists(
473 mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
474 rc = ecryptfs_add_new_key_tfm(
475 NULL, mount_crypt_stat->global_default_fn_cipher_name,
476 mount_crypt_stat->global_default_fn_cipher_key_bytes);
477 if (rc) {
478 printk(KERN_ERR "Error attempting to initialize "
479 "cipher with name = [%s] and key size = [%td]; "
480 "rc = [%d]\n",
481 mount_crypt_stat->global_default_fn_cipher_name,
482 mount_crypt_stat->global_default_fn_cipher_key_bytes,
483 rc);
484 rc = -EINVAL;
485 mutex_unlock(&key_tfm_list_mutex);
486 goto out;
489 mutex_unlock(&key_tfm_list_mutex);
490 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
491 if (rc)
492 printk(KERN_WARNING "One or more global auth toks could not "
493 "properly register; rc = [%d]\n", rc);
494 out:
495 return rc;
498 struct kmem_cache *ecryptfs_sb_info_cache;
499 static struct file_system_type ecryptfs_fs_type;
502 * ecryptfs_fill_super
503 * @sb: The ecryptfs super block
504 * @raw_data: The options passed to mount
505 * @silent: Not used but required by function prototype
507 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
509 * Returns zero on success; non-zero otherwise
511 static int
512 ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
514 int rc = 0;
516 /* Released in ecryptfs_put_super() */
517 ecryptfs_set_superblock_private(sb,
518 kmem_cache_zalloc(ecryptfs_sb_info_cache,
519 GFP_KERNEL));
520 if (!ecryptfs_superblock_to_private(sb)) {
521 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
522 rc = -ENOMEM;
523 goto out;
525 sb->s_op = &ecryptfs_sops;
526 /* Released through deactivate_super(sb) from get_sb_nodev */
527 sb->s_root = d_alloc(NULL, &(const struct qstr) {
528 .hash = 0,.name = "/",.len = 1});
529 if (!sb->s_root) {
530 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
531 rc = -ENOMEM;
532 goto out;
534 sb->s_root->d_op = &ecryptfs_dops;
535 sb->s_root->d_sb = sb;
536 sb->s_root->d_parent = sb->s_root;
537 /* Released in d_release when dput(sb->s_root) is called */
538 /* through deactivate_super(sb) from get_sb_nodev() */
539 ecryptfs_set_dentry_private(sb->s_root,
540 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
541 GFP_KERNEL));
542 if (!ecryptfs_dentry_to_private(sb->s_root)) {
543 ecryptfs_printk(KERN_ERR,
544 "dentry_info_cache alloc failed\n");
545 rc = -ENOMEM;
546 goto out;
548 rc = 0;
549 out:
550 /* Should be able to rely on deactivate_super called from
551 * get_sb_nodev */
552 return rc;
556 * ecryptfs_read_super
557 * @sb: The ecryptfs super block
558 * @dev_name: The path to mount over
560 * Read the super block of the lower filesystem, and use
561 * ecryptfs_interpose to create our initial inode and super block
562 * struct.
564 static int ecryptfs_read_super(struct super_block *sb, const char *dev_name,
565 uid_t check_ruid)
567 struct path path;
568 int rc;
570 rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
571 if (rc) {
572 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
573 goto out;
575 if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
576 rc = -EINVAL;
577 printk(KERN_ERR "Mount on filesystem of type "
578 "eCryptfs explicitly disallowed due to "
579 "known incompatibilities\n");
580 goto out_free;
583 if (check_ruid && path.dentry->d_inode->i_uid != current_uid()) {
584 rc = -EPERM;
585 printk(KERN_ERR "Mount of device (uid: %d) not owned by "
586 "requested user (uid: %d)\n",
587 path.dentry->d_inode->i_uid, current_uid());
588 goto out_free;
591 ecryptfs_set_superblock_lower(sb, path.dentry->d_sb);
592 sb->s_maxbytes = path.dentry->d_sb->s_maxbytes;
593 sb->s_blocksize = path.dentry->d_sb->s_blocksize;
594 ecryptfs_set_dentry_lower(sb->s_root, path.dentry);
595 ecryptfs_set_dentry_lower_mnt(sb->s_root, path.mnt);
596 rc = ecryptfs_interpose(path.dentry, sb->s_root, sb, 0);
597 if (rc)
598 goto out_free;
599 rc = 0;
600 goto out;
601 out_free:
602 path_put(&path);
603 out:
604 return rc;
608 * ecryptfs_get_sb
609 * @fs_type
610 * @flags
611 * @dev_name: The path to mount over
612 * @raw_data: The options passed into the kernel
614 * The whole ecryptfs_get_sb process is broken into 4 functions:
615 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
616 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
617 * with as much information as it can before needing
618 * the lower filesystem.
619 * ecryptfs_read_super(): this accesses the lower filesystem and uses
620 * ecryptfs_interpolate to perform most of the linking
621 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
623 static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
624 const char *dev_name, void *raw_data,
625 struct vfsmount *mnt)
627 int rc;
628 struct super_block *sb;
629 uid_t check_ruid;
631 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
632 if (rc < 0) {
633 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
634 goto out;
636 sb = mnt->mnt_sb;
637 rc = ecryptfs_parse_options(sb, raw_data, &check_ruid);
638 if (rc) {
639 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
640 goto out_abort;
642 rc = ecryptfs_read_super(sb, dev_name, check_ruid);
643 if (rc) {
644 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
645 goto out_abort;
647 goto out;
648 out_abort:
649 dput(sb->s_root); /* aka mnt->mnt_root, as set by get_sb_nodev() */
650 deactivate_locked_super(sb);
651 out:
652 return rc;
656 * ecryptfs_kill_block_super
657 * @sb: The ecryptfs super block
659 * Used to bring the superblock down and free the private data.
660 * Private data is free'd in ecryptfs_put_super()
662 static void ecryptfs_kill_block_super(struct super_block *sb)
664 generic_shutdown_super(sb);
667 static struct file_system_type ecryptfs_fs_type = {
668 .owner = THIS_MODULE,
669 .name = "ecryptfs",
670 .get_sb = ecryptfs_get_sb,
671 .kill_sb = ecryptfs_kill_block_super,
672 .fs_flags = 0
676 * inode_info_init_once
678 * Initializes the ecryptfs_inode_info_cache when it is created
680 static void
681 inode_info_init_once(void *vptr)
683 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
685 inode_init_once(&ei->vfs_inode);
688 static struct ecryptfs_cache_info {
689 struct kmem_cache **cache;
690 const char *name;
691 size_t size;
692 void (*ctor)(void *obj);
693 } ecryptfs_cache_infos[] = {
695 .cache = &ecryptfs_auth_tok_list_item_cache,
696 .name = "ecryptfs_auth_tok_list_item",
697 .size = sizeof(struct ecryptfs_auth_tok_list_item),
700 .cache = &ecryptfs_file_info_cache,
701 .name = "ecryptfs_file_cache",
702 .size = sizeof(struct ecryptfs_file_info),
705 .cache = &ecryptfs_dentry_info_cache,
706 .name = "ecryptfs_dentry_info_cache",
707 .size = sizeof(struct ecryptfs_dentry_info),
710 .cache = &ecryptfs_inode_info_cache,
711 .name = "ecryptfs_inode_cache",
712 .size = sizeof(struct ecryptfs_inode_info),
713 .ctor = inode_info_init_once,
716 .cache = &ecryptfs_sb_info_cache,
717 .name = "ecryptfs_sb_cache",
718 .size = sizeof(struct ecryptfs_sb_info),
721 .cache = &ecryptfs_header_cache_1,
722 .name = "ecryptfs_headers_1",
723 .size = PAGE_CACHE_SIZE,
726 .cache = &ecryptfs_header_cache_2,
727 .name = "ecryptfs_headers_2",
728 .size = PAGE_CACHE_SIZE,
731 .cache = &ecryptfs_xattr_cache,
732 .name = "ecryptfs_xattr_cache",
733 .size = PAGE_CACHE_SIZE,
736 .cache = &ecryptfs_key_record_cache,
737 .name = "ecryptfs_key_record_cache",
738 .size = sizeof(struct ecryptfs_key_record),
741 .cache = &ecryptfs_key_sig_cache,
742 .name = "ecryptfs_key_sig_cache",
743 .size = sizeof(struct ecryptfs_key_sig),
746 .cache = &ecryptfs_global_auth_tok_cache,
747 .name = "ecryptfs_global_auth_tok_cache",
748 .size = sizeof(struct ecryptfs_global_auth_tok),
751 .cache = &ecryptfs_key_tfm_cache,
752 .name = "ecryptfs_key_tfm_cache",
753 .size = sizeof(struct ecryptfs_key_tfm),
756 .cache = &ecryptfs_open_req_cache,
757 .name = "ecryptfs_open_req_cache",
758 .size = sizeof(struct ecryptfs_open_req),
762 static void ecryptfs_free_kmem_caches(void)
764 int i;
766 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
767 struct ecryptfs_cache_info *info;
769 info = &ecryptfs_cache_infos[i];
770 if (*(info->cache))
771 kmem_cache_destroy(*(info->cache));
776 * ecryptfs_init_kmem_caches
778 * Returns zero on success; non-zero otherwise
780 static int ecryptfs_init_kmem_caches(void)
782 int i;
784 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
785 struct ecryptfs_cache_info *info;
787 info = &ecryptfs_cache_infos[i];
788 *(info->cache) = kmem_cache_create(info->name, info->size,
789 0, SLAB_HWCACHE_ALIGN, info->ctor);
790 if (!*(info->cache)) {
791 ecryptfs_free_kmem_caches();
792 ecryptfs_printk(KERN_WARNING, "%s: "
793 "kmem_cache_create failed\n",
794 info->name);
795 return -ENOMEM;
798 return 0;
801 static struct kobject *ecryptfs_kobj;
803 static ssize_t version_show(struct kobject *kobj,
804 struct kobj_attribute *attr, char *buff)
806 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
809 static struct kobj_attribute version_attr = __ATTR_RO(version);
811 static struct attribute *attributes[] = {
812 &version_attr.attr,
813 NULL,
816 static struct attribute_group attr_group = {
817 .attrs = attributes,
820 static int do_sysfs_registration(void)
822 int rc;
824 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
825 if (!ecryptfs_kobj) {
826 printk(KERN_ERR "Unable to create ecryptfs kset\n");
827 rc = -ENOMEM;
828 goto out;
830 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
831 if (rc) {
832 printk(KERN_ERR
833 "Unable to create ecryptfs version attributes\n");
834 kobject_put(ecryptfs_kobj);
836 out:
837 return rc;
840 static void do_sysfs_unregistration(void)
842 sysfs_remove_group(ecryptfs_kobj, &attr_group);
843 kobject_put(ecryptfs_kobj);
846 static int __init ecryptfs_init(void)
848 int rc;
850 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
851 rc = -EINVAL;
852 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
853 "larger than the host's page size, and so "
854 "eCryptfs cannot run on this system. The "
855 "default eCryptfs extent size is [%d] bytes; "
856 "the page size is [%d] bytes.\n",
857 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
858 goto out;
860 rc = ecryptfs_init_kmem_caches();
861 if (rc) {
862 printk(KERN_ERR
863 "Failed to allocate one or more kmem_cache objects\n");
864 goto out;
866 rc = register_filesystem(&ecryptfs_fs_type);
867 if (rc) {
868 printk(KERN_ERR "Failed to register filesystem\n");
869 goto out_free_kmem_caches;
871 rc = do_sysfs_registration();
872 if (rc) {
873 printk(KERN_ERR "sysfs registration failed\n");
874 goto out_unregister_filesystem;
876 rc = ecryptfs_init_kthread();
877 if (rc) {
878 printk(KERN_ERR "%s: kthread initialization failed; "
879 "rc = [%d]\n", __func__, rc);
880 goto out_do_sysfs_unregistration;
882 rc = ecryptfs_init_messaging();
883 if (rc) {
884 printk(KERN_ERR "Failure occured while attempting to "
885 "initialize the communications channel to "
886 "ecryptfsd\n");
887 goto out_destroy_kthread;
889 rc = ecryptfs_init_crypto();
890 if (rc) {
891 printk(KERN_ERR "Failure whilst attempting to init crypto; "
892 "rc = [%d]\n", rc);
893 goto out_release_messaging;
895 if (ecryptfs_verbosity > 0)
896 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
897 "will be written to the syslog!\n", ecryptfs_verbosity);
899 goto out;
900 out_release_messaging:
901 ecryptfs_release_messaging();
902 out_destroy_kthread:
903 ecryptfs_destroy_kthread();
904 out_do_sysfs_unregistration:
905 do_sysfs_unregistration();
906 out_unregister_filesystem:
907 unregister_filesystem(&ecryptfs_fs_type);
908 out_free_kmem_caches:
909 ecryptfs_free_kmem_caches();
910 out:
911 return rc;
914 static void __exit ecryptfs_exit(void)
916 int rc;
918 rc = ecryptfs_destroy_crypto();
919 if (rc)
920 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
921 "rc = [%d]\n", rc);
922 ecryptfs_release_messaging();
923 ecryptfs_destroy_kthread();
924 do_sysfs_unregistration();
925 unregister_filesystem(&ecryptfs_fs_type);
926 ecryptfs_free_kmem_caches();
929 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
930 MODULE_DESCRIPTION("eCryptfs");
932 MODULE_LICENSE("GPL");
934 module_init(ecryptfs_init)
935 module_exit(ecryptfs_exit)