tree-defrag.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 btrfs_node *node)
  25. {
  26. int i;
  27. u32 nritems;
  28. u64 blocknr;
  29. int ret;
  30. nritems = btrfs_header_nritems(&node->header);
  31. for (i = 0; i < nritems; i++) {
  32. blocknr = btrfs_node_blockptr(node, i);
  33. ret = readahead_tree_block(root, blocknr);
  34. if (ret)
  35. break;
  36. }
  37. }
  38. static int defrag_walk_down(struct btrfs_trans_handle *trans,
  39. struct btrfs_root *root,
  40. struct btrfs_path *path, int *level,
  41. int cache_only)
  42. {
  43. struct buffer_head *next;
  44. struct buffer_head *cur;
  45. u64 blocknr;
  46. int ret = 0;
  47. WARN_ON(*level < 0);
  48. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  49. while(*level > 0) {
  50. WARN_ON(*level < 0);
  51. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  52. cur = path->nodes[*level];
  53. if (!cache_only && *level > 1 && path->slots[*level] == 0)
  54. reada_defrag(root, btrfs_buffer_node(cur));
  55. if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
  56. WARN_ON(1);
  57. if (path->slots[*level] >=
  58. btrfs_header_nritems(btrfs_buffer_header(cur)))
  59. break;
  60. if (*level == 1) {
  61. ret = btrfs_realloc_node(trans, root,
  62. path->nodes[*level],
  63. cache_only);
  64. break;
  65. }
  66. blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
  67. path->slots[*level]);
  68. if (cache_only) {
  69. next = btrfs_find_tree_block(root, blocknr);
  70. if (!next || !buffer_uptodate(next) ||
  71. buffer_locked(next)) {
  72. brelse(next);
  73. path->slots[*level]++;
  74. continue;
  75. }
  76. } else {
  77. next = read_tree_block(root, blocknr);
  78. }
  79. ret = btrfs_cow_block(trans, root, next, path->nodes[*level],
  80. path->slots[*level], &next);
  81. BUG_ON(ret);
  82. ret = btrfs_realloc_node(trans, root, next, cache_only);
  83. BUG_ON(ret);
  84. WARN_ON(*level <= 0);
  85. if (path->nodes[*level-1])
  86. btrfs_block_release(root, path->nodes[*level-1]);
  87. path->nodes[*level-1] = next;
  88. *level = btrfs_header_level(btrfs_buffer_header(next));
  89. path->slots[*level] = 0;
  90. }
  91. WARN_ON(*level < 0);
  92. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  93. btrfs_block_release(root, path->nodes[*level]);
  94. path->nodes[*level] = NULL;
  95. *level += 1;
  96. WARN_ON(ret);
  97. return 0;
  98. }
  99. static int defrag_walk_up(struct btrfs_trans_handle *trans,
  100. struct btrfs_root *root,
  101. struct btrfs_path *path, int *level,
  102. int cache_only)
  103. {
  104. int i;
  105. int slot;
  106. struct btrfs_node *node;
  107. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  108. slot = path->slots[i];
  109. if (slot < btrfs_header_nritems(
  110. btrfs_buffer_header(path->nodes[i])) - 1) {
  111. path->slots[i]++;
  112. *level = i;
  113. node = btrfs_buffer_node(path->nodes[i]);
  114. WARN_ON(i == 0);
  115. btrfs_disk_key_to_cpu(&root->defrag_progress,
  116. &node->ptrs[path->slots[i]].key);
  117. root->defrag_level = i;
  118. return 0;
  119. } else {
  120. btrfs_block_release(root, path->nodes[*level]);
  121. path->nodes[*level] = NULL;
  122. *level = i + 1;
  123. }
  124. }
  125. return 1;
  126. }
  127. int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
  128. struct btrfs_root *root, int cache_only)
  129. {
  130. struct btrfs_path *path = NULL;
  131. struct buffer_head *tmp;
  132. int ret = 0;
  133. int wret;
  134. int level;
  135. int orig_level;
  136. int i;
  137. int num_runs = 0;
  138. if (root->ref_cows == 0) {
  139. goto out;
  140. }
  141. path = btrfs_alloc_path();
  142. if (!path)
  143. return -ENOMEM;
  144. level = btrfs_header_level(btrfs_buffer_header(root->node));
  145. orig_level = level;
  146. if (level == 0) {
  147. goto out;
  148. }
  149. if (root->defrag_progress.objectid == 0) {
  150. get_bh(root->node);
  151. ret = btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
  152. BUG_ON(ret);
  153. ret = btrfs_realloc_node(trans, root, root->node, cache_only);
  154. BUG_ON(ret);
  155. path->nodes[level] = root->node;
  156. path->slots[level] = 0;
  157. } else {
  158. level = root->defrag_level;
  159. path->lowest_level = level;
  160. wret = btrfs_search_slot(trans, root, &root->defrag_progress,
  161. path, 0, 1);
  162. if (wret < 0) {
  163. ret = wret;
  164. goto out;
  165. }
  166. while(level > 0 && !path->nodes[level])
  167. level--;
  168. if (!path->nodes[level]) {
  169. ret = 0;
  170. goto out;
  171. }
  172. }
  173. while(1) {
  174. wret = defrag_walk_down(trans, root, path, &level, cache_only);
  175. if (wret > 0)
  176. break;
  177. if (wret < 0)
  178. ret = wret;
  179. wret = defrag_walk_up(trans, root, path, &level, cache_only);
  180. if (wret > 0)
  181. break;
  182. if (wret < 0)
  183. ret = wret;
  184. if (num_runs++ > 8) {
  185. ret = -EAGAIN;
  186. break;
  187. }
  188. }
  189. for (i = 0; i <= orig_level; i++) {
  190. if (path->nodes[i]) {
  191. btrfs_block_release(root, path->nodes[i]);
  192. path->nodes[i] = 0;
  193. }
  194. }
  195. out:
  196. if (path)
  197. btrfs_free_path(path);
  198. if (ret != -EAGAIN) {
  199. memset(&root->defrag_progress, 0,
  200. sizeof(root->defrag_progress));
  201. }
  202. return ret;
  203. }