Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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 |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 9 | * Copyright (C) 2004-2007 International Business Machines Corp. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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 |
| 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 Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 37 | /** |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 38 | * ecryptfs_get_locked_page |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 39 | * |
| 40 | * Get one page from cache or lower f/s, return error otherwise. |
| 41 | * |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 42 | * Returns locked and up-to-date page (if ok), with increased |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 43 | * refcnt. |
| 44 | */ |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 45 | struct page *ecryptfs_get_locked_page(struct file *file, loff_t index) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 46 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 47 | struct dentry *dentry; |
| 48 | struct inode *inode; |
| 49 | struct address_space *mapping; |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 50 | struct page *page; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 51 | |
Josef "Jeff" Sipek | bd243a4 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 52 | dentry = file->f_path.dentry; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 53 | inode = dentry->d_inode; |
| 54 | mapping = inode->i_mapping; |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 55 | page = read_mapping_page(mapping, index, (void *)file); |
| 56 | if (!IS_ERR(page)) |
| 57 | lock_page(page); |
| 58 | return page; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 61 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 62 | * ecryptfs_writepage |
| 63 | * @page: Page that is locked before this call is made |
| 64 | * |
| 65 | * Returns zero on success; non-zero otherwise |
| 66 | */ |
| 67 | static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) |
| 68 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 69 | int rc; |
| 70 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 71 | rc = ecryptfs_encrypt_page(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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; |
| 77 | } |
| 78 | SetPageUptodate(page); |
| 79 | unlock_page(page); |
| 80 | out: |
| 81 | return rc; |
| 82 | } |
| 83 | |
| 84 | /** |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 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 |
| 98 | */ |
| 99 | static void set_header_info(char *page_virt, |
| 100 | struct ecryptfs_crypt_stat *crypt_stat) |
| 101 | { |
| 102 | size_t written; |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 103 | size_t save_num_header_bytes_at_front = |
| 104 | crypt_stat->num_header_bytes_at_front; |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 105 | |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 106 | crypt_stat->num_header_bytes_at_front = |
| 107 | ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE; |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 108 | ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written); |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 109 | crypt_stat->num_header_bytes_at_front = |
| 110 | save_num_header_bytes_at_front; |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 111 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 112 | |
| 113 | /** |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 114 | * ecryptfs_copy_up_encrypted_with_header |
| 115 | * @page: Sort of a ``virtual'' representation of the encrypted lower |
| 116 | * file. The actual lower file does not have the metadata in |
| 117 | * the header. This is locked. |
| 118 | * @crypt_stat: The eCryptfs inode's cryptographic context |
| 119 | * |
| 120 | * The ``view'' is the version of the file that userspace winds up |
| 121 | * seeing, with the header information inserted. |
| 122 | */ |
| 123 | static int |
| 124 | ecryptfs_copy_up_encrypted_with_header(struct page *page, |
| 125 | struct ecryptfs_crypt_stat *crypt_stat) |
| 126 | { |
| 127 | loff_t extent_num_in_page = 0; |
| 128 | loff_t num_extents_per_page = (PAGE_CACHE_SIZE |
| 129 | / crypt_stat->extent_size); |
| 130 | int rc = 0; |
| 131 | |
| 132 | while (extent_num_in_page < num_extents_per_page) { |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 133 | loff_t view_extent_num = ((((loff_t)page->index) |
| 134 | * num_extents_per_page) |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 135 | + extent_num_in_page); |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 136 | size_t num_header_extents_at_front = |
| 137 | (crypt_stat->num_header_bytes_at_front |
| 138 | / crypt_stat->extent_size); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 139 | |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 140 | if (view_extent_num < num_header_extents_at_front) { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 141 | /* This is a header extent */ |
| 142 | char *page_virt; |
| 143 | |
| 144 | page_virt = kmap_atomic(page, KM_USER0); |
| 145 | memset(page_virt, 0, PAGE_CACHE_SIZE); |
| 146 | /* TODO: Support more than one header extent */ |
| 147 | if (view_extent_num == 0) { |
| 148 | rc = ecryptfs_read_xattr_region( |
| 149 | page_virt, page->mapping->host); |
| 150 | set_header_info(page_virt, crypt_stat); |
| 151 | } |
| 152 | kunmap_atomic(page_virt, KM_USER0); |
| 153 | flush_dcache_page(page); |
| 154 | if (rc) { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 155 | printk(KERN_ERR "%s: Error reading xattr " |
| 156 | "region; rc = [%d]\n", __FUNCTION__, rc); |
| 157 | goto out; |
| 158 | } |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 159 | } else { |
| 160 | /* This is an encrypted data extent */ |
| 161 | loff_t lower_offset = |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 162 | ((view_extent_num * crypt_stat->extent_size) |
| 163 | - crypt_stat->num_header_bytes_at_front); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 164 | |
| 165 | rc = ecryptfs_read_lower_page_segment( |
| 166 | page, (lower_offset >> PAGE_CACHE_SHIFT), |
| 167 | (lower_offset & ~PAGE_CACHE_MASK), |
| 168 | crypt_stat->extent_size, page->mapping->host); |
| 169 | if (rc) { |
| 170 | printk(KERN_ERR "%s: Error attempting to read " |
| 171 | "extent at offset [%lld] in the lower " |
| 172 | "file; rc = [%d]\n", __FUNCTION__, |
| 173 | lower_offset, rc); |
| 174 | goto out; |
| 175 | } |
| 176 | } |
| 177 | extent_num_in_page++; |
| 178 | } |
| 179 | out: |
| 180 | return rc; |
| 181 | } |
| 182 | |
| 183 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 184 | * ecryptfs_readpage |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 185 | * @file: An eCryptfs file |
| 186 | * @page: Page from eCryptfs inode mapping into which to stick the read data |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 187 | * |
| 188 | * Read in a page, decrypting if necessary. |
| 189 | * |
| 190 | * Returns zero on success; non-zero on error. |
| 191 | */ |
| 192 | static int ecryptfs_readpage(struct file *file, struct page *page) |
| 193 | { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 194 | struct ecryptfs_crypt_stat *crypt_stat = |
| 195 | &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 196 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 197 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 198 | if (!crypt_stat |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 199 | || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED) |
| 200 | || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 201 | ecryptfs_printk(KERN_DEBUG, |
| 202 | "Passing through unencrypted page\n"); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 203 | rc = ecryptfs_read_lower_page_segment(page, page->index, 0, |
| 204 | PAGE_CACHE_SIZE, |
| 205 | page->mapping->host); |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 206 | } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { |
| 207 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 208 | rc = ecryptfs_copy_up_encrypted_with_header(page, |
| 209 | crypt_stat); |
| 210 | if (rc) { |
| 211 | printk(KERN_ERR "%s: Error attempting to copy " |
| 212 | "the encrypted content from the lower " |
| 213 | "file whilst inserting the metadata " |
| 214 | "from the xattr into the header; rc = " |
| 215 | "[%d]\n", __FUNCTION__, rc); |
| 216 | goto out; |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 217 | } |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 218 | |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 219 | } else { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 220 | rc = ecryptfs_read_lower_page_segment( |
| 221 | page, page->index, 0, PAGE_CACHE_SIZE, |
| 222 | page->mapping->host); |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 223 | if (rc) { |
| 224 | printk(KERN_ERR "Error reading page; rc = " |
| 225 | "[%d]\n", rc); |
| 226 | goto out; |
| 227 | } |
| 228 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 229 | } else { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 230 | rc = ecryptfs_decrypt_page(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 231 | if (rc) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 232 | ecryptfs_printk(KERN_ERR, "Error decrypting page; " |
| 233 | "rc = [%d]\n", rc); |
| 234 | goto out; |
| 235 | } |
| 236 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 237 | out: |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 238 | if (rc) |
| 239 | ClearPageUptodate(page); |
| 240 | else |
| 241 | SetPageUptodate(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 242 | ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n", |
| 243 | page->index); |
| 244 | unlock_page(page); |
| 245 | return rc; |
| 246 | } |
| 247 | |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 248 | /** |
| 249 | * Called with lower inode mutex held. |
| 250 | */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 251 | static int fill_zeros_to_end_of_page(struct page *page, unsigned int to) |
| 252 | { |
| 253 | struct inode *inode = page->mapping->host; |
| 254 | int end_byte_in_page; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 255 | |
Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 256 | if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index) |
| 257 | goto out; |
| 258 | end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE; |
| 259 | if (to > end_byte_in_page) |
| 260 | end_byte_in_page = to; |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 261 | zero_user_segment(page, end_byte_in_page, PAGE_CACHE_SIZE); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 262 | out: |
Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 263 | return 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 266 | /** |
| 267 | * ecryptfs_prepare_write |
| 268 | * @file: The eCryptfs file |
| 269 | * @page: The eCryptfs page |
| 270 | * @from: The start byte from which we will write |
| 271 | * @to: The end byte to which we will write |
| 272 | * |
| 273 | * This function must zero any hole we create |
| 274 | * |
| 275 | * Returns zero on success; non-zero otherwise |
| 276 | */ |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 277 | static int ecryptfs_prepare_write(struct file *file, struct page *page, |
| 278 | unsigned from, unsigned to) |
| 279 | { |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 280 | loff_t prev_page_end_size; |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 281 | int rc = 0; |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 282 | |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 283 | if (!PageUptodate(page)) { |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 284 | struct ecryptfs_crypt_stat *crypt_stat = |
| 285 | &ecryptfs_inode_to_private( |
| 286 | file->f_path.dentry->d_inode)->crypt_stat; |
| 287 | |
| 288 | if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED) |
| 289 | || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) { |
| 290 | rc = ecryptfs_read_lower_page_segment( |
| 291 | page, page->index, 0, PAGE_CACHE_SIZE, |
| 292 | page->mapping->host); |
| 293 | if (rc) { |
| 294 | printk(KERN_ERR "%s: Error attemping to read " |
| 295 | "lower page segment; rc = [%d]\n", |
| 296 | __FUNCTION__, rc); |
| 297 | ClearPageUptodate(page); |
| 298 | goto out; |
| 299 | } else |
| 300 | SetPageUptodate(page); |
| 301 | } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { |
| 302 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { |
| 303 | rc = ecryptfs_copy_up_encrypted_with_header( |
| 304 | page, crypt_stat); |
| 305 | if (rc) { |
| 306 | printk(KERN_ERR "%s: Error attempting " |
| 307 | "to copy the encrypted content " |
| 308 | "from the lower file whilst " |
| 309 | "inserting the metadata from " |
| 310 | "the xattr into the header; rc " |
| 311 | "= [%d]\n", __FUNCTION__, rc); |
| 312 | ClearPageUptodate(page); |
| 313 | goto out; |
| 314 | } |
| 315 | SetPageUptodate(page); |
| 316 | } else { |
| 317 | rc = ecryptfs_read_lower_page_segment( |
| 318 | page, page->index, 0, PAGE_CACHE_SIZE, |
| 319 | page->mapping->host); |
| 320 | if (rc) { |
| 321 | printk(KERN_ERR "%s: Error reading " |
| 322 | "page; rc = [%d]\n", |
| 323 | __FUNCTION__, rc); |
| 324 | ClearPageUptodate(page); |
| 325 | goto out; |
| 326 | } |
| 327 | SetPageUptodate(page); |
| 328 | } |
| 329 | } else { |
| 330 | rc = ecryptfs_decrypt_page(page); |
| 331 | if (rc) { |
| 332 | printk(KERN_ERR "%s: Error decrypting page " |
| 333 | "at index [%ld]; rc = [%d]\n", |
| 334 | __FUNCTION__, page->index, rc); |
| 335 | ClearPageUptodate(page); |
| 336 | goto out; |
| 337 | } |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 338 | SetPageUptodate(page); |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 339 | } |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 340 | } |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 341 | prev_page_end_size = ((loff_t)page->index << PAGE_CACHE_SHIFT); |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 342 | /* If creating a page or more of holes, zero them out via truncate. |
| 343 | * Note, this will increase i_size. */ |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 344 | if (page->index != 0) { |
| 345 | if (prev_page_end_size > i_size_read(page->mapping->host)) { |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 346 | rc = ecryptfs_truncate(file->f_path.dentry, |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 347 | prev_page_end_size); |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 348 | if (rc) { |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 349 | printk(KERN_ERR "%s: Error on attempt to " |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 350 | "truncate to (higher) offset [%lld];" |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 351 | " rc = [%d]\n", __FUNCTION__, |
| 352 | prev_page_end_size, rc); |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 353 | goto out; |
| 354 | } |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 355 | } |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 356 | } |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 357 | /* Writing to a new page, and creating a small hole from start |
| 358 | * of page? Zero it out. */ |
| 359 | if ((i_size_read(page->mapping->host) == prev_page_end_size) |
| 360 | && (from != 0)) |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 361 | zero_user(page, 0, PAGE_CACHE_SIZE); |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 362 | out: |
| 363 | return rc; |
| 364 | } |
| 365 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 366 | /** |
| 367 | * ecryptfs_write_inode_size_to_header |
| 368 | * |
| 369 | * Writes the lower file size to the first 8 bytes of the header. |
| 370 | * |
| 371 | * Returns zero on success; non-zero on error. |
| 372 | */ |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 373 | static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 374 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 375 | u64 file_size; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 376 | char *file_size_virt; |
| 377 | int rc; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 378 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 379 | file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL); |
| 380 | if (!file_size_virt) { |
| 381 | rc = -ENOMEM; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 382 | goto out; |
| 383 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 384 | file_size = (u64)i_size_read(ecryptfs_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 385 | file_size = cpu_to_be64(file_size); |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 386 | memcpy(file_size_virt, &file_size, sizeof(u64)); |
| 387 | rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0, |
| 388 | sizeof(u64)); |
| 389 | kfree(file_size_virt); |
| 390 | if (rc) |
| 391 | printk(KERN_ERR "%s: Error writing file size to header; " |
| 392 | "rc = [%d]\n", __FUNCTION__, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 393 | out: |
| 394 | return rc; |
| 395 | } |
| 396 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 397 | struct kmem_cache *ecryptfs_xattr_cache; |
| 398 | |
| 399 | static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 400 | { |
| 401 | ssize_t size; |
| 402 | void *xattr_virt; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 403 | struct dentry *lower_dentry = |
| 404 | ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry; |
| 405 | struct inode *lower_inode = lower_dentry->d_inode; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 406 | u64 file_size; |
| 407 | int rc; |
| 408 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 409 | if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) { |
| 410 | printk(KERN_WARNING |
| 411 | "No support for setting xattr in lower filesystem\n"); |
| 412 | rc = -ENOSYS; |
| 413 | goto out; |
| 414 | } |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 415 | xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL); |
| 416 | if (!xattr_virt) { |
| 417 | printk(KERN_ERR "Out of memory whilst attempting to write " |
| 418 | "inode size to xattr\n"); |
| 419 | rc = -ENOMEM; |
| 420 | goto out; |
| 421 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 422 | mutex_lock(&lower_inode->i_mutex); |
| 423 | size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME, |
| 424 | xattr_virt, PAGE_CACHE_SIZE); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 425 | if (size < 0) |
| 426 | size = 8; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 427 | file_size = (u64)i_size_read(ecryptfs_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 428 | file_size = cpu_to_be64(file_size); |
| 429 | memcpy(xattr_virt, &file_size, sizeof(u64)); |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 430 | rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME, |
| 431 | xattr_virt, size, 0); |
| 432 | mutex_unlock(&lower_inode->i_mutex); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 433 | if (rc) |
| 434 | printk(KERN_ERR "Error whilst attempting to write inode size " |
| 435 | "to lower file xattr; rc = [%d]\n", rc); |
| 436 | kmem_cache_free(ecryptfs_xattr_cache, xattr_virt); |
| 437 | out: |
| 438 | return rc; |
| 439 | } |
| 440 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 441 | int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 442 | { |
| 443 | struct ecryptfs_crypt_stat *crypt_stat; |
| 444 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 445 | crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 446 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 447 | return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 448 | else |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 449 | return ecryptfs_write_inode_size_to_header(ecryptfs_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 452 | /** |
| 453 | * ecryptfs_commit_write |
| 454 | * @file: The eCryptfs file object |
| 455 | * @page: The eCryptfs page |
| 456 | * @from: Ignored (we rotate the page IV on each write) |
| 457 | * @to: Ignored |
| 458 | * |
| 459 | * This is where we encrypt the data and pass the encrypted data to |
| 460 | * the lower filesystem. In OpenPGP-compatible mode, we operate on |
| 461 | * entire underlying packets. |
| 462 | */ |
| 463 | static int ecryptfs_commit_write(struct file *file, struct page *page, |
| 464 | unsigned from, unsigned to) |
| 465 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 466 | loff_t pos; |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 467 | struct inode *ecryptfs_inode = page->mapping->host; |
| 468 | struct ecryptfs_crypt_stat *crypt_stat = |
| 469 | &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 470 | int rc; |
| 471 | |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 472 | if (crypt_stat->flags & ECRYPTFS_NEW_FILE) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 473 | ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in " |
| 474 | "crypt_stat at memory location [%p]\n", crypt_stat); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 475 | crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 476 | } else |
| 477 | ecryptfs_printk(KERN_DEBUG, "Not a new file\n"); |
| 478 | ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" |
| 479 | "(page w/ index = [0x%.16x], to = [%d])\n", page->index, |
| 480 | to); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 481 | /* Fills in zeros if 'to' goes beyond inode size */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 482 | rc = fill_zeros_to_end_of_page(page, to); |
| 483 | if (rc) { |
| 484 | ecryptfs_printk(KERN_WARNING, "Error attempting to fill " |
| 485 | "zeros in page with index = [0x%.16x]\n", |
| 486 | page->index); |
| 487 | goto out; |
| 488 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 489 | rc = ecryptfs_encrypt_page(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 490 | if (rc) { |
| 491 | ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " |
| 492 | "index [0x%.16x])\n", page->index); |
| 493 | goto out; |
| 494 | } |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 495 | pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to; |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 496 | if (pos > i_size_read(ecryptfs_inode)) { |
| 497 | i_size_write(ecryptfs_inode, pos); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 498 | ecryptfs_printk(KERN_DEBUG, "Expanded file size to " |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 499 | "[0x%.16x]\n", i_size_read(ecryptfs_inode)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 500 | } |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 501 | rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 502 | if (rc) |
| 503 | printk(KERN_ERR "Error writing inode size to metadata; " |
| 504 | "rc = [%d]\n", rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 505 | out: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 506 | return rc; |
| 507 | } |
| 508 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 509 | static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block) |
| 510 | { |
| 511 | int rc = 0; |
| 512 | struct inode *inode; |
| 513 | struct inode *lower_inode; |
| 514 | |
| 515 | inode = (struct inode *)mapping->host; |
| 516 | lower_inode = ecryptfs_inode_to_lower(inode); |
| 517 | if (lower_inode->i_mapping->a_ops->bmap) |
| 518 | rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping, |
| 519 | block); |
| 520 | return rc; |
| 521 | } |
| 522 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 523 | struct address_space_operations ecryptfs_aops = { |
| 524 | .writepage = ecryptfs_writepage, |
| 525 | .readpage = ecryptfs_readpage, |
| 526 | .prepare_write = ecryptfs_prepare_write, |
| 527 | .commit_write = ecryptfs_commit_write, |
| 528 | .bmap = ecryptfs_bmap, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 529 | }; |