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" | |
e089f05c | 12 | #include "transaction.h" |
eb60ceac CM |
13 | |
14 | static int allocated_blocks = 0; | |
ed2ff2cb | 15 | int cache_max = 10000; |
eb60ceac | 16 | |
234b63a0 | 17 | static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf) |
eb60ceac | 18 | { |
7518a238 | 19 | if (buf->blocknr != btrfs_header_blocknr(&buf->node.header)) |
9a8dd150 | 20 | BUG(); |
7518a238 CM |
21 | if (root->node && btrfs_header_parentid(&buf->node.header) != |
22 | btrfs_header_parentid(&root->node->node.header)) | |
9a8dd150 CM |
23 | BUG(); |
24 | return 0; | |
eb60ceac CM |
25 | } |
26 | ||
234b63a0 | 27 | static int free_some_buffers(struct btrfs_root *root) |
ed2ff2cb CM |
28 | { |
29 | struct list_head *node, *next; | |
234b63a0 | 30 | struct btrfs_buffer *b; |
9f5fae2f | 31 | if (root->fs_info->cache_size < cache_max) |
ed2ff2cb | 32 | return 0; |
9f5fae2f | 33 | list_for_each_safe(node, next, &root->fs_info->cache) { |
234b63a0 | 34 | b = list_entry(node, struct btrfs_buffer, cache); |
ed2ff2cb CM |
35 | if (b->count == 1) { |
36 | BUG_ON(!list_empty(&b->dirty)); | |
37 | list_del_init(&b->cache); | |
234b63a0 | 38 | btrfs_block_release(root, b); |
9f5fae2f | 39 | if (root->fs_info->cache_size < cache_max) |
77ce6846 | 40 | break; |
ed2ff2cb CM |
41 | } |
42 | } | |
43 | return 0; | |
44 | } | |
45 | ||
234b63a0 | 46 | struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr) |
eb60ceac | 47 | { |
234b63a0 | 48 | struct btrfs_buffer *buf; |
eb60ceac | 49 | int ret; |
123abc88 CM |
50 | |
51 | buf = malloc(sizeof(struct btrfs_buffer) + root->blocksize); | |
eb60ceac CM |
52 | if (!buf) |
53 | return buf; | |
54 | allocated_blocks++; | |
55 | buf->blocknr = blocknr; | |
ed2ff2cb CM |
56 | buf->count = 2; |
57 | INIT_LIST_HEAD(&buf->dirty); | |
58 | free_some_buffers(root); | |
eb60ceac | 59 | radix_tree_preload(GFP_KERNEL); |
9f5fae2f | 60 | ret = radix_tree_insert(&root->fs_info->cache_radix, blocknr, buf); |
eb60ceac | 61 | radix_tree_preload_end(); |
9f5fae2f CM |
62 | list_add_tail(&buf->cache, &root->fs_info->cache); |
63 | root->fs_info->cache_size++; | |
eb60ceac CM |
64 | if (ret) { |
65 | free(buf); | |
66 | return NULL; | |
67 | } | |
68 | return buf; | |
69 | } | |
70 | ||
234b63a0 | 71 | struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr) |
eb60ceac | 72 | { |
234b63a0 | 73 | struct btrfs_buffer *buf; |
9f5fae2f | 74 | buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr); |
9a8dd150 CM |
75 | if (buf) { |
76 | buf->count++; | |
77 | } else { | |
78 | buf = alloc_tree_block(root, blocknr); | |
79 | if (!buf) { | |
80 | BUG(); | |
81 | return NULL; | |
82 | } | |
eb60ceac | 83 | } |
eb60ceac CM |
84 | return buf; |
85 | } | |
86 | ||
234b63a0 | 87 | struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr) |
eb60ceac | 88 | { |
123abc88 | 89 | loff_t offset = blocknr * root->blocksize; |
234b63a0 | 90 | struct btrfs_buffer *buf; |
eb60ceac CM |
91 | int ret; |
92 | ||
9f5fae2f | 93 | buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr); |
eb60ceac CM |
94 | if (buf) { |
95 | buf->count++; | |
9a8dd150 CM |
96 | } else { |
97 | buf = alloc_tree_block(root, blocknr); | |
98 | if (!buf) | |
99 | return NULL; | |
9f5fae2f CM |
100 | ret = pread(root->fs_info->fp, &buf->node, root->blocksize, |
101 | offset); | |
123abc88 | 102 | if (ret != root->blocksize) { |
9a8dd150 CM |
103 | free(buf); |
104 | return NULL; | |
105 | } | |
eb60ceac | 106 | } |
9a8dd150 | 107 | if (check_tree_block(root, buf)) |
cfaa7295 | 108 | BUG(); |
eb60ceac CM |
109 | return buf; |
110 | } | |
111 | ||
e089f05c CM |
112 | int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
113 | struct btrfs_buffer *buf) | |
ed2ff2cb CM |
114 | { |
115 | if (!list_empty(&buf->dirty)) | |
116 | return 0; | |
9f5fae2f | 117 | list_add_tail(&buf->dirty, &root->fs_info->trans); |
ed2ff2cb CM |
118 | buf->count++; |
119 | return 0; | |
120 | } | |
121 | ||
e089f05c CM |
122 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
123 | struct btrfs_buffer *buf) | |
ed2ff2cb CM |
124 | { |
125 | if (!list_empty(&buf->dirty)) { | |
126 | list_del_init(&buf->dirty); | |
234b63a0 | 127 | btrfs_block_release(root, buf); |
ed2ff2cb CM |
128 | } |
129 | return 0; | |
130 | } | |
131 | ||
e089f05c CM |
132 | int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
133 | struct btrfs_buffer *buf) | |
eb60ceac CM |
134 | { |
135 | u64 blocknr = buf->blocknr; | |
123abc88 | 136 | loff_t offset = blocknr * root->blocksize; |
eb60ceac CM |
137 | int ret; |
138 | ||
7518a238 | 139 | if (buf->blocknr != btrfs_header_blocknr(&buf->node.header)) |
eb60ceac | 140 | BUG(); |
9f5fae2f | 141 | ret = pwrite(root->fs_info->fp, &buf->node, root->blocksize, offset); |
123abc88 | 142 | if (ret != root->blocksize) |
eb60ceac | 143 | return ret; |
eb60ceac CM |
144 | return 0; |
145 | } | |
146 | ||
e089f05c CM |
147 | static int __commit_transaction(struct btrfs_trans_handle *trans, struct |
148 | btrfs_root *root) | |
ed2ff2cb | 149 | { |
234b63a0 | 150 | struct btrfs_buffer *b; |
ed2ff2cb CM |
151 | int ret = 0; |
152 | int wret; | |
9f5fae2f CM |
153 | while(!list_empty(&root->fs_info->trans)) { |
154 | b = list_entry(root->fs_info->trans.next, struct btrfs_buffer, | |
155 | dirty); | |
ed2ff2cb | 156 | list_del_init(&b->dirty); |
e089f05c | 157 | wret = write_tree_block(trans, root, b); |
ed2ff2cb CM |
158 | if (wret) |
159 | ret = wret; | |
234b63a0 | 160 | btrfs_block_release(root, b); |
ed2ff2cb CM |
161 | } |
162 | return ret; | |
163 | } | |
164 | ||
9f5fae2f CM |
165 | static int commit_tree_roots(struct btrfs_trans_handle *trans, |
166 | struct btrfs_fs_info *fs_info) | |
3768f368 CM |
167 | { |
168 | int ret; | |
169 | u64 old_extent_block; | |
9f5fae2f CM |
170 | struct btrfs_root *tree_root = fs_info->tree_root; |
171 | struct btrfs_root *extent_root = fs_info->extent_root; | |
172 | struct btrfs_root *inode_root = fs_info->inode_root; | |
173 | ||
174 | btrfs_set_root_blocknr(&inode_root->root_item, | |
175 | inode_root->node->blocknr); | |
176 | ret = btrfs_update_root(trans, tree_root, | |
177 | &inode_root->root_key, | |
178 | &inode_root->root_item); | |
179 | BUG_ON(ret); | |
3768f368 CM |
180 | while(1) { |
181 | old_extent_block = btrfs_root_blocknr(&extent_root->root_item); | |
182 | if (old_extent_block == extent_root->node->blocknr) | |
183 | break; | |
184 | btrfs_set_root_blocknr(&extent_root->root_item, | |
185 | extent_root->node->blocknr); | |
e089f05c | 186 | ret = btrfs_update_root(trans, tree_root, |
3768f368 CM |
187 | &extent_root->root_key, |
188 | &extent_root->root_item); | |
189 | BUG_ON(ret); | |
190 | } | |
3768f368 CM |
191 | return 0; |
192 | } | |
193 | ||
e089f05c CM |
194 | int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct |
195 | btrfs_root *root, struct btrfs_super_block *s) | |
ed2ff2cb | 196 | { |
a28ec197 | 197 | int ret = 0; |
3768f368 CM |
198 | struct btrfs_buffer *snap = root->commit_root; |
199 | struct btrfs_key snap_key; | |
a28ec197 | 200 | |
3768f368 CM |
201 | if (root->commit_root == root->node) |
202 | return 0; | |
203 | ||
204 | memcpy(&snap_key, &root->root_key, sizeof(snap_key)); | |
205 | root->root_key.offset++; | |
206 | ||
207 | btrfs_set_root_blocknr(&root->root_item, root->node->blocknr); | |
9f5fae2f CM |
208 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
209 | &root->root_key, &root->root_item); | |
210 | BUG_ON(ret); | |
211 | ||
212 | ret = commit_tree_roots(trans, root->fs_info); | |
3768f368 CM |
213 | BUG_ON(ret); |
214 | ||
9f5fae2f | 215 | ret = __commit_transaction(trans, root); |
3768f368 CM |
216 | BUG_ON(ret); |
217 | ||
e089f05c | 218 | write_ctree_super(trans, root, s); |
9f5fae2f CM |
219 | btrfs_finish_extent_commit(trans, root->fs_info->extent_root); |
220 | btrfs_finish_extent_commit(trans, root->fs_info->tree_root); | |
3768f368 CM |
221 | |
222 | root->commit_root = root->node; | |
223 | root->node->count++; | |
e089f05c | 224 | ret = btrfs_drop_snapshot(trans, root, snap); |
3768f368 CM |
225 | BUG_ON(ret); |
226 | ||
9f5fae2f | 227 | ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key); |
3768f368 CM |
228 | BUG_ON(ret); |
229 | ||
ed2ff2cb CM |
230 | return ret; |
231 | } | |
232 | ||
123abc88 | 233 | static int __setup_root(struct btrfs_super_block *super, |
9f5fae2f CM |
234 | struct btrfs_root *root, |
235 | struct btrfs_fs_info *fs_info, | |
236 | u64 objectid, int fp) | |
d97e63b6 | 237 | { |
cfaa7295 | 238 | root->node = NULL; |
a28ec197 | 239 | root->commit_root = NULL; |
123abc88 CM |
240 | root->blocksize = btrfs_super_blocksize(super); |
241 | root->ref_cows = 0; | |
9f5fae2f | 242 | root->fs_info = fs_info; |
3768f368 CM |
243 | memset(&root->root_key, 0, sizeof(root->root_key)); |
244 | memset(&root->root_item, 0, sizeof(root->root_item)); | |
245 | return 0; | |
246 | } | |
247 | ||
123abc88 | 248 | static int find_and_setup_root(struct btrfs_super_block *super, |
9f5fae2f CM |
249 | struct btrfs_root *tree_root, |
250 | struct btrfs_fs_info *fs_info, | |
251 | u64 objectid, | |
123abc88 | 252 | struct btrfs_root *root, int fp) |
3768f368 CM |
253 | { |
254 | int ret; | |
255 | ||
9f5fae2f | 256 | __setup_root(super, root, fs_info, objectid, fp); |
3768f368 CM |
257 | ret = btrfs_find_last_root(tree_root, objectid, |
258 | &root->root_item, &root->root_key); | |
259 | BUG_ON(ret); | |
260 | ||
261 | root->node = read_tree_block(root, | |
262 | btrfs_root_blocknr(&root->root_item)); | |
3768f368 | 263 | BUG_ON(!root->node); |
d97e63b6 CM |
264 | return 0; |
265 | } | |
266 | ||
234b63a0 | 267 | struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super) |
eb60ceac | 268 | { |
234b63a0 CM |
269 | struct btrfs_root *root = malloc(sizeof(struct btrfs_root)); |
270 | struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root)); | |
3768f368 | 271 | struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root)); |
9f5fae2f CM |
272 | struct btrfs_root *inode_root = malloc(sizeof(struct btrfs_root)); |
273 | struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info)); | |
eb60ceac | 274 | int fp; |
eb60ceac CM |
275 | int ret; |
276 | ||
c673024a | 277 | fp = open(filename, O_CREAT | O_RDWR, 0600); |
eb60ceac CM |
278 | if (fp < 0) { |
279 | free(root); | |
280 | return NULL; | |
281 | } | |
9f5fae2f CM |
282 | INIT_RADIX_TREE(&fs_info->cache_radix, GFP_KERNEL); |
283 | INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL); | |
284 | INIT_LIST_HEAD(&fs_info->trans); | |
285 | INIT_LIST_HEAD(&fs_info->cache); | |
286 | fs_info->cache_size = 0; | |
287 | fs_info->fp = fp; | |
288 | fs_info->running_transaction = NULL; | |
289 | fs_info->fs_root = root; | |
290 | fs_info->tree_root = tree_root; | |
291 | fs_info->extent_root = extent_root; | |
292 | fs_info->inode_root = inode_root; | |
293 | fs_info->last_inode_alloc = 0; | |
294 | fs_info->last_inode_alloc_dirid = 0; | |
295 | memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert)); | |
296 | memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert)); | |
3768f368 | 297 | |
234b63a0 | 298 | ret = pread(fp, super, sizeof(struct btrfs_super_block), |
123abc88 | 299 | BTRFS_SUPER_INFO_OFFSET); |
3768f368 | 300 | if (ret == 0 || btrfs_super_root(super) == 0) { |
5c680ed6 | 301 | printf("making new FS!\n"); |
123abc88 | 302 | ret = mkfs(fp, 0, 1024); |
d97e63b6 CM |
303 | if (ret) |
304 | return NULL; | |
234b63a0 | 305 | ret = pread(fp, super, sizeof(struct btrfs_super_block), |
123abc88 | 306 | BTRFS_SUPER_INFO_OFFSET); |
234b63a0 | 307 | if (ret != sizeof(struct btrfs_super_block)) |
d97e63b6 CM |
308 | return NULL; |
309 | } | |
310 | BUG_ON(ret < 0); | |
3768f368 | 311 | |
9f5fae2f | 312 | __setup_root(super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID, fp); |
3768f368 CM |
313 | tree_root->node = read_tree_block(tree_root, btrfs_super_root(super)); |
314 | BUG_ON(!tree_root->node); | |
315 | ||
9f5fae2f CM |
316 | ret = find_and_setup_root(super, tree_root, fs_info, |
317 | BTRFS_EXTENT_TREE_OBJECTID, extent_root, fp); | |
3768f368 CM |
318 | BUG_ON(ret); |
319 | ||
9f5fae2f CM |
320 | ret = find_and_setup_root(super, tree_root, fs_info, |
321 | BTRFS_INODE_MAP_OBJECTID, inode_root, fp); | |
322 | BUG_ON(ret); | |
323 | ||
324 | ret = find_and_setup_root(super, tree_root, fs_info, | |
325 | BTRFS_FS_TREE_OBJECTID, root, fp); | |
3768f368 CM |
326 | BUG_ON(ret); |
327 | ||
a28ec197 CM |
328 | root->commit_root = root->node; |
329 | root->node->count++; | |
3768f368 | 330 | root->ref_cows = 1; |
eb60ceac CM |
331 | return root; |
332 | } | |
333 | ||
e089f05c CM |
334 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
335 | *root, struct btrfs_super_block *s) | |
eb60ceac CM |
336 | { |
337 | int ret; | |
9f5fae2f CM |
338 | btrfs_set_super_root(s, root->fs_info->tree_root->node->blocknr); |
339 | ret = pwrite(root->fs_info->fp, s, sizeof(*s), | |
123abc88 | 340 | BTRFS_SUPER_INFO_OFFSET); |
cfaa7295 CM |
341 | if (ret != sizeof(*s)) { |
342 | fprintf(stderr, "failed to write new super block err %d\n", ret); | |
eb60ceac | 343 | return ret; |
cfaa7295 CM |
344 | } |
345 | return 0; | |
346 | } | |
347 | ||
234b63a0 | 348 | static int drop_cache(struct btrfs_root *root) |
ed2ff2cb | 349 | { |
9f5fae2f CM |
350 | while(!list_empty(&root->fs_info->cache)) { |
351 | struct btrfs_buffer *b = list_entry(root->fs_info->cache.next, | |
352 | struct btrfs_buffer, | |
353 | cache); | |
ed2ff2cb | 354 | list_del_init(&b->cache); |
234b63a0 | 355 | btrfs_block_release(root, b); |
ed2ff2cb CM |
356 | } |
357 | return 0; | |
358 | } | |
234b63a0 | 359 | int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s) |
cfaa7295 | 360 | { |
3768f368 | 361 | int ret; |
e089f05c CM |
362 | struct btrfs_trans_handle *trans; |
363 | ||
9f5fae2f | 364 | trans = root->fs_info->running_transaction; |
e089f05c | 365 | btrfs_commit_transaction(trans, root, s); |
9f5fae2f CM |
366 | ret = commit_tree_roots(trans, root->fs_info); |
367 | BUG_ON(ret); | |
368 | ret = __commit_transaction(trans, root); | |
3768f368 | 369 | BUG_ON(ret); |
e089f05c | 370 | write_ctree_super(trans, root, s); |
ed2ff2cb | 371 | drop_cache(root); |
9f5fae2f | 372 | BUG_ON(!list_empty(&root->fs_info->trans)); |
ed2ff2cb | 373 | |
9f5fae2f | 374 | close(root->fs_info->fp); |
cfaa7295 | 375 | if (root->node) |
234b63a0 | 376 | btrfs_block_release(root, root->node); |
9f5fae2f CM |
377 | if (root->fs_info->extent_root->node) |
378 | btrfs_block_release(root->fs_info->extent_root, | |
379 | root->fs_info->extent_root->node); | |
380 | if (root->fs_info->inode_root->node) | |
381 | btrfs_block_release(root->fs_info->inode_root, | |
382 | root->fs_info->inode_root->node); | |
383 | if (root->fs_info->tree_root->node) | |
384 | btrfs_block_release(root->fs_info->tree_root, | |
385 | root->fs_info->tree_root->node); | |
234b63a0 | 386 | btrfs_block_release(root, root->commit_root); |
cfaa7295 CM |
387 | free(root); |
388 | printf("on close %d blocks are allocated\n", allocated_blocks); | |
eb60ceac CM |
389 | return 0; |
390 | } | |
391 | ||
234b63a0 | 392 | void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf) |
eb60ceac CM |
393 | { |
394 | buf->count--; | |
cfaa7295 CM |
395 | if (buf->count < 0) |
396 | BUG(); | |
eb60ceac | 397 | if (buf->count == 0) { |
02217ed2 CM |
398 | BUG_ON(!list_empty(&buf->cache)); |
399 | BUG_ON(!list_empty(&buf->dirty)); | |
9f5fae2f CM |
400 | if (!radix_tree_lookup(&root->fs_info->cache_radix, |
401 | buf->blocknr)) | |
eb60ceac | 402 | BUG(); |
9f5fae2f | 403 | radix_tree_delete(&root->fs_info->cache_radix, buf->blocknr); |
eb60ceac CM |
404 | memset(buf, 0, sizeof(*buf)); |
405 | free(buf); | |
406 | BUG_ON(allocated_blocks == 0); | |
407 | allocated_blocks--; | |
9f5fae2f CM |
408 | BUG_ON(root->fs_info->cache_size == 0); |
409 | root->fs_info->cache_size--; | |
eb60ceac CM |
410 | } |
411 | } | |
412 |