inode-map.c 14 KB

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