xref: /openbmc/linux/fs/ecryptfs/mmap.c (revision fed8859b3ab94274c986cbdf7d27130e0545f02c)
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 
650216f7f7SMichael Halcrow 	rc = ecryptfs_encrypt_page(page);
66237fead6SMichael Halcrow 	if (rc) {
67237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error encrypting "
68888d57bbSJoe Perches 				"page (upper index [0x%.16lx])\n", page->index);
69237fead6SMichael Halcrow 		ClearPageUptodate(page);
70237fead6SMichael Halcrow 		goto out;
71237fead6SMichael Halcrow 	}
72237fead6SMichael Halcrow 	SetPageUptodate(page);
73237fead6SMichael Halcrow 	unlock_page(page);
74237fead6SMichael Halcrow out:
75237fead6SMichael Halcrow 	return rc;
76237fead6SMichael Halcrow }
77237fead6SMichael Halcrow 
78f4e60e6bSTyler Hicks static void strip_xattr_flag(char *page_virt,
79f4e60e6bSTyler Hicks 			     struct ecryptfs_crypt_stat *crypt_stat)
80f4e60e6bSTyler Hicks {
81f4e60e6bSTyler Hicks 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
82f4e60e6bSTyler Hicks 		size_t written;
83f4e60e6bSTyler Hicks 
84f4e60e6bSTyler Hicks 		crypt_stat->flags &= ~ECRYPTFS_METADATA_IN_XATTR;
85f4e60e6bSTyler Hicks 		ecryptfs_write_crypt_stat_flags(page_virt, crypt_stat,
86f4e60e6bSTyler Hicks 						&written);
87f4e60e6bSTyler Hicks 		crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
88f4e60e6bSTyler Hicks 	}
89f4e60e6bSTyler Hicks }
90f4e60e6bSTyler Hicks 
91237fead6SMichael Halcrow /**
92e77a56ddSMichael Halcrow  *   Header Extent:
93e77a56ddSMichael Halcrow  *     Octets 0-7:        Unencrypted file size (big-endian)
94e77a56ddSMichael Halcrow  *     Octets 8-15:       eCryptfs special marker
95e77a56ddSMichael Halcrow  *     Octets 16-19:      Flags
96e77a56ddSMichael Halcrow  *      Octet 16:         File format version number (between 0 and 255)
97e77a56ddSMichael Halcrow  *      Octets 17-18:     Reserved
98e77a56ddSMichael Halcrow  *      Octet 19:         Bit 1 (lsb): Reserved
99e77a56ddSMichael Halcrow  *                        Bit 2: Encrypted?
100e77a56ddSMichael Halcrow  *                        Bits 3-8: Reserved
101e77a56ddSMichael Halcrow  *     Octets 20-23:      Header extent size (big-endian)
102e77a56ddSMichael Halcrow  *     Octets 24-25:      Number of header extents at front of file
103e77a56ddSMichael Halcrow  *                        (big-endian)
104e77a56ddSMichael Halcrow  *     Octet  26:         Begin RFC 2440 authentication token packet set
105e77a56ddSMichael Halcrow  */
106237fead6SMichael Halcrow 
107237fead6SMichael Halcrow /**
108bf12be1cSMichael Halcrow  * ecryptfs_copy_up_encrypted_with_header
109bf12be1cSMichael Halcrow  * @page: Sort of a ``virtual'' representation of the encrypted lower
110bf12be1cSMichael Halcrow  *        file. The actual lower file does not have the metadata in
111bf12be1cSMichael Halcrow  *        the header. This is locked.
112bf12be1cSMichael Halcrow  * @crypt_stat: The eCryptfs inode's cryptographic context
113237fead6SMichael Halcrow  *
114bf12be1cSMichael Halcrow  * The ``view'' is the version of the file that userspace winds up
115bf12be1cSMichael Halcrow  * seeing, with the header information inserted.
116237fead6SMichael Halcrow  */
117bf12be1cSMichael Halcrow static int
118bf12be1cSMichael Halcrow ecryptfs_copy_up_encrypted_with_header(struct page *page,
119bf12be1cSMichael Halcrow 				       struct ecryptfs_crypt_stat *crypt_stat)
120237fead6SMichael Halcrow {
121bf12be1cSMichael Halcrow 	loff_t extent_num_in_page = 0;
122bf12be1cSMichael Halcrow 	loff_t num_extents_per_page = (PAGE_CACHE_SIZE
123bf12be1cSMichael Halcrow 				       / crypt_stat->extent_size);
124237fead6SMichael Halcrow 	int rc = 0;
125237fead6SMichael Halcrow 
126bf12be1cSMichael Halcrow 	while (extent_num_in_page < num_extents_per_page) {
127d6a13c17SMichael Halcrow 		loff_t view_extent_num = ((((loff_t)page->index)
128d6a13c17SMichael Halcrow 					   * num_extents_per_page)
129bf12be1cSMichael Halcrow 					  + extent_num_in_page);
130cc11beffSMichael Halcrow 		size_t num_header_extents_at_front =
131fa3ef1cbSTyler Hicks 			(crypt_stat->metadata_size / crypt_stat->extent_size);
132e77a56ddSMichael Halcrow 
133cc11beffSMichael Halcrow 		if (view_extent_num < num_header_extents_at_front) {
134bf12be1cSMichael Halcrow 			/* This is a header extent */
135e77a56ddSMichael Halcrow 			char *page_virt;
136e77a56ddSMichael Halcrow 
1379d8b8ce5SMichael Halcrow 			page_virt = kmap_atomic(page, KM_USER0);
138e77a56ddSMichael Halcrow 			memset(page_virt, 0, PAGE_CACHE_SIZE);
139bf12be1cSMichael Halcrow 			/* TODO: Support more than one header extent */
140bf12be1cSMichael Halcrow 			if (view_extent_num == 0) {
141157f1071STyler Hicks 				size_t written;
142157f1071STyler Hicks 
143e77a56ddSMichael Halcrow 				rc = ecryptfs_read_xattr_region(
144d7cdc5feSMichael Halcrow 					page_virt, page->mapping->host);
145f4e60e6bSTyler Hicks 				strip_xattr_flag(page_virt + 16, crypt_stat);
146157f1071STyler Hicks 				ecryptfs_write_header_metadata(page_virt + 20,
147157f1071STyler Hicks 							       crypt_stat,
148157f1071STyler Hicks 							       &written);
149e77a56ddSMichael Halcrow 			}
1509d8b8ce5SMichael Halcrow 			kunmap_atomic(page_virt, KM_USER0);
1510a9ac382SMichael Halcrow 			flush_dcache_page(page);
152e77a56ddSMichael Halcrow 			if (rc) {
153bf12be1cSMichael Halcrow 				printk(KERN_ERR "%s: Error reading xattr "
15418d1dbf1SHarvey Harrison 				       "region; rc = [%d]\n", __func__, rc);
155e77a56ddSMichael Halcrow 				goto out;
156e77a56ddSMichael Halcrow 			}
157e77a56ddSMichael Halcrow 		} else {
158bf12be1cSMichael Halcrow 			/* This is an encrypted data extent */
159bf12be1cSMichael Halcrow 			loff_t lower_offset =
160cc11beffSMichael Halcrow 				((view_extent_num * crypt_stat->extent_size)
161fa3ef1cbSTyler Hicks 				 - crypt_stat->metadata_size);
162bf12be1cSMichael Halcrow 
163bf12be1cSMichael Halcrow 			rc = ecryptfs_read_lower_page_segment(
164bf12be1cSMichael Halcrow 				page, (lower_offset >> PAGE_CACHE_SHIFT),
165bf12be1cSMichael Halcrow 				(lower_offset & ~PAGE_CACHE_MASK),
166bf12be1cSMichael Halcrow 				crypt_stat->extent_size, page->mapping->host);
167e77a56ddSMichael Halcrow 			if (rc) {
168bf12be1cSMichael Halcrow 				printk(KERN_ERR "%s: Error attempting to read "
169bf12be1cSMichael Halcrow 				       "extent at offset [%lld] in the lower "
17018d1dbf1SHarvey Harrison 				       "file; rc = [%d]\n", __func__,
171bf12be1cSMichael Halcrow 				       lower_offset, rc);
172e77a56ddSMichael Halcrow 				goto out;
173e77a56ddSMichael Halcrow 			}
174e77a56ddSMichael Halcrow 		}
175bf12be1cSMichael Halcrow 		extent_num_in_page++;
176bf12be1cSMichael Halcrow 	}
177bf12be1cSMichael Halcrow out:
178bf12be1cSMichael Halcrow 	return rc;
179bf12be1cSMichael Halcrow }
180bf12be1cSMichael Halcrow 
181bf12be1cSMichael Halcrow /**
182bf12be1cSMichael Halcrow  * ecryptfs_readpage
183bf12be1cSMichael Halcrow  * @file: An eCryptfs file
184bf12be1cSMichael Halcrow  * @page: Page from eCryptfs inode mapping into which to stick the read data
185bf12be1cSMichael Halcrow  *
186bf12be1cSMichael Halcrow  * Read in a page, decrypting if necessary.
187bf12be1cSMichael Halcrow  *
188bf12be1cSMichael Halcrow  * Returns zero on success; non-zero on error.
189bf12be1cSMichael Halcrow  */
190bf12be1cSMichael Halcrow static int ecryptfs_readpage(struct file *file, struct page *page)
191bf12be1cSMichael Halcrow {
192bf12be1cSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
193bef5bc24SAl Viro 		&ecryptfs_inode_to_private(page->mapping->host)->crypt_stat;
194bf12be1cSMichael Halcrow 	int rc = 0;
195bf12be1cSMichael Halcrow 
196*fed8859bSTyler Hicks 	if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
197bf12be1cSMichael Halcrow 		rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
198bf12be1cSMichael Halcrow 						      PAGE_CACHE_SIZE,
199bf12be1cSMichael Halcrow 						      page->mapping->host);
200bf12be1cSMichael Halcrow 	} else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
201bf12be1cSMichael Halcrow 		if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
202bf12be1cSMichael Halcrow 			rc = ecryptfs_copy_up_encrypted_with_header(page,
203bf12be1cSMichael Halcrow 								    crypt_stat);
204bf12be1cSMichael Halcrow 			if (rc) {
205bf12be1cSMichael Halcrow 				printk(KERN_ERR "%s: Error attempting to copy "
206bf12be1cSMichael Halcrow 				       "the encrypted content from the lower "
207bf12be1cSMichael Halcrow 				       "file whilst inserting the metadata "
208bf12be1cSMichael Halcrow 				       "from the xattr into the header; rc = "
20918d1dbf1SHarvey Harrison 				       "[%d]\n", __func__, rc);
210bf12be1cSMichael Halcrow 				goto out;
211bf12be1cSMichael Halcrow 			}
212bf12be1cSMichael Halcrow 
213e77a56ddSMichael Halcrow 		} else {
214bf12be1cSMichael Halcrow 			rc = ecryptfs_read_lower_page_segment(
215bf12be1cSMichael Halcrow 				page, page->index, 0, PAGE_CACHE_SIZE,
216bf12be1cSMichael Halcrow 				page->mapping->host);
217e77a56ddSMichael Halcrow 			if (rc) {
218e77a56ddSMichael Halcrow 				printk(KERN_ERR "Error reading page; rc = "
219e77a56ddSMichael Halcrow 				       "[%d]\n", rc);
220e77a56ddSMichael Halcrow 				goto out;
221e77a56ddSMichael Halcrow 			}
222e77a56ddSMichael Halcrow 		}
223237fead6SMichael Halcrow 	} else {
2240216f7f7SMichael Halcrow 		rc = ecryptfs_decrypt_page(page);
225237fead6SMichael Halcrow 		if (rc) {
226237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error decrypting page; "
227237fead6SMichael Halcrow 					"rc = [%d]\n", rc);
228237fead6SMichael Halcrow 			goto out;
229237fead6SMichael Halcrow 		}
230237fead6SMichael Halcrow 	}
231237fead6SMichael Halcrow out:
23216a72c45SMichael Halcrow 	if (rc)
23316a72c45SMichael Halcrow 		ClearPageUptodate(page);
23416a72c45SMichael Halcrow 	else
23516a72c45SMichael Halcrow 		SetPageUptodate(page);
236888d57bbSJoe Perches 	ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16lx]\n",
237237fead6SMichael Halcrow 			page->index);
238237fead6SMichael Halcrow 	unlock_page(page);
239237fead6SMichael Halcrow 	return rc;
240237fead6SMichael Halcrow }
241237fead6SMichael Halcrow 
242dd2a3b7aSMichael Halcrow /**
243dd2a3b7aSMichael Halcrow  * Called with lower inode mutex held.
244dd2a3b7aSMichael Halcrow  */
245237fead6SMichael Halcrow static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
246237fead6SMichael Halcrow {
247237fead6SMichael Halcrow 	struct inode *inode = page->mapping->host;
248237fead6SMichael Halcrow 	int end_byte_in_page;
249237fead6SMichael Halcrow 
2509d8b8ce5SMichael Halcrow 	if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
2519d8b8ce5SMichael Halcrow 		goto out;
252237fead6SMichael Halcrow 	end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
253237fead6SMichael Halcrow 	if (to > end_byte_in_page)
254237fead6SMichael Halcrow 		end_byte_in_page = to;
255eebd2aa3SChristoph Lameter 	zero_user_segment(page, end_byte_in_page, PAGE_CACHE_SIZE);
256237fead6SMichael Halcrow out:
2579d8b8ce5SMichael Halcrow 	return 0;
258237fead6SMichael Halcrow }
259237fead6SMichael Halcrow 
260e4465fdaSMichael Halcrow /**
261807b7ebeSBadari Pulavarty  * ecryptfs_write_begin
262e4465fdaSMichael Halcrow  * @file: The eCryptfs file
263807b7ebeSBadari Pulavarty  * @mapping: The eCryptfs object
264807b7ebeSBadari Pulavarty  * @pos: The file offset at which to start writing
265807b7ebeSBadari Pulavarty  * @len: Length of the write
266807b7ebeSBadari Pulavarty  * @flags: Various flags
267807b7ebeSBadari Pulavarty  * @pagep: Pointer to return the page
268807b7ebeSBadari Pulavarty  * @fsdata: Pointer to return fs data (unused)
269e4465fdaSMichael Halcrow  *
270e4465fdaSMichael Halcrow  * This function must zero any hole we create
271e4465fdaSMichael Halcrow  *
272e4465fdaSMichael Halcrow  * Returns zero on success; non-zero otherwise
273e4465fdaSMichael Halcrow  */
274807b7ebeSBadari Pulavarty static int ecryptfs_write_begin(struct file *file,
275807b7ebeSBadari Pulavarty 			struct address_space *mapping,
276807b7ebeSBadari Pulavarty 			loff_t pos, unsigned len, unsigned flags,
277807b7ebeSBadari Pulavarty 			struct page **pagep, void **fsdata)
27853a2731fSMichael Halcrow {
279807b7ebeSBadari Pulavarty 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
280807b7ebeSBadari Pulavarty 	struct page *page;
2817a3f595cSEric Sandeen 	loff_t prev_page_end_size;
282e4465fdaSMichael Halcrow 	int rc = 0;
28353a2731fSMichael Halcrow 
28454566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
285807b7ebeSBadari Pulavarty 	if (!page)
286807b7ebeSBadari Pulavarty 		return -ENOMEM;
287807b7ebeSBadari Pulavarty 	*pagep = page;
288807b7ebeSBadari Pulavarty 
28924562486SFrank Swiderski 	prev_page_end_size = ((loff_t)index << PAGE_CACHE_SHIFT);
29016a72c45SMichael Halcrow 	if (!PageUptodate(page)) {
291e4465fdaSMichael Halcrow 		struct ecryptfs_crypt_stat *crypt_stat =
292bef5bc24SAl Viro 			&ecryptfs_inode_to_private(mapping->host)->crypt_stat;
293e4465fdaSMichael Halcrow 
294*fed8859bSTyler Hicks 		if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
295e4465fdaSMichael Halcrow 			rc = ecryptfs_read_lower_page_segment(
296807b7ebeSBadari Pulavarty 				page, index, 0, PAGE_CACHE_SIZE, mapping->host);
29716a72c45SMichael Halcrow 			if (rc) {
298e4465fdaSMichael Halcrow 				printk(KERN_ERR "%s: Error attemping to read "
299e4465fdaSMichael Halcrow 				       "lower page segment; rc = [%d]\n",
30018d1dbf1SHarvey Harrison 				       __func__, rc);
30116a72c45SMichael Halcrow 				ClearPageUptodate(page);
30216a72c45SMichael Halcrow 				goto out;
30316a72c45SMichael Halcrow 			} else
30416a72c45SMichael Halcrow 				SetPageUptodate(page);
305e4465fdaSMichael Halcrow 		} else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
306e4465fdaSMichael Halcrow 			if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
307e4465fdaSMichael Halcrow 				rc = ecryptfs_copy_up_encrypted_with_header(
308e4465fdaSMichael Halcrow 					page, crypt_stat);
309e4465fdaSMichael Halcrow 				if (rc) {
310e4465fdaSMichael Halcrow 					printk(KERN_ERR "%s: Error attempting "
311e4465fdaSMichael Halcrow 					       "to copy the encrypted content "
312e4465fdaSMichael Halcrow 					       "from the lower file whilst "
313e4465fdaSMichael Halcrow 					       "inserting the metadata from "
314e4465fdaSMichael Halcrow 					       "the xattr into the header; rc "
31518d1dbf1SHarvey Harrison 					       "= [%d]\n", __func__, rc);
316e4465fdaSMichael Halcrow 					ClearPageUptodate(page);
317e4465fdaSMichael Halcrow 					goto out;
31816a72c45SMichael Halcrow 				}
319e4465fdaSMichael Halcrow 				SetPageUptodate(page);
320e4465fdaSMichael Halcrow 			} else {
321e4465fdaSMichael Halcrow 				rc = ecryptfs_read_lower_page_segment(
322807b7ebeSBadari Pulavarty 					page, index, 0, PAGE_CACHE_SIZE,
323807b7ebeSBadari Pulavarty 					mapping->host);
324e4465fdaSMichael Halcrow 				if (rc) {
325e4465fdaSMichael Halcrow 					printk(KERN_ERR "%s: Error reading "
326e4465fdaSMichael Halcrow 					       "page; rc = [%d]\n",
32718d1dbf1SHarvey Harrison 					       __func__, rc);
328e4465fdaSMichael Halcrow 					ClearPageUptodate(page);
329e4465fdaSMichael Halcrow 					goto out;
330e4465fdaSMichael Halcrow 				}
331e4465fdaSMichael Halcrow 				SetPageUptodate(page);
332e4465fdaSMichael Halcrow 			}
333e4465fdaSMichael Halcrow 		} else {
33424562486SFrank Swiderski 			if (prev_page_end_size
33524562486SFrank Swiderski 			    >= i_size_read(page->mapping->host)) {
33624562486SFrank Swiderski 				zero_user(page, 0, PAGE_CACHE_SIZE);
33724562486SFrank Swiderski 			} else {
338e4465fdaSMichael Halcrow 				rc = ecryptfs_decrypt_page(page);
339e4465fdaSMichael Halcrow 				if (rc) {
34024562486SFrank Swiderski 					printk(KERN_ERR "%s: Error decrypting "
34124562486SFrank Swiderski 					       "page at index [%ld]; "
34224562486SFrank Swiderski 					       "rc = [%d]\n",
34318d1dbf1SHarvey Harrison 					       __func__, page->index, rc);
344e4465fdaSMichael Halcrow 					ClearPageUptodate(page);
345e4465fdaSMichael Halcrow 					goto out;
346e4465fdaSMichael Halcrow 				}
34724562486SFrank Swiderski 			}
348e4465fdaSMichael Halcrow 			SetPageUptodate(page);
349e4465fdaSMichael Halcrow 		}
350e4465fdaSMichael Halcrow 	}
351e4465fdaSMichael Halcrow 	/* If creating a page or more of holes, zero them out via truncate.
352e4465fdaSMichael Halcrow 	 * Note, this will increase i_size. */
353807b7ebeSBadari Pulavarty 	if (index != 0) {
3547a3f595cSEric Sandeen 		if (prev_page_end_size > i_size_read(page->mapping->host)) {
355240e2df5SMichael Halcrow 			rc = ecryptfs_truncate(file->f_path.dentry,
3567a3f595cSEric Sandeen 					       prev_page_end_size);
35753a2731fSMichael Halcrow 			if (rc) {
358e4465fdaSMichael Halcrow 				printk(KERN_ERR "%s: Error on attempt to "
35953a2731fSMichael Halcrow 				       "truncate to (higher) offset [%lld];"
36018d1dbf1SHarvey Harrison 				       " rc = [%d]\n", __func__,
361e4465fdaSMichael Halcrow 				       prev_page_end_size, rc);
36253a2731fSMichael Halcrow 				goto out;
36353a2731fSMichael Halcrow 			}
36453a2731fSMichael Halcrow 		}
3657a3f595cSEric Sandeen 	}
366e4465fdaSMichael Halcrow 	/* Writing to a new page, and creating a small hole from start
367e4465fdaSMichael Halcrow 	 * of page?  Zero it out. */
368807b7ebeSBadari Pulavarty 	if ((i_size_read(mapping->host) == prev_page_end_size)
369807b7ebeSBadari Pulavarty 	    && (pos != 0))
370eebd2aa3SChristoph Lameter 		zero_user(page, 0, PAGE_CACHE_SIZE);
37153a2731fSMichael Halcrow out:
37253a2731fSMichael Halcrow 	return rc;
37353a2731fSMichael Halcrow }
37453a2731fSMichael Halcrow 
375237fead6SMichael Halcrow /**
376237fead6SMichael Halcrow  * ecryptfs_write_inode_size_to_header
377237fead6SMichael Halcrow  *
378237fead6SMichael Halcrow  * Writes the lower file size to the first 8 bytes of the header.
379237fead6SMichael Halcrow  *
380237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
381237fead6SMichael Halcrow  */
3820216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
383237fead6SMichael Halcrow {
3840216f7f7SMichael Halcrow 	char *file_size_virt;
3850216f7f7SMichael Halcrow 	int rc;
386237fead6SMichael Halcrow 
3870216f7f7SMichael Halcrow 	file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
3880216f7f7SMichael Halcrow 	if (!file_size_virt) {
3890216f7f7SMichael Halcrow 		rc = -ENOMEM;
390237fead6SMichael Halcrow 		goto out;
391237fead6SMichael Halcrow 	}
3920a688ad7SHarvey Harrison 	put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
3930216f7f7SMichael Halcrow 	rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
3940216f7f7SMichael Halcrow 				  sizeof(u64));
3950216f7f7SMichael Halcrow 	kfree(file_size_virt);
39696a7b9c2STyler Hicks 	if (rc < 0)
3970216f7f7SMichael Halcrow 		printk(KERN_ERR "%s: Error writing file size to header; "
39818d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, rc);
39996a7b9c2STyler Hicks 	else
40096a7b9c2STyler Hicks 		rc = 0;
401237fead6SMichael Halcrow out:
402237fead6SMichael Halcrow 	return rc;
403237fead6SMichael Halcrow }
404237fead6SMichael Halcrow 
4050216f7f7SMichael Halcrow struct kmem_cache *ecryptfs_xattr_cache;
4060216f7f7SMichael Halcrow 
4070216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
408dd2a3b7aSMichael Halcrow {
409dd2a3b7aSMichael Halcrow 	ssize_t size;
410dd2a3b7aSMichael Halcrow 	void *xattr_virt;
4110216f7f7SMichael Halcrow 	struct dentry *lower_dentry =
4120216f7f7SMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
4130216f7f7SMichael Halcrow 	struct inode *lower_inode = lower_dentry->d_inode;
414dd2a3b7aSMichael Halcrow 	int rc;
415dd2a3b7aSMichael Halcrow 
4160216f7f7SMichael Halcrow 	if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
4170216f7f7SMichael Halcrow 		printk(KERN_WARNING
4180216f7f7SMichael Halcrow 		       "No support for setting xattr in lower filesystem\n");
4190216f7f7SMichael Halcrow 		rc = -ENOSYS;
4200216f7f7SMichael Halcrow 		goto out;
4210216f7f7SMichael Halcrow 	}
422dd2a3b7aSMichael Halcrow 	xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
423dd2a3b7aSMichael Halcrow 	if (!xattr_virt) {
424dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Out of memory whilst attempting to write "
425dd2a3b7aSMichael Halcrow 		       "inode size to xattr\n");
426dd2a3b7aSMichael Halcrow 		rc = -ENOMEM;
427dd2a3b7aSMichael Halcrow 		goto out;
428dd2a3b7aSMichael Halcrow 	}
4290216f7f7SMichael Halcrow 	mutex_lock(&lower_inode->i_mutex);
4300216f7f7SMichael Halcrow 	size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
4310216f7f7SMichael Halcrow 					   xattr_virt, PAGE_CACHE_SIZE);
432dd2a3b7aSMichael Halcrow 	if (size < 0)
433dd2a3b7aSMichael Halcrow 		size = 8;
4340a688ad7SHarvey Harrison 	put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
4350216f7f7SMichael Halcrow 	rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
436dd2a3b7aSMichael Halcrow 					 xattr_virt, size, 0);
4370216f7f7SMichael Halcrow 	mutex_unlock(&lower_inode->i_mutex);
438dd2a3b7aSMichael Halcrow 	if (rc)
439dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error whilst attempting to write inode size "
440dd2a3b7aSMichael Halcrow 		       "to lower file xattr; rc = [%d]\n", rc);
441dd2a3b7aSMichael Halcrow 	kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
442dd2a3b7aSMichael Halcrow out:
443dd2a3b7aSMichael Halcrow 	return rc;
444dd2a3b7aSMichael Halcrow }
445dd2a3b7aSMichael Halcrow 
4460216f7f7SMichael Halcrow int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
447dd2a3b7aSMichael Halcrow {
448dd2a3b7aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
449dd2a3b7aSMichael Halcrow 
4500216f7f7SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
45113a791b4STyler Hicks 	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
452dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
4530216f7f7SMichael Halcrow 		return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
454dd2a3b7aSMichael Halcrow 	else
4550216f7f7SMichael Halcrow 		return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
456dd2a3b7aSMichael Halcrow }
457dd2a3b7aSMichael Halcrow 
458237fead6SMichael Halcrow /**
459807b7ebeSBadari Pulavarty  * ecryptfs_write_end
460237fead6SMichael Halcrow  * @file: The eCryptfs file object
461807b7ebeSBadari Pulavarty  * @mapping: The eCryptfs object
462807b7ebeSBadari Pulavarty  * @pos: The file position
463807b7ebeSBadari Pulavarty  * @len: The length of the data (unused)
464807b7ebeSBadari Pulavarty  * @copied: The amount of data copied
465237fead6SMichael Halcrow  * @page: The eCryptfs page
466807b7ebeSBadari Pulavarty  * @fsdata: The fsdata (unused)
467237fead6SMichael Halcrow  *
468237fead6SMichael Halcrow  * This is where we encrypt the data and pass the encrypted data to
469237fead6SMichael Halcrow  * the lower filesystem.  In OpenPGP-compatible mode, we operate on
470237fead6SMichael Halcrow  * entire underlying packets.
471237fead6SMichael Halcrow  */
472807b7ebeSBadari Pulavarty static int ecryptfs_write_end(struct file *file,
473807b7ebeSBadari Pulavarty 			struct address_space *mapping,
474807b7ebeSBadari Pulavarty 			loff_t pos, unsigned len, unsigned copied,
475807b7ebeSBadari Pulavarty 			struct page *page, void *fsdata)
476237fead6SMichael Halcrow {
477807b7ebeSBadari Pulavarty 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
478807b7ebeSBadari Pulavarty 	unsigned from = pos & (PAGE_CACHE_SIZE - 1);
479807b7ebeSBadari Pulavarty 	unsigned to = from + copied;
480807b7ebeSBadari Pulavarty 	struct inode *ecryptfs_inode = mapping->host;
481bf12be1cSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
482bef5bc24SAl Viro 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
483237fead6SMichael Halcrow 	int rc;
484237fead6SMichael Halcrow 
485237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
486888d57bbSJoe Perches 			"(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
48713a791b4STyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
48813a791b4STyler Hicks 		rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0,
48913a791b4STyler Hicks 						       to);
49013a791b4STyler Hicks 		if (!rc) {
49113a791b4STyler Hicks 			rc = copied;
49213a791b4STyler Hicks 			fsstack_copy_inode_size(ecryptfs_inode,
49313a791b4STyler Hicks 				ecryptfs_inode_to_lower(ecryptfs_inode));
49413a791b4STyler Hicks 		}
49513a791b4STyler Hicks 		goto out;
49613a791b4STyler Hicks 	}
497bf12be1cSMichael Halcrow 	/* Fills in zeros if 'to' goes beyond inode size */
498237fead6SMichael Halcrow 	rc = fill_zeros_to_end_of_page(page, to);
499237fead6SMichael Halcrow 	if (rc) {
500237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
501888d57bbSJoe Perches 			"zeros in page with index = [0x%.16lx]\n", index);
502237fead6SMichael Halcrow 		goto out;
503237fead6SMichael Halcrow 	}
5040216f7f7SMichael Halcrow 	rc = ecryptfs_encrypt_page(page);
505237fead6SMichael Halcrow 	if (rc) {
506237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
507888d57bbSJoe Perches 				"index [0x%.16lx])\n", index);
508237fead6SMichael Halcrow 		goto out;
509237fead6SMichael Halcrow 	}
510807b7ebeSBadari Pulavarty 	if (pos + copied > i_size_read(ecryptfs_inode)) {
511807b7ebeSBadari Pulavarty 		i_size_write(ecryptfs_inode, pos + copied);
512237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
513888d57bbSJoe Perches 			"[0x%.16llx]\n",
514888d57bbSJoe Perches 			(unsigned long long)i_size_read(ecryptfs_inode));
515237fead6SMichael Halcrow 	}
516bf12be1cSMichael Halcrow 	rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
517dd2a3b7aSMichael Halcrow 	if (rc)
518dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error writing inode size to metadata; "
519dd2a3b7aSMichael Halcrow 		       "rc = [%d]\n", rc);
520807b7ebeSBadari Pulavarty 	else
521807b7ebeSBadari Pulavarty 		rc = copied;
522237fead6SMichael Halcrow out:
523807b7ebeSBadari Pulavarty 	unlock_page(page);
524807b7ebeSBadari Pulavarty 	page_cache_release(page);
525237fead6SMichael Halcrow 	return rc;
526237fead6SMichael Halcrow }
527237fead6SMichael Halcrow 
528237fead6SMichael Halcrow static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
529237fead6SMichael Halcrow {
530237fead6SMichael Halcrow 	int rc = 0;
531237fead6SMichael Halcrow 	struct inode *inode;
532237fead6SMichael Halcrow 	struct inode *lower_inode;
533237fead6SMichael Halcrow 
534237fead6SMichael Halcrow 	inode = (struct inode *)mapping->host;
535237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
536237fead6SMichael Halcrow 	if (lower_inode->i_mapping->a_ops->bmap)
537237fead6SMichael Halcrow 		rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
538237fead6SMichael Halcrow 							 block);
539237fead6SMichael Halcrow 	return rc;
540237fead6SMichael Halcrow }
541237fead6SMichael Halcrow 
5427f09410bSAlexey Dobriyan const struct address_space_operations ecryptfs_aops = {
543237fead6SMichael Halcrow 	.writepage = ecryptfs_writepage,
544237fead6SMichael Halcrow 	.readpage = ecryptfs_readpage,
545807b7ebeSBadari Pulavarty 	.write_begin = ecryptfs_write_begin,
546807b7ebeSBadari Pulavarty 	.write_end = ecryptfs_write_end,
547237fead6SMichael Halcrow 	.bmap = ecryptfs_bmap,
548237fead6SMichael Halcrow };
549