ecryptfs: fix broken build
[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>
237fead6 33#include <linux/mount.h>
237fead6
MH
34#include <linux/pagemap.h>
35#include <linux/key.h>
36#include <linux/parser.h>
0cc72dc7 37#include <linux/fs_stack.h>
5a0e3ad6 38#include <linux/slab.h>
237fead6
MH
39#include "ecryptfs_kernel.h"
40
41/**
42 * Module parameter that defines the ecryptfs_verbosity level.
43 */
44int ecryptfs_verbosity = 0;
45
46module_param(ecryptfs_verbosity, int, 0);
47MODULE_PARM_DESC(ecryptfs_verbosity,
48 "Initial verbosity level (0 or 1; defaults to "
49 "0, which is Quiet)");
50
dddfa461 51/**
624ae528 52 * Module parameter that defines the number of message buffer elements
dddfa461
MH
53 */
54unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
55
56module_param(ecryptfs_message_buf_len, uint, 0);
57MODULE_PARM_DESC(ecryptfs_message_buf_len,
58 "Number of message buffer elements");
59
60/**
61 * Module parameter that defines the maximum guaranteed amount of time to wait
624ae528 62 * for a response from ecryptfsd. The actual sleep time will be, more than
dddfa461 63 * likely, a small amount greater than this specified value, but only less if
624ae528 64 * the message successfully arrives.
dddfa461
MH
65 */
66signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
67
68module_param(ecryptfs_message_wait_timeout, long, 0);
69MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
70 "Maximum number of seconds that an operation will "
71 "sleep while waiting for a message response from "
72 "userspace");
73
74/**
75 * Module parameter that is an estimate of the maximum number of users
76 * that will be concurrently using eCryptfs. Set this to the right
77 * value to balance performance and memory use.
78 */
79unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
80
81module_param(ecryptfs_number_of_users, uint, 0);
82MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
83 "concurrent users of eCryptfs");
84
237fead6
MH
85void __ecryptfs_printk(const char *fmt, ...)
86{
87 va_list args;
88 va_start(args, fmt);
89 if (fmt[1] == '7') { /* KERN_DEBUG */
90 if (ecryptfs_verbosity >= 1)
91 vprintk(fmt, args);
92 } else
93 vprintk(fmt, args);
94 va_end(args);
95}
96
4981e081
MH
97/**
98 * ecryptfs_init_persistent_file
99 * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
100 * the lower dentry and the lower mount set
101 *
102 * eCryptfs only ever keeps a single open file for every lower
103 * inode. All I/O operations to the lower inode occur through that
104 * file. When the first eCryptfs dentry that interposes with the first
105 * lower dentry for that inode is created, this function creates the
106 * persistent file struct and associates it with the eCryptfs
107 * inode. When the eCryptfs inode is destroyed, the file is closed.
108 *
109 * The persistent file will be opened with read/write permissions, if
110 * possible. Otherwise, it is opened read-only.
111 *
112 * This function does nothing if a lower persistent file is already
113 * associated with the eCryptfs inode.
114 *
115 * Returns zero on success; non-zero otherwise
116 */
72b55fff 117int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
4981e081 118{
745ca247 119 const struct cred *cred = current_cred();
4981e081
MH
120 struct ecryptfs_inode_info *inode_info =
121 ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
122 int rc = 0;
123
124 mutex_lock(&inode_info->lower_file_mutex);
125 if (!inode_info->lower_file) {
126 struct dentry *lower_dentry;
127 struct vfsmount *lower_mnt =
128 ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
129
130 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
746f1e55 131 rc = ecryptfs_privileged_open(&inode_info->lower_file,
745ca247 132 lower_dentry, lower_mnt, cred);
ac22ba23 133 if (rc) {
4981e081 134 printk(KERN_ERR "Error opening lower persistent file "
746f1e55
MH
135 "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
136 "rc = [%d]\n", lower_dentry, lower_mnt, rc);
4981e081 137 inode_info->lower_file = NULL;
b65a9cfc 138 }
4981e081
MH
139 }
140 mutex_unlock(&inode_info->lower_file_mutex);
141 return rc;
142}
143
6254b32b 144static struct inode *ecryptfs_get_inode(struct inode *lower_inode,
66cb7666 145 struct super_block *sb)
237fead6 146{
237fead6
MH
147 struct inode *inode;
148 int rc = 0;
149
237fead6
MH
150 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
151 rc = -EXDEV;
152 goto out;
153 }
154 if (!igrab(lower_inode)) {
155 rc = -ESTALE;
156 goto out;
157 }
158 inode = iget5_locked(sb, (unsigned long)lower_inode,
159 ecryptfs_inode_test, ecryptfs_inode_set,
160 lower_inode);
161 if (!inode) {
162 rc = -EACCES;
163 iput(lower_inode);
164 goto out;
165 }
166 if (inode->i_state & I_NEW)
167 unlock_new_inode(inode);
168 else
169 iput(lower_inode);
170 if (S_ISLNK(lower_inode->i_mode))
171 inode->i_op = &ecryptfs_symlink_iops;
172 else if (S_ISDIR(lower_inode->i_mode))
173 inode->i_op = &ecryptfs_dir_iops;
174 if (S_ISDIR(lower_inode->i_mode))
175 inode->i_fop = &ecryptfs_dir_fops;
26da8205 176 if (special_file(lower_inode->i_mode))
237fead6
MH
177 init_special_inode(inode, lower_inode->i_mode,
178 lower_inode->i_rdev);
9afa2fb6 179 fsstack_copy_attr_all(inode, lower_inode);
237fead6
MH
180 /* This size will be overwritten for real files w/ headers and
181 * other metadata */
0cc72dc7 182 fsstack_copy_inode_size(inode, lower_inode);
66cb7666
AV
183 return inode;
184out:
185 return ERR_PTR(rc);
186}
187
188/**
189 * ecryptfs_interpose
190 * @lower_dentry: Existing dentry in the lower filesystem
191 * @dentry: ecryptfs' dentry
192 * @sb: ecryptfs's super_block
193 * @flags: flags to govern behavior of interpose procedure
194 *
195 * Interposes upper and lower dentries.
196 *
197 * Returns zero on success; non-zero otherwise
198 */
199int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
200 struct super_block *sb, u32 flags)
201{
202 struct inode *lower_inode = lower_dentry->d_inode;
203 struct inode *inode = ecryptfs_get_inode(lower_inode, sb);
6254b32b 204 if (IS_ERR(inode))
66cb7666 205 return PTR_ERR(inode);
ae6e8459
TH
206 if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
207 d_add(dentry, inode);
208 else
209 d_instantiate(dentry, inode);
66cb7666 210 return 0;
237fead6
MH
211}
212
2830bfd6
ES
213enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
214 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
215 ecryptfs_opt_ecryptfs_key_bytes,
17398957 216 ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
87c94c4d
MH
217 ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
218 ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
f16feb51
RS
219 ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
220 ecryptfs_opt_err };
237fead6 221
a447c093 222static const match_table_t tokens = {
237fead6
MH
223 {ecryptfs_opt_sig, "sig=%s"},
224 {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
237fead6
MH
225 {ecryptfs_opt_cipher, "cipher=%s"},
226 {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
227 {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
228 {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
17398957
MH
229 {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
230 {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
87c94c4d
MH
231 {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
232 {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
233 {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
e77cc8d2 234 {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
f16feb51 235 {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
237fead6
MH
236 {ecryptfs_opt_err, NULL}
237};
238
f4aad16a
MH
239static int ecryptfs_init_global_auth_toks(
240 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
237fead6 241{
f4aad16a 242 struct ecryptfs_global_auth_tok *global_auth_tok;
237fead6 243 int rc = 0;
237fead6 244
f4aad16a
MH
245 list_for_each_entry(global_auth_tok,
246 &mount_crypt_stat->global_auth_tok_list,
247 mount_crypt_stat_list) {
5dda6992
MH
248 rc = ecryptfs_keyring_auth_tok_for_sig(
249 &global_auth_tok->global_auth_tok_key,
250 &global_auth_tok->global_auth_tok,
251 global_auth_tok->sig);
252 if (rc) {
f4aad16a
MH
253 printk(KERN_ERR "Could not find valid key in user "
254 "session keyring for sig specified in mount "
255 "option: [%s]\n", global_auth_tok->sig);
256 global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
982363c9 257 goto out;
f4aad16a
MH
258 } else
259 global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
237fead6 260 }
982363c9 261out:
237fead6
MH
262 return rc;
263}
264
f4aad16a
MH
265static void ecryptfs_init_mount_crypt_stat(
266 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
267{
268 memset((void *)mount_crypt_stat, 0,
269 sizeof(struct ecryptfs_mount_crypt_stat));
270 INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
271 mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
272 mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
273}
274
237fead6
MH
275/**
276 * ecryptfs_parse_options
277 * @sb: The ecryptfs super block
278 * @options: The options pased to the kernel
279 *
280 * Parse mount options:
281 * debug=N - ecryptfs_verbosity level for debug output
282 * sig=XXX - description(signature) of the key to use
283 *
284 * Returns the dentry object of the lower-level (lower/interposed)
285 * directory; We want to mount our stackable file system on top of
286 * that lower directory.
287 *
288 * The signature of the key to use must be the description of a key
289 * already in the keyring. Mounting will fail if the key can not be
290 * found.
291 *
292 * Returns zero on success; non-zero on error
293 */
2ccde7c6 294static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options)
237fead6
MH
295{
296 char *p;
297 int rc = 0;
298 int sig_set = 0;
299 int cipher_name_set = 0;
87c94c4d 300 int fn_cipher_name_set = 0;
237fead6
MH
301 int cipher_key_bytes;
302 int cipher_key_bytes_set = 0;
87c94c4d
MH
303 int fn_cipher_key_bytes;
304 int fn_cipher_key_bytes_set = 0;
237fead6 305 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
2ccde7c6 306 &sbi->mount_crypt_stat;
237fead6
MH
307 substring_t args[MAX_OPT_ARGS];
308 int token;
309 char *sig_src;
237fead6
MH
310 char *cipher_name_dst;
311 char *cipher_name_src;
87c94c4d
MH
312 char *fn_cipher_name_dst;
313 char *fn_cipher_name_src;
314 char *fnek_dst;
315 char *fnek_src;
237fead6 316 char *cipher_key_bytes_src;
87c94c4d 317 char *fn_cipher_key_bytes_src;
237fead6
MH
318
319 if (!options) {
320 rc = -EINVAL;
321 goto out;
322 }
956159c3 323 ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
237fead6
MH
324 while ((p = strsep(&options, ",")) != NULL) {
325 if (!*p)
326 continue;
327 token = match_token(p, tokens, args);
328 switch (token) {
329 case ecryptfs_opt_sig:
330 case ecryptfs_opt_ecryptfs_sig:
331 sig_src = args[0].from;
f4aad16a 332 rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
84814d64 333 sig_src, 0);
f4aad16a
MH
334 if (rc) {
335 printk(KERN_ERR "Error attempting to register "
336 "global sig; rc = [%d]\n", rc);
337 goto out;
338 }
237fead6
MH
339 sig_set = 1;
340 break;
237fead6
MH
341 case ecryptfs_opt_cipher:
342 case ecryptfs_opt_ecryptfs_cipher:
343 cipher_name_src = args[0].from;
344 cipher_name_dst =
345 mount_crypt_stat->
346 global_default_cipher_name;
347 strncpy(cipher_name_dst, cipher_name_src,
348 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
87c94c4d 349 cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
237fead6
MH
350 cipher_name_set = 1;
351 break;
352 case ecryptfs_opt_ecryptfs_key_bytes:
353 cipher_key_bytes_src = args[0].from;
354 cipher_key_bytes =
355 (int)simple_strtol(cipher_key_bytes_src,
356 &cipher_key_bytes_src, 0);
357 mount_crypt_stat->global_default_cipher_key_size =
358 cipher_key_bytes;
237fead6
MH
359 cipher_key_bytes_set = 1;
360 break;
361 case ecryptfs_opt_passthrough:
362 mount_crypt_stat->flags |=
363 ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
364 break;
17398957
MH
365 case ecryptfs_opt_xattr_metadata:
366 mount_crypt_stat->flags |=
367 ECRYPTFS_XATTR_METADATA_ENABLED;
368 break;
369 case ecryptfs_opt_encrypted_view:
370 mount_crypt_stat->flags |=
371 ECRYPTFS_XATTR_METADATA_ENABLED;
372 mount_crypt_stat->flags |=
373 ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
374 break;
87c94c4d
MH
375 case ecryptfs_opt_fnek_sig:
376 fnek_src = args[0].from;
377 fnek_dst =
378 mount_crypt_stat->global_default_fnek_sig;
379 strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
380 mount_crypt_stat->global_default_fnek_sig[
381 ECRYPTFS_SIG_SIZE_HEX] = '\0';
382 rc = ecryptfs_add_global_auth_tok(
383 mount_crypt_stat,
84814d64
TH
384 mount_crypt_stat->global_default_fnek_sig,
385 ECRYPTFS_AUTH_TOK_FNEK);
87c94c4d
MH
386 if (rc) {
387 printk(KERN_ERR "Error attempting to register "
388 "global fnek sig [%s]; rc = [%d]\n",
389 mount_crypt_stat->global_default_fnek_sig,
390 rc);
391 goto out;
392 }
393 mount_crypt_stat->flags |=
394 (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
395 | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
396 break;
397 case ecryptfs_opt_fn_cipher:
398 fn_cipher_name_src = args[0].from;
399 fn_cipher_name_dst =
400 mount_crypt_stat->global_default_fn_cipher_name;
401 strncpy(fn_cipher_name_dst, fn_cipher_name_src,
402 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
403 mount_crypt_stat->global_default_fn_cipher_name[
404 ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
405 fn_cipher_name_set = 1;
406 break;
407 case ecryptfs_opt_fn_cipher_key_bytes:
408 fn_cipher_key_bytes_src = args[0].from;
409 fn_cipher_key_bytes =
410 (int)simple_strtol(fn_cipher_key_bytes_src,
411 &fn_cipher_key_bytes_src, 0);
412 mount_crypt_stat->global_default_fn_cipher_key_bytes =
413 fn_cipher_key_bytes;
414 fn_cipher_key_bytes_set = 1;
415 break;
e77cc8d2
TH
416 case ecryptfs_opt_unlink_sigs:
417 mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
418 break;
f16feb51
RS
419 case ecryptfs_opt_mount_auth_tok_only:
420 mount_crypt_stat->flags |=
421 ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
422 break;
237fead6
MH
423 case ecryptfs_opt_err:
424 default:
87c94c4d
MH
425 printk(KERN_WARNING
426 "%s: eCryptfs: unrecognized option [%s]\n",
427 __func__, p);
237fead6
MH
428 }
429 }
237fead6
MH
430 if (!sig_set) {
431 rc = -EINVAL;
956159c3
MH
432 ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
433 "auth tok signature as a mount "
237fead6
MH
434 "parameter; see the eCryptfs README\n");
435 goto out;
436 }
437 if (!cipher_name_set) {
8f236809
MS
438 int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
439
440 BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
8f236809
MS
441 strcpy(mount_crypt_stat->global_default_cipher_name,
442 ECRYPTFS_DEFAULT_CIPHER);
237fead6 443 }
87c94c4d
MH
444 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
445 && !fn_cipher_name_set)
446 strcpy(mount_crypt_stat->global_default_fn_cipher_name,
447 mount_crypt_stat->global_default_cipher_name);
448 if (!cipher_key_bytes_set)
e5d9cbde 449 mount_crypt_stat->global_default_cipher_key_size = 0;
87c94c4d
MH
450 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
451 && !fn_cipher_key_bytes_set)
452 mount_crypt_stat->global_default_fn_cipher_key_bytes =
453 mount_crypt_stat->global_default_cipher_key_size;
af440f52
ES
454 mutex_lock(&key_tfm_list_mutex);
455 if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
87c94c4d 456 NULL)) {
af440f52
ES
457 rc = ecryptfs_add_new_key_tfm(
458 NULL, mount_crypt_stat->global_default_cipher_name,
459 mount_crypt_stat->global_default_cipher_key_size);
87c94c4d
MH
460 if (rc) {
461 printk(KERN_ERR "Error attempting to initialize "
462 "cipher with name = [%s] and key size = [%td]; "
463 "rc = [%d]\n",
464 mount_crypt_stat->global_default_cipher_name,
465 mount_crypt_stat->global_default_cipher_key_size,
466 rc);
467 rc = -EINVAL;
468 mutex_unlock(&key_tfm_list_mutex);
469 goto out;
470 }
471 }
472 if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
473 && !ecryptfs_tfm_exists(
474 mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
475 rc = ecryptfs_add_new_key_tfm(
476 NULL, mount_crypt_stat->global_default_fn_cipher_name,
477 mount_crypt_stat->global_default_fn_cipher_key_bytes);
478 if (rc) {
479 printk(KERN_ERR "Error attempting to initialize "
480 "cipher with name = [%s] and key size = [%td]; "
481 "rc = [%d]\n",
482 mount_crypt_stat->global_default_fn_cipher_name,
483 mount_crypt_stat->global_default_fn_cipher_key_bytes,
484 rc);
485 rc = -EINVAL;
486 mutex_unlock(&key_tfm_list_mutex);
487 goto out;
488 }
237fead6 489 }
87c94c4d 490 mutex_unlock(&key_tfm_list_mutex);
5dda6992 491 rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
87c94c4d 492 if (rc)
f4aad16a
MH
493 printk(KERN_WARNING "One or more global auth toks could not "
494 "properly register; rc = [%d]\n", rc);
237fead6
MH
495out:
496 return rc;
497}
498
499struct kmem_cache *ecryptfs_sb_info_cache;
4403158b 500static struct file_system_type ecryptfs_fs_type;
237fead6 501
237fead6
MH
502/**
503 * ecryptfs_get_sb
504 * @fs_type
505 * @flags
506 * @dev_name: The path to mount over
507 * @raw_data: The options passed into the kernel
237fead6 508 */
4d143beb
AV
509static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
510 const char *dev_name, void *raw_data)
237fead6 511{
2ccde7c6
AV
512 struct super_block *s;
513 struct ecryptfs_sb_info *sbi;
514 struct ecryptfs_dentry_info *root_info;
515 const char *err = "Getting sb failed";
66cb7666
AV
516 struct inode *inode;
517 struct path path;
237fead6 518 int rc;
237fead6 519
2ccde7c6
AV
520 sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
521 if (!sbi) {
522 rc = -ENOMEM;
237fead6
MH
523 goto out;
524 }
2ccde7c6
AV
525
526 rc = ecryptfs_parse_options(sbi, raw_data);
237fead6 527 if (rc) {
2ccde7c6
AV
528 err = "Error parsing options";
529 goto out;
237fead6 530 }
2ccde7c6
AV
531
532 s = sget(fs_type, NULL, set_anon_super, NULL);
533 if (IS_ERR(s)) {
534 rc = PTR_ERR(s);
535 goto out;
536 }
537
538 s->s_flags = flags;
539 rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs", BDI_CAP_MAP_COPY);
66cb7666
AV
540 if (rc)
541 goto out1;
2ccde7c6
AV
542
543 ecryptfs_set_superblock_private(s, sbi);
544 s->s_bdi = &sbi->bdi;
545
546 /* ->kill_sb() will take care of sbi after that point */
547 sbi = NULL;
548 s->s_op = &ecryptfs_sops;
66cb7666 549 s->s_d_op = &ecryptfs_dops;
2ccde7c6 550
66cb7666
AV
551 err = "Reading sb failed";
552 rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
553 if (rc) {
554 ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
555 goto out1;
556 }
557 if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
558 rc = -EINVAL;
559 printk(KERN_ERR "Mount on filesystem of type "
560 "eCryptfs explicitly disallowed due to "
561 "known incompatibilities\n");
562 goto out_free;
563 }
564 ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
565 s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
566 s->s_blocksize = path.dentry->d_sb->s_blocksize;
567
568 inode = ecryptfs_get_inode(path.dentry->d_inode, s);
569 rc = PTR_ERR(inode);
570 if (IS_ERR(inode))
571 goto out_free;
572
573 s->s_root = d_alloc_root(inode);
2ccde7c6 574 if (!s->s_root) {
66cb7666
AV
575 iput(inode);
576 rc = -ENOMEM;
577 goto out_free;
2ccde7c6 578 }
2ccde7c6 579
66cb7666 580 rc = -ENOMEM;
2ccde7c6 581 root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
66cb7666
AV
582 if (!root_info)
583 goto out_free;
584
2ccde7c6
AV
585 /* ->kill_sb() will take care of root_info */
586 ecryptfs_set_dentry_private(s->s_root, root_info);
66cb7666
AV
587 ecryptfs_set_dentry_lower(s->s_root, path.dentry);
588 ecryptfs_set_dentry_lower_mnt(s->s_root, path.mnt);
589
2ccde7c6 590 s->s_flags |= MS_ACTIVE;
4d143beb 591 return dget(s->s_root);
2ccde7c6 592
66cb7666
AV
593out_free:
594 path_put(&path);
595out1:
596 deactivate_locked_super(s);
237fead6 597out:
2ccde7c6
AV
598 if (sbi) {
599 ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
600 kmem_cache_free(ecryptfs_sb_info_cache, sbi);
601 }
602 printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
4d143beb 603 return ERR_PTR(rc);
237fead6
MH
604}
605
606/**
607 * ecryptfs_kill_block_super
608 * @sb: The ecryptfs super block
609 *
610 * Used to bring the superblock down and free the private data.
237fead6
MH
611 */
612static void ecryptfs_kill_block_super(struct super_block *sb)
613{
decabd66
AV
614 struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
615 kill_anon_super(sb);
616 if (!sb_info)
617 return;
618 ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
619 bdi_destroy(&sb_info->bdi);
620 kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
237fead6
MH
621}
622
623static struct file_system_type ecryptfs_fs_type = {
624 .owner = THIS_MODULE,
625 .name = "ecryptfs",
4d143beb 626 .mount = ecryptfs_mount,
237fead6
MH
627 .kill_sb = ecryptfs_kill_block_super,
628 .fs_flags = 0
629};
630
631/**
632 * inode_info_init_once
633 *
634 * Initializes the ecryptfs_inode_info_cache when it is created
635 */
636static void
51cc5068 637inode_info_init_once(void *vptr)
237fead6
MH
638{
639 struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
640
a35afb83 641 inode_init_once(&ei->vfs_inode);
237fead6
MH
642}
643
644static struct ecryptfs_cache_info {
e18b890b 645 struct kmem_cache **cache;
237fead6
MH
646 const char *name;
647 size_t size;
51cc5068 648 void (*ctor)(void *obj);
237fead6
MH
649} ecryptfs_cache_infos[] = {
650 {
651 .cache = &ecryptfs_auth_tok_list_item_cache,
652 .name = "ecryptfs_auth_tok_list_item",
653 .size = sizeof(struct ecryptfs_auth_tok_list_item),
654 },
655 {
656 .cache = &ecryptfs_file_info_cache,
657 .name = "ecryptfs_file_cache",
658 .size = sizeof(struct ecryptfs_file_info),
659 },
660 {
661 .cache = &ecryptfs_dentry_info_cache,
662 .name = "ecryptfs_dentry_info_cache",
663 .size = sizeof(struct ecryptfs_dentry_info),
664 },
665 {
666 .cache = &ecryptfs_inode_info_cache,
667 .name = "ecryptfs_inode_cache",
668 .size = sizeof(struct ecryptfs_inode_info),
669 .ctor = inode_info_init_once,
670 },
671 {
672 .cache = &ecryptfs_sb_info_cache,
673 .name = "ecryptfs_sb_cache",
674 .size = sizeof(struct ecryptfs_sb_info),
675 },
237fead6
MH
676 {
677 .cache = &ecryptfs_header_cache_1,
678 .name = "ecryptfs_headers_1",
679 .size = PAGE_CACHE_SIZE,
680 },
681 {
682 .cache = &ecryptfs_header_cache_2,
683 .name = "ecryptfs_headers_2",
684 .size = PAGE_CACHE_SIZE,
685 },
dd2a3b7a
MH
686 {
687 .cache = &ecryptfs_xattr_cache,
688 .name = "ecryptfs_xattr_cache",
689 .size = PAGE_CACHE_SIZE,
690 },
eb95e7ff
MH
691 {
692 .cache = &ecryptfs_key_record_cache,
693 .name = "ecryptfs_key_record_cache",
694 .size = sizeof(struct ecryptfs_key_record),
695 },
956159c3
MH
696 {
697 .cache = &ecryptfs_key_sig_cache,
698 .name = "ecryptfs_key_sig_cache",
699 .size = sizeof(struct ecryptfs_key_sig),
700 },
701 {
702 .cache = &ecryptfs_global_auth_tok_cache,
703 .name = "ecryptfs_global_auth_tok_cache",
704 .size = sizeof(struct ecryptfs_global_auth_tok),
705 },
706 {
707 .cache = &ecryptfs_key_tfm_cache,
708 .name = "ecryptfs_key_tfm_cache",
709 .size = sizeof(struct ecryptfs_key_tfm),
710 },
746f1e55
MH
711 {
712 .cache = &ecryptfs_open_req_cache,
713 .name = "ecryptfs_open_req_cache",
714 .size = sizeof(struct ecryptfs_open_req),
715 },
237fead6
MH
716};
717
718static void ecryptfs_free_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 if (*(info->cache))
727 kmem_cache_destroy(*(info->cache));
728 }
729}
730
731/**
732 * ecryptfs_init_kmem_caches
733 *
734 * Returns zero on success; non-zero otherwise
735 */
736static int ecryptfs_init_kmem_caches(void)
737{
738 int i;
739
740 for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
741 struct ecryptfs_cache_info *info;
742
743 info = &ecryptfs_cache_infos[i];
744 *(info->cache) = kmem_cache_create(info->name, info->size,
20c2df83 745 0, SLAB_HWCACHE_ALIGN, info->ctor);
237fead6
MH
746 if (!*(info->cache)) {
747 ecryptfs_free_kmem_caches();
748 ecryptfs_printk(KERN_WARNING, "%s: "
749 "kmem_cache_create failed\n",
750 info->name);
751 return -ENOMEM;
752 }
753 }
754 return 0;
755}
756
6e90aa97 757static struct kobject *ecryptfs_kobj;
237fead6 758
386f275f
KS
759static ssize_t version_show(struct kobject *kobj,
760 struct kobj_attribute *attr, char *buff)
237fead6
MH
761{
762 return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
763}
764
386f275f 765static struct kobj_attribute version_attr = __ATTR_RO(version);
237fead6 766
30a468b1
GKH
767static struct attribute *attributes[] = {
768 &version_attr.attr,
30a468b1
GKH
769 NULL,
770};
771
772static struct attribute_group attr_group = {
773 .attrs = attributes,
774};
237fead6
MH
775
776static int do_sysfs_registration(void)
777{
778 int rc;
779
6e90aa97
GKH
780 ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
781 if (!ecryptfs_kobj) {
917e865d
GKH
782 printk(KERN_ERR "Unable to create ecryptfs kset\n");
783 rc = -ENOMEM;
237fead6
MH
784 goto out;
785 }
6e90aa97 786 rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
237fead6
MH
787 if (rc) {
788 printk(KERN_ERR
30a468b1 789 "Unable to create ecryptfs version attributes\n");
197b12d6 790 kobject_put(ecryptfs_kobj);
237fead6
MH
791 }
792out:
793 return rc;
794}
795
a75de1b3
RK
796static void do_sysfs_unregistration(void)
797{
6e90aa97 798 sysfs_remove_group(ecryptfs_kobj, &attr_group);
197b12d6 799 kobject_put(ecryptfs_kobj);
a75de1b3
RK
800}
801
237fead6
MH
802static int __init ecryptfs_init(void)
803{
804 int rc;
805
806 if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
807 rc = -EINVAL;
808 ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
809 "larger than the host's page size, and so "
810 "eCryptfs cannot run on this system. The "
811 "default eCryptfs extent size is [%d] bytes; "
812 "the page size is [%d] bytes.\n",
813 ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE);
814 goto out;
815 }
816 rc = ecryptfs_init_kmem_caches();
817 if (rc) {
818 printk(KERN_ERR
819 "Failed to allocate one or more kmem_cache objects\n");
820 goto out;
821 }
822 rc = register_filesystem(&ecryptfs_fs_type);
823 if (rc) {
824 printk(KERN_ERR "Failed to register filesystem\n");
cf81f89d 825 goto out_free_kmem_caches;
237fead6 826 }
237fead6
MH
827 rc = do_sysfs_registration();
828 if (rc) {
829 printk(KERN_ERR "sysfs registration failed\n");
cf81f89d 830 goto out_unregister_filesystem;
237fead6 831 }
746f1e55
MH
832 rc = ecryptfs_init_kthread();
833 if (rc) {
834 printk(KERN_ERR "%s: kthread initialization failed; "
835 "rc = [%d]\n", __func__, rc);
836 goto out_do_sysfs_unregistration;
837 }
624ae528 838 rc = ecryptfs_init_messaging();
dddfa461 839 if (rc) {
746f1e55 840 printk(KERN_ERR "Failure occured while attempting to "
624ae528
TH
841 "initialize the communications channel to "
842 "ecryptfsd\n");
746f1e55 843 goto out_destroy_kthread;
956159c3
MH
844 }
845 rc = ecryptfs_init_crypto();
846 if (rc) {
847 printk(KERN_ERR "Failure whilst attempting to init crypto; "
848 "rc = [%d]\n", rc);
cf81f89d 849 goto out_release_messaging;
dddfa461 850 }
2830bfd6
ES
851 if (ecryptfs_verbosity > 0)
852 printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
853 "will be written to the syslog!\n", ecryptfs_verbosity);
854
cf81f89d
MH
855 goto out;
856out_release_messaging:
624ae528 857 ecryptfs_release_messaging();
746f1e55
MH
858out_destroy_kthread:
859 ecryptfs_destroy_kthread();
cf81f89d
MH
860out_do_sysfs_unregistration:
861 do_sysfs_unregistration();
862out_unregister_filesystem:
863 unregister_filesystem(&ecryptfs_fs_type);
864out_free_kmem_caches:
865 ecryptfs_free_kmem_caches();
237fead6
MH
866out:
867 return rc;
868}
869
870static void __exit ecryptfs_exit(void)
871{
cf81f89d
MH
872 int rc;
873
874 rc = ecryptfs_destroy_crypto();
875 if (rc)
876 printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
877 "rc = [%d]\n", rc);
624ae528 878 ecryptfs_release_messaging();
746f1e55 879 ecryptfs_destroy_kthread();
cf81f89d 880 do_sysfs_unregistration();
237fead6
MH
881 unregister_filesystem(&ecryptfs_fs_type);
882 ecryptfs_free_kmem_caches();
883}
884
885MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
886MODULE_DESCRIPTION("eCryptfs");
887
888MODULE_LICENSE("GPL");
889
890module_init(ecryptfs_init)
891module_exit(ecryptfs_exit)
This page took 0.419069 seconds and 5 git commands to generate.