xref: /openbmc/linux/fs/ecryptfs/mmap.c (revision eebd2aa355692afaf9906f62118620f1a1c19dbb)
1237fead6SMichael Halcrow /**
2237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3237fead6SMichael Halcrow  * This is where eCryptfs coordinates the symmetric encryption and
4237fead6SMichael Halcrow  * decryption of the file data as it passes between the lower
5237fead6SMichael Halcrow  * encrypted file and the upper decrypted file.
6237fead6SMichael Halcrow  *
7237fead6SMichael Halcrow  * Copyright (C) 1997-2003 Erez Zadok
8237fead6SMichael Halcrow  * Copyright (C) 2001-2003 Stony Brook University
9dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
10237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
11237fead6SMichael Halcrow  *
12237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
13237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
14237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
15237fead6SMichael Halcrow  * License, or (at your option) any later version.
16237fead6SMichael Halcrow  *
17237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
18237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
19237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20237fead6SMichael Halcrow  * General Public License for more details.
21237fead6SMichael Halcrow  *
22237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
23237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
24237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25237fead6SMichael Halcrow  * 02111-1307, USA.
26237fead6SMichael Halcrow  */
27237fead6SMichael Halcrow 
28237fead6SMichael Halcrow #include <linux/pagemap.h>
29237fead6SMichael Halcrow #include <linux/writeback.h>
30237fead6SMichael Halcrow #include <linux/page-flags.h>
31237fead6SMichael Halcrow #include <linux/mount.h>
32237fead6SMichael Halcrow #include <linux/file.h>
33237fead6SMichael Halcrow #include <linux/crypto.h>
34237fead6SMichael Halcrow #include <linux/scatterlist.h>
35237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
36237fead6SMichael Halcrow 
37237fead6SMichael Halcrow struct kmem_cache *ecryptfs_lower_page_cache;
38237fead6SMichael Halcrow 
39237fead6SMichael Halcrow /**
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  */
4716a72c45SMichael Halcrow struct page *ecryptfs_get_locked_page(struct file *file, loff_t index)
48237fead6SMichael Halcrow {
49237fead6SMichael Halcrow 	struct dentry *dentry;
50237fead6SMichael Halcrow 	struct inode *inode;
51237fead6SMichael Halcrow 	struct address_space *mapping;
5216a72c45SMichael Halcrow 	struct page *page;
53237fead6SMichael Halcrow 
54bd243a4bSJosef "Jeff" Sipek 	dentry = file->f_path.dentry;
55237fead6SMichael Halcrow 	inode = dentry->d_inode;
56237fead6SMichael Halcrow 	mapping = inode->i_mapping;
5716a72c45SMichael Halcrow 	page = read_mapping_page(mapping, index, (void *)file);
5816a72c45SMichael Halcrow 	if (!IS_ERR(page))
5916a72c45SMichael Halcrow 		lock_page(page);
6016a72c45SMichael Halcrow 	return page;
61237fead6SMichael Halcrow }
62237fead6SMichael Halcrow 
63237fead6SMichael Halcrow /**
64237fead6SMichael Halcrow  * ecryptfs_writepage
65237fead6SMichael Halcrow  * @page: Page that is locked before this call is made
66237fead6SMichael Halcrow  *
67237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
68237fead6SMichael Halcrow  */
69237fead6SMichael Halcrow static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
70237fead6SMichael Halcrow {
71237fead6SMichael Halcrow 	int rc;
72237fead6SMichael Halcrow 
730216f7f7SMichael Halcrow 	rc = ecryptfs_encrypt_page(page);
74237fead6SMichael Halcrow 	if (rc) {
75237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error encrypting "
76237fead6SMichael Halcrow 				"page (upper index [0x%.16x])\n", page->index);
77237fead6SMichael Halcrow 		ClearPageUptodate(page);
78237fead6SMichael Halcrow 		goto out;
79237fead6SMichael Halcrow 	}
80237fead6SMichael Halcrow 	SetPageUptodate(page);
81237fead6SMichael Halcrow 	unlock_page(page);
82237fead6SMichael Halcrow out:
83237fead6SMichael Halcrow 	return rc;
84237fead6SMichael Halcrow }
85237fead6SMichael Halcrow 
86237fead6SMichael Halcrow /**
87e77a56ddSMichael Halcrow  *   Header Extent:
88e77a56ddSMichael Halcrow  *     Octets 0-7:        Unencrypted file size (big-endian)
89e77a56ddSMichael Halcrow  *     Octets 8-15:       eCryptfs special marker
90e77a56ddSMichael Halcrow  *     Octets 16-19:      Flags
91e77a56ddSMichael Halcrow  *      Octet 16:         File format version number (between 0 and 255)
92e77a56ddSMichael Halcrow  *      Octets 17-18:     Reserved
93e77a56ddSMichael Halcrow  *      Octet 19:         Bit 1 (lsb): Reserved
94e77a56ddSMichael Halcrow  *                        Bit 2: Encrypted?
95e77a56ddSMichael Halcrow  *                        Bits 3-8: Reserved
96e77a56ddSMichael Halcrow  *     Octets 20-23:      Header extent size (big-endian)
97e77a56ddSMichael Halcrow  *     Octets 24-25:      Number of header extents at front of file
98e77a56ddSMichael Halcrow  *                        (big-endian)
99e77a56ddSMichael Halcrow  *     Octet  26:         Begin RFC 2440 authentication token packet set
100e77a56ddSMichael Halcrow  */
101e77a56ddSMichael Halcrow static void set_header_info(char *page_virt,
102e77a56ddSMichael Halcrow 			    struct ecryptfs_crypt_stat *crypt_stat)
103e77a56ddSMichael Halcrow {
104e77a56ddSMichael Halcrow 	size_t written;
105e77a56ddSMichael Halcrow 	int save_num_header_extents_at_front =
106e77a56ddSMichael Halcrow 		crypt_stat->num_header_extents_at_front;
107e77a56ddSMichael Halcrow 
108e77a56ddSMichael Halcrow 	crypt_stat->num_header_extents_at_front = 1;
109e77a56ddSMichael Halcrow 	ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
110e77a56ddSMichael Halcrow 	crypt_stat->num_header_extents_at_front =
111e77a56ddSMichael Halcrow 		save_num_header_extents_at_front;
112e77a56ddSMichael Halcrow }
113237fead6SMichael Halcrow 
114237fead6SMichael Halcrow /**
115bf12be1cSMichael Halcrow  * ecryptfs_copy_up_encrypted_with_header
116bf12be1cSMichael Halcrow  * @page: Sort of a ``virtual'' representation of the encrypted lower
117bf12be1cSMichael Halcrow  *        file. The actual lower file does not have the metadata in
118bf12be1cSMichael Halcrow  *        the header. This is locked.
119bf12be1cSMichael Halcrow  * @crypt_stat: The eCryptfs inode's cryptographic context
120237fead6SMichael Halcrow  *
121bf12be1cSMichael Halcrow  * The ``view'' is the version of the file that userspace winds up
122bf12be1cSMichael Halcrow  * seeing, with the header information inserted.
123237fead6SMichael Halcrow  */
124bf12be1cSMichael Halcrow static int
125bf12be1cSMichael Halcrow ecryptfs_copy_up_encrypted_with_header(struct page *page,
126bf12be1cSMichael Halcrow 				       struct ecryptfs_crypt_stat *crypt_stat)
127237fead6SMichael Halcrow {
128bf12be1cSMichael Halcrow 	loff_t extent_num_in_page = 0;
129bf12be1cSMichael Halcrow 	loff_t num_extents_per_page = (PAGE_CACHE_SIZE
130bf12be1cSMichael Halcrow 				       / crypt_stat->extent_size);
131237fead6SMichael Halcrow 	int rc = 0;
132237fead6SMichael Halcrow 
133bf12be1cSMichael Halcrow 	while (extent_num_in_page < num_extents_per_page) {
134d6a13c17SMichael Halcrow 		loff_t view_extent_num = ((((loff_t)page->index)
135d6a13c17SMichael Halcrow 					   * num_extents_per_page)
136bf12be1cSMichael Halcrow 					  + extent_num_in_page);
137e77a56ddSMichael Halcrow 
138bf12be1cSMichael Halcrow 		if (view_extent_num < crypt_stat->num_header_extents_at_front) {
139bf12be1cSMichael Halcrow 			/* This is a header extent */
140e77a56ddSMichael Halcrow 			char *page_virt;
141e77a56ddSMichael Halcrow 
1429d8b8ce5SMichael Halcrow 			page_virt = kmap_atomic(page, KM_USER0);
143e77a56ddSMichael Halcrow 			memset(page_virt, 0, PAGE_CACHE_SIZE);
144bf12be1cSMichael Halcrow 			/* TODO: Support more than one header extent */
145bf12be1cSMichael Halcrow 			if (view_extent_num == 0) {
146e77a56ddSMichael Halcrow 				rc = ecryptfs_read_xattr_region(
147d7cdc5feSMichael Halcrow 					page_virt, page->mapping->host);
148e77a56ddSMichael Halcrow 				set_header_info(page_virt, crypt_stat);
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 "
154bf12be1cSMichael Halcrow 				       "region; rc = [%d]\n", __FUNCTION__, rc);
155e77a56ddSMichael Halcrow 				goto out;
156e77a56ddSMichael Halcrow 			}
157e77a56ddSMichael Halcrow 		} else {
158bf12be1cSMichael Halcrow 			/* This is an encrypted data extent */
159bf12be1cSMichael Halcrow 			loff_t lower_offset =
160bf12be1cSMichael Halcrow 				((view_extent_num -
161bf12be1cSMichael Halcrow 				  crypt_stat->num_header_extents_at_front)
162bf12be1cSMichael Halcrow 				 * crypt_stat->extent_size);
163bf12be1cSMichael Halcrow 
164bf12be1cSMichael Halcrow 			rc = ecryptfs_read_lower_page_segment(
165bf12be1cSMichael Halcrow 				page, (lower_offset >> PAGE_CACHE_SHIFT),
166bf12be1cSMichael Halcrow 				(lower_offset & ~PAGE_CACHE_MASK),
167bf12be1cSMichael Halcrow 				crypt_stat->extent_size, page->mapping->host);
168e77a56ddSMichael Halcrow 			if (rc) {
169bf12be1cSMichael Halcrow 				printk(KERN_ERR "%s: Error attempting to read "
170bf12be1cSMichael Halcrow 				       "extent at offset [%lld] in the lower "
171bf12be1cSMichael Halcrow 				       "file; rc = [%d]\n", __FUNCTION__,
172bf12be1cSMichael Halcrow 				       lower_offset, rc);
173e77a56ddSMichael Halcrow 				goto out;
174e77a56ddSMichael Halcrow 			}
175e77a56ddSMichael Halcrow 		}
176bf12be1cSMichael Halcrow 		extent_num_in_page++;
177bf12be1cSMichael Halcrow 	}
178bf12be1cSMichael Halcrow out:
179bf12be1cSMichael Halcrow 	return rc;
180bf12be1cSMichael Halcrow }
181bf12be1cSMichael Halcrow 
182bf12be1cSMichael Halcrow /**
183bf12be1cSMichael Halcrow  * ecryptfs_readpage
184bf12be1cSMichael Halcrow  * @file: An eCryptfs file
185bf12be1cSMichael Halcrow  * @page: Page from eCryptfs inode mapping into which to stick the read data
186bf12be1cSMichael Halcrow  *
187bf12be1cSMichael Halcrow  * Read in a page, decrypting if necessary.
188bf12be1cSMichael Halcrow  *
189bf12be1cSMichael Halcrow  * Returns zero on success; non-zero on error.
190bf12be1cSMichael Halcrow  */
191bf12be1cSMichael Halcrow static int ecryptfs_readpage(struct file *file, struct page *page)
192bf12be1cSMichael Halcrow {
193bf12be1cSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
194bf12be1cSMichael Halcrow 		&ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
195bf12be1cSMichael Halcrow 	int rc = 0;
196bf12be1cSMichael Halcrow 
197bf12be1cSMichael Halcrow 	if (!crypt_stat
198bf12be1cSMichael Halcrow 	    || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
199bf12be1cSMichael Halcrow 	    || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
200bf12be1cSMichael Halcrow 		ecryptfs_printk(KERN_DEBUG,
201bf12be1cSMichael Halcrow 				"Passing through unencrypted page\n");
202bf12be1cSMichael Halcrow 		rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
203bf12be1cSMichael Halcrow 						      PAGE_CACHE_SIZE,
204bf12be1cSMichael Halcrow 						      page->mapping->host);
205bf12be1cSMichael Halcrow 	} else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
206bf12be1cSMichael Halcrow 		if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
207bf12be1cSMichael Halcrow 			rc = ecryptfs_copy_up_encrypted_with_header(page,
208bf12be1cSMichael Halcrow 								    crypt_stat);
209bf12be1cSMichael Halcrow 			if (rc) {
210bf12be1cSMichael Halcrow 				printk(KERN_ERR "%s: Error attempting to copy "
211bf12be1cSMichael Halcrow 				       "the encrypted content from the lower "
212bf12be1cSMichael Halcrow 				       "file whilst inserting the metadata "
213bf12be1cSMichael Halcrow 				       "from the xattr into the header; rc = "
214bf12be1cSMichael Halcrow 				       "[%d]\n", __FUNCTION__, rc);
215bf12be1cSMichael Halcrow 				goto out;
216bf12be1cSMichael Halcrow 			}
217bf12be1cSMichael Halcrow 
218e77a56ddSMichael Halcrow 		} else {
219bf12be1cSMichael Halcrow 			rc = ecryptfs_read_lower_page_segment(
220bf12be1cSMichael Halcrow 				page, page->index, 0, PAGE_CACHE_SIZE,
221bf12be1cSMichael Halcrow 				page->mapping->host);
222e77a56ddSMichael Halcrow 			if (rc) {
223e77a56ddSMichael Halcrow 				printk(KERN_ERR "Error reading page; rc = "
224e77a56ddSMichael Halcrow 				       "[%d]\n", rc);
225e77a56ddSMichael Halcrow 				goto out;
226e77a56ddSMichael Halcrow 			}
227e77a56ddSMichael Halcrow 		}
228237fead6SMichael Halcrow 	} else {
2290216f7f7SMichael Halcrow 		rc = ecryptfs_decrypt_page(page);
230237fead6SMichael Halcrow 		if (rc) {
231237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error decrypting page; "
232237fead6SMichael Halcrow 					"rc = [%d]\n", rc);
233237fead6SMichael Halcrow 			goto out;
234237fead6SMichael Halcrow 		}
235237fead6SMichael Halcrow 	}
236237fead6SMichael Halcrow out:
23716a72c45SMichael Halcrow 	if (rc)
23816a72c45SMichael Halcrow 		ClearPageUptodate(page);
23916a72c45SMichael Halcrow 	else
24016a72c45SMichael Halcrow 		SetPageUptodate(page);
241237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
242237fead6SMichael Halcrow 			page->index);
243237fead6SMichael Halcrow 	unlock_page(page);
244237fead6SMichael Halcrow 	return rc;
245237fead6SMichael Halcrow }
246237fead6SMichael Halcrow 
247dd2a3b7aSMichael Halcrow /**
248dd2a3b7aSMichael Halcrow  * Called with lower inode mutex held.
249dd2a3b7aSMichael Halcrow  */
250237fead6SMichael Halcrow static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
251237fead6SMichael Halcrow {
252237fead6SMichael Halcrow 	struct inode *inode = page->mapping->host;
253237fead6SMichael Halcrow 	int end_byte_in_page;
254237fead6SMichael Halcrow 
2559d8b8ce5SMichael Halcrow 	if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
2569d8b8ce5SMichael Halcrow 		goto out;
257237fead6SMichael Halcrow 	end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
258237fead6SMichael Halcrow 	if (to > end_byte_in_page)
259237fead6SMichael Halcrow 		end_byte_in_page = to;
260*eebd2aa3SChristoph Lameter 	zero_user_segment(page, end_byte_in_page, PAGE_CACHE_SIZE);
261237fead6SMichael Halcrow out:
2629d8b8ce5SMichael Halcrow 	return 0;
263237fead6SMichael Halcrow }
264237fead6SMichael Halcrow 
2657a3f595cSEric Sandeen /* This function must zero any hole we create */
26653a2731fSMichael Halcrow static int ecryptfs_prepare_write(struct file *file, struct page *page,
26753a2731fSMichael Halcrow 				  unsigned from, unsigned to)
26853a2731fSMichael Halcrow {
26953a2731fSMichael Halcrow 	int rc = 0;
2707a3f595cSEric Sandeen 	loff_t prev_page_end_size;
27153a2731fSMichael Halcrow 
27216a72c45SMichael Halcrow 	if (!PageUptodate(page)) {
273bf12be1cSMichael Halcrow 		rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
274bf12be1cSMichael Halcrow 						      PAGE_CACHE_SIZE,
275bf12be1cSMichael Halcrow 						      page->mapping->host);
27616a72c45SMichael Halcrow 		if (rc) {
27716a72c45SMichael Halcrow 			printk(KERN_ERR "%s: Error attemping to read lower "
27816a72c45SMichael Halcrow 			       "page segment; rc = [%d]\n", __FUNCTION__, rc);
27916a72c45SMichael Halcrow 			ClearPageUptodate(page);
28016a72c45SMichael Halcrow 			goto out;
28116a72c45SMichael Halcrow 		} else
28216a72c45SMichael Halcrow 			SetPageUptodate(page);
28316a72c45SMichael Halcrow 	}
284240e2df5SMichael Halcrow 
2857a3f595cSEric Sandeen 	prev_page_end_size = ((loff_t)page->index << PAGE_CACHE_SHIFT);
2867a3f595cSEric Sandeen 
2877a3f595cSEric Sandeen 	/*
2887a3f595cSEric Sandeen 	 * If creating a page or more of holes, zero them out via truncate.
2897a3f595cSEric Sandeen 	 * Note, this will increase i_size.
2907a3f595cSEric Sandeen 	 */
2917a3f595cSEric Sandeen 	if (page->index != 0) {
2927a3f595cSEric Sandeen 		if (prev_page_end_size > i_size_read(page->mapping->host)) {
293240e2df5SMichael Halcrow 			rc = ecryptfs_truncate(file->f_path.dentry,
2947a3f595cSEric Sandeen 					       prev_page_end_size);
29553a2731fSMichael Halcrow 			if (rc) {
29653a2731fSMichael Halcrow 				printk(KERN_ERR "Error on attempt to "
29753a2731fSMichael Halcrow 				       "truncate to (higher) offset [%lld];"
2987a3f595cSEric Sandeen 				       " rc = [%d]\n", prev_page_end_size, rc);
29953a2731fSMichael Halcrow 				goto out;
30053a2731fSMichael Halcrow 			}
30153a2731fSMichael Halcrow 		}
3027a3f595cSEric Sandeen 	}
3037a3f595cSEric Sandeen 	/*
3047a3f595cSEric Sandeen 	 * Writing to a new page, and creating a small hole from start of page?
3057a3f595cSEric Sandeen 	 * Zero it out.
3067a3f595cSEric Sandeen 	 */
3077a3f595cSEric Sandeen 	if ((i_size_read(page->mapping->host) == prev_page_end_size) &&
3087a3f595cSEric Sandeen 	    (from != 0)) {
309*eebd2aa3SChristoph Lameter 		zero_user(page, 0, PAGE_CACHE_SIZE);
310240e2df5SMichael Halcrow 	}
31153a2731fSMichael Halcrow out:
31253a2731fSMichael Halcrow 	return rc;
31353a2731fSMichael Halcrow }
31453a2731fSMichael Halcrow 
315237fead6SMichael Halcrow /**
316237fead6SMichael Halcrow  * ecryptfs_write_inode_size_to_header
317237fead6SMichael Halcrow  *
318237fead6SMichael Halcrow  * Writes the lower file size to the first 8 bytes of the header.
319237fead6SMichael Halcrow  *
320237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
321237fead6SMichael Halcrow  */
3220216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
323237fead6SMichael Halcrow {
324237fead6SMichael Halcrow 	u64 file_size;
3250216f7f7SMichael Halcrow 	char *file_size_virt;
3260216f7f7SMichael Halcrow 	int rc;
327237fead6SMichael Halcrow 
3280216f7f7SMichael Halcrow 	file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
3290216f7f7SMichael Halcrow 	if (!file_size_virt) {
3300216f7f7SMichael Halcrow 		rc = -ENOMEM;
331237fead6SMichael Halcrow 		goto out;
332237fead6SMichael Halcrow 	}
3330216f7f7SMichael Halcrow 	file_size = (u64)i_size_read(ecryptfs_inode);
334237fead6SMichael Halcrow 	file_size = cpu_to_be64(file_size);
3350216f7f7SMichael Halcrow 	memcpy(file_size_virt, &file_size, sizeof(u64));
3360216f7f7SMichael Halcrow 	rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
3370216f7f7SMichael Halcrow 				  sizeof(u64));
3380216f7f7SMichael Halcrow 	kfree(file_size_virt);
3390216f7f7SMichael Halcrow 	if (rc)
3400216f7f7SMichael Halcrow 		printk(KERN_ERR "%s: Error writing file size to header; "
3410216f7f7SMichael Halcrow 		       "rc = [%d]\n", __FUNCTION__, rc);
342237fead6SMichael Halcrow out:
343237fead6SMichael Halcrow 	return rc;
344237fead6SMichael Halcrow }
345237fead6SMichael Halcrow 
3460216f7f7SMichael Halcrow struct kmem_cache *ecryptfs_xattr_cache;
3470216f7f7SMichael Halcrow 
3480216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
349dd2a3b7aSMichael Halcrow {
350dd2a3b7aSMichael Halcrow 	ssize_t size;
351dd2a3b7aSMichael Halcrow 	void *xattr_virt;
3520216f7f7SMichael Halcrow 	struct dentry *lower_dentry =
3530216f7f7SMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
3540216f7f7SMichael Halcrow 	struct inode *lower_inode = lower_dentry->d_inode;
355dd2a3b7aSMichael Halcrow 	u64 file_size;
356dd2a3b7aSMichael Halcrow 	int rc;
357dd2a3b7aSMichael Halcrow 
3580216f7f7SMichael Halcrow 	if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
3590216f7f7SMichael Halcrow 		printk(KERN_WARNING
3600216f7f7SMichael Halcrow 		       "No support for setting xattr in lower filesystem\n");
3610216f7f7SMichael Halcrow 		rc = -ENOSYS;
3620216f7f7SMichael Halcrow 		goto out;
3630216f7f7SMichael Halcrow 	}
364dd2a3b7aSMichael Halcrow 	xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
365dd2a3b7aSMichael Halcrow 	if (!xattr_virt) {
366dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Out of memory whilst attempting to write "
367dd2a3b7aSMichael Halcrow 		       "inode size to xattr\n");
368dd2a3b7aSMichael Halcrow 		rc = -ENOMEM;
369dd2a3b7aSMichael Halcrow 		goto out;
370dd2a3b7aSMichael Halcrow 	}
3710216f7f7SMichael Halcrow 	mutex_lock(&lower_inode->i_mutex);
3720216f7f7SMichael Halcrow 	size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
3730216f7f7SMichael Halcrow 					   xattr_virt, PAGE_CACHE_SIZE);
374dd2a3b7aSMichael Halcrow 	if (size < 0)
375dd2a3b7aSMichael Halcrow 		size = 8;
3760216f7f7SMichael Halcrow 	file_size = (u64)i_size_read(ecryptfs_inode);
377dd2a3b7aSMichael Halcrow 	file_size = cpu_to_be64(file_size);
378dd2a3b7aSMichael Halcrow 	memcpy(xattr_virt, &file_size, sizeof(u64));
3790216f7f7SMichael Halcrow 	rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
380dd2a3b7aSMichael Halcrow 					 xattr_virt, size, 0);
3810216f7f7SMichael Halcrow 	mutex_unlock(&lower_inode->i_mutex);
382dd2a3b7aSMichael Halcrow 	if (rc)
383dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error whilst attempting to write inode size "
384dd2a3b7aSMichael Halcrow 		       "to lower file xattr; rc = [%d]\n", rc);
385dd2a3b7aSMichael Halcrow 	kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
386dd2a3b7aSMichael Halcrow out:
387dd2a3b7aSMichael Halcrow 	return rc;
388dd2a3b7aSMichael Halcrow }
389dd2a3b7aSMichael Halcrow 
3900216f7f7SMichael Halcrow int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
391dd2a3b7aSMichael Halcrow {
392dd2a3b7aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
393dd2a3b7aSMichael Halcrow 
3940216f7f7SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
395dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
3960216f7f7SMichael Halcrow 		return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
397dd2a3b7aSMichael Halcrow 	else
3980216f7f7SMichael Halcrow 		return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
399dd2a3b7aSMichael Halcrow }
400dd2a3b7aSMichael Halcrow 
401237fead6SMichael Halcrow /**
402237fead6SMichael Halcrow  * ecryptfs_commit_write
403237fead6SMichael Halcrow  * @file: The eCryptfs file object
404237fead6SMichael Halcrow  * @page: The eCryptfs page
405237fead6SMichael Halcrow  * @from: Ignored (we rotate the page IV on each write)
406237fead6SMichael Halcrow  * @to: Ignored
407237fead6SMichael Halcrow  *
408237fead6SMichael Halcrow  * This is where we encrypt the data and pass the encrypted data to
409237fead6SMichael Halcrow  * the lower filesystem.  In OpenPGP-compatible mode, we operate on
410237fead6SMichael Halcrow  * entire underlying packets.
411237fead6SMichael Halcrow  */
412237fead6SMichael Halcrow static int ecryptfs_commit_write(struct file *file, struct page *page,
413237fead6SMichael Halcrow 				 unsigned from, unsigned to)
414237fead6SMichael Halcrow {
415237fead6SMichael Halcrow 	loff_t pos;
416bf12be1cSMichael Halcrow 	struct inode *ecryptfs_inode = page->mapping->host;
417bf12be1cSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
418bf12be1cSMichael Halcrow 		&ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
419237fead6SMichael Halcrow 	int rc;
420237fead6SMichael Halcrow 
421e2bd99ecSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
422237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
423237fead6SMichael Halcrow 			"crypt_stat at memory location [%p]\n", crypt_stat);
424e2bd99ecSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
425237fead6SMichael Halcrow 	} else
426237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
427237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
428237fead6SMichael Halcrow 			"(page w/ index = [0x%.16x], to = [%d])\n", page->index,
429237fead6SMichael Halcrow 			to);
430bf12be1cSMichael Halcrow 	/* Fills in zeros if 'to' goes beyond inode size */
431237fead6SMichael Halcrow 	rc = fill_zeros_to_end_of_page(page, to);
432237fead6SMichael Halcrow 	if (rc) {
433237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
434237fead6SMichael Halcrow 				"zeros in page with index = [0x%.16x]\n",
435237fead6SMichael Halcrow 				page->index);
436237fead6SMichael Halcrow 		goto out;
437237fead6SMichael Halcrow 	}
4380216f7f7SMichael Halcrow 	rc = ecryptfs_encrypt_page(page);
439237fead6SMichael Halcrow 	if (rc) {
440237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
441237fead6SMichael Halcrow 				"index [0x%.16x])\n", page->index);
442237fead6SMichael Halcrow 		goto out;
443237fead6SMichael Halcrow 	}
444d6a13c17SMichael Halcrow 	pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to;
445bf12be1cSMichael Halcrow 	if (pos > i_size_read(ecryptfs_inode)) {
446bf12be1cSMichael Halcrow 		i_size_write(ecryptfs_inode, pos);
447237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
448bf12be1cSMichael Halcrow 				"[0x%.16x]\n", i_size_read(ecryptfs_inode));
449237fead6SMichael Halcrow 	}
450bf12be1cSMichael Halcrow 	rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
451dd2a3b7aSMichael Halcrow 	if (rc)
452dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error writing inode size to metadata; "
453dd2a3b7aSMichael Halcrow 		       "rc = [%d]\n", rc);
454237fead6SMichael Halcrow out:
455237fead6SMichael Halcrow 	return rc;
456237fead6SMichael Halcrow }
457237fead6SMichael Halcrow 
458237fead6SMichael Halcrow static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
459237fead6SMichael Halcrow {
460237fead6SMichael Halcrow 	int rc = 0;
461237fead6SMichael Halcrow 	struct inode *inode;
462237fead6SMichael Halcrow 	struct inode *lower_inode;
463237fead6SMichael Halcrow 
464237fead6SMichael Halcrow 	inode = (struct inode *)mapping->host;
465237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
466237fead6SMichael Halcrow 	if (lower_inode->i_mapping->a_ops->bmap)
467237fead6SMichael Halcrow 		rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
468237fead6SMichael Halcrow 							 block);
469237fead6SMichael Halcrow 	return rc;
470237fead6SMichael Halcrow }
471237fead6SMichael Halcrow 
472237fead6SMichael Halcrow struct address_space_operations ecryptfs_aops = {
473237fead6SMichael Halcrow 	.writepage = ecryptfs_writepage,
474237fead6SMichael Halcrow 	.readpage = ecryptfs_readpage,
475237fead6SMichael Halcrow 	.prepare_write = ecryptfs_prepare_write,
476237fead6SMichael Halcrow 	.commit_write = ecryptfs_commit_write,
477237fead6SMichael Halcrow 	.bmap = ecryptfs_bmap,
478237fead6SMichael Halcrow };
479