free-space-cache.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 "btrfs adding space in the middle of an "
  188. "existing free space area. existing: "
  189. "offset=%llu, bytes=%llu. new: offset=%llu, "
  190. "bytes=%llu\n", (unsigned long long)right_info->offset,
  191. (unsigned long long)right_info->bytes,
  192. (unsigned long long)offset,
  193. (unsigned long long)bytes);
  194. BUG();
  195. }
  196. if (left_info) {
  197. unlink_free_space(block_group, left_info);
  198. if (unlikely((left_info->offset + left_info->bytes) !=
  199. offset)) {
  200. printk(KERN_ERR "btrfs free space to the left "
  201. "of new free space isn't "
  202. "quite right. existing: offset=%llu, "
  203. "bytes=%llu. new: offset=%llu, bytes=%llu\n",
  204. (unsigned long long)left_info->offset,
  205. (unsigned long long)left_info->bytes,
  206. (unsigned long long)offset,
  207. (unsigned long long)bytes);
  208. BUG();
  209. }
  210. if (info) {
  211. info->offset = left_info->offset;
  212. info->bytes += left_info->bytes;
  213. kfree(left_info);
  214. } else {
  215. info = left_info;
  216. info->bytes += bytes;
  217. }
  218. }
  219. if (info) {
  220. ret = link_free_space(block_group, info);
  221. if (!ret)
  222. info = NULL;
  223. goto out;
  224. }
  225. info = alloc_info;
  226. alloc_info = NULL;
  227. info->offset = offset;
  228. info->bytes = bytes;
  229. ret = link_free_space(block_group, info);
  230. if (ret)
  231. kfree(info);
  232. out:
  233. if (ret) {
  234. printk(KERN_ERR "btrfs: unable to add free space :%d\n", ret);
  235. if (ret == -EEXIST)
  236. BUG();
  237. }
  238. kfree(alloc_info);
  239. return ret;
  240. }
  241. static int
  242. __btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  243. u64 offset, u64 bytes)
  244. {
  245. struct btrfs_free_space *info;
  246. int ret = 0;
  247. info = tree_search_offset(&block_group->free_space_offset, offset, 0,
  248. 1);
  249. if (info && info->offset == offset) {
  250. if (info->bytes < bytes) {
  251. printk(KERN_ERR "Found free space at %llu, size %llu,"
  252. "trying to use %llu\n",
  253. (unsigned long long)info->offset,
  254. (unsigned long long)info->bytes,
  255. (unsigned long long)bytes);
  256. WARN_ON(1);
  257. ret = -EINVAL;
  258. goto out;
  259. }
  260. unlink_free_space(block_group, info);
  261. if (info->bytes == bytes) {
  262. kfree(info);
  263. goto out;
  264. }
  265. info->offset += bytes;
  266. info->bytes -= bytes;
  267. ret = link_free_space(block_group, info);
  268. BUG_ON(ret);
  269. } else if (info && info->offset < offset &&
  270. info->offset + info->bytes >= offset + bytes) {
  271. u64 old_start = info->offset;
  272. /*
  273. * we're freeing space in the middle of the info,
  274. * this can happen during tree log replay
  275. *
  276. * first unlink the old info and then
  277. * insert it again after the hole we're creating
  278. */
  279. unlink_free_space(block_group, info);
  280. if (offset + bytes < info->offset + info->bytes) {
  281. u64 old_end = info->offset + info->bytes;
  282. info->offset = offset + bytes;
  283. info->bytes = old_end - info->offset;
  284. ret = link_free_space(block_group, info);
  285. BUG_ON(ret);
  286. } else {
  287. /* the hole we're creating ends at the end
  288. * of the info struct, just free the info
  289. */
  290. kfree(info);
  291. }
  292. /* step two, insert a new info struct to cover anything
  293. * before the hole
  294. */
  295. ret = __btrfs_add_free_space(block_group, old_start,
  296. offset - old_start);
  297. BUG_ON(ret);
  298. } else {
  299. WARN_ON(1);
  300. }
  301. out:
  302. return ret;
  303. }
  304. int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
  305. u64 offset, u64 bytes)
  306. {
  307. int ret;
  308. struct btrfs_free_space *sp;
  309. mutex_lock(&block_group->alloc_mutex);
  310. ret = __btrfs_add_free_space(block_group, offset, bytes);
  311. sp = tree_search_offset(&block_group->free_space_offset, offset, 0, 1);
  312. BUG_ON(!sp);
  313. mutex_unlock(&block_group->alloc_mutex);
  314. return ret;
  315. }
  316. int btrfs_add_free_space_lock(struct btrfs_block_group_cache *block_group,
  317. u64 offset, u64 bytes)
  318. {
  319. int ret;
  320. struct btrfs_free_space *sp;
  321. ret = __btrfs_add_free_space(block_group, offset, bytes);
  322. sp = tree_search_offset(&block_group->free_space_offset, offset, 0, 1);
  323. BUG_ON(!sp);
  324. return ret;
  325. }
  326. int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  327. u64 offset, u64 bytes)
  328. {
  329. int ret = 0;
  330. mutex_lock(&block_group->alloc_mutex);
  331. ret = __btrfs_remove_free_space(block_group, offset, bytes);
  332. mutex_unlock(&block_group->alloc_mutex);
  333. return ret;
  334. }
  335. int btrfs_remove_free_space_lock(struct btrfs_block_group_cache *block_group,
  336. u64 offset, u64 bytes)
  337. {
  338. int ret;
  339. ret = __btrfs_remove_free_space(block_group, offset, bytes);
  340. return ret;
  341. }
  342. void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
  343. u64 bytes)
  344. {
  345. struct btrfs_free_space *info;
  346. struct rb_node *n;
  347. int count = 0;
  348. for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
  349. info = rb_entry(n, struct btrfs_free_space, offset_index);
  350. if (info->bytes >= bytes)
  351. count++;
  352. }
  353. printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
  354. "\n", count);
  355. }
  356. u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
  357. {
  358. struct btrfs_free_space *info;
  359. struct rb_node *n;
  360. u64 ret = 0;
  361. for (n = rb_first(&block_group->free_space_offset); n;
  362. n = rb_next(n)) {
  363. info = rb_entry(n, struct btrfs_free_space, offset_index);
  364. ret += info->bytes;
  365. }
  366. return ret;
  367. }
  368. void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
  369. {
  370. struct btrfs_free_space *info;
  371. struct rb_node *node;
  372. mutex_lock(&block_group->alloc_mutex);
  373. while ((node = rb_last(&block_group->free_space_bytes)) != NULL) {
  374. info = rb_entry(node, struct btrfs_free_space, bytes_index);
  375. unlink_free_space(block_group, info);
  376. kfree(info);
  377. if (need_resched()) {
  378. mutex_unlock(&block_group->alloc_mutex);
  379. cond_resched();
  380. mutex_lock(&block_group->alloc_mutex);
  381. }
  382. }
  383. mutex_unlock(&block_group->alloc_mutex);
  384. }
  385. #if 0
  386. static struct btrfs_free_space *btrfs_find_free_space_offset(struct
  387. btrfs_block_group_cache
  388. *block_group, u64 offset,
  389. u64 bytes)
  390. {
  391. struct btrfs_free_space *ret;
  392. mutex_lock(&block_group->alloc_mutex);
  393. ret = tree_search_offset(&block_group->free_space_offset, offset,
  394. bytes, 0);
  395. mutex_unlock(&block_group->alloc_mutex);
  396. return ret;
  397. }
  398. static struct btrfs_free_space *btrfs_find_free_space_bytes(struct
  399. btrfs_block_group_cache
  400. *block_group, u64 offset,
  401. u64 bytes)
  402. {
  403. struct btrfs_free_space *ret;
  404. mutex_lock(&block_group->alloc_mutex);
  405. ret = tree_search_bytes(&block_group->free_space_bytes, offset, bytes);
  406. mutex_unlock(&block_group->alloc_mutex);
  407. return ret;
  408. }
  409. #endif
  410. struct btrfs_free_space *btrfs_find_free_space(struct btrfs_block_group_cache
  411. *block_group, u64 offset,
  412. u64 bytes)
  413. {
  414. struct btrfs_free_space *ret = NULL;
  415. ret = tree_search_offset(&block_group->free_space_offset, offset,
  416. bytes, 0);
  417. if (!ret)
  418. ret = tree_search_bytes(&block_group->free_space_bytes,
  419. offset, bytes);
  420. return ret;
  421. }