2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #define _XOPEN_SOURCE 600
23 #include <sys/types.h>
27 #include "kerncompat.h"
28 #include "radix-tree.h"
31 #include "transaction.h"
34 int btrfs_map_bh_to_logical(struct btrfs_root
*root
, struct extent_buffer
*buf
,
37 buf
->fd
= root
->fs_info
->fp
;
38 buf
->dev_bytenr
= logical
;
42 static int check_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
)
44 if (buf
->start
!= btrfs_header_bytenr(buf
))
47 if (memcmp_extent_buffer(buf
, root
->fs_info
->fsid
,
48 (unsigned long)btrfs_header_fsid(buf
),
54 u32
btrfs_csum_data(struct btrfs_root
*root
, char *data
, u32 seed
, size_t len
)
56 return crc32c(seed
, data
, len
);
59 void btrfs_csum_final(u32 crc
, char *result
)
61 *(__le32
*)result
= ~cpu_to_le32(crc
);
64 static int csum_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
,
67 char result
[BTRFS_CRC32_SIZE
];
71 len
= buf
->len
- BTRFS_CSUM_SIZE
;
72 crc
= crc32c(crc
, buf
->data
+ BTRFS_CSUM_SIZE
, len
);
73 btrfs_csum_final(crc
, result
);
76 if (memcmp_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
)) {
77 printk("checksum verify failed on %llu\n", buf
->start
);
81 write_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
);
86 struct extent_buffer
*btrfs_find_tree_block(struct btrfs_root
*root
,
87 u64 bytenr
, u32 blocksize
)
89 return find_extent_buffer(&root
->fs_info
->extent_cache
,
93 struct extent_buffer
*btrfs_find_create_tree_block(struct btrfs_root
*root
,
94 u64 bytenr
, u32 blocksize
)
96 return alloc_extent_buffer(&root
->fs_info
->extent_cache
, bytenr
,
100 int readahead_tree_block(struct btrfs_root
*root
, u64 bytenr
, u32 blocksize
)
105 struct extent_buffer
*read_tree_block(struct btrfs_root
*root
, u64 bytenr
,
109 struct extent_buffer
*eb
;
111 eb
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
114 if (!btrfs_buffer_uptodate(eb
)) {
115 btrfs_map_bh_to_logical(root
, eb
, eb
->start
);
116 ret
= read_extent_from_disk(eb
);
118 free_extent_buffer(eb
);
121 btrfs_set_buffer_uptodate(eb
);
126 int write_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
127 struct extent_buffer
*eb
)
129 if (check_tree_block(root
, eb
))
131 if (!btrfs_buffer_uptodate(eb
))
133 btrfs_map_bh_to_logical(root
, eb
, eb
->start
);
134 csum_tree_block(root
, eb
, 0);
135 return write_extent_to_disk(eb
);
138 static int __setup_root(u32 nodesize
, u32 leafsize
, u32 sectorsize
,
139 u32 stripesize
, struct btrfs_root
*root
,
140 struct btrfs_fs_info
*fs_info
, u64 objectid
)
143 root
->commit_root
= NULL
;
144 root
->sectorsize
= sectorsize
;
145 root
->nodesize
= nodesize
;
146 root
->leafsize
= leafsize
;
147 root
->stripesize
= stripesize
;
149 root
->fs_info
= fs_info
;
150 root
->objectid
= objectid
;
151 root
->last_trans
= 0;
152 root
->highest_inode
= 0;
153 root
->last_inode_alloc
= 0;
154 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
155 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
156 root
->root_key
.objectid
= objectid
;
160 static int commit_tree_roots(struct btrfs_trans_handle
*trans
,
161 struct btrfs_fs_info
*fs_info
)
164 u64 old_extent_bytenr
;
165 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
166 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
168 btrfs_write_dirty_block_groups(trans
, fs_info
->extent_root
);
170 old_extent_bytenr
= btrfs_root_bytenr(&extent_root
->root_item
);
171 if (old_extent_bytenr
== extent_root
->node
->start
)
173 btrfs_set_root_bytenr(&extent_root
->root_item
,
174 extent_root
->node
->start
);
175 extent_root
->root_item
.level
=
176 btrfs_header_level(extent_root
->node
);
177 ret
= btrfs_update_root(trans
, tree_root
,
178 &extent_root
->root_key
,
179 &extent_root
->root_item
);
181 btrfs_write_dirty_block_groups(trans
, fs_info
->extent_root
);
186 static int __commit_transaction(struct btrfs_trans_handle
*trans
,
187 struct btrfs_root
*root
)
191 struct extent_buffer
*eb
;
192 struct extent_map_tree
*tree
= &root
->fs_info
->extent_cache
;
196 ret
= find_first_extent_bit(tree
, 0, &start
, &end
,
200 while(start
<= end
) {
201 eb
= find_first_extent_buffer(tree
, start
);
202 BUG_ON(!eb
|| eb
->start
!= start
);
203 ret
= write_tree_block(trans
, root
, eb
);
206 clear_extent_buffer_dirty(eb
);
207 free_extent_buffer(eb
);
213 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
,
214 struct btrfs_root
*root
)
217 struct btrfs_root
*new_root
= NULL
;
218 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
220 if (root
->commit_root
== root
->node
)
223 new_root
= malloc(sizeof(*new_root
));
226 memcpy(new_root
, root
, sizeof(*new_root
));
227 new_root
->node
= root
->commit_root
;
228 root
->commit_root
= NULL
;
230 root
->root_key
.offset
= trans
->transid
;
231 btrfs_set_root_bytenr(&root
->root_item
, root
->node
->start
);
232 root
->root_item
.level
= btrfs_header_level(root
->node
);
233 ret
= btrfs_insert_root(trans
, fs_info
->tree_root
,
234 &root
->root_key
, &root
->root_item
);
237 btrfs_set_root_refs(&new_root
->root_item
, 0);
238 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
239 &new_root
->root_key
, &new_root
->root_item
);
242 ret
= commit_tree_roots(trans
, fs_info
);
244 ret
= __commit_transaction(trans
, root
);
246 write_ctree_super(trans
, root
);
247 btrfs_finish_extent_commit(trans
, fs_info
->extent_root
,
248 &fs_info
->pinned_extents
);
249 btrfs_free_transaction(root
, trans
);
250 fs_info
->running_transaction
= NULL
;
252 trans
= btrfs_start_transaction(root
, 1);
253 ret
= btrfs_drop_snapshot(trans
, new_root
);
255 ret
= btrfs_del_root(trans
, fs_info
->tree_root
, &new_root
->root_key
);
258 ret
= commit_tree_roots(trans
, fs_info
);
260 ret
= __commit_transaction(trans
, root
);
262 write_ctree_super(trans
, root
);
263 btrfs_finish_extent_commit(trans
, fs_info
->extent_root
,
264 &fs_info
->pinned_extents
);
265 btrfs_free_transaction(root
, trans
);
266 free_extent_buffer(root
->commit_root
);
267 root
->commit_root
= NULL
;
268 fs_info
->running_transaction
= NULL
;
270 free_extent_buffer(new_root
->node
);
276 static int find_and_setup_root(struct btrfs_root
*tree_root
,
277 struct btrfs_fs_info
*fs_info
,
278 u64 objectid
, struct btrfs_root
*root
)
283 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
284 tree_root
->sectorsize
, tree_root
->stripesize
,
285 root
, fs_info
, objectid
);
286 ret
= btrfs_find_last_root(tree_root
, objectid
,
287 &root
->root_item
, &root
->root_key
);
290 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
291 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
297 int btrfs_free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
300 free_extent_buffer(root
->node
);
301 if (root
->commit_root
)
302 free_extent_buffer(root
->commit_root
);
308 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
309 struct btrfs_key
*location
)
311 struct btrfs_root
*root
;
312 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
313 struct btrfs_path
*path
;
314 struct extent_buffer
*l
;
318 root
= malloc(sizeof(*root
));
320 return ERR_PTR(-ENOMEM
);
321 memset(root
, 0, sizeof(*root
));
322 if (location
->offset
== (u64
)-1) {
323 ret
= find_and_setup_root(tree_root
, fs_info
,
324 location
->objectid
, root
);
332 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
333 tree_root
->sectorsize
, tree_root
->stripesize
,
334 root
, fs_info
, location
->objectid
);
336 path
= btrfs_alloc_path();
338 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
345 read_extent_buffer(l
, &root
->root_item
,
346 btrfs_item_ptr_offset(l
, path
->slots
[0]),
347 sizeof(root
->root_item
));
348 memcpy(&root
->root_key
, location
, sizeof(*location
));
351 btrfs_release_path(root
, path
);
352 btrfs_free_path(path
);
357 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
358 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
366 struct btrfs_root
*open_ctree(char *filename
, u64 sb_bytenr
)
370 fp
= open(filename
, O_CREAT
| O_RDWR
, 0600);
374 return open_ctree_fd(fp
, sb_bytenr
);
377 struct btrfs_root
*open_ctree_fd(int fp
, u64 sb_bytenr
)
384 struct btrfs_root
*root
= malloc(sizeof(struct btrfs_root
));
385 struct btrfs_root
*tree_root
= malloc(sizeof(struct btrfs_root
));
386 struct btrfs_root
*extent_root
= malloc(sizeof(struct btrfs_root
));
387 struct btrfs_fs_info
*fs_info
= malloc(sizeof(*fs_info
));
389 struct btrfs_super_block
*disk_super
;
392 sb_bytenr
= BTRFS_SUPER_INFO_OFFSET
;
395 fs_info
->running_transaction
= NULL
;
396 fs_info
->fs_root
= root
;
397 fs_info
->tree_root
= tree_root
;
398 fs_info
->extent_root
= extent_root
;
399 fs_info
->extent_ops
= NULL
;
400 fs_info
->priv_data
= NULL
;
401 extent_map_tree_init(&fs_info
->extent_cache
);
402 extent_map_tree_init(&fs_info
->free_space_cache
);
403 extent_map_tree_init(&fs_info
->pending_tree
);
404 extent_map_tree_init(&fs_info
->pinned_extents
);
405 extent_map_tree_init(&fs_info
->del_pending
);
406 extent_map_tree_init(&fs_info
->block_group_cache
);
408 mutex_init(&fs_info
->fs_mutex
);
410 __setup_root(512, 512, 512, 512, tree_root
,
411 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
413 fs_info
->sb_buffer
= read_tree_block(tree_root
, sb_bytenr
, 512);
414 BUG_ON(!fs_info
->sb_buffer
);
415 read_extent_buffer(fs_info
->sb_buffer
, &fs_info
->super_copy
, 0,
416 sizeof(fs_info
->super_copy
));
417 read_extent_buffer(fs_info
->sb_buffer
, fs_info
->fsid
,
418 (unsigned long)btrfs_super_fsid(fs_info
->sb_buffer
),
420 disk_super
= &fs_info
->super_copy
;
422 nodesize
= btrfs_super_nodesize(disk_super
);
423 leafsize
= btrfs_super_leafsize(disk_super
);
424 sectorsize
= btrfs_super_sectorsize(disk_super
);
425 stripesize
= btrfs_super_stripesize(disk_super
);
426 tree_root
->nodesize
= nodesize
;
427 tree_root
->leafsize
= leafsize
;
428 tree_root
->sectorsize
= sectorsize
;
429 tree_root
->stripesize
= stripesize
;
431 blocksize
= btrfs_level_size(tree_root
,
432 btrfs_super_root_level(disk_super
));
433 tree_root
->node
= read_tree_block(tree_root
,
434 btrfs_super_root(disk_super
),
436 BUG_ON(!tree_root
->node
);
437 ret
= find_and_setup_root(tree_root
, fs_info
,
438 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
440 ret
= find_and_setup_root(tree_root
, fs_info
,
441 BTRFS_FS_TREE_OBJECTID
, root
);
444 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
445 btrfs_read_block_groups(root
);
449 int write_ctree_super(struct btrfs_trans_handle
*trans
,
450 struct btrfs_root
*root
)
453 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
454 btrfs_set_super_generation(&root
->fs_info
->super_copy
,
456 btrfs_set_super_root(&root
->fs_info
->super_copy
,
457 tree_root
->node
->start
);
458 btrfs_set_super_root_level(&root
->fs_info
->super_copy
,
459 btrfs_header_level(tree_root
->node
));
460 write_extent_buffer(root
->fs_info
->sb_buffer
,
461 &root
->fs_info
->super_copy
, 0,
462 sizeof(root
->fs_info
->super_copy
));
463 ret
= write_tree_block(trans
, root
, root
->fs_info
->sb_buffer
);
465 fprintf(stderr
, "failed to write new super block err %d\n", ret
);
469 int close_ctree(struct btrfs_root
*root
)
472 struct btrfs_trans_handle
*trans
;
473 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
475 trans
= btrfs_start_transaction(root
, 1);
476 btrfs_commit_transaction(trans
, root
);
477 trans
= btrfs_start_transaction(root
, 1);
478 ret
= commit_tree_roots(trans
, root
->fs_info
);
480 ret
= __commit_transaction(trans
, root
);
482 write_ctree_super(trans
, root
);
483 btrfs_free_transaction(root
, trans
);
484 btrfs_free_block_groups(root
->fs_info
);
485 close(root
->fs_info
->fp
);
487 free_extent_buffer(root
->node
);
488 if (root
->fs_info
->extent_root
->node
)
489 free_extent_buffer(root
->fs_info
->extent_root
->node
);
490 if (root
->fs_info
->tree_root
->node
)
491 free_extent_buffer(root
->fs_info
->tree_root
->node
);
492 free_extent_buffer(root
->commit_root
);
493 free_extent_buffer(root
->fs_info
->sb_buffer
);
495 extent_map_tree_cleanup(&fs_info
->extent_cache
);
496 extent_map_tree_cleanup(&fs_info
->free_space_cache
);
497 extent_map_tree_cleanup(&fs_info
->pending_tree
);
498 extent_map_tree_cleanup(&fs_info
->pinned_extents
);
499 extent_map_tree_cleanup(&fs_info
->del_pending
);
500 extent_map_tree_cleanup(&fs_info
->block_group_cache
);
502 free(fs_info
->tree_root
);
503 free(fs_info
->extent_root
);
504 free(fs_info
->fs_root
);
510 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
511 struct extent_buffer
*eb
)
513 return clear_extent_buffer_dirty(eb
);
516 int wait_on_tree_block_writeback(struct btrfs_root
*root
,
517 struct extent_buffer
*eb
)
522 void btrfs_mark_buffer_dirty(struct extent_buffer
*eb
)
524 set_extent_buffer_dirty(eb
);
527 int btrfs_buffer_uptodate(struct extent_buffer
*eb
)
529 return extent_buffer_uptodate(eb
);
532 int btrfs_set_buffer_uptodate(struct extent_buffer
*eb
)
534 return set_extent_buffer_uptodate(eb
);