1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/kthread.h>
7 #include <linux/pagemap.h>
11 #include "free-space-cache.h"
12 #include "inode-map.h"
13 #include "transaction.h"
15 static void fail_caching_thread(struct btrfs_root
*root
)
17 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
19 btrfs_warn(fs_info
, "failed to start inode caching task");
20 btrfs_clear_pending_and_info(fs_info
, INODE_MAP_CACHE
,
21 "disabling inode map caching");
22 spin_lock(&root
->ino_cache_lock
);
23 root
->ino_cache_state
= BTRFS_CACHE_ERROR
;
24 spin_unlock(&root
->ino_cache_lock
);
25 wake_up(&root
->ino_cache_wait
);
28 static int caching_kthread(void *data
)
30 struct btrfs_root
*root
= data
;
31 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
32 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
34 struct btrfs_path
*path
;
35 struct extent_buffer
*leaf
;
40 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
43 path
= btrfs_alloc_path();
45 fail_caching_thread(root
);
49 /* Since the commit root is read-only, we can safely skip locking. */
50 path
->skip_locking
= 1;
51 path
->search_commit_root
= 1;
52 path
->reada
= READA_FORWARD
;
54 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
56 key
.type
= BTRFS_INODE_ITEM_KEY
;
58 /* need to make sure the commit_root doesn't disappear */
59 down_read(&fs_info
->commit_root_sem
);
61 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
66 if (btrfs_fs_closing(fs_info
))
69 leaf
= path
->nodes
[0];
70 slot
= path
->slots
[0];
71 if (slot
>= btrfs_header_nritems(leaf
)) {
72 ret
= btrfs_next_leaf(root
, path
);
79 btrfs_transaction_in_commit(fs_info
)) {
80 leaf
= path
->nodes
[0];
82 if (WARN_ON(btrfs_header_nritems(leaf
) == 0))
86 * Save the key so we can advances forward
89 btrfs_item_key_to_cpu(leaf
, &key
, 0);
90 btrfs_release_path(path
);
91 root
->ino_cache_progress
= last
;
92 up_read(&fs_info
->commit_root_sem
);
99 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
101 if (key
.type
!= BTRFS_INODE_ITEM_KEY
)
104 if (key
.objectid
>= root
->highest_objectid
)
107 if (last
!= (u64
)-1 && last
+ 1 != key
.objectid
) {
108 __btrfs_add_free_space(fs_info
, ctl
, last
+ 1,
109 key
.objectid
- last
- 1);
110 wake_up(&root
->ino_cache_wait
);
118 if (last
< root
->highest_objectid
- 1) {
119 __btrfs_add_free_space(fs_info
, ctl
, last
+ 1,
120 root
->highest_objectid
- last
- 1);
123 spin_lock(&root
->ino_cache_lock
);
124 root
->ino_cache_state
= BTRFS_CACHE_FINISHED
;
125 spin_unlock(&root
->ino_cache_lock
);
127 root
->ino_cache_progress
= (u64
)-1;
128 btrfs_unpin_free_ino(root
);
130 wake_up(&root
->ino_cache_wait
);
131 up_read(&fs_info
->commit_root_sem
);
133 btrfs_free_path(path
);
138 static void start_caching(struct btrfs_root
*root
)
140 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
141 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
142 struct task_struct
*tsk
;
146 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
149 spin_lock(&root
->ino_cache_lock
);
150 if (root
->ino_cache_state
!= BTRFS_CACHE_NO
) {
151 spin_unlock(&root
->ino_cache_lock
);
155 root
->ino_cache_state
= BTRFS_CACHE_STARTED
;
156 spin_unlock(&root
->ino_cache_lock
);
158 ret
= load_free_ino_cache(fs_info
, root
);
160 spin_lock(&root
->ino_cache_lock
);
161 root
->ino_cache_state
= BTRFS_CACHE_FINISHED
;
162 spin_unlock(&root
->ino_cache_lock
);
163 wake_up(&root
->ino_cache_wait
);
168 * It can be quite time-consuming to fill the cache by searching
169 * through the extent tree, and this can keep ino allocation path
170 * waiting. Therefore at start we quickly find out the highest
171 * inode number and we know we can use inode numbers which fall in
172 * [highest_ino + 1, BTRFS_LAST_FREE_OBJECTID].
174 ret
= btrfs_find_free_objectid(root
, &objectid
);
175 if (!ret
&& objectid
<= BTRFS_LAST_FREE_OBJECTID
) {
176 __btrfs_add_free_space(fs_info
, ctl
, objectid
,
177 BTRFS_LAST_FREE_OBJECTID
- objectid
+ 1);
180 tsk
= kthread_run(caching_kthread
, root
, "btrfs-ino-cache-%llu",
181 root
->root_key
.objectid
);
183 fail_caching_thread(root
);
186 int btrfs_find_free_ino(struct btrfs_root
*root
, u64
*objectid
)
188 if (!btrfs_test_opt(root
->fs_info
, INODE_MAP_CACHE
))
189 return btrfs_find_free_objectid(root
, objectid
);
192 *objectid
= btrfs_find_ino_for_alloc(root
);
199 wait_event(root
->ino_cache_wait
,
200 root
->ino_cache_state
== BTRFS_CACHE_FINISHED
||
201 root
->ino_cache_state
== BTRFS_CACHE_ERROR
||
202 root
->free_ino_ctl
->free_space
> 0);
204 if (root
->ino_cache_state
== BTRFS_CACHE_FINISHED
&&
205 root
->free_ino_ctl
->free_space
== 0)
207 else if (root
->ino_cache_state
== BTRFS_CACHE_ERROR
)
208 return btrfs_find_free_objectid(root
, objectid
);
213 void btrfs_return_ino(struct btrfs_root
*root
, u64 objectid
)
215 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
216 struct btrfs_free_space_ctl
*pinned
= root
->free_ino_pinned
;
218 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
221 if (root
->ino_cache_state
== BTRFS_CACHE_FINISHED
) {
222 __btrfs_add_free_space(fs_info
, pinned
, objectid
, 1);
224 down_write(&fs_info
->commit_root_sem
);
225 spin_lock(&root
->ino_cache_lock
);
226 if (root
->ino_cache_state
== BTRFS_CACHE_FINISHED
) {
227 spin_unlock(&root
->ino_cache_lock
);
228 up_write(&fs_info
->commit_root_sem
);
231 spin_unlock(&root
->ino_cache_lock
);
235 __btrfs_add_free_space(fs_info
, pinned
, objectid
, 1);
237 up_write(&fs_info
->commit_root_sem
);
242 * When a transaction is committed, we'll move those inode numbers which are
243 * smaller than root->ino_cache_progress from pinned tree to free_ino tree, and
244 * others will just be dropped, because the commit root we were searching has
247 * Must be called with root->fs_info->commit_root_sem held
249 void btrfs_unpin_free_ino(struct btrfs_root
*root
)
251 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
252 struct rb_root
*rbroot
= &root
->free_ino_pinned
->free_space_offset
;
253 spinlock_t
*rbroot_lock
= &root
->free_ino_pinned
->tree_lock
;
254 struct btrfs_free_space
*info
;
258 if (!btrfs_test_opt(root
->fs_info
, INODE_MAP_CACHE
))
262 spin_lock(rbroot_lock
);
263 n
= rb_first(rbroot
);
265 spin_unlock(rbroot_lock
);
269 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
270 BUG_ON(info
->bitmap
); /* Logic error */
272 if (info
->offset
> root
->ino_cache_progress
)
275 count
= min(root
->ino_cache_progress
- info
->offset
+ 1,
278 rb_erase(&info
->offset_index
, rbroot
);
279 spin_unlock(rbroot_lock
);
281 __btrfs_add_free_space(root
->fs_info
, ctl
,
282 info
->offset
, count
);
283 kmem_cache_free(btrfs_free_space_cachep
, info
);
287 #define INIT_THRESHOLD ((SZ_32K / 2) / sizeof(struct btrfs_free_space))
288 #define INODES_PER_BITMAP (PAGE_SIZE * 8)
291 * The goal is to keep the memory used by the free_ino tree won't
292 * exceed the memory if we use bitmaps only.
294 static void recalculate_thresholds(struct btrfs_free_space_ctl
*ctl
)
296 struct btrfs_free_space
*info
;
301 n
= rb_last(&ctl
->free_space_offset
);
303 ctl
->extents_thresh
= INIT_THRESHOLD
;
306 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
309 * Find the maximum inode number in the filesystem. Note we
310 * ignore the fact that this can be a bitmap, because we are
311 * not doing precise calculation.
313 max_ino
= info
->bytes
- 1;
315 max_bitmaps
= ALIGN(max_ino
, INODES_PER_BITMAP
) / INODES_PER_BITMAP
;
316 if (max_bitmaps
<= ctl
->total_bitmaps
) {
317 ctl
->extents_thresh
= 0;
321 ctl
->extents_thresh
= (max_bitmaps
- ctl
->total_bitmaps
) *
322 PAGE_SIZE
/ sizeof(*info
);
326 * We don't fall back to bitmap, if we are below the extents threshold
327 * or this chunk of inode numbers is a big one.
329 static bool use_bitmap(struct btrfs_free_space_ctl
*ctl
,
330 struct btrfs_free_space
*info
)
332 if (ctl
->free_extents
< ctl
->extents_thresh
||
333 info
->bytes
> INODES_PER_BITMAP
/ 10)
339 static const struct btrfs_free_space_op free_ino_op
= {
340 .recalc_thresholds
= recalculate_thresholds
,
341 .use_bitmap
= use_bitmap
,
344 static void pinned_recalc_thresholds(struct btrfs_free_space_ctl
*ctl
)
348 static bool pinned_use_bitmap(struct btrfs_free_space_ctl
*ctl
,
349 struct btrfs_free_space
*info
)
352 * We always use extents for two reasons:
354 * - The pinned tree is only used during the process of caching
356 * - Make code simpler. See btrfs_unpin_free_ino().
361 static const struct btrfs_free_space_op pinned_free_ino_op
= {
362 .recalc_thresholds
= pinned_recalc_thresholds
,
363 .use_bitmap
= pinned_use_bitmap
,
366 void btrfs_init_free_ino_ctl(struct btrfs_root
*root
)
368 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
369 struct btrfs_free_space_ctl
*pinned
= root
->free_ino_pinned
;
371 spin_lock_init(&ctl
->tree_lock
);
375 ctl
->op
= &free_ino_op
;
376 INIT_LIST_HEAD(&ctl
->trimming_ranges
);
377 mutex_init(&ctl
->cache_writeout_mutex
);
380 * Initially we allow to use 16K of ram to cache chunks of
381 * inode numbers before we resort to bitmaps. This is somewhat
382 * arbitrary, but it will be adjusted in runtime.
384 ctl
->extents_thresh
= INIT_THRESHOLD
;
386 spin_lock_init(&pinned
->tree_lock
);
389 pinned
->private = NULL
;
390 pinned
->extents_thresh
= 0;
391 pinned
->op
= &pinned_free_ino_op
;
394 int btrfs_save_ino_cache(struct btrfs_root
*root
,
395 struct btrfs_trans_handle
*trans
)
397 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
398 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
399 struct btrfs_path
*path
;
401 struct btrfs_block_rsv
*rsv
;
402 struct extent_changeset
*data_reserved
= NULL
;
409 /* only fs tree and subvol/snap needs ino cache */
410 if (root
->root_key
.objectid
!= BTRFS_FS_TREE_OBJECTID
&&
411 (root
->root_key
.objectid
< BTRFS_FIRST_FREE_OBJECTID
||
412 root
->root_key
.objectid
> BTRFS_LAST_FREE_OBJECTID
))
415 /* Don't save inode cache if we are deleting this root */
416 if (btrfs_root_refs(&root
->root_item
) == 0)
419 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
422 path
= btrfs_alloc_path();
426 rsv
= trans
->block_rsv
;
427 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
429 num_bytes
= trans
->bytes_reserved
;
431 * 1 item for inode item insertion if need
432 * 4 items for inode item update (in the worst case)
433 * 1 items for slack space if we need do truncation
434 * 1 item for free space object
435 * 3 items for pre-allocation
437 trans
->bytes_reserved
= btrfs_calc_trans_metadata_size(fs_info
, 10);
438 ret
= btrfs_block_rsv_add(root
, trans
->block_rsv
,
439 trans
->bytes_reserved
,
440 BTRFS_RESERVE_NO_FLUSH
);
443 trace_btrfs_space_reservation(fs_info
, "ino_cache", trans
->transid
,
444 trans
->bytes_reserved
, 1);
446 inode
= lookup_free_ino_inode(root
, path
);
447 if (IS_ERR(inode
) && (PTR_ERR(inode
) != -ENOENT
|| retry
)) {
448 ret
= PTR_ERR(inode
);
453 BUG_ON(retry
); /* Logic error */
456 ret
= create_free_ino_inode(root
, trans
, path
);
462 BTRFS_I(inode
)->generation
= 0;
463 ret
= btrfs_update_inode(trans
, root
, inode
);
465 btrfs_abort_transaction(trans
, ret
);
469 if (i_size_read(inode
) > 0) {
470 ret
= btrfs_truncate_free_space_cache(trans
, NULL
, inode
);
473 btrfs_abort_transaction(trans
, ret
);
478 spin_lock(&root
->ino_cache_lock
);
479 if (root
->ino_cache_state
!= BTRFS_CACHE_FINISHED
) {
481 spin_unlock(&root
->ino_cache_lock
);
484 spin_unlock(&root
->ino_cache_lock
);
486 spin_lock(&ctl
->tree_lock
);
487 prealloc
= sizeof(struct btrfs_free_space
) * ctl
->free_extents
;
488 prealloc
= ALIGN(prealloc
, PAGE_SIZE
);
489 prealloc
+= ctl
->total_bitmaps
* PAGE_SIZE
;
490 spin_unlock(&ctl
->tree_lock
);
492 /* Just to make sure we have enough space */
493 prealloc
+= 8 * PAGE_SIZE
;
495 ret
= btrfs_delalloc_reserve_space(inode
, &data_reserved
, 0, prealloc
);
499 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, prealloc
,
500 prealloc
, prealloc
, &alloc_hint
);
502 btrfs_delalloc_release_extents(BTRFS_I(inode
), prealloc
);
503 btrfs_delalloc_release_metadata(BTRFS_I(inode
), prealloc
, true);
507 ret
= btrfs_write_out_ino_cache(root
, trans
, path
, inode
);
508 btrfs_delalloc_release_extents(BTRFS_I(inode
), prealloc
);
512 trace_btrfs_space_reservation(fs_info
, "ino_cache", trans
->transid
,
513 trans
->bytes_reserved
, 0);
514 btrfs_block_rsv_release(fs_info
, trans
->block_rsv
,
515 trans
->bytes_reserved
);
517 trans
->block_rsv
= rsv
;
518 trans
->bytes_reserved
= num_bytes
;
520 btrfs_free_path(path
);
521 extent_changeset_free(data_reserved
);
525 int btrfs_find_highest_objectid(struct btrfs_root
*root
, u64
*objectid
)
527 struct btrfs_path
*path
;
529 struct extent_buffer
*l
;
530 struct btrfs_key search_key
;
531 struct btrfs_key found_key
;
534 path
= btrfs_alloc_path();
538 search_key
.objectid
= BTRFS_LAST_FREE_OBJECTID
;
539 search_key
.type
= -1;
540 search_key
.offset
= (u64
)-1;
541 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 0);
544 BUG_ON(ret
== 0); /* Corruption */
545 if (path
->slots
[0] > 0) {
546 slot
= path
->slots
[0] - 1;
548 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
549 *objectid
= max_t(u64
, found_key
.objectid
,
550 BTRFS_FIRST_FREE_OBJECTID
- 1);
552 *objectid
= BTRFS_FIRST_FREE_OBJECTID
- 1;
556 btrfs_free_path(path
);
560 int btrfs_find_free_objectid(struct btrfs_root
*root
, u64
*objectid
)
563 mutex_lock(&root
->objectid_mutex
);
565 if (unlikely(root
->highest_objectid
>= BTRFS_LAST_FREE_OBJECTID
)) {
566 btrfs_warn(root
->fs_info
,
567 "the objectid of root %llu reaches its highest value",
568 root
->root_key
.objectid
);
573 *objectid
= ++root
->highest_objectid
;
576 mutex_unlock(&root
->objectid_mutex
);