aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2011-05-29 17:13:25 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2011-05-29 17:13:25 -0400
commitbd1bfe40ac6bdf9593da29b822bc301b77a97d6a (patch)
tree332328b76dc4a3d51419c1d1cb23f50eb6c034c8
parentcd1acdf1723d71b28175f95b04305f1cc74ce363 (diff)
parent3063287053bca5207e121c567b95b2b6f0bdc2c8 (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ecryptfs/ecryptfs-2.6
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ecryptfs/ecryptfs-2.6: eCryptfs: Remove ecryptfs_header_cache_2 eCryptfs: Cleanup and optimize ecryptfs_lookup_interpose() eCryptfs: Return useful code from contains_ecryptfs_marker eCryptfs: Fix new inode race condition eCryptfs: Cleanup inode initialization code eCryptfs: Consolidate inode functions into inode.c
-rw-r--r--fs/ecryptfs/crypto.c74
-rw-r--r--fs/ecryptfs/ecryptfs_kernel.h26
-rw-r--r--fs/ecryptfs/file.c2
-rw-r--r--fs/ecryptfs/inode.c281
-rw-r--r--fs/ecryptfs/main.c84
-rw-r--r--fs/ecryptfs/super.c16
6 files changed, 220 insertions, 263 deletions
diff --git a/fs/ecryptfs/crypto.c b/fs/ecryptfs/crypto.c
index b8d5c8091024..58609bde3b9f 100644
--- a/fs/ecryptfs/crypto.c
+++ b/fs/ecryptfs/crypto.c
@@ -1024,25 +1024,25 @@ out:
1024} 1024}
1025 1025
1026/** 1026/**
1027 * contains_ecryptfs_marker - check for the ecryptfs marker 1027 * ecryptfs_validate_marker - check for the ecryptfs marker
1028 * @data: The data block in which to check 1028 * @data: The data block in which to check
1029 * 1029 *
1030 * Returns one if marker found; zero if not found 1030 * Returns zero if marker found; -EINVAL if not found
1031 */ 1031 */
1032static int contains_ecryptfs_marker(char *data) 1032static int ecryptfs_validate_marker(char *data)
1033{ 1033{
1034 u32 m_1, m_2; 1034 u32 m_1, m_2;
1035 1035
1036 m_1 = get_unaligned_be32(data); 1036 m_1 = get_unaligned_be32(data);
1037 m_2 = get_unaligned_be32(data + 4); 1037 m_2 = get_unaligned_be32(data + 4);
1038 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2) 1038 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
1039 return 1; 1039 return 0;
1040 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; " 1040 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1041 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2, 1041 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1042 MAGIC_ECRYPTFS_MARKER); 1042 MAGIC_ECRYPTFS_MARKER);
1043 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = " 1043 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1044 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER)); 1044 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
1045 return 0; 1045 return -EINVAL;
1046} 1046}
1047 1047
1048struct ecryptfs_flag_map_elem { 1048struct ecryptfs_flag_map_elem {
@@ -1201,27 +1201,19 @@ int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
1201 return rc; 1201 return rc;
1202} 1202}
1203 1203
1204int ecryptfs_read_and_validate_header_region(char *data, 1204int ecryptfs_read_and_validate_header_region(struct inode *inode)
1205 struct inode *ecryptfs_inode)
1206{ 1205{
1207 struct ecryptfs_crypt_stat *crypt_stat = 1206 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1208 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); 1207 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1209 int rc; 1208 int rc;
1210 1209
1211 if (crypt_stat->extent_size == 0) 1210 rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
1212 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE; 1211 inode);
1213 rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size, 1212 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1214 ecryptfs_inode); 1213 return rc >= 0 ? -EINVAL : rc;
1215 if (rc < 0) { 1214 rc = ecryptfs_validate_marker(marker);
1216 printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n", 1215 if (!rc)
1217 __func__, rc); 1216 ecryptfs_i_size_init(file_size, inode);
1218 goto out;
1219 }
1220 if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
1221 rc = -EINVAL;
1222 } else
1223 rc = 0;
1224out:
1225 return rc; 1217 return rc;
1226} 1218}
1227 1219
@@ -1242,8 +1234,7 @@ ecryptfs_write_header_metadata(char *virt,
1242 (*written) = 6; 1234 (*written) = 6;
1243} 1235}
1244 1236
1245struct kmem_cache *ecryptfs_header_cache_1; 1237struct kmem_cache *ecryptfs_header_cache;
1246struct kmem_cache *ecryptfs_header_cache_2;
1247 1238
1248/** 1239/**
1249 * ecryptfs_write_headers_virt 1240 * ecryptfs_write_headers_virt
@@ -1496,11 +1487,9 @@ static int ecryptfs_read_headers_virt(char *page_virt,
1496 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private( 1487 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1497 ecryptfs_dentry->d_sb)->mount_crypt_stat; 1488 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1498 offset = ECRYPTFS_FILE_SIZE_BYTES; 1489 offset = ECRYPTFS_FILE_SIZE_BYTES;
1499 rc = contains_ecryptfs_marker(page_virt + offset); 1490 rc = ecryptfs_validate_marker(page_virt + offset);
1500 if (rc == 0) { 1491 if (rc)
1501 rc = -EINVAL;
1502 goto out; 1492 goto out;
1503 }
1504 if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED)) 1493 if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
1505 ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode); 1494 ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode);
1506 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; 1495 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
@@ -1567,20 +1556,21 @@ out:
1567 return rc; 1556 return rc;
1568} 1557}
1569 1558
1570int ecryptfs_read_and_validate_xattr_region(char *page_virt, 1559int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
1571 struct dentry *ecryptfs_dentry) 1560 struct inode *inode)
1572{ 1561{
1562 u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1563 u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1573 int rc; 1564 int rc;
1574 1565
1575 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode); 1566 rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1576 if (rc) 1567 ECRYPTFS_XATTR_NAME, file_size,
1577 goto out; 1568 ECRYPTFS_SIZE_AND_MARKER_BYTES);
1578 if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) { 1569 if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1579 printk(KERN_WARNING "Valid data found in [%s] xattr, but " 1570 return rc >= 0 ? -EINVAL : rc;
1580 "the marker is invalid\n", ECRYPTFS_XATTR_NAME); 1571 rc = ecryptfs_validate_marker(marker);
1581 rc = -EINVAL; 1572 if (!rc)
1582 } 1573 ecryptfs_i_size_init(file_size, inode);
1583out:
1584 return rc; 1574 return rc;
1585} 1575}
1586 1576
@@ -1610,7 +1600,7 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
1610 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, 1600 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1611 mount_crypt_stat); 1601 mount_crypt_stat);
1612 /* Read the first page from the underlying file */ 1602 /* Read the first page from the underlying file */
1613 page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER); 1603 page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
1614 if (!page_virt) { 1604 if (!page_virt) {
1615 rc = -ENOMEM; 1605 rc = -ENOMEM;
1616 printk(KERN_ERR "%s: Unable to allocate page_virt\n", 1606 printk(KERN_ERR "%s: Unable to allocate page_virt\n",
@@ -1655,7 +1645,7 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
1655out: 1645out:
1656 if (page_virt) { 1646 if (page_virt) {
1657 memset(page_virt, 0, PAGE_CACHE_SIZE); 1647 memset(page_virt, 0, PAGE_CACHE_SIZE);
1658 kmem_cache_free(ecryptfs_header_cache_1, page_virt); 1648 kmem_cache_free(ecryptfs_header_cache, page_virt);
1659 } 1649 }
1660 return rc; 1650 return rc;
1661} 1651}
diff --git a/fs/ecryptfs/ecryptfs_kernel.h b/fs/ecryptfs/ecryptfs_kernel.h
index e70282775e2c..43c7c43b06f5 100644
--- a/fs/ecryptfs/ecryptfs_kernel.h
+++ b/fs/ecryptfs/ecryptfs_kernel.h
@@ -200,6 +200,8 @@ ecryptfs_get_key_payload_data(struct key *key)
200#define MAGIC_ECRYPTFS_MARKER 0x3c81b7f5 200#define MAGIC_ECRYPTFS_MARKER 0x3c81b7f5
201#define MAGIC_ECRYPTFS_MARKER_SIZE_BYTES 8 /* 4*2 */ 201#define MAGIC_ECRYPTFS_MARKER_SIZE_BYTES 8 /* 4*2 */
202#define ECRYPTFS_FILE_SIZE_BYTES (sizeof(u64)) 202#define ECRYPTFS_FILE_SIZE_BYTES (sizeof(u64))
203#define ECRYPTFS_SIZE_AND_MARKER_BYTES (ECRYPTFS_FILE_SIZE_BYTES \
204 + MAGIC_ECRYPTFS_MARKER_SIZE_BYTES)
203#define ECRYPTFS_DEFAULT_CIPHER "aes" 205#define ECRYPTFS_DEFAULT_CIPHER "aes"
204#define ECRYPTFS_DEFAULT_KEY_BYTES 16 206#define ECRYPTFS_DEFAULT_KEY_BYTES 16
205#define ECRYPTFS_DEFAULT_HASH "md5" 207#define ECRYPTFS_DEFAULT_HASH "md5"
@@ -603,8 +605,7 @@ extern struct kmem_cache *ecryptfs_file_info_cache;
603extern struct kmem_cache *ecryptfs_dentry_info_cache; 605extern struct kmem_cache *ecryptfs_dentry_info_cache;
604extern struct kmem_cache *ecryptfs_inode_info_cache; 606extern struct kmem_cache *ecryptfs_inode_info_cache;
605extern struct kmem_cache *ecryptfs_sb_info_cache; 607extern struct kmem_cache *ecryptfs_sb_info_cache;
606extern struct kmem_cache *ecryptfs_header_cache_1; 608extern struct kmem_cache *ecryptfs_header_cache;
607extern struct kmem_cache *ecryptfs_header_cache_2;
608extern struct kmem_cache *ecryptfs_xattr_cache; 609extern struct kmem_cache *ecryptfs_xattr_cache;
609extern struct kmem_cache *ecryptfs_key_record_cache; 610extern struct kmem_cache *ecryptfs_key_record_cache;
610extern struct kmem_cache *ecryptfs_key_sig_cache; 611extern struct kmem_cache *ecryptfs_key_sig_cache;
@@ -625,14 +626,9 @@ struct ecryptfs_open_req {
625 struct list_head kthread_ctl_list; 626 struct list_head kthread_ctl_list;
626}; 627};
627 628
628#define ECRYPTFS_INTERPOSE_FLAG_D_ADD 0x00000001 629struct inode *ecryptfs_get_inode(struct inode *lower_inode,
629int ecryptfs_interpose(struct dentry *hidden_dentry, 630 struct super_block *sb);
630 struct dentry *this_dentry, struct super_block *sb,
631 u32 flags);
632void ecryptfs_i_size_init(const char *page_virt, struct inode *inode); 631void ecryptfs_i_size_init(const char *page_virt, struct inode *inode);
633int ecryptfs_lookup_and_interpose_lower(struct dentry *ecryptfs_dentry,
634 struct dentry *lower_dentry,
635 struct inode *ecryptfs_dir_inode);
636int ecryptfs_decode_and_decrypt_filename(char **decrypted_name, 632int ecryptfs_decode_and_decrypt_filename(char **decrypted_name,
637 size_t *decrypted_name_size, 633 size_t *decrypted_name_size,
638 struct dentry *ecryptfs_dentry, 634 struct dentry *ecryptfs_dentry,
@@ -664,10 +660,9 @@ int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry);
664void ecryptfs_write_crypt_stat_flags(char *page_virt, 660void ecryptfs_write_crypt_stat_flags(char *page_virt,
665 struct ecryptfs_crypt_stat *crypt_stat, 661 struct ecryptfs_crypt_stat *crypt_stat,
666 size_t *written); 662 size_t *written);
667int ecryptfs_read_and_validate_header_region(char *data, 663int ecryptfs_read_and_validate_header_region(struct inode *inode);
668 struct inode *ecryptfs_inode); 664int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
669int ecryptfs_read_and_validate_xattr_region(char *page_virt, 665 struct inode *inode);
670 struct dentry *ecryptfs_dentry);
671u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes); 666u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes);
672int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code); 667int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code);
673void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat); 668void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat);
@@ -679,9 +674,6 @@ int
679ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, 674ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
680 unsigned char *src, struct dentry *ecryptfs_dentry); 675 unsigned char *src, struct dentry *ecryptfs_dentry);
681int ecryptfs_truncate(struct dentry *dentry, loff_t new_length); 676int ecryptfs_truncate(struct dentry *dentry, loff_t new_length);
682int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode);
683int ecryptfs_inode_set(struct inode *inode, void *lower_inode);
684void ecryptfs_init_inode(struct inode *inode, struct inode *lower_inode);
685ssize_t 677ssize_t
686ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name, 678ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
687 void *value, size_t size); 679 void *value, size_t size);
@@ -761,7 +753,7 @@ int ecryptfs_privileged_open(struct file **lower_file,
761 struct dentry *lower_dentry, 753 struct dentry *lower_dentry,
762 struct vfsmount *lower_mnt, 754 struct vfsmount *lower_mnt,
763 const struct cred *cred); 755 const struct cred *cred);
764int ecryptfs_get_lower_file(struct dentry *ecryptfs_dentry); 756int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode);
765void ecryptfs_put_lower_file(struct inode *inode); 757void ecryptfs_put_lower_file(struct inode *inode);
766int 758int
767ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, 759ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
diff --git a/fs/ecryptfs/file.c b/fs/ecryptfs/file.c
index 566e5472f78c..4ec9eb00a241 100644
--- a/fs/ecryptfs/file.c
+++ b/fs/ecryptfs/file.c
@@ -191,7 +191,7 @@ static int ecryptfs_open(struct inode *inode, struct file *file)
191 | ECRYPTFS_ENCRYPTED); 191 | ECRYPTFS_ENCRYPTED);
192 } 192 }
193 mutex_unlock(&crypt_stat->cs_mutex); 193 mutex_unlock(&crypt_stat->cs_mutex);
194 rc = ecryptfs_get_lower_file(ecryptfs_dentry); 194 rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
195 if (rc) { 195 if (rc) {
196 printk(KERN_ERR "%s: Error attempting to initialize " 196 printk(KERN_ERR "%s: Error attempting to initialize "
197 "the lower file for the dentry with name " 197 "the lower file for the dentry with name "
diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c
index 94ab3c06317a..7349ade17de6 100644
--- a/fs/ecryptfs/inode.c
+++ b/fs/ecryptfs/inode.c
@@ -51,6 +51,97 @@ static void unlock_dir(struct dentry *dir)
51 dput(dir); 51 dput(dir);
52} 52}
53 53
54static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
55{
56 if (ecryptfs_inode_to_lower(inode) == (struct inode *)lower_inode)
57 return 1;
58 return 0;
59}
60
61static int ecryptfs_inode_set(struct inode *inode, void *opaque)
62{
63 struct inode *lower_inode = opaque;
64
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;
70 inode->i_version++;
71 inode->i_mapping->a_ops = &ecryptfs_aops;
72
73 if (S_ISLNK(inode->i_mode))
74 inode->i_op = &ecryptfs_symlink_iops;
75 else if (S_ISDIR(inode->i_mode))
76 inode->i_op = &ecryptfs_dir_iops;
77 else
78 inode->i_op = &ecryptfs_main_iops;
79
80 if (S_ISDIR(inode->i_mode))
81 inode->i_fop = &ecryptfs_dir_fops;
82 else if (special_file(inode->i_mode))
83 init_special_inode(inode, inode->i_mode, inode->i_rdev);
84 else
85 inode->i_fop = &ecryptfs_main_fops;
86
87 return 0;
88}
89
90static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
91 struct super_block *sb)
92{
93 struct inode *inode;
94
95 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
96 return ERR_PTR(-EXDEV);
97 if (!igrab(lower_inode))
98 return ERR_PTR(-ESTALE);
99 inode = iget5_locked(sb, (unsigned long)lower_inode,
100 ecryptfs_inode_test, ecryptfs_inode_set,
101 lower_inode);
102 if (!inode) {
103 iput(lower_inode);
104 return ERR_PTR(-EACCES);
105 }
106 if (!(inode->i_state & I_NEW))
107 iput(lower_inode);
108
109 return inode;
110}
111
112struct inode *ecryptfs_get_inode(struct inode *lower_inode,
113 struct super_block *sb)
114{
115 struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
116
117 if (!IS_ERR(inode) && (inode->i_state & I_NEW))
118 unlock_new_inode(inode);
119
120 return inode;
121}
122
123/**
124 * ecryptfs_interpose
125 * @lower_dentry: Existing dentry in the lower filesystem
126 * @dentry: ecryptfs' dentry
127 * @sb: ecryptfs's super_block
128 *
129 * Interposes upper and lower dentries.
130 *
131 * Returns zero on success; non-zero otherwise
132 */
133static int ecryptfs_interpose(struct dentry *lower_dentry,
134 struct dentry *dentry, struct super_block *sb)
135{
136 struct inode *inode = ecryptfs_get_inode(lower_dentry->d_inode, sb);
137
138 if (IS_ERR(inode))
139 return PTR_ERR(inode);
140 d_instantiate(dentry, inode);
141
142 return 0;
143}
144
54/** 145/**
55 * ecryptfs_create_underlying_file 146 * ecryptfs_create_underlying_file
56 * @lower_dir_inode: inode of the parent in the lower fs of the new file 147 * @lower_dir_inode: inode of the parent in the lower fs of the new file
@@ -129,7 +220,7 @@ ecryptfs_do_create(struct inode *directory_inode,
129 goto out_lock; 220 goto out_lock;
130 } 221 }
131 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry, 222 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
132 directory_inode->i_sb, 0); 223 directory_inode->i_sb);
133 if (rc) { 224 if (rc) {
134 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n"); 225 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
135 goto out_lock; 226 goto out_lock;
@@ -168,7 +259,8 @@ static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
168 "context; rc = [%d]\n", rc); 259 "context; rc = [%d]\n", rc);
169 goto out; 260 goto out;
170 } 261 }
171 rc = ecryptfs_get_lower_file(ecryptfs_dentry); 262 rc = ecryptfs_get_lower_file(ecryptfs_dentry,
263 ecryptfs_dentry->d_inode);
172 if (rc) { 264 if (rc) {
173 printk(KERN_ERR "%s: Error attempting to initialize " 265 printk(KERN_ERR "%s: Error attempting to initialize "
174 "the lower file for the dentry with name " 266 "the lower file for the dentry with name "
@@ -215,102 +307,90 @@ out:
215 return rc; 307 return rc;
216} 308}
217 309
310static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
311{
312 struct ecryptfs_crypt_stat *crypt_stat;
313 int rc;
314
315 rc = ecryptfs_get_lower_file(dentry, inode);
316 if (rc) {
317 printk(KERN_ERR "%s: Error attempting to initialize "
318 "the lower file for the dentry with name "
319 "[%s]; rc = [%d]\n", __func__,
320 dentry->d_name.name, rc);
321 return rc;
322 }
323
324 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
325 /* TODO: lock for crypt_stat comparison */
326 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
327 ecryptfs_set_default_sizes(crypt_stat);
328
329 rc = ecryptfs_read_and_validate_header_region(inode);
330 ecryptfs_put_lower_file(inode);
331 if (rc) {
332 rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
333 if (!rc)
334 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
335 }
336
337 /* Must return 0 to allow non-eCryptfs files to be looked up, too */
338 return 0;
339}
340
218/** 341/**
219 * ecryptfs_lookup_and_interpose_lower - Perform a lookup 342 * ecryptfs_lookup_interpose - Dentry interposition for a lookup
220 */ 343 */
221int ecryptfs_lookup_and_interpose_lower(struct dentry *ecryptfs_dentry, 344static int ecryptfs_lookup_interpose(struct dentry *dentry,
222 struct dentry *lower_dentry, 345 struct dentry *lower_dentry,
223 struct inode *ecryptfs_dir_inode) 346 struct inode *dir_inode)
224{ 347{
225 struct dentry *lower_dir_dentry; 348 struct inode *inode, *lower_inode = lower_dentry->d_inode;
349 struct ecryptfs_dentry_info *dentry_info;
226 struct vfsmount *lower_mnt; 350 struct vfsmount *lower_mnt;
227 struct inode *lower_inode; 351 int rc = 0;
228 struct ecryptfs_crypt_stat *crypt_stat; 352
229 char *page_virt = NULL; 353 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
230 int put_lower = 0, rc = 0; 354 fsstack_copy_attr_atime(dir_inode, lower_dentry->d_parent->d_inode);
231
232 lower_dir_dentry = lower_dentry->d_parent;
233 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(
234 ecryptfs_dentry->d_parent));
235 lower_inode = lower_dentry->d_inode;
236 fsstack_copy_attr_atime(ecryptfs_dir_inode, lower_dir_dentry->d_inode);
237 BUG_ON(!lower_dentry->d_count); 355 BUG_ON(!lower_dentry->d_count);
238 ecryptfs_set_dentry_private(ecryptfs_dentry, 356
239 kmem_cache_alloc(ecryptfs_dentry_info_cache, 357 dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
240 GFP_KERNEL)); 358 ecryptfs_set_dentry_private(dentry, dentry_info);
241 if (!ecryptfs_dentry_to_private(ecryptfs_dentry)) { 359 if (!dentry_info) {
242 rc = -ENOMEM;
243 printk(KERN_ERR "%s: Out of memory whilst attempting " 360 printk(KERN_ERR "%s: Out of memory whilst attempting "
244 "to allocate ecryptfs_dentry_info struct\n", 361 "to allocate ecryptfs_dentry_info struct\n",
245 __func__); 362 __func__);
246 goto out_put; 363 dput(lower_dentry);
364 mntput(lower_mnt);
365 d_drop(dentry);
366 return -ENOMEM;
247 } 367 }
248 ecryptfs_set_dentry_lower(ecryptfs_dentry, lower_dentry); 368 ecryptfs_set_dentry_lower(dentry, lower_dentry);
249 ecryptfs_set_dentry_lower_mnt(ecryptfs_dentry, lower_mnt); 369 ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
370
250 if (!lower_dentry->d_inode) { 371 if (!lower_dentry->d_inode) {
251 /* We want to add because we couldn't find in lower */ 372 /* We want to add because we couldn't find in lower */
252 d_add(ecryptfs_dentry, NULL); 373 d_add(dentry, NULL);
253 goto out; 374 return 0;
254 } 375 }
255 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry, 376 inode = __ecryptfs_get_inode(lower_inode, dir_inode->i_sb);
256 ecryptfs_dir_inode->i_sb, 377 if (IS_ERR(inode)) {
257 ECRYPTFS_INTERPOSE_FLAG_D_ADD); 378 printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
258 if (rc) { 379 __func__, PTR_ERR(inode));
259 printk(KERN_ERR "%s: Error interposing; rc = [%d]\n", 380 return PTR_ERR(inode);
260 __func__, rc);
261 goto out;
262 } 381 }
263 if (S_ISDIR(lower_inode->i_mode)) 382 if (S_ISREG(inode->i_mode)) {
264 goto out; 383 rc = ecryptfs_i_size_read(dentry, inode);
265 if (S_ISLNK(lower_inode->i_mode))
266 goto out;
267 if (special_file(lower_inode->i_mode))
268 goto out;
269 /* Released in this function */
270 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2, GFP_USER);
271 if (!page_virt) {
272 printk(KERN_ERR "%s: Cannot kmem_cache_zalloc() a page\n",
273 __func__);
274 rc = -ENOMEM;
275 goto out;
276 }
277 rc = ecryptfs_get_lower_file(ecryptfs_dentry);
278 if (rc) {
279 printk(KERN_ERR "%s: Error attempting to initialize "
280 "the lower file for the dentry with name "
281 "[%s]; rc = [%d]\n", __func__,
282 ecryptfs_dentry->d_name.name, rc);
283 goto out_free_kmem;
284 }
285 put_lower = 1;
286 crypt_stat = &ecryptfs_inode_to_private(
287 ecryptfs_dentry->d_inode)->crypt_stat;
288 /* TODO: lock for crypt_stat comparison */
289 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
290 ecryptfs_set_default_sizes(crypt_stat);
291 rc = ecryptfs_read_and_validate_header_region(page_virt,
292 ecryptfs_dentry->d_inode);
293 if (rc) {
294 memset(page_virt, 0, PAGE_CACHE_SIZE);
295 rc = ecryptfs_read_and_validate_xattr_region(page_virt,
296 ecryptfs_dentry);
297 if (rc) { 384 if (rc) {
298 rc = 0; 385 make_bad_inode(inode);
299 goto out_free_kmem; 386 return rc;
300 } 387 }
301 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
302 } 388 }
303 ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode); 389
304out_free_kmem: 390 if (inode->i_state & I_NEW)
305 kmem_cache_free(ecryptfs_header_cache_2, page_virt); 391 unlock_new_inode(inode);
306 goto out; 392 d_add(dentry, inode);
307out_put: 393
308 dput(lower_dentry);
309 mntput(lower_mnt);
310 d_drop(ecryptfs_dentry);
311out:
312 if (put_lower)
313 ecryptfs_put_lower_file(ecryptfs_dentry->d_inode);
314 return rc; 394 return rc;
315} 395}
316 396
@@ -353,12 +433,12 @@ static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
353 goto out_d_drop; 433 goto out_d_drop;
354 } 434 }
355 if (lower_dentry->d_inode) 435 if (lower_dentry->d_inode)
356 goto lookup_and_interpose; 436 goto interpose;
357 mount_crypt_stat = &ecryptfs_superblock_to_private( 437 mount_crypt_stat = &ecryptfs_superblock_to_private(
358 ecryptfs_dentry->d_sb)->mount_crypt_stat; 438 ecryptfs_dentry->d_sb)->mount_crypt_stat;
359 if (!(mount_crypt_stat 439 if (!(mount_crypt_stat
360 && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) 440 && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)))
361 goto lookup_and_interpose; 441 goto interpose;
362 dput(lower_dentry); 442 dput(lower_dentry);
363 rc = ecryptfs_encrypt_and_encode_filename( 443 rc = ecryptfs_encrypt_and_encode_filename(
364 &encrypted_and_encoded_name, &encrypted_and_encoded_name_size, 444 &encrypted_and_encoded_name, &encrypted_and_encoded_name_size,
@@ -381,9 +461,9 @@ static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
381 encrypted_and_encoded_name); 461 encrypted_and_encoded_name);
382 goto out_d_drop; 462 goto out_d_drop;
383 } 463 }
384lookup_and_interpose: 464interpose:
385 rc = ecryptfs_lookup_and_interpose_lower(ecryptfs_dentry, lower_dentry, 465 rc = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry,
386 ecryptfs_dir_inode); 466 ecryptfs_dir_inode);
387 goto out; 467 goto out;
388out_d_drop: 468out_d_drop:
389 d_drop(ecryptfs_dentry); 469 d_drop(ecryptfs_dentry);
@@ -411,7 +491,7 @@ static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
411 lower_new_dentry); 491 lower_new_dentry);
412 if (rc || !lower_new_dentry->d_inode) 492 if (rc || !lower_new_dentry->d_inode)
413 goto out_lock; 493 goto out_lock;
414 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0); 494 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
415 if (rc) 495 if (rc)
416 goto out_lock; 496 goto out_lock;
417 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 497 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
@@ -478,7 +558,7 @@ static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
478 kfree(encoded_symname); 558 kfree(encoded_symname);
479 if (rc || !lower_dentry->d_inode) 559 if (rc || !lower_dentry->d_inode)
480 goto out_lock; 560 goto out_lock;
481 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0); 561 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
482 if (rc) 562 if (rc)
483 goto out_lock; 563 goto out_lock;
484 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 564 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
@@ -502,7 +582,7 @@ static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
502 rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode); 582 rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
503 if (rc || !lower_dentry->d_inode) 583 if (rc || !lower_dentry->d_inode)
504 goto out; 584 goto out;
505 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0); 585 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
506 if (rc) 586 if (rc)
507 goto out; 587 goto out;
508 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 588 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
@@ -550,7 +630,7 @@ ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
550 rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev); 630 rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
551 if (rc || !lower_dentry->d_inode) 631 if (rc || !lower_dentry->d_inode)
552 goto out; 632 goto out;
553 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0); 633 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
554 if (rc) 634 if (rc)
555 goto out; 635 goto out;
556 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 636 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
@@ -750,7 +830,7 @@ static int truncate_upper(struct dentry *dentry, struct iattr *ia,
750 lower_ia->ia_valid &= ~ATTR_SIZE; 830 lower_ia->ia_valid &= ~ATTR_SIZE;
751 return 0; 831 return 0;
752 } 832 }
753 rc = ecryptfs_get_lower_file(dentry); 833 rc = ecryptfs_get_lower_file(dentry, inode);
754 if (rc) 834 if (rc)
755 return rc; 835 return rc;
756 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat; 836 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
@@ -906,7 +986,7 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
906 986
907 mount_crypt_stat = &ecryptfs_superblock_to_private( 987 mount_crypt_stat = &ecryptfs_superblock_to_private(
908 dentry->d_sb)->mount_crypt_stat; 988 dentry->d_sb)->mount_crypt_stat;
909 rc = ecryptfs_get_lower_file(dentry); 989 rc = ecryptfs_get_lower_file(dentry, inode);
910 if (rc) { 990 if (rc) {
911 mutex_unlock(&crypt_stat->cs_mutex); 991 mutex_unlock(&crypt_stat->cs_mutex);
912 goto out; 992 goto out;
@@ -1079,21 +1159,6 @@ out:
1079 return rc; 1159 return rc;
1080} 1160}
1081 1161
1082int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
1083{
1084 if ((ecryptfs_inode_to_lower(inode)
1085 == (struct inode *)candidate_lower_inode))
1086 return 1;
1087 else
1088 return 0;
1089}
1090
1091int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
1092{
1093 ecryptfs_init_inode(inode, (struct inode *)lower_inode);
1094 return 0;
1095}
1096
1097const struct inode_operations ecryptfs_symlink_iops = { 1162const struct inode_operations ecryptfs_symlink_iops = {
1098 .readlink = ecryptfs_readlink, 1163 .readlink = ecryptfs_readlink,
1099 .follow_link = ecryptfs_follow_link, 1164 .follow_link = ecryptfs_follow_link,
diff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c
index 89b93389af8e..9f1bb747d77d 100644
--- a/fs/ecryptfs/main.c
+++ b/fs/ecryptfs/main.c
@@ -135,12 +135,12 @@ static int ecryptfs_init_lower_file(struct dentry *dentry,
135 return rc; 135 return rc;
136} 136}
137 137
138int ecryptfs_get_lower_file(struct dentry *dentry) 138int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
139{ 139{
140 struct ecryptfs_inode_info *inode_info = 140 struct ecryptfs_inode_info *inode_info;
141 ecryptfs_inode_to_private(dentry->d_inode);
142 int count, rc = 0; 141 int count, rc = 0;
143 142
143 inode_info = ecryptfs_inode_to_private(inode);
144 mutex_lock(&inode_info->lower_file_mutex); 144 mutex_lock(&inode_info->lower_file_mutex);
145 count = atomic_inc_return(&inode_info->lower_file_count); 145 count = atomic_inc_return(&inode_info->lower_file_count);
146 if (WARN_ON_ONCE(count < 1)) 146 if (WARN_ON_ONCE(count < 1))
@@ -168,75 +168,6 @@ void ecryptfs_put_lower_file(struct inode *inode)
168 } 168 }
169} 169}
170 170
171static struct inode *ecryptfs_get_inode(struct inode *lower_inode,
172 struct super_block *sb)
173{
174 struct inode *inode;
175 int rc = 0;
176
177 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) {
178 rc = -EXDEV;
179 goto out;
180 }
181 if (!igrab(lower_inode)) {
182 rc = -ESTALE;
183 goto out;
184 }
185 inode = iget5_locked(sb, (unsigned long)lower_inode,
186 ecryptfs_inode_test, ecryptfs_inode_set,
187 lower_inode);
188 if (!inode) {
189 rc = -EACCES;
190 iput(lower_inode);
191 goto out;
192 }
193 if (inode->i_state & I_NEW)
194 unlock_new_inode(inode);
195 else
196 iput(lower_inode);
197 if (S_ISLNK(lower_inode->i_mode))
198 inode->i_op = &ecryptfs_symlink_iops;
199 else if (S_ISDIR(lower_inode->i_mode))
200 inode->i_op = &ecryptfs_dir_iops;
201 if (S_ISDIR(lower_inode->i_mode))
202 inode->i_fop = &ecryptfs_dir_fops;
203 if (special_file(lower_inode->i_mode))
204 init_special_inode(inode, lower_inode->i_mode,
205 lower_inode->i_rdev);
206 fsstack_copy_attr_all(inode, lower_inode);
207 /* This size will be overwritten for real files w/ headers and
208 * other metadata */
209 fsstack_copy_inode_size(inode, lower_inode);
210 return inode;
211out:
212 return ERR_PTR(rc);
213}
214
215/**
216 * ecryptfs_interpose
217 * @lower_dentry: Existing dentry in the lower filesystem
218 * @dentry: ecryptfs' dentry
219 * @sb: ecryptfs's super_block
220 * @flags: flags to govern behavior of interpose procedure
221 *
222 * Interposes upper and lower dentries.
223 *
224 * Returns zero on success; non-zero otherwise
225 */
226int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
227 struct super_block *sb, u32 flags)
228{
229 struct inode *lower_inode = lower_dentry->d_inode;
230 struct inode *inode = ecryptfs_get_inode(lower_inode, sb);
231 if (IS_ERR(inode))
232 return PTR_ERR(inode);
233 if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
234 d_add(dentry, inode);
235 else
236 d_instantiate(dentry, inode);
237 return 0;
238}
239
240enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, 171enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
241 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher, 172 ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
242 ecryptfs_opt_ecryptfs_key_bytes, 173 ecryptfs_opt_ecryptfs_key_bytes,
@@ -704,13 +635,8 @@ static struct ecryptfs_cache_info {
704 .size = sizeof(struct ecryptfs_sb_info), 635 .size = sizeof(struct ecryptfs_sb_info),
705 }, 636 },
706 { 637 {
707 .cache = &ecryptfs_header_cache_1, 638 .cache = &ecryptfs_header_cache,
708 .name = "ecryptfs_headers_1", 639 .name = "ecryptfs_headers",
709 .size = PAGE_CACHE_SIZE,
710 },
711 {
712 .cache = &ecryptfs_header_cache_2,
713 .name = "ecryptfs_headers_2",
714 .size = PAGE_CACHE_SIZE, 640 .size = PAGE_CACHE_SIZE,
715 }, 641 },
716 { 642 {
diff --git a/fs/ecryptfs/super.c b/fs/ecryptfs/super.c
index 245b517bf1b6..dbd52d40df4c 100644
--- a/fs/ecryptfs/super.c
+++ b/fs/ecryptfs/super.c
@@ -93,22 +93,6 @@ static void ecryptfs_destroy_inode(struct inode *inode)
93} 93}
94 94
95/** 95/**
96 * ecryptfs_init_inode
97 * @inode: The ecryptfs inode
98 *
99 * Set up the ecryptfs inode.
100 */
101void ecryptfs_init_inode(struct inode *inode, struct inode *lower_inode)
102{
103 ecryptfs_set_inode_lower(inode, lower_inode);
104 inode->i_ino = lower_inode->i_ino;
105 inode->i_version++;
106 inode->i_op = &ecryptfs_main_iops;
107 inode->i_fop = &ecryptfs_main_fops;
108 inode->i_mapping->a_ops = &ecryptfs_aops;
109}
110
111/**
112 * ecryptfs_statfs 96 * ecryptfs_statfs
113 * @sb: The ecryptfs super block 97 * @sb: The ecryptfs super block
114 * @buf: The struct kstatfs to fill in with stats 98 * @buf: The struct kstatfs to fill in with stats