Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6] / 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 "ecryptfs_kernel.h"
35
36 static struct dentry *lock_parent(struct dentry *dentry)
37 {
38         struct dentry *dir;
39
40         dir = dget_parent(dentry);
41         mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT);
42         return dir;
43 }
44
45 static void unlock_dir(struct dentry *dir)
46 {
47         mutex_unlock(&dir->d_inode->i_mutex);
48         dput(dir);
49 }
50
51 /**
52  * ecryptfs_create_underlying_file
53  * @lower_dir_inode: inode of the parent in the lower fs of the new file
54  * @lower_dentry: New file's dentry in the lower fs
55  * @ecryptfs_dentry: New file's dentry in ecryptfs
56  * @mode: The mode of the new file
57  * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
58  *
59  * Creates the file in the lower file system.
60  *
61  * Returns zero on success; non-zero on error condition
62  */
63 static int
64 ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
65                                 struct dentry *dentry, int mode,
66                                 struct nameidata *nd)
67 {
68         struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
69         struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
70         struct dentry *dentry_save;
71         struct vfsmount *vfsmount_save;
72         int rc;
73
74         dentry_save = nd->path.dentry;
75         vfsmount_save = nd->path.mnt;
76         nd->path.dentry = lower_dentry;
77         nd->path.mnt = lower_mnt;
78         rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
79         nd->path.dentry = dentry_save;
80         nd->path.mnt = vfsmount_save;
81         return rc;
82 }
83
84 /**
85  * ecryptfs_do_create
86  * @directory_inode: inode of the new file's dentry's parent in ecryptfs
87  * @ecryptfs_dentry: New file's dentry in ecryptfs
88  * @mode: The mode of the new file
89  * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
90  *
91  * Creates the underlying file and the eCryptfs inode which will link to
92  * it. It will also update the eCryptfs directory inode to mimic the
93  * stat of the lower directory inode.
94  *
95  * Returns zero on success; non-zero on error condition
96  */
97 static int
98 ecryptfs_do_create(struct inode *directory_inode,
99                    struct dentry *ecryptfs_dentry, int mode,
100                    struct nameidata *nd)
101 {
102         int rc;
103         struct dentry *lower_dentry;
104         struct dentry *lower_dir_dentry;
105
106         lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
107         lower_dir_dentry = lock_parent(lower_dentry);
108         if (IS_ERR(lower_dir_dentry)) {
109                 ecryptfs_printk(KERN_ERR, "Error locking directory of "
110                                 "dentry\n");
111                 rc = PTR_ERR(lower_dir_dentry);
112                 goto out;
113         }
114         rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
115                                              ecryptfs_dentry, mode, nd);
116         if (rc) {
117                 printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
118                        "rc = [%d]\n", __func__, rc);
119                 goto out_lock;
120         }
121         rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
122                                 directory_inode->i_sb, 0);
123         if (rc) {
124                 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
125                 goto out_lock;
126         }
127         fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
128         fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
129 out_lock:
130         unlock_dir(lower_dir_dentry);
131 out:
132         return rc;
133 }
134
135 /**
136  * grow_file
137  * @ecryptfs_dentry: the eCryptfs dentry
138  *
139  * This is the code which will grow the file to its correct size.
140  */
141 static int grow_file(struct dentry *ecryptfs_dentry)
142 {
143         struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
144         struct file fake_file;
145         struct ecryptfs_file_info tmp_file_info;
146         char zero_virt[] = { 0x00 };
147         int rc = 0;
148
149         memset(&fake_file, 0, sizeof(fake_file));
150         fake_file.f_path.dentry = ecryptfs_dentry;
151         memset(&tmp_file_info, 0, sizeof(tmp_file_info));
152         ecryptfs_set_file_private(&fake_file, &tmp_file_info);
153         ecryptfs_set_file_lower(
154                 &fake_file,
155                 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file);
156         rc = ecryptfs_write(&fake_file, zero_virt, 0, 1);
157         i_size_write(ecryptfs_inode, 0);
158         rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
159         ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat.flags |=
160                 ECRYPTFS_NEW_FILE;
161         return rc;
162 }
163
164 /**
165  * ecryptfs_initialize_file
166  *
167  * Cause the file to be changed from a basic empty file to an ecryptfs
168  * file with a header and first data page.
169  *
170  * Returns zero on success
171  */
172 static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
173 {
174         struct ecryptfs_crypt_stat *crypt_stat =
175                 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
176         int rc = 0;
177
178         if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
179                 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
180                 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
181                 goto out;
182         }
183         crypt_stat->flags |= ECRYPTFS_NEW_FILE;
184         ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
185         rc = ecryptfs_new_file_context(ecryptfs_dentry);
186         if (rc) {
187                 ecryptfs_printk(KERN_ERR, "Error creating new file "
188                                 "context; rc = [%d]\n", rc);
189                 goto out;
190         }
191         rc = ecryptfs_write_metadata(ecryptfs_dentry);
192         if (rc) {
193                 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
194                 goto out;
195         }
196         rc = grow_file(ecryptfs_dentry);
197         if (rc)
198                 printk(KERN_ERR "Error growing file; rc = [%d]\n", rc);
199 out:
200         return rc;
201 }
202
203 /**
204  * ecryptfs_create
205  * @dir: The inode of the directory in which to create the file.
206  * @dentry: The eCryptfs dentry
207  * @mode: The mode of the new file.
208  * @nd: nameidata
209  *
210  * Creates a new file.
211  *
212  * Returns zero on success; non-zero on error condition
213  */
214 static int
215 ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
216                 int mode, struct nameidata *nd)
217 {
218         int rc;
219
220         /* ecryptfs_do_create() calls ecryptfs_interpose(), which opens
221          * the crypt_stat->lower_file (persistent file) */
222         rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
223         if (unlikely(rc)) {
224                 ecryptfs_printk(KERN_WARNING, "Failed to create file in"
225                                 "lower filesystem\n");
226                 goto out;
227         }
228         /* At this point, a file exists on "disk"; we need to make sure
229          * that this on disk file is prepared to be an ecryptfs file */
230         rc = ecryptfs_initialize_file(ecryptfs_dentry);
231 out:
232         return rc;
233 }
234
235 /**
236  * ecryptfs_lookup
237  * @dir: inode
238  * @dentry: The dentry
239  * @nd: nameidata, may be NULL
240  *
241  * Find a file on disk. If the file does not exist, then we'll add it to the
242  * dentry cache and continue on to read it from the disk.
243  */
244 static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
245                                       struct nameidata *nd)
246 {
247         int rc = 0;
248         struct dentry *lower_dir_dentry;
249         struct dentry *lower_dentry;
250         struct vfsmount *lower_mnt;
251         char *encoded_name;
252         int encoded_namelen;
253         struct ecryptfs_crypt_stat *crypt_stat = NULL;
254         struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
255         char *page_virt = NULL;
256         struct inode *lower_inode;
257         u64 file_size;
258
259         lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
260         dentry->d_op = &ecryptfs_dops;
261         if ((dentry->d_name.len == 1 && !strcmp(dentry->d_name.name, "."))
262             || (dentry->d_name.len == 2
263                 && !strcmp(dentry->d_name.name, ".."))) {
264                 d_drop(dentry);
265                 goto out;
266         }
267         encoded_namelen = ecryptfs_encode_filename(crypt_stat,
268                                                    dentry->d_name.name,
269                                                    dentry->d_name.len,
270                                                    &encoded_name);
271         if (encoded_namelen < 0) {
272                 rc = encoded_namelen;
273                 d_drop(dentry);
274                 goto out;
275         }
276         ecryptfs_printk(KERN_DEBUG, "encoded_name = [%s]; encoded_namelen "
277                         "= [%d]\n", encoded_name, encoded_namelen);
278         lower_dentry = lookup_one_len(encoded_name, lower_dir_dentry,
279                                       encoded_namelen - 1);
280         kfree(encoded_name);
281         if (IS_ERR(lower_dentry)) {
282                 ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
283                 rc = PTR_ERR(lower_dentry);
284                 d_drop(dentry);
285                 goto out;
286         }
287         lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
288         ecryptfs_printk(KERN_DEBUG, "lower_dentry = [%p]; lower_dentry->"
289                 "d_name.name = [%s]\n", lower_dentry,
290                 lower_dentry->d_name.name);
291         lower_inode = lower_dentry->d_inode;
292         fsstack_copy_attr_atime(dir, lower_dir_dentry->d_inode);
293         BUG_ON(!atomic_read(&lower_dentry->d_count));
294         ecryptfs_set_dentry_private(dentry,
295                                     kmem_cache_alloc(ecryptfs_dentry_info_cache,
296                                                      GFP_KERNEL));
297         if (!ecryptfs_dentry_to_private(dentry)) {
298                 rc = -ENOMEM;
299                 ecryptfs_printk(KERN_ERR, "Out of memory whilst attempting "
300                                 "to allocate ecryptfs_dentry_info struct\n");
301                 goto out_dput;
302         }
303         ecryptfs_set_dentry_lower(dentry, lower_dentry);
304         ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
305         if (!lower_dentry->d_inode) {
306                 /* We want to add because we couldn't find in lower */
307                 d_add(dentry, NULL);
308                 goto out;
309         }
310         rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 1);
311         if (rc) {
312                 ecryptfs_printk(KERN_ERR, "Error interposing\n");
313                 goto out_dput;
314         }
315         if (S_ISDIR(lower_inode->i_mode)) {
316                 ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n");
317                 goto out;
318         }
319         if (S_ISLNK(lower_inode->i_mode)) {
320                 ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n");
321                 goto out;
322         }
323         if (special_file(lower_inode->i_mode)) {
324                 ecryptfs_printk(KERN_DEBUG, "Is a special file; returning\n");
325                 goto out;
326         }
327         if (!nd) {
328                 ecryptfs_printk(KERN_DEBUG, "We have a NULL nd, just leave"
329                                 "as we *think* we are about to unlink\n");
330                 goto out;
331         }
332         /* Released in this function */
333         page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2,
334                                       GFP_USER);
335         if (!page_virt) {
336                 rc = -ENOMEM;
337                 ecryptfs_printk(KERN_ERR,
338                                 "Cannot ecryptfs_kmalloc a page\n");
339                 goto out_dput;
340         }
341         crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
342         if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
343                 ecryptfs_set_default_sizes(crypt_stat);
344         rc = ecryptfs_read_and_validate_header_region(page_virt,
345                                                       dentry->d_inode);
346         if (rc) {
347                 rc = ecryptfs_read_and_validate_xattr_region(page_virt, dentry);
348                 if (rc) {
349                         printk(KERN_DEBUG "Valid metadata not found in header "
350                                "region or xattr region; treating file as "
351                                "unencrypted\n");
352                         rc = 0;
353                         kmem_cache_free(ecryptfs_header_cache_2, page_virt);
354                         goto out;
355                 }
356                 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
357         }
358         mount_crypt_stat = &ecryptfs_superblock_to_private(
359                 dentry->d_sb)->mount_crypt_stat;
360         if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
361                 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
362                         file_size = (crypt_stat->num_header_bytes_at_front
363                                      + i_size_read(lower_dentry->d_inode));
364                 else
365                         file_size = i_size_read(lower_dentry->d_inode);
366         } else {
367                 memcpy(&file_size, page_virt, sizeof(file_size));
368                 file_size = be64_to_cpu(file_size);
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 };