2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/delay.h>
20 #include <linux/kthread.h>
21 #include <linux/pagemap.h>
25 #include "free-space-cache.h"
26 #include "inode-map.h"
27 #include "transaction.h"
29 static void fail_caching_thread(struct btrfs_root
*root
)
31 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
33 btrfs_warn(fs_info
, "failed to start inode caching task");
34 btrfs_clear_pending_and_info(fs_info
, INODE_MAP_CACHE
,
35 "disabling inode map caching");
36 spin_lock(&root
->ino_cache_lock
);
37 root
->ino_cache_state
= BTRFS_CACHE_ERROR
;
38 spin_unlock(&root
->ino_cache_lock
);
39 wake_up(&root
->ino_cache_wait
);
42 static int caching_kthread(void *data
)
44 struct btrfs_root
*root
= data
;
45 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
46 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
48 struct btrfs_path
*path
;
49 struct extent_buffer
*leaf
;
54 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
57 path
= btrfs_alloc_path();
59 fail_caching_thread(root
);
63 /* Since the commit root is read-only, we can safely skip locking. */
64 path
->skip_locking
= 1;
65 path
->search_commit_root
= 1;
66 path
->reada
= READA_FORWARD
;
68 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
70 key
.type
= BTRFS_INODE_ITEM_KEY
;
72 /* need to make sure the commit_root doesn't disappear */
73 down_read(&fs_info
->commit_root_sem
);
75 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
80 if (btrfs_fs_closing(fs_info
))
83 leaf
= path
->nodes
[0];
84 slot
= path
->slots
[0];
85 if (slot
>= btrfs_header_nritems(leaf
)) {
86 ret
= btrfs_next_leaf(root
, path
);
93 btrfs_transaction_in_commit(fs_info
)) {
94 leaf
= path
->nodes
[0];
96 if (WARN_ON(btrfs_header_nritems(leaf
) == 0))
100 * Save the key so we can advances forward
101 * in the next search.
103 btrfs_item_key_to_cpu(leaf
, &key
, 0);
104 btrfs_release_path(path
);
105 root
->ino_cache_progress
= last
;
106 up_read(&fs_info
->commit_root_sem
);
113 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
115 if (key
.type
!= BTRFS_INODE_ITEM_KEY
)
118 if (key
.objectid
>= root
->highest_objectid
)
121 if (last
!= (u64
)-1 && last
+ 1 != key
.objectid
) {
122 __btrfs_add_free_space(fs_info
, ctl
, last
+ 1,
123 key
.objectid
- last
- 1);
124 wake_up(&root
->ino_cache_wait
);
132 if (last
< root
->highest_objectid
- 1) {
133 __btrfs_add_free_space(fs_info
, ctl
, last
+ 1,
134 root
->highest_objectid
- last
- 1);
137 spin_lock(&root
->ino_cache_lock
);
138 root
->ino_cache_state
= BTRFS_CACHE_FINISHED
;
139 spin_unlock(&root
->ino_cache_lock
);
141 root
->ino_cache_progress
= (u64
)-1;
142 btrfs_unpin_free_ino(root
);
144 wake_up(&root
->ino_cache_wait
);
145 up_read(&fs_info
->commit_root_sem
);
147 btrfs_free_path(path
);
152 static void start_caching(struct btrfs_root
*root
)
154 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
155 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
156 struct task_struct
*tsk
;
160 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
163 spin_lock(&root
->ino_cache_lock
);
164 if (root
->ino_cache_state
!= BTRFS_CACHE_NO
) {
165 spin_unlock(&root
->ino_cache_lock
);
169 root
->ino_cache_state
= BTRFS_CACHE_STARTED
;
170 spin_unlock(&root
->ino_cache_lock
);
172 ret
= load_free_ino_cache(fs_info
, root
);
174 spin_lock(&root
->ino_cache_lock
);
175 root
->ino_cache_state
= BTRFS_CACHE_FINISHED
;
176 spin_unlock(&root
->ino_cache_lock
);
177 wake_up(&root
->ino_cache_wait
);
182 * It can be quite time-consuming to fill the cache by searching
183 * through the extent tree, and this can keep ino allocation path
184 * waiting. Therefore at start we quickly find out the highest
185 * inode number and we know we can use inode numbers which fall in
186 * [highest_ino + 1, BTRFS_LAST_FREE_OBJECTID].
188 ret
= btrfs_find_free_objectid(root
, &objectid
);
189 if (!ret
&& objectid
<= BTRFS_LAST_FREE_OBJECTID
) {
190 __btrfs_add_free_space(fs_info
, ctl
, objectid
,
191 BTRFS_LAST_FREE_OBJECTID
- objectid
+ 1);
194 tsk
= kthread_run(caching_kthread
, root
, "btrfs-ino-cache-%llu",
195 root
->root_key
.objectid
);
197 fail_caching_thread(root
);
200 int btrfs_find_free_ino(struct btrfs_root
*root
, u64
*objectid
)
202 if (!btrfs_test_opt(root
->fs_info
, INODE_MAP_CACHE
))
203 return btrfs_find_free_objectid(root
, objectid
);
206 *objectid
= btrfs_find_ino_for_alloc(root
);
213 wait_event(root
->ino_cache_wait
,
214 root
->ino_cache_state
== BTRFS_CACHE_FINISHED
||
215 root
->ino_cache_state
== BTRFS_CACHE_ERROR
||
216 root
->free_ino_ctl
->free_space
> 0);
218 if (root
->ino_cache_state
== BTRFS_CACHE_FINISHED
&&
219 root
->free_ino_ctl
->free_space
== 0)
221 else if (root
->ino_cache_state
== BTRFS_CACHE_ERROR
)
222 return btrfs_find_free_objectid(root
, objectid
);
227 void btrfs_return_ino(struct btrfs_root
*root
, u64 objectid
)
229 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
230 struct btrfs_free_space_ctl
*pinned
= root
->free_ino_pinned
;
232 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
235 if (root
->ino_cache_state
== BTRFS_CACHE_FINISHED
) {
236 __btrfs_add_free_space(fs_info
, pinned
, objectid
, 1);
238 down_write(&fs_info
->commit_root_sem
);
239 spin_lock(&root
->ino_cache_lock
);
240 if (root
->ino_cache_state
== BTRFS_CACHE_FINISHED
) {
241 spin_unlock(&root
->ino_cache_lock
);
242 up_write(&fs_info
->commit_root_sem
);
245 spin_unlock(&root
->ino_cache_lock
);
249 __btrfs_add_free_space(fs_info
, pinned
, objectid
, 1);
251 up_write(&fs_info
->commit_root_sem
);
256 * When a transaction is committed, we'll move those inode numbers which are
257 * smaller than root->ino_cache_progress from pinned tree to free_ino tree, and
258 * others will just be dropped, because the commit root we were searching has
261 * Must be called with root->fs_info->commit_root_sem held
263 void btrfs_unpin_free_ino(struct btrfs_root
*root
)
265 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
266 struct rb_root
*rbroot
= &root
->free_ino_pinned
->free_space_offset
;
267 spinlock_t
*rbroot_lock
= &root
->free_ino_pinned
->tree_lock
;
268 struct btrfs_free_space
*info
;
272 if (!btrfs_test_opt(root
->fs_info
, INODE_MAP_CACHE
))
276 bool add_to_ctl
= true;
278 spin_lock(rbroot_lock
);
279 n
= rb_first(rbroot
);
281 spin_unlock(rbroot_lock
);
285 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
286 BUG_ON(info
->bitmap
); /* Logic error */
288 if (info
->offset
> root
->ino_cache_progress
)
290 else if (info
->offset
+ info
->bytes
> root
->ino_cache_progress
)
291 count
= root
->ino_cache_progress
- info
->offset
+ 1;
295 rb_erase(&info
->offset_index
, rbroot
);
296 spin_unlock(rbroot_lock
);
298 __btrfs_add_free_space(root
->fs_info
, ctl
,
299 info
->offset
, count
);
300 kmem_cache_free(btrfs_free_space_cachep
, info
);
304 #define INIT_THRESHOLD ((SZ_32K / 2) / sizeof(struct btrfs_free_space))
305 #define INODES_PER_BITMAP (PAGE_SIZE * 8)
308 * The goal is to keep the memory used by the free_ino tree won't
309 * exceed the memory if we use bitmaps only.
311 static void recalculate_thresholds(struct btrfs_free_space_ctl
*ctl
)
313 struct btrfs_free_space
*info
;
318 n
= rb_last(&ctl
->free_space_offset
);
320 ctl
->extents_thresh
= INIT_THRESHOLD
;
323 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
326 * Find the maximum inode number in the filesystem. Note we
327 * ignore the fact that this can be a bitmap, because we are
328 * not doing precise calculation.
330 max_ino
= info
->bytes
- 1;
332 max_bitmaps
= ALIGN(max_ino
, INODES_PER_BITMAP
) / INODES_PER_BITMAP
;
333 if (max_bitmaps
<= ctl
->total_bitmaps
) {
334 ctl
->extents_thresh
= 0;
338 ctl
->extents_thresh
= (max_bitmaps
- ctl
->total_bitmaps
) *
339 PAGE_SIZE
/ sizeof(*info
);
343 * We don't fall back to bitmap, if we are below the extents threshold
344 * or this chunk of inode numbers is a big one.
346 static bool use_bitmap(struct btrfs_free_space_ctl
*ctl
,
347 struct btrfs_free_space
*info
)
349 if (ctl
->free_extents
< ctl
->extents_thresh
||
350 info
->bytes
> INODES_PER_BITMAP
/ 10)
356 static const struct btrfs_free_space_op free_ino_op
= {
357 .recalc_thresholds
= recalculate_thresholds
,
358 .use_bitmap
= use_bitmap
,
361 static void pinned_recalc_thresholds(struct btrfs_free_space_ctl
*ctl
)
365 static bool pinned_use_bitmap(struct btrfs_free_space_ctl
*ctl
,
366 struct btrfs_free_space
*info
)
369 * We always use extents for two reasons:
371 * - The pinned tree is only used during the process of caching
373 * - Make code simpler. See btrfs_unpin_free_ino().
378 static const struct btrfs_free_space_op pinned_free_ino_op
= {
379 .recalc_thresholds
= pinned_recalc_thresholds
,
380 .use_bitmap
= pinned_use_bitmap
,
383 void btrfs_init_free_ino_ctl(struct btrfs_root
*root
)
385 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
386 struct btrfs_free_space_ctl
*pinned
= root
->free_ino_pinned
;
388 spin_lock_init(&ctl
->tree_lock
);
392 ctl
->op
= &free_ino_op
;
393 INIT_LIST_HEAD(&ctl
->trimming_ranges
);
394 mutex_init(&ctl
->cache_writeout_mutex
);
397 * Initially we allow to use 16K of ram to cache chunks of
398 * inode numbers before we resort to bitmaps. This is somewhat
399 * arbitrary, but it will be adjusted in runtime.
401 ctl
->extents_thresh
= INIT_THRESHOLD
;
403 spin_lock_init(&pinned
->tree_lock
);
406 pinned
->private = NULL
;
407 pinned
->extents_thresh
= 0;
408 pinned
->op
= &pinned_free_ino_op
;
411 int btrfs_save_ino_cache(struct btrfs_root
*root
,
412 struct btrfs_trans_handle
*trans
)
414 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
415 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
416 struct btrfs_path
*path
;
418 struct btrfs_block_rsv
*rsv
;
419 struct extent_changeset
*data_reserved
= NULL
;
426 /* only fs tree and subvol/snap needs ino cache */
427 if (root
->root_key
.objectid
!= BTRFS_FS_TREE_OBJECTID
&&
428 (root
->root_key
.objectid
< BTRFS_FIRST_FREE_OBJECTID
||
429 root
->root_key
.objectid
> BTRFS_LAST_FREE_OBJECTID
))
432 /* Don't save inode cache if we are deleting this root */
433 if (btrfs_root_refs(&root
->root_item
) == 0)
436 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
439 path
= btrfs_alloc_path();
443 rsv
= trans
->block_rsv
;
444 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
446 num_bytes
= trans
->bytes_reserved
;
448 * 1 item for inode item insertion if need
449 * 4 items for inode item update (in the worst case)
450 * 1 items for slack space if we need do truncation
451 * 1 item for free space object
452 * 3 items for pre-allocation
454 trans
->bytes_reserved
= btrfs_calc_trans_metadata_size(fs_info
, 10);
455 ret
= btrfs_block_rsv_add(root
, trans
->block_rsv
,
456 trans
->bytes_reserved
,
457 BTRFS_RESERVE_NO_FLUSH
);
460 trace_btrfs_space_reservation(fs_info
, "ino_cache", trans
->transid
,
461 trans
->bytes_reserved
, 1);
463 inode
= lookup_free_ino_inode(root
, path
);
464 if (IS_ERR(inode
) && (PTR_ERR(inode
) != -ENOENT
|| retry
)) {
465 ret
= PTR_ERR(inode
);
470 BUG_ON(retry
); /* Logic error */
473 ret
= create_free_ino_inode(root
, trans
, path
);
479 BTRFS_I(inode
)->generation
= 0;
480 ret
= btrfs_update_inode(trans
, root
, inode
);
482 btrfs_abort_transaction(trans
, ret
);
486 if (i_size_read(inode
) > 0) {
487 ret
= btrfs_truncate_free_space_cache(trans
, NULL
, inode
);
490 btrfs_abort_transaction(trans
, ret
);
495 spin_lock(&root
->ino_cache_lock
);
496 if (root
->ino_cache_state
!= BTRFS_CACHE_FINISHED
) {
498 spin_unlock(&root
->ino_cache_lock
);
501 spin_unlock(&root
->ino_cache_lock
);
503 spin_lock(&ctl
->tree_lock
);
504 prealloc
= sizeof(struct btrfs_free_space
) * ctl
->free_extents
;
505 prealloc
= ALIGN(prealloc
, PAGE_SIZE
);
506 prealloc
+= ctl
->total_bitmaps
* PAGE_SIZE
;
507 spin_unlock(&ctl
->tree_lock
);
509 /* Just to make sure we have enough space */
510 prealloc
+= 8 * PAGE_SIZE
;
512 ret
= btrfs_delalloc_reserve_space(inode
, &data_reserved
, 0, prealloc
);
516 ret
= btrfs_prealloc_file_range_trans(inode
, trans
, 0, 0, prealloc
,
517 prealloc
, prealloc
, &alloc_hint
);
519 btrfs_delalloc_release_metadata(BTRFS_I(inode
), prealloc
);
523 ret
= btrfs_write_out_ino_cache(root
, trans
, path
, inode
);
527 trace_btrfs_space_reservation(fs_info
, "ino_cache", trans
->transid
,
528 trans
->bytes_reserved
, 0);
529 btrfs_block_rsv_release(fs_info
, trans
->block_rsv
,
530 trans
->bytes_reserved
);
532 trans
->block_rsv
= rsv
;
533 trans
->bytes_reserved
= num_bytes
;
535 btrfs_free_path(path
);
536 extent_changeset_free(data_reserved
);
540 int btrfs_find_highest_objectid(struct btrfs_root
*root
, u64
*objectid
)
542 struct btrfs_path
*path
;
544 struct extent_buffer
*l
;
545 struct btrfs_key search_key
;
546 struct btrfs_key found_key
;
549 path
= btrfs_alloc_path();
553 search_key
.objectid
= BTRFS_LAST_FREE_OBJECTID
;
554 search_key
.type
= -1;
555 search_key
.offset
= (u64
)-1;
556 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 0);
559 BUG_ON(ret
== 0); /* Corruption */
560 if (path
->slots
[0] > 0) {
561 slot
= path
->slots
[0] - 1;
563 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
564 *objectid
= max_t(u64
, found_key
.objectid
,
565 BTRFS_FIRST_FREE_OBJECTID
- 1);
567 *objectid
= BTRFS_FIRST_FREE_OBJECTID
- 1;
571 btrfs_free_path(path
);
575 int btrfs_find_free_objectid(struct btrfs_root
*root
, u64
*objectid
)
578 mutex_lock(&root
->objectid_mutex
);
580 if (unlikely(root
->highest_objectid
>= BTRFS_LAST_FREE_OBJECTID
)) {
581 btrfs_warn(root
->fs_info
,
582 "the objectid of root %llu reaches its highest value",
583 root
->root_key
.objectid
);
588 *objectid
= ++root
->highest_objectid
;
591 mutex_unlock(&root
->objectid_mutex
);