blob: 7aa8a395d838c1e228cd00cd3801685b7d84eb56 [file] [log] [blame]
Josef Bacik86736342019-06-19 15:12:00 -04001// SPDX-License-Identifier: GPL-2.0
2
Josef Bacik9b569ea2022-10-19 10:50:49 -04003#include "messages.h"
Josef Bacik86736342019-06-19 15:12:00 -04004#include "ctree.h"
5#include "delalloc-space.h"
6#include "block-rsv.h"
7#include "btrfs_inode.h"
8#include "space-info.h"
Josef Bacik86736342019-06-19 15:12:00 -04009#include "qgroup.h"
Josef Bacikec8eb372022-10-19 10:50:51 -040010#include "fs.h"
Josef Bacik86736342019-06-19 15:12:00 -040011
Josef Bacik6f4ad5592020-02-04 13:18:55 -050012/*
13 * HOW DOES THIS WORK
14 *
15 * There are two stages to data reservations, one for data and one for metadata
16 * to handle the new extents and checksums generated by writing data.
17 *
18 *
19 * DATA RESERVATION
20 * The general flow of the data reservation is as follows
21 *
22 * -> Reserve
23 * We call into btrfs_reserve_data_bytes() for the user request bytes that
24 * they wish to write. We make this reservation and add it to
25 * space_info->bytes_may_use. We set EXTENT_DELALLOC on the inode io_tree
26 * for the range and carry on if this is buffered, or follow up trying to
27 * make a real allocation if we are pre-allocating or doing O_DIRECT.
28 *
29 * -> Use
30 * At writepages()/prealloc/O_DIRECT time we will call into
31 * btrfs_reserve_extent() for some part or all of this range of bytes. We
32 * will make the allocation and subtract space_info->bytes_may_use by the
33 * original requested length and increase the space_info->bytes_reserved by
34 * the allocated length. This distinction is important because compression
35 * may allocate a smaller on disk extent than we previously reserved.
36 *
37 * -> Allocation
38 * finish_ordered_io() will insert the new file extent item for this range,
39 * and then add a delayed ref update for the extent tree. Once that delayed
40 * ref is written the extent size is subtracted from
41 * space_info->bytes_reserved and added to space_info->bytes_used.
42 *
43 * Error handling
44 *
45 * -> By the reservation maker
46 * This is the simplest case, we haven't completed our operation and we know
47 * how much we reserved, we can simply call
48 * btrfs_free_reserved_data_space*() and it will be removed from
49 * space_info->bytes_may_use.
50 *
51 * -> After the reservation has been made, but before cow_file_range()
52 * This is specifically for the delalloc case. You must clear
53 * EXTENT_DELALLOC with the EXTENT_CLEAR_DATA_RESV bit, and the range will
54 * be subtracted from space_info->bytes_may_use.
55 *
56 * METADATA RESERVATION
57 * The general metadata reservation lifetimes are discussed elsewhere, this
58 * will just focus on how it is used for delalloc space.
59 *
60 * We keep track of two things on a per inode bases
61 *
62 * ->outstanding_extents
63 * This is the number of file extent items we'll need to handle all of the
64 * outstanding DELALLOC space we have in this inode. We limit the maximum
65 * size of an extent, so a large contiguous dirty area may require more than
66 * one outstanding_extent, which is why count_max_extents() is used to
67 * determine how many outstanding_extents get added.
68 *
69 * ->csum_bytes
70 * This is essentially how many dirty bytes we have for this inode, so we
71 * can calculate the number of checksum items we would have to add in order
72 * to checksum our outstanding data.
73 *
74 * We keep a per-inode block_rsv in order to make it easier to keep track of
75 * our reservation. We use btrfs_calculate_inode_block_rsv_size() to
76 * calculate the current theoretical maximum reservation we would need for the
77 * metadata for this inode. We call this and then adjust our reservation as
78 * necessary, either by attempting to reserve more space, or freeing up excess
79 * space.
80 *
81 * OUTSTANDING_EXTENTS HANDLING
82 *
83 * ->outstanding_extents is used for keeping track of how many extents we will
84 * need to use for this inode, and it will fluctuate depending on where you are
85 * in the life cycle of the dirty data. Consider the following normal case for
86 * a completely clean inode, with a num_bytes < our maximum allowed extent size
87 *
88 * -> reserve
89 * ->outstanding_extents += 1 (current value is 1)
90 *
91 * -> set_delalloc
David Sterba1a9fd412021-05-21 17:42:23 +020092 * ->outstanding_extents += 1 (current value is 2)
Josef Bacik6f4ad5592020-02-04 13:18:55 -050093 *
94 * -> btrfs_delalloc_release_extents()
95 * ->outstanding_extents -= 1 (current value is 1)
96 *
97 * We must call this once we are done, as we hold our reservation for the
98 * duration of our operation, and then assume set_delalloc will update the
99 * counter appropriately.
100 *
101 * -> add ordered extent
102 * ->outstanding_extents += 1 (current value is 2)
103 *
104 * -> btrfs_clear_delalloc_extent
105 * ->outstanding_extents -= 1 (current value is 1)
106 *
107 * -> finish_ordered_io/btrfs_remove_ordered_extent
108 * ->outstanding_extents -= 1 (current value is 0)
109 *
110 * Each stage is responsible for their own accounting of the extent, thus
111 * making error handling and cleanup easier.
112 */
113
David Sterba2917f742024-05-30 19:14:12 +0200114int btrfs_alloc_data_chunk_ondemand(const struct btrfs_inode *inode, u64 bytes)
Josef Bacik86736342019-06-19 15:12:00 -0400115{
116 struct btrfs_root *root = inode->root;
117 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik8698fc42020-07-21 10:22:25 -0400118 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
Josef Bacik86736342019-06-19 15:12:00 -0400119
120 /* Make sure bytes are sectorsize aligned */
121 bytes = ALIGN(bytes, fs_info->sectorsize);
122
Josef Bacik8698fc42020-07-21 10:22:25 -0400123 if (btrfs_is_free_space_inode(inode))
124 flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
Josef Bacik86736342019-06-19 15:12:00 -0400125
Josef Bacik8698fc42020-07-21 10:22:25 -0400126 return btrfs_reserve_data_bytes(fs_info, bytes, flush);
Josef Bacik86736342019-06-19 15:12:00 -0400127}
128
Nikolay Borisov36ea6f32020-06-03 08:55:41 +0300129int btrfs_check_data_free_space(struct btrfs_inode *inode,
Josef Bacik1daedb12022-09-12 12:27:44 -0700130 struct extent_changeset **reserved, u64 start,
131 u64 len, bool noflush)
Josef Bacik86736342019-06-19 15:12:00 -0400132{
Nikolay Borisov36ea6f32020-06-03 08:55:41 +0300133 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik1daedb12022-09-12 12:27:44 -0700134 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
Josef Bacik86736342019-06-19 15:12:00 -0400135 int ret;
136
137 /* align the range */
138 len = round_up(start + len, fs_info->sectorsize) -
139 round_down(start, fs_info->sectorsize);
140 start = round_down(start, fs_info->sectorsize);
141
Josef Bacik1daedb12022-09-12 12:27:44 -0700142 if (noflush)
143 flush = BTRFS_RESERVE_NO_FLUSH;
144 else if (btrfs_is_free_space_inode(inode))
145 flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
146
147 ret = btrfs_reserve_data_bytes(fs_info, len, flush);
Josef Bacik86736342019-06-19 15:12:00 -0400148 if (ret < 0)
149 return ret;
150
151 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
Nikolay Borisov36ea6f32020-06-03 08:55:41 +0300152 ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
Johannes Thumshirnda5e8172021-12-03 02:55:33 -0800153 if (ret < 0) {
Nikolay Borisov9db5d512020-06-03 08:55:38 +0300154 btrfs_free_reserved_data_space_noquota(fs_info, len);
Johannes Thumshirnda5e8172021-12-03 02:55:33 -0800155 extent_changeset_free(*reserved);
156 *reserved = NULL;
157 } else {
Josef Bacik86736342019-06-19 15:12:00 -0400158 ret = 0;
Johannes Thumshirnda5e8172021-12-03 02:55:33 -0800159 }
Josef Bacik86736342019-06-19 15:12:00 -0400160 return ret;
161}
162
163/*
164 * Called if we need to clear a data reservation for this inode
165 * Normally in a error case.
166 *
167 * This one will *NOT* use accurate qgroup reserved space API, just for case
168 * which we can't sleep and is sure it won't affect qgroup reserved space.
169 * Like clear_bit_hook().
170 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +0300171void btrfs_free_reserved_data_space_noquota(struct btrfs_fs_info *fs_info,
Josef Bacik86736342019-06-19 15:12:00 -0400172 u64 len)
173{
Josef Bacik86736342019-06-19 15:12:00 -0400174 struct btrfs_space_info *data_sinfo;
175
Filipe Manana46d4dac2020-06-09 11:19:33 +0100176 ASSERT(IS_ALIGNED(len, fs_info->sectorsize));
Josef Bacik86736342019-06-19 15:12:00 -0400177
178 data_sinfo = fs_info->data_sinfo;
Josef Bacik39753e42020-07-21 10:22:20 -0400179 btrfs_space_info_free_bytes_may_use(fs_info, data_sinfo, len);
Josef Bacik86736342019-06-19 15:12:00 -0400180}
181
182/*
183 * Called if we need to clear a data reservation for this inode
184 * Normally in a error case.
185 *
186 * This one will handle the per-inode data rsv map for accurate reserved
187 * space framework.
188 */
Nikolay Borisov25ce28c2020-06-03 08:55:39 +0300189void btrfs_free_reserved_data_space(struct btrfs_inode *inode,
Josef Bacik86736342019-06-19 15:12:00 -0400190 struct extent_changeset *reserved, u64 start, u64 len)
191{
Nikolay Borisov25ce28c2020-06-03 08:55:39 +0300192 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik86736342019-06-19 15:12:00 -0400193
194 /* Make sure the range is aligned to sectorsize */
Nikolay Borisov25ce28c2020-06-03 08:55:39 +0300195 len = round_up(start + len, fs_info->sectorsize) -
196 round_down(start, fs_info->sectorsize);
197 start = round_down(start, fs_info->sectorsize);
Josef Bacik86736342019-06-19 15:12:00 -0400198
Nikolay Borisov25ce28c2020-06-03 08:55:39 +0300199 btrfs_free_reserved_data_space_noquota(fs_info, len);
Boris Burkov9e65bfc2023-12-01 13:00:10 -0800200 btrfs_qgroup_free_data(inode, reserved, start, len, NULL);
Josef Bacik86736342019-06-19 15:12:00 -0400201}
202
David Sterba43dd5292022-10-27 14:21:42 +0200203/*
204 * Release any excessive reservations for an inode.
Nikolay Borisovb762d1d2021-01-22 11:58:01 +0200205 *
206 * @inode: the inode we need to release from
207 * @qgroup_free: free or convert qgroup meta. Unlike normal operation, qgroup
208 * meta reservation needs to know if we are freeing qgroup
209 * reservation or just converting it into per-trans. Normally
210 * @qgroup_free is true for error handling, and false for normal
211 * release.
Josef Bacik86736342019-06-19 15:12:00 -0400212 *
213 * This is the same as btrfs_block_rsv_release, except that it handles the
214 * tracepoint for the reservation.
215 */
216static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
217{
218 struct btrfs_fs_info *fs_info = inode->root->fs_info;
219 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
220 u64 released = 0;
221 u64 qgroup_to_release = 0;
222
223 /*
224 * Since we statically set the block_rsv->size we just want to say we
225 * are releasing 0 bytes, and then we'll just get the reservation over
226 * the size free'd.
227 */
Nikolay Borisov63f018b2020-03-10 10:59:31 +0200228 released = btrfs_block_rsv_release(fs_info, block_rsv, 0,
229 &qgroup_to_release);
Josef Bacik86736342019-06-19 15:12:00 -0400230 if (released > 0)
231 trace_btrfs_space_reservation(fs_info, "delalloc",
232 btrfs_ino(inode), released, 0);
233 if (qgroup_free)
234 btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
235 else
236 btrfs_qgroup_convert_reserved_meta(inode->root,
237 qgroup_to_release);
238}
239
240static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
241 struct btrfs_inode *inode)
242{
243 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
244 u64 reserve_size = 0;
245 u64 qgroup_rsv_size = 0;
Josef Bacik86736342019-06-19 15:12:00 -0400246 unsigned outstanding_extents;
247
248 lockdep_assert_held(&inode->lock);
249 outstanding_extents = inode->outstanding_extents;
Josef Bacikbcacf5f32019-08-22 15:14:34 -0400250
251 /*
252 * Insert size for the number of outstanding extents, 1 normal size for
253 * updating the inode.
254 */
255 if (outstanding_extents) {
Josef Bacik2bd36e72019-08-22 15:14:33 -0400256 reserve_size = btrfs_calc_insert_metadata_size(fs_info,
Josef Bacikbcacf5f32019-08-22 15:14:34 -0400257 outstanding_extents);
258 reserve_size += btrfs_calc_metadata_size(fs_info, 1);
259 }
Filipe Mananafeefe1f2024-01-31 17:18:04 +0000260 if (!(inode->flags & BTRFS_INODE_NODATASUM)) {
261 u64 csum_leaves;
262
263 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes);
264 reserve_size += btrfs_calc_insert_metadata_size(fs_info, csum_leaves);
265 }
Josef Bacik86736342019-06-19 15:12:00 -0400266 /*
267 * For qgroup rsv, the calculation is very simple:
268 * account one nodesize for each outstanding extent
269 *
270 * This is overestimating in most cases.
271 */
272 qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
273
274 spin_lock(&block_rsv->lock);
275 block_rsv->size = reserve_size;
276 block_rsv->qgroup_rsv_size = qgroup_rsv_size;
277 spin_unlock(&block_rsv->lock);
278}
279
Filipe Mananafeefe1f2024-01-31 17:18:04 +0000280static void calc_inode_reservations(struct btrfs_inode *inode,
Omar Sandoval28c9b1e2019-11-18 22:45:55 -0800281 u64 num_bytes, u64 disk_num_bytes,
282 u64 *meta_reserve, u64 *qgroup_reserve)
Josef Bacik86736342019-06-19 15:12:00 -0400283{
Filipe Mananafeefe1f2024-01-31 17:18:04 +0000284 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Naohiro Aota7d7672b2022-07-09 08:18:41 +0900285 u64 nr_extents = count_max_extents(fs_info, num_bytes);
Filipe Mananafeefe1f2024-01-31 17:18:04 +0000286 u64 csum_leaves;
Josef Bacikbcacf5f32019-08-22 15:14:34 -0400287 u64 inode_update = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacik86736342019-06-19 15:12:00 -0400288
Filipe Mananafeefe1f2024-01-31 17:18:04 +0000289 if (inode->flags & BTRFS_INODE_NODATASUM)
290 csum_leaves = 0;
291 else
292 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, disk_num_bytes);
293
Josef Bacik2bd36e72019-08-22 15:14:33 -0400294 *meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
Josef Bacikbcacf5f32019-08-22 15:14:34 -0400295 nr_extents + csum_leaves);
296
297 /*
298 * finish_ordered_io has to update the inode, so add the space required
299 * for an inode update.
300 */
301 *meta_reserve += inode_update;
Josef Bacik86736342019-06-19 15:12:00 -0400302 *qgroup_reserve = nr_extents * fs_info->nodesize;
303}
304
Omar Sandoval28c9b1e2019-11-18 22:45:55 -0800305int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
Filipe Mananad4135132022-03-23 16:19:30 +0000306 u64 disk_num_bytes, bool noflush)
Josef Bacik86736342019-06-19 15:12:00 -0400307{
308 struct btrfs_root *root = inode->root;
309 struct btrfs_fs_info *fs_info = root->fs_info;
310 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
311 u64 meta_reserve, qgroup_reserve;
312 unsigned nr_extents;
313 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
314 int ret = 0;
Josef Bacik86736342019-06-19 15:12:00 -0400315
316 /*
317 * If we are a free space inode we need to not flush since we will be in
318 * the middle of a transaction commit. We also don't need the delalloc
319 * mutex since we won't race with anybody. We need this mostly to make
320 * lockdep shut its filthy mouth.
321 *
322 * If we have a transaction open (can happen if we call truncate_block
323 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
324 */
Filipe Mananad4135132022-03-23 16:19:30 +0000325 if (noflush || btrfs_is_free_space_inode(inode)) {
Josef Bacik86736342019-06-19 15:12:00 -0400326 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik86736342019-06-19 15:12:00 -0400327 } else {
328 if (current->journal_info)
329 flush = BTRFS_RESERVE_FLUSH_LIMIT;
Josef Bacik86736342019-06-19 15:12:00 -0400330 }
331
Josef Bacik86736342019-06-19 15:12:00 -0400332 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Omar Sandoval28c9b1e2019-11-18 22:45:55 -0800333 disk_num_bytes = ALIGN(disk_num_bytes, fs_info->sectorsize);
Josef Bacik86736342019-06-19 15:12:00 -0400334
335 /*
336 * We always want to do it this way, every other way is wrong and ends
337 * in tears. Pre-reserving the amount we are going to add will always
338 * be the right way, because otherwise if we have enough parallelism we
339 * could end up with thousands of inodes all holding little bits of
340 * reservations they were able to make previously and the only way to
341 * reclaim that space is to ENOSPC out the operations and clear
342 * everything out and try again, which is bad. This way we just
343 * over-reserve slightly, and clean up the mess when we are done.
344 */
Filipe Mananafeefe1f2024-01-31 17:18:04 +0000345 calc_inode_reservations(inode, num_bytes, disk_num_bytes,
Omar Sandoval28c9b1e2019-11-18 22:45:55 -0800346 &meta_reserve, &qgroup_reserve);
Filipe Mananad4135132022-03-23 16:19:30 +0000347 ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserve, true,
348 noflush);
Josef Bacik86736342019-06-19 15:12:00 -0400349 if (ret)
Filipe Manana16ad3be2019-10-25 10:52:42 +0100350 return ret;
Filipe Manana03551d62023-09-08 18:20:20 +0100351 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info,
352 meta_reserve, flush);
Filipe Manana16ad3be2019-10-25 10:52:42 +0100353 if (ret) {
354 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
355 return ret;
356 }
Josef Bacik86736342019-06-19 15:12:00 -0400357
358 /*
359 * Now we need to update our outstanding extents and csum bytes _first_
360 * and then add the reservation to the block_rsv. This keeps us from
361 * racing with an ordered completion or some such that would think it
362 * needs to free the reservation we just made.
363 */
Naohiro Aota7d7672b2022-07-09 08:18:41 +0900364 nr_extents = count_max_extents(fs_info, num_bytes);
Filipe Manana9aa06c72023-03-21 11:13:46 +0000365 spin_lock(&inode->lock);
Josef Bacik86736342019-06-19 15:12:00 -0400366 btrfs_mod_outstanding_extents(inode, nr_extents);
Filipe Mananafeefe1f2024-01-31 17:18:04 +0000367 if (!(inode->flags & BTRFS_INODE_NODATASUM))
368 inode->csum_bytes += disk_num_bytes;
Josef Bacik86736342019-06-19 15:12:00 -0400369 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
370 spin_unlock(&inode->lock);
371
372 /* Now we can safely add our space to our block rsv */
373 btrfs_block_rsv_add_bytes(block_rsv, meta_reserve, false);
374 trace_btrfs_space_reservation(root->fs_info, "delalloc",
375 btrfs_ino(inode), meta_reserve, 1);
376
377 spin_lock(&block_rsv->lock);
378 block_rsv->qgroup_rsv_reserved += qgroup_reserve;
379 spin_unlock(&block_rsv->lock);
380
Josef Bacik86736342019-06-19 15:12:00 -0400381 return 0;
Josef Bacik86736342019-06-19 15:12:00 -0400382}
383
David Sterba43dd5292022-10-27 14:21:42 +0200384/*
385 * Release a metadata reservation for an inode.
Nikolay Borisovb762d1d2021-01-22 11:58:01 +0200386 *
David Sterba43dd5292022-10-27 14:21:42 +0200387 * @inode: the inode to release the reservation for.
388 * @num_bytes: the number of bytes we are releasing.
389 * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
Josef Bacik86736342019-06-19 15:12:00 -0400390 *
391 * This will release the metadata reservation for an inode. This can be called
392 * once we complete IO for a given set of bytes to release their metadata
393 * reservations, or on error for the same reason.
394 */
395void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
396 bool qgroup_free)
397{
398 struct btrfs_fs_info *fs_info = inode->root->fs_info;
399
400 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
401 spin_lock(&inode->lock);
Filipe Mananafeefe1f2024-01-31 17:18:04 +0000402 if (!(inode->flags & BTRFS_INODE_NODATASUM))
403 inode->csum_bytes -= num_bytes;
Josef Bacik86736342019-06-19 15:12:00 -0400404 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
405 spin_unlock(&inode->lock);
406
407 if (btrfs_is_testing(fs_info))
408 return;
409
410 btrfs_inode_rsv_release(inode, qgroup_free);
411}
412
David Sterba43dd5292022-10-27 14:21:42 +0200413/*
414 * Release our outstanding_extents for an inode.
415 *
416 * @inode: the inode to balance the reservation for.
417 * @num_bytes: the number of bytes we originally reserved with
Josef Bacik86736342019-06-19 15:12:00 -0400418 *
419 * When we reserve space we increase outstanding_extents for the extents we may
420 * add. Once we've set the range as delalloc or created our ordered extents we
421 * have outstanding_extents to track the real usage, so we use this to free our
422 * temporarily tracked outstanding_extents. This _must_ be used in conjunction
423 * with btrfs_delalloc_reserve_metadata.
424 */
Qu Wenruo8702ba92019-10-14 14:34:51 +0800425void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes)
Josef Bacik86736342019-06-19 15:12:00 -0400426{
427 struct btrfs_fs_info *fs_info = inode->root->fs_info;
428 unsigned num_extents;
429
430 spin_lock(&inode->lock);
Naohiro Aota7d7672b2022-07-09 08:18:41 +0900431 num_extents = count_max_extents(fs_info, num_bytes);
Josef Bacik86736342019-06-19 15:12:00 -0400432 btrfs_mod_outstanding_extents(inode, -num_extents);
433 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
434 spin_unlock(&inode->lock);
435
436 if (btrfs_is_testing(fs_info))
437 return;
438
Qu Wenruo8702ba92019-10-14 14:34:51 +0800439 btrfs_inode_rsv_release(inode, true);
Josef Bacik86736342019-06-19 15:12:00 -0400440}
441
David Sterba43dd5292022-10-27 14:21:42 +0200442/*
443 * Reserve data and metadata space for delalloc
444 *
445 * @inode: inode we're writing to
446 * @start: start range we are writing to
447 * @len: how long the range we are writing to
448 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
449 * current reservation.
Josef Bacik86736342019-06-19 15:12:00 -0400450 *
451 * This will do the following things
452 *
David Sterba43dd5292022-10-27 14:21:42 +0200453 * - reserve space in data space info for num bytes and reserve precious
454 * corresponding qgroup space
Josef Bacik86736342019-06-19 15:12:00 -0400455 * (Done in check_data_free_space)
456 *
457 * - reserve space for metadata space, based on the number of outstanding
David Sterba43dd5292022-10-27 14:21:42 +0200458 * extents and how much csums will be needed also reserve metadata space in a
459 * per root over-reserve method.
Josef Bacik86736342019-06-19 15:12:00 -0400460 * - add to the inodes->delalloc_bytes
461 * - add it to the fs_info's delalloc inodes list.
462 * (Above 3 all done in delalloc_reserve_metadata)
463 *
464 * Return 0 for success
David Sterba43dd5292022-10-27 14:21:42 +0200465 * Return <0 for error(-ENOSPC or -EDQUOT)
Josef Bacik86736342019-06-19 15:12:00 -0400466 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +0300467int btrfs_delalloc_reserve_space(struct btrfs_inode *inode,
Josef Bacik86736342019-06-19 15:12:00 -0400468 struct extent_changeset **reserved, u64 start, u64 len)
469{
470 int ret;
471
Josef Bacik1daedb12022-09-12 12:27:44 -0700472 ret = btrfs_check_data_free_space(inode, reserved, start, len, false);
Josef Bacik86736342019-06-19 15:12:00 -0400473 if (ret < 0)
474 return ret;
Filipe Mananad4135132022-03-23 16:19:30 +0000475 ret = btrfs_delalloc_reserve_metadata(inode, len, len, false);
Johannes Thumshirnda5e8172021-12-03 02:55:33 -0800476 if (ret < 0) {
Nikolay Borisove5b7231e2020-06-03 08:55:42 +0300477 btrfs_free_reserved_data_space(inode, *reserved, start, len);
Johannes Thumshirnda5e8172021-12-03 02:55:33 -0800478 extent_changeset_free(*reserved);
479 *reserved = NULL;
480 }
Josef Bacik86736342019-06-19 15:12:00 -0400481 return ret;
482}
483
David Sterba43dd5292022-10-27 14:21:42 +0200484/*
Nikolay Borisovb762d1d2021-01-22 11:58:01 +0200485 * Release data and metadata space for delalloc
486 *
487 * @inode: inode we're releasing space for
488 * @reserved: list of changed/reserved ranges
489 * @start: start position of the space already reserved
490 * @len: length of the space already reserved
491 * @qgroup_free: should qgroup reserved-space also be freed
Josef Bacik86736342019-06-19 15:12:00 -0400492 *
David Sterba43dd5292022-10-27 14:21:42 +0200493 * Release the metadata space that was not used and will decrement
494 * ->delalloc_bytes and remove it from the fs_info->delalloc_inodes list if
495 * there are no delalloc bytes left. Also it will handle the qgroup reserved
496 * space.
Josef Bacik86736342019-06-19 15:12:00 -0400497 */
Nikolay Borisov86d52922020-06-03 08:55:40 +0300498void btrfs_delalloc_release_space(struct btrfs_inode *inode,
Josef Bacik86736342019-06-19 15:12:00 -0400499 struct extent_changeset *reserved,
500 u64 start, u64 len, bool qgroup_free)
501{
Nikolay Borisov86d52922020-06-03 08:55:40 +0300502 btrfs_delalloc_release_metadata(inode, len, qgroup_free);
503 btrfs_free_reserved_data_space(inode, reserved, start, len);
Josef Bacik86736342019-06-19 15:12:00 -0400504}