diff options
Diffstat (limited to 'fs/ecryptfs/mmap.c')
-rw-r--r-- | fs/ecryptfs/mmap.c | 378 |
1 files changed, 199 insertions, 179 deletions
diff --git a/fs/ecryptfs/mmap.c b/fs/ecryptfs/mmap.c index 06843d24f239..3a6f65c3f14f 100644 --- a/fs/ecryptfs/mmap.c +++ b/fs/ecryptfs/mmap.c | |||
@@ -6,7 +6,7 @@ | |||
6 | * | 6 | * |
7 | * Copyright (C) 1997-2003 Erez Zadok | 7 | * Copyright (C) 1997-2003 Erez Zadok |
8 | * Copyright (C) 2001-2003 Stony Brook University | 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> | 10 | * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> |
11 | * | 11 | * |
12 | * This program is free software; you can redistribute it and/or | 12 | * This program is free software; you can redistribute it and/or |
@@ -234,22 +234,13 @@ int ecryptfs_do_readpage(struct file *file, struct page *page, | |||
234 | goto out; | 234 | goto out; |
235 | } | 235 | } |
236 | wait_on_page_locked(lower_page); | 236 | wait_on_page_locked(lower_page); |
237 | page_data = (char *)kmap(page); | 237 | page_data = kmap_atomic(page, KM_USER0); |
238 | if (!page_data) { | 238 | lower_page_data = kmap_atomic(lower_page, KM_USER1); |
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); | 239 | memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE); |
251 | kunmap(lower_page); | 240 | kunmap_atomic(lower_page_data, KM_USER1); |
252 | kunmap(page); | 241 | flush_dcache_page(lower_page); |
242 | kunmap_atomic(page_data, KM_USER0); | ||
243 | flush_dcache_page(page); | ||
253 | rc = 0; | 244 | rc = 0; |
254 | out: | 245 | out: |
255 | if (likely(lower_page)) | 246 | if (likely(lower_page)) |
@@ -260,6 +251,33 @@ out: | |||
260 | ClearPageUptodate(page); | 251 | ClearPageUptodate(page); |
261 | return rc; | 252 | return rc; |
262 | } | 253 | } |
254 | /** | ||
255 | * Header Extent: | ||
256 | * Octets 0-7: Unencrypted file size (big-endian) | ||
257 | * Octets 8-15: eCryptfs special marker | ||
258 | * Octets 16-19: Flags | ||
259 | * Octet 16: File format version number (between 0 and 255) | ||
260 | * Octets 17-18: Reserved | ||
261 | * Octet 19: Bit 1 (lsb): Reserved | ||
262 | * Bit 2: Encrypted? | ||
263 | * Bits 3-8: Reserved | ||
264 | * Octets 20-23: Header extent size (big-endian) | ||
265 | * Octets 24-25: Number of header extents at front of file | ||
266 | * (big-endian) | ||
267 | * Octet 26: Begin RFC 2440 authentication token packet set | ||
268 | */ | ||
269 | static void set_header_info(char *page_virt, | ||
270 | struct ecryptfs_crypt_stat *crypt_stat) | ||
271 | { | ||
272 | size_t written; | ||
273 | int save_num_header_extents_at_front = | ||
274 | crypt_stat->num_header_extents_at_front; | ||
275 | |||
276 | crypt_stat->num_header_extents_at_front = 1; | ||
277 | ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written); | ||
278 | crypt_stat->num_header_extents_at_front = | ||
279 | save_num_header_extents_at_front; | ||
280 | } | ||
263 | 281 | ||
264 | /** | 282 | /** |
265 | * ecryptfs_readpage | 283 | * ecryptfs_readpage |
@@ -279,8 +297,8 @@ static int ecryptfs_readpage(struct file *file, struct page *page) | |||
279 | crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode) | 297 | crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode) |
280 | ->crypt_stat; | 298 | ->crypt_stat; |
281 | if (!crypt_stat | 299 | if (!crypt_stat |
282 | || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED) | 300 | || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED) |
283 | || ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) { | 301 | || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) { |
284 | ecryptfs_printk(KERN_DEBUG, | 302 | ecryptfs_printk(KERN_DEBUG, |
285 | "Passing through unencrypted page\n"); | 303 | "Passing through unencrypted page\n"); |
286 | rc = ecryptfs_do_readpage(file, page, page->index); | 304 | rc = ecryptfs_do_readpage(file, page, page->index); |
@@ -289,10 +307,51 @@ static int ecryptfs_readpage(struct file *file, struct page *page) | |||
289 | "[%d]\n", rc); | 307 | "[%d]\n", rc); |
290 | goto out; | 308 | goto out; |
291 | } | 309 | } |
310 | } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { | ||
311 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { | ||
312 | int num_pages_in_header_region = | ||
313 | (crypt_stat->header_extent_size | ||
314 | / PAGE_CACHE_SIZE); | ||
315 | |||
316 | if (page->index < num_pages_in_header_region) { | ||
317 | char *page_virt; | ||
318 | |||
319 | page_virt = kmap_atomic(page, KM_USER0); | ||
320 | memset(page_virt, 0, PAGE_CACHE_SIZE); | ||
321 | if (page->index == 0) { | ||
322 | rc = ecryptfs_read_xattr_region( | ||
323 | page_virt, file->f_path.dentry); | ||
324 | set_header_info(page_virt, crypt_stat); | ||
325 | } | ||
326 | kunmap_atomic(page_virt, KM_USER0); | ||
327 | flush_dcache_page(page); | ||
328 | if (rc) { | ||
329 | printk(KERN_ERR "Error reading xattr " | ||
330 | "region\n"); | ||
331 | goto out; | ||
332 | } | ||
333 | } else { | ||
334 | rc = ecryptfs_do_readpage( | ||
335 | file, page, | ||
336 | (page->index | ||
337 | - num_pages_in_header_region)); | ||
338 | if (rc) { | ||
339 | printk(KERN_ERR "Error reading page; " | ||
340 | "rc = [%d]\n", rc); | ||
341 | goto out; | ||
342 | } | ||
343 | } | ||
344 | } else { | ||
345 | rc = ecryptfs_do_readpage(file, page, page->index); | ||
346 | if (rc) { | ||
347 | printk(KERN_ERR "Error reading page; rc = " | ||
348 | "[%d]\n", rc); | ||
349 | goto out; | ||
350 | } | ||
351 | } | ||
292 | } else { | 352 | } else { |
293 | rc = ecryptfs_decrypt_page(file, page); | 353 | rc = ecryptfs_decrypt_page(file, page); |
294 | if (rc) { | 354 | if (rc) { |
295 | |||
296 | ecryptfs_printk(KERN_ERR, "Error decrypting page; " | 355 | ecryptfs_printk(KERN_ERR, "Error decrypting page; " |
297 | "rc = [%d]\n", rc); | 356 | "rc = [%d]\n", rc); |
298 | goto out; | 357 | goto out; |
@@ -308,30 +367,27 @@ out: | |||
308 | return rc; | 367 | return rc; |
309 | } | 368 | } |
310 | 369 | ||
370 | /** | ||
371 | * Called with lower inode mutex held. | ||
372 | */ | ||
311 | static int fill_zeros_to_end_of_page(struct page *page, unsigned int to) | 373 | static int fill_zeros_to_end_of_page(struct page *page, unsigned int to) |
312 | { | 374 | { |
313 | struct inode *inode = page->mapping->host; | 375 | struct inode *inode = page->mapping->host; |
314 | int end_byte_in_page; | 376 | int end_byte_in_page; |
315 | int rc = 0; | ||
316 | char *page_virt; | 377 | char *page_virt; |
317 | 378 | ||
318 | if ((i_size_read(inode) / PAGE_CACHE_SIZE) == page->index) { | 379 | if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index) |
319 | end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE; | 380 | goto out; |
320 | if (to > end_byte_in_page) | 381 | end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE; |
321 | end_byte_in_page = to; | 382 | if (to > end_byte_in_page) |
322 | page_virt = kmap(page); | 383 | end_byte_in_page = to; |
323 | if (!page_virt) { | 384 | page_virt = kmap_atomic(page, KM_USER0); |
324 | rc = -ENOMEM; | 385 | memset((page_virt + end_byte_in_page), 0, |
325 | ecryptfs_printk(KERN_WARNING, | 386 | (PAGE_CACHE_SIZE - end_byte_in_page)); |
326 | "Could not map page\n"); | 387 | kunmap_atomic(page_virt, KM_USER0); |
327 | goto out; | 388 | flush_dcache_page(page); |
328 | } | ||
329 | memset((page_virt + end_byte_in_page), 0, | ||
330 | (PAGE_CACHE_SIZE - end_byte_in_page)); | ||
331 | kunmap(page); | ||
332 | } | ||
333 | out: | 389 | out: |
334 | return rc; | 390 | return 0; |
335 | } | 391 | } |
336 | 392 | ||
337 | static int ecryptfs_prepare_write(struct file *file, struct page *page, | 393 | static int ecryptfs_prepare_write(struct file *file, struct page *page, |
@@ -339,7 +395,6 @@ static int ecryptfs_prepare_write(struct file *file, struct page *page, | |||
339 | { | 395 | { |
340 | int rc = 0; | 396 | int rc = 0; |
341 | 397 | ||
342 | kmap(page); | ||
343 | if (from == 0 && to == PAGE_CACHE_SIZE) | 398 | if (from == 0 && to == PAGE_CACHE_SIZE) |
344 | goto out; /* If we are writing a full page, it will be | 399 | goto out; /* If we are writing a full page, it will be |
345 | up to date. */ | 400 | up to date. */ |
@@ -349,30 +404,6 @@ out: | |||
349 | return rc; | 404 | return rc; |
350 | } | 405 | } |
351 | 406 | ||
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, | 407 | int ecryptfs_writepage_and_release_lower_page(struct page *lower_page, |
377 | struct inode *lower_inode, | 408 | struct inode *lower_inode, |
378 | struct writeback_control *wbc) | 409 | struct writeback_control *wbc) |
@@ -391,11 +422,8 @@ out: | |||
391 | return rc; | 422 | return rc; |
392 | } | 423 | } |
393 | 424 | ||
394 | static void ecryptfs_unmap_and_release_lower_page(struct page *lower_page) | 425 | static void ecryptfs_release_lower_page(struct page *lower_page) |
395 | { | 426 | { |
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); | 427 | unlock_page(lower_page); |
400 | page_cache_release(lower_page); | 428 | page_cache_release(lower_page); |
401 | } | 429 | } |
@@ -407,10 +435,9 @@ static void ecryptfs_unmap_and_release_lower_page(struct page *lower_page) | |||
407 | * | 435 | * |
408 | * Returns zero on success; non-zero on error. | 436 | * Returns zero on success; non-zero on error. |
409 | */ | 437 | */ |
410 | int | 438 | static int ecryptfs_write_inode_size_to_header(struct file *lower_file, |
411 | ecryptfs_write_inode_size_to_header(struct file *lower_file, | 439 | struct inode *lower_inode, |
412 | struct inode *lower_inode, | 440 | struct inode *inode) |
413 | struct inode *inode) | ||
414 | { | 441 | { |
415 | int rc = 0; | 442 | int rc = 0; |
416 | struct page *header_page; | 443 | struct page *header_page; |
@@ -418,11 +445,11 @@ ecryptfs_write_inode_size_to_header(struct file *lower_file, | |||
418 | const struct address_space_operations *lower_a_ops; | 445 | const struct address_space_operations *lower_a_ops; |
419 | u64 file_size; | 446 | u64 file_size; |
420 | 447 | ||
421 | rc = ecryptfs_grab_and_map_lower_page(&header_page, &header_virt, | 448 | header_page = grab_cache_page(lower_inode->i_mapping, 0); |
422 | lower_inode, 0); | 449 | if (!header_page) { |
423 | if (rc) { | 450 | ecryptfs_printk(KERN_ERR, "grab_cache_page for " |
424 | ecryptfs_printk(KERN_ERR, "grab_cache_page for header page " | 451 | "lower_page_index 0 failed\n"); |
425 | "failed\n"); | 452 | rc = -EINVAL; |
426 | goto out; | 453 | goto out; |
427 | } | 454 | } |
428 | lower_a_ops = lower_inode->i_mapping->a_ops; | 455 | lower_a_ops = lower_inode->i_mapping->a_ops; |
@@ -430,18 +457,95 @@ ecryptfs_write_inode_size_to_header(struct file *lower_file, | |||
430 | file_size = (u64)i_size_read(inode); | 457 | file_size = (u64)i_size_read(inode); |
431 | ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size); | 458 | ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size); |
432 | file_size = cpu_to_be64(file_size); | 459 | file_size = cpu_to_be64(file_size); |
460 | header_virt = kmap_atomic(header_page, KM_USER0); | ||
433 | memcpy(header_virt, &file_size, sizeof(u64)); | 461 | memcpy(header_virt, &file_size, sizeof(u64)); |
462 | kunmap_atomic(header_virt, KM_USER0); | ||
463 | flush_dcache_page(header_page); | ||
434 | rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8); | 464 | rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8); |
435 | if (rc < 0) | 465 | if (rc < 0) |
436 | ecryptfs_printk(KERN_ERR, "Error commiting header page " | 466 | ecryptfs_printk(KERN_ERR, "Error commiting header page " |
437 | "write\n"); | 467 | "write\n"); |
438 | ecryptfs_unmap_and_release_lower_page(header_page); | 468 | ecryptfs_release_lower_page(header_page); |
439 | lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; | 469 | lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; |
440 | mark_inode_dirty_sync(inode); | 470 | mark_inode_dirty_sync(inode); |
441 | out: | 471 | out: |
442 | return rc; | 472 | return rc; |
443 | } | 473 | } |
444 | 474 | ||
475 | static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode, | ||
476 | struct inode *inode, | ||
477 | struct dentry *ecryptfs_dentry, | ||
478 | int lower_i_mutex_held) | ||
479 | { | ||
480 | ssize_t size; | ||
481 | void *xattr_virt; | ||
482 | struct dentry *lower_dentry; | ||
483 | u64 file_size; | ||
484 | int rc; | ||
485 | |||
486 | xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL); | ||
487 | if (!xattr_virt) { | ||
488 | printk(KERN_ERR "Out of memory whilst attempting to write " | ||
489 | "inode size to xattr\n"); | ||
490 | rc = -ENOMEM; | ||
491 | goto out; | ||
492 | } | ||
493 | lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); | ||
494 | if (!lower_dentry->d_inode->i_op->getxattr) { | ||
495 | printk(KERN_WARNING | ||
496 | "No support for setting xattr in lower filesystem\n"); | ||
497 | rc = -ENOSYS; | ||
498 | kmem_cache_free(ecryptfs_xattr_cache, xattr_virt); | ||
499 | goto out; | ||
500 | } | ||
501 | if (!lower_i_mutex_held) | ||
502 | mutex_lock(&lower_dentry->d_inode->i_mutex); | ||
503 | size = lower_dentry->d_inode->i_op->getxattr(lower_dentry, | ||
504 | ECRYPTFS_XATTR_NAME, | ||
505 | xattr_virt, | ||
506 | PAGE_CACHE_SIZE); | ||
507 | if (!lower_i_mutex_held) | ||
508 | mutex_unlock(&lower_dentry->d_inode->i_mutex); | ||
509 | if (size < 0) | ||
510 | size = 8; | ||
511 | file_size = (u64)i_size_read(inode); | ||
512 | file_size = cpu_to_be64(file_size); | ||
513 | memcpy(xattr_virt, &file_size, sizeof(u64)); | ||
514 | if (!lower_i_mutex_held) | ||
515 | mutex_lock(&lower_dentry->d_inode->i_mutex); | ||
516 | rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, | ||
517 | ECRYPTFS_XATTR_NAME, | ||
518 | xattr_virt, size, 0); | ||
519 | if (!lower_i_mutex_held) | ||
520 | mutex_unlock(&lower_dentry->d_inode->i_mutex); | ||
521 | if (rc) | ||
522 | printk(KERN_ERR "Error whilst attempting to write inode size " | ||
523 | "to lower file xattr; rc = [%d]\n", rc); | ||
524 | kmem_cache_free(ecryptfs_xattr_cache, xattr_virt); | ||
525 | out: | ||
526 | return rc; | ||
527 | } | ||
528 | |||
529 | int | ||
530 | ecryptfs_write_inode_size_to_metadata(struct file *lower_file, | ||
531 | struct inode *lower_inode, | ||
532 | struct inode *inode, | ||
533 | struct dentry *ecryptfs_dentry, | ||
534 | int lower_i_mutex_held) | ||
535 | { | ||
536 | struct ecryptfs_crypt_stat *crypt_stat; | ||
537 | |||
538 | crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; | ||
539 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) | ||
540 | return ecryptfs_write_inode_size_to_xattr(lower_inode, inode, | ||
541 | ecryptfs_dentry, | ||
542 | lower_i_mutex_held); | ||
543 | else | ||
544 | return ecryptfs_write_inode_size_to_header(lower_file, | ||
545 | lower_inode, | ||
546 | inode); | ||
547 | } | ||
548 | |||
445 | int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode, | 549 | int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode, |
446 | struct file *lower_file, | 550 | struct file *lower_file, |
447 | unsigned long lower_page_index, int byte_offset, | 551 | unsigned long lower_page_index, int byte_offset, |
@@ -449,10 +553,10 @@ int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode, | |||
449 | { | 553 | { |
450 | int rc = 0; | 554 | int rc = 0; |
451 | 555 | ||
452 | rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL, lower_inode, | 556 | *lower_page = grab_cache_page(lower_inode->i_mapping, lower_page_index); |
453 | lower_page_index); | 557 | if (!(*lower_page)) { |
454 | if (rc) { | 558 | rc = -EINVAL; |
455 | ecryptfs_printk(KERN_ERR, "Error attempting to grab and map " | 559 | ecryptfs_printk(KERN_ERR, "Error attempting to grab " |
456 | "lower page with index [0x%.16x]\n", | 560 | "lower page with index [0x%.16x]\n", |
457 | lower_page_index); | 561 | lower_page_index); |
458 | goto out; | 562 | goto out; |
@@ -468,7 +572,7 @@ int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode, | |||
468 | } | 572 | } |
469 | out: | 573 | out: |
470 | if (rc && (*lower_page)) { | 574 | if (rc && (*lower_page)) { |
471 | ecryptfs_unmap_and_release_lower_page(*lower_page); | 575 | ecryptfs_release_lower_page(*lower_page); |
472 | (*lower_page) = NULL; | 576 | (*lower_page) = NULL; |
473 | } | 577 | } |
474 | return rc; | 578 | return rc; |
@@ -493,7 +597,7 @@ ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode, | |||
493 | "Error committing write; rc = [%d]\n", rc); | 597 | "Error committing write; rc = [%d]\n", rc); |
494 | } else | 598 | } else |
495 | rc = 0; | 599 | rc = 0; |
496 | ecryptfs_unmap_and_release_lower_page(lower_page); | 600 | ecryptfs_release_lower_page(lower_page); |
497 | return rc; | 601 | return rc; |
498 | } | 602 | } |
499 | 603 | ||
@@ -528,89 +632,7 @@ out: | |||
528 | return rc; | 632 | return rc; |
529 | } | 633 | } |
530 | 634 | ||
531 | static int | 635 | struct kmem_cache *ecryptfs_xattr_cache; |
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 | 636 | ||
615 | /** | 637 | /** |
616 | * ecryptfs_commit_write | 638 | * ecryptfs_commit_write |
@@ -640,15 +662,10 @@ static int ecryptfs_commit_write(struct file *file, struct page *page, | |||
640 | mutex_lock(&lower_inode->i_mutex); | 662 | mutex_lock(&lower_inode->i_mutex); |
641 | crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode) | 663 | crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode) |
642 | ->crypt_stat; | 664 | ->crypt_stat; |
643 | if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) { | 665 | if (crypt_stat->flags & ECRYPTFS_NEW_FILE) { |
644 | ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in " | 666 | ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in " |
645 | "crypt_stat at memory location [%p]\n", crypt_stat); | 667 | "crypt_stat at memory location [%p]\n", crypt_stat); |
646 | rc = process_new_file(crypt_stat, file, inode); | 668 | crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE); |
647 | if (rc) { | ||
648 | ecryptfs_printk(KERN_ERR, "Error processing new " | ||
649 | "file; rc = [%d]\n", rc); | ||
650 | goto out; | ||
651 | } | ||
652 | } else | 669 | } else |
653 | ecryptfs_printk(KERN_DEBUG, "Not a new file\n"); | 670 | ecryptfs_printk(KERN_DEBUG, "Not a new file\n"); |
654 | ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" | 671 | ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" |
@@ -670,7 +687,6 @@ static int ecryptfs_commit_write(struct file *file, struct page *page, | |||
670 | "index [0x%.16x])\n", page->index); | 687 | "index [0x%.16x])\n", page->index); |
671 | goto out; | 688 | goto out; |
672 | } | 689 | } |
673 | rc = 0; | ||
674 | inode->i_blocks = lower_inode->i_blocks; | 690 | inode->i_blocks = lower_inode->i_blocks; |
675 | pos = (page->index << PAGE_CACHE_SHIFT) + to; | 691 | pos = (page->index << PAGE_CACHE_SHIFT) + to; |
676 | if (pos > i_size_read(inode)) { | 692 | if (pos > i_size_read(inode)) { |
@@ -678,11 +694,15 @@ static int ecryptfs_commit_write(struct file *file, struct page *page, | |||
678 | ecryptfs_printk(KERN_DEBUG, "Expanded file size to " | 694 | ecryptfs_printk(KERN_DEBUG, "Expanded file size to " |
679 | "[0x%.16x]\n", i_size_read(inode)); | 695 | "[0x%.16x]\n", i_size_read(inode)); |
680 | } | 696 | } |
681 | ecryptfs_write_inode_size_to_header(lower_file, lower_inode, inode); | 697 | rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode, |
698 | inode, file->f_dentry, | ||
699 | ECRYPTFS_LOWER_I_MUTEX_HELD); | ||
700 | if (rc) | ||
701 | printk(KERN_ERR "Error writing inode size to metadata; " | ||
702 | "rc = [%d]\n", rc); | ||
682 | lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; | 703 | lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; |
683 | mark_inode_dirty_sync(inode); | 704 | mark_inode_dirty_sync(inode); |
684 | out: | 705 | out: |
685 | kunmap(page); /* mapped in prior call (prepare_write) */ | ||
686 | if (rc < 0) | 706 | if (rc < 0) |
687 | ClearPageUptodate(page); | 707 | ClearPageUptodate(page); |
688 | else | 708 | else |
@@ -707,6 +727,7 @@ int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros) | |||
707 | { | 727 | { |
708 | int rc = 0; | 728 | int rc = 0; |
709 | struct page *tmp_page; | 729 | struct page *tmp_page; |
730 | char *tmp_page_virt; | ||
710 | 731 | ||
711 | tmp_page = ecryptfs_get1page(file, index); | 732 | tmp_page = ecryptfs_get1page(file, index); |
712 | if (IS_ERR(tmp_page)) { | 733 | if (IS_ERR(tmp_page)) { |
@@ -715,28 +736,27 @@ int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros) | |||
715 | rc = PTR_ERR(tmp_page); | 736 | rc = PTR_ERR(tmp_page); |
716 | goto out; | 737 | goto out; |
717 | } | 738 | } |
718 | kmap(tmp_page); | ||
719 | rc = ecryptfs_prepare_write(file, tmp_page, start, start + num_zeros); | 739 | rc = ecryptfs_prepare_write(file, tmp_page, start, start + num_zeros); |
720 | if (rc) { | 740 | if (rc) { |
721 | ecryptfs_printk(KERN_ERR, "Error preparing to write zero's " | 741 | ecryptfs_printk(KERN_ERR, "Error preparing to write zero's " |
722 | "to remainder of page at index [0x%.16x]\n", | 742 | "to remainder of page at index [0x%.16x]\n", |
723 | index); | 743 | index); |
724 | kunmap(tmp_page); | ||
725 | page_cache_release(tmp_page); | 744 | page_cache_release(tmp_page); |
726 | goto out; | 745 | goto out; |
727 | } | 746 | } |
728 | memset(((char *)page_address(tmp_page) + start), 0, num_zeros); | 747 | tmp_page_virt = kmap_atomic(tmp_page, KM_USER0); |
748 | memset(((char *)tmp_page_virt + start), 0, num_zeros); | ||
749 | kunmap_atomic(tmp_page_virt, KM_USER0); | ||
750 | flush_dcache_page(tmp_page); | ||
729 | rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros); | 751 | rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros); |
730 | if (rc < 0) { | 752 | if (rc < 0) { |
731 | ecryptfs_printk(KERN_ERR, "Error attempting to write zero's " | 753 | ecryptfs_printk(KERN_ERR, "Error attempting to write zero's " |
732 | "to remainder of page at index [0x%.16x]\n", | 754 | "to remainder of page at index [0x%.16x]\n", |
733 | index); | 755 | index); |
734 | kunmap(tmp_page); | ||
735 | page_cache_release(tmp_page); | 756 | page_cache_release(tmp_page); |
736 | goto out; | 757 | goto out; |
737 | } | 758 | } |
738 | rc = 0; | 759 | rc = 0; |
739 | kunmap(tmp_page); | ||
740 | page_cache_release(tmp_page); | 760 | page_cache_release(tmp_page); |
741 | out: | 761 | out: |
742 | return rc; | 762 | return rc; |