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