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