bio.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * This contains encryption functions for per-file encryption.
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. * Copyright (C) 2015, Motorola Mobility
  6. *
  7. * Written by Michael Halcrow, 2014.
  8. *
  9. * Filename encryption additions
  10. * Uday Savagaonkar, 2014
  11. * Encryption policy handling additions
  12. * Ildar Muslukhov, 2014
  13. * Add fscrypt_pullback_bio_page()
  14. * Jaegeuk Kim, 2015.
  15. *
  16. * This has not yet undergone a rigorous security audit.
  17. *
  18. * The usage of AES-XTS should conform to recommendations in NIST
  19. * Special Publication 800-38E and IEEE P1619/D16.
  20. */
  21. #include <linux/pagemap.h>
  22. #include <linux/module.h>
  23. #include <linux/bio.h>
  24. #include <linux/namei.h>
  25. #include "fscrypt_private.h"
  26. /*
  27. * Call fscrypt_decrypt_page on every single page, reusing the encryption
  28. * context.
  29. */
  30. static void completion_pages(struct work_struct *work)
  31. {
  32. struct fscrypt_ctx *ctx =
  33. container_of(work, struct fscrypt_ctx, r.work);
  34. struct bio *bio = ctx->r.bio;
  35. struct bio_vec *bv;
  36. int i;
  37. bio_for_each_segment_all(bv, bio, i) {
  38. struct page *page = bv->bv_page;
  39. int ret = fscrypt_decrypt_page(page->mapping->host, page,
  40. PAGE_SIZE, 0, page->index);
  41. if (ret) {
  42. WARN_ON_ONCE(1);
  43. SetPageError(page);
  44. } else {
  45. SetPageUptodate(page);
  46. }
  47. unlock_page(page);
  48. }
  49. fscrypt_release_ctx(ctx);
  50. bio_put(bio);
  51. }
  52. void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *ctx, struct bio *bio)
  53. {
  54. INIT_WORK(&ctx->r.work, completion_pages);
  55. ctx->r.bio = bio;
  56. queue_work(fscrypt_read_workqueue, &ctx->r.work);
  57. }
  58. EXPORT_SYMBOL(fscrypt_decrypt_bio_pages);
  59. void fscrypt_pullback_bio_page(struct page **page, bool restore)
  60. {
  61. struct fscrypt_ctx *ctx;
  62. struct page *bounce_page;
  63. /* The bounce data pages are unmapped. */
  64. if ((*page)->mapping)
  65. return;
  66. /* The bounce data page is unmapped. */
  67. bounce_page = *page;
  68. ctx = (struct fscrypt_ctx *)page_private(bounce_page);
  69. /* restore control page */
  70. *page = ctx->w.control_page;
  71. if (restore)
  72. fscrypt_restore_control_page(bounce_page);
  73. }
  74. EXPORT_SYMBOL(fscrypt_pullback_bio_page);
  75. int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
  76. sector_t pblk, unsigned int len)
  77. {
  78. struct fscrypt_ctx *ctx;
  79. struct page *ciphertext_page = NULL;
  80. struct bio *bio;
  81. int ret, err = 0;
  82. BUG_ON(inode->i_sb->s_blocksize != PAGE_SIZE);
  83. ctx = fscrypt_get_ctx(inode, GFP_NOFS);
  84. if (IS_ERR(ctx))
  85. return PTR_ERR(ctx);
  86. ciphertext_page = fscrypt_alloc_bounce_page(ctx, GFP_NOWAIT);
  87. if (IS_ERR(ciphertext_page)) {
  88. err = PTR_ERR(ciphertext_page);
  89. goto errout;
  90. }
  91. while (len--) {
  92. err = fscrypt_do_page_crypto(inode, FS_ENCRYPT, lblk,
  93. ZERO_PAGE(0), ciphertext_page,
  94. PAGE_SIZE, 0, GFP_NOFS);
  95. if (err)
  96. goto errout;
  97. bio = bio_alloc(GFP_NOWAIT, 1);
  98. if (!bio) {
  99. err = -ENOMEM;
  100. goto errout;
  101. }
  102. bio_set_dev(bio, inode->i_sb->s_bdev);
  103. bio->bi_iter.bi_sector =
  104. pblk << (inode->i_sb->s_blocksize_bits - 9);
  105. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  106. ret = bio_add_page(bio, ciphertext_page,
  107. inode->i_sb->s_blocksize, 0);
  108. if (ret != inode->i_sb->s_blocksize) {
  109. /* should never happen! */
  110. WARN_ON(1);
  111. bio_put(bio);
  112. err = -EIO;
  113. goto errout;
  114. }
  115. err = submit_bio_wait(bio);
  116. if (err == 0 && bio->bi_status)
  117. err = -EIO;
  118. bio_put(bio);
  119. if (err)
  120. goto errout;
  121. lblk++;
  122. pblk++;
  123. }
  124. err = 0;
  125. errout:
  126. fscrypt_release_ctx(ctx);
  127. return err;
  128. }
  129. EXPORT_SYMBOL(fscrypt_zeroout_range);