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