34 #include <linux/slab.h>
35 #include <linux/xattr.h>
36 #include <asm/unaligned.h>
48 static void unlock_dir(
struct dentry *dir)
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);
91 static struct inode *__ecryptfs_get_inode(
struct inode *lower_inode,
96 if (lower_inode->
i_sb != ecryptfs_superblock_to_lower(sb))
97 return ERR_PTR(-
EXDEV);
98 if (!
igrab(lower_inode))
101 ecryptfs_inode_test, ecryptfs_inode_set,
116 struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
134 static int ecryptfs_interpose(
struct dentry *lower_dentry,
140 return PTR_ERR(inode);
146 static int ecryptfs_do_unlink(
struct inode *dir,
struct dentry *dentry,
149 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
150 struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
151 struct dentry *lower_dir_dentry;
155 lower_dir_dentry = lock_parent(lower_dentry);
156 rc =
vfs_unlink(lower_dir_inode, lower_dentry);
161 fsstack_copy_attr_times(dir, lower_dir_inode);
162 set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
166 unlock_dir(lower_dir_dentry);
184 static struct inode *
185 ecryptfs_do_create(
struct inode *directory_inode,
189 struct dentry *lower_dentry;
190 struct dentry *lower_dir_dentry;
193 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
194 lower_dir_dentry = lock_parent(lower_dentry);
195 if (IS_ERR(lower_dir_dentry)) {
198 inode = ERR_CAST(lower_dir_dentry);
204 "rc = [%d]\n", __func__, rc);
208 inode = __ecryptfs_get_inode(lower_dentry->
d_inode,
209 directory_inode->
i_sb);
214 fsstack_copy_attr_times(directory_inode, lower_dir_dentry->
d_inode);
217 unlock_dir(lower_dir_dentry);
231 struct inode *ecryptfs_inode)
234 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
246 "context; rc = [%d]\n", rc);
252 "the lower file for the dentry with name "
253 "[%s]; rc = [%d]\n", __func__,
254 ecryptfs_dentry->
d_name.name, rc);
276 ecryptfs_create(
struct inode *directory_inode,
struct dentry *ecryptfs_dentry,
279 struct inode *ecryptfs_inode;
282 ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
284 if (
unlikely(IS_ERR(ecryptfs_inode))) {
286 "lower filesystem\n");
287 rc = PTR_ERR(ecryptfs_inode);
294 ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
298 iput(ecryptfs_inode);
307 static int ecryptfs_i_size_read(
struct dentry *dentry,
struct inode *inode)
315 "the lower file for the dentry with name "
316 "[%s]; rc = [%d]\n", __func__,
321 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
341 static int ecryptfs_lookup_interpose(
struct dentry *dentry,
342 struct dentry *lower_dentry,
343 struct inode *dir_inode)
345 struct inode *
inode, *lower_inode = lower_dentry->
d_inode;
353 "to allocate ecryptfs_dentry_info struct\n",
359 lower_mnt =
mntget(ecryptfs_dentry_to_lower_mnt(dentry->
d_parent));
360 fsstack_copy_attr_atime(dir_inode, lower_dentry->
d_parent->d_inode);
363 ecryptfs_set_dentry_private(dentry, dentry_info);
364 ecryptfs_set_dentry_lower(dentry, lower_dentry);
365 ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
372 inode = __ecryptfs_get_inode(lower_inode, dir_inode->
i_sb);
375 __func__, PTR_ERR(inode));
376 return PTR_ERR(inode);
379 rc = ecryptfs_i_size_read(dentry, inode);
388 d_add(dentry, inode);
402 static struct dentry *ecryptfs_lookup(
struct inode *ecryptfs_dir_inode,
403 struct dentry *ecryptfs_dentry,
406 char *encrypted_and_encoded_name =
NULL;
407 size_t encrypted_and_encoded_name_size;
409 struct dentry *lower_dir_dentry, *lower_dentry;
412 lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->
d_parent);
416 ecryptfs_dentry->
d_name.len);
418 if (IS_ERR(lower_dentry)) {
419 rc = PTR_ERR(lower_dentry);
421 "[%d] on lower_dentry = [%s]\n", __func__, rc,
422 ecryptfs_dentry->
d_name.name);
427 mount_crypt_stat = &ecryptfs_superblock_to_private(
428 ecryptfs_dentry->
d_sb)->mount_crypt_stat;
429 if (!(mount_crypt_stat
434 &encrypted_and_encoded_name, &encrypted_and_encoded_name_size,
435 NULL, mount_crypt_stat, ecryptfs_dentry->
d_name.name,
436 ecryptfs_dentry->
d_name.len);
439 "filename; rc = [%d]\n", __func__, rc);
445 encrypted_and_encoded_name_size);
447 if (IS_ERR(lower_dentry)) {
448 rc = PTR_ERR(lower_dentry);
450 "[%d] on lower_dentry = [%s]\n", __func__, rc,
451 encrypted_and_encoded_name);
455 rc = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry,
458 kfree(encrypted_and_encoded_name);
462 static int ecryptfs_link(
struct dentry *old_dentry,
struct inode *dir,
463 struct dentry *new_dentry)
465 struct dentry *lower_old_dentry;
466 struct dentry *lower_new_dentry;
467 struct dentry *lower_dir_dentry;
471 file_size_save = i_size_read(old_dentry->
d_inode);
472 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
473 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
474 dget(lower_old_dentry);
475 dget(lower_new_dentry);
476 lower_dir_dentry = lock_parent(lower_new_dentry);
479 if (rc || !lower_new_dentry->
d_inode)
481 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->
i_sb);
484 fsstack_copy_attr_times(dir, lower_dir_dentry->
d_inode);
487 ecryptfs_inode_to_lower(old_dentry->
d_inode)->i_nlink);
488 i_size_write(new_dentry->
d_inode, file_size_save);
490 unlock_dir(lower_dir_dentry);
491 dput(lower_new_dentry);
492 dput(lower_old_dentry);
496 static int ecryptfs_unlink(
struct inode *dir,
struct dentry *dentry)
498 return ecryptfs_do_unlink(dir, dentry, dentry->
d_inode);
501 static int ecryptfs_symlink(
struct inode *dir,
struct dentry *dentry,
505 struct dentry *lower_dentry;
506 struct dentry *lower_dir_dentry;
507 char *encoded_symname;
508 size_t encoded_symlen;
511 lower_dentry = ecryptfs_dentry_to_lower(dentry);
513 lower_dir_dentry = lock_parent(lower_dentry);
514 mount_crypt_stat = &ecryptfs_superblock_to_private(
515 dir->
i_sb)->mount_crypt_stat;
519 mount_crypt_stat, symname,
525 kfree(encoded_symname);
526 if (rc || !lower_dentry->
d_inode)
528 rc = ecryptfs_interpose(lower_dentry, dentry, dir->
i_sb);
531 fsstack_copy_attr_times(dir, lower_dir_dentry->
d_inode);
534 unlock_dir(lower_dir_dentry);
541 static int ecryptfs_mkdir(
struct inode *dir,
struct dentry *dentry,
umode_t mode)
544 struct dentry *lower_dentry;
545 struct dentry *lower_dir_dentry;
547 lower_dentry = ecryptfs_dentry_to_lower(dentry);
548 lower_dir_dentry = lock_parent(lower_dentry);
550 if (rc || !lower_dentry->
d_inode)
552 rc = ecryptfs_interpose(lower_dentry, dentry, dir->
i_sb);
555 fsstack_copy_attr_times(dir, lower_dir_dentry->
d_inode);
559 unlock_dir(lower_dir_dentry);
565 static int ecryptfs_rmdir(
struct inode *dir,
struct dentry *dentry)
567 struct dentry *lower_dentry;
568 struct dentry *lower_dir_dentry;
571 lower_dentry = ecryptfs_dentry_to_lower(dentry);
573 lower_dir_dentry = lock_parent(lower_dentry);
579 fsstack_copy_attr_times(dir, lower_dir_dentry->
d_inode);
581 unlock_dir(lower_dir_dentry);
589 ecryptfs_mknod(
struct inode *dir,
struct dentry *dentry,
umode_t mode,
dev_t dev)
592 struct dentry *lower_dentry;
593 struct dentry *lower_dir_dentry;
595 lower_dentry = ecryptfs_dentry_to_lower(dentry);
596 lower_dir_dentry = lock_parent(lower_dentry);
598 if (rc || !lower_dentry->
d_inode)
600 rc = ecryptfs_interpose(lower_dentry, dentry, dir->
i_sb);
603 fsstack_copy_attr_times(dir, lower_dir_dentry->
d_inode);
606 unlock_dir(lower_dir_dentry);
613 ecryptfs_rename(
struct inode *old_dir,
struct dentry *old_dentry,
614 struct inode *new_dir,
struct dentry *new_dentry)
617 struct dentry *lower_old_dentry;
618 struct dentry *lower_new_dentry;
619 struct dentry *lower_old_dir_dentry;
620 struct dentry *lower_new_dir_dentry;
622 struct inode *target_inode;
624 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
625 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
626 dget(lower_old_dentry);
627 dget(lower_new_dentry);
628 lower_old_dir_dentry =
dget_parent(lower_old_dentry);
629 lower_new_dir_dentry =
dget_parent(lower_new_dentry);
630 target_inode = new_dentry->
d_inode;
631 trap =
lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
633 if (trap == lower_old_dentry) {
638 if (trap == lower_new_dentry) {
643 lower_new_dir_dentry->
d_inode, lower_new_dentry);
648 ecryptfs_inode_to_lower(target_inode));
650 if (new_dir != old_dir)
654 dput(lower_new_dir_dentry);
655 dput(lower_old_dir_dentry);
656 dput(lower_new_dentry);
657 dput(lower_old_dentry);
661 static int ecryptfs_readlink_lower(
struct dentry *dentry,
char **
buf,
664 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
676 rc = lower_dentry->
d_inode->i_op->readlink(lower_dentry,
677 (
char __user *)lower_buf,
689 static void *ecryptfs_follow_link(
struct dentry *dentry,
struct nameidata *nd)
695 rc = ecryptfs_readlink_lower(dentry, &buf, &len);
698 fsstack_copy_attr_atime(dentry->
d_inode,
699 ecryptfs_dentry_to_lower(dentry)->
d_inode);
702 nd_set_link(nd, buf);
707 ecryptfs_put_link(
struct dentry *dentry,
struct nameidata *nd,
void *
ptr)
709 char *buf = nd_get_link(nd);
733 lower_size = ecryptfs_lower_header_size(crypt_stat);
734 if (upper_size != 0) {
740 lower_size += (num_extents * crypt_stat->
extent_size);
761 static int truncate_upper(
struct dentry *dentry,
struct iattr *ia,
762 struct iattr *lower_ia)
765 struct inode *inode = dentry->
d_inode;
767 loff_t i_size = i_size_read(inode);
768 loff_t lower_size_before_truncate;
769 loff_t lower_size_after_truncate;
778 crypt_stat = &ecryptfs_inode_to_private(dentry->
d_inode)->crypt_stat;
781 char zero[] = { 0x00 };
817 "the remainder of the end page on "
818 "reducing truncate; rc = [%d]\n", rc);
826 "ecryptfs_write_inode_size_to_metadata; "
832 lower_size_before_truncate =
833 upper_size_to_lower_size(crypt_stat, i_size);
834 lower_size_after_truncate =
835 upper_size_to_lower_size(crypt_stat, ia->
ia_size);
836 if (lower_size_after_truncate < lower_size_before_truncate) {
837 lower_ia->
ia_size = lower_size_after_truncate;
847 static int ecryptfs_inode_newsize_ok(
struct inode *inode, loff_t
offset)
850 loff_t lower_oldsize, lower_newsize;
852 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
853 lower_oldsize = upper_size_to_lower_size(crypt_stat,
855 lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
856 if (lower_newsize > lower_oldsize) {
885 rc = ecryptfs_inode_newsize_ok(dentry->
d_inode, new_length);
889 rc = truncate_upper(dentry, &ia, &lower_ia);
891 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
901 ecryptfs_permission(
struct inode *inode,
int mask)
918 static int ecryptfs_setattr(
struct dentry *dentry,
struct iattr *ia)
921 struct dentry *lower_dentry;
922 struct iattr lower_ia;
924 struct inode *lower_inode;
927 crypt_stat = &ecryptfs_inode_to_private(dentry->
d_inode)->crypt_stat;
931 lower_inode = ecryptfs_inode_to_lower(inode);
932 lower_dentry = ecryptfs_dentry_to_lower(dentry);
941 mount_crypt_stat = &ecryptfs_superblock_to_private(
942 dentry->
d_sb)->mount_crypt_stat;
951 if (!(mount_crypt_stat->
flags
955 "is not in a valid eCryptfs format, "
956 "or the key could not be retrieved. "
957 "Plaintext passthrough mode is not "
958 "enabled; returning -EIO\n");
973 rc = ecryptfs_inode_newsize_ok(inode, ia->
ia_size);
978 memcpy(&lower_ia, ia,
sizeof(lower_ia));
982 rc = truncate_upper(dentry, ia, &lower_ia);
1008 mount_crypt_stat = &ecryptfs_superblock_to_private(
1009 dentry->
d_sb)->mount_crypt_stat;
1015 rc = ecryptfs_readlink_lower(dentry, &target, &targetsiz);
1018 stat->
size = targetsiz;
1027 struct kstat lower_stat;
1030 rc =
vfs_getattr(ecryptfs_dentry_to_lower_mnt(dentry),
1031 ecryptfs_dentry_to_lower(dentry), &lower_stat);
1034 ecryptfs_inode_to_lower(dentry->
d_inode));
1043 size_t size,
int flags)
1046 struct dentry *lower_dentry;
1048 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1049 if (!lower_dentry->
d_inode->i_op->setxattr) {
1054 rc =
vfs_setxattr(lower_dentry, name, value, size, flags);
1067 if (!lower_dentry->
d_inode->i_op->getxattr) {
1072 rc = lower_dentry->
d_inode->i_op->getxattr(lower_dentry, name, value,
1080 ecryptfs_getxattr(
struct dentry *dentry,
const char *
name,
void *
value,
1088 ecryptfs_listxattr(
struct dentry *dentry,
char *
list,
size_t size)
1091 struct dentry *lower_dentry;
1093 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1094 if (!lower_dentry->
d_inode->i_op->listxattr) {
1099 rc = lower_dentry->
d_inode->i_op->listxattr(lower_dentry, list, size);
1105 static int ecryptfs_removexattr(
struct dentry *dentry,
const char *name)
1108 struct dentry *lower_dentry;
1110 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1111 if (!lower_dentry->
d_inode->i_op->removexattr) {
1116 rc = lower_dentry->
d_inode->i_op->removexattr(lower_dentry, name);
1124 .follow_link = ecryptfs_follow_link,
1125 .put_link = ecryptfs_put_link,
1126 .permission = ecryptfs_permission,
1127 .setattr = ecryptfs_setattr,
1130 .getxattr = ecryptfs_getxattr,
1131 .listxattr = ecryptfs_listxattr,
1132 .removexattr = ecryptfs_removexattr
1136 .create = ecryptfs_create,
1137 .lookup = ecryptfs_lookup,
1138 .link = ecryptfs_link,
1139 .unlink = ecryptfs_unlink,
1140 .symlink = ecryptfs_symlink,
1141 .mkdir = ecryptfs_mkdir,
1142 .rmdir = ecryptfs_rmdir,
1143 .mknod = ecryptfs_mknod,
1144 .rename = ecryptfs_rename,
1145 .permission = ecryptfs_permission,
1146 .setattr = ecryptfs_setattr,
1148 .getxattr = ecryptfs_getxattr,
1149 .listxattr = ecryptfs_listxattr,
1150 .removexattr = ecryptfs_removexattr
1154 .permission = ecryptfs_permission,
1155 .setattr = ecryptfs_setattr,
1158 .getxattr = ecryptfs_getxattr,
1159 .listxattr = ecryptfs_listxattr,
1160 .removexattr = ecryptfs_removexattr