inode-map.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. path = btrfs_alloc_path();
  38. if (!path)
  39. return -ENOMEM;
  40. /* Since the commit root is read-only, we can safely skip locking. */
  41. path->skip_locking = 1;
  42. path->search_commit_root = 1;
  43. path->reada = 2;
  44. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  45. key.offset = 0;
  46. key.type = BTRFS_INODE_ITEM_KEY;
  47. again:
  48. /* need to make sure the commit_root doesn't disappear */
  49. mutex_lock(&root->fs_commit_mutex);
  50. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  51. if (ret < 0)
  52. goto out;
  53. while (1) {
  54. smp_mb();
  55. if (fs_info->closing)
  56. goto out;
  57. leaf = path->nodes[0];
  58. slot = path->slots[0];
  59. if (slot >= btrfs_header_nritems(leaf)) {
  60. ret = btrfs_next_leaf(root, path);
  61. if (ret < 0)
  62. goto out;
  63. else if (ret > 0)
  64. break;
  65. if (need_resched() ||
  66. btrfs_transaction_in_commit(fs_info)) {
  67. leaf = path->nodes[0];
  68. if (btrfs_header_nritems(leaf) == 0) {
  69. WARN_ON(1);
  70. break;
  71. }
  72. /*
  73. * Save the key so we can advances forward
  74. * in the next search.
  75. */
  76. btrfs_item_key_to_cpu(leaf, &key, 0);
  77. btrfs_release_path(path);
  78. root->cache_progress = last;
  79. mutex_unlock(&root->fs_commit_mutex);
  80. schedule_timeout(1);
  81. goto again;
  82. } else
  83. continue;
  84. }
  85. btrfs_item_key_to_cpu(leaf, &key, slot);
  86. if (key.type != BTRFS_INODE_ITEM_KEY)
  87. goto next;
  88. if (key.objectid >= root->highest_objectid)
  89. break;
  90. if (last != (u64)-1 && last + 1 != key.objectid) {
  91. __btrfs_add_free_space(ctl, last + 1,
  92. key.objectid - last - 1);
  93. wake_up(&root->cache_wait);
  94. }
  95. last = key.objectid;
  96. next:
  97. path->slots[0]++;
  98. }
  99. if (last < root->highest_objectid - 1) {
  100. __btrfs_add_free_space(ctl, last + 1,
  101. root->highest_objectid - last - 1);
  102. }
  103. spin_lock(&root->cache_lock);
  104. root->cached = BTRFS_CACHE_FINISHED;
  105. spin_unlock(&root->cache_lock);
  106. root->cache_progress = (u64)-1;
  107. btrfs_unpin_free_ino(root);
  108. out:
  109. wake_up(&root->cache_wait);
  110. mutex_unlock(&root->fs_commit_mutex);
  111. btrfs_free_path(path);
  112. return ret;
  113. }
  114. static void start_caching(struct btrfs_root *root)
  115. {
  116. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  117. struct task_struct *tsk;
  118. int ret;
  119. u64 objectid;
  120. spin_lock(&root->cache_lock);
  121. if (root->cached != BTRFS_CACHE_NO) {
  122. spin_unlock(&root->cache_lock);
  123. return;
  124. }
  125. root->cached = BTRFS_CACHE_STARTED;
  126. spin_unlock(&root->cache_lock);
  127. ret = load_free_ino_cache(root->fs_info, root);
  128. if (ret == 1) {
  129. spin_lock(&root->cache_lock);
  130. root->cached = BTRFS_CACHE_FINISHED;
  131. spin_unlock(&root->cache_lock);
  132. return;
  133. }
  134. /*
  135. * It can be quite time-consuming to fill the cache by searching
  136. * through the extent tree, and this can keep ino allocation path
  137. * waiting. Therefore at start we quickly find out the highest
  138. * inode number and we know we can use inode numbers which fall in
  139. * [highest_ino + 1, BTRFS_LAST_FREE_OBJECTID].
  140. */
  141. ret = btrfs_find_free_objectid(root, &objectid);
  142. if (!ret && objectid <= BTRFS_LAST_FREE_OBJECTID) {
  143. __btrfs_add_free_space(ctl, objectid,
  144. BTRFS_LAST_FREE_OBJECTID - objectid + 1);
  145. }
  146. tsk = kthread_run(caching_kthread, root, "btrfs-ino-cache-%llu\n",
  147. root->root_key.objectid);
  148. BUG_ON(IS_ERR(tsk));
  149. }
  150. int btrfs_find_free_ino(struct btrfs_root *root, u64 *objectid)
  151. {
  152. again:
  153. *objectid = btrfs_find_ino_for_alloc(root);
  154. if (*objectid != 0)
  155. return 0;
  156. start_caching(root);
  157. wait_event(root->cache_wait,
  158. root->cached == BTRFS_CACHE_FINISHED ||
  159. root->free_ino_ctl->free_space > 0);
  160. if (root->cached == BTRFS_CACHE_FINISHED &&
  161. root->free_ino_ctl->free_space == 0)
  162. return -ENOSPC;
  163. else
  164. goto again;
  165. }
  166. void btrfs_return_ino(struct btrfs_root *root, u64 objectid)
  167. {
  168. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  169. struct btrfs_free_space_ctl *pinned = root->free_ino_pinned;
  170. again:
  171. if (root->cached == BTRFS_CACHE_FINISHED) {
  172. __btrfs_add_free_space(ctl, objectid, 1);
  173. } else {
  174. /*
  175. * If we are in the process of caching free ino chunks,
  176. * to avoid adding the same inode number to the free_ino
  177. * tree twice due to cross transaction, we'll leave it
  178. * in the pinned tree until a transaction is committed
  179. * or the caching work is done.
  180. */
  181. mutex_lock(&root->fs_commit_mutex);
  182. spin_lock(&root->cache_lock);
  183. if (root->cached == BTRFS_CACHE_FINISHED) {
  184. spin_unlock(&root->cache_lock);
  185. mutex_unlock(&root->fs_commit_mutex);
  186. goto again;
  187. }
  188. spin_unlock(&root->cache_lock);
  189. start_caching(root);
  190. if (objectid <= root->cache_progress ||
  191. objectid > root->highest_objectid)
  192. __btrfs_add_free_space(ctl, objectid, 1);
  193. else
  194. __btrfs_add_free_space(pinned, objectid, 1);
  195. mutex_unlock(&root->fs_commit_mutex);
  196. }
  197. }
  198. /*
  199. * When a transaction is committed, we'll move those inode numbers which
  200. * are smaller than root->cache_progress from pinned tree to free_ino tree,
  201. * and others will just be dropped, because the commit root we were
  202. * searching has changed.
  203. *
  204. * Must be called with root->fs_commit_mutex held
  205. */
  206. void btrfs_unpin_free_ino(struct btrfs_root *root)
  207. {
  208. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  209. struct rb_root *rbroot = &root->free_ino_pinned->free_space_offset;
  210. struct btrfs_free_space *info;
  211. struct rb_node *n;
  212. u64 count;
  213. while (1) {
  214. n = rb_first(rbroot);
  215. if (!n)
  216. break;
  217. info = rb_entry(n, struct btrfs_free_space, offset_index);
  218. BUG_ON(info->bitmap);
  219. if (info->offset > root->cache_progress)
  220. goto free;
  221. else if (info->offset + info->bytes > root->cache_progress)
  222. count = root->cache_progress - info->offset + 1;
  223. else
  224. count = info->bytes;
  225. __btrfs_add_free_space(ctl, info->offset, count);
  226. free:
  227. rb_erase(&info->offset_index, rbroot);
  228. kfree(info);
  229. }
  230. }
  231. #define INIT_THRESHOLD (((1024 * 32) / 2) / sizeof(struct btrfs_free_space))
  232. #define INODES_PER_BITMAP (PAGE_CACHE_SIZE * 8)
  233. /*
  234. * The goal is to keep the memory used by the free_ino tree won't
  235. * exceed the memory if we use bitmaps only.
  236. */
  237. static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
  238. {
  239. struct btrfs_free_space *info;
  240. struct rb_node *n;
  241. int max_ino;
  242. int max_bitmaps;
  243. n = rb_last(&ctl->free_space_offset);
  244. if (!n) {
  245. ctl->extents_thresh = INIT_THRESHOLD;
  246. return;
  247. }
  248. info = rb_entry(n, struct btrfs_free_space, offset_index);
  249. /*
  250. * Find the maximum inode number in the filesystem. Note we
  251. * ignore the fact that this can be a bitmap, because we are
  252. * not doing precise calculation.
  253. */
  254. max_ino = info->bytes - 1;
  255. max_bitmaps = ALIGN(max_ino, INODES_PER_BITMAP) / INODES_PER_BITMAP;
  256. if (max_bitmaps <= ctl->total_bitmaps) {
  257. ctl->extents_thresh = 0;
  258. return;
  259. }
  260. ctl->extents_thresh = (max_bitmaps - ctl->total_bitmaps) *
  261. PAGE_CACHE_SIZE / sizeof(*info);
  262. }
  263. /*
  264. * We don't fall back to bitmap, if we are below the extents threshold
  265. * or this chunk of inode numbers is a big one.
  266. */
  267. static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
  268. struct btrfs_free_space *info)
  269. {
  270. if (ctl->free_extents < ctl->extents_thresh ||
  271. info->bytes > INODES_PER_BITMAP / 10)
  272. return false;
  273. return true;
  274. }
  275. static struct btrfs_free_space_op free_ino_op = {
  276. .recalc_thresholds = recalculate_thresholds,
  277. .use_bitmap = use_bitmap,
  278. };
  279. static void pinned_recalc_thresholds(struct btrfs_free_space_ctl *ctl)
  280. {
  281. }
  282. static bool pinned_use_bitmap(struct btrfs_free_space_ctl *ctl,
  283. struct btrfs_free_space *info)
  284. {
  285. /*
  286. * We always use extents for two reasons:
  287. *
  288. * - The pinned tree is only used during the process of caching
  289. * work.
  290. * - Make code simpler. See btrfs_unpin_free_ino().
  291. */
  292. return false;
  293. }
  294. static struct btrfs_free_space_op pinned_free_ino_op = {
  295. .recalc_thresholds = pinned_recalc_thresholds,
  296. .use_bitmap = pinned_use_bitmap,
  297. };
  298. void btrfs_init_free_ino_ctl(struct btrfs_root *root)
  299. {
  300. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  301. struct btrfs_free_space_ctl *pinned = root->free_ino_pinned;
  302. spin_lock_init(&ctl->tree_lock);
  303. ctl->unit = 1;
  304. ctl->start = 0;
  305. ctl->private = NULL;
  306. ctl->op = &free_ino_op;
  307. /*
  308. * Initially we allow to use 16K of ram to cache chunks of
  309. * inode numbers before we resort to bitmaps. This is somewhat
  310. * arbitrary, but it will be adjusted in runtime.
  311. */
  312. ctl->extents_thresh = INIT_THRESHOLD;
  313. spin_lock_init(&pinned->tree_lock);
  314. pinned->unit = 1;
  315. pinned->start = 0;
  316. pinned->private = NULL;
  317. pinned->extents_thresh = 0;
  318. pinned->op = &pinned_free_ino_op;
  319. }
  320. int btrfs_save_ino_cache(struct btrfs_root *root,
  321. struct btrfs_trans_handle *trans)
  322. {
  323. struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
  324. struct btrfs_path *path;
  325. struct inode *inode;
  326. u64 alloc_hint = 0;
  327. int ret;
  328. int prealloc;
  329. bool retry = false;
  330. path = btrfs_alloc_path();
  331. if (!path)
  332. return -ENOMEM;
  333. again:
  334. inode = lookup_free_ino_inode(root, path);
  335. if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
  336. ret = PTR_ERR(inode);
  337. goto out;
  338. }
  339. if (IS_ERR(inode)) {
  340. BUG_ON(retry);
  341. retry = true;
  342. ret = create_free_ino_inode(root, trans, path);
  343. if (ret)
  344. goto out;
  345. goto again;
  346. }
  347. BTRFS_I(inode)->generation = 0;
  348. ret = btrfs_update_inode(trans, root, inode);
  349. WARN_ON(ret);
  350. if (i_size_read(inode) > 0) {
  351. ret = btrfs_truncate_free_space_cache(root, trans, path, inode);
  352. if (ret)
  353. goto out_put;
  354. }
  355. spin_lock(&root->cache_lock);
  356. if (root->cached != BTRFS_CACHE_FINISHED) {
  357. ret = -1;
  358. spin_unlock(&root->cache_lock);
  359. goto out_put;
  360. }
  361. spin_unlock(&root->cache_lock);
  362. spin_lock(&ctl->tree_lock);
  363. prealloc = sizeof(struct btrfs_free_space) * ctl->free_extents;
  364. prealloc = ALIGN(prealloc, PAGE_CACHE_SIZE);
  365. prealloc += ctl->total_bitmaps * PAGE_CACHE_SIZE;
  366. spin_unlock(&ctl->tree_lock);
  367. /* Just to make sure we have enough space */
  368. prealloc += 8 * PAGE_CACHE_SIZE;
  369. ret = btrfs_check_data_free_space(inode, prealloc);
  370. if (ret)
  371. goto out_put;
  372. ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, prealloc,
  373. prealloc, prealloc, &alloc_hint);
  374. if (ret)
  375. goto out_put;
  376. btrfs_free_reserved_data_space(inode, prealloc);
  377. out_put:
  378. iput(inode);
  379. out:
  380. if (ret == 0)
  381. ret = btrfs_write_out_ino_cache(root, trans, path);
  382. btrfs_free_path(path);
  383. return ret;
  384. }
  385. static int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid)
  386. {
  387. struct btrfs_path *path;
  388. int ret;
  389. struct extent_buffer *l;
  390. struct btrfs_key search_key;
  391. struct btrfs_key found_key;
  392. int slot;
  393. path = btrfs_alloc_path();
  394. if (!path)
  395. return -ENOMEM;
  396. search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
  397. search_key.type = -1;
  398. search_key.offset = (u64)-1;
  399. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  400. if (ret < 0)
  401. goto error;
  402. BUG_ON(ret == 0);
  403. if (path->slots[0] > 0) {
  404. slot = path->slots[0] - 1;
  405. l = path->nodes[0];
  406. btrfs_item_key_to_cpu(l, &found_key, slot);
  407. *objectid = max_t(u64, found_key.objectid,
  408. BTRFS_FIRST_FREE_OBJECTID - 1);
  409. } else {
  410. *objectid = BTRFS_FIRST_FREE_OBJECTID - 1;
  411. }
  412. ret = 0;
  413. error:
  414. btrfs_free_path(path);
  415. return ret;
  416. }
  417. int btrfs_find_free_objectid(struct btrfs_root *root, u64 *objectid)
  418. {
  419. int ret;
  420. mutex_lock(&root->objectid_mutex);
  421. if (unlikely(root->highest_objectid < BTRFS_FIRST_FREE_OBJECTID)) {
  422. ret = btrfs_find_highest_objectid(root,
  423. &root->highest_objectid);
  424. if (ret)
  425. goto out;
  426. }
  427. if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
  428. ret = -ENOSPC;
  429. goto out;
  430. }
  431. *objectid = ++root->highest_objectid;
  432. ret = 0;
  433. out:
  434. mutex_unlock(&root->objectid_mutex);
  435. return ret;
  436. }