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 int caching_kthread(void *data
)
17 struct btrfs_root
*root
= data
;
18 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
19 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
21 struct btrfs_path
*path
;
22 struct extent_buffer
*leaf
;
27 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
30 path
= btrfs_alloc_path();
34 /* Since the commit root is read-only, we can safely skip locking. */
35 path
->skip_locking
= 1;
36 path
->search_commit_root
= 1;
37 path
->reada
= READA_FORWARD
;
39 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
41 key
.type
= BTRFS_INODE_ITEM_KEY
;
43 /* need to make sure the commit_root doesn't disappear */
44 down_read(&fs_info
->commit_root_sem
);
46 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
51 if (btrfs_fs_closing(fs_info
))
54 leaf
= path
->nodes
[0];
55 slot
= path
->slots
[0];
56 if (slot
>= btrfs_header_nritems(leaf
)) {
57 ret
= btrfs_next_leaf(root
, path
);
64 btrfs_transaction_in_commit(fs_info
)) {
65 leaf
= path
->nodes
[0];
67 if (WARN_ON(btrfs_header_nritems(leaf
) == 0))
71 * Save the key so we can advances forward
74 btrfs_item_key_to_cpu(leaf
, &key
, 0);
75 btrfs_release_path(path
);
76 root
->ino_cache_progress
= last
;
77 up_read(&fs_info
->commit_root_sem
);
84 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
86 if (key
.type
!= BTRFS_INODE_ITEM_KEY
)
89 if (key
.objectid
>= root
->highest_objectid
)
92 if (last
!= (u64
)-1 && last
+ 1 != key
.objectid
) {
93 __btrfs_add_free_space(fs_info
, ctl
, last
+ 1,
94 key
.objectid
- last
- 1);
95 wake_up(&root
->ino_cache_wait
);
103 if (last
< root
->highest_objectid
- 1) {
104 __btrfs_add_free_space(fs_info
, ctl
, last
+ 1,
105 root
->highest_objectid
- last
- 1);
108 spin_lock(&root
->ino_cache_lock
);
109 root
->ino_cache_state
= BTRFS_CACHE_FINISHED
;
110 spin_unlock(&root
->ino_cache_lock
);
112 root
->ino_cache_progress
= (u64
)-1;
113 btrfs_unpin_free_ino(root
);
115 wake_up(&root
->ino_cache_wait
);
116 up_read(&fs_info
->commit_root_sem
);
118 btrfs_free_path(path
);
123 static void start_caching(struct btrfs_root
*root
)
125 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
126 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
127 struct task_struct
*tsk
;
131 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
134 spin_lock(&root
->ino_cache_lock
);
135 if (root
->ino_cache_state
!= BTRFS_CACHE_NO
) {
136 spin_unlock(&root
->ino_cache_lock
);
140 root
->ino_cache_state
= BTRFS_CACHE_STARTED
;
141 spin_unlock(&root
->ino_cache_lock
);
143 ret
= load_free_ino_cache(fs_info
, root
);
145 spin_lock(&root
->ino_cache_lock
);
146 root
->ino_cache_state
= BTRFS_CACHE_FINISHED
;
147 spin_unlock(&root
->ino_cache_lock
);
152 * It can be quite time-consuming to fill the cache by searching
153 * through the extent tree, and this can keep ino allocation path
154 * waiting. Therefore at start we quickly find out the highest
155 * inode number and we know we can use inode numbers which fall in
156 * [highest_ino + 1, BTRFS_LAST_FREE_OBJECTID].
158 ret
= btrfs_find_free_objectid(root
, &objectid
);
159 if (!ret
&& objectid
<= BTRFS_LAST_FREE_OBJECTID
) {
160 __btrfs_add_free_space(fs_info
, ctl
, objectid
,
161 BTRFS_LAST_FREE_OBJECTID
- objectid
+ 1);
164 tsk
= kthread_run(caching_kthread
, root
, "btrfs-ino-cache-%llu",
165 root
->root_key
.objectid
);
167 btrfs_warn(fs_info
, "failed to start inode caching task");
168 btrfs_clear_pending_and_info(fs_info
, INODE_MAP_CACHE
,
169 "disabling inode map caching");
173 int btrfs_find_free_ino(struct btrfs_root
*root
, u64
*objectid
)
175 if (!btrfs_test_opt(root
->fs_info
, INODE_MAP_CACHE
))
176 return btrfs_find_free_objectid(root
, objectid
);
179 *objectid
= btrfs_find_ino_for_alloc(root
);
186 wait_event(root
->ino_cache_wait
,
187 root
->ino_cache_state
== BTRFS_CACHE_FINISHED
||
188 root
->free_ino_ctl
->free_space
> 0);
190 if (root
->ino_cache_state
== BTRFS_CACHE_FINISHED
&&
191 root
->free_ino_ctl
->free_space
== 0)
197 void btrfs_return_ino(struct btrfs_root
*root
, u64 objectid
)
199 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
200 struct btrfs_free_space_ctl
*pinned
= root
->free_ino_pinned
;
202 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
205 if (root
->ino_cache_state
== BTRFS_CACHE_FINISHED
) {
206 __btrfs_add_free_space(fs_info
, pinned
, objectid
, 1);
208 down_write(&fs_info
->commit_root_sem
);
209 spin_lock(&root
->ino_cache_lock
);
210 if (root
->ino_cache_state
== BTRFS_CACHE_FINISHED
) {
211 spin_unlock(&root
->ino_cache_lock
);
212 up_write(&fs_info
->commit_root_sem
);
215 spin_unlock(&root
->ino_cache_lock
);
219 __btrfs_add_free_space(fs_info
, pinned
, objectid
, 1);
221 up_write(&fs_info
->commit_root_sem
);
226 * When a transaction is committed, we'll move those inode numbers which are
227 * smaller than root->ino_cache_progress from pinned tree to free_ino tree, and
228 * others will just be dropped, because the commit root we were searching has
231 * Must be called with root->fs_info->commit_root_sem held
233 void btrfs_unpin_free_ino(struct btrfs_root
*root
)
235 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
236 struct rb_root
*rbroot
= &root
->free_ino_pinned
->free_space_offset
;
237 spinlock_t
*rbroot_lock
= &root
->free_ino_pinned
->tree_lock
;
238 struct btrfs_free_space
*info
;
242 if (!btrfs_test_opt(root
->fs_info
, INODE_MAP_CACHE
))
246 spin_lock(rbroot_lock
);
247 n
= rb_first(rbroot
);
249 spin_unlock(rbroot_lock
);
253 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
254 BUG_ON(info
->bitmap
); /* Logic error */
256 if (info
->offset
> root
->ino_cache_progress
)
259 count
= min(root
->ino_cache_progress
- info
->offset
+ 1,
262 rb_erase(&info
->offset_index
, rbroot
);
263 spin_unlock(rbroot_lock
);
265 __btrfs_add_free_space(root
->fs_info
, ctl
,
266 info
->offset
, count
);
267 kmem_cache_free(btrfs_free_space_cachep
, info
);
271 #define INIT_THRESHOLD ((SZ_32K / 2) / sizeof(struct btrfs_free_space))
272 #define INODES_PER_BITMAP (PAGE_SIZE * 8)
275 * The goal is to keep the memory used by the free_ino tree won't
276 * exceed the memory if we use bitmaps only.
278 static void recalculate_thresholds(struct btrfs_free_space_ctl
*ctl
)
280 struct btrfs_free_space
*info
;
285 n
= rb_last(&ctl
->free_space_offset
);
287 ctl
->extents_thresh
= INIT_THRESHOLD
;
290 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
293 * Find the maximum inode number in the filesystem. Note we
294 * ignore the fact that this can be a bitmap, because we are
295 * not doing precise calculation.
297 max_ino
= info
->bytes
- 1;
299 max_bitmaps
= ALIGN(max_ino
, INODES_PER_BITMAP
) / INODES_PER_BITMAP
;
300 if (max_bitmaps
<= ctl
->total_bitmaps
) {
301 ctl
->extents_thresh
= 0;
305 ctl
->extents_thresh
= (max_bitmaps
- ctl
->total_bitmaps
) *
306 PAGE_SIZE
/ sizeof(*info
);
310 * We don't fall back to bitmap, if we are below the extents threshold
311 * or this chunk of inode numbers is a big one.
313 static bool use_bitmap(struct btrfs_free_space_ctl
*ctl
,
314 struct btrfs_free_space
*info
)
316 if (ctl
->free_extents
< ctl
->extents_thresh
||
317 info
->bytes
> INODES_PER_BITMAP
/ 10)
323 static const struct btrfs_free_space_op free_ino_op
= {
324 .recalc_thresholds
= recalculate_thresholds
,
325 .use_bitmap
= use_bitmap
,
328 static void pinned_recalc_thresholds(struct btrfs_free_space_ctl
*ctl
)
332 static bool pinned_use_bitmap(struct btrfs_free_space_ctl
*ctl
,
333 struct btrfs_free_space
*info
)
336 * We always use extents for two reasons:
338 * - The pinned tree is only used during the process of caching
340 * - Make code simpler. See btrfs_unpin_free_ino().
345 static const struct btrfs_free_space_op pinned_free_ino_op
= {
346 .recalc_thresholds
= pinned_recalc_thresholds
,
347 .use_bitmap
= pinned_use_bitmap
,
350 void btrfs_init_free_ino_ctl(struct btrfs_root
*root
)
352 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
353 struct btrfs_free_space_ctl
*pinned
= root
->free_ino_pinned
;
355 spin_lock_init(&ctl
->tree_lock
);
359 ctl
->op
= &free_ino_op
;
360 INIT_LIST_HEAD(&ctl
->trimming_ranges
);
361 mutex_init(&ctl
->cache_writeout_mutex
);
364 * Initially we allow to use 16K of ram to cache chunks of
365 * inode numbers before we resort to bitmaps. This is somewhat
366 * arbitrary, but it will be adjusted in runtime.
368 ctl
->extents_thresh
= INIT_THRESHOLD
;
370 spin_lock_init(&pinned
->tree_lock
);
373 pinned
->private = NULL
;
374 pinned
->extents_thresh
= 0;
375 pinned
->op
= &pinned_free_ino_op
;
378 int btrfs_save_ino_cache(struct btrfs_root
*root
,
379 struct btrfs_trans_handle
*trans
)
381 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
382 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
383 struct btrfs_path
*path
;
385 struct btrfs_block_rsv
*rsv
;
386 struct extent_changeset
*data_reserved
= NULL
;
393 /* only fs tree and subvol/snap needs ino cache */
394 if (root
->root_key
.objectid
!= BTRFS_FS_TREE_OBJECTID
&&
395 (root
->root_key
.objectid
< BTRFS_FIRST_FREE_OBJECTID
||
396 root
->root_key
.objectid
> BTRFS_LAST_FREE_OBJECTID
))
399 /* Don't save inode cache if we are deleting this root */
400 if (btrfs_root_refs(&root
->root_item
) == 0)
403 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
406 path
= btrfs_alloc_path();
410 rsv
= trans
->block_rsv
;
411 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
413 num_bytes
= trans
->bytes_reserved
;
415 * 1 item for inode item insertion if need
416 * 4 items for inode item update (in the worst case)
417 * 1 items for slack space if we need do truncation
418 * 1 item for free space object
419 * 3 items for pre-allocation
421 trans
->bytes_reserved
= btrfs_calc_trans_metadata_size(fs_info
, 10);
422 ret
= btrfs_block_rsv_add(root
, trans
->block_rsv
,
423 trans
->bytes_reserved
,
424 BTRFS_RESERVE_NO_FLUSH
);
427 trace_btrfs_space_reservation(fs_info
, "ino_cache", trans
->transid
,
428 trans
->bytes_reserved
, 1);
430 inode
= lookup_free_ino_inode(root
, path
);
431 if (IS_ERR(inode
) && (PTR_ERR(inode
) != -ENOENT
|| retry
)) {
432 ret
= PTR_ERR(inode
);
437 BUG_ON(retry
); /* Logic error */
440 ret
= create_free_ino_inode(root
, trans
, path
);
446 BTRFS_I(inode
)->generation
= 0;
447 ret
= btrfs_update_inode(trans
, root
, inode
);
449 btrfs_abort_transaction(trans
, ret
);
453 if (i_size_read(inode
) > 0) {
454 ret
= btrfs_truncate_free_space_cache(trans
, NULL
, inode
);
457 btrfs_abort_transaction(trans
, ret
);
462 spin_lock(&root
->ino_cache_lock
);
463 if (root
->ino_cache_state
!= BTRFS_CACHE_FINISHED
) {
465 spin_unlock(&root
->ino_cache_lock
);
468 spin_unlock(&root
->ino_cache_lock
);
470 spin_lock(&ctl
->tree_lock
);
471 prealloc
= sizeof(struct btrfs_free_space
) * ctl
->free_extents
;
472 prealloc
= ALIGN(prealloc
, PAGE_SIZE
);
473 prealloc
+= ctl
->total_bitmaps
* PAGE_SIZE
;
474 spin_unlock(&ctl
->tree_lock
);
476 /* Just to make sure we have enough space */
477 prealloc
+= 8 * PAGE_SIZE
;
479 ret
= btrfs_delalloc_reserve_space(inode
, &data_reserved
, 0, prealloc
);
483 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, prealloc
,
484 prealloc
, prealloc
, &alloc_hint
);
486 btrfs_delalloc_release_extents(BTRFS_I(inode
), prealloc
, true);
490 ret
= btrfs_write_out_ino_cache(root
, trans
, path
, inode
);
491 btrfs_delalloc_release_extents(BTRFS_I(inode
), prealloc
, false);
495 trace_btrfs_space_reservation(fs_info
, "ino_cache", trans
->transid
,
496 trans
->bytes_reserved
, 0);
497 btrfs_block_rsv_release(fs_info
, trans
->block_rsv
,
498 trans
->bytes_reserved
);
500 trans
->block_rsv
= rsv
;
501 trans
->bytes_reserved
= num_bytes
;
503 btrfs_free_path(path
);
504 extent_changeset_free(data_reserved
);
508 int btrfs_find_highest_objectid(struct btrfs_root
*root
, u64
*objectid
)
510 struct btrfs_path
*path
;
512 struct extent_buffer
*l
;
513 struct btrfs_key search_key
;
514 struct btrfs_key found_key
;
517 path
= btrfs_alloc_path();
521 search_key
.objectid
= BTRFS_LAST_FREE_OBJECTID
;
522 search_key
.type
= -1;
523 search_key
.offset
= (u64
)-1;
524 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 0);
527 BUG_ON(ret
== 0); /* Corruption */
528 if (path
->slots
[0] > 0) {
529 slot
= path
->slots
[0] - 1;
531 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
532 *objectid
= max_t(u64
, found_key
.objectid
,
533 BTRFS_FIRST_FREE_OBJECTID
- 1);
535 *objectid
= BTRFS_FIRST_FREE_OBJECTID
- 1;
539 btrfs_free_path(path
);
543 int btrfs_find_free_objectid(struct btrfs_root
*root
, u64
*objectid
)
546 mutex_lock(&root
->objectid_mutex
);
548 if (unlikely(root
->highest_objectid
>= BTRFS_LAST_FREE_OBJECTID
)) {
549 btrfs_warn(root
->fs_info
,
550 "the objectid of root %llu reaches its highest value",
551 root
->root_key
.objectid
);
556 *objectid
= ++root
->highest_objectid
;
559 mutex_unlock(&root
->objectid_mutex
);