bio.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Utility functions for file contents encryption/decryption on
  4. * block device-based filesystems.
  5. *
  6. * Copyright (C) 2015, Google, Inc.
  7. * Copyright (C) 2015, Motorola Mobility
  8. */
  9. #include <linux/bio.h>
  10. #include <linux/export.h>
  11. #include <linux/module.h>
  12. #include <linux/namei.h>
  13. #include <linux/pagemap.h>
  14. #include "fscrypt_private.h"
  15. /**
  16. * fscrypt_decrypt_bio() - decrypt the contents of a bio
  17. * @bio: the bio to decrypt
  18. *
  19. * Decrypt the contents of a "read" bio following successful completion of the
  20. * underlying disk read. The bio must be reading a whole number of blocks of an
  21. * encrypted file directly into the page cache. If the bio is reading the
  22. * ciphertext into bounce pages instead of the page cache (for example, because
  23. * the file is also compressed, so decompression is required after decryption),
  24. * then this function isn't applicable. This function may sleep, so it must be
  25. * called from a workqueue rather than from the bio's bi_end_io callback.
  26. *
  27. * Return: %true on success; %false on failure. On failure, bio->bi_status is
  28. * also set to an error status.
  29. */
  30. bool fscrypt_decrypt_bio(struct bio *bio)
  31. {
  32. struct folio_iter fi;
  33. bio_for_each_folio_all(fi, bio) {
  34. int err = fscrypt_decrypt_pagecache_blocks(fi.folio, fi.length,
  35. fi.offset);
  36. if (err) {
  37. bio->bi_status = errno_to_blk_status(err);
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. EXPORT_SYMBOL(fscrypt_decrypt_bio);
  44. struct fscrypt_zero_done {
  45. atomic_t pending;
  46. blk_status_t status;
  47. struct completion done;
  48. };
  49. static void fscrypt_zeroout_range_done(struct fscrypt_zero_done *done)
  50. {
  51. if (atomic_dec_and_test(&done->pending))
  52. complete(&done->done);
  53. }
  54. static void fscrypt_zeroout_range_end_io(struct bio *bio)
  55. {
  56. struct fscrypt_zero_done *done = bio->bi_private;
  57. if (bio->bi_status)
  58. cmpxchg(&done->status, 0, bio->bi_status);
  59. fscrypt_zeroout_range_done(done);
  60. bio_put(bio);
  61. }
  62. static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
  63. pgoff_t lblk, sector_t sector,
  64. unsigned int len)
  65. {
  66. const unsigned int blockbits = inode->i_blkbits;
  67. const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits);
  68. struct fscrypt_zero_done done = {
  69. .pending = ATOMIC_INIT(1),
  70. .done = COMPLETION_INITIALIZER_ONSTACK(done.done),
  71. };
  72. while (len) {
  73. struct bio *bio;
  74. unsigned int n;
  75. bio = bio_alloc(inode->i_sb->s_bdev, BIO_MAX_VECS, REQ_OP_WRITE,
  76. GFP_NOFS);
  77. bio->bi_iter.bi_sector = sector;
  78. bio->bi_private = &done;
  79. bio->bi_end_io = fscrypt_zeroout_range_end_io;
  80. fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS);
  81. for (n = 0; n < BIO_MAX_VECS; n++) {
  82. unsigned int blocks_this_page =
  83. min(len, blocks_per_page);
  84. unsigned int bytes_this_page = blocks_this_page << blockbits;
  85. __bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0);
  86. len -= blocks_this_page;
  87. lblk += blocks_this_page;
  88. sector += (bytes_this_page >> SECTOR_SHIFT);
  89. if (!len || !fscrypt_mergeable_bio(bio, inode, lblk))
  90. break;
  91. }
  92. atomic_inc(&done.pending);
  93. blk_crypto_submit_bio(bio);
  94. }
  95. fscrypt_zeroout_range_done(&done);
  96. wait_for_completion(&done.done);
  97. return blk_status_to_errno(done.status);
  98. }
  99. /**
  100. * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file
  101. * @inode: the file's inode
  102. * @lblk: the first file logical block to zero out
  103. * @pblk: the first filesystem physical block to zero out
  104. * @len: number of blocks to zero out
  105. *
  106. * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write
  107. * ciphertext blocks which decrypt to the all-zeroes block. The blocks must be
  108. * both logically and physically contiguous. It's also assumed that the
  109. * filesystem only uses a single block device, ->s_bdev.
  110. *
  111. * Note that since each block uses a different IV, this involves writing a
  112. * different ciphertext to each block; we can't simply reuse the same one.
  113. *
  114. * Return: 0 on success; -errno on failure.
  115. */
  116. int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
  117. sector_t pblk, unsigned int len)
  118. {
  119. const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode);
  120. const unsigned int du_bits = ci->ci_data_unit_bits;
  121. const unsigned int du_size = 1U << du_bits;
  122. const unsigned int du_per_page_bits = PAGE_SHIFT - du_bits;
  123. const unsigned int du_per_page = 1U << du_per_page_bits;
  124. u64 du_index = (u64)lblk << (inode->i_blkbits - du_bits);
  125. u64 du_remaining = (u64)len << (inode->i_blkbits - du_bits);
  126. sector_t sector = pblk << (inode->i_blkbits - SECTOR_SHIFT);
  127. struct page *pages[16]; /* write up to 16 pages at a time */
  128. unsigned int nr_pages;
  129. unsigned int i;
  130. unsigned int offset;
  131. struct bio *bio;
  132. int ret, err;
  133. if (len == 0)
  134. return 0;
  135. if (fscrypt_inode_uses_inline_crypto(inode))
  136. return fscrypt_zeroout_range_inline_crypt(inode, lblk, sector,
  137. len);
  138. BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_VECS);
  139. nr_pages = min_t(u64, ARRAY_SIZE(pages),
  140. (du_remaining + du_per_page - 1) >> du_per_page_bits);
  141. /*
  142. * We need at least one page for ciphertext. Allocate the first one
  143. * from a mempool, with __GFP_DIRECT_RECLAIM set so that it can't fail.
  144. *
  145. * Any additional page allocations are allowed to fail, as they only
  146. * help performance, and waiting on the mempool for them could deadlock.
  147. */
  148. for (i = 0; i < nr_pages; i++) {
  149. pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS :
  150. GFP_NOWAIT);
  151. if (!pages[i])
  152. break;
  153. }
  154. nr_pages = i;
  155. if (WARN_ON_ONCE(nr_pages <= 0))
  156. return -EINVAL;
  157. /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
  158. bio = bio_alloc(inode->i_sb->s_bdev, nr_pages, REQ_OP_WRITE, GFP_NOFS);
  159. do {
  160. bio->bi_iter.bi_sector = sector;
  161. i = 0;
  162. offset = 0;
  163. do {
  164. err = fscrypt_crypt_data_unit(ci, FS_ENCRYPT, du_index,
  165. ZERO_PAGE(0), pages[i],
  166. du_size, offset);
  167. if (err)
  168. goto out;
  169. du_index++;
  170. sector += 1U << (du_bits - SECTOR_SHIFT);
  171. du_remaining--;
  172. offset += du_size;
  173. if (offset == PAGE_SIZE || du_remaining == 0) {
  174. ret = bio_add_page(bio, pages[i++], offset, 0);
  175. if (WARN_ON_ONCE(ret != offset)) {
  176. err = -EIO;
  177. goto out;
  178. }
  179. offset = 0;
  180. }
  181. } while (i != nr_pages && du_remaining != 0);
  182. err = submit_bio_wait(bio);
  183. if (err)
  184. goto out;
  185. bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
  186. } while (du_remaining != 0);
  187. err = 0;
  188. out:
  189. bio_put(bio);
  190. for (i = 0; i < nr_pages; i++)
  191. fscrypt_free_bounce_page(pages[i]);
  192. return err;
  193. }
  194. EXPORT_SYMBOL(fscrypt_zeroout_range);