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 |
| 9 | * Copyright (C) 2004-2006 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 |
| 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 | |
| 37 | struct 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 | */ |
| 47 | static struct page *ecryptfs_get1page(struct file *file, int index) |
| 48 | { |
| 49 | struct page *page; |
| 50 | struct dentry *dentry; |
| 51 | struct inode *inode; |
| 52 | struct address_space *mapping; |
| 53 | |
| 54 | dentry = file->f_dentry; |
| 55 | inode = dentry->d_inode; |
| 56 | mapping = inode->i_mapping; |
| 57 | page = read_cache_page(mapping, index, |
| 58 | (filler_t *)mapping->a_ops->readpage, |
| 59 | (void *)file); |
| 60 | if (IS_ERR(page)) |
| 61 | goto out; |
| 62 | wait_on_page_locked(page); |
| 63 | out: |
| 64 | return page; |
| 65 | } |
| 66 | |
| 67 | static |
| 68 | int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros); |
| 69 | |
| 70 | /** |
| 71 | * ecryptfs_fill_zeros |
| 72 | * @file: The ecryptfs file |
| 73 | * @new_length: The new length of the data in the underlying file; |
| 74 | * everything between the prior end of the file and the |
| 75 | * new end of the file will be filled with zero's. |
| 76 | * new_length must be greater than current length |
| 77 | * |
| 78 | * Function for handling lseek-ing past the end of the file. |
| 79 | * |
| 80 | * This function does not support shrinking, only growing a file. |
| 81 | * |
| 82 | * Returns zero on success; non-zero otherwise. |
| 83 | */ |
| 84 | int ecryptfs_fill_zeros(struct file *file, loff_t new_length) |
| 85 | { |
| 86 | int rc = 0; |
| 87 | struct dentry *dentry = file->f_dentry; |
| 88 | struct inode *inode = dentry->d_inode; |
| 89 | pgoff_t old_end_page_index = 0; |
| 90 | pgoff_t index = old_end_page_index; |
| 91 | int old_end_pos_in_page = -1; |
| 92 | pgoff_t new_end_page_index; |
| 93 | int new_end_pos_in_page; |
| 94 | loff_t cur_length = i_size_read(inode); |
| 95 | |
| 96 | if (cur_length != 0) { |
| 97 | index = old_end_page_index = |
| 98 | ((cur_length - 1) >> PAGE_CACHE_SHIFT); |
| 99 | old_end_pos_in_page = ((cur_length - 1) & ~PAGE_CACHE_MASK); |
| 100 | } |
| 101 | new_end_page_index = ((new_length - 1) >> PAGE_CACHE_SHIFT); |
| 102 | new_end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK); |
| 103 | ecryptfs_printk(KERN_DEBUG, "old_end_page_index = [0x%.16x]; " |
| 104 | "old_end_pos_in_page = [%d]; " |
| 105 | "new_end_page_index = [0x%.16x]; " |
| 106 | "new_end_pos_in_page = [%d]\n", |
| 107 | old_end_page_index, old_end_pos_in_page, |
| 108 | new_end_page_index, new_end_pos_in_page); |
| 109 | if (old_end_page_index == new_end_page_index) { |
| 110 | /* Start and end are in the same page; we just need to |
| 111 | * set a portion of the existing page to zero's */ |
| 112 | rc = write_zeros(file, index, (old_end_pos_in_page + 1), |
| 113 | (new_end_pos_in_page - old_end_pos_in_page)); |
| 114 | if (rc) |
| 115 | ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], " |
| 116 | "index=[0x%.16x], " |
| 117 | "old_end_pos_in_page=[d], " |
| 118 | "(PAGE_CACHE_SIZE - new_end_pos_in_page" |
| 119 | "=[%d]" |
| 120 | ")=[d]) returned [%d]\n", file, index, |
| 121 | old_end_pos_in_page, |
| 122 | new_end_pos_in_page, |
| 123 | (PAGE_CACHE_SIZE - new_end_pos_in_page), |
| 124 | rc); |
| 125 | goto out; |
| 126 | } |
| 127 | /* Fill the remainder of the previous last page with zeros */ |
| 128 | rc = write_zeros(file, index, (old_end_pos_in_page + 1), |
| 129 | ((PAGE_CACHE_SIZE - 1) - old_end_pos_in_page)); |
| 130 | if (rc) { |
| 131 | ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], " |
| 132 | "index=[0x%.16x], old_end_pos_in_page=[d], " |
| 133 | "(PAGE_CACHE_SIZE - old_end_pos_in_page)=[d]) " |
| 134 | "returned [%d]\n", file, index, |
| 135 | old_end_pos_in_page, |
| 136 | (PAGE_CACHE_SIZE - old_end_pos_in_page), rc); |
| 137 | goto out; |
| 138 | } |
| 139 | index++; |
| 140 | while (index < new_end_page_index) { |
| 141 | /* Fill all intermediate pages with zeros */ |
| 142 | rc = write_zeros(file, index, 0, PAGE_CACHE_SIZE); |
| 143 | if (rc) { |
| 144 | ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], " |
| 145 | "index=[0x%.16x], " |
| 146 | "old_end_pos_in_page=[d], " |
| 147 | "(PAGE_CACHE_SIZE - new_end_pos_in_page" |
| 148 | "=[%d]" |
| 149 | ")=[d]) returned [%d]\n", file, index, |
| 150 | old_end_pos_in_page, |
| 151 | new_end_pos_in_page, |
| 152 | (PAGE_CACHE_SIZE - new_end_pos_in_page), |
| 153 | rc); |
| 154 | goto out; |
| 155 | } |
| 156 | index++; |
| 157 | } |
| 158 | /* Fill the portion at the beginning of the last new page with |
| 159 | * zero's */ |
| 160 | rc = write_zeros(file, index, 0, (new_end_pos_in_page + 1)); |
| 161 | if (rc) { |
| 162 | ecryptfs_printk(KERN_ERR, "write_zeros(file=" |
| 163 | "[%p], index=[0x%.16x], 0, " |
| 164 | "new_end_pos_in_page=[%d]" |
| 165 | "returned [%d]\n", file, index, |
| 166 | new_end_pos_in_page, rc); |
| 167 | goto out; |
| 168 | } |
| 169 | out: |
| 170 | return rc; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * ecryptfs_writepage |
| 175 | * @page: Page that is locked before this call is made |
| 176 | * |
| 177 | * Returns zero on success; non-zero otherwise |
| 178 | */ |
| 179 | static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) |
| 180 | { |
| 181 | struct ecryptfs_page_crypt_context ctx; |
| 182 | int rc; |
| 183 | |
| 184 | ctx.page = page; |
| 185 | ctx.mode = ECRYPTFS_WRITEPAGE_MODE; |
| 186 | ctx.param.wbc = wbc; |
| 187 | rc = ecryptfs_encrypt_page(&ctx); |
| 188 | if (rc) { |
| 189 | ecryptfs_printk(KERN_WARNING, "Error encrypting " |
| 190 | "page (upper index [0x%.16x])\n", page->index); |
| 191 | ClearPageUptodate(page); |
| 192 | goto out; |
| 193 | } |
| 194 | SetPageUptodate(page); |
| 195 | unlock_page(page); |
| 196 | out: |
| 197 | return rc; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Reads the data from the lower file file at index lower_page_index |
| 202 | * and copies that data into page. |
| 203 | * |
| 204 | * @param page Page to fill |
| 205 | * @param lower_page_index Index of the page in the lower file to get |
| 206 | */ |
| 207 | int ecryptfs_do_readpage(struct file *file, struct page *page, |
| 208 | pgoff_t lower_page_index) |
| 209 | { |
| 210 | int rc; |
| 211 | struct dentry *dentry; |
| 212 | struct file *lower_file; |
| 213 | struct dentry *lower_dentry; |
| 214 | struct inode *inode; |
| 215 | struct inode *lower_inode; |
| 216 | char *page_data; |
| 217 | struct page *lower_page = NULL; |
| 218 | char *lower_page_data; |
| 219 | const struct address_space_operations *lower_a_ops; |
| 220 | |
| 221 | dentry = file->f_dentry; |
| 222 | lower_file = ecryptfs_file_to_lower(file); |
| 223 | lower_dentry = ecryptfs_dentry_to_lower(dentry); |
| 224 | inode = dentry->d_inode; |
| 225 | lower_inode = ecryptfs_inode_to_lower(inode); |
| 226 | lower_a_ops = lower_inode->i_mapping->a_ops; |
| 227 | lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index, |
| 228 | (filler_t *)lower_a_ops->readpage, |
| 229 | (void *)lower_file); |
| 230 | if (IS_ERR(lower_page)) { |
| 231 | rc = PTR_ERR(lower_page); |
| 232 | lower_page = NULL; |
| 233 | ecryptfs_printk(KERN_ERR, "Error reading from page cache\n"); |
| 234 | goto out; |
| 235 | } |
| 236 | wait_on_page_locked(lower_page); |
| 237 | page_data = (char *)kmap(page); |
| 238 | if (!page_data) { |
| 239 | rc = -ENOMEM; |
| 240 | ecryptfs_printk(KERN_ERR, "Error mapping page\n"); |
| 241 | goto out; |
| 242 | } |
| 243 | lower_page_data = (char *)kmap(lower_page); |
| 244 | if (!lower_page_data) { |
| 245 | rc = -ENOMEM; |
| 246 | ecryptfs_printk(KERN_ERR, "Error mapping page\n"); |
| 247 | kunmap(page); |
| 248 | goto out; |
| 249 | } |
| 250 | memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE); |
| 251 | kunmap(lower_page); |
| 252 | kunmap(page); |
| 253 | rc = 0; |
| 254 | out: |
| 255 | if (likely(lower_page)) |
| 256 | page_cache_release(lower_page); |
| 257 | if (rc == 0) |
| 258 | SetPageUptodate(page); |
| 259 | else |
| 260 | ClearPageUptodate(page); |
| 261 | return rc; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * ecryptfs_readpage |
| 266 | * @file: This is an ecryptfs file |
| 267 | * @page: ecryptfs associated page to stick the read data into |
| 268 | * |
| 269 | * Read in a page, decrypting if necessary. |
| 270 | * |
| 271 | * Returns zero on success; non-zero on error. |
| 272 | */ |
| 273 | static int ecryptfs_readpage(struct file *file, struct page *page) |
| 274 | { |
| 275 | int rc = 0; |
| 276 | struct ecryptfs_crypt_stat *crypt_stat; |
| 277 | |
| 278 | BUG_ON(!(file && file->f_dentry && file->f_dentry->d_inode)); |
| 279 | crypt_stat = |
| 280 | &ecryptfs_inode_to_private(file->f_dentry->d_inode)->crypt_stat; |
| 281 | if (!crypt_stat |
| 282 | || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED) |
| 283 | || ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) { |
| 284 | ecryptfs_printk(KERN_DEBUG, |
| 285 | "Passing through unencrypted page\n"); |
| 286 | rc = ecryptfs_do_readpage(file, page, page->index); |
| 287 | if (rc) { |
| 288 | ecryptfs_printk(KERN_ERR, "Error reading page; rc = " |
| 289 | "[%d]\n", rc); |
| 290 | goto out; |
| 291 | } |
| 292 | } else { |
| 293 | rc = ecryptfs_decrypt_page(file, page); |
| 294 | if (rc) { |
| 295 | |
| 296 | ecryptfs_printk(KERN_ERR, "Error decrypting page; " |
| 297 | "rc = [%d]\n", rc); |
| 298 | goto out; |
| 299 | } |
| 300 | } |
| 301 | SetPageUptodate(page); |
| 302 | out: |
| 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 | static 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) { |
| 319 | end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE; |
| 320 | if (to > end_byte_in_page) |
| 321 | end_byte_in_page = to; |
| 322 | page_virt = kmap(page); |
| 323 | if (!page_virt) { |
| 324 | rc = -ENOMEM; |
| 325 | ecryptfs_printk(KERN_WARNING, |
| 326 | "Could not map page\n"); |
| 327 | goto out; |
| 328 | } |
| 329 | memset((page_virt + end_byte_in_page), 0, |
| 330 | (PAGE_CACHE_SIZE - end_byte_in_page)); |
| 331 | kunmap(page); |
| 332 | } |
| 333 | out: |
| 334 | return rc; |
| 335 | } |
| 336 | |
| 337 | static int ecryptfs_prepare_write(struct file *file, struct page *page, |
| 338 | unsigned from, unsigned to) |
| 339 | { |
| 340 | int rc = 0; |
| 341 | |
| 342 | kmap(page); |
| 343 | if (from == 0 && to == PAGE_CACHE_SIZE) |
| 344 | goto out; /* If we are writing a full page, it will be |
| 345 | up to date. */ |
| 346 | if (!PageUptodate(page)) |
| 347 | rc = ecryptfs_do_readpage(file, page, page->index); |
| 348 | out: |
| 349 | return rc; |
| 350 | } |
| 351 | |
| 352 | int ecryptfs_grab_and_map_lower_page(struct page **lower_page, |
| 353 | char **lower_virt, |
| 354 | struct inode *lower_inode, |
| 355 | unsigned long lower_page_index) |
| 356 | { |
| 357 | int rc = 0; |
| 358 | |
| 359 | (*lower_page) = grab_cache_page(lower_inode->i_mapping, |
| 360 | lower_page_index); |
| 361 | if (!(*lower_page)) { |
| 362 | ecryptfs_printk(KERN_ERR, "grab_cache_page for " |
| 363 | "lower_page_index = [0x%.16x] failed\n", |
| 364 | lower_page_index); |
| 365 | rc = -EINVAL; |
| 366 | goto out; |
| 367 | } |
| 368 | if (lower_virt) |
| 369 | (*lower_virt) = kmap((*lower_page)); |
| 370 | else |
| 371 | kmap((*lower_page)); |
| 372 | out: |
| 373 | return rc; |
| 374 | } |
| 375 | |
| 376 | int ecryptfs_writepage_and_release_lower_page(struct page *lower_page, |
| 377 | struct inode *lower_inode, |
| 378 | struct writeback_control *wbc) |
| 379 | { |
| 380 | int rc = 0; |
| 381 | |
| 382 | rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc); |
| 383 | if (rc) { |
| 384 | ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); " |
| 385 | "rc = [%d]\n", rc); |
| 386 | goto out; |
| 387 | } |
| 388 | lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; |
| 389 | page_cache_release(lower_page); |
| 390 | out: |
| 391 | return rc; |
| 392 | } |
| 393 | |
| 394 | static void ecryptfs_unmap_and_release_lower_page(struct page *lower_page) |
| 395 | { |
| 396 | kunmap(lower_page); |
| 397 | ecryptfs_printk(KERN_DEBUG, "Unlocking lower page with index = " |
| 398 | "[0x%.16x]\n", lower_page->index); |
| 399 | unlock_page(lower_page); |
| 400 | page_cache_release(lower_page); |
| 401 | } |
| 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 | */ |
| 410 | int |
| 411 | ecryptfs_write_inode_size_to_header(struct file *lower_file, |
| 412 | struct inode *lower_inode, |
| 413 | 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, |
| 422 | lower_inode, 0); |
| 423 | if (rc) { |
| 424 | ecryptfs_printk(KERN_ERR, "grab_cache_page for header page " |
| 425 | "failed\n"); |
| 426 | goto out; |
| 427 | } |
| 428 | lower_a_ops = lower_inode->i_mapping->a_ops; |
| 429 | rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8); |
| 430 | file_size = (u64)i_size_read(inode); |
| 431 | ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size); |
| 432 | file_size = cpu_to_be64(file_size); |
| 433 | memcpy(header_virt, &file_size, sizeof(u64)); |
| 434 | rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8); |
| 435 | if (rc < 0) |
| 436 | ecryptfs_printk(KERN_ERR, "Error commiting header page " |
| 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); |
| 441 | out: |
| 442 | return rc; |
| 443 | } |
| 444 | |
| 445 | int 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, |
| 453 | lower_page_index); |
| 454 | if (rc) { |
| 455 | ecryptfs_printk(KERN_ERR, "Error attempting to grab and map " |
| 456 | "lower page with index [0x%.16x]\n", |
| 457 | lower_page_index); |
| 458 | goto out; |
| 459 | } |
| 460 | rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file, |
| 461 | (*lower_page), |
| 462 | byte_offset, |
| 463 | region_bytes); |
| 464 | if (rc) { |
| 465 | ecryptfs_printk(KERN_ERR, "prepare_write for " |
| 466 | "lower_page_index = [0x%.16x] failed; rc = " |
| 467 | "[%d]\n", lower_page_index, rc); |
| 468 | } |
| 469 | out: |
| 470 | if (rc && (*lower_page)) { |
| 471 | ecryptfs_unmap_and_release_lower_page(*lower_page); |
| 472 | (*lower_page) = NULL; |
| 473 | } |
| 474 | return rc; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * ecryptfs_commit_lower_page |
| 479 | * |
| 480 | * Returns zero on success; non-zero on error |
| 481 | */ |
| 482 | int |
| 483 | ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode, |
| 484 | struct file *lower_file, int byte_offset, |
| 485 | int region_size) |
| 486 | { |
| 487 | int rc = 0; |
| 488 | |
| 489 | rc = lower_inode->i_mapping->a_ops->commit_write( |
| 490 | lower_file, lower_page, byte_offset, region_size); |
| 491 | if (rc < 0) { |
| 492 | ecryptfs_printk(KERN_ERR, |
| 493 | "Error committing write; rc = [%d]\n", rc); |
| 494 | } else |
| 495 | rc = 0; |
| 496 | ecryptfs_unmap_and_release_lower_page(lower_page); |
| 497 | return rc; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * ecryptfs_copy_page_to_lower |
| 502 | * |
| 503 | * Used for plaintext pass-through; no page index interpolation |
| 504 | * required. |
| 505 | */ |
| 506 | int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode, |
| 507 | struct file *lower_file) |
| 508 | { |
| 509 | int rc = 0; |
| 510 | struct page *lower_page; |
| 511 | |
| 512 | rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file, |
| 513 | page->index, 0, PAGE_CACHE_SIZE); |
| 514 | if (rc) { |
| 515 | ecryptfs_printk(KERN_ERR, "Error attempting to get page " |
| 516 | "at index [0x%.16x]\n", page->index); |
| 517 | goto out; |
| 518 | } |
| 519 | /* TODO: aops */ |
| 520 | memcpy((char *)page_address(lower_page), page_address(page), |
| 521 | PAGE_CACHE_SIZE); |
| 522 | rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file, |
| 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); |
| 527 | out: |
| 528 | return rc; |
| 529 | } |
| 530 | |
| 531 | static int |
| 532 | process_new_file(struct ecryptfs_crypt_stat *crypt_stat, |
| 533 | struct file *file, struct inode *inode) |
| 534 | { |
| 535 | struct page *header_page; |
| 536 | const struct address_space_operations *lower_a_ops; |
| 537 | struct inode *lower_inode; |
| 538 | struct file *lower_file; |
| 539 | char *header_virt; |
| 540 | int rc = 0; |
| 541 | int current_header_page = 0; |
| 542 | int header_pages; |
| 543 | int more_header_data_to_be_written = 1; |
| 544 | |
| 545 | lower_inode = ecryptfs_inode_to_lower(inode); |
| 546 | lower_file = ecryptfs_file_to_lower(file); |
| 547 | lower_a_ops = lower_inode->i_mapping->a_ops; |
| 548 | header_pages = ((crypt_stat->header_extent_size |
| 549 | * crypt_stat->num_header_extents_at_front) |
| 550 | / PAGE_CACHE_SIZE); |
| 551 | BUG_ON(header_pages < 1); |
| 552 | while (current_header_page < header_pages) { |
| 553 | rc = ecryptfs_grab_and_map_lower_page(&header_page, |
| 554 | &header_virt, |
| 555 | lower_inode, |
| 556 | current_header_page); |
| 557 | if (rc) { |
| 558 | ecryptfs_printk(KERN_ERR, "grab_cache_page for " |
| 559 | "header page [%d] failed; rc = [%d]\n", |
| 560 | current_header_page, rc); |
| 561 | goto out; |
| 562 | } |
| 563 | rc = lower_a_ops->prepare_write(lower_file, header_page, 0, |
| 564 | PAGE_CACHE_SIZE); |
| 565 | if (rc) { |
| 566 | ecryptfs_printk(KERN_ERR, "Error preparing to write " |
| 567 | "header page out; rc = [%d]\n", rc); |
| 568 | goto out; |
| 569 | } |
| 570 | memset(header_virt, 0, PAGE_CACHE_SIZE); |
| 571 | if (more_header_data_to_be_written) { |
| 572 | rc = ecryptfs_write_headers_virt(header_virt, |
| 573 | crypt_stat, |
| 574 | file->f_dentry); |
| 575 | if (rc) { |
| 576 | ecryptfs_printk(KERN_WARNING, "Error " |
| 577 | "generating header; rc = " |
| 578 | "[%d]\n", rc); |
| 579 | rc = -EIO; |
| 580 | memset(header_virt, 0, PAGE_CACHE_SIZE); |
| 581 | ecryptfs_unmap_and_release_lower_page( |
| 582 | header_page); |
| 583 | goto out; |
| 584 | } |
| 585 | if (current_header_page == 0) |
| 586 | memset(header_virt, 0, 8); |
| 587 | more_header_data_to_be_written = 0; |
| 588 | } |
| 589 | rc = lower_a_ops->commit_write(lower_file, header_page, 0, |
| 590 | PAGE_CACHE_SIZE); |
| 591 | ecryptfs_unmap_and_release_lower_page(header_page); |
| 592 | if (rc < 0) { |
| 593 | ecryptfs_printk(KERN_ERR, |
| 594 | "Error commiting header page write; " |
| 595 | "rc = [%d]\n", rc); |
| 596 | break; |
| 597 | } |
| 598 | current_header_page++; |
| 599 | } |
| 600 | if (rc >= 0) { |
| 601 | rc = 0; |
| 602 | ecryptfs_printk(KERN_DEBUG, "lower_inode->i_blocks = " |
| 603 | "[0x%.16x]\n", lower_inode->i_blocks); |
| 604 | i_size_write(inode, 0); |
| 605 | lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; |
| 606 | mark_inode_dirty_sync(inode); |
| 607 | } |
| 608 | ecryptfs_printk(KERN_DEBUG, "Clearing ECRYPTFS_NEW_FILE flag in " |
| 609 | "crypt_stat at memory location [%p]\n", crypt_stat); |
| 610 | ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE); |
| 611 | out: |
| 612 | return rc; |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * ecryptfs_commit_write |
| 617 | * @file: The eCryptfs file object |
| 618 | * @page: The eCryptfs page |
| 619 | * @from: Ignored (we rotate the page IV on each write) |
| 620 | * @to: Ignored |
| 621 | * |
| 622 | * This is where we encrypt the data and pass the encrypted data to |
| 623 | * the lower filesystem. In OpenPGP-compatible mode, we operate on |
| 624 | * entire underlying packets. |
| 625 | */ |
| 626 | static int ecryptfs_commit_write(struct file *file, struct page *page, |
| 627 | unsigned from, unsigned to) |
| 628 | { |
| 629 | struct ecryptfs_page_crypt_context ctx; |
| 630 | loff_t pos; |
| 631 | struct inode *inode; |
| 632 | struct inode *lower_inode; |
| 633 | struct file *lower_file; |
| 634 | struct ecryptfs_crypt_stat *crypt_stat; |
| 635 | int rc; |
| 636 | |
| 637 | inode = page->mapping->host; |
| 638 | lower_inode = ecryptfs_inode_to_lower(inode); |
| 639 | lower_file = ecryptfs_file_to_lower(file); |
| 640 | mutex_lock(&lower_inode->i_mutex); |
| 641 | crypt_stat = |
| 642 | &ecryptfs_inode_to_private(file->f_dentry->d_inode)->crypt_stat; |
| 643 | if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) { |
| 644 | ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in " |
| 645 | "crypt_stat at memory location [%p]\n", crypt_stat); |
| 646 | rc = process_new_file(crypt_stat, file, inode); |
| 647 | if (rc) { |
| 648 | ecryptfs_printk(KERN_ERR, "Error processing new " |
| 649 | "file; rc = [%d]\n", rc); |
| 650 | goto out; |
| 651 | } |
| 652 | } else |
| 653 | ecryptfs_printk(KERN_DEBUG, "Not a new file\n"); |
| 654 | ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" |
| 655 | "(page w/ index = [0x%.16x], to = [%d])\n", page->index, |
| 656 | to); |
| 657 | rc = fill_zeros_to_end_of_page(page, to); |
| 658 | if (rc) { |
| 659 | ecryptfs_printk(KERN_WARNING, "Error attempting to fill " |
| 660 | "zeros in page with index = [0x%.16x]\n", |
| 661 | page->index); |
| 662 | goto out; |
| 663 | } |
| 664 | ctx.page = page; |
| 665 | ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE; |
| 666 | ctx.param.lower_file = lower_file; |
| 667 | rc = ecryptfs_encrypt_page(&ctx); |
| 668 | if (rc) { |
| 669 | ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " |
| 670 | "index [0x%.16x])\n", page->index); |
| 671 | goto out; |
| 672 | } |
| 673 | rc = 0; |
| 674 | inode->i_blocks = lower_inode->i_blocks; |
| 675 | pos = (page->index << PAGE_CACHE_SHIFT) + to; |
| 676 | if (pos > i_size_read(inode)) { |
| 677 | i_size_write(inode, pos); |
| 678 | ecryptfs_printk(KERN_DEBUG, "Expanded file size to " |
| 679 | "[0x%.16x]\n", i_size_read(inode)); |
| 680 | } |
| 681 | ecryptfs_write_inode_size_to_header(lower_file, lower_inode, inode); |
| 682 | lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; |
| 683 | mark_inode_dirty_sync(inode); |
| 684 | out: |
| 685 | kunmap(page); /* mapped in prior call (prepare_write) */ |
| 686 | if (rc < 0) |
| 687 | ClearPageUptodate(page); |
| 688 | else |
| 689 | SetPageUptodate(page); |
| 690 | mutex_unlock(&lower_inode->i_mutex); |
| 691 | return rc; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * write_zeros |
| 696 | * @file: The ecryptfs file |
| 697 | * @index: The index in which we are writing |
| 698 | * @start: The position after the last block of data |
| 699 | * @num_zeros: The number of zeros to write |
| 700 | * |
| 701 | * Write a specified number of zero's to a page. |
| 702 | * |
| 703 | * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE |
| 704 | */ |
| 705 | static |
| 706 | int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros) |
| 707 | { |
| 708 | int rc = 0; |
| 709 | struct page *tmp_page; |
| 710 | |
| 711 | tmp_page = ecryptfs_get1page(file, index); |
| 712 | if (IS_ERR(tmp_page)) { |
| 713 | ecryptfs_printk(KERN_ERR, "Error getting page at index " |
| 714 | "[0x%.16x]\n", index); |
| 715 | rc = PTR_ERR(tmp_page); |
| 716 | goto out; |
| 717 | } |
| 718 | kmap(tmp_page); |
| 719 | rc = ecryptfs_prepare_write(file, tmp_page, start, start + num_zeros); |
| 720 | if (rc) { |
| 721 | ecryptfs_printk(KERN_ERR, "Error preparing to write zero's " |
| 722 | "to remainder of page at index [0x%.16x]\n", |
| 723 | index); |
| 724 | kunmap(tmp_page); |
| 725 | page_cache_release(tmp_page); |
| 726 | goto out; |
| 727 | } |
| 728 | memset(((char *)page_address(tmp_page) + start), 0, num_zeros); |
| 729 | rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros); |
| 730 | if (rc < 0) { |
| 731 | ecryptfs_printk(KERN_ERR, "Error attempting to write zero's " |
| 732 | "to remainder of page at index [0x%.16x]\n", |
| 733 | index); |
| 734 | kunmap(tmp_page); |
| 735 | page_cache_release(tmp_page); |
| 736 | goto out; |
| 737 | } |
| 738 | rc = 0; |
| 739 | kunmap(tmp_page); |
| 740 | page_cache_release(tmp_page); |
| 741 | out: |
| 742 | return rc; |
| 743 | } |
| 744 | |
| 745 | static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block) |
| 746 | { |
| 747 | int rc = 0; |
| 748 | struct inode *inode; |
| 749 | struct inode *lower_inode; |
| 750 | |
| 751 | inode = (struct inode *)mapping->host; |
| 752 | lower_inode = ecryptfs_inode_to_lower(inode); |
| 753 | if (lower_inode->i_mapping->a_ops->bmap) |
| 754 | rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping, |
| 755 | block); |
| 756 | return rc; |
| 757 | } |
| 758 | |
| 759 | static void ecryptfs_sync_page(struct page *page) |
| 760 | { |
| 761 | struct inode *inode; |
| 762 | struct inode *lower_inode; |
| 763 | struct page *lower_page; |
| 764 | |
| 765 | inode = page->mapping->host; |
| 766 | lower_inode = ecryptfs_inode_to_lower(inode); |
| 767 | /* NOTE: Recently swapped with grab_cache_page(), since |
| 768 | * sync_page() just makes sure that pending I/O gets done. */ |
| 769 | lower_page = find_lock_page(lower_inode->i_mapping, page->index); |
| 770 | if (!lower_page) { |
| 771 | ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n"); |
| 772 | return; |
| 773 | } |
| 774 | lower_page->mapping->a_ops->sync_page(lower_page); |
| 775 | ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n", |
| 776 | lower_page->index); |
| 777 | unlock_page(lower_page); |
| 778 | page_cache_release(lower_page); |
| 779 | } |
| 780 | |
| 781 | struct address_space_operations ecryptfs_aops = { |
| 782 | .writepage = ecryptfs_writepage, |
| 783 | .readpage = ecryptfs_readpage, |
| 784 | .prepare_write = ecryptfs_prepare_write, |
| 785 | .commit_write = ecryptfs_commit_write, |
| 786 | .bmap = ecryptfs_bmap, |
| 787 | .sync_page = ecryptfs_sync_page, |
| 788 | }; |