Loading...
1// SPDX-License-Identifier: GPL-2.0
2
3#include "messages.h"
4#include "ctree.h"
5#include "delalloc-space.h"
6#include "block-rsv.h"
7#include "btrfs_inode.h"
8#include "space-info.h"
9#include "transaction.h"
10#include "qgroup.h"
11#include "block-group.h"
12#include "fs.h"
13
14/*
15 * HOW DOES THIS WORK
16 *
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.
19 *
20 *
21 * DATA RESERVATION
22 * The general flow of the data reservation is as follows
23 *
24 * -> Reserve
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.
30 *
31 * -> Use
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.
38 *
39 * -> Allocation
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.
44 *
45 * Error handling
46 *
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.
52 *
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.
57 *
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.
61 *
62 * We keep track of two things on a per inode bases
63 *
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.
70 *
71 * ->csum_bytes
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.
75 *
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
81 * space.
82 *
83 * OUTSTANDING_EXTENTS HANDLING
84 *
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
89 *
90 * -> reserve
91 * ->outstanding_extents += 1 (current value is 1)
92 *
93 * -> set_delalloc
94 * ->outstanding_extents += 1 (current value is 2)
95 *
96 * -> btrfs_delalloc_release_extents()
97 * ->outstanding_extents -= 1 (current value is 1)
98 *
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.
102 *
103 * -> add ordered extent
104 * ->outstanding_extents += 1 (current value is 2)
105 *
106 * -> btrfs_clear_delalloc_extent
107 * ->outstanding_extents -= 1 (current value is 1)
108 *
109 * -> finish_ordered_io/btrfs_remove_ordered_extent
110 * ->outstanding_extents -= 1 (current value is 0)
111 *
112 * Each stage is responsible for their own accounting of the extent, thus
113 * making error handling and cleanup easier.
114 */
115
116int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
117{
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;
121
122 /* Make sure bytes are sectorsize aligned */
123 bytes = ALIGN(bytes, fs_info->sectorsize);
124
125 if (btrfs_is_free_space_inode(inode))
126 flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
127
128 return btrfs_reserve_data_bytes(fs_info, bytes, flush);
129}
130
131int btrfs_check_data_free_space(struct btrfs_inode *inode,
132 struct extent_changeset **reserved, u64 start,
133 u64 len, bool noflush)
134{
135 struct btrfs_fs_info *fs_info = inode->root->fs_info;
136 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
137 int ret;
138
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
144 if (noflush)
145 flush = BTRFS_RESERVE_NO_FLUSH;
146 else if (btrfs_is_free_space_inode(inode))
147 flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
148
149 ret = btrfs_reserve_data_bytes(fs_info, len, flush);
150 if (ret < 0)
151 return ret;
152
153 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
154 ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
155 if (ret < 0) {
156 btrfs_free_reserved_data_space_noquota(fs_info, len);
157 extent_changeset_free(*reserved);
158 *reserved = NULL;
159 } else {
160 ret = 0;
161 }
162 return ret;
163}
164
165/*
166 * Called if we need to clear a data reservation for this inode
167 * Normally in a error case.
168 *
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().
172 */
173void btrfs_free_reserved_data_space_noquota(struct btrfs_fs_info *fs_info,
174 u64 len)
175{
176 struct btrfs_space_info *data_sinfo;
177
178 ASSERT(IS_ALIGNED(len, fs_info->sectorsize));
179
180 data_sinfo = fs_info->data_sinfo;
181 btrfs_space_info_free_bytes_may_use(fs_info, data_sinfo, len);
182}
183
184/*
185 * Called if we need to clear a data reservation for this inode
186 * Normally in a error case.
187 *
188 * This one will handle the per-inode data rsv map for accurate reserved
189 * space framework.
190 */
191void btrfs_free_reserved_data_space(struct btrfs_inode *inode,
192 struct extent_changeset *reserved, u64 start, u64 len)
193{
194 struct btrfs_fs_info *fs_info = inode->root->fs_info;
195
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);
200
201 btrfs_free_reserved_data_space_noquota(fs_info, len);
202 btrfs_qgroup_free_data(inode, reserved, start, len);
203}
204
205/*
206 * Release any excessive reservations for an inode.
207 *
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
213 * release.
214 *
215 * This is the same as btrfs_block_rsv_release, except that it handles the
216 * tracepoint for the reservation.
217 */
218static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
219{
220 struct btrfs_fs_info *fs_info = inode->root->fs_info;
221 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
222 u64 released = 0;
223 u64 qgroup_to_release = 0;
224
225 /*
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
228 * the size free'd.
229 */
230 released = btrfs_block_rsv_release(fs_info, block_rsv, 0,
231 &qgroup_to_release);
232 if (released > 0)
233 trace_btrfs_space_reservation(fs_info, "delalloc",
234 btrfs_ino(inode), released, 0);
235 if (qgroup_free)
236 btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
237 else
238 btrfs_qgroup_convert_reserved_meta(inode->root,
239 qgroup_to_release);
240}
241
242static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
243 struct btrfs_inode *inode)
244{
245 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
246 u64 reserve_size = 0;
247 u64 qgroup_rsv_size = 0;
248 u64 csum_leaves;
249 unsigned outstanding_extents;
250
251 lockdep_assert_held(&inode->lock);
252 outstanding_extents = inode->outstanding_extents;
253
254 /*
255 * Insert size for the number of outstanding extents, 1 normal size for
256 * updating the inode.
257 */
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);
262 }
263 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
264 inode->csum_bytes);
265 reserve_size += btrfs_calc_insert_metadata_size(fs_info,
266 csum_leaves);
267 /*
268 * For qgroup rsv, the calculation is very simple:
269 * account one nodesize for each outstanding extent
270 *
271 * This is overestimating in most cases.
272 */
273 qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
274
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);
279}
280
281static 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)
284{
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);
288
289 *meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
290 nr_extents + csum_leaves);
291
292 /*
293 * finish_ordered_io has to update the inode, so add the space required
294 * for an inode update.
295 */
296 *meta_reserve += inode_update;
297 *qgroup_reserve = nr_extents * fs_info->nodesize;
298}
299
300int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
301 u64 disk_num_bytes, bool noflush)
302{
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;
307 unsigned nr_extents;
308 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
309 int ret = 0;
310
311 /*
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.
316 *
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.
319 */
320 if (noflush || btrfs_is_free_space_inode(inode)) {
321 flush = BTRFS_RESERVE_NO_FLUSH;
322 } else {
323 if (current->journal_info)
324 flush = BTRFS_RESERVE_FLUSH_LIMIT;
325
326 if (btrfs_transaction_in_commit(fs_info))
327 schedule_timeout(1);
328 }
329
330 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
331 disk_num_bytes = ALIGN(disk_num_bytes, fs_info->sectorsize);
332
333 /*
334 * We always want to do it this way, every other way is wrong and ends
335 * in tears. Pre-reserving the amount we are going to add will always
336 * be the right way, because otherwise if we have enough parallelism we
337 * could end up with thousands of inodes all holding little bits of
338 * reservations they were able to make previously and the only way to
339 * reclaim that space is to ENOSPC out the operations and clear
340 * everything out and try again, which is bad. This way we just
341 * over-reserve slightly, and clean up the mess when we are done.
342 */
343 calc_inode_reservations(fs_info, num_bytes, disk_num_bytes,
344 &meta_reserve, &qgroup_reserve);
345 ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserve, true,
346 noflush);
347 if (ret)
348 return ret;
349 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, meta_reserve, flush);
350 if (ret) {
351 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
352 return ret;
353 }
354
355 /*
356 * Now we need to update our outstanding extents and csum bytes _first_
357 * and then add the reservation to the block_rsv. This keeps us from
358 * racing with an ordered completion or some such that would think it
359 * needs to free the reservation we just made.
360 */
361 spin_lock(&inode->lock);
362 nr_extents = count_max_extents(fs_info, num_bytes);
363 btrfs_mod_outstanding_extents(inode, nr_extents);
364 inode->csum_bytes += disk_num_bytes;
365 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
366 spin_unlock(&inode->lock);
367
368 /* Now we can safely add our space to our block rsv */
369 btrfs_block_rsv_add_bytes(block_rsv, meta_reserve, false);
370 trace_btrfs_space_reservation(root->fs_info, "delalloc",
371 btrfs_ino(inode), meta_reserve, 1);
372
373 spin_lock(&block_rsv->lock);
374 block_rsv->qgroup_rsv_reserved += qgroup_reserve;
375 spin_unlock(&block_rsv->lock);
376
377 return 0;
378}
379
380/*
381 * Release a metadata reservation for an inode.
382 *
383 * @inode: the inode to release the reservation for.
384 * @num_bytes: the number of bytes we are releasing.
385 * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
386 *
387 * This will release the metadata reservation for an inode. This can be called
388 * once we complete IO for a given set of bytes to release their metadata
389 * reservations, or on error for the same reason.
390 */
391void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
392 bool qgroup_free)
393{
394 struct btrfs_fs_info *fs_info = inode->root->fs_info;
395
396 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
397 spin_lock(&inode->lock);
398 inode->csum_bytes -= num_bytes;
399 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
400 spin_unlock(&inode->lock);
401
402 if (btrfs_is_testing(fs_info))
403 return;
404
405 btrfs_inode_rsv_release(inode, qgroup_free);
406}
407
408/*
409 * Release our outstanding_extents for an inode.
410 *
411 * @inode: the inode to balance the reservation for.
412 * @num_bytes: the number of bytes we originally reserved with
413 *
414 * When we reserve space we increase outstanding_extents for the extents we may
415 * add. Once we've set the range as delalloc or created our ordered extents we
416 * have outstanding_extents to track the real usage, so we use this to free our
417 * temporarily tracked outstanding_extents. This _must_ be used in conjunction
418 * with btrfs_delalloc_reserve_metadata.
419 */
420void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes)
421{
422 struct btrfs_fs_info *fs_info = inode->root->fs_info;
423 unsigned num_extents;
424
425 spin_lock(&inode->lock);
426 num_extents = count_max_extents(fs_info, num_bytes);
427 btrfs_mod_outstanding_extents(inode, -num_extents);
428 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
429 spin_unlock(&inode->lock);
430
431 if (btrfs_is_testing(fs_info))
432 return;
433
434 btrfs_inode_rsv_release(inode, true);
435}
436
437/*
438 * Reserve data and metadata space for delalloc
439 *
440 * @inode: inode we're writing to
441 * @start: start range we are writing to
442 * @len: how long the range we are writing to
443 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
444 * current reservation.
445 *
446 * This will do the following things
447 *
448 * - reserve space in data space info for num bytes and reserve precious
449 * corresponding qgroup space
450 * (Done in check_data_free_space)
451 *
452 * - reserve space for metadata space, based on the number of outstanding
453 * extents and how much csums will be needed also reserve metadata space in a
454 * per root over-reserve method.
455 * - add to the inodes->delalloc_bytes
456 * - add it to the fs_info's delalloc inodes list.
457 * (Above 3 all done in delalloc_reserve_metadata)
458 *
459 * Return 0 for success
460 * Return <0 for error(-ENOSPC or -EDQUOT)
461 */
462int btrfs_delalloc_reserve_space(struct btrfs_inode *inode,
463 struct extent_changeset **reserved, u64 start, u64 len)
464{
465 int ret;
466
467 ret = btrfs_check_data_free_space(inode, reserved, start, len, false);
468 if (ret < 0)
469 return ret;
470 ret = btrfs_delalloc_reserve_metadata(inode, len, len, false);
471 if (ret < 0) {
472 btrfs_free_reserved_data_space(inode, *reserved, start, len);
473 extent_changeset_free(*reserved);
474 *reserved = NULL;
475 }
476 return ret;
477}
478
479/*
480 * Release data and metadata space for delalloc
481 *
482 * @inode: inode we're releasing space for
483 * @reserved: list of changed/reserved ranges
484 * @start: start position of the space already reserved
485 * @len: length of the space already reserved
486 * @qgroup_free: should qgroup reserved-space also be freed
487 *
488 * Release the metadata space that was not used and will decrement
489 * ->delalloc_bytes and remove it from the fs_info->delalloc_inodes list if
490 * there are no delalloc bytes left. Also it will handle the qgroup reserved
491 * space.
492 */
493void btrfs_delalloc_release_space(struct btrfs_inode *inode,
494 struct extent_changeset *reserved,
495 u64 start, u64 len, bool qgroup_free)
496{
497 btrfs_delalloc_release_metadata(inode, len, qgroup_free);
498 btrfs_free_reserved_data_space(inode, reserved, start, len);
499}
1// SPDX-License-Identifier: GPL-2.0
2
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 "transaction.h"
9#include "qgroup.h"
10#include "block-group.h"
11
12/*
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
92 * ->outstanding_extents += 1 (current value is 2)
93 *
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
114int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
115{
116 struct btrfs_root *root = inode->root;
117 struct btrfs_fs_info *fs_info = root->fs_info;
118 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
119
120 /* Make sure bytes are sectorsize aligned */
121 bytes = ALIGN(bytes, fs_info->sectorsize);
122
123 if (btrfs_is_free_space_inode(inode))
124 flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
125
126 return btrfs_reserve_data_bytes(fs_info, bytes, flush);
127}
128
129int btrfs_check_data_free_space(struct btrfs_inode *inode,
130 struct extent_changeset **reserved, u64 start, u64 len)
131{
132 struct btrfs_fs_info *fs_info = inode->root->fs_info;
133 int ret;
134
135 /* align the range */
136 len = round_up(start + len, fs_info->sectorsize) -
137 round_down(start, fs_info->sectorsize);
138 start = round_down(start, fs_info->sectorsize);
139
140 ret = btrfs_alloc_data_chunk_ondemand(inode, len);
141 if (ret < 0)
142 return ret;
143
144 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
145 ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
146 if (ret < 0)
147 btrfs_free_reserved_data_space_noquota(fs_info, len);
148 else
149 ret = 0;
150 return ret;
151}
152
153/*
154 * Called if we need to clear a data reservation for this inode
155 * Normally in a error case.
156 *
157 * This one will *NOT* use accurate qgroup reserved space API, just for case
158 * which we can't sleep and is sure it won't affect qgroup reserved space.
159 * Like clear_bit_hook().
160 */
161void btrfs_free_reserved_data_space_noquota(struct btrfs_fs_info *fs_info,
162 u64 len)
163{
164 struct btrfs_space_info *data_sinfo;
165
166 ASSERT(IS_ALIGNED(len, fs_info->sectorsize));
167
168 data_sinfo = fs_info->data_sinfo;
169 btrfs_space_info_free_bytes_may_use(fs_info, data_sinfo, len);
170}
171
172/*
173 * Called if we need to clear a data reservation for this inode
174 * Normally in a error case.
175 *
176 * This one will handle the per-inode data rsv map for accurate reserved
177 * space framework.
178 */
179void btrfs_free_reserved_data_space(struct btrfs_inode *inode,
180 struct extent_changeset *reserved, u64 start, u64 len)
181{
182 struct btrfs_fs_info *fs_info = inode->root->fs_info;
183
184 /* Make sure the range is aligned to sectorsize */
185 len = round_up(start + len, fs_info->sectorsize) -
186 round_down(start, fs_info->sectorsize);
187 start = round_down(start, fs_info->sectorsize);
188
189 btrfs_free_reserved_data_space_noquota(fs_info, len);
190 btrfs_qgroup_free_data(inode, reserved, start, len);
191}
192
193/**
194 * Release any excessive reservation
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 */
206static 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 /*
214 * Since we statically set the block_rsv->size we just want to say we
215 * are releasing 0 bytes, and then we'll just get the reservation over
216 * the size free'd.
217 */
218 released = btrfs_block_rsv_release(fs_info, block_rsv, 0,
219 &qgroup_to_release);
220 if (released > 0)
221 trace_btrfs_space_reservation(fs_info, "delalloc",
222 btrfs_ino(inode), released, 0);
223 if (qgroup_free)
224 btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
225 else
226 btrfs_qgroup_convert_reserved_meta(inode->root,
227 qgroup_to_release);
228}
229
230static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
231 struct btrfs_inode *inode)
232{
233 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
234 u64 reserve_size = 0;
235 u64 qgroup_rsv_size = 0;
236 u64 csum_leaves;
237 unsigned outstanding_extents;
238
239 lockdep_assert_held(&inode->lock);
240 outstanding_extents = inode->outstanding_extents;
241
242 /*
243 * Insert size for the number of outstanding extents, 1 normal size for
244 * updating the inode.
245 */
246 if (outstanding_extents) {
247 reserve_size = btrfs_calc_insert_metadata_size(fs_info,
248 outstanding_extents);
249 reserve_size += btrfs_calc_metadata_size(fs_info, 1);
250 }
251 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
252 inode->csum_bytes);
253 reserve_size += btrfs_calc_insert_metadata_size(fs_info,
254 csum_leaves);
255 /*
256 * For qgroup rsv, the calculation is very simple:
257 * account one nodesize for each outstanding extent
258 *
259 * This is overestimating in most cases.
260 */
261 qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
262
263 spin_lock(&block_rsv->lock);
264 block_rsv->size = reserve_size;
265 block_rsv->qgroup_rsv_size = qgroup_rsv_size;
266 spin_unlock(&block_rsv->lock);
267}
268
269static void calc_inode_reservations(struct btrfs_fs_info *fs_info,
270 u64 num_bytes, u64 *meta_reserve,
271 u64 *qgroup_reserve)
272{
273 u64 nr_extents = count_max_extents(num_bytes);
274 u64 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, num_bytes);
275 u64 inode_update = btrfs_calc_metadata_size(fs_info, 1);
276
277 *meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
278 nr_extents + csum_leaves);
279
280 /*
281 * finish_ordered_io has to update the inode, so add the space required
282 * for an inode update.
283 */
284 *meta_reserve += inode_update;
285 *qgroup_reserve = nr_extents * fs_info->nodesize;
286}
287
288int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
289{
290 struct btrfs_root *root = inode->root;
291 struct btrfs_fs_info *fs_info = root->fs_info;
292 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
293 u64 meta_reserve, qgroup_reserve;
294 unsigned nr_extents;
295 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
296 int ret = 0;
297
298 /*
299 * If we are a free space inode we need to not flush since we will be in
300 * the middle of a transaction commit. We also don't need the delalloc
301 * mutex since we won't race with anybody. We need this mostly to make
302 * lockdep shut its filthy mouth.
303 *
304 * If we have a transaction open (can happen if we call truncate_block
305 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
306 */
307 if (btrfs_is_free_space_inode(inode)) {
308 flush = BTRFS_RESERVE_NO_FLUSH;
309 } else {
310 if (current->journal_info)
311 flush = BTRFS_RESERVE_FLUSH_LIMIT;
312
313 if (btrfs_transaction_in_commit(fs_info))
314 schedule_timeout(1);
315 }
316
317 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
318
319 /*
320 * We always want to do it this way, every other way is wrong and ends
321 * in tears. Pre-reserving the amount we are going to add will always
322 * be the right way, because otherwise if we have enough parallelism we
323 * could end up with thousands of inodes all holding little bits of
324 * reservations they were able to make previously and the only way to
325 * reclaim that space is to ENOSPC out the operations and clear
326 * everything out and try again, which is bad. This way we just
327 * over-reserve slightly, and clean up the mess when we are done.
328 */
329 calc_inode_reservations(fs_info, num_bytes, &meta_reserve,
330 &qgroup_reserve);
331 ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserve, true);
332 if (ret)
333 return ret;
334 ret = btrfs_reserve_metadata_bytes(root, block_rsv, meta_reserve, flush);
335 if (ret) {
336 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
337 return ret;
338 }
339
340 /*
341 * Now we need to update our outstanding extents and csum bytes _first_
342 * and then add the reservation to the block_rsv. This keeps us from
343 * racing with an ordered completion or some such that would think it
344 * needs to free the reservation we just made.
345 */
346 spin_lock(&inode->lock);
347 nr_extents = count_max_extents(num_bytes);
348 btrfs_mod_outstanding_extents(inode, nr_extents);
349 inode->csum_bytes += num_bytes;
350 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
351 spin_unlock(&inode->lock);
352
353 /* Now we can safely add our space to our block rsv */
354 btrfs_block_rsv_add_bytes(block_rsv, meta_reserve, false);
355 trace_btrfs_space_reservation(root->fs_info, "delalloc",
356 btrfs_ino(inode), meta_reserve, 1);
357
358 spin_lock(&block_rsv->lock);
359 block_rsv->qgroup_rsv_reserved += qgroup_reserve;
360 spin_unlock(&block_rsv->lock);
361
362 return 0;
363}
364
365/**
366 * Release a metadata reservation for an inode
367 *
368 * @inode: the inode to release the reservation for.
369 * @num_bytes: the number of bytes we are releasing.
370 * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
371 *
372 * This will release the metadata reservation for an inode. This can be called
373 * once we complete IO for a given set of bytes to release their metadata
374 * reservations, or on error for the same reason.
375 */
376void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
377 bool qgroup_free)
378{
379 struct btrfs_fs_info *fs_info = inode->root->fs_info;
380
381 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
382 spin_lock(&inode->lock);
383 inode->csum_bytes -= num_bytes;
384 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
385 spin_unlock(&inode->lock);
386
387 if (btrfs_is_testing(fs_info))
388 return;
389
390 btrfs_inode_rsv_release(inode, qgroup_free);
391}
392
393/**
394 * btrfs_delalloc_release_extents - release our outstanding_extents
395 * @inode: the inode to balance the reservation for.
396 * @num_bytes: the number of bytes we originally reserved with
397 *
398 * When we reserve space we increase outstanding_extents for the extents we may
399 * add. Once we've set the range as delalloc or created our ordered extents we
400 * have outstanding_extents to track the real usage, so we use this to free our
401 * temporarily tracked outstanding_extents. This _must_ be used in conjunction
402 * with btrfs_delalloc_reserve_metadata.
403 */
404void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes)
405{
406 struct btrfs_fs_info *fs_info = inode->root->fs_info;
407 unsigned num_extents;
408
409 spin_lock(&inode->lock);
410 num_extents = count_max_extents(num_bytes);
411 btrfs_mod_outstanding_extents(inode, -num_extents);
412 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
413 spin_unlock(&inode->lock);
414
415 if (btrfs_is_testing(fs_info))
416 return;
417
418 btrfs_inode_rsv_release(inode, true);
419}
420
421/**
422 * btrfs_delalloc_reserve_space - reserve data and metadata space for
423 * delalloc
424 * @inode: inode we're writing to
425 * @start: start range we are writing to
426 * @len: how long the range we are writing to
427 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
428 * current reservation.
429 *
430 * This will do the following things
431 *
432 * - reserve space in data space info for num bytes
433 * and reserve precious corresponding qgroup space
434 * (Done in check_data_free_space)
435 *
436 * - reserve space for metadata space, based on the number of outstanding
437 * extents and how much csums will be needed
438 * also reserve metadata space in a per root over-reserve method.
439 * - add to the inodes->delalloc_bytes
440 * - add it to the fs_info's delalloc inodes list.
441 * (Above 3 all done in delalloc_reserve_metadata)
442 *
443 * Return 0 for success
444 * Return <0 for error(-ENOSPC or -EQUOT)
445 */
446int btrfs_delalloc_reserve_space(struct btrfs_inode *inode,
447 struct extent_changeset **reserved, u64 start, u64 len)
448{
449 int ret;
450
451 ret = btrfs_check_data_free_space(inode, reserved, start, len);
452 if (ret < 0)
453 return ret;
454 ret = btrfs_delalloc_reserve_metadata(inode, len);
455 if (ret < 0)
456 btrfs_free_reserved_data_space(inode, *reserved, start, len);
457 return ret;
458}
459
460/**
461 * Release data and metadata space for delalloc
462 *
463 * @inode: inode we're releasing space for
464 * @reserved: list of changed/reserved ranges
465 * @start: start position of the space already reserved
466 * @len: length of the space already reserved
467 * @qgroup_free: should qgroup reserved-space also be freed
468 *
469 * This function will release the metadata space that was not used and will
470 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
471 * list if there are no delalloc bytes left.
472 * Also it will handle the qgroup reserved space.
473 */
474void btrfs_delalloc_release_space(struct btrfs_inode *inode,
475 struct extent_changeset *reserved,
476 u64 start, u64 len, bool qgroup_free)
477{
478 btrfs_delalloc_release_metadata(inode, len, qgroup_free);
479 btrfs_free_reserved_data_space(inode, reserved, start, len);
480}