blob: dc74b186145d74db8a56470d5ef2e63ea5c0a3c0 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 * This is where eCryptfs coordinates the symmetric encryption and
4 * decryption of the file data as it passes between the lower
5 * encrypted file and the upper decrypted file.
6 *
7 * Copyright (C) 1997-2003 Erez Zadok
8 * Copyright (C) 2001-2003 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08009 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -070010 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA.
26 */
27
28#include <linux/pagemap.h>
29#include <linux/writeback.h>
30#include <linux/page-flags.h>
31#include <linux/mount.h>
32#include <linux/file.h>
33#include <linux/crypto.h>
34#include <linux/scatterlist.h>
35#include "ecryptfs_kernel.h"
36
Michael Halcrow237fead2006-10-04 02:16:22 -070037/**
Michael Halcrow16a72c42007-10-16 01:28:14 -070038 * ecryptfs_get_locked_page
Michael Halcrow237fead2006-10-04 02:16:22 -070039 *
40 * Get one page from cache or lower f/s, return error otherwise.
41 *
Michael Halcrow16a72c42007-10-16 01:28:14 -070042 * Returns locked and up-to-date page (if ok), with increased
Michael Halcrow237fead2006-10-04 02:16:22 -070043 * refcnt.
44 */
Michael Halcrow16a72c42007-10-16 01:28:14 -070045struct page *ecryptfs_get_locked_page(struct file *file, loff_t index)
Michael Halcrow237fead2006-10-04 02:16:22 -070046{
Michael Halcrow237fead2006-10-04 02:16:22 -070047 struct dentry *dentry;
48 struct inode *inode;
49 struct address_space *mapping;
Michael Halcrow16a72c42007-10-16 01:28:14 -070050 struct page *page;
Michael Halcrow237fead2006-10-04 02:16:22 -070051
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -080052 dentry = file->f_path.dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -070053 inode = dentry->d_inode;
54 mapping = inode->i_mapping;
Michael Halcrow16a72c42007-10-16 01:28:14 -070055 page = read_mapping_page(mapping, index, (void *)file);
56 if (!IS_ERR(page))
57 lock_page(page);
58 return page;
Michael Halcrow237fead2006-10-04 02:16:22 -070059}
60
Michael Halcrow237fead2006-10-04 02:16:22 -070061/**
Michael Halcrow237fead2006-10-04 02:16:22 -070062 * ecryptfs_writepage
63 * @page: Page that is locked before this call is made
64 *
65 * Returns zero on success; non-zero otherwise
66 */
67static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
68{
Michael Halcrow237fead2006-10-04 02:16:22 -070069 int rc;
70
Michael Halcrow0216f7f2007-10-16 01:28:08 -070071 rc = ecryptfs_encrypt_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -070072 if (rc) {
73 ecryptfs_printk(KERN_WARNING, "Error encrypting "
74 "page (upper index [0x%.16x])\n", page->index);
75 ClearPageUptodate(page);
76 goto out;
77 }
78 SetPageUptodate(page);
79 unlock_page(page);
80out:
81 return rc;
82}
83
84/**
Michael Halcrowe77a56d2007-02-12 00:53:47 -080085 * Header Extent:
86 * Octets 0-7: Unencrypted file size (big-endian)
87 * Octets 8-15: eCryptfs special marker
88 * Octets 16-19: Flags
89 * Octet 16: File format version number (between 0 and 255)
90 * Octets 17-18: Reserved
91 * Octet 19: Bit 1 (lsb): Reserved
92 * Bit 2: Encrypted?
93 * Bits 3-8: Reserved
94 * Octets 20-23: Header extent size (big-endian)
95 * Octets 24-25: Number of header extents at front of file
96 * (big-endian)
97 * Octet 26: Begin RFC 2440 authentication token packet set
98 */
99static void set_header_info(char *page_virt,
100 struct ecryptfs_crypt_stat *crypt_stat)
101{
102 size_t written;
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800103 size_t save_num_header_bytes_at_front =
104 crypt_stat->num_header_bytes_at_front;
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800105
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800106 crypt_stat->num_header_bytes_at_front =
107 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800108 ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800109 crypt_stat->num_header_bytes_at_front =
110 save_num_header_bytes_at_front;
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800111}
Michael Halcrow237fead2006-10-04 02:16:22 -0700112
113/**
Michael Halcrowbf12be12007-10-16 01:28:11 -0700114 * ecryptfs_copy_up_encrypted_with_header
115 * @page: Sort of a ``virtual'' representation of the encrypted lower
116 * file. The actual lower file does not have the metadata in
117 * the header. This is locked.
118 * @crypt_stat: The eCryptfs inode's cryptographic context
119 *
120 * The ``view'' is the version of the file that userspace winds up
121 * seeing, with the header information inserted.
122 */
123static int
124ecryptfs_copy_up_encrypted_with_header(struct page *page,
125 struct ecryptfs_crypt_stat *crypt_stat)
126{
127 loff_t extent_num_in_page = 0;
128 loff_t num_extents_per_page = (PAGE_CACHE_SIZE
129 / crypt_stat->extent_size);
130 int rc = 0;
131
132 while (extent_num_in_page < num_extents_per_page) {
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700133 loff_t view_extent_num = ((((loff_t)page->index)
134 * num_extents_per_page)
Michael Halcrowbf12be12007-10-16 01:28:11 -0700135 + extent_num_in_page);
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800136 size_t num_header_extents_at_front =
137 (crypt_stat->num_header_bytes_at_front
138 / crypt_stat->extent_size);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700139
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800140 if (view_extent_num < num_header_extents_at_front) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700141 /* This is a header extent */
142 char *page_virt;
143
144 page_virt = kmap_atomic(page, KM_USER0);
145 memset(page_virt, 0, PAGE_CACHE_SIZE);
146 /* TODO: Support more than one header extent */
147 if (view_extent_num == 0) {
148 rc = ecryptfs_read_xattr_region(
149 page_virt, page->mapping->host);
150 set_header_info(page_virt, crypt_stat);
151 }
152 kunmap_atomic(page_virt, KM_USER0);
153 flush_dcache_page(page);
154 if (rc) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700155 printk(KERN_ERR "%s: Error reading xattr "
156 "region; rc = [%d]\n", __FUNCTION__, rc);
157 goto out;
158 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700159 } else {
160 /* This is an encrypted data extent */
161 loff_t lower_offset =
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800162 ((view_extent_num * crypt_stat->extent_size)
163 - crypt_stat->num_header_bytes_at_front);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700164
165 rc = ecryptfs_read_lower_page_segment(
166 page, (lower_offset >> PAGE_CACHE_SHIFT),
167 (lower_offset & ~PAGE_CACHE_MASK),
168 crypt_stat->extent_size, page->mapping->host);
169 if (rc) {
170 printk(KERN_ERR "%s: Error attempting to read "
171 "extent at offset [%lld] in the lower "
172 "file; rc = [%d]\n", __FUNCTION__,
173 lower_offset, rc);
174 goto out;
175 }
176 }
177 extent_num_in_page++;
178 }
179out:
180 return rc;
181}
182
183/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700184 * ecryptfs_readpage
Michael Halcrowbf12be12007-10-16 01:28:11 -0700185 * @file: An eCryptfs file
186 * @page: Page from eCryptfs inode mapping into which to stick the read data
Michael Halcrow237fead2006-10-04 02:16:22 -0700187 *
188 * Read in a page, decrypting if necessary.
189 *
190 * Returns zero on success; non-zero on error.
191 */
192static int ecryptfs_readpage(struct file *file, struct page *page)
193{
Michael Halcrowbf12be12007-10-16 01:28:11 -0700194 struct ecryptfs_crypt_stat *crypt_stat =
195 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700196 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700197
Michael Halcrow237fead2006-10-04 02:16:22 -0700198 if (!crypt_stat
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800199 || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
200 || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700201 ecryptfs_printk(KERN_DEBUG,
202 "Passing through unencrypted page\n");
Michael Halcrowbf12be12007-10-16 01:28:11 -0700203 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
204 PAGE_CACHE_SIZE,
205 page->mapping->host);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800206 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
207 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700208 rc = ecryptfs_copy_up_encrypted_with_header(page,
209 crypt_stat);
210 if (rc) {
211 printk(KERN_ERR "%s: Error attempting to copy "
212 "the encrypted content from the lower "
213 "file whilst inserting the metadata "
214 "from the xattr into the header; rc = "
215 "[%d]\n", __FUNCTION__, rc);
216 goto out;
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800217 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700218
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800219 } else {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700220 rc = ecryptfs_read_lower_page_segment(
221 page, page->index, 0, PAGE_CACHE_SIZE,
222 page->mapping->host);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800223 if (rc) {
224 printk(KERN_ERR "Error reading page; rc = "
225 "[%d]\n", rc);
226 goto out;
227 }
228 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700229 } else {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700230 rc = ecryptfs_decrypt_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700231 if (rc) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700232 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
233 "rc = [%d]\n", rc);
234 goto out;
235 }
236 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700237out:
Michael Halcrow16a72c42007-10-16 01:28:14 -0700238 if (rc)
239 ClearPageUptodate(page);
240 else
241 SetPageUptodate(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700242 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
243 page->index);
244 unlock_page(page);
245 return rc;
246}
247
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800248/**
249 * Called with lower inode mutex held.
250 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700251static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
252{
253 struct inode *inode = page->mapping->host;
254 int end_byte_in_page;
Michael Halcrow237fead2006-10-04 02:16:22 -0700255
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800256 if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
257 goto out;
258 end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
259 if (to > end_byte_in_page)
260 end_byte_in_page = to;
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800261 zero_user_segment(page, end_byte_in_page, PAGE_CACHE_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700262out:
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800263 return 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700264}
265
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800266/* This function must zero any hole we create */
Michael Halcrow53a27312007-05-23 13:58:15 -0700267static int ecryptfs_prepare_write(struct file *file, struct page *page,
268 unsigned from, unsigned to)
269{
Michael Halcrow53a27312007-05-23 13:58:15 -0700270 int rc = 0;
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800271 loff_t prev_page_end_size;
Michael Halcrow53a27312007-05-23 13:58:15 -0700272
Michael Halcrow16a72c42007-10-16 01:28:14 -0700273 if (!PageUptodate(page)) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700274 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
275 PAGE_CACHE_SIZE,
276 page->mapping->host);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700277 if (rc) {
278 printk(KERN_ERR "%s: Error attemping to read lower "
279 "page segment; rc = [%d]\n", __FUNCTION__, rc);
280 ClearPageUptodate(page);
281 goto out;
282 } else
283 SetPageUptodate(page);
284 }
Michael Halcrow240e2df2007-06-27 14:09:44 -0700285
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800286 prev_page_end_size = ((loff_t)page->index << PAGE_CACHE_SHIFT);
287
288 /*
289 * If creating a page or more of holes, zero them out via truncate.
290 * Note, this will increase i_size.
291 */
292 if (page->index != 0) {
293 if (prev_page_end_size > i_size_read(page->mapping->host)) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700294 rc = ecryptfs_truncate(file->f_path.dentry,
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800295 prev_page_end_size);
Michael Halcrow240e2df2007-06-27 14:09:44 -0700296 if (rc) {
297 printk(KERN_ERR "Error on attempt to "
298 "truncate to (higher) offset [%lld];"
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800299 " rc = [%d]\n", prev_page_end_size, rc);
Michael Halcrow240e2df2007-06-27 14:09:44 -0700300 goto out;
301 }
Michael Halcrow53a27312007-05-23 13:58:15 -0700302 }
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800303 }
304 /*
305 * Writing to a new page, and creating a small hole from start of page?
306 * Zero it out.
307 */
308 if ((i_size_read(page->mapping->host) == prev_page_end_size) &&
309 (from != 0)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800310 zero_user(page, 0, PAGE_CACHE_SIZE);
Michael Halcrow53a27312007-05-23 13:58:15 -0700311 }
312out:
313 return rc;
314}
315
Michael Halcrow237fead2006-10-04 02:16:22 -0700316/**
317 * ecryptfs_write_inode_size_to_header
318 *
319 * Writes the lower file size to the first 8 bytes of the header.
320 *
321 * Returns zero on success; non-zero on error.
322 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700323static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700324{
Michael Halcrow237fead2006-10-04 02:16:22 -0700325 u64 file_size;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700326 char *file_size_virt;
327 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700328
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700329 file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
330 if (!file_size_virt) {
331 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700332 goto out;
333 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700334 file_size = (u64)i_size_read(ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700335 file_size = cpu_to_be64(file_size);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700336 memcpy(file_size_virt, &file_size, sizeof(u64));
337 rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
338 sizeof(u64));
339 kfree(file_size_virt);
340 if (rc)
341 printk(KERN_ERR "%s: Error writing file size to header; "
342 "rc = [%d]\n", __FUNCTION__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700343out:
344 return rc;
345}
346
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700347struct kmem_cache *ecryptfs_xattr_cache;
348
349static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800350{
351 ssize_t size;
352 void *xattr_virt;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700353 struct dentry *lower_dentry =
354 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
355 struct inode *lower_inode = lower_dentry->d_inode;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800356 u64 file_size;
357 int rc;
358
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700359 if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
360 printk(KERN_WARNING
361 "No support for setting xattr in lower filesystem\n");
362 rc = -ENOSYS;
363 goto out;
364 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800365 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
366 if (!xattr_virt) {
367 printk(KERN_ERR "Out of memory whilst attempting to write "
368 "inode size to xattr\n");
369 rc = -ENOMEM;
370 goto out;
371 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700372 mutex_lock(&lower_inode->i_mutex);
373 size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
374 xattr_virt, PAGE_CACHE_SIZE);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800375 if (size < 0)
376 size = 8;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700377 file_size = (u64)i_size_read(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800378 file_size = cpu_to_be64(file_size);
379 memcpy(xattr_virt, &file_size, sizeof(u64));
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700380 rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
381 xattr_virt, size, 0);
382 mutex_unlock(&lower_inode->i_mutex);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800383 if (rc)
384 printk(KERN_ERR "Error whilst attempting to write inode size "
385 "to lower file xattr; rc = [%d]\n", rc);
386 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
387out:
388 return rc;
389}
390
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700391int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800392{
393 struct ecryptfs_crypt_stat *crypt_stat;
394
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700395 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800396 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700397 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800398 else
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700399 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800400}
401
Michael Halcrow237fead2006-10-04 02:16:22 -0700402/**
403 * ecryptfs_commit_write
404 * @file: The eCryptfs file object
405 * @page: The eCryptfs page
406 * @from: Ignored (we rotate the page IV on each write)
407 * @to: Ignored
408 *
409 * This is where we encrypt the data and pass the encrypted data to
410 * the lower filesystem. In OpenPGP-compatible mode, we operate on
411 * entire underlying packets.
412 */
413static int ecryptfs_commit_write(struct file *file, struct page *page,
414 unsigned from, unsigned to)
415{
Michael Halcrow237fead2006-10-04 02:16:22 -0700416 loff_t pos;
Michael Halcrowbf12be12007-10-16 01:28:11 -0700417 struct inode *ecryptfs_inode = page->mapping->host;
418 struct ecryptfs_crypt_stat *crypt_stat =
419 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700420 int rc;
421
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800422 if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700423 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
424 "crypt_stat at memory location [%p]\n", crypt_stat);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800425 crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700426 } else
427 ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
428 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
429 "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
430 to);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700431 /* Fills in zeros if 'to' goes beyond inode size */
Michael Halcrow237fead2006-10-04 02:16:22 -0700432 rc = fill_zeros_to_end_of_page(page, to);
433 if (rc) {
434 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
435 "zeros in page with index = [0x%.16x]\n",
436 page->index);
437 goto out;
438 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700439 rc = ecryptfs_encrypt_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700440 if (rc) {
441 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
442 "index [0x%.16x])\n", page->index);
443 goto out;
444 }
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700445 pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to;
Michael Halcrowbf12be12007-10-16 01:28:11 -0700446 if (pos > i_size_read(ecryptfs_inode)) {
447 i_size_write(ecryptfs_inode, pos);
Michael Halcrow237fead2006-10-04 02:16:22 -0700448 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
Michael Halcrowbf12be12007-10-16 01:28:11 -0700449 "[0x%.16x]\n", i_size_read(ecryptfs_inode));
Michael Halcrow237fead2006-10-04 02:16:22 -0700450 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700451 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800452 if (rc)
453 printk(KERN_ERR "Error writing inode size to metadata; "
454 "rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700455out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700456 return rc;
457}
458
Michael Halcrow237fead2006-10-04 02:16:22 -0700459static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
460{
461 int rc = 0;
462 struct inode *inode;
463 struct inode *lower_inode;
464
465 inode = (struct inode *)mapping->host;
466 lower_inode = ecryptfs_inode_to_lower(inode);
467 if (lower_inode->i_mapping->a_ops->bmap)
468 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
469 block);
470 return rc;
471}
472
Michael Halcrow237fead2006-10-04 02:16:22 -0700473struct address_space_operations ecryptfs_aops = {
474 .writepage = ecryptfs_writepage,
475 .readpage = ecryptfs_readpage,
476 .prepare_write = ecryptfs_prepare_write,
477 .commit_write = ecryptfs_commit_write,
478 .bmap = ecryptfs_bmap,
Michael Halcrow237fead2006-10-04 02:16:22 -0700479};