]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ecryptfs/file.c
[PATCH] slab: remove SLAB_KERNEL
[net-next-2.6.git] / fs / ecryptfs / file.c
CommitLineData
237fead6
MH
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-2006 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/mount.h>
29#include <linux/pagemap.h>
30#include <linux/security.h>
31#include <linux/smp_lock.h>
32#include <linux/compat.h>
33#include "ecryptfs_kernel.h"
34
35/**
36 * ecryptfs_llseek
37 * @file: File we are seeking in
38 * @offset: The offset to seek to
39 * @origin: 2 - offset from i_size; 1 - offset from f_pos
40 *
41 * Returns the position we have seeked to, or negative on error
42 */
43static loff_t ecryptfs_llseek(struct file *file, loff_t offset, int origin)
44{
45 loff_t rv;
46 loff_t new_end_pos;
47 int rc;
48 int expanding_file = 0;
49 struct inode *inode = file->f_mapping->host;
50
51 /* If our offset is past the end of our file, we're going to
52 * need to grow it so we have a valid length of 0's */
53 new_end_pos = offset;
54 switch (origin) {
55 case 2:
56 new_end_pos += i_size_read(inode);
57 expanding_file = 1;
58 break;
59 case 1:
60 new_end_pos += file->f_pos;
61 if (new_end_pos > i_size_read(inode)) {
62 ecryptfs_printk(KERN_DEBUG, "new_end_pos(=[0x%.16x]) "
63 "> i_size_read(inode)(=[0x%.16x])\n",
64 new_end_pos, i_size_read(inode));
65 expanding_file = 1;
66 }
67 break;
68 default:
69 if (new_end_pos > i_size_read(inode)) {
70 ecryptfs_printk(KERN_DEBUG, "new_end_pos(=[0x%.16x]) "
71 "> i_size_read(inode)(=[0x%.16x])\n",
72 new_end_pos, i_size_read(inode));
73 expanding_file = 1;
74 }
75 }
76 ecryptfs_printk(KERN_DEBUG, "new_end_pos = [0x%.16x]\n", new_end_pos);
77 if (expanding_file) {
78 rc = ecryptfs_truncate(file->f_dentry, new_end_pos);
79 if (rc) {
80 rv = rc;
81 ecryptfs_printk(KERN_ERR, "Error on attempt to "
82 "truncate to (higher) offset [0x%.16x];"
83 " rc = [%d]\n", new_end_pos, rc);
84 goto out;
85 }
86 }
87 rv = generic_file_llseek(file, offset, origin);
88out:
89 return rv;
90}
91
92/**
93 * ecryptfs_read_update_atime
94 *
95 * generic_file_read updates the atime of upper layer inode. But, it
96 * doesn't give us a chance to update the atime of the lower layer
97 * inode. This function is a wrapper to generic_file_read. It
98 * updates the atime of the lower level inode if generic_file_read
99 * returns without any errors. This is to be used only for file reads.
100 * The function to be used for directory reads is ecryptfs_read.
101 */
102static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
103 const struct iovec *iov,
104 unsigned long nr_segs, loff_t pos)
105{
106 int rc;
107 struct dentry *lower_dentry;
108 struct vfsmount *lower_vfsmount;
109 struct file *file = iocb->ki_filp;
110
111 rc = generic_file_aio_read(iocb, iov, nr_segs, pos);
112 /*
113 * Even though this is a async interface, we need to wait
114 * for IO to finish to update atime
115 */
116 if (-EIOCBQUEUED == rc)
117 rc = wait_on_sync_kiocb(iocb);
118 if (rc >= 0) {
119 lower_dentry = ecryptfs_dentry_to_lower(file->f_dentry);
120 lower_vfsmount = ecryptfs_dentry_to_lower_mnt(file->f_dentry);
121 touch_atime(lower_vfsmount, lower_dentry);
122 }
123 return rc;
124}
125
126struct ecryptfs_getdents_callback {
127 void *dirent;
128 struct dentry *dentry;
129 filldir_t filldir;
130 int err;
131 int filldir_called;
132 int entries_written;
133};
134
135/* Inspired by generic filldir in fs/readir.c */
136static int
137ecryptfs_filldir(void *dirent, const char *name, int namelen, loff_t offset,
138 u64 ino, unsigned int d_type)
139{
140 struct ecryptfs_crypt_stat *crypt_stat;
141 struct ecryptfs_getdents_callback *buf =
142 (struct ecryptfs_getdents_callback *)dirent;
143 int rc;
144 int decoded_length;
145 char *decoded_name;
146
147 crypt_stat = ecryptfs_dentry_to_private(buf->dentry)->crypt_stat;
148 buf->filldir_called++;
149 decoded_length = ecryptfs_decode_filename(crypt_stat, name, namelen,
150 &decoded_name);
151 if (decoded_length < 0) {
152 rc = decoded_length;
153 goto out;
154 }
155 rc = buf->filldir(buf->dirent, decoded_name, decoded_length, offset,
156 ino, d_type);
157 kfree(decoded_name);
158 if (rc >= 0)
159 buf->entries_written++;
160out:
161 return rc;
162}
163
164/**
165 * ecryptfs_readdir
166 * @file: The ecryptfs file struct
167 * @dirent: Directory entry
168 * @filldir: The filldir callback function
169 */
170static int ecryptfs_readdir(struct file *file, void *dirent, filldir_t filldir)
171{
172 int rc;
173 struct file *lower_file;
174 struct inode *inode;
175 struct ecryptfs_getdents_callback buf;
176
177 lower_file = ecryptfs_file_to_lower(file);
178 lower_file->f_pos = file->f_pos;
179 inode = file->f_dentry->d_inode;
180 memset(&buf, 0, sizeof(buf));
181 buf.dirent = dirent;
182 buf.dentry = file->f_dentry;
183 buf.filldir = filldir;
184retry:
185 buf.filldir_called = 0;
186 buf.entries_written = 0;
187 buf.err = 0;
188 rc = vfs_readdir(lower_file, ecryptfs_filldir, (void *)&buf);
189 if (buf.err)
190 rc = buf.err;
191 if (buf.filldir_called && !buf.entries_written)
192 goto retry;
193 file->f_pos = lower_file->f_pos;
194 if (rc >= 0)
195 ecryptfs_copy_attr_atime(inode, lower_file->f_dentry->d_inode);
196 return rc;
197}
198
199struct kmem_cache *ecryptfs_file_info_cache;
200
7ff1d74f
MH
201int ecryptfs_open_lower_file(struct file **lower_file,
202 struct dentry *lower_dentry,
203 struct vfsmount *lower_mnt, int flags)
204{
205 int rc = 0;
206
207 dget(lower_dentry);
208 mntget(lower_mnt);
209 *lower_file = dentry_open(lower_dentry, lower_mnt, flags);
210 if (IS_ERR(*lower_file)) {
211 printk(KERN_ERR "Error opening lower file for lower_dentry "
212 "[0x%p], lower_mnt [0x%p], and flags [0x%x]\n",
213 lower_dentry, lower_mnt, flags);
214 rc = PTR_ERR(*lower_file);
215 *lower_file = NULL;
216 goto out;
217 }
218out:
219 return rc;
220}
221
222int ecryptfs_close_lower_file(struct file *lower_file)
223{
224 fput(lower_file);
225 return 0;
226}
227
237fead6
MH
228/**
229 * ecryptfs_open
230 * @inode: inode speciying file to open
231 * @file: Structure to return filled in
232 *
233 * Opens the file specified by inode.
234 *
235 * Returns zero on success; non-zero otherwise
236 */
237static int ecryptfs_open(struct inode *inode, struct file *file)
238{
239 int rc = 0;
240 struct ecryptfs_crypt_stat *crypt_stat = NULL;
241 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
242 struct dentry *ecryptfs_dentry = file->f_dentry;
243 /* Private value of ecryptfs_dentry allocated in
244 * ecryptfs_lookup() */
245 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
246 struct inode *lower_inode = NULL;
247 struct file *lower_file = NULL;
248 struct vfsmount *lower_mnt;
249 struct ecryptfs_file_info *file_info;
250 int lower_flags;
251
252 /* Released in ecryptfs_release or end of function if failure */
e94b1766 253 file_info = kmem_cache_alloc(ecryptfs_file_info_cache, GFP_KERNEL);
237fead6
MH
254 ecryptfs_set_file_private(file, file_info);
255 if (!file_info) {
256 ecryptfs_printk(KERN_ERR,
257 "Error attempting to allocate memory\n");
258 rc = -ENOMEM;
259 goto out;
260 }
261 memset(file_info, 0, sizeof(*file_info));
262 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
263 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
264 mount_crypt_stat = &ecryptfs_superblock_to_private(
265 ecryptfs_dentry->d_sb)->mount_crypt_stat;
266 mutex_lock(&crypt_stat->cs_mutex);
267 if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_POLICY_APPLIED)) {
268 ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
269 /* Policy code enabled in future release */
270 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_POLICY_APPLIED);
271 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
272 }
273 mutex_unlock(&crypt_stat->cs_mutex);
237fead6
MH
274 lower_flags = file->f_flags;
275 if ((lower_flags & O_ACCMODE) == O_WRONLY)
276 lower_flags = (lower_flags & O_ACCMODE) | O_RDWR;
277 if (file->f_flags & O_APPEND)
278 lower_flags &= ~O_APPEND;
279 lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
237fead6 280 /* Corresponding fput() in ecryptfs_release() */
7ff1d74f
MH
281 if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
282 lower_flags))) {
237fead6
MH
283 ecryptfs_printk(KERN_ERR, "Error opening lower file\n");
284 goto out_puts;
285 }
286 ecryptfs_set_file_lower(file, lower_file);
287 /* Isn't this check the same as the one in lookup? */
288 lower_inode = lower_dentry->d_inode;
289 if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
290 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
291 ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
292 rc = 0;
293 goto out;
294 }
295 mutex_lock(&crypt_stat->cs_mutex);
296 if (i_size_read(lower_inode) < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
297 if (!(mount_crypt_stat->flags
298 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
299 rc = -EIO;
300 printk(KERN_WARNING "Attempt to read file that is "
301 "not in a valid eCryptfs format, and plaintext "
302 "passthrough mode is not enabled; returning "
303 "-EIO\n");
304 mutex_unlock(&crypt_stat->cs_mutex);
305 goto out_puts;
306 }
307 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
308 rc = 0;
309 mutex_unlock(&crypt_stat->cs_mutex);
310 goto out;
311 } else if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
312 ECRYPTFS_POLICY_APPLIED)
313 || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
314 ECRYPTFS_KEY_VALID)) {
315 rc = ecryptfs_read_headers(ecryptfs_dentry, lower_file);
316 if (rc) {
317 ecryptfs_printk(KERN_DEBUG,
318 "Valid headers not found\n");
319 if (!(mount_crypt_stat->flags
320 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
321 rc = -EIO;
322 printk(KERN_WARNING "Attempt to read file that "
323 "is not in a valid eCryptfs format, "
324 "and plaintext passthrough mode is not "
325 "enabled; returning -EIO\n");
326 mutex_unlock(&crypt_stat->cs_mutex);
327 goto out_puts;
328 }
329 ECRYPTFS_CLEAR_FLAG(crypt_stat->flags,
330 ECRYPTFS_ENCRYPTED);
331 rc = 0;
332 mutex_unlock(&crypt_stat->cs_mutex);
333 goto out;
334 }
335 }
336 mutex_unlock(&crypt_stat->cs_mutex);
337 ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = [0x%.16x] "
338 "size: [0x%.16x]\n", inode, inode->i_ino,
339 i_size_read(inode));
340 ecryptfs_set_file_lower(file, lower_file);
341 goto out;
342out_puts:
343 mntput(lower_mnt);
344 dput(lower_dentry);
345 kmem_cache_free(ecryptfs_file_info_cache,
346 ecryptfs_file_to_private(file));
347out:
348 return rc;
349}
350
351static int ecryptfs_flush(struct file *file, fl_owner_t td)
352{
353 int rc = 0;
354 struct file *lower_file = NULL;
355
356 lower_file = ecryptfs_file_to_lower(file);
357 if (lower_file->f_op && lower_file->f_op->flush)
358 rc = lower_file->f_op->flush(lower_file, td);
359 return rc;
360}
361
362static int ecryptfs_release(struct inode *inode, struct file *file)
363{
364 struct file *lower_file = ecryptfs_file_to_lower(file);
365 struct ecryptfs_file_info *file_info = ecryptfs_file_to_private(file);
366 struct inode *lower_inode = ecryptfs_inode_to_lower(inode);
7ff1d74f 367 int rc;
237fead6 368
7ff1d74f
MH
369 if ((rc = ecryptfs_close_lower_file(lower_file))) {
370 printk(KERN_ERR "Error closing lower_file\n");
371 goto out;
372 }
237fead6
MH
373 inode->i_blocks = lower_inode->i_blocks;
374 kmem_cache_free(ecryptfs_file_info_cache, file_info);
7ff1d74f
MH
375out:
376 return rc;
237fead6
MH
377}
378
379static int
380ecryptfs_fsync(struct file *file, struct dentry *dentry, int datasync)
381{
382 struct file *lower_file = ecryptfs_file_to_lower(file);
383 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
384 struct inode *lower_inode = lower_dentry->d_inode;
385 int rc = -EINVAL;
386
387 if (lower_inode->i_fop->fsync) {
388 mutex_lock(&lower_inode->i_mutex);
389 rc = lower_inode->i_fop->fsync(lower_file, lower_dentry,
390 datasync);
391 mutex_unlock(&lower_inode->i_mutex);
392 }
393 return rc;
394}
395
396static int ecryptfs_fasync(int fd, struct file *file, int flag)
397{
398 int rc = 0;
399 struct file *lower_file = NULL;
400
401 lower_file = ecryptfs_file_to_lower(file);
402 if (lower_file->f_op && lower_file->f_op->fasync)
403 rc = lower_file->f_op->fasync(fd, lower_file, flag);
404 return rc;
405}
406
407static ssize_t ecryptfs_sendfile(struct file *file, loff_t * ppos,
408 size_t count, read_actor_t actor, void *target)
409{
410 struct file *lower_file = NULL;
411 int rc = -EINVAL;
412
413 lower_file = ecryptfs_file_to_lower(file);
414 if (lower_file->f_op && lower_file->f_op->sendfile)
415 rc = lower_file->f_op->sendfile(lower_file, ppos, count,
416 actor, target);
417
418 return rc;
419}
420
421static int ecryptfs_ioctl(struct inode *inode, struct file *file,
422 unsigned int cmd, unsigned long arg);
423
424const struct file_operations ecryptfs_dir_fops = {
425 .readdir = ecryptfs_readdir,
426 .ioctl = ecryptfs_ioctl,
427 .mmap = generic_file_mmap,
428 .open = ecryptfs_open,
429 .flush = ecryptfs_flush,
430 .release = ecryptfs_release,
431 .fsync = ecryptfs_fsync,
432 .fasync = ecryptfs_fasync,
433 .sendfile = ecryptfs_sendfile,
434};
435
436const struct file_operations ecryptfs_main_fops = {
437 .llseek = ecryptfs_llseek,
438 .read = do_sync_read,
439 .aio_read = ecryptfs_read_update_atime,
440 .write = do_sync_write,
441 .aio_write = generic_file_aio_write,
442 .readdir = ecryptfs_readdir,
443 .ioctl = ecryptfs_ioctl,
444 .mmap = generic_file_mmap,
445 .open = ecryptfs_open,
446 .flush = ecryptfs_flush,
447 .release = ecryptfs_release,
448 .fsync = ecryptfs_fsync,
449 .fasync = ecryptfs_fasync,
450 .sendfile = ecryptfs_sendfile,
451};
452
453static int
454ecryptfs_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
455 unsigned long arg)
456{
457 int rc = 0;
458 struct file *lower_file = NULL;
459
460 if (ecryptfs_file_to_private(file))
461 lower_file = ecryptfs_file_to_lower(file);
462 if (lower_file && lower_file->f_op && lower_file->f_op->ioctl)
463 rc = lower_file->f_op->ioctl(ecryptfs_inode_to_lower(inode),
464 lower_file, cmd, arg);
465 else
466 rc = -ENOTTY;
467 return rc;
468}