xref: /openbmc/linux/fs/ecryptfs/mmap.c (revision dd2a3b7ad98f8482cae481cad89dfed5eee48365)
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
9*dd2a3b7aSMichael 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 page *page;
50237fead6SMichael Halcrow 	struct dentry *dentry;
51237fead6SMichael Halcrow 	struct inode *inode;
52237fead6SMichael Halcrow 	struct address_space *mapping;
53237fead6SMichael Halcrow 
54bd243a4bSJosef "Jeff" Sipek 	dentry = file->f_path.dentry;
55237fead6SMichael Halcrow 	inode = dentry->d_inode;
56237fead6SMichael Halcrow 	mapping = inode->i_mapping;
57237fead6SMichael Halcrow 	page = read_cache_page(mapping, index,
58237fead6SMichael Halcrow 			       (filler_t *)mapping->a_ops->readpage,
59237fead6SMichael Halcrow 			       (void *)file);
60237fead6SMichael Halcrow 	if (IS_ERR(page))
61237fead6SMichael Halcrow 		goto out;
62237fead6SMichael Halcrow 	wait_on_page_locked(page);
63237fead6SMichael Halcrow out:
64237fead6SMichael Halcrow 	return page;
65237fead6SMichael Halcrow }
66237fead6SMichael Halcrow 
67237fead6SMichael Halcrow static
68237fead6SMichael Halcrow int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros);
69237fead6SMichael Halcrow 
70237fead6SMichael Halcrow /**
71237fead6SMichael Halcrow  * ecryptfs_fill_zeros
72237fead6SMichael Halcrow  * @file: The ecryptfs file
73237fead6SMichael Halcrow  * @new_length: The new length of the data in the underlying file;
74237fead6SMichael Halcrow  *              everything between the prior end of the file and the
75237fead6SMichael Halcrow  *              new end of the file will be filled with zero's.
76237fead6SMichael Halcrow  *              new_length must be greater than  current length
77237fead6SMichael Halcrow  *
78237fead6SMichael Halcrow  * Function for handling lseek-ing past the end of the file.
79237fead6SMichael Halcrow  *
80237fead6SMichael Halcrow  * This function does not support shrinking, only growing a file.
81237fead6SMichael Halcrow  *
82237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise.
83237fead6SMichael Halcrow  */
84237fead6SMichael Halcrow int ecryptfs_fill_zeros(struct file *file, loff_t new_length)
85237fead6SMichael Halcrow {
86237fead6SMichael Halcrow 	int rc = 0;
87bd243a4bSJosef "Jeff" Sipek 	struct dentry *dentry = file->f_path.dentry;
88237fead6SMichael Halcrow 	struct inode *inode = dentry->d_inode;
89237fead6SMichael Halcrow 	pgoff_t old_end_page_index = 0;
90237fead6SMichael Halcrow 	pgoff_t index = old_end_page_index;
91237fead6SMichael Halcrow 	int old_end_pos_in_page = -1;
92237fead6SMichael Halcrow 	pgoff_t new_end_page_index;
93237fead6SMichael Halcrow 	int new_end_pos_in_page;
94237fead6SMichael Halcrow 	loff_t cur_length = i_size_read(inode);
95237fead6SMichael Halcrow 
96237fead6SMichael Halcrow 	if (cur_length != 0) {
97237fead6SMichael Halcrow 		index = old_end_page_index =
98237fead6SMichael Halcrow 		    ((cur_length - 1) >> PAGE_CACHE_SHIFT);
99237fead6SMichael Halcrow 		old_end_pos_in_page = ((cur_length - 1) & ~PAGE_CACHE_MASK);
100237fead6SMichael Halcrow 	}
101237fead6SMichael Halcrow 	new_end_page_index = ((new_length - 1) >> PAGE_CACHE_SHIFT);
102237fead6SMichael Halcrow 	new_end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK);
103237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "old_end_page_index = [0x%.16x]; "
104237fead6SMichael Halcrow 			"old_end_pos_in_page = [%d]; "
105237fead6SMichael Halcrow 			"new_end_page_index = [0x%.16x]; "
106237fead6SMichael Halcrow 			"new_end_pos_in_page = [%d]\n",
107237fead6SMichael Halcrow 			old_end_page_index, old_end_pos_in_page,
108237fead6SMichael Halcrow 			new_end_page_index, new_end_pos_in_page);
109237fead6SMichael Halcrow 	if (old_end_page_index == new_end_page_index) {
110237fead6SMichael Halcrow 		/* Start and end are in the same page; we just need to
111237fead6SMichael Halcrow 		 * set a portion of the existing page to zero's */
112237fead6SMichael Halcrow 		rc = write_zeros(file, index, (old_end_pos_in_page + 1),
113237fead6SMichael Halcrow 				 (new_end_pos_in_page - old_end_pos_in_page));
114237fead6SMichael Halcrow 		if (rc)
115237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
116237fead6SMichael Halcrow 					"index=[0x%.16x], "
117237fead6SMichael Halcrow 					"old_end_pos_in_page=[d], "
118237fead6SMichael Halcrow 					"(PAGE_CACHE_SIZE - new_end_pos_in_page"
119237fead6SMichael Halcrow 					"=[%d]"
120237fead6SMichael Halcrow 					")=[d]) returned [%d]\n", file, index,
121237fead6SMichael Halcrow 					old_end_pos_in_page,
122237fead6SMichael Halcrow 					new_end_pos_in_page,
123237fead6SMichael Halcrow 					(PAGE_CACHE_SIZE - new_end_pos_in_page),
124237fead6SMichael Halcrow 					rc);
125237fead6SMichael Halcrow 		goto out;
126237fead6SMichael Halcrow 	}
127237fead6SMichael Halcrow 	/* Fill the remainder of the previous last page with zeros */
128237fead6SMichael Halcrow 	rc = write_zeros(file, index, (old_end_pos_in_page + 1),
129237fead6SMichael Halcrow 			 ((PAGE_CACHE_SIZE - 1) - old_end_pos_in_page));
130237fead6SMichael Halcrow 	if (rc) {
131237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
132237fead6SMichael Halcrow 				"index=[0x%.16x], old_end_pos_in_page=[d], "
133237fead6SMichael Halcrow 				"(PAGE_CACHE_SIZE - old_end_pos_in_page)=[d]) "
134237fead6SMichael Halcrow 				"returned [%d]\n", file, index,
135237fead6SMichael Halcrow 				old_end_pos_in_page,
136237fead6SMichael Halcrow 				(PAGE_CACHE_SIZE - old_end_pos_in_page), rc);
137237fead6SMichael Halcrow 		goto out;
138237fead6SMichael Halcrow 	}
139237fead6SMichael Halcrow 	index++;
140237fead6SMichael Halcrow 	while (index < new_end_page_index) {
141237fead6SMichael Halcrow 		/* Fill all intermediate pages with zeros */
142237fead6SMichael Halcrow 		rc = write_zeros(file, index, 0, PAGE_CACHE_SIZE);
143237fead6SMichael Halcrow 		if (rc) {
144237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
145237fead6SMichael Halcrow 					"index=[0x%.16x], "
146237fead6SMichael Halcrow 					"old_end_pos_in_page=[d], "
147237fead6SMichael Halcrow 					"(PAGE_CACHE_SIZE - new_end_pos_in_page"
148237fead6SMichael Halcrow 					"=[%d]"
149237fead6SMichael Halcrow 					")=[d]) returned [%d]\n", file, index,
150237fead6SMichael Halcrow 					old_end_pos_in_page,
151237fead6SMichael Halcrow 					new_end_pos_in_page,
152237fead6SMichael Halcrow 					(PAGE_CACHE_SIZE - new_end_pos_in_page),
153237fead6SMichael Halcrow 					rc);
154237fead6SMichael Halcrow 			goto out;
155237fead6SMichael Halcrow 		}
156237fead6SMichael Halcrow 		index++;
157237fead6SMichael Halcrow 	}
158237fead6SMichael Halcrow 	/* Fill the portion at the beginning of the last new page with
159237fead6SMichael Halcrow 	 * zero's */
160237fead6SMichael Halcrow 	rc = write_zeros(file, index, 0, (new_end_pos_in_page + 1));
161237fead6SMichael Halcrow 	if (rc) {
162237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "write_zeros(file="
163237fead6SMichael Halcrow 				"[%p], index=[0x%.16x], 0, "
164237fead6SMichael Halcrow 				"new_end_pos_in_page=[%d]"
165237fead6SMichael Halcrow 				"returned [%d]\n", file, index,
166237fead6SMichael Halcrow 				new_end_pos_in_page, rc);
167237fead6SMichael Halcrow 		goto out;
168237fead6SMichael Halcrow 	}
169237fead6SMichael Halcrow out:
170237fead6SMichael Halcrow 	return rc;
171237fead6SMichael Halcrow }
172237fead6SMichael Halcrow 
173237fead6SMichael Halcrow /**
174237fead6SMichael Halcrow  * ecryptfs_writepage
175237fead6SMichael Halcrow  * @page: Page that is locked before this call is made
176237fead6SMichael Halcrow  *
177237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
178237fead6SMichael Halcrow  */
179237fead6SMichael Halcrow static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
180237fead6SMichael Halcrow {
181237fead6SMichael Halcrow 	struct ecryptfs_page_crypt_context ctx;
182237fead6SMichael Halcrow 	int rc;
183237fead6SMichael Halcrow 
184237fead6SMichael Halcrow 	ctx.page = page;
185237fead6SMichael Halcrow 	ctx.mode = ECRYPTFS_WRITEPAGE_MODE;
186237fead6SMichael Halcrow 	ctx.param.wbc = wbc;
187237fead6SMichael Halcrow 	rc = ecryptfs_encrypt_page(&ctx);
188237fead6SMichael Halcrow 	if (rc) {
189237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error encrypting "
190237fead6SMichael Halcrow 				"page (upper index [0x%.16x])\n", page->index);
191237fead6SMichael Halcrow 		ClearPageUptodate(page);
192237fead6SMichael Halcrow 		goto out;
193237fead6SMichael Halcrow 	}
194237fead6SMichael Halcrow 	SetPageUptodate(page);
195237fead6SMichael Halcrow 	unlock_page(page);
196237fead6SMichael Halcrow out:
197237fead6SMichael Halcrow 	return rc;
198237fead6SMichael Halcrow }
199237fead6SMichael Halcrow 
200237fead6SMichael Halcrow /**
201237fead6SMichael Halcrow  * Reads the data from the lower file file at index lower_page_index
202237fead6SMichael Halcrow  * and copies that data into page.
203237fead6SMichael Halcrow  *
204237fead6SMichael Halcrow  * @param page	Page to fill
205237fead6SMichael Halcrow  * @param lower_page_index Index of the page in the lower file to get
206237fead6SMichael Halcrow  */
207237fead6SMichael Halcrow int ecryptfs_do_readpage(struct file *file, struct page *page,
208237fead6SMichael Halcrow 			 pgoff_t lower_page_index)
209237fead6SMichael Halcrow {
210237fead6SMichael Halcrow 	int rc;
211237fead6SMichael Halcrow 	struct dentry *dentry;
212237fead6SMichael Halcrow 	struct file *lower_file;
213237fead6SMichael Halcrow 	struct dentry *lower_dentry;
214237fead6SMichael Halcrow 	struct inode *inode;
215237fead6SMichael Halcrow 	struct inode *lower_inode;
216237fead6SMichael Halcrow 	char *page_data;
217237fead6SMichael Halcrow 	struct page *lower_page = NULL;
218237fead6SMichael Halcrow 	char *lower_page_data;
219237fead6SMichael Halcrow 	const struct address_space_operations *lower_a_ops;
220237fead6SMichael Halcrow 
221bd243a4bSJosef "Jeff" Sipek 	dentry = file->f_path.dentry;
222237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
223237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
224237fead6SMichael Halcrow 	inode = dentry->d_inode;
225237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
226237fead6SMichael Halcrow 	lower_a_ops = lower_inode->i_mapping->a_ops;
227237fead6SMichael Halcrow 	lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index,
228237fead6SMichael Halcrow 				     (filler_t *)lower_a_ops->readpage,
229237fead6SMichael Halcrow 				     (void *)lower_file);
230237fead6SMichael Halcrow 	if (IS_ERR(lower_page)) {
231237fead6SMichael Halcrow 		rc = PTR_ERR(lower_page);
232237fead6SMichael Halcrow 		lower_page = NULL;
233237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error reading from page cache\n");
234237fead6SMichael Halcrow 		goto out;
235237fead6SMichael Halcrow 	}
236237fead6SMichael Halcrow 	wait_on_page_locked(lower_page);
237237fead6SMichael Halcrow 	page_data = (char *)kmap(page);
238237fead6SMichael Halcrow 	if (!page_data) {
239237fead6SMichael Halcrow 		rc = -ENOMEM;
240237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error mapping page\n");
241237fead6SMichael Halcrow 		goto out;
242237fead6SMichael Halcrow 	}
243237fead6SMichael Halcrow 	lower_page_data = (char *)kmap(lower_page);
244237fead6SMichael Halcrow 	if (!lower_page_data) {
245237fead6SMichael Halcrow 		rc = -ENOMEM;
246237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error mapping page\n");
247237fead6SMichael Halcrow 		kunmap(page);
248237fead6SMichael Halcrow 		goto out;
249237fead6SMichael Halcrow 	}
250237fead6SMichael Halcrow 	memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE);
251237fead6SMichael Halcrow 	kunmap(lower_page);
252237fead6SMichael Halcrow 	kunmap(page);
253237fead6SMichael Halcrow 	rc = 0;
254237fead6SMichael Halcrow out:
255237fead6SMichael Halcrow 	if (likely(lower_page))
256237fead6SMichael Halcrow 		page_cache_release(lower_page);
257237fead6SMichael Halcrow 	if (rc == 0)
258237fead6SMichael Halcrow 		SetPageUptodate(page);
259237fead6SMichael Halcrow 	else
260237fead6SMichael Halcrow 		ClearPageUptodate(page);
261237fead6SMichael Halcrow 	return rc;
262237fead6SMichael Halcrow }
263237fead6SMichael Halcrow 
264237fead6SMichael Halcrow /**
265237fead6SMichael Halcrow  * ecryptfs_readpage
266237fead6SMichael Halcrow  * @file: This is an ecryptfs file
267237fead6SMichael Halcrow  * @page: ecryptfs associated page to stick the read data into
268237fead6SMichael Halcrow  *
269237fead6SMichael Halcrow  * Read in a page, decrypting if necessary.
270237fead6SMichael Halcrow  *
271237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
272237fead6SMichael Halcrow  */
273237fead6SMichael Halcrow static int ecryptfs_readpage(struct file *file, struct page *page)
274237fead6SMichael Halcrow {
275237fead6SMichael Halcrow 	int rc = 0;
276237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
277237fead6SMichael Halcrow 
278bd243a4bSJosef "Jeff" Sipek 	BUG_ON(!(file && file->f_path.dentry && file->f_path.dentry->d_inode));
279bd243a4bSJosef "Jeff" Sipek 	crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
280bd243a4bSJosef "Jeff" Sipek 			->crypt_stat;
281237fead6SMichael Halcrow 	if (!crypt_stat
282237fead6SMichael Halcrow 	    || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)
283237fead6SMichael Halcrow 	    || ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) {
284237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG,
285237fead6SMichael Halcrow 				"Passing through unencrypted page\n");
286237fead6SMichael Halcrow 		rc = ecryptfs_do_readpage(file, page, page->index);
287237fead6SMichael Halcrow 		if (rc) {
288237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error reading page; rc = "
289237fead6SMichael Halcrow 					"[%d]\n", rc);
290237fead6SMichael Halcrow 			goto out;
291237fead6SMichael Halcrow 		}
292237fead6SMichael Halcrow 	} else {
293237fead6SMichael Halcrow 		rc = ecryptfs_decrypt_page(file, page);
294237fead6SMichael Halcrow 		if (rc) {
295237fead6SMichael Halcrow 
296237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error decrypting page; "
297237fead6SMichael Halcrow 					"rc = [%d]\n", rc);
298237fead6SMichael Halcrow 			goto out;
299237fead6SMichael Halcrow 		}
300237fead6SMichael Halcrow 	}
301237fead6SMichael Halcrow 	SetPageUptodate(page);
302237fead6SMichael Halcrow out:
303237fead6SMichael Halcrow 	if (rc)
304237fead6SMichael Halcrow 		ClearPageUptodate(page);
305237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
306237fead6SMichael Halcrow 			page->index);
307237fead6SMichael Halcrow 	unlock_page(page);
308237fead6SMichael Halcrow 	return rc;
309237fead6SMichael Halcrow }
310237fead6SMichael Halcrow 
311*dd2a3b7aSMichael Halcrow /**
312*dd2a3b7aSMichael Halcrow  * Called with lower inode mutex held.
313*dd2a3b7aSMichael Halcrow  */
314237fead6SMichael Halcrow static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
315237fead6SMichael Halcrow {
316237fead6SMichael Halcrow 	struct inode *inode = page->mapping->host;
317237fead6SMichael Halcrow 	int end_byte_in_page;
318237fead6SMichael Halcrow 	int rc = 0;
319237fead6SMichael Halcrow 	char *page_virt;
320237fead6SMichael Halcrow 
321237fead6SMichael Halcrow 	if ((i_size_read(inode) / PAGE_CACHE_SIZE) == page->index) {
322237fead6SMichael Halcrow 		end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
323237fead6SMichael Halcrow 		if (to > end_byte_in_page)
324237fead6SMichael Halcrow 			end_byte_in_page = to;
325237fead6SMichael Halcrow 		page_virt = kmap(page);
326237fead6SMichael Halcrow 		if (!page_virt) {
327237fead6SMichael Halcrow 			rc = -ENOMEM;
328237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING,
329237fead6SMichael Halcrow 					"Could not map page\n");
330237fead6SMichael Halcrow 			goto out;
331237fead6SMichael Halcrow 		}
332237fead6SMichael Halcrow 		memset((page_virt + end_byte_in_page), 0,
333237fead6SMichael Halcrow 		       (PAGE_CACHE_SIZE - end_byte_in_page));
334237fead6SMichael Halcrow 		kunmap(page);
335237fead6SMichael Halcrow 	}
336237fead6SMichael Halcrow out:
337237fead6SMichael Halcrow 	return rc;
338237fead6SMichael Halcrow }
339237fead6SMichael Halcrow 
340237fead6SMichael Halcrow static int ecryptfs_prepare_write(struct file *file, struct page *page,
341237fead6SMichael Halcrow 				  unsigned from, unsigned to)
342237fead6SMichael Halcrow {
343237fead6SMichael Halcrow 	int rc = 0;
344237fead6SMichael Halcrow 
345237fead6SMichael Halcrow 	kmap(page);
346237fead6SMichael Halcrow 	if (from == 0 && to == PAGE_CACHE_SIZE)
347237fead6SMichael Halcrow 		goto out;	/* If we are writing a full page, it will be
348237fead6SMichael Halcrow 				   up to date. */
349237fead6SMichael Halcrow 	if (!PageUptodate(page))
350237fead6SMichael Halcrow 		rc = ecryptfs_do_readpage(file, page, page->index);
351237fead6SMichael Halcrow out:
352237fead6SMichael Halcrow 	return rc;
353237fead6SMichael Halcrow }
354237fead6SMichael Halcrow 
355237fead6SMichael Halcrow int ecryptfs_grab_and_map_lower_page(struct page **lower_page,
356237fead6SMichael Halcrow 				     char **lower_virt,
357237fead6SMichael Halcrow 				     struct inode *lower_inode,
358237fead6SMichael Halcrow 				     unsigned long lower_page_index)
359237fead6SMichael Halcrow {
360237fead6SMichael Halcrow 	int rc = 0;
361237fead6SMichael Halcrow 
362237fead6SMichael Halcrow 	(*lower_page) = grab_cache_page(lower_inode->i_mapping,
363237fead6SMichael Halcrow 					lower_page_index);
364237fead6SMichael Halcrow 	if (!(*lower_page)) {
365237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "grab_cache_page for "
366237fead6SMichael Halcrow 				"lower_page_index = [0x%.16x] failed\n",
367237fead6SMichael Halcrow 				lower_page_index);
368237fead6SMichael Halcrow 		rc = -EINVAL;
369237fead6SMichael Halcrow 		goto out;
370237fead6SMichael Halcrow 	}
371237fead6SMichael Halcrow 	if (lower_virt)
372237fead6SMichael Halcrow 		(*lower_virt) = kmap((*lower_page));
373237fead6SMichael Halcrow 	else
374237fead6SMichael Halcrow 		kmap((*lower_page));
375237fead6SMichael Halcrow out:
376237fead6SMichael Halcrow 	return rc;
377237fead6SMichael Halcrow }
378237fead6SMichael Halcrow 
379237fead6SMichael Halcrow int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
380237fead6SMichael Halcrow 					      struct inode *lower_inode,
381237fead6SMichael Halcrow 					      struct writeback_control *wbc)
382237fead6SMichael Halcrow {
383237fead6SMichael Halcrow 	int rc = 0;
384237fead6SMichael Halcrow 
385237fead6SMichael Halcrow 	rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
386237fead6SMichael Halcrow 	if (rc) {
387237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
388237fead6SMichael Halcrow 				"rc = [%d]\n", rc);
389237fead6SMichael Halcrow 		goto out;
390237fead6SMichael Halcrow 	}
391237fead6SMichael Halcrow 	lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
392237fead6SMichael Halcrow 	page_cache_release(lower_page);
393237fead6SMichael Halcrow out:
394237fead6SMichael Halcrow 	return rc;
395237fead6SMichael Halcrow }
396237fead6SMichael Halcrow 
397237fead6SMichael Halcrow static void ecryptfs_unmap_and_release_lower_page(struct page *lower_page)
398237fead6SMichael Halcrow {
399237fead6SMichael Halcrow 	kunmap(lower_page);
400237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Unlocking lower page with index = "
401237fead6SMichael Halcrow 			"[0x%.16x]\n", lower_page->index);
402237fead6SMichael Halcrow 	unlock_page(lower_page);
403237fead6SMichael Halcrow 	page_cache_release(lower_page);
404237fead6SMichael Halcrow }
405237fead6SMichael Halcrow 
406237fead6SMichael Halcrow /**
407237fead6SMichael Halcrow  * ecryptfs_write_inode_size_to_header
408237fead6SMichael Halcrow  *
409237fead6SMichael Halcrow  * Writes the lower file size to the first 8 bytes of the header.
410237fead6SMichael Halcrow  *
411237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
412237fead6SMichael Halcrow  */
413*dd2a3b7aSMichael Halcrow static int ecryptfs_write_inode_size_to_header(struct file *lower_file,
414237fead6SMichael Halcrow 					       struct inode *lower_inode,
415237fead6SMichael Halcrow 					       struct inode *inode)
416237fead6SMichael Halcrow {
417237fead6SMichael Halcrow 	int rc = 0;
418237fead6SMichael Halcrow 	struct page *header_page;
419237fead6SMichael Halcrow 	char *header_virt;
420237fead6SMichael Halcrow 	const struct address_space_operations *lower_a_ops;
421237fead6SMichael Halcrow 	u64 file_size;
422237fead6SMichael Halcrow 
423237fead6SMichael Halcrow 	rc = ecryptfs_grab_and_map_lower_page(&header_page, &header_virt,
424237fead6SMichael Halcrow 					      lower_inode, 0);
425237fead6SMichael Halcrow 	if (rc) {
426237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "grab_cache_page for header page "
427237fead6SMichael Halcrow 				"failed\n");
428237fead6SMichael Halcrow 		goto out;
429237fead6SMichael Halcrow 	}
430237fead6SMichael Halcrow 	lower_a_ops = lower_inode->i_mapping->a_ops;
431237fead6SMichael Halcrow 	rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8);
432237fead6SMichael Halcrow 	file_size = (u64)i_size_read(inode);
433237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size);
434237fead6SMichael Halcrow 	file_size = cpu_to_be64(file_size);
435237fead6SMichael Halcrow 	memcpy(header_virt, &file_size, sizeof(u64));
436237fead6SMichael Halcrow 	rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8);
437237fead6SMichael Halcrow 	if (rc < 0)
438237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error commiting header page "
439237fead6SMichael Halcrow 				"write\n");
440237fead6SMichael Halcrow 	ecryptfs_unmap_and_release_lower_page(header_page);
441237fead6SMichael Halcrow 	lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
442237fead6SMichael Halcrow 	mark_inode_dirty_sync(inode);
443237fead6SMichael Halcrow out:
444237fead6SMichael Halcrow 	return rc;
445237fead6SMichael Halcrow }
446237fead6SMichael Halcrow 
447*dd2a3b7aSMichael Halcrow static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode,
448*dd2a3b7aSMichael Halcrow 					      struct inode *inode,
449*dd2a3b7aSMichael Halcrow 					      struct dentry *ecryptfs_dentry,
450*dd2a3b7aSMichael Halcrow 					      int lower_i_mutex_held)
451*dd2a3b7aSMichael Halcrow {
452*dd2a3b7aSMichael Halcrow 	ssize_t size;
453*dd2a3b7aSMichael Halcrow 	void *xattr_virt;
454*dd2a3b7aSMichael Halcrow 	struct dentry *lower_dentry;
455*dd2a3b7aSMichael Halcrow 	u64 file_size;
456*dd2a3b7aSMichael Halcrow 	int rc;
457*dd2a3b7aSMichael Halcrow 
458*dd2a3b7aSMichael Halcrow 	xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
459*dd2a3b7aSMichael Halcrow 	if (!xattr_virt) {
460*dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Out of memory whilst attempting to write "
461*dd2a3b7aSMichael Halcrow 		       "inode size to xattr\n");
462*dd2a3b7aSMichael Halcrow 		rc = -ENOMEM;
463*dd2a3b7aSMichael Halcrow 		goto out;
464*dd2a3b7aSMichael Halcrow 	}
465*dd2a3b7aSMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
466*dd2a3b7aSMichael Halcrow 	if (!lower_dentry->d_inode->i_op->getxattr) {
467*dd2a3b7aSMichael Halcrow 		printk(KERN_WARNING
468*dd2a3b7aSMichael Halcrow 		       "No support for setting xattr in lower filesystem\n");
469*dd2a3b7aSMichael Halcrow 		rc = -ENOSYS;
470*dd2a3b7aSMichael Halcrow 		kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
471*dd2a3b7aSMichael Halcrow 		goto out;
472*dd2a3b7aSMichael Halcrow 	}
473*dd2a3b7aSMichael Halcrow 	if (!lower_i_mutex_held)
474*dd2a3b7aSMichael Halcrow 		mutex_lock(&lower_dentry->d_inode->i_mutex);
475*dd2a3b7aSMichael Halcrow 	size = lower_dentry->d_inode->i_op->getxattr(lower_dentry,
476*dd2a3b7aSMichael Halcrow 						     ECRYPTFS_XATTR_NAME,
477*dd2a3b7aSMichael Halcrow 						     xattr_virt,
478*dd2a3b7aSMichael Halcrow 						     PAGE_CACHE_SIZE);
479*dd2a3b7aSMichael Halcrow 	if (!lower_i_mutex_held)
480*dd2a3b7aSMichael Halcrow 		mutex_unlock(&lower_dentry->d_inode->i_mutex);
481*dd2a3b7aSMichael Halcrow 	if (size < 0)
482*dd2a3b7aSMichael Halcrow 		size = 8;
483*dd2a3b7aSMichael Halcrow 	file_size = (u64)i_size_read(inode);
484*dd2a3b7aSMichael Halcrow 	file_size = cpu_to_be64(file_size);
485*dd2a3b7aSMichael Halcrow 	memcpy(xattr_virt, &file_size, sizeof(u64));
486*dd2a3b7aSMichael Halcrow 	if (!lower_i_mutex_held)
487*dd2a3b7aSMichael Halcrow 		mutex_lock(&lower_dentry->d_inode->i_mutex);
488*dd2a3b7aSMichael Halcrow 	rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry,
489*dd2a3b7aSMichael Halcrow 						   ECRYPTFS_XATTR_NAME,
490*dd2a3b7aSMichael Halcrow 						   xattr_virt, size, 0);
491*dd2a3b7aSMichael Halcrow 	if (!lower_i_mutex_held)
492*dd2a3b7aSMichael Halcrow 		mutex_unlock(&lower_dentry->d_inode->i_mutex);
493*dd2a3b7aSMichael Halcrow 	if (rc)
494*dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error whilst attempting to write inode size "
495*dd2a3b7aSMichael Halcrow 		       "to lower file xattr; rc = [%d]\n", rc);
496*dd2a3b7aSMichael Halcrow 	kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
497*dd2a3b7aSMichael Halcrow out:
498*dd2a3b7aSMichael Halcrow 	return rc;
499*dd2a3b7aSMichael Halcrow }
500*dd2a3b7aSMichael Halcrow 
501*dd2a3b7aSMichael Halcrow int
502*dd2a3b7aSMichael Halcrow ecryptfs_write_inode_size_to_metadata(struct file *lower_file,
503*dd2a3b7aSMichael Halcrow 				      struct inode *lower_inode,
504*dd2a3b7aSMichael Halcrow 				      struct inode *inode,
505*dd2a3b7aSMichael Halcrow 				      struct dentry *ecryptfs_dentry,
506*dd2a3b7aSMichael Halcrow 				      int lower_i_mutex_held)
507*dd2a3b7aSMichael Halcrow {
508*dd2a3b7aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
509*dd2a3b7aSMichael Halcrow 
510*dd2a3b7aSMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
511*dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
512*dd2a3b7aSMichael Halcrow 		return ecryptfs_write_inode_size_to_xattr(lower_inode, inode,
513*dd2a3b7aSMichael Halcrow 							  ecryptfs_dentry,
514*dd2a3b7aSMichael Halcrow 							  lower_i_mutex_held);
515*dd2a3b7aSMichael Halcrow 	else
516*dd2a3b7aSMichael Halcrow 		return ecryptfs_write_inode_size_to_header(lower_file,
517*dd2a3b7aSMichael Halcrow 							   lower_inode,
518*dd2a3b7aSMichael Halcrow 							   inode);
519*dd2a3b7aSMichael Halcrow }
520*dd2a3b7aSMichael Halcrow 
521237fead6SMichael Halcrow int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
522237fead6SMichael Halcrow 			    struct file *lower_file,
523237fead6SMichael Halcrow 			    unsigned long lower_page_index, int byte_offset,
524237fead6SMichael Halcrow 			    int region_bytes)
525237fead6SMichael Halcrow {
526237fead6SMichael Halcrow 	int rc = 0;
527237fead6SMichael Halcrow 
528237fead6SMichael Halcrow 	rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL, lower_inode,
529237fead6SMichael Halcrow 					      lower_page_index);
530237fead6SMichael Halcrow 	if (rc) {
531237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to grab and map "
532237fead6SMichael Halcrow 				"lower page with index [0x%.16x]\n",
533237fead6SMichael Halcrow 				lower_page_index);
534237fead6SMichael Halcrow 		goto out;
535237fead6SMichael Halcrow 	}
536237fead6SMichael Halcrow 	rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
537237fead6SMichael Halcrow 							  (*lower_page),
538237fead6SMichael Halcrow 							  byte_offset,
539237fead6SMichael Halcrow 							  region_bytes);
540237fead6SMichael Halcrow 	if (rc) {
541237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "prepare_write for "
542237fead6SMichael Halcrow 				"lower_page_index = [0x%.16x] failed; rc = "
543237fead6SMichael Halcrow 				"[%d]\n", lower_page_index, rc);
544237fead6SMichael Halcrow 	}
545237fead6SMichael Halcrow out:
546237fead6SMichael Halcrow 	if (rc && (*lower_page)) {
547237fead6SMichael Halcrow 		ecryptfs_unmap_and_release_lower_page(*lower_page);
548237fead6SMichael Halcrow 		(*lower_page) = NULL;
549237fead6SMichael Halcrow 	}
550237fead6SMichael Halcrow 	return rc;
551237fead6SMichael Halcrow }
552237fead6SMichael Halcrow 
553237fead6SMichael Halcrow /**
554237fead6SMichael Halcrow  * ecryptfs_commit_lower_page
555237fead6SMichael Halcrow  *
556237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
557237fead6SMichael Halcrow  */
558237fead6SMichael Halcrow int
559237fead6SMichael Halcrow ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
560237fead6SMichael Halcrow 			   struct file *lower_file, int byte_offset,
561237fead6SMichael Halcrow 			   int region_size)
562237fead6SMichael Halcrow {
563237fead6SMichael Halcrow 	int rc = 0;
564237fead6SMichael Halcrow 
565237fead6SMichael Halcrow 	rc = lower_inode->i_mapping->a_ops->commit_write(
566237fead6SMichael Halcrow 		lower_file, lower_page, byte_offset, region_size);
567237fead6SMichael Halcrow 	if (rc < 0) {
568237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
569237fead6SMichael Halcrow 				"Error committing write; rc = [%d]\n", rc);
570237fead6SMichael Halcrow 	} else
571237fead6SMichael Halcrow 		rc = 0;
572237fead6SMichael Halcrow 	ecryptfs_unmap_and_release_lower_page(lower_page);
573237fead6SMichael Halcrow 	return rc;
574237fead6SMichael Halcrow }
575237fead6SMichael Halcrow 
576237fead6SMichael Halcrow /**
577237fead6SMichael Halcrow  * ecryptfs_copy_page_to_lower
578237fead6SMichael Halcrow  *
579237fead6SMichael Halcrow  * Used for plaintext pass-through; no page index interpolation
580237fead6SMichael Halcrow  * required.
581237fead6SMichael Halcrow  */
582237fead6SMichael Halcrow int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
583237fead6SMichael Halcrow 				struct file *lower_file)
584237fead6SMichael Halcrow {
585237fead6SMichael Halcrow 	int rc = 0;
586237fead6SMichael Halcrow 	struct page *lower_page;
587237fead6SMichael Halcrow 
588237fead6SMichael Halcrow 	rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
589237fead6SMichael Halcrow 				     page->index, 0, PAGE_CACHE_SIZE);
590237fead6SMichael Halcrow 	if (rc) {
591237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to get page "
592237fead6SMichael Halcrow 				"at index [0x%.16x]\n", page->index);
593237fead6SMichael Halcrow 		goto out;
594237fead6SMichael Halcrow 	}
595237fead6SMichael Halcrow 	/* TODO: aops */
596237fead6SMichael Halcrow 	memcpy((char *)page_address(lower_page), page_address(page),
597237fead6SMichael Halcrow 	       PAGE_CACHE_SIZE);
598237fead6SMichael Halcrow 	rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
599237fead6SMichael Halcrow 					0, PAGE_CACHE_SIZE);
600237fead6SMichael Halcrow 	if (rc)
601237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
602237fead6SMichael Halcrow 				"at index [0x%.16x]\n", page->index);
603237fead6SMichael Halcrow out:
604237fead6SMichael Halcrow 	return rc;
605237fead6SMichael Halcrow }
606237fead6SMichael Halcrow 
607*dd2a3b7aSMichael Halcrow struct kmem_cache *ecryptfs_xattr_cache;
608*dd2a3b7aSMichael Halcrow 
609237fead6SMichael Halcrow /**
610237fead6SMichael Halcrow  * ecryptfs_commit_write
611237fead6SMichael Halcrow  * @file: The eCryptfs file object
612237fead6SMichael Halcrow  * @page: The eCryptfs page
613237fead6SMichael Halcrow  * @from: Ignored (we rotate the page IV on each write)
614237fead6SMichael Halcrow  * @to: Ignored
615237fead6SMichael Halcrow  *
616237fead6SMichael Halcrow  * This is where we encrypt the data and pass the encrypted data to
617237fead6SMichael Halcrow  * the lower filesystem.  In OpenPGP-compatible mode, we operate on
618237fead6SMichael Halcrow  * entire underlying packets.
619237fead6SMichael Halcrow  */
620237fead6SMichael Halcrow static int ecryptfs_commit_write(struct file *file, struct page *page,
621237fead6SMichael Halcrow 				 unsigned from, unsigned to)
622237fead6SMichael Halcrow {
623237fead6SMichael Halcrow 	struct ecryptfs_page_crypt_context ctx;
624237fead6SMichael Halcrow 	loff_t pos;
625237fead6SMichael Halcrow 	struct inode *inode;
626237fead6SMichael Halcrow 	struct inode *lower_inode;
627237fead6SMichael Halcrow 	struct file *lower_file;
628237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
629237fead6SMichael Halcrow 	int rc;
630237fead6SMichael Halcrow 
631237fead6SMichael Halcrow 	inode = page->mapping->host;
632237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
633237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
634237fead6SMichael Halcrow 	mutex_lock(&lower_inode->i_mutex);
635bd243a4bSJosef "Jeff" Sipek 	crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
636bd243a4bSJosef "Jeff" Sipek 				->crypt_stat;
637237fead6SMichael Halcrow 	if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) {
638237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
639237fead6SMichael Halcrow 			"crypt_stat at memory location [%p]\n", crypt_stat);
640dddfa461SMichael Halcrow 		ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE);
641237fead6SMichael Halcrow 	} else
642237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
643237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
644237fead6SMichael Halcrow 			"(page w/ index = [0x%.16x], to = [%d])\n", page->index,
645237fead6SMichael Halcrow 			to);
646237fead6SMichael Halcrow 	rc = fill_zeros_to_end_of_page(page, to);
647237fead6SMichael Halcrow 	if (rc) {
648237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
649237fead6SMichael Halcrow 				"zeros in page with index = [0x%.16x]\n",
650237fead6SMichael Halcrow 				page->index);
651237fead6SMichael Halcrow 		goto out;
652237fead6SMichael Halcrow 	}
653237fead6SMichael Halcrow 	ctx.page = page;
654237fead6SMichael Halcrow 	ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE;
655237fead6SMichael Halcrow 	ctx.param.lower_file = lower_file;
656237fead6SMichael Halcrow 	rc = ecryptfs_encrypt_page(&ctx);
657237fead6SMichael Halcrow 	if (rc) {
658237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
659237fead6SMichael Halcrow 				"index [0x%.16x])\n", page->index);
660237fead6SMichael Halcrow 		goto out;
661237fead6SMichael Halcrow 	}
662237fead6SMichael Halcrow 	inode->i_blocks = lower_inode->i_blocks;
663237fead6SMichael Halcrow 	pos = (page->index << PAGE_CACHE_SHIFT) + to;
664237fead6SMichael Halcrow 	if (pos > i_size_read(inode)) {
665237fead6SMichael Halcrow 		i_size_write(inode, pos);
666237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
667237fead6SMichael Halcrow 				"[0x%.16x]\n", i_size_read(inode));
668237fead6SMichael Halcrow 	}
669*dd2a3b7aSMichael Halcrow 	rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode,
670*dd2a3b7aSMichael Halcrow 						   inode, file->f_dentry,
671*dd2a3b7aSMichael Halcrow 						   ECRYPTFS_LOWER_I_MUTEX_HELD);
672*dd2a3b7aSMichael Halcrow 	if (rc)
673*dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error writing inode size to metadata; "
674*dd2a3b7aSMichael Halcrow 		       "rc = [%d]\n", rc);
675237fead6SMichael Halcrow 	lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
676237fead6SMichael Halcrow 	mark_inode_dirty_sync(inode);
677237fead6SMichael Halcrow out:
678237fead6SMichael Halcrow 	kunmap(page); /* mapped in prior call (prepare_write) */
679237fead6SMichael Halcrow 	if (rc < 0)
680237fead6SMichael Halcrow 		ClearPageUptodate(page);
681237fead6SMichael Halcrow 	else
682237fead6SMichael Halcrow 		SetPageUptodate(page);
683237fead6SMichael Halcrow 	mutex_unlock(&lower_inode->i_mutex);
684237fead6SMichael Halcrow 	return rc;
685237fead6SMichael Halcrow }
686237fead6SMichael Halcrow 
687237fead6SMichael Halcrow /**
688237fead6SMichael Halcrow  * write_zeros
689237fead6SMichael Halcrow  * @file: The ecryptfs file
690237fead6SMichael Halcrow  * @index: The index in which we are writing
691237fead6SMichael Halcrow  * @start: The position after the last block of data
692237fead6SMichael Halcrow  * @num_zeros: The number of zeros to write
693237fead6SMichael Halcrow  *
694237fead6SMichael Halcrow  * Write a specified number of zero's to a page.
695237fead6SMichael Halcrow  *
696237fead6SMichael Halcrow  * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
697237fead6SMichael Halcrow  */
698237fead6SMichael Halcrow static
699237fead6SMichael Halcrow int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
700237fead6SMichael Halcrow {
701237fead6SMichael Halcrow 	int rc = 0;
702237fead6SMichael Halcrow 	struct page *tmp_page;
703237fead6SMichael Halcrow 
704237fead6SMichael Halcrow 	tmp_page = ecryptfs_get1page(file, index);
705237fead6SMichael Halcrow 	if (IS_ERR(tmp_page)) {
706237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error getting page at index "
707237fead6SMichael Halcrow 				"[0x%.16x]\n", index);
708237fead6SMichael Halcrow 		rc = PTR_ERR(tmp_page);
709237fead6SMichael Halcrow 		goto out;
710237fead6SMichael Halcrow 	}
711237fead6SMichael Halcrow 	kmap(tmp_page);
712237fead6SMichael Halcrow 	rc = ecryptfs_prepare_write(file, tmp_page, start, start + num_zeros);
713237fead6SMichael Halcrow 	if (rc) {
714237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
715237fead6SMichael Halcrow 				"to remainder of page at index [0x%.16x]\n",
716237fead6SMichael Halcrow 				index);
717237fead6SMichael Halcrow 		kunmap(tmp_page);
718237fead6SMichael Halcrow 		page_cache_release(tmp_page);
719237fead6SMichael Halcrow 		goto out;
720237fead6SMichael Halcrow 	}
721237fead6SMichael Halcrow 	memset(((char *)page_address(tmp_page) + start), 0, num_zeros);
722237fead6SMichael Halcrow 	rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
723237fead6SMichael Halcrow 	if (rc < 0) {
724237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
725237fead6SMichael Halcrow 				"to remainder of page at index [0x%.16x]\n",
726237fead6SMichael Halcrow 				index);
727237fead6SMichael Halcrow 		kunmap(tmp_page);
728237fead6SMichael Halcrow 		page_cache_release(tmp_page);
729237fead6SMichael Halcrow 		goto out;
730237fead6SMichael Halcrow 	}
731237fead6SMichael Halcrow 	rc = 0;
732237fead6SMichael Halcrow 	kunmap(tmp_page);
733237fead6SMichael Halcrow 	page_cache_release(tmp_page);
734237fead6SMichael Halcrow out:
735237fead6SMichael Halcrow 	return rc;
736237fead6SMichael Halcrow }
737237fead6SMichael Halcrow 
738237fead6SMichael Halcrow static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
739237fead6SMichael Halcrow {
740237fead6SMichael Halcrow 	int rc = 0;
741237fead6SMichael Halcrow 	struct inode *inode;
742237fead6SMichael Halcrow 	struct inode *lower_inode;
743237fead6SMichael Halcrow 
744237fead6SMichael Halcrow 	inode = (struct inode *)mapping->host;
745237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
746237fead6SMichael Halcrow 	if (lower_inode->i_mapping->a_ops->bmap)
747237fead6SMichael Halcrow 		rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
748237fead6SMichael Halcrow 							 block);
749237fead6SMichael Halcrow 	return rc;
750237fead6SMichael Halcrow }
751237fead6SMichael Halcrow 
752237fead6SMichael Halcrow static void ecryptfs_sync_page(struct page *page)
753237fead6SMichael Halcrow {
754237fead6SMichael Halcrow 	struct inode *inode;
755237fead6SMichael Halcrow 	struct inode *lower_inode;
756237fead6SMichael Halcrow 	struct page *lower_page;
757237fead6SMichael Halcrow 
758237fead6SMichael Halcrow 	inode = page->mapping->host;
759237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
760237fead6SMichael Halcrow 	/* NOTE: Recently swapped with grab_cache_page(), since
761237fead6SMichael Halcrow 	 * sync_page() just makes sure that pending I/O gets done. */
762237fead6SMichael Halcrow 	lower_page = find_lock_page(lower_inode->i_mapping, page->index);
763237fead6SMichael Halcrow 	if (!lower_page) {
764237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n");
765237fead6SMichael Halcrow 		return;
766237fead6SMichael Halcrow 	}
767237fead6SMichael Halcrow 	lower_page->mapping->a_ops->sync_page(lower_page);
768237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
769237fead6SMichael Halcrow 			lower_page->index);
770237fead6SMichael Halcrow 	unlock_page(lower_page);
771237fead6SMichael Halcrow 	page_cache_release(lower_page);
772237fead6SMichael Halcrow }
773237fead6SMichael Halcrow 
774237fead6SMichael Halcrow struct address_space_operations ecryptfs_aops = {
775237fead6SMichael Halcrow 	.writepage = ecryptfs_writepage,
776237fead6SMichael Halcrow 	.readpage = ecryptfs_readpage,
777237fead6SMichael Halcrow 	.prepare_write = ecryptfs_prepare_write,
778237fead6SMichael Halcrow 	.commit_write = ecryptfs_commit_write,
779237fead6SMichael Halcrow 	.bmap = ecryptfs_bmap,
780237fead6SMichael Halcrow 	.sync_page = ecryptfs_sync_page,
781237fead6SMichael Halcrow };
782