btrfs-progs: Unify the messy error message formats
[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
41         struct btrfs_fs_devices *fs_devices;
42         int ret = 1;
43
44         if (buf->start != btrfs_header_bytenr(buf)) {
45                 printk("Check tree block failed, want=%Lu, have=%Lu\n",
46                        buf->start, btrfs_header_bytenr(buf));
47                 return ret;
48         }
49
50         fs_devices = root->fs_info->fs_devices;
51         while (fs_devices) {
52                 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
53                                           btrfs_header_fsid(),
54                                           BTRFS_FSID_SIZE)) {
55                         ret = 0;
56                         break;
57                 }
58                 fs_devices = fs_devices->seed;
59         }
60         return ret;
61 }
62
63 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
64 {
65         return crc32c(seed, data, len);
66 }
67
68 void btrfs_csum_final(u32 crc, char *result)
69 {
70         *(__le32 *)result = ~cpu_to_le32(crc);
71 }
72
73 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
74                                   int verify, int silent)
75 {
76         char *result;
77         u32 len;
78         u32 crc = ~(u32)0;
79
80         result = malloc(csum_size * sizeof(char));
81         if (!result)
82                 return 1;
83
84         len = buf->len - BTRFS_CSUM_SIZE;
85         crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
86         btrfs_csum_final(crc, result);
87
88         if (verify) {
89                 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
90                         if (!silent)
91                                 printk("checksum verify failed on %llu found %08X wanted %08X\n",
92                                        (unsigned long long)buf->start,
93                                        *((u32 *)result),
94                                        *((u32*)(char *)buf->data));
95                         free(result);
96                         return 1;
97                 }
98         } else {
99                 write_extent_buffer(buf, result, 0, csum_size);
100         }
101         free(result);
102         return 0;
103 }
104
105 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
106 {
107         return __csum_tree_block_size(buf, csum_size, verify, 0);
108 }
109
110 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
111 {
112         return __csum_tree_block_size(buf, csum_size, 1, 1);
113 }
114
115 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
116                            int verify)
117 {
118         u16 csum_size =
119                 btrfs_super_csum_size(root->fs_info->super_copy);
120         return csum_tree_block_size(buf, csum_size, verify);
121 }
122
123 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
124                                             u64 bytenr, u32 blocksize)
125 {
126         return find_extent_buffer(&root->fs_info->extent_cache,
127                                   bytenr, blocksize);
128 }
129
130 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
131                                                  u64 bytenr, u32 blocksize)
132 {
133         return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
134                                    blocksize);
135 }
136
137 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
138                          u64 parent_transid)
139 {
140         int ret;
141         struct extent_buffer *eb;
142         u64 length;
143         struct btrfs_multi_bio *multi = NULL;
144         struct btrfs_device *device;
145
146         eb = btrfs_find_tree_block(root, bytenr, blocksize);
147         if (eb && btrfs_buffer_uptodate(eb, parent_transid)) {
148                 free_extent_buffer(eb);
149                 return 0;
150         }
151
152         length = blocksize;
153         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
154                               bytenr, &length, &multi, 0, NULL);
155         BUG_ON(ret);
156         device = multi->stripes[0].dev;
157         device->total_ios++;
158         blocksize = min(blocksize, (u32)(64 * 1024));
159         readahead(device->fd, multi->stripes[0].physical, blocksize);
160         kfree(multi);
161         return 0;
162 }
163
164 static int verify_parent_transid(struct extent_io_tree *io_tree,
165                                  struct extent_buffer *eb, u64 parent_transid,
166                                  int ignore)
167 {
168         int ret;
169
170         if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
171                 return 0;
172
173         if (extent_buffer_uptodate(eb) &&
174             btrfs_header_generation(eb) == parent_transid) {
175                 ret = 0;
176                 goto out;
177         }
178         printk("parent transid verify failed on %llu wanted %llu found %llu\n",
179                (unsigned long long)eb->start,
180                (unsigned long long)parent_transid,
181                (unsigned long long)btrfs_header_generation(eb));
182         if (ignore) {
183                 eb->flags |= EXTENT_BAD_TRANSID;
184                 printk("Ignoring transid failure\n");
185                 return 0;
186         }
187
188         ret = 1;
189 out:
190         clear_extent_buffer_uptodate(io_tree, eb);
191         return ret;
192
193 }
194
195
196 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
197 {
198         unsigned long offset = 0;
199         struct btrfs_multi_bio *multi = NULL;
200         struct btrfs_device *device;
201         int ret = 0;
202         u64 read_len;
203         unsigned long bytes_left = eb->len;
204
205         while (bytes_left) {
206                 read_len = bytes_left;
207                 device = NULL;
208
209                 if (!info->on_restoring) {
210                         ret = btrfs_map_block(&info->mapping_tree, READ,
211                                               eb->start + offset, &read_len, &multi,
212                                               mirror, NULL);
213                         if (ret) {
214                                 printk("Couldn't map the block %Lu\n", eb->start + offset);
215                                 kfree(multi);
216                                 return -EIO;
217                         }
218                         device = multi->stripes[0].dev;
219
220                         if (device->fd == 0) {
221                                 kfree(multi);
222                                 return -EIO;
223                         }
224
225                         eb->fd = device->fd;
226                         device->total_ios++;
227                         eb->dev_bytenr = multi->stripes[0].physical;
228                         kfree(multi);
229                         multi = NULL;
230                 } else {
231                         /* special case for restore metadump */
232                         list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
233                                 if (device->devid == 1)
234                                         break;
235                         }
236
237                         eb->fd = device->fd;
238                         eb->dev_bytenr = eb->start;
239                         device->total_ios++;
240                 }
241
242                 if (read_len > bytes_left)
243                         read_len = bytes_left;
244
245                 ret = read_extent_from_disk(eb, offset, read_len);
246                 if (ret)
247                         return -EIO;
248                 offset += read_len;
249                 bytes_left -= read_len;
250         }
251         return 0;
252 }
253
254 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
255                                      u32 blocksize, u64 parent_transid)
256 {
257         int ret;
258         struct extent_buffer *eb;
259         u64 best_transid = 0;
260         int mirror_num = 0;
261         int good_mirror = 0;
262         int num_copies;
263         int ignore = 0;
264
265         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
266         if (!eb)
267                 return NULL;
268
269         if (btrfs_buffer_uptodate(eb, parent_transid))
270                 return eb;
271
272         while (1) {
273                 ret = read_whole_eb(root->fs_info, eb, mirror_num);
274                 if (ret == 0 && check_tree_block(root, eb) == 0 &&
275                     csum_tree_block(root, eb, 1) == 0 &&
276                     verify_parent_transid(eb->tree, eb, parent_transid, ignore)
277                     == 0) {
278                         if (eb->flags & EXTENT_BAD_TRANSID &&
279                             list_empty(&eb->recow)) {
280                                 list_add_tail(&eb->recow,
281                                               &root->fs_info->recow_ebs);
282                                 eb->refs++;
283                         }
284                         btrfs_set_buffer_uptodate(eb);
285                         return eb;
286                 }
287                 if (ignore) {
288                         if (check_tree_block(root, eb))
289                                 printk("read block failed check_tree_block\n");
290                         else
291                                 printk("Csum didn't match\n");
292                         break;
293                 }
294                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
295                                               eb->start, eb->len);
296                 if (num_copies == 1) {
297                         ignore = 1;
298                         continue;
299                 }
300                 if (btrfs_header_generation(eb) > best_transid && mirror_num) {
301                         best_transid = btrfs_header_generation(eb);
302                         good_mirror = mirror_num;
303                 }
304                 mirror_num++;
305                 if (mirror_num > num_copies) {
306                         mirror_num = good_mirror;
307                         ignore = 1;
308                         continue;
309                 }
310         }
311         free_extent_buffer(eb);
312         return NULL;
313 }
314
315 int write_and_map_eb(struct btrfs_trans_handle *trans,
316                      struct btrfs_root *root,
317                      struct extent_buffer *eb)
318 {
319         int ret;
320         int dev_nr;
321         u64 length;
322         u64 *raid_map = NULL;
323         struct btrfs_multi_bio *multi = NULL;
324
325         dev_nr = 0;
326         length = eb->len;
327         ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
328                               eb->start, &length, &multi, 0, &raid_map);
329
330         if (raid_map) {
331                 ret = write_raid56_with_parity(root->fs_info, eb, multi,
332                                                length, raid_map);
333                 BUG_ON(ret);
334         } else while (dev_nr < multi->num_stripes) {
335                 BUG_ON(ret);
336                 eb->fd = multi->stripes[dev_nr].dev->fd;
337                 eb->dev_bytenr = multi->stripes[dev_nr].physical;
338                 multi->stripes[dev_nr].dev->total_ios++;
339                 dev_nr++;
340                 ret = write_extent_to_disk(eb);
341                 BUG_ON(ret);
342         }
343         kfree(multi);
344         return 0;
345 }
346
347 static int write_tree_block(struct btrfs_trans_handle *trans,
348                      struct btrfs_root *root,
349                      struct extent_buffer *eb)
350 {
351         if (check_tree_block(root, eb))
352                 BUG();
353
354         if (!btrfs_buffer_uptodate(eb, trans->transid))
355                 BUG();
356
357         btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
358         csum_tree_block(root, eb, 0);
359
360         return write_and_map_eb(trans, root, eb);
361 }
362
363 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
364                         u32 stripesize, struct btrfs_root *root,
365                         struct btrfs_fs_info *fs_info, u64 objectid)
366 {
367         root->node = NULL;
368         root->commit_root = NULL;
369         root->sectorsize = sectorsize;
370         root->nodesize = nodesize;
371         root->leafsize = leafsize;
372         root->stripesize = stripesize;
373         root->ref_cows = 0;
374         root->track_dirty = 0;
375
376         root->fs_info = fs_info;
377         root->objectid = objectid;
378         root->last_trans = 0;
379         root->highest_inode = 0;
380         root->last_inode_alloc = 0;
381
382         INIT_LIST_HEAD(&root->dirty_list);
383         memset(&root->root_key, 0, sizeof(root->root_key));
384         memset(&root->root_item, 0, sizeof(root->root_item));
385         root->root_key.objectid = objectid;
386         return 0;
387 }
388
389 static int update_cowonly_root(struct btrfs_trans_handle *trans,
390                                struct btrfs_root *root)
391 {
392         int ret;
393         u64 old_root_bytenr;
394         struct btrfs_root *tree_root = root->fs_info->tree_root;
395
396         btrfs_write_dirty_block_groups(trans, root);
397         while(1) {
398                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
399                 if (old_root_bytenr == root->node->start)
400                         break;
401                 btrfs_set_root_bytenr(&root->root_item,
402                                        root->node->start);
403                 btrfs_set_root_generation(&root->root_item,
404                                           trans->transid);
405                 root->root_item.level = btrfs_header_level(root->node);
406                 ret = btrfs_update_root(trans, tree_root,
407                                         &root->root_key,
408                                         &root->root_item);
409                 BUG_ON(ret);
410                 btrfs_write_dirty_block_groups(trans, root);
411         }
412         return 0;
413 }
414
415 static int commit_tree_roots(struct btrfs_trans_handle *trans,
416                              struct btrfs_fs_info *fs_info)
417 {
418         struct btrfs_root *root;
419         struct list_head *next;
420         struct extent_buffer *eb;
421         int ret;
422
423         if (fs_info->readonly)
424                 return 0;
425
426         eb = fs_info->tree_root->node;
427         extent_buffer_get(eb);
428         ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
429         free_extent_buffer(eb);
430         if (ret)
431                 return ret;
432
433         while(!list_empty(&fs_info->dirty_cowonly_roots)) {
434                 next = fs_info->dirty_cowonly_roots.next;
435                 list_del_init(next);
436                 root = list_entry(next, struct btrfs_root, dirty_list);
437                 update_cowonly_root(trans, root);
438                 free_extent_buffer(root->commit_root);
439                 root->commit_root = NULL;
440         }
441
442         return 0;
443 }
444
445 static int __commit_transaction(struct btrfs_trans_handle *trans,
446                                 struct btrfs_root *root)
447 {
448         u64 start;
449         u64 end;
450         struct extent_buffer *eb;
451         struct extent_io_tree *tree = &root->fs_info->extent_cache;
452         int ret;
453
454         while(1) {
455                 ret = find_first_extent_bit(tree, 0, &start, &end,
456                                             EXTENT_DIRTY);
457                 if (ret)
458                         break;
459                 while(start <= end) {
460                         eb = find_first_extent_buffer(tree, start);
461                         BUG_ON(!eb || eb->start != start);
462                         ret = write_tree_block(trans, root, eb);
463                         BUG_ON(ret);
464                         start += eb->len;
465                         clear_extent_buffer_dirty(eb);
466                         free_extent_buffer(eb);
467                 }
468         }
469         return 0;
470 }
471
472 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
473                              struct btrfs_root *root)
474 {
475         u64 transid = trans->transid;
476         int ret = 0;
477         struct btrfs_fs_info *fs_info = root->fs_info;
478
479         if (root->commit_root == root->node)
480                 goto commit_tree;
481
482         free_extent_buffer(root->commit_root);
483         root->commit_root = NULL;
484
485         btrfs_set_root_bytenr(&root->root_item, root->node->start);
486         btrfs_set_root_generation(&root->root_item, trans->transid);
487         root->root_item.level = btrfs_header_level(root->node);
488         ret = btrfs_update_root(trans, root->fs_info->tree_root,
489                                 &root->root_key, &root->root_item);
490         BUG_ON(ret);
491 commit_tree:
492         ret = commit_tree_roots(trans, fs_info);
493         BUG_ON(ret);
494         ret = __commit_transaction(trans, root);
495         BUG_ON(ret);
496         write_ctree_super(trans, root);
497         btrfs_finish_extent_commit(trans, fs_info->extent_root,
498                                    &fs_info->pinned_extents);
499         btrfs_free_transaction(root, trans);
500         free_extent_buffer(root->commit_root);
501         root->commit_root = NULL;
502         fs_info->running_transaction = NULL;
503         fs_info->last_trans_committed = transid;
504         return 0;
505 }
506
507 static int find_and_setup_root(struct btrfs_root *tree_root,
508                                struct btrfs_fs_info *fs_info,
509                                u64 objectid, struct btrfs_root *root)
510 {
511         int ret;
512         u32 blocksize;
513         u64 generation;
514
515         __setup_root(tree_root->nodesize, tree_root->leafsize,
516                      tree_root->sectorsize, tree_root->stripesize,
517                      root, fs_info, objectid);
518         ret = btrfs_find_last_root(tree_root, objectid,
519                                    &root->root_item, &root->root_key);
520         if (ret)
521                 return ret;
522
523         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
524         generation = btrfs_root_generation(&root->root_item);
525         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
526                                      blocksize, generation);
527         if (!extent_buffer_uptodate(root->node))
528                 return -EIO;
529
530         return 0;
531 }
532
533 static int find_and_setup_log_root(struct btrfs_root *tree_root,
534                                struct btrfs_fs_info *fs_info,
535                                struct btrfs_super_block *disk_super)
536 {
537         u32 blocksize;
538         u64 blocknr = btrfs_super_log_root(disk_super);
539         struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
540
541         if (!log_root)
542                 return -ENOMEM;
543
544         if (blocknr == 0) {
545                 free(log_root);
546                 return 0;
547         }
548
549         blocksize = btrfs_level_size(tree_root,
550                              btrfs_super_log_root_level(disk_super));
551
552         __setup_root(tree_root->nodesize, tree_root->leafsize,
553                      tree_root->sectorsize, tree_root->stripesize,
554                      log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
555
556         log_root->node = read_tree_block(tree_root, blocknr,
557                                      blocksize,
558                                      btrfs_super_generation(disk_super) + 1);
559
560         fs_info->log_root_tree = log_root;
561
562         if (!extent_buffer_uptodate(log_root->node)) {
563                 free_extent_buffer(log_root->node);
564                 free(log_root);
565                 fs_info->log_root_tree = NULL;
566                 return -EIO;
567         }
568
569         return 0;
570 }
571
572 int btrfs_free_fs_root(struct btrfs_root *root)
573 {
574         if (root->node)
575                 free_extent_buffer(root->node);
576         if (root->commit_root)
577                 free_extent_buffer(root->commit_root);
578         kfree(root);
579         return 0;
580 }
581
582 static void __free_fs_root(struct rb_node *node)
583 {
584         struct btrfs_root *root;
585
586         root = container_of(node, struct btrfs_root, rb_node);
587         btrfs_free_fs_root(root);
588 }
589
590 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
591
592 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
593                                                struct btrfs_key *location)
594 {
595         struct btrfs_root *root;
596         struct btrfs_root *tree_root = fs_info->tree_root;
597         struct btrfs_path *path;
598         struct extent_buffer *l;
599         u64 generation;
600         u32 blocksize;
601         int ret = 0;
602
603         root = malloc(sizeof(*root));
604         if (!root)
605                 return ERR_PTR(-ENOMEM);
606         memset(root, 0, sizeof(*root));
607         if (location->offset == (u64)-1) {
608                 ret = find_and_setup_root(tree_root, fs_info,
609                                           location->objectid, root);
610                 if (ret) {
611                         free(root);
612                         return ERR_PTR(ret);
613                 }
614                 goto insert;
615         }
616
617         __setup_root(tree_root->nodesize, tree_root->leafsize,
618                      tree_root->sectorsize, tree_root->stripesize,
619                      root, fs_info, location->objectid);
620
621         path = btrfs_alloc_path();
622         BUG_ON(!path);
623         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
624         if (ret != 0) {
625                 if (ret > 0)
626                         ret = -ENOENT;
627                 goto out;
628         }
629         l = path->nodes[0];
630         read_extent_buffer(l, &root->root_item,
631                btrfs_item_ptr_offset(l, path->slots[0]),
632                sizeof(root->root_item));
633         memcpy(&root->root_key, location, sizeof(*location));
634         ret = 0;
635 out:
636         btrfs_release_path(path);
637         btrfs_free_path(path);
638         if (ret) {
639                 free(root);
640                 return ERR_PTR(ret);
641         }
642         generation = btrfs_root_generation(&root->root_item);
643         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
644         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
645                                      blocksize, generation);
646         if (!root->node) {
647                 free(root);
648                 return ERR_PTR(-EIO);
649         }
650 insert:
651         root->ref_cows = 1;
652         return root;
653 }
654
655 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
656                                             void *data)
657 {
658         u64 objectid = *((u64 *)data);
659         struct btrfs_root *root;
660
661         root = rb_entry(node, struct btrfs_root, rb_node);
662         if (objectid > root->objectid)
663                 return 1;
664         else if (objectid < root->objectid)
665                 return -1;
666         else
667                 return 0;
668 }
669
670 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
671                                         struct rb_node *node2)
672 {
673         struct btrfs_root *root;
674
675         root = rb_entry(node2, struct btrfs_root, rb_node);
676         return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
677 }
678
679 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
680                                       struct btrfs_key *location)
681 {
682         struct btrfs_root *root;
683         struct rb_node *node;
684         int ret;
685         u64 objectid = location->objectid;
686
687         if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
688                 return fs_info->tree_root;
689         if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
690                 return fs_info->extent_root;
691         if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
692                 return fs_info->chunk_root;
693         if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
694                 return fs_info->dev_root;
695         if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
696                 return fs_info->csum_root;
697         if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
698                 return fs_info->csum_root;
699
700         BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
701                location->offset != (u64)-1);
702
703         node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
704                          btrfs_fs_roots_compare_objectids, NULL);
705         if (node)
706                 return container_of(node, struct btrfs_root, rb_node);
707
708         root = btrfs_read_fs_root_no_cache(fs_info, location);
709         if (IS_ERR(root))
710                 return root;
711
712         ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
713                         btrfs_fs_roots_compare_roots);
714         BUG_ON(ret);
715         return root;
716 }
717
718 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
719 {
720         free(fs_info->tree_root);
721         free(fs_info->extent_root);
722         free(fs_info->chunk_root);
723         free(fs_info->dev_root);
724         free(fs_info->csum_root);
725         free(fs_info->quota_root);
726         free(fs_info->super_copy);
727         free(fs_info->log_root_tree);
728         free(fs_info);
729 }
730
731 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
732 {
733         struct btrfs_fs_info *fs_info;
734
735         fs_info = malloc(sizeof(struct btrfs_fs_info));
736         if (!fs_info)
737                 return NULL;
738
739         memset(fs_info, 0, sizeof(struct btrfs_fs_info));
740
741         fs_info->tree_root = malloc(sizeof(struct btrfs_root));
742         fs_info->extent_root = malloc(sizeof(struct btrfs_root));
743         fs_info->chunk_root = malloc(sizeof(struct btrfs_root));
744         fs_info->dev_root = malloc(sizeof(struct btrfs_root));
745         fs_info->csum_root = malloc(sizeof(struct btrfs_root));
746         fs_info->quota_root = malloc(sizeof(struct btrfs_root));
747         fs_info->super_copy = malloc(BTRFS_SUPER_INFO_SIZE);
748
749         if (!fs_info->tree_root || !fs_info->extent_root ||
750             !fs_info->chunk_root || !fs_info->dev_root ||
751             !fs_info->csum_root || !fs_info->quota_root ||
752             !fs_info->super_copy)
753                 goto free_all;
754
755         memset(fs_info->super_copy, 0, BTRFS_SUPER_INFO_SIZE);
756         memset(fs_info->tree_root, 0, sizeof(struct btrfs_root));
757         memset(fs_info->extent_root, 0, sizeof(struct btrfs_root));
758         memset(fs_info->chunk_root, 0, sizeof(struct btrfs_root));
759         memset(fs_info->dev_root, 0, sizeof(struct btrfs_root));
760         memset(fs_info->csum_root, 0, sizeof(struct btrfs_root));
761         memset(fs_info->quota_root, 0, sizeof(struct btrfs_root));
762
763         extent_io_tree_init(&fs_info->extent_cache);
764         extent_io_tree_init(&fs_info->free_space_cache);
765         extent_io_tree_init(&fs_info->block_group_cache);
766         extent_io_tree_init(&fs_info->pinned_extents);
767         extent_io_tree_init(&fs_info->pending_del);
768         extent_io_tree_init(&fs_info->extent_ins);
769         fs_info->fs_root_tree = RB_ROOT;
770         cache_tree_init(&fs_info->mapping_tree.cache_tree);
771
772         mutex_init(&fs_info->fs_mutex);
773         INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
774         INIT_LIST_HEAD(&fs_info->space_info);
775         INIT_LIST_HEAD(&fs_info->recow_ebs);
776
777         if (!writable)
778                 fs_info->readonly = 1;
779
780         fs_info->super_bytenr = sb_bytenr;
781         fs_info->data_alloc_profile = (u64)-1;
782         fs_info->metadata_alloc_profile = (u64)-1;
783         fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
784         return fs_info;
785 free_all:
786         btrfs_free_fs_info(fs_info);
787         return NULL;
788 }
789
790 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
791 {
792         u64 features;
793
794         features = btrfs_super_incompat_flags(sb) &
795                    ~BTRFS_FEATURE_INCOMPAT_SUPP;
796         if (features) {
797                 printk("couldn't open because of unsupported "
798                        "option features (%Lx).\n",
799                        (unsigned long long)features);
800                 return -ENOTSUP;
801         }
802
803         features = btrfs_super_incompat_flags(sb);
804         if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
805                 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
806                 btrfs_set_super_incompat_flags(sb, features);
807         }
808
809         features = btrfs_super_compat_ro_flags(sb) &
810                 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
811         if (writable && features) {
812                 printk("couldn't open RDWR because of unsupported "
813                        "option features (%Lx).\n",
814                        (unsigned long long)features);
815                 return -ENOTSUP;
816         }
817         return 0;
818 }
819
820 static int find_best_backup_root(struct btrfs_super_block *super)
821 {
822         struct btrfs_root_backup *backup;
823         u64 orig_gen = btrfs_super_generation(super);
824         u64 gen = 0;
825         int best_index = 0;
826         int i;
827
828         for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
829                 backup = super->super_roots + i;
830                 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
831                     btrfs_backup_tree_root_gen(backup) > gen) {
832                         best_index = i;
833                         gen = btrfs_backup_tree_root_gen(backup);
834                 }
835         }
836         return best_index;
837 }
838
839 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
840                           enum btrfs_open_ctree_flags flags)
841 {
842         struct btrfs_super_block *sb = fs_info->super_copy;
843         struct btrfs_root *root;
844         struct btrfs_key key;
845         u32 sectorsize;
846         u32 nodesize;
847         u32 leafsize;
848         u32 stripesize;
849         u64 generation;
850         u32 blocksize;
851         int ret;
852
853         nodesize = btrfs_super_nodesize(sb);
854         leafsize = btrfs_super_leafsize(sb);
855         sectorsize = btrfs_super_sectorsize(sb);
856         stripesize = btrfs_super_stripesize(sb);
857
858         root = fs_info->tree_root;
859         __setup_root(nodesize, leafsize, sectorsize, stripesize,
860                      root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
861         blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
862         generation = btrfs_super_generation(sb);
863
864         if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
865                 root_tree_bytenr = btrfs_super_root(sb);
866         } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
867                 struct btrfs_root_backup *backup;
868                 int index = find_best_backup_root(sb);
869                 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
870                         fprintf(stderr, "Invalid backup root number\n");
871                         return -EIO;
872                 }
873                 backup = fs_info->super_copy->super_roots + index;
874                 root_tree_bytenr = btrfs_backup_tree_root(backup);
875                 generation = btrfs_backup_tree_root_gen(backup);
876         }
877
878         root->node = read_tree_block(root, root_tree_bytenr, blocksize,
879                                      generation);
880         if (!extent_buffer_uptodate(root->node)) {
881                 fprintf(stderr, "Couldn't read tree root\n");
882                 return -EIO;
883         }
884
885         ret = find_and_setup_root(root, fs_info, BTRFS_EXTENT_TREE_OBJECTID,
886                                   fs_info->extent_root);
887         if (ret) {
888                 printk("Couldn't setup extent tree\n");
889                 if (!(flags & OPEN_CTREE_PARTIAL))
890                         return -EIO;
891                 /* Need a blank node here just so we don't screw up in the
892                  * million of places that assume a root has a valid ->node
893                  */
894                 fs_info->extent_root->node =
895                         btrfs_find_create_tree_block(fs_info->extent_root, 0,
896                                                      leafsize);
897                 if (!fs_info->extent_root->node)
898                         return -ENOMEM;
899                 clear_extent_buffer_uptodate(NULL, fs_info->extent_root->node);
900         }
901         fs_info->extent_root->track_dirty = 1;
902
903         ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
904                                   fs_info->dev_root);
905         if (ret) {
906                 printk("Couldn't setup device tree\n");
907                 return -EIO;
908         }
909         fs_info->dev_root->track_dirty = 1;
910
911         ret = find_and_setup_root(root, fs_info, BTRFS_CSUM_TREE_OBJECTID,
912                                   fs_info->csum_root);
913         if (ret) {
914                 printk("Couldn't setup csum tree\n");
915                 if (!(flags & OPEN_CTREE_PARTIAL))
916                         return -EIO;
917         }
918         fs_info->csum_root->track_dirty = 1;
919
920         ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
921                                   fs_info->quota_root);
922         if (ret == 0)
923                 fs_info->quota_enabled = 1;
924
925         ret = find_and_setup_log_root(root, fs_info, sb);
926         if (ret) {
927                 printk("Couldn't setup log root tree\n");
928                 return -EIO;
929         }
930
931         fs_info->generation = generation;
932         fs_info->last_trans_committed = generation;
933         if (extent_buffer_uptodate(fs_info->extent_root->node) &&
934             !(flags & OPEN_CTREE_NO_BLOCK_GROUPS))
935                 btrfs_read_block_groups(fs_info->tree_root);
936
937         key.objectid = BTRFS_FS_TREE_OBJECTID;
938         key.type = BTRFS_ROOT_ITEM_KEY;
939         key.offset = (u64)-1;
940         fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
941
942         if (IS_ERR(fs_info->fs_root))
943                 return -EIO;
944         return 0;
945 }
946
947 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
948 {
949         if (fs_info->quota_root)
950                 free_extent_buffer(fs_info->quota_root->node);
951         if (fs_info->csum_root)
952                 free_extent_buffer(fs_info->csum_root->node);
953         if (fs_info->dev_root)
954                 free_extent_buffer(fs_info->dev_root->node);
955         if (fs_info->extent_root)
956                 free_extent_buffer(fs_info->extent_root->node);
957         if (fs_info->tree_root)
958                 free_extent_buffer(fs_info->tree_root->node);
959         if (fs_info->log_root_tree)
960                 free_extent_buffer(fs_info->log_root_tree->node);
961         if (fs_info->chunk_root)
962                 free_extent_buffer(fs_info->chunk_root->node);
963 }
964
965 static void free_map_lookup(struct cache_extent *ce)
966 {
967         struct map_lookup *map;
968
969         map = container_of(ce, struct map_lookup, ce);
970         kfree(map);
971 }
972
973 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
974
975 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
976 {
977         while (!list_empty(&fs_info->recow_ebs)) {
978                 struct extent_buffer *eb;
979                 eb = list_first_entry(&fs_info->recow_ebs,
980                                       struct extent_buffer, recow);
981                 list_del_init(&eb->recow);
982                 free_extent_buffer(eb);
983         }
984         free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
985         extent_io_tree_cleanup(&fs_info->extent_cache);
986         extent_io_tree_cleanup(&fs_info->free_space_cache);
987         extent_io_tree_cleanup(&fs_info->block_group_cache);
988         extent_io_tree_cleanup(&fs_info->pinned_extents);
989         extent_io_tree_cleanup(&fs_info->pending_del);
990         extent_io_tree_cleanup(&fs_info->extent_ins);
991 }
992
993 int btrfs_scan_fs_devices(int fd, const char *path,
994                           struct btrfs_fs_devices **fs_devices,
995                           u64 sb_bytenr, int run_ioctl, int super_recover)
996 {
997         u64 total_devs;
998         int ret;
999         if (!sb_bytenr)
1000                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1001
1002         ret = btrfs_scan_one_device(fd, path, fs_devices,
1003                                     &total_devs, sb_bytenr, super_recover);
1004         if (ret) {
1005                 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1006                 return ret;
1007         }
1008
1009         if (total_devs != 1) {
1010                 ret = btrfs_scan_for_fsid(run_ioctl);
1011                 if (ret)
1012                         return ret;
1013         }
1014         return 0;
1015 }
1016
1017 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
1018 {
1019         struct btrfs_super_block *sb = fs_info->super_copy;
1020         u32 sectorsize;
1021         u32 nodesize;
1022         u32 leafsize;
1023         u32 blocksize;
1024         u32 stripesize;
1025         u64 generation;
1026         int ret;
1027
1028         nodesize = btrfs_super_nodesize(sb);
1029         leafsize = btrfs_super_leafsize(sb);
1030         sectorsize = btrfs_super_sectorsize(sb);
1031         stripesize = btrfs_super_stripesize(sb);
1032
1033         __setup_root(nodesize, leafsize, sectorsize, stripesize,
1034                      fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1035
1036         ret = btrfs_read_sys_array(fs_info->chunk_root);
1037         if (ret)
1038                 return ret;
1039
1040         blocksize = btrfs_level_size(fs_info->chunk_root,
1041                                      btrfs_super_chunk_root_level(sb));
1042         generation = btrfs_super_chunk_root_generation(sb);
1043
1044         fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
1045                                                     btrfs_super_chunk_root(sb),
1046                                                     blocksize, generation);
1047         if (!fs_info->chunk_root->node ||
1048             !extent_buffer_uptodate(fs_info->chunk_root->node)) {
1049                 fprintf(stderr, "Couldn't read chunk root\n");
1050                 return -EIO;
1051         }
1052
1053         if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1054                 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
1055                 if (ret) {
1056                         fprintf(stderr, "Couldn't read chunk tree\n");
1057                         return ret;
1058                 }
1059         }
1060         return 0;
1061 }
1062
1063 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1064                                              u64 sb_bytenr,
1065                                              u64 root_tree_bytenr,
1066                                              enum btrfs_open_ctree_flags flags)
1067 {
1068         struct btrfs_fs_info *fs_info;
1069         struct btrfs_super_block *disk_super;
1070         struct btrfs_fs_devices *fs_devices = NULL;
1071         struct extent_buffer *eb;
1072         int ret;
1073         int oflags;
1074
1075         if (sb_bytenr == 0)
1076                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1077
1078         /* try to drop all the caches */
1079         if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1080                 fprintf(stderr, "Warning, could not drop caches\n");
1081
1082         fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1083         if (!fs_info) {
1084                 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1085                 return NULL;
1086         }
1087         if (flags & OPEN_CTREE_RESTORE)
1088                 fs_info->on_restoring = 1;
1089
1090         ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
1091                                     !(flags & OPEN_CTREE_RECOVER_SUPER),
1092                                     (flags & OPEN_CTREE_RECOVER_SUPER));
1093         if (ret)
1094                 goto out;
1095
1096         fs_info->fs_devices = fs_devices;
1097         if (flags & OPEN_CTREE_WRITES)
1098                 oflags = O_RDWR;
1099         else
1100                 oflags = O_RDONLY;
1101
1102         if (flags & OPEN_CTREE_EXCLUSIVE)
1103                 oflags |= O_EXCL;
1104
1105         ret = btrfs_open_devices(fs_devices, oflags);
1106         if (ret)
1107                 goto out;
1108
1109         disk_super = fs_info->super_copy;
1110         if (!(flags & OPEN_CTREE_RECOVER_SUPER))
1111                 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1112                                            disk_super, sb_bytenr, 1);
1113         else
1114                 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr, 0);
1115         if (ret) {
1116                 printk("No valid btrfs found\n");
1117                 goto out_devices;
1118         }
1119
1120         memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1121
1122         ret = btrfs_check_fs_compatibility(fs_info->super_copy,
1123                                            flags & OPEN_CTREE_WRITES);
1124         if (ret)
1125                 goto out_devices;
1126
1127         ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1128         if (ret)
1129                 goto out_chunk;
1130
1131         eb = fs_info->chunk_root->node;
1132         read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1133                            btrfs_header_chunk_tree_uuid(eb),
1134                            BTRFS_UUID_SIZE);
1135
1136         ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1137         if (ret)
1138                 goto out_failed;
1139
1140         return fs_info;
1141
1142 out_failed:
1143         if (flags & OPEN_CTREE_PARTIAL)
1144                 return fs_info;
1145 out_chunk:
1146         btrfs_release_all_roots(fs_info);
1147         btrfs_cleanup_all_caches(fs_info);
1148 out_devices:
1149         btrfs_close_devices(fs_devices);
1150 out:
1151         btrfs_free_fs_info(fs_info);
1152         return NULL;
1153 }
1154
1155 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1156                                          u64 sb_bytenr, u64 root_tree_bytenr,
1157                                          enum btrfs_open_ctree_flags flags)
1158 {
1159         int fp;
1160         struct btrfs_fs_info *info;
1161         int oflags = O_CREAT | O_RDWR;
1162
1163         if (!(flags & OPEN_CTREE_WRITES))
1164                 oflags = O_RDONLY;
1165
1166         fp = open(filename, oflags, 0600);
1167         if (fp < 0) {
1168                 fprintf (stderr, "Could not open %s\n", filename);
1169                 return NULL;
1170         }
1171         info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1172                                flags);
1173         close(fp);
1174         return info;
1175 }
1176
1177 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1178                               enum btrfs_open_ctree_flags flags)
1179 {
1180         struct btrfs_fs_info *info;
1181
1182         info = open_ctree_fs_info(filename, sb_bytenr, 0, flags);
1183         if (!info)
1184                 return NULL;
1185         return info->fs_root;
1186 }
1187
1188 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1189                                  enum btrfs_open_ctree_flags flags)
1190 {
1191         struct btrfs_fs_info *info;
1192         info = __open_ctree_fd(fp, path, sb_bytenr, 0, flags);
1193         if (!info)
1194                 return NULL;
1195         return info->fs_root;
1196 }
1197
1198 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1199                          int super_recover)
1200 {
1201         u8 fsid[BTRFS_FSID_SIZE];
1202         int fsid_is_initialized = 0;
1203         struct btrfs_super_block buf;
1204         int i;
1205         int ret;
1206         int max_super = super_recover ? BTRFS_SUPER_MIRROR_MAX : 1;
1207         u64 transid = 0;
1208         u64 bytenr;
1209
1210         if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1211                 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1212                 if (ret < sizeof(buf))
1213                         return -1;
1214
1215                 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1216                     btrfs_super_magic(&buf) != BTRFS_MAGIC)
1217                         return -1;
1218
1219                 memcpy(sb, &buf, sizeof(*sb));
1220                 return 0;
1221         }
1222
1223         /*
1224         * we would like to check all the supers, but that would make
1225         * a btrfs mount succeed after a mkfs from a different FS.
1226         * So, we need to add a special mount option to scan for
1227         * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1228         */
1229
1230         for (i = 0; i < max_super; i++) {
1231                 bytenr = btrfs_sb_offset(i);
1232                 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1233                 if (ret < sizeof(buf))
1234                         break;
1235
1236                 if (btrfs_super_bytenr(&buf) != bytenr )
1237                         continue;
1238                 /* if magic is NULL, the device was removed */
1239                 if (btrfs_super_magic(&buf) == 0 && i == 0)
1240                         return -1;
1241                 if (btrfs_super_magic(&buf) != BTRFS_MAGIC)
1242                         continue;
1243
1244                 if (!fsid_is_initialized) {
1245                         memcpy(fsid, buf.fsid, sizeof(fsid));
1246                         fsid_is_initialized = 1;
1247                 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1248                         /*
1249                          * the superblocks (the original one and
1250                          * its backups) contain data of different
1251                          * filesystems -> the super cannot be trusted
1252                          */
1253                         continue;
1254                 }
1255
1256                 if (btrfs_super_generation(&buf) > transid) {
1257                         memcpy(sb, &buf, sizeof(*sb));
1258                         transid = btrfs_super_generation(&buf);
1259                 }
1260         }
1261
1262         return transid > 0 ? 0 : -1;
1263 }
1264
1265 static int write_dev_supers(struct btrfs_root *root,
1266                             struct btrfs_super_block *sb,
1267                             struct btrfs_device *device)
1268 {
1269         u64 bytenr;
1270         u32 crc;
1271         int i, ret;
1272
1273         if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1274                 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1275                 crc = ~(u32)0;
1276                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1277                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1278                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1279
1280                 /*
1281                  * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1282                  * zero filled, we can use it directly
1283                  */
1284                 ret = pwrite64(device->fd, root->fs_info->super_copy,
1285                                 BTRFS_SUPER_INFO_SIZE,
1286                                 root->fs_info->super_bytenr);
1287                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1288                 return 0;
1289         }
1290
1291         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1292                 bytenr = btrfs_sb_offset(i);
1293                 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1294                         break;
1295
1296                 btrfs_set_super_bytenr(sb, bytenr);
1297
1298                 crc = ~(u32)0;
1299                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1300                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1301                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1302
1303                 /*
1304                  * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1305                  * zero filled, we can use it directly
1306                  */
1307                 ret = pwrite64(device->fd, root->fs_info->super_copy,
1308                                 BTRFS_SUPER_INFO_SIZE, bytenr);
1309                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1310         }
1311
1312         return 0;
1313 }
1314
1315 int write_all_supers(struct btrfs_root *root)
1316 {
1317         struct list_head *cur;
1318         struct list_head *head = &root->fs_info->fs_devices->devices;
1319         struct btrfs_device *dev;
1320         struct btrfs_super_block *sb;
1321         struct btrfs_dev_item *dev_item;
1322         int ret;
1323         u64 flags;
1324
1325         sb = root->fs_info->super_copy;
1326         dev_item = &sb->dev_item;
1327         list_for_each(cur, head) {
1328                 dev = list_entry(cur, struct btrfs_device, dev_list);
1329                 if (!dev->writeable)
1330                         continue;
1331
1332                 btrfs_set_stack_device_generation(dev_item, 0);
1333                 btrfs_set_stack_device_type(dev_item, dev->type);
1334                 btrfs_set_stack_device_id(dev_item, dev->devid);
1335                 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1336                 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1337                 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1338                 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1339                 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1340                 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1341                 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1342
1343                 flags = btrfs_super_flags(sb);
1344                 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1345
1346                 ret = write_dev_supers(root, sb, dev);
1347                 BUG_ON(ret);
1348         }
1349         return 0;
1350 }
1351
1352 int write_ctree_super(struct btrfs_trans_handle *trans,
1353                       struct btrfs_root *root)
1354 {
1355         int ret;
1356         struct btrfs_root *tree_root = root->fs_info->tree_root;
1357         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1358
1359         if (root->fs_info->readonly)
1360                 return 0;
1361
1362         btrfs_set_super_generation(root->fs_info->super_copy,
1363                                    trans->transid);
1364         btrfs_set_super_root(root->fs_info->super_copy,
1365                              tree_root->node->start);
1366         btrfs_set_super_root_level(root->fs_info->super_copy,
1367                                    btrfs_header_level(tree_root->node));
1368         btrfs_set_super_chunk_root(root->fs_info->super_copy,
1369                                    chunk_root->node->start);
1370         btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1371                                          btrfs_header_level(chunk_root->node));
1372         btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1373                                 btrfs_header_generation(chunk_root->node));
1374
1375         ret = write_all_supers(root);
1376         if (ret)
1377                 fprintf(stderr, "failed to write new super block err %d\n", ret);
1378         return ret;
1379 }
1380
1381 int close_ctree(struct btrfs_root *root)
1382 {
1383         int ret;
1384         struct btrfs_trans_handle *trans;
1385         struct btrfs_fs_info *fs_info = root->fs_info;
1386
1387         if (fs_info->last_trans_committed !=
1388             fs_info->generation) {
1389                 trans = btrfs_start_transaction(root, 1);
1390                 btrfs_commit_transaction(trans, root);
1391                 trans = btrfs_start_transaction(root, 1);
1392                 ret = commit_tree_roots(trans, fs_info);
1393                 BUG_ON(ret);
1394                 ret = __commit_transaction(trans, root);
1395                 BUG_ON(ret);
1396                 write_ctree_super(trans, root);
1397                 btrfs_free_transaction(root, trans);
1398         }
1399         btrfs_free_block_groups(fs_info);
1400
1401         free_fs_roots_tree(&fs_info->fs_root_tree);
1402
1403         btrfs_release_all_roots(fs_info);
1404         btrfs_close_devices(fs_info->fs_devices);
1405         btrfs_cleanup_all_caches(fs_info);
1406         btrfs_free_fs_info(fs_info);
1407         return 0;
1408 }
1409
1410 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1411                      struct extent_buffer *eb)
1412 {
1413         return clear_extent_buffer_dirty(eb);
1414 }
1415
1416 int wait_on_tree_block_writeback(struct btrfs_root *root,
1417                                  struct extent_buffer *eb)
1418 {
1419         return 0;
1420 }
1421
1422 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1423 {
1424         set_extent_buffer_dirty(eb);
1425 }
1426
1427 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1428 {
1429         int ret;
1430
1431         ret = extent_buffer_uptodate(buf);
1432         if (!ret)
1433                 return ret;
1434
1435         ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1436         return !ret;
1437 }
1438
1439 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1440 {
1441         return set_extent_buffer_uptodate(eb);
1442 }