xref: /openbmc/linux/fs/ecryptfs/mmap.c (revision 50f198ae16ac66508d4b8d5a40967a8507ad19ee)
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>
355a0e3ad6STejun Heo #include <linux/slab.h>
360a688ad7SHarvey Harrison #include <asm/unaligned.h>
37237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
38237fead6SMichael Halcrow 
39237fead6SMichael Halcrow /**
4016a72c45SMichael Halcrow  * ecryptfs_get_locked_page
41237fead6SMichael Halcrow  *
42237fead6SMichael Halcrow  * Get one page from cache or lower f/s, return error otherwise.
43237fead6SMichael Halcrow  *
4416a72c45SMichael Halcrow  * Returns locked and up-to-date page (if ok), with increased
45237fead6SMichael Halcrow  * refcnt.
46237fead6SMichael Halcrow  */
4702bd9799SAl Viro struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index)
48237fead6SMichael Halcrow {
4902bd9799SAl Viro 	struct page *page = read_mapping_page(inode->i_mapping, index, NULL);
5016a72c45SMichael Halcrow 	if (!IS_ERR(page))
5116a72c45SMichael Halcrow 		lock_page(page);
5216a72c45SMichael Halcrow 	return page;
53237fead6SMichael Halcrow }
54237fead6SMichael Halcrow 
55237fead6SMichael Halcrow /**
56237fead6SMichael Halcrow  * ecryptfs_writepage
57237fead6SMichael Halcrow  * @page: Page that is locked before this call is made
58237fead6SMichael Halcrow  *
59237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
60237fead6SMichael Halcrow  */
61237fead6SMichael Halcrow static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
62237fead6SMichael Halcrow {
63237fead6SMichael Halcrow 	int rc;
64237fead6SMichael Halcrow 
6557db4e8dSThieu Le 	/*
6657db4e8dSThieu Le 	 * Refuse to write the page out if we are called from reclaim context
6757db4e8dSThieu Le 	 * since our writepage() path may potentially allocate memory when
6857db4e8dSThieu Le 	 * calling into the lower fs vfs_write() which may in turn invoke
6957db4e8dSThieu Le 	 * us again.
7057db4e8dSThieu Le 	 */
7157db4e8dSThieu Le 	if (current->flags & PF_MEMALLOC) {
7257db4e8dSThieu Le 		redirty_page_for_writepage(wbc, page);
7357db4e8dSThieu Le 		rc = 0;
7457db4e8dSThieu Le 		goto out;
7557db4e8dSThieu Le 	}
7657db4e8dSThieu Le 
770216f7f7SMichael Halcrow 	rc = ecryptfs_encrypt_page(page);
78237fead6SMichael Halcrow 	if (rc) {
79237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error encrypting "
80888d57bbSJoe Perches 				"page (upper index [0x%.16lx])\n", page->index);
81237fead6SMichael Halcrow 		ClearPageUptodate(page);
82237fead6SMichael Halcrow 		goto out;
83237fead6SMichael Halcrow 	}
84237fead6SMichael Halcrow 	SetPageUptodate(page);
85237fead6SMichael Halcrow out:
8657db4e8dSThieu Le 	unlock_page(page);
87237fead6SMichael Halcrow 	return rc;
88237fead6SMichael Halcrow }
89237fead6SMichael Halcrow 
90f4e60e6bSTyler Hicks static void strip_xattr_flag(char *page_virt,
91f4e60e6bSTyler Hicks 			     struct ecryptfs_crypt_stat *crypt_stat)
92f4e60e6bSTyler Hicks {
93f4e60e6bSTyler Hicks 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
94f4e60e6bSTyler Hicks 		size_t written;
95f4e60e6bSTyler Hicks 
96f4e60e6bSTyler Hicks 		crypt_stat->flags &= ~ECRYPTFS_METADATA_IN_XATTR;
97f4e60e6bSTyler Hicks 		ecryptfs_write_crypt_stat_flags(page_virt, crypt_stat,
98f4e60e6bSTyler Hicks 						&written);
99f4e60e6bSTyler Hicks 		crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
100f4e60e6bSTyler Hicks 	}
101f4e60e6bSTyler Hicks }
102f4e60e6bSTyler Hicks 
103237fead6SMichael Halcrow /**
104e77a56ddSMichael Halcrow  *   Header Extent:
105e77a56ddSMichael Halcrow  *     Octets 0-7:        Unencrypted file size (big-endian)
106e77a56ddSMichael Halcrow  *     Octets 8-15:       eCryptfs special marker
107e77a56ddSMichael Halcrow  *     Octets 16-19:      Flags
108e77a56ddSMichael Halcrow  *      Octet 16:         File format version number (between 0 and 255)
109e77a56ddSMichael Halcrow  *      Octets 17-18:     Reserved
110e77a56ddSMichael Halcrow  *      Octet 19:         Bit 1 (lsb): Reserved
111e77a56ddSMichael Halcrow  *                        Bit 2: Encrypted?
112e77a56ddSMichael Halcrow  *                        Bits 3-8: Reserved
113e77a56ddSMichael Halcrow  *     Octets 20-23:      Header extent size (big-endian)
114e77a56ddSMichael Halcrow  *     Octets 24-25:      Number of header extents at front of file
115e77a56ddSMichael Halcrow  *                        (big-endian)
116e77a56ddSMichael Halcrow  *     Octet  26:         Begin RFC 2440 authentication token packet set
117e77a56ddSMichael Halcrow  */
118237fead6SMichael Halcrow 
119237fead6SMichael Halcrow /**
120bf12be1cSMichael Halcrow  * ecryptfs_copy_up_encrypted_with_header
121bf12be1cSMichael Halcrow  * @page: Sort of a ``virtual'' representation of the encrypted lower
122bf12be1cSMichael Halcrow  *        file. The actual lower file does not have the metadata in
123bf12be1cSMichael Halcrow  *        the header. This is locked.
124bf12be1cSMichael Halcrow  * @crypt_stat: The eCryptfs inode's cryptographic context
125237fead6SMichael Halcrow  *
126bf12be1cSMichael Halcrow  * The ``view'' is the version of the file that userspace winds up
127bf12be1cSMichael Halcrow  * seeing, with the header information inserted.
128237fead6SMichael Halcrow  */
129bf12be1cSMichael Halcrow static int
130bf12be1cSMichael Halcrow ecryptfs_copy_up_encrypted_with_header(struct page *page,
131bf12be1cSMichael Halcrow 				       struct ecryptfs_crypt_stat *crypt_stat)
132237fead6SMichael Halcrow {
133bf12be1cSMichael Halcrow 	loff_t extent_num_in_page = 0;
134bf12be1cSMichael Halcrow 	loff_t num_extents_per_page = (PAGE_CACHE_SIZE
135bf12be1cSMichael Halcrow 				       / crypt_stat->extent_size);
136237fead6SMichael Halcrow 	int rc = 0;
137237fead6SMichael Halcrow 
138bf12be1cSMichael Halcrow 	while (extent_num_in_page < num_extents_per_page) {
139d6a13c17SMichael Halcrow 		loff_t view_extent_num = ((((loff_t)page->index)
140d6a13c17SMichael Halcrow 					   * num_extents_per_page)
141bf12be1cSMichael Halcrow 					  + extent_num_in_page);
142cc11beffSMichael Halcrow 		size_t num_header_extents_at_front =
143fa3ef1cbSTyler Hicks 			(crypt_stat->metadata_size / crypt_stat->extent_size);
144e77a56ddSMichael Halcrow 
145cc11beffSMichael Halcrow 		if (view_extent_num < num_header_extents_at_front) {
146bf12be1cSMichael Halcrow 			/* This is a header extent */
147e77a56ddSMichael Halcrow 			char *page_virt;
148e77a56ddSMichael Halcrow 
1499d8b8ce5SMichael Halcrow 			page_virt = kmap_atomic(page, KM_USER0);
150e77a56ddSMichael Halcrow 			memset(page_virt, 0, PAGE_CACHE_SIZE);
151bf12be1cSMichael Halcrow 			/* TODO: Support more than one header extent */
152bf12be1cSMichael Halcrow 			if (view_extent_num == 0) {
153157f1071STyler Hicks 				size_t written;
154157f1071STyler Hicks 
155e77a56ddSMichael Halcrow 				rc = ecryptfs_read_xattr_region(
156d7cdc5feSMichael Halcrow 					page_virt, page->mapping->host);
157f4e60e6bSTyler Hicks 				strip_xattr_flag(page_virt + 16, crypt_stat);
158157f1071STyler Hicks 				ecryptfs_write_header_metadata(page_virt + 20,
159157f1071STyler Hicks 							       crypt_stat,
160157f1071STyler Hicks 							       &written);
161e77a56ddSMichael Halcrow 			}
1629d8b8ce5SMichael Halcrow 			kunmap_atomic(page_virt, KM_USER0);
1630a9ac382SMichael Halcrow 			flush_dcache_page(page);
164e77a56ddSMichael Halcrow 			if (rc) {
165bf12be1cSMichael Halcrow 				printk(KERN_ERR "%s: Error reading xattr "
16618d1dbf1SHarvey Harrison 				       "region; rc = [%d]\n", __func__, rc);
167e77a56ddSMichael Halcrow 				goto out;
168e77a56ddSMichael Halcrow 			}
169e77a56ddSMichael Halcrow 		} else {
170bf12be1cSMichael Halcrow 			/* This is an encrypted data extent */
171bf12be1cSMichael Halcrow 			loff_t lower_offset =
172cc11beffSMichael Halcrow 				((view_extent_num * crypt_stat->extent_size)
173fa3ef1cbSTyler Hicks 				 - crypt_stat->metadata_size);
174bf12be1cSMichael Halcrow 
175bf12be1cSMichael Halcrow 			rc = ecryptfs_read_lower_page_segment(
176bf12be1cSMichael Halcrow 				page, (lower_offset >> PAGE_CACHE_SHIFT),
177bf12be1cSMichael Halcrow 				(lower_offset & ~PAGE_CACHE_MASK),
178bf12be1cSMichael Halcrow 				crypt_stat->extent_size, page->mapping->host);
179e77a56ddSMichael Halcrow 			if (rc) {
180bf12be1cSMichael Halcrow 				printk(KERN_ERR "%s: Error attempting to read "
181bf12be1cSMichael Halcrow 				       "extent at offset [%lld] in the lower "
18218d1dbf1SHarvey Harrison 				       "file; rc = [%d]\n", __func__,
183bf12be1cSMichael Halcrow 				       lower_offset, rc);
184e77a56ddSMichael Halcrow 				goto out;
185e77a56ddSMichael Halcrow 			}
186e77a56ddSMichael Halcrow 		}
187bf12be1cSMichael Halcrow 		extent_num_in_page++;
188bf12be1cSMichael Halcrow 	}
189bf12be1cSMichael Halcrow out:
190bf12be1cSMichael Halcrow 	return rc;
191bf12be1cSMichael Halcrow }
192bf12be1cSMichael Halcrow 
193bf12be1cSMichael Halcrow /**
194bf12be1cSMichael Halcrow  * ecryptfs_readpage
195bf12be1cSMichael Halcrow  * @file: An eCryptfs file
196bf12be1cSMichael Halcrow  * @page: Page from eCryptfs inode mapping into which to stick the read data
197bf12be1cSMichael Halcrow  *
198bf12be1cSMichael Halcrow  * Read in a page, decrypting if necessary.
199bf12be1cSMichael Halcrow  *
200bf12be1cSMichael Halcrow  * Returns zero on success; non-zero on error.
201bf12be1cSMichael Halcrow  */
202bf12be1cSMichael Halcrow static int ecryptfs_readpage(struct file *file, struct page *page)
203bf12be1cSMichael Halcrow {
204bf12be1cSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
205bef5bc24SAl Viro 		&ecryptfs_inode_to_private(page->mapping->host)->crypt_stat;
206bf12be1cSMichael Halcrow 	int rc = 0;
207bf12be1cSMichael Halcrow 
208fed8859bSTyler Hicks 	if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
209bf12be1cSMichael Halcrow 		rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
210bf12be1cSMichael Halcrow 						      PAGE_CACHE_SIZE,
211bf12be1cSMichael Halcrow 						      page->mapping->host);
212bf12be1cSMichael Halcrow 	} else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
213bf12be1cSMichael Halcrow 		if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
214bf12be1cSMichael Halcrow 			rc = ecryptfs_copy_up_encrypted_with_header(page,
215bf12be1cSMichael Halcrow 								    crypt_stat);
216bf12be1cSMichael Halcrow 			if (rc) {
217bf12be1cSMichael Halcrow 				printk(KERN_ERR "%s: Error attempting to copy "
218bf12be1cSMichael Halcrow 				       "the encrypted content from the lower "
219bf12be1cSMichael Halcrow 				       "file whilst inserting the metadata "
220bf12be1cSMichael Halcrow 				       "from the xattr into the header; rc = "
22118d1dbf1SHarvey Harrison 				       "[%d]\n", __func__, rc);
222bf12be1cSMichael Halcrow 				goto out;
223bf12be1cSMichael Halcrow 			}
224bf12be1cSMichael Halcrow 
225e77a56ddSMichael Halcrow 		} else {
226bf12be1cSMichael Halcrow 			rc = ecryptfs_read_lower_page_segment(
227bf12be1cSMichael Halcrow 				page, page->index, 0, PAGE_CACHE_SIZE,
228bf12be1cSMichael Halcrow 				page->mapping->host);
229e77a56ddSMichael Halcrow 			if (rc) {
230e77a56ddSMichael Halcrow 				printk(KERN_ERR "Error reading page; rc = "
231e77a56ddSMichael Halcrow 				       "[%d]\n", rc);
232e77a56ddSMichael Halcrow 				goto out;
233e77a56ddSMichael Halcrow 			}
234e77a56ddSMichael Halcrow 		}
235237fead6SMichael Halcrow 	} else {
2360216f7f7SMichael Halcrow 		rc = ecryptfs_decrypt_page(page);
237237fead6SMichael Halcrow 		if (rc) {
238237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error decrypting page; "
239237fead6SMichael Halcrow 					"rc = [%d]\n", rc);
240237fead6SMichael Halcrow 			goto out;
241237fead6SMichael Halcrow 		}
242237fead6SMichael Halcrow 	}
243237fead6SMichael Halcrow out:
24416a72c45SMichael Halcrow 	if (rc)
24516a72c45SMichael Halcrow 		ClearPageUptodate(page);
24616a72c45SMichael Halcrow 	else
24716a72c45SMichael Halcrow 		SetPageUptodate(page);
248888d57bbSJoe Perches 	ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16lx]\n",
249237fead6SMichael Halcrow 			page->index);
250237fead6SMichael Halcrow 	unlock_page(page);
251237fead6SMichael Halcrow 	return rc;
252237fead6SMichael Halcrow }
253237fead6SMichael Halcrow 
254dd2a3b7aSMichael Halcrow /**
255dd2a3b7aSMichael Halcrow  * Called with lower inode mutex held.
256dd2a3b7aSMichael Halcrow  */
257237fead6SMichael Halcrow static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
258237fead6SMichael Halcrow {
259237fead6SMichael Halcrow 	struct inode *inode = page->mapping->host;
260237fead6SMichael Halcrow 	int end_byte_in_page;
261237fead6SMichael Halcrow 
2629d8b8ce5SMichael Halcrow 	if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
2639d8b8ce5SMichael Halcrow 		goto out;
264237fead6SMichael Halcrow 	end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
265237fead6SMichael Halcrow 	if (to > end_byte_in_page)
266237fead6SMichael Halcrow 		end_byte_in_page = to;
267eebd2aa3SChristoph Lameter 	zero_user_segment(page, end_byte_in_page, PAGE_CACHE_SIZE);
268237fead6SMichael Halcrow out:
2699d8b8ce5SMichael Halcrow 	return 0;
270237fead6SMichael Halcrow }
271237fead6SMichael Halcrow 
272e4465fdaSMichael Halcrow /**
273807b7ebeSBadari Pulavarty  * ecryptfs_write_begin
274e4465fdaSMichael Halcrow  * @file: The eCryptfs file
275807b7ebeSBadari Pulavarty  * @mapping: The eCryptfs object
276807b7ebeSBadari Pulavarty  * @pos: The file offset at which to start writing
277807b7ebeSBadari Pulavarty  * @len: Length of the write
278807b7ebeSBadari Pulavarty  * @flags: Various flags
279807b7ebeSBadari Pulavarty  * @pagep: Pointer to return the page
280807b7ebeSBadari Pulavarty  * @fsdata: Pointer to return fs data (unused)
281e4465fdaSMichael Halcrow  *
282e4465fdaSMichael Halcrow  * This function must zero any hole we create
283e4465fdaSMichael Halcrow  *
284e4465fdaSMichael Halcrow  * Returns zero on success; non-zero otherwise
285e4465fdaSMichael Halcrow  */
286807b7ebeSBadari Pulavarty static int ecryptfs_write_begin(struct file *file,
287807b7ebeSBadari Pulavarty 			struct address_space *mapping,
288807b7ebeSBadari Pulavarty 			loff_t pos, unsigned len, unsigned flags,
289807b7ebeSBadari Pulavarty 			struct page **pagep, void **fsdata)
29053a2731fSMichael Halcrow {
291807b7ebeSBadari Pulavarty 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
292807b7ebeSBadari Pulavarty 	struct page *page;
2937a3f595cSEric Sandeen 	loff_t prev_page_end_size;
294e4465fdaSMichael Halcrow 	int rc = 0;
29553a2731fSMichael Halcrow 
29654566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
297807b7ebeSBadari Pulavarty 	if (!page)
298807b7ebeSBadari Pulavarty 		return -ENOMEM;
299807b7ebeSBadari Pulavarty 	*pagep = page;
300807b7ebeSBadari Pulavarty 
30124562486SFrank Swiderski 	prev_page_end_size = ((loff_t)index << PAGE_CACHE_SHIFT);
30216a72c45SMichael Halcrow 	if (!PageUptodate(page)) {
303e4465fdaSMichael Halcrow 		struct ecryptfs_crypt_stat *crypt_stat =
304bef5bc24SAl Viro 			&ecryptfs_inode_to_private(mapping->host)->crypt_stat;
305e4465fdaSMichael Halcrow 
306fed8859bSTyler Hicks 		if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
307e4465fdaSMichael Halcrow 			rc = ecryptfs_read_lower_page_segment(
308807b7ebeSBadari Pulavarty 				page, index, 0, PAGE_CACHE_SIZE, mapping->host);
30916a72c45SMichael Halcrow 			if (rc) {
310e4465fdaSMichael Halcrow 				printk(KERN_ERR "%s: Error attemping to read "
311e4465fdaSMichael Halcrow 				       "lower page segment; rc = [%d]\n",
31218d1dbf1SHarvey Harrison 				       __func__, rc);
31316a72c45SMichael Halcrow 				ClearPageUptodate(page);
31416a72c45SMichael Halcrow 				goto out;
31516a72c45SMichael Halcrow 			} else
31616a72c45SMichael Halcrow 				SetPageUptodate(page);
317e4465fdaSMichael Halcrow 		} else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
318e4465fdaSMichael Halcrow 			if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
319e4465fdaSMichael Halcrow 				rc = ecryptfs_copy_up_encrypted_with_header(
320e4465fdaSMichael Halcrow 					page, crypt_stat);
321e4465fdaSMichael Halcrow 				if (rc) {
322e4465fdaSMichael Halcrow 					printk(KERN_ERR "%s: Error attempting "
323e4465fdaSMichael Halcrow 					       "to copy the encrypted content "
324e4465fdaSMichael Halcrow 					       "from the lower file whilst "
325e4465fdaSMichael Halcrow 					       "inserting the metadata from "
326e4465fdaSMichael Halcrow 					       "the xattr into the header; rc "
32718d1dbf1SHarvey Harrison 					       "= [%d]\n", __func__, rc);
328e4465fdaSMichael Halcrow 					ClearPageUptodate(page);
329e4465fdaSMichael Halcrow 					goto out;
33016a72c45SMichael Halcrow 				}
331e4465fdaSMichael Halcrow 				SetPageUptodate(page);
332e4465fdaSMichael Halcrow 			} else {
333e4465fdaSMichael Halcrow 				rc = ecryptfs_read_lower_page_segment(
334807b7ebeSBadari Pulavarty 					page, index, 0, PAGE_CACHE_SIZE,
335807b7ebeSBadari Pulavarty 					mapping->host);
336e4465fdaSMichael Halcrow 				if (rc) {
337e4465fdaSMichael Halcrow 					printk(KERN_ERR "%s: Error reading "
338e4465fdaSMichael Halcrow 					       "page; rc = [%d]\n",
33918d1dbf1SHarvey Harrison 					       __func__, rc);
340e4465fdaSMichael Halcrow 					ClearPageUptodate(page);
341e4465fdaSMichael Halcrow 					goto out;
342e4465fdaSMichael Halcrow 				}
343e4465fdaSMichael Halcrow 				SetPageUptodate(page);
344e4465fdaSMichael Halcrow 			}
345e4465fdaSMichael Halcrow 		} else {
34624562486SFrank Swiderski 			if (prev_page_end_size
34724562486SFrank Swiderski 			    >= i_size_read(page->mapping->host)) {
34824562486SFrank Swiderski 				zero_user(page, 0, PAGE_CACHE_SIZE);
34924562486SFrank Swiderski 			} else {
350e4465fdaSMichael Halcrow 				rc = ecryptfs_decrypt_page(page);
351e4465fdaSMichael Halcrow 				if (rc) {
35224562486SFrank Swiderski 					printk(KERN_ERR "%s: Error decrypting "
35324562486SFrank Swiderski 					       "page at index [%ld]; "
35424562486SFrank Swiderski 					       "rc = [%d]\n",
35518d1dbf1SHarvey Harrison 					       __func__, page->index, rc);
356e4465fdaSMichael Halcrow 					ClearPageUptodate(page);
357e4465fdaSMichael Halcrow 					goto out;
358e4465fdaSMichael Halcrow 				}
35924562486SFrank Swiderski 			}
360e4465fdaSMichael Halcrow 			SetPageUptodate(page);
361e4465fdaSMichael Halcrow 		}
362e4465fdaSMichael Halcrow 	}
363e4465fdaSMichael Halcrow 	/* If creating a page or more of holes, zero them out via truncate.
364e4465fdaSMichael Halcrow 	 * Note, this will increase i_size. */
365807b7ebeSBadari Pulavarty 	if (index != 0) {
3667a3f595cSEric Sandeen 		if (prev_page_end_size > i_size_read(page->mapping->host)) {
367240e2df5SMichael Halcrow 			rc = ecryptfs_truncate(file->f_path.dentry,
3687a3f595cSEric Sandeen 					       prev_page_end_size);
36953a2731fSMichael Halcrow 			if (rc) {
370e4465fdaSMichael Halcrow 				printk(KERN_ERR "%s: Error on attempt to "
37153a2731fSMichael Halcrow 				       "truncate to (higher) offset [%lld];"
37218d1dbf1SHarvey Harrison 				       " rc = [%d]\n", __func__,
373e4465fdaSMichael Halcrow 				       prev_page_end_size, rc);
37453a2731fSMichael Halcrow 				goto out;
37553a2731fSMichael Halcrow 			}
37653a2731fSMichael Halcrow 		}
3777a3f595cSEric Sandeen 	}
378e4465fdaSMichael Halcrow 	/* Writing to a new page, and creating a small hole from start
379e4465fdaSMichael Halcrow 	 * of page?  Zero it out. */
380807b7ebeSBadari Pulavarty 	if ((i_size_read(mapping->host) == prev_page_end_size)
381807b7ebeSBadari Pulavarty 	    && (pos != 0))
382eebd2aa3SChristoph Lameter 		zero_user(page, 0, PAGE_CACHE_SIZE);
38353a2731fSMichael Halcrow out:
384*50f198aeSTyler Hicks 	if (unlikely(rc)) {
385*50f198aeSTyler Hicks 		unlock_page(page);
386*50f198aeSTyler Hicks 		page_cache_release(page);
387*50f198aeSTyler Hicks 		*pagep = NULL;
388*50f198aeSTyler Hicks 	}
38953a2731fSMichael Halcrow 	return rc;
39053a2731fSMichael Halcrow }
39153a2731fSMichael Halcrow 
392237fead6SMichael Halcrow /**
393237fead6SMichael Halcrow  * ecryptfs_write_inode_size_to_header
394237fead6SMichael Halcrow  *
395237fead6SMichael Halcrow  * Writes the lower file size to the first 8 bytes of the header.
396237fead6SMichael Halcrow  *
397237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
398237fead6SMichael Halcrow  */
3990216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
400237fead6SMichael Halcrow {
4010216f7f7SMichael Halcrow 	char *file_size_virt;
4020216f7f7SMichael Halcrow 	int rc;
403237fead6SMichael Halcrow 
4040216f7f7SMichael Halcrow 	file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
4050216f7f7SMichael Halcrow 	if (!file_size_virt) {
4060216f7f7SMichael Halcrow 		rc = -ENOMEM;
407237fead6SMichael Halcrow 		goto out;
408237fead6SMichael Halcrow 	}
4090a688ad7SHarvey Harrison 	put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
4100216f7f7SMichael Halcrow 	rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
4110216f7f7SMichael Halcrow 				  sizeof(u64));
4120216f7f7SMichael Halcrow 	kfree(file_size_virt);
41396a7b9c2STyler Hicks 	if (rc < 0)
4140216f7f7SMichael Halcrow 		printk(KERN_ERR "%s: Error writing file size to header; "
41518d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, rc);
41696a7b9c2STyler Hicks 	else
41796a7b9c2STyler Hicks 		rc = 0;
418237fead6SMichael Halcrow out:
419237fead6SMichael Halcrow 	return rc;
420237fead6SMichael Halcrow }
421237fead6SMichael Halcrow 
4220216f7f7SMichael Halcrow struct kmem_cache *ecryptfs_xattr_cache;
4230216f7f7SMichael Halcrow 
4240216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
425dd2a3b7aSMichael Halcrow {
426dd2a3b7aSMichael Halcrow 	ssize_t size;
427dd2a3b7aSMichael Halcrow 	void *xattr_virt;
4280216f7f7SMichael Halcrow 	struct dentry *lower_dentry =
4290216f7f7SMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
4300216f7f7SMichael Halcrow 	struct inode *lower_inode = lower_dentry->d_inode;
431dd2a3b7aSMichael Halcrow 	int rc;
432dd2a3b7aSMichael Halcrow 
4330216f7f7SMichael Halcrow 	if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
4340216f7f7SMichael Halcrow 		printk(KERN_WARNING
4350216f7f7SMichael Halcrow 		       "No support for setting xattr in lower filesystem\n");
4360216f7f7SMichael Halcrow 		rc = -ENOSYS;
4370216f7f7SMichael Halcrow 		goto out;
4380216f7f7SMichael Halcrow 	}
439dd2a3b7aSMichael Halcrow 	xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
440dd2a3b7aSMichael Halcrow 	if (!xattr_virt) {
441dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Out of memory whilst attempting to write "
442dd2a3b7aSMichael Halcrow 		       "inode size to xattr\n");
443dd2a3b7aSMichael Halcrow 		rc = -ENOMEM;
444dd2a3b7aSMichael Halcrow 		goto out;
445dd2a3b7aSMichael Halcrow 	}
4460216f7f7SMichael Halcrow 	mutex_lock(&lower_inode->i_mutex);
4470216f7f7SMichael Halcrow 	size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
4480216f7f7SMichael Halcrow 					   xattr_virt, PAGE_CACHE_SIZE);
449dd2a3b7aSMichael Halcrow 	if (size < 0)
450dd2a3b7aSMichael Halcrow 		size = 8;
4510a688ad7SHarvey Harrison 	put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
4520216f7f7SMichael Halcrow 	rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
453dd2a3b7aSMichael Halcrow 					 xattr_virt, size, 0);
4540216f7f7SMichael Halcrow 	mutex_unlock(&lower_inode->i_mutex);
455dd2a3b7aSMichael Halcrow 	if (rc)
456dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error whilst attempting to write inode size "
457dd2a3b7aSMichael Halcrow 		       "to lower file xattr; rc = [%d]\n", rc);
458dd2a3b7aSMichael Halcrow 	kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
459dd2a3b7aSMichael Halcrow out:
460dd2a3b7aSMichael Halcrow 	return rc;
461dd2a3b7aSMichael Halcrow }
462dd2a3b7aSMichael Halcrow 
4630216f7f7SMichael Halcrow int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
464dd2a3b7aSMichael Halcrow {
465dd2a3b7aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
466dd2a3b7aSMichael Halcrow 
4670216f7f7SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
46813a791b4STyler Hicks 	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
469dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
4700216f7f7SMichael Halcrow 		return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
471dd2a3b7aSMichael Halcrow 	else
4720216f7f7SMichael Halcrow 		return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
473dd2a3b7aSMichael Halcrow }
474dd2a3b7aSMichael Halcrow 
475237fead6SMichael Halcrow /**
476807b7ebeSBadari Pulavarty  * ecryptfs_write_end
477237fead6SMichael Halcrow  * @file: The eCryptfs file object
478807b7ebeSBadari Pulavarty  * @mapping: The eCryptfs object
479807b7ebeSBadari Pulavarty  * @pos: The file position
480807b7ebeSBadari Pulavarty  * @len: The length of the data (unused)
481807b7ebeSBadari Pulavarty  * @copied: The amount of data copied
482237fead6SMichael Halcrow  * @page: The eCryptfs page
483807b7ebeSBadari Pulavarty  * @fsdata: The fsdata (unused)
484237fead6SMichael Halcrow  *
485237fead6SMichael Halcrow  * This is where we encrypt the data and pass the encrypted data to
486237fead6SMichael Halcrow  * the lower filesystem.  In OpenPGP-compatible mode, we operate on
487237fead6SMichael Halcrow  * entire underlying packets.
488237fead6SMichael Halcrow  */
489807b7ebeSBadari Pulavarty static int ecryptfs_write_end(struct file *file,
490807b7ebeSBadari Pulavarty 			struct address_space *mapping,
491807b7ebeSBadari Pulavarty 			loff_t pos, unsigned len, unsigned copied,
492807b7ebeSBadari Pulavarty 			struct page *page, void *fsdata)
493237fead6SMichael Halcrow {
494807b7ebeSBadari Pulavarty 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
495807b7ebeSBadari Pulavarty 	unsigned from = pos & (PAGE_CACHE_SIZE - 1);
496807b7ebeSBadari Pulavarty 	unsigned to = from + copied;
497807b7ebeSBadari Pulavarty 	struct inode *ecryptfs_inode = mapping->host;
498bf12be1cSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
499bef5bc24SAl Viro 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
500237fead6SMichael Halcrow 	int rc;
50157db4e8dSThieu Le 	int need_unlock_page = 1;
502237fead6SMichael Halcrow 
503237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
504888d57bbSJoe Perches 			"(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
50513a791b4STyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
50613a791b4STyler Hicks 		rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0,
50713a791b4STyler Hicks 						       to);
50813a791b4STyler Hicks 		if (!rc) {
50913a791b4STyler Hicks 			rc = copied;
51013a791b4STyler Hicks 			fsstack_copy_inode_size(ecryptfs_inode,
51113a791b4STyler Hicks 				ecryptfs_inode_to_lower(ecryptfs_inode));
51213a791b4STyler Hicks 		}
51313a791b4STyler Hicks 		goto out;
51413a791b4STyler Hicks 	}
515bf12be1cSMichael Halcrow 	/* Fills in zeros if 'to' goes beyond inode size */
516237fead6SMichael Halcrow 	rc = fill_zeros_to_end_of_page(page, to);
517237fead6SMichael Halcrow 	if (rc) {
518237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
519888d57bbSJoe Perches 			"zeros in page with index = [0x%.16lx]\n", index);
520237fead6SMichael Halcrow 		goto out;
521237fead6SMichael Halcrow 	}
52257db4e8dSThieu Le 	set_page_dirty(page);
52357db4e8dSThieu Le 	unlock_page(page);
52457db4e8dSThieu Le 	need_unlock_page = 0;
525807b7ebeSBadari Pulavarty 	if (pos + copied > i_size_read(ecryptfs_inode)) {
526807b7ebeSBadari Pulavarty 		i_size_write(ecryptfs_inode, pos + copied);
527237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
528888d57bbSJoe Perches 			"[0x%.16llx]\n",
529888d57bbSJoe Perches 			(unsigned long long)i_size_read(ecryptfs_inode));
53057db4e8dSThieu Le 		balance_dirty_pages_ratelimited(mapping);
531bf12be1cSMichael Halcrow 		rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
53257db4e8dSThieu Le 		if (rc) {
533dd2a3b7aSMichael Halcrow 			printk(KERN_ERR "Error writing inode size to metadata; "
534dd2a3b7aSMichael Halcrow 			       "rc = [%d]\n", rc);
53557db4e8dSThieu Le 			goto out;
53657db4e8dSThieu Le 		}
53757db4e8dSThieu Le 	}
538807b7ebeSBadari Pulavarty 	rc = copied;
539237fead6SMichael Halcrow out:
54057db4e8dSThieu Le 	if (need_unlock_page)
541807b7ebeSBadari Pulavarty 		unlock_page(page);
542807b7ebeSBadari Pulavarty 	page_cache_release(page);
543237fead6SMichael Halcrow 	return rc;
544237fead6SMichael Halcrow }
545237fead6SMichael Halcrow 
546237fead6SMichael Halcrow static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
547237fead6SMichael Halcrow {
548237fead6SMichael Halcrow 	int rc = 0;
549237fead6SMichael Halcrow 	struct inode *inode;
550237fead6SMichael Halcrow 	struct inode *lower_inode;
551237fead6SMichael Halcrow 
552237fead6SMichael Halcrow 	inode = (struct inode *)mapping->host;
553237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
554237fead6SMichael Halcrow 	if (lower_inode->i_mapping->a_ops->bmap)
555237fead6SMichael Halcrow 		rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
556237fead6SMichael Halcrow 							 block);
557237fead6SMichael Halcrow 	return rc;
558237fead6SMichael Halcrow }
559237fead6SMichael Halcrow 
5607f09410bSAlexey Dobriyan const struct address_space_operations ecryptfs_aops = {
561237fead6SMichael Halcrow 	.writepage = ecryptfs_writepage,
562237fead6SMichael Halcrow 	.readpage = ecryptfs_readpage,
563807b7ebeSBadari Pulavarty 	.write_begin = ecryptfs_write_begin,
564807b7ebeSBadari Pulavarty 	.write_end = ecryptfs_write_end,
565237fead6SMichael Halcrow 	.bmap = ecryptfs_bmap,
566237fead6SMichael Halcrow };
567