root-tree.c 2.0 KB

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