eCryptfs: Initialize empty lower files when opening them
[pandora-kernel.git] / fs / ecryptfs / file.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 <mhalcrow@us.ibm.com>
8  *              Michael C. Thompson <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/poll.h>
28 #include <linux/slab.h>
29 #include <linux/mount.h>
30 #include <linux/pagemap.h>
31 #include <linux/security.h>
32 #include <linux/compat.h>
33 #include <linux/fs_stack.h>
34 #include "ecryptfs_kernel.h"
35
36 /**
37  * ecryptfs_read_update_atime
38  *
39  * generic_file_read updates the atime of upper layer inode.  But, it
40  * doesn't give us a chance to update the atime of the lower layer
41  * inode.  This function is a wrapper to generic_file_read.  It
42  * updates the atime of the lower level inode if generic_file_read
43  * returns without any errors. This is to be used only for file reads.
44  * The function to be used for directory reads is ecryptfs_read.
45  */
46 static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
47                                 const struct iovec *iov,
48                                 unsigned long nr_segs, loff_t pos)
49 {
50         ssize_t rc;
51         struct dentry *lower_dentry;
52         struct vfsmount *lower_vfsmount;
53         struct file *file = iocb->ki_filp;
54
55         rc = generic_file_aio_read(iocb, iov, nr_segs, pos);
56         /*
57          * Even though this is a async interface, we need to wait
58          * for IO to finish to update atime
59          */
60         if (-EIOCBQUEUED == rc)
61                 rc = wait_on_sync_kiocb(iocb);
62         if (rc >= 0) {
63                 lower_dentry = ecryptfs_dentry_to_lower(file->f_path.dentry);
64                 lower_vfsmount = ecryptfs_dentry_to_lower_mnt(file->f_path.dentry);
65                 touch_atime(lower_vfsmount, lower_dentry);
66         }
67         return rc;
68 }
69
70 struct ecryptfs_getdents_callback {
71         void *dirent;
72         struct dentry *dentry;
73         filldir_t filldir;
74         int filldir_called;
75         int entries_written;
76 };
77
78 /* Inspired by generic filldir in fs/readdir.c */
79 static int
80 ecryptfs_filldir(void *dirent, const char *lower_name, int lower_namelen,
81                  loff_t offset, u64 ino, unsigned int d_type)
82 {
83         struct ecryptfs_getdents_callback *buf =
84             (struct ecryptfs_getdents_callback *)dirent;
85         size_t name_size;
86         char *name;
87         int rc;
88
89         buf->filldir_called++;
90         rc = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
91                                                   buf->dentry, lower_name,
92                                                   lower_namelen);
93         if (rc) {
94                 printk(KERN_ERR "%s: Error attempting to decode and decrypt "
95                        "filename [%s]; rc = [%d]\n", __func__, lower_name,
96                        rc);
97                 goto out;
98         }
99         rc = buf->filldir(buf->dirent, name, name_size, offset, ino, d_type);
100         kfree(name);
101         if (rc >= 0)
102                 buf->entries_written++;
103 out:
104         return rc;
105 }
106
107 /**
108  * ecryptfs_readdir
109  * @file: The eCryptfs directory file
110  * @dirent: Directory entry handle
111  * @filldir: The filldir callback function
112  */
113 static int ecryptfs_readdir(struct file *file, void *dirent, filldir_t filldir)
114 {
115         int rc;
116         struct file *lower_file;
117         struct inode *inode;
118         struct ecryptfs_getdents_callback buf;
119
120         lower_file = ecryptfs_file_to_lower(file);
121         lower_file->f_pos = file->f_pos;
122         inode = file->f_path.dentry->d_inode;
123         memset(&buf, 0, sizeof(buf));
124         buf.dirent = dirent;
125         buf.dentry = file->f_path.dentry;
126         buf.filldir = filldir;
127         buf.filldir_called = 0;
128         buf.entries_written = 0;
129         rc = vfs_readdir(lower_file, ecryptfs_filldir, (void *)&buf);
130         file->f_pos = lower_file->f_pos;
131         if (rc < 0)
132                 goto out;
133         if (buf.filldir_called && !buf.entries_written)
134                 goto out;
135         if (rc >= 0)
136                 fsstack_copy_attr_atime(inode,
137                                         lower_file->f_path.dentry->d_inode);
138 out:
139         return rc;
140 }
141
142 static void ecryptfs_vma_close(struct vm_area_struct *vma)
143 {
144         filemap_write_and_wait(vma->vm_file->f_mapping);
145 }
146
147 static const struct vm_operations_struct ecryptfs_file_vm_ops = {
148         .close          = ecryptfs_vma_close,
149         .fault          = filemap_fault,
150 };
151
152 static int ecryptfs_file_mmap(struct file *file, struct vm_area_struct *vma)
153 {
154         int rc;
155
156         rc = generic_file_mmap(file, vma);
157         if (!rc)
158                 vma->vm_ops = &ecryptfs_file_vm_ops;
159
160         return rc;
161 }
162
163 struct kmem_cache *ecryptfs_file_info_cache;
164
165 static int read_or_initialize_metadata(struct dentry *dentry)
166 {
167         struct inode *inode = dentry->d_inode;
168         struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
169         struct ecryptfs_crypt_stat *crypt_stat;
170         int rc;
171
172         crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
173         mount_crypt_stat = &ecryptfs_superblock_to_private(
174                                                 inode->i_sb)->mount_crypt_stat;
175         mutex_lock(&crypt_stat->cs_mutex);
176
177         if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
178             crypt_stat->flags & ECRYPTFS_KEY_VALID) {
179                 rc = 0;
180                 goto out;
181         }
182
183         rc = ecryptfs_read_metadata(dentry);
184         if (!rc)
185                 goto out;
186
187         if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
188                 crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
189                                        | ECRYPTFS_ENCRYPTED);
190                 rc = 0;
191                 goto out;
192         }
193
194         if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
195             !i_size_read(ecryptfs_inode_to_lower(inode))) {
196                 rc = ecryptfs_initialize_file(dentry, inode);
197                 if (!rc)
198                         goto out;
199         }
200
201         rc = -EIO;
202 out:
203         mutex_unlock(&crypt_stat->cs_mutex);
204         return rc;
205 }
206
207 /**
208  * ecryptfs_open
209  * @inode: inode speciying file to open
210  * @file: Structure to return filled in
211  *
212  * Opens the file specified by inode.
213  *
214  * Returns zero on success; non-zero otherwise
215  */
216 static int ecryptfs_open(struct inode *inode, struct file *file)
217 {
218         int rc = 0;
219         struct ecryptfs_crypt_stat *crypt_stat = NULL;
220         struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
221         struct dentry *ecryptfs_dentry = file->f_path.dentry;
222         /* Private value of ecryptfs_dentry allocated in
223          * ecryptfs_lookup() */
224         struct dentry *lower_dentry;
225         struct ecryptfs_file_info *file_info;
226
227         mount_crypt_stat = &ecryptfs_superblock_to_private(
228                 ecryptfs_dentry->d_sb)->mount_crypt_stat;
229         if ((mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
230             && ((file->f_flags & O_WRONLY) || (file->f_flags & O_RDWR)
231                 || (file->f_flags & O_CREAT) || (file->f_flags & O_TRUNC)
232                 || (file->f_flags & O_APPEND))) {
233                 printk(KERN_WARNING "Mount has encrypted view enabled; "
234                        "files may only be read\n");
235                 rc = -EPERM;
236                 goto out;
237         }
238         /* Released in ecryptfs_release or end of function if failure */
239         file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
240         ecryptfs_set_file_private(file, file_info);
241         if (!file_info) {
242                 ecryptfs_printk(KERN_ERR,
243                                 "Error attempting to allocate memory\n");
244                 rc = -ENOMEM;
245                 goto out;
246         }
247         lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
248         crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
249         mutex_lock(&crypt_stat->cs_mutex);
250         if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
251                 ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
252                 /* Policy code enabled in future release */
253                 crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
254                                       | ECRYPTFS_ENCRYPTED);
255         }
256         mutex_unlock(&crypt_stat->cs_mutex);
257         rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
258         if (rc) {
259                 printk(KERN_ERR "%s: Error attempting to initialize "
260                         "the lower file for the dentry with name "
261                         "[%s]; rc = [%d]\n", __func__,
262                         ecryptfs_dentry->d_name.name, rc);
263                 goto out_free;
264         }
265         if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
266             == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
267                 rc = -EPERM;
268                 printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
269                        "file must hence be opened RO\n", __func__);
270                 goto out_put;
271         }
272         ecryptfs_set_file_lower(
273                 file, ecryptfs_inode_to_private(inode)->lower_file);
274         if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
275                 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
276                 mutex_lock(&crypt_stat->cs_mutex);
277                 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
278                 mutex_unlock(&crypt_stat->cs_mutex);
279                 rc = 0;
280                 goto out;
281         }
282         rc = read_or_initialize_metadata(ecryptfs_dentry);
283         if (rc)
284                 goto out_put;
285         ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
286                         "[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
287                         (unsigned long long)i_size_read(inode));
288         goto out;
289 out_put:
290         ecryptfs_put_lower_file(inode);
291 out_free:
292         kmem_cache_free(ecryptfs_file_info_cache,
293                         ecryptfs_file_to_private(file));
294 out:
295         return rc;
296 }
297
298 static int ecryptfs_flush(struct file *file, fl_owner_t td)
299 {
300         return file->f_mode & FMODE_WRITE
301                ? filemap_write_and_wait(file->f_mapping) : 0;
302 }
303
304 static int ecryptfs_release(struct inode *inode, struct file *file)
305 {
306         ecryptfs_put_lower_file(inode);
307         kmem_cache_free(ecryptfs_file_info_cache,
308                         ecryptfs_file_to_private(file));
309         return 0;
310 }
311
312 static int
313 ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
314 {
315         int rc = 0;
316
317         rc = generic_file_fsync(file, start, end, datasync);
318         if (rc)
319                 goto out;
320         rc = vfs_fsync_range(ecryptfs_file_to_lower(file), start, end,
321                              datasync);
322 out:
323         return rc;
324 }
325
326 static int ecryptfs_fasync(int fd, struct file *file, int flag)
327 {
328         int rc = 0;
329         struct file *lower_file = NULL;
330
331         lower_file = ecryptfs_file_to_lower(file);
332         if (lower_file->f_op && lower_file->f_op->fasync)
333                 rc = lower_file->f_op->fasync(fd, lower_file, flag);
334         return rc;
335 }
336
337 static long
338 ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
339 {
340         struct file *lower_file = NULL;
341         long rc = -ENOTTY;
342
343         if (ecryptfs_file_to_private(file))
344                 lower_file = ecryptfs_file_to_lower(file);
345         if (lower_file && lower_file->f_op && lower_file->f_op->unlocked_ioctl)
346                 rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
347         return rc;
348 }
349
350 #ifdef CONFIG_COMPAT
351 static long
352 ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
353 {
354         struct file *lower_file = NULL;
355         long rc = -ENOIOCTLCMD;
356
357         if (ecryptfs_file_to_private(file))
358                 lower_file = ecryptfs_file_to_lower(file);
359         if (lower_file && lower_file->f_op && lower_file->f_op->compat_ioctl)
360                 rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
361         return rc;
362 }
363 #endif
364
365 const struct file_operations ecryptfs_dir_fops = {
366         .readdir = ecryptfs_readdir,
367         .read = generic_read_dir,
368         .unlocked_ioctl = ecryptfs_unlocked_ioctl,
369 #ifdef CONFIG_COMPAT
370         .compat_ioctl = ecryptfs_compat_ioctl,
371 #endif
372         .open = ecryptfs_open,
373         .flush = ecryptfs_flush,
374         .release = ecryptfs_release,
375         .fsync = ecryptfs_fsync,
376         .fasync = ecryptfs_fasync,
377         .splice_read = generic_file_splice_read,
378         .llseek = default_llseek,
379 };
380
381 const struct file_operations ecryptfs_main_fops = {
382         .llseek = generic_file_llseek,
383         .read = do_sync_read,
384         .aio_read = ecryptfs_read_update_atime,
385         .write = do_sync_write,
386         .aio_write = generic_file_aio_write,
387         .readdir = ecryptfs_readdir,
388         .unlocked_ioctl = ecryptfs_unlocked_ioctl,
389 #ifdef CONFIG_COMPAT
390         .compat_ioctl = ecryptfs_compat_ioctl,
391 #endif
392         .mmap = ecryptfs_file_mmap,
393         .open = ecryptfs_open,
394         .flush = ecryptfs_flush,
395         .release = ecryptfs_release,
396         .fsync = ecryptfs_fsync,
397         .fasync = ecryptfs_fasync,
398         .splice_read = generic_file_splice_read,
399 };