eCryptfs: remove header_extent_size
[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) {
191 if ((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 printk(KERN_ERR "Could not find valid key in user "
196 "session keyring for sig specified in mount "
197 "option: [%s]\n", global_auth_tok->sig);
198 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
199 rc = 0;
200 } else
201 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
237fead6 202 }
237fead6
MH
203 return rc;
204}
205
f4aad16a
MH
206static void ecryptfs_init_mount_crypt_stat(
207 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
208{
209 memset((void *)mount_crypt_stat, 0,
210 sizeof(struct ecryptfs_mount_crypt_stat));
211 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
212 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
213 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
214}
215
237fead6
MH
216/**
217 * ecryptfs_parse_options
218 * @sb: The ecryptfs super block
219 * @options: The options pased to the kernel
220 *
221 * Parse mount options:
222 * debug=N - ecryptfs_verbosity level for debug output
223 * sig=XXX - description(signature) of the key to use
224 *
225 * Returns the dentry object of the lower-level (lower/interposed)
226 * directory; We want to mount our stackable file system on top of
227 * that lower directory.
228 *
229 * The signature of the key to use must be the description of a key
230 * already in the keyring. Mounting will fail if the key can not be
231 * found.
232 *
233 * Returns zero on success; non-zero on error
234 */
235static int ecryptfs_parse_options(struct super_block *sb, char *options)
236{
237 char *p;
238 int rc = 0;
239 int sig_set = 0;
240 int cipher_name_set = 0;
241 int cipher_key_bytes;
242 int cipher_key_bytes_set = 0;
237fead6
MH
243 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
244 &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
245 substring_t args[MAX_OPT_ARGS];
246 int token;
247 char *sig_src;
237fead6
MH
248 char *debug_src;
249 char *cipher_name_dst;
250 char *cipher_name_src;
251 char *cipher_key_bytes_src;
237fead6
MH
252 int cipher_name_len;
253
254 if (!options) {
255 rc = -EINVAL;
256 goto out;
257 }
956159c3 258 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
237fead6
MH
259 while ((p = strsep(&options, ",")) != NULL) {
260 if (!*p)
261 continue;
262 token = match_token(p, tokens, args);
263 switch (token) {
264 case ecryptfs_opt_sig:
265 case ecryptfs_opt_ecryptfs_sig:
266 sig_src = args[0].from;
f4aad16a
MH
267 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
268 sig_src);
269 if (rc) {
270 printk(KERN_ERR "Error attempting to register "
271 "global sig; rc = [%d]\n", rc);
272 goto out;
273 }
237fead6
MH
274 sig_set = 1;
275 break;
276 case ecryptfs_opt_debug:
277 case ecryptfs_opt_ecryptfs_debug:
278 debug_src = args[0].from;
279 ecryptfs_verbosity =
280 (int)simple_strtol(debug_src, &debug_src,
281 0);
282 ecryptfs_printk(KERN_DEBUG,
283 "Verbosity set to [%d]" "\n",
284 ecryptfs_verbosity);
285 break;
286 case ecryptfs_opt_cipher:
287 case ecryptfs_opt_ecryptfs_cipher:
288 cipher_name_src = args[0].from;
289 cipher_name_dst =
290 mount_crypt_stat->
291 global_default_cipher_name;
292 strncpy(cipher_name_dst, cipher_name_src,
293 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
294 ecryptfs_printk(KERN_DEBUG,
295 "The mount_crypt_stat "
296 "global_default_cipher_name set to: "
297 "[%s]\n", cipher_name_dst);
298 cipher_name_set = 1;
299 break;
300 case ecryptfs_opt_ecryptfs_key_bytes:
301 cipher_key_bytes_src = args[0].from;
302 cipher_key_bytes =
303 (int)simple_strtol(cipher_key_bytes_src,
304 &cipher_key_bytes_src, 0);
305 mount_crypt_stat->global_default_cipher_key_size =
306 cipher_key_bytes;
307 ecryptfs_printk(KERN_DEBUG,
308 "The mount_crypt_stat "
309 "global_default_cipher_key_size "
310 "set to: [%d]\n", mount_crypt_stat->
311 global_default_cipher_key_size);
312 cipher_key_bytes_set = 1;
313 break;
314 case ecryptfs_opt_passthrough:
315 mount_crypt_stat->flags |=
316 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
317 break;
17398957
MH
318 case ecryptfs_opt_xattr_metadata:
319 mount_crypt_stat->flags |=
320 ECRYPTFS_XATTR_METADATA_ENABLED;
321 break;
322 case ecryptfs_opt_encrypted_view:
323 mount_crypt_stat->flags |=
324 ECRYPTFS_XATTR_METADATA_ENABLED;
325 mount_crypt_stat->flags |=
326 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
327 break;
237fead6
MH
328 case ecryptfs_opt_err:
329 default:
330 ecryptfs_printk(KERN_WARNING,
331 "eCryptfs: unrecognized option '%s'\n",
332 p);
333 }
334 }
237fead6
MH
335 if (!sig_set) {
336 rc = -EINVAL;
956159c3
MH
337 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
338 "auth tok signature as a mount "
237fead6
MH
339 "parameter; see the eCryptfs README\n");
340 goto out;
341 }
342 if (!cipher_name_set) {
343 cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
344 if (unlikely(cipher_name_len
345 >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
346 rc = -EINVAL;
347 BUG();
348 goto out;
349 }
350 memcpy(mount_crypt_stat->global_default_cipher_name,
351 ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
352 mount_crypt_stat->global_default_cipher_name[cipher_name_len]
353 = '\0';
354 }
355 if (!cipher_key_bytes_set) {
e5d9cbde 356 mount_crypt_stat->global_default_cipher_key_size = 0;
237fead6 357 }
f4aad16a
MH
358 if ((rc = ecryptfs_add_new_key_tfm(
359 NULL, mount_crypt_stat->global_default_cipher_name,
360 mount_crypt_stat->global_default_cipher_key_size))) {
361 printk(KERN_ERR "Error attempting to initialize cipher with "
81acbcd6 362 "name = [%s] and key size = [%td]; rc = [%d]\n",
237fead6
MH
363 mount_crypt_stat->global_default_cipher_name,
364 mount_crypt_stat->global_default_cipher_key_size, rc);
237fead6
MH
365 rc = -EINVAL;
366 goto out;
367 }
f4aad16a
MH
368 if ((rc = ecryptfs_init_global_auth_toks(mount_crypt_stat))) {
369 printk(KERN_WARNING "One or more global auth toks could not "
370 "properly register; rc = [%d]\n", rc);
237fead6 371 }
f4aad16a 372 rc = 0;
237fead6
MH
373out:
374 return rc;
375}
376
377struct kmem_cache *ecryptfs_sb_info_cache;
378
379/**
380 * ecryptfs_fill_super
381 * @sb: The ecryptfs super block
382 * @raw_data: The options passed to mount
383 * @silent: Not used but required by function prototype
384 *
385 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
386 *
387 * Returns zero on success; non-zero otherwise
388 */
389static int
390ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
391{
392 int rc = 0;
393
394 /* Released in ecryptfs_put_super() */
395 ecryptfs_set_superblock_private(sb,
c3762229 396 kmem_cache_zalloc(ecryptfs_sb_info_cache,
e94b1766 397 GFP_KERNEL));
237fead6
MH
398 if (!ecryptfs_superblock_to_private(sb)) {
399 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
400 rc = -ENOMEM;
401 goto out;
402 }
237fead6
MH
403 sb->s_op = &ecryptfs_sops;
404 /* Released through deactivate_super(sb) from get_sb_nodev */
405 sb->s_root = d_alloc(NULL, &(const struct qstr) {
406 .hash = 0,.name = "/",.len = 1});
407 if (!sb->s_root) {
408 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
409 rc = -ENOMEM;
410 goto out;
411 }
412 sb->s_root->d_op = &ecryptfs_dops;
413 sb->s_root->d_sb = sb;
414 sb->s_root->d_parent = sb->s_root;
415 /* Released in d_release when dput(sb->s_root) is called */
416 /* through deactivate_super(sb) from get_sb_nodev() */
417 ecryptfs_set_dentry_private(sb->s_root,
c3762229 418 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
e94b1766 419 GFP_KERNEL));
237fead6
MH
420 if (!ecryptfs_dentry_to_private(sb->s_root)) {
421 ecryptfs_printk(KERN_ERR,
422 "dentry_info_cache alloc failed\n");
423 rc = -ENOMEM;
424 goto out;
425 }
237fead6
MH
426 rc = 0;
427out:
428 /* Should be able to rely on deactivate_super called from
429 * get_sb_nodev */
430 return rc;
431}
432
433/**
434 * ecryptfs_read_super
435 * @sb: The ecryptfs super block
436 * @dev_name: The path to mount over
437 *
438 * Read the super block of the lower filesystem, and use
439 * ecryptfs_interpose to create our initial inode and super block
440 * struct.
441 */
442static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
443{
444 int rc;
445 struct nameidata nd;
446 struct dentry *lower_root;
447 struct vfsmount *lower_mnt;
448
449 memset(&nd, 0, sizeof(struct nameidata));
82b16528 450 rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
237fead6
MH
451 if (rc) {
452 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
65dc8145 453 goto out;
237fead6
MH
454 }
455 lower_root = nd.dentry;
237fead6
MH
456 lower_mnt = nd.mnt;
457 ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
458 sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
459 ecryptfs_set_dentry_lower(sb->s_root, lower_root);
460 ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
461 if ((rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0)))
462 goto out_free;
463 rc = 0;
464 goto out;
465out_free:
466 path_release(&nd);
467out:
468 return rc;
469}
470
471/**
472 * ecryptfs_get_sb
473 * @fs_type
474 * @flags
475 * @dev_name: The path to mount over
476 * @raw_data: The options passed into the kernel
477 *
478 * The whole ecryptfs_get_sb process is broken into 4 functions:
479 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
480 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
481 * with as much information as it can before needing
482 * the lower filesystem.
483 * ecryptfs_read_super(): this accesses the lower filesystem and uses
484 * ecryptfs_interpolate to perform most of the linking
485 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
486 */
487static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
488 const char *dev_name, void *raw_data,
489 struct vfsmount *mnt)
490{
491 int rc;
492 struct super_block *sb;
493
494 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
495 if (rc < 0) {
496 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
497 goto out;
498 }
499 sb = mnt->mnt_sb;
500 rc = ecryptfs_parse_options(sb, raw_data);
501 if (rc) {
502 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
503 goto out_abort;
504 }
505 rc = ecryptfs_read_super(sb, dev_name);
506 if (rc) {
507 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
508 goto out_abort;
509 }
510 goto out;
511out_abort:
512 dput(sb->s_root);
513 up_write(&sb->s_umount);
514 deactivate_super(sb);
515out:
516 return rc;
517}
518
519/**
520 * ecryptfs_kill_block_super
521 * @sb: The ecryptfs super block
522 *
523 * Used to bring the superblock down and free the private data.
524 * Private data is free'd in ecryptfs_put_super()
525 */
526static void ecryptfs_kill_block_super(struct super_block *sb)
527{
528 generic_shutdown_super(sb);
529}
530
531static struct file_system_type ecryptfs_fs_type = {
532 .owner = THIS_MODULE,
533 .name = "ecryptfs",
534 .get_sb = ecryptfs_get_sb,
535 .kill_sb = ecryptfs_kill_block_super,
536 .fs_flags = 0
537};
538
539/**
540 * inode_info_init_once
541 *
542 * Initializes the ecryptfs_inode_info_cache when it is created
543 */
544static void
545inode_info_init_once(void *vptr, struct kmem_cache *cachep, unsigned long flags)
546{
547 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
548
a35afb83 549 inode_init_once(&ei->vfs_inode);
237fead6
MH
550}
551
552static struct ecryptfs_cache_info {
e18b890b 553 struct kmem_cache **cache;
237fead6
MH
554 const char *name;
555 size_t size;
556 void (*ctor)(void*, struct kmem_cache *, unsigned long);
557} ecryptfs_cache_infos[] = {
558 {
559 .cache = &ecryptfs_auth_tok_list_item_cache,
560 .name = "ecryptfs_auth_tok_list_item",
561 .size = sizeof(struct ecryptfs_auth_tok_list_item),
562 },
563 {
564 .cache = &ecryptfs_file_info_cache,
565 .name = "ecryptfs_file_cache",
566 .size = sizeof(struct ecryptfs_file_info),
567 },
568 {
569 .cache = &ecryptfs_dentry_info_cache,
570 .name = "ecryptfs_dentry_info_cache",
571 .size = sizeof(struct ecryptfs_dentry_info),
572 },
573 {
574 .cache = &ecryptfs_inode_info_cache,
575 .name = "ecryptfs_inode_cache",
576 .size = sizeof(struct ecryptfs_inode_info),
577 .ctor = inode_info_init_once,
578 },
579 {
580 .cache = &ecryptfs_sb_info_cache,
581 .name = "ecryptfs_sb_cache",
582 .size = sizeof(struct ecryptfs_sb_info),
583 },
584 {
585 .cache = &ecryptfs_header_cache_0,
586 .name = "ecryptfs_headers_0",
587 .size = PAGE_CACHE_SIZE,
588 },
589 {
590 .cache = &ecryptfs_header_cache_1,
591 .name = "ecryptfs_headers_1",
592 .size = PAGE_CACHE_SIZE,
593 },
594 {
595 .cache = &ecryptfs_header_cache_2,
596 .name = "ecryptfs_headers_2",
597 .size = PAGE_CACHE_SIZE,
598 },
dd2a3b7a
MH
599 {
600 .cache = &ecryptfs_xattr_cache,
601 .name = "ecryptfs_xattr_cache",
602 .size = PAGE_CACHE_SIZE,
603 },
237fead6
MH
604 {
605 .cache = &ecryptfs_lower_page_cache,
606 .name = "ecryptfs_lower_page_cache",
607 .size = PAGE_CACHE_SIZE,
608 },
eb95e7ff
MH
609 {
610 .cache = &ecryptfs_key_record_cache,
611 .name = "ecryptfs_key_record_cache",
612 .size = sizeof(struct ecryptfs_key_record),
613 },
956159c3
MH
614 {
615 .cache = &ecryptfs_key_sig_cache,
616 .name = "ecryptfs_key_sig_cache",
617 .size = sizeof(struct ecryptfs_key_sig),
618 },
619 {
620 .cache = &ecryptfs_global_auth_tok_cache,
621 .name = "ecryptfs_global_auth_tok_cache",
622 .size = sizeof(struct ecryptfs_global_auth_tok),
623 },
624 {
625 .cache = &ecryptfs_key_tfm_cache,
626 .name = "ecryptfs_key_tfm_cache",
627 .size = sizeof(struct ecryptfs_key_tfm),
628 },
237fead6
MH
629};
630
631static void ecryptfs_free_kmem_caches(void)
632{
633 int i;
634
635 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
636 struct ecryptfs_cache_info *info;
637
638 info = &ecryptfs_cache_infos[i];
639 if (*(info->cache))
640 kmem_cache_destroy(*(info->cache));
641 }
642}
643
644/**
645 * ecryptfs_init_kmem_caches
646 *
647 * Returns zero on success; non-zero otherwise
648 */
649static int ecryptfs_init_kmem_caches(void)
650{
651 int i;
652
653 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
654 struct ecryptfs_cache_info *info;
655
656 info = &ecryptfs_cache_infos[i];
657 *(info->cache) = kmem_cache_create(info->name, info->size,
20c2df83 658 0, SLAB_HWCACHE_ALIGN, info->ctor);
237fead6
MH
659 if (!*(info->cache)) {
660 ecryptfs_free_kmem_caches();
661 ecryptfs_printk(KERN_WARNING, "%s: "
662 "kmem_cache_create failed\n",
663 info->name);
664 return -ENOMEM;
665 }
666 }
667 return 0;
668}
669
670struct ecryptfs_obj {
671 char *name;
672 struct list_head slot_list;
673 struct kobject kobj;
674};
675
676struct ecryptfs_attribute {
677 struct attribute attr;
678 ssize_t(*show) (struct ecryptfs_obj *, char *);
679 ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t);
680};
681
682static ssize_t
683ecryptfs_attr_store(struct kobject *kobj,
684 struct attribute *attr, const char *buf, size_t len)
685{
686 struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
687 kobj);
688 struct ecryptfs_attribute *attribute =
689 container_of(attr, struct ecryptfs_attribute, attr);
690
691 return (attribute->store ? attribute->store(obj, buf, len) : 0);
692}
693
694static ssize_t
695ecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
696{
697 struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
698 kobj);
699 struct ecryptfs_attribute *attribute =
700 container_of(attr, struct ecryptfs_attribute, attr);
701
702 return (attribute->show ? attribute->show(obj, buf) : 0);
703}
704
705static struct sysfs_ops ecryptfs_sysfs_ops = {
706 .show = ecryptfs_attr_show,
707 .store = ecryptfs_attr_store
708};
709
710static struct kobj_type ecryptfs_ktype = {
711 .sysfs_ops = &ecryptfs_sysfs_ops
712};
713
714static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL);
715
716static ssize_t version_show(struct ecryptfs_obj *obj, char *buff)
717{
718 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
719}
720
721static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version);
722
8487f2e4 723static struct ecryptfs_version_str_map_elem {
237fead6
MH
724 u32 flag;
725 char *str;
726} ecryptfs_version_str_map[] = {
727 {ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"},
728 {ECRYPTFS_VERSIONING_PUBKEY, "pubkey"},
729 {ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"},
17398957 730 {ECRYPTFS_VERSIONING_POLICY, "policy"},
956159c3
MH
731 {ECRYPTFS_VERSIONING_XATTR, "metadata in extended attribute"},
732 {ECRYPTFS_VERSIONING_MULTKEY, "multiple keys per file"}
237fead6
MH
733};
734
735static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff)
736{
737 int i;
738 int remaining = PAGE_SIZE;
739 int total_written = 0;
740
741 buff[0] = '\0';
742 for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) {
743 int entry_size;
744
745 if (!(ECRYPTFS_VERSIONING_MASK
746 & ecryptfs_version_str_map[i].flag))
747 continue;
748 entry_size = strlen(ecryptfs_version_str_map[i].str);
749 if ((entry_size + 2) > remaining)
750 goto out;
751 memcpy(buff, ecryptfs_version_str_map[i].str, entry_size);
752 buff[entry_size++] = '\n';
753 buff[entry_size] = '\0';
754 buff += entry_size;
755 total_written += entry_size;
756 remaining -= entry_size;
757 }
758out:
759 return total_written;
760}
761
762static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str);
763
764static int do_sysfs_registration(void)
765{
766 int rc;
767
768 if ((rc = subsystem_register(&ecryptfs_subsys))) {
769 printk(KERN_ERR
770 "Unable to register ecryptfs sysfs subsystem\n");
771 goto out;
772 }
823bccfc 773 rc = sysfs_create_file(&ecryptfs_subsys.kobj,
237fead6
MH
774 &sysfs_attr_version.attr);
775 if (rc) {
776 printk(KERN_ERR
777 "Unable to create ecryptfs version attribute\n");
778 subsystem_unregister(&ecryptfs_subsys);
779 goto out;
780 }
823bccfc 781 rc = sysfs_create_file(&ecryptfs_subsys.kobj,
237fead6
MH
782 &sysfs_attr_version_str.attr);
783 if (rc) {
784 printk(KERN_ERR
785 "Unable to create ecryptfs version_str attribute\n");
823bccfc 786 sysfs_remove_file(&ecryptfs_subsys.kobj,
237fead6
MH
787 &sysfs_attr_version.attr);
788 subsystem_unregister(&ecryptfs_subsys);
789 goto out;
790 }
791out:
792 return rc;
793}
794
a75de1b3
RK
795static void do_sysfs_unregistration(void)
796{
956159c3
MH
797 int rc;
798
fcd12835
MH
799 if ((rc = ecryptfs_destroy_crypto())) {
800 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
956159c3
MH
801 "rc = [%d]\n", rc);
802 }
a75de1b3
RK
803 sysfs_remove_file(&ecryptfs_subsys.kobj,
804 &sysfs_attr_version.attr);
805 sysfs_remove_file(&ecryptfs_subsys.kobj,
806 &sysfs_attr_version_str.attr);
807 subsystem_unregister(&ecryptfs_subsys);
808}
809
237fead6
MH
810static int __init ecryptfs_init(void)
811{
812 int rc;
813
814 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
815 rc = -EINVAL;
816 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
817 "larger than the host's page size, and so "
818 "eCryptfs cannot run on this system. The "
819 "default eCryptfs extent size is [%d] bytes; "
820 "the page size is [%d] bytes.\n",
821 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
822 goto out;
823 }
824 rc = ecryptfs_init_kmem_caches();
825 if (rc) {
826 printk(KERN_ERR
827 "Failed to allocate one or more kmem_cache objects\n");
828 goto out;
829 }
830 rc = register_filesystem(&ecryptfs_fs_type);
831 if (rc) {
832 printk(KERN_ERR "Failed to register filesystem\n");
833 ecryptfs_free_kmem_caches();
834 goto out;
835 }
823bccfc 836 kobj_set_kset_s(&ecryptfs_subsys, fs_subsys);
237fead6
MH
837 rc = do_sysfs_registration();
838 if (rc) {
839 printk(KERN_ERR "sysfs registration failed\n");
840 unregister_filesystem(&ecryptfs_fs_type);
841 ecryptfs_free_kmem_caches();
842 goto out;
843 }
dddfa461
MH
844 rc = ecryptfs_init_messaging(ecryptfs_transport);
845 if (rc) {
846 ecryptfs_printk(KERN_ERR, "Failure occured while attempting to "
847 "initialize the eCryptfs netlink socket\n");
a75de1b3
RK
848 do_sysfs_unregistration();
849 unregister_filesystem(&ecryptfs_fs_type);
850 ecryptfs_free_kmem_caches();
956159c3
MH
851 goto out;
852 }
853 rc = ecryptfs_init_crypto();
854 if (rc) {
855 printk(KERN_ERR "Failure whilst attempting to init crypto; "
856 "rc = [%d]\n", rc);
857 do_sysfs_unregistration();
858 unregister_filesystem(&ecryptfs_fs_type);
859 ecryptfs_free_kmem_caches();
860 goto out;
dddfa461 861 }
237fead6
MH
862out:
863 return rc;
864}
865
866static void __exit ecryptfs_exit(void)
867{
a75de1b3 868 do_sysfs_unregistration();
dddfa461 869 ecryptfs_release_messaging(ecryptfs_transport);
237fead6
MH
870 unregister_filesystem(&ecryptfs_fs_type);
871 ecryptfs_free_kmem_caches();
872}
873
874MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
875MODULE_DESCRIPTION("eCryptfs");
876
877MODULE_LICENSE("GPL");
878
879module_init(ecryptfs_init)
880module_exit(ecryptfs_exit)
This page took 0.17007 seconds and 5 git commands to generate.