free-space-cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. If contains is set we will return
  60. * the free space that contains the given offset. If contains is not set we
  61. * will return the free space that starts at or after the given offset and is
  62. * at least bytes long.
  63. */
  64. static struct btrfs_free_space *tree_search_offset(struct rb_root *root,
  65. u64 offset, u64 bytes,
  66. int contains)
  67. {
  68. struct rb_node *n = root->rb_node;
  69. struct btrfs_free_space *entry, *ret = NULL;
  70. while (n) {
  71. entry = rb_entry(n, struct btrfs_free_space, offset_index);
  72. if (offset < entry->offset) {
  73. if (!contains &&
  74. (!ret || entry->offset < ret->offset) &&
  75. (bytes <= entry->bytes))
  76. ret = entry;
  77. n = n->rb_left;
  78. } else if (offset > entry->offset) {
  79. if ((entry->offset + entry->bytes - 1) >= offset &&
  80. bytes <= entry->bytes) {
  81. ret = entry;
  82. break;
  83. }
  84. n = n->rb_right;
  85. } else {
  86. if (bytes > entry->bytes) {
  87. n = n->rb_right;
  88. continue;
  89. }
  90. ret = entry;
  91. break;
  92. }
  93. }
  94. return ret;
  95. }
  96. /*
  97. * return a chunk at least bytes size, as close to offset that we can get.
  98. */
  99. static struct btrfs_free_space *tree_search_bytes(struct rb_root *root,
  100. u64 offset, u64 bytes)
  101. {
  102. struct rb_node *n = root->rb_node;
  103. struct btrfs_free_space *entry, *ret = NULL;
  104. while (n) {
  105. entry = rb_entry(n, struct btrfs_free_space, bytes_index);
  106. if (bytes < entry->bytes) {
  107. /*
  108. * We prefer to get a hole size as close to the size we
  109. * are asking for so we don't take small slivers out of
  110. * huge holes, but we also want to get as close to the
  111. * offset as possible so we don't have a whole lot of
  112. * fragmentation.
  113. */
  114. if (offset <= entry->offset) {
  115. if (!ret)
  116. ret = entry;
  117. else if (entry->bytes < ret->bytes)
  118. ret = entry;
  119. else if (entry->offset < ret->offset)
  120. ret = entry;
  121. }
  122. n = n->rb_left;
  123. } else if (bytes > entry->bytes) {
  124. n = n->rb_right;
  125. } else {
  126. /*
  127. * Ok we may have multiple chunks of the wanted size,
  128. * so we don't want to take the first one we find, we
  129. * want to take the one closest to our given offset, so
  130. * keep searching just in case theres a better match.
  131. */
  132. n = n->rb_right;
  133. if (offset > entry->offset)
  134. continue;
  135. else if (!ret || entry->offset < ret->offset)
  136. ret = entry;
  137. }
  138. }
  139. return ret;
  140. }
  141. static void unlink_free_space(struct btrfs_block_group_cache *block_group,
  142. struct btrfs_free_space *info)
  143. {
  144. rb_erase(&info->offset_index, &block_group->free_space_offset);
  145. rb_erase(&info->bytes_index, &block_group->free_space_bytes);
  146. }
  147. static int link_free_space(struct btrfs_block_group_cache *block_group,
  148. struct btrfs_free_space *info)
  149. {
  150. int ret = 0;
  151. ret = tree_insert_offset(&block_group->free_space_offset, info->offset,
  152. &info->offset_index);
  153. if (ret)
  154. return ret;
  155. ret = tree_insert_bytes(&block_group->free_space_bytes, info->bytes,
  156. &info->bytes_index);
  157. if (ret)
  158. return ret;
  159. return ret;
  160. }
  161. int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
  162. u64 offset, u64 bytes)
  163. {
  164. struct btrfs_free_space *right_info;
  165. struct btrfs_free_space *left_info;
  166. struct btrfs_free_space *info = NULL;
  167. struct btrfs_free_space *alloc_info;
  168. int ret = 0;
  169. alloc_info = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS);
  170. if (!alloc_info)
  171. return -ENOMEM;
  172. /*
  173. * first we want to see if there is free space adjacent to the range we
  174. * are adding, if there is remove that struct and add a new one to
  175. * cover the entire range
  176. */
  177. spin_lock(&block_group->lock);
  178. right_info = tree_search_offset(&block_group->free_space_offset,
  179. offset+bytes, 0, 1);
  180. left_info = tree_search_offset(&block_group->free_space_offset,
  181. offset-1, 0, 1);
  182. if (right_info && right_info->offset == offset+bytes) {
  183. unlink_free_space(block_group, right_info);
  184. info = right_info;
  185. info->offset = offset;
  186. info->bytes += bytes;
  187. } else if (right_info && right_info->offset != offset+bytes) {
  188. printk(KERN_ERR "adding space in the middle of an existing "
  189. "free space area. existing: offset=%Lu, bytes=%Lu. "
  190. "new: offset=%Lu, bytes=%Lu\n", right_info->offset,
  191. right_info->bytes, offset, bytes);
  192. BUG();
  193. }
  194. if (left_info) {
  195. unlink_free_space(block_group, left_info);
  196. if (unlikely((left_info->offset + left_info->bytes) !=
  197. offset)) {
  198. printk(KERN_ERR "free space to the left of new free "
  199. "space isn't quite right. existing: offset=%Lu,"
  200. " bytes=%Lu. new: offset=%Lu, bytes=%Lu\n",
  201. left_info->offset, left_info->bytes, offset,
  202. bytes);
  203. BUG();
  204. }
  205. if (info) {
  206. info->offset = left_info->offset;
  207. info->bytes += left_info->bytes;
  208. kfree(left_info);
  209. } else {
  210. info = left_info;
  211. info->bytes += bytes;
  212. }
  213. }
  214. if (info) {
  215. ret = link_free_space(block_group, info);
  216. if (!ret)
  217. info = NULL;
  218. goto out;
  219. }
  220. info = alloc_info;
  221. alloc_info = NULL;
  222. info->offset = offset;
  223. info->bytes = bytes;
  224. ret = link_free_space(block_group, info);
  225. if (ret)
  226. kfree(info);
  227. out:
  228. spin_unlock(&block_group->lock);
  229. if (ret) {
  230. printk(KERN_ERR "btrfs: unable to add free space :%d\n", ret);
  231. if (ret == -EEXIST)
  232. BUG();
  233. }
  234. if (alloc_info)
  235. kfree(alloc_info);
  236. return ret;
  237. }
  238. int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  239. u64 offset, u64 bytes)
  240. {
  241. struct btrfs_free_space *info;
  242. int ret = 0;
  243. spin_lock(&block_group->lock);
  244. info = tree_search_offset(&block_group->free_space_offset, offset, 0,
  245. 1);
  246. if (info && info->offset == offset) {
  247. if (info->bytes < bytes) {
  248. printk(KERN_ERR "Found free space at %Lu, size %Lu,"
  249. "trying to use %Lu\n",
  250. info->offset, info->bytes, bytes);
  251. WARN_ON(1);
  252. ret = -EINVAL;
  253. goto out;
  254. }
  255. unlink_free_space(block_group, info);
  256. if (info->bytes == bytes) {
  257. kfree(info);
  258. goto out;
  259. }
  260. info->offset += bytes;
  261. info->bytes -= bytes;
  262. ret = link_free_space(block_group, info);
  263. BUG_ON(ret);
  264. } else if (info && info->offset < offset &&
  265. info->offset + info->bytes >= offset + bytes) {
  266. u64 old_start = info->offset;
  267. /*
  268. * we're freeing space in the middle of the info,
  269. * this can happen during tree log replay
  270. *
  271. * first unlink the old info and then
  272. * insert it again after the hole we're creating
  273. */
  274. unlink_free_space(block_group, info);
  275. if (offset + bytes < info->offset + info->bytes) {
  276. u64 old_end = info->offset + info->bytes;
  277. info->offset = offset + bytes;
  278. info->bytes = old_end - info->offset;
  279. ret = link_free_space(block_group, info);
  280. BUG_ON(ret);
  281. } else {
  282. /* the hole we're creating ends at the end
  283. * of the info struct, just free the info
  284. */
  285. kfree(info);
  286. }
  287. /* step two, insert a new info struct to cover anything
  288. * before the hole
  289. */
  290. spin_unlock(&block_group->lock);
  291. ret = btrfs_add_free_space(block_group, old_start,
  292. offset - old_start);
  293. BUG_ON(ret);
  294. goto out_nolock;
  295. } else {
  296. WARN_ON(1);
  297. }
  298. out:
  299. spin_unlock(&block_group->lock);
  300. out_nolock:
  301. return ret;
  302. }
  303. void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
  304. u64 bytes)
  305. {
  306. struct btrfs_free_space *info;
  307. struct rb_node *n;
  308. int count = 0;
  309. for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
  310. info = rb_entry(n, struct btrfs_free_space, offset_index);
  311. if (info->bytes >= bytes)
  312. count++;
  313. //printk(KERN_INFO "offset=%Lu, bytes=%Lu\n", info->offset,
  314. // info->bytes);
  315. }
  316. printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
  317. "\n", count);
  318. }
  319. u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
  320. {
  321. struct btrfs_free_space *info;
  322. struct rb_node *n;
  323. u64 ret = 0;
  324. for (n = rb_first(&block_group->free_space_offset); n;
  325. n = rb_next(n)) {
  326. info = rb_entry(n, struct btrfs_free_space, offset_index);
  327. ret += info->bytes;
  328. }
  329. return ret;
  330. }
  331. void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
  332. {
  333. struct btrfs_free_space *info;
  334. struct rb_node *node;
  335. spin_lock(&block_group->lock);
  336. while ((node = rb_last(&block_group->free_space_bytes)) != NULL) {
  337. info = rb_entry(node, struct btrfs_free_space, bytes_index);
  338. unlink_free_space(block_group, info);
  339. kfree(info);
  340. if (need_resched()) {
  341. spin_unlock(&block_group->lock);
  342. cond_resched();
  343. spin_lock(&block_group->lock);
  344. }
  345. }
  346. spin_unlock(&block_group->lock);
  347. }
  348. struct btrfs_free_space *btrfs_find_free_space_offset(struct
  349. btrfs_block_group_cache
  350. *block_group, u64 offset,
  351. u64 bytes)
  352. {
  353. struct btrfs_free_space *ret;
  354. spin_lock(&block_group->lock);
  355. ret = tree_search_offset(&block_group->free_space_offset, offset,
  356. bytes, 0);
  357. spin_unlock(&block_group->lock);
  358. return ret;
  359. }
  360. struct btrfs_free_space *btrfs_find_free_space_bytes(struct
  361. btrfs_block_group_cache
  362. *block_group, u64 offset,
  363. u64 bytes)
  364. {
  365. struct btrfs_free_space *ret;
  366. spin_lock(&block_group->lock);
  367. ret = tree_search_bytes(&block_group->free_space_bytes, offset, bytes);
  368. spin_unlock(&block_group->lock);
  369. return ret;
  370. }
  371. struct btrfs_free_space *btrfs_find_free_space(struct btrfs_block_group_cache
  372. *block_group, u64 offset,
  373. u64 bytes)
  374. {
  375. struct btrfs_free_space *ret;
  376. spin_lock(&block_group->lock);
  377. ret = tree_search_offset(&block_group->free_space_offset, offset,
  378. bytes, 0);
  379. if (!ret)
  380. ret = tree_search_bytes(&block_group->free_space_bytes,
  381. offset, bytes);
  382. spin_unlock(&block_group->lock);
  383. return ret;
  384. }