Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | ||
2e635a27 | 19 | #include <linux/module.h> |
3768f368 CM |
20 | #include "ctree.h" |
21 | #include "disk-io.h" | |
22 | #include "print-tree.h" | |
23 | ||
24 | int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, | |
25 | struct btrfs_root_item *item, struct btrfs_key *key) | |
26 | { | |
5caf2a00 | 27 | struct btrfs_path *path; |
3768f368 CM |
28 | struct btrfs_key search_key; |
29 | struct btrfs_leaf *l; | |
30 | int ret; | |
31 | int slot; | |
32 | ||
33 | search_key.objectid = objectid; | |
34 | search_key.flags = (u32)-1; | |
35 | search_key.offset = (u32)-1; | |
36 | ||
5caf2a00 CM |
37 | path = btrfs_alloc_path(); |
38 | BUG_ON(!path); | |
39 | btrfs_init_path(path); | |
40 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | |
3768f368 CM |
41 | if (ret < 0) |
42 | goto out; | |
43 | BUG_ON(ret == 0); | |
5caf2a00 CM |
44 | l = btrfs_buffer_leaf(path->nodes[0]); |
45 | BUG_ON(path->slots[0] == 0); | |
46 | slot = path->slots[0] - 1; | |
62e2749e | 47 | if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) { |
3768f368 CM |
48 | ret = 1; |
49 | goto out; | |
50 | } | |
123abc88 | 51 | memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item), |
3768f368 CM |
52 | sizeof(*item)); |
53 | btrfs_disk_key_to_cpu(key, &l->items[slot].key); | |
3768f368 CM |
54 | ret = 0; |
55 | out: | |
5caf2a00 CM |
56 | btrfs_release_path(root, path); |
57 | btrfs_free_path(path); | |
3768f368 CM |
58 | return ret; |
59 | } | |
60 | ||
e089f05c CM |
61 | int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root |
62 | *root, struct btrfs_key *key, struct btrfs_root_item | |
63 | *item) | |
3768f368 | 64 | { |
5caf2a00 | 65 | struct btrfs_path *path; |
3768f368 CM |
66 | struct btrfs_leaf *l; |
67 | int ret; | |
68 | int slot; | |
d6025579 | 69 | struct btrfs_root_item *update_item; |
3768f368 | 70 | |
5caf2a00 CM |
71 | path = btrfs_alloc_path(); |
72 | BUG_ON(!path); | |
73 | btrfs_init_path(path); | |
74 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); | |
3768f368 CM |
75 | if (ret < 0) |
76 | goto out; | |
77 | BUG_ON(ret != 0); | |
5caf2a00 CM |
78 | l = btrfs_buffer_leaf(path->nodes[0]); |
79 | slot = path->slots[0]; | |
d6025579 CM |
80 | update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item); |
81 | btrfs_memcpy(root, l, update_item, item, sizeof(*item)); | |
5caf2a00 | 82 | btrfs_mark_buffer_dirty(path->nodes[0]); |
3768f368 | 83 | out: |
5caf2a00 CM |
84 | btrfs_release_path(root, path); |
85 | btrfs_free_path(path); | |
3768f368 CM |
86 | return ret; |
87 | } | |
88 | ||
e089f05c CM |
89 | int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root |
90 | *root, struct btrfs_key *key, struct btrfs_root_item | |
91 | *item) | |
3768f368 CM |
92 | { |
93 | int ret; | |
e089f05c | 94 | ret = btrfs_insert_item(trans, root, key, item, sizeof(*item)); |
3768f368 CM |
95 | BUG_ON(ret); |
96 | return ret; | |
97 | } | |
98 | ||
e089f05c CM |
99 | int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
100 | struct btrfs_key *key) | |
3768f368 | 101 | { |
5caf2a00 | 102 | struct btrfs_path *path; |
3768f368 | 103 | int ret; |
c5739bba CM |
104 | u32 refs; |
105 | struct btrfs_root_item *ri; | |
3768f368 | 106 | |
5caf2a00 CM |
107 | path = btrfs_alloc_path(); |
108 | BUG_ON(!path); | |
109 | btrfs_init_path(path); | |
110 | ret = btrfs_search_slot(trans, root, key, path, -1, 1); | |
3768f368 CM |
111 | if (ret < 0) |
112 | goto out; | |
113 | BUG_ON(ret != 0); | |
c5739bba CM |
114 | ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
115 | path->slots[0], struct btrfs_root_item); | |
116 | ||
117 | refs = btrfs_root_refs(ri); | |
118 | BUG_ON(refs == 0); | |
119 | if (refs == 1) { | |
120 | ret = btrfs_del_item(trans, root, path); | |
c5739bba CM |
121 | } else { |
122 | btrfs_set_root_refs(ri, refs - 1); | |
5af3981c | 123 | WARN_ON(1); |
c5739bba CM |
124 | mark_buffer_dirty(path->nodes[0]); |
125 | } | |
3768f368 | 126 | out: |
5caf2a00 CM |
127 | btrfs_release_path(root, path); |
128 | btrfs_free_path(path); | |
3768f368 CM |
129 | return ret; |
130 | } |