bio.c 3.5 KB

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