7315547193ea7e832f9de89f29d6545a1ec720ee
[pandora-kernel.git] / fs / ecryptfs / inode.c
1 /**
2  * eCryptfs: Linux filesystem encryption layer
3  *
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>
9  *
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.
14  *
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.
19  *
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.
24  */
25
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 <asm/unaligned.h>
35 #include "ecryptfs_kernel.h"
36
37 static struct dentry *lock_parent(struct dentry *dentry)
38 {
39         struct dentry *dir;
40
41         dir = dget_parent(dentry);
42         mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT);
43         return dir;
44 }
45
46 static void unlock_dir(struct dentry *dir)
47 {
48         mutex_unlock(&dir->d_inode->i_mutex);
49         dput(dir);
50 }
51
52 /**
53  * ecryptfs_create_underlying_file
54  * @lower_dir_inode: inode of the parent in the lower fs of the new file
55  * @lower_dentry: New file's dentry in the lower fs
56  * @ecryptfs_dentry: New file's dentry in ecryptfs
57  * @mode: The mode of the new file
58  * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
59  *
60  * Creates the file in the lower file system.
61  *
62  * Returns zero on success; non-zero on error condition
63  */
64 static int
65 ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
66                                 struct dentry *dentry, int mode,
67                                 struct nameidata *nd)
68 {
69         struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
70         struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
71         struct dentry *dentry_save;
72         struct vfsmount *vfsmount_save;
73         int rc;
74
75         dentry_save = nd->path.dentry;
76         vfsmount_save = nd->path.mnt;
77         nd->path.dentry = lower_dentry;
78         nd->path.mnt = lower_mnt;
79         rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
80         nd->path.dentry = dentry_save;
81         nd->path.mnt = vfsmount_save;
82         return rc;
83 }
84
85 /**
86  * ecryptfs_do_create
87  * @directory_inode: inode of the new file's dentry's parent in ecryptfs
88  * @ecryptfs_dentry: New file's dentry in ecryptfs
89  * @mode: The mode of the new file
90  * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
91  *
92  * Creates the underlying file and the eCryptfs inode which will link to
93  * it. It will also update the eCryptfs directory inode to mimic the
94  * stat of the lower directory inode.
95  *
96  * Returns zero on success; non-zero on error condition
97  */
98 static int
99 ecryptfs_do_create(struct inode *directory_inode,
100                    struct dentry *ecryptfs_dentry, int mode,
101                    struct nameidata *nd)
102 {
103         int rc;
104         struct dentry *lower_dentry;
105         struct dentry *lower_dir_dentry;
106
107         lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
108         lower_dir_dentry = lock_parent(lower_dentry);
109         if (IS_ERR(lower_dir_dentry)) {
110                 ecryptfs_printk(KERN_ERR, "Error locking directory of "
111                                 "dentry\n");
112                 rc = PTR_ERR(lower_dir_dentry);
113                 goto out;
114         }
115         rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
116                                              ecryptfs_dentry, mode, nd);
117         if (rc) {
118                 printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
119                        "rc = [%d]\n", __func__, rc);
120                 goto out_lock;
121         }
122         rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
123                                 directory_inode->i_sb, 0);
124         if (rc) {
125                 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
126                 goto out_lock;
127         }
128         fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
129         fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
130 out_lock:
131         unlock_dir(lower_dir_dentry);
132 out:
133         return rc;
134 }
135
136 /**
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  */
142 static int grow_file(struct dentry *ecryptfs_dentry)
143 {
144         struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
145         struct file fake_file;
146         struct ecryptfs_file_info tmp_file_info;
147         char zero_virt[] = { 0x00 };
148         int rc = 0;
149
150         memset(&fake_file, 0, sizeof(fake_file));
151         fake_file.f_path.dentry = ecryptfs_dentry;
152         memset(&tmp_file_info, 0, sizeof(tmp_file_info));
153         ecryptfs_set_file_private(&fake_file, &tmp_file_info);
154         ecryptfs_set_file_lower(
155                 &fake_file,
156                 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file);
157         rc = ecryptfs_write(&fake_file, zero_virt, 0, 1);
158         i_size_write(ecryptfs_inode, 0);
159         rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
160         ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat.flags |=
161                 ECRYPTFS_NEW_FILE;
162         return rc;
163 }
164
165 /**
166  * ecryptfs_initialize_file
167  *
168  * Cause the file to be changed from a basic empty file to an ecryptfs
169  * file with a header and first data page.
170  *
171  * Returns zero on success
172  */
173 static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
174 {
175         struct ecryptfs_crypt_stat *crypt_stat =
176                 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
177         int rc = 0;
178
179         if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
180                 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
181                 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
182                 goto out;
183         }
184         crypt_stat->flags |= ECRYPTFS_NEW_FILE;
185         ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
186         rc = ecryptfs_new_file_context(ecryptfs_dentry);
187         if (rc) {
188                 ecryptfs_printk(KERN_ERR, "Error creating new file "
189                                 "context; rc = [%d]\n", rc);
190                 goto out;
191         }
192         rc = ecryptfs_write_metadata(ecryptfs_dentry);
193         if (rc) {
194                 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
195                 goto out;
196         }
197         rc = grow_file(ecryptfs_dentry);
198         if (rc)
199                 printk(KERN_ERR "Error growing file; rc = [%d]\n", rc);
200 out:
201         return rc;
202 }
203
204 /**
205  * ecryptfs_create
206  * @dir: The inode of the directory in which to create the file.
207  * @dentry: The eCryptfs dentry
208  * @mode: The mode of the new file.
209  * @nd: nameidata
210  *
211  * Creates a new file.
212  *
213  * Returns zero on success; non-zero on error condition
214  */
215 static int
216 ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
217                 int mode, struct nameidata *nd)
218 {
219         int rc;
220
221         /* ecryptfs_do_create() calls ecryptfs_interpose(), which opens
222          * the crypt_stat->lower_file (persistent file) */
223         rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
224         if (unlikely(rc)) {
225                 ecryptfs_printk(KERN_WARNING, "Failed to create file in"
226                                 "lower filesystem\n");
227                 goto out;
228         }
229         /* At this point, a file exists on "disk"; we need to make sure
230          * that this on disk file is prepared to be an ecryptfs file */
231         rc = ecryptfs_initialize_file(ecryptfs_dentry);
232 out:
233         return rc;
234 }
235
236 /**
237  * ecryptfs_lookup
238  * @dir: inode
239  * @dentry: The dentry
240  * @nd: nameidata, may be NULL
241  *
242  * Find a file on disk. If the file does not exist, then we'll add it to the
243  * dentry cache and continue on to read it from the disk.
244  */
245 static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
246                                       struct nameidata *nd)
247 {
248         int rc = 0;
249         struct dentry *lower_dir_dentry;
250         struct dentry *lower_dentry;
251         struct vfsmount *lower_mnt;
252         char *encoded_name;
253         int encoded_namelen;
254         struct ecryptfs_crypt_stat *crypt_stat = NULL;
255         struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
256         char *page_virt = NULL;
257         struct inode *lower_inode;
258         u64 file_size;
259
260         lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
261         dentry->d_op = &ecryptfs_dops;
262         if ((dentry->d_name.len == 1 && !strcmp(dentry->d_name.name, "."))
263             || (dentry->d_name.len == 2
264                 && !strcmp(dentry->d_name.name, ".."))) {
265                 d_drop(dentry);
266                 goto out;
267         }
268         encoded_namelen = ecryptfs_encode_filename(crypt_stat,
269                                                    dentry->d_name.name,
270                                                    dentry->d_name.len,
271                                                    &encoded_name);
272         if (encoded_namelen < 0) {
273                 rc = encoded_namelen;
274                 d_drop(dentry);
275                 goto out;
276         }
277         ecryptfs_printk(KERN_DEBUG, "encoded_name = [%s]; encoded_namelen "
278                         "= [%d]\n", encoded_name, encoded_namelen);
279         lower_dentry = lookup_one_len(encoded_name, lower_dir_dentry,
280                                       encoded_namelen - 1);
281         kfree(encoded_name);
282         if (IS_ERR(lower_dentry)) {
283                 ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
284                 rc = PTR_ERR(lower_dentry);
285                 d_drop(dentry);
286                 goto out;
287         }
288         lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
289         ecryptfs_printk(KERN_DEBUG, "lower_dentry = [%p]; lower_dentry->"
290                 "d_name.name = [%s]\n", lower_dentry,
291                 lower_dentry->d_name.name);
292         lower_inode = lower_dentry->d_inode;
293         fsstack_copy_attr_atime(dir, lower_dir_dentry->d_inode);
294         BUG_ON(!atomic_read(&lower_dentry->d_count));
295         ecryptfs_set_dentry_private(dentry,
296                                     kmem_cache_alloc(ecryptfs_dentry_info_cache,
297                                                      GFP_KERNEL));
298         if (!ecryptfs_dentry_to_private(dentry)) {
299                 rc = -ENOMEM;
300                 ecryptfs_printk(KERN_ERR, "Out of memory whilst attempting "
301                                 "to allocate ecryptfs_dentry_info struct\n");
302                 goto out_dput;
303         }
304         ecryptfs_set_dentry_lower(dentry, lower_dentry);
305         ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
306         if (!lower_dentry->d_inode) {
307                 /* We want to add because we couldn't find in lower */
308                 d_add(dentry, NULL);
309                 goto out;
310         }
311         rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 1);
312         if (rc) {
313                 ecryptfs_printk(KERN_ERR, "Error interposing\n");
314                 goto out_dput;
315         }
316         if (S_ISDIR(lower_inode->i_mode)) {
317                 ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n");
318                 goto out;
319         }
320         if (S_ISLNK(lower_inode->i_mode)) {
321                 ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n");
322                 goto out;
323         }
324         if (special_file(lower_inode->i_mode)) {
325                 ecryptfs_printk(KERN_DEBUG, "Is a special file; returning\n");
326                 goto out;
327         }
328         if (!nd) {
329                 ecryptfs_printk(KERN_DEBUG, "We have a NULL nd, just leave"
330                                 "as we *think* we are about to unlink\n");
331                 goto out;
332         }
333         /* Released in this function */
334         page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2,
335                                       GFP_USER);
336         if (!page_virt) {
337                 rc = -ENOMEM;
338                 ecryptfs_printk(KERN_ERR,
339                                 "Cannot ecryptfs_kmalloc a page\n");
340                 goto out_dput;
341         }
342         crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
343         if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
344                 ecryptfs_set_default_sizes(crypt_stat);
345         rc = ecryptfs_read_and_validate_header_region(page_virt,
346                                                       dentry->d_inode);
347         if (rc) {
348                 rc = ecryptfs_read_and_validate_xattr_region(page_virt, dentry);
349                 if (rc) {
350                         printk(KERN_DEBUG "Valid metadata not found in header "
351                                "region or xattr region; treating file as "
352                                "unencrypted\n");
353                         rc = 0;
354                         kmem_cache_free(ecryptfs_header_cache_2, page_virt);
355                         goto out;
356                 }
357                 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
358         }
359         mount_crypt_stat = &ecryptfs_superblock_to_private(
360                 dentry->d_sb)->mount_crypt_stat;
361         if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
362                 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
363                         file_size = (crypt_stat->num_header_bytes_at_front
364                                      + i_size_read(lower_dentry->d_inode));
365                 else
366                         file_size = i_size_read(lower_dentry->d_inode);
367         } else {
368                 file_size = get_unaligned_be64(page_virt);
369         }
370         i_size_write(dentry->d_inode, (loff_t)file_size);
371         kmem_cache_free(ecryptfs_header_cache_2, page_virt);
372         goto out;
373
374 out_dput:
375         dput(lower_dentry);
376         d_drop(dentry);
377 out:
378         return ERR_PTR(rc);
379 }
380
381 static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
382                          struct dentry *new_dentry)
383 {
384         struct dentry *lower_old_dentry;
385         struct dentry *lower_new_dentry;
386         struct dentry *lower_dir_dentry;
387         u64 file_size_save;
388         int rc;
389
390         file_size_save = i_size_read(old_dentry->d_inode);
391         lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
392         lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
393         dget(lower_old_dentry);
394         dget(lower_new_dentry);
395         lower_dir_dentry = lock_parent(lower_new_dentry);
396         rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
397                       lower_new_dentry);
398         if (rc || !lower_new_dentry->d_inode)
399                 goto out_lock;
400         rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
401         if (rc)
402                 goto out_lock;
403         fsstack_copy_attr_times(dir, lower_new_dentry->d_inode);
404         fsstack_copy_inode_size(dir, lower_new_dentry->d_inode);
405         old_dentry->d_inode->i_nlink =
406                 ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
407         i_size_write(new_dentry->d_inode, file_size_save);
408 out_lock:
409         unlock_dir(lower_dir_dentry);
410         dput(lower_new_dentry);
411         dput(lower_old_dentry);
412         d_drop(lower_old_dentry);
413         d_drop(new_dentry);
414         d_drop(old_dentry);
415         return rc;
416 }
417
418 static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
419 {
420         int rc = 0;
421         struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
422         struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
423         struct dentry *lower_dir_dentry;
424
425         lower_dir_dentry = lock_parent(lower_dentry);
426         rc = vfs_unlink(lower_dir_inode, lower_dentry);
427         if (rc) {
428                 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
429                 goto out_unlock;
430         }
431         fsstack_copy_attr_times(dir, lower_dir_inode);
432         dentry->d_inode->i_nlink =
433                 ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
434         dentry->d_inode->i_ctime = dir->i_ctime;
435         d_drop(dentry);
436 out_unlock:
437         unlock_dir(lower_dir_dentry);
438         return rc;
439 }
440
441 static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
442                             const char *symname)
443 {
444         int rc;
445         struct dentry *lower_dentry;
446         struct dentry *lower_dir_dentry;
447         umode_t mode;
448         char *encoded_symname;
449         int encoded_symlen;
450         struct ecryptfs_crypt_stat *crypt_stat = NULL;
451
452         lower_dentry = ecryptfs_dentry_to_lower(dentry);
453         dget(lower_dentry);
454         lower_dir_dentry = lock_parent(lower_dentry);
455         mode = S_IALLUGO;
456         encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
457                                                   strlen(symname),
458                                                   &encoded_symname);
459         if (encoded_symlen < 0) {
460                 rc = encoded_symlen;
461                 goto out_lock;
462         }
463         rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
464                          encoded_symname, mode);
465         kfree(encoded_symname);
466         if (rc || !lower_dentry->d_inode)
467                 goto out_lock;
468         rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
469         if (rc)
470                 goto out_lock;
471         fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
472         fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
473 out_lock:
474         unlock_dir(lower_dir_dentry);
475         dput(lower_dentry);
476         if (!dentry->d_inode)
477                 d_drop(dentry);
478         return rc;
479 }
480
481 static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
482 {
483         int rc;
484         struct dentry *lower_dentry;
485         struct dentry *lower_dir_dentry;
486
487         lower_dentry = ecryptfs_dentry_to_lower(dentry);
488         lower_dir_dentry = lock_parent(lower_dentry);
489         rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
490         if (rc || !lower_dentry->d_inode)
491                 goto out;
492         rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
493         if (rc)
494                 goto out;
495         fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
496         fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
497         dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
498 out:
499         unlock_dir(lower_dir_dentry);
500         if (!dentry->d_inode)
501                 d_drop(dentry);
502         return rc;
503 }
504
505 static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
506 {
507         struct dentry *lower_dentry;
508         struct dentry *lower_dir_dentry;
509         int rc;
510
511         lower_dentry = ecryptfs_dentry_to_lower(dentry);
512         dget(dentry);
513         lower_dir_dentry = lock_parent(lower_dentry);
514         dget(lower_dentry);
515         rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
516         dput(lower_dentry);
517         if (!rc)
518                 d_delete(lower_dentry);
519         fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
520         dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
521         unlock_dir(lower_dir_dentry);
522         if (!rc)
523                 d_drop(dentry);
524         dput(dentry);
525         return rc;
526 }
527
528 static int
529 ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
530 {
531         int rc;
532         struct dentry *lower_dentry;
533         struct dentry *lower_dir_dentry;
534
535         lower_dentry = ecryptfs_dentry_to_lower(dentry);
536         lower_dir_dentry = lock_parent(lower_dentry);
537         rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
538         if (rc || !lower_dentry->d_inode)
539                 goto out;
540         rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
541         if (rc)
542                 goto out;
543         fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
544         fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
545 out:
546         unlock_dir(lower_dir_dentry);
547         if (!dentry->d_inode)
548                 d_drop(dentry);
549         return rc;
550 }
551
552 static int
553 ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
554                 struct inode *new_dir, struct dentry *new_dentry)
555 {
556         int rc;
557         struct dentry *lower_old_dentry;
558         struct dentry *lower_new_dentry;
559         struct dentry *lower_old_dir_dentry;
560         struct dentry *lower_new_dir_dentry;
561
562         lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
563         lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
564         dget(lower_old_dentry);
565         dget(lower_new_dentry);
566         lower_old_dir_dentry = dget_parent(lower_old_dentry);
567         lower_new_dir_dentry = dget_parent(lower_new_dentry);
568         lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
569         rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
570                         lower_new_dir_dentry->d_inode, lower_new_dentry);
571         if (rc)
572                 goto out_lock;
573         fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
574         if (new_dir != old_dir)
575                 fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
576 out_lock:
577         unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
578         dput(lower_new_dentry->d_parent);
579         dput(lower_old_dentry->d_parent);
580         dput(lower_new_dentry);
581         dput(lower_old_dentry);
582         return rc;
583 }
584
585 static int
586 ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz)
587 {
588         int rc;
589         struct dentry *lower_dentry;
590         char *decoded_name;
591         char *lower_buf;
592         mm_segment_t old_fs;
593         struct ecryptfs_crypt_stat *crypt_stat;
594
595         lower_dentry = ecryptfs_dentry_to_lower(dentry);
596         if (!lower_dentry->d_inode->i_op ||
597             !lower_dentry->d_inode->i_op->readlink) {
598                 rc = -EINVAL;
599                 goto out;
600         }
601         /* Released in this function */
602         lower_buf = kmalloc(bufsiz, GFP_KERNEL);
603         if (lower_buf == NULL) {
604                 ecryptfs_printk(KERN_ERR, "Out of memory\n");
605                 rc = -ENOMEM;
606                 goto out;
607         }
608         old_fs = get_fs();
609         set_fs(get_ds());
610         ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
611                         "lower_dentry->d_name.name = [%s]\n",
612                         lower_dentry->d_name.name);
613         rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
614                                                    (char __user *)lower_buf,
615                                                    bufsiz);
616         set_fs(old_fs);
617         if (rc >= 0) {
618                 crypt_stat = NULL;
619                 rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc,
620                                               &decoded_name);
621                 if (rc == -ENOMEM)
622                         goto out_free_lower_buf;
623                 if (rc > 0) {
624                         ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes "
625                                         "to userspace: [%*s]\n", rc,
626                                         decoded_name);
627                         if (copy_to_user(buf, decoded_name, rc))
628                                 rc = -EFAULT;
629                 }
630                 kfree(decoded_name);
631                 fsstack_copy_attr_atime(dentry->d_inode,
632                                         lower_dentry->d_inode);
633         }
634 out_free_lower_buf:
635         kfree(lower_buf);
636 out:
637         return rc;
638 }
639
640 static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
641 {
642         char *buf;
643         int len = PAGE_SIZE, rc;
644         mm_segment_t old_fs;
645
646         /* Released in ecryptfs_put_link(); only release here on error */
647         buf = kmalloc(len, GFP_KERNEL);
648         if (!buf) {
649                 rc = -ENOMEM;
650                 goto out;
651         }
652         old_fs = get_fs();
653         set_fs(get_ds());
654         ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
655                         "dentry->d_name.name = [%s]\n", dentry->d_name.name);
656         rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
657         buf[rc] = '\0';
658         set_fs(old_fs);
659         if (rc < 0)
660                 goto out_free;
661         rc = 0;
662         nd_set_link(nd, buf);
663         goto out;
664 out_free:
665         kfree(buf);
666 out:
667         return ERR_PTR(rc);
668 }
669
670 static void
671 ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
672 {
673         /* Free the char* */
674         kfree(nd_get_link(nd));
675 }
676
677 /**
678  * upper_size_to_lower_size
679  * @crypt_stat: Crypt_stat associated with file
680  * @upper_size: Size of the upper file
681  *
682  * Calculate the required size of the lower file based on the
683  * specified size of the upper file. This calculation is based on the
684  * number of headers in the underlying file and the extent size.
685  *
686  * Returns Calculated size of the lower file.
687  */
688 static loff_t
689 upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
690                          loff_t upper_size)
691 {
692         loff_t lower_size;
693
694         lower_size = crypt_stat->num_header_bytes_at_front;
695         if (upper_size != 0) {
696                 loff_t num_extents;
697
698                 num_extents = upper_size >> crypt_stat->extent_shift;
699                 if (upper_size & ~crypt_stat->extent_mask)
700                         num_extents++;
701                 lower_size += (num_extents * crypt_stat->extent_size);
702         }
703         return lower_size;
704 }
705
706 /**
707  * ecryptfs_truncate
708  * @dentry: The ecryptfs layer dentry
709  * @new_length: The length to expand the file to
710  *
711  * Function to handle truncations modifying the size of the file. Note
712  * that the file sizes are interpolated. When expanding, we are simply
713  * writing strings of 0's out. When truncating, we need to modify the
714  * underlying file size according to the page index interpolations.
715  *
716  * Returns zero on success; non-zero otherwise
717  */
718 int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
719 {
720         int rc = 0;
721         struct inode *inode = dentry->d_inode;
722         struct dentry *lower_dentry;
723         struct file fake_ecryptfs_file;
724         struct ecryptfs_crypt_stat *crypt_stat;
725         loff_t i_size = i_size_read(inode);
726         loff_t lower_size_before_truncate;
727         loff_t lower_size_after_truncate;
728
729         if (unlikely((new_length == i_size)))
730                 goto out;
731         crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
732         /* Set up a fake ecryptfs file, this is used to interface with
733          * the file in the underlying filesystem so that the
734          * truncation has an effect there as well. */
735         memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
736         fake_ecryptfs_file.f_path.dentry = dentry;
737         /* Released at out_free: label */
738         ecryptfs_set_file_private(&fake_ecryptfs_file,
739                                   kmem_cache_alloc(ecryptfs_file_info_cache,
740                                                    GFP_KERNEL));
741         if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
742                 rc = -ENOMEM;
743                 goto out;
744         }
745         lower_dentry = ecryptfs_dentry_to_lower(dentry);
746         ecryptfs_set_file_lower(
747                 &fake_ecryptfs_file,
748                 ecryptfs_inode_to_private(dentry->d_inode)->lower_file);
749         /* Switch on growing or shrinking file */
750         if (new_length > i_size) {
751                 char zero[] = { 0x00 };
752
753                 /* Write a single 0 at the last position of the file;
754                  * this triggers code that will fill in 0's throughout
755                  * the intermediate portion of the previous end of the
756                  * file and the new and of the file */
757                 rc = ecryptfs_write(&fake_ecryptfs_file, zero,
758                                     (new_length - 1), 1);
759         } else { /* new_length < i_size_read(inode) */
760                 /* We're chopping off all the pages down do the page
761                  * in which new_length is located. Fill in the end of
762                  * that page from (new_length & ~PAGE_CACHE_MASK) to
763                  * PAGE_CACHE_SIZE with zeros. */
764                 size_t num_zeros = (PAGE_CACHE_SIZE
765                                     - (new_length & ~PAGE_CACHE_MASK));
766
767                 if (num_zeros) {
768                         char *zeros_virt;
769
770                         zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
771                         if (!zeros_virt) {
772                                 rc = -ENOMEM;
773                                 goto out_free;
774                         }
775                         rc = ecryptfs_write(&fake_ecryptfs_file, zeros_virt,
776                                             new_length, num_zeros);
777                         kfree(zeros_virt);
778                         if (rc) {
779                                 printk(KERN_ERR "Error attempting to zero out "
780                                        "the remainder of the end page on "
781                                        "reducing truncate; rc = [%d]\n", rc);
782                                 goto out_free;
783                         }
784                 }
785                 vmtruncate(inode, new_length);
786                 rc = ecryptfs_write_inode_size_to_metadata(inode);
787                 if (rc) {
788                         printk(KERN_ERR "Problem with "
789                                "ecryptfs_write_inode_size_to_metadata; "
790                                "rc = [%d]\n", rc);
791                         goto out_free;
792                 }
793                 /* We are reducing the size of the ecryptfs file, and need to
794                  * know if we need to reduce the size of the lower file. */
795                 lower_size_before_truncate =
796                     upper_size_to_lower_size(crypt_stat, i_size);
797                 lower_size_after_truncate =
798                     upper_size_to_lower_size(crypt_stat, new_length);
799                 if (lower_size_after_truncate < lower_size_before_truncate)
800                         vmtruncate(lower_dentry->d_inode,
801                                    lower_size_after_truncate);
802         }
803 out_free:
804         if (ecryptfs_file_to_private(&fake_ecryptfs_file))
805                 kmem_cache_free(ecryptfs_file_info_cache,
806                                 ecryptfs_file_to_private(&fake_ecryptfs_file));
807 out:
808         return rc;
809 }
810
811 static int
812 ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd)
813 {
814         int rc;
815
816         if (nd) {
817                 struct vfsmount *vfsmnt_save = nd->path.mnt;
818                 struct dentry *dentry_save = nd->path.dentry;
819
820                 nd->path.mnt = ecryptfs_dentry_to_lower_mnt(nd->path.dentry);
821                 nd->path.dentry = ecryptfs_dentry_to_lower(nd->path.dentry);
822                 rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
823                 nd->path.mnt = vfsmnt_save;
824                 nd->path.dentry = dentry_save;
825         } else
826                 rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL);
827         return rc;
828 }
829
830 /**
831  * ecryptfs_setattr
832  * @dentry: dentry handle to the inode to modify
833  * @ia: Structure with flags of what to change and values
834  *
835  * Updates the metadata of an inode. If the update is to the size
836  * i.e. truncation, then ecryptfs_truncate will handle the size modification
837  * of both the ecryptfs inode and the lower inode.
838  *
839  * All other metadata changes will be passed right to the lower filesystem,
840  * and we will just update our inode to look like the lower.
841  */
842 static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
843 {
844         int rc = 0;
845         struct dentry *lower_dentry;
846         struct inode *inode;
847         struct inode *lower_inode;
848         struct ecryptfs_crypt_stat *crypt_stat;
849
850         crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
851         if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
852                 ecryptfs_init_crypt_stat(crypt_stat);
853         inode = dentry->d_inode;
854         lower_inode = ecryptfs_inode_to_lower(inode);
855         lower_dentry = ecryptfs_dentry_to_lower(dentry);
856         mutex_lock(&crypt_stat->cs_mutex);
857         if (S_ISDIR(dentry->d_inode->i_mode))
858                 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
859         else if (S_ISREG(dentry->d_inode->i_mode)
860                  && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
861                      || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
862                 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
863
864                 mount_crypt_stat = &ecryptfs_superblock_to_private(
865                         dentry->d_sb)->mount_crypt_stat;
866                 rc = ecryptfs_read_metadata(dentry);
867                 if (rc) {
868                         if (!(mount_crypt_stat->flags
869                               & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
870                                 rc = -EIO;
871                                 printk(KERN_WARNING "Either the lower file "
872                                        "is not in a valid eCryptfs format, "
873                                        "or the key could not be retrieved. "
874                                        "Plaintext passthrough mode is not "
875                                        "enabled; returning -EIO\n");
876                                 mutex_unlock(&crypt_stat->cs_mutex);
877                                 goto out;
878                         }
879                         rc = 0;
880                         crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
881                         mutex_unlock(&crypt_stat->cs_mutex);
882                         goto out;
883                 }
884         }
885         mutex_unlock(&crypt_stat->cs_mutex);
886         if (ia->ia_valid & ATTR_SIZE) {
887                 ecryptfs_printk(KERN_DEBUG,
888                                 "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
889                                 ia->ia_valid, ATTR_SIZE);
890                 rc = ecryptfs_truncate(dentry, ia->ia_size);
891                 /* ecryptfs_truncate handles resizing of the lower file */
892                 ia->ia_valid &= ~ATTR_SIZE;
893                 ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
894                                 ia->ia_valid);
895                 if (rc < 0)
896                         goto out;
897         }
898
899         /*
900          * mode change is for clearing setuid/setgid bits. Allow lower fs
901          * to interpret this in its own way.
902          */
903         if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
904                 ia->ia_valid &= ~ATTR_MODE;
905
906         mutex_lock(&lower_dentry->d_inode->i_mutex);
907         rc = notify_change(lower_dentry, ia);
908         mutex_unlock(&lower_dentry->d_inode->i_mutex);
909 out:
910         fsstack_copy_attr_all(inode, lower_inode, NULL);
911         return rc;
912 }
913
914 int
915 ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
916                   size_t size, int flags)
917 {
918         int rc = 0;
919         struct dentry *lower_dentry;
920
921         lower_dentry = ecryptfs_dentry_to_lower(dentry);
922         if (!lower_dentry->d_inode->i_op->setxattr) {
923                 rc = -ENOSYS;
924                 goto out;
925         }
926         mutex_lock(&lower_dentry->d_inode->i_mutex);
927         rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
928                                                    size, flags);
929         mutex_unlock(&lower_dentry->d_inode->i_mutex);
930 out:
931         return rc;
932 }
933
934 ssize_t
935 ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
936                         void *value, size_t size)
937 {
938         int rc = 0;
939
940         if (!lower_dentry->d_inode->i_op->getxattr) {
941                 rc = -ENOSYS;
942                 goto out;
943         }
944         mutex_lock(&lower_dentry->d_inode->i_mutex);
945         rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
946                                                    size);
947         mutex_unlock(&lower_dentry->d_inode->i_mutex);
948 out:
949         return rc;
950 }
951
952 static ssize_t
953 ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
954                   size_t size)
955 {
956         return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
957                                        value, size);
958 }
959
960 static ssize_t
961 ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
962 {
963         int rc = 0;
964         struct dentry *lower_dentry;
965
966         lower_dentry = ecryptfs_dentry_to_lower(dentry);
967         if (!lower_dentry->d_inode->i_op->listxattr) {
968                 rc = -ENOSYS;
969                 goto out;
970         }
971         mutex_lock(&lower_dentry->d_inode->i_mutex);
972         rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
973         mutex_unlock(&lower_dentry->d_inode->i_mutex);
974 out:
975         return rc;
976 }
977
978 static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
979 {
980         int rc = 0;
981         struct dentry *lower_dentry;
982
983         lower_dentry = ecryptfs_dentry_to_lower(dentry);
984         if (!lower_dentry->d_inode->i_op->removexattr) {
985                 rc = -ENOSYS;
986                 goto out;
987         }
988         mutex_lock(&lower_dentry->d_inode->i_mutex);
989         rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
990         mutex_unlock(&lower_dentry->d_inode->i_mutex);
991 out:
992         return rc;
993 }
994
995 int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
996 {
997         if ((ecryptfs_inode_to_lower(inode)
998              == (struct inode *)candidate_lower_inode))
999                 return 1;
1000         else
1001                 return 0;
1002 }
1003
1004 int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
1005 {
1006         ecryptfs_init_inode(inode, (struct inode *)lower_inode);
1007         return 0;
1008 }
1009
1010 const struct inode_operations ecryptfs_symlink_iops = {
1011         .readlink = ecryptfs_readlink,
1012         .follow_link = ecryptfs_follow_link,
1013         .put_link = ecryptfs_put_link,
1014         .permission = ecryptfs_permission,
1015         .setattr = ecryptfs_setattr,
1016         .setxattr = ecryptfs_setxattr,
1017         .getxattr = ecryptfs_getxattr,
1018         .listxattr = ecryptfs_listxattr,
1019         .removexattr = ecryptfs_removexattr
1020 };
1021
1022 const struct inode_operations ecryptfs_dir_iops = {
1023         .create = ecryptfs_create,
1024         .lookup = ecryptfs_lookup,
1025         .link = ecryptfs_link,
1026         .unlink = ecryptfs_unlink,
1027         .symlink = ecryptfs_symlink,
1028         .mkdir = ecryptfs_mkdir,
1029         .rmdir = ecryptfs_rmdir,
1030         .mknod = ecryptfs_mknod,
1031         .rename = ecryptfs_rename,
1032         .permission = ecryptfs_permission,
1033         .setattr = ecryptfs_setattr,
1034         .setxattr = ecryptfs_setxattr,
1035         .getxattr = ecryptfs_getxattr,
1036         .listxattr = ecryptfs_listxattr,
1037         .removexattr = ecryptfs_removexattr
1038 };
1039
1040 const struct inode_operations ecryptfs_main_iops = {
1041         .permission = ecryptfs_permission,
1042         .setattr = ecryptfs_setattr,
1043         .setxattr = ecryptfs_setxattr,
1044         .getxattr = ecryptfs_getxattr,
1045         .listxattr = ecryptfs_listxattr,
1046         .removexattr = ecryptfs_removexattr
1047 };