]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/btrfs/inode-map.c
Btrfs: add mount -o inode_cache
[mirror_ubuntu-jammy-kernel.git] / fs / btrfs / inode-map.c
CommitLineData
6cbd5570
CM
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
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.
7 *
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.
12 *
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.
17 */
18
581bb050
LZ
19#include <linux/delay.h>
20#include <linux/kthread.h>
21#include <linux/pagemap.h>
22
9f5fae2f
CM
23#include "ctree.h"
24#include "disk-io.h"
581bb050
LZ
25#include "free-space-cache.h"
26#include "inode-map.h"
9f5fae2f
CM
27#include "transaction.h"
28
581bb050
LZ
29static int caching_kthread(void *data)
30{
31 struct btrfs_root *root = data;
32 struct btrfs_fs_info *fs_info = root->fs_info;
33 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
34 struct btrfs_key key;
35 struct btrfs_path *path;
36 struct extent_buffer *leaf;
37 u64 last = (u64)-1;
38 int slot;
39 int ret;
40
4b9465cb
CM
41 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
42 return 0;
43
581bb050
LZ
44 path = btrfs_alloc_path();
45 if (!path)
46 return -ENOMEM;
47
48 /* Since the commit root is read-only, we can safely skip locking. */
49 path->skip_locking = 1;
50 path->search_commit_root = 1;
51 path->reada = 2;
52
53 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
54 key.offset = 0;
55 key.type = BTRFS_INODE_ITEM_KEY;
56again:
57 /* need to make sure the commit_root doesn't disappear */
58 mutex_lock(&root->fs_commit_mutex);
59
60 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
61 if (ret < 0)
62 goto out;
63
64 while (1) {
65 smp_mb();
a47d6b70 66 if (fs_info->closing)
581bb050
LZ
67 goto out;
68
69 leaf = path->nodes[0];
70 slot = path->slots[0];
a47d6b70 71 if (slot >= btrfs_header_nritems(leaf)) {
581bb050
LZ
72 ret = btrfs_next_leaf(root, path);
73 if (ret < 0)
74 goto out;
75 else if (ret > 0)
76 break;
77
78 if (need_resched() ||
79 btrfs_transaction_in_commit(fs_info)) {
80 leaf = path->nodes[0];
81
82 if (btrfs_header_nritems(leaf) == 0) {
83 WARN_ON(1);
84 break;
85 }
86
87 /*
88 * Save the key so we can advances forward
89 * in the next search.
90 */
91 btrfs_item_key_to_cpu(leaf, &key, 0);
945d8962 92 btrfs_release_path(path);
581bb050
LZ
93 root->cache_progress = last;
94 mutex_unlock(&root->fs_commit_mutex);
95 schedule_timeout(1);
96 goto again;
97 } else
98 continue;
99 }
100
101 btrfs_item_key_to_cpu(leaf, &key, slot);
102
103 if (key.type != BTRFS_INODE_ITEM_KEY)
104 goto next;
105
a47d6b70 106 if (key.objectid >= root->highest_objectid)
581bb050
LZ
107 break;
108
109 if (last != (u64)-1 && last + 1 != key.objectid) {
110 __btrfs_add_free_space(ctl, last + 1,
111 key.objectid - last - 1);
112 wake_up(&root->cache_wait);
113 }
114
115 last = key.objectid;
116next:
117 path->slots[0]++;
118 }
119
a47d6b70 120 if (last < root->highest_objectid - 1) {
581bb050 121 __btrfs_add_free_space(ctl, last + 1,
a47d6b70 122 root->highest_objectid - last - 1);
581bb050
LZ
123 }
124
125 spin_lock(&root->cache_lock);
126 root->cached = BTRFS_CACHE_FINISHED;
127 spin_unlock(&root->cache_lock);
128
129 root->cache_progress = (u64)-1;
130 btrfs_unpin_free_ino(root);
131out:
132 wake_up(&root->cache_wait);
133 mutex_unlock(&root->fs_commit_mutex);
134
135 btrfs_free_path(path);
136
137 return ret;
138}
139
140static void start_caching(struct btrfs_root *root)
141{
a47d6b70 142 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
581bb050 143 struct task_struct *tsk;
82d5902d 144 int ret;
a47d6b70 145 u64 objectid;
581bb050 146
4b9465cb
CM
147 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
148 return;
149
581bb050
LZ
150 spin_lock(&root->cache_lock);
151 if (root->cached != BTRFS_CACHE_NO) {
152 spin_unlock(&root->cache_lock);
153 return;
154 }
155
156 root->cached = BTRFS_CACHE_STARTED;
157 spin_unlock(&root->cache_lock);
158
82d5902d
LZ
159 ret = load_free_ino_cache(root->fs_info, root);
160 if (ret == 1) {
161 spin_lock(&root->cache_lock);
162 root->cached = BTRFS_CACHE_FINISHED;
163 spin_unlock(&root->cache_lock);
164 return;
165 }
166
a47d6b70
LZ
167 /*
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].
173 */
174 ret = btrfs_find_free_objectid(root, &objectid);
175 if (!ret && objectid <= BTRFS_LAST_FREE_OBJECTID) {
176 __btrfs_add_free_space(ctl, objectid,
177 BTRFS_LAST_FREE_OBJECTID - objectid + 1);
178 }
179
581bb050
LZ
180 tsk = kthread_run(caching_kthread, root, "btrfs-ino-cache-%llu\n",
181 root->root_key.objectid);
182 BUG_ON(IS_ERR(tsk));
183}
184
185int btrfs_find_free_ino(struct btrfs_root *root, u64 *objectid)
186{
4b9465cb
CM
187 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
188 return btrfs_find_free_objectid(root, objectid);
189
581bb050
LZ
190again:
191 *objectid = btrfs_find_ino_for_alloc(root);
192
193 if (*objectid != 0)
194 return 0;
195
196 start_caching(root);
197
198 wait_event(root->cache_wait,
199 root->cached == BTRFS_CACHE_FINISHED ||
200 root->free_ino_ctl->free_space > 0);
201
202 if (root->cached == BTRFS_CACHE_FINISHED &&
203 root->free_ino_ctl->free_space == 0)
204 return -ENOSPC;
205 else
206 goto again;
207}
208
209void btrfs_return_ino(struct btrfs_root *root, u64 objectid)
210{
211 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
212 struct btrfs_free_space_ctl *pinned = root->free_ino_pinned;
4b9465cb
CM
213
214 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
215 return;
216
581bb050
LZ
217again:
218 if (root->cached == BTRFS_CACHE_FINISHED) {
219 __btrfs_add_free_space(ctl, objectid, 1);
220 } else {
221 /*
222 * If we are in the process of caching free ino chunks,
223 * to avoid adding the same inode number to the free_ino
224 * tree twice due to cross transaction, we'll leave it
225 * in the pinned tree until a transaction is committed
226 * or the caching work is done.
227 */
228
229 mutex_lock(&root->fs_commit_mutex);
230 spin_lock(&root->cache_lock);
231 if (root->cached == BTRFS_CACHE_FINISHED) {
232 spin_unlock(&root->cache_lock);
233 mutex_unlock(&root->fs_commit_mutex);
234 goto again;
235 }
236 spin_unlock(&root->cache_lock);
237
238 start_caching(root);
239
a47d6b70
LZ
240 if (objectid <= root->cache_progress ||
241 objectid > root->highest_objectid)
581bb050
LZ
242 __btrfs_add_free_space(ctl, objectid, 1);
243 else
244 __btrfs_add_free_space(pinned, objectid, 1);
245
246 mutex_unlock(&root->fs_commit_mutex);
247 }
248}
249
250/*
251 * When a transaction is committed, we'll move those inode numbers which
252 * are smaller than root->cache_progress from pinned tree to free_ino tree,
253 * and others will just be dropped, because the commit root we were
254 * searching has changed.
255 *
256 * Must be called with root->fs_commit_mutex held
257 */
258void btrfs_unpin_free_ino(struct btrfs_root *root)
259{
260 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
261 struct rb_root *rbroot = &root->free_ino_pinned->free_space_offset;
262 struct btrfs_free_space *info;
263 struct rb_node *n;
264 u64 count;
265
4b9465cb
CM
266 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
267 return;
268
581bb050
LZ
269 while (1) {
270 n = rb_first(rbroot);
271 if (!n)
272 break;
273
274 info = rb_entry(n, struct btrfs_free_space, offset_index);
275 BUG_ON(info->bitmap);
276
277 if (info->offset > root->cache_progress)
278 goto free;
279 else if (info->offset + info->bytes > root->cache_progress)
280 count = root->cache_progress - info->offset + 1;
281 else
282 count = info->bytes;
283
284 __btrfs_add_free_space(ctl, info->offset, count);
285free:
286 rb_erase(&info->offset_index, rbroot);
287 kfree(info);
288 }
289}
290
291#define INIT_THRESHOLD (((1024 * 32) / 2) / sizeof(struct btrfs_free_space))
292#define INODES_PER_BITMAP (PAGE_CACHE_SIZE * 8)
293
294/*
295 * The goal is to keep the memory used by the free_ino tree won't
296 * exceed the memory if we use bitmaps only.
297 */
298static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
299{
300 struct btrfs_free_space *info;
301 struct rb_node *n;
302 int max_ino;
303 int max_bitmaps;
304
305 n = rb_last(&ctl->free_space_offset);
306 if (!n) {
307 ctl->extents_thresh = INIT_THRESHOLD;
308 return;
309 }
310 info = rb_entry(n, struct btrfs_free_space, offset_index);
311
312 /*
313 * Find the maximum inode number in the filesystem. Note we
314 * ignore the fact that this can be a bitmap, because we are
315 * not doing precise calculation.
316 */
317 max_ino = info->bytes - 1;
318
319 max_bitmaps = ALIGN(max_ino, INODES_PER_BITMAP) / INODES_PER_BITMAP;
320 if (max_bitmaps <= ctl->total_bitmaps) {
321 ctl->extents_thresh = 0;
322 return;
323 }
324
325 ctl->extents_thresh = (max_bitmaps - ctl->total_bitmaps) *
326 PAGE_CACHE_SIZE / sizeof(*info);
327}
328
329/*
330 * We don't fall back to bitmap, if we are below the extents threshold
331 * or this chunk of inode numbers is a big one.
332 */
333static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
334 struct btrfs_free_space *info)
335{
336 if (ctl->free_extents < ctl->extents_thresh ||
337 info->bytes > INODES_PER_BITMAP / 10)
338 return false;
339
340 return true;
341}
342
343static struct btrfs_free_space_op free_ino_op = {
344 .recalc_thresholds = recalculate_thresholds,
345 .use_bitmap = use_bitmap,
346};
347
348static void pinned_recalc_thresholds(struct btrfs_free_space_ctl *ctl)
349{
350}
351
352static bool pinned_use_bitmap(struct btrfs_free_space_ctl *ctl,
353 struct btrfs_free_space *info)
354{
355 /*
356 * We always use extents for two reasons:
357 *
358 * - The pinned tree is only used during the process of caching
359 * work.
360 * - Make code simpler. See btrfs_unpin_free_ino().
361 */
362 return false;
363}
364
365static struct btrfs_free_space_op pinned_free_ino_op = {
366 .recalc_thresholds = pinned_recalc_thresholds,
367 .use_bitmap = pinned_use_bitmap,
368};
369
370void btrfs_init_free_ino_ctl(struct btrfs_root *root)
371{
372 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
373 struct btrfs_free_space_ctl *pinned = root->free_ino_pinned;
374
375 spin_lock_init(&ctl->tree_lock);
376 ctl->unit = 1;
377 ctl->start = 0;
378 ctl->private = NULL;
379 ctl->op = &free_ino_op;
380
381 /*
382 * Initially we allow to use 16K of ram to cache chunks of
383 * inode numbers before we resort to bitmaps. This is somewhat
384 * arbitrary, but it will be adjusted in runtime.
385 */
386 ctl->extents_thresh = INIT_THRESHOLD;
387
388 spin_lock_init(&pinned->tree_lock);
389 pinned->unit = 1;
390 pinned->start = 0;
391 pinned->private = NULL;
392 pinned->extents_thresh = 0;
393 pinned->op = &pinned_free_ino_op;
394}
395
82d5902d
LZ
396int btrfs_save_ino_cache(struct btrfs_root *root,
397 struct btrfs_trans_handle *trans)
398{
399 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
400 struct btrfs_path *path;
401 struct inode *inode;
402 u64 alloc_hint = 0;
403 int ret;
404 int prealloc;
405 bool retry = false;
406
ca456ae2 407 /* only fs tree and subvol/snap needs ino cache */
408 if (root->root_key.objectid != BTRFS_FS_TREE_OBJECTID &&
409 (root->root_key.objectid < BTRFS_FIRST_FREE_OBJECTID ||
410 root->root_key.objectid > BTRFS_LAST_FREE_OBJECTID))
411 return 0;
412
d132a538
JB
413 /* Don't save inode cache if we are deleting this root */
414 if (btrfs_root_refs(&root->root_item) == 0 &&
415 root != root->fs_info->tree_root)
416 return 0;
417
4b9465cb
CM
418 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
419 return 0;
420
82d5902d
LZ
421 path = btrfs_alloc_path();
422 if (!path)
423 return -ENOMEM;
4b9465cb 424
82d5902d
LZ
425again:
426 inode = lookup_free_ino_inode(root, path);
427 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
428 ret = PTR_ERR(inode);
429 goto out;
430 }
431
432 if (IS_ERR(inode)) {
433 BUG_ON(retry);
434 retry = true;
435
436 ret = create_free_ino_inode(root, trans, path);
437 if (ret)
438 goto out;
439 goto again;
440 }
441
442 BTRFS_I(inode)->generation = 0;
443 ret = btrfs_update_inode(trans, root, inode);
444 WARN_ON(ret);
445
446 if (i_size_read(inode) > 0) {
447 ret = btrfs_truncate_free_space_cache(root, trans, path, inode);
448 if (ret)
449 goto out_put;
450 }
451
452 spin_lock(&root->cache_lock);
453 if (root->cached != BTRFS_CACHE_FINISHED) {
454 ret = -1;
455 spin_unlock(&root->cache_lock);
456 goto out_put;
457 }
458 spin_unlock(&root->cache_lock);
459
460 spin_lock(&ctl->tree_lock);
461 prealloc = sizeof(struct btrfs_free_space) * ctl->free_extents;
462 prealloc = ALIGN(prealloc, PAGE_CACHE_SIZE);
463 prealloc += ctl->total_bitmaps * PAGE_CACHE_SIZE;
464 spin_unlock(&ctl->tree_lock);
465
466 /* Just to make sure we have enough space */
467 prealloc += 8 * PAGE_CACHE_SIZE;
468
469 ret = btrfs_check_data_free_space(inode, prealloc);
470 if (ret)
471 goto out_put;
472
473 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, prealloc,
474 prealloc, prealloc, &alloc_hint);
475 if (ret)
476 goto out_put;
477 btrfs_free_reserved_data_space(inode, prealloc);
478
479out_put:
480 iput(inode);
481out:
482 if (ret == 0)
483 ret = btrfs_write_out_ino_cache(root, trans, path);
484
485 btrfs_free_path(path);
486 return ret;
487}
488
581bb050 489static int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid)
5be6f7f1
CM
490{
491 struct btrfs_path *path;
492 int ret;
5f39d397 493 struct extent_buffer *l;
5be6f7f1 494 struct btrfs_key search_key;
5f39d397 495 struct btrfs_key found_key;
5be6f7f1
CM
496 int slot;
497
498 path = btrfs_alloc_path();
db5b493a
TI
499 if (!path)
500 return -ENOMEM;
5be6f7f1 501
6527cdbe
ZY
502 search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
503 search_key.type = -1;
5be6f7f1
CM
504 search_key.offset = (u64)-1;
505 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
506 if (ret < 0)
507 goto error;
508 BUG_ON(ret == 0);
509 if (path->slots[0] > 0) {
510 slot = path->slots[0] - 1;
5f39d397
CM
511 l = path->nodes[0];
512 btrfs_item_key_to_cpu(l, &found_key, slot);
13a8a7c8
YZ
513 *objectid = max_t(u64, found_key.objectid,
514 BTRFS_FIRST_FREE_OBJECTID - 1);
5be6f7f1 515 } else {
13a8a7c8 516 *objectid = BTRFS_FIRST_FREE_OBJECTID - 1;
5be6f7f1
CM
517 }
518 ret = 0;
519error:
520 btrfs_free_path(path);
521 return ret;
522}
523
581bb050 524int btrfs_find_free_objectid(struct btrfs_root *root, u64 *objectid)
9f5fae2f 525{
9f5fae2f 526 int ret;
a2135011 527 mutex_lock(&root->objectid_mutex);
9f5fae2f 528
13a8a7c8 529 if (unlikely(root->highest_objectid < BTRFS_FIRST_FREE_OBJECTID)) {
581bb050
LZ
530 ret = btrfs_find_highest_objectid(root,
531 &root->highest_objectid);
13a8a7c8
YZ
532 if (ret)
533 goto out;
534 }
5b21f2ed 535
13a8a7c8
YZ
536 if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
537 ret = -ENOSPC;
538 goto out;
9f5fae2f 539 }
13a8a7c8
YZ
540
541 *objectid = ++root->highest_objectid;
542 ret = 0;
543out:
a2135011 544 mutex_unlock(&root->objectid_mutex);
9f5fae2f
CM
545 return ret;
546}