blob: 6df410c7726471c28b493c9d2b91f61ca6072696 [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
37struct kmem_cache *ecryptfs_lower_page_cache;
38
39/**
40 * ecryptfs_get1page
41 *
42 * Get one page from cache or lower f/s, return error otherwise.
43 *
44 * Returns unlocked and up-to-date page (if ok), with increased
45 * refcnt.
46 */
47static struct page *ecryptfs_get1page(struct file *file, int index)
48{
Michael Halcrow237fead2006-10-04 02:16:22 -070049 struct dentry *dentry;
50 struct inode *inode;
51 struct address_space *mapping;
52
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -080053 dentry = file->f_path.dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -070054 inode = dentry->d_inode;
55 mapping = inode->i_mapping;
Nick Piggin6fe69002007-05-06 14:49:04 -070056 return read_mapping_page(mapping, index, (void *)file);
Michael Halcrow237fead2006-10-04 02:16:22 -070057}
58
Michael Halcrow237fead2006-10-04 02:16:22 -070059/**
60 * ecryptfs_fill_zeros
61 * @file: The ecryptfs file
62 * @new_length: The new length of the data in the underlying file;
63 * everything between the prior end of the file and the
64 * new end of the file will be filled with zero's.
65 * new_length must be greater than current length
66 *
67 * Function for handling lseek-ing past the end of the file.
68 *
69 * This function does not support shrinking, only growing a file.
70 *
71 * Returns zero on success; non-zero otherwise.
72 */
73int ecryptfs_fill_zeros(struct file *file, loff_t new_length)
74{
75 int rc = 0;
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -080076 struct dentry *dentry = file->f_path.dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -070077 struct inode *inode = dentry->d_inode;
78 pgoff_t old_end_page_index = 0;
79 pgoff_t index = old_end_page_index;
80 int old_end_pos_in_page = -1;
81 pgoff_t new_end_page_index;
82 int new_end_pos_in_page;
83 loff_t cur_length = i_size_read(inode);
84
85 if (cur_length != 0) {
86 index = old_end_page_index =
87 ((cur_length - 1) >> PAGE_CACHE_SHIFT);
88 old_end_pos_in_page = ((cur_length - 1) & ~PAGE_CACHE_MASK);
89 }
90 new_end_page_index = ((new_length - 1) >> PAGE_CACHE_SHIFT);
91 new_end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK);
92 ecryptfs_printk(KERN_DEBUG, "old_end_page_index = [0x%.16x]; "
93 "old_end_pos_in_page = [%d]; "
94 "new_end_page_index = [0x%.16x]; "
95 "new_end_pos_in_page = [%d]\n",
96 old_end_page_index, old_end_pos_in_page,
97 new_end_page_index, new_end_pos_in_page);
98 if (old_end_page_index == new_end_page_index) {
99 /* Start and end are in the same page; we just need to
100 * set a portion of the existing page to zero's */
Michael Halcrow240e2df2007-06-27 14:09:44 -0700101 rc = ecryptfs_write_zeros(file, index,
102 (old_end_pos_in_page + 1),
103 (new_end_pos_in_page
104 - old_end_pos_in_page));
Michael Halcrow237fead2006-10-04 02:16:22 -0700105 if (rc)
Michael Halcrow240e2df2007-06-27 14:09:44 -0700106 ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros("
107 "file=[%p], "
Michael Halcrow237fead2006-10-04 02:16:22 -0700108 "index=[0x%.16x], "
109 "old_end_pos_in_page=[d], "
110 "(PAGE_CACHE_SIZE - new_end_pos_in_page"
111 "=[%d]"
112 ")=[d]) returned [%d]\n", file, index,
113 old_end_pos_in_page,
114 new_end_pos_in_page,
115 (PAGE_CACHE_SIZE - new_end_pos_in_page),
116 rc);
117 goto out;
118 }
119 /* Fill the remainder of the previous last page with zeros */
Michael Halcrow240e2df2007-06-27 14:09:44 -0700120 rc = ecryptfs_write_zeros(file, index, (old_end_pos_in_page + 1),
Michael Halcrow237fead2006-10-04 02:16:22 -0700121 ((PAGE_CACHE_SIZE - 1) - old_end_pos_in_page));
122 if (rc) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700123 ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros(file=[%p], "
Michael Halcrow237fead2006-10-04 02:16:22 -0700124 "index=[0x%.16x], old_end_pos_in_page=[d], "
125 "(PAGE_CACHE_SIZE - old_end_pos_in_page)=[d]) "
126 "returned [%d]\n", file, index,
127 old_end_pos_in_page,
128 (PAGE_CACHE_SIZE - old_end_pos_in_page), rc);
129 goto out;
130 }
131 index++;
132 while (index < new_end_page_index) {
133 /* Fill all intermediate pages with zeros */
Michael Halcrow240e2df2007-06-27 14:09:44 -0700134 rc = ecryptfs_write_zeros(file, index, 0, PAGE_CACHE_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700135 if (rc) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700136 ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros("
137 "file=[%p], "
Michael Halcrow237fead2006-10-04 02:16:22 -0700138 "index=[0x%.16x], "
139 "old_end_pos_in_page=[d], "
140 "(PAGE_CACHE_SIZE - new_end_pos_in_page"
141 "=[%d]"
142 ")=[d]) returned [%d]\n", file, index,
143 old_end_pos_in_page,
144 new_end_pos_in_page,
145 (PAGE_CACHE_SIZE - new_end_pos_in_page),
146 rc);
147 goto out;
148 }
149 index++;
150 }
151 /* Fill the portion at the beginning of the last new page with
152 * zero's */
Michael Halcrow240e2df2007-06-27 14:09:44 -0700153 rc = ecryptfs_write_zeros(file, index, 0, (new_end_pos_in_page + 1));
Michael Halcrow237fead2006-10-04 02:16:22 -0700154 if (rc) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700155 ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros(file="
Michael Halcrow237fead2006-10-04 02:16:22 -0700156 "[%p], index=[0x%.16x], 0, "
157 "new_end_pos_in_page=[%d]"
158 "returned [%d]\n", file, index,
159 new_end_pos_in_page, rc);
160 goto out;
161 }
162out:
163 return rc;
164}
165
166/**
167 * ecryptfs_writepage
168 * @page: Page that is locked before this call is made
169 *
170 * Returns zero on success; non-zero otherwise
171 */
172static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
173{
174 struct ecryptfs_page_crypt_context ctx;
175 int rc;
176
177 ctx.page = page;
178 ctx.mode = ECRYPTFS_WRITEPAGE_MODE;
179 ctx.param.wbc = wbc;
180 rc = ecryptfs_encrypt_page(&ctx);
181 if (rc) {
182 ecryptfs_printk(KERN_WARNING, "Error encrypting "
183 "page (upper index [0x%.16x])\n", page->index);
184 ClearPageUptodate(page);
185 goto out;
186 }
187 SetPageUptodate(page);
188 unlock_page(page);
189out:
190 return rc;
191}
192
193/**
194 * Reads the data from the lower file file at index lower_page_index
195 * and copies that data into page.
196 *
197 * @param page Page to fill
198 * @param lower_page_index Index of the page in the lower file to get
199 */
200int ecryptfs_do_readpage(struct file *file, struct page *page,
201 pgoff_t lower_page_index)
202{
203 int rc;
204 struct dentry *dentry;
205 struct file *lower_file;
206 struct dentry *lower_dentry;
207 struct inode *inode;
208 struct inode *lower_inode;
209 char *page_data;
210 struct page *lower_page = NULL;
211 char *lower_page_data;
212 const struct address_space_operations *lower_a_ops;
213
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -0800214 dentry = file->f_path.dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700215 lower_file = ecryptfs_file_to_lower(file);
216 lower_dentry = ecryptfs_dentry_to_lower(dentry);
217 inode = dentry->d_inode;
218 lower_inode = ecryptfs_inode_to_lower(inode);
219 lower_a_ops = lower_inode->i_mapping->a_ops;
220 lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index,
221 (filler_t *)lower_a_ops->readpage,
222 (void *)lower_file);
223 if (IS_ERR(lower_page)) {
224 rc = PTR_ERR(lower_page);
225 lower_page = NULL;
226 ecryptfs_printk(KERN_ERR, "Error reading from page cache\n");
227 goto out;
228 }
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800229 page_data = kmap_atomic(page, KM_USER0);
230 lower_page_data = kmap_atomic(lower_page, KM_USER1);
Michael Halcrow237fead2006-10-04 02:16:22 -0700231 memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE);
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800232 kunmap_atomic(lower_page_data, KM_USER1);
233 kunmap_atomic(page_data, KM_USER0);
Michael Halcrow0a9ac382007-02-12 00:53:50 -0800234 flush_dcache_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700235 rc = 0;
236out:
237 if (likely(lower_page))
238 page_cache_release(lower_page);
239 if (rc == 0)
240 SetPageUptodate(page);
241 else
242 ClearPageUptodate(page);
243 return rc;
244}
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800245/**
246 * Header Extent:
247 * Octets 0-7: Unencrypted file size (big-endian)
248 * Octets 8-15: eCryptfs special marker
249 * Octets 16-19: Flags
250 * Octet 16: File format version number (between 0 and 255)
251 * Octets 17-18: Reserved
252 * Octet 19: Bit 1 (lsb): Reserved
253 * Bit 2: Encrypted?
254 * Bits 3-8: Reserved
255 * Octets 20-23: Header extent size (big-endian)
256 * Octets 24-25: Number of header extents at front of file
257 * (big-endian)
258 * Octet 26: Begin RFC 2440 authentication token packet set
259 */
260static void set_header_info(char *page_virt,
261 struct ecryptfs_crypt_stat *crypt_stat)
262{
263 size_t written;
264 int save_num_header_extents_at_front =
265 crypt_stat->num_header_extents_at_front;
266
267 crypt_stat->num_header_extents_at_front = 1;
268 ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
269 crypt_stat->num_header_extents_at_front =
270 save_num_header_extents_at_front;
271}
Michael Halcrow237fead2006-10-04 02:16:22 -0700272
273/**
274 * ecryptfs_readpage
275 * @file: This is an ecryptfs file
276 * @page: ecryptfs associated page to stick the read data into
277 *
278 * Read in a page, decrypting if necessary.
279 *
280 * Returns zero on success; non-zero on error.
281 */
282static int ecryptfs_readpage(struct file *file, struct page *page)
283{
284 int rc = 0;
285 struct ecryptfs_crypt_stat *crypt_stat;
286
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -0800287 BUG_ON(!(file && file->f_path.dentry && file->f_path.dentry->d_inode));
288 crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
289 ->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700290 if (!crypt_stat
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800291 || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
292 || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700293 ecryptfs_printk(KERN_DEBUG,
294 "Passing through unencrypted page\n");
295 rc = ecryptfs_do_readpage(file, page, page->index);
296 if (rc) {
297 ecryptfs_printk(KERN_ERR, "Error reading page; rc = "
298 "[%d]\n", rc);
299 goto out;
300 }
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800301 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
302 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
303 int num_pages_in_header_region =
304 (crypt_stat->header_extent_size
305 / PAGE_CACHE_SIZE);
306
307 if (page->index < num_pages_in_header_region) {
308 char *page_virt;
309
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800310 page_virt = kmap_atomic(page, KM_USER0);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800311 memset(page_virt, 0, PAGE_CACHE_SIZE);
312 if (page->index == 0) {
313 rc = ecryptfs_read_xattr_region(
314 page_virt, file->f_path.dentry);
315 set_header_info(page_virt, crypt_stat);
316 }
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800317 kunmap_atomic(page_virt, KM_USER0);
Michael Halcrow0a9ac382007-02-12 00:53:50 -0800318 flush_dcache_page(page);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800319 if (rc) {
320 printk(KERN_ERR "Error reading xattr "
321 "region\n");
322 goto out;
323 }
324 } else {
325 rc = ecryptfs_do_readpage(
326 file, page,
327 (page->index
328 - num_pages_in_header_region));
329 if (rc) {
330 printk(KERN_ERR "Error reading page; "
331 "rc = [%d]\n", rc);
332 goto out;
333 }
334 }
335 } else {
336 rc = ecryptfs_do_readpage(file, page, page->index);
337 if (rc) {
338 printk(KERN_ERR "Error reading page; rc = "
339 "[%d]\n", rc);
340 goto out;
341 }
342 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700343 } else {
344 rc = ecryptfs_decrypt_page(file, page);
345 if (rc) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700346 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
347 "rc = [%d]\n", rc);
348 goto out;
349 }
350 }
351 SetPageUptodate(page);
352out:
353 if (rc)
354 ClearPageUptodate(page);
355 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
356 page->index);
357 unlock_page(page);
358 return rc;
359}
360
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800361/**
362 * Called with lower inode mutex held.
363 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700364static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
365{
366 struct inode *inode = page->mapping->host;
367 int end_byte_in_page;
Michael Halcrow237fead2006-10-04 02:16:22 -0700368
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800369 if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
370 goto out;
371 end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
372 if (to > end_byte_in_page)
373 end_byte_in_page = to;
Nate Dillerc9f28752007-05-16 22:11:17 -0700374 zero_user_page(page, end_byte_in_page,
375 PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0);
Michael Halcrow237fead2006-10-04 02:16:22 -0700376out:
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800377 return 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700378}
379
Michael Halcrow53a27312007-05-23 13:58:15 -0700380/**
381 * eCryptfs does not currently support holes. When writing after a
382 * seek past the end of the file, eCryptfs fills in 0's through to the
383 * current location. The code to fill in the 0's to all the
384 * intermediate pages calls ecryptfs_prepare_write_no_truncate().
385 */
386static int
387ecryptfs_prepare_write_no_truncate(struct file *file, struct page *page,
388 unsigned from, unsigned to)
Michael Halcrow237fead2006-10-04 02:16:22 -0700389{
390 int rc = 0;
391
Michael Halcrow237fead2006-10-04 02:16:22 -0700392 if (from == 0 && to == PAGE_CACHE_SIZE)
393 goto out; /* If we are writing a full page, it will be
394 up to date. */
395 if (!PageUptodate(page))
396 rc = ecryptfs_do_readpage(file, page, page->index);
397out:
398 return rc;
399}
400
Michael Halcrow53a27312007-05-23 13:58:15 -0700401static int ecryptfs_prepare_write(struct file *file, struct page *page,
402 unsigned from, unsigned to)
403{
Michael Halcrow53a27312007-05-23 13:58:15 -0700404 int rc = 0;
405
406 if (from == 0 && to == PAGE_CACHE_SIZE)
407 goto out; /* If we are writing a full page, it will be
408 up to date. */
409 if (!PageUptodate(page))
410 rc = ecryptfs_do_readpage(file, page, page->index);
Michael Halcrow240e2df2007-06-27 14:09:44 -0700411 if (page->index != 0) {
412 loff_t end_of_prev_pg_pos =
413 (((loff_t)page->index << PAGE_CACHE_SHIFT) - 1);
414
415 if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) {
416 rc = ecryptfs_truncate(file->f_path.dentry,
417 end_of_prev_pg_pos);
418 if (rc) {
419 printk(KERN_ERR "Error on attempt to "
420 "truncate to (higher) offset [%lld];"
421 " rc = [%d]\n", end_of_prev_pg_pos, rc);
422 goto out;
423 }
Michael Halcrow53a27312007-05-23 13:58:15 -0700424 }
425 }
426out:
427 return rc;
428}
429
Michael Halcrow237fead2006-10-04 02:16:22 -0700430int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
431 struct inode *lower_inode,
432 struct writeback_control *wbc)
433{
434 int rc = 0;
435
436 rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
437 if (rc) {
438 ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
439 "rc = [%d]\n", rc);
440 goto out;
441 }
442 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
443 page_cache_release(lower_page);
444out:
445 return rc;
446}
447
Michael Halcrowae73fc02007-02-28 20:12:16 -0800448static
449void ecryptfs_release_lower_page(struct page *lower_page, int page_locked)
Michael Halcrow237fead2006-10-04 02:16:22 -0700450{
Michael Halcrowae73fc02007-02-28 20:12:16 -0800451 if (page_locked)
452 unlock_page(lower_page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700453 page_cache_release(lower_page);
454}
455
456/**
457 * ecryptfs_write_inode_size_to_header
458 *
459 * Writes the lower file size to the first 8 bytes of the header.
460 *
461 * Returns zero on success; non-zero on error.
462 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800463static int ecryptfs_write_inode_size_to_header(struct file *lower_file,
464 struct inode *lower_inode,
465 struct inode *inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700466{
467 int rc = 0;
468 struct page *header_page;
469 char *header_virt;
470 const struct address_space_operations *lower_a_ops;
471 u64 file_size;
472
Dmitriy Monakhova8fa74a2007-03-05 00:30:47 -0800473retry:
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800474 header_page = grab_cache_page(lower_inode->i_mapping, 0);
475 if (!header_page) {
476 ecryptfs_printk(KERN_ERR, "grab_cache_page for "
477 "lower_page_index 0 failed\n");
478 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700479 goto out;
480 }
481 lower_a_ops = lower_inode->i_mapping->a_ops;
482 rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8);
Michael Halcrowae73fc02007-02-28 20:12:16 -0800483 if (rc) {
Dmitriy Monakhova8fa74a2007-03-05 00:30:47 -0800484 if (rc == AOP_TRUNCATED_PAGE) {
Michael Halcrowae73fc02007-02-28 20:12:16 -0800485 ecryptfs_release_lower_page(header_page, 0);
Dmitriy Monakhova8fa74a2007-03-05 00:30:47 -0800486 goto retry;
487 } else
Michael Halcrowae73fc02007-02-28 20:12:16 -0800488 ecryptfs_release_lower_page(header_page, 1);
489 goto out;
490 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700491 file_size = (u64)i_size_read(inode);
492 ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size);
493 file_size = cpu_to_be64(file_size);
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800494 header_virt = kmap_atomic(header_page, KM_USER0);
Michael Halcrow237fead2006-10-04 02:16:22 -0700495 memcpy(header_virt, &file_size, sizeof(u64));
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800496 kunmap_atomic(header_virt, KM_USER0);
Michael Halcrow0a9ac382007-02-12 00:53:50 -0800497 flush_dcache_page(header_page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700498 rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8);
499 if (rc < 0)
500 ecryptfs_printk(KERN_ERR, "Error commiting header page "
501 "write\n");
Dmitriy Monakhova8fa74a2007-03-05 00:30:47 -0800502 if (rc == AOP_TRUNCATED_PAGE) {
Michael Halcrowae73fc02007-02-28 20:12:16 -0800503 ecryptfs_release_lower_page(header_page, 0);
Dmitriy Monakhova8fa74a2007-03-05 00:30:47 -0800504 goto retry;
505 } else
Michael Halcrowae73fc02007-02-28 20:12:16 -0800506 ecryptfs_release_lower_page(header_page, 1);
Michael Halcrow237fead2006-10-04 02:16:22 -0700507 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
508 mark_inode_dirty_sync(inode);
509out:
510 return rc;
511}
512
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800513static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode,
514 struct inode *inode,
515 struct dentry *ecryptfs_dentry,
516 int lower_i_mutex_held)
517{
518 ssize_t size;
519 void *xattr_virt;
520 struct dentry *lower_dentry;
521 u64 file_size;
522 int rc;
523
524 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
525 if (!xattr_virt) {
526 printk(KERN_ERR "Out of memory whilst attempting to write "
527 "inode size to xattr\n");
528 rc = -ENOMEM;
529 goto out;
530 }
531 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
Dmitriy Monakhovad5f1192007-03-05 00:30:12 -0800532 if (!lower_dentry->d_inode->i_op->getxattr ||
533 !lower_dentry->d_inode->i_op->setxattr) {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800534 printk(KERN_WARNING
535 "No support for setting xattr in lower filesystem\n");
536 rc = -ENOSYS;
537 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
538 goto out;
539 }
540 if (!lower_i_mutex_held)
541 mutex_lock(&lower_dentry->d_inode->i_mutex);
542 size = lower_dentry->d_inode->i_op->getxattr(lower_dentry,
543 ECRYPTFS_XATTR_NAME,
544 xattr_virt,
545 PAGE_CACHE_SIZE);
546 if (!lower_i_mutex_held)
547 mutex_unlock(&lower_dentry->d_inode->i_mutex);
548 if (size < 0)
549 size = 8;
550 file_size = (u64)i_size_read(inode);
551 file_size = cpu_to_be64(file_size);
552 memcpy(xattr_virt, &file_size, sizeof(u64));
553 if (!lower_i_mutex_held)
554 mutex_lock(&lower_dentry->d_inode->i_mutex);
555 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry,
556 ECRYPTFS_XATTR_NAME,
557 xattr_virt, size, 0);
558 if (!lower_i_mutex_held)
559 mutex_unlock(&lower_dentry->d_inode->i_mutex);
560 if (rc)
561 printk(KERN_ERR "Error whilst attempting to write inode size "
562 "to lower file xattr; rc = [%d]\n", rc);
563 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
564out:
565 return rc;
566}
567
568int
569ecryptfs_write_inode_size_to_metadata(struct file *lower_file,
570 struct inode *lower_inode,
571 struct inode *inode,
572 struct dentry *ecryptfs_dentry,
573 int lower_i_mutex_held)
574{
575 struct ecryptfs_crypt_stat *crypt_stat;
576
577 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
578 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
579 return ecryptfs_write_inode_size_to_xattr(lower_inode, inode,
580 ecryptfs_dentry,
581 lower_i_mutex_held);
582 else
583 return ecryptfs_write_inode_size_to_header(lower_file,
584 lower_inode,
585 inode);
586}
587
Michael Halcrow237fead2006-10-04 02:16:22 -0700588int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
589 struct file *lower_file,
590 unsigned long lower_page_index, int byte_offset,
591 int region_bytes)
592{
593 int rc = 0;
594
Dmitriy Monakhova8fa74a2007-03-05 00:30:47 -0800595retry:
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800596 *lower_page = grab_cache_page(lower_inode->i_mapping, lower_page_index);
597 if (!(*lower_page)) {
598 rc = -EINVAL;
599 ecryptfs_printk(KERN_ERR, "Error attempting to grab "
Michael Halcrow237fead2006-10-04 02:16:22 -0700600 "lower page with index [0x%.16x]\n",
601 lower_page_index);
602 goto out;
603 }
604 rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
605 (*lower_page),
606 byte_offset,
607 region_bytes);
608 if (rc) {
Dmitriy Monakhova8fa74a2007-03-05 00:30:47 -0800609 if (rc == AOP_TRUNCATED_PAGE) {
610 ecryptfs_release_lower_page(*lower_page, 0);
611 goto retry;
612 } else {
613 ecryptfs_printk(KERN_ERR, "prepare_write for "
Michael Halcrow237fead2006-10-04 02:16:22 -0700614 "lower_page_index = [0x%.16x] failed; rc = "
615 "[%d]\n", lower_page_index, rc);
Dmitriy Monakhova8fa74a2007-03-05 00:30:47 -0800616 ecryptfs_release_lower_page(*lower_page, 1);
617 (*lower_page) = NULL;
618 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700619 }
620out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700621 return rc;
622}
623
624/**
625 * ecryptfs_commit_lower_page
626 *
627 * Returns zero on success; non-zero on error
628 */
629int
630ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
631 struct file *lower_file, int byte_offset,
632 int region_size)
633{
Michael Halcrowae73fc02007-02-28 20:12:16 -0800634 int page_locked = 1;
Michael Halcrow237fead2006-10-04 02:16:22 -0700635 int rc = 0;
636
637 rc = lower_inode->i_mapping->a_ops->commit_write(
638 lower_file, lower_page, byte_offset, region_size);
Michael Halcrowae73fc02007-02-28 20:12:16 -0800639 if (rc == AOP_TRUNCATED_PAGE)
640 page_locked = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700641 if (rc < 0) {
642 ecryptfs_printk(KERN_ERR,
643 "Error committing write; rc = [%d]\n", rc);
644 } else
645 rc = 0;
Michael Halcrowae73fc02007-02-28 20:12:16 -0800646 ecryptfs_release_lower_page(lower_page, page_locked);
Michael Halcrow237fead2006-10-04 02:16:22 -0700647 return rc;
648}
649
650/**
651 * ecryptfs_copy_page_to_lower
652 *
653 * Used for plaintext pass-through; no page index interpolation
654 * required.
655 */
656int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
657 struct file *lower_file)
658{
659 int rc = 0;
660 struct page *lower_page;
661
662 rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
663 page->index, 0, PAGE_CACHE_SIZE);
664 if (rc) {
665 ecryptfs_printk(KERN_ERR, "Error attempting to get page "
666 "at index [0x%.16x]\n", page->index);
667 goto out;
668 }
669 /* TODO: aops */
670 memcpy((char *)page_address(lower_page), page_address(page),
671 PAGE_CACHE_SIZE);
672 rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
673 0, PAGE_CACHE_SIZE);
674 if (rc)
675 ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
676 "at index [0x%.16x]\n", page->index);
677out:
678 return rc;
679}
680
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800681struct kmem_cache *ecryptfs_xattr_cache;
682
Michael Halcrow237fead2006-10-04 02:16:22 -0700683/**
684 * ecryptfs_commit_write
685 * @file: The eCryptfs file object
686 * @page: The eCryptfs page
687 * @from: Ignored (we rotate the page IV on each write)
688 * @to: Ignored
689 *
690 * This is where we encrypt the data and pass the encrypted data to
691 * the lower filesystem. In OpenPGP-compatible mode, we operate on
692 * entire underlying packets.
693 */
694static int ecryptfs_commit_write(struct file *file, struct page *page,
695 unsigned from, unsigned to)
696{
697 struct ecryptfs_page_crypt_context ctx;
698 loff_t pos;
699 struct inode *inode;
700 struct inode *lower_inode;
701 struct file *lower_file;
702 struct ecryptfs_crypt_stat *crypt_stat;
703 int rc;
704
705 inode = page->mapping->host;
706 lower_inode = ecryptfs_inode_to_lower(inode);
707 lower_file = ecryptfs_file_to_lower(file);
708 mutex_lock(&lower_inode->i_mutex);
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -0800709 crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
710 ->crypt_stat;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800711 if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700712 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
713 "crypt_stat at memory location [%p]\n", crypt_stat);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800714 crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700715 } else
716 ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
717 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
718 "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
719 to);
720 rc = fill_zeros_to_end_of_page(page, to);
721 if (rc) {
722 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
723 "zeros in page with index = [0x%.16x]\n",
724 page->index);
725 goto out;
726 }
727 ctx.page = page;
728 ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE;
729 ctx.param.lower_file = lower_file;
730 rc = ecryptfs_encrypt_page(&ctx);
731 if (rc) {
732 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
733 "index [0x%.16x])\n", page->index);
734 goto out;
735 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700736 inode->i_blocks = lower_inode->i_blocks;
737 pos = (page->index << PAGE_CACHE_SHIFT) + to;
738 if (pos > i_size_read(inode)) {
739 i_size_write(inode, pos);
740 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
741 "[0x%.16x]\n", i_size_read(inode));
742 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800743 rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode,
744 inode, file->f_dentry,
745 ECRYPTFS_LOWER_I_MUTEX_HELD);
746 if (rc)
747 printk(KERN_ERR "Error writing inode size to metadata; "
748 "rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700749 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
750 mark_inode_dirty_sync(inode);
751out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700752 if (rc < 0)
753 ClearPageUptodate(page);
754 else
755 SetPageUptodate(page);
756 mutex_unlock(&lower_inode->i_mutex);
757 return rc;
758}
759
760/**
Michael Halcrow240e2df2007-06-27 14:09:44 -0700761 * ecryptfs_write_zeros
Michael Halcrow237fead2006-10-04 02:16:22 -0700762 * @file: The ecryptfs file
763 * @index: The index in which we are writing
764 * @start: The position after the last block of data
765 * @num_zeros: The number of zeros to write
766 *
767 * Write a specified number of zero's to a page.
768 *
769 * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
770 */
Michael Halcrow240e2df2007-06-27 14:09:44 -0700771int
772ecryptfs_write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
Michael Halcrow237fead2006-10-04 02:16:22 -0700773{
774 int rc = 0;
775 struct page *tmp_page;
776
777 tmp_page = ecryptfs_get1page(file, index);
778 if (IS_ERR(tmp_page)) {
779 ecryptfs_printk(KERN_ERR, "Error getting page at index "
780 "[0x%.16x]\n", index);
781 rc = PTR_ERR(tmp_page);
782 goto out;
783 }
Michael Halcrow53a27312007-05-23 13:58:15 -0700784 if ((rc = ecryptfs_prepare_write_no_truncate(file, tmp_page, start,
785 (start + num_zeros)))) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700786 ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
Michael Halcrow53a27312007-05-23 13:58:15 -0700787 "to page at index [0x%.16x]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700788 index);
Michael Halcrow237fead2006-10-04 02:16:22 -0700789 page_cache_release(tmp_page);
790 goto out;
791 }
Nate Dillerc9f28752007-05-16 22:11:17 -0700792 zero_user_page(tmp_page, start, num_zeros, KM_USER0);
Michael Halcrow237fead2006-10-04 02:16:22 -0700793 rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
794 if (rc < 0) {
795 ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
796 "to remainder of page at index [0x%.16x]\n",
797 index);
Michael Halcrow237fead2006-10-04 02:16:22 -0700798 page_cache_release(tmp_page);
799 goto out;
800 }
801 rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700802 page_cache_release(tmp_page);
803out:
804 return rc;
805}
806
807static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
808{
809 int rc = 0;
810 struct inode *inode;
811 struct inode *lower_inode;
812
813 inode = (struct inode *)mapping->host;
814 lower_inode = ecryptfs_inode_to_lower(inode);
815 if (lower_inode->i_mapping->a_ops->bmap)
816 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
817 block);
818 return rc;
819}
820
821static void ecryptfs_sync_page(struct page *page)
822{
823 struct inode *inode;
824 struct inode *lower_inode;
825 struct page *lower_page;
826
827 inode = page->mapping->host;
828 lower_inode = ecryptfs_inode_to_lower(inode);
829 /* NOTE: Recently swapped with grab_cache_page(), since
830 * sync_page() just makes sure that pending I/O gets done. */
831 lower_page = find_lock_page(lower_inode->i_mapping, page->index);
832 if (!lower_page) {
833 ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n");
834 return;
835 }
836 lower_page->mapping->a_ops->sync_page(lower_page);
837 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
838 lower_page->index);
839 unlock_page(lower_page);
840 page_cache_release(lower_page);
841}
842
843struct address_space_operations ecryptfs_aops = {
844 .writepage = ecryptfs_writepage,
845 .readpage = ecryptfs_readpage,
846 .prepare_write = ecryptfs_prepare_write,
847 .commit_write = ecryptfs_commit_write,
848 .bmap = ecryptfs_bmap,
849 .sync_page = ecryptfs_sync_page,
850};