inode-map.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/delay.h>
  19. #include <linux/kthread.h>
  20. #include <linux/pagemap.h>
  21. #include "ctree.h"
  22. #include "disk-io.h"
  23. #include "free-space-cache.h"
  24. #include "inode-map.h"
  25. #include "transaction.h"
  26. static int caching_kthread(void *data)
  27. {
  28. struct btrfs_root *root = data;
  29. struct btrfs_fs_info *fs_info = root->fs_info;
  30. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  31. struct btrfs_key key;
  32. struct btrfs_path *path;
  33. struct extent_buffer *leaf;
  34. u64 last = (u64)-1;
  35. int slot;
  36. int ret;
  37. if (!btrfs_test_opt(root->fs_info, INODE_MAP_CACHE))
  38. return 0;
  39. path = btrfs_alloc_path();
  40. if (!path)
  41. return -ENOMEM;
  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. 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. btrfs_warn(fs_info, "failed to start inode caching task");
  152. btrfs_clear_pending_and_info(fs_info, INODE_MAP_CACHE,
  153. "disabling inode map caching");
  154. }
  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->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
  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. bool add_to_ctl = true;
  217. spin_lock(rbroot_lock);
  218. n = rb_first(rbroot);
  219. if (!n) {
  220. spin_unlock(rbroot_lock);
  221. break;
  222. }
  223. info = rb_entry(n, struct btrfs_free_space, offset_index);
  224. BUG_ON(info->bitmap); /* Logic error */
  225. if (info->offset > root->ino_cache_progress)
  226. add_to_ctl = false;
  227. else if (info->offset + info->bytes > root->ino_cache_progress)
  228. count = root->ino_cache_progress - info->offset + 1;
  229. else
  230. count = info->bytes;
  231. rb_erase(&info->offset_index, rbroot);
  232. spin_unlock(rbroot_lock);
  233. if (add_to_ctl)
  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_free_space_ctl *ctl = root->free_ino_ctl;
  334. struct btrfs_path *path;
  335. struct inode *inode;
  336. struct btrfs_block_rsv *rsv;
  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(root->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 = &root->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(root, 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(root->fs_info, "ino_cache",
  372. trans->transid, 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(root, 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, 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_metadata(inode, prealloc);
  422. goto out_put;
  423. }
  424. ret = btrfs_write_out_ino_cache(root, trans, path, inode);
  425. out_put:
  426. iput(inode);
  427. out_release:
  428. trace_btrfs_space_reservation(root->fs_info, "ino_cache",
  429. trans->transid, trans->bytes_reserved, 0);
  430. btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
  431. out:
  432. trans->block_rsv = rsv;
  433. trans->bytes_reserved = num_bytes;
  434. btrfs_free_path(path);
  435. return ret;
  436. }
  437. int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid)
  438. {
  439. struct btrfs_path *path;
  440. int ret;
  441. struct extent_buffer *l;
  442. struct btrfs_key search_key;
  443. struct btrfs_key found_key;
  444. int slot;
  445. path = btrfs_alloc_path();
  446. if (!path)
  447. return -ENOMEM;
  448. search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
  449. search_key.type = -1;
  450. search_key.offset = (u64)-1;
  451. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  452. if (ret < 0)
  453. goto error;
  454. BUG_ON(ret == 0); /* Corruption */
  455. if (path->slots[0] > 0) {
  456. slot = path->slots[0] - 1;
  457. l = path->nodes[0];
  458. btrfs_item_key_to_cpu(l, &found_key, slot);
  459. *objectid = max_t(u64, found_key.objectid,
  460. BTRFS_FIRST_FREE_OBJECTID - 1);
  461. } else {
  462. *objectid = BTRFS_FIRST_FREE_OBJECTID - 1;
  463. }
  464. ret = 0;
  465. error:
  466. btrfs_free_path(path);
  467. return ret;
  468. }
  469. int btrfs_find_free_objectid(struct btrfs_root *root, u64 *objectid)
  470. {
  471. int ret;
  472. mutex_lock(&root->objectid_mutex);
  473. if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
  474. btrfs_warn(root->fs_info,
  475. "the objectid of root %llu reaches its highest value",
  476. root->root_key.objectid);
  477. ret = -ENOSPC;
  478. goto out;
  479. }
  480. *objectid = ++root->highest_objectid;
  481. ret = 0;
  482. out:
  483. mutex_unlock(&root->objectid_mutex);
  484. return ret;
  485. }