inode-map.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include <linux/kthread.h>
  6. #include <linux/pagemap.h>
  7. #include "ctree.h"
  8. #include "disk-io.h"
  9. #include "free-space-cache.h"
  10. #include "inode-map.h"
  11. #include "transaction.h"
  12. static int caching_kthread(void *data)
  13. {
  14. struct btrfs_root *root = data;
  15. struct btrfs_fs_info *fs_info = root->fs_info;
  16. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  17. struct btrfs_key key;
  18. struct btrfs_path *path;
  19. struct extent_buffer *leaf;
  20. u64 last = (u64)-1;
  21. int slot;
  22. int ret;
  23. if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
  24. return 0;
  25. path = btrfs_alloc_path();
  26. if (!path)
  27. return -ENOMEM;
  28. /* Since the commit root is read-only, we can safely skip locking. */
  29. path->skip_locking = 1;
  30. path->search_commit_root = 1;
  31. path->reada = READA_FORWARD;
  32. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  33. key.offset = 0;
  34. key.type = BTRFS_INODE_ITEM_KEY;
  35. again:
  36. /* need to make sure the commit_root doesn't disappear */
  37. down_read(&fs_info->commit_root_sem);
  38. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  39. if (ret < 0)
  40. goto out;
  41. while (1) {
  42. if (btrfs_fs_closing(fs_info))
  43. goto out;
  44. leaf = path->nodes[0];
  45. slot = path->slots[0];
  46. if (slot >= btrfs_header_nritems(leaf)) {
  47. ret = btrfs_next_leaf(root, path);
  48. if (ret < 0)
  49. goto out;
  50. else if (ret > 0)
  51. break;
  52. if (need_resched() ||
  53. btrfs_transaction_in_commit(fs_info)) {
  54. leaf = path->nodes[0];
  55. if (WARN_ON(btrfs_header_nritems(leaf) == 0))
  56. break;
  57. /*
  58. * Save the key so we can advances forward
  59. * in the next search.
  60. */
  61. btrfs_item_key_to_cpu(leaf, &key, 0);
  62. btrfs_release_path(path);
  63. root->ino_cache_progress = last;
  64. up_read(&fs_info->commit_root_sem);
  65. schedule_timeout(1);
  66. goto again;
  67. } else
  68. continue;
  69. }
  70. btrfs_item_key_to_cpu(leaf, &key, slot);
  71. if (key.type != BTRFS_INODE_ITEM_KEY)
  72. goto next;
  73. if (key.objectid >= root->highest_objectid)
  74. break;
  75. if (last != (u64)-1 && last + 1 != key.objectid) {
  76. __btrfs_add_free_space(fs_info, ctl, last + 1,
  77. key.objectid - last - 1);
  78. wake_up(&root->ino_cache_wait);
  79. }
  80. last = key.objectid;
  81. next:
  82. path->slots[0]++;
  83. }
  84. if (last < root->highest_objectid - 1) {
  85. __btrfs_add_free_space(fs_info, ctl, last + 1,
  86. root->highest_objectid - last - 1);
  87. }
  88. spin_lock(&root->ino_cache_lock);
  89. root->ino_cache_state = BTRFS_CACHE_FINISHED;
  90. spin_unlock(&root->ino_cache_lock);
  91. root->ino_cache_progress = (u64)-1;
  92. btrfs_unpin_free_ino(root);
  93. out:
  94. wake_up(&root->ino_cache_wait);
  95. up_read(&fs_info->commit_root_sem);
  96. btrfs_free_path(path);
  97. return ret;
  98. }
  99. static void start_caching(struct btrfs_root *root)
  100. {
  101. struct btrfs_fs_info *fs_info = root->fs_info;
  102. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  103. struct task_struct *tsk;
  104. int ret;
  105. u64 objectid;
  106. if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
  107. return;
  108. spin_lock(&root->ino_cache_lock);
  109. if (root->ino_cache_state != BTRFS_CACHE_NO) {
  110. spin_unlock(&root->ino_cache_lock);
  111. return;
  112. }
  113. root->ino_cache_state = BTRFS_CACHE_STARTED;
  114. spin_unlock(&root->ino_cache_lock);
  115. ret = load_free_ino_cache(fs_info, root);
  116. if (ret == 1) {
  117. spin_lock(&root->ino_cache_lock);
  118. root->ino_cache_state = BTRFS_CACHE_FINISHED;
  119. spin_unlock(&root->ino_cache_lock);
  120. return;
  121. }
  122. /*
  123. * It can be quite time-consuming to fill the cache by searching
  124. * through the extent tree, and this can keep ino allocation path
  125. * waiting. Therefore at start we quickly find out the highest
  126. * inode number and we know we can use inode numbers which fall in
  127. * [highest_ino + 1, BTRFS_LAST_FREE_OBJECTID].
  128. */
  129. ret = btrfs_find_free_objectid(root, &objectid);
  130. if (!ret && objectid <= BTRFS_LAST_FREE_OBJECTID) {
  131. __btrfs_add_free_space(fs_info, ctl, objectid,
  132. BTRFS_LAST_FREE_OBJECTID - objectid + 1);
  133. }
  134. tsk = kthread_run(caching_kthread, root, "btrfs-ino-cache-%llu",
  135. root->root_key.objectid);
  136. if (IS_ERR(tsk)) {
  137. btrfs_warn(fs_info, "failed to start inode caching task");
  138. btrfs_clear_pending_and_info(fs_info, INODE_MAP_CACHE,
  139. "disabling inode map caching");
  140. }
  141. }
  142. int btrfs_find_free_ino(struct btrfs_root *root, u64 *objectid)
  143. {
  144. if (!btrfs_test_opt(root->fs_info, INODE_MAP_CACHE))
  145. return btrfs_find_free_objectid(root, objectid);
  146. again:
  147. *objectid = btrfs_find_ino_for_alloc(root);
  148. if (*objectid != 0)
  149. return 0;
  150. start_caching(root);
  151. wait_event(root->ino_cache_wait,
  152. root->ino_cache_state == BTRFS_CACHE_FINISHED ||
  153. root->free_ino_ctl->free_space > 0);
  154. if (root->ino_cache_state == BTRFS_CACHE_FINISHED &&
  155. root->free_ino_ctl->free_space == 0)
  156. return -ENOSPC;
  157. else
  158. goto again;
  159. }
  160. void btrfs_return_ino(struct btrfs_root *root, u64 objectid)
  161. {
  162. struct btrfs_fs_info *fs_info = root->fs_info;
  163. struct btrfs_free_space_ctl *pinned = root->free_ino_pinned;
  164. if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
  165. return;
  166. again:
  167. if (root->ino_cache_state == BTRFS_CACHE_FINISHED) {
  168. __btrfs_add_free_space(fs_info, pinned, objectid, 1);
  169. } else {
  170. down_write(&fs_info->commit_root_sem);
  171. spin_lock(&root->ino_cache_lock);
  172. if (root->ino_cache_state == BTRFS_CACHE_FINISHED) {
  173. spin_unlock(&root->ino_cache_lock);
  174. up_write(&fs_info->commit_root_sem);
  175. goto again;
  176. }
  177. spin_unlock(&root->ino_cache_lock);
  178. start_caching(root);
  179. __btrfs_add_free_space(fs_info, pinned, objectid, 1);
  180. up_write(&fs_info->commit_root_sem);
  181. }
  182. }
  183. /*
  184. * When a transaction is committed, we'll move those inode numbers which are
  185. * smaller than root->ino_cache_progress from pinned tree to free_ino tree, and
  186. * others will just be dropped, because the commit root we were searching has
  187. * changed.
  188. *
  189. * Must be called with root->fs_info->commit_root_sem held
  190. */
  191. void btrfs_unpin_free_ino(struct btrfs_root *root)
  192. {
  193. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  194. struct rb_root *rbroot = &root->free_ino_pinned->free_space_offset;
  195. spinlock_t *rbroot_lock = &root->free_ino_pinned->tree_lock;
  196. struct btrfs_free_space *info;
  197. struct rb_node *n;
  198. u64 count;
  199. if (!btrfs_test_opt(root->fs_info, INODE_MAP_CACHE))
  200. return;
  201. while (1) {
  202. spin_lock(rbroot_lock);
  203. n = rb_first(rbroot);
  204. if (!n) {
  205. spin_unlock(rbroot_lock);
  206. break;
  207. }
  208. info = rb_entry(n, struct btrfs_free_space, offset_index);
  209. BUG_ON(info->bitmap); /* Logic error */
  210. if (info->offset > root->ino_cache_progress)
  211. count = 0;
  212. else
  213. count = min(root->ino_cache_progress - info->offset + 1,
  214. info->bytes);
  215. rb_erase(&info->offset_index, rbroot);
  216. spin_unlock(rbroot_lock);
  217. if (count)
  218. __btrfs_add_free_space(root->fs_info, ctl,
  219. info->offset, count);
  220. kmem_cache_free(btrfs_free_space_cachep, info);
  221. }
  222. }
  223. #define INIT_THRESHOLD ((SZ_32K / 2) / sizeof(struct btrfs_free_space))
  224. #define INODES_PER_BITMAP (PAGE_SIZE * 8)
  225. /*
  226. * The goal is to keep the memory used by the free_ino tree won't
  227. * exceed the memory if we use bitmaps only.
  228. */
  229. static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
  230. {
  231. struct btrfs_free_space *info;
  232. struct rb_node *n;
  233. int max_ino;
  234. int max_bitmaps;
  235. n = rb_last(&ctl->free_space_offset);
  236. if (!n) {
  237. ctl->extents_thresh = INIT_THRESHOLD;
  238. return;
  239. }
  240. info = rb_entry(n, struct btrfs_free_space, offset_index);
  241. /*
  242. * Find the maximum inode number in the filesystem. Note we
  243. * ignore the fact that this can be a bitmap, because we are
  244. * not doing precise calculation.
  245. */
  246. max_ino = info->bytes - 1;
  247. max_bitmaps = ALIGN(max_ino, INODES_PER_BITMAP) / INODES_PER_BITMAP;
  248. if (max_bitmaps <= ctl->total_bitmaps) {
  249. ctl->extents_thresh = 0;
  250. return;
  251. }
  252. ctl->extents_thresh = (max_bitmaps - ctl->total_bitmaps) *
  253. PAGE_SIZE / sizeof(*info);
  254. }
  255. /*
  256. * We don't fall back to bitmap, if we are below the extents threshold
  257. * or this chunk of inode numbers is a big one.
  258. */
  259. static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
  260. struct btrfs_free_space *info)
  261. {
  262. if (ctl->free_extents < ctl->extents_thresh ||
  263. info->bytes > INODES_PER_BITMAP / 10)
  264. return false;
  265. return true;
  266. }
  267. static const struct btrfs_free_space_op free_ino_op = {
  268. .recalc_thresholds = recalculate_thresholds,
  269. .use_bitmap = use_bitmap,
  270. };
  271. static void pinned_recalc_thresholds(struct btrfs_free_space_ctl *ctl)
  272. {
  273. }
  274. static bool pinned_use_bitmap(struct btrfs_free_space_ctl *ctl,
  275. struct btrfs_free_space *info)
  276. {
  277. /*
  278. * We always use extents for two reasons:
  279. *
  280. * - The pinned tree is only used during the process of caching
  281. * work.
  282. * - Make code simpler. See btrfs_unpin_free_ino().
  283. */
  284. return false;
  285. }
  286. static const struct btrfs_free_space_op pinned_free_ino_op = {
  287. .recalc_thresholds = pinned_recalc_thresholds,
  288. .use_bitmap = pinned_use_bitmap,
  289. };
  290. void btrfs_init_free_ino_ctl(struct btrfs_root *root)
  291. {
  292. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  293. struct btrfs_free_space_ctl *pinned = root->free_ino_pinned;
  294. spin_lock_init(&ctl->tree_lock);
  295. ctl->unit = 1;
  296. ctl->start = 0;
  297. ctl->private = NULL;
  298. ctl->op = &free_ino_op;
  299. INIT_LIST_HEAD(&ctl->trimming_ranges);
  300. mutex_init(&ctl->cache_writeout_mutex);
  301. /*
  302. * Initially we allow to use 16K of ram to cache chunks of
  303. * inode numbers before we resort to bitmaps. This is somewhat
  304. * arbitrary, but it will be adjusted in runtime.
  305. */
  306. ctl->extents_thresh = INIT_THRESHOLD;
  307. spin_lock_init(&pinned->tree_lock);
  308. pinned->unit = 1;
  309. pinned->start = 0;
  310. pinned->private = NULL;
  311. pinned->extents_thresh = 0;
  312. pinned->op = &pinned_free_ino_op;
  313. }
  314. int btrfs_save_ino_cache(struct btrfs_root *root,
  315. struct btrfs_trans_handle *trans)
  316. {
  317. struct btrfs_fs_info *fs_info = root->fs_info;
  318. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  319. struct btrfs_path *path;
  320. struct inode *inode;
  321. struct btrfs_block_rsv *rsv;
  322. struct extent_changeset *data_reserved = NULL;
  323. u64 num_bytes;
  324. u64 alloc_hint = 0;
  325. int ret;
  326. int prealloc;
  327. bool retry = false;
  328. /* only fs tree and subvol/snap needs ino cache */
  329. if (root->root_key.objectid != BTRFS_FS_TREE_OBJECTID &&
  330. (root->root_key.objectid < BTRFS_FIRST_FREE_OBJECTID ||
  331. root->root_key.objectid > BTRFS_LAST_FREE_OBJECTID))
  332. return 0;
  333. /* Don't save inode cache if we are deleting this root */
  334. if (btrfs_root_refs(&root->root_item) == 0)
  335. return 0;
  336. if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
  337. return 0;
  338. path = btrfs_alloc_path();
  339. if (!path)
  340. return -ENOMEM;
  341. rsv = trans->block_rsv;
  342. trans->block_rsv = &fs_info->trans_block_rsv;
  343. num_bytes = trans->bytes_reserved;
  344. /*
  345. * 1 item for inode item insertion if need
  346. * 4 items for inode item update (in the worst case)
  347. * 1 items for slack space if we need do truncation
  348. * 1 item for free space object
  349. * 3 items for pre-allocation
  350. */
  351. trans->bytes_reserved = btrfs_calc_trans_metadata_size(fs_info, 10);
  352. ret = btrfs_block_rsv_add(root, trans->block_rsv,
  353. trans->bytes_reserved,
  354. BTRFS_RESERVE_NO_FLUSH);
  355. if (ret)
  356. goto out;
  357. trace_btrfs_space_reservation(fs_info, "ino_cache", trans->transid,
  358. trans->bytes_reserved, 1);
  359. again:
  360. inode = lookup_free_ino_inode(root, path);
  361. if (IS_ERR(inode) && (PTR_ERR(inode) != -ENOENT || retry)) {
  362. ret = PTR_ERR(inode);
  363. goto out_release;
  364. }
  365. if (IS_ERR(inode)) {
  366. BUG_ON(retry); /* Logic error */
  367. retry = true;
  368. ret = create_free_ino_inode(root, trans, path);
  369. if (ret)
  370. goto out_release;
  371. goto again;
  372. }
  373. BTRFS_I(inode)->generation = 0;
  374. ret = btrfs_update_inode(trans, root, inode);
  375. if (ret) {
  376. btrfs_abort_transaction(trans, ret);
  377. goto out_put;
  378. }
  379. if (i_size_read(inode) > 0) {
  380. ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
  381. if (ret) {
  382. if (ret != -ENOSPC)
  383. btrfs_abort_transaction(trans, ret);
  384. goto out_put;
  385. }
  386. }
  387. spin_lock(&root->ino_cache_lock);
  388. if (root->ino_cache_state != BTRFS_CACHE_FINISHED) {
  389. ret = -1;
  390. spin_unlock(&root->ino_cache_lock);
  391. goto out_put;
  392. }
  393. spin_unlock(&root->ino_cache_lock);
  394. spin_lock(&ctl->tree_lock);
  395. prealloc = sizeof(struct btrfs_free_space) * ctl->free_extents;
  396. prealloc = ALIGN(prealloc, PAGE_SIZE);
  397. prealloc += ctl->total_bitmaps * PAGE_SIZE;
  398. spin_unlock(&ctl->tree_lock);
  399. /* Just to make sure we have enough space */
  400. prealloc += 8 * PAGE_SIZE;
  401. ret = btrfs_delalloc_reserve_space(inode, &data_reserved, 0, prealloc);
  402. if (ret)
  403. goto out_put;
  404. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, prealloc,
  405. prealloc, prealloc, &alloc_hint);
  406. if (ret) {
  407. btrfs_delalloc_release_extents(BTRFS_I(inode), prealloc, true);
  408. goto out_put;
  409. }
  410. ret = btrfs_write_out_ino_cache(root, trans, path, inode);
  411. btrfs_delalloc_release_extents(BTRFS_I(inode), prealloc, false);
  412. out_put:
  413. iput(inode);
  414. out_release:
  415. trace_btrfs_space_reservation(fs_info, "ino_cache", trans->transid,
  416. trans->bytes_reserved, 0);
  417. btrfs_block_rsv_release(fs_info, trans->block_rsv,
  418. trans->bytes_reserved);
  419. out:
  420. trans->block_rsv = rsv;
  421. trans->bytes_reserved = num_bytes;
  422. btrfs_free_path(path);
  423. extent_changeset_free(data_reserved);
  424. return ret;
  425. }
  426. int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid)
  427. {
  428. struct btrfs_path *path;
  429. int ret;
  430. struct extent_buffer *l;
  431. struct btrfs_key search_key;
  432. struct btrfs_key found_key;
  433. int slot;
  434. path = btrfs_alloc_path();
  435. if (!path)
  436. return -ENOMEM;
  437. search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
  438. search_key.type = -1;
  439. search_key.offset = (u64)-1;
  440. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  441. if (ret < 0)
  442. goto error;
  443. BUG_ON(ret == 0); /* Corruption */
  444. if (path->slots[0] > 0) {
  445. slot = path->slots[0] - 1;
  446. l = path->nodes[0];
  447. btrfs_item_key_to_cpu(l, &found_key, slot);
  448. *objectid = max_t(u64, found_key.objectid,
  449. BTRFS_FIRST_FREE_OBJECTID - 1);
  450. } else {
  451. *objectid = BTRFS_FIRST_FREE_OBJECTID - 1;
  452. }
  453. ret = 0;
  454. error:
  455. btrfs_free_path(path);
  456. return ret;
  457. }
  458. int btrfs_find_free_objectid(struct btrfs_root *root, u64 *objectid)
  459. {
  460. int ret;
  461. mutex_lock(&root->objectid_mutex);
  462. if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
  463. btrfs_warn(root->fs_info,
  464. "the objectid of root %llu reaches its highest value",
  465. root->root_key.objectid);
  466. ret = -ENOSPC;
  467. goto out;
  468. }
  469. *objectid = ++root->highest_objectid;
  470. ret = 0;
  471. out:
  472. mutex_unlock(&root->objectid_mutex);
  473. return ret;
  474. }