]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/ecryptfs/mmap.c
[PATCH] eCryptfs: convert f_op->write() to vfs_write()
[mirror_ubuntu-zesty-kernel.git] / fs / ecryptfs / mmap.c
CommitLineData
237fead6
MH
1/**
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
dd2a3b7a 9 * Copyright (C) 2004-2007 International Business Machines Corp.
237fead6
MH
10 * 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
bd243a4b 54 dentry = file->f_path.dentry;
237fead6
MH
55 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;
bd243a4b 87 struct dentry *dentry = file->f_path.dentry;
237fead6
MH
88 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
bd243a4b 221 dentry = file->f_path.dentry;
237fead6
MH
222 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);
237 page_data = (char *)kmap(page);
238 if (!page_data) {
239 rc = -ENOMEM;
240 ecryptfs_printk(KERN_ERR, "Error mapping page\n");
241 goto out;
242 }
243 lower_page_data = (char *)kmap(lower_page);
244 if (!lower_page_data) {
245 rc = -ENOMEM;
246 ecryptfs_printk(KERN_ERR, "Error mapping page\n");
247 kunmap(page);
248 goto out;
249 }
250 memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE);
251 kunmap(lower_page);
252 kunmap(page);
253 rc = 0;
254out:
255 if (likely(lower_page))
256 page_cache_release(lower_page);
257 if (rc == 0)
258 SetPageUptodate(page);
259 else
260 ClearPageUptodate(page);
261 return rc;
262}
e77a56dd
MH
263/**
264 * Header Extent:
265 * Octets 0-7: Unencrypted file size (big-endian)
266 * Octets 8-15: eCryptfs special marker
267 * Octets 16-19: Flags
268 * Octet 16: File format version number (between 0 and 255)
269 * Octets 17-18: Reserved
270 * Octet 19: Bit 1 (lsb): Reserved
271 * Bit 2: Encrypted?
272 * Bits 3-8: Reserved
273 * Octets 20-23: Header extent size (big-endian)
274 * Octets 24-25: Number of header extents at front of file
275 * (big-endian)
276 * Octet 26: Begin RFC 2440 authentication token packet set
277 */
278static void set_header_info(char *page_virt,
279 struct ecryptfs_crypt_stat *crypt_stat)
280{
281 size_t written;
282 int save_num_header_extents_at_front =
283 crypt_stat->num_header_extents_at_front;
284
285 crypt_stat->num_header_extents_at_front = 1;
286 ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
287 crypt_stat->num_header_extents_at_front =
288 save_num_header_extents_at_front;
289}
237fead6
MH
290
291/**
292 * ecryptfs_readpage
293 * @file: This is an ecryptfs file
294 * @page: ecryptfs associated page to stick the read data into
295 *
296 * Read in a page, decrypting if necessary.
297 *
298 * Returns zero on success; non-zero on error.
299 */
300static int ecryptfs_readpage(struct file *file, struct page *page)
301{
302 int rc = 0;
303 struct ecryptfs_crypt_stat *crypt_stat;
304
bd243a4b
JJS
305 BUG_ON(!(file && file->f_path.dentry && file->f_path.dentry->d_inode));
306 crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
307 ->crypt_stat;
237fead6
MH
308 if (!crypt_stat
309 || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)
310 || ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) {
311 ecryptfs_printk(KERN_DEBUG,
312 "Passing through unencrypted page\n");
313 rc = ecryptfs_do_readpage(file, page, page->index);
314 if (rc) {
315 ecryptfs_printk(KERN_ERR, "Error reading page; rc = "
316 "[%d]\n", rc);
317 goto out;
318 }
e77a56dd
MH
319 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
320 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
321 int num_pages_in_header_region =
322 (crypt_stat->header_extent_size
323 / PAGE_CACHE_SIZE);
324
325 if (page->index < num_pages_in_header_region) {
326 char *page_virt;
327
328 page_virt = (char *)kmap(page);
329 if (!page_virt) {
330 rc = -ENOMEM;
331 printk(KERN_ERR "Error mapping page\n");
332 goto out;
333 }
334 memset(page_virt, 0, PAGE_CACHE_SIZE);
335 if (page->index == 0) {
336 rc = ecryptfs_read_xattr_region(
337 page_virt, file->f_path.dentry);
338 set_header_info(page_virt, crypt_stat);
339 }
340 kunmap(page);
341 if (rc) {
342 printk(KERN_ERR "Error reading xattr "
343 "region\n");
344 goto out;
345 }
346 } else {
347 rc = ecryptfs_do_readpage(
348 file, page,
349 (page->index
350 - num_pages_in_header_region));
351 if (rc) {
352 printk(KERN_ERR "Error reading page; "
353 "rc = [%d]\n", rc);
354 goto out;
355 }
356 }
357 } else {
358 rc = ecryptfs_do_readpage(file, page, page->index);
359 if (rc) {
360 printk(KERN_ERR "Error reading page; rc = "
361 "[%d]\n", rc);
362 goto out;
363 }
364 }
237fead6
MH
365 } else {
366 rc = ecryptfs_decrypt_page(file, page);
367 if (rc) {
237fead6
MH
368 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
369 "rc = [%d]\n", rc);
370 goto out;
371 }
372 }
373 SetPageUptodate(page);
374out:
375 if (rc)
376 ClearPageUptodate(page);
377 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
378 page->index);
379 unlock_page(page);
380 return rc;
381}
382
dd2a3b7a
MH
383/**
384 * Called with lower inode mutex held.
385 */
237fead6
MH
386static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
387{
388 struct inode *inode = page->mapping->host;
389 int end_byte_in_page;
390 int rc = 0;
391 char *page_virt;
392
393 if ((i_size_read(inode) / PAGE_CACHE_SIZE) == page->index) {
394 end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
395 if (to > end_byte_in_page)
396 end_byte_in_page = to;
397 page_virt = kmap(page);
398 if (!page_virt) {
399 rc = -ENOMEM;
400 ecryptfs_printk(KERN_WARNING,
401 "Could not map page\n");
402 goto out;
403 }
404 memset((page_virt + end_byte_in_page), 0,
405 (PAGE_CACHE_SIZE - end_byte_in_page));
406 kunmap(page);
407 }
408out:
409 return rc;
410}
411
412static int ecryptfs_prepare_write(struct file *file, struct page *page,
413 unsigned from, unsigned to)
414{
415 int rc = 0;
416
417 kmap(page);
418 if (from == 0 && to == PAGE_CACHE_SIZE)
419 goto out; /* If we are writing a full page, it will be
420 up to date. */
421 if (!PageUptodate(page))
422 rc = ecryptfs_do_readpage(file, page, page->index);
423out:
424 return rc;
425}
426
427int ecryptfs_grab_and_map_lower_page(struct page **lower_page,
428 char **lower_virt,
429 struct inode *lower_inode,
430 unsigned long lower_page_index)
431{
432 int rc = 0;
433
434 (*lower_page) = grab_cache_page(lower_inode->i_mapping,
435 lower_page_index);
436 if (!(*lower_page)) {
437 ecryptfs_printk(KERN_ERR, "grab_cache_page for "
438 "lower_page_index = [0x%.16x] failed\n",
439 lower_page_index);
440 rc = -EINVAL;
441 goto out;
442 }
443 if (lower_virt)
444 (*lower_virt) = kmap((*lower_page));
445 else
446 kmap((*lower_page));
447out:
448 return rc;
449}
450
451int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
452 struct inode *lower_inode,
453 struct writeback_control *wbc)
454{
455 int rc = 0;
456
457 rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
458 if (rc) {
459 ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
460 "rc = [%d]\n", rc);
461 goto out;
462 }
463 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
464 page_cache_release(lower_page);
465out:
466 return rc;
467}
468
469static void ecryptfs_unmap_and_release_lower_page(struct page *lower_page)
470{
471 kunmap(lower_page);
472 ecryptfs_printk(KERN_DEBUG, "Unlocking lower page with index = "
473 "[0x%.16x]\n", lower_page->index);
474 unlock_page(lower_page);
475 page_cache_release(lower_page);
476}
477
478/**
479 * ecryptfs_write_inode_size_to_header
480 *
481 * Writes the lower file size to the first 8 bytes of the header.
482 *
483 * Returns zero on success; non-zero on error.
484 */
dd2a3b7a
MH
485static int ecryptfs_write_inode_size_to_header(struct file *lower_file,
486 struct inode *lower_inode,
487 struct inode *inode)
237fead6
MH
488{
489 int rc = 0;
490 struct page *header_page;
491 char *header_virt;
492 const struct address_space_operations *lower_a_ops;
493 u64 file_size;
494
495 rc = ecryptfs_grab_and_map_lower_page(&header_page, &header_virt,
496 lower_inode, 0);
497 if (rc) {
498 ecryptfs_printk(KERN_ERR, "grab_cache_page for header page "
499 "failed\n");
500 goto out;
501 }
502 lower_a_ops = lower_inode->i_mapping->a_ops;
503 rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8);
504 file_size = (u64)i_size_read(inode);
505 ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size);
506 file_size = cpu_to_be64(file_size);
507 memcpy(header_virt, &file_size, sizeof(u64));
508 rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8);
509 if (rc < 0)
510 ecryptfs_printk(KERN_ERR, "Error commiting header page "
511 "write\n");
512 ecryptfs_unmap_and_release_lower_page(header_page);
513 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
514 mark_inode_dirty_sync(inode);
515out:
516 return rc;
517}
518
dd2a3b7a
MH
519static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode,
520 struct inode *inode,
521 struct dentry *ecryptfs_dentry,
522 int lower_i_mutex_held)
523{
524 ssize_t size;
525 void *xattr_virt;
526 struct dentry *lower_dentry;
527 u64 file_size;
528 int rc;
529
530 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
531 if (!xattr_virt) {
532 printk(KERN_ERR "Out of memory whilst attempting to write "
533 "inode size to xattr\n");
534 rc = -ENOMEM;
535 goto out;
536 }
537 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
538 if (!lower_dentry->d_inode->i_op->getxattr) {
539 printk(KERN_WARNING
540 "No support for setting xattr in lower filesystem\n");
541 rc = -ENOSYS;
542 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
543 goto out;
544 }
545 if (!lower_i_mutex_held)
546 mutex_lock(&lower_dentry->d_inode->i_mutex);
547 size = lower_dentry->d_inode->i_op->getxattr(lower_dentry,
548 ECRYPTFS_XATTR_NAME,
549 xattr_virt,
550 PAGE_CACHE_SIZE);
551 if (!lower_i_mutex_held)
552 mutex_unlock(&lower_dentry->d_inode->i_mutex);
553 if (size < 0)
554 size = 8;
555 file_size = (u64)i_size_read(inode);
556 file_size = cpu_to_be64(file_size);
557 memcpy(xattr_virt, &file_size, sizeof(u64));
558 if (!lower_i_mutex_held)
559 mutex_lock(&lower_dentry->d_inode->i_mutex);
560 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry,
561 ECRYPTFS_XATTR_NAME,
562 xattr_virt, size, 0);
563 if (!lower_i_mutex_held)
564 mutex_unlock(&lower_dentry->d_inode->i_mutex);
565 if (rc)
566 printk(KERN_ERR "Error whilst attempting to write inode size "
567 "to lower file xattr; rc = [%d]\n", rc);
568 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
569out:
570 return rc;
571}
572
573int
574ecryptfs_write_inode_size_to_metadata(struct file *lower_file,
575 struct inode *lower_inode,
576 struct inode *inode,
577 struct dentry *ecryptfs_dentry,
578 int lower_i_mutex_held)
579{
580 struct ecryptfs_crypt_stat *crypt_stat;
581
582 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
583 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
584 return ecryptfs_write_inode_size_to_xattr(lower_inode, inode,
585 ecryptfs_dentry,
586 lower_i_mutex_held);
587 else
588 return ecryptfs_write_inode_size_to_header(lower_file,
589 lower_inode,
590 inode);
591}
592
237fead6
MH
593int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
594 struct file *lower_file,
595 unsigned long lower_page_index, int byte_offset,
596 int region_bytes)
597{
598 int rc = 0;
599
600 rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL, lower_inode,
601 lower_page_index);
602 if (rc) {
603 ecryptfs_printk(KERN_ERR, "Error attempting to grab and map "
604 "lower page with index [0x%.16x]\n",
605 lower_page_index);
606 goto out;
607 }
608 rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
609 (*lower_page),
610 byte_offset,
611 region_bytes);
612 if (rc) {
613 ecryptfs_printk(KERN_ERR, "prepare_write for "
614 "lower_page_index = [0x%.16x] failed; rc = "
615 "[%d]\n", lower_page_index, rc);
616 }
617out:
618 if (rc && (*lower_page)) {
619 ecryptfs_unmap_and_release_lower_page(*lower_page);
620 (*lower_page) = NULL;
621 }
622 return rc;
623}
624
625/**
626 * ecryptfs_commit_lower_page
627 *
628 * Returns zero on success; non-zero on error
629 */
630int
631ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
632 struct file *lower_file, int byte_offset,
633 int region_size)
634{
635 int rc = 0;
636
637 rc = lower_inode->i_mapping->a_ops->commit_write(
638 lower_file, lower_page, byte_offset, region_size);
639 if (rc < 0) {
640 ecryptfs_printk(KERN_ERR,
641 "Error committing write; rc = [%d]\n", rc);
642 } else
643 rc = 0;
644 ecryptfs_unmap_and_release_lower_page(lower_page);
645 return rc;
646}
647
648/**
649 * ecryptfs_copy_page_to_lower
650 *
651 * Used for plaintext pass-through; no page index interpolation
652 * required.
653 */
654int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
655 struct file *lower_file)
656{
657 int rc = 0;
658 struct page *lower_page;
659
660 rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
661 page->index, 0, PAGE_CACHE_SIZE);
662 if (rc) {
663 ecryptfs_printk(KERN_ERR, "Error attempting to get page "
664 "at index [0x%.16x]\n", page->index);
665 goto out;
666 }
667 /* TODO: aops */
668 memcpy((char *)page_address(lower_page), page_address(page),
669 PAGE_CACHE_SIZE);
670 rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
671 0, PAGE_CACHE_SIZE);
672 if (rc)
673 ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
674 "at index [0x%.16x]\n", page->index);
675out:
676 return rc;
677}
678
dd2a3b7a
MH
679struct kmem_cache *ecryptfs_xattr_cache;
680
237fead6
MH
681/**
682 * ecryptfs_commit_write
683 * @file: The eCryptfs file object
684 * @page: The eCryptfs page
685 * @from: Ignored (we rotate the page IV on each write)
686 * @to: Ignored
687 *
688 * This is where we encrypt the data and pass the encrypted data to
689 * the lower filesystem. In OpenPGP-compatible mode, we operate on
690 * entire underlying packets.
691 */
692static int ecryptfs_commit_write(struct file *file, struct page *page,
693 unsigned from, unsigned to)
694{
695 struct ecryptfs_page_crypt_context ctx;
696 loff_t pos;
697 struct inode *inode;
698 struct inode *lower_inode;
699 struct file *lower_file;
700 struct ecryptfs_crypt_stat *crypt_stat;
701 int rc;
702
703 inode = page->mapping->host;
704 lower_inode = ecryptfs_inode_to_lower(inode);
705 lower_file = ecryptfs_file_to_lower(file);
706 mutex_lock(&lower_inode->i_mutex);
bd243a4b
JJS
707 crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
708 ->crypt_stat;
237fead6
MH
709 if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) {
710 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
711 "crypt_stat at memory location [%p]\n", crypt_stat);
dddfa461 712 ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE);
237fead6
MH
713 } else
714 ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
715 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
716 "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
717 to);
718 rc = fill_zeros_to_end_of_page(page, to);
719 if (rc) {
720 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
721 "zeros in page with index = [0x%.16x]\n",
722 page->index);
723 goto out;
724 }
725 ctx.page = page;
726 ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE;
727 ctx.param.lower_file = lower_file;
728 rc = ecryptfs_encrypt_page(&ctx);
729 if (rc) {
730 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
731 "index [0x%.16x])\n", page->index);
732 goto out;
733 }
237fead6
MH
734 inode->i_blocks = lower_inode->i_blocks;
735 pos = (page->index << PAGE_CACHE_SHIFT) + to;
736 if (pos > i_size_read(inode)) {
737 i_size_write(inode, pos);
738 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
739 "[0x%.16x]\n", i_size_read(inode));
740 }
dd2a3b7a
MH
741 rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode,
742 inode, file->f_dentry,
743 ECRYPTFS_LOWER_I_MUTEX_HELD);
744 if (rc)
745 printk(KERN_ERR "Error writing inode size to metadata; "
746 "rc = [%d]\n", rc);
237fead6
MH
747 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
748 mark_inode_dirty_sync(inode);
749out:
750 kunmap(page); /* mapped in prior call (prepare_write) */
751 if (rc < 0)
752 ClearPageUptodate(page);
753 else
754 SetPageUptodate(page);
755 mutex_unlock(&lower_inode->i_mutex);
756 return rc;
757}
758
759/**
760 * write_zeros
761 * @file: The ecryptfs file
762 * @index: The index in which we are writing
763 * @start: The position after the last block of data
764 * @num_zeros: The number of zeros to write
765 *
766 * Write a specified number of zero's to a page.
767 *
768 * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
769 */
770static
771int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
772{
773 int rc = 0;
774 struct page *tmp_page;
775
776 tmp_page = ecryptfs_get1page(file, index);
777 if (IS_ERR(tmp_page)) {
778 ecryptfs_printk(KERN_ERR, "Error getting page at index "
779 "[0x%.16x]\n", index);
780 rc = PTR_ERR(tmp_page);
781 goto out;
782 }
783 kmap(tmp_page);
784 rc = ecryptfs_prepare_write(file, tmp_page, start, start + num_zeros);
785 if (rc) {
786 ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
787 "to remainder of page at index [0x%.16x]\n",
788 index);
789 kunmap(tmp_page);
790 page_cache_release(tmp_page);
791 goto out;
792 }
793 memset(((char *)page_address(tmp_page) + start), 0, num_zeros);
794 rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
795 if (rc < 0) {
796 ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
797 "to remainder of page at index [0x%.16x]\n",
798 index);
799 kunmap(tmp_page);
800 page_cache_release(tmp_page);
801 goto out;
802 }
803 rc = 0;
804 kunmap(tmp_page);
805 page_cache_release(tmp_page);
806out:
807 return rc;
808}
809
810static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
811{
812 int rc = 0;
813 struct inode *inode;
814 struct inode *lower_inode;
815
816 inode = (struct inode *)mapping->host;
817 lower_inode = ecryptfs_inode_to_lower(inode);
818 if (lower_inode->i_mapping->a_ops->bmap)
819 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
820 block);
821 return rc;
822}
823
824static void ecryptfs_sync_page(struct page *page)
825{
826 struct inode *inode;
827 struct inode *lower_inode;
828 struct page *lower_page;
829
830 inode = page->mapping->host;
831 lower_inode = ecryptfs_inode_to_lower(inode);
832 /* NOTE: Recently swapped with grab_cache_page(), since
833 * sync_page() just makes sure that pending I/O gets done. */
834 lower_page = find_lock_page(lower_inode->i_mapping, page->index);
835 if (!lower_page) {
836 ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n");
837 return;
838 }
839 lower_page->mapping->a_ops->sync_page(lower_page);
840 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
841 lower_page->index);
842 unlock_page(lower_page);
843 page_cache_release(lower_page);
844}
845
846struct address_space_operations ecryptfs_aops = {
847 .writepage = ecryptfs_writepage,
848 .readpage = ecryptfs_readpage,
849 .prepare_write = ecryptfs_prepare_write,
850 .commit_write = ecryptfs_commit_write,
851 .bmap = ecryptfs_bmap,
852 .sync_page = ecryptfs_sync_page,
853};