xref: /openbmc/linux/fs/ecryptfs/mmap.c (revision c9f2875b7968453a852e1de5d3504fd2241e8989)
1237fead6SMichael Halcrow /**
2237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3237fead6SMichael Halcrow  * This is where eCryptfs coordinates the symmetric encryption and
4237fead6SMichael Halcrow  * decryption of the file data as it passes between the lower
5237fead6SMichael Halcrow  * encrypted file and the upper decrypted file.
6237fead6SMichael Halcrow  *
7237fead6SMichael Halcrow  * Copyright (C) 1997-2003 Erez Zadok
8237fead6SMichael Halcrow  * Copyright (C) 2001-2003 Stony Brook University
9dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
10237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
11237fead6SMichael Halcrow  *
12237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
13237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
14237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
15237fead6SMichael Halcrow  * License, or (at your option) any later version.
16237fead6SMichael Halcrow  *
17237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
18237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
19237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20237fead6SMichael Halcrow  * General Public License for more details.
21237fead6SMichael Halcrow  *
22237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
23237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
24237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25237fead6SMichael Halcrow  * 02111-1307, USA.
26237fead6SMichael Halcrow  */
27237fead6SMichael Halcrow 
28237fead6SMichael Halcrow #include <linux/pagemap.h>
29237fead6SMichael Halcrow #include <linux/writeback.h>
30237fead6SMichael Halcrow #include <linux/page-flags.h>
31237fead6SMichael Halcrow #include <linux/mount.h>
32237fead6SMichael Halcrow #include <linux/file.h>
33237fead6SMichael Halcrow #include <linux/crypto.h>
34237fead6SMichael Halcrow #include <linux/scatterlist.h>
35237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
36237fead6SMichael Halcrow 
37237fead6SMichael Halcrow struct kmem_cache *ecryptfs_lower_page_cache;
38237fead6SMichael Halcrow 
39237fead6SMichael Halcrow /**
40237fead6SMichael Halcrow  * ecryptfs_get1page
41237fead6SMichael Halcrow  *
42237fead6SMichael Halcrow  * Get one page from cache or lower f/s, return error otherwise.
43237fead6SMichael Halcrow  *
44237fead6SMichael Halcrow  * Returns unlocked and up-to-date page (if ok), with increased
45237fead6SMichael Halcrow  * refcnt.
46237fead6SMichael Halcrow  */
47237fead6SMichael Halcrow static struct page *ecryptfs_get1page(struct file *file, int index)
48237fead6SMichael Halcrow {
49237fead6SMichael Halcrow 	struct dentry *dentry;
50237fead6SMichael Halcrow 	struct inode *inode;
51237fead6SMichael Halcrow 	struct address_space *mapping;
52237fead6SMichael Halcrow 
53bd243a4bSJosef "Jeff" Sipek 	dentry = file->f_path.dentry;
54237fead6SMichael Halcrow 	inode = dentry->d_inode;
55237fead6SMichael Halcrow 	mapping = inode->i_mapping;
566fe6900eSNick Piggin 	return read_mapping_page(mapping, index, (void *)file);
57237fead6SMichael Halcrow }
58237fead6SMichael Halcrow 
59237fead6SMichael Halcrow static
60237fead6SMichael Halcrow int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros);
61237fead6SMichael Halcrow 
62237fead6SMichael Halcrow /**
63237fead6SMichael Halcrow  * ecryptfs_fill_zeros
64237fead6SMichael Halcrow  * @file: The ecryptfs file
65237fead6SMichael Halcrow  * @new_length: The new length of the data in the underlying file;
66237fead6SMichael Halcrow  *              everything between the prior end of the file and the
67237fead6SMichael Halcrow  *              new end of the file will be filled with zero's.
68237fead6SMichael Halcrow  *              new_length must be greater than  current length
69237fead6SMichael Halcrow  *
70237fead6SMichael Halcrow  * Function for handling lseek-ing past the end of the file.
71237fead6SMichael Halcrow  *
72237fead6SMichael Halcrow  * This function does not support shrinking, only growing a file.
73237fead6SMichael Halcrow  *
74237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise.
75237fead6SMichael Halcrow  */
76237fead6SMichael Halcrow int ecryptfs_fill_zeros(struct file *file, loff_t new_length)
77237fead6SMichael Halcrow {
78237fead6SMichael Halcrow 	int rc = 0;
79bd243a4bSJosef "Jeff" Sipek 	struct dentry *dentry = file->f_path.dentry;
80237fead6SMichael Halcrow 	struct inode *inode = dentry->d_inode;
81237fead6SMichael Halcrow 	pgoff_t old_end_page_index = 0;
82237fead6SMichael Halcrow 	pgoff_t index = old_end_page_index;
83237fead6SMichael Halcrow 	int old_end_pos_in_page = -1;
84237fead6SMichael Halcrow 	pgoff_t new_end_page_index;
85237fead6SMichael Halcrow 	int new_end_pos_in_page;
86237fead6SMichael Halcrow 	loff_t cur_length = i_size_read(inode);
87237fead6SMichael Halcrow 
88237fead6SMichael Halcrow 	if (cur_length != 0) {
89237fead6SMichael Halcrow 		index = old_end_page_index =
90237fead6SMichael Halcrow 		    ((cur_length - 1) >> PAGE_CACHE_SHIFT);
91237fead6SMichael Halcrow 		old_end_pos_in_page = ((cur_length - 1) & ~PAGE_CACHE_MASK);
92237fead6SMichael Halcrow 	}
93237fead6SMichael Halcrow 	new_end_page_index = ((new_length - 1) >> PAGE_CACHE_SHIFT);
94237fead6SMichael Halcrow 	new_end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK);
95237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "old_end_page_index = [0x%.16x]; "
96237fead6SMichael Halcrow 			"old_end_pos_in_page = [%d]; "
97237fead6SMichael Halcrow 			"new_end_page_index = [0x%.16x]; "
98237fead6SMichael Halcrow 			"new_end_pos_in_page = [%d]\n",
99237fead6SMichael Halcrow 			old_end_page_index, old_end_pos_in_page,
100237fead6SMichael Halcrow 			new_end_page_index, new_end_pos_in_page);
101237fead6SMichael Halcrow 	if (old_end_page_index == new_end_page_index) {
102237fead6SMichael Halcrow 		/* Start and end are in the same page; we just need to
103237fead6SMichael Halcrow 		 * set a portion of the existing page to zero's */
104237fead6SMichael Halcrow 		rc = write_zeros(file, index, (old_end_pos_in_page + 1),
105237fead6SMichael Halcrow 				 (new_end_pos_in_page - old_end_pos_in_page));
106237fead6SMichael Halcrow 		if (rc)
107237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
108237fead6SMichael Halcrow 					"index=[0x%.16x], "
109237fead6SMichael Halcrow 					"old_end_pos_in_page=[d], "
110237fead6SMichael Halcrow 					"(PAGE_CACHE_SIZE - new_end_pos_in_page"
111237fead6SMichael Halcrow 					"=[%d]"
112237fead6SMichael Halcrow 					")=[d]) returned [%d]\n", file, index,
113237fead6SMichael Halcrow 					old_end_pos_in_page,
114237fead6SMichael Halcrow 					new_end_pos_in_page,
115237fead6SMichael Halcrow 					(PAGE_CACHE_SIZE - new_end_pos_in_page),
116237fead6SMichael Halcrow 					rc);
117237fead6SMichael Halcrow 		goto out;
118237fead6SMichael Halcrow 	}
119237fead6SMichael Halcrow 	/* Fill the remainder of the previous last page with zeros */
120237fead6SMichael Halcrow 	rc = write_zeros(file, index, (old_end_pos_in_page + 1),
121237fead6SMichael Halcrow 			 ((PAGE_CACHE_SIZE - 1) - old_end_pos_in_page));
122237fead6SMichael Halcrow 	if (rc) {
123237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
124237fead6SMichael Halcrow 				"index=[0x%.16x], old_end_pos_in_page=[d], "
125237fead6SMichael Halcrow 				"(PAGE_CACHE_SIZE - old_end_pos_in_page)=[d]) "
126237fead6SMichael Halcrow 				"returned [%d]\n", file, index,
127237fead6SMichael Halcrow 				old_end_pos_in_page,
128237fead6SMichael Halcrow 				(PAGE_CACHE_SIZE - old_end_pos_in_page), rc);
129237fead6SMichael Halcrow 		goto out;
130237fead6SMichael Halcrow 	}
131237fead6SMichael Halcrow 	index++;
132237fead6SMichael Halcrow 	while (index < new_end_page_index) {
133237fead6SMichael Halcrow 		/* Fill all intermediate pages with zeros */
134237fead6SMichael Halcrow 		rc = write_zeros(file, index, 0, PAGE_CACHE_SIZE);
135237fead6SMichael Halcrow 		if (rc) {
136237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
137237fead6SMichael Halcrow 					"index=[0x%.16x], "
138237fead6SMichael Halcrow 					"old_end_pos_in_page=[d], "
139237fead6SMichael Halcrow 					"(PAGE_CACHE_SIZE - new_end_pos_in_page"
140237fead6SMichael Halcrow 					"=[%d]"
141237fead6SMichael Halcrow 					")=[d]) returned [%d]\n", file, index,
142237fead6SMichael Halcrow 					old_end_pos_in_page,
143237fead6SMichael Halcrow 					new_end_pos_in_page,
144237fead6SMichael Halcrow 					(PAGE_CACHE_SIZE - new_end_pos_in_page),
145237fead6SMichael Halcrow 					rc);
146237fead6SMichael Halcrow 			goto out;
147237fead6SMichael Halcrow 		}
148237fead6SMichael Halcrow 		index++;
149237fead6SMichael Halcrow 	}
150237fead6SMichael Halcrow 	/* Fill the portion at the beginning of the last new page with
151237fead6SMichael Halcrow 	 * zero's */
152237fead6SMichael Halcrow 	rc = write_zeros(file, index, 0, (new_end_pos_in_page + 1));
153237fead6SMichael Halcrow 	if (rc) {
154237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "write_zeros(file="
155237fead6SMichael Halcrow 				"[%p], index=[0x%.16x], 0, "
156237fead6SMichael Halcrow 				"new_end_pos_in_page=[%d]"
157237fead6SMichael Halcrow 				"returned [%d]\n", file, index,
158237fead6SMichael Halcrow 				new_end_pos_in_page, rc);
159237fead6SMichael Halcrow 		goto out;
160237fead6SMichael Halcrow 	}
161237fead6SMichael Halcrow out:
162237fead6SMichael Halcrow 	return rc;
163237fead6SMichael Halcrow }
164237fead6SMichael Halcrow 
165237fead6SMichael Halcrow /**
166237fead6SMichael Halcrow  * ecryptfs_writepage
167237fead6SMichael Halcrow  * @page: Page that is locked before this call is made
168237fead6SMichael Halcrow  *
169237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
170237fead6SMichael Halcrow  */
171237fead6SMichael Halcrow static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
172237fead6SMichael Halcrow {
173237fead6SMichael Halcrow 	struct ecryptfs_page_crypt_context ctx;
174237fead6SMichael Halcrow 	int rc;
175237fead6SMichael Halcrow 
176237fead6SMichael Halcrow 	ctx.page = page;
177237fead6SMichael Halcrow 	ctx.mode = ECRYPTFS_WRITEPAGE_MODE;
178237fead6SMichael Halcrow 	ctx.param.wbc = wbc;
179237fead6SMichael Halcrow 	rc = ecryptfs_encrypt_page(&ctx);
180237fead6SMichael Halcrow 	if (rc) {
181237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error encrypting "
182237fead6SMichael Halcrow 				"page (upper index [0x%.16x])\n", page->index);
183237fead6SMichael Halcrow 		ClearPageUptodate(page);
184237fead6SMichael Halcrow 		goto out;
185237fead6SMichael Halcrow 	}
186237fead6SMichael Halcrow 	SetPageUptodate(page);
187237fead6SMichael Halcrow 	unlock_page(page);
188237fead6SMichael Halcrow out:
189237fead6SMichael Halcrow 	return rc;
190237fead6SMichael Halcrow }
191237fead6SMichael Halcrow 
192237fead6SMichael Halcrow /**
193237fead6SMichael Halcrow  * Reads the data from the lower file file at index lower_page_index
194237fead6SMichael Halcrow  * and copies that data into page.
195237fead6SMichael Halcrow  *
196237fead6SMichael Halcrow  * @param page	Page to fill
197237fead6SMichael Halcrow  * @param lower_page_index Index of the page in the lower file to get
198237fead6SMichael Halcrow  */
199237fead6SMichael Halcrow int ecryptfs_do_readpage(struct file *file, struct page *page,
200237fead6SMichael Halcrow 			 pgoff_t lower_page_index)
201237fead6SMichael Halcrow {
202237fead6SMichael Halcrow 	int rc;
203237fead6SMichael Halcrow 	struct dentry *dentry;
204237fead6SMichael Halcrow 	struct file *lower_file;
205237fead6SMichael Halcrow 	struct dentry *lower_dentry;
206237fead6SMichael Halcrow 	struct inode *inode;
207237fead6SMichael Halcrow 	struct inode *lower_inode;
208237fead6SMichael Halcrow 	char *page_data;
209237fead6SMichael Halcrow 	struct page *lower_page = NULL;
210237fead6SMichael Halcrow 	char *lower_page_data;
211237fead6SMichael Halcrow 	const struct address_space_operations *lower_a_ops;
212237fead6SMichael Halcrow 
213bd243a4bSJosef "Jeff" Sipek 	dentry = file->f_path.dentry;
214237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
215237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
216237fead6SMichael Halcrow 	inode = dentry->d_inode;
217237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
218237fead6SMichael Halcrow 	lower_a_ops = lower_inode->i_mapping->a_ops;
219237fead6SMichael Halcrow 	lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index,
220237fead6SMichael Halcrow 				     (filler_t *)lower_a_ops->readpage,
221237fead6SMichael Halcrow 				     (void *)lower_file);
222237fead6SMichael Halcrow 	if (IS_ERR(lower_page)) {
223237fead6SMichael Halcrow 		rc = PTR_ERR(lower_page);
224237fead6SMichael Halcrow 		lower_page = NULL;
225237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error reading from page cache\n");
226237fead6SMichael Halcrow 		goto out;
227237fead6SMichael Halcrow 	}
2289d8b8ce5SMichael Halcrow 	page_data = kmap_atomic(page, KM_USER0);
2299d8b8ce5SMichael Halcrow 	lower_page_data = kmap_atomic(lower_page, KM_USER1);
230237fead6SMichael Halcrow 	memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE);
2319d8b8ce5SMichael Halcrow 	kunmap_atomic(lower_page_data, KM_USER1);
2329d8b8ce5SMichael Halcrow 	kunmap_atomic(page_data, KM_USER0);
2330a9ac382SMichael Halcrow 	flush_dcache_page(page);
234237fead6SMichael Halcrow 	rc = 0;
235237fead6SMichael Halcrow out:
236237fead6SMichael Halcrow 	if (likely(lower_page))
237237fead6SMichael Halcrow 		page_cache_release(lower_page);
238237fead6SMichael Halcrow 	if (rc == 0)
239237fead6SMichael Halcrow 		SetPageUptodate(page);
240237fead6SMichael Halcrow 	else
241237fead6SMichael Halcrow 		ClearPageUptodate(page);
242237fead6SMichael Halcrow 	return rc;
243237fead6SMichael Halcrow }
244e77a56ddSMichael Halcrow /**
245e77a56ddSMichael Halcrow  *   Header Extent:
246e77a56ddSMichael Halcrow  *     Octets 0-7:        Unencrypted file size (big-endian)
247e77a56ddSMichael Halcrow  *     Octets 8-15:       eCryptfs special marker
248e77a56ddSMichael Halcrow  *     Octets 16-19:      Flags
249e77a56ddSMichael Halcrow  *      Octet 16:         File format version number (between 0 and 255)
250e77a56ddSMichael Halcrow  *      Octets 17-18:     Reserved
251e77a56ddSMichael Halcrow  *      Octet 19:         Bit 1 (lsb): Reserved
252e77a56ddSMichael Halcrow  *                        Bit 2: Encrypted?
253e77a56ddSMichael Halcrow  *                        Bits 3-8: Reserved
254e77a56ddSMichael Halcrow  *     Octets 20-23:      Header extent size (big-endian)
255e77a56ddSMichael Halcrow  *     Octets 24-25:      Number of header extents at front of file
256e77a56ddSMichael Halcrow  *                        (big-endian)
257e77a56ddSMichael Halcrow  *     Octet  26:         Begin RFC 2440 authentication token packet set
258e77a56ddSMichael Halcrow  */
259e77a56ddSMichael Halcrow static void set_header_info(char *page_virt,
260e77a56ddSMichael Halcrow 			    struct ecryptfs_crypt_stat *crypt_stat)
261e77a56ddSMichael Halcrow {
262e77a56ddSMichael Halcrow 	size_t written;
263e77a56ddSMichael Halcrow 	int save_num_header_extents_at_front =
264e77a56ddSMichael Halcrow 		crypt_stat->num_header_extents_at_front;
265e77a56ddSMichael Halcrow 
266e77a56ddSMichael Halcrow 	crypt_stat->num_header_extents_at_front = 1;
267e77a56ddSMichael Halcrow 	ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
268e77a56ddSMichael Halcrow 	crypt_stat->num_header_extents_at_front =
269e77a56ddSMichael Halcrow 		save_num_header_extents_at_front;
270e77a56ddSMichael Halcrow }
271237fead6SMichael Halcrow 
272237fead6SMichael Halcrow /**
273237fead6SMichael Halcrow  * ecryptfs_readpage
274237fead6SMichael Halcrow  * @file: This is an ecryptfs file
275237fead6SMichael Halcrow  * @page: ecryptfs associated page to stick the read data into
276237fead6SMichael Halcrow  *
277237fead6SMichael Halcrow  * Read in a page, decrypting if necessary.
278237fead6SMichael Halcrow  *
279237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
280237fead6SMichael Halcrow  */
281237fead6SMichael Halcrow static int ecryptfs_readpage(struct file *file, struct page *page)
282237fead6SMichael Halcrow {
283237fead6SMichael Halcrow 	int rc = 0;
284237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
285237fead6SMichael Halcrow 
286bd243a4bSJosef "Jeff" Sipek 	BUG_ON(!(file && file->f_path.dentry && file->f_path.dentry->d_inode));
287bd243a4bSJosef "Jeff" Sipek 	crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
288bd243a4bSJosef "Jeff" Sipek 			->crypt_stat;
289237fead6SMichael Halcrow 	if (!crypt_stat
290e2bd99ecSMichael Halcrow 	    || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
291e2bd99ecSMichael Halcrow 	    || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
292237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG,
293237fead6SMichael Halcrow 				"Passing through unencrypted page\n");
294237fead6SMichael Halcrow 		rc = ecryptfs_do_readpage(file, page, page->index);
295237fead6SMichael Halcrow 		if (rc) {
296237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error reading page; rc = "
297237fead6SMichael Halcrow 					"[%d]\n", rc);
298237fead6SMichael Halcrow 			goto out;
299237fead6SMichael Halcrow 		}
300e77a56ddSMichael Halcrow 	} else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
301e77a56ddSMichael Halcrow 		if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
302e77a56ddSMichael Halcrow 			int num_pages_in_header_region =
303e77a56ddSMichael Halcrow 				(crypt_stat->header_extent_size
304e77a56ddSMichael Halcrow 				 / PAGE_CACHE_SIZE);
305e77a56ddSMichael Halcrow 
306e77a56ddSMichael Halcrow 			if (page->index < num_pages_in_header_region) {
307e77a56ddSMichael Halcrow 				char *page_virt;
308e77a56ddSMichael Halcrow 
3099d8b8ce5SMichael Halcrow 				page_virt = kmap_atomic(page, KM_USER0);
310e77a56ddSMichael Halcrow 				memset(page_virt, 0, PAGE_CACHE_SIZE);
311e77a56ddSMichael Halcrow 				if (page->index == 0) {
312e77a56ddSMichael Halcrow 					rc = ecryptfs_read_xattr_region(
313e77a56ddSMichael Halcrow 						page_virt, file->f_path.dentry);
314e77a56ddSMichael Halcrow 					set_header_info(page_virt, crypt_stat);
315e77a56ddSMichael Halcrow 				}
3169d8b8ce5SMichael Halcrow 				kunmap_atomic(page_virt, KM_USER0);
3170a9ac382SMichael Halcrow 				flush_dcache_page(page);
318e77a56ddSMichael Halcrow 				if (rc) {
319e77a56ddSMichael Halcrow 					printk(KERN_ERR "Error reading xattr "
320e77a56ddSMichael Halcrow 					       "region\n");
321e77a56ddSMichael Halcrow 					goto out;
322e77a56ddSMichael Halcrow 				}
323e77a56ddSMichael Halcrow 			} else {
324e77a56ddSMichael Halcrow 				rc = ecryptfs_do_readpage(
325e77a56ddSMichael Halcrow 					file, page,
326e77a56ddSMichael Halcrow 					(page->index
327e77a56ddSMichael Halcrow 					 - num_pages_in_header_region));
328e77a56ddSMichael Halcrow 				if (rc) {
329e77a56ddSMichael Halcrow 					printk(KERN_ERR "Error reading page; "
330e77a56ddSMichael Halcrow 					       "rc = [%d]\n", rc);
331e77a56ddSMichael Halcrow 					goto out;
332e77a56ddSMichael Halcrow 				}
333e77a56ddSMichael Halcrow 			}
334e77a56ddSMichael Halcrow 		} else {
335e77a56ddSMichael Halcrow 			rc = ecryptfs_do_readpage(file, page, page->index);
336e77a56ddSMichael Halcrow 			if (rc) {
337e77a56ddSMichael Halcrow 				printk(KERN_ERR "Error reading page; rc = "
338e77a56ddSMichael Halcrow 				       "[%d]\n", rc);
339e77a56ddSMichael Halcrow 				goto out;
340e77a56ddSMichael Halcrow 			}
341e77a56ddSMichael Halcrow 		}
342237fead6SMichael Halcrow 	} else {
343237fead6SMichael Halcrow 		rc = ecryptfs_decrypt_page(file, page);
344237fead6SMichael Halcrow 		if (rc) {
345237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error decrypting page; "
346237fead6SMichael Halcrow 					"rc = [%d]\n", rc);
347237fead6SMichael Halcrow 			goto out;
348237fead6SMichael Halcrow 		}
349237fead6SMichael Halcrow 	}
350237fead6SMichael Halcrow 	SetPageUptodate(page);
351237fead6SMichael Halcrow out:
352237fead6SMichael Halcrow 	if (rc)
353237fead6SMichael Halcrow 		ClearPageUptodate(page);
354237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
355237fead6SMichael Halcrow 			page->index);
356237fead6SMichael Halcrow 	unlock_page(page);
357237fead6SMichael Halcrow 	return rc;
358237fead6SMichael Halcrow }
359237fead6SMichael Halcrow 
360dd2a3b7aSMichael Halcrow /**
361dd2a3b7aSMichael Halcrow  * Called with lower inode mutex held.
362dd2a3b7aSMichael Halcrow  */
363237fead6SMichael Halcrow static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
364237fead6SMichael Halcrow {
365237fead6SMichael Halcrow 	struct inode *inode = page->mapping->host;
366237fead6SMichael Halcrow 	int end_byte_in_page;
367237fead6SMichael Halcrow 
3689d8b8ce5SMichael Halcrow 	if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
3699d8b8ce5SMichael Halcrow 		goto out;
370237fead6SMichael Halcrow 	end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
371237fead6SMichael Halcrow 	if (to > end_byte_in_page)
372237fead6SMichael Halcrow 		end_byte_in_page = to;
373*c9f2875bSNate Diller 	zero_user_page(page, end_byte_in_page,
374*c9f2875bSNate Diller 		PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0);
375237fead6SMichael Halcrow out:
3769d8b8ce5SMichael Halcrow 	return 0;
377237fead6SMichael Halcrow }
378237fead6SMichael Halcrow 
379237fead6SMichael Halcrow static int ecryptfs_prepare_write(struct file *file, struct page *page,
380237fead6SMichael Halcrow 				  unsigned from, unsigned to)
381237fead6SMichael Halcrow {
382237fead6SMichael Halcrow 	int rc = 0;
383237fead6SMichael Halcrow 
384237fead6SMichael Halcrow 	if (from == 0 && to == PAGE_CACHE_SIZE)
385237fead6SMichael Halcrow 		goto out;	/* If we are writing a full page, it will be
386237fead6SMichael Halcrow 				   up to date. */
387237fead6SMichael Halcrow 	if (!PageUptodate(page))
388237fead6SMichael Halcrow 		rc = ecryptfs_do_readpage(file, page, page->index);
389237fead6SMichael Halcrow out:
390237fead6SMichael Halcrow 	return rc;
391237fead6SMichael Halcrow }
392237fead6SMichael Halcrow 
393237fead6SMichael Halcrow int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
394237fead6SMichael Halcrow 					      struct inode *lower_inode,
395237fead6SMichael Halcrow 					      struct writeback_control *wbc)
396237fead6SMichael Halcrow {
397237fead6SMichael Halcrow 	int rc = 0;
398237fead6SMichael Halcrow 
399237fead6SMichael Halcrow 	rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
400237fead6SMichael Halcrow 	if (rc) {
401237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
402237fead6SMichael Halcrow 				"rc = [%d]\n", rc);
403237fead6SMichael Halcrow 		goto out;
404237fead6SMichael Halcrow 	}
405237fead6SMichael Halcrow 	lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
406237fead6SMichael Halcrow 	page_cache_release(lower_page);
407237fead6SMichael Halcrow out:
408237fead6SMichael Halcrow 	return rc;
409237fead6SMichael Halcrow }
410237fead6SMichael Halcrow 
411ae73fc09SMichael Halcrow static
412ae73fc09SMichael Halcrow void ecryptfs_release_lower_page(struct page *lower_page, int page_locked)
413237fead6SMichael Halcrow {
414ae73fc09SMichael Halcrow 	if (page_locked)
415237fead6SMichael Halcrow 		unlock_page(lower_page);
416237fead6SMichael Halcrow 	page_cache_release(lower_page);
417237fead6SMichael Halcrow }
418237fead6SMichael Halcrow 
419237fead6SMichael Halcrow /**
420237fead6SMichael Halcrow  * ecryptfs_write_inode_size_to_header
421237fead6SMichael Halcrow  *
422237fead6SMichael Halcrow  * Writes the lower file size to the first 8 bytes of the header.
423237fead6SMichael Halcrow  *
424237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
425237fead6SMichael Halcrow  */
426dd2a3b7aSMichael Halcrow static int ecryptfs_write_inode_size_to_header(struct file *lower_file,
427237fead6SMichael Halcrow 					       struct inode *lower_inode,
428237fead6SMichael Halcrow 					       struct inode *inode)
429237fead6SMichael Halcrow {
430237fead6SMichael Halcrow 	int rc = 0;
431237fead6SMichael Halcrow 	struct page *header_page;
432237fead6SMichael Halcrow 	char *header_virt;
433237fead6SMichael Halcrow 	const struct address_space_operations *lower_a_ops;
434237fead6SMichael Halcrow 	u64 file_size;
435237fead6SMichael Halcrow 
436a8fa74abSDmitriy Monakhov retry:
4379d8b8ce5SMichael Halcrow 	header_page = grab_cache_page(lower_inode->i_mapping, 0);
4389d8b8ce5SMichael Halcrow 	if (!header_page) {
4399d8b8ce5SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "grab_cache_page for "
4409d8b8ce5SMichael Halcrow 				"lower_page_index 0 failed\n");
4419d8b8ce5SMichael Halcrow 		rc = -EINVAL;
442237fead6SMichael Halcrow 		goto out;
443237fead6SMichael Halcrow 	}
444237fead6SMichael Halcrow 	lower_a_ops = lower_inode->i_mapping->a_ops;
445237fead6SMichael Halcrow 	rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8);
446ae73fc09SMichael Halcrow 	if (rc) {
447a8fa74abSDmitriy Monakhov 		if (rc == AOP_TRUNCATED_PAGE) {
448ae73fc09SMichael Halcrow 			ecryptfs_release_lower_page(header_page, 0);
449a8fa74abSDmitriy Monakhov 			goto retry;
450a8fa74abSDmitriy Monakhov 		} else
451ae73fc09SMichael Halcrow 			ecryptfs_release_lower_page(header_page, 1);
452ae73fc09SMichael Halcrow 		goto out;
453ae73fc09SMichael Halcrow 	}
454237fead6SMichael Halcrow 	file_size = (u64)i_size_read(inode);
455237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size);
456237fead6SMichael Halcrow 	file_size = cpu_to_be64(file_size);
4579d8b8ce5SMichael Halcrow 	header_virt = kmap_atomic(header_page, KM_USER0);
458237fead6SMichael Halcrow 	memcpy(header_virt, &file_size, sizeof(u64));
4599d8b8ce5SMichael Halcrow 	kunmap_atomic(header_virt, KM_USER0);
4600a9ac382SMichael Halcrow 	flush_dcache_page(header_page);
461237fead6SMichael Halcrow 	rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8);
462237fead6SMichael Halcrow 	if (rc < 0)
463237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error commiting header page "
464237fead6SMichael Halcrow 				"write\n");
465a8fa74abSDmitriy Monakhov 	if (rc == AOP_TRUNCATED_PAGE) {
466ae73fc09SMichael Halcrow 		ecryptfs_release_lower_page(header_page, 0);
467a8fa74abSDmitriy Monakhov 		goto retry;
468a8fa74abSDmitriy Monakhov 	} else
469ae73fc09SMichael Halcrow 		ecryptfs_release_lower_page(header_page, 1);
470237fead6SMichael Halcrow 	lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
471237fead6SMichael Halcrow 	mark_inode_dirty_sync(inode);
472237fead6SMichael Halcrow out:
473237fead6SMichael Halcrow 	return rc;
474237fead6SMichael Halcrow }
475237fead6SMichael Halcrow 
476dd2a3b7aSMichael Halcrow static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode,
477dd2a3b7aSMichael Halcrow 					      struct inode *inode,
478dd2a3b7aSMichael Halcrow 					      struct dentry *ecryptfs_dentry,
479dd2a3b7aSMichael Halcrow 					      int lower_i_mutex_held)
480dd2a3b7aSMichael Halcrow {
481dd2a3b7aSMichael Halcrow 	ssize_t size;
482dd2a3b7aSMichael Halcrow 	void *xattr_virt;
483dd2a3b7aSMichael Halcrow 	struct dentry *lower_dentry;
484dd2a3b7aSMichael Halcrow 	u64 file_size;
485dd2a3b7aSMichael Halcrow 	int rc;
486dd2a3b7aSMichael Halcrow 
487dd2a3b7aSMichael Halcrow 	xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
488dd2a3b7aSMichael Halcrow 	if (!xattr_virt) {
489dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Out of memory whilst attempting to write "
490dd2a3b7aSMichael Halcrow 		       "inode size to xattr\n");
491dd2a3b7aSMichael Halcrow 		rc = -ENOMEM;
492dd2a3b7aSMichael Halcrow 		goto out;
493dd2a3b7aSMichael Halcrow 	}
494dd2a3b7aSMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
495ad5f1196SDmitriy Monakhov 	if (!lower_dentry->d_inode->i_op->getxattr ||
496ad5f1196SDmitriy Monakhov 			!lower_dentry->d_inode->i_op->setxattr) {
497dd2a3b7aSMichael Halcrow 		printk(KERN_WARNING
498dd2a3b7aSMichael Halcrow 		       "No support for setting xattr in lower filesystem\n");
499dd2a3b7aSMichael Halcrow 		rc = -ENOSYS;
500dd2a3b7aSMichael Halcrow 		kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
501dd2a3b7aSMichael Halcrow 		goto out;
502dd2a3b7aSMichael Halcrow 	}
503dd2a3b7aSMichael Halcrow 	if (!lower_i_mutex_held)
504dd2a3b7aSMichael Halcrow 		mutex_lock(&lower_dentry->d_inode->i_mutex);
505dd2a3b7aSMichael Halcrow 	size = lower_dentry->d_inode->i_op->getxattr(lower_dentry,
506dd2a3b7aSMichael Halcrow 						     ECRYPTFS_XATTR_NAME,
507dd2a3b7aSMichael Halcrow 						     xattr_virt,
508dd2a3b7aSMichael Halcrow 						     PAGE_CACHE_SIZE);
509dd2a3b7aSMichael Halcrow 	if (!lower_i_mutex_held)
510dd2a3b7aSMichael Halcrow 		mutex_unlock(&lower_dentry->d_inode->i_mutex);
511dd2a3b7aSMichael Halcrow 	if (size < 0)
512dd2a3b7aSMichael Halcrow 		size = 8;
513dd2a3b7aSMichael Halcrow 	file_size = (u64)i_size_read(inode);
514dd2a3b7aSMichael Halcrow 	file_size = cpu_to_be64(file_size);
515dd2a3b7aSMichael Halcrow 	memcpy(xattr_virt, &file_size, sizeof(u64));
516dd2a3b7aSMichael Halcrow 	if (!lower_i_mutex_held)
517dd2a3b7aSMichael Halcrow 		mutex_lock(&lower_dentry->d_inode->i_mutex);
518dd2a3b7aSMichael Halcrow 	rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry,
519dd2a3b7aSMichael Halcrow 						   ECRYPTFS_XATTR_NAME,
520dd2a3b7aSMichael Halcrow 						   xattr_virt, size, 0);
521dd2a3b7aSMichael Halcrow 	if (!lower_i_mutex_held)
522dd2a3b7aSMichael Halcrow 		mutex_unlock(&lower_dentry->d_inode->i_mutex);
523dd2a3b7aSMichael Halcrow 	if (rc)
524dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error whilst attempting to write inode size "
525dd2a3b7aSMichael Halcrow 		       "to lower file xattr; rc = [%d]\n", rc);
526dd2a3b7aSMichael Halcrow 	kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
527dd2a3b7aSMichael Halcrow out:
528dd2a3b7aSMichael Halcrow 	return rc;
529dd2a3b7aSMichael Halcrow }
530dd2a3b7aSMichael Halcrow 
531dd2a3b7aSMichael Halcrow int
532dd2a3b7aSMichael Halcrow ecryptfs_write_inode_size_to_metadata(struct file *lower_file,
533dd2a3b7aSMichael Halcrow 				      struct inode *lower_inode,
534dd2a3b7aSMichael Halcrow 				      struct inode *inode,
535dd2a3b7aSMichael Halcrow 				      struct dentry *ecryptfs_dentry,
536dd2a3b7aSMichael Halcrow 				      int lower_i_mutex_held)
537dd2a3b7aSMichael Halcrow {
538dd2a3b7aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
539dd2a3b7aSMichael Halcrow 
540dd2a3b7aSMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
541dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
542dd2a3b7aSMichael Halcrow 		return ecryptfs_write_inode_size_to_xattr(lower_inode, inode,
543dd2a3b7aSMichael Halcrow 							  ecryptfs_dentry,
544dd2a3b7aSMichael Halcrow 							  lower_i_mutex_held);
545dd2a3b7aSMichael Halcrow 	else
546dd2a3b7aSMichael Halcrow 		return ecryptfs_write_inode_size_to_header(lower_file,
547dd2a3b7aSMichael Halcrow 							   lower_inode,
548dd2a3b7aSMichael Halcrow 							   inode);
549dd2a3b7aSMichael Halcrow }
550dd2a3b7aSMichael Halcrow 
551237fead6SMichael Halcrow int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
552237fead6SMichael Halcrow 			    struct file *lower_file,
553237fead6SMichael Halcrow 			    unsigned long lower_page_index, int byte_offset,
554237fead6SMichael Halcrow 			    int region_bytes)
555237fead6SMichael Halcrow {
556237fead6SMichael Halcrow 	int rc = 0;
557237fead6SMichael Halcrow 
558a8fa74abSDmitriy Monakhov retry:
5599d8b8ce5SMichael Halcrow 	*lower_page = grab_cache_page(lower_inode->i_mapping, lower_page_index);
5609d8b8ce5SMichael Halcrow 	if (!(*lower_page)) {
5619d8b8ce5SMichael Halcrow 		rc = -EINVAL;
5629d8b8ce5SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to grab "
563237fead6SMichael Halcrow 				"lower page with index [0x%.16x]\n",
564237fead6SMichael Halcrow 				lower_page_index);
565237fead6SMichael Halcrow 		goto out;
566237fead6SMichael Halcrow 	}
567237fead6SMichael Halcrow 	rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
568237fead6SMichael Halcrow 							  (*lower_page),
569237fead6SMichael Halcrow 							  byte_offset,
570237fead6SMichael Halcrow 							  region_bytes);
571237fead6SMichael Halcrow 	if (rc) {
572a8fa74abSDmitriy Monakhov 		if (rc == AOP_TRUNCATED_PAGE) {
573a8fa74abSDmitriy Monakhov 			ecryptfs_release_lower_page(*lower_page, 0);
574a8fa74abSDmitriy Monakhov 			goto retry;
575a8fa74abSDmitriy Monakhov 		} else {
576237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "prepare_write for "
577237fead6SMichael Halcrow 				"lower_page_index = [0x%.16x] failed; rc = "
578237fead6SMichael Halcrow 				"[%d]\n", lower_page_index, rc);
579ae73fc09SMichael Halcrow 			ecryptfs_release_lower_page(*lower_page, 1);
580237fead6SMichael Halcrow 			(*lower_page) = NULL;
581237fead6SMichael Halcrow 		}
582a8fa74abSDmitriy Monakhov 	}
583a8fa74abSDmitriy Monakhov out:
584237fead6SMichael Halcrow 	return rc;
585237fead6SMichael Halcrow }
586237fead6SMichael Halcrow 
587237fead6SMichael Halcrow /**
588237fead6SMichael Halcrow  * ecryptfs_commit_lower_page
589237fead6SMichael Halcrow  *
590237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
591237fead6SMichael Halcrow  */
592237fead6SMichael Halcrow int
593237fead6SMichael Halcrow ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
594237fead6SMichael Halcrow 			   struct file *lower_file, int byte_offset,
595237fead6SMichael Halcrow 			   int region_size)
596237fead6SMichael Halcrow {
597ae73fc09SMichael Halcrow 	int page_locked = 1;
598237fead6SMichael Halcrow 	int rc = 0;
599237fead6SMichael Halcrow 
600237fead6SMichael Halcrow 	rc = lower_inode->i_mapping->a_ops->commit_write(
601237fead6SMichael Halcrow 		lower_file, lower_page, byte_offset, region_size);
602ae73fc09SMichael Halcrow 	if (rc == AOP_TRUNCATED_PAGE)
603ae73fc09SMichael Halcrow 		page_locked = 0;
604237fead6SMichael Halcrow 	if (rc < 0) {
605237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
606237fead6SMichael Halcrow 				"Error committing write; rc = [%d]\n", rc);
607237fead6SMichael Halcrow 	} else
608237fead6SMichael Halcrow 		rc = 0;
609ae73fc09SMichael Halcrow 	ecryptfs_release_lower_page(lower_page, page_locked);
610237fead6SMichael Halcrow 	return rc;
611237fead6SMichael Halcrow }
612237fead6SMichael Halcrow 
613237fead6SMichael Halcrow /**
614237fead6SMichael Halcrow  * ecryptfs_copy_page_to_lower
615237fead6SMichael Halcrow  *
616237fead6SMichael Halcrow  * Used for plaintext pass-through; no page index interpolation
617237fead6SMichael Halcrow  * required.
618237fead6SMichael Halcrow  */
619237fead6SMichael Halcrow int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
620237fead6SMichael Halcrow 				struct file *lower_file)
621237fead6SMichael Halcrow {
622237fead6SMichael Halcrow 	int rc = 0;
623237fead6SMichael Halcrow 	struct page *lower_page;
624237fead6SMichael Halcrow 
625237fead6SMichael Halcrow 	rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
626237fead6SMichael Halcrow 				     page->index, 0, PAGE_CACHE_SIZE);
627237fead6SMichael Halcrow 	if (rc) {
628237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to get page "
629237fead6SMichael Halcrow 				"at index [0x%.16x]\n", page->index);
630237fead6SMichael Halcrow 		goto out;
631237fead6SMichael Halcrow 	}
632237fead6SMichael Halcrow 	/* TODO: aops */
633237fead6SMichael Halcrow 	memcpy((char *)page_address(lower_page), page_address(page),
634237fead6SMichael Halcrow 	       PAGE_CACHE_SIZE);
635237fead6SMichael Halcrow 	rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
636237fead6SMichael Halcrow 					0, PAGE_CACHE_SIZE);
637237fead6SMichael Halcrow 	if (rc)
638237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
639237fead6SMichael Halcrow 				"at index [0x%.16x]\n", page->index);
640237fead6SMichael Halcrow out:
641237fead6SMichael Halcrow 	return rc;
642237fead6SMichael Halcrow }
643237fead6SMichael Halcrow 
644dd2a3b7aSMichael Halcrow struct kmem_cache *ecryptfs_xattr_cache;
645dd2a3b7aSMichael Halcrow 
646237fead6SMichael Halcrow /**
647237fead6SMichael Halcrow  * ecryptfs_commit_write
648237fead6SMichael Halcrow  * @file: The eCryptfs file object
649237fead6SMichael Halcrow  * @page: The eCryptfs page
650237fead6SMichael Halcrow  * @from: Ignored (we rotate the page IV on each write)
651237fead6SMichael Halcrow  * @to: Ignored
652237fead6SMichael Halcrow  *
653237fead6SMichael Halcrow  * This is where we encrypt the data and pass the encrypted data to
654237fead6SMichael Halcrow  * the lower filesystem.  In OpenPGP-compatible mode, we operate on
655237fead6SMichael Halcrow  * entire underlying packets.
656237fead6SMichael Halcrow  */
657237fead6SMichael Halcrow static int ecryptfs_commit_write(struct file *file, struct page *page,
658237fead6SMichael Halcrow 				 unsigned from, unsigned to)
659237fead6SMichael Halcrow {
660237fead6SMichael Halcrow 	struct ecryptfs_page_crypt_context ctx;
661237fead6SMichael Halcrow 	loff_t pos;
662237fead6SMichael Halcrow 	struct inode *inode;
663237fead6SMichael Halcrow 	struct inode *lower_inode;
664237fead6SMichael Halcrow 	struct file *lower_file;
665237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
666237fead6SMichael Halcrow 	int rc;
667237fead6SMichael Halcrow 
668237fead6SMichael Halcrow 	inode = page->mapping->host;
669237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
670237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
671237fead6SMichael Halcrow 	mutex_lock(&lower_inode->i_mutex);
672bd243a4bSJosef "Jeff" Sipek 	crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
673bd243a4bSJosef "Jeff" Sipek 				->crypt_stat;
674e2bd99ecSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
675237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
676237fead6SMichael Halcrow 			"crypt_stat at memory location [%p]\n", crypt_stat);
677e2bd99ecSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
678237fead6SMichael Halcrow 	} else
679237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
680237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
681237fead6SMichael Halcrow 			"(page w/ index = [0x%.16x], to = [%d])\n", page->index,
682237fead6SMichael Halcrow 			to);
683237fead6SMichael Halcrow 	rc = fill_zeros_to_end_of_page(page, to);
684237fead6SMichael Halcrow 	if (rc) {
685237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
686237fead6SMichael Halcrow 				"zeros in page with index = [0x%.16x]\n",
687237fead6SMichael Halcrow 				page->index);
688237fead6SMichael Halcrow 		goto out;
689237fead6SMichael Halcrow 	}
690237fead6SMichael Halcrow 	ctx.page = page;
691237fead6SMichael Halcrow 	ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE;
692237fead6SMichael Halcrow 	ctx.param.lower_file = lower_file;
693237fead6SMichael Halcrow 	rc = ecryptfs_encrypt_page(&ctx);
694237fead6SMichael Halcrow 	if (rc) {
695237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
696237fead6SMichael Halcrow 				"index [0x%.16x])\n", page->index);
697237fead6SMichael Halcrow 		goto out;
698237fead6SMichael Halcrow 	}
699237fead6SMichael Halcrow 	inode->i_blocks = lower_inode->i_blocks;
700237fead6SMichael Halcrow 	pos = (page->index << PAGE_CACHE_SHIFT) + to;
701237fead6SMichael Halcrow 	if (pos > i_size_read(inode)) {
702237fead6SMichael Halcrow 		i_size_write(inode, pos);
703237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
704237fead6SMichael Halcrow 				"[0x%.16x]\n", i_size_read(inode));
705237fead6SMichael Halcrow 	}
706dd2a3b7aSMichael Halcrow 	rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode,
707dd2a3b7aSMichael Halcrow 						   inode, file->f_dentry,
708dd2a3b7aSMichael Halcrow 						   ECRYPTFS_LOWER_I_MUTEX_HELD);
709dd2a3b7aSMichael Halcrow 	if (rc)
710dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error writing inode size to metadata; "
711dd2a3b7aSMichael Halcrow 		       "rc = [%d]\n", rc);
712237fead6SMichael Halcrow 	lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
713237fead6SMichael Halcrow 	mark_inode_dirty_sync(inode);
714237fead6SMichael Halcrow out:
715237fead6SMichael Halcrow 	if (rc < 0)
716237fead6SMichael Halcrow 		ClearPageUptodate(page);
717237fead6SMichael Halcrow 	else
718237fead6SMichael Halcrow 		SetPageUptodate(page);
719237fead6SMichael Halcrow 	mutex_unlock(&lower_inode->i_mutex);
720237fead6SMichael Halcrow 	return rc;
721237fead6SMichael Halcrow }
722237fead6SMichael Halcrow 
723237fead6SMichael Halcrow /**
724237fead6SMichael Halcrow  * write_zeros
725237fead6SMichael Halcrow  * @file: The ecryptfs file
726237fead6SMichael Halcrow  * @index: The index in which we are writing
727237fead6SMichael Halcrow  * @start: The position after the last block of data
728237fead6SMichael Halcrow  * @num_zeros: The number of zeros to write
729237fead6SMichael Halcrow  *
730237fead6SMichael Halcrow  * Write a specified number of zero's to a page.
731237fead6SMichael Halcrow  *
732237fead6SMichael Halcrow  * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
733237fead6SMichael Halcrow  */
734237fead6SMichael Halcrow static
735237fead6SMichael Halcrow int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
736237fead6SMichael Halcrow {
737237fead6SMichael Halcrow 	int rc = 0;
738237fead6SMichael Halcrow 	struct page *tmp_page;
739237fead6SMichael Halcrow 
740237fead6SMichael Halcrow 	tmp_page = ecryptfs_get1page(file, index);
741237fead6SMichael Halcrow 	if (IS_ERR(tmp_page)) {
742237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error getting page at index "
743237fead6SMichael Halcrow 				"[0x%.16x]\n", index);
744237fead6SMichael Halcrow 		rc = PTR_ERR(tmp_page);
745237fead6SMichael Halcrow 		goto out;
746237fead6SMichael Halcrow 	}
747237fead6SMichael Halcrow 	rc = ecryptfs_prepare_write(file, tmp_page, start, start + num_zeros);
748237fead6SMichael Halcrow 	if (rc) {
749237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
750237fead6SMichael Halcrow 				"to remainder of page at index [0x%.16x]\n",
751237fead6SMichael Halcrow 				index);
752237fead6SMichael Halcrow 		page_cache_release(tmp_page);
753237fead6SMichael Halcrow 		goto out;
754237fead6SMichael Halcrow 	}
755*c9f2875bSNate Diller 	zero_user_page(tmp_page, start, num_zeros, KM_USER0);
756237fead6SMichael Halcrow 	rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
757237fead6SMichael Halcrow 	if (rc < 0) {
758237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
759237fead6SMichael Halcrow 				"to remainder of page at index [0x%.16x]\n",
760237fead6SMichael Halcrow 				index);
761237fead6SMichael Halcrow 		page_cache_release(tmp_page);
762237fead6SMichael Halcrow 		goto out;
763237fead6SMichael Halcrow 	}
764237fead6SMichael Halcrow 	rc = 0;
765237fead6SMichael Halcrow 	page_cache_release(tmp_page);
766237fead6SMichael Halcrow out:
767237fead6SMichael Halcrow 	return rc;
768237fead6SMichael Halcrow }
769237fead6SMichael Halcrow 
770237fead6SMichael Halcrow static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
771237fead6SMichael Halcrow {
772237fead6SMichael Halcrow 	int rc = 0;
773237fead6SMichael Halcrow 	struct inode *inode;
774237fead6SMichael Halcrow 	struct inode *lower_inode;
775237fead6SMichael Halcrow 
776237fead6SMichael Halcrow 	inode = (struct inode *)mapping->host;
777237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
778237fead6SMichael Halcrow 	if (lower_inode->i_mapping->a_ops->bmap)
779237fead6SMichael Halcrow 		rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
780237fead6SMichael Halcrow 							 block);
781237fead6SMichael Halcrow 	return rc;
782237fead6SMichael Halcrow }
783237fead6SMichael Halcrow 
784237fead6SMichael Halcrow static void ecryptfs_sync_page(struct page *page)
785237fead6SMichael Halcrow {
786237fead6SMichael Halcrow 	struct inode *inode;
787237fead6SMichael Halcrow 	struct inode *lower_inode;
788237fead6SMichael Halcrow 	struct page *lower_page;
789237fead6SMichael Halcrow 
790237fead6SMichael Halcrow 	inode = page->mapping->host;
791237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
792237fead6SMichael Halcrow 	/* NOTE: Recently swapped with grab_cache_page(), since
793237fead6SMichael Halcrow 	 * sync_page() just makes sure that pending I/O gets done. */
794237fead6SMichael Halcrow 	lower_page = find_lock_page(lower_inode->i_mapping, page->index);
795237fead6SMichael Halcrow 	if (!lower_page) {
796237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n");
797237fead6SMichael Halcrow 		return;
798237fead6SMichael Halcrow 	}
799237fead6SMichael Halcrow 	lower_page->mapping->a_ops->sync_page(lower_page);
800237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
801237fead6SMichael Halcrow 			lower_page->index);
802237fead6SMichael Halcrow 	unlock_page(lower_page);
803237fead6SMichael Halcrow 	page_cache_release(lower_page);
804237fead6SMichael Halcrow }
805237fead6SMichael Halcrow 
806237fead6SMichael Halcrow struct address_space_operations ecryptfs_aops = {
807237fead6SMichael Halcrow 	.writepage = ecryptfs_writepage,
808237fead6SMichael Halcrow 	.readpage = ecryptfs_readpage,
809237fead6SMichael Halcrow 	.prepare_write = ecryptfs_prepare_write,
810237fead6SMichael Halcrow 	.commit_write = ecryptfs_commit_write,
811237fead6SMichael Halcrow 	.bmap = ecryptfs_bmap,
812237fead6SMichael Halcrow 	.sync_page = ecryptfs_sync_page,
813237fead6SMichael Halcrow };
814