fs/ecryptfs/: possible cleanups
[linux-2.6/sactl.git] / fs / ecryptfs / mmap.c
blob9a5e0d17f1c5f03e08e12662688729758138dfa6
1 /**
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.
7 * Copyright (C) 1997-2003 Erez Zadok
8 * Copyright (C) 2001-2003 Stony Brook University
9 * Copyright (C) 2004-2007 International Business Machines Corp.
10 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
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.
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.
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.
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"
37 /**
38 * ecryptfs_get_locked_page
40 * Get one page from cache or lower f/s, return error otherwise.
42 * Returns locked and up-to-date page (if ok), with increased
43 * refcnt.
45 struct page *ecryptfs_get_locked_page(struct file *file, loff_t index)
47 struct dentry *dentry;
48 struct inode *inode;
49 struct address_space *mapping;
50 struct page *page;
52 dentry = file->f_path.dentry;
53 inode = dentry->d_inode;
54 mapping = inode->i_mapping;
55 page = read_mapping_page(mapping, index, (void *)file);
56 if (!IS_ERR(page))
57 lock_page(page);
58 return page;
61 /**
62 * ecryptfs_writepage
63 * @page: Page that is locked before this call is made
65 * Returns zero on success; non-zero otherwise
67 static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
69 int rc;
71 rc = ecryptfs_encrypt_page(page);
72 if (rc) {
73 ecryptfs_printk(KERN_WARNING, "Error encrypting "
74 "page (upper index [0x%.16x])\n", page->index);
75 ClearPageUptodate(page);
76 goto out;
78 SetPageUptodate(page);
79 unlock_page(page);
80 out:
81 return rc;
84 /**
85 * 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
99 static void set_header_info(char *page_virt,
100 struct ecryptfs_crypt_stat *crypt_stat)
102 size_t written;
103 int save_num_header_extents_at_front =
104 crypt_stat->num_header_extents_at_front;
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;
113 * 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
119 * The ``view'' is the version of the file that userspace winds up
120 * seeing, with the header information inserted.
122 static int
123 ecryptfs_copy_up_encrypted_with_header(struct page *page,
124 struct ecryptfs_crypt_stat *crypt_stat)
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;
131 while (extent_num_in_page < num_extents_per_page) {
132 loff_t view_extent_num = ((((loff_t)page->index)
133 * num_extents_per_page)
134 + extent_num_in_page);
136 if (view_extent_num < crypt_stat->num_header_extents_at_front) {
137 /* This is a header extent */
138 char *page_virt;
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);
148 kunmap_atomic(page_virt, KM_USER0);
149 flush_dcache_page(page);
150 if (rc) {
151 printk(KERN_ERR "%s: Error reading xattr "
152 "region; rc = [%d]\n", __FUNCTION__, rc);
153 goto out;
155 } 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);
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;
174 extent_num_in_page++;
176 out:
177 return rc;
181 * ecryptfs_readpage
182 * @file: An eCryptfs file
183 * @page: Page from eCryptfs inode mapping into which to stick the read data
185 * Read in a page, decrypting if necessary.
187 * Returns zero on success; non-zero on error.
189 static int ecryptfs_readpage(struct file *file, struct page *page)
191 struct ecryptfs_crypt_stat *crypt_stat =
192 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
193 int rc = 0;
195 if (!crypt_stat
196 || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
197 || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
198 ecryptfs_printk(KERN_DEBUG,
199 "Passing through unencrypted page\n");
200 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
201 PAGE_CACHE_SIZE,
202 page->mapping->host);
203 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
204 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
205 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;
216 } else {
217 rc = ecryptfs_read_lower_page_segment(
218 page, page->index, 0, PAGE_CACHE_SIZE,
219 page->mapping->host);
220 if (rc) {
221 printk(KERN_ERR "Error reading page; rc = "
222 "[%d]\n", rc);
223 goto out;
226 } else {
227 rc = ecryptfs_decrypt_page(page);
228 if (rc) {
229 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
230 "rc = [%d]\n", rc);
231 goto out;
234 out:
235 if (rc)
236 ClearPageUptodate(page);
237 else
238 SetPageUptodate(page);
239 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
240 page->index);
241 unlock_page(page);
242 return rc;
246 * Called with lower inode mutex held.
248 static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
250 struct inode *inode = page->mapping->host;
251 int end_byte_in_page;
253 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;
258 zero_user_segment(page, end_byte_in_page, PAGE_CACHE_SIZE);
259 out:
260 return 0;
263 /* This function must zero any hole we create */
264 static int ecryptfs_prepare_write(struct file *file, struct page *page,
265 unsigned from, unsigned to)
267 int rc = 0;
268 loff_t prev_page_end_size;
270 if (!PageUptodate(page)) {
271 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
272 PAGE_CACHE_SIZE,
273 page->mapping->host);
274 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);
283 prev_page_end_size = ((loff_t)page->index << PAGE_CACHE_SHIFT);
286 * If creating a page or more of holes, zero them out via truncate.
287 * Note, this will increase i_size.
289 if (page->index != 0) {
290 if (prev_page_end_size > i_size_read(page->mapping->host)) {
291 rc = ecryptfs_truncate(file->f_path.dentry,
292 prev_page_end_size);
293 if (rc) {
294 printk(KERN_ERR "Error on attempt to "
295 "truncate to (higher) offset [%lld];"
296 " rc = [%d]\n", prev_page_end_size, rc);
297 goto out;
302 * Writing to a new page, and creating a small hole from start of page?
303 * Zero it out.
305 if ((i_size_read(page->mapping->host) == prev_page_end_size) &&
306 (from != 0)) {
307 zero_user(page, 0, PAGE_CACHE_SIZE);
309 out:
310 return rc;
314 * ecryptfs_write_inode_size_to_header
316 * Writes the lower file size to the first 8 bytes of the header.
318 * Returns zero on success; non-zero on error.
320 static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
322 u64 file_size;
323 char *file_size_virt;
324 int rc;
326 file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
327 if (!file_size_virt) {
328 rc = -ENOMEM;
329 goto out;
331 file_size = (u64)i_size_read(ecryptfs_inode);
332 file_size = cpu_to_be64(file_size);
333 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);
340 out:
341 return rc;
344 struct kmem_cache *ecryptfs_xattr_cache;
346 static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
348 ssize_t size;
349 void *xattr_virt;
350 struct dentry *lower_dentry =
351 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
352 struct inode *lower_inode = lower_dentry->d_inode;
353 u64 file_size;
354 int rc;
356 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;
362 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;
369 mutex_lock(&lower_inode->i_mutex);
370 size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
371 xattr_virt, PAGE_CACHE_SIZE);
372 if (size < 0)
373 size = 8;
374 file_size = (u64)i_size_read(ecryptfs_inode);
375 file_size = cpu_to_be64(file_size);
376 memcpy(xattr_virt, &file_size, sizeof(u64));
377 rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
378 xattr_virt, size, 0);
379 mutex_unlock(&lower_inode->i_mutex);
380 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);
384 out:
385 return rc;
388 int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
390 struct ecryptfs_crypt_stat *crypt_stat;
392 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
393 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
394 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
395 else
396 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
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
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.
410 static int ecryptfs_commit_write(struct file *file, struct page *page,
411 unsigned from, unsigned to)
413 loff_t pos;
414 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;
417 int rc;
419 if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
420 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
421 "crypt_stat at memory location [%p]\n", crypt_stat);
422 crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
423 } 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);
428 /* Fills in zeros if 'to' goes beyond inode size */
429 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;
436 rc = ecryptfs_encrypt_page(page);
437 if (rc) {
438 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
439 "index [0x%.16x])\n", page->index);
440 goto out;
442 pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to;
443 if (pos > i_size_read(ecryptfs_inode)) {
444 i_size_write(ecryptfs_inode, pos);
445 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
446 "[0x%.16x]\n", i_size_read(ecryptfs_inode));
448 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
449 if (rc)
450 printk(KERN_ERR "Error writing inode size to metadata; "
451 "rc = [%d]\n", rc);
452 out:
453 return rc;
456 static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
458 int rc = 0;
459 struct inode *inode;
460 struct inode *lower_inode;
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;
470 struct 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,