Print the version string in a few programs
[platform/upstream/btrfs-progs.git] / disk-io.c
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
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"
37
38 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
39 {
40         if (buf->start != btrfs_header_bytenr(buf))
41                 return 1;
42
43         if (memcmp_extent_buffer(buf, root->fs_info->fsid,
44                                  (unsigned long)btrfs_header_fsid(buf),
45                                  BTRFS_FSID_SIZE))
46                 return 1;
47         return 0;
48 }
49
50 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
51 {
52         return crc32c(seed, data, len);
53 }
54
55 void btrfs_csum_final(u32 crc, char *result)
56 {
57         *(__le32 *)result = ~cpu_to_le32(crc);
58 }
59
60 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
61                     int verify)
62 {
63         char result[BTRFS_CRC32_SIZE];
64         u32 len;
65         u32 crc = ~(u32)0;
66
67         len = buf->len - BTRFS_CSUM_SIZE;
68         crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
69         btrfs_csum_final(crc, result);
70
71         if (verify) {
72                 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
73                         printk("checksum verify failed on %llu wanted %X "
74                                "found %X\n", (unsigned long long)buf->start,
75                                *((int *)result), *((int *)buf));
76                         return 1;
77                 }
78         } else {
79                 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
80         }
81         return 0;
82 }
83
84 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
85                                             u64 bytenr, u32 blocksize)
86 {
87         return find_extent_buffer(&root->fs_info->extent_cache,
88                                   bytenr, blocksize);
89 }
90
91 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
92                                                  u64 bytenr, u32 blocksize)
93 {
94         return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
95                                    blocksize);
96 }
97
98 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
99                          u64 parent_transid)
100 {
101         int ret;
102         int dev_nr;
103         struct extent_buffer *eb;
104         u64 length;
105         struct btrfs_multi_bio *multi = NULL;
106         struct btrfs_device *device;
107
108         eb = btrfs_find_tree_block(root, bytenr, blocksize);
109         if (eb && btrfs_buffer_uptodate(eb, parent_transid)) {
110                 free_extent_buffer(eb);
111                 return 0;
112         }
113
114         dev_nr = 0;
115         length = blocksize;
116         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
117                               bytenr, &length, &multi, 0);
118         BUG_ON(ret);
119         device = multi->stripes[0].dev;
120         device->total_ios++;
121         blocksize = min(blocksize, (u32)(64 * 1024));
122         readahead(device->fd, multi->stripes[0].physical, blocksize);
123         kfree(multi);
124         return 0;
125 }
126
127 static int verify_parent_transid(struct extent_io_tree *io_tree,
128                                  struct extent_buffer *eb, u64 parent_transid)
129 {
130         int ret;
131
132         if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
133                 return 0;
134
135         if (extent_buffer_uptodate(eb) &&
136             btrfs_header_generation(eb) == parent_transid) {
137                 ret = 0;
138                 goto out;
139         }
140         printk("parent transid verify failed on %llu wanted %llu found %llu\n",
141                (unsigned long long)eb->start,
142                (unsigned long long)parent_transid,
143                (unsigned long long)btrfs_header_generation(eb));
144         ret = 1;
145 out:
146         clear_extent_buffer_uptodate(io_tree, eb);
147         return ret;
148
149 }
150
151
152 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
153                                      u32 blocksize, u64 parent_transid)
154 {
155         int ret;
156         int dev_nr;
157         struct extent_buffer *eb;
158         u64 length;
159         struct btrfs_multi_bio *multi = NULL;
160         struct btrfs_device *device;
161         int mirror_num = 0;
162         int num_copies;
163
164         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
165         if (!eb)
166                 return NULL;
167
168         if (btrfs_buffer_uptodate(eb, parent_transid))
169                 return eb;
170
171         dev_nr = 0;
172         length = blocksize;
173         while (1) {
174                 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
175                                       eb->start, &length, &multi, mirror_num);
176                 BUG_ON(ret);
177                 device = multi->stripes[0].dev;
178                 eb->fd = device->fd;
179                 device->total_ios++;
180                 eb->dev_bytenr = multi->stripes[0].physical;
181                 kfree(multi);
182                 ret = read_extent_from_disk(eb);
183                 if (ret == 0 && check_tree_block(root, eb) == 0 &&
184                     csum_tree_block(root, eb, 1) == 0 &&
185                     verify_parent_transid(eb->tree, eb, parent_transid) == 0) {
186                         btrfs_set_buffer_uptodate(eb);
187                         return eb;
188                 }
189                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
190                                               eb->start, eb->len);
191                 if (num_copies == 1) {
192                         break;
193                 }
194                 mirror_num++;
195                 if (mirror_num > num_copies) {
196                         break;
197                 }
198         }
199         free_extent_buffer(eb);
200         return NULL;
201 }
202
203 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
204                      struct extent_buffer *eb)
205 {
206         int ret;
207         int dev_nr;
208         u64 length;
209         struct btrfs_multi_bio *multi = NULL;
210
211         if (check_tree_block(root, eb))
212                 BUG();
213         if (!btrfs_buffer_uptodate(eb, trans->transid))
214                 BUG();
215
216         btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
217         csum_tree_block(root, eb, 0);
218
219         dev_nr = 0;
220         length = eb->len;
221         ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
222                               eb->start, &length, &multi, 0);
223
224         while(dev_nr < multi->num_stripes) {
225                 BUG_ON(ret);
226                 eb->fd = multi->stripes[dev_nr].dev->fd;
227                 eb->dev_bytenr = multi->stripes[dev_nr].physical;
228                 multi->stripes[dev_nr].dev->total_ios++;
229                 dev_nr++;
230                 ret = write_extent_to_disk(eb);
231                 BUG_ON(ret);
232         }
233         kfree(multi);
234         return 0;
235 }
236
237 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
238                         u32 stripesize, struct btrfs_root *root,
239                         struct btrfs_fs_info *fs_info, u64 objectid)
240 {
241         root->node = NULL;
242         root->commit_root = NULL;
243         root->sectorsize = sectorsize;
244         root->nodesize = nodesize;
245         root->leafsize = leafsize;
246         root->stripesize = stripesize;
247         root->ref_cows = 0;
248         root->track_dirty = 0;
249
250         root->fs_info = fs_info;
251         root->objectid = objectid;
252         root->last_trans = 0;
253         root->highest_inode = 0;
254         root->last_inode_alloc = 0;
255
256         INIT_LIST_HEAD(&root->dirty_list);
257         memset(&root->root_key, 0, sizeof(root->root_key));
258         memset(&root->root_item, 0, sizeof(root->root_item));
259         root->root_key.objectid = objectid;
260         return 0;
261 }
262
263 static int update_cowonly_root(struct btrfs_trans_handle *trans,
264                                struct btrfs_root *root)
265 {
266         int ret;
267         u64 old_root_bytenr;
268         struct btrfs_root *tree_root = root->fs_info->tree_root;
269
270         btrfs_write_dirty_block_groups(trans, root);
271         while(1) {
272                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
273                 if (old_root_bytenr == root->node->start)
274                         break;
275                 btrfs_set_root_bytenr(&root->root_item,
276                                        root->node->start);
277                 root->root_item.level = btrfs_header_level(root->node);
278                 ret = btrfs_update_root(trans, tree_root,
279                                         &root->root_key,
280                                         &root->root_item);
281                 BUG_ON(ret);
282                 btrfs_write_dirty_block_groups(trans, root);
283         }
284         return 0;
285 }
286
287 static int commit_tree_roots(struct btrfs_trans_handle *trans,
288                              struct btrfs_fs_info *fs_info)
289 {
290         struct btrfs_root *root;
291         struct list_head *next;
292
293         while(!list_empty(&fs_info->dirty_cowonly_roots)) {
294                 next = fs_info->dirty_cowonly_roots.next;
295                 list_del_init(next);
296                 root = list_entry(next, struct btrfs_root, dirty_list);
297                 update_cowonly_root(trans, root);
298         }
299         return 0;
300 }
301
302 static int __commit_transaction(struct btrfs_trans_handle *trans,
303                                 struct btrfs_root *root)
304 {
305         u64 start;
306         u64 end;
307         struct extent_buffer *eb;
308         struct extent_io_tree *tree = &root->fs_info->extent_cache;
309         int ret;
310
311         while(1) {
312                 ret = find_first_extent_bit(tree, 0, &start, &end,
313                                             EXTENT_DIRTY);
314                 if (ret)
315                         break;
316                 while(start <= end) {
317                         eb = find_first_extent_buffer(tree, start);
318                         BUG_ON(!eb || eb->start != start);
319                         ret = write_tree_block(trans, root, eb);
320                         BUG_ON(ret);
321                         start += eb->len;
322                         clear_extent_buffer_dirty(eb);
323                         free_extent_buffer(eb);
324                 }
325         }
326         return 0;
327 }
328
329 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
330                              struct btrfs_root *root)
331 {
332         int ret = 0;
333         struct btrfs_root *new_root = NULL;
334         struct btrfs_fs_info *fs_info = root->fs_info;
335
336         if (root->commit_root == root->node)
337                 goto commit_tree;
338
339         new_root = malloc(sizeof(*new_root));
340         if (!new_root)
341                 return -ENOMEM;
342         memcpy(new_root, root, sizeof(*new_root));
343         new_root->node = root->commit_root;
344         root->commit_root = NULL;
345
346         root->root_key.offset = trans->transid;
347         btrfs_set_root_bytenr(&root->root_item, root->node->start);
348         root->root_item.level = btrfs_header_level(root->node);
349         ret = btrfs_insert_root(trans, fs_info->tree_root,
350                                 &root->root_key, &root->root_item);
351         BUG_ON(ret);
352
353         btrfs_set_root_refs(&new_root->root_item, 0);
354         ret = btrfs_update_root(trans, root->fs_info->tree_root,
355                                 &new_root->root_key, &new_root->root_item);
356         BUG_ON(ret);
357
358         ret = commit_tree_roots(trans, fs_info);
359         BUG_ON(ret);
360         ret = __commit_transaction(trans, root);
361         BUG_ON(ret);
362         write_ctree_super(trans, root);
363         btrfs_finish_extent_commit(trans, fs_info->extent_root,
364                                    &fs_info->pinned_extents);
365         btrfs_free_transaction(root, trans);
366         fs_info->running_transaction = NULL;
367
368         trans = btrfs_start_transaction(root, 1);
369         ret = btrfs_drop_snapshot(trans, new_root);
370         BUG_ON(ret);
371         ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
372         BUG_ON(ret);
373 commit_tree:
374         ret = commit_tree_roots(trans, fs_info);
375         BUG_ON(ret);
376         ret = __commit_transaction(trans, root);
377         BUG_ON(ret);
378         write_ctree_super(trans, root);
379         btrfs_finish_extent_commit(trans, fs_info->extent_root,
380                                    &fs_info->pinned_extents);
381         btrfs_free_transaction(root, trans);
382         free_extent_buffer(root->commit_root);
383         root->commit_root = NULL;
384         fs_info->running_transaction = NULL;
385         if (new_root) {
386                 free_extent_buffer(new_root->node);
387                 free(new_root);
388         }
389         return 0;
390 }
391
392 static int find_and_setup_root(struct btrfs_root *tree_root,
393                                struct btrfs_fs_info *fs_info,
394                                u64 objectid, struct btrfs_root *root)
395 {
396         int ret;
397         u32 blocksize;
398
399         __setup_root(tree_root->nodesize, tree_root->leafsize,
400                      tree_root->sectorsize, tree_root->stripesize,
401                      root, fs_info, objectid);
402         ret = btrfs_find_last_root(tree_root, objectid,
403                                    &root->root_item, &root->root_key);
404         BUG_ON(ret);
405
406         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
407         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
408                                      blocksize, 0);
409         BUG_ON(!root->node);
410         return 0;
411 }
412
413 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
414 {
415         if (root->node)
416                 free_extent_buffer(root->node);
417         if (root->commit_root)
418                 free_extent_buffer(root->commit_root);
419
420         free(root);
421         return 0;
422 }
423
424 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
425                                       struct btrfs_key *location)
426 {
427         struct btrfs_root *root;
428         struct btrfs_root *tree_root = fs_info->tree_root;
429         struct btrfs_path *path;
430         struct extent_buffer *l;
431         u32 blocksize;
432         int ret = 0;
433
434         root = malloc(sizeof(*root));
435         if (!root)
436                 return ERR_PTR(-ENOMEM);
437         memset(root, 0, sizeof(*root));
438         if (location->offset == (u64)-1) {
439                 ret = find_and_setup_root(tree_root, fs_info,
440                                           location->objectid, root);
441                 if (ret) {
442                         free(root);
443                         return ERR_PTR(ret);
444                 }
445                 goto insert;
446         }
447
448         __setup_root(tree_root->nodesize, tree_root->leafsize,
449                      tree_root->sectorsize, tree_root->stripesize,
450                      root, fs_info, location->objectid);
451
452         path = btrfs_alloc_path();
453         BUG_ON(!path);
454         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
455         if (ret != 0) {
456                 if (ret > 0)
457                         ret = -ENOENT;
458                 goto out;
459         }
460         l = path->nodes[0];
461         read_extent_buffer(l, &root->root_item,
462                btrfs_item_ptr_offset(l, path->slots[0]),
463                sizeof(root->root_item));
464         memcpy(&root->root_key, location, sizeof(*location));
465         ret = 0;
466 out:
467         btrfs_release_path(root, path);
468         btrfs_free_path(path);
469         if (ret) {
470                 free(root);
471                 return ERR_PTR(ret);
472         }
473         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
474         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
475                                      blocksize, 0);
476         BUG_ON(!root->node);
477 insert:
478         root->ref_cows = 1;
479         return root;
480 }
481
482 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
483 {
484         int fp;
485         struct btrfs_root *root;
486         int flags = O_CREAT | O_RDWR;
487
488         if (!writes)
489                 flags = O_RDONLY;
490
491         fp = open(filename, flags, 0600);
492         if (fp < 0) {
493                 return NULL;
494         }
495         root = open_ctree_fd(fp, filename, sb_bytenr, writes);
496         close(fp);
497
498         return root;
499 }
500
501 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
502                                  int writes)
503 {
504         u32 sectorsize;
505         u32 nodesize;
506         u32 leafsize;
507         u32 blocksize;
508         u32 stripesize;
509         struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
510         struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
511         struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
512         struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
513         struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
514         struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
515         int ret;
516         struct btrfs_super_block *disk_super;
517         struct btrfs_fs_devices *fs_devices = NULL;
518         u64 total_devs;
519
520         if (sb_bytenr == 0)
521                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
522
523         ret = btrfs_scan_one_device(fp, path, &fs_devices,
524                                     &total_devs, sb_bytenr);
525
526         if (ret) {
527                 fprintf(stderr, "No valid Btrfs found on %s\n", path);
528                 return NULL;
529         }
530
531         if (total_devs != 1) {
532                 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
533                 BUG_ON(ret);
534         }
535
536         memset(fs_info, 0, sizeof(*fs_info));
537         fs_info->fs_root = root;
538         fs_info->tree_root = tree_root;
539         fs_info->extent_root = extent_root;
540         fs_info->chunk_root = chunk_root;
541         fs_info->dev_root = dev_root;
542
543         if (!writes)
544                 fs_info->readonly = 1;
545
546         extent_io_tree_init(&fs_info->extent_cache);
547         extent_io_tree_init(&fs_info->free_space_cache);
548         extent_io_tree_init(&fs_info->block_group_cache);
549         extent_io_tree_init(&fs_info->pinned_extents);
550         extent_io_tree_init(&fs_info->pending_del);
551         extent_io_tree_init(&fs_info->extent_ins);
552
553         cache_tree_init(&fs_info->mapping_tree.cache_tree);
554
555         mutex_init(&fs_info->fs_mutex);
556         fs_info->fs_devices = fs_devices;
557         INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
558         INIT_LIST_HEAD(&fs_info->space_info);
559
560         __setup_root(4096, 4096, 4096, 4096, tree_root,
561                      fs_info, BTRFS_ROOT_TREE_OBJECTID);
562
563         if (writes)
564                 ret = btrfs_open_devices(fs_devices, O_RDWR);
565         else
566                 ret = btrfs_open_devices(fs_devices, O_RDONLY);
567         BUG_ON(ret);
568
569         ret = btrfs_bootstrap_super_map(&fs_info->mapping_tree, fs_devices);
570         BUG_ON(ret);
571         fs_info->sb_buffer = btrfs_find_create_tree_block(tree_root, sb_bytenr,
572                                                           4096);
573         BUG_ON(!fs_info->sb_buffer);
574         fs_info->sb_buffer->fd = fs_devices->latest_bdev;
575         fs_info->sb_buffer->dev_bytenr = sb_bytenr;
576         ret = read_extent_from_disk(fs_info->sb_buffer);
577         BUG_ON(ret);
578         btrfs_set_buffer_uptodate(fs_info->sb_buffer);
579
580         read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
581                            sizeof(fs_info->super_copy));
582         read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
583                            (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
584                            BTRFS_FSID_SIZE);
585
586         disk_super = &fs_info->super_copy;
587         if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
588                     sizeof(disk_super->magic))) {
589                 printk("No valid btrfs found\n");
590                 BUG_ON(1);
591         }
592         nodesize = btrfs_super_nodesize(disk_super);
593         leafsize = btrfs_super_leafsize(disk_super);
594         sectorsize = btrfs_super_sectorsize(disk_super);
595         stripesize = btrfs_super_stripesize(disk_super);
596         tree_root->nodesize = nodesize;
597         tree_root->leafsize = leafsize;
598         tree_root->sectorsize = sectorsize;
599         tree_root->stripesize = stripesize;
600
601         ret = btrfs_read_super_device(tree_root, fs_info->sb_buffer);
602         BUG_ON(ret);
603         ret = btrfs_read_sys_array(tree_root);
604         BUG_ON(ret);
605         blocksize = btrfs_level_size(tree_root,
606                                      btrfs_super_chunk_root_level(disk_super));
607
608         __setup_root(nodesize, leafsize, sectorsize, stripesize,
609                      chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
610
611         chunk_root->node = read_tree_block(chunk_root,
612                                            btrfs_super_chunk_root(disk_super),
613                                            blocksize, 0);
614
615         BUG_ON(!chunk_root->node);
616
617         read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
618                  (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
619                  BTRFS_UUID_SIZE);
620
621         ret = btrfs_read_chunk_tree(chunk_root);
622         BUG_ON(ret);
623
624         blocksize = btrfs_level_size(tree_root,
625                                      btrfs_super_root_level(disk_super));
626
627         tree_root->node = read_tree_block(tree_root,
628                                           btrfs_super_root(disk_super),
629                                           blocksize, 0);
630         BUG_ON(!tree_root->node);
631         ret = find_and_setup_root(tree_root, fs_info,
632                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root);
633         BUG_ON(ret);
634         extent_root->track_dirty = 1;
635
636         ret = find_and_setup_root(tree_root, fs_info,
637                                   BTRFS_DEV_TREE_OBJECTID, dev_root);
638         BUG_ON(ret);
639         dev_root->track_dirty = 1;
640
641         ret = find_and_setup_root(tree_root, fs_info,
642                                   BTRFS_FS_TREE_OBJECTID, root);
643         BUG_ON(ret);
644         root->ref_cows = 1;
645         fs_info->generation = btrfs_super_generation(disk_super) + 1;
646         btrfs_read_block_groups(root);
647
648         fs_info->data_alloc_profile = (u64)-1;
649         fs_info->metadata_alloc_profile = (u64)-1;
650         fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
651
652         return root;
653 }
654
655 int write_all_supers(struct btrfs_root *root)
656 {
657         struct list_head *cur;
658         struct list_head *head = &root->fs_info->fs_devices->devices;
659         struct btrfs_device *dev;
660         struct extent_buffer *sb;
661         struct btrfs_dev_item *dev_item;
662         int ret;
663
664         sb = root->fs_info->sb_buffer;
665         dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
666                                                       dev_item);
667         list_for_each(cur, head) {
668                 dev = list_entry(cur, struct btrfs_device, dev_list);
669                 btrfs_set_device_type(sb, dev_item, dev->type);
670                 btrfs_set_device_id(sb, dev_item, dev->devid);
671                 btrfs_set_device_total_bytes(sb, dev_item, dev->total_bytes);
672                 btrfs_set_device_bytes_used(sb, dev_item, dev->bytes_used);
673                 btrfs_set_device_io_align(sb, dev_item, dev->io_align);
674                 btrfs_set_device_io_width(sb, dev_item, dev->io_width);
675                 btrfs_set_device_sector_size(sb, dev_item, dev->sector_size);
676                 write_extent_buffer(sb, dev->uuid,
677                                     (unsigned long)btrfs_device_uuid(dev_item),
678                                     BTRFS_UUID_SIZE);
679                 sb->fd = dev->fd;
680                 sb->dev_bytenr = sb->start;
681                 btrfs_set_header_flag(sb, BTRFS_HEADER_FLAG_WRITTEN);
682                 csum_tree_block(root, sb, 0);
683                 ret = write_extent_to_disk(sb);
684                 BUG_ON(ret);
685         }
686         return 0;
687 }
688
689 int write_ctree_super(struct btrfs_trans_handle *trans,
690                       struct btrfs_root *root)
691 {
692         int ret;
693         struct btrfs_root *tree_root = root->fs_info->tree_root;
694         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
695
696         if (root->fs_info->readonly)
697                 return 0;
698
699         btrfs_set_super_generation(&root->fs_info->super_copy,
700                                    trans->transid);
701         btrfs_set_super_root(&root->fs_info->super_copy,
702                              tree_root->node->start);
703         btrfs_set_super_root_level(&root->fs_info->super_copy,
704                                    btrfs_header_level(tree_root->node));
705         btrfs_set_super_chunk_root(&root->fs_info->super_copy,
706                                    chunk_root->node->start);
707         btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
708                                          btrfs_header_level(chunk_root->node));
709         write_extent_buffer(root->fs_info->sb_buffer,
710                             &root->fs_info->super_copy, 0,
711                             sizeof(root->fs_info->super_copy));
712         ret = write_all_supers(root);
713         if (ret)
714                 fprintf(stderr, "failed to write new super block err %d\n", ret);
715         return ret;
716 }
717
718 static int close_all_devices(struct btrfs_fs_info *fs_info)
719 {
720         struct list_head *list;
721         struct list_head *next;
722         struct btrfs_device *device;
723
724         return 0;
725
726         list = &fs_info->fs_devices->devices;
727         list_for_each(next, list) {
728                 device = list_entry(next, struct btrfs_device, dev_list);
729                 close(device->fd);
730         }
731         return 0;
732 }
733
734 int close_ctree(struct btrfs_root *root)
735 {
736         int ret;
737         struct btrfs_trans_handle *trans;
738         struct btrfs_fs_info *fs_info = root->fs_info;
739
740         trans = btrfs_start_transaction(root, 1);
741         btrfs_commit_transaction(trans, root);
742         trans = btrfs_start_transaction(root, 1);
743         ret = commit_tree_roots(trans, root->fs_info);
744         BUG_ON(ret);
745         ret = __commit_transaction(trans, root);
746         BUG_ON(ret);
747         write_ctree_super(trans, root);
748         btrfs_free_transaction(root, trans);
749         btrfs_free_block_groups(root->fs_info);
750         if (root->node)
751                 free_extent_buffer(root->node);
752         if (root->fs_info->extent_root->node)
753                 free_extent_buffer(root->fs_info->extent_root->node);
754         if (root->fs_info->tree_root->node)
755                 free_extent_buffer(root->fs_info->tree_root->node);
756         free_extent_buffer(root->commit_root);
757         free_extent_buffer(root->fs_info->sb_buffer);
758
759         if (root->fs_info->chunk_root->node);
760                 free_extent_buffer(root->fs_info->chunk_root->node);
761
762         if (root->fs_info->dev_root->node);
763                 free_extent_buffer(root->fs_info->dev_root->node);
764
765         close_all_devices(root->fs_info);
766         extent_io_tree_cleanup(&fs_info->extent_cache);
767         extent_io_tree_cleanup(&fs_info->free_space_cache);
768         extent_io_tree_cleanup(&fs_info->block_group_cache);
769         extent_io_tree_cleanup(&fs_info->pinned_extents);
770         extent_io_tree_cleanup(&fs_info->pending_del);
771         extent_io_tree_cleanup(&fs_info->extent_ins);
772
773         free(fs_info->tree_root);
774         free(fs_info->extent_root);
775         free(fs_info->fs_root);
776         free(fs_info->chunk_root);
777         free(fs_info->dev_root);
778         free(fs_info);
779
780         return 0;
781 }
782
783 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
784                      struct extent_buffer *eb)
785 {
786         return clear_extent_buffer_dirty(eb);
787 }
788
789 int wait_on_tree_block_writeback(struct btrfs_root *root,
790                                  struct extent_buffer *eb)
791 {
792         return 0;
793 }
794
795 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
796 {
797         set_extent_buffer_dirty(eb);
798 }
799
800 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
801 {
802         int ret;
803
804         ret = extent_buffer_uptodate(buf);
805         if (!ret)
806                 return ret;
807
808         ret = verify_parent_transid(buf->tree, buf, parent_transid);
809         return !ret;
810 }
811
812 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
813 {
814         return set_extent_buffer_uptodate(eb);
815 }