root-tree.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_leaf *l;
  28. int ret;
  29. int slot;
  30. search_key.objectid = objectid;
  31. search_key.flags = (u32)-1;
  32. search_key.offset = (u64)-1;
  33. path = btrfs_alloc_path();
  34. BUG_ON(!path);
  35. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  36. if (ret < 0)
  37. goto out;
  38. BUG_ON(ret == 0);
  39. l = btrfs_buffer_leaf(path->nodes[0]);
  40. BUG_ON(path->slots[0] == 0);
  41. slot = path->slots[0] - 1;
  42. if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
  43. ret = 1;
  44. goto out;
  45. }
  46. memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
  47. sizeof(*item));
  48. btrfs_disk_key_to_cpu(key, &l->items[slot].key);
  49. ret = 0;
  50. out:
  51. btrfs_release_path(root, path);
  52. btrfs_free_path(path);
  53. return ret;
  54. }
  55. int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
  56. *root, struct btrfs_key *key, struct btrfs_root_item
  57. *item)
  58. {
  59. struct btrfs_path *path;
  60. struct btrfs_leaf *l;
  61. int ret;
  62. int slot;
  63. struct btrfs_root_item *update_item;
  64. path = btrfs_alloc_path();
  65. BUG_ON(!path);
  66. ret = btrfs_search_slot(trans, root, key, path, 0, 1);
  67. if (ret < 0)
  68. goto out;
  69. BUG_ON(ret != 0);
  70. l = btrfs_buffer_leaf(path->nodes[0]);
  71. slot = path->slots[0];
  72. update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item);
  73. btrfs_memcpy(root, l, update_item, item, sizeof(*item));
  74. btrfs_mark_buffer_dirty(path->nodes[0]);
  75. out:
  76. btrfs_release_path(root, path);
  77. btrfs_free_path(path);
  78. return ret;
  79. }
  80. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
  81. *root, struct btrfs_key *key, struct btrfs_root_item
  82. *item)
  83. {
  84. int ret;
  85. ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
  86. return ret;
  87. }
  88. int btrfs_find_dead_roots(struct btrfs_root *root)
  89. {
  90. struct btrfs_root *dead_root;
  91. struct btrfs_item *item;
  92. struct btrfs_root_item *ri;
  93. struct btrfs_key key;
  94. struct btrfs_path *path;
  95. int ret;
  96. u32 nritems;
  97. struct btrfs_leaf *leaf;
  98. int slot;
  99. key.objectid = 0;
  100. key.flags = 0;
  101. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  102. key.offset = 0;
  103. path = btrfs_alloc_path();
  104. if (!path)
  105. return -ENOMEM;
  106. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  107. if (ret < 0)
  108. goto err;
  109. while(1) {
  110. leaf = btrfs_buffer_leaf(path->nodes[0]);
  111. nritems = btrfs_header_nritems(&leaf->header);
  112. slot = path->slots[0];
  113. if (slot >= nritems) {
  114. ret = btrfs_next_leaf(root, path);
  115. if (ret)
  116. break;
  117. leaf = btrfs_buffer_leaf(path->nodes[0]);
  118. nritems = btrfs_header_nritems(&leaf->header);
  119. slot = path->slots[0];
  120. }
  121. item = leaf->items + slot;
  122. btrfs_disk_key_to_cpu(&key, &item->key);
  123. if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
  124. goto next;
  125. ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
  126. if (btrfs_root_refs(ri) != 0)
  127. goto next;
  128. dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
  129. if (IS_ERR(dead_root)) {
  130. ret = PTR_ERR(dead_root);
  131. goto err;
  132. }
  133. ret = btrfs_add_dead_root(dead_root,
  134. &root->fs_info->dead_roots);
  135. if (ret)
  136. goto err;
  137. next:
  138. slot++;
  139. path->slots[0]++;
  140. }
  141. ret = 0;
  142. err:
  143. btrfs_free_path(path);
  144. return ret;
  145. }
  146. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  147. struct btrfs_key *key)
  148. {
  149. struct btrfs_path *path;
  150. int ret;
  151. u32 refs;
  152. struct btrfs_root_item *ri;
  153. path = btrfs_alloc_path();
  154. BUG_ON(!path);
  155. ret = btrfs_search_slot(trans, root, key, path, -1, 1);
  156. if (ret < 0)
  157. goto out;
  158. BUG_ON(ret != 0);
  159. ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  160. path->slots[0], struct btrfs_root_item);
  161. refs = btrfs_root_refs(ri);
  162. BUG_ON(refs != 0);
  163. ret = btrfs_del_item(trans, root, path);
  164. out:
  165. btrfs_release_path(root, path);
  166. btrfs_free_path(path);
  167. return ret;
  168. }