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