inode-map.c 15 KB

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