blob: a42943bd9179c8d8a1ecb00feea0a447ca64151d [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Mason3768f362007-03-13 16:47:54 -04002#include "ctree.h"
3#include "disk-io.h"
4#include "print-tree.h"
5
6int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
7 struct btrfs_root_item *item, struct btrfs_key *key)
8{
Chris Mason5caf2a02007-04-02 11:20:42 -04009 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040010 struct btrfs_key search_key;
11 struct btrfs_leaf *l;
12 int ret;
13 int slot;
14
15 search_key.objectid = objectid;
16 search_key.flags = (u32)-1;
17 search_key.offset = (u32)-1;
18
Chris Mason5caf2a02007-04-02 11:20:42 -040019 path = btrfs_alloc_path();
20 BUG_ON(!path);
21 btrfs_init_path(path);
22 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Chris Mason3768f362007-03-13 16:47:54 -040023 if (ret < 0)
24 goto out;
25 BUG_ON(ret == 0);
Chris Mason5caf2a02007-04-02 11:20:42 -040026 l = btrfs_buffer_leaf(path->nodes[0]);
27 BUG_ON(path->slots[0] == 0);
28 slot = path->slots[0] - 1;
Chris Mason62e27492007-03-15 12:56:47 -040029 if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
Chris Mason3768f362007-03-13 16:47:54 -040030 ret = 1;
31 goto out;
32 }
Chris Mason123abc82007-03-14 14:14:43 -040033 memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
Chris Mason3768f362007-03-13 16:47:54 -040034 sizeof(*item));
35 btrfs_disk_key_to_cpu(key, &l->items[slot].key);
Chris Mason3768f362007-03-13 16:47:54 -040036 ret = 0;
37out:
Chris Mason5caf2a02007-04-02 11:20:42 -040038 btrfs_release_path(root, path);
39 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -040040 return ret;
41}
42
Chris Masone089f052007-03-16 16:20:31 -040043int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
44 *root, struct btrfs_key *key, struct btrfs_root_item
45 *item)
Chris Mason3768f362007-03-13 16:47:54 -040046{
Chris Mason5caf2a02007-04-02 11:20:42 -040047 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040048 struct btrfs_leaf *l;
49 int ret;
50 int slot;
Chris Masond6025572007-03-30 14:27:56 -040051 struct btrfs_root_item *update_item;
Chris Mason3768f362007-03-13 16:47:54 -040052
Chris Mason5caf2a02007-04-02 11:20:42 -040053 path = btrfs_alloc_path();
54 BUG_ON(!path);
55 btrfs_init_path(path);
56 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
Chris Mason3768f362007-03-13 16:47:54 -040057 if (ret < 0)
58 goto out;
59 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -040060 l = btrfs_buffer_leaf(path->nodes[0]);
61 slot = path->slots[0];
Chris Masond6025572007-03-30 14:27:56 -040062 update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item);
63 btrfs_memcpy(root, l, update_item, item, sizeof(*item));
Chris Mason5caf2a02007-04-02 11:20:42 -040064 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason3768f362007-03-13 16:47:54 -040065out:
Chris Mason5caf2a02007-04-02 11:20:42 -040066 btrfs_release_path(root, path);
67 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -040068 return ret;
69}
70
Chris Masone089f052007-03-16 16:20:31 -040071int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
72 *root, struct btrfs_key *key, struct btrfs_root_item
73 *item)
Chris Mason3768f362007-03-13 16:47:54 -040074{
75 int ret;
Chris Masone089f052007-03-16 16:20:31 -040076 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -040077 BUG_ON(ret);
78 return ret;
79}
80
Chris Masone089f052007-03-16 16:20:31 -040081int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
82 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -040083{
Chris Mason5caf2a02007-04-02 11:20:42 -040084 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040085 int ret;
Chris Masonc5739bb2007-04-10 09:27:04 -040086 u32 refs;
87 struct btrfs_root_item *ri;
Chris Mason3768f362007-03-13 16:47:54 -040088
Chris Mason5caf2a02007-04-02 11:20:42 -040089 path = btrfs_alloc_path();
90 BUG_ON(!path);
91 btrfs_init_path(path);
92 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -040093 if (ret < 0)
94 goto out;
95 BUG_ON(ret != 0);
Chris Masonc5739bb2007-04-10 09:27:04 -040096 ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
97 path->slots[0], struct btrfs_root_item);
98
99 refs = btrfs_root_refs(ri);
100 BUG_ON(refs == 0);
101 if (refs == 1) {
102 ret = btrfs_del_item(trans, root, path);
Chris Masonc5739bb2007-04-10 09:27:04 -0400103 } else {
104 btrfs_set_root_refs(ri, refs - 1);
105printk("ref now %u root %Lu %Lu %u\n", refs -1, key->objectid, key->offset, key->flags);
106 mark_buffer_dirty(path->nodes[0]);
107 }
Chris Mason3768f362007-03-13 16:47:54 -0400108out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400109 btrfs_release_path(root, path);
110 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400111 return ret;
112}