Commit | Line | Data |
---|---|---|
eb60ceac CM |
1 | #define _XOPEN_SOURCE 500 |
2 | #include <stdio.h> | |
3 | #include <stdlib.h> | |
4 | #include <sys/types.h> | |
5 | #include <sys/stat.h> | |
6 | #include <fcntl.h> | |
7 | #include <unistd.h> | |
8 | #include "kerncompat.h" | |
9 | #include "radix-tree.h" | |
10 | #include "ctree.h" | |
11 | #include "disk-io.h" | |
12 | ||
13 | static int allocated_blocks = 0; | |
ed2ff2cb | 14 | int cache_max = 10000; |
eb60ceac | 15 | |
9a8dd150 | 16 | static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf) |
eb60ceac | 17 | { |
9a8dd150 CM |
18 | if (buf->blocknr != buf->node.header.blocknr) |
19 | BUG(); | |
20 | if (root->node && buf->node.header.parentid != root->node->node.header.parentid) | |
21 | BUG(); | |
22 | return 0; | |
eb60ceac CM |
23 | } |
24 | ||
ed2ff2cb CM |
25 | static int free_some_buffers(struct ctree_root *root) |
26 | { | |
27 | struct list_head *node, *next; | |
28 | struct tree_buffer *b; | |
29 | if (root->cache_size < cache_max) | |
30 | return 0; | |
31 | list_for_each_safe(node, next, &root->cache) { | |
32 | b = list_entry(node, struct tree_buffer, cache); | |
33 | if (b->count == 1) { | |
34 | BUG_ON(!list_empty(&b->dirty)); | |
35 | list_del_init(&b->cache); | |
36 | tree_block_release(root, b); | |
37 | if (root->cache_size < cache_max) | |
77ce6846 | 38 | break; |
ed2ff2cb CM |
39 | } |
40 | } | |
41 | return 0; | |
42 | } | |
43 | ||
eb60ceac CM |
44 | struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr) |
45 | { | |
46 | struct tree_buffer *buf; | |
47 | int ret; | |
48 | buf = malloc(sizeof(struct tree_buffer)); | |
49 | if (!buf) | |
50 | return buf; | |
51 | allocated_blocks++; | |
52 | buf->blocknr = blocknr; | |
ed2ff2cb CM |
53 | buf->count = 2; |
54 | INIT_LIST_HEAD(&buf->dirty); | |
55 | free_some_buffers(root); | |
eb60ceac CM |
56 | radix_tree_preload(GFP_KERNEL); |
57 | ret = radix_tree_insert(&root->cache_radix, blocknr, buf); | |
58 | radix_tree_preload_end(); | |
ed2ff2cb CM |
59 | list_add_tail(&buf->cache, &root->cache); |
60 | root->cache_size++; | |
eb60ceac CM |
61 | if (ret) { |
62 | free(buf); | |
63 | return NULL; | |
64 | } | |
65 | return buf; | |
66 | } | |
67 | ||
9a8dd150 | 68 | struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr) |
eb60ceac | 69 | { |
9a8dd150 CM |
70 | struct tree_buffer *buf; |
71 | buf = radix_tree_lookup(&root->cache_radix, blocknr); | |
72 | if (buf) { | |
73 | buf->count++; | |
74 | } else { | |
75 | buf = alloc_tree_block(root, blocknr); | |
76 | if (!buf) { | |
77 | BUG(); | |
78 | return NULL; | |
79 | } | |
eb60ceac | 80 | } |
eb60ceac CM |
81 | return buf; |
82 | } | |
83 | ||
84 | struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr) | |
85 | { | |
d97e63b6 | 86 | loff_t offset = blocknr * CTREE_BLOCKSIZE; |
eb60ceac CM |
87 | struct tree_buffer *buf; |
88 | int ret; | |
89 | ||
90 | buf = radix_tree_lookup(&root->cache_radix, blocknr); | |
91 | if (buf) { | |
92 | buf->count++; | |
9a8dd150 CM |
93 | } else { |
94 | buf = alloc_tree_block(root, blocknr); | |
95 | if (!buf) | |
96 | return NULL; | |
97 | ret = pread(root->fp, &buf->node, CTREE_BLOCKSIZE, offset); | |
98 | if (ret != CTREE_BLOCKSIZE) { | |
99 | free(buf); | |
100 | return NULL; | |
101 | } | |
eb60ceac | 102 | } |
9a8dd150 | 103 | if (check_tree_block(root, buf)) |
cfaa7295 | 104 | BUG(); |
eb60ceac CM |
105 | return buf; |
106 | } | |
107 | ||
ed2ff2cb CM |
108 | int dirty_tree_block(struct ctree_root *root, struct tree_buffer *buf) |
109 | { | |
110 | if (!list_empty(&buf->dirty)) | |
111 | return 0; | |
112 | list_add_tail(&buf->dirty, &root->trans); | |
113 | buf->count++; | |
114 | return 0; | |
115 | } | |
116 | ||
117 | int clean_tree_block(struct ctree_root *root, struct tree_buffer *buf) | |
118 | { | |
119 | if (!list_empty(&buf->dirty)) { | |
120 | list_del_init(&buf->dirty); | |
121 | tree_block_release(root, buf); | |
122 | } | |
123 | return 0; | |
124 | } | |
125 | ||
eb60ceac CM |
126 | int write_tree_block(struct ctree_root *root, struct tree_buffer *buf) |
127 | { | |
128 | u64 blocknr = buf->blocknr; | |
d97e63b6 | 129 | loff_t offset = blocknr * CTREE_BLOCKSIZE; |
eb60ceac CM |
130 | int ret; |
131 | ||
132 | if (buf->blocknr != buf->node.header.blocknr) | |
133 | BUG(); | |
134 | ret = pwrite(root->fp, &buf->node, CTREE_BLOCKSIZE, offset); | |
135 | if (ret != CTREE_BLOCKSIZE) | |
136 | return ret; | |
eb60ceac CM |
137 | return 0; |
138 | } | |
139 | ||
ed2ff2cb CM |
140 | static int __commit_transaction(struct ctree_root *root) |
141 | { | |
142 | struct tree_buffer *b; | |
143 | int ret = 0; | |
144 | int wret; | |
145 | while(!list_empty(&root->trans)) { | |
146 | b = list_entry(root->trans.next, struct tree_buffer, dirty); | |
147 | list_del_init(&b->dirty); | |
148 | wret = write_tree_block(root, b); | |
149 | if (wret) | |
150 | ret = wret; | |
151 | tree_block_release(root, b); | |
152 | } | |
153 | return ret; | |
154 | } | |
155 | ||
a28ec197 | 156 | int commit_transaction(struct ctree_root *root, struct ctree_super_block *s) |
ed2ff2cb | 157 | { |
a28ec197 CM |
158 | int ret = 0; |
159 | ||
ed2ff2cb CM |
160 | ret = __commit_transaction(root); |
161 | if (!ret && root != root->extent_root) | |
162 | ret = __commit_transaction(root->extent_root); | |
163 | BUG_ON(ret); | |
a28ec197 CM |
164 | if (root->commit_root != root->node) { |
165 | struct tree_buffer *snap = root->commit_root; | |
166 | root->commit_root = root->node; | |
167 | root->node->count++; | |
168 | ret = btrfs_drop_snapshot(root, snap); | |
169 | BUG_ON(ret); | |
170 | tree_block_release(root, snap); | |
171 | } | |
172 | write_ctree_super(root, s); | |
173 | btrfs_finish_extent_commit(root); | |
ed2ff2cb CM |
174 | return ret; |
175 | } | |
176 | ||
d97e63b6 CM |
177 | static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root, |
178 | struct ctree_root_info *info, int fp) | |
179 | { | |
ed2ff2cb CM |
180 | INIT_LIST_HEAD(&root->trans); |
181 | INIT_LIST_HEAD(&root->cache); | |
a28ec197 | 182 | root->cache_size = 0; |
d97e63b6 | 183 | root->fp = fp; |
cfaa7295 | 184 | root->node = NULL; |
d97e63b6 | 185 | root->extent_root = extent_root; |
a28ec197 CM |
186 | root->commit_root = NULL; |
187 | root->node = read_tree_block(root, info->tree_root); | |
188 | memset(&root->current_insert, 0, sizeof(root->current_insert)); | |
d97e63b6 CM |
189 | return 0; |
190 | } | |
191 | ||
cfaa7295 | 192 | struct ctree_root *open_ctree(char *filename, struct ctree_super_block *super) |
eb60ceac CM |
193 | { |
194 | struct ctree_root *root = malloc(sizeof(struct ctree_root)); | |
d97e63b6 | 195 | struct ctree_root *extent_root = malloc(sizeof(struct ctree_root)); |
eb60ceac | 196 | int fp; |
eb60ceac CM |
197 | int ret; |
198 | ||
c673024a | 199 | fp = open(filename, O_CREAT | O_RDWR, 0600); |
eb60ceac CM |
200 | if (fp < 0) { |
201 | free(root); | |
202 | return NULL; | |
203 | } | |
9a8dd150 | 204 | INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL); |
a28ec197 CM |
205 | INIT_RADIX_TREE(&root->pinned_radix, GFP_KERNEL); |
206 | INIT_RADIX_TREE(&extent_root->pinned_radix, GFP_KERNEL); | |
9a8dd150 | 207 | INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL); |
cfaa7295 | 208 | ret = pread(fp, super, sizeof(struct ctree_super_block), |
d97e63b6 | 209 | CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
5c680ed6 CM |
210 | if (ret == 0 || super->root_info.tree_root == 0) { |
211 | printf("making new FS!\n"); | |
d97e63b6 CM |
212 | ret = mkfs(fp); |
213 | if (ret) | |
214 | return NULL; | |
cfaa7295 | 215 | ret = pread(fp, super, sizeof(struct ctree_super_block), |
d97e63b6 CM |
216 | CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
217 | if (ret != sizeof(struct ctree_super_block)) | |
218 | return NULL; | |
219 | } | |
220 | BUG_ON(ret < 0); | |
cfaa7295 CM |
221 | __setup_root(root, extent_root, &super->root_info, fp); |
222 | __setup_root(extent_root, extent_root, &super->extent_info, fp); | |
a28ec197 CM |
223 | root->commit_root = root->node; |
224 | root->node->count++; | |
eb60ceac CM |
225 | return root; |
226 | } | |
227 | ||
cfaa7295 | 228 | static int __update_root(struct ctree_root *root, struct ctree_root_info *info) |
eb60ceac | 229 | { |
cfaa7295 | 230 | info->tree_root = root->node->blocknr; |
eb60ceac CM |
231 | return 0; |
232 | } | |
233 | ||
cfaa7295 | 234 | int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s) |
eb60ceac CM |
235 | { |
236 | int ret; | |
cfaa7295 CM |
237 | __update_root(root, &s->root_info); |
238 | __update_root(root->extent_root, &s->extent_info); | |
239 | ret = pwrite(root->fp, s, sizeof(*s), CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); | |
240 | if (ret != sizeof(*s)) { | |
241 | fprintf(stderr, "failed to write new super block err %d\n", ret); | |
eb60ceac | 242 | return ret; |
cfaa7295 CM |
243 | } |
244 | return 0; | |
245 | } | |
246 | ||
ed2ff2cb CM |
247 | static int drop_cache(struct ctree_root *root) |
248 | { | |
249 | while(!list_empty(&root->cache)) { | |
250 | struct tree_buffer *b = list_entry(root->cache.next, | |
251 | struct tree_buffer, cache); | |
252 | list_del_init(&b->cache); | |
253 | tree_block_release(root, b); | |
254 | } | |
255 | return 0; | |
256 | } | |
a28ec197 | 257 | int close_ctree(struct ctree_root *root, struct ctree_super_block *s) |
cfaa7295 | 258 | { |
a28ec197 CM |
259 | commit_transaction(root, s); |
260 | __commit_transaction(root->extent_root); | |
261 | write_ctree_super(root, s); | |
ed2ff2cb CM |
262 | drop_cache(root->extent_root); |
263 | drop_cache(root); | |
264 | BUG_ON(!list_empty(&root->trans)); | |
265 | BUG_ON(!list_empty(&root->extent_root->trans)); | |
266 | ||
cfaa7295 CM |
267 | close(root->fp); |
268 | if (root->node) | |
269 | tree_block_release(root, root->node); | |
270 | if (root->extent_root->node) | |
271 | tree_block_release(root->extent_root, root->extent_root->node); | |
a28ec197 | 272 | tree_block_release(root, root->commit_root); |
cfaa7295 CM |
273 | free(root); |
274 | printf("on close %d blocks are allocated\n", allocated_blocks); | |
eb60ceac CM |
275 | return 0; |
276 | } | |
277 | ||
278 | void tree_block_release(struct ctree_root *root, struct tree_buffer *buf) | |
279 | { | |
280 | buf->count--; | |
cfaa7295 CM |
281 | if (buf->count < 0) |
282 | BUG(); | |
eb60ceac | 283 | if (buf->count == 0) { |
02217ed2 CM |
284 | BUG_ON(!list_empty(&buf->cache)); |
285 | BUG_ON(!list_empty(&buf->dirty)); | |
eb60ceac CM |
286 | if (!radix_tree_lookup(&root->cache_radix, buf->blocknr)) |
287 | BUG(); | |
288 | radix_tree_delete(&root->cache_radix, buf->blocknr); | |
289 | memset(buf, 0, sizeof(*buf)); | |
290 | free(buf); | |
291 | BUG_ON(allocated_blocks == 0); | |
292 | allocated_blocks--; | |
ed2ff2cb CM |
293 | BUG_ON(root->cache_size == 0); |
294 | root->cache_size--; | |
eb60ceac CM |
295 | } |
296 | } | |
297 |