inode-map.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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);
  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);
  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. wake_up(&root->ino_cache_wait);
  149. }
  150. tsk = kthread_run(caching_kthread, root, "btrfs-ino-cache-%llu",
  151. root->root_key.objectid);
  152. if (IS_ERR(tsk))
  153. fail_caching_thread(root);
  154. }
  155. int btrfs_find_free_ino(struct btrfs_root *root, u64 *objectid)
  156. {
  157. if (!btrfs_test_opt(root->fs_info, INODE_MAP_CACHE))
  158. return btrfs_find_free_objectid(root, objectid);
  159. again:
  160. *objectid = btrfs_find_ino_for_alloc(root);
  161. if (*objectid != 0)
  162. return 0;
  163. start_caching(root);
  164. wait_event(root->ino_cache_wait,
  165. root->ino_cache_state == BTRFS_CACHE_FINISHED ||
  166. root->ino_cache_state == BTRFS_CACHE_ERROR ||
  167. root->free_ino_ctl->free_space > 0);
  168. if (root->ino_cache_state == BTRFS_CACHE_FINISHED &&
  169. root->free_ino_ctl->free_space == 0)
  170. return -ENOSPC;
  171. else if (root->ino_cache_state == BTRFS_CACHE_ERROR)
  172. return btrfs_find_free_objectid(root, objectid);
  173. else
  174. goto again;
  175. }
  176. void btrfs_return_ino(struct btrfs_root *root, u64 objectid)
  177. {
  178. struct btrfs_fs_info *fs_info = root->fs_info;
  179. struct btrfs_free_space_ctl *pinned = root->free_ino_pinned;
  180. if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
  181. return;
  182. again:
  183. if (root->ino_cache_state == BTRFS_CACHE_FINISHED) {
  184. __btrfs_add_free_space(fs_info, pinned, objectid, 1);
  185. } else {
  186. down_write(&fs_info->commit_root_sem);
  187. spin_lock(&root->ino_cache_lock);
  188. if (root->ino_cache_state == BTRFS_CACHE_FINISHED) {
  189. spin_unlock(&root->ino_cache_lock);
  190. up_write(&fs_info->commit_root_sem);
  191. goto again;
  192. }
  193. spin_unlock(&root->ino_cache_lock);
  194. start_caching(root);
  195. __btrfs_add_free_space(fs_info, pinned, objectid, 1);
  196. up_write(&fs_info->commit_root_sem);
  197. }
  198. }
  199. /*
  200. * When a transaction is committed, we'll move those inode numbers which are
  201. * smaller than root->ino_cache_progress from pinned tree to free_ino tree, and
  202. * others will just be dropped, because the commit root we were searching has
  203. * changed.
  204. *
  205. * Must be called with root->fs_info->commit_root_sem held
  206. */
  207. void btrfs_unpin_free_ino(struct btrfs_root *root)
  208. {
  209. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  210. struct rb_root *rbroot = &root->free_ino_pinned->free_space_offset;
  211. spinlock_t *rbroot_lock = &root->free_ino_pinned->tree_lock;
  212. struct btrfs_free_space *info;
  213. struct rb_node *n;
  214. u64 count;
  215. if (!btrfs_test_opt(root->fs_info, INODE_MAP_CACHE))
  216. return;
  217. while (1) {
  218. spin_lock(rbroot_lock);
  219. n = rb_first(rbroot);
  220. if (!n) {
  221. spin_unlock(rbroot_lock);
  222. break;
  223. }
  224. info = rb_entry(n, struct btrfs_free_space, offset_index);
  225. BUG_ON(info->bitmap); /* Logic error */
  226. if (info->offset > root->ino_cache_progress)
  227. count = 0;
  228. else
  229. count = min(root->ino_cache_progress - info->offset + 1,
  230. info->bytes);
  231. rb_erase(&info->offset_index, rbroot);
  232. spin_unlock(rbroot_lock);
  233. if (count)
  234. __btrfs_add_free_space(root->fs_info, ctl,
  235. info->offset, count);
  236. kmem_cache_free(btrfs_free_space_cachep, info);
  237. }
  238. }
  239. #define INIT_THRESHOLD ((SZ_32K / 2) / sizeof(struct btrfs_free_space))
  240. #define INODES_PER_BITMAP (PAGE_SIZE * 8)
  241. /*
  242. * The goal is to keep the memory used by the free_ino tree won't
  243. * exceed the memory if we use bitmaps only.
  244. */
  245. static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
  246. {
  247. struct btrfs_free_space *info;
  248. struct rb_node *n;
  249. int max_ino;
  250. int max_bitmaps;
  251. n = rb_last(&ctl->free_space_offset);
  252. if (!n) {
  253. ctl->extents_thresh = INIT_THRESHOLD;
  254. return;
  255. }
  256. info = rb_entry(n, struct btrfs_free_space, offset_index);
  257. /*
  258. * Find the maximum inode number in the filesystem. Note we
  259. * ignore the fact that this can be a bitmap, because we are
  260. * not doing precise calculation.
  261. */
  262. max_ino = info->bytes - 1;
  263. max_bitmaps = ALIGN(max_ino, INODES_PER_BITMAP) / INODES_PER_BITMAP;
  264. if (max_bitmaps <= ctl->total_bitmaps) {
  265. ctl->extents_thresh = 0;
  266. return;
  267. }
  268. ctl->extents_thresh = (max_bitmaps - ctl->total_bitmaps) *
  269. PAGE_SIZE / sizeof(*info);
  270. }
  271. /*
  272. * We don't fall back to bitmap, if we are below the extents threshold
  273. * or this chunk of inode numbers is a big one.
  274. */
  275. static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
  276. struct btrfs_free_space *info)
  277. {
  278. if (ctl->free_extents < ctl->extents_thresh ||
  279. info->bytes > INODES_PER_BITMAP / 10)
  280. return false;
  281. return true;
  282. }
  283. static const struct btrfs_free_space_op free_ino_op = {
  284. .recalc_thresholds = recalculate_thresholds,
  285. .use_bitmap = use_bitmap,
  286. };
  287. static void pinned_recalc_thresholds(struct btrfs_free_space_ctl *ctl)
  288. {
  289. }
  290. static bool pinned_use_bitmap(struct btrfs_free_space_ctl *ctl,
  291. struct btrfs_free_space *info)
  292. {
  293. /*
  294. * We always use extents for two reasons:
  295. *
  296. * - The pinned tree is only used during the process of caching
  297. * work.
  298. * - Make code simpler. See btrfs_unpin_free_ino().
  299. */
  300. return false;
  301. }
  302. static const struct btrfs_free_space_op pinned_free_ino_op = {
  303. .recalc_thresholds = pinned_recalc_thresholds,
  304. .use_bitmap = pinned_use_bitmap,
  305. };
  306. void btrfs_init_free_ino_ctl(struct btrfs_root *root)
  307. {
  308. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  309. struct btrfs_free_space_ctl *pinned = root->free_ino_pinned;
  310. spin_lock_init(&ctl->tree_lock);
  311. ctl->unit = 1;
  312. ctl->start = 0;
  313. ctl->private = NULL;
  314. ctl->op = &free_ino_op;
  315. INIT_LIST_HEAD(&ctl->trimming_ranges);
  316. mutex_init(&ctl->cache_writeout_mutex);
  317. /*
  318. * Initially we allow to use 16K of ram to cache chunks of
  319. * inode numbers before we resort to bitmaps. This is somewhat
  320. * arbitrary, but it will be adjusted in runtime.
  321. */
  322. ctl->extents_thresh = INIT_THRESHOLD;
  323. spin_lock_init(&pinned->tree_lock);
  324. pinned->unit = 1;
  325. pinned->start = 0;
  326. pinned->private = NULL;
  327. pinned->extents_thresh = 0;
  328. pinned->op = &pinned_free_ino_op;
  329. }
  330. int btrfs_save_ino_cache(struct btrfs_root *root,
  331. struct btrfs_trans_handle *trans)
  332. {
  333. struct btrfs_fs_info *fs_info = root->fs_info;
  334. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  335. struct btrfs_path *path;
  336. struct inode *inode;
  337. struct btrfs_block_rsv *rsv;
  338. struct extent_changeset *data_reserved = NULL;
  339. u64 num_bytes;
  340. u64 alloc_hint = 0;
  341. int ret;
  342. int prealloc;
  343. bool retry = false;
  344. /* only fs tree and subvol/snap needs ino cache */
  345. if (root->root_key.objectid != BTRFS_FS_TREE_OBJECTID &&
  346. (root->root_key.objectid < BTRFS_FIRST_FREE_OBJECTID ||
  347. root->root_key.objectid > BTRFS_LAST_FREE_OBJECTID))
  348. return 0;
  349. /* Don't save inode cache if we are deleting this root */
  350. if (btrfs_root_refs(&root->root_item) == 0)
  351. return 0;
  352. if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
  353. return 0;
  354. path = btrfs_alloc_path();
  355. if (!path)
  356. return -ENOMEM;
  357. rsv = trans->block_rsv;
  358. trans->block_rsv = &fs_info->trans_block_rsv;
  359. num_bytes = trans->bytes_reserved;
  360. /*
  361. * 1 item for inode item insertion if need
  362. * 4 items for inode item update (in the worst case)
  363. * 1 items for slack space if we need do truncation
  364. * 1 item for free space object
  365. * 3 items for pre-allocation
  366. */
  367. trans->bytes_reserved = btrfs_calc_insert_metadata_size(fs_info, 10);
  368. ret = btrfs_block_rsv_add(root, trans->block_rsv,
  369. trans->bytes_reserved,
  370. BTRFS_RESERVE_NO_FLUSH);
  371. if (ret)
  372. goto out;
  373. trace_btrfs_space_reservation(fs_info, "ino_cache", trans->transid,
  374. trans->bytes_reserved, 1);
  375. again:
  376. inode = lookup_free_ino_inode(root, path);
  377. if (IS_ERR(inode) && (PTR_ERR(inode) != -ENOENT || retry)) {
  378. ret = PTR_ERR(inode);
  379. goto out_release;
  380. }
  381. if (IS_ERR(inode)) {
  382. BUG_ON(retry); /* Logic error */
  383. retry = true;
  384. ret = create_free_ino_inode(root, trans, path);
  385. if (ret)
  386. goto out_release;
  387. goto again;
  388. }
  389. BTRFS_I(inode)->generation = 0;
  390. ret = btrfs_update_inode(trans, root, inode);
  391. if (ret) {
  392. btrfs_abort_transaction(trans, ret);
  393. goto out_put;
  394. }
  395. if (i_size_read(inode) > 0) {
  396. ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
  397. if (ret) {
  398. if (ret != -ENOSPC)
  399. btrfs_abort_transaction(trans, ret);
  400. goto out_put;
  401. }
  402. }
  403. spin_lock(&root->ino_cache_lock);
  404. if (root->ino_cache_state != BTRFS_CACHE_FINISHED) {
  405. ret = -1;
  406. spin_unlock(&root->ino_cache_lock);
  407. goto out_put;
  408. }
  409. spin_unlock(&root->ino_cache_lock);
  410. spin_lock(&ctl->tree_lock);
  411. prealloc = sizeof(struct btrfs_free_space) * ctl->free_extents;
  412. prealloc = ALIGN(prealloc, PAGE_SIZE);
  413. prealloc += ctl->total_bitmaps * PAGE_SIZE;
  414. spin_unlock(&ctl->tree_lock);
  415. /* Just to make sure we have enough space */
  416. prealloc += 8 * PAGE_SIZE;
  417. ret = btrfs_delalloc_reserve_space(inode, &data_reserved, 0, prealloc);
  418. if (ret)
  419. goto out_put;
  420. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, prealloc,
  421. prealloc, prealloc, &alloc_hint);
  422. if (ret) {
  423. btrfs_delalloc_release_extents(BTRFS_I(inode), prealloc);
  424. btrfs_delalloc_release_metadata(BTRFS_I(inode), prealloc, true);
  425. goto out_put;
  426. }
  427. ret = btrfs_write_out_ino_cache(root, trans, path, inode);
  428. btrfs_delalloc_release_extents(BTRFS_I(inode), prealloc);
  429. out_put:
  430. iput(inode);
  431. out_release:
  432. trace_btrfs_space_reservation(fs_info, "ino_cache", trans->transid,
  433. trans->bytes_reserved, 0);
  434. btrfs_block_rsv_release(fs_info, trans->block_rsv,
  435. trans->bytes_reserved);
  436. out:
  437. trans->block_rsv = rsv;
  438. trans->bytes_reserved = num_bytes;
  439. btrfs_free_path(path);
  440. extent_changeset_free(data_reserved);
  441. return ret;
  442. }
  443. int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid)
  444. {
  445. struct btrfs_path *path;
  446. int ret;
  447. struct extent_buffer *l;
  448. struct btrfs_key search_key;
  449. struct btrfs_key found_key;
  450. int slot;
  451. path = btrfs_alloc_path();
  452. if (!path)
  453. return -ENOMEM;
  454. search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
  455. search_key.type = -1;
  456. search_key.offset = (u64)-1;
  457. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  458. if (ret < 0)
  459. goto error;
  460. BUG_ON(ret == 0); /* Corruption */
  461. if (path->slots[0] > 0) {
  462. slot = path->slots[0] - 1;
  463. l = path->nodes[0];
  464. btrfs_item_key_to_cpu(l, &found_key, slot);
  465. *objectid = max_t(u64, found_key.objectid,
  466. BTRFS_FIRST_FREE_OBJECTID - 1);
  467. } else {
  468. *objectid = BTRFS_FIRST_FREE_OBJECTID - 1;
  469. }
  470. ret = 0;
  471. error:
  472. btrfs_free_path(path);
  473. return ret;
  474. }
  475. int btrfs_find_free_objectid(struct btrfs_root *root, u64 *objectid)
  476. {
  477. int ret;
  478. mutex_lock(&root->objectid_mutex);
  479. if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
  480. btrfs_warn(root->fs_info,
  481. "the objectid of root %llu reaches its highest value",
  482. root->root_key.objectid);
  483. ret = -ENOSPC;
  484. goto out;
  485. }
  486. *objectid = ++root->highest_objectid;
  487. ret = 0;
  488. out:
  489. mutex_unlock(&root->objectid_mutex);
  490. return ret;
  491. }