tree-defrag.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/sched.h>
  19. #include "ctree.h"
  20. #include "disk-io.h"
  21. #include "print-tree.h"
  22. #include "transaction.h"
  23. static void reada_defrag(struct btrfs_root *root,
  24. struct extent_buffer *node)
  25. {
  26. int i;
  27. u32 nritems;
  28. u64 bytenr;
  29. u32 blocksize;
  30. int ret;
  31. blocksize = btrfs_level_size(root, btrfs_header_level(node) - 1);
  32. nritems = btrfs_header_nritems(node);
  33. for (i = 0; i < nritems; i++) {
  34. bytenr = btrfs_node_blockptr(node, i);
  35. ret = readahead_tree_block(root, bytenr, blocksize);
  36. if (ret)
  37. break;
  38. }
  39. }
  40. static int defrag_walk_down(struct btrfs_trans_handle *trans,
  41. struct btrfs_root *root,
  42. struct btrfs_path *path, int *level,
  43. int cache_only, u64 *last_ret)
  44. {
  45. struct extent_buffer *next;
  46. struct extent_buffer *cur;
  47. u64 bytenr;
  48. int ret = 0;
  49. int is_extent = 0;
  50. WARN_ON(*level < 0);
  51. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  52. if (root->fs_info->extent_root == root)
  53. is_extent = 1;
  54. while(*level > 0) {
  55. WARN_ON(*level < 0);
  56. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  57. cur = path->nodes[*level];
  58. if (!cache_only && *level > 1 && path->slots[*level] == 0)
  59. reada_defrag(root, cur);
  60. if (btrfs_header_level(cur) != *level)
  61. WARN_ON(1);
  62. if (path->slots[*level] >=
  63. btrfs_header_nritems(cur))
  64. break;
  65. if (*level == 1) {
  66. ret = btrfs_realloc_node(trans, root,
  67. path->nodes[*level],
  68. cache_only, last_ret);
  69. if (is_extent)
  70. btrfs_extent_post_op(trans, root);
  71. break;
  72. }
  73. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  74. if (cache_only) {
  75. next = btrfs_find_tree_block(root, bytenr,
  76. btrfs_level_size(root, *level - 1));
  77. /* FIXME, test for defrag */
  78. if (!next || !btrfs_buffer_uptodate(next)) {
  79. free_extent_buffer(next);
  80. path->slots[*level]++;
  81. continue;
  82. }
  83. } else {
  84. next = read_tree_block(root, bytenr,
  85. btrfs_level_size(root, *level - 1));
  86. }
  87. ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
  88. path->slots[*level], &next);
  89. BUG_ON(ret);
  90. ret = btrfs_realloc_node(trans, root, next, cache_only,
  91. last_ret);
  92. BUG_ON(ret);
  93. if (is_extent)
  94. btrfs_extent_post_op(trans, root);
  95. WARN_ON(*level <= 0);
  96. if (path->nodes[*level-1])
  97. free_extent_buffer(path->nodes[*level-1]);
  98. path->nodes[*level-1] = next;
  99. *level = btrfs_header_level(next);
  100. path->slots[*level] = 0;
  101. }
  102. WARN_ON(*level < 0);
  103. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  104. btrfs_clear_buffer_defrag(path->nodes[*level]);
  105. btrfs_clear_buffer_defrag_done(path->nodes[*level]);
  106. free_extent_buffer(path->nodes[*level]);
  107. path->nodes[*level] = NULL;
  108. *level += 1;
  109. WARN_ON(ret);
  110. return 0;
  111. }
  112. static int defrag_walk_up(struct btrfs_trans_handle *trans,
  113. struct btrfs_root *root,
  114. struct btrfs_path *path, int *level,
  115. int cache_only)
  116. {
  117. int i;
  118. int slot;
  119. struct extent_buffer *node;
  120. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  121. slot = path->slots[i];
  122. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  123. path->slots[i]++;
  124. *level = i;
  125. node = path->nodes[i];
  126. WARN_ON(i == 0);
  127. btrfs_node_key_to_cpu(node, &root->defrag_progress,
  128. path->slots[i]);
  129. root->defrag_level = i;
  130. return 0;
  131. } else {
  132. btrfs_clear_buffer_defrag(path->nodes[*level]);
  133. btrfs_clear_buffer_defrag_done(path->nodes[*level]);
  134. free_extent_buffer(path->nodes[*level]);
  135. path->nodes[*level] = NULL;
  136. *level = i + 1;
  137. }
  138. }
  139. return 1;
  140. }
  141. int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
  142. struct btrfs_root *root, int cache_only)
  143. {
  144. struct btrfs_path *path = NULL;
  145. struct extent_buffer *tmp;
  146. int ret = 0;
  147. int wret;
  148. int level;
  149. int orig_level;
  150. int i;
  151. int is_extent = 0;
  152. u64 last_ret = 0;
  153. if (root->fs_info->extent_root == root)
  154. is_extent = 1;
  155. if (root->ref_cows == 0 && !is_extent)
  156. goto out;
  157. path = btrfs_alloc_path();
  158. if (!path)
  159. return -ENOMEM;
  160. level = btrfs_header_level(root->node);
  161. orig_level = level;
  162. if (level == 0) {
  163. goto out;
  164. }
  165. if (root->defrag_progress.objectid == 0) {
  166. extent_buffer_get(root->node);
  167. ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
  168. BUG_ON(ret);
  169. ret = btrfs_realloc_node(trans, root, root->node, cache_only,
  170. &last_ret);
  171. BUG_ON(ret);
  172. path->nodes[level] = root->node;
  173. path->slots[level] = 0;
  174. if (is_extent)
  175. btrfs_extent_post_op(trans, root);
  176. } else {
  177. level = root->defrag_level;
  178. path->lowest_level = level;
  179. wret = btrfs_search_slot(trans, root, &root->defrag_progress,
  180. path, 0, 1);
  181. if (is_extent)
  182. btrfs_extent_post_op(trans, root);
  183. if (wret < 0) {
  184. ret = wret;
  185. goto out;
  186. }
  187. while(level > 0 && !path->nodes[level])
  188. level--;
  189. if (!path->nodes[level]) {
  190. ret = 0;
  191. goto out;
  192. }
  193. }
  194. while(1) {
  195. wret = defrag_walk_down(trans, root, path, &level, cache_only,
  196. &last_ret);
  197. if (wret > 0)
  198. break;
  199. if (wret < 0)
  200. ret = wret;
  201. wret = defrag_walk_up(trans, root, path, &level, cache_only);
  202. if (wret > 0)
  203. break;
  204. if (wret < 0)
  205. ret = wret;
  206. ret = -EAGAIN;
  207. break;
  208. }
  209. for (i = 0; i <= orig_level; i++) {
  210. if (path->nodes[i]) {
  211. free_extent_buffer(path->nodes[i]);
  212. path->nodes[i] = NULL;
  213. }
  214. }
  215. out:
  216. if (path)
  217. btrfs_free_path(path);
  218. if (ret != -EAGAIN) {
  219. memset(&root->defrag_progress, 0,
  220. sizeof(root->defrag_progress));
  221. }
  222. return ret;
  223. }