eCryptfs: Write out all dirty pages just before releasing the lower file
[pandora-kernel.git] / 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 <linux/slab.h>
39 #include <linux/magic.h>
40 #include "ecryptfs_kernel.h"
41
42 /**
43  * Module parameter that defines the ecryptfs_verbosity level.
44  */
45 int ecryptfs_verbosity = 0;
46
47 module_param(ecryptfs_verbosity, int, 0);
48 MODULE_PARM_DESC(ecryptfs_verbosity,
49                  "Initial verbosity level (0 or 1; defaults to "
50                  "0, which is Quiet)");
51
52 /**
53  * Module parameter that defines the number of message buffer elements
54  */
55 unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
56
57 module_param(ecryptfs_message_buf_len, uint, 0);
58 MODULE_PARM_DESC(ecryptfs_message_buf_len,
59                  "Number of message buffer elements");
60
61 /**
62  * Module parameter that defines the maximum guaranteed amount of time to wait
63  * for a response from ecryptfsd.  The actual sleep time will be, more than
64  * likely, a small amount greater than this specified value, but only less if
65  * the message successfully arrives.
66  */
67 signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
68
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");
74
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.
79  */
80 unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
81
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");
85
86 void __ecryptfs_printk(const char *fmt, ...)
87 {
88         va_list args;
89         va_start(args, fmt);
90         if (fmt[1] == '7') { /* KERN_DEBUG */
91                 if (ecryptfs_verbosity >= 1)
92                         vprintk(fmt, args);
93         } else
94                 vprintk(fmt, args);
95         va_end(args);
96 }
97
98 /**
99  * ecryptfs_init_lower_file
100  * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
101  *                   the lower dentry and the lower mount set
102  *
103  * eCryptfs only ever keeps a single open file for every lower
104  * inode. All I/O operations to the lower inode occur through that
105  * file. When the first eCryptfs dentry that interposes with the first
106  * lower dentry for that inode is created, this function creates the
107  * lower file struct and associates it with the eCryptfs
108  * inode. When all eCryptfs files associated with the inode are released, the
109  * file is closed.
110  *
111  * The lower file will be opened with read/write permissions, if
112  * possible. Otherwise, it is opened read-only.
113  *
114  * This function does nothing if a lower file is already
115  * associated with the eCryptfs inode.
116  *
117  * Returns zero on success; non-zero otherwise
118  */
119 static int ecryptfs_init_lower_file(struct dentry *dentry,
120                                     struct file **lower_file)
121 {
122         const struct cred *cred = current_cred();
123         struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
124         struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
125         int rc;
126
127         rc = ecryptfs_privileged_open(lower_file, lower_dentry, lower_mnt,
128                                       cred);
129         if (rc) {
130                 printk(KERN_ERR "Error opening lower file "
131                        "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
132                        "rc = [%d]\n", lower_dentry, lower_mnt, rc);
133                 (*lower_file) = NULL;
134         }
135         return rc;
136 }
137
138 int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
139 {
140         struct ecryptfs_inode_info *inode_info;
141         int count, rc = 0;
142
143         inode_info = ecryptfs_inode_to_private(inode);
144         mutex_lock(&inode_info->lower_file_mutex);
145         count = atomic_inc_return(&inode_info->lower_file_count);
146         if (WARN_ON_ONCE(count < 1))
147                 rc = -EINVAL;
148         else if (count == 1) {
149                 rc = ecryptfs_init_lower_file(dentry,
150                                               &inode_info->lower_file);
151                 if (rc)
152                         atomic_set(&inode_info->lower_file_count, 0);
153         }
154         mutex_unlock(&inode_info->lower_file_mutex);
155         return rc;
156 }
157
158 void ecryptfs_put_lower_file(struct inode *inode)
159 {
160         struct ecryptfs_inode_info *inode_info;
161
162         inode_info = ecryptfs_inode_to_private(inode);
163         if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
164                                       &inode_info->lower_file_mutex)) {
165                 filemap_write_and_wait(inode->i_mapping);
166                 fput(inode_info->lower_file);
167                 inode_info->lower_file = NULL;
168                 mutex_unlock(&inode_info->lower_file_mutex);
169         }
170 }
171
172 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
173        ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
174        ecryptfs_opt_ecryptfs_key_bytes,
175        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
176        ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
177        ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
178        ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
179        ecryptfs_opt_check_dev_ruid,
180        ecryptfs_opt_err };
181
182 static const match_table_t tokens = {
183         {ecryptfs_opt_sig, "sig=%s"},
184         {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
185         {ecryptfs_opt_cipher, "cipher=%s"},
186         {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
187         {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
188         {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
189         {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
190         {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
191         {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
192         {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
193         {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
194         {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
195         {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
196         {ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
197         {ecryptfs_opt_err, NULL}
198 };
199
200 static int ecryptfs_init_global_auth_toks(
201         struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
202 {
203         struct ecryptfs_global_auth_tok *global_auth_tok;
204         struct ecryptfs_auth_tok *auth_tok;
205         int rc = 0;
206
207         list_for_each_entry(global_auth_tok,
208                             &mount_crypt_stat->global_auth_tok_list,
209                             mount_crypt_stat_list) {
210                 rc = ecryptfs_keyring_auth_tok_for_sig(
211                         &global_auth_tok->global_auth_tok_key, &auth_tok,
212                         global_auth_tok->sig);
213                 if (rc) {
214                         printk(KERN_ERR "Could not find valid key in user "
215                                "session keyring for sig specified in mount "
216                                "option: [%s]\n", global_auth_tok->sig);
217                         global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
218                         goto out;
219                 } else {
220                         global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
221                         up_write(&(global_auth_tok->global_auth_tok_key)->sem);
222                 }
223         }
224 out:
225         return rc;
226 }
227
228 static void ecryptfs_init_mount_crypt_stat(
229         struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
230 {
231         memset((void *)mount_crypt_stat, 0,
232                sizeof(struct ecryptfs_mount_crypt_stat));
233         INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
234         mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
235         mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
236 }
237
238 /**
239  * ecryptfs_parse_options
240  * @sb: The ecryptfs super block
241  * @options: The options passed to the kernel
242  * @check_ruid: set to 1 if device uid should be checked against the ruid
243  *
244  * Parse mount options:
245  * debug=N         - ecryptfs_verbosity level for debug output
246  * sig=XXX         - description(signature) of the key to use
247  *
248  * Returns the dentry object of the lower-level (lower/interposed)
249  * directory; We want to mount our stackable file system on top of
250  * that lower directory.
251  *
252  * The signature of the key to use must be the description of a key
253  * already in the keyring. Mounting will fail if the key can not be
254  * found.
255  *
256  * Returns zero on success; non-zero on error
257  */
258 static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
259                                   uid_t *check_ruid)
260 {
261         char *p;
262         int rc = 0;
263         int sig_set = 0;
264         int cipher_name_set = 0;
265         int fn_cipher_name_set = 0;
266         int cipher_key_bytes;
267         int cipher_key_bytes_set = 0;
268         int fn_cipher_key_bytes;
269         int fn_cipher_key_bytes_set = 0;
270         struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
271                 &sbi->mount_crypt_stat;
272         substring_t args[MAX_OPT_ARGS];
273         int token;
274         char *sig_src;
275         char *cipher_name_dst;
276         char *cipher_name_src;
277         char *fn_cipher_name_dst;
278         char *fn_cipher_name_src;
279         char *fnek_dst;
280         char *fnek_src;
281         char *cipher_key_bytes_src;
282         char *fn_cipher_key_bytes_src;
283
284         *check_ruid = 0;
285
286         if (!options) {
287                 rc = -EINVAL;
288                 goto out;
289         }
290         ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
291         while ((p = strsep(&options, ",")) != NULL) {
292                 if (!*p)
293                         continue;
294                 token = match_token(p, tokens, args);
295                 switch (token) {
296                 case ecryptfs_opt_sig:
297                 case ecryptfs_opt_ecryptfs_sig:
298                         sig_src = args[0].from;
299                         rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
300                                                           sig_src, 0);
301                         if (rc) {
302                                 printk(KERN_ERR "Error attempting to register "
303                                        "global sig; rc = [%d]\n", rc);
304                                 goto out;
305                         }
306                         sig_set = 1;
307                         break;
308                 case ecryptfs_opt_cipher:
309                 case ecryptfs_opt_ecryptfs_cipher:
310                         cipher_name_src = args[0].from;
311                         cipher_name_dst =
312                                 mount_crypt_stat->
313                                 global_default_cipher_name;
314                         strncpy(cipher_name_dst, cipher_name_src,
315                                 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
316                         cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
317                         cipher_name_set = 1;
318                         break;
319                 case ecryptfs_opt_ecryptfs_key_bytes:
320                         cipher_key_bytes_src = args[0].from;
321                         cipher_key_bytes =
322                                 (int)simple_strtol(cipher_key_bytes_src,
323                                                    &cipher_key_bytes_src, 0);
324                         mount_crypt_stat->global_default_cipher_key_size =
325                                 cipher_key_bytes;
326                         cipher_key_bytes_set = 1;
327                         break;
328                 case ecryptfs_opt_passthrough:
329                         mount_crypt_stat->flags |=
330                                 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
331                         break;
332                 case ecryptfs_opt_xattr_metadata:
333                         mount_crypt_stat->flags |=
334                                 ECRYPTFS_XATTR_METADATA_ENABLED;
335                         break;
336                 case ecryptfs_opt_encrypted_view:
337                         mount_crypt_stat->flags |=
338                                 ECRYPTFS_XATTR_METADATA_ENABLED;
339                         mount_crypt_stat->flags |=
340                                 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
341                         break;
342                 case ecryptfs_opt_fnek_sig:
343                         fnek_src = args[0].from;
344                         fnek_dst =
345                                 mount_crypt_stat->global_default_fnek_sig;
346                         strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
347                         mount_crypt_stat->global_default_fnek_sig[
348                                 ECRYPTFS_SIG_SIZE_HEX] = '\0';
349                         rc = ecryptfs_add_global_auth_tok(
350                                 mount_crypt_stat,
351                                 mount_crypt_stat->global_default_fnek_sig,
352                                 ECRYPTFS_AUTH_TOK_FNEK);
353                         if (rc) {
354                                 printk(KERN_ERR "Error attempting to register "
355                                        "global fnek sig [%s]; rc = [%d]\n",
356                                        mount_crypt_stat->global_default_fnek_sig,
357                                        rc);
358                                 goto out;
359                         }
360                         mount_crypt_stat->flags |=
361                                 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
362                                  | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
363                         break;
364                 case ecryptfs_opt_fn_cipher:
365                         fn_cipher_name_src = args[0].from;
366                         fn_cipher_name_dst =
367                                 mount_crypt_stat->global_default_fn_cipher_name;
368                         strncpy(fn_cipher_name_dst, fn_cipher_name_src,
369                                 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
370                         mount_crypt_stat->global_default_fn_cipher_name[
371                                 ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
372                         fn_cipher_name_set = 1;
373                         break;
374                 case ecryptfs_opt_fn_cipher_key_bytes:
375                         fn_cipher_key_bytes_src = args[0].from;
376                         fn_cipher_key_bytes =
377                                 (int)simple_strtol(fn_cipher_key_bytes_src,
378                                                    &fn_cipher_key_bytes_src, 0);
379                         mount_crypt_stat->global_default_fn_cipher_key_bytes =
380                                 fn_cipher_key_bytes;
381                         fn_cipher_key_bytes_set = 1;
382                         break;
383                 case ecryptfs_opt_unlink_sigs:
384                         mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
385                         break;
386                 case ecryptfs_opt_mount_auth_tok_only:
387                         mount_crypt_stat->flags |=
388                                 ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
389                         break;
390                 case ecryptfs_opt_check_dev_ruid:
391                         *check_ruid = 1;
392                         break;
393                 case ecryptfs_opt_err:
394                 default:
395                         printk(KERN_WARNING
396                                "%s: eCryptfs: unrecognized option [%s]\n",
397                                __func__, p);
398                 }
399         }
400         if (!sig_set) {
401                 rc = -EINVAL;
402                 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
403                                 "auth tok signature as a mount "
404                                 "parameter; see the eCryptfs README\n");
405                 goto out;
406         }
407         if (!cipher_name_set) {
408                 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
409
410                 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
411                 strcpy(mount_crypt_stat->global_default_cipher_name,
412                        ECRYPTFS_DEFAULT_CIPHER);
413         }
414         if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
415             && !fn_cipher_name_set)
416                 strcpy(mount_crypt_stat->global_default_fn_cipher_name,
417                        mount_crypt_stat->global_default_cipher_name);
418         if (!cipher_key_bytes_set)
419                 mount_crypt_stat->global_default_cipher_key_size = 0;
420         if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
421             && !fn_cipher_key_bytes_set)
422                 mount_crypt_stat->global_default_fn_cipher_key_bytes =
423                         mount_crypt_stat->global_default_cipher_key_size;
424         mutex_lock(&key_tfm_list_mutex);
425         if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
426                                  NULL)) {
427                 rc = ecryptfs_add_new_key_tfm(
428                         NULL, mount_crypt_stat->global_default_cipher_name,
429                         mount_crypt_stat->global_default_cipher_key_size);
430                 if (rc) {
431                         printk(KERN_ERR "Error attempting to initialize "
432                                "cipher with name = [%s] and key size = [%td]; "
433                                "rc = [%d]\n",
434                                mount_crypt_stat->global_default_cipher_name,
435                                mount_crypt_stat->global_default_cipher_key_size,
436                                rc);
437                         rc = -EINVAL;
438                         mutex_unlock(&key_tfm_list_mutex);
439                         goto out;
440                 }
441         }
442         if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
443             && !ecryptfs_tfm_exists(
444                     mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
445                 rc = ecryptfs_add_new_key_tfm(
446                         NULL, mount_crypt_stat->global_default_fn_cipher_name,
447                         mount_crypt_stat->global_default_fn_cipher_key_bytes);
448                 if (rc) {
449                         printk(KERN_ERR "Error attempting to initialize "
450                                "cipher with name = [%s] and key size = [%td]; "
451                                "rc = [%d]\n",
452                                mount_crypt_stat->global_default_fn_cipher_name,
453                                mount_crypt_stat->global_default_fn_cipher_key_bytes,
454                                rc);
455                         rc = -EINVAL;
456                         mutex_unlock(&key_tfm_list_mutex);
457                         goto out;
458                 }
459         }
460         mutex_unlock(&key_tfm_list_mutex);
461         rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
462         if (rc)
463                 printk(KERN_WARNING "One or more global auth toks could not "
464                        "properly register; rc = [%d]\n", rc);
465 out:
466         return rc;
467 }
468
469 struct kmem_cache *ecryptfs_sb_info_cache;
470 static struct file_system_type ecryptfs_fs_type;
471
472 /**
473  * ecryptfs_get_sb
474  * @fs_type
475  * @flags
476  * @dev_name: The path to mount over
477  * @raw_data: The options passed into the kernel
478  */
479 static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
480                         const char *dev_name, void *raw_data)
481 {
482         struct super_block *s;
483         struct ecryptfs_sb_info *sbi;
484         struct ecryptfs_dentry_info *root_info;
485         const char *err = "Getting sb failed";
486         struct inode *inode;
487         struct path path;
488         uid_t check_ruid;
489         int rc;
490
491         sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
492         if (!sbi) {
493                 rc = -ENOMEM;
494                 goto out;
495         }
496
497         rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
498         if (rc) {
499                 err = "Error parsing options";
500                 goto out;
501         }
502
503         s = sget(fs_type, NULL, set_anon_super, NULL);
504         if (IS_ERR(s)) {
505                 rc = PTR_ERR(s);
506                 goto out;
507         }
508
509         s->s_flags = flags;
510         rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs", BDI_CAP_MAP_COPY);
511         if (rc)
512                 goto out1;
513
514         ecryptfs_set_superblock_private(s, sbi);
515         s->s_bdi = &sbi->bdi;
516
517         /* ->kill_sb() will take care of sbi after that point */
518         sbi = NULL;
519         s->s_op = &ecryptfs_sops;
520         s->s_d_op = &ecryptfs_dops;
521
522         err = "Reading sb failed";
523         rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
524         if (rc) {
525                 ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
526                 goto out1;
527         }
528         if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
529                 rc = -EINVAL;
530                 printk(KERN_ERR "Mount on filesystem of type "
531                         "eCryptfs explicitly disallowed due to "
532                         "known incompatibilities\n");
533                 goto out_free;
534         }
535
536         if (check_ruid && path.dentry->d_inode->i_uid != current_uid()) {
537                 rc = -EPERM;
538                 printk(KERN_ERR "Mount of device (uid: %d) not owned by "
539                        "requested user (uid: %d)\n",
540                        path.dentry->d_inode->i_uid, current_uid());
541                 goto out_free;
542         }
543
544         ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
545         s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
546         s->s_blocksize = path.dentry->d_sb->s_blocksize;
547         s->s_magic = ECRYPTFS_SUPER_MAGIC;
548
549         inode = ecryptfs_get_inode(path.dentry->d_inode, s);
550         rc = PTR_ERR(inode);
551         if (IS_ERR(inode))
552                 goto out_free;
553
554         s->s_root = d_alloc_root(inode);
555         if (!s->s_root) {
556                 iput(inode);
557                 rc = -ENOMEM;
558                 goto out_free;
559         }
560
561         rc = -ENOMEM;
562         root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
563         if (!root_info)
564                 goto out_free;
565
566         /* ->kill_sb() will take care of root_info */
567         ecryptfs_set_dentry_private(s->s_root, root_info);
568         ecryptfs_set_dentry_lower(s->s_root, path.dentry);
569         ecryptfs_set_dentry_lower_mnt(s->s_root, path.mnt);
570
571         s->s_flags |= MS_ACTIVE;
572         return dget(s->s_root);
573
574 out_free:
575         path_put(&path);
576 out1:
577         deactivate_locked_super(s);
578 out:
579         if (sbi) {
580                 ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
581                 kmem_cache_free(ecryptfs_sb_info_cache, sbi);
582         }
583         printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
584         return ERR_PTR(rc);
585 }
586
587 /**
588  * ecryptfs_kill_block_super
589  * @sb: The ecryptfs super block
590  *
591  * Used to bring the superblock down and free the private data.
592  */
593 static void ecryptfs_kill_block_super(struct super_block *sb)
594 {
595         struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
596         kill_anon_super(sb);
597         if (!sb_info)
598                 return;
599         ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
600         bdi_destroy(&sb_info->bdi);
601         kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
602 }
603
604 static struct file_system_type ecryptfs_fs_type = {
605         .owner = THIS_MODULE,
606         .name = "ecryptfs",
607         .mount = ecryptfs_mount,
608         .kill_sb = ecryptfs_kill_block_super,
609         .fs_flags = 0
610 };
611
612 /**
613  * inode_info_init_once
614  *
615  * Initializes the ecryptfs_inode_info_cache when it is created
616  */
617 static void
618 inode_info_init_once(void *vptr)
619 {
620         struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
621
622         inode_init_once(&ei->vfs_inode);
623 }
624
625 static struct ecryptfs_cache_info {
626         struct kmem_cache **cache;
627         const char *name;
628         size_t size;
629         void (*ctor)(void *obj);
630 } ecryptfs_cache_infos[] = {
631         {
632                 .cache = &ecryptfs_auth_tok_list_item_cache,
633                 .name = "ecryptfs_auth_tok_list_item",
634                 .size = sizeof(struct ecryptfs_auth_tok_list_item),
635         },
636         {
637                 .cache = &ecryptfs_file_info_cache,
638                 .name = "ecryptfs_file_cache",
639                 .size = sizeof(struct ecryptfs_file_info),
640         },
641         {
642                 .cache = &ecryptfs_dentry_info_cache,
643                 .name = "ecryptfs_dentry_info_cache",
644                 .size = sizeof(struct ecryptfs_dentry_info),
645         },
646         {
647                 .cache = &ecryptfs_inode_info_cache,
648                 .name = "ecryptfs_inode_cache",
649                 .size = sizeof(struct ecryptfs_inode_info),
650                 .ctor = inode_info_init_once,
651         },
652         {
653                 .cache = &ecryptfs_sb_info_cache,
654                 .name = "ecryptfs_sb_cache",
655                 .size = sizeof(struct ecryptfs_sb_info),
656         },
657         {
658                 .cache = &ecryptfs_header_cache,
659                 .name = "ecryptfs_headers",
660                 .size = PAGE_CACHE_SIZE,
661         },
662         {
663                 .cache = &ecryptfs_xattr_cache,
664                 .name = "ecryptfs_xattr_cache",
665                 .size = PAGE_CACHE_SIZE,
666         },
667         {
668                 .cache = &ecryptfs_key_record_cache,
669                 .name = "ecryptfs_key_record_cache",
670                 .size = sizeof(struct ecryptfs_key_record),
671         },
672         {
673                 .cache = &ecryptfs_key_sig_cache,
674                 .name = "ecryptfs_key_sig_cache",
675                 .size = sizeof(struct ecryptfs_key_sig),
676         },
677         {
678                 .cache = &ecryptfs_global_auth_tok_cache,
679                 .name = "ecryptfs_global_auth_tok_cache",
680                 .size = sizeof(struct ecryptfs_global_auth_tok),
681         },
682         {
683                 .cache = &ecryptfs_key_tfm_cache,
684                 .name = "ecryptfs_key_tfm_cache",
685                 .size = sizeof(struct ecryptfs_key_tfm),
686         },
687         {
688                 .cache = &ecryptfs_open_req_cache,
689                 .name = "ecryptfs_open_req_cache",
690                 .size = sizeof(struct ecryptfs_open_req),
691         },
692 };
693
694 static void ecryptfs_free_kmem_caches(void)
695 {
696         int i;
697
698         for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
699                 struct ecryptfs_cache_info *info;
700
701                 info = &ecryptfs_cache_infos[i];
702                 if (*(info->cache))
703                         kmem_cache_destroy(*(info->cache));
704         }
705 }
706
707 /**
708  * ecryptfs_init_kmem_caches
709  *
710  * Returns zero on success; non-zero otherwise
711  */
712 static int ecryptfs_init_kmem_caches(void)
713 {
714         int i;
715
716         for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
717                 struct ecryptfs_cache_info *info;
718
719                 info = &ecryptfs_cache_infos[i];
720                 *(info->cache) = kmem_cache_create(info->name, info->size,
721                                 0, SLAB_HWCACHE_ALIGN, info->ctor);
722                 if (!*(info->cache)) {
723                         ecryptfs_free_kmem_caches();
724                         ecryptfs_printk(KERN_WARNING, "%s: "
725                                         "kmem_cache_create failed\n",
726                                         info->name);
727                         return -ENOMEM;
728                 }
729         }
730         return 0;
731 }
732
733 static struct kobject *ecryptfs_kobj;
734
735 static ssize_t version_show(struct kobject *kobj,
736                             struct kobj_attribute *attr, char *buff)
737 {
738         return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
739 }
740
741 static struct kobj_attribute version_attr = __ATTR_RO(version);
742
743 static struct attribute *attributes[] = {
744         &version_attr.attr,
745         NULL,
746 };
747
748 static struct attribute_group attr_group = {
749         .attrs = attributes,
750 };
751
752 static int do_sysfs_registration(void)
753 {
754         int rc;
755
756         ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
757         if (!ecryptfs_kobj) {
758                 printk(KERN_ERR "Unable to create ecryptfs kset\n");
759                 rc = -ENOMEM;
760                 goto out;
761         }
762         rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
763         if (rc) {
764                 printk(KERN_ERR
765                        "Unable to create ecryptfs version attributes\n");
766                 kobject_put(ecryptfs_kobj);
767         }
768 out:
769         return rc;
770 }
771
772 static void do_sysfs_unregistration(void)
773 {
774         sysfs_remove_group(ecryptfs_kobj, &attr_group);
775         kobject_put(ecryptfs_kobj);
776 }
777
778 static int __init ecryptfs_init(void)
779 {
780         int rc;
781
782         if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
783                 rc = -EINVAL;
784                 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
785                                 "larger than the host's page size, and so "
786                                 "eCryptfs cannot run on this system. The "
787                                 "default eCryptfs extent size is [%u] bytes; "
788                                 "the page size is [%lu] bytes.\n",
789                                 ECRYPTFS_DEFAULT_EXTENT_SIZE,
790                                 (unsigned long)PAGE_CACHE_SIZE);
791                 goto out;
792         }
793         rc = ecryptfs_init_kmem_caches();
794         if (rc) {
795                 printk(KERN_ERR
796                        "Failed to allocate one or more kmem_cache objects\n");
797                 goto out;
798         }
799         rc = register_filesystem(&ecryptfs_fs_type);
800         if (rc) {
801                 printk(KERN_ERR "Failed to register filesystem\n");
802                 goto out_free_kmem_caches;
803         }
804         rc = do_sysfs_registration();
805         if (rc) {
806                 printk(KERN_ERR "sysfs registration failed\n");
807                 goto out_unregister_filesystem;
808         }
809         rc = ecryptfs_init_kthread();
810         if (rc) {
811                 printk(KERN_ERR "%s: kthread initialization failed; "
812                        "rc = [%d]\n", __func__, rc);
813                 goto out_do_sysfs_unregistration;
814         }
815         rc = ecryptfs_init_messaging();
816         if (rc) {
817                 printk(KERN_ERR "Failure occurred while attempting to "
818                                 "initialize the communications channel to "
819                                 "ecryptfsd\n");
820                 goto out_destroy_kthread;
821         }
822         rc = ecryptfs_init_crypto();
823         if (rc) {
824                 printk(KERN_ERR "Failure whilst attempting to init crypto; "
825                        "rc = [%d]\n", rc);
826                 goto out_release_messaging;
827         }
828         if (ecryptfs_verbosity > 0)
829                 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
830                         "will be written to the syslog!\n", ecryptfs_verbosity);
831
832         goto out;
833 out_release_messaging:
834         ecryptfs_release_messaging();
835 out_destroy_kthread:
836         ecryptfs_destroy_kthread();
837 out_do_sysfs_unregistration:
838         do_sysfs_unregistration();
839 out_unregister_filesystem:
840         unregister_filesystem(&ecryptfs_fs_type);
841 out_free_kmem_caches:
842         ecryptfs_free_kmem_caches();
843 out:
844         return rc;
845 }
846
847 static void __exit ecryptfs_exit(void)
848 {
849         int rc;
850
851         rc = ecryptfs_destroy_crypto();
852         if (rc)
853                 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
854                        "rc = [%d]\n", rc);
855         ecryptfs_release_messaging();
856         ecryptfs_destroy_kthread();
857         do_sysfs_unregistration();
858         unregister_filesystem(&ecryptfs_fs_type);
859         ecryptfs_free_kmem_caches();
860 }
861
862 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
863 MODULE_DESCRIPTION("eCryptfs");
864
865 MODULE_LICENSE("GPL");
866
867 module_init(ecryptfs_init)
868 module_exit(ecryptfs_exit)