inode-map.c 15 KB

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