free-space-cache.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Copyright (C) 2008 Red Hat. 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/sched.h>
  19. #include "ctree.h"
  20. static int tree_insert_offset(struct rb_root *root, u64 offset,
  21. struct rb_node *node)
  22. {
  23. struct rb_node **p = &root->rb_node;
  24. struct rb_node *parent = NULL;
  25. struct btrfs_free_space *info;
  26. while (*p) {
  27. parent = *p;
  28. info = rb_entry(parent, struct btrfs_free_space, offset_index);
  29. if (offset < info->offset)
  30. p = &(*p)->rb_left;
  31. else if (offset > info->offset)
  32. p = &(*p)->rb_right;
  33. else
  34. return -EEXIST;
  35. }
  36. rb_link_node(node, parent, p);
  37. rb_insert_color(node, root);
  38. return 0;
  39. }
  40. static int tree_insert_bytes(struct rb_root *root, u64 bytes,
  41. struct rb_node *node)
  42. {
  43. struct rb_node **p = &root->rb_node;
  44. struct rb_node *parent = NULL;
  45. struct btrfs_free_space *info;
  46. while (*p) {
  47. parent = *p;
  48. info = rb_entry(parent, struct btrfs_free_space, bytes_index);
  49. if (bytes < info->bytes)
  50. p = &(*p)->rb_left;
  51. else
  52. p = &(*p)->rb_right;
  53. }
  54. rb_link_node(node, parent, p);
  55. rb_insert_color(node, root);
  56. return 0;
  57. }
  58. /*
  59. * searches the tree for the given offset.
  60. *
  61. * fuzzy == 1: this is used for allocations where we are given a hint of where
  62. * to look for free space. Because the hint may not be completely on an offset
  63. * mark, or the hint may no longer point to free space we need to fudge our
  64. * results a bit. So we look for free space starting at or after offset with at
  65. * least bytes size. We prefer to find as close to the given offset as we can.
  66. * Also if the offset is within a free space range, then we will return the free
  67. * space that contains the given offset, which means we can return a free space
  68. * chunk with an offset before the provided offset.
  69. *
  70. * fuzzy == 0: this is just a normal tree search. Give us the free space that
  71. * starts at the given offset which is at least bytes size, and if its not there
  72. * return NULL.
  73. */
  74. static struct btrfs_free_space *tree_search_offset(struct rb_root *root,
  75. u64 offset, u64 bytes,
  76. int fuzzy)
  77. {
  78. struct rb_node *n = root->rb_node;
  79. struct btrfs_free_space *entry, *ret = NULL;
  80. while (n) {
  81. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  82. if (offset < entry->offset) {
  83. if (fuzzy &&
  84. (!ret || entry->offset < ret->offset) &&
  85. (bytes <= entry->bytes))
  86. ret = entry;
  87. n = n->rb_left;
  88. } else if (offset > entry->offset) {
  89. if (fuzzy &&
  90. (entry->offset + entry->bytes - 1) >= offset &&
  91. bytes <= entry->bytes) {
  92. ret = entry;
  93. break;
  94. }
  95. n = n->rb_right;
  96. } else {
  97. if (bytes > entry->bytes) {
  98. n = n->rb_right;
  99. continue;
  100. }
  101. ret = entry;
  102. break;
  103. }
  104. }
  105. return ret;
  106. }
  107. /*
  108. * return a chunk at least bytes size, as close to offset that we can get.
  109. */
  110. static struct btrfs_free_space *tree_search_bytes(struct rb_root *root,
  111. u64 offset, u64 bytes)
  112. {
  113. struct rb_node *n = root->rb_node;
  114. struct btrfs_free_space *entry, *ret = NULL;
  115. while (n) {
  116. entry = rb_entry(n, struct btrfs_free_space, bytes_index);
  117. if (bytes < entry->bytes) {
  118. /*
  119. * We prefer to get a hole size as close to the size we
  120. * are asking for so we don't take small slivers out of
  121. * huge holes, but we also want to get as close to the
  122. * offset as possible so we don't have a whole lot of
  123. * fragmentation.
  124. */
  125. if (offset <= entry->offset) {
  126. if (!ret)
  127. ret = entry;
  128. else if (entry->bytes < ret->bytes)
  129. ret = entry;
  130. else if (entry->offset < ret->offset)
  131. ret = entry;
  132. }
  133. n = n->rb_left;
  134. } else if (bytes > entry->bytes) {
  135. n = n->rb_right;
  136. } else {
  137. /*
  138. * Ok we may have multiple chunks of the wanted size,
  139. * so we don't want to take the first one we find, we
  140. * want to take the one closest to our given offset, so
  141. * keep searching just in case theres a better match.
  142. */
  143. n = n->rb_right;
  144. if (offset > entry->offset)
  145. continue;
  146. else if (!ret || entry->offset < ret->offset)
  147. ret = entry;
  148. }
  149. }
  150. return ret;
  151. }
  152. static void unlink_free_space(struct btrfs_block_group_cache *block_group,
  153. struct btrfs_free_space *info)
  154. {
  155. rb_erase(&info->offset_index, &block_group->free_space_offset);
  156. rb_erase(&info->bytes_index, &block_group->free_space_bytes);
  157. }
  158. static int link_free_space(struct btrfs_block_group_cache *block_group,
  159. struct btrfs_free_space *info)
  160. {
  161. int ret = 0;
  162. ret = tree_insert_offset(&block_group->free_space_offset, info->offset,
  163. &info->offset_index);
  164. if (ret)
  165. return ret;
  166. ret = tree_insert_bytes(&block_group->free_space_bytes, info->bytes,
  167. &info->bytes_index);
  168. if (ret)
  169. return ret;
  170. return ret;
  171. }
  172. static int __btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
  173. u64 offset, u64 bytes)
  174. {
  175. struct btrfs_free_space *right_info;
  176. struct btrfs_free_space *left_info;
  177. struct btrfs_free_space *info = NULL;
  178. int ret = 0;
  179. /*
  180. * first we want to see if there is free space adjacent to the range we
  181. * are adding, if there is remove that struct and add a new one to
  182. * cover the entire range
  183. */
  184. right_info = tree_search_offset(&block_group->free_space_offset,
  185. offset+bytes, 0, 0);
  186. left_info = tree_search_offset(&block_group->free_space_offset,
  187. offset-1, 0, 1);
  188. if (right_info) {
  189. unlink_free_space(block_group, right_info);
  190. info = right_info;
  191. info->offset = offset;
  192. info->bytes += bytes;
  193. }
  194. if (left_info && left_info->offset + left_info->bytes == offset) {
  195. unlink_free_space(block_group, left_info);
  196. if (info) {
  197. info->offset = left_info->offset;
  198. info->bytes += left_info->bytes;
  199. kfree(left_info);
  200. } else {
  201. info = left_info;
  202. info->bytes += bytes;
  203. }
  204. }
  205. if (info) {
  206. ret = link_free_space(block_group, info);
  207. if (ret)
  208. kfree(info);
  209. goto out;
  210. }
  211. info = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS);
  212. if (!info)
  213. return -ENOMEM;
  214. info->offset = offset;
  215. info->bytes = bytes;
  216. ret = link_free_space(block_group, info);
  217. if (ret)
  218. kfree(info);
  219. out:
  220. if (ret) {
  221. printk(KERN_ERR "btrfs: unable to add free space :%d\n", ret);
  222. if (ret == -EEXIST)
  223. BUG();
  224. }
  225. return ret;
  226. }
  227. static int
  228. __btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  229. u64 offset, u64 bytes)
  230. {
  231. struct btrfs_free_space *info;
  232. int ret = 0;
  233. BUG_ON(!block_group->cached);
  234. info = tree_search_offset(&block_group->free_space_offset, offset, 0,
  235. 1);
  236. if (info && info->offset == offset) {
  237. if (info->bytes < bytes) {
  238. printk(KERN_ERR "Found free space at %llu, size %llu,"
  239. "trying to use %llu\n",
  240. (unsigned long long)info->offset,
  241. (unsigned long long)info->bytes,
  242. (unsigned long long)bytes);
  243. WARN_ON(1);
  244. ret = -EINVAL;
  245. goto out;
  246. }
  247. unlink_free_space(block_group, info);
  248. if (info->bytes == bytes) {
  249. kfree(info);
  250. goto out;
  251. }
  252. info->offset += bytes;
  253. info->bytes -= bytes;
  254. ret = link_free_space(block_group, info);
  255. BUG_ON(ret);
  256. } else if (info && info->offset < offset &&
  257. info->offset + info->bytes >= offset + bytes) {
  258. u64 old_start = info->offset;
  259. /*
  260. * we're freeing space in the middle of the info,
  261. * this can happen during tree log replay
  262. *
  263. * first unlink the old info and then
  264. * insert it again after the hole we're creating
  265. */
  266. unlink_free_space(block_group, info);
  267. if (offset + bytes < info->offset + info->bytes) {
  268. u64 old_end = info->offset + info->bytes;
  269. info->offset = offset + bytes;
  270. info->bytes = old_end - info->offset;
  271. ret = link_free_space(block_group, info);
  272. BUG_ON(ret);
  273. } else {
  274. /* the hole we're creating ends at the end
  275. * of the info struct, just free the info
  276. */
  277. kfree(info);
  278. }
  279. /* step two, insert a new info struct to cover anything
  280. * before the hole
  281. */
  282. ret = __btrfs_add_free_space(block_group, old_start,
  283. offset - old_start);
  284. BUG_ON(ret);
  285. } else {
  286. if (!info) {
  287. printk(KERN_ERR "couldn't find space %llu to free\n",
  288. (unsigned long long)offset);
  289. printk(KERN_ERR "cached is %d, offset %llu bytes %llu\n",
  290. block_group->cached, block_group->key.objectid,
  291. block_group->key.offset);
  292. btrfs_dump_free_space(block_group, bytes);
  293. } else if (info) {
  294. printk(KERN_ERR "hmm, found offset=%llu bytes=%llu, "
  295. "but wanted offset=%llu bytes=%llu\n",
  296. info->offset, info->bytes, offset, bytes);
  297. }
  298. WARN_ON(1);
  299. }
  300. out:
  301. return ret;
  302. }
  303. int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
  304. u64 offset, u64 bytes)
  305. {
  306. int ret;
  307. mutex_lock(&block_group->alloc_mutex);
  308. ret = __btrfs_add_free_space(block_group, offset, bytes);
  309. mutex_unlock(&block_group->alloc_mutex);
  310. return ret;
  311. }
  312. int btrfs_add_free_space_lock(struct btrfs_block_group_cache *block_group,
  313. u64 offset, u64 bytes)
  314. {
  315. int ret;
  316. ret = __btrfs_add_free_space(block_group, offset, bytes);
  317. return ret;
  318. }
  319. int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  320. u64 offset, u64 bytes)
  321. {
  322. int ret = 0;
  323. mutex_lock(&block_group->alloc_mutex);
  324. ret = __btrfs_remove_free_space(block_group, offset, bytes);
  325. mutex_unlock(&block_group->alloc_mutex);
  326. return ret;
  327. }
  328. int btrfs_remove_free_space_lock(struct btrfs_block_group_cache *block_group,
  329. u64 offset, u64 bytes)
  330. {
  331. int ret;
  332. ret = __btrfs_remove_free_space(block_group, offset, bytes);
  333. return ret;
  334. }
  335. void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
  336. u64 bytes)
  337. {
  338. struct btrfs_free_space *info;
  339. struct rb_node *n;
  340. int count = 0;
  341. for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
  342. info = rb_entry(n, struct btrfs_free_space, offset_index);
  343. if (info->bytes >= bytes)
  344. count++;
  345. printk(KERN_ERR "entry offset %llu, bytes %llu\n", info->offset,
  346. info->bytes);
  347. }
  348. printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
  349. "\n", count);
  350. }
  351. u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
  352. {
  353. struct btrfs_free_space *info;
  354. struct rb_node *n;
  355. u64 ret = 0;
  356. for (n = rb_first(&block_group->free_space_offset); n;
  357. n = rb_next(n)) {
  358. info = rb_entry(n, struct btrfs_free_space, offset_index);
  359. ret += info->bytes;
  360. }
  361. return ret;
  362. }
  363. void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
  364. {
  365. struct btrfs_free_space *info;
  366. struct rb_node *node;
  367. mutex_lock(&block_group->alloc_mutex);
  368. while ((node = rb_last(&block_group->free_space_bytes)) != NULL) {
  369. info = rb_entry(node, struct btrfs_free_space, bytes_index);
  370. unlink_free_space(block_group, info);
  371. kfree(info);
  372. if (need_resched()) {
  373. mutex_unlock(&block_group->alloc_mutex);
  374. cond_resched();
  375. mutex_lock(&block_group->alloc_mutex);
  376. }
  377. }
  378. mutex_unlock(&block_group->alloc_mutex);
  379. }
  380. #if 0
  381. static struct btrfs_free_space *btrfs_find_free_space_offset(struct
  382. btrfs_block_group_cache
  383. *block_group, u64 offset,
  384. u64 bytes)
  385. {
  386. struct btrfs_free_space *ret;
  387. mutex_lock(&block_group->alloc_mutex);
  388. ret = tree_search_offset(&block_group->free_space_offset, offset,
  389. bytes, 0);
  390. mutex_unlock(&block_group->alloc_mutex);
  391. return ret;
  392. }
  393. static struct btrfs_free_space *btrfs_find_free_space_bytes(struct
  394. btrfs_block_group_cache
  395. *block_group, u64 offset,
  396. u64 bytes)
  397. {
  398. struct btrfs_free_space *ret;
  399. mutex_lock(&block_group->alloc_mutex);
  400. ret = tree_search_bytes(&block_group->free_space_bytes, offset, bytes);
  401. mutex_unlock(&block_group->alloc_mutex);
  402. return ret;
  403. }
  404. #endif
  405. struct btrfs_free_space *btrfs_find_free_space(struct btrfs_block_group_cache
  406. *block_group, u64 offset,
  407. u64 bytes)
  408. {
  409. struct btrfs_free_space *ret = NULL;
  410. ret = tree_search_offset(&block_group->free_space_offset, offset,
  411. bytes, 1);
  412. if (!ret)
  413. ret = tree_search_bytes(&block_group->free_space_bytes,
  414. offset, bytes);
  415. return ret;
  416. }