inode-map.c 14 KB

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