1 // SPDX-License-Identifier: GPL-2.0
5 #include "delalloc-space.h"
7 #include "btrfs_inode.h"
8 #include "space-info.h"
9 #include "transaction.h"
11 #include "block-group.h"
17 * There are two stages to data reservations, one for data and one for metadata
18 * to handle the new extents and checksums generated by writing data.
22 * The general flow of the data reservation is as follows
25 * We call into btrfs_reserve_data_bytes() for the user request bytes that
26 * they wish to write. We make this reservation and add it to
27 * space_info->bytes_may_use. We set EXTENT_DELALLOC on the inode io_tree
28 * for the range and carry on if this is buffered, or follow up trying to
29 * make a real allocation if we are pre-allocating or doing O_DIRECT.
32 * At writepages()/prealloc/O_DIRECT time we will call into
33 * btrfs_reserve_extent() for some part or all of this range of bytes. We
34 * will make the allocation and subtract space_info->bytes_may_use by the
35 * original requested length and increase the space_info->bytes_reserved by
36 * the allocated length. This distinction is important because compression
37 * may allocate a smaller on disk extent than we previously reserved.
40 * finish_ordered_io() will insert the new file extent item for this range,
41 * and then add a delayed ref update for the extent tree. Once that delayed
42 * ref is written the extent size is subtracted from
43 * space_info->bytes_reserved and added to space_info->bytes_used.
47 * -> By the reservation maker
48 * This is the simplest case, we haven't completed our operation and we know
49 * how much we reserved, we can simply call
50 * btrfs_free_reserved_data_space*() and it will be removed from
51 * space_info->bytes_may_use.
53 * -> After the reservation has been made, but before cow_file_range()
54 * This is specifically for the delalloc case. You must clear
55 * EXTENT_DELALLOC with the EXTENT_CLEAR_DATA_RESV bit, and the range will
56 * be subtracted from space_info->bytes_may_use.
58 * METADATA RESERVATION
59 * The general metadata reservation lifetimes are discussed elsewhere, this
60 * will just focus on how it is used for delalloc space.
62 * We keep track of two things on a per inode bases
64 * ->outstanding_extents
65 * This is the number of file extent items we'll need to handle all of the
66 * outstanding DELALLOC space we have in this inode. We limit the maximum
67 * size of an extent, so a large contiguous dirty area may require more than
68 * one outstanding_extent, which is why count_max_extents() is used to
69 * determine how many outstanding_extents get added.
72 * This is essentially how many dirty bytes we have for this inode, so we
73 * can calculate the number of checksum items we would have to add in order
74 * to checksum our outstanding data.
76 * We keep a per-inode block_rsv in order to make it easier to keep track of
77 * our reservation. We use btrfs_calculate_inode_block_rsv_size() to
78 * calculate the current theoretical maximum reservation we would need for the
79 * metadata for this inode. We call this and then adjust our reservation as
80 * necessary, either by attempting to reserve more space, or freeing up excess
83 * OUTSTANDING_EXTENTS HANDLING
85 * ->outstanding_extents is used for keeping track of how many extents we will
86 * need to use for this inode, and it will fluctuate depending on where you are
87 * in the life cycle of the dirty data. Consider the following normal case for
88 * a completely clean inode, with a num_bytes < our maximum allowed extent size
91 * ->outstanding_extents += 1 (current value is 1)
94 * ->outstanding_extents += 1 (current value is 2)
96 * -> btrfs_delalloc_release_extents()
97 * ->outstanding_extents -= 1 (current value is 1)
99 * We must call this once we are done, as we hold our reservation for the
100 * duration of our operation, and then assume set_delalloc will update the
101 * counter appropriately.
103 * -> add ordered extent
104 * ->outstanding_extents += 1 (current value is 2)
106 * -> btrfs_clear_delalloc_extent
107 * ->outstanding_extents -= 1 (current value is 1)
109 * -> finish_ordered_io/btrfs_remove_ordered_extent
110 * ->outstanding_extents -= 1 (current value is 0)
112 * Each stage is responsible for their own accounting of the extent, thus
113 * making error handling and cleanup easier.
116 int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
118 struct btrfs_root *root = inode->root;
119 struct btrfs_fs_info *fs_info = root->fs_info;
120 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
122 /* Make sure bytes are sectorsize aligned */
123 bytes = ALIGN(bytes, fs_info->sectorsize);
125 if (btrfs_is_free_space_inode(inode))
126 flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
128 return btrfs_reserve_data_bytes(fs_info, bytes, flush);
131 int btrfs_check_data_free_space(struct btrfs_inode *inode,
132 struct extent_changeset **reserved, u64 start,
133 u64 len, bool noflush)
135 struct btrfs_fs_info *fs_info = inode->root->fs_info;
136 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
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);
145 flush = BTRFS_RESERVE_NO_FLUSH;
146 else if (btrfs_is_free_space_inode(inode))
147 flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
149 ret = btrfs_reserve_data_bytes(fs_info, len, flush);
153 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
154 ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
156 btrfs_free_reserved_data_space_noquota(fs_info, len);
157 extent_changeset_free(*reserved);
166 * Called if we need to clear a data reservation for this inode
167 * Normally in a error case.
169 * This one will *NOT* use accurate qgroup reserved space API, just for case
170 * which we can't sleep and is sure it won't affect qgroup reserved space.
171 * Like clear_bit_hook().
173 void btrfs_free_reserved_data_space_noquota(struct btrfs_fs_info *fs_info,
176 struct btrfs_space_info *data_sinfo;
178 ASSERT(IS_ALIGNED(len, fs_info->sectorsize));
180 data_sinfo = fs_info->data_sinfo;
181 btrfs_space_info_free_bytes_may_use(fs_info, data_sinfo, len);
185 * Called if we need to clear a data reservation for this inode
186 * Normally in a error case.
188 * This one will handle the per-inode data rsv map for accurate reserved
191 void btrfs_free_reserved_data_space(struct btrfs_inode *inode,
192 struct extent_changeset *reserved, u64 start, u64 len)
194 struct btrfs_fs_info *fs_info = inode->root->fs_info;
196 /* Make sure the range is aligned to sectorsize */
197 len = round_up(start + len, fs_info->sectorsize) -
198 round_down(start, fs_info->sectorsize);
199 start = round_down(start, fs_info->sectorsize);
201 btrfs_free_reserved_data_space_noquota(fs_info, len);
202 btrfs_qgroup_free_data(inode, reserved, start, len, NULL);
206 * Release any excessive reservations for an inode.
208 * @inode: the inode we need to release from
209 * @qgroup_free: free or convert qgroup meta. Unlike normal operation, qgroup
210 * meta reservation needs to know if we are freeing qgroup
211 * reservation or just converting it into per-trans. Normally
212 * @qgroup_free is true for error handling, and false for normal
215 * This is the same as btrfs_block_rsv_release, except that it handles the
216 * tracepoint for the reservation.
218 static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
220 struct btrfs_fs_info *fs_info = inode->root->fs_info;
221 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
223 u64 qgroup_to_release = 0;
226 * Since we statically set the block_rsv->size we just want to say we
227 * are releasing 0 bytes, and then we'll just get the reservation over
230 released = btrfs_block_rsv_release(fs_info, block_rsv, 0,
233 trace_btrfs_space_reservation(fs_info, "delalloc",
234 btrfs_ino(inode), released, 0);
236 btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
238 btrfs_qgroup_convert_reserved_meta(inode->root,
242 static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
243 struct btrfs_inode *inode)
245 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
246 u64 reserve_size = 0;
247 u64 qgroup_rsv_size = 0;
249 unsigned outstanding_extents;
251 lockdep_assert_held(&inode->lock);
252 outstanding_extents = inode->outstanding_extents;
255 * Insert size for the number of outstanding extents, 1 normal size for
256 * updating the inode.
258 if (outstanding_extents) {
259 reserve_size = btrfs_calc_insert_metadata_size(fs_info,
260 outstanding_extents);
261 reserve_size += btrfs_calc_metadata_size(fs_info, 1);
263 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
265 reserve_size += btrfs_calc_insert_metadata_size(fs_info,
268 * For qgroup rsv, the calculation is very simple:
269 * account one nodesize for each outstanding extent
271 * This is overestimating in most cases.
273 qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
275 spin_lock(&block_rsv->lock);
276 block_rsv->size = reserve_size;
277 block_rsv->qgroup_rsv_size = qgroup_rsv_size;
278 spin_unlock(&block_rsv->lock);
281 static void calc_inode_reservations(struct btrfs_fs_info *fs_info,
282 u64 num_bytes, u64 disk_num_bytes,
283 u64 *meta_reserve, u64 *qgroup_reserve)
285 u64 nr_extents = count_max_extents(fs_info, num_bytes);
286 u64 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, disk_num_bytes);
287 u64 inode_update = btrfs_calc_metadata_size(fs_info, 1);
289 *meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
290 nr_extents + csum_leaves);
293 * finish_ordered_io has to update the inode, so add the space required
294 * for an inode update.
296 *meta_reserve += inode_update;
297 *qgroup_reserve = nr_extents * fs_info->nodesize;
300 int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
301 u64 disk_num_bytes, bool noflush)
303 struct btrfs_root *root = inode->root;
304 struct btrfs_fs_info *fs_info = root->fs_info;
305 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
306 u64 meta_reserve, qgroup_reserve;
308 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
312 * If we are a free space inode we need to not flush since we will be in
313 * the middle of a transaction commit. We also don't need the delalloc
314 * mutex since we won't race with anybody. We need this mostly to make
315 * lockdep shut its filthy mouth.
317 * If we have a transaction open (can happen if we call truncate_block
318 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
320 if (noflush || btrfs_is_free_space_inode(inode)) {
321 flush = BTRFS_RESERVE_NO_FLUSH;
323 if (current->journal_info)
324 flush = BTRFS_RESERVE_FLUSH_LIMIT;
327 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
328 disk_num_bytes = ALIGN(disk_num_bytes, fs_info->sectorsize);
331 * We always want to do it this way, every other way is wrong and ends
332 * in tears. Pre-reserving the amount we are going to add will always
333 * be the right way, because otherwise if we have enough parallelism we
334 * could end up with thousands of inodes all holding little bits of
335 * reservations they were able to make previously and the only way to
336 * reclaim that space is to ENOSPC out the operations and clear
337 * everything out and try again, which is bad. This way we just
338 * over-reserve slightly, and clean up the mess when we are done.
340 calc_inode_reservations(fs_info, num_bytes, disk_num_bytes,
341 &meta_reserve, &qgroup_reserve);
342 ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserve, true,
346 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, meta_reserve, flush);
348 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
353 * Now we need to update our outstanding extents and csum bytes _first_
354 * and then add the reservation to the block_rsv. This keeps us from
355 * racing with an ordered completion or some such that would think it
356 * needs to free the reservation we just made.
358 nr_extents = count_max_extents(fs_info, num_bytes);
359 spin_lock(&inode->lock);
360 btrfs_mod_outstanding_extents(inode, nr_extents);
361 inode->csum_bytes += disk_num_bytes;
362 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
363 spin_unlock(&inode->lock);
365 /* Now we can safely add our space to our block rsv */
366 btrfs_block_rsv_add_bytes(block_rsv, meta_reserve, false);
367 trace_btrfs_space_reservation(root->fs_info, "delalloc",
368 btrfs_ino(inode), meta_reserve, 1);
370 spin_lock(&block_rsv->lock);
371 block_rsv->qgroup_rsv_reserved += qgroup_reserve;
372 spin_unlock(&block_rsv->lock);
378 * Release a metadata reservation for an inode.
380 * @inode: the inode to release the reservation for.
381 * @num_bytes: the number of bytes we are releasing.
382 * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
384 * This will release the metadata reservation for an inode. This can be called
385 * once we complete IO for a given set of bytes to release their metadata
386 * reservations, or on error for the same reason.
388 void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
391 struct btrfs_fs_info *fs_info = inode->root->fs_info;
393 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
394 spin_lock(&inode->lock);
395 inode->csum_bytes -= num_bytes;
396 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
397 spin_unlock(&inode->lock);
399 if (btrfs_is_testing(fs_info))
402 btrfs_inode_rsv_release(inode, qgroup_free);
406 * Release our outstanding_extents for an inode.
408 * @inode: the inode to balance the reservation for.
409 * @num_bytes: the number of bytes we originally reserved with
411 * When we reserve space we increase outstanding_extents for the extents we may
412 * add. Once we've set the range as delalloc or created our ordered extents we
413 * have outstanding_extents to track the real usage, so we use this to free our
414 * temporarily tracked outstanding_extents. This _must_ be used in conjunction
415 * with btrfs_delalloc_reserve_metadata.
417 void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes)
419 struct btrfs_fs_info *fs_info = inode->root->fs_info;
420 unsigned num_extents;
422 spin_lock(&inode->lock);
423 num_extents = count_max_extents(fs_info, num_bytes);
424 btrfs_mod_outstanding_extents(inode, -num_extents);
425 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
426 spin_unlock(&inode->lock);
428 if (btrfs_is_testing(fs_info))
431 btrfs_inode_rsv_release(inode, true);
435 * Reserve data and metadata space for delalloc
437 * @inode: inode we're writing to
438 * @start: start range we are writing to
439 * @len: how long the range we are writing to
440 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
441 * current reservation.
443 * This will do the following things
445 * - reserve space in data space info for num bytes and reserve precious
446 * corresponding qgroup space
447 * (Done in check_data_free_space)
449 * - reserve space for metadata space, based on the number of outstanding
450 * extents and how much csums will be needed also reserve metadata space in a
451 * per root over-reserve method.
452 * - add to the inodes->delalloc_bytes
453 * - add it to the fs_info's delalloc inodes list.
454 * (Above 3 all done in delalloc_reserve_metadata)
456 * Return 0 for success
457 * Return <0 for error(-ENOSPC or -EDQUOT)
459 int btrfs_delalloc_reserve_space(struct btrfs_inode *inode,
460 struct extent_changeset **reserved, u64 start, u64 len)
464 ret = btrfs_check_data_free_space(inode, reserved, start, len, false);
467 ret = btrfs_delalloc_reserve_metadata(inode, len, len, false);
469 btrfs_free_reserved_data_space(inode, *reserved, start, len);
470 extent_changeset_free(*reserved);
477 * Release data and metadata space for delalloc
479 * @inode: inode we're releasing space for
480 * @reserved: list of changed/reserved ranges
481 * @start: start position of the space already reserved
482 * @len: length of the space already reserved
483 * @qgroup_free: should qgroup reserved-space also be freed
485 * Release the metadata space that was not used and will decrement
486 * ->delalloc_bytes and remove it from the fs_info->delalloc_inodes list if
487 * there are no delalloc bytes left. Also it will handle the qgroup reserved
490 void btrfs_delalloc_release_space(struct btrfs_inode *inode,
491 struct extent_changeset *reserved,
492 u64 start, u64 len, bool qgroup_free)
494 btrfs_delalloc_release_metadata(inode, len, qgroup_free);
495 btrfs_free_reserved_data_space(inode, reserved, start, len);