root-tree.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include "ctree.h"
  3. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
  4. struct btrfs_root_item *item, struct btrfs_key *key)
  5. {
  6. struct btrfs_path *path;
  7. struct btrfs_key search_key;
  8. struct btrfs_key found_key;
  9. struct extent_buffer *l;
  10. int ret;
  11. int slot;
  12. path = btrfs_alloc_path();
  13. if (!path)
  14. return -ENOMEM;
  15. search_key.objectid = objectid;
  16. search_key.type = BTRFS_ROOT_ITEM_KEY;
  17. search_key.offset = (u64)-1;
  18. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  19. if (ret < 0)
  20. goto out;
  21. if (path->slots[0] == 0) {
  22. ret = -ENOENT;
  23. goto out;
  24. }
  25. BUG_ON(ret == 0);
  26. l = path->nodes[0];
  27. slot = path->slots[0] - 1;
  28. btrfs_item_key_to_cpu(l, &found_key, slot);
  29. if (found_key.type != BTRFS_ROOT_ITEM_KEY ||
  30. found_key.objectid != objectid) {
  31. ret = -ENOENT;
  32. goto out;
  33. }
  34. read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
  35. sizeof(*item));
  36. memcpy(key, &found_key, sizeof(found_key));
  37. ret = 0;
  38. out:
  39. btrfs_free_path(path);
  40. return ret;
  41. }