root-tree.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "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 = (u32)-1;
  33. path = btrfs_alloc_path();
  34. BUG_ON(!path);
  35. btrfs_init_path(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. btrfs_init_path(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 = btrfs_buffer_leaf(path->nodes[0]);
  73. slot = path->slots[0];
  74. update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item);
  75. btrfs_memcpy(root, l, update_item, item, 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. BUG_ON(ret);
  89. return ret;
  90. }
  91. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  92. struct btrfs_key *key)
  93. {
  94. struct btrfs_path *path;
  95. int ret;
  96. u32 refs;
  97. struct btrfs_root_item *ri;
  98. path = btrfs_alloc_path();
  99. BUG_ON(!path);
  100. btrfs_init_path(path);
  101. ret = btrfs_search_slot(trans, root, key, path, -1, 1);
  102. if (ret < 0)
  103. goto out;
  104. BUG_ON(ret != 0);
  105. ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  106. path->slots[0], struct btrfs_root_item);
  107. refs = btrfs_root_refs(ri);
  108. BUG_ON(refs == 0);
  109. if (refs == 1) {
  110. ret = btrfs_del_item(trans, root, path);
  111. } else {
  112. btrfs_set_root_refs(ri, refs - 1);
  113. WARN_ON(1);
  114. mark_buffer_dirty(path->nodes[0]);
  115. }
  116. out:
  117. btrfs_release_path(root, path);
  118. btrfs_free_path(path);
  119. return ret;
  120. }