xref: /openbmc/linux/fs/ecryptfs/mmap.c (revision cc11beffdf80ca31dff21422fa2a5e54d25f1494)
1237fead6SMichael Halcrow /**
2237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3237fead6SMichael Halcrow  * This is where eCryptfs coordinates the symmetric encryption and
4237fead6SMichael Halcrow  * decryption of the file data as it passes between the lower
5237fead6SMichael Halcrow  * encrypted file and the upper decrypted file.
6237fead6SMichael Halcrow  *
7237fead6SMichael Halcrow  * Copyright (C) 1997-2003 Erez Zadok
8237fead6SMichael Halcrow  * Copyright (C) 2001-2003 Stony Brook University
9dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
10237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
11237fead6SMichael Halcrow  *
12237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
13237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
14237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
15237fead6SMichael Halcrow  * License, or (at your option) any later version.
16237fead6SMichael Halcrow  *
17237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
18237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
19237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20237fead6SMichael Halcrow  * General Public License for more details.
21237fead6SMichael Halcrow  *
22237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
23237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
24237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25237fead6SMichael Halcrow  * 02111-1307, USA.
26237fead6SMichael Halcrow  */
27237fead6SMichael Halcrow 
28237fead6SMichael Halcrow #include <linux/pagemap.h>
29237fead6SMichael Halcrow #include <linux/writeback.h>
30237fead6SMichael Halcrow #include <linux/page-flags.h>
31237fead6SMichael Halcrow #include <linux/mount.h>
32237fead6SMichael Halcrow #include <linux/file.h>
33237fead6SMichael Halcrow #include <linux/crypto.h>
34237fead6SMichael Halcrow #include <linux/scatterlist.h>
35237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
36237fead6SMichael Halcrow 
37237fead6SMichael Halcrow /**
3816a72c45SMichael Halcrow  * ecryptfs_get_locked_page
39237fead6SMichael Halcrow  *
40237fead6SMichael Halcrow  * Get one page from cache or lower f/s, return error otherwise.
41237fead6SMichael Halcrow  *
4216a72c45SMichael Halcrow  * Returns locked and up-to-date page (if ok), with increased
43237fead6SMichael Halcrow  * refcnt.
44237fead6SMichael Halcrow  */
4516a72c45SMichael Halcrow struct page *ecryptfs_get_locked_page(struct file *file, loff_t index)
46237fead6SMichael Halcrow {
47237fead6SMichael Halcrow 	struct dentry *dentry;
48237fead6SMichael Halcrow 	struct inode *inode;
49237fead6SMichael Halcrow 	struct address_space *mapping;
5016a72c45SMichael Halcrow 	struct page *page;
51237fead6SMichael Halcrow 
52bd243a4bSJosef "Jeff" Sipek 	dentry = file->f_path.dentry;
53237fead6SMichael Halcrow 	inode = dentry->d_inode;
54237fead6SMichael Halcrow 	mapping = inode->i_mapping;
5516a72c45SMichael Halcrow 	page = read_mapping_page(mapping, index, (void *)file);
5616a72c45SMichael Halcrow 	if (!IS_ERR(page))
5716a72c45SMichael Halcrow 		lock_page(page);
5816a72c45SMichael Halcrow 	return page;
59237fead6SMichael Halcrow }
60237fead6SMichael Halcrow 
61237fead6SMichael Halcrow /**
62237fead6SMichael Halcrow  * ecryptfs_writepage
63237fead6SMichael Halcrow  * @page: Page that is locked before this call is made
64237fead6SMichael Halcrow  *
65237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
66237fead6SMichael Halcrow  */
67237fead6SMichael Halcrow static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
68237fead6SMichael Halcrow {
69237fead6SMichael Halcrow 	int rc;
70237fead6SMichael Halcrow 
710216f7f7SMichael Halcrow 	rc = ecryptfs_encrypt_page(page);
72237fead6SMichael Halcrow 	if (rc) {
73237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error encrypting "
74237fead6SMichael Halcrow 				"page (upper index [0x%.16x])\n", page->index);
75237fead6SMichael Halcrow 		ClearPageUptodate(page);
76237fead6SMichael Halcrow 		goto out;
77237fead6SMichael Halcrow 	}
78237fead6SMichael Halcrow 	SetPageUptodate(page);
79237fead6SMichael Halcrow 	unlock_page(page);
80237fead6SMichael Halcrow out:
81237fead6SMichael Halcrow 	return rc;
82237fead6SMichael Halcrow }
83237fead6SMichael Halcrow 
84237fead6SMichael Halcrow /**
85e77a56ddSMichael Halcrow  *   Header Extent:
86e77a56ddSMichael Halcrow  *     Octets 0-7:        Unencrypted file size (big-endian)
87e77a56ddSMichael Halcrow  *     Octets 8-15:       eCryptfs special marker
88e77a56ddSMichael Halcrow  *     Octets 16-19:      Flags
89e77a56ddSMichael Halcrow  *      Octet 16:         File format version number (between 0 and 255)
90e77a56ddSMichael Halcrow  *      Octets 17-18:     Reserved
91e77a56ddSMichael Halcrow  *      Octet 19:         Bit 1 (lsb): Reserved
92e77a56ddSMichael Halcrow  *                        Bit 2: Encrypted?
93e77a56ddSMichael Halcrow  *                        Bits 3-8: Reserved
94e77a56ddSMichael Halcrow  *     Octets 20-23:      Header extent size (big-endian)
95e77a56ddSMichael Halcrow  *     Octets 24-25:      Number of header extents at front of file
96e77a56ddSMichael Halcrow  *                        (big-endian)
97e77a56ddSMichael Halcrow  *     Octet  26:         Begin RFC 2440 authentication token packet set
98e77a56ddSMichael Halcrow  */
99e77a56ddSMichael Halcrow static void set_header_info(char *page_virt,
100e77a56ddSMichael Halcrow 			    struct ecryptfs_crypt_stat *crypt_stat)
101e77a56ddSMichael Halcrow {
102e77a56ddSMichael Halcrow 	size_t written;
103*cc11beffSMichael Halcrow 	size_t save_num_header_bytes_at_front =
104*cc11beffSMichael Halcrow 		crypt_stat->num_header_bytes_at_front;
105e77a56ddSMichael Halcrow 
106*cc11beffSMichael Halcrow 	crypt_stat->num_header_bytes_at_front =
107*cc11beffSMichael Halcrow 		ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
108e77a56ddSMichael Halcrow 	ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
109*cc11beffSMichael Halcrow 	crypt_stat->num_header_bytes_at_front =
110*cc11beffSMichael Halcrow 		save_num_header_bytes_at_front;
111e77a56ddSMichael Halcrow }
112237fead6SMichael Halcrow 
113237fead6SMichael Halcrow /**
114bf12be1cSMichael Halcrow  * ecryptfs_copy_up_encrypted_with_header
115bf12be1cSMichael Halcrow  * @page: Sort of a ``virtual'' representation of the encrypted lower
116bf12be1cSMichael Halcrow  *        file. The actual lower file does not have the metadata in
117bf12be1cSMichael Halcrow  *        the header. This is locked.
118bf12be1cSMichael Halcrow  * @crypt_stat: The eCryptfs inode's cryptographic context
119237fead6SMichael Halcrow  *
120bf12be1cSMichael Halcrow  * The ``view'' is the version of the file that userspace winds up
121bf12be1cSMichael Halcrow  * seeing, with the header information inserted.
122237fead6SMichael Halcrow  */
123bf12be1cSMichael Halcrow static int
124bf12be1cSMichael Halcrow ecryptfs_copy_up_encrypted_with_header(struct page *page,
125bf12be1cSMichael Halcrow 				       struct ecryptfs_crypt_stat *crypt_stat)
126237fead6SMichael Halcrow {
127bf12be1cSMichael Halcrow 	loff_t extent_num_in_page = 0;
128bf12be1cSMichael Halcrow 	loff_t num_extents_per_page = (PAGE_CACHE_SIZE
129bf12be1cSMichael Halcrow 				       / crypt_stat->extent_size);
130237fead6SMichael Halcrow 	int rc = 0;
131237fead6SMichael Halcrow 
132bf12be1cSMichael Halcrow 	while (extent_num_in_page < num_extents_per_page) {
133d6a13c17SMichael Halcrow 		loff_t view_extent_num = ((((loff_t)page->index)
134d6a13c17SMichael Halcrow 					   * num_extents_per_page)
135bf12be1cSMichael Halcrow 					  + extent_num_in_page);
136*cc11beffSMichael Halcrow 		size_t num_header_extents_at_front =
137*cc11beffSMichael Halcrow 			(crypt_stat->num_header_bytes_at_front
138*cc11beffSMichael Halcrow 			 / crypt_stat->extent_size);
139e77a56ddSMichael Halcrow 
140*cc11beffSMichael Halcrow 		if (view_extent_num < num_header_extents_at_front) {
141bf12be1cSMichael Halcrow 			/* This is a header extent */
142e77a56ddSMichael Halcrow 			char *page_virt;
143e77a56ddSMichael Halcrow 
1449d8b8ce5SMichael Halcrow 			page_virt = kmap_atomic(page, KM_USER0);
145e77a56ddSMichael Halcrow 			memset(page_virt, 0, PAGE_CACHE_SIZE);
146bf12be1cSMichael Halcrow 			/* TODO: Support more than one header extent */
147bf12be1cSMichael Halcrow 			if (view_extent_num == 0) {
148e77a56ddSMichael Halcrow 				rc = ecryptfs_read_xattr_region(
149d7cdc5feSMichael Halcrow 					page_virt, page->mapping->host);
150e77a56ddSMichael Halcrow 				set_header_info(page_virt, crypt_stat);
151e77a56ddSMichael Halcrow 			}
1529d8b8ce5SMichael Halcrow 			kunmap_atomic(page_virt, KM_USER0);
1530a9ac382SMichael Halcrow 			flush_dcache_page(page);
154e77a56ddSMichael Halcrow 			if (rc) {
155bf12be1cSMichael Halcrow 				printk(KERN_ERR "%s: Error reading xattr "
156bf12be1cSMichael Halcrow 				       "region; rc = [%d]\n", __FUNCTION__, rc);
157e77a56ddSMichael Halcrow 				goto out;
158e77a56ddSMichael Halcrow 			}
159e77a56ddSMichael Halcrow 		} else {
160bf12be1cSMichael Halcrow 			/* This is an encrypted data extent */
161bf12be1cSMichael Halcrow 			loff_t lower_offset =
162*cc11beffSMichael Halcrow 				((view_extent_num * crypt_stat->extent_size)
163*cc11beffSMichael Halcrow 				 - crypt_stat->num_header_bytes_at_front);
164bf12be1cSMichael Halcrow 
165bf12be1cSMichael Halcrow 			rc = ecryptfs_read_lower_page_segment(
166bf12be1cSMichael Halcrow 				page, (lower_offset >> PAGE_CACHE_SHIFT),
167bf12be1cSMichael Halcrow 				(lower_offset & ~PAGE_CACHE_MASK),
168bf12be1cSMichael Halcrow 				crypt_stat->extent_size, page->mapping->host);
169e77a56ddSMichael Halcrow 			if (rc) {
170bf12be1cSMichael Halcrow 				printk(KERN_ERR "%s: Error attempting to read "
171bf12be1cSMichael Halcrow 				       "extent at offset [%lld] in the lower "
172bf12be1cSMichael Halcrow 				       "file; rc = [%d]\n", __FUNCTION__,
173bf12be1cSMichael Halcrow 				       lower_offset, rc);
174e77a56ddSMichael Halcrow 				goto out;
175e77a56ddSMichael Halcrow 			}
176e77a56ddSMichael Halcrow 		}
177bf12be1cSMichael Halcrow 		extent_num_in_page++;
178bf12be1cSMichael Halcrow 	}
179bf12be1cSMichael Halcrow out:
180bf12be1cSMichael Halcrow 	return rc;
181bf12be1cSMichael Halcrow }
182bf12be1cSMichael Halcrow 
183bf12be1cSMichael Halcrow /**
184bf12be1cSMichael Halcrow  * ecryptfs_readpage
185bf12be1cSMichael Halcrow  * @file: An eCryptfs file
186bf12be1cSMichael Halcrow  * @page: Page from eCryptfs inode mapping into which to stick the read data
187bf12be1cSMichael Halcrow  *
188bf12be1cSMichael Halcrow  * Read in a page, decrypting if necessary.
189bf12be1cSMichael Halcrow  *
190bf12be1cSMichael Halcrow  * Returns zero on success; non-zero on error.
191bf12be1cSMichael Halcrow  */
192bf12be1cSMichael Halcrow static int ecryptfs_readpage(struct file *file, struct page *page)
193bf12be1cSMichael Halcrow {
194bf12be1cSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
195bf12be1cSMichael Halcrow 		&ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
196bf12be1cSMichael Halcrow 	int rc = 0;
197bf12be1cSMichael Halcrow 
198bf12be1cSMichael Halcrow 	if (!crypt_stat
199bf12be1cSMichael Halcrow 	    || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
200bf12be1cSMichael Halcrow 	    || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
201bf12be1cSMichael Halcrow 		ecryptfs_printk(KERN_DEBUG,
202bf12be1cSMichael Halcrow 				"Passing through unencrypted page\n");
203bf12be1cSMichael Halcrow 		rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
204bf12be1cSMichael Halcrow 						      PAGE_CACHE_SIZE,
205bf12be1cSMichael Halcrow 						      page->mapping->host);
206bf12be1cSMichael Halcrow 	} else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
207bf12be1cSMichael Halcrow 		if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
208bf12be1cSMichael Halcrow 			rc = ecryptfs_copy_up_encrypted_with_header(page,
209bf12be1cSMichael Halcrow 								    crypt_stat);
210bf12be1cSMichael Halcrow 			if (rc) {
211bf12be1cSMichael Halcrow 				printk(KERN_ERR "%s: Error attempting to copy "
212bf12be1cSMichael Halcrow 				       "the encrypted content from the lower "
213bf12be1cSMichael Halcrow 				       "file whilst inserting the metadata "
214bf12be1cSMichael Halcrow 				       "from the xattr into the header; rc = "
215bf12be1cSMichael Halcrow 				       "[%d]\n", __FUNCTION__, rc);
216bf12be1cSMichael Halcrow 				goto out;
217bf12be1cSMichael Halcrow 			}
218bf12be1cSMichael Halcrow 
219e77a56ddSMichael Halcrow 		} else {
220bf12be1cSMichael Halcrow 			rc = ecryptfs_read_lower_page_segment(
221bf12be1cSMichael Halcrow 				page, page->index, 0, PAGE_CACHE_SIZE,
222bf12be1cSMichael Halcrow 				page->mapping->host);
223e77a56ddSMichael Halcrow 			if (rc) {
224e77a56ddSMichael Halcrow 				printk(KERN_ERR "Error reading page; rc = "
225e77a56ddSMichael Halcrow 				       "[%d]\n", rc);
226e77a56ddSMichael Halcrow 				goto out;
227e77a56ddSMichael Halcrow 			}
228e77a56ddSMichael Halcrow 		}
229237fead6SMichael Halcrow 	} else {
2300216f7f7SMichael Halcrow 		rc = ecryptfs_decrypt_page(page);
231237fead6SMichael Halcrow 		if (rc) {
232237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error decrypting page; "
233237fead6SMichael Halcrow 					"rc = [%d]\n", rc);
234237fead6SMichael Halcrow 			goto out;
235237fead6SMichael Halcrow 		}
236237fead6SMichael Halcrow 	}
237237fead6SMichael Halcrow out:
23816a72c45SMichael Halcrow 	if (rc)
23916a72c45SMichael Halcrow 		ClearPageUptodate(page);
24016a72c45SMichael Halcrow 	else
24116a72c45SMichael Halcrow 		SetPageUptodate(page);
242237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
243237fead6SMichael Halcrow 			page->index);
244237fead6SMichael Halcrow 	unlock_page(page);
245237fead6SMichael Halcrow 	return rc;
246237fead6SMichael Halcrow }
247237fead6SMichael Halcrow 
248dd2a3b7aSMichael Halcrow /**
249dd2a3b7aSMichael Halcrow  * Called with lower inode mutex held.
250dd2a3b7aSMichael Halcrow  */
251237fead6SMichael Halcrow static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
252237fead6SMichael Halcrow {
253237fead6SMichael Halcrow 	struct inode *inode = page->mapping->host;
254237fead6SMichael Halcrow 	int end_byte_in_page;
255237fead6SMichael Halcrow 
2569d8b8ce5SMichael Halcrow 	if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
2579d8b8ce5SMichael Halcrow 		goto out;
258237fead6SMichael Halcrow 	end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
259237fead6SMichael Halcrow 	if (to > end_byte_in_page)
260237fead6SMichael Halcrow 		end_byte_in_page = to;
261eebd2aa3SChristoph Lameter 	zero_user_segment(page, end_byte_in_page, PAGE_CACHE_SIZE);
262237fead6SMichael Halcrow out:
2639d8b8ce5SMichael Halcrow 	return 0;
264237fead6SMichael Halcrow }
265237fead6SMichael Halcrow 
2667a3f595cSEric Sandeen /* This function must zero any hole we create */
26753a2731fSMichael Halcrow static int ecryptfs_prepare_write(struct file *file, struct page *page,
26853a2731fSMichael Halcrow 				  unsigned from, unsigned to)
26953a2731fSMichael Halcrow {
27053a2731fSMichael Halcrow 	int rc = 0;
2717a3f595cSEric Sandeen 	loff_t prev_page_end_size;
27253a2731fSMichael Halcrow 
27316a72c45SMichael Halcrow 	if (!PageUptodate(page)) {
274bf12be1cSMichael Halcrow 		rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
275bf12be1cSMichael Halcrow 						      PAGE_CACHE_SIZE,
276bf12be1cSMichael Halcrow 						      page->mapping->host);
27716a72c45SMichael Halcrow 		if (rc) {
27816a72c45SMichael Halcrow 			printk(KERN_ERR "%s: Error attemping to read lower "
27916a72c45SMichael Halcrow 			       "page segment; rc = [%d]\n", __FUNCTION__, rc);
28016a72c45SMichael Halcrow 			ClearPageUptodate(page);
28116a72c45SMichael Halcrow 			goto out;
28216a72c45SMichael Halcrow 		} else
28316a72c45SMichael Halcrow 			SetPageUptodate(page);
28416a72c45SMichael Halcrow 	}
285240e2df5SMichael Halcrow 
2867a3f595cSEric Sandeen 	prev_page_end_size = ((loff_t)page->index << PAGE_CACHE_SHIFT);
2877a3f595cSEric Sandeen 
2887a3f595cSEric Sandeen 	/*
2897a3f595cSEric Sandeen 	 * If creating a page or more of holes, zero them out via truncate.
2907a3f595cSEric Sandeen 	 * Note, this will increase i_size.
2917a3f595cSEric Sandeen 	 */
2927a3f595cSEric Sandeen 	if (page->index != 0) {
2937a3f595cSEric Sandeen 		if (prev_page_end_size > i_size_read(page->mapping->host)) {
294240e2df5SMichael Halcrow 			rc = ecryptfs_truncate(file->f_path.dentry,
2957a3f595cSEric Sandeen 					       prev_page_end_size);
29653a2731fSMichael Halcrow 			if (rc) {
29753a2731fSMichael Halcrow 				printk(KERN_ERR "Error on attempt to "
29853a2731fSMichael Halcrow 				       "truncate to (higher) offset [%lld];"
2997a3f595cSEric Sandeen 				       " rc = [%d]\n", prev_page_end_size, rc);
30053a2731fSMichael Halcrow 				goto out;
30153a2731fSMichael Halcrow 			}
30253a2731fSMichael Halcrow 		}
3037a3f595cSEric Sandeen 	}
3047a3f595cSEric Sandeen 	/*
3057a3f595cSEric Sandeen 	 * Writing to a new page, and creating a small hole from start of page?
3067a3f595cSEric Sandeen 	 * Zero it out.
3077a3f595cSEric Sandeen 	 */
3087a3f595cSEric Sandeen 	if ((i_size_read(page->mapping->host) == prev_page_end_size) &&
3097a3f595cSEric Sandeen 	    (from != 0)) {
310eebd2aa3SChristoph Lameter 		zero_user(page, 0, PAGE_CACHE_SIZE);
311240e2df5SMichael Halcrow 	}
31253a2731fSMichael Halcrow out:
31353a2731fSMichael Halcrow 	return rc;
31453a2731fSMichael Halcrow }
31553a2731fSMichael Halcrow 
316237fead6SMichael Halcrow /**
317237fead6SMichael Halcrow  * ecryptfs_write_inode_size_to_header
318237fead6SMichael Halcrow  *
319237fead6SMichael Halcrow  * Writes the lower file size to the first 8 bytes of the header.
320237fead6SMichael Halcrow  *
321237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
322237fead6SMichael Halcrow  */
3230216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
324237fead6SMichael Halcrow {
325237fead6SMichael Halcrow 	u64 file_size;
3260216f7f7SMichael Halcrow 	char *file_size_virt;
3270216f7f7SMichael Halcrow 	int rc;
328237fead6SMichael Halcrow 
3290216f7f7SMichael Halcrow 	file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
3300216f7f7SMichael Halcrow 	if (!file_size_virt) {
3310216f7f7SMichael Halcrow 		rc = -ENOMEM;
332237fead6SMichael Halcrow 		goto out;
333237fead6SMichael Halcrow 	}
3340216f7f7SMichael Halcrow 	file_size = (u64)i_size_read(ecryptfs_inode);
335237fead6SMichael Halcrow 	file_size = cpu_to_be64(file_size);
3360216f7f7SMichael Halcrow 	memcpy(file_size_virt, &file_size, sizeof(u64));
3370216f7f7SMichael Halcrow 	rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
3380216f7f7SMichael Halcrow 				  sizeof(u64));
3390216f7f7SMichael Halcrow 	kfree(file_size_virt);
3400216f7f7SMichael Halcrow 	if (rc)
3410216f7f7SMichael Halcrow 		printk(KERN_ERR "%s: Error writing file size to header; "
3420216f7f7SMichael Halcrow 		       "rc = [%d]\n", __FUNCTION__, rc);
343237fead6SMichael Halcrow out:
344237fead6SMichael Halcrow 	return rc;
345237fead6SMichael Halcrow }
346237fead6SMichael Halcrow 
3470216f7f7SMichael Halcrow struct kmem_cache *ecryptfs_xattr_cache;
3480216f7f7SMichael Halcrow 
3490216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
350dd2a3b7aSMichael Halcrow {
351dd2a3b7aSMichael Halcrow 	ssize_t size;
352dd2a3b7aSMichael Halcrow 	void *xattr_virt;
3530216f7f7SMichael Halcrow 	struct dentry *lower_dentry =
3540216f7f7SMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
3550216f7f7SMichael Halcrow 	struct inode *lower_inode = lower_dentry->d_inode;
356dd2a3b7aSMichael Halcrow 	u64 file_size;
357dd2a3b7aSMichael Halcrow 	int rc;
358dd2a3b7aSMichael Halcrow 
3590216f7f7SMichael Halcrow 	if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
3600216f7f7SMichael Halcrow 		printk(KERN_WARNING
3610216f7f7SMichael Halcrow 		       "No support for setting xattr in lower filesystem\n");
3620216f7f7SMichael Halcrow 		rc = -ENOSYS;
3630216f7f7SMichael Halcrow 		goto out;
3640216f7f7SMichael Halcrow 	}
365dd2a3b7aSMichael Halcrow 	xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
366dd2a3b7aSMichael Halcrow 	if (!xattr_virt) {
367dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Out of memory whilst attempting to write "
368dd2a3b7aSMichael Halcrow 		       "inode size to xattr\n");
369dd2a3b7aSMichael Halcrow 		rc = -ENOMEM;
370dd2a3b7aSMichael Halcrow 		goto out;
371dd2a3b7aSMichael Halcrow 	}
3720216f7f7SMichael Halcrow 	mutex_lock(&lower_inode->i_mutex);
3730216f7f7SMichael Halcrow 	size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
3740216f7f7SMichael Halcrow 					   xattr_virt, PAGE_CACHE_SIZE);
375dd2a3b7aSMichael Halcrow 	if (size < 0)
376dd2a3b7aSMichael Halcrow 		size = 8;
3770216f7f7SMichael Halcrow 	file_size = (u64)i_size_read(ecryptfs_inode);
378dd2a3b7aSMichael Halcrow 	file_size = cpu_to_be64(file_size);
379dd2a3b7aSMichael Halcrow 	memcpy(xattr_virt, &file_size, sizeof(u64));
3800216f7f7SMichael Halcrow 	rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
381dd2a3b7aSMichael Halcrow 					 xattr_virt, size, 0);
3820216f7f7SMichael Halcrow 	mutex_unlock(&lower_inode->i_mutex);
383dd2a3b7aSMichael Halcrow 	if (rc)
384dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error whilst attempting to write inode size "
385dd2a3b7aSMichael Halcrow 		       "to lower file xattr; rc = [%d]\n", rc);
386dd2a3b7aSMichael Halcrow 	kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
387dd2a3b7aSMichael Halcrow out:
388dd2a3b7aSMichael Halcrow 	return rc;
389dd2a3b7aSMichael Halcrow }
390dd2a3b7aSMichael Halcrow 
3910216f7f7SMichael Halcrow int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
392dd2a3b7aSMichael Halcrow {
393dd2a3b7aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
394dd2a3b7aSMichael Halcrow 
3950216f7f7SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
396dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
3970216f7f7SMichael Halcrow 		return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
398dd2a3b7aSMichael Halcrow 	else
3990216f7f7SMichael Halcrow 		return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
400dd2a3b7aSMichael Halcrow }
401dd2a3b7aSMichael Halcrow 
402237fead6SMichael Halcrow /**
403237fead6SMichael Halcrow  * ecryptfs_commit_write
404237fead6SMichael Halcrow  * @file: The eCryptfs file object
405237fead6SMichael Halcrow  * @page: The eCryptfs page
406237fead6SMichael Halcrow  * @from: Ignored (we rotate the page IV on each write)
407237fead6SMichael Halcrow  * @to: Ignored
408237fead6SMichael Halcrow  *
409237fead6SMichael Halcrow  * This is where we encrypt the data and pass the encrypted data to
410237fead6SMichael Halcrow  * the lower filesystem.  In OpenPGP-compatible mode, we operate on
411237fead6SMichael Halcrow  * entire underlying packets.
412237fead6SMichael Halcrow  */
413237fead6SMichael Halcrow static int ecryptfs_commit_write(struct file *file, struct page *page,
414237fead6SMichael Halcrow 				 unsigned from, unsigned to)
415237fead6SMichael Halcrow {
416237fead6SMichael Halcrow 	loff_t pos;
417bf12be1cSMichael Halcrow 	struct inode *ecryptfs_inode = page->mapping->host;
418bf12be1cSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
419bf12be1cSMichael Halcrow 		&ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
420237fead6SMichael Halcrow 	int rc;
421237fead6SMichael Halcrow 
422e2bd99ecSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
423237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
424237fead6SMichael Halcrow 			"crypt_stat at memory location [%p]\n", crypt_stat);
425e2bd99ecSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
426237fead6SMichael Halcrow 	} else
427237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
428237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
429237fead6SMichael Halcrow 			"(page w/ index = [0x%.16x], to = [%d])\n", page->index,
430237fead6SMichael Halcrow 			to);
431bf12be1cSMichael Halcrow 	/* Fills in zeros if 'to' goes beyond inode size */
432237fead6SMichael Halcrow 	rc = fill_zeros_to_end_of_page(page, to);
433237fead6SMichael Halcrow 	if (rc) {
434237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
435237fead6SMichael Halcrow 				"zeros in page with index = [0x%.16x]\n",
436237fead6SMichael Halcrow 				page->index);
437237fead6SMichael Halcrow 		goto out;
438237fead6SMichael Halcrow 	}
4390216f7f7SMichael Halcrow 	rc = ecryptfs_encrypt_page(page);
440237fead6SMichael Halcrow 	if (rc) {
441237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
442237fead6SMichael Halcrow 				"index [0x%.16x])\n", page->index);
443237fead6SMichael Halcrow 		goto out;
444237fead6SMichael Halcrow 	}
445d6a13c17SMichael Halcrow 	pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to;
446bf12be1cSMichael Halcrow 	if (pos > i_size_read(ecryptfs_inode)) {
447bf12be1cSMichael Halcrow 		i_size_write(ecryptfs_inode, pos);
448237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
449bf12be1cSMichael Halcrow 				"[0x%.16x]\n", i_size_read(ecryptfs_inode));
450237fead6SMichael Halcrow 	}
451bf12be1cSMichael Halcrow 	rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
452dd2a3b7aSMichael Halcrow 	if (rc)
453dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error writing inode size to metadata; "
454dd2a3b7aSMichael Halcrow 		       "rc = [%d]\n", rc);
455237fead6SMichael Halcrow out:
456237fead6SMichael Halcrow 	return rc;
457237fead6SMichael Halcrow }
458237fead6SMichael Halcrow 
459237fead6SMichael Halcrow static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
460237fead6SMichael Halcrow {
461237fead6SMichael Halcrow 	int rc = 0;
462237fead6SMichael Halcrow 	struct inode *inode;
463237fead6SMichael Halcrow 	struct inode *lower_inode;
464237fead6SMichael Halcrow 
465237fead6SMichael Halcrow 	inode = (struct inode *)mapping->host;
466237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
467237fead6SMichael Halcrow 	if (lower_inode->i_mapping->a_ops->bmap)
468237fead6SMichael Halcrow 		rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
469237fead6SMichael Halcrow 							 block);
470237fead6SMichael Halcrow 	return rc;
471237fead6SMichael Halcrow }
472237fead6SMichael Halcrow 
473237fead6SMichael Halcrow struct address_space_operations ecryptfs_aops = {
474237fead6SMichael Halcrow 	.writepage = ecryptfs_writepage,
475237fead6SMichael Halcrow 	.readpage = ecryptfs_readpage,
476237fead6SMichael Halcrow 	.prepare_write = ecryptfs_prepare_write,
477237fead6SMichael Halcrow 	.commit_write = ecryptfs_commit_write,
478237fead6SMichael Halcrow 	.bmap = ecryptfs_bmap,
479237fead6SMichael Halcrow };
480