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 <asm/unaligned.h>
35 #include "ecryptfs_kernel.h"
37 static struct dentry
*lock_parent(struct dentry
*dentry
)
41 dir
= dget_parent(dentry
);
42 mutex_lock_nested(&(dir
->d_inode
->i_mutex
), I_MUTEX_PARENT
);
46 static void unlock_dir(struct dentry
*dir
)
48 mutex_unlock(&dir
->d_inode
->i_mutex
);
53 * ecryptfs_create_underlying_file
54 * @lower_dir_inode: inode of the parent in the lower fs of the new file
55 * @lower_dentry: New file's dentry in the lower fs
56 * @ecryptfs_dentry: New file's dentry in ecryptfs
57 * @mode: The mode of the new file
58 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
60 * Creates the file in the lower file system.
62 * Returns zero on success; non-zero on error condition
65 ecryptfs_create_underlying_file(struct inode
*lower_dir_inode
,
66 struct dentry
*dentry
, int mode
,
69 struct dentry
*lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
70 struct vfsmount
*lower_mnt
= ecryptfs_dentry_to_lower_mnt(dentry
);
71 struct dentry
*dentry_save
;
72 struct vfsmount
*vfsmount_save
;
75 dentry_save
= nd
->path
.dentry
;
76 vfsmount_save
= nd
->path
.mnt
;
77 nd
->path
.dentry
= lower_dentry
;
78 nd
->path
.mnt
= lower_mnt
;
79 rc
= vfs_create(lower_dir_inode
, lower_dentry
, mode
, nd
);
80 nd
->path
.dentry
= dentry_save
;
81 nd
->path
.mnt
= vfsmount_save
;
87 * @directory_inode: inode of the new file's dentry's parent in ecryptfs
88 * @ecryptfs_dentry: New file's dentry in ecryptfs
89 * @mode: The mode of the new file
90 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
92 * Creates the underlying file and the eCryptfs inode which will link to
93 * it. It will also update the eCryptfs directory inode to mimic the
94 * stat of the lower directory inode.
96 * Returns zero on success; non-zero on error condition
99 ecryptfs_do_create(struct inode
*directory_inode
,
100 struct dentry
*ecryptfs_dentry
, int mode
,
101 struct nameidata
*nd
)
104 struct dentry
*lower_dentry
;
105 struct dentry
*lower_dir_dentry
;
107 lower_dentry
= ecryptfs_dentry_to_lower(ecryptfs_dentry
);
108 lower_dir_dentry
= lock_parent(lower_dentry
);
109 if (IS_ERR(lower_dir_dentry
)) {
110 ecryptfs_printk(KERN_ERR
, "Error locking directory of "
112 rc
= PTR_ERR(lower_dir_dentry
);
115 rc
= ecryptfs_create_underlying_file(lower_dir_dentry
->d_inode
,
116 ecryptfs_dentry
, mode
, nd
);
118 printk(KERN_ERR
"%s: Failure to create dentry in lower fs; "
119 "rc = [%d]\n", __func__
, rc
);
122 rc
= ecryptfs_interpose(lower_dentry
, ecryptfs_dentry
,
123 directory_inode
->i_sb
, 0);
125 ecryptfs_printk(KERN_ERR
, "Failure in ecryptfs_interpose\n");
128 fsstack_copy_attr_times(directory_inode
, lower_dir_dentry
->d_inode
);
129 fsstack_copy_inode_size(directory_inode
, lower_dir_dentry
->d_inode
);
131 unlock_dir(lower_dir_dentry
);
138 * @ecryptfs_dentry: the eCryptfs dentry
140 * This is the code which will grow the file to its correct size.
142 static int grow_file(struct dentry
*ecryptfs_dentry
)
144 struct inode
*ecryptfs_inode
= ecryptfs_dentry
->d_inode
;
145 struct file fake_file
;
146 struct ecryptfs_file_info tmp_file_info
;
147 char zero_virt
[] = { 0x00 };
150 memset(&fake_file
, 0, sizeof(fake_file
));
151 fake_file
.f_path
.dentry
= ecryptfs_dentry
;
152 memset(&tmp_file_info
, 0, sizeof(tmp_file_info
));
153 ecryptfs_set_file_private(&fake_file
, &tmp_file_info
);
154 ecryptfs_set_file_lower(
156 ecryptfs_inode_to_private(ecryptfs_inode
)->lower_file
);
157 rc
= ecryptfs_write(&fake_file
, zero_virt
, 0, 1);
158 i_size_write(ecryptfs_inode
, 0);
159 rc
= ecryptfs_write_inode_size_to_metadata(ecryptfs_inode
);
160 ecryptfs_inode_to_private(ecryptfs_inode
)->crypt_stat
.flags
|=
166 * ecryptfs_initialize_file
168 * Cause the file to be changed from a basic empty file to an ecryptfs
169 * file with a header and first data page.
171 * Returns zero on success
173 static int ecryptfs_initialize_file(struct dentry
*ecryptfs_dentry
)
175 struct ecryptfs_crypt_stat
*crypt_stat
=
176 &ecryptfs_inode_to_private(ecryptfs_dentry
->d_inode
)->crypt_stat
;
179 if (S_ISDIR(ecryptfs_dentry
->d_inode
->i_mode
)) {
180 ecryptfs_printk(KERN_DEBUG
, "This is a directory\n");
181 crypt_stat
->flags
&= ~(ECRYPTFS_ENCRYPTED
);
184 crypt_stat
->flags
|= ECRYPTFS_NEW_FILE
;
185 ecryptfs_printk(KERN_DEBUG
, "Initializing crypto context\n");
186 rc
= ecryptfs_new_file_context(ecryptfs_dentry
);
188 ecryptfs_printk(KERN_ERR
, "Error creating new file "
189 "context; rc = [%d]\n", rc
);
192 rc
= ecryptfs_write_metadata(ecryptfs_dentry
);
194 printk(KERN_ERR
"Error writing headers; rc = [%d]\n", rc
);
197 rc
= grow_file(ecryptfs_dentry
);
199 printk(KERN_ERR
"Error growing file; rc = [%d]\n", rc
);
206 * @dir: The inode of the directory in which to create the file.
207 * @dentry: The eCryptfs dentry
208 * @mode: The mode of the new file.
211 * Creates a new file.
213 * Returns zero on success; non-zero on error condition
216 ecryptfs_create(struct inode
*directory_inode
, struct dentry
*ecryptfs_dentry
,
217 int mode
, struct nameidata
*nd
)
221 /* ecryptfs_do_create() calls ecryptfs_interpose(), which opens
222 * the crypt_stat->lower_file (persistent file) */
223 rc
= ecryptfs_do_create(directory_inode
, ecryptfs_dentry
, mode
, nd
);
225 ecryptfs_printk(KERN_WARNING
, "Failed to create file in"
226 "lower filesystem\n");
229 /* At this point, a file exists on "disk"; we need to make sure
230 * that this on disk file is prepared to be an ecryptfs file */
231 rc
= ecryptfs_initialize_file(ecryptfs_dentry
);
239 * @dentry: The dentry
240 * @nd: nameidata, may be NULL
242 * Find a file on disk. If the file does not exist, then we'll add it to the
243 * dentry cache and continue on to read it from the disk.
245 static struct dentry
*ecryptfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
246 struct nameidata
*nd
)
249 struct dentry
*lower_dir_dentry
;
250 struct dentry
*lower_dentry
;
251 struct vfsmount
*lower_mnt
;
254 struct ecryptfs_crypt_stat
*crypt_stat
= NULL
;
255 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
;
256 char *page_virt
= NULL
;
257 struct inode
*lower_inode
;
260 lower_dir_dentry
= ecryptfs_dentry_to_lower(dentry
->d_parent
);
261 dentry
->d_op
= &ecryptfs_dops
;
262 if ((dentry
->d_name
.len
== 1 && !strcmp(dentry
->d_name
.name
, "."))
263 || (dentry
->d_name
.len
== 2
264 && !strcmp(dentry
->d_name
.name
, ".."))) {
268 encoded_namelen
= ecryptfs_encode_filename(crypt_stat
,
272 if (encoded_namelen
< 0) {
273 rc
= encoded_namelen
;
277 ecryptfs_printk(KERN_DEBUG
, "encoded_name = [%s]; encoded_namelen "
278 "= [%d]\n", encoded_name
, encoded_namelen
);
279 lower_dentry
= lookup_one_len(encoded_name
, lower_dir_dentry
,
280 encoded_namelen
- 1);
282 if (IS_ERR(lower_dentry
)) {
283 ecryptfs_printk(KERN_ERR
, "ERR from lower_dentry\n");
284 rc
= PTR_ERR(lower_dentry
);
288 lower_mnt
= mntget(ecryptfs_dentry_to_lower_mnt(dentry
->d_parent
));
289 ecryptfs_printk(KERN_DEBUG
, "lower_dentry = [%p]; lower_dentry->"
290 "d_name.name = [%s]\n", lower_dentry
,
291 lower_dentry
->d_name
.name
);
292 lower_inode
= lower_dentry
->d_inode
;
293 fsstack_copy_attr_atime(dir
, lower_dir_dentry
->d_inode
);
294 BUG_ON(!atomic_read(&lower_dentry
->d_count
));
295 ecryptfs_set_dentry_private(dentry
,
296 kmem_cache_alloc(ecryptfs_dentry_info_cache
,
298 if (!ecryptfs_dentry_to_private(dentry
)) {
300 ecryptfs_printk(KERN_ERR
, "Out of memory whilst attempting "
301 "to allocate ecryptfs_dentry_info struct\n");
304 ecryptfs_set_dentry_lower(dentry
, lower_dentry
);
305 ecryptfs_set_dentry_lower_mnt(dentry
, lower_mnt
);
306 if (!lower_dentry
->d_inode
) {
307 /* We want to add because we couldn't find in lower */
311 rc
= ecryptfs_interpose(lower_dentry
, dentry
, dir
->i_sb
, 1);
313 ecryptfs_printk(KERN_ERR
, "Error interposing\n");
316 if (S_ISDIR(lower_inode
->i_mode
)) {
317 ecryptfs_printk(KERN_DEBUG
, "Is a directory; returning\n");
320 if (S_ISLNK(lower_inode
->i_mode
)) {
321 ecryptfs_printk(KERN_DEBUG
, "Is a symlink; returning\n");
324 if (special_file(lower_inode
->i_mode
)) {
325 ecryptfs_printk(KERN_DEBUG
, "Is a special file; returning\n");
329 ecryptfs_printk(KERN_DEBUG
, "We have a NULL nd, just leave"
330 "as we *think* we are about to unlink\n");
333 /* Released in this function */
334 page_virt
= kmem_cache_zalloc(ecryptfs_header_cache_2
,
338 ecryptfs_printk(KERN_ERR
,
339 "Cannot ecryptfs_kmalloc a page\n");
342 crypt_stat
= &ecryptfs_inode_to_private(dentry
->d_inode
)->crypt_stat
;
343 if (!(crypt_stat
->flags
& ECRYPTFS_POLICY_APPLIED
))
344 ecryptfs_set_default_sizes(crypt_stat
);
345 rc
= ecryptfs_read_and_validate_header_region(page_virt
,
348 rc
= ecryptfs_read_and_validate_xattr_region(page_virt
, dentry
);
350 printk(KERN_DEBUG
"Valid metadata not found in header "
351 "region or xattr region; treating file as "
354 kmem_cache_free(ecryptfs_header_cache_2
, page_virt
);
357 crypt_stat
->flags
|= ECRYPTFS_METADATA_IN_XATTR
;
359 mount_crypt_stat
= &ecryptfs_superblock_to_private(
360 dentry
->d_sb
)->mount_crypt_stat
;
361 if (mount_crypt_stat
->flags
& ECRYPTFS_ENCRYPTED_VIEW_ENABLED
) {
362 if (crypt_stat
->flags
& ECRYPTFS_METADATA_IN_XATTR
)
363 file_size
= (crypt_stat
->num_header_bytes_at_front
364 + i_size_read(lower_dentry
->d_inode
));
366 file_size
= i_size_read(lower_dentry
->d_inode
);
368 file_size
= get_unaligned_be64(page_virt
);
370 i_size_write(dentry
->d_inode
, (loff_t
)file_size
);
371 kmem_cache_free(ecryptfs_header_cache_2
, page_virt
);
381 static int ecryptfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
382 struct dentry
*new_dentry
)
384 struct dentry
*lower_old_dentry
;
385 struct dentry
*lower_new_dentry
;
386 struct dentry
*lower_dir_dentry
;
390 file_size_save
= i_size_read(old_dentry
->d_inode
);
391 lower_old_dentry
= ecryptfs_dentry_to_lower(old_dentry
);
392 lower_new_dentry
= ecryptfs_dentry_to_lower(new_dentry
);
393 dget(lower_old_dentry
);
394 dget(lower_new_dentry
);
395 lower_dir_dentry
= lock_parent(lower_new_dentry
);
396 rc
= vfs_link(lower_old_dentry
, lower_dir_dentry
->d_inode
,
398 if (rc
|| !lower_new_dentry
->d_inode
)
400 rc
= ecryptfs_interpose(lower_new_dentry
, new_dentry
, dir
->i_sb
, 0);
403 fsstack_copy_attr_times(dir
, lower_new_dentry
->d_inode
);
404 fsstack_copy_inode_size(dir
, lower_new_dentry
->d_inode
);
405 old_dentry
->d_inode
->i_nlink
=
406 ecryptfs_inode_to_lower(old_dentry
->d_inode
)->i_nlink
;
407 i_size_write(new_dentry
->d_inode
, file_size_save
);
409 unlock_dir(lower_dir_dentry
);
410 dput(lower_new_dentry
);
411 dput(lower_old_dentry
);
412 d_drop(lower_old_dentry
);
418 static int ecryptfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
421 struct dentry
*lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
422 struct inode
*lower_dir_inode
= ecryptfs_inode_to_lower(dir
);
423 struct dentry
*lower_dir_dentry
;
425 lower_dir_dentry
= lock_parent(lower_dentry
);
426 rc
= vfs_unlink(lower_dir_inode
, lower_dentry
);
428 printk(KERN_ERR
"Error in vfs_unlink; rc = [%d]\n", rc
);
431 fsstack_copy_attr_times(dir
, lower_dir_inode
);
432 dentry
->d_inode
->i_nlink
=
433 ecryptfs_inode_to_lower(dentry
->d_inode
)->i_nlink
;
434 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
437 unlock_dir(lower_dir_dentry
);
441 static int ecryptfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
445 struct dentry
*lower_dentry
;
446 struct dentry
*lower_dir_dentry
;
448 char *encoded_symname
;
450 struct ecryptfs_crypt_stat
*crypt_stat
= NULL
;
452 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
454 lower_dir_dentry
= lock_parent(lower_dentry
);
456 encoded_symlen
= ecryptfs_encode_filename(crypt_stat
, symname
,
459 if (encoded_symlen
< 0) {
463 rc
= vfs_symlink(lower_dir_dentry
->d_inode
, lower_dentry
,
464 encoded_symname
, mode
);
465 kfree(encoded_symname
);
466 if (rc
|| !lower_dentry
->d_inode
)
468 rc
= ecryptfs_interpose(lower_dentry
, dentry
, dir
->i_sb
, 0);
471 fsstack_copy_attr_times(dir
, lower_dir_dentry
->d_inode
);
472 fsstack_copy_inode_size(dir
, lower_dir_dentry
->d_inode
);
474 unlock_dir(lower_dir_dentry
);
476 if (!dentry
->d_inode
)
481 static int ecryptfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
484 struct dentry
*lower_dentry
;
485 struct dentry
*lower_dir_dentry
;
487 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
488 lower_dir_dentry
= lock_parent(lower_dentry
);
489 rc
= vfs_mkdir(lower_dir_dentry
->d_inode
, lower_dentry
, mode
);
490 if (rc
|| !lower_dentry
->d_inode
)
492 rc
= ecryptfs_interpose(lower_dentry
, dentry
, dir
->i_sb
, 0);
495 fsstack_copy_attr_times(dir
, lower_dir_dentry
->d_inode
);
496 fsstack_copy_inode_size(dir
, lower_dir_dentry
->d_inode
);
497 dir
->i_nlink
= lower_dir_dentry
->d_inode
->i_nlink
;
499 unlock_dir(lower_dir_dentry
);
500 if (!dentry
->d_inode
)
505 static int ecryptfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
507 struct dentry
*lower_dentry
;
508 struct dentry
*lower_dir_dentry
;
511 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
513 lower_dir_dentry
= lock_parent(lower_dentry
);
515 rc
= vfs_rmdir(lower_dir_dentry
->d_inode
, lower_dentry
);
518 d_delete(lower_dentry
);
519 fsstack_copy_attr_times(dir
, lower_dir_dentry
->d_inode
);
520 dir
->i_nlink
= lower_dir_dentry
->d_inode
->i_nlink
;
521 unlock_dir(lower_dir_dentry
);
529 ecryptfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
532 struct dentry
*lower_dentry
;
533 struct dentry
*lower_dir_dentry
;
535 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
536 lower_dir_dentry
= lock_parent(lower_dentry
);
537 rc
= vfs_mknod(lower_dir_dentry
->d_inode
, lower_dentry
, mode
, dev
);
538 if (rc
|| !lower_dentry
->d_inode
)
540 rc
= ecryptfs_interpose(lower_dentry
, dentry
, dir
->i_sb
, 0);
543 fsstack_copy_attr_times(dir
, lower_dir_dentry
->d_inode
);
544 fsstack_copy_inode_size(dir
, lower_dir_dentry
->d_inode
);
546 unlock_dir(lower_dir_dentry
);
547 if (!dentry
->d_inode
)
553 ecryptfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
554 struct inode
*new_dir
, struct dentry
*new_dentry
)
557 struct dentry
*lower_old_dentry
;
558 struct dentry
*lower_new_dentry
;
559 struct dentry
*lower_old_dir_dentry
;
560 struct dentry
*lower_new_dir_dentry
;
562 lower_old_dentry
= ecryptfs_dentry_to_lower(old_dentry
);
563 lower_new_dentry
= ecryptfs_dentry_to_lower(new_dentry
);
564 dget(lower_old_dentry
);
565 dget(lower_new_dentry
);
566 lower_old_dir_dentry
= dget_parent(lower_old_dentry
);
567 lower_new_dir_dentry
= dget_parent(lower_new_dentry
);
568 lock_rename(lower_old_dir_dentry
, lower_new_dir_dentry
);
569 rc
= vfs_rename(lower_old_dir_dentry
->d_inode
, lower_old_dentry
,
570 lower_new_dir_dentry
->d_inode
, lower_new_dentry
);
573 fsstack_copy_attr_all(new_dir
, lower_new_dir_dentry
->d_inode
, NULL
);
574 if (new_dir
!= old_dir
)
575 fsstack_copy_attr_all(old_dir
, lower_old_dir_dentry
->d_inode
, NULL
);
577 unlock_rename(lower_old_dir_dentry
, lower_new_dir_dentry
);
578 dput(lower_new_dentry
->d_parent
);
579 dput(lower_old_dentry
->d_parent
);
580 dput(lower_new_dentry
);
581 dput(lower_old_dentry
);
586 ecryptfs_readlink(struct dentry
*dentry
, char __user
* buf
, int bufsiz
)
589 struct dentry
*lower_dentry
;
593 struct ecryptfs_crypt_stat
*crypt_stat
;
595 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
596 if (!lower_dentry
->d_inode
->i_op
||
597 !lower_dentry
->d_inode
->i_op
->readlink
) {
601 /* Released in this function */
602 lower_buf
= kmalloc(bufsiz
, GFP_KERNEL
);
603 if (lower_buf
== NULL
) {
604 ecryptfs_printk(KERN_ERR
, "Out of memory\n");
610 ecryptfs_printk(KERN_DEBUG
, "Calling readlink w/ "
611 "lower_dentry->d_name.name = [%s]\n",
612 lower_dentry
->d_name
.name
);
613 rc
= lower_dentry
->d_inode
->i_op
->readlink(lower_dentry
,
614 (char __user
*)lower_buf
,
619 rc
= ecryptfs_decode_filename(crypt_stat
, lower_buf
, rc
,
622 goto out_free_lower_buf
;
624 ecryptfs_printk(KERN_DEBUG
, "Copying [%d] bytes "
625 "to userspace: [%*s]\n", rc
,
627 if (copy_to_user(buf
, decoded_name
, rc
))
631 fsstack_copy_attr_atime(dentry
->d_inode
,
632 lower_dentry
->d_inode
);
640 static void *ecryptfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
643 int len
= PAGE_SIZE
, rc
;
646 /* Released in ecryptfs_put_link(); only release here on error */
647 buf
= kmalloc(len
, GFP_KERNEL
);
654 ecryptfs_printk(KERN_DEBUG
, "Calling readlink w/ "
655 "dentry->d_name.name = [%s]\n", dentry
->d_name
.name
);
656 rc
= dentry
->d_inode
->i_op
->readlink(dentry
, (char __user
*)buf
, len
);
662 nd_set_link(nd
, buf
);
671 ecryptfs_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *ptr
)
674 kfree(nd_get_link(nd
));
678 * upper_size_to_lower_size
679 * @crypt_stat: Crypt_stat associated with file
680 * @upper_size: Size of the upper file
682 * Calculate the required size of the lower file based on the
683 * specified size of the upper file. This calculation is based on the
684 * number of headers in the underlying file and the extent size.
686 * Returns Calculated size of the lower file.
689 upper_size_to_lower_size(struct ecryptfs_crypt_stat
*crypt_stat
,
694 lower_size
= crypt_stat
->num_header_bytes_at_front
;
695 if (upper_size
!= 0) {
698 num_extents
= upper_size
>> crypt_stat
->extent_shift
;
699 if (upper_size
& ~crypt_stat
->extent_mask
)
701 lower_size
+= (num_extents
* crypt_stat
->extent_size
);
708 * @dentry: The ecryptfs layer dentry
709 * @new_length: The length to expand the file to
711 * Function to handle truncations modifying the size of the file. Note
712 * that the file sizes are interpolated. When expanding, we are simply
713 * writing strings of 0's out. When truncating, we need to modify the
714 * underlying file size according to the page index interpolations.
716 * Returns zero on success; non-zero otherwise
718 int ecryptfs_truncate(struct dentry
*dentry
, loff_t new_length
)
721 struct inode
*inode
= dentry
->d_inode
;
722 struct dentry
*lower_dentry
;
723 struct file fake_ecryptfs_file
;
724 struct ecryptfs_crypt_stat
*crypt_stat
;
725 loff_t i_size
= i_size_read(inode
);
726 loff_t lower_size_before_truncate
;
727 loff_t lower_size_after_truncate
;
729 if (unlikely((new_length
== i_size
)))
731 crypt_stat
= &ecryptfs_inode_to_private(dentry
->d_inode
)->crypt_stat
;
732 /* Set up a fake ecryptfs file, this is used to interface with
733 * the file in the underlying filesystem so that the
734 * truncation has an effect there as well. */
735 memset(&fake_ecryptfs_file
, 0, sizeof(fake_ecryptfs_file
));
736 fake_ecryptfs_file
.f_path
.dentry
= dentry
;
737 /* Released at out_free: label */
738 ecryptfs_set_file_private(&fake_ecryptfs_file
,
739 kmem_cache_alloc(ecryptfs_file_info_cache
,
741 if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file
))) {
745 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
746 ecryptfs_set_file_lower(
748 ecryptfs_inode_to_private(dentry
->d_inode
)->lower_file
);
749 /* Switch on growing or shrinking file */
750 if (new_length
> i_size
) {
751 char zero
[] = { 0x00 };
753 /* Write a single 0 at the last position of the file;
754 * this triggers code that will fill in 0's throughout
755 * the intermediate portion of the previous end of the
756 * file and the new and of the file */
757 rc
= ecryptfs_write(&fake_ecryptfs_file
, zero
,
758 (new_length
- 1), 1);
759 } else { /* new_length < i_size_read(inode) */
760 /* We're chopping off all the pages down do the page
761 * in which new_length is located. Fill in the end of
762 * that page from (new_length & ~PAGE_CACHE_MASK) to
763 * PAGE_CACHE_SIZE with zeros. */
764 size_t num_zeros
= (PAGE_CACHE_SIZE
765 - (new_length
& ~PAGE_CACHE_MASK
));
770 zeros_virt
= kzalloc(num_zeros
, GFP_KERNEL
);
775 rc
= ecryptfs_write(&fake_ecryptfs_file
, zeros_virt
,
776 new_length
, num_zeros
);
779 printk(KERN_ERR
"Error attempting to zero out "
780 "the remainder of the end page on "
781 "reducing truncate; rc = [%d]\n", rc
);
785 vmtruncate(inode
, new_length
);
786 rc
= ecryptfs_write_inode_size_to_metadata(inode
);
788 printk(KERN_ERR
"Problem with "
789 "ecryptfs_write_inode_size_to_metadata; "
793 /* We are reducing the size of the ecryptfs file, and need to
794 * know if we need to reduce the size of the lower file. */
795 lower_size_before_truncate
=
796 upper_size_to_lower_size(crypt_stat
, i_size
);
797 lower_size_after_truncate
=
798 upper_size_to_lower_size(crypt_stat
, new_length
);
799 if (lower_size_after_truncate
< lower_size_before_truncate
)
800 vmtruncate(lower_dentry
->d_inode
,
801 lower_size_after_truncate
);
804 if (ecryptfs_file_to_private(&fake_ecryptfs_file
))
805 kmem_cache_free(ecryptfs_file_info_cache
,
806 ecryptfs_file_to_private(&fake_ecryptfs_file
));
812 ecryptfs_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
817 struct vfsmount
*vfsmnt_save
= nd
->path
.mnt
;
818 struct dentry
*dentry_save
= nd
->path
.dentry
;
820 nd
->path
.mnt
= ecryptfs_dentry_to_lower_mnt(nd
->path
.dentry
);
821 nd
->path
.dentry
= ecryptfs_dentry_to_lower(nd
->path
.dentry
);
822 rc
= permission(ecryptfs_inode_to_lower(inode
), mask
, nd
);
823 nd
->path
.mnt
= vfsmnt_save
;
824 nd
->path
.dentry
= dentry_save
;
826 rc
= permission(ecryptfs_inode_to_lower(inode
), mask
, NULL
);
832 * @dentry: dentry handle to the inode to modify
833 * @ia: Structure with flags of what to change and values
835 * Updates the metadata of an inode. If the update is to the size
836 * i.e. truncation, then ecryptfs_truncate will handle the size modification
837 * of both the ecryptfs inode and the lower inode.
839 * All other metadata changes will be passed right to the lower filesystem,
840 * and we will just update our inode to look like the lower.
842 static int ecryptfs_setattr(struct dentry
*dentry
, struct iattr
*ia
)
845 struct dentry
*lower_dentry
;
847 struct inode
*lower_inode
;
848 struct ecryptfs_crypt_stat
*crypt_stat
;
850 crypt_stat
= &ecryptfs_inode_to_private(dentry
->d_inode
)->crypt_stat
;
851 if (!(crypt_stat
->flags
& ECRYPTFS_STRUCT_INITIALIZED
))
852 ecryptfs_init_crypt_stat(crypt_stat
);
853 inode
= dentry
->d_inode
;
854 lower_inode
= ecryptfs_inode_to_lower(inode
);
855 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
856 mutex_lock(&crypt_stat
->cs_mutex
);
857 if (S_ISDIR(dentry
->d_inode
->i_mode
))
858 crypt_stat
->flags
&= ~(ECRYPTFS_ENCRYPTED
);
859 else if (S_ISREG(dentry
->d_inode
->i_mode
)
860 && (!(crypt_stat
->flags
& ECRYPTFS_POLICY_APPLIED
)
861 || !(crypt_stat
->flags
& ECRYPTFS_KEY_VALID
))) {
862 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
;
864 mount_crypt_stat
= &ecryptfs_superblock_to_private(
865 dentry
->d_sb
)->mount_crypt_stat
;
866 rc
= ecryptfs_read_metadata(dentry
);
868 if (!(mount_crypt_stat
->flags
869 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED
)) {
871 printk(KERN_WARNING
"Either the lower file "
872 "is not in a valid eCryptfs format, "
873 "or the key could not be retrieved. "
874 "Plaintext passthrough mode is not "
875 "enabled; returning -EIO\n");
876 mutex_unlock(&crypt_stat
->cs_mutex
);
880 crypt_stat
->flags
&= ~(ECRYPTFS_ENCRYPTED
);
881 mutex_unlock(&crypt_stat
->cs_mutex
);
885 mutex_unlock(&crypt_stat
->cs_mutex
);
886 if (ia
->ia_valid
& ATTR_SIZE
) {
887 ecryptfs_printk(KERN_DEBUG
,
888 "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
889 ia
->ia_valid
, ATTR_SIZE
);
890 rc
= ecryptfs_truncate(dentry
, ia
->ia_size
);
891 /* ecryptfs_truncate handles resizing of the lower file */
892 ia
->ia_valid
&= ~ATTR_SIZE
;
893 ecryptfs_printk(KERN_DEBUG
, "ia->ia_valid = [%x]\n",
900 * mode change is for clearing setuid/setgid bits. Allow lower fs
901 * to interpret this in its own way.
903 if (ia
->ia_valid
& (ATTR_KILL_SUID
| ATTR_KILL_SGID
))
904 ia
->ia_valid
&= ~ATTR_MODE
;
906 mutex_lock(&lower_dentry
->d_inode
->i_mutex
);
907 rc
= notify_change(lower_dentry
, ia
);
908 mutex_unlock(&lower_dentry
->d_inode
->i_mutex
);
910 fsstack_copy_attr_all(inode
, lower_inode
, NULL
);
915 ecryptfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
916 size_t size
, int flags
)
919 struct dentry
*lower_dentry
;
921 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
922 if (!lower_dentry
->d_inode
->i_op
->setxattr
) {
926 mutex_lock(&lower_dentry
->d_inode
->i_mutex
);
927 rc
= lower_dentry
->d_inode
->i_op
->setxattr(lower_dentry
, name
, value
,
929 mutex_unlock(&lower_dentry
->d_inode
->i_mutex
);
935 ecryptfs_getxattr_lower(struct dentry
*lower_dentry
, const char *name
,
936 void *value
, size_t size
)
940 if (!lower_dentry
->d_inode
->i_op
->getxattr
) {
944 mutex_lock(&lower_dentry
->d_inode
->i_mutex
);
945 rc
= lower_dentry
->d_inode
->i_op
->getxattr(lower_dentry
, name
, value
,
947 mutex_unlock(&lower_dentry
->d_inode
->i_mutex
);
953 ecryptfs_getxattr(struct dentry
*dentry
, const char *name
, void *value
,
956 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry
), name
,
961 ecryptfs_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
964 struct dentry
*lower_dentry
;
966 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
967 if (!lower_dentry
->d_inode
->i_op
->listxattr
) {
971 mutex_lock(&lower_dentry
->d_inode
->i_mutex
);
972 rc
= lower_dentry
->d_inode
->i_op
->listxattr(lower_dentry
, list
, size
);
973 mutex_unlock(&lower_dentry
->d_inode
->i_mutex
);
978 static int ecryptfs_removexattr(struct dentry
*dentry
, const char *name
)
981 struct dentry
*lower_dentry
;
983 lower_dentry
= ecryptfs_dentry_to_lower(dentry
);
984 if (!lower_dentry
->d_inode
->i_op
->removexattr
) {
988 mutex_lock(&lower_dentry
->d_inode
->i_mutex
);
989 rc
= lower_dentry
->d_inode
->i_op
->removexattr(lower_dentry
, name
);
990 mutex_unlock(&lower_dentry
->d_inode
->i_mutex
);
995 int ecryptfs_inode_test(struct inode
*inode
, void *candidate_lower_inode
)
997 if ((ecryptfs_inode_to_lower(inode
)
998 == (struct inode
*)candidate_lower_inode
))
1004 int ecryptfs_inode_set(struct inode
*inode
, void *lower_inode
)
1006 ecryptfs_init_inode(inode
, (struct inode
*)lower_inode
);
1010 const struct inode_operations ecryptfs_symlink_iops
= {
1011 .readlink
= ecryptfs_readlink
,
1012 .follow_link
= ecryptfs_follow_link
,
1013 .put_link
= ecryptfs_put_link
,
1014 .permission
= ecryptfs_permission
,
1015 .setattr
= ecryptfs_setattr
,
1016 .setxattr
= ecryptfs_setxattr
,
1017 .getxattr
= ecryptfs_getxattr
,
1018 .listxattr
= ecryptfs_listxattr
,
1019 .removexattr
= ecryptfs_removexattr
1022 const struct inode_operations ecryptfs_dir_iops
= {
1023 .create
= ecryptfs_create
,
1024 .lookup
= ecryptfs_lookup
,
1025 .link
= ecryptfs_link
,
1026 .unlink
= ecryptfs_unlink
,
1027 .symlink
= ecryptfs_symlink
,
1028 .mkdir
= ecryptfs_mkdir
,
1029 .rmdir
= ecryptfs_rmdir
,
1030 .mknod
= ecryptfs_mknod
,
1031 .rename
= ecryptfs_rename
,
1032 .permission
= ecryptfs_permission
,
1033 .setattr
= ecryptfs_setattr
,
1034 .setxattr
= ecryptfs_setxattr
,
1035 .getxattr
= ecryptfs_getxattr
,
1036 .listxattr
= ecryptfs_listxattr
,
1037 .removexattr
= ecryptfs_removexattr
1040 const struct inode_operations ecryptfs_main_iops
= {
1041 .permission
= ecryptfs_permission
,
1042 .setattr
= ecryptfs_setattr
,
1043 .setxattr
= ecryptfs_setxattr
,
1044 .getxattr
= ecryptfs_getxattr
,
1045 .listxattr
= ecryptfs_listxattr
,
1046 .removexattr
= ecryptfs_removexattr