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 "ecryptfs_kernel.h"
36 static struct dentry *lock_parent(struct dentry *dentry)
40 dir = dget_parent(dentry);
41 mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT);
45 static void unlock_dir(struct dentry *dir)
47 mutex_unlock(&dir->d_inode->i_mutex);
52 * ecryptfs_create_underlying_file
53 * @lower_dir_inode: inode of the parent in the lower fs of the new file
54 * @lower_dentry: New file's dentry in the lower fs
55 * @ecryptfs_dentry: New file's dentry in ecryptfs
56 * @mode: The mode of the new file
57 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
59 * Creates the file in the lower file system.
61 * Returns zero on success; non-zero on error condition
64 ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
65 struct dentry *dentry, int mode,
68 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
69 struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
70 struct dentry *dentry_save;
71 struct vfsmount *vfsmount_save;
74 dentry_save = nd->path.dentry;
75 vfsmount_save = nd->path.mnt;
76 nd->path.dentry = lower_dentry;
77 nd->path.mnt = lower_mnt;
78 rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
79 nd->path.dentry = dentry_save;
80 nd->path.mnt = vfsmount_save;
86 * @directory_inode: inode of the new file's dentry's parent in ecryptfs
87 * @ecryptfs_dentry: New file's dentry in ecryptfs
88 * @mode: The mode of the new file
89 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
91 * Creates the underlying file and the eCryptfs inode which will link to
92 * it. It will also update the eCryptfs directory inode to mimic the
93 * stat of the lower directory inode.
95 * Returns zero on success; non-zero on error condition
98 ecryptfs_do_create(struct inode *directory_inode,
99 struct dentry *ecryptfs_dentry, int mode,
100 struct nameidata *nd)
103 struct dentry *lower_dentry;
104 struct dentry *lower_dir_dentry;
106 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
107 lower_dir_dentry = lock_parent(lower_dentry);
108 if (IS_ERR(lower_dir_dentry)) {
109 ecryptfs_printk(KERN_ERR, "Error locking directory of "
111 rc = PTR_ERR(lower_dir_dentry);
114 rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
115 ecryptfs_dentry, mode, nd);
117 printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
118 "rc = [%d]\n", __func__, rc);
121 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
122 directory_inode->i_sb, 0);
124 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
127 fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
128 fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
130 unlock_dir(lower_dir_dentry);
137 * @ecryptfs_dentry: the eCryptfs dentry
139 * This is the code which will grow the file to its correct size.
141 static int grow_file(struct dentry *ecryptfs_dentry)
143 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
144 struct file fake_file;
145 struct ecryptfs_file_info tmp_file_info;
146 char zero_virt[] = { 0x00 };
149 memset(&fake_file, 0, sizeof(fake_file));
150 fake_file.f_path.dentry = ecryptfs_dentry;
151 memset(&tmp_file_info, 0, sizeof(tmp_file_info));
152 ecryptfs_set_file_private(&fake_file, &tmp_file_info);
153 ecryptfs_set_file_lower(
155 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file);
156 rc = ecryptfs_write(&fake_file, zero_virt, 0, 1);
157 i_size_write(ecryptfs_inode, 0);
158 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
159 ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat.flags |=
165 * ecryptfs_initialize_file
167 * Cause the file to be changed from a basic empty file to an ecryptfs
168 * file with a header and first data page.
170 * Returns zero on success
172 static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
174 struct ecryptfs_crypt_stat *crypt_stat =
175 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
178 if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
179 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
180 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
183 crypt_stat->flags |= ECRYPTFS_NEW_FILE;
184 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
185 rc = ecryptfs_new_file_context(ecryptfs_dentry);
187 ecryptfs_printk(KERN_ERR, "Error creating new file "
188 "context; rc = [%d]\n", rc);
191 rc = ecryptfs_write_metadata(ecryptfs_dentry);
193 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
196 rc = grow_file(ecryptfs_dentry);
198 printk(KERN_ERR "Error growing file; rc = [%d]\n", rc);
205 * @dir: The inode of the directory in which to create the file.
206 * @dentry: The eCryptfs dentry
207 * @mode: The mode of the new file.
210 * Creates a new file.
212 * Returns zero on success; non-zero on error condition
215 ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
216 int mode, struct nameidata *nd)
220 /* ecryptfs_do_create() calls ecryptfs_interpose(), which opens
221 * the crypt_stat->lower_file (persistent file) */
222 rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
224 ecryptfs_printk(KERN_WARNING, "Failed to create file in"
225 "lower filesystem\n");
228 /* At this point, a file exists on "disk"; we need to make sure
229 * that this on disk file is prepared to be an ecryptfs file */
230 rc = ecryptfs_initialize_file(ecryptfs_dentry);
238 * @dentry: The dentry
239 * @nd: nameidata, may be NULL
241 * Find a file on disk. If the file does not exist, then we'll add it to the
242 * dentry cache and continue on to read it from the disk.
244 static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
245 struct nameidata *nd)
248 struct dentry *lower_dir_dentry;
249 struct dentry *lower_dentry;
250 struct vfsmount *lower_mnt;
253 struct ecryptfs_crypt_stat *crypt_stat = NULL;
254 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
255 char *page_virt = NULL;
256 struct inode *lower_inode;
259 lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
260 dentry->d_op = &ecryptfs_dops;
261 if ((dentry->d_name.len == 1 && !strcmp(dentry->d_name.name, "."))
262 || (dentry->d_name.len == 2
263 && !strcmp(dentry->d_name.name, ".."))) {
267 encoded_namelen = ecryptfs_encode_filename(crypt_stat,
271 if (encoded_namelen < 0) {
272 rc = encoded_namelen;
276 ecryptfs_printk(KERN_DEBUG, "encoded_name = [%s]; encoded_namelen "
277 "= [%d]\n", encoded_name, encoded_namelen);
278 lower_dentry = lookup_one_len(encoded_name, lower_dir_dentry,
279 encoded_namelen - 1);
281 if (IS_ERR(lower_dentry)) {
282 ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
283 rc = PTR_ERR(lower_dentry);
287 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
288 ecryptfs_printk(KERN_DEBUG, "lower_dentry = [%p]; lower_dentry->"
289 "d_name.name = [%s]\n", lower_dentry,
290 lower_dentry->d_name.name);
291 lower_inode = lower_dentry->d_inode;
292 fsstack_copy_attr_atime(dir, lower_dir_dentry->d_inode);
293 BUG_ON(!atomic_read(&lower_dentry->d_count));
294 ecryptfs_set_dentry_private(dentry,
295 kmem_cache_alloc(ecryptfs_dentry_info_cache,
297 if (!ecryptfs_dentry_to_private(dentry)) {
299 ecryptfs_printk(KERN_ERR, "Out of memory whilst attempting "
300 "to allocate ecryptfs_dentry_info struct\n");
303 ecryptfs_set_dentry_lower(dentry, lower_dentry);
304 ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
305 if (!lower_dentry->d_inode) {
306 /* We want to add because we couldn't find in lower */
310 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 1);
312 ecryptfs_printk(KERN_ERR, "Error interposing\n");
315 if (S_ISDIR(lower_inode->i_mode)) {
316 ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n");
319 if (S_ISLNK(lower_inode->i_mode)) {
320 ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n");
323 if (special_file(lower_inode->i_mode)) {
324 ecryptfs_printk(KERN_DEBUG, "Is a special file; returning\n");
328 ecryptfs_printk(KERN_DEBUG, "We have a NULL nd, just leave"
329 "as we *think* we are about to unlink\n");
332 /* Released in this function */
333 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2,
337 ecryptfs_printk(KERN_ERR,
338 "Cannot ecryptfs_kmalloc a page\n");
341 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
342 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
343 ecryptfs_set_default_sizes(crypt_stat);
344 rc = ecryptfs_read_and_validate_header_region(page_virt,
347 rc = ecryptfs_read_and_validate_xattr_region(page_virt, dentry);
349 printk(KERN_DEBUG "Valid metadata not found in header "
350 "region or xattr region; treating file as "
353 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
356 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
358 mount_crypt_stat = &ecryptfs_superblock_to_private(
359 dentry->d_sb)->mount_crypt_stat;
360 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
361 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
362 file_size = (crypt_stat->num_header_bytes_at_front
363 + i_size_read(lower_dentry->d_inode));
365 file_size = i_size_read(lower_dentry->d_inode);
367 memcpy(&file_size, page_virt, sizeof(file_size));
368 file_size = be64_to_cpu(file_size);
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