1 #include <linux/module.h>
4 #include "print-tree.h"
6 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
7 struct btrfs_root_item *item, struct btrfs_key *key)
9 struct btrfs_path *path;
10 struct btrfs_key search_key;
15 search_key.objectid = objectid;
16 search_key.flags = (u32)-1;
17 search_key.offset = (u32)-1;
19 path = btrfs_alloc_path();
21 btrfs_init_path(path);
22 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
26 l = btrfs_buffer_leaf(path->nodes[0]);
27 BUG_ON(path->slots[0] == 0);
28 slot = path->slots[0] - 1;
29 if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
33 memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
35 btrfs_disk_key_to_cpu(key, &l->items[slot].key);
38 btrfs_release_path(root, path);
39 btrfs_free_path(path);
43 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
44 *root, struct btrfs_key *key, struct btrfs_root_item
47 struct btrfs_path *path;
51 struct btrfs_root_item *update_item;
53 path = btrfs_alloc_path();
55 btrfs_init_path(path);
56 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
60 l = btrfs_buffer_leaf(path->nodes[0]);
61 slot = path->slots[0];
62 update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item);
63 btrfs_memcpy(root, l, update_item, item, sizeof(*item));
64 btrfs_mark_buffer_dirty(path->nodes[0]);
66 btrfs_release_path(root, path);
67 btrfs_free_path(path);
71 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
72 *root, struct btrfs_key *key, struct btrfs_root_item
76 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
81 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
82 struct btrfs_key *key)
84 struct btrfs_path *path;
87 struct btrfs_root_item *ri;
89 path = btrfs_alloc_path();
91 btrfs_init_path(path);
92 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
96 ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
97 path->slots[0], struct btrfs_root_item);
99 refs = btrfs_root_refs(ri);
102 ret = btrfs_del_item(trans, root, path);
104 btrfs_set_root_refs(ri, refs - 1);
105 printk("ref now %u root %llu %Lu %u\n", refs -1, key->objectid, key->offset, key->flags);
106 mark_buffer_dirty(path->nodes[0]);
109 btrfs_release_path(root, path);
110 btrfs_free_path(path);