free-space-cache.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. static 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. right_info = tree_search_offset(&block_group->free_space_offset,
  178. offset+bytes, 0, 1);
  179. left_info = tree_search_offset(&block_group->free_space_offset,
  180. offset-1, 0, 1);
  181. if (right_info && right_info->offset == offset+bytes) {
  182. unlink_free_space(block_group, right_info);
  183. info = right_info;
  184. info->offset = offset;
  185. info->bytes += bytes;
  186. } else if (right_info && right_info->offset != offset+bytes) {
  187. printk(KERN_ERR "adding space in the middle of an existing "
  188. "free space area. existing: offset=%Lu, bytes=%Lu. "
  189. "new: offset=%Lu, bytes=%Lu\n", right_info->offset,
  190. right_info->bytes, offset, bytes);
  191. BUG();
  192. }
  193. if (left_info) {
  194. unlink_free_space(block_group, left_info);
  195. if (unlikely((left_info->offset + left_info->bytes) !=
  196. offset)) {
  197. printk(KERN_ERR "free space to the left of new free "
  198. "space isn't quite right. existing: offset=%Lu,"
  199. " bytes=%Lu. new: offset=%Lu, bytes=%Lu\n",
  200. left_info->offset, left_info->bytes, offset,
  201. bytes);
  202. BUG();
  203. }
  204. if (info) {
  205. info->offset = left_info->offset;
  206. info->bytes += left_info->bytes;
  207. kfree(left_info);
  208. } else {
  209. info = left_info;
  210. info->bytes += bytes;
  211. }
  212. }
  213. if (info) {
  214. ret = link_free_space(block_group, info);
  215. if (!ret)
  216. info = NULL;
  217. goto out;
  218. }
  219. info = alloc_info;
  220. alloc_info = NULL;
  221. info->offset = offset;
  222. info->bytes = bytes;
  223. ret = link_free_space(block_group, info);
  224. if (ret)
  225. kfree(info);
  226. out:
  227. if (ret) {
  228. printk(KERN_ERR "btrfs: unable to add free space :%d\n", ret);
  229. if (ret == -EEXIST)
  230. BUG();
  231. }
  232. if (alloc_info)
  233. kfree(alloc_info);
  234. return ret;
  235. }
  236. static int
  237. __btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  238. u64 offset, u64 bytes)
  239. {
  240. struct btrfs_free_space *info;
  241. int ret = 0;
  242. info = tree_search_offset(&block_group->free_space_offset, offset, 0,
  243. 1);
  244. if (info && info->offset == offset) {
  245. if (info->bytes < bytes) {
  246. printk(KERN_ERR "Found free space at %Lu, size %Lu,"
  247. "trying to use %Lu\n",
  248. info->offset, info->bytes, bytes);
  249. WARN_ON(1);
  250. ret = -EINVAL;
  251. goto out;
  252. }
  253. unlink_free_space(block_group, info);
  254. if (info->bytes == bytes) {
  255. kfree(info);
  256. goto out;
  257. }
  258. info->offset += bytes;
  259. info->bytes -= bytes;
  260. ret = link_free_space(block_group, info);
  261. BUG_ON(ret);
  262. } else if (info && info->offset < offset &&
  263. info->offset + info->bytes >= offset + bytes) {
  264. u64 old_start = info->offset;
  265. /*
  266. * we're freeing space in the middle of the info,
  267. * this can happen during tree log replay
  268. *
  269. * first unlink the old info and then
  270. * insert it again after the hole we're creating
  271. */
  272. unlink_free_space(block_group, info);
  273. if (offset + bytes < info->offset + info->bytes) {
  274. u64 old_end = info->offset + info->bytes;
  275. info->offset = offset + bytes;
  276. info->bytes = old_end - info->offset;
  277. ret = link_free_space(block_group, info);
  278. BUG_ON(ret);
  279. } else {
  280. /* the hole we're creating ends at the end
  281. * of the info struct, just free the info
  282. */
  283. kfree(info);
  284. }
  285. /* step two, insert a new info struct to cover anything
  286. * before the hole
  287. */
  288. ret = __btrfs_add_free_space(block_group, old_start,
  289. offset - old_start);
  290. BUG_ON(ret);
  291. } else {
  292. WARN_ON(1);
  293. }
  294. out:
  295. return ret;
  296. }
  297. int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
  298. u64 offset, u64 bytes)
  299. {
  300. int ret;
  301. struct btrfs_free_space *sp;
  302. mutex_lock(&block_group->alloc_mutex);
  303. ret = __btrfs_add_free_space(block_group, offset, bytes);
  304. sp = tree_search_offset(&block_group->free_space_offset, offset, 0, 1);
  305. BUG_ON(!sp);
  306. mutex_unlock(&block_group->alloc_mutex);
  307. return ret;
  308. }
  309. int btrfs_add_free_space_lock(struct btrfs_block_group_cache *block_group,
  310. u64 offset, u64 bytes)
  311. {
  312. int ret;
  313. struct btrfs_free_space *sp;
  314. ret = __btrfs_add_free_space(block_group, offset, bytes);
  315. sp = tree_search_offset(&block_group->free_space_offset, offset, 0, 1);
  316. BUG_ON(!sp);
  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_INFO "offset=%Lu, bytes=%Lu\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, 0);
  412. if (!ret)
  413. ret = tree_search_bytes(&block_group->free_space_bytes,
  414. offset, bytes);
  415. return ret;
  416. }