blob: f5f962d7a123288d17195035c005441b26d7cbf5 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 * This is where eCryptfs coordinates the symmetric encryption and
4 * decryption of the file data as it passes between the lower
5 * encrypted file and the upper decrypted file.
6 *
7 * Copyright (C) 1997-2003 Erez Zadok
8 * Copyright (C) 2001-2003 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08009 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -070010 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA.
26 */
27
28#include <linux/pagemap.h>
29#include <linux/writeback.h>
30#include <linux/page-flags.h>
31#include <linux/mount.h>
32#include <linux/file.h>
33#include <linux/crypto.h>
34#include <linux/scatterlist.h>
35#include "ecryptfs_kernel.h"
36
37struct kmem_cache *ecryptfs_lower_page_cache;
38
39/**
40 * ecryptfs_get1page
41 *
42 * Get one page from cache or lower f/s, return error otherwise.
43 *
44 * Returns unlocked and up-to-date page (if ok), with increased
45 * refcnt.
46 */
47static struct page *ecryptfs_get1page(struct file *file, int index)
48{
49 struct page *page;
50 struct dentry *dentry;
51 struct inode *inode;
52 struct address_space *mapping;
53
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -080054 dentry = file->f_path.dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -070055 inode = dentry->d_inode;
56 mapping = inode->i_mapping;
57 page = read_cache_page(mapping, index,
58 (filler_t *)mapping->a_ops->readpage,
59 (void *)file);
60 if (IS_ERR(page))
61 goto out;
62 wait_on_page_locked(page);
63out:
64 return page;
65}
66
67static
68int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros);
69
70/**
71 * ecryptfs_fill_zeros
72 * @file: The ecryptfs file
73 * @new_length: The new length of the data in the underlying file;
74 * everything between the prior end of the file and the
75 * new end of the file will be filled with zero's.
76 * new_length must be greater than current length
77 *
78 * Function for handling lseek-ing past the end of the file.
79 *
80 * This function does not support shrinking, only growing a file.
81 *
82 * Returns zero on success; non-zero otherwise.
83 */
84int ecryptfs_fill_zeros(struct file *file, loff_t new_length)
85{
86 int rc = 0;
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -080087 struct dentry *dentry = file->f_path.dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -070088 struct inode *inode = dentry->d_inode;
89 pgoff_t old_end_page_index = 0;
90 pgoff_t index = old_end_page_index;
91 int old_end_pos_in_page = -1;
92 pgoff_t new_end_page_index;
93 int new_end_pos_in_page;
94 loff_t cur_length = i_size_read(inode);
95
96 if (cur_length != 0) {
97 index = old_end_page_index =
98 ((cur_length - 1) >> PAGE_CACHE_SHIFT);
99 old_end_pos_in_page = ((cur_length - 1) & ~PAGE_CACHE_MASK);
100 }
101 new_end_page_index = ((new_length - 1) >> PAGE_CACHE_SHIFT);
102 new_end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK);
103 ecryptfs_printk(KERN_DEBUG, "old_end_page_index = [0x%.16x]; "
104 "old_end_pos_in_page = [%d]; "
105 "new_end_page_index = [0x%.16x]; "
106 "new_end_pos_in_page = [%d]\n",
107 old_end_page_index, old_end_pos_in_page,
108 new_end_page_index, new_end_pos_in_page);
109 if (old_end_page_index == new_end_page_index) {
110 /* Start and end are in the same page; we just need to
111 * set a portion of the existing page to zero's */
112 rc = write_zeros(file, index, (old_end_pos_in_page + 1),
113 (new_end_pos_in_page - old_end_pos_in_page));
114 if (rc)
115 ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
116 "index=[0x%.16x], "
117 "old_end_pos_in_page=[d], "
118 "(PAGE_CACHE_SIZE - new_end_pos_in_page"
119 "=[%d]"
120 ")=[d]) returned [%d]\n", file, index,
121 old_end_pos_in_page,
122 new_end_pos_in_page,
123 (PAGE_CACHE_SIZE - new_end_pos_in_page),
124 rc);
125 goto out;
126 }
127 /* Fill the remainder of the previous last page with zeros */
128 rc = write_zeros(file, index, (old_end_pos_in_page + 1),
129 ((PAGE_CACHE_SIZE - 1) - old_end_pos_in_page));
130 if (rc) {
131 ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
132 "index=[0x%.16x], old_end_pos_in_page=[d], "
133 "(PAGE_CACHE_SIZE - old_end_pos_in_page)=[d]) "
134 "returned [%d]\n", file, index,
135 old_end_pos_in_page,
136 (PAGE_CACHE_SIZE - old_end_pos_in_page), rc);
137 goto out;
138 }
139 index++;
140 while (index < new_end_page_index) {
141 /* Fill all intermediate pages with zeros */
142 rc = write_zeros(file, index, 0, PAGE_CACHE_SIZE);
143 if (rc) {
144 ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
145 "index=[0x%.16x], "
146 "old_end_pos_in_page=[d], "
147 "(PAGE_CACHE_SIZE - new_end_pos_in_page"
148 "=[%d]"
149 ")=[d]) returned [%d]\n", file, index,
150 old_end_pos_in_page,
151 new_end_pos_in_page,
152 (PAGE_CACHE_SIZE - new_end_pos_in_page),
153 rc);
154 goto out;
155 }
156 index++;
157 }
158 /* Fill the portion at the beginning of the last new page with
159 * zero's */
160 rc = write_zeros(file, index, 0, (new_end_pos_in_page + 1));
161 if (rc) {
162 ecryptfs_printk(KERN_ERR, "write_zeros(file="
163 "[%p], index=[0x%.16x], 0, "
164 "new_end_pos_in_page=[%d]"
165 "returned [%d]\n", file, index,
166 new_end_pos_in_page, rc);
167 goto out;
168 }
169out:
170 return rc;
171}
172
173/**
174 * ecryptfs_writepage
175 * @page: Page that is locked before this call is made
176 *
177 * Returns zero on success; non-zero otherwise
178 */
179static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
180{
181 struct ecryptfs_page_crypt_context ctx;
182 int rc;
183
184 ctx.page = page;
185 ctx.mode = ECRYPTFS_WRITEPAGE_MODE;
186 ctx.param.wbc = wbc;
187 rc = ecryptfs_encrypt_page(&ctx);
188 if (rc) {
189 ecryptfs_printk(KERN_WARNING, "Error encrypting "
190 "page (upper index [0x%.16x])\n", page->index);
191 ClearPageUptodate(page);
192 goto out;
193 }
194 SetPageUptodate(page);
195 unlock_page(page);
196out:
197 return rc;
198}
199
200/**
201 * Reads the data from the lower file file at index lower_page_index
202 * and copies that data into page.
203 *
204 * @param page Page to fill
205 * @param lower_page_index Index of the page in the lower file to get
206 */
207int ecryptfs_do_readpage(struct file *file, struct page *page,
208 pgoff_t lower_page_index)
209{
210 int rc;
211 struct dentry *dentry;
212 struct file *lower_file;
213 struct dentry *lower_dentry;
214 struct inode *inode;
215 struct inode *lower_inode;
216 char *page_data;
217 struct page *lower_page = NULL;
218 char *lower_page_data;
219 const struct address_space_operations *lower_a_ops;
220
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -0800221 dentry = file->f_path.dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700222 lower_file = ecryptfs_file_to_lower(file);
223 lower_dentry = ecryptfs_dentry_to_lower(dentry);
224 inode = dentry->d_inode;
225 lower_inode = ecryptfs_inode_to_lower(inode);
226 lower_a_ops = lower_inode->i_mapping->a_ops;
227 lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index,
228 (filler_t *)lower_a_ops->readpage,
229 (void *)lower_file);
230 if (IS_ERR(lower_page)) {
231 rc = PTR_ERR(lower_page);
232 lower_page = NULL;
233 ecryptfs_printk(KERN_ERR, "Error reading from page cache\n");
234 goto out;
235 }
236 wait_on_page_locked(lower_page);
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800237 page_data = kmap_atomic(page, KM_USER0);
238 lower_page_data = kmap_atomic(lower_page, KM_USER1);
Michael Halcrow237fead2006-10-04 02:16:22 -0700239 memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE);
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800240 kunmap_atomic(lower_page_data, KM_USER1);
241 kunmap_atomic(page_data, KM_USER0);
Michael Halcrow237fead2006-10-04 02:16:22 -0700242 rc = 0;
243out:
244 if (likely(lower_page))
245 page_cache_release(lower_page);
246 if (rc == 0)
247 SetPageUptodate(page);
248 else
249 ClearPageUptodate(page);
250 return rc;
251}
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800252/**
253 * Header Extent:
254 * Octets 0-7: Unencrypted file size (big-endian)
255 * Octets 8-15: eCryptfs special marker
256 * Octets 16-19: Flags
257 * Octet 16: File format version number (between 0 and 255)
258 * Octets 17-18: Reserved
259 * Octet 19: Bit 1 (lsb): Reserved
260 * Bit 2: Encrypted?
261 * Bits 3-8: Reserved
262 * Octets 20-23: Header extent size (big-endian)
263 * Octets 24-25: Number of header extents at front of file
264 * (big-endian)
265 * Octet 26: Begin RFC 2440 authentication token packet set
266 */
267static void set_header_info(char *page_virt,
268 struct ecryptfs_crypt_stat *crypt_stat)
269{
270 size_t written;
271 int save_num_header_extents_at_front =
272 crypt_stat->num_header_extents_at_front;
273
274 crypt_stat->num_header_extents_at_front = 1;
275 ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
276 crypt_stat->num_header_extents_at_front =
277 save_num_header_extents_at_front;
278}
Michael Halcrow237fead2006-10-04 02:16:22 -0700279
280/**
281 * ecryptfs_readpage
282 * @file: This is an ecryptfs file
283 * @page: ecryptfs associated page to stick the read data into
284 *
285 * Read in a page, decrypting if necessary.
286 *
287 * Returns zero on success; non-zero on error.
288 */
289static int ecryptfs_readpage(struct file *file, struct page *page)
290{
291 int rc = 0;
292 struct ecryptfs_crypt_stat *crypt_stat;
293
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -0800294 BUG_ON(!(file && file->f_path.dentry && file->f_path.dentry->d_inode));
295 crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
296 ->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700297 if (!crypt_stat
298 || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)
299 || ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) {
300 ecryptfs_printk(KERN_DEBUG,
301 "Passing through unencrypted page\n");
302 rc = ecryptfs_do_readpage(file, page, page->index);
303 if (rc) {
304 ecryptfs_printk(KERN_ERR, "Error reading page; rc = "
305 "[%d]\n", rc);
306 goto out;
307 }
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800308 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
309 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
310 int num_pages_in_header_region =
311 (crypt_stat->header_extent_size
312 / PAGE_CACHE_SIZE);
313
314 if (page->index < num_pages_in_header_region) {
315 char *page_virt;
316
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800317 page_virt = kmap_atomic(page, KM_USER0);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800318 memset(page_virt, 0, PAGE_CACHE_SIZE);
319 if (page->index == 0) {
320 rc = ecryptfs_read_xattr_region(
321 page_virt, file->f_path.dentry);
322 set_header_info(page_virt, crypt_stat);
323 }
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800324 kunmap_atomic(page_virt, KM_USER0);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800325 if (rc) {
326 printk(KERN_ERR "Error reading xattr "
327 "region\n");
328 goto out;
329 }
330 } else {
331 rc = ecryptfs_do_readpage(
332 file, page,
333 (page->index
334 - num_pages_in_header_region));
335 if (rc) {
336 printk(KERN_ERR "Error reading page; "
337 "rc = [%d]\n", rc);
338 goto out;
339 }
340 }
341 } else {
342 rc = ecryptfs_do_readpage(file, page, page->index);
343 if (rc) {
344 printk(KERN_ERR "Error reading page; rc = "
345 "[%d]\n", rc);
346 goto out;
347 }
348 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700349 } else {
350 rc = ecryptfs_decrypt_page(file, page);
351 if (rc) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700352 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
353 "rc = [%d]\n", rc);
354 goto out;
355 }
356 }
357 SetPageUptodate(page);
358out:
359 if (rc)
360 ClearPageUptodate(page);
361 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
362 page->index);
363 unlock_page(page);
364 return rc;
365}
366
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800367/**
368 * Called with lower inode mutex held.
369 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700370static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
371{
372 struct inode *inode = page->mapping->host;
373 int end_byte_in_page;
Michael Halcrow237fead2006-10-04 02:16:22 -0700374 char *page_virt;
375
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800376 if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
377 goto out;
378 end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
379 if (to > end_byte_in_page)
380 end_byte_in_page = to;
381 page_virt = kmap_atomic(page, KM_USER0);
382 memset((page_virt + end_byte_in_page), 0,
383 (PAGE_CACHE_SIZE - end_byte_in_page));
384 kunmap_atomic(page_virt, KM_USER0);
Michael Halcrow237fead2006-10-04 02:16:22 -0700385out:
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800386 return 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700387}
388
389static int ecryptfs_prepare_write(struct file *file, struct page *page,
390 unsigned from, unsigned to)
391{
392 int rc = 0;
393
Michael Halcrow237fead2006-10-04 02:16:22 -0700394 if (from == 0 && to == PAGE_CACHE_SIZE)
395 goto out; /* If we are writing a full page, it will be
396 up to date. */
397 if (!PageUptodate(page))
398 rc = ecryptfs_do_readpage(file, page, page->index);
399out:
400 return rc;
401}
402
Michael Halcrow237fead2006-10-04 02:16:22 -0700403int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
404 struct inode *lower_inode,
405 struct writeback_control *wbc)
406{
407 int rc = 0;
408
409 rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
410 if (rc) {
411 ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
412 "rc = [%d]\n", rc);
413 goto out;
414 }
415 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
416 page_cache_release(lower_page);
417out:
418 return rc;
419}
420
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800421static void ecryptfs_release_lower_page(struct page *lower_page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700422{
Michael Halcrow237fead2006-10-04 02:16:22 -0700423 unlock_page(lower_page);
424 page_cache_release(lower_page);
425}
426
427/**
428 * ecryptfs_write_inode_size_to_header
429 *
430 * Writes the lower file size to the first 8 bytes of the header.
431 *
432 * Returns zero on success; non-zero on error.
433 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800434static int ecryptfs_write_inode_size_to_header(struct file *lower_file,
435 struct inode *lower_inode,
436 struct inode *inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700437{
438 int rc = 0;
439 struct page *header_page;
440 char *header_virt;
441 const struct address_space_operations *lower_a_ops;
442 u64 file_size;
443
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800444 header_page = grab_cache_page(lower_inode->i_mapping, 0);
445 if (!header_page) {
446 ecryptfs_printk(KERN_ERR, "grab_cache_page for "
447 "lower_page_index 0 failed\n");
448 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700449 goto out;
450 }
451 lower_a_ops = lower_inode->i_mapping->a_ops;
452 rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8);
453 file_size = (u64)i_size_read(inode);
454 ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size);
455 file_size = cpu_to_be64(file_size);
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800456 header_virt = kmap_atomic(header_page, KM_USER0);
Michael Halcrow237fead2006-10-04 02:16:22 -0700457 memcpy(header_virt, &file_size, sizeof(u64));
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800458 kunmap_atomic(header_virt, KM_USER0);
Michael Halcrow237fead2006-10-04 02:16:22 -0700459 rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8);
460 if (rc < 0)
461 ecryptfs_printk(KERN_ERR, "Error commiting header page "
462 "write\n");
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800463 ecryptfs_release_lower_page(header_page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700464 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
465 mark_inode_dirty_sync(inode);
466out:
467 return rc;
468}
469
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800470static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode,
471 struct inode *inode,
472 struct dentry *ecryptfs_dentry,
473 int lower_i_mutex_held)
474{
475 ssize_t size;
476 void *xattr_virt;
477 struct dentry *lower_dentry;
478 u64 file_size;
479 int rc;
480
481 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
482 if (!xattr_virt) {
483 printk(KERN_ERR "Out of memory whilst attempting to write "
484 "inode size to xattr\n");
485 rc = -ENOMEM;
486 goto out;
487 }
488 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
489 if (!lower_dentry->d_inode->i_op->getxattr) {
490 printk(KERN_WARNING
491 "No support for setting xattr in lower filesystem\n");
492 rc = -ENOSYS;
493 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
494 goto out;
495 }
496 if (!lower_i_mutex_held)
497 mutex_lock(&lower_dentry->d_inode->i_mutex);
498 size = lower_dentry->d_inode->i_op->getxattr(lower_dentry,
499 ECRYPTFS_XATTR_NAME,
500 xattr_virt,
501 PAGE_CACHE_SIZE);
502 if (!lower_i_mutex_held)
503 mutex_unlock(&lower_dentry->d_inode->i_mutex);
504 if (size < 0)
505 size = 8;
506 file_size = (u64)i_size_read(inode);
507 file_size = cpu_to_be64(file_size);
508 memcpy(xattr_virt, &file_size, sizeof(u64));
509 if (!lower_i_mutex_held)
510 mutex_lock(&lower_dentry->d_inode->i_mutex);
511 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry,
512 ECRYPTFS_XATTR_NAME,
513 xattr_virt, size, 0);
514 if (!lower_i_mutex_held)
515 mutex_unlock(&lower_dentry->d_inode->i_mutex);
516 if (rc)
517 printk(KERN_ERR "Error whilst attempting to write inode size "
518 "to lower file xattr; rc = [%d]\n", rc);
519 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
520out:
521 return rc;
522}
523
524int
525ecryptfs_write_inode_size_to_metadata(struct file *lower_file,
526 struct inode *lower_inode,
527 struct inode *inode,
528 struct dentry *ecryptfs_dentry,
529 int lower_i_mutex_held)
530{
531 struct ecryptfs_crypt_stat *crypt_stat;
532
533 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
534 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
535 return ecryptfs_write_inode_size_to_xattr(lower_inode, inode,
536 ecryptfs_dentry,
537 lower_i_mutex_held);
538 else
539 return ecryptfs_write_inode_size_to_header(lower_file,
540 lower_inode,
541 inode);
542}
543
Michael Halcrow237fead2006-10-04 02:16:22 -0700544int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
545 struct file *lower_file,
546 unsigned long lower_page_index, int byte_offset,
547 int region_bytes)
548{
549 int rc = 0;
550
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800551 *lower_page = grab_cache_page(lower_inode->i_mapping, lower_page_index);
552 if (!(*lower_page)) {
553 rc = -EINVAL;
554 ecryptfs_printk(KERN_ERR, "Error attempting to grab "
Michael Halcrow237fead2006-10-04 02:16:22 -0700555 "lower page with index [0x%.16x]\n",
556 lower_page_index);
557 goto out;
558 }
559 rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
560 (*lower_page),
561 byte_offset,
562 region_bytes);
563 if (rc) {
564 ecryptfs_printk(KERN_ERR, "prepare_write for "
565 "lower_page_index = [0x%.16x] failed; rc = "
566 "[%d]\n", lower_page_index, rc);
567 }
568out:
569 if (rc && (*lower_page)) {
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800570 ecryptfs_release_lower_page(*lower_page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700571 (*lower_page) = NULL;
572 }
573 return rc;
574}
575
576/**
577 * ecryptfs_commit_lower_page
578 *
579 * Returns zero on success; non-zero on error
580 */
581int
582ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
583 struct file *lower_file, int byte_offset,
584 int region_size)
585{
586 int rc = 0;
587
588 rc = lower_inode->i_mapping->a_ops->commit_write(
589 lower_file, lower_page, byte_offset, region_size);
590 if (rc < 0) {
591 ecryptfs_printk(KERN_ERR,
592 "Error committing write; rc = [%d]\n", rc);
593 } else
594 rc = 0;
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800595 ecryptfs_release_lower_page(lower_page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700596 return rc;
597}
598
599/**
600 * ecryptfs_copy_page_to_lower
601 *
602 * Used for plaintext pass-through; no page index interpolation
603 * required.
604 */
605int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
606 struct file *lower_file)
607{
608 int rc = 0;
609 struct page *lower_page;
610
611 rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
612 page->index, 0, PAGE_CACHE_SIZE);
613 if (rc) {
614 ecryptfs_printk(KERN_ERR, "Error attempting to get page "
615 "at index [0x%.16x]\n", page->index);
616 goto out;
617 }
618 /* TODO: aops */
619 memcpy((char *)page_address(lower_page), page_address(page),
620 PAGE_CACHE_SIZE);
621 rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
622 0, PAGE_CACHE_SIZE);
623 if (rc)
624 ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
625 "at index [0x%.16x]\n", page->index);
626out:
627 return rc;
628}
629
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800630struct kmem_cache *ecryptfs_xattr_cache;
631
Michael Halcrow237fead2006-10-04 02:16:22 -0700632/**
633 * ecryptfs_commit_write
634 * @file: The eCryptfs file object
635 * @page: The eCryptfs page
636 * @from: Ignored (we rotate the page IV on each write)
637 * @to: Ignored
638 *
639 * This is where we encrypt the data and pass the encrypted data to
640 * the lower filesystem. In OpenPGP-compatible mode, we operate on
641 * entire underlying packets.
642 */
643static int ecryptfs_commit_write(struct file *file, struct page *page,
644 unsigned from, unsigned to)
645{
646 struct ecryptfs_page_crypt_context ctx;
647 loff_t pos;
648 struct inode *inode;
649 struct inode *lower_inode;
650 struct file *lower_file;
651 struct ecryptfs_crypt_stat *crypt_stat;
652 int rc;
653
654 inode = page->mapping->host;
655 lower_inode = ecryptfs_inode_to_lower(inode);
656 lower_file = ecryptfs_file_to_lower(file);
657 mutex_lock(&lower_inode->i_mutex);
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -0800658 crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
659 ->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700660 if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) {
661 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
662 "crypt_stat at memory location [%p]\n", crypt_stat);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800663 ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700664 } else
665 ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
666 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
667 "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
668 to);
669 rc = fill_zeros_to_end_of_page(page, to);
670 if (rc) {
671 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
672 "zeros in page with index = [0x%.16x]\n",
673 page->index);
674 goto out;
675 }
676 ctx.page = page;
677 ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE;
678 ctx.param.lower_file = lower_file;
679 rc = ecryptfs_encrypt_page(&ctx);
680 if (rc) {
681 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
682 "index [0x%.16x])\n", page->index);
683 goto out;
684 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700685 inode->i_blocks = lower_inode->i_blocks;
686 pos = (page->index << PAGE_CACHE_SHIFT) + to;
687 if (pos > i_size_read(inode)) {
688 i_size_write(inode, pos);
689 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
690 "[0x%.16x]\n", i_size_read(inode));
691 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800692 rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode,
693 inode, file->f_dentry,
694 ECRYPTFS_LOWER_I_MUTEX_HELD);
695 if (rc)
696 printk(KERN_ERR "Error writing inode size to metadata; "
697 "rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700698 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
699 mark_inode_dirty_sync(inode);
700out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700701 if (rc < 0)
702 ClearPageUptodate(page);
703 else
704 SetPageUptodate(page);
705 mutex_unlock(&lower_inode->i_mutex);
706 return rc;
707}
708
709/**
710 * write_zeros
711 * @file: The ecryptfs file
712 * @index: The index in which we are writing
713 * @start: The position after the last block of data
714 * @num_zeros: The number of zeros to write
715 *
716 * Write a specified number of zero's to a page.
717 *
718 * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
719 */
720static
721int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
722{
723 int rc = 0;
724 struct page *tmp_page;
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800725 char *tmp_page_virt;
Michael Halcrow237fead2006-10-04 02:16:22 -0700726
727 tmp_page = ecryptfs_get1page(file, index);
728 if (IS_ERR(tmp_page)) {
729 ecryptfs_printk(KERN_ERR, "Error getting page at index "
730 "[0x%.16x]\n", index);
731 rc = PTR_ERR(tmp_page);
732 goto out;
733 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700734 rc = ecryptfs_prepare_write(file, tmp_page, start, start + num_zeros);
735 if (rc) {
736 ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
737 "to remainder of page at index [0x%.16x]\n",
738 index);
Michael Halcrow237fead2006-10-04 02:16:22 -0700739 page_cache_release(tmp_page);
740 goto out;
741 }
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800742 tmp_page_virt = kmap_atomic(tmp_page, KM_USER0);
743 memset(((char *)tmp_page_virt + start), 0, num_zeros);
744 kunmap_atomic(tmp_page_virt, KM_USER0);
Michael Halcrow237fead2006-10-04 02:16:22 -0700745 rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
746 if (rc < 0) {
747 ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
748 "to remainder of page at index [0x%.16x]\n",
749 index);
Michael Halcrow237fead2006-10-04 02:16:22 -0700750 page_cache_release(tmp_page);
751 goto out;
752 }
753 rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700754 page_cache_release(tmp_page);
755out:
756 return rc;
757}
758
759static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
760{
761 int rc = 0;
762 struct inode *inode;
763 struct inode *lower_inode;
764
765 inode = (struct inode *)mapping->host;
766 lower_inode = ecryptfs_inode_to_lower(inode);
767 if (lower_inode->i_mapping->a_ops->bmap)
768 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
769 block);
770 return rc;
771}
772
773static void ecryptfs_sync_page(struct page *page)
774{
775 struct inode *inode;
776 struct inode *lower_inode;
777 struct page *lower_page;
778
779 inode = page->mapping->host;
780 lower_inode = ecryptfs_inode_to_lower(inode);
781 /* NOTE: Recently swapped with grab_cache_page(), since
782 * sync_page() just makes sure that pending I/O gets done. */
783 lower_page = find_lock_page(lower_inode->i_mapping, page->index);
784 if (!lower_page) {
785 ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n");
786 return;
787 }
788 lower_page->mapping->a_ops->sync_page(lower_page);
789 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
790 lower_page->index);
791 unlock_page(lower_page);
792 page_cache_release(lower_page);
793}
794
795struct address_space_operations ecryptfs_aops = {
796 .writepage = ecryptfs_writepage,
797 .readpage = ecryptfs_readpage,
798 .prepare_write = ecryptfs_prepare_write,
799 .commit_write = ecryptfs_commit_write,
800 .bmap = ecryptfs_bmap,
801 .sync_page = ecryptfs_sync_page,
802};