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