aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ecryptfs/inode.c
diff options
context:
space:
mode:
authorGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
committerGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
commitc71c03bda1e86c9d5198c5d83f712e695c4f2a1e (patch)
treeecb166cb3e2b7e2adb3b5e292245fefd23381ac8 /fs/ecryptfs/inode.c
parentea53c912f8a86a8567697115b6a0d8152beee5c8 (diff)
parent6a00f206debf8a5c8899055726ad127dbeeed098 (diff)
Merge branch 'mpi-master' into wip-k-fmlpwip-k-fmlp
Conflicts: litmus/sched_cedf.c
Diffstat (limited to 'fs/ecryptfs/inode.c')
-rw-r--r--fs/ecryptfs/inode.c497
1 files changed, 232 insertions, 265 deletions
diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c
index 3fbc94203380..7349ade17de6 100644
--- a/fs/ecryptfs/inode.c
+++ b/fs/ecryptfs/inode.c
@@ -32,6 +32,7 @@
32#include <linux/crypto.h> 32#include <linux/crypto.h>
33#include <linux/fs_stack.h> 33#include <linux/fs_stack.h>
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include <linux/xattr.h>
35#include <asm/unaligned.h> 36#include <asm/unaligned.h>
36#include "ecryptfs_kernel.h" 37#include "ecryptfs_kernel.h"
37 38
@@ -50,6 +51,97 @@ static void unlock_dir(struct dentry *dir)
50 dput(dir); 51 dput(dir);
51} 52}
52 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
53/** 145/**
54 * ecryptfs_create_underlying_file 146 * ecryptfs_create_underlying_file
55 * @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
@@ -70,15 +162,23 @@ ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
70 struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry); 162 struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
71 struct dentry *dentry_save; 163 struct dentry *dentry_save;
72 struct vfsmount *vfsmount_save; 164 struct vfsmount *vfsmount_save;
165 unsigned int flags_save;
73 int rc; 166 int rc;
74 167
75 dentry_save = nd->path.dentry; 168 if (nd) {
76 vfsmount_save = nd->path.mnt; 169 dentry_save = nd->path.dentry;
77 nd->path.dentry = lower_dentry; 170 vfsmount_save = nd->path.mnt;
78 nd->path.mnt = lower_mnt; 171 flags_save = nd->flags;
172 nd->path.dentry = lower_dentry;
173 nd->path.mnt = lower_mnt;
174 nd->flags &= ~LOOKUP_OPEN;
175 }
79 rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd); 176 rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
80 nd->path.dentry = dentry_save; 177 if (nd) {
81 nd->path.mnt = vfsmount_save; 178 nd->path.dentry = dentry_save;
179 nd->path.mnt = vfsmount_save;
180 nd->flags = flags_save;
181 }
82 return rc; 182 return rc;
83} 183}
84 184
@@ -120,7 +220,7 @@ ecryptfs_do_create(struct inode *directory_inode,
120 goto out_lock; 220 goto out_lock;
121 } 221 }
122 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry, 222 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
123 directory_inode->i_sb, 0); 223 directory_inode->i_sb);
124 if (rc) { 224 if (rc) {
125 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n"); 225 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
126 goto out_lock; 226 goto out_lock;
@@ -134,26 +234,6 @@ out:
134} 234}
135 235
136/** 236/**
137 * grow_file
138 * @ecryptfs_dentry: the eCryptfs dentry
139 *
140 * This is the code which will grow the file to its correct size.
141 */
142static int grow_file(struct dentry *ecryptfs_dentry)
143{
144 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
145 char zero_virt[] = { 0x00 };
146 int rc = 0;
147
148 rc = ecryptfs_write(ecryptfs_inode, zero_virt, 0, 1);
149 i_size_write(ecryptfs_inode, 0);
150 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
151 ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat.flags |=
152 ECRYPTFS_NEW_FILE;
153 return rc;
154}
155
156/**
157 * ecryptfs_initialize_file 237 * ecryptfs_initialize_file
158 * 238 *
159 * Cause the file to be changed from a basic empty file to an ecryptfs 239 * Cause the file to be changed from a basic empty file to an ecryptfs
@@ -172,7 +252,6 @@ static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
172 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); 252 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
173 goto out; 253 goto out;
174 } 254 }
175 crypt_stat->flags |= ECRYPTFS_NEW_FILE;
176 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n"); 255 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
177 rc = ecryptfs_new_file_context(ecryptfs_dentry); 256 rc = ecryptfs_new_file_context(ecryptfs_dentry);
178 if (rc) { 257 if (rc) {
@@ -180,24 +259,19 @@ static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
180 "context; rc = [%d]\n", rc); 259 "context; rc = [%d]\n", rc);
181 goto out; 260 goto out;
182 } 261 }
183 if (!ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->lower_file) { 262 rc = ecryptfs_get_lower_file(ecryptfs_dentry,
184 rc = ecryptfs_init_persistent_file(ecryptfs_dentry); 263 ecryptfs_dentry->d_inode);
185 if (rc) {
186 printk(KERN_ERR "%s: Error attempting to initialize "
187 "the persistent file for the dentry with name "
188 "[%s]; rc = [%d]\n", __func__,
189 ecryptfs_dentry->d_name.name, rc);
190 goto out;
191 }
192 }
193 rc = ecryptfs_write_metadata(ecryptfs_dentry);
194 if (rc) { 264 if (rc) {
195 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc); 265 printk(KERN_ERR "%s: Error attempting to initialize "
266 "the lower file for the dentry with name "
267 "[%s]; rc = [%d]\n", __func__,
268 ecryptfs_dentry->d_name.name, rc);
196 goto out; 269 goto out;
197 } 270 }
198 rc = grow_file(ecryptfs_dentry); 271 rc = ecryptfs_write_metadata(ecryptfs_dentry);
199 if (rc) 272 if (rc)
200 printk(KERN_ERR "Error growing file; rc = [%d]\n", rc); 273 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
274 ecryptfs_put_lower_file(ecryptfs_dentry->d_inode);
201out: 275out:
202 return rc; 276 return rc;
203} 277}
@@ -233,187 +307,91 @@ out:
233 return rc; 307 return rc;
234} 308}
235 309
236/** 310static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
237 * ecryptfs_lookup_and_interpose_lower - Perform a lookup
238 */
239int ecryptfs_lookup_and_interpose_lower(struct dentry *ecryptfs_dentry,
240 struct dentry *lower_dentry,
241 struct inode *ecryptfs_dir_inode,
242 struct nameidata *ecryptfs_nd)
243{ 311{
244 struct dentry *lower_dir_dentry;
245 struct vfsmount *lower_mnt;
246 struct inode *lower_inode;
247 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
248 struct ecryptfs_crypt_stat *crypt_stat; 312 struct ecryptfs_crypt_stat *crypt_stat;
249 char *page_virt = NULL; 313 int rc;
250 u64 file_size;
251 int rc = 0;
252 314
253 lower_dir_dentry = lower_dentry->d_parent; 315 rc = ecryptfs_get_lower_file(dentry, inode);
254 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(
255 ecryptfs_dentry->d_parent));
256 lower_inode = lower_dentry->d_inode;
257 fsstack_copy_attr_atime(ecryptfs_dir_inode, lower_dir_dentry->d_inode);
258 BUG_ON(!atomic_read(&lower_dentry->d_count));
259 ecryptfs_set_dentry_private(ecryptfs_dentry,
260 kmem_cache_alloc(ecryptfs_dentry_info_cache,
261 GFP_KERNEL));
262 if (!ecryptfs_dentry_to_private(ecryptfs_dentry)) {
263 rc = -ENOMEM;
264 printk(KERN_ERR "%s: Out of memory whilst attempting "
265 "to allocate ecryptfs_dentry_info struct\n",
266 __func__);
267 goto out_put;
268 }
269 ecryptfs_set_dentry_lower(ecryptfs_dentry, lower_dentry);
270 ecryptfs_set_dentry_lower_mnt(ecryptfs_dentry, lower_mnt);
271 if (!lower_dentry->d_inode) {
272 /* We want to add because we couldn't find in lower */
273 d_add(ecryptfs_dentry, NULL);
274 goto out;
275 }
276 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
277 ecryptfs_dir_inode->i_sb,
278 ECRYPTFS_INTERPOSE_FLAG_D_ADD);
279 if (rc) { 316 if (rc) {
280 printk(KERN_ERR "%s: Error interposing; rc = [%d]\n", 317 printk(KERN_ERR "%s: Error attempting to initialize "
281 __func__, rc); 318 "the lower file for the dentry with name "
282 goto out; 319 "[%s]; rc = [%d]\n", __func__,
283 } 320 dentry->d_name.name, rc);
284 if (S_ISDIR(lower_inode->i_mode)) 321 return rc;
285 goto out;
286 if (S_ISLNK(lower_inode->i_mode))
287 goto out;
288 if (special_file(lower_inode->i_mode))
289 goto out;
290 if (!ecryptfs_nd)
291 goto out;
292 /* Released in this function */
293 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2, GFP_USER);
294 if (!page_virt) {
295 printk(KERN_ERR "%s: Cannot kmem_cache_zalloc() a page\n",
296 __func__);
297 rc = -ENOMEM;
298 goto out;
299 } 322 }
300 if (!ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->lower_file) { 323
301 rc = ecryptfs_init_persistent_file(ecryptfs_dentry); 324 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
302 if (rc) {
303 printk(KERN_ERR "%s: Error attempting to initialize "
304 "the persistent file for the dentry with name "
305 "[%s]; rc = [%d]\n", __func__,
306 ecryptfs_dentry->d_name.name, rc);
307 goto out_free_kmem;
308 }
309 }
310 crypt_stat = &ecryptfs_inode_to_private(
311 ecryptfs_dentry->d_inode)->crypt_stat;
312 /* TODO: lock for crypt_stat comparison */ 325 /* TODO: lock for crypt_stat comparison */
313 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) 326 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
314 ecryptfs_set_default_sizes(crypt_stat); 327 ecryptfs_set_default_sizes(crypt_stat);
315 rc = ecryptfs_read_and_validate_header_region(page_virt, 328
316 ecryptfs_dentry->d_inode); 329 rc = ecryptfs_read_and_validate_header_region(inode);
330 ecryptfs_put_lower_file(inode);
317 if (rc) { 331 if (rc) {
318 memset(page_virt, 0, PAGE_CACHE_SIZE); 332 rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
319 rc = ecryptfs_read_and_validate_xattr_region(page_virt, 333 if (!rc)
320 ecryptfs_dentry); 334 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
321 if (rc) {
322 rc = 0;
323 goto out_free_kmem;
324 }
325 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
326 }
327 mount_crypt_stat = &ecryptfs_superblock_to_private(
328 ecryptfs_dentry->d_sb)->mount_crypt_stat;
329 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
330 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
331 file_size = (crypt_stat->metadata_size
332 + i_size_read(lower_dentry->d_inode));
333 else
334 file_size = i_size_read(lower_dentry->d_inode);
335 } else {
336 file_size = get_unaligned_be64(page_virt);
337 } 335 }
338 i_size_write(ecryptfs_dentry->d_inode, (loff_t)file_size);
339out_free_kmem:
340 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
341 goto out;
342out_put:
343 dput(lower_dentry);
344 mntput(lower_mnt);
345 d_drop(ecryptfs_dentry);
346out:
347 return rc;
348}
349
350/**
351 * ecryptfs_new_lower_dentry
352 * @name: The name of the new dentry.
353 * @lower_dir_dentry: Parent directory of the new dentry.
354 * @nd: nameidata from last lookup.
355 *
356 * Create a new dentry or get it from lower parent dir.
357 */
358static struct dentry *
359ecryptfs_new_lower_dentry(struct qstr *name, struct dentry *lower_dir_dentry,
360 struct nameidata *nd)
361{
362 struct dentry *new_dentry;
363 struct dentry *tmp;
364 struct inode *lower_dir_inode;
365
366 lower_dir_inode = lower_dir_dentry->d_inode;
367
368 tmp = d_alloc(lower_dir_dentry, name);
369 if (!tmp)
370 return ERR_PTR(-ENOMEM);
371 336
372 mutex_lock(&lower_dir_inode->i_mutex); 337 /* Must return 0 to allow non-eCryptfs files to be looked up, too */
373 new_dentry = lower_dir_inode->i_op->lookup(lower_dir_inode, tmp, nd); 338 return 0;
374 mutex_unlock(&lower_dir_inode->i_mutex);
375
376 if (!new_dentry)
377 new_dentry = tmp;
378 else
379 dput(tmp);
380
381 return new_dentry;
382} 339}
383 340
384
385/** 341/**
386 * ecryptfs_lookup_one_lower 342 * ecryptfs_lookup_interpose - Dentry interposition for a lookup
387 * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
388 * @lower_dir_dentry: lower parent directory
389 * @name: lower file name
390 *
391 * Get the lower dentry from vfs. If lower dentry does not exist yet,
392 * create it.
393 */ 343 */
394static struct dentry * 344static int ecryptfs_lookup_interpose(struct dentry *dentry,
395ecryptfs_lookup_one_lower(struct dentry *ecryptfs_dentry, 345 struct dentry *lower_dentry,
396 struct dentry *lower_dir_dentry, struct qstr *name) 346 struct inode *dir_inode)
397{ 347{
398 struct nameidata nd; 348 struct inode *inode, *lower_inode = lower_dentry->d_inode;
349 struct ecryptfs_dentry_info *dentry_info;
399 struct vfsmount *lower_mnt; 350 struct vfsmount *lower_mnt;
400 int err; 351 int rc = 0;
352
353 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
354 fsstack_copy_attr_atime(dir_inode, lower_dentry->d_parent->d_inode);
355 BUG_ON(!lower_dentry->d_count);
401 356
402 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt( 357 dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
403 ecryptfs_dentry->d_parent)); 358 ecryptfs_set_dentry_private(dentry, dentry_info);
404 err = vfs_path_lookup(lower_dir_dentry, lower_mnt, name->name , 0, &nd); 359 if (!dentry_info) {
405 mntput(lower_mnt); 360 printk(KERN_ERR "%s: Out of memory whilst attempting "
361 "to allocate ecryptfs_dentry_info struct\n",
362 __func__);
363 dput(lower_dentry);
364 mntput(lower_mnt);
365 d_drop(dentry);
366 return -ENOMEM;
367 }
368 ecryptfs_set_dentry_lower(dentry, lower_dentry);
369 ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
406 370
407 if (!err) { 371 if (!lower_dentry->d_inode) {
408 /* we dont need the mount */ 372 /* We want to add because we couldn't find in lower */
409 mntput(nd.path.mnt); 373 d_add(dentry, NULL);
410 return nd.path.dentry; 374 return 0;
375 }
376 inode = __ecryptfs_get_inode(lower_inode, dir_inode->i_sb);
377 if (IS_ERR(inode)) {
378 printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
379 __func__, PTR_ERR(inode));
380 return PTR_ERR(inode);
381 }
382 if (S_ISREG(inode->i_mode)) {
383 rc = ecryptfs_i_size_read(dentry, inode);
384 if (rc) {
385 make_bad_inode(inode);
386 return rc;
387 }
411 } 388 }
412 if (err != -ENOENT)
413 return ERR_PTR(err);
414 389
415 /* create a new lower dentry */ 390 if (inode->i_state & I_NEW)
416 return ecryptfs_new_lower_dentry(name, lower_dir_dentry, &nd); 391 unlock_new_inode(inode);
392 d_add(dentry, inode);
393
394 return rc;
417} 395}
418 396
419/** 397/**
@@ -433,10 +411,8 @@ static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
433 size_t encrypted_and_encoded_name_size; 411 size_t encrypted_and_encoded_name_size;
434 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL; 412 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
435 struct dentry *lower_dir_dentry, *lower_dentry; 413 struct dentry *lower_dir_dentry, *lower_dentry;
436 struct qstr lower_name;
437 int rc = 0; 414 int rc = 0;
438 415
439 ecryptfs_dentry->d_op = &ecryptfs_dops;
440 if ((ecryptfs_dentry->d_name.len == 1 416 if ((ecryptfs_dentry->d_name.len == 1
441 && !strcmp(ecryptfs_dentry->d_name.name, ".")) 417 && !strcmp(ecryptfs_dentry->d_name.name, "."))
442 || (ecryptfs_dentry->d_name.len == 2 418 || (ecryptfs_dentry->d_name.len == 2
@@ -444,31 +420,25 @@ static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
444 goto out_d_drop; 420 goto out_d_drop;
445 } 421 }
446 lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent); 422 lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
447 lower_name.name = ecryptfs_dentry->d_name.name; 423 mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
448 lower_name.len = ecryptfs_dentry->d_name.len; 424 lower_dentry = lookup_one_len(ecryptfs_dentry->d_name.name,
449 lower_name.hash = ecryptfs_dentry->d_name.hash; 425 lower_dir_dentry,
450 if (lower_dir_dentry->d_op && lower_dir_dentry->d_op->d_hash) { 426 ecryptfs_dentry->d_name.len);
451 rc = lower_dir_dentry->d_op->d_hash(lower_dir_dentry, 427 mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
452 &lower_name);
453 if (rc < 0)
454 goto out_d_drop;
455 }
456 lower_dentry = ecryptfs_lookup_one_lower(ecryptfs_dentry,
457 lower_dir_dentry, &lower_name);
458 if (IS_ERR(lower_dentry)) { 428 if (IS_ERR(lower_dentry)) {
459 rc = PTR_ERR(lower_dentry); 429 rc = PTR_ERR(lower_dentry);
460 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_lower() returned " 430 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
461 "[%d] on lower_dentry = [%s]\n", __func__, rc, 431 "[%d] on lower_dentry = [%s]\n", __func__, rc,
462 encrypted_and_encoded_name); 432 encrypted_and_encoded_name);
463 goto out_d_drop; 433 goto out_d_drop;
464 } 434 }
465 if (lower_dentry->d_inode) 435 if (lower_dentry->d_inode)
466 goto lookup_and_interpose; 436 goto interpose;
467 mount_crypt_stat = &ecryptfs_superblock_to_private( 437 mount_crypt_stat = &ecryptfs_superblock_to_private(
468 ecryptfs_dentry->d_sb)->mount_crypt_stat; 438 ecryptfs_dentry->d_sb)->mount_crypt_stat;
469 if (!(mount_crypt_stat 439 if (!(mount_crypt_stat
470 && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) 440 && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)))
471 goto lookup_and_interpose; 441 goto interpose;
472 dput(lower_dentry); 442 dput(lower_dentry);
473 rc = ecryptfs_encrypt_and_encode_filename( 443 rc = ecryptfs_encrypt_and_encode_filename(
474 &encrypted_and_encoded_name, &encrypted_and_encoded_name_size, 444 &encrypted_and_encoded_name, &encrypted_and_encoded_name_size,
@@ -479,28 +449,21 @@ static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
479 "filename; rc = [%d]\n", __func__, rc); 449 "filename; rc = [%d]\n", __func__, rc);
480 goto out_d_drop; 450 goto out_d_drop;
481 } 451 }
482 lower_name.name = encrypted_and_encoded_name; 452 mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
483 lower_name.len = encrypted_and_encoded_name_size; 453 lower_dentry = lookup_one_len(encrypted_and_encoded_name,
484 lower_name.hash = full_name_hash(lower_name.name, lower_name.len); 454 lower_dir_dentry,
485 if (lower_dir_dentry->d_op && lower_dir_dentry->d_op->d_hash) { 455 encrypted_and_encoded_name_size);
486 rc = lower_dir_dentry->d_op->d_hash(lower_dir_dentry, 456 mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
487 &lower_name);
488 if (rc < 0)
489 goto out_d_drop;
490 }
491 lower_dentry = ecryptfs_lookup_one_lower(ecryptfs_dentry,
492 lower_dir_dentry, &lower_name);
493 if (IS_ERR(lower_dentry)) { 457 if (IS_ERR(lower_dentry)) {
494 rc = PTR_ERR(lower_dentry); 458 rc = PTR_ERR(lower_dentry);
495 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_lower() returned " 459 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
496 "[%d] on lower_dentry = [%s]\n", __func__, rc, 460 "[%d] on lower_dentry = [%s]\n", __func__, rc,
497 encrypted_and_encoded_name); 461 encrypted_and_encoded_name);
498 goto out_d_drop; 462 goto out_d_drop;
499 } 463 }
500lookup_and_interpose: 464interpose:
501 rc = ecryptfs_lookup_and_interpose_lower(ecryptfs_dentry, lower_dentry, 465 rc = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry,
502 ecryptfs_dir_inode, 466 ecryptfs_dir_inode);
503 ecryptfs_nd);
504 goto out; 467 goto out;
505out_d_drop: 468out_d_drop:
506 d_drop(ecryptfs_dentry); 469 d_drop(ecryptfs_dentry);
@@ -528,7 +491,7 @@ static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
528 lower_new_dentry); 491 lower_new_dentry);
529 if (rc || !lower_new_dentry->d_inode) 492 if (rc || !lower_new_dentry->d_inode)
530 goto out_lock; 493 goto out_lock;
531 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0); 494 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
532 if (rc) 495 if (rc)
533 goto out_lock; 496 goto out_lock;
534 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 497 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
@@ -595,7 +558,7 @@ static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
595 kfree(encoded_symname); 558 kfree(encoded_symname);
596 if (rc || !lower_dentry->d_inode) 559 if (rc || !lower_dentry->d_inode)
597 goto out_lock; 560 goto out_lock;
598 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0); 561 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
599 if (rc) 562 if (rc)
600 goto out_lock; 563 goto out_lock;
601 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 564 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
@@ -619,7 +582,7 @@ static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
619 rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode); 582 rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
620 if (rc || !lower_dentry->d_inode) 583 if (rc || !lower_dentry->d_inode)
621 goto out; 584 goto out;
622 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0); 585 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
623 if (rc) 586 if (rc)
624 goto out; 587 goto out;
625 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 588 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
@@ -644,8 +607,8 @@ static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
644 dget(lower_dentry); 607 dget(lower_dentry);
645 rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry); 608 rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
646 dput(lower_dentry); 609 dput(lower_dentry);
647 if (!rc) 610 if (!rc && dentry->d_inode)
648 d_delete(lower_dentry); 611 clear_nlink(dentry->d_inode);
649 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 612 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
650 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink; 613 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
651 unlock_dir(lower_dir_dentry); 614 unlock_dir(lower_dir_dentry);
@@ -667,7 +630,7 @@ ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
667 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);
668 if (rc || !lower_dentry->d_inode) 631 if (rc || !lower_dentry->d_inode)
669 goto out; 632 goto out;
670 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0); 633 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
671 if (rc) 634 if (rc)
672 goto out; 635 goto out;
673 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); 636 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
@@ -716,8 +679,8 @@ ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
716 fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode); 679 fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
717out_lock: 680out_lock:
718 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry); 681 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
719 dput(lower_new_dentry->d_parent); 682 dput(lower_new_dir_dentry);
720 dput(lower_old_dentry->d_parent); 683 dput(lower_old_dir_dentry);
721 dput(lower_new_dentry); 684 dput(lower_new_dentry);
722 dput(lower_old_dentry); 685 dput(lower_old_dentry);
723 return rc; 686 return rc;
@@ -865,8 +828,11 @@ static int truncate_upper(struct dentry *dentry, struct iattr *ia,
865 828
866 if (unlikely((ia->ia_size == i_size))) { 829 if (unlikely((ia->ia_size == i_size))) {
867 lower_ia->ia_valid &= ~ATTR_SIZE; 830 lower_ia->ia_valid &= ~ATTR_SIZE;
868 goto out; 831 return 0;
869 } 832 }
833 rc = ecryptfs_get_lower_file(dentry, inode);
834 if (rc)
835 return rc;
870 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat; 836 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
871 /* Switch on growing or shrinking file */ 837 /* Switch on growing or shrinking file */
872 if (ia->ia_size > i_size) { 838 if (ia->ia_size > i_size) {
@@ -944,6 +910,7 @@ static int truncate_upper(struct dentry *dentry, struct iattr *ia,
944 lower_ia->ia_valid &= ~ATTR_SIZE; 910 lower_ia->ia_valid &= ~ATTR_SIZE;
945 } 911 }
946out: 912out:
913 ecryptfs_put_lower_file(inode);
947 return rc; 914 return rc;
948} 915}
949 916
@@ -975,8 +942,10 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
975} 942}
976 943
977static int 944static int
978ecryptfs_permission(struct inode *inode, int mask) 945ecryptfs_permission(struct inode *inode, int mask, unsigned int flags)
979{ 946{
947 if (flags & IPERM_FLAG_RCU)
948 return -ECHILD;
980 return inode_permission(ecryptfs_inode_to_lower(inode), mask); 949 return inode_permission(ecryptfs_inode_to_lower(inode), mask);
981} 950}
982 951
@@ -1017,7 +986,13 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
1017 986
1018 mount_crypt_stat = &ecryptfs_superblock_to_private( 987 mount_crypt_stat = &ecryptfs_superblock_to_private(
1019 dentry->d_sb)->mount_crypt_stat; 988 dentry->d_sb)->mount_crypt_stat;
989 rc = ecryptfs_get_lower_file(dentry, inode);
990 if (rc) {
991 mutex_unlock(&crypt_stat->cs_mutex);
992 goto out;
993 }
1020 rc = ecryptfs_read_metadata(dentry); 994 rc = ecryptfs_read_metadata(dentry);
995 ecryptfs_put_lower_file(inode);
1021 if (rc) { 996 if (rc) {
1022 if (!(mount_crypt_stat->flags 997 if (!(mount_crypt_stat->flags
1023 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) { 998 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
@@ -1031,10 +1006,17 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
1031 goto out; 1006 goto out;
1032 } 1007 }
1033 rc = 0; 1008 rc = 0;
1034 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); 1009 crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
1010 | ECRYPTFS_ENCRYPTED);
1035 } 1011 }
1036 } 1012 }
1037 mutex_unlock(&crypt_stat->cs_mutex); 1013 mutex_unlock(&crypt_stat->cs_mutex);
1014 if (S_ISREG(inode->i_mode)) {
1015 rc = filemap_write_and_wait(inode->i_mapping);
1016 if (rc)
1017 goto out;
1018 fsstack_copy_attr_all(inode, lower_inode);
1019 }
1038 memcpy(&lower_ia, ia, sizeof(lower_ia)); 1020 memcpy(&lower_ia, ia, sizeof(lower_ia));
1039 if (ia->ia_valid & ATTR_FILE) 1021 if (ia->ia_valid & ATTR_FILE)
1040 lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file); 1022 lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
@@ -1090,6 +1072,8 @@ int ecryptfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1090 rc = vfs_getattr(ecryptfs_dentry_to_lower_mnt(dentry), 1072 rc = vfs_getattr(ecryptfs_dentry_to_lower_mnt(dentry),
1091 ecryptfs_dentry_to_lower(dentry), &lower_stat); 1073 ecryptfs_dentry_to_lower(dentry), &lower_stat);
1092 if (!rc) { 1074 if (!rc) {
1075 fsstack_copy_attr_all(dentry->d_inode,
1076 ecryptfs_inode_to_lower(dentry->d_inode));
1093 generic_fillattr(dentry->d_inode, stat); 1077 generic_fillattr(dentry->d_inode, stat);
1094 stat->blocks = lower_stat.blocks; 1078 stat->blocks = lower_stat.blocks;
1095 } 1079 }
@@ -1108,10 +1092,8 @@ ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
1108 rc = -EOPNOTSUPP; 1092 rc = -EOPNOTSUPP;
1109 goto out; 1093 goto out;
1110 } 1094 }
1111 mutex_lock(&lower_dentry->d_inode->i_mutex); 1095
1112 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value, 1096 rc = vfs_setxattr(lower_dentry, name, value, size, flags);
1113 size, flags);
1114 mutex_unlock(&lower_dentry->d_inode->i_mutex);
1115out: 1097out:
1116 return rc; 1098 return rc;
1117} 1099}
@@ -1177,21 +1159,6 @@ out:
1177 return rc; 1159 return rc;
1178} 1160}
1179 1161
1180int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
1181{
1182 if ((ecryptfs_inode_to_lower(inode)
1183 == (struct inode *)candidate_lower_inode))
1184 return 1;
1185 else
1186 return 0;
1187}
1188
1189int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
1190{
1191 ecryptfs_init_inode(inode, (struct inode *)lower_inode);
1192 return 0;
1193}
1194
1195const struct inode_operations ecryptfs_symlink_iops = { 1162const struct inode_operations ecryptfs_symlink_iops = {
1196 .readlink = ecryptfs_readlink, 1163 .readlink = ecryptfs_readlink,
1197 .follow_link = ecryptfs_follow_link, 1164 .follow_link = ecryptfs_follow_link,