eCryptfs: Check inode changes in setattr
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ecryptfs / inode.c
blobe3562f2a59dc10e1243e05e7ba56651a44e68740
1 /**
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
23 * 02111-1307, USA.
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 <linux/slab.h>
35 #include <linux/xattr.h>
36 #include <asm/unaligned.h>
37 #include "ecryptfs_kernel.h"
39 static struct dentry *lock_parent(struct dentry *dentry)
41 struct dentry *dir;
43 dir = dget_parent(dentry);
44 mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT);
45 return dir;
48 static void unlock_dir(struct dentry *dir)
50 mutex_unlock(&dir->d_inode->i_mutex);
51 dput(dir);
54 static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
56 if (ecryptfs_inode_to_lower(inode) == (struct inode *)lower_inode)
57 return 1;
58 return 0;
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);
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 inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
74 if (S_ISLNK(inode->i_mode))
75 inode->i_op = &ecryptfs_symlink_iops;
76 else if (S_ISDIR(inode->i_mode))
77 inode->i_op = &ecryptfs_dir_iops;
78 else
79 inode->i_op = &ecryptfs_main_iops;
81 if (S_ISDIR(inode->i_mode))
82 inode->i_fop = &ecryptfs_dir_fops;
83 else if (special_file(inode->i_mode))
84 init_special_inode(inode, inode->i_mode, inode->i_rdev);
85 else
86 inode->i_fop = &ecryptfs_main_fops;
88 return 0;
91 static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
92 struct super_block *sb)
94 struct inode *inode;
96 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
97 return ERR_PTR(-EXDEV);
98 if (!igrab(lower_inode))
99 return ERR_PTR(-ESTALE);
100 inode = iget5_locked(sb, (unsigned long)lower_inode,
101 ecryptfs_inode_test, ecryptfs_inode_set,
102 lower_inode);
103 if (!inode) {
104 iput(lower_inode);
105 return ERR_PTR(-EACCES);
107 if (!(inode->i_state & I_NEW))
108 iput(lower_inode);
110 return inode;
113 struct inode *ecryptfs_get_inode(struct inode *lower_inode,
114 struct super_block *sb)
116 struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
118 if (!IS_ERR(inode) && (inode->i_state & I_NEW))
119 unlock_new_inode(inode);
121 return inode;
125 * ecryptfs_interpose
126 * @lower_dentry: Existing dentry in the lower filesystem
127 * @dentry: ecryptfs' dentry
128 * @sb: ecryptfs's super_block
130 * Interposes upper and lower dentries.
132 * Returns zero on success; non-zero otherwise
134 static int ecryptfs_interpose(struct dentry *lower_dentry,
135 struct dentry *dentry, struct super_block *sb)
137 struct inode *inode = ecryptfs_get_inode(lower_dentry->d_inode, sb);
139 if (IS_ERR(inode))
140 return PTR_ERR(inode);
141 d_instantiate(dentry, inode);
143 return 0;
147 * ecryptfs_create_underlying_file
148 * @lower_dir_inode: inode of the parent in the lower fs of the new file
149 * @dentry: New file's dentry
150 * @mode: The mode of the new file
151 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
153 * Creates the file in the lower file system.
155 * Returns zero on success; non-zero on error condition
157 static int
158 ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
159 struct dentry *dentry, int mode,
160 struct nameidata *nd)
162 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
163 struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
164 struct dentry *dentry_save;
165 struct vfsmount *vfsmount_save;
166 unsigned int flags_save;
167 int rc;
169 if (nd) {
170 dentry_save = nd->path.dentry;
171 vfsmount_save = nd->path.mnt;
172 flags_save = nd->flags;
173 nd->path.dentry = lower_dentry;
174 nd->path.mnt = lower_mnt;
175 nd->flags &= ~LOOKUP_OPEN;
177 rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
178 if (nd) {
179 nd->path.dentry = dentry_save;
180 nd->path.mnt = vfsmount_save;
181 nd->flags = flags_save;
183 return rc;
187 * ecryptfs_do_create
188 * @directory_inode: inode of the new file's dentry's parent in ecryptfs
189 * @ecryptfs_dentry: New file's dentry in ecryptfs
190 * @mode: The mode of the new file
191 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
193 * Creates the underlying file and the eCryptfs inode which will link to
194 * it. It will also update the eCryptfs directory inode to mimic the
195 * stat of the lower directory inode.
197 * Returns zero on success; non-zero on error condition
199 static int
200 ecryptfs_do_create(struct inode *directory_inode,
201 struct dentry *ecryptfs_dentry, int mode,
202 struct nameidata *nd)
204 int rc;
205 struct dentry *lower_dentry;
206 struct dentry *lower_dir_dentry;
208 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
209 lower_dir_dentry = lock_parent(lower_dentry);
210 if (IS_ERR(lower_dir_dentry)) {
211 ecryptfs_printk(KERN_ERR, "Error locking directory of "
212 "dentry\n");
213 rc = PTR_ERR(lower_dir_dentry);
214 goto out;
216 rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
217 ecryptfs_dentry, mode, nd);
218 if (rc) {
219 printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
220 "rc = [%d]\n", __func__, rc);
221 goto out_lock;
223 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
224 directory_inode->i_sb);
225 if (rc) {
226 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
227 goto out_lock;
229 fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
230 fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
231 out_lock:
232 unlock_dir(lower_dir_dentry);
233 out:
234 return rc;
238 * ecryptfs_initialize_file
240 * Cause the file to be changed from a basic empty file to an ecryptfs
241 * file with a header and first data page.
243 * Returns zero on success
245 static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
247 struct ecryptfs_crypt_stat *crypt_stat =
248 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
249 int rc = 0;
251 if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
252 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
253 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
254 goto out;
256 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
257 rc = ecryptfs_new_file_context(ecryptfs_dentry);
258 if (rc) {
259 ecryptfs_printk(KERN_ERR, "Error creating new file "
260 "context; rc = [%d]\n", rc);
261 goto out;
263 rc = ecryptfs_get_lower_file(ecryptfs_dentry,
264 ecryptfs_dentry->d_inode);
265 if (rc) {
266 printk(KERN_ERR "%s: Error attempting to initialize "
267 "the lower file for the dentry with name "
268 "[%s]; rc = [%d]\n", __func__,
269 ecryptfs_dentry->d_name.name, rc);
270 goto out;
272 rc = ecryptfs_write_metadata(ecryptfs_dentry);
273 if (rc)
274 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
275 ecryptfs_put_lower_file(ecryptfs_dentry->d_inode);
276 out:
277 return rc;
281 * ecryptfs_create
282 * @dir: The inode of the directory in which to create the file.
283 * @dentry: The eCryptfs dentry
284 * @mode: The mode of the new file.
285 * @nd: nameidata
287 * Creates a new file.
289 * Returns zero on success; non-zero on error condition
291 static int
292 ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
293 int mode, struct nameidata *nd)
295 int rc;
297 /* ecryptfs_do_create() calls ecryptfs_interpose() */
298 rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
299 if (unlikely(rc)) {
300 ecryptfs_printk(KERN_WARNING, "Failed to create file in"
301 "lower filesystem\n");
302 goto out;
304 /* At this point, a file exists on "disk"; we need to make sure
305 * that this on disk file is prepared to be an ecryptfs file */
306 rc = ecryptfs_initialize_file(ecryptfs_dentry);
307 out:
308 return rc;
311 static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
313 struct ecryptfs_crypt_stat *crypt_stat;
314 int rc;
316 rc = ecryptfs_get_lower_file(dentry, inode);
317 if (rc) {
318 printk(KERN_ERR "%s: Error attempting to initialize "
319 "the lower file for the dentry with name "
320 "[%s]; rc = [%d]\n", __func__,
321 dentry->d_name.name, rc);
322 return rc;
325 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
326 /* TODO: lock for crypt_stat comparison */
327 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
328 ecryptfs_set_default_sizes(crypt_stat);
330 rc = ecryptfs_read_and_validate_header_region(inode);
331 ecryptfs_put_lower_file(inode);
332 if (rc) {
333 rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
334 if (!rc)
335 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
338 /* Must return 0 to allow non-eCryptfs files to be looked up, too */
339 return 0;
343 * ecryptfs_lookup_interpose - Dentry interposition for a lookup
345 static int ecryptfs_lookup_interpose(struct dentry *dentry,
346 struct dentry *lower_dentry,
347 struct inode *dir_inode)
349 struct inode *inode, *lower_inode = lower_dentry->d_inode;
350 struct ecryptfs_dentry_info *dentry_info;
351 struct vfsmount *lower_mnt;
352 int rc = 0;
354 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
355 fsstack_copy_attr_atime(dir_inode, lower_dentry->d_parent->d_inode);
356 BUG_ON(!lower_dentry->d_count);
358 dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
359 ecryptfs_set_dentry_private(dentry, dentry_info);
360 if (!dentry_info) {
361 printk(KERN_ERR "%s: Out of memory whilst attempting "
362 "to allocate ecryptfs_dentry_info struct\n",
363 __func__);
364 dput(lower_dentry);
365 mntput(lower_mnt);
366 d_drop(dentry);
367 return -ENOMEM;
369 ecryptfs_set_dentry_lower(dentry, lower_dentry);
370 ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
372 if (!lower_dentry->d_inode) {
373 /* We want to add because we couldn't find in lower */
374 d_add(dentry, NULL);
375 return 0;
377 inode = __ecryptfs_get_inode(lower_inode, dir_inode->i_sb);
378 if (IS_ERR(inode)) {
379 printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
380 __func__, PTR_ERR(inode));
381 return PTR_ERR(inode);
383 if (S_ISREG(inode->i_mode)) {
384 rc = ecryptfs_i_size_read(dentry, inode);
385 if (rc) {
386 make_bad_inode(inode);
387 return rc;
391 if (inode->i_state & I_NEW)
392 unlock_new_inode(inode);
393 d_add(dentry, inode);
395 return rc;
399 * ecryptfs_lookup
400 * @ecryptfs_dir_inode: The eCryptfs directory inode
401 * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
402 * @ecryptfs_nd: nameidata; may be NULL
404 * Find a file on disk. If the file does not exist, then we'll add it to the
405 * dentry cache and continue on to read it from the disk.
407 static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
408 struct dentry *ecryptfs_dentry,
409 struct nameidata *ecryptfs_nd)
411 char *encrypted_and_encoded_name = NULL;
412 size_t encrypted_and_encoded_name_size;
413 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
414 struct dentry *lower_dir_dentry, *lower_dentry;
415 int rc = 0;
417 if ((ecryptfs_dentry->d_name.len == 1
418 && !strcmp(ecryptfs_dentry->d_name.name, "."))
419 || (ecryptfs_dentry->d_name.len == 2
420 && !strcmp(ecryptfs_dentry->d_name.name, ".."))) {
421 goto out_d_drop;
423 lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
424 mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
425 lower_dentry = lookup_one_len(ecryptfs_dentry->d_name.name,
426 lower_dir_dentry,
427 ecryptfs_dentry->d_name.len);
428 mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
429 if (IS_ERR(lower_dentry)) {
430 rc = PTR_ERR(lower_dentry);
431 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
432 "[%d] on lower_dentry = [%s]\n", __func__, rc,
433 encrypted_and_encoded_name);
434 goto out_d_drop;
436 if (lower_dentry->d_inode)
437 goto interpose;
438 mount_crypt_stat = &ecryptfs_superblock_to_private(
439 ecryptfs_dentry->d_sb)->mount_crypt_stat;
440 if (!(mount_crypt_stat
441 && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)))
442 goto interpose;
443 dput(lower_dentry);
444 rc = ecryptfs_encrypt_and_encode_filename(
445 &encrypted_and_encoded_name, &encrypted_and_encoded_name_size,
446 NULL, mount_crypt_stat, ecryptfs_dentry->d_name.name,
447 ecryptfs_dentry->d_name.len);
448 if (rc) {
449 printk(KERN_ERR "%s: Error attempting to encrypt and encode "
450 "filename; rc = [%d]\n", __func__, rc);
451 goto out_d_drop;
453 mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
454 lower_dentry = lookup_one_len(encrypted_and_encoded_name,
455 lower_dir_dentry,
456 encrypted_and_encoded_name_size);
457 mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
458 if (IS_ERR(lower_dentry)) {
459 rc = PTR_ERR(lower_dentry);
460 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
461 "[%d] on lower_dentry = [%s]\n", __func__, rc,
462 encrypted_and_encoded_name);
463 goto out_d_drop;
465 interpose:
466 rc = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry,
467 ecryptfs_dir_inode);
468 goto out;
469 out_d_drop:
470 d_drop(ecryptfs_dentry);
471 out:
472 kfree(encrypted_and_encoded_name);
473 return ERR_PTR(rc);
476 static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
477 struct dentry *new_dentry)
479 struct dentry *lower_old_dentry;
480 struct dentry *lower_new_dentry;
481 struct dentry *lower_dir_dentry;
482 u64 file_size_save;
483 int rc;
485 file_size_save = i_size_read(old_dentry->d_inode);
486 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
487 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
488 dget(lower_old_dentry);
489 dget(lower_new_dentry);
490 lower_dir_dentry = lock_parent(lower_new_dentry);
491 rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
492 lower_new_dentry);
493 if (rc || !lower_new_dentry->d_inode)
494 goto out_lock;
495 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
496 if (rc)
497 goto out_lock;
498 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
499 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
500 old_dentry->d_inode->i_nlink =
501 ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
502 i_size_write(new_dentry->d_inode, file_size_save);
503 out_lock:
504 unlock_dir(lower_dir_dentry);
505 dput(lower_new_dentry);
506 dput(lower_old_dentry);
507 return rc;
510 static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
512 int rc = 0;
513 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
514 struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
515 struct dentry *lower_dir_dentry;
517 dget(lower_dentry);
518 lower_dir_dentry = lock_parent(lower_dentry);
519 rc = vfs_unlink(lower_dir_inode, lower_dentry);
520 if (rc) {
521 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
522 goto out_unlock;
524 fsstack_copy_attr_times(dir, lower_dir_inode);
525 dentry->d_inode->i_nlink =
526 ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
527 dentry->d_inode->i_ctime = dir->i_ctime;
528 d_drop(dentry);
529 out_unlock:
530 unlock_dir(lower_dir_dentry);
531 dput(lower_dentry);
532 return rc;
535 static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
536 const char *symname)
538 int rc;
539 struct dentry *lower_dentry;
540 struct dentry *lower_dir_dentry;
541 char *encoded_symname;
542 size_t encoded_symlen;
543 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
545 lower_dentry = ecryptfs_dentry_to_lower(dentry);
546 dget(lower_dentry);
547 lower_dir_dentry = lock_parent(lower_dentry);
548 mount_crypt_stat = &ecryptfs_superblock_to_private(
549 dir->i_sb)->mount_crypt_stat;
550 rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
551 &encoded_symlen,
552 NULL,
553 mount_crypt_stat, symname,
554 strlen(symname));
555 if (rc)
556 goto out_lock;
557 rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
558 encoded_symname);
559 kfree(encoded_symname);
560 if (rc || !lower_dentry->d_inode)
561 goto out_lock;
562 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
563 if (rc)
564 goto out_lock;
565 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
566 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
567 out_lock:
568 unlock_dir(lower_dir_dentry);
569 dput(lower_dentry);
570 if (!dentry->d_inode)
571 d_drop(dentry);
572 return rc;
575 static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
577 int rc;
578 struct dentry *lower_dentry;
579 struct dentry *lower_dir_dentry;
581 lower_dentry = ecryptfs_dentry_to_lower(dentry);
582 lower_dir_dentry = lock_parent(lower_dentry);
583 rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
584 if (rc || !lower_dentry->d_inode)
585 goto out;
586 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
587 if (rc)
588 goto out;
589 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
590 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
591 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
592 out:
593 unlock_dir(lower_dir_dentry);
594 if (!dentry->d_inode)
595 d_drop(dentry);
596 return rc;
599 static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
601 struct dentry *lower_dentry;
602 struct dentry *lower_dir_dentry;
603 int rc;
605 lower_dentry = ecryptfs_dentry_to_lower(dentry);
606 dget(dentry);
607 lower_dir_dentry = lock_parent(lower_dentry);
608 dget(lower_dentry);
609 rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
610 dput(lower_dentry);
611 if (!rc && dentry->d_inode)
612 clear_nlink(dentry->d_inode);
613 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
614 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
615 unlock_dir(lower_dir_dentry);
616 if (!rc)
617 d_drop(dentry);
618 dput(dentry);
619 return rc;
622 static int
623 ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
625 int rc;
626 struct dentry *lower_dentry;
627 struct dentry *lower_dir_dentry;
629 lower_dentry = ecryptfs_dentry_to_lower(dentry);
630 lower_dir_dentry = lock_parent(lower_dentry);
631 rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
632 if (rc || !lower_dentry->d_inode)
633 goto out;
634 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
635 if (rc)
636 goto out;
637 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
638 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
639 out:
640 unlock_dir(lower_dir_dentry);
641 if (!dentry->d_inode)
642 d_drop(dentry);
643 return rc;
646 static int
647 ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
648 struct inode *new_dir, struct dentry *new_dentry)
650 int rc;
651 struct dentry *lower_old_dentry;
652 struct dentry *lower_new_dentry;
653 struct dentry *lower_old_dir_dentry;
654 struct dentry *lower_new_dir_dentry;
655 struct dentry *trap = NULL;
657 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
658 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
659 dget(lower_old_dentry);
660 dget(lower_new_dentry);
661 lower_old_dir_dentry = dget_parent(lower_old_dentry);
662 lower_new_dir_dentry = dget_parent(lower_new_dentry);
663 trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
664 /* source should not be ancestor of target */
665 if (trap == lower_old_dentry) {
666 rc = -EINVAL;
667 goto out_lock;
669 /* target should not be ancestor of source */
670 if (trap == lower_new_dentry) {
671 rc = -ENOTEMPTY;
672 goto out_lock;
674 rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
675 lower_new_dir_dentry->d_inode, lower_new_dentry);
676 if (rc)
677 goto out_lock;
678 fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
679 if (new_dir != old_dir)
680 fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
681 out_lock:
682 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
683 dput(lower_new_dir_dentry);
684 dput(lower_old_dir_dentry);
685 dput(lower_new_dentry);
686 dput(lower_old_dentry);
687 return rc;
690 static int ecryptfs_readlink_lower(struct dentry *dentry, char **buf,
691 size_t *bufsiz)
693 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
694 char *lower_buf;
695 size_t lower_bufsiz = PATH_MAX;
696 mm_segment_t old_fs;
697 int rc;
699 lower_buf = kmalloc(lower_bufsiz, GFP_KERNEL);
700 if (!lower_buf) {
701 rc = -ENOMEM;
702 goto out;
704 old_fs = get_fs();
705 set_fs(get_ds());
706 rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
707 (char __user *)lower_buf,
708 lower_bufsiz);
709 set_fs(old_fs);
710 if (rc < 0)
711 goto out;
712 lower_bufsiz = rc;
713 rc = ecryptfs_decode_and_decrypt_filename(buf, bufsiz, dentry,
714 lower_buf, lower_bufsiz);
715 out:
716 kfree(lower_buf);
717 return rc;
720 static int
721 ecryptfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
723 char *kbuf;
724 size_t kbufsiz, copied;
725 int rc;
727 rc = ecryptfs_readlink_lower(dentry, &kbuf, &kbufsiz);
728 if (rc)
729 goto out;
730 copied = min_t(size_t, bufsiz, kbufsiz);
731 rc = copy_to_user(buf, kbuf, copied) ? -EFAULT : copied;
732 kfree(kbuf);
733 fsstack_copy_attr_atime(dentry->d_inode,
734 ecryptfs_dentry_to_lower(dentry)->d_inode);
735 out:
736 return rc;
739 static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
741 char *buf;
742 int len = PAGE_SIZE, rc;
743 mm_segment_t old_fs;
745 /* Released in ecryptfs_put_link(); only release here on error */
746 buf = kmalloc(len, GFP_KERNEL);
747 if (!buf) {
748 buf = ERR_PTR(-ENOMEM);
749 goto out;
751 old_fs = get_fs();
752 set_fs(get_ds());
753 rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
754 set_fs(old_fs);
755 if (rc < 0) {
756 kfree(buf);
757 buf = ERR_PTR(rc);
758 } else
759 buf[rc] = '\0';
760 out:
761 nd_set_link(nd, buf);
762 return NULL;
765 static void
766 ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
768 char *buf = nd_get_link(nd);
769 if (!IS_ERR(buf)) {
770 /* Free the char* */
771 kfree(buf);
776 * upper_size_to_lower_size
777 * @crypt_stat: Crypt_stat associated with file
778 * @upper_size: Size of the upper file
780 * Calculate the required size of the lower file based on the
781 * specified size of the upper file. This calculation is based on the
782 * number of headers in the underlying file and the extent size.
784 * Returns Calculated size of the lower file.
786 static loff_t
787 upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
788 loff_t upper_size)
790 loff_t lower_size;
792 lower_size = ecryptfs_lower_header_size(crypt_stat);
793 if (upper_size != 0) {
794 loff_t num_extents;
796 num_extents = upper_size >> crypt_stat->extent_shift;
797 if (upper_size & ~crypt_stat->extent_mask)
798 num_extents++;
799 lower_size += (num_extents * crypt_stat->extent_size);
801 return lower_size;
805 * truncate_upper
806 * @dentry: The ecryptfs layer dentry
807 * @ia: Address of the ecryptfs inode's attributes
808 * @lower_ia: Address of the lower inode's attributes
810 * Function to handle truncations modifying the size of the file. Note
811 * that the file sizes are interpolated. When expanding, we are simply
812 * writing strings of 0's out. When truncating, we truncate the upper
813 * inode and update the lower_ia according to the page index
814 * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
815 * the caller must use lower_ia in a call to notify_change() to perform
816 * the truncation of the lower inode.
818 * Returns zero on success; non-zero otherwise
820 static int truncate_upper(struct dentry *dentry, struct iattr *ia,
821 struct iattr *lower_ia)
823 int rc = 0;
824 struct inode *inode = dentry->d_inode;
825 struct ecryptfs_crypt_stat *crypt_stat;
826 loff_t i_size = i_size_read(inode);
827 loff_t lower_size_before_truncate;
828 loff_t lower_size_after_truncate;
830 if (unlikely((ia->ia_size == i_size))) {
831 lower_ia->ia_valid &= ~ATTR_SIZE;
832 return 0;
834 rc = ecryptfs_get_lower_file(dentry, inode);
835 if (rc)
836 return rc;
837 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
838 /* Switch on growing or shrinking file */
839 if (ia->ia_size > i_size) {
840 char zero[] = { 0x00 };
842 lower_ia->ia_valid &= ~ATTR_SIZE;
843 /* Write a single 0 at the last position of the file;
844 * this triggers code that will fill in 0's throughout
845 * the intermediate portion of the previous end of the
846 * file and the new and of the file */
847 rc = ecryptfs_write(inode, zero,
848 (ia->ia_size - 1), 1);
849 } else { /* ia->ia_size < i_size_read(inode) */
850 /* We're chopping off all the pages down to the page
851 * in which ia->ia_size is located. Fill in the end of
852 * that page from (ia->ia_size & ~PAGE_CACHE_MASK) to
853 * PAGE_CACHE_SIZE with zeros. */
854 size_t num_zeros = (PAGE_CACHE_SIZE
855 - (ia->ia_size & ~PAGE_CACHE_MASK));
857 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
858 truncate_setsize(inode, ia->ia_size);
859 lower_ia->ia_size = ia->ia_size;
860 lower_ia->ia_valid |= ATTR_SIZE;
861 goto out;
863 if (num_zeros) {
864 char *zeros_virt;
866 zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
867 if (!zeros_virt) {
868 rc = -ENOMEM;
869 goto out;
871 rc = ecryptfs_write(inode, zeros_virt,
872 ia->ia_size, num_zeros);
873 kfree(zeros_virt);
874 if (rc) {
875 printk(KERN_ERR "Error attempting to zero out "
876 "the remainder of the end page on "
877 "reducing truncate; rc = [%d]\n", rc);
878 goto out;
881 truncate_setsize(inode, ia->ia_size);
882 rc = ecryptfs_write_inode_size_to_metadata(inode);
883 if (rc) {
884 printk(KERN_ERR "Problem with "
885 "ecryptfs_write_inode_size_to_metadata; "
886 "rc = [%d]\n", rc);
887 goto out;
889 /* We are reducing the size of the ecryptfs file, and need to
890 * know if we need to reduce the size of the lower file. */
891 lower_size_before_truncate =
892 upper_size_to_lower_size(crypt_stat, i_size);
893 lower_size_after_truncate =
894 upper_size_to_lower_size(crypt_stat, ia->ia_size);
895 if (lower_size_after_truncate < lower_size_before_truncate) {
896 lower_ia->ia_size = lower_size_after_truncate;
897 lower_ia->ia_valid |= ATTR_SIZE;
898 } else
899 lower_ia->ia_valid &= ~ATTR_SIZE;
901 out:
902 ecryptfs_put_lower_file(inode);
903 return rc;
906 static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
908 struct ecryptfs_crypt_stat *crypt_stat;
909 loff_t lower_oldsize, lower_newsize;
911 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
912 lower_oldsize = upper_size_to_lower_size(crypt_stat,
913 i_size_read(inode));
914 lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
915 if (lower_newsize > lower_oldsize) {
917 * The eCryptfs inode and the new *lower* size are mixed here
918 * because we may not have the lower i_mutex held and/or it may
919 * not be appropriate to call inode_newsize_ok() with inodes
920 * from other filesystems.
922 return inode_newsize_ok(inode, lower_newsize);
925 return 0;
929 * ecryptfs_truncate
930 * @dentry: The ecryptfs layer dentry
931 * @new_length: The length to expand the file to
933 * Simple function that handles the truncation of an eCryptfs inode and
934 * its corresponding lower inode.
936 * Returns zero on success; non-zero otherwise
938 int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
940 struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
941 struct iattr lower_ia = { .ia_valid = 0 };
942 int rc;
944 rc = ecryptfs_inode_newsize_ok(dentry->d_inode, new_length);
945 if (rc)
946 return rc;
948 rc = truncate_upper(dentry, &ia, &lower_ia);
949 if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
950 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
952 mutex_lock(&lower_dentry->d_inode->i_mutex);
953 rc = notify_change(lower_dentry, &lower_ia);
954 mutex_unlock(&lower_dentry->d_inode->i_mutex);
956 return rc;
959 static int
960 ecryptfs_permission(struct inode *inode, int mask, unsigned int flags)
962 if (flags & IPERM_FLAG_RCU)
963 return -ECHILD;
964 return inode_permission(ecryptfs_inode_to_lower(inode), mask);
968 * ecryptfs_setattr
969 * @dentry: dentry handle to the inode to modify
970 * @ia: Structure with flags of what to change and values
972 * Updates the metadata of an inode. If the update is to the size
973 * i.e. truncation, then ecryptfs_truncate will handle the size modification
974 * of both the ecryptfs inode and the lower inode.
976 * All other metadata changes will be passed right to the lower filesystem,
977 * and we will just update our inode to look like the lower.
979 static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
981 int rc = 0;
982 struct dentry *lower_dentry;
983 struct iattr lower_ia;
984 struct inode *inode;
985 struct inode *lower_inode;
986 struct ecryptfs_crypt_stat *crypt_stat;
988 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
989 if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
990 ecryptfs_init_crypt_stat(crypt_stat);
991 inode = dentry->d_inode;
992 lower_inode = ecryptfs_inode_to_lower(inode);
993 lower_dentry = ecryptfs_dentry_to_lower(dentry);
994 mutex_lock(&crypt_stat->cs_mutex);
995 if (S_ISDIR(dentry->d_inode->i_mode))
996 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
997 else if (S_ISREG(dentry->d_inode->i_mode)
998 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
999 || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
1000 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
1002 mount_crypt_stat = &ecryptfs_superblock_to_private(
1003 dentry->d_sb)->mount_crypt_stat;
1004 rc = ecryptfs_get_lower_file(dentry, inode);
1005 if (rc) {
1006 mutex_unlock(&crypt_stat->cs_mutex);
1007 goto out;
1009 rc = ecryptfs_read_metadata(dentry);
1010 ecryptfs_put_lower_file(inode);
1011 if (rc) {
1012 if (!(mount_crypt_stat->flags
1013 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
1014 rc = -EIO;
1015 printk(KERN_WARNING "Either the lower file "
1016 "is not in a valid eCryptfs format, "
1017 "or the key could not be retrieved. "
1018 "Plaintext passthrough mode is not "
1019 "enabled; returning -EIO\n");
1020 mutex_unlock(&crypt_stat->cs_mutex);
1021 goto out;
1023 rc = 0;
1024 crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
1025 | ECRYPTFS_ENCRYPTED);
1028 mutex_unlock(&crypt_stat->cs_mutex);
1030 rc = inode_change_ok(inode, ia);
1031 if (rc)
1032 goto out;
1033 if (ia->ia_valid & ATTR_SIZE) {
1034 rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
1035 if (rc)
1036 goto out;
1039 if (S_ISREG(inode->i_mode)) {
1040 rc = filemap_write_and_wait(inode->i_mapping);
1041 if (rc)
1042 goto out;
1043 fsstack_copy_attr_all(inode, lower_inode);
1045 memcpy(&lower_ia, ia, sizeof(lower_ia));
1046 if (ia->ia_valid & ATTR_FILE)
1047 lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
1048 if (ia->ia_valid & ATTR_SIZE) {
1049 rc = truncate_upper(dentry, ia, &lower_ia);
1050 if (rc < 0)
1051 goto out;
1055 * mode change is for clearing setuid/setgid bits. Allow lower fs
1056 * to interpret this in its own way.
1058 if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
1059 lower_ia.ia_valid &= ~ATTR_MODE;
1061 mutex_lock(&lower_dentry->d_inode->i_mutex);
1062 rc = notify_change(lower_dentry, &lower_ia);
1063 mutex_unlock(&lower_dentry->d_inode->i_mutex);
1064 out:
1065 fsstack_copy_attr_all(inode, lower_inode);
1066 return rc;
1069 int ecryptfs_getattr_link(struct vfsmount *mnt, struct dentry *dentry,
1070 struct kstat *stat)
1072 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
1073 int rc = 0;
1075 mount_crypt_stat = &ecryptfs_superblock_to_private(
1076 dentry->d_sb)->mount_crypt_stat;
1077 generic_fillattr(dentry->d_inode, stat);
1078 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
1079 char *target;
1080 size_t targetsiz;
1082 rc = ecryptfs_readlink_lower(dentry, &target, &targetsiz);
1083 if (!rc) {
1084 kfree(target);
1085 stat->size = targetsiz;
1088 return rc;
1091 int ecryptfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1092 struct kstat *stat)
1094 struct kstat lower_stat;
1095 int rc;
1097 rc = vfs_getattr(ecryptfs_dentry_to_lower_mnt(dentry),
1098 ecryptfs_dentry_to_lower(dentry), &lower_stat);
1099 if (!rc) {
1100 fsstack_copy_attr_all(dentry->d_inode,
1101 ecryptfs_inode_to_lower(dentry->d_inode));
1102 generic_fillattr(dentry->d_inode, stat);
1103 stat->blocks = lower_stat.blocks;
1105 return rc;
1109 ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
1110 size_t size, int flags)
1112 int rc = 0;
1113 struct dentry *lower_dentry;
1115 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1116 if (!lower_dentry->d_inode->i_op->setxattr) {
1117 rc = -EOPNOTSUPP;
1118 goto out;
1121 rc = vfs_setxattr(lower_dentry, name, value, size, flags);
1122 out:
1123 return rc;
1126 ssize_t
1127 ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
1128 void *value, size_t size)
1130 int rc = 0;
1132 if (!lower_dentry->d_inode->i_op->getxattr) {
1133 rc = -EOPNOTSUPP;
1134 goto out;
1136 mutex_lock(&lower_dentry->d_inode->i_mutex);
1137 rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
1138 size);
1139 mutex_unlock(&lower_dentry->d_inode->i_mutex);
1140 out:
1141 return rc;
1144 static ssize_t
1145 ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
1146 size_t size)
1148 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
1149 value, size);
1152 static ssize_t
1153 ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
1155 int rc = 0;
1156 struct dentry *lower_dentry;
1158 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1159 if (!lower_dentry->d_inode->i_op->listxattr) {
1160 rc = -EOPNOTSUPP;
1161 goto out;
1163 mutex_lock(&lower_dentry->d_inode->i_mutex);
1164 rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
1165 mutex_unlock(&lower_dentry->d_inode->i_mutex);
1166 out:
1167 return rc;
1170 static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
1172 int rc = 0;
1173 struct dentry *lower_dentry;
1175 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1176 if (!lower_dentry->d_inode->i_op->removexattr) {
1177 rc = -EOPNOTSUPP;
1178 goto out;
1180 mutex_lock(&lower_dentry->d_inode->i_mutex);
1181 rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
1182 mutex_unlock(&lower_dentry->d_inode->i_mutex);
1183 out:
1184 return rc;
1187 const struct inode_operations ecryptfs_symlink_iops = {
1188 .readlink = ecryptfs_readlink,
1189 .follow_link = ecryptfs_follow_link,
1190 .put_link = ecryptfs_put_link,
1191 .permission = ecryptfs_permission,
1192 .setattr = ecryptfs_setattr,
1193 .getattr = ecryptfs_getattr_link,
1194 .setxattr = ecryptfs_setxattr,
1195 .getxattr = ecryptfs_getxattr,
1196 .listxattr = ecryptfs_listxattr,
1197 .removexattr = ecryptfs_removexattr
1200 const struct inode_operations ecryptfs_dir_iops = {
1201 .create = ecryptfs_create,
1202 .lookup = ecryptfs_lookup,
1203 .link = ecryptfs_link,
1204 .unlink = ecryptfs_unlink,
1205 .symlink = ecryptfs_symlink,
1206 .mkdir = ecryptfs_mkdir,
1207 .rmdir = ecryptfs_rmdir,
1208 .mknod = ecryptfs_mknod,
1209 .rename = ecryptfs_rename,
1210 .permission = ecryptfs_permission,
1211 .setattr = ecryptfs_setattr,
1212 .setxattr = ecryptfs_setxattr,
1213 .getxattr = ecryptfs_getxattr,
1214 .listxattr = ecryptfs_listxattr,
1215 .removexattr = ecryptfs_removexattr
1218 const struct inode_operations ecryptfs_main_iops = {
1219 .permission = ecryptfs_permission,
1220 .setattr = ecryptfs_setattr,
1221 .getattr = ecryptfs_getattr,
1222 .setxattr = ecryptfs_setxattr,
1223 .getxattr = ecryptfs_getxattr,
1224 .listxattr = ecryptfs_listxattr,
1225 .removexattr = ecryptfs_removexattr