root-tree.c 4.5 KB

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