delalloc-space.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "messages.h"
  3. #include "ctree.h"
  4. #include "delalloc-space.h"
  5. #include "block-rsv.h"
  6. #include "btrfs_inode.h"
  7. #include "space-info.h"
  8. #include "qgroup.h"
  9. #include "fs.h"
  10. /*
  11. * HOW DOES THIS WORK
  12. *
  13. * There are two stages to data reservations, one for data and one for metadata
  14. * to handle the new extents and checksums generated by writing data.
  15. *
  16. *
  17. * DATA RESERVATION
  18. * The general flow of the data reservation is as follows
  19. *
  20. * -> Reserve
  21. * We call into btrfs_reserve_data_bytes() for the user request bytes that
  22. * they wish to write. We make this reservation and add it to
  23. * space_info->bytes_may_use. We set EXTENT_DELALLOC on the inode io_tree
  24. * for the range and carry on if this is buffered, or follow up trying to
  25. * make a real allocation if we are pre-allocating or doing O_DIRECT.
  26. *
  27. * -> Use
  28. * At writepages()/prealloc/O_DIRECT time we will call into
  29. * btrfs_reserve_extent() for some part or all of this range of bytes. We
  30. * will make the allocation and subtract space_info->bytes_may_use by the
  31. * original requested length and increase the space_info->bytes_reserved by
  32. * the allocated length. This distinction is important because compression
  33. * may allocate a smaller on disk extent than we previously reserved.
  34. *
  35. * -> Allocation
  36. * finish_ordered_io() will insert the new file extent item for this range,
  37. * and then add a delayed ref update for the extent tree. Once that delayed
  38. * ref is written the extent size is subtracted from
  39. * space_info->bytes_reserved and added to space_info->bytes_used.
  40. *
  41. * Error handling
  42. *
  43. * -> By the reservation maker
  44. * This is the simplest case, we haven't completed our operation and we know
  45. * how much we reserved, we can simply call
  46. * btrfs_free_reserved_data_space*() and it will be removed from
  47. * space_info->bytes_may_use.
  48. *
  49. * -> After the reservation has been made, but before cow_file_range()
  50. * This is specifically for the delalloc case. You must clear
  51. * EXTENT_DELALLOC with the EXTENT_CLEAR_DATA_RESV bit, and the range will
  52. * be subtracted from space_info->bytes_may_use.
  53. *
  54. * METADATA RESERVATION
  55. * The general metadata reservation lifetimes are discussed elsewhere, this
  56. * will just focus on how it is used for delalloc space.
  57. *
  58. * We keep track of two things on a per inode bases
  59. *
  60. * ->outstanding_extents
  61. * This is the number of file extent items we'll need to handle all of the
  62. * outstanding DELALLOC space we have in this inode. We limit the maximum
  63. * size of an extent, so a large contiguous dirty area may require more than
  64. * one outstanding_extent, which is why count_max_extents() is used to
  65. * determine how many outstanding_extents get added.
  66. *
  67. * ->csum_bytes
  68. * This is essentially how many dirty bytes we have for this inode, so we
  69. * can calculate the number of checksum items we would have to add in order
  70. * to checksum our outstanding data.
  71. *
  72. * We keep a per-inode block_rsv in order to make it easier to keep track of
  73. * our reservation. We use btrfs_calculate_inode_block_rsv_size() to
  74. * calculate the current theoretical maximum reservation we would need for the
  75. * metadata for this inode. We call this and then adjust our reservation as
  76. * necessary, either by attempting to reserve more space, or freeing up excess
  77. * space.
  78. *
  79. * OUTSTANDING_EXTENTS HANDLING
  80. *
  81. * ->outstanding_extents is used for keeping track of how many extents we will
  82. * need to use for this inode, and it will fluctuate depending on where you are
  83. * in the life cycle of the dirty data. Consider the following normal case for
  84. * a completely clean inode, with a num_bytes < our maximum allowed extent size
  85. *
  86. * -> reserve
  87. * ->outstanding_extents += 1 (current value is 1)
  88. *
  89. * -> set_delalloc
  90. * ->outstanding_extents += 1 (current value is 2)
  91. *
  92. * -> btrfs_delalloc_release_extents()
  93. * ->outstanding_extents -= 1 (current value is 1)
  94. *
  95. * We must call this once we are done, as we hold our reservation for the
  96. * duration of our operation, and then assume set_delalloc will update the
  97. * counter appropriately.
  98. *
  99. * -> add ordered extent
  100. * ->outstanding_extents += 1 (current value is 2)
  101. *
  102. * -> btrfs_clear_delalloc_extent
  103. * ->outstanding_extents -= 1 (current value is 1)
  104. *
  105. * -> finish_ordered_io/btrfs_remove_ordered_extent
  106. * ->outstanding_extents -= 1 (current value is 0)
  107. *
  108. * Each stage is responsible for their own accounting of the extent, thus
  109. * making error handling and cleanup easier.
  110. */
  111. static inline struct btrfs_space_info *data_sinfo_for_inode(const struct btrfs_inode *inode)
  112. {
  113. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  114. if (btrfs_is_zoned(fs_info) && btrfs_is_data_reloc_root(inode->root)) {
  115. ASSERT(fs_info->data_sinfo->sub_group[0]->subgroup_id ==
  116. BTRFS_SUB_GROUP_DATA_RELOC);
  117. return fs_info->data_sinfo->sub_group[0];
  118. }
  119. return fs_info->data_sinfo;
  120. }
  121. int btrfs_alloc_data_chunk_ondemand(const struct btrfs_inode *inode, u64 bytes)
  122. {
  123. struct btrfs_root *root = inode->root;
  124. struct btrfs_fs_info *fs_info = root->fs_info;
  125. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
  126. /* Make sure bytes are sectorsize aligned */
  127. bytes = ALIGN(bytes, fs_info->sectorsize);
  128. if (btrfs_is_free_space_inode(inode))
  129. flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
  130. return btrfs_reserve_data_bytes(data_sinfo_for_inode(inode), bytes, flush);
  131. }
  132. int btrfs_check_data_free_space(struct btrfs_inode *inode,
  133. struct extent_changeset **reserved, u64 start,
  134. u64 len, bool noflush)
  135. {
  136. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  137. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
  138. int ret;
  139. /* align the range */
  140. len = round_up(start + len, fs_info->sectorsize) -
  141. round_down(start, fs_info->sectorsize);
  142. start = round_down(start, fs_info->sectorsize);
  143. if (noflush)
  144. flush = BTRFS_RESERVE_NO_FLUSH;
  145. else if (btrfs_is_free_space_inode(inode))
  146. flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
  147. ret = btrfs_reserve_data_bytes(data_sinfo_for_inode(inode), len, flush);
  148. if (ret < 0)
  149. return ret;
  150. /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
  151. ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
  152. if (ret < 0) {
  153. btrfs_free_reserved_data_space_noquota(inode, len);
  154. extent_changeset_free(*reserved);
  155. *reserved = NULL;
  156. } else {
  157. ret = 0;
  158. }
  159. return ret;
  160. }
  161. /*
  162. * Called if we need to clear a data reservation for this inode
  163. * Normally in a error case.
  164. *
  165. * This one will *NOT* use accurate qgroup reserved space API, just for case
  166. * which we can't sleep and is sure it won't affect qgroup reserved space.
  167. * Like clear_bit_hook().
  168. */
  169. void btrfs_free_reserved_data_space_noquota(struct btrfs_inode *inode, u64 len)
  170. {
  171. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  172. ASSERT(IS_ALIGNED(len, fs_info->sectorsize));
  173. btrfs_space_info_free_bytes_may_use(data_sinfo_for_inode(inode), len);
  174. }
  175. /*
  176. * Called if we need to clear a data reservation for this inode
  177. * Normally in a error case.
  178. *
  179. * This one will handle the per-inode data rsv map for accurate reserved
  180. * space framework.
  181. */
  182. void btrfs_free_reserved_data_space(struct btrfs_inode *inode,
  183. struct extent_changeset *reserved, u64 start, u64 len)
  184. {
  185. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  186. /* Make sure the range is aligned to sectorsize */
  187. len = round_up(start + len, fs_info->sectorsize) -
  188. round_down(start, fs_info->sectorsize);
  189. start = round_down(start, fs_info->sectorsize);
  190. btrfs_free_reserved_data_space_noquota(inode, len);
  191. btrfs_qgroup_free_data(inode, reserved, start, len, NULL);
  192. }
  193. /*
  194. * Release any excessive reservations for an inode.
  195. *
  196. * @inode: the inode we need to release from
  197. * @qgroup_free: free or convert qgroup meta. Unlike normal operation, qgroup
  198. * meta reservation needs to know if we are freeing qgroup
  199. * reservation or just converting it into per-trans. Normally
  200. * @qgroup_free is true for error handling, and false for normal
  201. * release.
  202. *
  203. * This is the same as btrfs_block_rsv_release, except that it handles the
  204. * tracepoint for the reservation.
  205. */
  206. static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
  207. {
  208. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  209. struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
  210. u64 released = 0;
  211. u64 qgroup_to_release = 0;
  212. /*
  213. * Since we statically set the block_rsv->size we just want to say we
  214. * are releasing 0 bytes, and then we'll just get the reservation over
  215. * the size free'd.
  216. */
  217. released = btrfs_block_rsv_release(fs_info, block_rsv, 0,
  218. &qgroup_to_release);
  219. if (released > 0)
  220. trace_btrfs_space_reservation(fs_info, "delalloc",
  221. btrfs_ino(inode), released, 0);
  222. if (qgroup_free)
  223. btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
  224. else
  225. btrfs_qgroup_convert_reserved_meta(inode->root,
  226. qgroup_to_release);
  227. }
  228. static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
  229. struct btrfs_inode *inode)
  230. {
  231. struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
  232. u64 reserve_size = 0;
  233. u64 qgroup_rsv_size = 0;
  234. unsigned outstanding_extents;
  235. lockdep_assert_held(&inode->lock);
  236. outstanding_extents = inode->outstanding_extents;
  237. /*
  238. * Insert size for the number of outstanding extents, 1 normal size for
  239. * updating the inode.
  240. */
  241. if (outstanding_extents) {
  242. reserve_size = btrfs_calc_insert_metadata_size(fs_info,
  243. outstanding_extents);
  244. reserve_size += btrfs_calc_metadata_size(fs_info, 1);
  245. }
  246. if (!(inode->flags & BTRFS_INODE_NODATASUM)) {
  247. u64 csum_leaves;
  248. csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes);
  249. reserve_size += btrfs_calc_insert_metadata_size(fs_info, csum_leaves);
  250. }
  251. /*
  252. * For qgroup rsv, the calculation is very simple:
  253. * account one nodesize for each outstanding extent
  254. *
  255. * This is overestimating in most cases.
  256. */
  257. qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
  258. spin_lock(&block_rsv->lock);
  259. block_rsv->size = reserve_size;
  260. block_rsv->qgroup_rsv_size = qgroup_rsv_size;
  261. spin_unlock(&block_rsv->lock);
  262. }
  263. static void calc_inode_reservations(struct btrfs_inode *inode,
  264. u64 num_bytes, u64 disk_num_bytes,
  265. u64 *meta_reserve, u64 *qgroup_reserve)
  266. {
  267. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  268. u64 nr_extents = count_max_extents(fs_info, num_bytes);
  269. u64 csum_leaves;
  270. u64 inode_update = btrfs_calc_metadata_size(fs_info, 1);
  271. if (inode->flags & BTRFS_INODE_NODATASUM)
  272. csum_leaves = 0;
  273. else
  274. csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, disk_num_bytes);
  275. *meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
  276. nr_extents + csum_leaves);
  277. /*
  278. * finish_ordered_io has to update the inode, so add the space required
  279. * for an inode update.
  280. */
  281. *meta_reserve += inode_update;
  282. *qgroup_reserve = nr_extents * fs_info->nodesize;
  283. }
  284. int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
  285. u64 disk_num_bytes, bool noflush)
  286. {
  287. struct btrfs_root *root = inode->root;
  288. struct btrfs_fs_info *fs_info = root->fs_info;
  289. struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
  290. u64 meta_reserve, qgroup_reserve;
  291. unsigned nr_extents;
  292. enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
  293. int ret = 0;
  294. /*
  295. * If we are a free space inode we need to not flush since we will be in
  296. * the middle of a transaction commit. We also don't need the delalloc
  297. * mutex since we won't race with anybody. We need this mostly to make
  298. * lockdep shut its filthy mouth.
  299. *
  300. * If we have a transaction open (can happen if we call truncate_block
  301. * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
  302. */
  303. if (noflush || btrfs_is_free_space_inode(inode)) {
  304. flush = BTRFS_RESERVE_NO_FLUSH;
  305. } else {
  306. if (current->journal_info)
  307. flush = BTRFS_RESERVE_FLUSH_LIMIT;
  308. }
  309. num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
  310. disk_num_bytes = ALIGN(disk_num_bytes, fs_info->sectorsize);
  311. /*
  312. * We always want to do it this way, every other way is wrong and ends
  313. * in tears. Pre-reserving the amount we are going to add will always
  314. * be the right way, because otherwise if we have enough parallelism we
  315. * could end up with thousands of inodes all holding little bits of
  316. * reservations they were able to make previously and the only way to
  317. * reclaim that space is to ENOSPC out the operations and clear
  318. * everything out and try again, which is bad. This way we just
  319. * over-reserve slightly, and clean up the mess when we are done.
  320. */
  321. calc_inode_reservations(inode, num_bytes, disk_num_bytes,
  322. &meta_reserve, &qgroup_reserve);
  323. ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserve, true,
  324. noflush);
  325. if (ret)
  326. return ret;
  327. ret = btrfs_reserve_metadata_bytes(block_rsv->space_info, meta_reserve,
  328. flush);
  329. if (ret) {
  330. btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
  331. return ret;
  332. }
  333. /*
  334. * Now we need to update our outstanding extents and csum bytes _first_
  335. * and then add the reservation to the block_rsv. This keeps us from
  336. * racing with an ordered completion or some such that would think it
  337. * needs to free the reservation we just made.
  338. */
  339. nr_extents = count_max_extents(fs_info, num_bytes);
  340. spin_lock(&inode->lock);
  341. btrfs_mod_outstanding_extents(inode, nr_extents);
  342. if (!(inode->flags & BTRFS_INODE_NODATASUM))
  343. inode->csum_bytes += disk_num_bytes;
  344. btrfs_calculate_inode_block_rsv_size(fs_info, inode);
  345. spin_unlock(&inode->lock);
  346. /* Now we can safely add our space to our block rsv */
  347. btrfs_block_rsv_add_bytes(block_rsv, meta_reserve, false);
  348. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  349. btrfs_ino(inode), meta_reserve, 1);
  350. spin_lock(&block_rsv->lock);
  351. block_rsv->qgroup_rsv_reserved += qgroup_reserve;
  352. spin_unlock(&block_rsv->lock);
  353. return 0;
  354. }
  355. /*
  356. * Release a metadata reservation for an inode.
  357. *
  358. * @inode: the inode to release the reservation for.
  359. * @num_bytes: the number of bytes we are releasing.
  360. * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
  361. *
  362. * This will release the metadata reservation for an inode. This can be called
  363. * once we complete IO for a given set of bytes to release their metadata
  364. * reservations, or on error for the same reason.
  365. */
  366. void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
  367. bool qgroup_free)
  368. {
  369. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  370. num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
  371. spin_lock(&inode->lock);
  372. if (!(inode->flags & BTRFS_INODE_NODATASUM))
  373. inode->csum_bytes -= num_bytes;
  374. btrfs_calculate_inode_block_rsv_size(fs_info, inode);
  375. spin_unlock(&inode->lock);
  376. if (btrfs_is_testing(fs_info))
  377. return;
  378. btrfs_inode_rsv_release(inode, qgroup_free);
  379. }
  380. /*
  381. * Release our outstanding_extents for an inode.
  382. *
  383. * @inode: the inode to balance the reservation for.
  384. * @num_bytes: the number of bytes we originally reserved with
  385. *
  386. * When we reserve space we increase outstanding_extents for the extents we may
  387. * add. Once we've set the range as delalloc or created our ordered extents we
  388. * have outstanding_extents to track the real usage, so we use this to free our
  389. * temporarily tracked outstanding_extents. This _must_ be used in conjunction
  390. * with btrfs_delalloc_reserve_metadata.
  391. */
  392. void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes)
  393. {
  394. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  395. unsigned num_extents;
  396. spin_lock(&inode->lock);
  397. num_extents = count_max_extents(fs_info, num_bytes);
  398. btrfs_mod_outstanding_extents(inode, -num_extents);
  399. btrfs_calculate_inode_block_rsv_size(fs_info, inode);
  400. spin_unlock(&inode->lock);
  401. if (btrfs_is_testing(fs_info))
  402. return;
  403. btrfs_inode_rsv_release(inode, true);
  404. }
  405. /* Shrink a previously reserved extent to a new length. */
  406. void btrfs_delalloc_shrink_extents(struct btrfs_inode *inode, u64 reserved_len, u64 new_len)
  407. {
  408. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  409. const u32 reserved_num_extents = count_max_extents(fs_info, reserved_len);
  410. const u32 new_num_extents = count_max_extents(fs_info, new_len);
  411. const int diff_num_extents = new_num_extents - reserved_num_extents;
  412. ASSERT(new_len <= reserved_len);
  413. if (new_num_extents == reserved_num_extents)
  414. return;
  415. spin_lock(&inode->lock);
  416. btrfs_mod_outstanding_extents(inode, diff_num_extents);
  417. btrfs_calculate_inode_block_rsv_size(fs_info, inode);
  418. spin_unlock(&inode->lock);
  419. if (btrfs_is_testing(fs_info))
  420. return;
  421. btrfs_inode_rsv_release(inode, true);
  422. }
  423. /*
  424. * Reserve data and metadata space for delalloc
  425. *
  426. * @inode: inode we're writing to
  427. * @start: start range we are writing to
  428. * @len: how long the range we are writing to
  429. * @reserved: mandatory parameter, record actually reserved qgroup ranges of
  430. * current reservation.
  431. *
  432. * This will do the following things
  433. *
  434. * - reserve space in data space info for num bytes and reserve precious
  435. * corresponding qgroup space
  436. * (Done in check_data_free_space)
  437. *
  438. * - reserve space for metadata space, based on the number of outstanding
  439. * extents and how much csums will be needed also reserve metadata space in a
  440. * per root over-reserve method.
  441. * - add to the inodes->delalloc_bytes
  442. * - add it to the fs_info's delalloc inodes list.
  443. * (Above 3 all done in delalloc_reserve_metadata)
  444. *
  445. * Return 0 for success
  446. * Return <0 for error(-ENOSPC or -EDQUOT)
  447. */
  448. int btrfs_delalloc_reserve_space(struct btrfs_inode *inode,
  449. struct extent_changeset **reserved, u64 start, u64 len)
  450. {
  451. int ret;
  452. ret = btrfs_check_data_free_space(inode, reserved, start, len, false);
  453. if (ret < 0)
  454. return ret;
  455. ret = btrfs_delalloc_reserve_metadata(inode, len, len, false);
  456. if (ret < 0) {
  457. btrfs_free_reserved_data_space(inode, *reserved, start, len);
  458. extent_changeset_free(*reserved);
  459. *reserved = NULL;
  460. }
  461. return ret;
  462. }
  463. /*
  464. * Release data and metadata space for delalloc
  465. *
  466. * @inode: inode we're releasing space for
  467. * @reserved: list of changed/reserved ranges
  468. * @start: start position of the space already reserved
  469. * @len: length of the space already reserved
  470. * @qgroup_free: should qgroup reserved-space also be freed
  471. *
  472. * Release the metadata space that was not used and will decrement
  473. * ->delalloc_bytes and remove it from the fs_info->delalloc_inodes list if
  474. * there are no delalloc bytes left. Also it will handle the qgroup reserved
  475. * space.
  476. */
  477. void btrfs_delalloc_release_space(struct btrfs_inode *inode,
  478. struct extent_changeset *reserved,
  479. u64 start, u64 len, bool qgroup_free)
  480. {
  481. btrfs_delalloc_release_metadata(inode, len, qgroup_free);
  482. btrfs_free_reserved_data_space(inode, reserved, start, len);
  483. }