Modify the subvol and snapshot creation ioctls to work anywhere in the tree
[btrfs-progs-unstable.git] / disk-io.c
blobd2e1bdd6e159638be08fe736fc558103b27558bc
1 /*
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
20 #define __USE_XOPEN2K
21 #define _GNU_SOURCE 1
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include "kerncompat.h"
29 #include "radix-tree.h"
30 #include "ctree.h"
31 #include "disk-io.h"
32 #include "volumes.h"
33 #include "transaction.h"
34 #include "crc32c.h"
35 #include "utils.h"
36 #include "print-tree.h"
38 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
41 struct btrfs_fs_devices *fs_devices;
42 int ret = 1;
44 if (buf->start != btrfs_header_bytenr(buf))
45 return ret;
47 fs_devices = root->fs_info->fs_devices;
48 while (fs_devices) {
49 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
50 (unsigned long)btrfs_header_fsid(buf),
51 BTRFS_FSID_SIZE)) {
52 ret = 0;
53 break;
55 fs_devices = fs_devices->seed;
57 return ret;
60 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
62 return crc32c(seed, data, len);
65 void btrfs_csum_final(u32 crc, char *result)
67 *(__le32 *)result = ~cpu_to_le32(crc);
70 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
71 int verify)
73 char result[BTRFS_CRC32_SIZE];
74 u32 len;
75 u32 crc = ~(u32)0;
77 len = buf->len - BTRFS_CSUM_SIZE;
78 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
79 btrfs_csum_final(crc, result);
81 if (verify) {
82 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
83 printk("checksum verify failed on %llu wanted %X "
84 "found %X\n", (unsigned long long)buf->start,
85 *((int *)result), *((int *)buf));
86 return 1;
88 } else {
89 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
91 return 0;
94 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
95 u64 bytenr, u32 blocksize)
97 return find_extent_buffer(&root->fs_info->extent_cache,
98 bytenr, blocksize);
101 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
102 u64 bytenr, u32 blocksize)
104 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
105 blocksize);
108 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
109 u64 parent_transid)
111 int ret;
112 int dev_nr;
113 struct extent_buffer *eb;
114 u64 length;
115 struct btrfs_multi_bio *multi = NULL;
116 struct btrfs_device *device;
118 eb = btrfs_find_tree_block(root, bytenr, blocksize);
119 if (eb && btrfs_buffer_uptodate(eb, parent_transid)) {
120 free_extent_buffer(eb);
121 return 0;
124 dev_nr = 0;
125 length = blocksize;
126 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
127 bytenr, &length, &multi, 0);
128 BUG_ON(ret);
129 device = multi->stripes[0].dev;
130 device->total_ios++;
131 blocksize = min(blocksize, (u32)(64 * 1024));
132 readahead(device->fd, multi->stripes[0].physical, blocksize);
133 kfree(multi);
134 return 0;
137 static int verify_parent_transid(struct extent_io_tree *io_tree,
138 struct extent_buffer *eb, u64 parent_transid)
140 int ret;
142 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
143 return 0;
145 if (extent_buffer_uptodate(eb) &&
146 btrfs_header_generation(eb) == parent_transid) {
147 ret = 0;
148 goto out;
150 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
151 (unsigned long long)eb->start,
152 (unsigned long long)parent_transid,
153 (unsigned long long)btrfs_header_generation(eb));
154 ret = 1;
155 out:
156 clear_extent_buffer_uptodate(io_tree, eb);
157 return ret;
162 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
163 u32 blocksize, u64 parent_transid)
165 int ret;
166 int dev_nr;
167 struct extent_buffer *eb;
168 u64 length;
169 struct btrfs_multi_bio *multi = NULL;
170 struct btrfs_device *device;
171 int mirror_num = 0;
172 int num_copies;
174 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
175 if (!eb)
176 return NULL;
178 if (btrfs_buffer_uptodate(eb, parent_transid))
179 return eb;
181 dev_nr = 0;
182 length = blocksize;
183 while (1) {
184 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
185 eb->start, &length, &multi, mirror_num);
186 BUG_ON(ret);
187 device = multi->stripes[0].dev;
188 eb->fd = device->fd;
189 device->total_ios++;
190 eb->dev_bytenr = multi->stripes[0].physical;
191 kfree(multi);
192 ret = read_extent_from_disk(eb);
193 if (ret == 0 && check_tree_block(root, eb) == 0 &&
194 csum_tree_block(root, eb, 1) == 0 &&
195 verify_parent_transid(eb->tree, eb, parent_transid) == 0) {
196 btrfs_set_buffer_uptodate(eb);
197 return eb;
199 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
200 eb->start, eb->len);
201 if (num_copies == 1) {
202 break;
204 mirror_num++;
205 if (mirror_num > num_copies) {
206 break;
209 free_extent_buffer(eb);
210 return NULL;
213 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
214 struct extent_buffer *eb)
216 int ret;
217 int dev_nr;
218 u64 length;
219 struct btrfs_multi_bio *multi = NULL;
221 if (check_tree_block(root, eb))
222 BUG();
223 if (!btrfs_buffer_uptodate(eb, trans->transid))
224 BUG();
226 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
227 csum_tree_block(root, eb, 0);
229 dev_nr = 0;
230 length = eb->len;
231 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
232 eb->start, &length, &multi, 0);
234 while(dev_nr < multi->num_stripes) {
235 BUG_ON(ret);
236 eb->fd = multi->stripes[dev_nr].dev->fd;
237 eb->dev_bytenr = multi->stripes[dev_nr].physical;
238 multi->stripes[dev_nr].dev->total_ios++;
239 dev_nr++;
240 ret = write_extent_to_disk(eb);
241 BUG_ON(ret);
243 kfree(multi);
244 return 0;
247 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
248 u32 stripesize, struct btrfs_root *root,
249 struct btrfs_fs_info *fs_info, u64 objectid)
251 root->node = NULL;
252 root->commit_root = NULL;
253 root->sectorsize = sectorsize;
254 root->nodesize = nodesize;
255 root->leafsize = leafsize;
256 root->stripesize = stripesize;
257 root->ref_cows = 0;
258 root->track_dirty = 0;
260 root->fs_info = fs_info;
261 root->objectid = objectid;
262 root->last_trans = 0;
263 root->highest_inode = 0;
264 root->last_inode_alloc = 0;
266 INIT_LIST_HEAD(&root->dirty_list);
267 memset(&root->root_key, 0, sizeof(root->root_key));
268 memset(&root->root_item, 0, sizeof(root->root_item));
269 root->root_key.objectid = objectid;
270 return 0;
273 static int update_cowonly_root(struct btrfs_trans_handle *trans,
274 struct btrfs_root *root)
276 int ret;
277 u64 old_root_bytenr;
278 struct btrfs_root *tree_root = root->fs_info->tree_root;
280 btrfs_write_dirty_block_groups(trans, root);
281 while(1) {
282 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
283 if (old_root_bytenr == root->node->start)
284 break;
285 btrfs_set_root_bytenr(&root->root_item,
286 root->node->start);
287 btrfs_set_root_generation(&root->root_item,
288 trans->transid);
289 root->root_item.level = btrfs_header_level(root->node);
290 ret = btrfs_update_root(trans, tree_root,
291 &root->root_key,
292 &root->root_item);
293 BUG_ON(ret);
294 btrfs_write_dirty_block_groups(trans, root);
296 return 0;
299 static int commit_tree_roots(struct btrfs_trans_handle *trans,
300 struct btrfs_fs_info *fs_info)
302 struct btrfs_root *root;
303 struct list_head *next;
304 struct extent_buffer *eb;
306 if (fs_info->readonly)
307 return 0;
309 eb = fs_info->tree_root->node;
310 extent_buffer_get(eb);
311 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
312 free_extent_buffer(eb);
314 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
315 next = fs_info->dirty_cowonly_roots.next;
316 list_del_init(next);
317 root = list_entry(next, struct btrfs_root, dirty_list);
318 update_cowonly_root(trans, root);
320 return 0;
323 static int __commit_transaction(struct btrfs_trans_handle *trans,
324 struct btrfs_root *root)
326 u64 start;
327 u64 end;
328 struct extent_buffer *eb;
329 struct extent_io_tree *tree = &root->fs_info->extent_cache;
330 int ret;
332 while(1) {
333 ret = find_first_extent_bit(tree, 0, &start, &end,
334 EXTENT_DIRTY);
335 if (ret)
336 break;
337 while(start <= end) {
338 eb = find_first_extent_buffer(tree, start);
339 BUG_ON(!eb || eb->start != start);
340 ret = write_tree_block(trans, root, eb);
341 BUG_ON(ret);
342 start += eb->len;
343 clear_extent_buffer_dirty(eb);
344 free_extent_buffer(eb);
347 return 0;
350 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
351 struct btrfs_root *root)
353 int ret = 0;
354 struct btrfs_root *new_root = NULL;
355 struct btrfs_fs_info *fs_info = root->fs_info;
357 if (root->commit_root == root->node)
358 goto commit_tree;
360 new_root = malloc(sizeof(*new_root));
361 if (!new_root)
362 return -ENOMEM;
363 memcpy(new_root, root, sizeof(*new_root));
364 new_root->node = root->commit_root;
365 root->commit_root = NULL;
367 root->root_key.offset = trans->transid;
368 btrfs_set_root_bytenr(&root->root_item, root->node->start);
369 btrfs_set_root_generation(&root->root_item, root->root_key.offset);
370 root->root_item.level = btrfs_header_level(root->node);
371 ret = btrfs_insert_root(trans, fs_info->tree_root,
372 &root->root_key, &root->root_item);
373 BUG_ON(ret);
375 btrfs_set_root_refs(&new_root->root_item, 0);
376 ret = btrfs_update_root(trans, root->fs_info->tree_root,
377 &new_root->root_key, &new_root->root_item);
378 BUG_ON(ret);
380 ret = commit_tree_roots(trans, fs_info);
381 BUG_ON(ret);
382 ret = __commit_transaction(trans, root);
383 BUG_ON(ret);
384 write_ctree_super(trans, root);
385 btrfs_finish_extent_commit(trans, fs_info->extent_root,
386 &fs_info->pinned_extents);
387 btrfs_free_transaction(root, trans);
388 fs_info->running_transaction = NULL;
390 trans = btrfs_start_transaction(root, 1);
391 ret = btrfs_drop_snapshot(trans, new_root);
392 BUG_ON(ret);
393 ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
394 BUG_ON(ret);
395 commit_tree:
396 ret = commit_tree_roots(trans, fs_info);
397 BUG_ON(ret);
398 ret = __commit_transaction(trans, root);
399 BUG_ON(ret);
400 write_ctree_super(trans, root);
401 btrfs_finish_extent_commit(trans, fs_info->extent_root,
402 &fs_info->pinned_extents);
403 btrfs_free_transaction(root, trans);
404 free_extent_buffer(root->commit_root);
405 root->commit_root = NULL;
406 fs_info->running_transaction = NULL;
407 if (new_root) {
408 free_extent_buffer(new_root->node);
409 free(new_root);
411 return 0;
414 static int find_and_setup_root(struct btrfs_root *tree_root,
415 struct btrfs_fs_info *fs_info,
416 u64 objectid, struct btrfs_root *root)
418 int ret;
419 u32 blocksize;
420 u64 generation;
422 __setup_root(tree_root->nodesize, tree_root->leafsize,
423 tree_root->sectorsize, tree_root->stripesize,
424 root, fs_info, objectid);
425 ret = btrfs_find_last_root(tree_root, objectid,
426 &root->root_item, &root->root_key);
427 BUG_ON(ret);
429 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
430 generation = btrfs_root_generation(&root->root_item);
431 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
432 blocksize, generation);
433 BUG_ON(!root->node);
434 return 0;
437 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
439 if (root->node)
440 free_extent_buffer(root->node);
441 if (root->commit_root)
442 free_extent_buffer(root->commit_root);
444 free(root);
445 return 0;
448 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
449 struct btrfs_key *location)
451 struct btrfs_root *root;
452 struct btrfs_root *tree_root = fs_info->tree_root;
453 struct btrfs_path *path;
454 struct extent_buffer *l;
455 u64 generation;
456 u32 blocksize;
457 int ret = 0;
459 root = malloc(sizeof(*root));
460 if (!root)
461 return ERR_PTR(-ENOMEM);
462 memset(root, 0, sizeof(*root));
463 if (location->offset == (u64)-1) {
464 ret = find_and_setup_root(tree_root, fs_info,
465 location->objectid, root);
466 if (ret) {
467 free(root);
468 return ERR_PTR(ret);
470 goto insert;
473 __setup_root(tree_root->nodesize, tree_root->leafsize,
474 tree_root->sectorsize, tree_root->stripesize,
475 root, fs_info, location->objectid);
477 path = btrfs_alloc_path();
478 BUG_ON(!path);
479 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
480 if (ret != 0) {
481 if (ret > 0)
482 ret = -ENOENT;
483 goto out;
485 l = path->nodes[0];
486 read_extent_buffer(l, &root->root_item,
487 btrfs_item_ptr_offset(l, path->slots[0]),
488 sizeof(root->root_item));
489 memcpy(&root->root_key, location, sizeof(*location));
490 ret = 0;
491 out:
492 btrfs_release_path(root, path);
493 btrfs_free_path(path);
494 if (ret) {
495 free(root);
496 return ERR_PTR(ret);
498 generation = btrfs_root_generation(&root->root_item);
499 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
500 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
501 blocksize, generation);
502 BUG_ON(!root->node);
503 insert:
504 root->ref_cows = 1;
505 return root;
508 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
510 int fp;
511 struct btrfs_root *root;
512 int flags = O_CREAT | O_RDWR;
514 if (!writes)
515 flags = O_RDONLY;
517 fp = open(filename, flags, 0600);
518 if (fp < 0) {
519 return NULL;
521 root = open_ctree_fd(fp, filename, sb_bytenr, writes);
522 close(fp);
524 return root;
527 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
528 int writes)
530 u32 sectorsize;
531 u32 nodesize;
532 u32 leafsize;
533 u32 blocksize;
534 u32 stripesize;
535 u64 generation;
536 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
537 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
538 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
539 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
540 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
541 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
542 int ret;
543 struct btrfs_super_block *disk_super;
544 struct btrfs_fs_devices *fs_devices = NULL;
545 u64 total_devs;
547 if (sb_bytenr == 0)
548 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
550 ret = btrfs_scan_one_device(fp, path, &fs_devices,
551 &total_devs, sb_bytenr);
553 if (ret) {
554 fprintf(stderr, "No valid Btrfs found on %s\n", path);
555 return NULL;
558 if (total_devs != 1) {
559 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
560 BUG_ON(ret);
563 memset(fs_info, 0, sizeof(*fs_info));
564 fs_info->fs_root = root;
565 fs_info->tree_root = tree_root;
566 fs_info->extent_root = extent_root;
567 fs_info->chunk_root = chunk_root;
568 fs_info->dev_root = dev_root;
570 if (!writes)
571 fs_info->readonly = 1;
573 extent_io_tree_init(&fs_info->extent_cache);
574 extent_io_tree_init(&fs_info->free_space_cache);
575 extent_io_tree_init(&fs_info->block_group_cache);
576 extent_io_tree_init(&fs_info->pinned_extents);
577 extent_io_tree_init(&fs_info->pending_del);
578 extent_io_tree_init(&fs_info->extent_ins);
580 cache_tree_init(&fs_info->mapping_tree.cache_tree);
582 mutex_init(&fs_info->fs_mutex);
583 fs_info->fs_devices = fs_devices;
584 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
585 INIT_LIST_HEAD(&fs_info->space_info);
587 __setup_root(4096, 4096, 4096, 4096, tree_root,
588 fs_info, BTRFS_ROOT_TREE_OBJECTID);
590 if (writes)
591 ret = btrfs_open_devices(fs_devices, O_RDWR);
592 else
593 ret = btrfs_open_devices(fs_devices, O_RDONLY);
594 BUG_ON(ret);
596 ret = btrfs_bootstrap_super_map(&fs_info->mapping_tree, fs_devices);
597 BUG_ON(ret);
598 fs_info->sb_buffer = btrfs_find_create_tree_block(tree_root, sb_bytenr,
599 4096);
600 BUG_ON(!fs_info->sb_buffer);
601 fs_info->sb_buffer->fd = fs_devices->latest_bdev;
602 fs_info->sb_buffer->dev_bytenr = sb_bytenr;
603 ret = read_extent_from_disk(fs_info->sb_buffer);
604 BUG_ON(ret);
605 btrfs_set_buffer_uptodate(fs_info->sb_buffer);
607 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
608 sizeof(fs_info->super_copy));
609 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
610 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
611 BTRFS_FSID_SIZE);
613 disk_super = &fs_info->super_copy;
614 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
615 sizeof(disk_super->magic))) {
616 printk("No valid btrfs found\n");
617 BUG_ON(1);
619 nodesize = btrfs_super_nodesize(disk_super);
620 leafsize = btrfs_super_leafsize(disk_super);
621 sectorsize = btrfs_super_sectorsize(disk_super);
622 stripesize = btrfs_super_stripesize(disk_super);
623 tree_root->nodesize = nodesize;
624 tree_root->leafsize = leafsize;
625 tree_root->sectorsize = sectorsize;
626 tree_root->stripesize = stripesize;
628 ret = btrfs_read_super_device(tree_root, fs_info->sb_buffer);
629 BUG_ON(ret);
630 ret = btrfs_read_sys_array(tree_root);
631 BUG_ON(ret);
632 blocksize = btrfs_level_size(tree_root,
633 btrfs_super_chunk_root_level(disk_super));
634 generation = btrfs_super_chunk_root_generation(disk_super);
636 __setup_root(nodesize, leafsize, sectorsize, stripesize,
637 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
639 chunk_root->node = read_tree_block(chunk_root,
640 btrfs_super_chunk_root(disk_super),
641 blocksize, generation);
643 BUG_ON(!chunk_root->node);
645 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
646 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
647 BTRFS_UUID_SIZE);
649 ret = btrfs_read_chunk_tree(chunk_root);
650 BUG_ON(ret);
652 blocksize = btrfs_level_size(tree_root,
653 btrfs_super_root_level(disk_super));
654 generation = btrfs_super_generation(disk_super);
656 tree_root->node = read_tree_block(tree_root,
657 btrfs_super_root(disk_super),
658 blocksize, generation);
659 BUG_ON(!tree_root->node);
660 ret = find_and_setup_root(tree_root, fs_info,
661 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
662 BUG_ON(ret);
663 extent_root->track_dirty = 1;
665 ret = find_and_setup_root(tree_root, fs_info,
666 BTRFS_DEV_TREE_OBJECTID, dev_root);
667 BUG_ON(ret);
668 dev_root->track_dirty = 1;
670 ret = find_and_setup_root(tree_root, fs_info,
671 BTRFS_FS_TREE_OBJECTID, root);
672 BUG_ON(ret);
673 root->ref_cows = 1;
674 fs_info->generation = btrfs_super_generation(disk_super) + 1;
675 btrfs_read_block_groups(root);
677 fs_info->data_alloc_profile = (u64)-1;
678 fs_info->metadata_alloc_profile = (u64)-1;
679 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
681 return root;
684 int write_all_supers(struct btrfs_root *root)
686 struct list_head *cur;
687 struct list_head *head = &root->fs_info->fs_devices->devices;
688 struct btrfs_device *dev;
689 struct extent_buffer *sb;
690 struct btrfs_dev_item *dev_item;
691 int ret;
693 sb = root->fs_info->sb_buffer;
694 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
695 dev_item);
696 list_for_each(cur, head) {
697 dev = list_entry(cur, struct btrfs_device, dev_list);
698 if (!dev->writeable)
699 continue;
701 btrfs_set_device_generation(sb, dev_item, 0);
702 btrfs_set_device_type(sb, dev_item, dev->type);
703 btrfs_set_device_id(sb, dev_item, dev->devid);
704 btrfs_set_device_total_bytes(sb, dev_item, dev->total_bytes);
705 btrfs_set_device_bytes_used(sb, dev_item, dev->bytes_used);
706 btrfs_set_device_io_align(sb, dev_item, dev->io_align);
707 btrfs_set_device_io_width(sb, dev_item, dev->io_width);
708 btrfs_set_device_sector_size(sb, dev_item, dev->sector_size);
709 write_extent_buffer(sb, dev->uuid,
710 (unsigned long)btrfs_device_uuid(dev_item),
711 BTRFS_UUID_SIZE);
712 write_extent_buffer(sb, dev->fs_devices->fsid,
713 (unsigned long)btrfs_device_fsid(dev_item),
714 BTRFS_UUID_SIZE);
715 sb->fd = dev->fd;
716 sb->dev_bytenr = sb->start;
717 btrfs_set_header_flag(sb, BTRFS_HEADER_FLAG_WRITTEN);
718 csum_tree_block(root, sb, 0);
719 ret = write_extent_to_disk(sb);
720 BUG_ON(ret);
722 return 0;
725 int write_ctree_super(struct btrfs_trans_handle *trans,
726 struct btrfs_root *root)
728 int ret;
729 struct btrfs_root *tree_root = root->fs_info->tree_root;
730 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
732 if (root->fs_info->readonly)
733 return 0;
735 btrfs_set_super_generation(&root->fs_info->super_copy,
736 trans->transid);
737 btrfs_set_super_root(&root->fs_info->super_copy,
738 tree_root->node->start);
739 btrfs_set_super_root_level(&root->fs_info->super_copy,
740 btrfs_header_level(tree_root->node));
741 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
742 chunk_root->node->start);
743 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
744 btrfs_header_level(chunk_root->node));
745 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
746 btrfs_header_generation(chunk_root->node));
747 write_extent_buffer(root->fs_info->sb_buffer,
748 &root->fs_info->super_copy, 0,
749 sizeof(root->fs_info->super_copy));
750 ret = write_all_supers(root);
751 if (ret)
752 fprintf(stderr, "failed to write new super block err %d\n", ret);
753 return ret;
756 static int close_all_devices(struct btrfs_fs_info *fs_info)
758 struct list_head *list;
759 struct list_head *next;
760 struct btrfs_device *device;
762 return 0;
764 list = &fs_info->fs_devices->devices;
765 list_for_each(next, list) {
766 device = list_entry(next, struct btrfs_device, dev_list);
767 close(device->fd);
769 return 0;
772 int close_ctree(struct btrfs_root *root)
774 int ret;
775 struct btrfs_trans_handle *trans;
776 struct btrfs_fs_info *fs_info = root->fs_info;
778 trans = btrfs_start_transaction(root, 1);
779 btrfs_commit_transaction(trans, root);
780 trans = btrfs_start_transaction(root, 1);
781 ret = commit_tree_roots(trans, root->fs_info);
782 BUG_ON(ret);
783 ret = __commit_transaction(trans, root);
784 BUG_ON(ret);
785 write_ctree_super(trans, root);
786 btrfs_free_transaction(root, trans);
787 btrfs_free_block_groups(root->fs_info);
788 if (root->node)
789 free_extent_buffer(root->node);
790 if (root->fs_info->extent_root->node)
791 free_extent_buffer(root->fs_info->extent_root->node);
792 if (root->fs_info->tree_root->node)
793 free_extent_buffer(root->fs_info->tree_root->node);
794 free_extent_buffer(root->commit_root);
795 free_extent_buffer(root->fs_info->sb_buffer);
797 if (root->fs_info->chunk_root->node);
798 free_extent_buffer(root->fs_info->chunk_root->node);
800 if (root->fs_info->dev_root->node);
801 free_extent_buffer(root->fs_info->dev_root->node);
803 close_all_devices(root->fs_info);
804 extent_io_tree_cleanup(&fs_info->extent_cache);
805 extent_io_tree_cleanup(&fs_info->free_space_cache);
806 extent_io_tree_cleanup(&fs_info->block_group_cache);
807 extent_io_tree_cleanup(&fs_info->pinned_extents);
808 extent_io_tree_cleanup(&fs_info->pending_del);
809 extent_io_tree_cleanup(&fs_info->extent_ins);
811 free(fs_info->tree_root);
812 free(fs_info->extent_root);
813 free(fs_info->fs_root);
814 free(fs_info->chunk_root);
815 free(fs_info->dev_root);
816 free(fs_info);
818 return 0;
821 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
822 struct extent_buffer *eb)
824 return clear_extent_buffer_dirty(eb);
827 int wait_on_tree_block_writeback(struct btrfs_root *root,
828 struct extent_buffer *eb)
830 return 0;
833 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
835 set_extent_buffer_dirty(eb);
838 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
840 int ret;
842 ret = extent_buffer_uptodate(buf);
843 if (!ret)
844 return ret;
846 ret = verify_parent_transid(buf->tree, buf, parent_transid);
847 return !ret;
850 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
852 return set_extent_buffer_uptodate(eb);