root-tree.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "ctree.h"
  19. #include "transaction.h"
  20. #include "disk-io.h"
  21. #include "print-tree.h"
  22. /*
  23. * returns 0 on finding something, 1 if no more roots are there
  24. * and < 0 on error
  25. */
  26. int btrfs_search_root(struct btrfs_root *root, u64 search_start,
  27. u64 *found_objectid)
  28. {
  29. struct btrfs_path *path;
  30. struct btrfs_key search_key;
  31. int ret;
  32. root = root->fs_info->tree_root;
  33. search_key.objectid = search_start;
  34. search_key.type = (u8)-1;
  35. search_key.offset = (u64)-1;
  36. path = btrfs_alloc_path();
  37. BUG_ON(!path);
  38. again:
  39. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  40. if (ret < 0)
  41. goto out;
  42. if (ret == 0) {
  43. ret = 1;
  44. goto out;
  45. }
  46. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  47. ret = btrfs_next_leaf(root, path);
  48. if (ret)
  49. goto out;
  50. }
  51. btrfs_item_key_to_cpu(path->nodes[0], &search_key, path->slots[0]);
  52. if (search_key.type != BTRFS_ROOT_ITEM_KEY) {
  53. search_key.offset++;
  54. btrfs_release_path(root, path);
  55. goto again;
  56. }
  57. ret = 0;
  58. *found_objectid = search_key.objectid;
  59. out:
  60. btrfs_free_path(path);
  61. return ret;
  62. }
  63. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
  64. struct btrfs_root_item *item, struct btrfs_key *key)
  65. {
  66. struct btrfs_path *path;
  67. struct btrfs_key search_key;
  68. struct btrfs_key found_key;
  69. struct extent_buffer *l;
  70. int ret;
  71. int slot;
  72. search_key.objectid = objectid;
  73. search_key.type = (u8)-1;
  74. search_key.offset = (u64)-1;
  75. path = btrfs_alloc_path();
  76. BUG_ON(!path);
  77. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  78. if (ret < 0)
  79. goto out;
  80. BUG_ON(ret == 0);
  81. l = path->nodes[0];
  82. BUG_ON(path->slots[0] == 0);
  83. slot = path->slots[0] - 1;
  84. btrfs_item_key_to_cpu(l, &found_key, slot);
  85. if (found_key.objectid != objectid) {
  86. ret = 1;
  87. goto out;
  88. }
  89. read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
  90. sizeof(*item));
  91. memcpy(key, &found_key, sizeof(found_key));
  92. ret = 0;
  93. out:
  94. btrfs_free_path(path);
  95. return ret;
  96. }
  97. int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
  98. *root, struct btrfs_key *key, struct btrfs_root_item
  99. *item)
  100. {
  101. struct btrfs_path *path;
  102. struct extent_buffer *l;
  103. int ret;
  104. int slot;
  105. unsigned long ptr;
  106. path = btrfs_alloc_path();
  107. BUG_ON(!path);
  108. ret = btrfs_search_slot(trans, root, key, path, 0, 1);
  109. if (ret < 0)
  110. goto out;
  111. if (ret != 0) {
  112. btrfs_print_leaf(root, path->nodes[0]);
  113. printk("unable to update root key %Lu %u %Lu\n",
  114. key->objectid, key->type, key->offset);
  115. BUG_ON(1);
  116. }
  117. l = path->nodes[0];
  118. slot = path->slots[0];
  119. ptr = btrfs_item_ptr_offset(l, slot);
  120. write_extent_buffer(l, item, ptr, sizeof(*item));
  121. btrfs_mark_buffer_dirty(path->nodes[0]);
  122. out:
  123. btrfs_release_path(root, path);
  124. btrfs_free_path(path);
  125. return ret;
  126. }
  127. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
  128. *root, struct btrfs_key *key, struct btrfs_root_item
  129. *item)
  130. {
  131. int ret;
  132. ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
  133. return ret;
  134. }
  135. int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
  136. struct btrfs_root *latest)
  137. {
  138. struct btrfs_root *dead_root;
  139. struct btrfs_item *item;
  140. struct btrfs_root_item *ri;
  141. struct btrfs_key key;
  142. struct btrfs_key found_key;
  143. struct btrfs_path *path;
  144. int ret;
  145. u32 nritems;
  146. struct extent_buffer *leaf;
  147. int slot;
  148. key.objectid = objectid;
  149. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  150. key.offset = 0;
  151. path = btrfs_alloc_path();
  152. if (!path)
  153. return -ENOMEM;
  154. again:
  155. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  156. if (ret < 0)
  157. goto err;
  158. while(1) {
  159. leaf = path->nodes[0];
  160. nritems = btrfs_header_nritems(leaf);
  161. slot = path->slots[0];
  162. if (slot >= nritems) {
  163. ret = btrfs_next_leaf(root, path);
  164. if (ret)
  165. break;
  166. leaf = path->nodes[0];
  167. nritems = btrfs_header_nritems(leaf);
  168. slot = path->slots[0];
  169. }
  170. item = btrfs_item_nr(leaf, slot);
  171. btrfs_item_key_to_cpu(leaf, &key, slot);
  172. if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
  173. goto next;
  174. if (key.objectid < objectid)
  175. goto next;
  176. if (key.objectid > objectid)
  177. break;
  178. ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
  179. if (btrfs_disk_root_refs(leaf, ri) != 0)
  180. goto next;
  181. memcpy(&found_key, &key, sizeof(key));
  182. key.offset++;
  183. btrfs_release_path(root, path);
  184. dead_root = btrfs_read_fs_root_no_radix(root->fs_info,
  185. &found_key);
  186. if (IS_ERR(dead_root)) {
  187. ret = PTR_ERR(dead_root);
  188. goto err;
  189. }
  190. ret = btrfs_add_dead_root(dead_root, latest,
  191. &root->fs_info->dead_roots);
  192. if (ret)
  193. goto err;
  194. goto again;
  195. next:
  196. slot++;
  197. path->slots[0]++;
  198. }
  199. ret = 0;
  200. err:
  201. btrfs_free_path(path);
  202. return ret;
  203. }
  204. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  205. struct btrfs_key *key)
  206. {
  207. struct btrfs_path *path;
  208. int ret;
  209. u32 refs;
  210. struct btrfs_root_item *ri;
  211. struct extent_buffer *leaf;
  212. path = btrfs_alloc_path();
  213. BUG_ON(!path);
  214. ret = btrfs_search_slot(trans, root, key, path, -1, 1);
  215. if (ret < 0)
  216. goto out;
  217. if (ret) {
  218. btrfs_print_leaf(root, path->nodes[0]);
  219. printk("failed to del %Lu %u %Lu\n", key->objectid, key->type, key->offset);
  220. }
  221. BUG_ON(ret != 0);
  222. leaf = path->nodes[0];
  223. ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
  224. refs = btrfs_disk_root_refs(leaf, ri);
  225. BUG_ON(refs != 0);
  226. ret = btrfs_del_item(trans, root, path);
  227. out:
  228. btrfs_release_path(root, path);
  229. btrfs_free_path(path);
  230. return ret;
  231. }