perf bpf: Move perf_event_output() from stdio.h to bpf.h
[linux/fpc-iii.git] / fs / btrfs / inode-map.c
blobffca2abf13d0cb241e0c0a0e12ba44c91d924ddc
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
6 #include <linux/kthread.h>
7 #include <linux/pagemap.h>
9 #include "ctree.h"
10 #include "disk-io.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;
20 struct btrfs_key key;
21 struct btrfs_path *path;
22 struct extent_buffer *leaf;
23 u64 last = (u64)-1;
24 int slot;
25 int ret;
27 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
28 return 0;
30 path = btrfs_alloc_path();
31 if (!path)
32 return -ENOMEM;
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;
40 key.offset = 0;
41 key.type = BTRFS_INODE_ITEM_KEY;
42 again:
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);
47 if (ret < 0)
48 goto out;
50 while (1) {
51 if (btrfs_fs_closing(fs_info))
52 goto out;
54 leaf = path->nodes[0];
55 slot = path->slots[0];
56 if (slot >= btrfs_header_nritems(leaf)) {
57 ret = btrfs_next_leaf(root, path);
58 if (ret < 0)
59 goto out;
60 else if (ret > 0)
61 break;
63 if (need_resched() ||
64 btrfs_transaction_in_commit(fs_info)) {
65 leaf = path->nodes[0];
67 if (WARN_ON(btrfs_header_nritems(leaf) == 0))
68 break;
71 * Save the key so we can advances forward
72 * in the next search.
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);
78 schedule_timeout(1);
79 goto again;
80 } else
81 continue;
84 btrfs_item_key_to_cpu(leaf, &key, slot);
86 if (key.type != BTRFS_INODE_ITEM_KEY)
87 goto next;
89 if (key.objectid >= root->highest_objectid)
90 break;
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);
98 last = key.objectid;
99 next:
100 path->slots[0]++;
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);
114 out:
115 wake_up(&root->ino_cache_wait);
116 up_read(&fs_info->commit_root_sem);
118 btrfs_free_path(path);
120 return ret;
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;
128 int ret;
129 u64 objectid;
131 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
132 return;
134 spin_lock(&root->ino_cache_lock);
135 if (root->ino_cache_state != BTRFS_CACHE_NO) {
136 spin_unlock(&root->ino_cache_lock);
137 return;
140 root->ino_cache_state = BTRFS_CACHE_STARTED;
141 spin_unlock(&root->ino_cache_lock);
143 ret = load_free_ino_cache(fs_info, root);
144 if (ret == 1) {
145 spin_lock(&root->ino_cache_lock);
146 root->ino_cache_state = BTRFS_CACHE_FINISHED;
147 spin_unlock(&root->ino_cache_lock);
148 return;
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);
166 if (IS_ERR(tsk)) {
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);
178 again:
179 *objectid = btrfs_find_ino_for_alloc(root);
181 if (*objectid != 0)
182 return 0;
184 start_caching(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)
192 return -ENOSPC;
193 else
194 goto again;
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))
203 return;
204 again:
205 if (root->ino_cache_state == BTRFS_CACHE_FINISHED) {
206 __btrfs_add_free_space(fs_info, pinned, objectid, 1);
207 } else {
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);
213 goto again;
215 spin_unlock(&root->ino_cache_lock);
217 start_caching(root);
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
229 * changed.
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;
239 struct rb_node *n;
240 u64 count;
242 if (!btrfs_test_opt(root->fs_info, INODE_MAP_CACHE))
243 return;
245 while (1) {
246 spin_lock(rbroot_lock);
247 n = rb_first(rbroot);
248 if (!n) {
249 spin_unlock(rbroot_lock);
250 break;
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)
257 count = 0;
258 else
259 count = min(root->ino_cache_progress - info->offset + 1,
260 info->bytes);
262 rb_erase(&info->offset_index, rbroot);
263 spin_unlock(rbroot_lock);
264 if (count)
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;
281 struct rb_node *n;
282 int max_ino;
283 int max_bitmaps;
285 n = rb_last(&ctl->free_space_offset);
286 if (!n) {
287 ctl->extents_thresh = INIT_THRESHOLD;
288 return;
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;
302 return;
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)
318 return false;
320 return true;
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
339 * work.
340 * - Make code simpler. See btrfs_unpin_free_ino().
342 return false;
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);
356 ctl->unit = 1;
357 ctl->start = 0;
358 ctl->private = NULL;
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);
371 pinned->unit = 1;
372 pinned->start = 0;
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;
384 struct inode *inode;
385 struct btrfs_block_rsv *rsv;
386 struct extent_changeset *data_reserved = NULL;
387 u64 num_bytes;
388 u64 alloc_hint = 0;
389 int ret;
390 int prealloc;
391 bool retry = false;
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))
397 return 0;
399 /* Don't save inode cache if we are deleting this root */
400 if (btrfs_root_refs(&root->root_item) == 0)
401 return 0;
403 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
404 return 0;
406 path = btrfs_alloc_path();
407 if (!path)
408 return -ENOMEM;
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);
425 if (ret)
426 goto out;
427 trace_btrfs_space_reservation(fs_info, "ino_cache", trans->transid,
428 trans->bytes_reserved, 1);
429 again:
430 inode = lookup_free_ino_inode(root, path);
431 if (IS_ERR(inode) && (PTR_ERR(inode) != -ENOENT || retry)) {
432 ret = PTR_ERR(inode);
433 goto out_release;
436 if (IS_ERR(inode)) {
437 BUG_ON(retry); /* Logic error */
438 retry = true;
440 ret = create_free_ino_inode(root, trans, path);
441 if (ret)
442 goto out_release;
443 goto again;
446 BTRFS_I(inode)->generation = 0;
447 ret = btrfs_update_inode(trans, root, inode);
448 if (ret) {
449 btrfs_abort_transaction(trans, ret);
450 goto out_put;
453 if (i_size_read(inode) > 0) {
454 ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
455 if (ret) {
456 if (ret != -ENOSPC)
457 btrfs_abort_transaction(trans, ret);
458 goto out_put;
462 spin_lock(&root->ino_cache_lock);
463 if (root->ino_cache_state != BTRFS_CACHE_FINISHED) {
464 ret = -1;
465 spin_unlock(&root->ino_cache_lock);
466 goto out_put;
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);
480 if (ret)
481 goto out_put;
483 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, prealloc,
484 prealloc, prealloc, &alloc_hint);
485 if (ret) {
486 btrfs_delalloc_release_extents(BTRFS_I(inode), prealloc, true);
487 goto out_put;
490 ret = btrfs_write_out_ino_cache(root, trans, path, inode);
491 btrfs_delalloc_release_extents(BTRFS_I(inode), prealloc, false);
492 out_put:
493 iput(inode);
494 out_release:
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);
499 out:
500 trans->block_rsv = rsv;
501 trans->bytes_reserved = num_bytes;
503 btrfs_free_path(path);
504 extent_changeset_free(data_reserved);
505 return ret;
508 int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid)
510 struct btrfs_path *path;
511 int ret;
512 struct extent_buffer *l;
513 struct btrfs_key search_key;
514 struct btrfs_key found_key;
515 int slot;
517 path = btrfs_alloc_path();
518 if (!path)
519 return -ENOMEM;
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);
525 if (ret < 0)
526 goto error;
527 BUG_ON(ret == 0); /* Corruption */
528 if (path->slots[0] > 0) {
529 slot = path->slots[0] - 1;
530 l = path->nodes[0];
531 btrfs_item_key_to_cpu(l, &found_key, slot);
532 *objectid = max_t(u64, found_key.objectid,
533 BTRFS_FIRST_FREE_OBJECTID - 1);
534 } else {
535 *objectid = BTRFS_FIRST_FREE_OBJECTID - 1;
537 ret = 0;
538 error:
539 btrfs_free_path(path);
540 return ret;
543 int btrfs_find_free_objectid(struct btrfs_root *root, u64 *objectid)
545 int ret;
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);
552 ret = -ENOSPC;
553 goto out;
556 *objectid = ++root->highest_objectid;
557 ret = 0;
558 out:
559 mutex_unlock(&root->objectid_mutex);
560 return ret;