root-tree.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. BUG_ON(ret != 0);
  72. l = path->nodes[0];
  73. slot = path->slots[0];
  74. ptr = btrfs_item_ptr_offset(l, slot);
  75. write_extent_buffer(l, item, ptr, sizeof(*item));
  76. btrfs_mark_buffer_dirty(path->nodes[0]);
  77. out:
  78. btrfs_release_path(root, path);
  79. btrfs_free_path(path);
  80. return ret;
  81. }
  82. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
  83. *root, struct btrfs_key *key, struct btrfs_root_item
  84. *item)
  85. {
  86. int ret;
  87. ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
  88. return ret;
  89. }
  90. int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
  91. struct btrfs_root *latest)
  92. {
  93. struct btrfs_root *dead_root;
  94. struct btrfs_item *item;
  95. struct btrfs_root_item *ri;
  96. struct btrfs_key key;
  97. struct btrfs_path *path;
  98. int ret;
  99. u32 nritems;
  100. struct extent_buffer *leaf;
  101. int slot;
  102. key.objectid = objectid;
  103. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  104. key.offset = 0;
  105. path = btrfs_alloc_path();
  106. if (!path)
  107. return -ENOMEM;
  108. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  109. if (ret < 0)
  110. goto err;
  111. while(1) {
  112. leaf = path->nodes[0];
  113. nritems = btrfs_header_nritems(leaf);
  114. slot = path->slots[0];
  115. if (slot >= nritems) {
  116. ret = btrfs_next_leaf(root, path);
  117. if (ret)
  118. break;
  119. leaf = path->nodes[0];
  120. nritems = btrfs_header_nritems(leaf);
  121. slot = path->slots[0];
  122. }
  123. item = btrfs_item_nr(leaf, slot);
  124. btrfs_item_key_to_cpu(leaf, &key, slot);
  125. if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
  126. goto next;
  127. if (key.objectid < objectid)
  128. goto next;
  129. if (key.objectid > objectid)
  130. break;
  131. ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
  132. if (btrfs_disk_root_refs(leaf, ri) != 0)
  133. goto next;
  134. dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
  135. if (IS_ERR(dead_root)) {
  136. ret = PTR_ERR(dead_root);
  137. goto err;
  138. }
  139. ret = btrfs_add_dead_root(dead_root, latest,
  140. &root->fs_info->dead_roots);
  141. if (ret)
  142. goto err;
  143. next:
  144. slot++;
  145. path->slots[0]++;
  146. }
  147. ret = 0;
  148. err:
  149. btrfs_free_path(path);
  150. return ret;
  151. }
  152. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  153. struct btrfs_key *key)
  154. {
  155. struct btrfs_path *path;
  156. int ret;
  157. u32 refs;
  158. struct btrfs_root_item *ri;
  159. struct extent_buffer *leaf;
  160. path = btrfs_alloc_path();
  161. BUG_ON(!path);
  162. ret = btrfs_search_slot(trans, root, key, path, -1, 1);
  163. if (ret < 0)
  164. goto out;
  165. BUG_ON(ret != 0);
  166. leaf = path->nodes[0];
  167. ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
  168. refs = btrfs_disk_root_refs(leaf, ri);
  169. BUG_ON(refs != 0);
  170. ret = btrfs_del_item(trans, root, path);
  171. out:
  172. btrfs_release_path(root, path);
  173. btrfs_free_path(path);
  174. return ret;
  175. }