blob: 6ae0afb238d12c33aebdfaccc34b64fea0582ff8 [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 */
Michael Halcrowda0102a2007-10-16 01:28:07 -070047struct page *ecryptfs_get1page(struct file *file, loff_t index)
Michael Halcrow237fead2006-10-04 02:16:22 -070048{
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{
Michael Halcrow237fead2006-10-04 02:16:22 -0700174 int rc;
175
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700176 rc = ecryptfs_encrypt_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700177 if (rc) {
178 ecryptfs_printk(KERN_WARNING, "Error encrypting "
179 "page (upper index [0x%.16x])\n", page->index);
180 ClearPageUptodate(page);
181 goto out;
182 }
183 SetPageUptodate(page);
184 unlock_page(page);
185out:
186 return rc;
187}
188
189/**
190 * Reads the data from the lower file file at index lower_page_index
191 * and copies that data into page.
192 *
193 * @param page Page to fill
194 * @param lower_page_index Index of the page in the lower file to get
195 */
196int ecryptfs_do_readpage(struct file *file, struct page *page,
197 pgoff_t lower_page_index)
198{
199 int rc;
200 struct dentry *dentry;
201 struct file *lower_file;
202 struct dentry *lower_dentry;
203 struct inode *inode;
204 struct inode *lower_inode;
205 char *page_data;
206 struct page *lower_page = NULL;
207 char *lower_page_data;
208 const struct address_space_operations *lower_a_ops;
209
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -0800210 dentry = file->f_path.dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700211 lower_file = ecryptfs_file_to_lower(file);
212 lower_dentry = ecryptfs_dentry_to_lower(dentry);
213 inode = dentry->d_inode;
214 lower_inode = ecryptfs_inode_to_lower(inode);
215 lower_a_ops = lower_inode->i_mapping->a_ops;
216 lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index,
217 (filler_t *)lower_a_ops->readpage,
218 (void *)lower_file);
219 if (IS_ERR(lower_page)) {
220 rc = PTR_ERR(lower_page);
221 lower_page = NULL;
222 ecryptfs_printk(KERN_ERR, "Error reading from page cache\n");
223 goto out;
224 }
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800225 page_data = kmap_atomic(page, KM_USER0);
226 lower_page_data = kmap_atomic(lower_page, KM_USER1);
Michael Halcrow237fead2006-10-04 02:16:22 -0700227 memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE);
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800228 kunmap_atomic(lower_page_data, KM_USER1);
229 kunmap_atomic(page_data, KM_USER0);
Michael Halcrow0a9ac382007-02-12 00:53:50 -0800230 flush_dcache_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700231 rc = 0;
232out:
233 if (likely(lower_page))
234 page_cache_release(lower_page);
235 if (rc == 0)
236 SetPageUptodate(page);
237 else
238 ClearPageUptodate(page);
239 return rc;
240}
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800241/**
242 * Header Extent:
243 * Octets 0-7: Unencrypted file size (big-endian)
244 * Octets 8-15: eCryptfs special marker
245 * Octets 16-19: Flags
246 * Octet 16: File format version number (between 0 and 255)
247 * Octets 17-18: Reserved
248 * Octet 19: Bit 1 (lsb): Reserved
249 * Bit 2: Encrypted?
250 * Bits 3-8: Reserved
251 * Octets 20-23: Header extent size (big-endian)
252 * Octets 24-25: Number of header extents at front of file
253 * (big-endian)
254 * Octet 26: Begin RFC 2440 authentication token packet set
255 */
256static void set_header_info(char *page_virt,
257 struct ecryptfs_crypt_stat *crypt_stat)
258{
259 size_t written;
260 int save_num_header_extents_at_front =
261 crypt_stat->num_header_extents_at_front;
262
263 crypt_stat->num_header_extents_at_front = 1;
264 ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
265 crypt_stat->num_header_extents_at_front =
266 save_num_header_extents_at_front;
267}
Michael Halcrow237fead2006-10-04 02:16:22 -0700268
269/**
Michael Halcrowbf12be12007-10-16 01:28:11 -0700270 * ecryptfs_copy_up_encrypted_with_header
271 * @page: Sort of a ``virtual'' representation of the encrypted lower
272 * file. The actual lower file does not have the metadata in
273 * the header. This is locked.
274 * @crypt_stat: The eCryptfs inode's cryptographic context
275 *
276 * The ``view'' is the version of the file that userspace winds up
277 * seeing, with the header information inserted.
278 */
279static int
280ecryptfs_copy_up_encrypted_with_header(struct page *page,
281 struct ecryptfs_crypt_stat *crypt_stat)
282{
283 loff_t extent_num_in_page = 0;
284 loff_t num_extents_per_page = (PAGE_CACHE_SIZE
285 / crypt_stat->extent_size);
286 int rc = 0;
287
288 while (extent_num_in_page < num_extents_per_page) {
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700289 loff_t view_extent_num = ((((loff_t)page->index)
290 * num_extents_per_page)
Michael Halcrowbf12be12007-10-16 01:28:11 -0700291 + extent_num_in_page);
292
293 if (view_extent_num < crypt_stat->num_header_extents_at_front) {
294 /* This is a header extent */
295 char *page_virt;
296
297 page_virt = kmap_atomic(page, KM_USER0);
298 memset(page_virt, 0, PAGE_CACHE_SIZE);
299 /* TODO: Support more than one header extent */
300 if (view_extent_num == 0) {
301 rc = ecryptfs_read_xattr_region(
302 page_virt, page->mapping->host);
303 set_header_info(page_virt, crypt_stat);
304 }
305 kunmap_atomic(page_virt, KM_USER0);
306 flush_dcache_page(page);
307 if (rc) {
308 ClearPageUptodate(page);
309 printk(KERN_ERR "%s: Error reading xattr "
310 "region; rc = [%d]\n", __FUNCTION__, rc);
311 goto out;
312 }
313 SetPageUptodate(page);
314 } else {
315 /* This is an encrypted data extent */
316 loff_t lower_offset =
317 ((view_extent_num -
318 crypt_stat->num_header_extents_at_front)
319 * crypt_stat->extent_size);
320
321 rc = ecryptfs_read_lower_page_segment(
322 page, (lower_offset >> PAGE_CACHE_SHIFT),
323 (lower_offset & ~PAGE_CACHE_MASK),
324 crypt_stat->extent_size, page->mapping->host);
325 if (rc) {
326 printk(KERN_ERR "%s: Error attempting to read "
327 "extent at offset [%lld] in the lower "
328 "file; rc = [%d]\n", __FUNCTION__,
329 lower_offset, rc);
330 goto out;
331 }
332 }
333 extent_num_in_page++;
334 }
335out:
336 return rc;
337}
338
339/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700340 * ecryptfs_readpage
Michael Halcrowbf12be12007-10-16 01:28:11 -0700341 * @file: An eCryptfs file
342 * @page: Page from eCryptfs inode mapping into which to stick the read data
Michael Halcrow237fead2006-10-04 02:16:22 -0700343 *
344 * Read in a page, decrypting if necessary.
345 *
346 * Returns zero on success; non-zero on error.
347 */
348static int ecryptfs_readpage(struct file *file, struct page *page)
349{
Michael Halcrowbf12be12007-10-16 01:28:11 -0700350 struct ecryptfs_crypt_stat *crypt_stat =
351 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700352 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700353
Michael Halcrow237fead2006-10-04 02:16:22 -0700354 if (!crypt_stat
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800355 || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
356 || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700357 ecryptfs_printk(KERN_DEBUG,
358 "Passing through unencrypted page\n");
Michael Halcrowbf12be12007-10-16 01:28:11 -0700359 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
360 PAGE_CACHE_SIZE,
361 page->mapping->host);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800362 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
363 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700364 rc = ecryptfs_copy_up_encrypted_with_header(page,
365 crypt_stat);
366 if (rc) {
367 printk(KERN_ERR "%s: Error attempting to copy "
368 "the encrypted content from the lower "
369 "file whilst inserting the metadata "
370 "from the xattr into the header; rc = "
371 "[%d]\n", __FUNCTION__, rc);
372 goto out;
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800373 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700374
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800375 } else {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700376 rc = ecryptfs_read_lower_page_segment(
377 page, page->index, 0, PAGE_CACHE_SIZE,
378 page->mapping->host);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800379 if (rc) {
380 printk(KERN_ERR "Error reading page; rc = "
381 "[%d]\n", rc);
382 goto out;
383 }
384 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700385 } else {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700386 rc = ecryptfs_decrypt_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700387 if (rc) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700388 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
389 "rc = [%d]\n", rc);
390 goto out;
391 }
392 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700393out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700394 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
395 page->index);
396 unlock_page(page);
397 return rc;
398}
399
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800400/**
401 * Called with lower inode mutex held.
402 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700403static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
404{
405 struct inode *inode = page->mapping->host;
406 int end_byte_in_page;
Michael Halcrow237fead2006-10-04 02:16:22 -0700407
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800408 if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
409 goto out;
410 end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
411 if (to > end_byte_in_page)
412 end_byte_in_page = to;
Nate Dillerc9f28752007-05-16 22:11:17 -0700413 zero_user_page(page, end_byte_in_page,
414 PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0);
Michael Halcrow237fead2006-10-04 02:16:22 -0700415out:
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800416 return 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700417}
418
Michael Halcrow53a27312007-05-23 13:58:15 -0700419/**
420 * eCryptfs does not currently support holes. When writing after a
421 * seek past the end of the file, eCryptfs fills in 0's through to the
422 * current location. The code to fill in the 0's to all the
423 * intermediate pages calls ecryptfs_prepare_write_no_truncate().
424 */
425static int
426ecryptfs_prepare_write_no_truncate(struct file *file, struct page *page,
427 unsigned from, unsigned to)
Michael Halcrow237fead2006-10-04 02:16:22 -0700428{
429 int rc = 0;
430
Michael Halcrow237fead2006-10-04 02:16:22 -0700431 if (from == 0 && to == PAGE_CACHE_SIZE)
432 goto out; /* If we are writing a full page, it will be
433 up to date. */
434 if (!PageUptodate(page))
435 rc = ecryptfs_do_readpage(file, page, page->index);
436out:
437 return rc;
438}
439
Michael Halcrow53a27312007-05-23 13:58:15 -0700440static int ecryptfs_prepare_write(struct file *file, struct page *page,
441 unsigned from, unsigned to)
442{
Michael Halcrow53a27312007-05-23 13:58:15 -0700443 int rc = 0;
444
445 if (from == 0 && to == PAGE_CACHE_SIZE)
446 goto out; /* If we are writing a full page, it will be
447 up to date. */
448 if (!PageUptodate(page))
Michael Halcrowbf12be12007-10-16 01:28:11 -0700449 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
450 PAGE_CACHE_SIZE,
451 page->mapping->host);
Michael Halcrow240e2df2007-06-27 14:09:44 -0700452 if (page->index != 0) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700453 loff_t end_of_prev_pg_pos =
454 (((loff_t)page->index << PAGE_CACHE_SHIFT) - 1);
Michael Halcrow240e2df2007-06-27 14:09:44 -0700455
456 if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) {
457 rc = ecryptfs_truncate(file->f_path.dentry,
458 end_of_prev_pg_pos);
459 if (rc) {
460 printk(KERN_ERR "Error on attempt to "
461 "truncate to (higher) offset [%lld];"
462 " rc = [%d]\n", end_of_prev_pg_pos, rc);
463 goto out;
464 }
Michael Halcrow53a27312007-05-23 13:58:15 -0700465 }
Michael Halcrowd4c5cdb2007-06-27 14:09:45 -0700466 if (end_of_prev_pg_pos + 1 > i_size_read(page->mapping->host))
467 zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
Michael Halcrow53a27312007-05-23 13:58:15 -0700468 }
469out:
470 return rc;
471}
472
Michael Halcrow237fead2006-10-04 02:16:22 -0700473int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
474 struct inode *lower_inode,
475 struct writeback_control *wbc)
476{
477 int rc = 0;
478
479 rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
480 if (rc) {
481 ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
482 "rc = [%d]\n", rc);
483 goto out;
484 }
485 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
486 page_cache_release(lower_page);
487out:
488 return rc;
489}
490
Nick Piggin55144762007-10-16 01:25:26 -0700491static void ecryptfs_release_lower_page(struct page *lower_page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700492{
Nick Piggin55144762007-10-16 01:25:26 -0700493 unlock_page(lower_page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700494 page_cache_release(lower_page);
495}
496
497/**
498 * ecryptfs_write_inode_size_to_header
499 *
500 * Writes the lower file size to the first 8 bytes of the header.
501 *
502 * Returns zero on success; non-zero on error.
503 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700504static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700505{
Michael Halcrow237fead2006-10-04 02:16:22 -0700506 u64 file_size;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700507 char *file_size_virt;
508 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700509
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700510 file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
511 if (!file_size_virt) {
512 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700513 goto out;
514 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700515 file_size = (u64)i_size_read(ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700516 file_size = cpu_to_be64(file_size);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700517 memcpy(file_size_virt, &file_size, sizeof(u64));
518 rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
519 sizeof(u64));
520 kfree(file_size_virt);
521 if (rc)
522 printk(KERN_ERR "%s: Error writing file size to header; "
523 "rc = [%d]\n", __FUNCTION__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700524out:
525 return rc;
526}
527
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700528struct kmem_cache *ecryptfs_xattr_cache;
529
530static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800531{
532 ssize_t size;
533 void *xattr_virt;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700534 struct dentry *lower_dentry =
535 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
536 struct inode *lower_inode = lower_dentry->d_inode;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800537 u64 file_size;
538 int rc;
539
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700540 if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
541 printk(KERN_WARNING
542 "No support for setting xattr in lower filesystem\n");
543 rc = -ENOSYS;
544 goto out;
545 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800546 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
547 if (!xattr_virt) {
548 printk(KERN_ERR "Out of memory whilst attempting to write "
549 "inode size to xattr\n");
550 rc = -ENOMEM;
551 goto out;
552 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700553 mutex_lock(&lower_inode->i_mutex);
554 size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
555 xattr_virt, PAGE_CACHE_SIZE);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800556 if (size < 0)
557 size = 8;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700558 file_size = (u64)i_size_read(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800559 file_size = cpu_to_be64(file_size);
560 memcpy(xattr_virt, &file_size, sizeof(u64));
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700561 rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
562 xattr_virt, size, 0);
563 mutex_unlock(&lower_inode->i_mutex);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800564 if (rc)
565 printk(KERN_ERR "Error whilst attempting to write inode size "
566 "to lower file xattr; rc = [%d]\n", rc);
567 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
568out:
569 return rc;
570}
571
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700572int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800573{
574 struct ecryptfs_crypt_stat *crypt_stat;
575
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700576 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800577 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700578 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800579 else
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700580 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800581}
582
Michael Halcrow237fead2006-10-04 02:16:22 -0700583int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
584 struct file *lower_file,
585 unsigned long lower_page_index, int byte_offset,
586 int region_bytes)
587{
588 int rc = 0;
589
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800590 *lower_page = grab_cache_page(lower_inode->i_mapping, lower_page_index);
591 if (!(*lower_page)) {
592 rc = -EINVAL;
593 ecryptfs_printk(KERN_ERR, "Error attempting to grab "
Michael Halcrow237fead2006-10-04 02:16:22 -0700594 "lower page with index [0x%.16x]\n",
595 lower_page_index);
596 goto out;
597 }
598 rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
599 (*lower_page),
600 byte_offset,
601 region_bytes);
602 if (rc) {
Nick Piggin55144762007-10-16 01:25:26 -0700603 ecryptfs_printk(KERN_ERR, "prepare_write for "
604 "lower_page_index = [0x%.16x] failed; rc = "
605 "[%d]\n", lower_page_index, rc);
606 ecryptfs_release_lower_page(*lower_page);
607 (*lower_page) = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700608 }
609out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700610 return rc;
611}
612
613/**
614 * ecryptfs_commit_lower_page
615 *
616 * Returns zero on success; non-zero on error
617 */
618int
619ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
620 struct file *lower_file, int byte_offset,
621 int region_size)
622{
623 int rc = 0;
624
625 rc = lower_inode->i_mapping->a_ops->commit_write(
626 lower_file, lower_page, byte_offset, region_size);
627 if (rc < 0) {
628 ecryptfs_printk(KERN_ERR,
629 "Error committing write; rc = [%d]\n", rc);
630 } else
631 rc = 0;
Nick Piggin55144762007-10-16 01:25:26 -0700632 ecryptfs_release_lower_page(lower_page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700633 return rc;
634}
635
636/**
637 * ecryptfs_copy_page_to_lower
638 *
639 * Used for plaintext pass-through; no page index interpolation
640 * required.
641 */
642int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
643 struct file *lower_file)
644{
645 int rc = 0;
646 struct page *lower_page;
647
648 rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
649 page->index, 0, PAGE_CACHE_SIZE);
650 if (rc) {
651 ecryptfs_printk(KERN_ERR, "Error attempting to get page "
652 "at index [0x%.16x]\n", page->index);
653 goto out;
654 }
655 /* TODO: aops */
656 memcpy((char *)page_address(lower_page), page_address(page),
657 PAGE_CACHE_SIZE);
658 rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
659 0, PAGE_CACHE_SIZE);
660 if (rc)
661 ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
662 "at index [0x%.16x]\n", page->index);
663out:
664 return rc;
665}
666
Michael Halcrow237fead2006-10-04 02:16:22 -0700667/**
668 * ecryptfs_commit_write
669 * @file: The eCryptfs file object
670 * @page: The eCryptfs page
671 * @from: Ignored (we rotate the page IV on each write)
672 * @to: Ignored
673 *
674 * This is where we encrypt the data and pass the encrypted data to
675 * the lower filesystem. In OpenPGP-compatible mode, we operate on
676 * entire underlying packets.
677 */
678static int ecryptfs_commit_write(struct file *file, struct page *page,
679 unsigned from, unsigned to)
680{
Michael Halcrow237fead2006-10-04 02:16:22 -0700681 loff_t pos;
Michael Halcrowbf12be12007-10-16 01:28:11 -0700682 struct inode *ecryptfs_inode = page->mapping->host;
683 struct ecryptfs_crypt_stat *crypt_stat =
684 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700685 int rc;
686
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800687 if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700688 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
689 "crypt_stat at memory location [%p]\n", crypt_stat);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800690 crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700691 } else
692 ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
693 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
694 "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
695 to);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700696 /* Fills in zeros if 'to' goes beyond inode size */
Michael Halcrow237fead2006-10-04 02:16:22 -0700697 rc = fill_zeros_to_end_of_page(page, to);
698 if (rc) {
699 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
700 "zeros in page with index = [0x%.16x]\n",
701 page->index);
702 goto out;
703 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700704 rc = ecryptfs_encrypt_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700705 if (rc) {
706 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
707 "index [0x%.16x])\n", page->index);
708 goto out;
709 }
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700710 pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to;
Michael Halcrowbf12be12007-10-16 01:28:11 -0700711 if (pos > i_size_read(ecryptfs_inode)) {
712 i_size_write(ecryptfs_inode, pos);
Michael Halcrow237fead2006-10-04 02:16:22 -0700713 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
Michael Halcrowbf12be12007-10-16 01:28:11 -0700714 "[0x%.16x]\n", i_size_read(ecryptfs_inode));
Michael Halcrow237fead2006-10-04 02:16:22 -0700715 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700716 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800717 if (rc)
718 printk(KERN_ERR "Error writing inode size to metadata; "
719 "rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700720out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700721 return rc;
722}
723
724/**
Michael Halcrow240e2df2007-06-27 14:09:44 -0700725 * ecryptfs_write_zeros
Michael Halcrow237fead2006-10-04 02:16:22 -0700726 * @file: The ecryptfs file
727 * @index: The index in which we are writing
728 * @start: The position after the last block of data
729 * @num_zeros: The number of zeros to write
730 *
731 * Write a specified number of zero's to a page.
732 *
733 * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
734 */
Michael Halcrow240e2df2007-06-27 14:09:44 -0700735int
736ecryptfs_write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
Michael Halcrow237fead2006-10-04 02:16:22 -0700737{
738 int rc = 0;
739 struct page *tmp_page;
740
741 tmp_page = ecryptfs_get1page(file, index);
742 if (IS_ERR(tmp_page)) {
743 ecryptfs_printk(KERN_ERR, "Error getting page at index "
744 "[0x%.16x]\n", index);
745 rc = PTR_ERR(tmp_page);
746 goto out;
747 }
Michael Halcrow5dda6992007-10-16 01:28:06 -0700748 rc = ecryptfs_prepare_write_no_truncate(file, tmp_page, start,
749 (start + num_zeros));
750 if (rc) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700751 ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
Michael Halcrow53a27312007-05-23 13:58:15 -0700752 "to page at index [0x%.16x]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700753 index);
Michael Halcrow237fead2006-10-04 02:16:22 -0700754 page_cache_release(tmp_page);
755 goto out;
756 }
Nate Dillerc9f28752007-05-16 22:11:17 -0700757 zero_user_page(tmp_page, start, num_zeros, KM_USER0);
Michael Halcrow237fead2006-10-04 02:16:22 -0700758 rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
759 if (rc < 0) {
760 ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
761 "to remainder of page at index [0x%.16x]\n",
762 index);
Michael Halcrow237fead2006-10-04 02:16:22 -0700763 page_cache_release(tmp_page);
764 goto out;
765 }
766 rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700767 page_cache_release(tmp_page);
768out:
769 return rc;
770}
771
772static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
773{
774 int rc = 0;
775 struct inode *inode;
776 struct inode *lower_inode;
777
778 inode = (struct inode *)mapping->host;
779 lower_inode = ecryptfs_inode_to_lower(inode);
780 if (lower_inode->i_mapping->a_ops->bmap)
781 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
782 block);
783 return rc;
784}
785
Michael Halcrow237fead2006-10-04 02:16:22 -0700786struct address_space_operations ecryptfs_aops = {
787 .writepage = ecryptfs_writepage,
788 .readpage = ecryptfs_readpage,
789 .prepare_write = ecryptfs_prepare_write,
790 .commit_write = ecryptfs_commit_write,
791 .bmap = ecryptfs_bmap,
Michael Halcrow237fead2006-10-04 02:16:22 -0700792};