mmap.c (dddfa461fc8951f9b5f951c13565b6cac678635a) mmap.c (dd2a3b7ad98f8482cae481cad89dfed5eee48365)
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.
6 *
7 * Copyright (C) 1997-2003 Erez Zadok
8 * Copyright (C) 2001-2003 Stony Brook University
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.
6 *
7 * Copyright (C) 1997-2003 Erez Zadok
8 * Copyright (C) 2001-2003 Stony Brook University
9 * Copyright (C) 2004-2006 International Business Machines Corp.
9 * Copyright (C) 2004-2007 International Business Machines Corp.
10 * 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

--- 285 unchanged lines hidden (view full) ---

303 if (rc)
304 ClearPageUptodate(page);
305 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
306 page->index);
307 unlock_page(page);
308 return rc;
309}
310
10 * 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

--- 285 unchanged lines hidden (view full) ---

303 if (rc)
304 ClearPageUptodate(page);
305 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
306 page->index);
307 unlock_page(page);
308 return rc;
309}
310
311/**
312 * Called with lower inode mutex held.
313 */
311static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
312{
313 struct inode *inode = page->mapping->host;
314 int end_byte_in_page;
315 int rc = 0;
316 char *page_virt;
317
318 if ((i_size_read(inode) / PAGE_CACHE_SIZE) == page->index) {

--- 83 unchanged lines hidden (view full) ---

402
403/**
404 * ecryptfs_write_inode_size_to_header
405 *
406 * Writes the lower file size to the first 8 bytes of the header.
407 *
408 * Returns zero on success; non-zero on error.
409 */
314static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
315{
316 struct inode *inode = page->mapping->host;
317 int end_byte_in_page;
318 int rc = 0;
319 char *page_virt;
320
321 if ((i_size_read(inode) / PAGE_CACHE_SIZE) == page->index) {

--- 83 unchanged lines hidden (view full) ---

405
406/**
407 * ecryptfs_write_inode_size_to_header
408 *
409 * Writes the lower file size to the first 8 bytes of the header.
410 *
411 * Returns zero on success; non-zero on error.
412 */
410int
411ecryptfs_write_inode_size_to_header(struct file *lower_file,
412 struct inode *lower_inode,
413 struct inode *inode)
413static int ecryptfs_write_inode_size_to_header(struct file *lower_file,
414 struct inode *lower_inode,
415 struct inode *inode)
414{
415 int rc = 0;
416 struct page *header_page;
417 char *header_virt;
418 const struct address_space_operations *lower_a_ops;
419 u64 file_size;
420
421 rc = ecryptfs_grab_and_map_lower_page(&header_page, &header_virt,

--- 15 unchanged lines hidden (view full) ---

437 "write\n");
438 ecryptfs_unmap_and_release_lower_page(header_page);
439 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
440 mark_inode_dirty_sync(inode);
441out:
442 return rc;
443}
444
416{
417 int rc = 0;
418 struct page *header_page;
419 char *header_virt;
420 const struct address_space_operations *lower_a_ops;
421 u64 file_size;
422
423 rc = ecryptfs_grab_and_map_lower_page(&header_page, &header_virt,

--- 15 unchanged lines hidden (view full) ---

439 "write\n");
440 ecryptfs_unmap_and_release_lower_page(header_page);
441 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
442 mark_inode_dirty_sync(inode);
443out:
444 return rc;
445}
446
447static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode,
448 struct inode *inode,
449 struct dentry *ecryptfs_dentry,
450 int lower_i_mutex_held)
451{
452 ssize_t size;
453 void *xattr_virt;
454 struct dentry *lower_dentry;
455 u64 file_size;
456 int rc;
457
458 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
459 if (!xattr_virt) {
460 printk(KERN_ERR "Out of memory whilst attempting to write "
461 "inode size to xattr\n");
462 rc = -ENOMEM;
463 goto out;
464 }
465 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
466 if (!lower_dentry->d_inode->i_op->getxattr) {
467 printk(KERN_WARNING
468 "No support for setting xattr in lower filesystem\n");
469 rc = -ENOSYS;
470 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
471 goto out;
472 }
473 if (!lower_i_mutex_held)
474 mutex_lock(&lower_dentry->d_inode->i_mutex);
475 size = lower_dentry->d_inode->i_op->getxattr(lower_dentry,
476 ECRYPTFS_XATTR_NAME,
477 xattr_virt,
478 PAGE_CACHE_SIZE);
479 if (!lower_i_mutex_held)
480 mutex_unlock(&lower_dentry->d_inode->i_mutex);
481 if (size < 0)
482 size = 8;
483 file_size = (u64)i_size_read(inode);
484 file_size = cpu_to_be64(file_size);
485 memcpy(xattr_virt, &file_size, sizeof(u64));
486 if (!lower_i_mutex_held)
487 mutex_lock(&lower_dentry->d_inode->i_mutex);
488 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry,
489 ECRYPTFS_XATTR_NAME,
490 xattr_virt, size, 0);
491 if (!lower_i_mutex_held)
492 mutex_unlock(&lower_dentry->d_inode->i_mutex);
493 if (rc)
494 printk(KERN_ERR "Error whilst attempting to write inode size "
495 "to lower file xattr; rc = [%d]\n", rc);
496 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
497out:
498 return rc;
499}
500
501int
502ecryptfs_write_inode_size_to_metadata(struct file *lower_file,
503 struct inode *lower_inode,
504 struct inode *inode,
505 struct dentry *ecryptfs_dentry,
506 int lower_i_mutex_held)
507{
508 struct ecryptfs_crypt_stat *crypt_stat;
509
510 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
511 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
512 return ecryptfs_write_inode_size_to_xattr(lower_inode, inode,
513 ecryptfs_dentry,
514 lower_i_mutex_held);
515 else
516 return ecryptfs_write_inode_size_to_header(lower_file,
517 lower_inode,
518 inode);
519}
520
445int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
446 struct file *lower_file,
447 unsigned long lower_page_index, int byte_offset,
448 int region_bytes)
449{
450 int rc = 0;
451
452 rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL, lower_inode,

--- 70 unchanged lines hidden (view full) ---

523 0, PAGE_CACHE_SIZE);
524 if (rc)
525 ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
526 "at index [0x%.16x]\n", page->index);
527out:
528 return rc;
529}
530
521int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
522 struct file *lower_file,
523 unsigned long lower_page_index, int byte_offset,
524 int region_bytes)
525{
526 int rc = 0;
527
528 rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL, lower_inode,

--- 70 unchanged lines hidden (view full) ---

599 0, PAGE_CACHE_SIZE);
600 if (rc)
601 ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
602 "at index [0x%.16x]\n", page->index);
603out:
604 return rc;
605}
606
607struct kmem_cache *ecryptfs_xattr_cache;
608
531/**
532 * ecryptfs_commit_write
533 * @file: The eCryptfs file object
534 * @page: The eCryptfs page
535 * @from: Ignored (we rotate the page IV on each write)
536 * @to: Ignored
537 *
538 * This is where we encrypt the data and pass the encrypted data to

--- 37 unchanged lines hidden (view full) ---

576 ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE;
577 ctx.param.lower_file = lower_file;
578 rc = ecryptfs_encrypt_page(&ctx);
579 if (rc) {
580 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
581 "index [0x%.16x])\n", page->index);
582 goto out;
583 }
609/**
610 * ecryptfs_commit_write
611 * @file: The eCryptfs file object
612 * @page: The eCryptfs page
613 * @from: Ignored (we rotate the page IV on each write)
614 * @to: Ignored
615 *
616 * This is where we encrypt the data and pass the encrypted data to

--- 37 unchanged lines hidden (view full) ---

654 ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE;
655 ctx.param.lower_file = lower_file;
656 rc = ecryptfs_encrypt_page(&ctx);
657 if (rc) {
658 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
659 "index [0x%.16x])\n", page->index);
660 goto out;
661 }
584 rc = 0;
585 inode->i_blocks = lower_inode->i_blocks;
586 pos = (page->index << PAGE_CACHE_SHIFT) + to;
587 if (pos > i_size_read(inode)) {
588 i_size_write(inode, pos);
589 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
590 "[0x%.16x]\n", i_size_read(inode));
591 }
662 inode->i_blocks = lower_inode->i_blocks;
663 pos = (page->index << PAGE_CACHE_SHIFT) + to;
664 if (pos > i_size_read(inode)) {
665 i_size_write(inode, pos);
666 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
667 "[0x%.16x]\n", i_size_read(inode));
668 }
592 ecryptfs_write_inode_size_to_header(lower_file, lower_inode, inode);
669 rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode,
670 inode, file->f_dentry,
671 ECRYPTFS_LOWER_I_MUTEX_HELD);
672 if (rc)
673 printk(KERN_ERR "Error writing inode size to metadata; "
674 "rc = [%d]\n", rc);
593 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
594 mark_inode_dirty_sync(inode);
595out:
596 kunmap(page); /* mapped in prior call (prepare_write) */
597 if (rc < 0)
598 ClearPageUptodate(page);
599 else
600 SetPageUptodate(page);

--- 99 unchanged lines hidden ---
675 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
676 mark_inode_dirty_sync(inode);
677out:
678 kunmap(page); /* mapped in prior call (prepare_write) */
679 if (rc < 0)
680 ClearPageUptodate(page);
681 else
682 SetPageUptodate(page);

--- 99 unchanged lines hidden ---