blob: 391dcb2170985b5aebca46033582e546cc815d6e [file] [log] [blame]
Josef Bacik86736342019-06-19 15:12:00 -04001// 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"
Josef Bacik07730d82019-06-20 15:38:04 -040010#include "block-group.h"
Josef Bacik86736342019-06-19 15:12:00 -040011
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 trace_btrfs_space_reservation(fs_info, "space_info",
134 data_sinfo->flags, bytes, 1);
135 spin_unlock(&data_sinfo->lock);
136
137 return 0;
138}
139
140int btrfs_check_data_free_space(struct inode *inode,
141 struct extent_changeset **reserved, u64 start, u64 len)
142{
143 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
144 int ret;
145
146 /* align the range */
147 len = round_up(start + len, fs_info->sectorsize) -
148 round_down(start, fs_info->sectorsize);
149 start = round_down(start, fs_info->sectorsize);
150
151 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len);
152 if (ret < 0)
153 return ret;
154
155 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
156 ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
157 if (ret < 0)
158 btrfs_free_reserved_data_space_noquota(inode, start, len);
159 else
160 ret = 0;
161 return ret;
162}
163
164/*
165 * Called if we need to clear a data reservation for this inode
166 * Normally in a error case.
167 *
168 * This one will *NOT* use accurate qgroup reserved space API, just for case
169 * which we can't sleep and is sure it won't affect qgroup reserved space.
170 * Like clear_bit_hook().
171 */
172void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
173 u64 len)
174{
175 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
176 struct btrfs_space_info *data_sinfo;
177
178 /* Make sure the range is aligned to sectorsize */
179 len = round_up(start + len, fs_info->sectorsize) -
180 round_down(start, fs_info->sectorsize);
181 start = round_down(start, fs_info->sectorsize);
182
183 data_sinfo = fs_info->data_sinfo;
184 spin_lock(&data_sinfo->lock);
185 btrfs_space_info_update_bytes_may_use(fs_info, data_sinfo, -len);
186 trace_btrfs_space_reservation(fs_info, "space_info",
187 data_sinfo->flags, len, 0);
188 spin_unlock(&data_sinfo->lock);
189}
190
191/*
192 * Called if we need to clear a data reservation for this inode
193 * Normally in a error case.
194 *
195 * This one will handle the per-inode data rsv map for accurate reserved
196 * space framework.
197 */
198void btrfs_free_reserved_data_space(struct inode *inode,
199 struct extent_changeset *reserved, u64 start, u64 len)
200{
201 struct btrfs_root *root = BTRFS_I(inode)->root;
202
203 /* Make sure the range is aligned to sectorsize */
204 len = round_up(start + len, root->fs_info->sectorsize) -
205 round_down(start, root->fs_info->sectorsize);
206 start = round_down(start, root->fs_info->sectorsize);
207
208 btrfs_free_reserved_data_space_noquota(inode, start, len);
209 btrfs_qgroup_free_data(inode, reserved, start, len);
210}
211
212/**
213 * btrfs_inode_rsv_release - release any excessive reservation.
214 * @inode - the inode we need to release from.
215 * @qgroup_free - free or convert qgroup meta.
216 * Unlike normal operation, qgroup meta reservation needs to know if we are
217 * freeing qgroup reservation or just converting it into per-trans. Normally
218 * @qgroup_free is true for error handling, and false for normal release.
219 *
220 * This is the same as btrfs_block_rsv_release, except that it handles the
221 * tracepoint for the reservation.
222 */
223static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
224{
225 struct btrfs_fs_info *fs_info = inode->root->fs_info;
226 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
227 u64 released = 0;
228 u64 qgroup_to_release = 0;
229
230 /*
231 * Since we statically set the block_rsv->size we just want to say we
232 * are releasing 0 bytes, and then we'll just get the reservation over
233 * the size free'd.
234 */
235 released = __btrfs_block_rsv_release(fs_info, block_rsv, 0,
236 &qgroup_to_release);
237 if (released > 0)
238 trace_btrfs_space_reservation(fs_info, "delalloc",
239 btrfs_ino(inode), released, 0);
240 if (qgroup_free)
241 btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
242 else
243 btrfs_qgroup_convert_reserved_meta(inode->root,
244 qgroup_to_release);
245}
246
247static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
248 struct btrfs_inode *inode)
249{
250 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
251 u64 reserve_size = 0;
252 u64 qgroup_rsv_size = 0;
253 u64 csum_leaves;
254 unsigned outstanding_extents;
255
256 lockdep_assert_held(&inode->lock);
257 outstanding_extents = inode->outstanding_extents;
Josef Bacikbcacf5f32019-08-22 15:14:34 -0400258
259 /*
260 * Insert size for the number of outstanding extents, 1 normal size for
261 * updating the inode.
262 */
263 if (outstanding_extents) {
Josef Bacik2bd36e72019-08-22 15:14:33 -0400264 reserve_size = btrfs_calc_insert_metadata_size(fs_info,
Josef Bacikbcacf5f32019-08-22 15:14:34 -0400265 outstanding_extents);
266 reserve_size += btrfs_calc_metadata_size(fs_info, 1);
267 }
Josef Bacik86736342019-06-19 15:12:00 -0400268 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
269 inode->csum_bytes);
Josef Bacik2bd36e72019-08-22 15:14:33 -0400270 reserve_size += btrfs_calc_insert_metadata_size(fs_info,
271 csum_leaves);
Josef Bacik86736342019-06-19 15:12:00 -0400272 /*
273 * For qgroup rsv, the calculation is very simple:
274 * account one nodesize for each outstanding extent
275 *
276 * This is overestimating in most cases.
277 */
278 qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
279
280 spin_lock(&block_rsv->lock);
281 block_rsv->size = reserve_size;
282 block_rsv->qgroup_rsv_size = qgroup_rsv_size;
283 spin_unlock(&block_rsv->lock);
284}
285
286static void calc_inode_reservations(struct btrfs_fs_info *fs_info,
287 u64 num_bytes, u64 *meta_reserve,
288 u64 *qgroup_reserve)
289{
290 u64 nr_extents = count_max_extents(num_bytes);
291 u64 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, num_bytes);
Josef Bacikbcacf5f32019-08-22 15:14:34 -0400292 u64 inode_update = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacik86736342019-06-19 15:12:00 -0400293
Josef Bacik2bd36e72019-08-22 15:14:33 -0400294 *meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
Josef Bacikbcacf5f32019-08-22 15:14:34 -0400295 nr_extents + csum_leaves);
296
297 /*
298 * finish_ordered_io has to update the inode, so add the space required
299 * for an inode update.
300 */
301 *meta_reserve += inode_update;
Josef Bacik86736342019-06-19 15:12:00 -0400302 *qgroup_reserve = nr_extents * fs_info->nodesize;
303}
304
305int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
306{
307 struct btrfs_root *root = inode->root;
308 struct btrfs_fs_info *fs_info = root->fs_info;
309 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
310 u64 meta_reserve, qgroup_reserve;
311 unsigned nr_extents;
312 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
313 int ret = 0;
314 bool delalloc_lock = true;
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 (btrfs_is_free_space_inode(inode)) {
326 flush = BTRFS_RESERVE_NO_FLUSH;
327 delalloc_lock = false;
328 } else {
329 if (current->journal_info)
330 flush = BTRFS_RESERVE_FLUSH_LIMIT;
331
332 if (btrfs_transaction_in_commit(fs_info))
333 schedule_timeout(1);
334 }
335
336 if (delalloc_lock)
337 mutex_lock(&inode->delalloc_mutex);
338
339 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
340
341 /*
342 * We always want to do it this way, every other way is wrong and ends
343 * in tears. Pre-reserving the amount we are going to add will always
344 * be the right way, because otherwise if we have enough parallelism we
345 * could end up with thousands of inodes all holding little bits of
346 * reservations they were able to make previously and the only way to
347 * reclaim that space is to ENOSPC out the operations and clear
348 * everything out and try again, which is bad. This way we just
349 * over-reserve slightly, and clean up the mess when we are done.
350 */
351 calc_inode_reservations(fs_info, num_bytes, &meta_reserve,
352 &qgroup_reserve);
353 ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserve, true);
354 if (ret)
355 goto out_fail;
356 ret = btrfs_reserve_metadata_bytes(root, block_rsv, meta_reserve, flush);
357 if (ret)
358 goto out_qgroup;
359
360 /*
361 * Now we need to update our outstanding extents and csum bytes _first_
362 * and then add the reservation to the block_rsv. This keeps us from
363 * racing with an ordered completion or some such that would think it
364 * needs to free the reservation we just made.
365 */
366 spin_lock(&inode->lock);
367 nr_extents = count_max_extents(num_bytes);
368 btrfs_mod_outstanding_extents(inode, nr_extents);
369 inode->csum_bytes += num_bytes;
370 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
371 spin_unlock(&inode->lock);
372
373 /* Now we can safely add our space to our block rsv */
374 btrfs_block_rsv_add_bytes(block_rsv, meta_reserve, false);
375 trace_btrfs_space_reservation(root->fs_info, "delalloc",
376 btrfs_ino(inode), meta_reserve, 1);
377
378 spin_lock(&block_rsv->lock);
379 block_rsv->qgroup_rsv_reserved += qgroup_reserve;
380 spin_unlock(&block_rsv->lock);
381
382 if (delalloc_lock)
383 mutex_unlock(&inode->delalloc_mutex);
384 return 0;
385out_qgroup:
386 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
387out_fail:
388 btrfs_inode_rsv_release(inode, true);
389 if (delalloc_lock)
390 mutex_unlock(&inode->delalloc_mutex);
391 return ret;
392}
393
394/**
395 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
396 * @inode: the inode to release the reservation for.
397 * @num_bytes: the number of bytes we are releasing.
398 * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
399 *
400 * This will release the metadata reservation for an inode. This can be called
401 * once we complete IO for a given set of bytes to release their metadata
402 * reservations, or on error for the same reason.
403 */
404void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
405 bool qgroup_free)
406{
407 struct btrfs_fs_info *fs_info = inode->root->fs_info;
408
409 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
410 spin_lock(&inode->lock);
411 inode->csum_bytes -= num_bytes;
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, qgroup_free);
419}
420
421/**
422 * btrfs_delalloc_release_extents - release our outstanding_extents
423 * @inode: the inode to balance the reservation for.
424 * @num_bytes: the number of bytes we originally reserved with
425 * @qgroup_free: do we need to free qgroup meta reservation or convert them.
426 *
427 * When we reserve space we increase outstanding_extents for the extents we may
428 * add. Once we've set the range as delalloc or created our ordered extents we
429 * have outstanding_extents to track the real usage, so we use this to free our
430 * temporarily tracked outstanding_extents. This _must_ be used in conjunction
431 * with btrfs_delalloc_reserve_metadata.
432 */
433void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes,
434 bool qgroup_free)
435{
436 struct btrfs_fs_info *fs_info = inode->root->fs_info;
437 unsigned num_extents;
438
439 spin_lock(&inode->lock);
440 num_extents = count_max_extents(num_bytes);
441 btrfs_mod_outstanding_extents(inode, -num_extents);
442 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
443 spin_unlock(&inode->lock);
444
445 if (btrfs_is_testing(fs_info))
446 return;
447
448 btrfs_inode_rsv_release(inode, qgroup_free);
449}
450
451/**
452 * btrfs_delalloc_reserve_space - reserve data and metadata space for
453 * delalloc
454 * @inode: inode we're writing to
455 * @start: start range we are writing to
456 * @len: how long the range we are writing to
457 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
458 * current reservation.
459 *
460 * This will do the following things
461 *
462 * - reserve space in data space info for num bytes
463 * and reserve precious corresponding qgroup space
464 * (Done in check_data_free_space)
465 *
466 * - reserve space for metadata space, based on the number of outstanding
467 * extents and how much csums will be needed
468 * also reserve metadata space in a per root over-reserve method.
469 * - add to the inodes->delalloc_bytes
470 * - add it to the fs_info's delalloc inodes list.
471 * (Above 3 all done in delalloc_reserve_metadata)
472 *
473 * Return 0 for success
474 * Return <0 for error(-ENOSPC or -EQUOT)
475 */
476int btrfs_delalloc_reserve_space(struct inode *inode,
477 struct extent_changeset **reserved, u64 start, u64 len)
478{
479 int ret;
480
481 ret = btrfs_check_data_free_space(inode, reserved, start, len);
482 if (ret < 0)
483 return ret;
484 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
485 if (ret < 0)
486 btrfs_free_reserved_data_space(inode, *reserved, start, len);
487 return ret;
488}
489
490/**
491 * btrfs_delalloc_release_space - release data and metadata space for delalloc
492 * @inode: inode we're releasing space for
493 * @start: start position of the space already reserved
494 * @len: the len of the space already reserved
495 * @release_bytes: the len of the space we consumed or didn't use
496 *
497 * This function will release the metadata space that was not used and will
498 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
499 * list if there are no delalloc bytes left.
500 * Also it will handle the qgroup reserved space.
501 */
502void btrfs_delalloc_release_space(struct inode *inode,
503 struct extent_changeset *reserved,
504 u64 start, u64 len, bool qgroup_free)
505{
506 btrfs_delalloc_release_metadata(BTRFS_I(inode), len, qgroup_free);
507 btrfs_free_reserved_data_space(inode, reserved, start, len);
508}