Merge branches 'x86/cleanups', 'x86/mm', 'x86/setup' and 'linus' into x86/core
[linux-2.6] / fs / ecryptfs / main.c
1 /**
2  * eCryptfs: Linux filesystem encryption layer
3  *
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>
10  *
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.
15  *
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.
20  *
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.
25  */
26
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"
39
40 /**
41  * Module parameter that defines the ecryptfs_verbosity level.
42  */
43 int ecryptfs_verbosity = 0;
44
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)");
49
50 /**
51  * Module parameter that defines the number of message buffer elements
52  */
53 unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
54
55 module_param(ecryptfs_message_buf_len, uint, 0);
56 MODULE_PARM_DESC(ecryptfs_message_buf_len,
57                  "Number of message buffer elements");
58
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.
64  */
65 signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
66
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");
72
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.
77  */
78 unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
79
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");
83
84 void __ecryptfs_printk(const char *fmt, ...)
85 {
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);
94 }
95
96 /**
97  * ecryptfs_init_persistent_file
98  * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
99  *                   the lower dentry and the lower mount set
100  *
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.
107  *
108  * The persistent file will be opened with read/write permissions, if
109  * possible. Otherwise, it is opened read-only.
110  *
111  * This function does nothing if a lower persistent file is already
112  * associated with the eCryptfs inode.
113  *
114  * Returns zero on success; non-zero otherwise
115  */
116 int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
117 {
118         const struct cred *cred = current_cred();
119         struct ecryptfs_inode_info *inode_info =
120                 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
121         int rc = 0;
122
123         mutex_lock(&inode_info->lower_file_mutex);
124         if (!inode_info->lower_file) {
125                 struct dentry *lower_dentry;
126                 struct vfsmount *lower_mnt =
127                         ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
128
129                 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
130                 rc = ecryptfs_privileged_open(&inode_info->lower_file,
131                                               lower_dentry, lower_mnt, cred);
132                 if (rc || IS_ERR(inode_info->lower_file)) {
133                         printk(KERN_ERR "Error opening lower persistent file "
134                                "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
135                                "rc = [%d]\n", lower_dentry, lower_mnt, rc);
136                         rc = PTR_ERR(inode_info->lower_file);
137                         inode_info->lower_file = NULL;
138                 }
139         }
140         mutex_unlock(&inode_info->lower_file_mutex);
141         return rc;
142 }
143
144 /**
145  * ecryptfs_interpose
146  * @lower_dentry: Existing dentry in the lower filesystem
147  * @dentry: ecryptfs' dentry
148  * @sb: ecryptfs's super_block
149  * @flags: flags to govern behavior of interpose procedure
150  *
151  * Interposes upper and lower dentries.
152  *
153  * Returns zero on success; non-zero otherwise
154  */
155 int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
156                        struct super_block *sb, u32 flags)
157 {
158         struct inode *lower_inode;
159         struct inode *inode;
160         int rc = 0;
161
162         lower_inode = lower_dentry->d_inode;
163         if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
164                 rc = -EXDEV;
165                 goto out;
166         }
167         if (!igrab(lower_inode)) {
168                 rc = -ESTALE;
169                 goto out;
170         }
171         inode = iget5_locked(sb, (unsigned long)lower_inode,
172                              ecryptfs_inode_test, ecryptfs_inode_set,
173                              lower_inode);
174         if (!inode) {
175                 rc = -EACCES;
176                 iput(lower_inode);
177                 goto out;
178         }
179         if (inode->i_state & I_NEW)
180                 unlock_new_inode(inode);
181         else
182                 iput(lower_inode);
183         if (S_ISLNK(lower_inode->i_mode))
184                 inode->i_op = &ecryptfs_symlink_iops;
185         else if (S_ISDIR(lower_inode->i_mode))
186                 inode->i_op = &ecryptfs_dir_iops;
187         if (S_ISDIR(lower_inode->i_mode))
188                 inode->i_fop = &ecryptfs_dir_fops;
189         if (special_file(lower_inode->i_mode))
190                 init_special_inode(inode, lower_inode->i_mode,
191                                    lower_inode->i_rdev);
192         dentry->d_op = &ecryptfs_dops;
193         if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
194                 d_add(dentry, inode);
195         else
196                 d_instantiate(dentry, inode);
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 out:
202         return rc;
203 }
204
205 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
206        ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
207        ecryptfs_opt_ecryptfs_key_bytes,
208        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
209        ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
210        ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
211        ecryptfs_opt_err };
212
213 static const match_table_t tokens = {
214         {ecryptfs_opt_sig, "sig=%s"},
215         {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
216         {ecryptfs_opt_cipher, "cipher=%s"},
217         {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
218         {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
219         {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
220         {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
221         {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
222         {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
223         {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
224         {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
225         {ecryptfs_opt_err, NULL}
226 };
227
228 static int ecryptfs_init_global_auth_toks(
229         struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
230 {
231         struct ecryptfs_global_auth_tok *global_auth_tok;
232         int rc = 0;
233
234         list_for_each_entry(global_auth_tok,
235                             &mount_crypt_stat->global_auth_tok_list,
236                             mount_crypt_stat_list) {
237                 rc = ecryptfs_keyring_auth_tok_for_sig(
238                         &global_auth_tok->global_auth_tok_key,
239                         &global_auth_tok->global_auth_tok,
240                         global_auth_tok->sig);
241                 if (rc) {
242                         printk(KERN_ERR "Could not find valid key in user "
243                                "session keyring for sig specified in mount "
244                                "option: [%s]\n", global_auth_tok->sig);
245                         global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
246                         goto out;
247                 } else
248                         global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
249         }
250 out:
251         return rc;
252 }
253
254 static void ecryptfs_init_mount_crypt_stat(
255         struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
256 {
257         memset((void *)mount_crypt_stat, 0,
258                sizeof(struct ecryptfs_mount_crypt_stat));
259         INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
260         mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
261         mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
262 }
263
264 /**
265  * ecryptfs_parse_options
266  * @sb: The ecryptfs super block
267  * @options: The options pased to the kernel
268  *
269  * Parse mount options:
270  * debug=N         - ecryptfs_verbosity level for debug output
271  * sig=XXX         - description(signature) of the key to use
272  *
273  * Returns the dentry object of the lower-level (lower/interposed)
274  * directory; We want to mount our stackable file system on top of
275  * that lower directory.
276  *
277  * The signature of the key to use must be the description of a key
278  * already in the keyring. Mounting will fail if the key can not be
279  * found.
280  *
281  * Returns zero on success; non-zero on error
282  */
283 static int ecryptfs_parse_options(struct super_block *sb, char *options)
284 {
285         char *p;
286         int rc = 0;
287         int sig_set = 0;
288         int cipher_name_set = 0;
289         int fn_cipher_name_set = 0;
290         int cipher_key_bytes;
291         int cipher_key_bytes_set = 0;
292         int fn_cipher_key_bytes;
293         int fn_cipher_key_bytes_set = 0;
294         struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
295                 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
296         substring_t args[MAX_OPT_ARGS];
297         int token;
298         char *sig_src;
299         char *cipher_name_dst;
300         char *cipher_name_src;
301         char *fn_cipher_name_dst;
302         char *fn_cipher_name_src;
303         char *fnek_dst;
304         char *fnek_src;
305         char *cipher_key_bytes_src;
306         char *fn_cipher_key_bytes_src;
307
308         if (!options) {
309                 rc = -EINVAL;
310                 goto out;
311         }
312         ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
313         while ((p = strsep(&options, ",")) != NULL) {
314                 if (!*p)
315                         continue;
316                 token = match_token(p, tokens, args);
317                 switch (token) {
318                 case ecryptfs_opt_sig:
319                 case ecryptfs_opt_ecryptfs_sig:
320                         sig_src = args[0].from;
321                         rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
322                                                           sig_src, 0);
323                         if (rc) {
324                                 printk(KERN_ERR "Error attempting to register "
325                                        "global sig; rc = [%d]\n", rc);
326                                 goto out;
327                         }
328                         sig_set = 1;
329                         break;
330                 case ecryptfs_opt_cipher:
331                 case ecryptfs_opt_ecryptfs_cipher:
332                         cipher_name_src = args[0].from;
333                         cipher_name_dst =
334                                 mount_crypt_stat->
335                                 global_default_cipher_name;
336                         strncpy(cipher_name_dst, cipher_name_src,
337                                 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
338                         cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
339                         cipher_name_set = 1;
340                         break;
341                 case ecryptfs_opt_ecryptfs_key_bytes:
342                         cipher_key_bytes_src = args[0].from;
343                         cipher_key_bytes =
344                                 (int)simple_strtol(cipher_key_bytes_src,
345                                                    &cipher_key_bytes_src, 0);
346                         mount_crypt_stat->global_default_cipher_key_size =
347                                 cipher_key_bytes;
348                         cipher_key_bytes_set = 1;
349                         break;
350                 case ecryptfs_opt_passthrough:
351                         mount_crypt_stat->flags |=
352                                 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
353                         break;
354                 case ecryptfs_opt_xattr_metadata:
355                         mount_crypt_stat->flags |=
356                                 ECRYPTFS_XATTR_METADATA_ENABLED;
357                         break;
358                 case ecryptfs_opt_encrypted_view:
359                         mount_crypt_stat->flags |=
360                                 ECRYPTFS_XATTR_METADATA_ENABLED;
361                         mount_crypt_stat->flags |=
362                                 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
363                         break;
364                 case ecryptfs_opt_fnek_sig:
365                         fnek_src = args[0].from;
366                         fnek_dst =
367                                 mount_crypt_stat->global_default_fnek_sig;
368                         strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
369                         mount_crypt_stat->global_default_fnek_sig[
370                                 ECRYPTFS_SIG_SIZE_HEX] = '\0';
371                         rc = ecryptfs_add_global_auth_tok(
372                                 mount_crypt_stat,
373                                 mount_crypt_stat->global_default_fnek_sig,
374                                 ECRYPTFS_AUTH_TOK_FNEK);
375                         if (rc) {
376                                 printk(KERN_ERR "Error attempting to register "
377                                        "global fnek sig [%s]; rc = [%d]\n",
378                                        mount_crypt_stat->global_default_fnek_sig,
379                                        rc);
380                                 goto out;
381                         }
382                         mount_crypt_stat->flags |=
383                                 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
384                                  | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
385                         break;
386                 case ecryptfs_opt_fn_cipher:
387                         fn_cipher_name_src = args[0].from;
388                         fn_cipher_name_dst =
389                                 mount_crypt_stat->global_default_fn_cipher_name;
390                         strncpy(fn_cipher_name_dst, fn_cipher_name_src,
391                                 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
392                         mount_crypt_stat->global_default_fn_cipher_name[
393                                 ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
394                         fn_cipher_name_set = 1;
395                         break;
396                 case ecryptfs_opt_fn_cipher_key_bytes:
397                         fn_cipher_key_bytes_src = args[0].from;
398                         fn_cipher_key_bytes =
399                                 (int)simple_strtol(fn_cipher_key_bytes_src,
400                                                    &fn_cipher_key_bytes_src, 0);
401                         mount_crypt_stat->global_default_fn_cipher_key_bytes =
402                                 fn_cipher_key_bytes;
403                         fn_cipher_key_bytes_set = 1;
404                         break;
405                 case ecryptfs_opt_err:
406                 default:
407                         printk(KERN_WARNING
408                                "%s: eCryptfs: unrecognized option [%s]\n",
409                                __func__, p);
410                 }
411         }
412         if (!sig_set) {
413                 rc = -EINVAL;
414                 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
415                                 "auth tok signature as a mount "
416                                 "parameter; see the eCryptfs README\n");
417                 goto out;
418         }
419         if (!cipher_name_set) {
420                 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
421
422                 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
423                 strcpy(mount_crypt_stat->global_default_cipher_name,
424                        ECRYPTFS_DEFAULT_CIPHER);
425         }
426         if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
427             && !fn_cipher_name_set)
428                 strcpy(mount_crypt_stat->global_default_fn_cipher_name,
429                        mount_crypt_stat->global_default_cipher_name);
430         if (!cipher_key_bytes_set)
431                 mount_crypt_stat->global_default_cipher_key_size = 0;
432         if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
433             && !fn_cipher_key_bytes_set)
434                 mount_crypt_stat->global_default_fn_cipher_key_bytes =
435                         mount_crypt_stat->global_default_cipher_key_size;
436         mutex_lock(&key_tfm_list_mutex);
437         if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
438                                  NULL)) {
439                 rc = ecryptfs_add_new_key_tfm(
440                         NULL, mount_crypt_stat->global_default_cipher_name,
441                         mount_crypt_stat->global_default_cipher_key_size);
442                 if (rc) {
443                         printk(KERN_ERR "Error attempting to initialize "
444                                "cipher with name = [%s] and key size = [%td]; "
445                                "rc = [%d]\n",
446                                mount_crypt_stat->global_default_cipher_name,
447                                mount_crypt_stat->global_default_cipher_key_size,
448                                rc);
449                         rc = -EINVAL;
450                         mutex_unlock(&key_tfm_list_mutex);
451                         goto out;
452                 }
453         }
454         if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
455             && !ecryptfs_tfm_exists(
456                     mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
457                 rc = ecryptfs_add_new_key_tfm(
458                         NULL, mount_crypt_stat->global_default_fn_cipher_name,
459                         mount_crypt_stat->global_default_fn_cipher_key_bytes);
460                 if (rc) {
461                         printk(KERN_ERR "Error attempting to initialize "
462                                "cipher with name = [%s] and key size = [%td]; "
463                                "rc = [%d]\n",
464                                mount_crypt_stat->global_default_fn_cipher_name,
465                                mount_crypt_stat->global_default_fn_cipher_key_bytes,
466                                rc);
467                         rc = -EINVAL;
468                         mutex_unlock(&key_tfm_list_mutex);
469                         goto out;
470                 }
471         }
472         mutex_unlock(&key_tfm_list_mutex);
473         rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
474         if (rc)
475                 printk(KERN_WARNING "One or more global auth toks could not "
476                        "properly register; rc = [%d]\n", rc);
477 out:
478         return rc;
479 }
480
481 struct kmem_cache *ecryptfs_sb_info_cache;
482
483 /**
484  * ecryptfs_fill_super
485  * @sb: The ecryptfs super block
486  * @raw_data: The options passed to mount
487  * @silent: Not used but required by function prototype
488  *
489  * Sets up what we can of the sb, rest is done in ecryptfs_read_super
490  *
491  * Returns zero on success; non-zero otherwise
492  */
493 static int
494 ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
495 {
496         int rc = 0;
497
498         /* Released in ecryptfs_put_super() */
499         ecryptfs_set_superblock_private(sb,
500                                         kmem_cache_zalloc(ecryptfs_sb_info_cache,
501                                                          GFP_KERNEL));
502         if (!ecryptfs_superblock_to_private(sb)) {
503                 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
504                 rc = -ENOMEM;
505                 goto out;
506         }
507         sb->s_op = &ecryptfs_sops;
508         /* Released through deactivate_super(sb) from get_sb_nodev */
509         sb->s_root = d_alloc(NULL, &(const struct qstr) {
510                              .hash = 0,.name = "/",.len = 1});
511         if (!sb->s_root) {
512                 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
513                 rc = -ENOMEM;
514                 goto out;
515         }
516         sb->s_root->d_op = &ecryptfs_dops;
517         sb->s_root->d_sb = sb;
518         sb->s_root->d_parent = sb->s_root;
519         /* Released in d_release when dput(sb->s_root) is called */
520         /* through deactivate_super(sb) from get_sb_nodev() */
521         ecryptfs_set_dentry_private(sb->s_root,
522                                     kmem_cache_zalloc(ecryptfs_dentry_info_cache,
523                                                      GFP_KERNEL));
524         if (!ecryptfs_dentry_to_private(sb->s_root)) {
525                 ecryptfs_printk(KERN_ERR,
526                                 "dentry_info_cache alloc failed\n");
527                 rc = -ENOMEM;
528                 goto out;
529         }
530         rc = 0;
531 out:
532         /* Should be able to rely on deactivate_super called from
533          * get_sb_nodev */
534         return rc;
535 }
536
537 /**
538  * ecryptfs_read_super
539  * @sb: The ecryptfs super block
540  * @dev_name: The path to mount over
541  *
542  * Read the super block of the lower filesystem, and use
543  * ecryptfs_interpose to create our initial inode and super block
544  * struct.
545  */
546 static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
547 {
548         struct path path;
549         int rc;
550
551         rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
552         if (rc) {
553                 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
554                 goto out;
555         }
556         ecryptfs_set_superblock_lower(sb, path.dentry->d_sb);
557         sb->s_maxbytes = path.dentry->d_sb->s_maxbytes;
558         sb->s_blocksize = path.dentry->d_sb->s_blocksize;
559         ecryptfs_set_dentry_lower(sb->s_root, path.dentry);
560         ecryptfs_set_dentry_lower_mnt(sb->s_root, path.mnt);
561         rc = ecryptfs_interpose(path.dentry, sb->s_root, sb, 0);
562         if (rc)
563                 goto out_free;
564         rc = 0;
565         goto out;
566 out_free:
567         path_put(&path);
568 out:
569         return rc;
570 }
571
572 /**
573  * ecryptfs_get_sb
574  * @fs_type
575  * @flags
576  * @dev_name: The path to mount over
577  * @raw_data: The options passed into the kernel
578  *
579  * The whole ecryptfs_get_sb process is broken into 4 functions:
580  * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
581  * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
582  *                        with as much information as it can before needing
583  *                        the lower filesystem.
584  * ecryptfs_read_super(): this accesses the lower filesystem and uses
585  *                        ecryptfs_interpolate to perform most of the linking
586  * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
587  */
588 static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
589                         const char *dev_name, void *raw_data,
590                         struct vfsmount *mnt)
591 {
592         int rc;
593         struct super_block *sb;
594
595         rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
596         if (rc < 0) {
597                 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
598                 goto out;
599         }
600         sb = mnt->mnt_sb;
601         rc = ecryptfs_parse_options(sb, raw_data);
602         if (rc) {
603                 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
604                 goto out_abort;
605         }
606         rc = ecryptfs_read_super(sb, dev_name);
607         if (rc) {
608                 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
609                 goto out_abort;
610         }
611         goto out;
612 out_abort:
613         dput(sb->s_root);
614         up_write(&sb->s_umount);
615         deactivate_super(sb);
616 out:
617         return rc;
618 }
619
620 /**
621  * ecryptfs_kill_block_super
622  * @sb: The ecryptfs super block
623  *
624  * Used to bring the superblock down and free the private data.
625  * Private data is free'd in ecryptfs_put_super()
626  */
627 static void ecryptfs_kill_block_super(struct super_block *sb)
628 {
629         generic_shutdown_super(sb);
630 }
631
632 static struct file_system_type ecryptfs_fs_type = {
633         .owner = THIS_MODULE,
634         .name = "ecryptfs",
635         .get_sb = ecryptfs_get_sb,
636         .kill_sb = ecryptfs_kill_block_super,
637         .fs_flags = 0
638 };
639
640 /**
641  * inode_info_init_once
642  *
643  * Initializes the ecryptfs_inode_info_cache when it is created
644  */
645 static void
646 inode_info_init_once(void *vptr)
647 {
648         struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
649
650         inode_init_once(&ei->vfs_inode);
651 }
652
653 static struct ecryptfs_cache_info {
654         struct kmem_cache **cache;
655         const char *name;
656         size_t size;
657         void (*ctor)(void *obj);
658 } ecryptfs_cache_infos[] = {
659         {
660                 .cache = &ecryptfs_auth_tok_list_item_cache,
661                 .name = "ecryptfs_auth_tok_list_item",
662                 .size = sizeof(struct ecryptfs_auth_tok_list_item),
663         },
664         {
665                 .cache = &ecryptfs_file_info_cache,
666                 .name = "ecryptfs_file_cache",
667                 .size = sizeof(struct ecryptfs_file_info),
668         },
669         {
670                 .cache = &ecryptfs_dentry_info_cache,
671                 .name = "ecryptfs_dentry_info_cache",
672                 .size = sizeof(struct ecryptfs_dentry_info),
673         },
674         {
675                 .cache = &ecryptfs_inode_info_cache,
676                 .name = "ecryptfs_inode_cache",
677                 .size = sizeof(struct ecryptfs_inode_info),
678                 .ctor = inode_info_init_once,
679         },
680         {
681                 .cache = &ecryptfs_sb_info_cache,
682                 .name = "ecryptfs_sb_cache",
683                 .size = sizeof(struct ecryptfs_sb_info),
684         },
685         {
686                 .cache = &ecryptfs_header_cache_1,
687                 .name = "ecryptfs_headers_1",
688                 .size = PAGE_CACHE_SIZE,
689         },
690         {
691                 .cache = &ecryptfs_header_cache_2,
692                 .name = "ecryptfs_headers_2",
693                 .size = PAGE_CACHE_SIZE,
694         },
695         {
696                 .cache = &ecryptfs_xattr_cache,
697                 .name = "ecryptfs_xattr_cache",
698                 .size = PAGE_CACHE_SIZE,
699         },
700         {
701                 .cache = &ecryptfs_key_record_cache,
702                 .name = "ecryptfs_key_record_cache",
703                 .size = sizeof(struct ecryptfs_key_record),
704         },
705         {
706                 .cache = &ecryptfs_key_sig_cache,
707                 .name = "ecryptfs_key_sig_cache",
708                 .size = sizeof(struct ecryptfs_key_sig),
709         },
710         {
711                 .cache = &ecryptfs_global_auth_tok_cache,
712                 .name = "ecryptfs_global_auth_tok_cache",
713                 .size = sizeof(struct ecryptfs_global_auth_tok),
714         },
715         {
716                 .cache = &ecryptfs_key_tfm_cache,
717                 .name = "ecryptfs_key_tfm_cache",
718                 .size = sizeof(struct ecryptfs_key_tfm),
719         },
720         {
721                 .cache = &ecryptfs_open_req_cache,
722                 .name = "ecryptfs_open_req_cache",
723                 .size = sizeof(struct ecryptfs_open_req),
724         },
725 };
726
727 static void ecryptfs_free_kmem_caches(void)
728 {
729         int i;
730
731         for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
732                 struct ecryptfs_cache_info *info;
733
734                 info = &ecryptfs_cache_infos[i];
735                 if (*(info->cache))
736                         kmem_cache_destroy(*(info->cache));
737         }
738 }
739
740 /**
741  * ecryptfs_init_kmem_caches
742  *
743  * Returns zero on success; non-zero otherwise
744  */
745 static int ecryptfs_init_kmem_caches(void)
746 {
747         int i;
748
749         for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
750                 struct ecryptfs_cache_info *info;
751
752                 info = &ecryptfs_cache_infos[i];
753                 *(info->cache) = kmem_cache_create(info->name, info->size,
754                                 0, SLAB_HWCACHE_ALIGN, info->ctor);
755                 if (!*(info->cache)) {
756                         ecryptfs_free_kmem_caches();
757                         ecryptfs_printk(KERN_WARNING, "%s: "
758                                         "kmem_cache_create failed\n",
759                                         info->name);
760                         return -ENOMEM;
761                 }
762         }
763         return 0;
764 }
765
766 static struct kobject *ecryptfs_kobj;
767
768 static ssize_t version_show(struct kobject *kobj,
769                             struct kobj_attribute *attr, char *buff)
770 {
771         return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
772 }
773
774 static struct kobj_attribute version_attr = __ATTR_RO(version);
775
776 static struct attribute *attributes[] = {
777         &version_attr.attr,
778         NULL,
779 };
780
781 static struct attribute_group attr_group = {
782         .attrs = attributes,
783 };
784
785 static int do_sysfs_registration(void)
786 {
787         int rc;
788
789         ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
790         if (!ecryptfs_kobj) {
791                 printk(KERN_ERR "Unable to create ecryptfs kset\n");
792                 rc = -ENOMEM;
793                 goto out;
794         }
795         rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
796         if (rc) {
797                 printk(KERN_ERR
798                        "Unable to create ecryptfs version attributes\n");
799                 kobject_put(ecryptfs_kobj);
800         }
801 out:
802         return rc;
803 }
804
805 static void do_sysfs_unregistration(void)
806 {
807         sysfs_remove_group(ecryptfs_kobj, &attr_group);
808         kobject_put(ecryptfs_kobj);
809 }
810
811 static int __init ecryptfs_init(void)
812 {
813         int rc;
814
815         if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
816                 rc = -EINVAL;
817                 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
818                                 "larger than the host's page size, and so "
819                                 "eCryptfs cannot run on this system. The "
820                                 "default eCryptfs extent size is [%d] bytes; "
821                                 "the page size is [%d] bytes.\n",
822                                 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
823                 goto out;
824         }
825         rc = ecryptfs_init_kmem_caches();
826         if (rc) {
827                 printk(KERN_ERR
828                        "Failed to allocate one or more kmem_cache objects\n");
829                 goto out;
830         }
831         rc = register_filesystem(&ecryptfs_fs_type);
832         if (rc) {
833                 printk(KERN_ERR "Failed to register filesystem\n");
834                 goto out_free_kmem_caches;
835         }
836         rc = do_sysfs_registration();
837         if (rc) {
838                 printk(KERN_ERR "sysfs registration failed\n");
839                 goto out_unregister_filesystem;
840         }
841         rc = ecryptfs_init_kthread();
842         if (rc) {
843                 printk(KERN_ERR "%s: kthread initialization failed; "
844                        "rc = [%d]\n", __func__, rc);
845                 goto out_do_sysfs_unregistration;
846         }
847         rc = ecryptfs_init_messaging();
848         if (rc) {
849                 printk(KERN_ERR "Failure occured while attempting to "
850                                 "initialize the communications channel to "
851                                 "ecryptfsd\n");
852                 goto out_destroy_kthread;
853         }
854         rc = ecryptfs_init_crypto();
855         if (rc) {
856                 printk(KERN_ERR "Failure whilst attempting to init crypto; "
857                        "rc = [%d]\n", rc);
858                 goto out_release_messaging;
859         }
860         if (ecryptfs_verbosity > 0)
861                 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
862                         "will be written to the syslog!\n", ecryptfs_verbosity);
863
864         goto out;
865 out_release_messaging:
866         ecryptfs_release_messaging();
867 out_destroy_kthread:
868         ecryptfs_destroy_kthread();
869 out_do_sysfs_unregistration:
870         do_sysfs_unregistration();
871 out_unregister_filesystem:
872         unregister_filesystem(&ecryptfs_fs_type);
873 out_free_kmem_caches:
874         ecryptfs_free_kmem_caches();
875 out:
876         return rc;
877 }
878
879 static void __exit ecryptfs_exit(void)
880 {
881         int rc;
882
883         rc = ecryptfs_destroy_crypto();
884         if (rc)
885                 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
886                        "rc = [%d]\n", rc);
887         ecryptfs_release_messaging();
888         ecryptfs_destroy_kthread();
889         do_sysfs_unregistration();
890         unregister_filesystem(&ecryptfs_fs_type);
891         ecryptfs_free_kmem_caches();
892 }
893
894 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
895 MODULE_DESCRIPTION("eCryptfs");
896
897 MODULE_LICENSE("GPL");
898
899 module_init(ecryptfs_init)
900 module_exit(ecryptfs_exit)