root-tree.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
  23. struct btrfs_root_item *item, struct btrfs_key *key)
  24. {
  25. struct btrfs_path *path;
  26. struct btrfs_key search_key;
  27. struct btrfs_key found_key;
  28. struct extent_buffer *l;
  29. int ret;
  30. int slot;
  31. search_key.objectid = objectid;
  32. search_key.type = (u8)-1;
  33. search_key.offset = (u64)-1;
  34. path = btrfs_alloc_path();
  35. BUG_ON(!path);
  36. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  37. if (ret < 0)
  38. goto out;
  39. BUG_ON(ret == 0);
  40. l = path->nodes[0];
  41. BUG_ON(path->slots[0] == 0);
  42. slot = path->slots[0] - 1;
  43. btrfs_item_key_to_cpu(l, &found_key, slot);
  44. if (found_key.objectid != objectid) {
  45. ret = 1;
  46. goto out;
  47. }
  48. read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
  49. sizeof(*item));
  50. memcpy(key, &found_key, sizeof(found_key));
  51. ret = 0;
  52. out:
  53. btrfs_release_path(root, path);
  54. btrfs_free_path(path);
  55. return ret;
  56. }
  57. int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
  58. *root, struct btrfs_key *key, struct btrfs_root_item
  59. *item)
  60. {
  61. struct btrfs_path *path;
  62. struct extent_buffer *l;
  63. int ret;
  64. int slot;
  65. unsigned long ptr;
  66. path = btrfs_alloc_path();
  67. BUG_ON(!path);
  68. ret = btrfs_search_slot(trans, root, key, path, 0, 1);
  69. if (ret < 0)
  70. goto out;
  71. if (ret != 0) {
  72. btrfs_print_leaf(root, path->nodes[0]);
  73. printk("unable to update root key %Lu %u %Lu\n",
  74. key->objectid, key->type, key->offset);
  75. BUG_ON(1);
  76. }
  77. l = path->nodes[0];
  78. slot = path->slots[0];
  79. ptr = btrfs_item_ptr_offset(l, slot);
  80. write_extent_buffer(l, item, ptr, sizeof(*item));
  81. btrfs_mark_buffer_dirty(path->nodes[0]);
  82. out:
  83. btrfs_release_path(root, path);
  84. btrfs_free_path(path);
  85. return ret;
  86. }
  87. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
  88. *root, struct btrfs_key *key, struct btrfs_root_item
  89. *item)
  90. {
  91. int ret;
  92. ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
  93. return ret;
  94. }
  95. int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
  96. struct btrfs_root *latest)
  97. {
  98. struct btrfs_root *dead_root;
  99. struct btrfs_item *item;
  100. struct btrfs_root_item *ri;
  101. struct btrfs_key key;
  102. struct btrfs_path *path;
  103. int ret;
  104. u32 nritems;
  105. struct extent_buffer *leaf;
  106. int slot;
  107. key.objectid = objectid;
  108. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  109. key.offset = 0;
  110. path = btrfs_alloc_path();
  111. if (!path)
  112. return -ENOMEM;
  113. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  114. if (ret < 0)
  115. goto err;
  116. while(1) {
  117. leaf = path->nodes[0];
  118. nritems = btrfs_header_nritems(leaf);
  119. slot = path->slots[0];
  120. if (slot >= nritems) {
  121. ret = btrfs_next_leaf(root, path);
  122. if (ret)
  123. break;
  124. leaf = path->nodes[0];
  125. nritems = btrfs_header_nritems(leaf);
  126. slot = path->slots[0];
  127. }
  128. item = btrfs_item_nr(leaf, slot);
  129. btrfs_item_key_to_cpu(leaf, &key, slot);
  130. if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
  131. goto next;
  132. if (key.objectid < objectid)
  133. goto next;
  134. if (key.objectid > objectid)
  135. break;
  136. ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
  137. if (btrfs_disk_root_refs(leaf, ri) != 0)
  138. goto next;
  139. dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
  140. if (IS_ERR(dead_root)) {
  141. ret = PTR_ERR(dead_root);
  142. goto err;
  143. }
  144. ret = btrfs_add_dead_root(dead_root, latest,
  145. &root->fs_info->dead_roots);
  146. if (ret)
  147. goto err;
  148. next:
  149. slot++;
  150. path->slots[0]++;
  151. }
  152. ret = 0;
  153. err:
  154. btrfs_free_path(path);
  155. return ret;
  156. }
  157. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  158. struct btrfs_key *key)
  159. {
  160. struct btrfs_path *path;
  161. int ret;
  162. u32 refs;
  163. struct btrfs_root_item *ri;
  164. struct extent_buffer *leaf;
  165. path = btrfs_alloc_path();
  166. BUG_ON(!path);
  167. ret = btrfs_search_slot(trans, root, key, path, -1, 1);
  168. if (ret < 0)
  169. goto out;
  170. if (ret) {
  171. btrfs_print_leaf(root, path->nodes[0]);
  172. printk("failed to del %Lu %u %Lu\n", key->objectid, key->type, key->offset);
  173. }
  174. BUG_ON(ret != 0);
  175. leaf = path->nodes[0];
  176. ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
  177. refs = btrfs_disk_root_refs(leaf, ri);
  178. BUG_ON(refs != 0);
  179. ret = btrfs_del_item(trans, root, path);
  180. out:
  181. btrfs_release_path(root, path);
  182. btrfs_free_path(path);
  183. return ret;
  184. }