Linux Audio

Check our new training course

Embedded Linux training

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