eCryptfs: set up and destroy persistent lower file
[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/netlink.h>
34 #include <linux/mount.h>
35 #include <linux/dcache.h>
36 #include <linux/pagemap.h>
37 #include <linux/key.h>
38 #include <linux/parser.h>
39 #include <linux/fs_stack.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 netlink message buffer
54  * elements
55  */
56 unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
57
58 module_param(ecryptfs_message_buf_len, uint, 0);
59 MODULE_PARM_DESC(ecryptfs_message_buf_len,
60                  "Number of message buffer elements");
61
62 /**
63  * Module parameter that defines the maximum guaranteed amount of time to wait
64  * for a response through netlink.  The actual sleep time will be, more than
65  * likely, a small amount greater than this specified value, but only less if
66  * the netlink message successfully arrives.
67  */
68 signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
69
70 module_param(ecryptfs_message_wait_timeout, long, 0);
71 MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
72                  "Maximum number of seconds that an operation will "
73                  "sleep while waiting for a message response from "
74                  "userspace");
75
76 /**
77  * Module parameter that is an estimate of the maximum number of users
78  * that will be concurrently using eCryptfs. Set this to the right
79  * value to balance performance and memory use.
80  */
81 unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
82
83 module_param(ecryptfs_number_of_users, uint, 0);
84 MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
85                  "concurrent users of eCryptfs");
86
87 unsigned int ecryptfs_transport = ECRYPTFS_DEFAULT_TRANSPORT;
88
89 void __ecryptfs_printk(const char *fmt, ...)
90 {
91         va_list args;
92         va_start(args, fmt);
93         if (fmt[1] == '7') { /* KERN_DEBUG */
94                 if (ecryptfs_verbosity >= 1)
95                         vprintk(fmt, args);
96         } else
97                 vprintk(fmt, args);
98         va_end(args);
99 }
100
101 /**
102  * ecryptfs_init_persistent_file
103  * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
104  *                   the lower dentry and the lower mount set
105  *
106  * eCryptfs only ever keeps a single open file for every lower
107  * inode. All I/O operations to the lower inode occur through that
108  * file. When the first eCryptfs dentry that interposes with the first
109  * lower dentry for that inode is created, this function creates the
110  * persistent file struct and associates it with the eCryptfs
111  * inode. When the eCryptfs inode is destroyed, the file is closed.
112  *
113  * The persistent file will be opened with read/write permissions, if
114  * possible. Otherwise, it is opened read-only.
115  *
116  * This function does nothing if a lower persistent file is already
117  * associated with the eCryptfs inode.
118  *
119  * Returns zero on success; non-zero otherwise
120  */
121 int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
122 {
123         struct ecryptfs_inode_info *inode_info =
124                 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
125         int rc = 0;
126
127         mutex_lock(&inode_info->lower_file_mutex);
128         if (!inode_info->lower_file) {
129                 struct dentry *lower_dentry;
130                 struct vfsmount *lower_mnt =
131                         ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
132
133                 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
134                 /* Corresponding dput() and mntput() are done when the
135                  * persistent file is fput() when the eCryptfs inode
136                  * is destroyed. */
137                 dget(lower_dentry);
138                 mntget(lower_mnt);
139                 inode_info->lower_file = dentry_open(lower_dentry,
140                                                      lower_mnt,
141                                                      (O_RDWR | O_LARGEFILE));
142                 if (IS_ERR(inode_info->lower_file))
143                         inode_info->lower_file = dentry_open(lower_dentry,
144                                                              lower_mnt,
145                                                              (O_RDONLY
146                                                               | O_LARGEFILE));
147                 if (IS_ERR(inode_info->lower_file)) {
148                         printk(KERN_ERR "Error opening lower persistent file "
149                                "for lower_dentry [0x%p] and lower_mnt [0x%p]\n",
150                                lower_dentry, lower_mnt);
151                         rc = PTR_ERR(inode_info->lower_file);
152                         inode_info->lower_file = NULL;
153                 }
154         }
155         mutex_unlock(&inode_info->lower_file_mutex);
156         return rc;
157 }
158
159 /**
160  * ecryptfs_interpose
161  * @lower_dentry: Existing dentry in the lower filesystem
162  * @dentry: ecryptfs' dentry
163  * @sb: ecryptfs's super_block
164  * @flag: If set to true, then d_add is called, else d_instantiate is called
165  *
166  * Interposes upper and lower dentries.
167  *
168  * Returns zero on success; non-zero otherwise
169  */
170 int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
171                        struct super_block *sb, int flag)
172 {
173         struct inode *lower_inode;
174         struct inode *inode;
175         int rc = 0;
176
177         lower_inode = lower_dentry->d_inode;
178         if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
179                 rc = -EXDEV;
180                 goto out;
181         }
182         if (!igrab(lower_inode)) {
183                 rc = -ESTALE;
184                 goto out;
185         }
186         inode = iget5_locked(sb, (unsigned long)lower_inode,
187                              ecryptfs_inode_test, ecryptfs_inode_set,
188                              lower_inode);
189         if (!inode) {
190                 rc = -EACCES;
191                 iput(lower_inode);
192                 goto out;
193         }
194         if (inode->i_state & I_NEW)
195                 unlock_new_inode(inode);
196         else
197                 iput(lower_inode);
198         if (S_ISLNK(lower_inode->i_mode))
199                 inode->i_op = &ecryptfs_symlink_iops;
200         else if (S_ISDIR(lower_inode->i_mode))
201                 inode->i_op = &ecryptfs_dir_iops;
202         if (S_ISDIR(lower_inode->i_mode))
203                 inode->i_fop = &ecryptfs_dir_fops;
204         if (special_file(lower_inode->i_mode))
205                 init_special_inode(inode, lower_inode->i_mode,
206                                    lower_inode->i_rdev);
207         dentry->d_op = &ecryptfs_dops;
208         if (flag)
209                 d_add(dentry, inode);
210         else
211                 d_instantiate(dentry, inode);
212         fsstack_copy_attr_all(inode, lower_inode, NULL);
213         /* This size will be overwritten for real files w/ headers and
214          * other metadata */
215         fsstack_copy_inode_size(inode, lower_inode);
216         rc = ecryptfs_init_persistent_file(dentry);
217         if (rc) {
218                 printk(KERN_ERR "%s: Error attempting to initialize the "
219                        "persistent file for the dentry with name [%s]; "
220                        "rc = [%d]\n", __FUNCTION__, dentry->d_name.name, rc);
221                 goto out;
222         }
223 out:
224         return rc;
225 }
226
227 enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, ecryptfs_opt_debug,
228        ecryptfs_opt_ecryptfs_debug, ecryptfs_opt_cipher,
229        ecryptfs_opt_ecryptfs_cipher, ecryptfs_opt_ecryptfs_key_bytes,
230        ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
231        ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
232
233 static match_table_t tokens = {
234         {ecryptfs_opt_sig, "sig=%s"},
235         {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
236         {ecryptfs_opt_debug, "debug=%u"},
237         {ecryptfs_opt_ecryptfs_debug, "ecryptfs_debug=%u"},
238         {ecryptfs_opt_cipher, "cipher=%s"},
239         {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
240         {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
241         {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
242         {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
243         {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
244         {ecryptfs_opt_err, NULL}
245 };
246
247 static int ecryptfs_init_global_auth_toks(
248         struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
249 {
250         struct ecryptfs_global_auth_tok *global_auth_tok;
251         int rc = 0;
252
253         list_for_each_entry(global_auth_tok,
254                             &mount_crypt_stat->global_auth_tok_list,
255                             mount_crypt_stat_list) {
256                 rc = ecryptfs_keyring_auth_tok_for_sig(
257                         &global_auth_tok->global_auth_tok_key,
258                         &global_auth_tok->global_auth_tok,
259                         global_auth_tok->sig);
260                 if (rc) {
261                         printk(KERN_ERR "Could not find valid key in user "
262                                "session keyring for sig specified in mount "
263                                "option: [%s]\n", global_auth_tok->sig);
264                         global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
265                         rc = 0;
266                 } else
267                         global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
268         }
269         return rc;
270 }
271
272 static void ecryptfs_init_mount_crypt_stat(
273         struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
274 {
275         memset((void *)mount_crypt_stat, 0,
276                sizeof(struct ecryptfs_mount_crypt_stat));
277         INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
278         mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
279         mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
280 }
281
282 /**
283  * ecryptfs_parse_options
284  * @sb: The ecryptfs super block
285  * @options: The options pased to the kernel
286  *
287  * Parse mount options:
288  * debug=N         - ecryptfs_verbosity level for debug output
289  * sig=XXX         - description(signature) of the key to use
290  *
291  * Returns the dentry object of the lower-level (lower/interposed)
292  * directory; We want to mount our stackable file system on top of
293  * that lower directory.
294  *
295  * The signature of the key to use must be the description of a key
296  * already in the keyring. Mounting will fail if the key can not be
297  * found.
298  *
299  * Returns zero on success; non-zero on error
300  */
301 static int ecryptfs_parse_options(struct super_block *sb, char *options)
302 {
303         char *p;
304         int rc = 0;
305         int sig_set = 0;
306         int cipher_name_set = 0;
307         int cipher_key_bytes;
308         int cipher_key_bytes_set = 0;
309         struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
310                 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
311         substring_t args[MAX_OPT_ARGS];
312         int token;
313         char *sig_src;
314         char *debug_src;
315         char *cipher_name_dst;
316         char *cipher_name_src;
317         char *cipher_key_bytes_src;
318         int cipher_name_len;
319
320         if (!options) {
321                 rc = -EINVAL;
322                 goto out;
323         }
324         ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
325         while ((p = strsep(&options, ",")) != NULL) {
326                 if (!*p)
327                         continue;
328                 token = match_token(p, tokens, args);
329                 switch (token) {
330                 case ecryptfs_opt_sig:
331                 case ecryptfs_opt_ecryptfs_sig:
332                         sig_src = args[0].from;
333                         rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
334                                                           sig_src);
335                         if (rc) {
336                                 printk(KERN_ERR "Error attempting to register "
337                                        "global sig; rc = [%d]\n", rc);
338                                 goto out;
339                         }
340                         sig_set = 1;
341                         break;
342                 case ecryptfs_opt_debug:
343                 case ecryptfs_opt_ecryptfs_debug:
344                         debug_src = args[0].from;
345                         ecryptfs_verbosity =
346                                 (int)simple_strtol(debug_src, &debug_src,
347                                                    0);
348                         ecryptfs_printk(KERN_DEBUG,
349                                         "Verbosity set to [%d]" "\n",
350                                         ecryptfs_verbosity);
351                         break;
352                 case ecryptfs_opt_cipher:
353                 case ecryptfs_opt_ecryptfs_cipher:
354                         cipher_name_src = args[0].from;
355                         cipher_name_dst =
356                                 mount_crypt_stat->
357                                 global_default_cipher_name;
358                         strncpy(cipher_name_dst, cipher_name_src,
359                                 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
360                         ecryptfs_printk(KERN_DEBUG,
361                                         "The mount_crypt_stat "
362                                         "global_default_cipher_name set to: "
363                                         "[%s]\n", cipher_name_dst);
364                         cipher_name_set = 1;
365                         break;
366                 case ecryptfs_opt_ecryptfs_key_bytes:
367                         cipher_key_bytes_src = args[0].from;
368                         cipher_key_bytes =
369                                 (int)simple_strtol(cipher_key_bytes_src,
370                                                    &cipher_key_bytes_src, 0);
371                         mount_crypt_stat->global_default_cipher_key_size =
372                                 cipher_key_bytes;
373                         ecryptfs_printk(KERN_DEBUG,
374                                         "The mount_crypt_stat "
375                                         "global_default_cipher_key_size "
376                                         "set to: [%d]\n", mount_crypt_stat->
377                                         global_default_cipher_key_size);
378                         cipher_key_bytes_set = 1;
379                         break;
380                 case ecryptfs_opt_passthrough:
381                         mount_crypt_stat->flags |=
382                                 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
383                         break;
384                 case ecryptfs_opt_xattr_metadata:
385                         mount_crypt_stat->flags |=
386                                 ECRYPTFS_XATTR_METADATA_ENABLED;
387                         break;
388                 case ecryptfs_opt_encrypted_view:
389                         mount_crypt_stat->flags |=
390                                 ECRYPTFS_XATTR_METADATA_ENABLED;
391                         mount_crypt_stat->flags |=
392                                 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
393                         break;
394                 case ecryptfs_opt_err:
395                 default:
396                         ecryptfs_printk(KERN_WARNING,
397                                         "eCryptfs: unrecognized option '%s'\n",
398                                         p);
399                 }
400         }
401         if (!sig_set) {
402                 rc = -EINVAL;
403                 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
404                                 "auth tok signature as a mount "
405                                 "parameter; see the eCryptfs README\n");
406                 goto out;
407         }
408         if (!cipher_name_set) {
409                 cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
410                 if (unlikely(cipher_name_len
411                              >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
412                         rc = -EINVAL;
413                         BUG();
414                         goto out;
415                 }
416                 memcpy(mount_crypt_stat->global_default_cipher_name,
417                        ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
418                 mount_crypt_stat->global_default_cipher_name[cipher_name_len]
419                     = '\0';
420         }
421         if (!cipher_key_bytes_set) {
422                 mount_crypt_stat->global_default_cipher_key_size = 0;
423         }
424         rc = ecryptfs_add_new_key_tfm(
425                 NULL, mount_crypt_stat->global_default_cipher_name,
426                 mount_crypt_stat->global_default_cipher_key_size);
427         if (rc) {
428                 printk(KERN_ERR "Error attempting to initialize cipher with "
429                        "name = [%s] and key size = [%td]; rc = [%d]\n",
430                        mount_crypt_stat->global_default_cipher_name,
431                        mount_crypt_stat->global_default_cipher_key_size, rc);
432                 rc = -EINVAL;
433                 goto out;
434         }
435         rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
436         if (rc) {
437                 printk(KERN_WARNING "One or more global auth toks could not "
438                        "properly register; rc = [%d]\n", rc);
439         }
440         rc = 0;
441 out:
442         return rc;
443 }
444
445 struct kmem_cache *ecryptfs_sb_info_cache;
446
447 /**
448  * ecryptfs_fill_super
449  * @sb: The ecryptfs super block
450  * @raw_data: The options passed to mount
451  * @silent: Not used but required by function prototype
452  *
453  * Sets up what we can of the sb, rest is done in ecryptfs_read_super
454  *
455  * Returns zero on success; non-zero otherwise
456  */
457 static int
458 ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
459 {
460         int rc = 0;
461
462         /* Released in ecryptfs_put_super() */
463         ecryptfs_set_superblock_private(sb,
464                                         kmem_cache_zalloc(ecryptfs_sb_info_cache,
465                                                          GFP_KERNEL));
466         if (!ecryptfs_superblock_to_private(sb)) {
467                 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
468                 rc = -ENOMEM;
469                 goto out;
470         }
471         sb->s_op = &ecryptfs_sops;
472         /* Released through deactivate_super(sb) from get_sb_nodev */
473         sb->s_root = d_alloc(NULL, &(const struct qstr) {
474                              .hash = 0,.name = "/",.len = 1});
475         if (!sb->s_root) {
476                 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
477                 rc = -ENOMEM;
478                 goto out;
479         }
480         sb->s_root->d_op = &ecryptfs_dops;
481         sb->s_root->d_sb = sb;
482         sb->s_root->d_parent = sb->s_root;
483         /* Released in d_release when dput(sb->s_root) is called */
484         /* through deactivate_super(sb) from get_sb_nodev() */
485         ecryptfs_set_dentry_private(sb->s_root,
486                                     kmem_cache_zalloc(ecryptfs_dentry_info_cache,
487                                                      GFP_KERNEL));
488         if (!ecryptfs_dentry_to_private(sb->s_root)) {
489                 ecryptfs_printk(KERN_ERR,
490                                 "dentry_info_cache alloc failed\n");
491                 rc = -ENOMEM;
492                 goto out;
493         }
494         rc = 0;
495 out:
496         /* Should be able to rely on deactivate_super called from
497          * get_sb_nodev */
498         return rc;
499 }
500
501 /**
502  * ecryptfs_read_super
503  * @sb: The ecryptfs super block
504  * @dev_name: The path to mount over
505  *
506  * Read the super block of the lower filesystem, and use
507  * ecryptfs_interpose to create our initial inode and super block
508  * struct.
509  */
510 static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
511 {
512         int rc;
513         struct nameidata nd;
514         struct dentry *lower_root;
515         struct vfsmount *lower_mnt;
516
517         memset(&nd, 0, sizeof(struct nameidata));
518         rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
519         if (rc) {
520                 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
521                 goto out;
522         }
523         lower_root = nd.dentry;
524         lower_mnt = nd.mnt;
525         ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
526         sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
527         ecryptfs_set_dentry_lower(sb->s_root, lower_root);
528         ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
529         rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0);
530         if (rc)
531                 goto out_free;
532         rc = 0;
533         goto out;
534 out_free:
535         path_release(&nd);
536 out:
537         return rc;
538 }
539
540 /**
541  * ecryptfs_get_sb
542  * @fs_type
543  * @flags
544  * @dev_name: The path to mount over
545  * @raw_data: The options passed into the kernel
546  *
547  * The whole ecryptfs_get_sb process is broken into 4 functions:
548  * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
549  * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
550  *                        with as much information as it can before needing
551  *                        the lower filesystem.
552  * ecryptfs_read_super(): this accesses the lower filesystem and uses
553  *                        ecryptfs_interpolate to perform most of the linking
554  * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
555  */
556 static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
557                         const char *dev_name, void *raw_data,
558                         struct vfsmount *mnt)
559 {
560         int rc;
561         struct super_block *sb;
562
563         rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
564         if (rc < 0) {
565                 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
566                 goto out;
567         }
568         sb = mnt->mnt_sb;
569         rc = ecryptfs_parse_options(sb, raw_data);
570         if (rc) {
571                 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
572                 goto out_abort;
573         }
574         rc = ecryptfs_read_super(sb, dev_name);
575         if (rc) {
576                 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
577                 goto out_abort;
578         }
579         goto out;
580 out_abort:
581         dput(sb->s_root);
582         up_write(&sb->s_umount);
583         deactivate_super(sb);
584 out:
585         return rc;
586 }
587
588 /**
589  * ecryptfs_kill_block_super
590  * @sb: The ecryptfs super block
591  *
592  * Used to bring the superblock down and free the private data.
593  * Private data is free'd in ecryptfs_put_super()
594  */
595 static void ecryptfs_kill_block_super(struct super_block *sb)
596 {
597         generic_shutdown_super(sb);
598 }
599
600 static struct file_system_type ecryptfs_fs_type = {
601         .owner = THIS_MODULE,
602         .name = "ecryptfs",
603         .get_sb = ecryptfs_get_sb,
604         .kill_sb = ecryptfs_kill_block_super,
605         .fs_flags = 0
606 };
607
608 /**
609  * inode_info_init_once
610  *
611  * Initializes the ecryptfs_inode_info_cache when it is created
612  */
613 static void
614 inode_info_init_once(void *vptr, struct kmem_cache *cachep, unsigned long flags)
615 {
616         struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
617
618         inode_init_once(&ei->vfs_inode);
619 }
620
621 static struct ecryptfs_cache_info {
622         struct kmem_cache **cache;
623         const char *name;
624         size_t size;
625         void (*ctor)(void*, struct kmem_cache *, unsigned long);
626 } ecryptfs_cache_infos[] = {
627         {
628                 .cache = &ecryptfs_auth_tok_list_item_cache,
629                 .name = "ecryptfs_auth_tok_list_item",
630                 .size = sizeof(struct ecryptfs_auth_tok_list_item),
631         },
632         {
633                 .cache = &ecryptfs_file_info_cache,
634                 .name = "ecryptfs_file_cache",
635                 .size = sizeof(struct ecryptfs_file_info),
636         },
637         {
638                 .cache = &ecryptfs_dentry_info_cache,
639                 .name = "ecryptfs_dentry_info_cache",
640                 .size = sizeof(struct ecryptfs_dentry_info),
641         },
642         {
643                 .cache = &ecryptfs_inode_info_cache,
644                 .name = "ecryptfs_inode_cache",
645                 .size = sizeof(struct ecryptfs_inode_info),
646                 .ctor = inode_info_init_once,
647         },
648         {
649                 .cache = &ecryptfs_sb_info_cache,
650                 .name = "ecryptfs_sb_cache",
651                 .size = sizeof(struct ecryptfs_sb_info),
652         },
653         {
654                 .cache = &ecryptfs_header_cache_0,
655                 .name = "ecryptfs_headers_0",
656                 .size = PAGE_CACHE_SIZE,
657         },
658         {
659                 .cache = &ecryptfs_header_cache_1,
660                 .name = "ecryptfs_headers_1",
661                 .size = PAGE_CACHE_SIZE,
662         },
663         {
664                 .cache = &ecryptfs_header_cache_2,
665                 .name = "ecryptfs_headers_2",
666                 .size = PAGE_CACHE_SIZE,
667         },
668         {
669                 .cache = &ecryptfs_xattr_cache,
670                 .name = "ecryptfs_xattr_cache",
671                 .size = PAGE_CACHE_SIZE,
672         },
673         {
674                 .cache = &ecryptfs_lower_page_cache,
675                 .name = "ecryptfs_lower_page_cache",
676                 .size = PAGE_CACHE_SIZE,
677         },
678         {
679                 .cache = &ecryptfs_key_record_cache,
680                 .name = "ecryptfs_key_record_cache",
681                 .size = sizeof(struct ecryptfs_key_record),
682         },
683         {
684                 .cache = &ecryptfs_key_sig_cache,
685                 .name = "ecryptfs_key_sig_cache",
686                 .size = sizeof(struct ecryptfs_key_sig),
687         },
688         {
689                 .cache = &ecryptfs_global_auth_tok_cache,
690                 .name = "ecryptfs_global_auth_tok_cache",
691                 .size = sizeof(struct ecryptfs_global_auth_tok),
692         },
693         {
694                 .cache = &ecryptfs_key_tfm_cache,
695                 .name = "ecryptfs_key_tfm_cache",
696                 .size = sizeof(struct ecryptfs_key_tfm),
697         },
698 };
699
700 static void ecryptfs_free_kmem_caches(void)
701 {
702         int i;
703
704         for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
705                 struct ecryptfs_cache_info *info;
706
707                 info = &ecryptfs_cache_infos[i];
708                 if (*(info->cache))
709                         kmem_cache_destroy(*(info->cache));
710         }
711 }
712
713 /**
714  * ecryptfs_init_kmem_caches
715  *
716  * Returns zero on success; non-zero otherwise
717  */
718 static int ecryptfs_init_kmem_caches(void)
719 {
720         int i;
721
722         for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
723                 struct ecryptfs_cache_info *info;
724
725                 info = &ecryptfs_cache_infos[i];
726                 *(info->cache) = kmem_cache_create(info->name, info->size,
727                                 0, SLAB_HWCACHE_ALIGN, info->ctor);
728                 if (!*(info->cache)) {
729                         ecryptfs_free_kmem_caches();
730                         ecryptfs_printk(KERN_WARNING, "%s: "
731                                         "kmem_cache_create failed\n",
732                                         info->name);
733                         return -ENOMEM;
734                 }
735         }
736         return 0;
737 }
738
739 struct ecryptfs_obj {
740         char *name;
741         struct list_head slot_list;
742         struct kobject kobj;
743 };
744
745 struct ecryptfs_attribute {
746         struct attribute attr;
747         ssize_t(*show) (struct ecryptfs_obj *, char *);
748         ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t);
749 };
750
751 static ssize_t
752 ecryptfs_attr_store(struct kobject *kobj,
753                     struct attribute *attr, const char *buf, size_t len)
754 {
755         struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
756                                                 kobj);
757         struct ecryptfs_attribute *attribute =
758                 container_of(attr, struct ecryptfs_attribute, attr);
759
760         return (attribute->store ? attribute->store(obj, buf, len) : 0);
761 }
762
763 static ssize_t
764 ecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
765 {
766         struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
767                                                 kobj);
768         struct ecryptfs_attribute *attribute =
769                 container_of(attr, struct ecryptfs_attribute, attr);
770
771         return (attribute->show ? attribute->show(obj, buf) : 0);
772 }
773
774 static struct sysfs_ops ecryptfs_sysfs_ops = {
775         .show = ecryptfs_attr_show,
776         .store = ecryptfs_attr_store
777 };
778
779 static struct kobj_type ecryptfs_ktype = {
780         .sysfs_ops = &ecryptfs_sysfs_ops
781 };
782
783 static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL);
784
785 static ssize_t version_show(struct ecryptfs_obj *obj, char *buff)
786 {
787         return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
788 }
789
790 static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version);
791
792 static struct ecryptfs_version_str_map_elem {
793         u32 flag;
794         char *str;
795 } ecryptfs_version_str_map[] = {
796         {ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"},
797         {ECRYPTFS_VERSIONING_PUBKEY, "pubkey"},
798         {ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"},
799         {ECRYPTFS_VERSIONING_POLICY, "policy"},
800         {ECRYPTFS_VERSIONING_XATTR, "metadata in extended attribute"},
801         {ECRYPTFS_VERSIONING_MULTKEY, "multiple keys per file"}
802 };
803
804 static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff)
805 {
806         int i;
807         int remaining = PAGE_SIZE;
808         int total_written = 0;
809
810         buff[0] = '\0';
811         for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) {
812                 int entry_size;
813
814                 if (!(ECRYPTFS_VERSIONING_MASK
815                       & ecryptfs_version_str_map[i].flag))
816                         continue;
817                 entry_size = strlen(ecryptfs_version_str_map[i].str);
818                 if ((entry_size + 2) > remaining)
819                         goto out;
820                 memcpy(buff, ecryptfs_version_str_map[i].str, entry_size);
821                 buff[entry_size++] = '\n';
822                 buff[entry_size] = '\0';
823                 buff += entry_size;
824                 total_written += entry_size;
825                 remaining -= entry_size;
826         }
827 out:
828         return total_written;
829 }
830
831 static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str);
832
833 static int do_sysfs_registration(void)
834 {
835         int rc;
836
837         rc = subsystem_register(&ecryptfs_subsys);
838         if (rc) {
839                 printk(KERN_ERR
840                        "Unable to register ecryptfs sysfs subsystem\n");
841                 goto out;
842         }
843         rc = sysfs_create_file(&ecryptfs_subsys.kobj,
844                                &sysfs_attr_version.attr);
845         if (rc) {
846                 printk(KERN_ERR
847                        "Unable to create ecryptfs version attribute\n");
848                 subsystem_unregister(&ecryptfs_subsys);
849                 goto out;
850         }
851         rc = sysfs_create_file(&ecryptfs_subsys.kobj,
852                                &sysfs_attr_version_str.attr);
853         if (rc) {
854                 printk(KERN_ERR
855                        "Unable to create ecryptfs version_str attribute\n");
856                 sysfs_remove_file(&ecryptfs_subsys.kobj,
857                                   &sysfs_attr_version.attr);
858                 subsystem_unregister(&ecryptfs_subsys);
859                 goto out;
860         }
861 out:
862         return rc;
863 }
864
865 static void do_sysfs_unregistration(void)
866 {
867         sysfs_remove_file(&ecryptfs_subsys.kobj,
868                           &sysfs_attr_version.attr);
869         sysfs_remove_file(&ecryptfs_subsys.kobj,
870                           &sysfs_attr_version_str.attr);
871         subsystem_unregister(&ecryptfs_subsys);
872 }
873
874 static int __init ecryptfs_init(void)
875 {
876         int rc;
877
878         if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
879                 rc = -EINVAL;
880                 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
881                                 "larger than the host's page size, and so "
882                                 "eCryptfs cannot run on this system. The "
883                                 "default eCryptfs extent size is [%d] bytes; "
884                                 "the page size is [%d] bytes.\n",
885                                 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
886                 goto out;
887         }
888         rc = ecryptfs_init_kmem_caches();
889         if (rc) {
890                 printk(KERN_ERR
891                        "Failed to allocate one or more kmem_cache objects\n");
892                 goto out;
893         }
894         rc = register_filesystem(&ecryptfs_fs_type);
895         if (rc) {
896                 printk(KERN_ERR "Failed to register filesystem\n");
897                 goto out_free_kmem_caches;
898         }
899         kobj_set_kset_s(&ecryptfs_subsys, fs_subsys);
900         rc = do_sysfs_registration();
901         if (rc) {
902                 printk(KERN_ERR "sysfs registration failed\n");
903                 goto out_unregister_filesystem;
904         }
905         rc = ecryptfs_init_messaging(ecryptfs_transport);
906         if (rc) {
907                 ecryptfs_printk(KERN_ERR, "Failure occured while attempting to "
908                                 "initialize the eCryptfs netlink socket\n");
909                 goto out_do_sysfs_unregistration;
910         }
911         rc = ecryptfs_init_crypto();
912         if (rc) {
913                 printk(KERN_ERR "Failure whilst attempting to init crypto; "
914                        "rc = [%d]\n", rc);
915                 goto out_release_messaging;
916         }
917         goto out;
918 out_release_messaging:
919         ecryptfs_release_messaging(ecryptfs_transport);
920 out_do_sysfs_unregistration:
921         do_sysfs_unregistration();
922 out_unregister_filesystem:
923         unregister_filesystem(&ecryptfs_fs_type);
924 out_free_kmem_caches:
925         ecryptfs_free_kmem_caches();
926 out:
927         return rc;
928 }
929
930 static void __exit ecryptfs_exit(void)
931 {
932         int rc;
933
934         rc = ecryptfs_destroy_crypto();
935         if (rc)
936                 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
937                        "rc = [%d]\n", rc);
938         ecryptfs_release_messaging(ecryptfs_transport);
939         do_sysfs_unregistration();
940         unregister_filesystem(&ecryptfs_fs_type);
941         ecryptfs_free_kmem_caches();
942 }
943
944 MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
945 MODULE_DESCRIPTION("eCryptfs");
946
947 MODULE_LICENSE("GPL");
948
949 module_init(ecryptfs_init)
950 module_exit(ecryptfs_exit)