xref: /openbmc/linux/fs/btrfs/delalloc-space.c (revision ec2da07c)
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 
11 int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
12 {
13 	struct btrfs_root *root = inode->root;
14 	struct btrfs_fs_info *fs_info = root->fs_info;
15 	struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
16 	u64 used;
17 	int ret = 0;
18 	int need_commit = 2;
19 	int have_pinned_space;
20 
21 	/* Make sure bytes are sectorsize aligned */
22 	bytes = ALIGN(bytes, fs_info->sectorsize);
23 
24 	if (btrfs_is_free_space_inode(inode)) {
25 		need_commit = 0;
26 		ASSERT(current->journal_info);
27 	}
28 
29 again:
30 	/* Make sure we have enough space to handle the data first */
31 	spin_lock(&data_sinfo->lock);
32 	used = btrfs_space_info_used(data_sinfo, true);
33 
34 	if (used + bytes > data_sinfo->total_bytes) {
35 		struct btrfs_trans_handle *trans;
36 
37 		/*
38 		 * If we don't have enough free bytes in this space then we need
39 		 * to alloc a new chunk.
40 		 */
41 		if (!data_sinfo->full) {
42 			u64 alloc_target;
43 
44 			data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
45 			spin_unlock(&data_sinfo->lock);
46 
47 			alloc_target = btrfs_data_alloc_profile(fs_info);
48 			/*
49 			 * It is ugly that we don't call nolock join
50 			 * transaction for the free space inode case here.
51 			 * But it is safe because we only do the data space
52 			 * reservation for the free space cache in the
53 			 * transaction context, the common join transaction
54 			 * just increase the counter of the current transaction
55 			 * handler, doesn't try to acquire the trans_lock of
56 			 * the fs.
57 			 */
58 			trans = btrfs_join_transaction(root);
59 			if (IS_ERR(trans))
60 				return PTR_ERR(trans);
61 
62 			ret = btrfs_chunk_alloc(trans, alloc_target,
63 						CHUNK_ALLOC_NO_FORCE);
64 			btrfs_end_transaction(trans);
65 			if (ret < 0) {
66 				if (ret != -ENOSPC)
67 					return ret;
68 				else {
69 					have_pinned_space = 1;
70 					goto commit_trans;
71 				}
72 			}
73 
74 			goto again;
75 		}
76 
77 		/*
78 		 * If we don't have enough pinned space to deal with this
79 		 * allocation, and no removed chunk in current transaction,
80 		 * don't bother committing the transaction.
81 		 */
82 		have_pinned_space = __percpu_counter_compare(
83 			&data_sinfo->total_bytes_pinned,
84 			used + bytes - data_sinfo->total_bytes,
85 			BTRFS_TOTAL_BYTES_PINNED_BATCH);
86 		spin_unlock(&data_sinfo->lock);
87 
88 		/* Commit the current transaction and try again */
89 commit_trans:
90 		if (need_commit) {
91 			need_commit--;
92 
93 			if (need_commit > 0) {
94 				btrfs_start_delalloc_roots(fs_info, -1);
95 				btrfs_wait_ordered_roots(fs_info, U64_MAX, 0,
96 							 (u64)-1);
97 			}
98 
99 			trans = btrfs_join_transaction(root);
100 			if (IS_ERR(trans))
101 				return PTR_ERR(trans);
102 			if (have_pinned_space >= 0 ||
103 			    test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
104 				     &trans->transaction->flags) ||
105 			    need_commit > 0) {
106 				ret = btrfs_commit_transaction(trans);
107 				if (ret)
108 					return ret;
109 				/*
110 				 * The cleaner kthread might still be doing iput
111 				 * operations. Wait for it to finish so that
112 				 * more space is released.  We don't need to
113 				 * explicitly run the delayed iputs here because
114 				 * the commit_transaction would have woken up
115 				 * the cleaner.
116 				 */
117 				ret = btrfs_wait_on_delayed_iputs(fs_info);
118 				if (ret)
119 					return ret;
120 				goto again;
121 			} else {
122 				btrfs_end_transaction(trans);
123 			}
124 		}
125 
126 		trace_btrfs_space_reservation(fs_info,
127 					      "space_info:enospc",
128 					      data_sinfo->flags, bytes, 1);
129 		return -ENOSPC;
130 	}
131 	btrfs_space_info_update_bytes_may_use(fs_info, data_sinfo, bytes);
132 	trace_btrfs_space_reservation(fs_info, "space_info",
133 				      data_sinfo->flags, bytes, 1);
134 	spin_unlock(&data_sinfo->lock);
135 
136 	return 0;
137 }
138 
139 int btrfs_check_data_free_space(struct inode *inode,
140 			struct extent_changeset **reserved, u64 start, u64 len)
141 {
142 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
143 	int ret;
144 
145 	/* align the range */
146 	len = round_up(start + len, fs_info->sectorsize) -
147 	      round_down(start, fs_info->sectorsize);
148 	start = round_down(start, fs_info->sectorsize);
149 
150 	ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len);
151 	if (ret < 0)
152 		return ret;
153 
154 	/* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
155 	ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
156 	if (ret < 0)
157 		btrfs_free_reserved_data_space_noquota(inode, start, len);
158 	else
159 		ret = 0;
160 	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  */
171 void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
172 					    u64 len)
173 {
174 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
175 	struct btrfs_space_info *data_sinfo;
176 
177 	/* Make sure the range is aligned to sectorsize */
178 	len = round_up(start + len, fs_info->sectorsize) -
179 	      round_down(start, fs_info->sectorsize);
180 	start = round_down(start, fs_info->sectorsize);
181 
182 	data_sinfo = fs_info->data_sinfo;
183 	spin_lock(&data_sinfo->lock);
184 	btrfs_space_info_update_bytes_may_use(fs_info, data_sinfo, -len);
185 	trace_btrfs_space_reservation(fs_info, "space_info",
186 				      data_sinfo->flags, len, 0);
187 	spin_unlock(&data_sinfo->lock);
188 }
189 
190 /*
191  * Called if we need to clear a data reservation for this inode
192  * Normally in a error case.
193  *
194  * This one will handle the per-inode data rsv map for accurate reserved
195  * space framework.
196  */
197 void btrfs_free_reserved_data_space(struct inode *inode,
198 			struct extent_changeset *reserved, u64 start, u64 len)
199 {
200 	struct btrfs_root *root = BTRFS_I(inode)->root;
201 
202 	/* Make sure the range is aligned to sectorsize */
203 	len = round_up(start + len, root->fs_info->sectorsize) -
204 	      round_down(start, root->fs_info->sectorsize);
205 	start = round_down(start, root->fs_info->sectorsize);
206 
207 	btrfs_free_reserved_data_space_noquota(inode, start, len);
208 	btrfs_qgroup_free_data(inode, reserved, start, len);
209 }
210 
211 /**
212  * btrfs_inode_rsv_release - release any excessive reservation.
213  * @inode - the inode we need to release from.
214  * @qgroup_free - free or convert qgroup meta.
215  *   Unlike normal operation, qgroup meta reservation needs to know if we are
216  *   freeing qgroup reservation or just converting it into per-trans.  Normally
217  *   @qgroup_free is true for error handling, and false for normal release.
218  *
219  * This is the same as btrfs_block_rsv_release, except that it handles the
220  * tracepoint for the reservation.
221  */
222 static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
223 {
224 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
225 	struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
226 	u64 released = 0;
227 	u64 qgroup_to_release = 0;
228 
229 	/*
230 	 * Since we statically set the block_rsv->size we just want to say we
231 	 * are releasing 0 bytes, and then we'll just get the reservation over
232 	 * the size free'd.
233 	 */
234 	released = __btrfs_block_rsv_release(fs_info, block_rsv, 0,
235 					     &qgroup_to_release);
236 	if (released > 0)
237 		trace_btrfs_space_reservation(fs_info, "delalloc",
238 					      btrfs_ino(inode), released, 0);
239 	if (qgroup_free)
240 		btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
241 	else
242 		btrfs_qgroup_convert_reserved_meta(inode->root,
243 						   qgroup_to_release);
244 }
245 
246 static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
247 						 struct btrfs_inode *inode)
248 {
249 	struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
250 	u64 reserve_size = 0;
251 	u64 qgroup_rsv_size = 0;
252 	u64 csum_leaves;
253 	unsigned outstanding_extents;
254 
255 	lockdep_assert_held(&inode->lock);
256 	outstanding_extents = inode->outstanding_extents;
257 	if (outstanding_extents)
258 		reserve_size = btrfs_calc_trans_metadata_size(fs_info,
259 						outstanding_extents + 1);
260 	csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
261 						 inode->csum_bytes);
262 	reserve_size += btrfs_calc_trans_metadata_size(fs_info,
263 						       csum_leaves);
264 	/*
265 	 * For qgroup rsv, the calculation is very simple:
266 	 * account one nodesize for each outstanding extent
267 	 *
268 	 * This is overestimating in most cases.
269 	 */
270 	qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
271 
272 	spin_lock(&block_rsv->lock);
273 	block_rsv->size = reserve_size;
274 	block_rsv->qgroup_rsv_size = qgroup_rsv_size;
275 	spin_unlock(&block_rsv->lock);
276 }
277 
278 static void calc_inode_reservations(struct btrfs_fs_info *fs_info,
279 				    u64 num_bytes, u64 *meta_reserve,
280 				    u64 *qgroup_reserve)
281 {
282 	u64 nr_extents = count_max_extents(num_bytes);
283 	u64 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, num_bytes);
284 
285 	/* We add one for the inode update at finish ordered time */
286 	*meta_reserve = btrfs_calc_trans_metadata_size(fs_info,
287 						nr_extents + csum_leaves + 1);
288 	*qgroup_reserve = nr_extents * fs_info->nodesize;
289 }
290 
291 int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
292 {
293 	struct btrfs_root *root = inode->root;
294 	struct btrfs_fs_info *fs_info = root->fs_info;
295 	struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
296 	u64 meta_reserve, qgroup_reserve;
297 	unsigned nr_extents;
298 	enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
299 	int ret = 0;
300 	bool delalloc_lock = true;
301 
302 	/*
303 	 * If we are a free space inode we need to not flush since we will be in
304 	 * the middle of a transaction commit.  We also don't need the delalloc
305 	 * mutex since we won't race with anybody.  We need this mostly to make
306 	 * lockdep shut its filthy mouth.
307 	 *
308 	 * If we have a transaction open (can happen if we call truncate_block
309 	 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
310 	 */
311 	if (btrfs_is_free_space_inode(inode)) {
312 		flush = BTRFS_RESERVE_NO_FLUSH;
313 		delalloc_lock = false;
314 	} else {
315 		if (current->journal_info)
316 			flush = BTRFS_RESERVE_FLUSH_LIMIT;
317 
318 		if (btrfs_transaction_in_commit(fs_info))
319 			schedule_timeout(1);
320 	}
321 
322 	if (delalloc_lock)
323 		mutex_lock(&inode->delalloc_mutex);
324 
325 	num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
326 
327 	/*
328 	 * We always want to do it this way, every other way is wrong and ends
329 	 * in tears.  Pre-reserving the amount we are going to add will always
330 	 * be the right way, because otherwise if we have enough parallelism we
331 	 * could end up with thousands of inodes all holding little bits of
332 	 * reservations they were able to make previously and the only way to
333 	 * reclaim that space is to ENOSPC out the operations and clear
334 	 * everything out and try again, which is bad.  This way we just
335 	 * over-reserve slightly, and clean up the mess when we are done.
336 	 */
337 	calc_inode_reservations(fs_info, num_bytes, &meta_reserve,
338 				&qgroup_reserve);
339 	ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserve, true);
340 	if (ret)
341 		goto out_fail;
342 	ret = btrfs_reserve_metadata_bytes(root, block_rsv, meta_reserve, flush);
343 	if (ret)
344 		goto out_qgroup;
345 
346 	/*
347 	 * Now we need to update our outstanding extents and csum bytes _first_
348 	 * and then add the reservation to the block_rsv.  This keeps us from
349 	 * racing with an ordered completion or some such that would think it
350 	 * needs to free the reservation we just made.
351 	 */
352 	spin_lock(&inode->lock);
353 	nr_extents = count_max_extents(num_bytes);
354 	btrfs_mod_outstanding_extents(inode, nr_extents);
355 	inode->csum_bytes += num_bytes;
356 	btrfs_calculate_inode_block_rsv_size(fs_info, inode);
357 	spin_unlock(&inode->lock);
358 
359 	/* Now we can safely add our space to our block rsv */
360 	btrfs_block_rsv_add_bytes(block_rsv, meta_reserve, false);
361 	trace_btrfs_space_reservation(root->fs_info, "delalloc",
362 				      btrfs_ino(inode), meta_reserve, 1);
363 
364 	spin_lock(&block_rsv->lock);
365 	block_rsv->qgroup_rsv_reserved += qgroup_reserve;
366 	spin_unlock(&block_rsv->lock);
367 
368 	if (delalloc_lock)
369 		mutex_unlock(&inode->delalloc_mutex);
370 	return 0;
371 out_qgroup:
372 	btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
373 out_fail:
374 	btrfs_inode_rsv_release(inode, true);
375 	if (delalloc_lock)
376 		mutex_unlock(&inode->delalloc_mutex);
377 	return ret;
378 }
379 
380 /**
381  * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
382  * @inode: the inode to release the reservation for.
383  * @num_bytes: the number of bytes we are releasing.
384  * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
385  *
386  * This will release the metadata reservation for an inode.  This can be called
387  * once we complete IO for a given set of bytes to release their metadata
388  * reservations, or on error for the same reason.
389  */
390 void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
391 				     bool qgroup_free)
392 {
393 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
394 
395 	num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
396 	spin_lock(&inode->lock);
397 	inode->csum_bytes -= num_bytes;
398 	btrfs_calculate_inode_block_rsv_size(fs_info, inode);
399 	spin_unlock(&inode->lock);
400 
401 	if (btrfs_is_testing(fs_info))
402 		return;
403 
404 	btrfs_inode_rsv_release(inode, qgroup_free);
405 }
406 
407 /**
408  * btrfs_delalloc_release_extents - release our outstanding_extents
409  * @inode: the inode to balance the reservation for.
410  * @num_bytes: the number of bytes we originally reserved with
411  * @qgroup_free: do we need to free qgroup meta reservation or convert them.
412  *
413  * When we reserve space we increase outstanding_extents for the extents we may
414  * add.  Once we've set the range as delalloc or created our ordered extents we
415  * have outstanding_extents to track the real usage, so we use this to free our
416  * temporarily tracked outstanding_extents.  This _must_ be used in conjunction
417  * with btrfs_delalloc_reserve_metadata.
418  */
419 void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes,
420 				    bool qgroup_free)
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(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, qgroup_free);
435 }
436 
437 /**
438  * btrfs_delalloc_reserve_space - reserve data and metadata space for
439  * delalloc
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
449  *   and reserve precious 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
454  *   also reserve metadata space in a 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 -EQUOT)
461  */
462 int btrfs_delalloc_reserve_space(struct 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);
468 	if (ret < 0)
469 		return ret;
470 	ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
471 	if (ret < 0)
472 		btrfs_free_reserved_data_space(inode, *reserved, start, len);
473 	return ret;
474 }
475 
476 /**
477  * btrfs_delalloc_release_space - release data and metadata space for delalloc
478  * @inode: inode we're releasing space for
479  * @start: start position of the space already reserved
480  * @len: the len of the space already reserved
481  * @release_bytes: the len of the space we consumed or didn't use
482  *
483  * This function will release the metadata space that was not used and will
484  * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
485  * list if there are no delalloc bytes left.
486  * Also it will handle the qgroup reserved space.
487  */
488 void btrfs_delalloc_release_space(struct inode *inode,
489 				  struct extent_changeset *reserved,
490 				  u64 start, u64 len, bool qgroup_free)
491 {
492 	btrfs_delalloc_release_metadata(BTRFS_I(inode), len, qgroup_free);
493 	btrfs_free_reserved_data_space(inode, reserved, start, len);
494 }
495