bio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. static void __fscrypt_decrypt_bio(struct bio *bio, bool done)
  27. {
  28. struct bio_vec *bv;
  29. int i;
  30. bio_for_each_segment_all(bv, bio, i) {
  31. struct page *page = bv->bv_page;
  32. int ret = fscrypt_decrypt_page(page->mapping->host, page,
  33. PAGE_SIZE, 0, page->index);
  34. if (ret) {
  35. WARN_ON_ONCE(1);
  36. SetPageError(page);
  37. } else if (done) {
  38. SetPageUptodate(page);
  39. }
  40. if (done)
  41. unlock_page(page);
  42. }
  43. }
  44. void fscrypt_decrypt_bio(struct bio *bio)
  45. {
  46. __fscrypt_decrypt_bio(bio, false);
  47. }
  48. EXPORT_SYMBOL(fscrypt_decrypt_bio);
  49. static void completion_pages(struct work_struct *work)
  50. {
  51. struct fscrypt_ctx *ctx =
  52. container_of(work, struct fscrypt_ctx, r.work);
  53. struct bio *bio = ctx->r.bio;
  54. __fscrypt_decrypt_bio(bio, true);
  55. fscrypt_release_ctx(ctx);
  56. bio_put(bio);
  57. }
  58. void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx, struct bio *bio)
  59. {
  60. INIT_WORK(&ctx->r.work, completion_pages);
  61. ctx->r.bio = bio;
  62. fscrypt_enqueue_decrypt_work(&ctx->r.work);
  63. }
  64. EXPORT_SYMBOL(fscrypt_enqueue_decrypt_bio);
  65. void fscrypt_pullback_bio_page(struct page **page, bool restore)
  66. {
  67. struct fscrypt_ctx *ctx;
  68. struct page *bounce_page;
  69. /* The bounce data pages are unmapped. */
  70. if ((*page)->mapping)
  71. return;
  72. /* The bounce data page is unmapped. */
  73. bounce_page = *page;
  74. ctx = (struct fscrypt_ctx *)page_private(bounce_page);
  75. /* restore control page */
  76. *page = ctx->w.control_page;
  77. if (restore)
  78. fscrypt_restore_control_page(bounce_page);
  79. }
  80. EXPORT_SYMBOL(fscrypt_pullback_bio_page);
  81. int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
  82. sector_t pblk, unsigned int len)
  83. {
  84. struct fscrypt_ctx *ctx;
  85. struct page *ciphertext_page = NULL;
  86. struct bio *bio;
  87. int ret, err = 0;
  88. BUG_ON(inode->i_sb->s_blocksize != PAGE_SIZE);
  89. ctx = fscrypt_get_ctx(inode, GFP_NOFS);
  90. if (IS_ERR(ctx))
  91. return PTR_ERR(ctx);
  92. ciphertext_page = fscrypt_alloc_bounce_page(ctx, GFP_NOWAIT);
  93. if (IS_ERR(ciphertext_page)) {
  94. err = PTR_ERR(ciphertext_page);
  95. goto errout;
  96. }
  97. while (len--) {
  98. err = fscrypt_do_page_crypto(inode, FS_ENCRYPT, lblk,
  99. ZERO_PAGE(0), ciphertext_page,
  100. PAGE_SIZE, 0, GFP_NOFS);
  101. if (err)
  102. goto errout;
  103. bio = bio_alloc(GFP_NOWAIT, 1);
  104. if (!bio) {
  105. err = -ENOMEM;
  106. goto errout;
  107. }
  108. bio->bi_bdev = inode->i_sb->s_bdev;
  109. bio->bi_iter.bi_sector =
  110. pblk << (inode->i_sb->s_blocksize_bits - 9);
  111. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  112. ret = bio_add_page(bio, ciphertext_page,
  113. inode->i_sb->s_blocksize, 0);
  114. if (ret != inode->i_sb->s_blocksize) {
  115. /* should never happen! */
  116. WARN_ON(1);
  117. bio_put(bio);
  118. err = -EIO;
  119. goto errout;
  120. }
  121. err = submit_bio_wait(bio);
  122. if (err == 0 && bio->bi_error)
  123. err = -EIO;
  124. bio_put(bio);
  125. if (err)
  126. goto errout;
  127. lblk++;
  128. pblk++;
  129. }
  130. err = 0;
  131. errout:
  132. fscrypt_release_ctx(ctx);
  133. return err;
  134. }
  135. EXPORT_SYMBOL(fscrypt_zeroout_range);