inode-map.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. mutex_lock(&root->fs_commit_mutex);
  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 (btrfs_header_nritems(leaf) == 0) {
  70. WARN_ON(1);
  71. break;
  72. }
  73. /*
  74. * Save the key so we can advances forward
  75. * in the next search.
  76. */
  77. btrfs_item_key_to_cpu(leaf, &key, 0);
  78. btrfs_release_path(path);
  79. root->cache_progress = last;
  80. mutex_unlock(&root->fs_commit_mutex);
  81. schedule_timeout(1);
  82. goto again;
  83. } else
  84. continue;
  85. }
  86. btrfs_item_key_to_cpu(leaf, &key, slot);
  87. if (key.type != BTRFS_INODE_ITEM_KEY)
  88. goto next;
  89. if (key.objectid >= root->highest_objectid)
  90. break;
  91. if (last != (u64)-1 && last + 1 != key.objectid) {
  92. __btrfs_add_free_space(ctl, last + 1,
  93. key.objectid - last - 1);
  94. wake_up(&root->cache_wait);
  95. }
  96. last = key.objectid;
  97. next:
  98. path->slots[0]++;
  99. }
  100. if (last < root->highest_objectid - 1) {
  101. __btrfs_add_free_space(ctl, last + 1,
  102. root->highest_objectid - last - 1);
  103. }
  104. spin_lock(&root->cache_lock);
  105. root->cached = BTRFS_CACHE_FINISHED;
  106. spin_unlock(&root->cache_lock);
  107. root->cache_progress = (u64)-1;
  108. btrfs_unpin_free_ino(root);
  109. out:
  110. wake_up(&root->cache_wait);
  111. mutex_unlock(&root->fs_commit_mutex);
  112. btrfs_free_path(path);
  113. return ret;
  114. }
  115. static void start_caching(struct btrfs_root *root)
  116. {
  117. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  118. struct task_struct *tsk;
  119. int ret;
  120. u64 objectid;
  121. if (!btrfs_test_opt(root, INODE_MAP_CACHE))
  122. return;
  123. spin_lock(&root->cache_lock);
  124. if (root->cached != BTRFS_CACHE_NO) {
  125. spin_unlock(&root->cache_lock);
  126. return;
  127. }
  128. root->cached = BTRFS_CACHE_STARTED;
  129. spin_unlock(&root->cache_lock);
  130. ret = load_free_ino_cache(root->fs_info, root);
  131. if (ret == 1) {
  132. spin_lock(&root->cache_lock);
  133. root->cached = BTRFS_CACHE_FINISHED;
  134. spin_unlock(&root->cache_lock);
  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(ctl, objectid,
  147. BTRFS_LAST_FREE_OBJECTID - objectid + 1);
  148. }
  149. tsk = kthread_run(caching_kthread, root, "btrfs-ino-cache-%llu\n",
  150. root->root_key.objectid);
  151. BUG_ON(IS_ERR(tsk)); /* -ENOMEM */
  152. }
  153. int btrfs_find_free_ino(struct btrfs_root *root, u64 *objectid)
  154. {
  155. if (!btrfs_test_opt(root, 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->cache_wait,
  163. root->cached == BTRFS_CACHE_FINISHED ||
  164. root->free_ino_ctl->free_space > 0);
  165. if (root->cached == BTRFS_CACHE_FINISHED &&
  166. root->free_ino_ctl->free_space == 0)
  167. return -ENOSPC;
  168. else
  169. goto again;
  170. }
  171. void btrfs_return_ino(struct btrfs_root *root, u64 objectid)
  172. {
  173. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  174. struct btrfs_free_space_ctl *pinned = root->free_ino_pinned;
  175. if (!btrfs_test_opt(root, INODE_MAP_CACHE))
  176. return;
  177. again:
  178. if (root->cached == BTRFS_CACHE_FINISHED) {
  179. __btrfs_add_free_space(ctl, objectid, 1);
  180. } else {
  181. /*
  182. * If we are in the process of caching free ino chunks,
  183. * to avoid adding the same inode number to the free_ino
  184. * tree twice due to cross transaction, we'll leave it
  185. * in the pinned tree until a transaction is committed
  186. * or the caching work is done.
  187. */
  188. mutex_lock(&root->fs_commit_mutex);
  189. spin_lock(&root->cache_lock);
  190. if (root->cached == BTRFS_CACHE_FINISHED) {
  191. spin_unlock(&root->cache_lock);
  192. mutex_unlock(&root->fs_commit_mutex);
  193. goto again;
  194. }
  195. spin_unlock(&root->cache_lock);
  196. start_caching(root);
  197. if (objectid <= root->cache_progress ||
  198. objectid > root->highest_objectid)
  199. __btrfs_add_free_space(ctl, objectid, 1);
  200. else
  201. __btrfs_add_free_space(pinned, objectid, 1);
  202. mutex_unlock(&root->fs_commit_mutex);
  203. }
  204. }
  205. /*
  206. * When a transaction is committed, we'll move those inode numbers which
  207. * are smaller than root->cache_progress from pinned tree to free_ino tree,
  208. * and others will just be dropped, because the commit root we were
  209. * searching has changed.
  210. *
  211. * Must be called with root->fs_commit_mutex held
  212. */
  213. void btrfs_unpin_free_ino(struct btrfs_root *root)
  214. {
  215. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  216. struct rb_root *rbroot = &root->free_ino_pinned->free_space_offset;
  217. struct btrfs_free_space *info;
  218. struct rb_node *n;
  219. u64 count;
  220. if (!btrfs_test_opt(root, INODE_MAP_CACHE))
  221. return;
  222. while (1) {
  223. n = rb_first(rbroot);
  224. if (!n)
  225. break;
  226. info = rb_entry(n, struct btrfs_free_space, offset_index);
  227. BUG_ON(info->bitmap); /* Logic error */
  228. if (info->offset > root->cache_progress)
  229. goto free;
  230. else if (info->offset + info->bytes > root->cache_progress)
  231. count = root->cache_progress - info->offset + 1;
  232. else
  233. count = info->bytes;
  234. __btrfs_add_free_space(ctl, info->offset, count);
  235. free:
  236. rb_erase(&info->offset_index, rbroot);
  237. kmem_cache_free(btrfs_free_space_cachep, info);
  238. }
  239. }
  240. #define INIT_THRESHOLD (((1024 * 32) / 2) / sizeof(struct btrfs_free_space))
  241. #define INODES_PER_BITMAP (PAGE_CACHE_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_CACHE_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 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 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. /*
  317. * Initially we allow to use 16K of ram to cache chunks of
  318. * inode numbers before we resort to bitmaps. This is somewhat
  319. * arbitrary, but it will be adjusted in runtime.
  320. */
  321. ctl->extents_thresh = INIT_THRESHOLD;
  322. spin_lock_init(&pinned->tree_lock);
  323. pinned->unit = 1;
  324. pinned->start = 0;
  325. pinned->private = NULL;
  326. pinned->extents_thresh = 0;
  327. pinned->op = &pinned_free_ino_op;
  328. }
  329. int btrfs_save_ino_cache(struct btrfs_root *root,
  330. struct btrfs_trans_handle *trans)
  331. {
  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. u64 num_bytes;
  337. u64 alloc_hint = 0;
  338. int ret;
  339. int prealloc;
  340. bool retry = false;
  341. /* only fs tree and subvol/snap needs ino cache */
  342. if (root->root_key.objectid != BTRFS_FS_TREE_OBJECTID &&
  343. (root->root_key.objectid < BTRFS_FIRST_FREE_OBJECTID ||
  344. root->root_key.objectid > BTRFS_LAST_FREE_OBJECTID))
  345. return 0;
  346. /* Don't save inode cache if we are deleting this root */
  347. if (btrfs_root_refs(&root->root_item) == 0 &&
  348. root != root->fs_info->tree_root)
  349. return 0;
  350. if (!btrfs_test_opt(root, 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. * 3 items for inode item update (in the worst case)
  361. * 1 item for free space object
  362. * 3 items for pre-allocation
  363. */
  364. trans->bytes_reserved = btrfs_calc_trans_metadata_size(root, 8);
  365. ret = btrfs_block_rsv_add_noflush(root, trans->block_rsv,
  366. trans->bytes_reserved);
  367. if (ret)
  368. goto out;
  369. trace_btrfs_space_reservation(root->fs_info, "ino_cache",
  370. trans->transid, trans->bytes_reserved, 1);
  371. again:
  372. inode = lookup_free_ino_inode(root, path);
  373. if (IS_ERR(inode) && (PTR_ERR(inode) != -ENOENT || retry)) {
  374. ret = PTR_ERR(inode);
  375. goto out_release;
  376. }
  377. if (IS_ERR(inode)) {
  378. BUG_ON(retry); /* Logic error */
  379. retry = true;
  380. ret = create_free_ino_inode(root, trans, path);
  381. if (ret)
  382. goto out_release;
  383. goto again;
  384. }
  385. BTRFS_I(inode)->generation = 0;
  386. ret = btrfs_update_inode(trans, root, inode);
  387. if (ret) {
  388. btrfs_abort_transaction(trans, root, ret);
  389. goto out_put;
  390. }
  391. if (i_size_read(inode) > 0) {
  392. ret = btrfs_truncate_free_space_cache(root, trans, path, inode);
  393. if (ret) {
  394. btrfs_abort_transaction(trans, root, ret);
  395. goto out_put;
  396. }
  397. }
  398. spin_lock(&root->cache_lock);
  399. if (root->cached != BTRFS_CACHE_FINISHED) {
  400. ret = -1;
  401. spin_unlock(&root->cache_lock);
  402. goto out_put;
  403. }
  404. spin_unlock(&root->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, 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, prealloc);
  419. goto out_put;
  420. }
  421. btrfs_free_reserved_data_space(inode, prealloc);
  422. ret = btrfs_write_out_ino_cache(root, trans, path);
  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. static 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_FIRST_FREE_OBJECTID)) {
  472. ret = btrfs_find_highest_objectid(root,
  473. &root->highest_objectid);
  474. if (ret)
  475. goto out;
  476. }
  477. if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
  478. ret = -ENOSPC;
  479. goto out;
  480. }
  481. *objectid = ++root->highest_objectid;
  482. ret = 0;
  483. out:
  484. mutex_unlock(&root->objectid_mutex);
  485. return ret;
  486. }