blob: 9a5e0d17f1c5f03e08e12662688729758138dfa6 [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;
103 int save_num_header_extents_at_front =
104 crypt_stat->num_header_extents_at_front;
105
106 crypt_stat->num_header_extents_at_front = 1;
107 ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
108 crypt_stat->num_header_extents_at_front =
109 save_num_header_extents_at_front;
110}
Michael Halcrow237fead2006-10-04 02:16:22 -0700111
112/**
Michael Halcrowbf12be12007-10-16 01:28:11 -0700113 * ecryptfs_copy_up_encrypted_with_header
114 * @page: Sort of a ``virtual'' representation of the encrypted lower
115 * file. The actual lower file does not have the metadata in
116 * the header. This is locked.
117 * @crypt_stat: The eCryptfs inode's cryptographic context
118 *
119 * The ``view'' is the version of the file that userspace winds up
120 * seeing, with the header information inserted.
121 */
122static int
123ecryptfs_copy_up_encrypted_with_header(struct page *page,
124 struct ecryptfs_crypt_stat *crypt_stat)
125{
126 loff_t extent_num_in_page = 0;
127 loff_t num_extents_per_page = (PAGE_CACHE_SIZE
128 / crypt_stat->extent_size);
129 int rc = 0;
130
131 while (extent_num_in_page < num_extents_per_page) {
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700132 loff_t view_extent_num = ((((loff_t)page->index)
133 * num_extents_per_page)
Michael Halcrowbf12be12007-10-16 01:28:11 -0700134 + extent_num_in_page);
135
136 if (view_extent_num < crypt_stat->num_header_extents_at_front) {
137 /* This is a header extent */
138 char *page_virt;
139
140 page_virt = kmap_atomic(page, KM_USER0);
141 memset(page_virt, 0, PAGE_CACHE_SIZE);
142 /* TODO: Support more than one header extent */
143 if (view_extent_num == 0) {
144 rc = ecryptfs_read_xattr_region(
145 page_virt, page->mapping->host);
146 set_header_info(page_virt, crypt_stat);
147 }
148 kunmap_atomic(page_virt, KM_USER0);
149 flush_dcache_page(page);
150 if (rc) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700151 printk(KERN_ERR "%s: Error reading xattr "
152 "region; rc = [%d]\n", __FUNCTION__, rc);
153 goto out;
154 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700155 } else {
156 /* This is an encrypted data extent */
157 loff_t lower_offset =
158 ((view_extent_num -
159 crypt_stat->num_header_extents_at_front)
160 * crypt_stat->extent_size);
161
162 rc = ecryptfs_read_lower_page_segment(
163 page, (lower_offset >> PAGE_CACHE_SHIFT),
164 (lower_offset & ~PAGE_CACHE_MASK),
165 crypt_stat->extent_size, page->mapping->host);
166 if (rc) {
167 printk(KERN_ERR "%s: Error attempting to read "
168 "extent at offset [%lld] in the lower "
169 "file; rc = [%d]\n", __FUNCTION__,
170 lower_offset, rc);
171 goto out;
172 }
173 }
174 extent_num_in_page++;
175 }
176out:
177 return rc;
178}
179
180/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700181 * ecryptfs_readpage
Michael Halcrowbf12be12007-10-16 01:28:11 -0700182 * @file: An eCryptfs file
183 * @page: Page from eCryptfs inode mapping into which to stick the read data
Michael Halcrow237fead2006-10-04 02:16:22 -0700184 *
185 * Read in a page, decrypting if necessary.
186 *
187 * Returns zero on success; non-zero on error.
188 */
189static int ecryptfs_readpage(struct file *file, struct page *page)
190{
Michael Halcrowbf12be12007-10-16 01:28:11 -0700191 struct ecryptfs_crypt_stat *crypt_stat =
192 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700193 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700194
Michael Halcrow237fead2006-10-04 02:16:22 -0700195 if (!crypt_stat
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800196 || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
197 || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700198 ecryptfs_printk(KERN_DEBUG,
199 "Passing through unencrypted page\n");
Michael Halcrowbf12be12007-10-16 01:28:11 -0700200 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
201 PAGE_CACHE_SIZE,
202 page->mapping->host);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800203 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
204 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700205 rc = ecryptfs_copy_up_encrypted_with_header(page,
206 crypt_stat);
207 if (rc) {
208 printk(KERN_ERR "%s: Error attempting to copy "
209 "the encrypted content from the lower "
210 "file whilst inserting the metadata "
211 "from the xattr into the header; rc = "
212 "[%d]\n", __FUNCTION__, rc);
213 goto out;
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800214 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700215
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800216 } else {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700217 rc = ecryptfs_read_lower_page_segment(
218 page, page->index, 0, PAGE_CACHE_SIZE,
219 page->mapping->host);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800220 if (rc) {
221 printk(KERN_ERR "Error reading page; rc = "
222 "[%d]\n", rc);
223 goto out;
224 }
225 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700226 } else {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700227 rc = ecryptfs_decrypt_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700228 if (rc) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700229 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
230 "rc = [%d]\n", rc);
231 goto out;
232 }
233 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700234out:
Michael Halcrow16a72c42007-10-16 01:28:14 -0700235 if (rc)
236 ClearPageUptodate(page);
237 else
238 SetPageUptodate(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700239 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
240 page->index);
241 unlock_page(page);
242 return rc;
243}
244
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800245/**
246 * Called with lower inode mutex held.
247 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700248static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
249{
250 struct inode *inode = page->mapping->host;
251 int end_byte_in_page;
Michael Halcrow237fead2006-10-04 02:16:22 -0700252
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800253 if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
254 goto out;
255 end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
256 if (to > end_byte_in_page)
257 end_byte_in_page = to;
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800258 zero_user_segment(page, end_byte_in_page, PAGE_CACHE_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700259out:
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800260 return 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700261}
262
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800263/* This function must zero any hole we create */
Michael Halcrow53a27312007-05-23 13:58:15 -0700264static int ecryptfs_prepare_write(struct file *file, struct page *page,
265 unsigned from, unsigned to)
266{
Michael Halcrow53a27312007-05-23 13:58:15 -0700267 int rc = 0;
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800268 loff_t prev_page_end_size;
Michael Halcrow53a27312007-05-23 13:58:15 -0700269
Michael Halcrow16a72c42007-10-16 01:28:14 -0700270 if (!PageUptodate(page)) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700271 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
272 PAGE_CACHE_SIZE,
273 page->mapping->host);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700274 if (rc) {
275 printk(KERN_ERR "%s: Error attemping to read lower "
276 "page segment; rc = [%d]\n", __FUNCTION__, rc);
277 ClearPageUptodate(page);
278 goto out;
279 } else
280 SetPageUptodate(page);
281 }
Michael Halcrow240e2df2007-06-27 14:09:44 -0700282
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800283 prev_page_end_size = ((loff_t)page->index << PAGE_CACHE_SHIFT);
284
285 /*
286 * If creating a page or more of holes, zero them out via truncate.
287 * Note, this will increase i_size.
288 */
289 if (page->index != 0) {
290 if (prev_page_end_size > i_size_read(page->mapping->host)) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700291 rc = ecryptfs_truncate(file->f_path.dentry,
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800292 prev_page_end_size);
Michael Halcrow240e2df2007-06-27 14:09:44 -0700293 if (rc) {
294 printk(KERN_ERR "Error on attempt to "
295 "truncate to (higher) offset [%lld];"
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800296 " rc = [%d]\n", prev_page_end_size, rc);
Michael Halcrow240e2df2007-06-27 14:09:44 -0700297 goto out;
298 }
Michael Halcrow53a27312007-05-23 13:58:15 -0700299 }
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800300 }
301 /*
302 * Writing to a new page, and creating a small hole from start of page?
303 * Zero it out.
304 */
305 if ((i_size_read(page->mapping->host) == prev_page_end_size) &&
306 (from != 0)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800307 zero_user(page, 0, PAGE_CACHE_SIZE);
Michael Halcrow53a27312007-05-23 13:58:15 -0700308 }
309out:
310 return rc;
311}
312
Michael Halcrow237fead2006-10-04 02:16:22 -0700313/**
314 * ecryptfs_write_inode_size_to_header
315 *
316 * Writes the lower file size to the first 8 bytes of the header.
317 *
318 * Returns zero on success; non-zero on error.
319 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700320static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700321{
Michael Halcrow237fead2006-10-04 02:16:22 -0700322 u64 file_size;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700323 char *file_size_virt;
324 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700325
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700326 file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
327 if (!file_size_virt) {
328 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700329 goto out;
330 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700331 file_size = (u64)i_size_read(ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700332 file_size = cpu_to_be64(file_size);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700333 memcpy(file_size_virt, &file_size, sizeof(u64));
334 rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
335 sizeof(u64));
336 kfree(file_size_virt);
337 if (rc)
338 printk(KERN_ERR "%s: Error writing file size to header; "
339 "rc = [%d]\n", __FUNCTION__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700340out:
341 return rc;
342}
343
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700344struct kmem_cache *ecryptfs_xattr_cache;
345
346static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800347{
348 ssize_t size;
349 void *xattr_virt;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700350 struct dentry *lower_dentry =
351 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
352 struct inode *lower_inode = lower_dentry->d_inode;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800353 u64 file_size;
354 int rc;
355
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700356 if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
357 printk(KERN_WARNING
358 "No support for setting xattr in lower filesystem\n");
359 rc = -ENOSYS;
360 goto out;
361 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800362 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
363 if (!xattr_virt) {
364 printk(KERN_ERR "Out of memory whilst attempting to write "
365 "inode size to xattr\n");
366 rc = -ENOMEM;
367 goto out;
368 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700369 mutex_lock(&lower_inode->i_mutex);
370 size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
371 xattr_virt, PAGE_CACHE_SIZE);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800372 if (size < 0)
373 size = 8;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700374 file_size = (u64)i_size_read(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800375 file_size = cpu_to_be64(file_size);
376 memcpy(xattr_virt, &file_size, sizeof(u64));
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700377 rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
378 xattr_virt, size, 0);
379 mutex_unlock(&lower_inode->i_mutex);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800380 if (rc)
381 printk(KERN_ERR "Error whilst attempting to write inode size "
382 "to lower file xattr; rc = [%d]\n", rc);
383 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
384out:
385 return rc;
386}
387
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700388int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800389{
390 struct ecryptfs_crypt_stat *crypt_stat;
391
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700392 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800393 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700394 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800395 else
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700396 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800397}
398
Michael Halcrow237fead2006-10-04 02:16:22 -0700399/**
400 * ecryptfs_commit_write
401 * @file: The eCryptfs file object
402 * @page: The eCryptfs page
403 * @from: Ignored (we rotate the page IV on each write)
404 * @to: Ignored
405 *
406 * This is where we encrypt the data and pass the encrypted data to
407 * the lower filesystem. In OpenPGP-compatible mode, we operate on
408 * entire underlying packets.
409 */
410static int ecryptfs_commit_write(struct file *file, struct page *page,
411 unsigned from, unsigned to)
412{
Michael Halcrow237fead2006-10-04 02:16:22 -0700413 loff_t pos;
Michael Halcrowbf12be12007-10-16 01:28:11 -0700414 struct inode *ecryptfs_inode = page->mapping->host;
415 struct ecryptfs_crypt_stat *crypt_stat =
416 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700417 int rc;
418
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800419 if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700420 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
421 "crypt_stat at memory location [%p]\n", crypt_stat);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800422 crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700423 } else
424 ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
425 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
426 "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
427 to);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700428 /* Fills in zeros if 'to' goes beyond inode size */
Michael Halcrow237fead2006-10-04 02:16:22 -0700429 rc = fill_zeros_to_end_of_page(page, to);
430 if (rc) {
431 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
432 "zeros in page with index = [0x%.16x]\n",
433 page->index);
434 goto out;
435 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700436 rc = ecryptfs_encrypt_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700437 if (rc) {
438 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
439 "index [0x%.16x])\n", page->index);
440 goto out;
441 }
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700442 pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to;
Michael Halcrowbf12be12007-10-16 01:28:11 -0700443 if (pos > i_size_read(ecryptfs_inode)) {
444 i_size_write(ecryptfs_inode, pos);
Michael Halcrow237fead2006-10-04 02:16:22 -0700445 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
Michael Halcrowbf12be12007-10-16 01:28:11 -0700446 "[0x%.16x]\n", i_size_read(ecryptfs_inode));
Michael Halcrow237fead2006-10-04 02:16:22 -0700447 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700448 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800449 if (rc)
450 printk(KERN_ERR "Error writing inode size to metadata; "
451 "rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700452out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700453 return rc;
454}
455
Michael Halcrow237fead2006-10-04 02:16:22 -0700456static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
457{
458 int rc = 0;
459 struct inode *inode;
460 struct inode *lower_inode;
461
462 inode = (struct inode *)mapping->host;
463 lower_inode = ecryptfs_inode_to_lower(inode);
464 if (lower_inode->i_mapping->a_ops->bmap)
465 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
466 block);
467 return rc;
468}
469
Michael Halcrow237fead2006-10-04 02:16:22 -0700470struct address_space_operations ecryptfs_aops = {
471 .writepage = ecryptfs_writepage,
472 .readpage = ecryptfs_readpage,
473 .prepare_write = ecryptfs_prepare_write,
474 .commit_write = ecryptfs_commit_write,
475 .bmap = ecryptfs_bmap,
Michael Halcrow237fead2006-10-04 02:16:22 -0700476};