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