13768f368SChris Mason #include <stdio.h> 23768f368SChris Mason #include <stdlib.h> 33768f368SChris Mason #include "kerncompat.h" 43768f368SChris Mason #include "radix-tree.h" 53768f368SChris Mason #include "ctree.h" 63768f368SChris Mason #include "disk-io.h" 73768f368SChris Mason #include "print-tree.h" 83768f368SChris Mason 93768f368SChris Mason int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, 103768f368SChris Mason struct btrfs_root_item *item, struct btrfs_key *key) 113768f368SChris Mason { 123768f368SChris Mason struct btrfs_path path; 133768f368SChris Mason struct btrfs_key search_key; 143768f368SChris Mason struct btrfs_leaf *l; 153768f368SChris Mason int ret; 163768f368SChris Mason int slot; 173768f368SChris Mason 183768f368SChris Mason search_key.objectid = objectid; 193768f368SChris Mason search_key.flags = (u32)-1; 203768f368SChris Mason search_key.offset = (u32)-1; 213768f368SChris Mason 223768f368SChris Mason btrfs_init_path(&path); 233768f368SChris Mason ret = btrfs_search_slot(root, &search_key, &path, 0, 0); 243768f368SChris Mason if (ret < 0) 253768f368SChris Mason goto out; 263768f368SChris Mason BUG_ON(ret == 0); 273768f368SChris Mason l = &path.nodes[0]->leaf; 283768f368SChris Mason BUG_ON(path.slots[0] == 0); 293768f368SChris Mason slot = path.slots[0] - 1; 303768f368SChris Mason if (btrfs_key_objectid(&l->items[slot].key) != objectid) { 313768f368SChris Mason ret = 1; 323768f368SChris Mason goto out; 333768f368SChris Mason } 34*123abc88SChris Mason memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item), 353768f368SChris Mason sizeof(*item)); 363768f368SChris Mason btrfs_disk_key_to_cpu(key, &l->items[slot].key); 373768f368SChris Mason btrfs_release_path(root, &path); 383768f368SChris Mason ret = 0; 393768f368SChris Mason out: 403768f368SChris Mason return ret; 413768f368SChris Mason } 423768f368SChris Mason 433768f368SChris Mason int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key, 443768f368SChris Mason struct btrfs_root_item *item) 453768f368SChris Mason { 463768f368SChris Mason struct btrfs_path path; 473768f368SChris Mason struct btrfs_leaf *l; 483768f368SChris Mason int ret; 493768f368SChris Mason int slot; 503768f368SChris Mason 513768f368SChris Mason btrfs_init_path(&path); 523768f368SChris Mason ret = btrfs_search_slot(root, key, &path, 0, 1); 533768f368SChris Mason if (ret < 0) 543768f368SChris Mason goto out; 553768f368SChris Mason BUG_ON(ret != 0); 563768f368SChris Mason l = &path.nodes[0]->leaf; 573768f368SChris Mason slot = path.slots[0]; 58*123abc88SChris Mason memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item, 593768f368SChris Mason sizeof(*item)); 603768f368SChris Mason out: 613768f368SChris Mason btrfs_release_path(root, &path); 623768f368SChris Mason return ret; 633768f368SChris Mason } 643768f368SChris Mason 653768f368SChris Mason int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key, 663768f368SChris Mason struct btrfs_root_item *item) 673768f368SChris Mason { 683768f368SChris Mason int ret; 693768f368SChris Mason ret = btrfs_insert_item(root, key, item, sizeof(*item)); 703768f368SChris Mason BUG_ON(ret); 713768f368SChris Mason return ret; 723768f368SChris Mason } 733768f368SChris Mason 743768f368SChris Mason int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key) 753768f368SChris Mason { 763768f368SChris Mason struct btrfs_path path; 773768f368SChris Mason int ret; 783768f368SChris Mason 793768f368SChris Mason btrfs_init_path(&path); 803768f368SChris Mason ret = btrfs_search_slot(root, key, &path, -1, 1); 813768f368SChris Mason if (ret < 0) 823768f368SChris Mason goto out; 833768f368SChris Mason BUG_ON(ret != 0); 843768f368SChris Mason ret = btrfs_del_item(root, &path); 853768f368SChris Mason out: 863768f368SChris Mason btrfs_release_path(root, &path); 873768f368SChris Mason return ret; 883768f368SChris Mason } 89