2 * eCryptfs: Linux filesystem encryption layer
4 * Copyright (C) 1997-2004 Erez Zadok
5 * Copyright (C) 2001-2004 Stony Brook University
6 * Copyright (C) 2004-2007 International Business Machines Corp.
7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompsion <mcthomps@us.ibm.com>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include <linux/file.h>
27 #include <linux/vmalloc.h>
28 #include <linux/pagemap.h>
29 #include <linux/dcache.h>
30 #include <linux/namei.h>
31 #include <linux/mount.h>
32 #include <linux/crypto.h>
33 #include <linux/fs_stack.h>
34 #include <linux/slab.h>
35 #include <linux/xattr.h>
36 #include <asm/unaligned.h>
37 #include "ecryptfs_kernel.h"
39 static struct dentry
*lock_parent(struct dentry
*dentry
)
43 dir
= dget_parent(dentry
);
44 mutex_lock_nested(&(dir
->d_inode
->i_mutex
), I_MUTEX_PARENT
);
48 static void unlock_dir(struct dentry
*dir
)
50 mutex_unlock(&dir
->d_inode
->i_mutex
);
54 static int ecryptfs_inode_test(struct inode
*inode
, void *lower_inode
)
56 if (ecryptfs_inode_to_lower(inode
) == (struct inode
*)lower_inode
)
61 static int ecryptfs_inode_set(struct inode
*inode
, void *opaque
)
63 struct inode
*lower_inode
= opaque
;
65 ecryptfs_set_inode_lower(inode
, lower_inode
);
66 fsstack_copy_attr_all(inode
, lower_inode
);
67 /* i_size will be overwritten for encrypted regular files */
68 fsstack_copy_inode_size(inode
, lower_inode
);
69 inode
->i_ino
= lower_inode
->i_ino
;
71 inode
->i_mapping
->a_ops
= &ecryptfs_aops
;
72 inode
->i_mapping
->backing_dev_info
= inode
->i_sb
->s_bdi
;
74 if (S_ISLNK(inode
->i_mode
))
75 inode
->i_op
= &ecryptfs_symlink_iops
;
76 else if (S_ISDIR(inode
->i_mode
))
77 inode
->i_op
= &ecryptfs_dir_iops
;
79 inode
->i_op
= &ecryptfs_main_iops
;
81 if (S_ISDIR(inode
->i_mode
))
82 inode
->i_fop
= &ecryptfs_dir_fops
;
83 else if (special_file(inode
->i_mode
))
84 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
86 inode
->i_fop
= &ecryptfs_main_fops
;
91 static struct inode
*__ecryptfs_get_inode(struct inode
*lower_inode
,
92 struct super_block
*sb
)
96 if (lower_inode
->i_sb
!= ecryptfs_superblock_to_lower(sb
))
97 return ERR_PTR(-EXDEV
);
98 if (!igrab(lower_inode
))
99 return ERR_PTR(-ESTALE
);
100 inode
= iget5_locked(sb
, (unsigned long)lower_inode
,
101 ecryptfs_inode_test
, ecryptfs_inode_set
,
105 return ERR_PTR(-EACCES
);
107 if (!(inode
->i_state
& I_NEW
))
113 struct inode
*ecryptfs_get_inode(struct inode
*lower_inode
,
114 struct super_block
*sb
)
116 struct inode
*inode
= __ecryptfs_get_inode(lower_inode
, sb
);
118 if (!IS_ERR(inode
) && (inode
->i_state
& I_NEW
))
119 unlock_new_inode(inode
);
126 * @lower_dentry: Existing dentry in the lower filesystem
127 * @dentry: ecryptfs' dentry
128 * @sb: ecryptfs's super_block
130 * Interposes upper and lower dentries.
132 * Returns zero on success; non-zero otherwise
134 static int ecryptfs_interpose(struct dentry
*lower_dentry
,
135 struct dentry
*dentry
, struct super_block
*sb
)
137 struct inode
*inode
= ecryptfs_get_inode(lower_dentry
->d_inode
, sb
);
140 return PTR_ERR(inode
);
141 d_instantiate(dentry
, inode
);
147 * ecryptfs_create_underlying_file
148 * @lower_dir_inode: inode of the parent in the lower fs of the new file
149 * @dentry: New file's dentry
150 * @mode: The mode of the new file
151 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
153 * Creates the file in the lower file system.
155 * Returns zero on success; non-zero on error condition
158 ecryptfs_create_underlying_file(struct inode
*lower_dir_inode
,
159 struct dentry
*dentry
, int mode
,
160 struct nameidata
*nd
)
162 struct dentry
*lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
163 struct vfsmount
*lower_mnt
= ecryptfs_dentry_to_lower_mnt(dentry
);
164 struct dentry
*dentry_save
;
165 struct vfsmount
*vfsmount_save
;
166 unsigned int flags_save
;
170 dentry_save
= nd
->path
.dentry
;
171 vfsmount_save
= nd
->path
.mnt
;
172 flags_save
= nd
->flags
;
173 nd
->path
.dentry
= lower_dentry
;
174 nd
->path
.mnt
= lower_mnt
;
175 nd
->flags
&= ~LOOKUP_OPEN
;
177 rc
= vfs_create(lower_dir_inode
, lower_dentry
, mode
, nd
);
179 nd
->path
.dentry
= dentry_save
;
180 nd
->path
.mnt
= vfsmount_save
;
181 nd
->flags
= flags_save
;
188 * @directory_inode: inode of the new file's dentry's parent in ecryptfs
189 * @ecryptfs_dentry: New file's dentry in ecryptfs
190 * @mode: The mode of the new file
191 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
193 * Creates the underlying file and the eCryptfs inode which will link to
194 * it. It will also update the eCryptfs directory inode to mimic the
195 * stat of the lower directory inode.
197 * Returns zero on success; non-zero on error condition
200 ecryptfs_do_create(struct inode
*directory_inode
,
201 struct dentry
*ecryptfs_dentry
, int mode
,
202 struct nameidata
*nd
)
205 struct dentry
*lower_dentry
;
206 struct dentry
*lower_dir_dentry
;
208 lower_dentry
= ecryptfs_dentry_to_lower(ecryptfs_dentry
);
209 lower_dir_dentry
= lock_parent(lower_dentry
);
210 if (IS_ERR(lower_dir_dentry
)) {
211 ecryptfs_printk(KERN_ERR
, "Error locking directory of "
213 rc
= PTR_ERR(lower_dir_dentry
);
216 rc
= ecryptfs_create_underlying_file(lower_dir_dentry
->d_inode
,
217 ecryptfs_dentry
, mode
, nd
);
219 printk(KERN_ERR
"%s: Failure to create dentry in lower fs; "
220 "rc = [%d]\n", __func__
, rc
);
223 rc
= ecryptfs_interpose(lower_dentry
, ecryptfs_dentry
,
224 directory_inode
->i_sb
);
226 ecryptfs_printk(KERN_ERR
, "Failure in ecryptfs_interpose\n");
229 fsstack_copy_attr_times(directory_inode
, lower_dir_dentry
->d_inode
);
230 fsstack_copy_inode_size(directory_inode
, lower_dir_dentry
->d_inode
);
232 unlock_dir(lower_dir_dentry
);
238 * ecryptfs_initialize_file
240 * Cause the file to be changed from a basic empty file to an ecryptfs
241 * file with a header and first data page.
243 * Returns zero on success
245 static int ecryptfs_initialize_file(struct dentry
*ecryptfs_dentry
)
247 struct ecryptfs_crypt_stat
*crypt_stat
=
248 &ecryptfs_inode_to_private(ecryptfs_dentry
->d_inode
)->crypt_stat
;
251 if (S_ISDIR(ecryptfs_dentry
->d_inode
->i_mode
)) {
252 ecryptfs_printk(KERN_DEBUG
, "This is a directory\n");
253 crypt_stat
->flags
&= ~(ECRYPTFS_ENCRYPTED
);
256 ecryptfs_printk(KERN_DEBUG
, "Initializing crypto context\n");
257 rc
= ecryptfs_new_file_context(ecryptfs_dentry
);
259 ecryptfs_printk(KERN_ERR
, "Error creating new file "
260 "context; rc = [%d]\n", rc
);
263 rc
= ecryptfs_get_lower_file(ecryptfs_dentry
,
264 ecryptfs_dentry
->d_inode
);
266 printk(KERN_ERR
"%s: Error attempting to initialize "
267 "the lower file for the dentry with name "
268 "[%s]; rc = [%d]\n", __func__
,
269 ecryptfs_dentry
->d_name
.name
, rc
);
272 rc
= ecryptfs_write_metadata(ecryptfs_dentry
);
274 printk(KERN_ERR
"Error writing headers; rc = [%d]\n", rc
);
275 ecryptfs_put_lower_file(ecryptfs_dentry
->d_inode
);
282 * @dir: The inode of the directory in which to create the file.
283 * @dentry: The eCryptfs dentry
284 * @mode: The mode of the new file.
287 * Creates a new file.
289 * Returns zero on success; non-zero on error condition
292 ecryptfs_create(struct inode
*directory_inode
, struct dentry
*ecryptfs_dentry
,
293 int mode
, struct nameidata
*nd
)
297 /* ecryptfs_do_create() calls ecryptfs_interpose() */
298 rc
= ecryptfs_do_create(directory_inode
, ecryptfs_dentry
, mode
, nd
);
300 ecryptfs_printk(KERN_WARNING
, "Failed to create file in"
301 "lower filesystem\n");
304 /* At this point, a file exists on "disk"; we need to make sure
305 * that this on disk file is prepared to be an ecryptfs file */
306 rc
= ecryptfs_initialize_file(ecryptfs_dentry
);
311 static int ecryptfs_i_size_read(struct dentry
*dentry
, struct inode
*inode
)
313 struct ecryptfs_crypt_stat
*crypt_stat
;
316 rc
= ecryptfs_get_lower_file(dentry
, inode
);
318 printk(KERN_ERR
"%s: Error attempting to initialize "
319 "the lower file for the dentry with name "
320 "[%s]; rc = [%d]\n", __func__
,
321 dentry
->d_name
.name
, rc
);
325 crypt_stat
= &ecryptfs_inode_to_private(inode
)->crypt_stat
;
326 /* TODO: lock for crypt_stat comparison */
327 if (!(crypt_stat
->flags
& ECRYPTFS_POLICY_APPLIED
))
328 ecryptfs_set_default_sizes(crypt_stat
);
330 rc
= ecryptfs_read_and_validate_header_region(inode
);
331 ecryptfs_put_lower_file(inode
);
333 rc
= ecryptfs_read_and_validate_xattr_region(dentry
, inode
);
335 crypt_stat
->flags
|= ECRYPTFS_METADATA_IN_XATTR
;
338 /* Must return 0 to allow non-eCryptfs files to be looked up, too */
343 * ecryptfs_lookup_interpose - Dentry interposition for a lookup
345 static int ecryptfs_lookup_interpose(struct dentry
*dentry
,
346 struct dentry
*lower_dentry
,
347 struct inode
*dir_inode
)
349 struct inode
*inode
, *lower_inode
= lower_dentry
->d_inode
;
350 struct ecryptfs_dentry_info
*dentry_info
;
351 struct vfsmount
*lower_mnt
;
354 lower_mnt
= mntget(ecryptfs_dentry_to_lower_mnt(dentry
->d_parent
));
355 fsstack_copy_attr_atime(dir_inode
, lower_dentry
->d_parent
->d_inode
);
356 BUG_ON(!lower_dentry
->d_count
);
358 dentry_info
= kmem_cache_alloc(ecryptfs_dentry_info_cache
, GFP_KERNEL
);
359 ecryptfs_set_dentry_private(dentry
, dentry_info
);
361 printk(KERN_ERR
"%s: Out of memory whilst attempting "
362 "to allocate ecryptfs_dentry_info struct\n",
369 ecryptfs_set_dentry_lower(dentry
, lower_dentry
);
370 ecryptfs_set_dentry_lower_mnt(dentry
, lower_mnt
);
372 if (!lower_dentry
->d_inode
) {
373 /* We want to add because we couldn't find in lower */
377 inode
= __ecryptfs_get_inode(lower_inode
, dir_inode
->i_sb
);
379 printk(KERN_ERR
"%s: Error interposing; rc = [%ld]\n",
380 __func__
, PTR_ERR(inode
));
381 return PTR_ERR(inode
);
383 if (S_ISREG(inode
->i_mode
)) {
384 rc
= ecryptfs_i_size_read(dentry
, inode
);
386 make_bad_inode(inode
);
391 if (inode
->i_state
& I_NEW
)
392 unlock_new_inode(inode
);
393 d_add(dentry
, inode
);
400 * @ecryptfs_dir_inode: The eCryptfs directory inode
401 * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
402 * @ecryptfs_nd: nameidata; may be NULL
404 * Find a file on disk. If the file does not exist, then we'll add it to the
405 * dentry cache and continue on to read it from the disk.
407 static struct dentry
*ecryptfs_lookup(struct inode
*ecryptfs_dir_inode
,
408 struct dentry
*ecryptfs_dentry
,
409 struct nameidata
*ecryptfs_nd
)
411 char *encrypted_and_encoded_name
= NULL
;
412 size_t encrypted_and_encoded_name_size
;
413 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
= NULL
;
414 struct dentry
*lower_dir_dentry
, *lower_dentry
;
417 if ((ecryptfs_dentry
->d_name
.len
== 1
418 && !strcmp(ecryptfs_dentry
->d_name
.name
, "."))
419 || (ecryptfs_dentry
->d_name
.len
== 2
420 && !strcmp(ecryptfs_dentry
->d_name
.name
, ".."))) {
423 lower_dir_dentry
= ecryptfs_dentry_to_lower(ecryptfs_dentry
->d_parent
);
424 mutex_lock(&lower_dir_dentry
->d_inode
->i_mutex
);
425 lower_dentry
= lookup_one_len(ecryptfs_dentry
->d_name
.name
,
427 ecryptfs_dentry
->d_name
.len
);
428 mutex_unlock(&lower_dir_dentry
->d_inode
->i_mutex
);
429 if (IS_ERR(lower_dentry
)) {
430 rc
= PTR_ERR(lower_dentry
);
431 ecryptfs_printk(KERN_DEBUG
, "%s: lookup_one_len() returned "
432 "[%d] on lower_dentry = [%s]\n", __func__
, rc
,
433 encrypted_and_encoded_name
);
436 if (lower_dentry
->d_inode
)
438 mount_crypt_stat
= &ecryptfs_superblock_to_private(
439 ecryptfs_dentry
->d_sb
)->mount_crypt_stat
;
440 if (!(mount_crypt_stat
441 && (mount_crypt_stat
->flags
& ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
)))
444 rc
= ecryptfs_encrypt_and_encode_filename(
445 &encrypted_and_encoded_name
, &encrypted_and_encoded_name_size
,
446 NULL
, mount_crypt_stat
, ecryptfs_dentry
->d_name
.name
,
447 ecryptfs_dentry
->d_name
.len
);
449 printk(KERN_ERR
"%s: Error attempting to encrypt and encode "
450 "filename; rc = [%d]\n", __func__
, rc
);
453 mutex_lock(&lower_dir_dentry
->d_inode
->i_mutex
);
454 lower_dentry
= lookup_one_len(encrypted_and_encoded_name
,
456 encrypted_and_encoded_name_size
);
457 mutex_unlock(&lower_dir_dentry
->d_inode
->i_mutex
);
458 if (IS_ERR(lower_dentry
)) {
459 rc
= PTR_ERR(lower_dentry
);
460 ecryptfs_printk(KERN_DEBUG
, "%s: lookup_one_len() returned "
461 "[%d] on lower_dentry = [%s]\n", __func__
, rc
,
462 encrypted_and_encoded_name
);
466 rc
= ecryptfs_lookup_interpose(ecryptfs_dentry
, lower_dentry
,
470 d_drop(ecryptfs_dentry
);
472 kfree(encrypted_and_encoded_name
);
476 static int ecryptfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
477 struct dentry
*new_dentry
)
479 struct dentry
*lower_old_dentry
;
480 struct dentry
*lower_new_dentry
;
481 struct dentry
*lower_dir_dentry
;
485 file_size_save
= i_size_read(old_dentry
->d_inode
);
486 lower_old_dentry
= ecryptfs_dentry_to_lower(old_dentry
);
487 lower_new_dentry
= ecryptfs_dentry_to_lower(new_dentry
);
488 dget(lower_old_dentry
);
489 dget(lower_new_dentry
);
490 lower_dir_dentry
= lock_parent(lower_new_dentry
);
491 rc
= vfs_link(lower_old_dentry
, lower_dir_dentry
->d_inode
,
493 if (rc
|| !lower_new_dentry
->d_inode
)
495 rc
= ecryptfs_interpose(lower_new_dentry
, new_dentry
, dir
->i_sb
);
498 fsstack_copy_attr_times(dir
, lower_dir_dentry
->d_inode
);
499 fsstack_copy_inode_size(dir
, lower_dir_dentry
->d_inode
);
500 old_dentry
->d_inode
->i_nlink
=
501 ecryptfs_inode_to_lower(old_dentry
->d_inode
)->i_nlink
;
502 i_size_write(new_dentry
->d_inode
, file_size_save
);
504 unlock_dir(lower_dir_dentry
);
505 dput(lower_new_dentry
);
506 dput(lower_old_dentry
);
510 static int ecryptfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
513 struct dentry
*lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
514 struct inode
*lower_dir_inode
= ecryptfs_inode_to_lower(dir
);
515 struct dentry
*lower_dir_dentry
;
518 lower_dir_dentry
= lock_parent(lower_dentry
);
519 rc
= vfs_unlink(lower_dir_inode
, lower_dentry
);
521 printk(KERN_ERR
"Error in vfs_unlink; rc = [%d]\n", rc
);
524 fsstack_copy_attr_times(dir
, lower_dir_inode
);
525 dentry
->d_inode
->i_nlink
=
526 ecryptfs_inode_to_lower(dentry
->d_inode
)->i_nlink
;
527 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
530 unlock_dir(lower_dir_dentry
);
535 static int ecryptfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
539 struct dentry
*lower_dentry
;
540 struct dentry
*lower_dir_dentry
;
541 char *encoded_symname
;
542 size_t encoded_symlen
;
543 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
= NULL
;
545 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
547 lower_dir_dentry
= lock_parent(lower_dentry
);
548 mount_crypt_stat
= &ecryptfs_superblock_to_private(
549 dir
->i_sb
)->mount_crypt_stat
;
550 rc
= ecryptfs_encrypt_and_encode_filename(&encoded_symname
,
553 mount_crypt_stat
, symname
,
557 rc
= vfs_symlink(lower_dir_dentry
->d_inode
, lower_dentry
,
559 kfree(encoded_symname
);
560 if (rc
|| !lower_dentry
->d_inode
)
562 rc
= ecryptfs_interpose(lower_dentry
, dentry
, dir
->i_sb
);
565 fsstack_copy_attr_times(dir
, lower_dir_dentry
->d_inode
);
566 fsstack_copy_inode_size(dir
, lower_dir_dentry
->d_inode
);
568 unlock_dir(lower_dir_dentry
);
570 if (!dentry
->d_inode
)
575 static int ecryptfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
578 struct dentry
*lower_dentry
;
579 struct dentry
*lower_dir_dentry
;
581 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
582 lower_dir_dentry
= lock_parent(lower_dentry
);
583 rc
= vfs_mkdir(lower_dir_dentry
->d_inode
, lower_dentry
, mode
);
584 if (rc
|| !lower_dentry
->d_inode
)
586 rc
= ecryptfs_interpose(lower_dentry
, dentry
, dir
->i_sb
);
589 fsstack_copy_attr_times(dir
, lower_dir_dentry
->d_inode
);
590 fsstack_copy_inode_size(dir
, lower_dir_dentry
->d_inode
);
591 dir
->i_nlink
= lower_dir_dentry
->d_inode
->i_nlink
;
593 unlock_dir(lower_dir_dentry
);
594 if (!dentry
->d_inode
)
599 static int ecryptfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
601 struct dentry
*lower_dentry
;
602 struct dentry
*lower_dir_dentry
;
605 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
607 lower_dir_dentry
= lock_parent(lower_dentry
);
609 rc
= vfs_rmdir(lower_dir_dentry
->d_inode
, lower_dentry
);
611 if (!rc
&& dentry
->d_inode
)
612 clear_nlink(dentry
->d_inode
);
613 fsstack_copy_attr_times(dir
, lower_dir_dentry
->d_inode
);
614 dir
->i_nlink
= lower_dir_dentry
->d_inode
->i_nlink
;
615 unlock_dir(lower_dir_dentry
);
623 ecryptfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
626 struct dentry
*lower_dentry
;
627 struct dentry
*lower_dir_dentry
;
629 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
630 lower_dir_dentry
= lock_parent(lower_dentry
);
631 rc
= vfs_mknod(lower_dir_dentry
->d_inode
, lower_dentry
, mode
, dev
);
632 if (rc
|| !lower_dentry
->d_inode
)
634 rc
= ecryptfs_interpose(lower_dentry
, dentry
, dir
->i_sb
);
637 fsstack_copy_attr_times(dir
, lower_dir_dentry
->d_inode
);
638 fsstack_copy_inode_size(dir
, lower_dir_dentry
->d_inode
);
640 unlock_dir(lower_dir_dentry
);
641 if (!dentry
->d_inode
)
647 ecryptfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
648 struct inode
*new_dir
, struct dentry
*new_dentry
)
651 struct dentry
*lower_old_dentry
;
652 struct dentry
*lower_new_dentry
;
653 struct dentry
*lower_old_dir_dentry
;
654 struct dentry
*lower_new_dir_dentry
;
655 struct dentry
*trap
= NULL
;
657 lower_old_dentry
= ecryptfs_dentry_to_lower(old_dentry
);
658 lower_new_dentry
= ecryptfs_dentry_to_lower(new_dentry
);
659 dget(lower_old_dentry
);
660 dget(lower_new_dentry
);
661 lower_old_dir_dentry
= dget_parent(lower_old_dentry
);
662 lower_new_dir_dentry
= dget_parent(lower_new_dentry
);
663 trap
= lock_rename(lower_old_dir_dentry
, lower_new_dir_dentry
);
664 /* source should not be ancestor of target */
665 if (trap
== lower_old_dentry
) {
669 /* target should not be ancestor of source */
670 if (trap
== lower_new_dentry
) {
674 rc
= vfs_rename(lower_old_dir_dentry
->d_inode
, lower_old_dentry
,
675 lower_new_dir_dentry
->d_inode
, lower_new_dentry
);
678 fsstack_copy_attr_all(new_dir
, lower_new_dir_dentry
->d_inode
);
679 if (new_dir
!= old_dir
)
680 fsstack_copy_attr_all(old_dir
, lower_old_dir_dentry
->d_inode
);
682 unlock_rename(lower_old_dir_dentry
, lower_new_dir_dentry
);
683 dput(lower_new_dir_dentry
);
684 dput(lower_old_dir_dentry
);
685 dput(lower_new_dentry
);
686 dput(lower_old_dentry
);
690 static int ecryptfs_readlink_lower(struct dentry
*dentry
, char **buf
,
693 struct dentry
*lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
695 size_t lower_bufsiz
= PATH_MAX
;
699 lower_buf
= kmalloc(lower_bufsiz
, GFP_KERNEL
);
706 rc
= lower_dentry
->d_inode
->i_op
->readlink(lower_dentry
,
707 (char __user
*)lower_buf
,
713 rc
= ecryptfs_decode_and_decrypt_filename(buf
, bufsiz
, dentry
,
714 lower_buf
, lower_bufsiz
);
721 ecryptfs_readlink(struct dentry
*dentry
, char __user
*buf
, int bufsiz
)
724 size_t kbufsiz
, copied
;
727 rc
= ecryptfs_readlink_lower(dentry
, &kbuf
, &kbufsiz
);
730 copied
= min_t(size_t, bufsiz
, kbufsiz
);
731 rc
= copy_to_user(buf
, kbuf
, copied
) ? -EFAULT
: copied
;
733 fsstack_copy_attr_atime(dentry
->d_inode
,
734 ecryptfs_dentry_to_lower(dentry
)->d_inode
);
739 static void *ecryptfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
742 int len
= PAGE_SIZE
, rc
;
745 /* Released in ecryptfs_put_link(); only release here on error */
746 buf
= kmalloc(len
, GFP_KERNEL
);
748 buf
= ERR_PTR(-ENOMEM
);
753 rc
= dentry
->d_inode
->i_op
->readlink(dentry
, (char __user
*)buf
, len
);
761 nd_set_link(nd
, buf
);
766 ecryptfs_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *ptr
)
768 char *buf
= nd_get_link(nd
);
776 * upper_size_to_lower_size
777 * @crypt_stat: Crypt_stat associated with file
778 * @upper_size: Size of the upper file
780 * Calculate the required size of the lower file based on the
781 * specified size of the upper file. This calculation is based on the
782 * number of headers in the underlying file and the extent size.
784 * Returns Calculated size of the lower file.
787 upper_size_to_lower_size(struct ecryptfs_crypt_stat
*crypt_stat
,
792 lower_size
= ecryptfs_lower_header_size(crypt_stat
);
793 if (upper_size
!= 0) {
796 num_extents
= upper_size
>> crypt_stat
->extent_shift
;
797 if (upper_size
& ~crypt_stat
->extent_mask
)
799 lower_size
+= (num_extents
* crypt_stat
->extent_size
);
806 * @dentry: The ecryptfs layer dentry
807 * @ia: Address of the ecryptfs inode's attributes
808 * @lower_ia: Address of the lower inode's attributes
810 * Function to handle truncations modifying the size of the file. Note
811 * that the file sizes are interpolated. When expanding, we are simply
812 * writing strings of 0's out. When truncating, we truncate the upper
813 * inode and update the lower_ia according to the page index
814 * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
815 * the caller must use lower_ia in a call to notify_change() to perform
816 * the truncation of the lower inode.
818 * Returns zero on success; non-zero otherwise
820 static int truncate_upper(struct dentry
*dentry
, struct iattr
*ia
,
821 struct iattr
*lower_ia
)
824 struct inode
*inode
= dentry
->d_inode
;
825 struct ecryptfs_crypt_stat
*crypt_stat
;
826 loff_t i_size
= i_size_read(inode
);
827 loff_t lower_size_before_truncate
;
828 loff_t lower_size_after_truncate
;
830 if (unlikely((ia
->ia_size
== i_size
))) {
831 lower_ia
->ia_valid
&= ~ATTR_SIZE
;
834 rc
= ecryptfs_get_lower_file(dentry
, inode
);
837 crypt_stat
= &ecryptfs_inode_to_private(dentry
->d_inode
)->crypt_stat
;
838 /* Switch on growing or shrinking file */
839 if (ia
->ia_size
> i_size
) {
840 char zero
[] = { 0x00 };
842 lower_ia
->ia_valid
&= ~ATTR_SIZE
;
843 /* Write a single 0 at the last position of the file;
844 * this triggers code that will fill in 0's throughout
845 * the intermediate portion of the previous end of the
846 * file and the new and of the file */
847 rc
= ecryptfs_write(inode
, zero
,
848 (ia
->ia_size
- 1), 1);
849 } else { /* ia->ia_size < i_size_read(inode) */
850 /* We're chopping off all the pages down to the page
851 * in which ia->ia_size is located. Fill in the end of
852 * that page from (ia->ia_size & ~PAGE_CACHE_MASK) to
853 * PAGE_CACHE_SIZE with zeros. */
854 size_t num_zeros
= (PAGE_CACHE_SIZE
855 - (ia
->ia_size
& ~PAGE_CACHE_MASK
));
859 * XXX(truncate) this should really happen at the begginning
860 * of ->setattr. But the code is too messy to that as part
861 * of a larger patch. ecryptfs is also totally missing out
862 * on the inode_change_ok check at the beginning of
863 * ->setattr while would include this.
865 rc
= inode_newsize_ok(inode
, ia
->ia_size
);
869 if (!(crypt_stat
->flags
& ECRYPTFS_ENCRYPTED
)) {
870 truncate_setsize(inode
, ia
->ia_size
);
871 lower_ia
->ia_size
= ia
->ia_size
;
872 lower_ia
->ia_valid
|= ATTR_SIZE
;
878 zeros_virt
= kzalloc(num_zeros
, GFP_KERNEL
);
883 rc
= ecryptfs_write(inode
, zeros_virt
,
884 ia
->ia_size
, num_zeros
);
887 printk(KERN_ERR
"Error attempting to zero out "
888 "the remainder of the end page on "
889 "reducing truncate; rc = [%d]\n", rc
);
893 truncate_setsize(inode
, ia
->ia_size
);
894 rc
= ecryptfs_write_inode_size_to_metadata(inode
);
896 printk(KERN_ERR
"Problem with "
897 "ecryptfs_write_inode_size_to_metadata; "
901 /* We are reducing the size of the ecryptfs file, and need to
902 * know if we need to reduce the size of the lower file. */
903 lower_size_before_truncate
=
904 upper_size_to_lower_size(crypt_stat
, i_size
);
905 lower_size_after_truncate
=
906 upper_size_to_lower_size(crypt_stat
, ia
->ia_size
);
907 if (lower_size_after_truncate
< lower_size_before_truncate
) {
908 lower_ia
->ia_size
= lower_size_after_truncate
;
909 lower_ia
->ia_valid
|= ATTR_SIZE
;
911 lower_ia
->ia_valid
&= ~ATTR_SIZE
;
914 ecryptfs_put_lower_file(inode
);
920 * @dentry: The ecryptfs layer dentry
921 * @new_length: The length to expand the file to
923 * Simple function that handles the truncation of an eCryptfs inode and
924 * its corresponding lower inode.
926 * Returns zero on success; non-zero otherwise
928 int ecryptfs_truncate(struct dentry
*dentry
, loff_t new_length
)
930 struct iattr ia
= { .ia_valid
= ATTR_SIZE
, .ia_size
= new_length
};
931 struct iattr lower_ia
= { .ia_valid
= 0 };
934 rc
= truncate_upper(dentry
, &ia
, &lower_ia
);
935 if (!rc
&& lower_ia
.ia_valid
& ATTR_SIZE
) {
936 struct dentry
*lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
938 mutex_lock(&lower_dentry
->d_inode
->i_mutex
);
939 rc
= notify_change(lower_dentry
, &lower_ia
);
940 mutex_unlock(&lower_dentry
->d_inode
->i_mutex
);
946 ecryptfs_permission(struct inode
*inode
, int mask
, unsigned int flags
)
948 if (flags
& IPERM_FLAG_RCU
)
950 return inode_permission(ecryptfs_inode_to_lower(inode
), mask
);
955 * @dentry: dentry handle to the inode to modify
956 * @ia: Structure with flags of what to change and values
958 * Updates the metadata of an inode. If the update is to the size
959 * i.e. truncation, then ecryptfs_truncate will handle the size modification
960 * of both the ecryptfs inode and the lower inode.
962 * All other metadata changes will be passed right to the lower filesystem,
963 * and we will just update our inode to look like the lower.
965 static int ecryptfs_setattr(struct dentry
*dentry
, struct iattr
*ia
)
968 struct dentry
*lower_dentry
;
969 struct iattr lower_ia
;
971 struct inode
*lower_inode
;
972 struct ecryptfs_crypt_stat
*crypt_stat
;
974 crypt_stat
= &ecryptfs_inode_to_private(dentry
->d_inode
)->crypt_stat
;
975 if (!(crypt_stat
->flags
& ECRYPTFS_STRUCT_INITIALIZED
))
976 ecryptfs_init_crypt_stat(crypt_stat
);
977 inode
= dentry
->d_inode
;
978 lower_inode
= ecryptfs_inode_to_lower(inode
);
979 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
980 mutex_lock(&crypt_stat
->cs_mutex
);
981 if (S_ISDIR(dentry
->d_inode
->i_mode
))
982 crypt_stat
->flags
&= ~(ECRYPTFS_ENCRYPTED
);
983 else if (S_ISREG(dentry
->d_inode
->i_mode
)
984 && (!(crypt_stat
->flags
& ECRYPTFS_POLICY_APPLIED
)
985 || !(crypt_stat
->flags
& ECRYPTFS_KEY_VALID
))) {
986 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
;
988 mount_crypt_stat
= &ecryptfs_superblock_to_private(
989 dentry
->d_sb
)->mount_crypt_stat
;
990 rc
= ecryptfs_get_lower_file(dentry
, inode
);
992 mutex_unlock(&crypt_stat
->cs_mutex
);
995 rc
= ecryptfs_read_metadata(dentry
);
996 ecryptfs_put_lower_file(inode
);
998 if (!(mount_crypt_stat
->flags
999 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED
)) {
1001 printk(KERN_WARNING
"Either the lower file "
1002 "is not in a valid eCryptfs format, "
1003 "or the key could not be retrieved. "
1004 "Plaintext passthrough mode is not "
1005 "enabled; returning -EIO\n");
1006 mutex_unlock(&crypt_stat
->cs_mutex
);
1010 crypt_stat
->flags
&= ~(ECRYPTFS_I_SIZE_INITIALIZED
1011 | ECRYPTFS_ENCRYPTED
);
1014 mutex_unlock(&crypt_stat
->cs_mutex
);
1015 if (S_ISREG(inode
->i_mode
)) {
1016 rc
= filemap_write_and_wait(inode
->i_mapping
);
1019 fsstack_copy_attr_all(inode
, lower_inode
);
1021 memcpy(&lower_ia
, ia
, sizeof(lower_ia
));
1022 if (ia
->ia_valid
& ATTR_FILE
)
1023 lower_ia
.ia_file
= ecryptfs_file_to_lower(ia
->ia_file
);
1024 if (ia
->ia_valid
& ATTR_SIZE
) {
1025 rc
= truncate_upper(dentry
, ia
, &lower_ia
);
1031 * mode change is for clearing setuid/setgid bits. Allow lower fs
1032 * to interpret this in its own way.
1034 if (lower_ia
.ia_valid
& (ATTR_KILL_SUID
| ATTR_KILL_SGID
))
1035 lower_ia
.ia_valid
&= ~ATTR_MODE
;
1037 mutex_lock(&lower_dentry
->d_inode
->i_mutex
);
1038 rc
= notify_change(lower_dentry
, &lower_ia
);
1039 mutex_unlock(&lower_dentry
->d_inode
->i_mutex
);
1041 fsstack_copy_attr_all(inode
, lower_inode
);
1045 int ecryptfs_getattr_link(struct vfsmount
*mnt
, struct dentry
*dentry
,
1048 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
;
1051 mount_crypt_stat
= &ecryptfs_superblock_to_private(
1052 dentry
->d_sb
)->mount_crypt_stat
;
1053 generic_fillattr(dentry
->d_inode
, stat
);
1054 if (mount_crypt_stat
->flags
& ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
) {
1058 rc
= ecryptfs_readlink_lower(dentry
, &target
, &targetsiz
);
1061 stat
->size
= targetsiz
;
1067 int ecryptfs_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1070 struct kstat lower_stat
;
1073 rc
= vfs_getattr(ecryptfs_dentry_to_lower_mnt(dentry
),
1074 ecryptfs_dentry_to_lower(dentry
), &lower_stat
);
1076 fsstack_copy_attr_all(dentry
->d_inode
,
1077 ecryptfs_inode_to_lower(dentry
->d_inode
));
1078 generic_fillattr(dentry
->d_inode
, stat
);
1079 stat
->blocks
= lower_stat
.blocks
;
1085 ecryptfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
1086 size_t size
, int flags
)
1089 struct dentry
*lower_dentry
;
1091 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
1092 if (!lower_dentry
->d_inode
->i_op
->setxattr
) {
1097 rc
= vfs_setxattr(lower_dentry
, name
, value
, size
, flags
);
1103 ecryptfs_getxattr_lower(struct dentry
*lower_dentry
, const char *name
,
1104 void *value
, size_t size
)
1108 if (!lower_dentry
->d_inode
->i_op
->getxattr
) {
1112 mutex_lock(&lower_dentry
->d_inode
->i_mutex
);
1113 rc
= lower_dentry
->d_inode
->i_op
->getxattr(lower_dentry
, name
, value
,
1115 mutex_unlock(&lower_dentry
->d_inode
->i_mutex
);
1121 ecryptfs_getxattr(struct dentry
*dentry
, const char *name
, void *value
,
1124 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry
), name
,
1129 ecryptfs_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
1132 struct dentry
*lower_dentry
;
1134 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
1135 if (!lower_dentry
->d_inode
->i_op
->listxattr
) {
1139 mutex_lock(&lower_dentry
->d_inode
->i_mutex
);
1140 rc
= lower_dentry
->d_inode
->i_op
->listxattr(lower_dentry
, list
, size
);
1141 mutex_unlock(&lower_dentry
->d_inode
->i_mutex
);
1146 static int ecryptfs_removexattr(struct dentry
*dentry
, const char *name
)
1149 struct dentry
*lower_dentry
;
1151 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
1152 if (!lower_dentry
->d_inode
->i_op
->removexattr
) {
1156 mutex_lock(&lower_dentry
->d_inode
->i_mutex
);
1157 rc
= lower_dentry
->d_inode
->i_op
->removexattr(lower_dentry
, name
);
1158 mutex_unlock(&lower_dentry
->d_inode
->i_mutex
);
1163 const struct inode_operations ecryptfs_symlink_iops
= {
1164 .readlink
= ecryptfs_readlink
,
1165 .follow_link
= ecryptfs_follow_link
,
1166 .put_link
= ecryptfs_put_link
,
1167 .permission
= ecryptfs_permission
,
1168 .setattr
= ecryptfs_setattr
,
1169 .getattr
= ecryptfs_getattr_link
,
1170 .setxattr
= ecryptfs_setxattr
,
1171 .getxattr
= ecryptfs_getxattr
,
1172 .listxattr
= ecryptfs_listxattr
,
1173 .removexattr
= ecryptfs_removexattr
1176 const struct inode_operations ecryptfs_dir_iops
= {
1177 .create
= ecryptfs_create
,
1178 .lookup
= ecryptfs_lookup
,
1179 .link
= ecryptfs_link
,
1180 .unlink
= ecryptfs_unlink
,
1181 .symlink
= ecryptfs_symlink
,
1182 .mkdir
= ecryptfs_mkdir
,
1183 .rmdir
= ecryptfs_rmdir
,
1184 .mknod
= ecryptfs_mknod
,
1185 .rename
= ecryptfs_rename
,
1186 .permission
= ecryptfs_permission
,
1187 .setattr
= ecryptfs_setattr
,
1188 .setxattr
= ecryptfs_setxattr
,
1189 .getxattr
= ecryptfs_getxattr
,
1190 .listxattr
= ecryptfs_listxattr
,
1191 .removexattr
= ecryptfs_removexattr
1194 const struct inode_operations ecryptfs_main_iops
= {
1195 .permission
= ecryptfs_permission
,
1196 .setattr
= ecryptfs_setattr
,
1197 .getattr
= ecryptfs_getattr
,
1198 .setxattr
= ecryptfs_setxattr
,
1199 .getxattr
= ecryptfs_getxattr
,
1200 .listxattr
= ecryptfs_listxattr
,
1201 .removexattr
= ecryptfs_removexattr