root-tree.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "kerncompat.h"
  4. #include "radix-tree.h"
  5. #include "ctree.h"
  6. #include "disk-io.h"
  7. #include "print-tree.h"
  8. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
  9. struct btrfs_root_item *item, struct btrfs_key *key)
  10. {
  11. struct btrfs_path path;
  12. struct btrfs_key search_key;
  13. struct btrfs_leaf *l;
  14. int ret;
  15. int slot;
  16. search_key.objectid = objectid;
  17. search_key.flags = (u32)-1;
  18. search_key.offset = (u32)-1;
  19. btrfs_init_path(&path);
  20. ret = btrfs_search_slot(root, &search_key, &path, 0, 0);
  21. if (ret < 0)
  22. goto out;
  23. BUG_ON(ret == 0);
  24. l = &path.nodes[0]->leaf;
  25. BUG_ON(path.slots[0] == 0);
  26. slot = path.slots[0] - 1;
  27. if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
  28. ret = 1;
  29. goto out;
  30. }
  31. memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
  32. sizeof(*item));
  33. btrfs_disk_key_to_cpu(key, &l->items[slot].key);
  34. btrfs_release_path(root, &path);
  35. ret = 0;
  36. out:
  37. return ret;
  38. }
  39. int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
  40. struct btrfs_root_item *item)
  41. {
  42. struct btrfs_path path;
  43. struct btrfs_leaf *l;
  44. int ret;
  45. int slot;
  46. btrfs_init_path(&path);
  47. ret = btrfs_search_slot(root, key, &path, 0, 1);
  48. if (ret < 0)
  49. goto out;
  50. BUG_ON(ret != 0);
  51. l = &path.nodes[0]->leaf;
  52. slot = path.slots[0];
  53. memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item,
  54. sizeof(*item));
  55. out:
  56. btrfs_release_path(root, &path);
  57. return ret;
  58. }
  59. int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
  60. struct btrfs_root_item *item)
  61. {
  62. int ret;
  63. ret = btrfs_insert_item(root, key, item, sizeof(*item));
  64. BUG_ON(ret);
  65. return ret;
  66. }
  67. int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key)
  68. {
  69. struct btrfs_path path;
  70. int ret;
  71. btrfs_init_path(&path);
  72. ret = btrfs_search_slot(root, key, &path, -1, 1);
  73. if (ret < 0)
  74. goto out;
  75. BUG_ON(ret != 0);
  76. ret = btrfs_del_item(root, &path);
  77. out:
  78. btrfs_release_path(root, &path);
  79. return ret;
  80. }