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.
7 * Copyright (C) 1997-2003 Erez Zadok
8 * Copyright (C) 2001-2003 Stony Brook University
9 * Copyright (C) 2004-2007 International Business Machines Corp.
10 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
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.
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.
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
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"
37 struct kmem_cache *ecryptfs_lower_page_cache;
42 * Get one page from cache or lower f/s, return error otherwise.
44 * Returns unlocked and up-to-date page (if ok), with increased
47 static struct page *ecryptfs_get1page(struct file *file, int index)
50 struct dentry *dentry;
52 struct address_space *mapping;
54 dentry = file->f_path.dentry;
55 inode = dentry->d_inode;
56 mapping = inode->i_mapping;
57 page = read_cache_page(mapping, index,
58 (filler_t *)mapping->a_ops->readpage,
62 wait_on_page_locked(page);
68 int write_zeros(struct file *file, pgoff_t index, int start, int num_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
78 * Function for handling lseek-ing past the end of the file.
80 * This function does not support shrinking, only growing a file.
82 * Returns zero on success; non-zero otherwise.
84 int ecryptfs_fill_zeros(struct file *file, loff_t new_length)
87 struct dentry *dentry = file->f_path.dentry;
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);
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);
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));
115 ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
117 "old_end_pos_in_page=[d], "
118 "(PAGE_CACHE_SIZE - new_end_pos_in_page"
120 ")=[d]) returned [%d]\n", file, index,
123 (PAGE_CACHE_SIZE - new_end_pos_in_page),
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));
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,
136 (PAGE_CACHE_SIZE - old_end_pos_in_page), rc);
140 while (index < new_end_page_index) {
141 /* Fill all intermediate pages with zeros */
142 rc = write_zeros(file, index, 0, PAGE_CACHE_SIZE);
144 ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
146 "old_end_pos_in_page=[d], "
147 "(PAGE_CACHE_SIZE - new_end_pos_in_page"
149 ")=[d]) returned [%d]\n", file, index,
152 (PAGE_CACHE_SIZE - new_end_pos_in_page),
158 /* Fill the portion at the beginning of the last new page with
160 rc = write_zeros(file, index, 0, (new_end_pos_in_page + 1));
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);
175 * @page: Page that is locked before this call is made
177 * Returns zero on success; non-zero otherwise
179 static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
181 struct ecryptfs_page_crypt_context ctx;
185 ctx.mode = ECRYPTFS_WRITEPAGE_MODE;
187 rc = ecryptfs_encrypt_page(&ctx);
189 ecryptfs_printk(KERN_WARNING, "Error encrypting "
190 "page (upper index [0x%.16x])\n", page->index);
191 ClearPageUptodate(page);
194 SetPageUptodate(page);
201 * Reads the data from the lower file file at index lower_page_index
202 * and copies that data into page.
204 * @param page Page to fill
205 * @param lower_page_index Index of the page in the lower file to get
207 int ecryptfs_do_readpage(struct file *file, struct page *page,
208 pgoff_t lower_page_index)
211 struct dentry *dentry;
212 struct file *lower_file;
213 struct dentry *lower_dentry;
215 struct inode *lower_inode;
217 struct page *lower_page = NULL;
218 char *lower_page_data;
219 const struct address_space_operations *lower_a_ops;
221 dentry = file->f_path.dentry;
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,
230 if (IS_ERR(lower_page)) {
231 rc = PTR_ERR(lower_page);
233 ecryptfs_printk(KERN_ERR, "Error reading from page cache\n");
236 wait_on_page_locked(lower_page);
237 page_data = (char *)kmap(page);
240 ecryptfs_printk(KERN_ERR, "Error mapping page\n");
243 lower_page_data = (char *)kmap(lower_page);
244 if (!lower_page_data) {
246 ecryptfs_printk(KERN_ERR, "Error mapping page\n");
250 memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE);
255 if (likely(lower_page))
256 page_cache_release(lower_page);
258 SetPageUptodate(page);
260 ClearPageUptodate(page);
266 * @file: This is an ecryptfs file
267 * @page: ecryptfs associated page to stick the read data into
269 * Read in a page, decrypting if necessary.
271 * Returns zero on success; non-zero on error.
273 static int ecryptfs_readpage(struct file *file, struct page *page)
276 struct ecryptfs_crypt_stat *crypt_stat;
278 BUG_ON(!(file && file->f_path.dentry && file->f_path.dentry->d_inode));
279 crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
282 || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)
283 || ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) {
284 ecryptfs_printk(KERN_DEBUG,
285 "Passing through unencrypted page\n");
286 rc = ecryptfs_do_readpage(file, page, page->index);
288 ecryptfs_printk(KERN_ERR, "Error reading page; rc = "
293 rc = ecryptfs_decrypt_page(file, page);
296 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
301 SetPageUptodate(page);
304 ClearPageUptodate(page);
305 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
312 * Called with lower inode mutex held.
314 static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
316 struct inode *inode = page->mapping->host;
317 int end_byte_in_page;
321 if ((i_size_read(inode) / PAGE_CACHE_SIZE) == page->index) {
322 end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
323 if (to > end_byte_in_page)
324 end_byte_in_page = to;
325 page_virt = kmap(page);
328 ecryptfs_printk(KERN_WARNING,
329 "Could not map page\n");
332 memset((page_virt + end_byte_in_page), 0,
333 (PAGE_CACHE_SIZE - end_byte_in_page));
340 static int ecryptfs_prepare_write(struct file *file, struct page *page,
341 unsigned from, unsigned to)
346 if (from == 0 && to == PAGE_CACHE_SIZE)
347 goto out; /* If we are writing a full page, it will be
349 if (!PageUptodate(page))
350 rc = ecryptfs_do_readpage(file, page, page->index);
355 int ecryptfs_grab_and_map_lower_page(struct page **lower_page,
357 struct inode *lower_inode,
358 unsigned long lower_page_index)
362 (*lower_page) = grab_cache_page(lower_inode->i_mapping,
364 if (!(*lower_page)) {
365 ecryptfs_printk(KERN_ERR, "grab_cache_page for "
366 "lower_page_index = [0x%.16x] failed\n",
372 (*lower_virt) = kmap((*lower_page));
379 int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
380 struct inode *lower_inode,
381 struct writeback_control *wbc)
385 rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
387 ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
391 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
392 page_cache_release(lower_page);
397 static void ecryptfs_unmap_and_release_lower_page(struct page *lower_page)
400 ecryptfs_printk(KERN_DEBUG, "Unlocking lower page with index = "
401 "[0x%.16x]\n", lower_page->index);
402 unlock_page(lower_page);
403 page_cache_release(lower_page);
407 * ecryptfs_write_inode_size_to_header
409 * Writes the lower file size to the first 8 bytes of the header.
411 * Returns zero on success; non-zero on error.
413 static int ecryptfs_write_inode_size_to_header(struct file *lower_file,
414 struct inode *lower_inode,
418 struct page *header_page;
420 const struct address_space_operations *lower_a_ops;
423 rc = ecryptfs_grab_and_map_lower_page(&header_page, &header_virt,
426 ecryptfs_printk(KERN_ERR, "grab_cache_page for header page "
430 lower_a_ops = lower_inode->i_mapping->a_ops;
431 rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8);
432 file_size = (u64)i_size_read(inode);
433 ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size);
434 file_size = cpu_to_be64(file_size);
435 memcpy(header_virt, &file_size, sizeof(u64));
436 rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8);
438 ecryptfs_printk(KERN_ERR, "Error commiting header page "
440 ecryptfs_unmap_and_release_lower_page(header_page);
441 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
442 mark_inode_dirty_sync(inode);
447 static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode,
449 struct dentry *ecryptfs_dentry,
450 int lower_i_mutex_held)
454 struct dentry *lower_dentry;
458 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
460 printk(KERN_ERR "Out of memory whilst attempting to write "
461 "inode size to xattr\n");
465 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
466 if (!lower_dentry->d_inode->i_op->getxattr) {
468 "No support for setting xattr in lower filesystem\n");
470 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
473 if (!lower_i_mutex_held)
474 mutex_lock(&lower_dentry->d_inode->i_mutex);
475 size = lower_dentry->d_inode->i_op->getxattr(lower_dentry,
479 if (!lower_i_mutex_held)
480 mutex_unlock(&lower_dentry->d_inode->i_mutex);
483 file_size = (u64)i_size_read(inode);
484 file_size = cpu_to_be64(file_size);
485 memcpy(xattr_virt, &file_size, sizeof(u64));
486 if (!lower_i_mutex_held)
487 mutex_lock(&lower_dentry->d_inode->i_mutex);
488 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry,
490 xattr_virt, size, 0);
491 if (!lower_i_mutex_held)
492 mutex_unlock(&lower_dentry->d_inode->i_mutex);
494 printk(KERN_ERR "Error whilst attempting to write inode size "
495 "to lower file xattr; rc = [%d]\n", rc);
496 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
502 ecryptfs_write_inode_size_to_metadata(struct file *lower_file,
503 struct inode *lower_inode,
505 struct dentry *ecryptfs_dentry,
506 int lower_i_mutex_held)
508 struct ecryptfs_crypt_stat *crypt_stat;
510 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
511 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
512 return ecryptfs_write_inode_size_to_xattr(lower_inode, inode,
516 return ecryptfs_write_inode_size_to_header(lower_file,
521 int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
522 struct file *lower_file,
523 unsigned long lower_page_index, int byte_offset,
528 rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL, lower_inode,
531 ecryptfs_printk(KERN_ERR, "Error attempting to grab and map "
532 "lower page with index [0x%.16x]\n",
536 rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
541 ecryptfs_printk(KERN_ERR, "prepare_write for "
542 "lower_page_index = [0x%.16x] failed; rc = "
543 "[%d]\n", lower_page_index, rc);
546 if (rc && (*lower_page)) {
547 ecryptfs_unmap_and_release_lower_page(*lower_page);
548 (*lower_page) = NULL;
554 * ecryptfs_commit_lower_page
556 * Returns zero on success; non-zero on error
559 ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
560 struct file *lower_file, int byte_offset,
565 rc = lower_inode->i_mapping->a_ops->commit_write(
566 lower_file, lower_page, byte_offset, region_size);
568 ecryptfs_printk(KERN_ERR,
569 "Error committing write; rc = [%d]\n", rc);
572 ecryptfs_unmap_and_release_lower_page(lower_page);
577 * ecryptfs_copy_page_to_lower
579 * Used for plaintext pass-through; no page index interpolation
582 int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
583 struct file *lower_file)
586 struct page *lower_page;
588 rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
589 page->index, 0, PAGE_CACHE_SIZE);
591 ecryptfs_printk(KERN_ERR, "Error attempting to get page "
592 "at index [0x%.16x]\n", page->index);
596 memcpy((char *)page_address(lower_page), page_address(page),
598 rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
601 ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
602 "at index [0x%.16x]\n", page->index);
607 struct kmem_cache *ecryptfs_xattr_cache;
610 * ecryptfs_commit_write
611 * @file: The eCryptfs file object
612 * @page: The eCryptfs page
613 * @from: Ignored (we rotate the page IV on each write)
616 * This is where we encrypt the data and pass the encrypted data to
617 * the lower filesystem. In OpenPGP-compatible mode, we operate on
618 * entire underlying packets.
620 static int ecryptfs_commit_write(struct file *file, struct page *page,
621 unsigned from, unsigned to)
623 struct ecryptfs_page_crypt_context ctx;
626 struct inode *lower_inode;
627 struct file *lower_file;
628 struct ecryptfs_crypt_stat *crypt_stat;
631 inode = page->mapping->host;
632 lower_inode = ecryptfs_inode_to_lower(inode);
633 lower_file = ecryptfs_file_to_lower(file);
634 mutex_lock(&lower_inode->i_mutex);
635 crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
637 if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) {
638 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
639 "crypt_stat at memory location [%p]\n", crypt_stat);
640 ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE);
642 ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
643 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
644 "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
646 rc = fill_zeros_to_end_of_page(page, to);
648 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
649 "zeros in page with index = [0x%.16x]\n",
654 ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE;
655 ctx.param.lower_file = lower_file;
656 rc = ecryptfs_encrypt_page(&ctx);
658 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
659 "index [0x%.16x])\n", page->index);
662 inode->i_blocks = lower_inode->i_blocks;
663 pos = (page->index << PAGE_CACHE_SHIFT) + to;
664 if (pos > i_size_read(inode)) {
665 i_size_write(inode, pos);
666 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
667 "[0x%.16x]\n", i_size_read(inode));
669 rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode,
670 inode, file->f_dentry,
671 ECRYPTFS_LOWER_I_MUTEX_HELD);
673 printk(KERN_ERR "Error writing inode size to metadata; "
675 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
676 mark_inode_dirty_sync(inode);
678 kunmap(page); /* mapped in prior call (prepare_write) */
680 ClearPageUptodate(page);
682 SetPageUptodate(page);
683 mutex_unlock(&lower_inode->i_mutex);
689 * @file: The ecryptfs file
690 * @index: The index in which we are writing
691 * @start: The position after the last block of data
692 * @num_zeros: The number of zeros to write
694 * Write a specified number of zero's to a page.
696 * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
699 int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
702 struct page *tmp_page;
704 tmp_page = ecryptfs_get1page(file, index);
705 if (IS_ERR(tmp_page)) {
706 ecryptfs_printk(KERN_ERR, "Error getting page at index "
707 "[0x%.16x]\n", index);
708 rc = PTR_ERR(tmp_page);
712 rc = ecryptfs_prepare_write(file, tmp_page, start, start + num_zeros);
714 ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
715 "to remainder of page at index [0x%.16x]\n",
718 page_cache_release(tmp_page);
721 memset(((char *)page_address(tmp_page) + start), 0, num_zeros);
722 rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
724 ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
725 "to remainder of page at index [0x%.16x]\n",
728 page_cache_release(tmp_page);
733 page_cache_release(tmp_page);
738 static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
742 struct inode *lower_inode;
744 inode = (struct inode *)mapping->host;
745 lower_inode = ecryptfs_inode_to_lower(inode);
746 if (lower_inode->i_mapping->a_ops->bmap)
747 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
752 static void ecryptfs_sync_page(struct page *page)
755 struct inode *lower_inode;
756 struct page *lower_page;
758 inode = page->mapping->host;
759 lower_inode = ecryptfs_inode_to_lower(inode);
760 /* NOTE: Recently swapped with grab_cache_page(), since
761 * sync_page() just makes sure that pending I/O gets done. */
762 lower_page = find_lock_page(lower_inode->i_mapping, page->index);
764 ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n");
767 lower_page->mapping->a_ops->sync_page(lower_page);
768 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
770 unlock_page(lower_page);
771 page_cache_release(lower_page);
774 struct address_space_operations ecryptfs_aops = {
775 .writepage = ecryptfs_writepage,
776 .readpage = ecryptfs_readpage,
777 .prepare_write = ecryptfs_prepare_write,
778 .commit_write = ecryptfs_commit_write,
779 .bmap = ecryptfs_bmap,
780 .sync_page = ecryptfs_sync_page,