blob: fc4a3a22464182ebb80e130c2d2013e102905776 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 1997-2003 Erez Zadok
5 * Copyright (C) 2001-2003 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08006 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -07007 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com>
Michael Halcrowdddfa462007-02-12 00:53:44 -08009 * Tyler Hicks <tyhicks@ou.edu>
Michael Halcrow237fead2006-10-04 02:16:22 -070010 *
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>
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -080039#include <linux/fs_stack.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070040#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
Michael Halcrowdddfa462007-02-12 00:53:44 -080052/**
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
Michael Halcrow237fead2006-10-04 02:16:22 -070089void __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;
Pekka Enberg26da8202006-10-19 23:28:14 -0700146 if (special_file(lower_inode->i_mode))
Michael Halcrow237fead2006-10-04 02:16:22 -0700147 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);
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800154 fsstack_copy_attr_all(inode, lower_inode, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700155 /* This size will be overwritten for real files w/ headers and
156 * other metadata */
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800157 fsstack_copy_inode_size(inode, lower_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700158out:
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,
Michael Halcrow17398952007-02-12 00:53:45 -0800165 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
166 ecryptfs_opt_encrypted_view, ecryptfs_opt_err };
Michael Halcrow237fead2006-10-04 02:16:22 -0700167
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"},
Michael Halcrow17398952007-02-12 00:53:45 -0800177 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
178 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
Michael Halcrow237fead2006-10-04 02:16:22 -0700179 {ecryptfs_opt_err, NULL}
180};
181
182/**
183 * ecryptfs_verify_version
184 * @version: The version number to confirm
185 *
186 * Returns zero on good version; non-zero otherwise
187 */
188static int ecryptfs_verify_version(u16 version)
189{
190 int rc = 0;
191 unsigned char major;
192 unsigned char minor;
193
194 major = ((version >> 8) & 0xFF);
195 minor = (version & 0xFF);
196 if (major != ECRYPTFS_VERSION_MAJOR) {
197 ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
198 "Expected [%d]; got [%d]\n",
199 ECRYPTFS_VERSION_MAJOR, major);
200 rc = -EINVAL;
201 goto out;
202 }
203 if (minor != ECRYPTFS_VERSION_MINOR) {
204 ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
205 "Expected [%d]; got [%d]\n",
206 ECRYPTFS_VERSION_MINOR, minor);
207 rc = -EINVAL;
208 goto out;
209 }
210out:
211 return rc;
212}
213
214/**
215 * ecryptfs_parse_options
216 * @sb: The ecryptfs super block
217 * @options: The options pased to the kernel
218 *
219 * Parse mount options:
220 * debug=N - ecryptfs_verbosity level for debug output
221 * sig=XXX - description(signature) of the key to use
222 *
223 * Returns the dentry object of the lower-level (lower/interposed)
224 * directory; We want to mount our stackable file system on top of
225 * that lower directory.
226 *
227 * The signature of the key to use must be the description of a key
228 * already in the keyring. Mounting will fail if the key can not be
229 * found.
230 *
231 * Returns zero on success; non-zero on error
232 */
233static int ecryptfs_parse_options(struct super_block *sb, char *options)
234{
235 char *p;
236 int rc = 0;
237 int sig_set = 0;
238 int cipher_name_set = 0;
239 int cipher_key_bytes;
240 int cipher_key_bytes_set = 0;
241 struct key *auth_tok_key = NULL;
242 struct ecryptfs_auth_tok *auth_tok = NULL;
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;
248 char *sig_dst;
249 char *debug_src;
250 char *cipher_name_dst;
251 char *cipher_name_src;
252 char *cipher_key_bytes_src;
Michael Halcrow237fead2006-10-04 02:16:22 -0700253 int cipher_name_len;
254
255 if (!options) {
256 rc = -EINVAL;
257 goto out;
258 }
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;
267 sig_dst =
268 mount_crypt_stat->global_auth_tok_sig;
269 memcpy(sig_dst, sig_src, ECRYPTFS_SIG_SIZE_HEX);
270 sig_dst[ECRYPTFS_SIG_SIZE_HEX] = '\0';
271 ecryptfs_printk(KERN_DEBUG,
272 "The mount_crypt_stat "
273 "global_auth_tok_sig set to: "
274 "[%s]\n", sig_dst);
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;
Michael Halcrow17398952007-02-12 00:53:45 -0800319 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;
Michael Halcrow237fead2006-10-04 02:16:22 -0700329 case ecryptfs_opt_err:
330 default:
331 ecryptfs_printk(KERN_WARNING,
332 "eCryptfs: unrecognized option '%s'\n",
333 p);
334 }
335 }
336 /* Do not support lack of mount-wide signature in 0.1
337 * release */
338 if (!sig_set) {
339 rc = -EINVAL;
340 ecryptfs_printk(KERN_ERR, "You must supply a valid "
341 "passphrase auth tok signature as a mount "
342 "parameter; see the eCryptfs README\n");
343 goto out;
344 }
345 if (!cipher_name_set) {
346 cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
347 if (unlikely(cipher_name_len
348 >= ECRYPTFS_MAX_CIPHER_NAME_SIZE)) {
349 rc = -EINVAL;
350 BUG();
351 goto out;
352 }
353 memcpy(mount_crypt_stat->global_default_cipher_name,
354 ECRYPTFS_DEFAULT_CIPHER, cipher_name_len);
355 mount_crypt_stat->global_default_cipher_name[cipher_name_len]
356 = '\0';
357 }
358 if (!cipher_key_bytes_set) {
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -0800359 mount_crypt_stat->global_default_cipher_key_size = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700360 }
361 rc = ecryptfs_process_cipher(
Michael Halcrow237fead2006-10-04 02:16:22 -0700362 &mount_crypt_stat->global_key_tfm,
363 mount_crypt_stat->global_default_cipher_name,
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -0800364 &mount_crypt_stat->global_default_cipher_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700365 if (rc) {
366 printk(KERN_ERR "Error attempting to initialize cipher [%s] "
367 "with key size [%Zd] bytes; rc = [%d]\n",
368 mount_crypt_stat->global_default_cipher_name,
369 mount_crypt_stat->global_default_cipher_key_size, rc);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800370 mount_crypt_stat->global_key_tfm = NULL;
371 mount_crypt_stat->global_auth_tok_key = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700372 rc = -EINVAL;
373 goto out;
374 }
375 mutex_init(&mount_crypt_stat->global_key_tfm_mutex);
376 ecryptfs_printk(KERN_DEBUG, "Requesting the key with description: "
377 "[%s]\n", mount_crypt_stat->global_auth_tok_sig);
378 /* The reference to this key is held until umount is done The
379 * call to key_put is done in ecryptfs_put_super() */
380 auth_tok_key = request_key(&key_type_user,
381 mount_crypt_stat->global_auth_tok_sig,
382 NULL);
383 if (!auth_tok_key || IS_ERR(auth_tok_key)) {
384 ecryptfs_printk(KERN_ERR, "Could not find key with "
385 "description: [%s]\n",
386 mount_crypt_stat->global_auth_tok_sig);
387 process_request_key_err(PTR_ERR(auth_tok_key));
388 rc = -EINVAL;
389 goto out;
390 }
391 auth_tok = ecryptfs_get_key_payload_data(auth_tok_key);
392 if (ecryptfs_verify_version(auth_tok->version)) {
393 ecryptfs_printk(KERN_ERR, "Data structure version mismatch. "
394 "Userspace tools must match eCryptfs kernel "
395 "module with major version [%d] and minor "
396 "version [%d]\n", ECRYPTFS_VERSION_MAJOR,
397 ECRYPTFS_VERSION_MINOR);
398 rc = -EINVAL;
399 goto out;
400 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800401 if (auth_tok->token_type != ECRYPTFS_PASSWORD
402 && auth_tok->token_type != ECRYPTFS_PRIVATE_KEY) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700403 ecryptfs_printk(KERN_ERR, "Invalid auth_tok structure "
Michael Halcrowdddfa462007-02-12 00:53:44 -0800404 "returned from key query\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700405 rc = -EINVAL;
406 goto out;
407 }
408 mount_crypt_stat->global_auth_tok_key = auth_tok_key;
409 mount_crypt_stat->global_auth_tok = auth_tok;
410out:
411 return rc;
412}
413
414struct kmem_cache *ecryptfs_sb_info_cache;
415
416/**
417 * ecryptfs_fill_super
418 * @sb: The ecryptfs super block
419 * @raw_data: The options passed to mount
420 * @silent: Not used but required by function prototype
421 *
422 * Sets up what we can of the sb, rest is done in ecryptfs_read_super
423 *
424 * Returns zero on success; non-zero otherwise
425 */
426static int
427ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent)
428{
429 int rc = 0;
430
431 /* Released in ecryptfs_put_super() */
432 ecryptfs_set_superblock_private(sb,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800433 kmem_cache_zalloc(ecryptfs_sb_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800434 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700435 if (!ecryptfs_superblock_to_private(sb)) {
436 ecryptfs_printk(KERN_WARNING, "Out of memory\n");
437 rc = -ENOMEM;
438 goto out;
439 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700440 sb->s_op = &ecryptfs_sops;
441 /* Released through deactivate_super(sb) from get_sb_nodev */
442 sb->s_root = d_alloc(NULL, &(const struct qstr) {
443 .hash = 0,.name = "/",.len = 1});
444 if (!sb->s_root) {
445 ecryptfs_printk(KERN_ERR, "d_alloc failed\n");
446 rc = -ENOMEM;
447 goto out;
448 }
449 sb->s_root->d_op = &ecryptfs_dops;
450 sb->s_root->d_sb = sb;
451 sb->s_root->d_parent = sb->s_root;
452 /* Released in d_release when dput(sb->s_root) is called */
453 /* through deactivate_super(sb) from get_sb_nodev() */
454 ecryptfs_set_dentry_private(sb->s_root,
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800455 kmem_cache_zalloc(ecryptfs_dentry_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800456 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700457 if (!ecryptfs_dentry_to_private(sb->s_root)) {
458 ecryptfs_printk(KERN_ERR,
459 "dentry_info_cache alloc failed\n");
460 rc = -ENOMEM;
461 goto out;
462 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700463 rc = 0;
464out:
465 /* Should be able to rely on deactivate_super called from
466 * get_sb_nodev */
467 return rc;
468}
469
470/**
471 * ecryptfs_read_super
472 * @sb: The ecryptfs super block
473 * @dev_name: The path to mount over
474 *
475 * Read the super block of the lower filesystem, and use
476 * ecryptfs_interpose to create our initial inode and super block
477 * struct.
478 */
479static int ecryptfs_read_super(struct super_block *sb, const char *dev_name)
480{
481 int rc;
482 struct nameidata nd;
483 struct dentry *lower_root;
484 struct vfsmount *lower_mnt;
485
486 memset(&nd, 0, sizeof(struct nameidata));
Dmitriy Monakhov82b16522007-03-05 00:30:46 -0800487 rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
Michael Halcrow237fead2006-10-04 02:16:22 -0700488 if (rc) {
489 ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n");
Michael Halcrow65dc8142007-02-28 20:12:57 -0800490 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700491 }
492 lower_root = nd.dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700493 lower_mnt = nd.mnt;
494 ecryptfs_set_superblock_lower(sb, lower_root->d_sb);
495 sb->s_maxbytes = lower_root->d_sb->s_maxbytes;
496 ecryptfs_set_dentry_lower(sb->s_root, lower_root);
497 ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt);
498 if ((rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0)))
499 goto out_free;
500 rc = 0;
501 goto out;
502out_free:
503 path_release(&nd);
504out:
505 return rc;
506}
507
508/**
509 * ecryptfs_get_sb
510 * @fs_type
511 * @flags
512 * @dev_name: The path to mount over
513 * @raw_data: The options passed into the kernel
514 *
515 * The whole ecryptfs_get_sb process is broken into 4 functions:
516 * ecryptfs_parse_options(): handle options passed to ecryptfs, if any
517 * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block
518 * with as much information as it can before needing
519 * the lower filesystem.
520 * ecryptfs_read_super(): this accesses the lower filesystem and uses
521 * ecryptfs_interpolate to perform most of the linking
522 * ecryptfs_interpolate(): links the lower filesystem into ecryptfs
523 */
524static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags,
525 const char *dev_name, void *raw_data,
526 struct vfsmount *mnt)
527{
528 int rc;
529 struct super_block *sb;
530
531 rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt);
532 if (rc < 0) {
533 printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc);
534 goto out;
535 }
536 sb = mnt->mnt_sb;
537 rc = ecryptfs_parse_options(sb, raw_data);
538 if (rc) {
539 printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc);
540 goto out_abort;
541 }
542 rc = ecryptfs_read_super(sb, dev_name);
543 if (rc) {
544 printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc);
545 goto out_abort;
546 }
547 goto out;
548out_abort:
549 dput(sb->s_root);
550 up_write(&sb->s_umount);
551 deactivate_super(sb);
552out:
553 return rc;
554}
555
556/**
557 * ecryptfs_kill_block_super
558 * @sb: The ecryptfs super block
559 *
560 * Used to bring the superblock down and free the private data.
561 * Private data is free'd in ecryptfs_put_super()
562 */
563static void ecryptfs_kill_block_super(struct super_block *sb)
564{
565 generic_shutdown_super(sb);
566}
567
568static struct file_system_type ecryptfs_fs_type = {
569 .owner = THIS_MODULE,
570 .name = "ecryptfs",
571 .get_sb = ecryptfs_get_sb,
572 .kill_sb = ecryptfs_kill_block_super,
573 .fs_flags = 0
574};
575
576/**
577 * inode_info_init_once
578 *
579 * Initializes the ecryptfs_inode_info_cache when it is created
580 */
581static void
582inode_info_init_once(void *vptr, struct kmem_cache *cachep, unsigned long flags)
583{
584 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
585
586 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
587 SLAB_CTOR_CONSTRUCTOR)
588 inode_init_once(&ei->vfs_inode);
589}
590
591static struct ecryptfs_cache_info {
Christoph Lametere18b8902006-12-06 20:33:20 -0800592 struct kmem_cache **cache;
Michael Halcrow237fead2006-10-04 02:16:22 -0700593 const char *name;
594 size_t size;
595 void (*ctor)(void*, struct kmem_cache *, unsigned long);
596} ecryptfs_cache_infos[] = {
597 {
598 .cache = &ecryptfs_auth_tok_list_item_cache,
599 .name = "ecryptfs_auth_tok_list_item",
600 .size = sizeof(struct ecryptfs_auth_tok_list_item),
601 },
602 {
603 .cache = &ecryptfs_file_info_cache,
604 .name = "ecryptfs_file_cache",
605 .size = sizeof(struct ecryptfs_file_info),
606 },
607 {
608 .cache = &ecryptfs_dentry_info_cache,
609 .name = "ecryptfs_dentry_info_cache",
610 .size = sizeof(struct ecryptfs_dentry_info),
611 },
612 {
613 .cache = &ecryptfs_inode_info_cache,
614 .name = "ecryptfs_inode_cache",
615 .size = sizeof(struct ecryptfs_inode_info),
616 .ctor = inode_info_init_once,
617 },
618 {
619 .cache = &ecryptfs_sb_info_cache,
620 .name = "ecryptfs_sb_cache",
621 .size = sizeof(struct ecryptfs_sb_info),
622 },
623 {
624 .cache = &ecryptfs_header_cache_0,
625 .name = "ecryptfs_headers_0",
626 .size = PAGE_CACHE_SIZE,
627 },
628 {
629 .cache = &ecryptfs_header_cache_1,
630 .name = "ecryptfs_headers_1",
631 .size = PAGE_CACHE_SIZE,
632 },
633 {
634 .cache = &ecryptfs_header_cache_2,
635 .name = "ecryptfs_headers_2",
636 .size = PAGE_CACHE_SIZE,
637 },
638 {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800639 .cache = &ecryptfs_xattr_cache,
640 .name = "ecryptfs_xattr_cache",
641 .size = PAGE_CACHE_SIZE,
642 },
643 {
Michael Halcrow237fead2006-10-04 02:16:22 -0700644 .cache = &ecryptfs_lower_page_cache,
645 .name = "ecryptfs_lower_page_cache",
646 .size = PAGE_CACHE_SIZE,
647 },
Michael Halcroweb95e7f2007-02-16 01:28:40 -0800648 {
649 .cache = &ecryptfs_key_record_cache,
650 .name = "ecryptfs_key_record_cache",
651 .size = sizeof(struct ecryptfs_key_record),
652 },
Michael Halcrow237fead2006-10-04 02:16:22 -0700653};
654
655static void ecryptfs_free_kmem_caches(void)
656{
657 int i;
658
659 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
660 struct ecryptfs_cache_info *info;
661
662 info = &ecryptfs_cache_infos[i];
663 if (*(info->cache))
664 kmem_cache_destroy(*(info->cache));
665 }
666}
667
668/**
669 * ecryptfs_init_kmem_caches
670 *
671 * Returns zero on success; non-zero otherwise
672 */
673static int ecryptfs_init_kmem_caches(void)
674{
675 int i;
676
677 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
678 struct ecryptfs_cache_info *info;
679
680 info = &ecryptfs_cache_infos[i];
681 *(info->cache) = kmem_cache_create(info->name, info->size,
682 0, SLAB_HWCACHE_ALIGN, info->ctor, NULL);
683 if (!*(info->cache)) {
684 ecryptfs_free_kmem_caches();
685 ecryptfs_printk(KERN_WARNING, "%s: "
686 "kmem_cache_create failed\n",
687 info->name);
688 return -ENOMEM;
689 }
690 }
691 return 0;
692}
693
694struct ecryptfs_obj {
695 char *name;
696 struct list_head slot_list;
697 struct kobject kobj;
698};
699
700struct ecryptfs_attribute {
701 struct attribute attr;
702 ssize_t(*show) (struct ecryptfs_obj *, char *);
703 ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t);
704};
705
706static ssize_t
707ecryptfs_attr_store(struct kobject *kobj,
708 struct attribute *attr, const char *buf, size_t len)
709{
710 struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
711 kobj);
712 struct ecryptfs_attribute *attribute =
713 container_of(attr, struct ecryptfs_attribute, attr);
714
715 return (attribute->store ? attribute->store(obj, buf, len) : 0);
716}
717
718static ssize_t
719ecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
720{
721 struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj,
722 kobj);
723 struct ecryptfs_attribute *attribute =
724 container_of(attr, struct ecryptfs_attribute, attr);
725
726 return (attribute->show ? attribute->show(obj, buf) : 0);
727}
728
729static struct sysfs_ops ecryptfs_sysfs_ops = {
730 .show = ecryptfs_attr_show,
731 .store = ecryptfs_attr_store
732};
733
734static struct kobj_type ecryptfs_ktype = {
735 .sysfs_ops = &ecryptfs_sysfs_ops
736};
737
738static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL);
739
740static ssize_t version_show(struct ecryptfs_obj *obj, char *buff)
741{
742 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
743}
744
745static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version);
746
Adrian Bunk8487f2e2006-12-06 20:38:25 -0800747static struct ecryptfs_version_str_map_elem {
Michael Halcrow237fead2006-10-04 02:16:22 -0700748 u32 flag;
749 char *str;
750} ecryptfs_version_str_map[] = {
751 {ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"},
752 {ECRYPTFS_VERSIONING_PUBKEY, "pubkey"},
753 {ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"},
Michael Halcrow17398952007-02-12 00:53:45 -0800754 {ECRYPTFS_VERSIONING_POLICY, "policy"},
755 {ECRYPTFS_VERSIONING_XATTR, "metadata in extended attribute"}
Michael Halcrow237fead2006-10-04 02:16:22 -0700756};
757
758static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff)
759{
760 int i;
761 int remaining = PAGE_SIZE;
762 int total_written = 0;
763
764 buff[0] = '\0';
765 for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) {
766 int entry_size;
767
768 if (!(ECRYPTFS_VERSIONING_MASK
769 & ecryptfs_version_str_map[i].flag))
770 continue;
771 entry_size = strlen(ecryptfs_version_str_map[i].str);
772 if ((entry_size + 2) > remaining)
773 goto out;
774 memcpy(buff, ecryptfs_version_str_map[i].str, entry_size);
775 buff[entry_size++] = '\n';
776 buff[entry_size] = '\0';
777 buff += entry_size;
778 total_written += entry_size;
779 remaining -= entry_size;
780 }
781out:
782 return total_written;
783}
784
785static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str);
786
787static int do_sysfs_registration(void)
788{
789 int rc;
790
791 if ((rc = subsystem_register(&ecryptfs_subsys))) {
792 printk(KERN_ERR
793 "Unable to register ecryptfs sysfs subsystem\n");
794 goto out;
795 }
796 rc = sysfs_create_file(&ecryptfs_subsys.kset.kobj,
797 &sysfs_attr_version.attr);
798 if (rc) {
799 printk(KERN_ERR
800 "Unable to create ecryptfs version attribute\n");
801 subsystem_unregister(&ecryptfs_subsys);
802 goto out;
803 }
804 rc = sysfs_create_file(&ecryptfs_subsys.kset.kobj,
805 &sysfs_attr_version_str.attr);
806 if (rc) {
807 printk(KERN_ERR
808 "Unable to create ecryptfs version_str attribute\n");
809 sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
810 &sysfs_attr_version.attr);
811 subsystem_unregister(&ecryptfs_subsys);
812 goto out;
813 }
814out:
815 return rc;
816}
817
818static int __init ecryptfs_init(void)
819{
820 int rc;
821
822 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
823 rc = -EINVAL;
824 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
825 "larger than the host's page size, and so "
826 "eCryptfs cannot run on this system. The "
827 "default eCryptfs extent size is [%d] bytes; "
828 "the page size is [%d] bytes.\n",
829 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
830 goto out;
831 }
832 rc = ecryptfs_init_kmem_caches();
833 if (rc) {
834 printk(KERN_ERR
835 "Failed to allocate one or more kmem_cache objects\n");
836 goto out;
837 }
838 rc = register_filesystem(&ecryptfs_fs_type);
839 if (rc) {
840 printk(KERN_ERR "Failed to register filesystem\n");
841 ecryptfs_free_kmem_caches();
842 goto out;
843 }
844 kset_set_kset_s(&ecryptfs_subsys, fs_subsys);
845 sysfs_attr_version.attr.owner = THIS_MODULE;
846 sysfs_attr_version_str.attr.owner = THIS_MODULE;
847 rc = do_sysfs_registration();
848 if (rc) {
849 printk(KERN_ERR "sysfs registration failed\n");
850 unregister_filesystem(&ecryptfs_fs_type);
851 ecryptfs_free_kmem_caches();
852 goto out;
853 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800854 rc = ecryptfs_init_messaging(ecryptfs_transport);
855 if (rc) {
856 ecryptfs_printk(KERN_ERR, "Failure occured while attempting to "
857 "initialize the eCryptfs netlink socket\n");
858 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700859out:
860 return rc;
861}
862
863static void __exit ecryptfs_exit(void)
864{
865 sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
866 &sysfs_attr_version.attr);
867 sysfs_remove_file(&ecryptfs_subsys.kset.kobj,
868 &sysfs_attr_version_str.attr);
869 subsystem_unregister(&ecryptfs_subsys);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800870 ecryptfs_release_messaging(ecryptfs_transport);
Michael Halcrow237fead2006-10-04 02:16:22 -0700871 unregister_filesystem(&ecryptfs_fs_type);
872 ecryptfs_free_kmem_caches();
873}
874
875MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
876MODULE_DESCRIPTION("eCryptfs");
877
878MODULE_LICENSE("GPL");
879
880module_init(ecryptfs_init)
881module_exit(ecryptfs_exit)