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