tree-defrag.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. u64 gen;
  30. u32 blocksize;
  31. int ret;
  32. blocksize = btrfs_level_size(root, btrfs_header_level(node) - 1);
  33. nritems = btrfs_header_nritems(node);
  34. for (i = 0; i < nritems; i++) {
  35. bytenr = btrfs_node_blockptr(node, i);
  36. gen = btrfs_node_ptr_generation(node, i);
  37. ret = readahead_tree_block(root, bytenr, blocksize, gen);
  38. if (ret)
  39. break;
  40. }
  41. }
  42. static int defrag_walk_down(struct btrfs_trans_handle *trans,
  43. struct btrfs_root *root,
  44. struct btrfs_path *path, int *level,
  45. int cache_only, u64 *last_ret)
  46. {
  47. struct extent_buffer *next;
  48. struct extent_buffer *cur;
  49. u64 bytenr;
  50. u64 ptr_gen;
  51. int ret = 0;
  52. int is_extent = 0;
  53. WARN_ON(*level < 0);
  54. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  55. if (root->fs_info->extent_root == root)
  56. is_extent = 1;
  57. if (*level == 1 && cache_only && path->nodes[1] &&
  58. !btrfs_buffer_defrag(path->nodes[1])) {
  59. goto out;
  60. }
  61. while(*level > 0) {
  62. WARN_ON(*level < 0);
  63. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  64. cur = path->nodes[*level];
  65. if (!cache_only && *level > 1 && path->slots[*level] == 0)
  66. reada_defrag(root, cur);
  67. if (btrfs_header_level(cur) != *level)
  68. WARN_ON(1);
  69. if (path->slots[*level] >=
  70. btrfs_header_nritems(cur))
  71. break;
  72. if (*level == 1) {
  73. WARN_ON(btrfs_header_generation(path->nodes[*level]) !=
  74. trans->transid);
  75. ret = btrfs_realloc_node(trans, root,
  76. path->nodes[*level],
  77. path->slots[*level],
  78. cache_only, last_ret,
  79. &root->defrag_progress);
  80. if (is_extent)
  81. btrfs_extent_post_op(trans, root);
  82. break;
  83. }
  84. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  85. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  86. if (cache_only) {
  87. next = btrfs_find_tree_block(root, bytenr,
  88. btrfs_level_size(root, *level - 1));
  89. if (!next || !btrfs_buffer_uptodate(next, ptr_gen) ||
  90. !btrfs_buffer_defrag(next)) {
  91. free_extent_buffer(next);
  92. path->slots[*level]++;
  93. continue;
  94. }
  95. } else {
  96. next = read_tree_block(root, bytenr,
  97. btrfs_level_size(root, *level - 1),
  98. ptr_gen);
  99. }
  100. ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
  101. path->slots[*level], &next);
  102. BUG_ON(ret);
  103. if (is_extent)
  104. btrfs_extent_post_op(trans, root);
  105. WARN_ON(*level <= 0);
  106. if (path->nodes[*level-1])
  107. free_extent_buffer(path->nodes[*level-1]);
  108. path->nodes[*level-1] = next;
  109. *level = btrfs_header_level(next);
  110. path->slots[*level] = 0;
  111. }
  112. WARN_ON(*level < 0);
  113. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  114. btrfs_clear_buffer_defrag(path->nodes[*level]);
  115. out:
  116. free_extent_buffer(path->nodes[*level]);
  117. path->nodes[*level] = NULL;
  118. *level += 1;
  119. WARN_ON(ret && ret != -EAGAIN);
  120. return ret;
  121. }
  122. static int defrag_walk_up(struct btrfs_trans_handle *trans,
  123. struct btrfs_root *root,
  124. struct btrfs_path *path, int *level,
  125. int cache_only)
  126. {
  127. int i;
  128. int slot;
  129. struct extent_buffer *node;
  130. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  131. slot = path->slots[i];
  132. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  133. path->slots[i]++;
  134. *level = i;
  135. node = path->nodes[i];
  136. WARN_ON(i == 0);
  137. btrfs_node_key_to_cpu(node, &root->defrag_progress,
  138. path->slots[i]);
  139. root->defrag_level = i;
  140. return 0;
  141. } else {
  142. btrfs_clear_buffer_defrag(path->nodes[*level]);
  143. free_extent_buffer(path->nodes[*level]);
  144. path->nodes[*level] = NULL;
  145. *level = i + 1;
  146. }
  147. }
  148. return 1;
  149. }
  150. int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
  151. struct btrfs_root *root, int cache_only)
  152. {
  153. struct btrfs_path *path = NULL;
  154. struct extent_buffer *tmp;
  155. int ret = 0;
  156. int wret;
  157. int level;
  158. int orig_level;
  159. int i;
  160. int is_extent = 0;
  161. u64 last_ret = 0;
  162. if (root->fs_info->extent_root == root)
  163. is_extent = 1;
  164. goto out;
  165. if (root->ref_cows == 0 && !is_extent)
  166. goto out;
  167. if (btrfs_test_opt(root, SSD))
  168. goto out;
  169. path = btrfs_alloc_path();
  170. if (!path)
  171. return -ENOMEM;
  172. level = btrfs_header_level(root->node);
  173. orig_level = level;
  174. if (level == 0) {
  175. goto out;
  176. }
  177. if (root->defrag_progress.objectid == 0) {
  178. u32 nritems;
  179. nritems = btrfs_header_nritems(root->node);
  180. root->defrag_max.objectid = 0;
  181. /* from above we know this is not a leaf */
  182. btrfs_node_key_to_cpu(root->node, &root->defrag_max,
  183. nritems - 1);
  184. extent_buffer_get(root->node);
  185. ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
  186. BUG_ON(ret);
  187. path->nodes[level] = root->node;
  188. path->slots[level] = 0;
  189. if (is_extent)
  190. btrfs_extent_post_op(trans, root);
  191. } else {
  192. level = root->defrag_level;
  193. path->lowest_level = level;
  194. wret = btrfs_search_slot(trans, root, &root->defrag_progress,
  195. path, 0, 1);
  196. if (is_extent)
  197. btrfs_extent_post_op(trans, root);
  198. if (wret < 0) {
  199. ret = wret;
  200. goto out;
  201. }
  202. while(level > 0 && !path->nodes[level])
  203. level--;
  204. if (!path->nodes[level]) {
  205. ret = 0;
  206. goto out;
  207. }
  208. }
  209. while(1) {
  210. wret = defrag_walk_down(trans, root, path, &level, cache_only,
  211. &last_ret);
  212. if (wret > 0)
  213. break;
  214. if (wret < 0)
  215. ret = wret;
  216. wret = defrag_walk_up(trans, root, path, &level, cache_only);
  217. if (wret > 0)
  218. break;
  219. if (wret < 0)
  220. ret = wret;
  221. else
  222. ret = -EAGAIN;
  223. break;
  224. }
  225. for (i = 0; i <= orig_level; i++) {
  226. if (path->nodes[i]) {
  227. free_extent_buffer(path->nodes[i]);
  228. path->nodes[i] = NULL;
  229. }
  230. }
  231. out:
  232. if (path)
  233. btrfs_free_path(path);
  234. if (ret == -EAGAIN) {
  235. if (root->defrag_max.objectid > root->defrag_progress.objectid)
  236. goto done;
  237. if (root->defrag_max.type > root->defrag_progress.type)
  238. goto done;
  239. if (root->defrag_max.offset > root->defrag_progress.offset)
  240. goto done;
  241. ret = 0;
  242. }
  243. done:
  244. if (ret != -EAGAIN) {
  245. memset(&root->defrag_progress, 0,
  246. sizeof(root->defrag_progress));
  247. }
  248. return ret;
  249. }