1 // SPDX-License-Identifier: GPL-2.0
4 #include "delalloc-space.h"
6 #include "btrfs_inode.h"
7 #include "space-info.h"
8 #include "transaction.h"
10 #include "block-group.h"
12 int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode
*inode
, u64 bytes
)
14 struct btrfs_root
*root
= inode
->root
;
15 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
16 struct btrfs_space_info
*data_sinfo
= fs_info
->data_sinfo
;
20 int have_pinned_space
;
22 /* Make sure bytes are sectorsize aligned */
23 bytes
= ALIGN(bytes
, fs_info
->sectorsize
);
25 if (btrfs_is_free_space_inode(inode
)) {
27 ASSERT(current
->journal_info
);
31 /* Make sure we have enough space to handle the data first */
32 spin_lock(&data_sinfo
->lock
);
33 used
= btrfs_space_info_used(data_sinfo
, true);
35 if (used
+ bytes
> data_sinfo
->total_bytes
) {
36 struct btrfs_trans_handle
*trans
;
39 * If we don't have enough free bytes in this space then we need
40 * to alloc a new chunk.
42 if (!data_sinfo
->full
) {
45 data_sinfo
->force_alloc
= CHUNK_ALLOC_FORCE
;
46 spin_unlock(&data_sinfo
->lock
);
48 alloc_target
= btrfs_data_alloc_profile(fs_info
);
50 * It is ugly that we don't call nolock join
51 * transaction for the free space inode case here.
52 * But it is safe because we only do the data space
53 * reservation for the free space cache in the
54 * transaction context, the common join transaction
55 * just increase the counter of the current transaction
56 * handler, doesn't try to acquire the trans_lock of
59 trans
= btrfs_join_transaction(root
);
61 return PTR_ERR(trans
);
63 ret
= btrfs_chunk_alloc(trans
, alloc_target
,
64 CHUNK_ALLOC_NO_FORCE
);
65 btrfs_end_transaction(trans
);
70 have_pinned_space
= 1;
79 * If we don't have enough pinned space to deal with this
80 * allocation, and no removed chunk in current transaction,
81 * don't bother committing the transaction.
83 have_pinned_space
= __percpu_counter_compare(
84 &data_sinfo
->total_bytes_pinned
,
85 used
+ bytes
- data_sinfo
->total_bytes
,
86 BTRFS_TOTAL_BYTES_PINNED_BATCH
);
87 spin_unlock(&data_sinfo
->lock
);
89 /* Commit the current transaction and try again */
94 if (need_commit
> 0) {
95 btrfs_start_delalloc_roots(fs_info
, -1);
96 btrfs_wait_ordered_roots(fs_info
, U64_MAX
, 0,
100 trans
= btrfs_join_transaction(root
);
102 return PTR_ERR(trans
);
103 if (have_pinned_space
>= 0 ||
104 test_bit(BTRFS_TRANS_HAVE_FREE_BGS
,
105 &trans
->transaction
->flags
) ||
107 ret
= btrfs_commit_transaction(trans
);
111 * The cleaner kthread might still be doing iput
112 * operations. Wait for it to finish so that
113 * more space is released. We don't need to
114 * explicitly run the delayed iputs here because
115 * the commit_transaction would have woken up
118 ret
= btrfs_wait_on_delayed_iputs(fs_info
);
123 btrfs_end_transaction(trans
);
127 trace_btrfs_space_reservation(fs_info
,
129 data_sinfo
->flags
, bytes
, 1);
132 btrfs_space_info_update_bytes_may_use(fs_info
, data_sinfo
, bytes
);
133 spin_unlock(&data_sinfo
->lock
);
138 int btrfs_check_data_free_space(struct inode
*inode
,
139 struct extent_changeset
**reserved
, u64 start
, u64 len
)
141 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
144 /* align the range */
145 len
= round_up(start
+ len
, fs_info
->sectorsize
) -
146 round_down(start
, fs_info
->sectorsize
);
147 start
= round_down(start
, fs_info
->sectorsize
);
149 ret
= btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode
), len
);
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(inode
, start
, len
);
163 * Called if we need to clear a data reservation for this inode
164 * Normally in a error case.
166 * This one will *NOT* use accurate qgroup reserved space API, just for case
167 * which we can't sleep and is sure it won't affect qgroup reserved space.
168 * Like clear_bit_hook().
170 void btrfs_free_reserved_data_space_noquota(struct inode
*inode
, u64 start
,
173 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
174 struct btrfs_space_info
*data_sinfo
;
176 /* Make sure the range is aligned to sectorsize */
177 len
= round_up(start
+ len
, fs_info
->sectorsize
) -
178 round_down(start
, fs_info
->sectorsize
);
179 start
= round_down(start
, fs_info
->sectorsize
);
181 data_sinfo
= fs_info
->data_sinfo
;
182 spin_lock(&data_sinfo
->lock
);
183 btrfs_space_info_update_bytes_may_use(fs_info
, data_sinfo
, -len
);
184 spin_unlock(&data_sinfo
->lock
);
188 * Called if we need to clear a data reservation for this inode
189 * Normally in a error case.
191 * This one will handle the per-inode data rsv map for accurate reserved
194 void btrfs_free_reserved_data_space(struct inode
*inode
,
195 struct extent_changeset
*reserved
, u64 start
, u64 len
)
197 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
199 /* Make sure the range is aligned to sectorsize */
200 len
= round_up(start
+ len
, root
->fs_info
->sectorsize
) -
201 round_down(start
, root
->fs_info
->sectorsize
);
202 start
= round_down(start
, root
->fs_info
->sectorsize
);
204 btrfs_free_reserved_data_space_noquota(inode
, start
, len
);
205 btrfs_qgroup_free_data(inode
, reserved
, start
, len
);
209 * btrfs_inode_rsv_release - release any excessive reservation.
210 * @inode - the inode we need to release from.
211 * @qgroup_free - free or convert qgroup meta.
212 * Unlike normal operation, qgroup meta reservation needs to know if we are
213 * freeing qgroup reservation or just converting it into per-trans. Normally
214 * @qgroup_free is true for error handling, and false for normal release.
216 * This is the same as btrfs_block_rsv_release, except that it handles the
217 * tracepoint for the reservation.
219 static void btrfs_inode_rsv_release(struct btrfs_inode
*inode
, bool qgroup_free
)
221 struct btrfs_fs_info
*fs_info
= inode
->root
->fs_info
;
222 struct btrfs_block_rsv
*block_rsv
= &inode
->block_rsv
;
224 u64 qgroup_to_release
= 0;
227 * Since we statically set the block_rsv->size we just want to say we
228 * are releasing 0 bytes, and then we'll just get the reservation over
231 released
= __btrfs_block_rsv_release(fs_info
, block_rsv
, 0,
234 trace_btrfs_space_reservation(fs_info
, "delalloc",
235 btrfs_ino(inode
), released
, 0);
237 btrfs_qgroup_free_meta_prealloc(inode
->root
, qgroup_to_release
);
239 btrfs_qgroup_convert_reserved_meta(inode
->root
,
243 static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info
*fs_info
,
244 struct btrfs_inode
*inode
)
246 struct btrfs_block_rsv
*block_rsv
= &inode
->block_rsv
;
247 u64 reserve_size
= 0;
248 u64 qgroup_rsv_size
= 0;
250 unsigned outstanding_extents
;
252 lockdep_assert_held(&inode
->lock
);
253 outstanding_extents
= inode
->outstanding_extents
;
256 * Insert size for the number of outstanding extents, 1 normal size for
257 * updating the inode.
259 if (outstanding_extents
) {
260 reserve_size
= btrfs_calc_insert_metadata_size(fs_info
,
261 outstanding_extents
);
262 reserve_size
+= btrfs_calc_metadata_size(fs_info
, 1);
264 csum_leaves
= btrfs_csum_bytes_to_leaves(fs_info
,
266 reserve_size
+= btrfs_calc_insert_metadata_size(fs_info
,
269 * For qgroup rsv, the calculation is very simple:
270 * account one nodesize for each outstanding extent
272 * This is overestimating in most cases.
274 qgroup_rsv_size
= (u64
)outstanding_extents
* fs_info
->nodesize
;
276 spin_lock(&block_rsv
->lock
);
277 block_rsv
->size
= reserve_size
;
278 block_rsv
->qgroup_rsv_size
= qgroup_rsv_size
;
279 spin_unlock(&block_rsv
->lock
);
282 static void calc_inode_reservations(struct btrfs_fs_info
*fs_info
,
283 u64 num_bytes
, u64
*meta_reserve
,
286 u64 nr_extents
= count_max_extents(num_bytes
);
287 u64 csum_leaves
= btrfs_csum_bytes_to_leaves(fs_info
, num_bytes
);
288 u64 inode_update
= btrfs_calc_metadata_size(fs_info
, 1);
290 *meta_reserve
= btrfs_calc_insert_metadata_size(fs_info
,
291 nr_extents
+ csum_leaves
);
294 * finish_ordered_io has to update the inode, so add the space required
295 * for an inode update.
297 *meta_reserve
+= inode_update
;
298 *qgroup_reserve
= nr_extents
* fs_info
->nodesize
;
301 int btrfs_delalloc_reserve_metadata(struct btrfs_inode
*inode
, u64 num_bytes
)
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 (btrfs_is_free_space_inode(inode
)) {
321 flush
= BTRFS_RESERVE_NO_FLUSH
;
323 if (current
->journal_info
)
324 flush
= BTRFS_RESERVE_FLUSH_LIMIT
;
326 if (btrfs_transaction_in_commit(fs_info
))
330 num_bytes
= ALIGN(num_bytes
, fs_info
->sectorsize
);
333 * We always want to do it this way, every other way is wrong and ends
334 * in tears. Pre-reserving the amount we are going to add will always
335 * be the right way, because otherwise if we have enough parallelism we
336 * could end up with thousands of inodes all holding little bits of
337 * reservations they were able to make previously and the only way to
338 * reclaim that space is to ENOSPC out the operations and clear
339 * everything out and try again, which is bad. This way we just
340 * over-reserve slightly, and clean up the mess when we are done.
342 calc_inode_reservations(fs_info
, num_bytes
, &meta_reserve
,
344 ret
= btrfs_qgroup_reserve_meta_prealloc(root
, qgroup_reserve
, true);
347 ret
= btrfs_reserve_metadata_bytes(root
, block_rsv
, meta_reserve
, flush
);
349 btrfs_qgroup_free_meta_prealloc(root
, qgroup_reserve
);
354 * Now we need to update our outstanding extents and csum bytes _first_
355 * and then add the reservation to the block_rsv. This keeps us from
356 * racing with an ordered completion or some such that would think it
357 * needs to free the reservation we just made.
359 spin_lock(&inode
->lock
);
360 nr_extents
= count_max_extents(num_bytes
);
361 btrfs_mod_outstanding_extents(inode
, nr_extents
);
362 inode
->csum_bytes
+= num_bytes
;
363 btrfs_calculate_inode_block_rsv_size(fs_info
, inode
);
364 spin_unlock(&inode
->lock
);
366 /* Now we can safely add our space to our block rsv */
367 btrfs_block_rsv_add_bytes(block_rsv
, meta_reserve
, false);
368 trace_btrfs_space_reservation(root
->fs_info
, "delalloc",
369 btrfs_ino(inode
), meta_reserve
, 1);
371 spin_lock(&block_rsv
->lock
);
372 block_rsv
->qgroup_rsv_reserved
+= qgroup_reserve
;
373 spin_unlock(&block_rsv
->lock
);
379 * btrfs_delalloc_release_metadata - 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 * btrfs_delalloc_release_extents - release our outstanding_extents
407 * @inode: the inode to balance the reservation for.
408 * @num_bytes: the number of bytes we originally reserved with
410 * When we reserve space we increase outstanding_extents for the extents we may
411 * add. Once we've set the range as delalloc or created our ordered extents we
412 * have outstanding_extents to track the real usage, so we use this to free our
413 * temporarily tracked outstanding_extents. This _must_ be used in conjunction
414 * with btrfs_delalloc_reserve_metadata.
416 void btrfs_delalloc_release_extents(struct btrfs_inode
*inode
, u64 num_bytes
)
418 struct btrfs_fs_info
*fs_info
= inode
->root
->fs_info
;
419 unsigned num_extents
;
421 spin_lock(&inode
->lock
);
422 num_extents
= count_max_extents(num_bytes
);
423 btrfs_mod_outstanding_extents(inode
, -num_extents
);
424 btrfs_calculate_inode_block_rsv_size(fs_info
, inode
);
425 spin_unlock(&inode
->lock
);
427 if (btrfs_is_testing(fs_info
))
430 btrfs_inode_rsv_release(inode
, true);
434 * btrfs_delalloc_reserve_space - reserve data and metadata space for
436 * @inode: inode we're writing to
437 * @start: start range we are writing to
438 * @len: how long the range we are writing to
439 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
440 * current reservation.
442 * This will do the following things
444 * - reserve space in data space info for num bytes
445 * and reserve precious corresponding qgroup space
446 * (Done in check_data_free_space)
448 * - reserve space for metadata space, based on the number of outstanding
449 * extents and how much csums will be needed
450 * also reserve metadata space in a per root over-reserve method.
451 * - add to the inodes->delalloc_bytes
452 * - add it to the fs_info's delalloc inodes list.
453 * (Above 3 all done in delalloc_reserve_metadata)
455 * Return 0 for success
456 * Return <0 for error(-ENOSPC or -EQUOT)
458 int btrfs_delalloc_reserve_space(struct inode
*inode
,
459 struct extent_changeset
**reserved
, u64 start
, u64 len
)
463 ret
= btrfs_check_data_free_space(inode
, reserved
, start
, len
);
466 ret
= btrfs_delalloc_reserve_metadata(BTRFS_I(inode
), len
);
468 btrfs_free_reserved_data_space(inode
, *reserved
, start
, len
);
473 * btrfs_delalloc_release_space - release data and metadata space for delalloc
474 * @inode: inode we're releasing space for
475 * @start: start position of the space already reserved
476 * @len: the len of the space already reserved
477 * @release_bytes: the len of the space we consumed or didn't use
479 * This function will release the metadata space that was not used and will
480 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
481 * list if there are no delalloc bytes left.
482 * Also it will handle the qgroup reserved space.
484 void btrfs_delalloc_release_space(struct inode
*inode
,
485 struct extent_changeset
*reserved
,
486 u64 start
, u64 len
, bool qgroup_free
)
488 btrfs_delalloc_release_metadata(BTRFS_I(inode
), len
, qgroup_free
);
489 btrfs_free_reserved_data_space(inode
, reserved
, start
, len
);