eff49dac2dc77b50f46a995d5e76583fda5a0fff
[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 close_all_devices(struct btrfs_fs_info *fs_info);
39
40 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
41 {
42
43         struct btrfs_fs_devices *fs_devices;
44         int ret = 1;
45
46         if (buf->start != btrfs_header_bytenr(buf)) {
47                 printk("Check tree block failed, want=%Lu, have=%Lu\n",
48                        buf->start, btrfs_header_bytenr(buf));
49                 return ret;
50         }
51
52         fs_devices = root->fs_info->fs_devices;
53         while (fs_devices) {
54                 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
55                                           (unsigned long)btrfs_header_fsid(buf),
56                                           BTRFS_FSID_SIZE)) {
57                         ret = 0;
58                         break;
59                 }
60                 fs_devices = fs_devices->seed;
61         }
62         return ret;
63 }
64
65 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
66 {
67         return crc32c(seed, data, len);
68 }
69
70 void btrfs_csum_final(u32 crc, char *result)
71 {
72         *(__le32 *)result = ~cpu_to_le32(crc);
73 }
74
75 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
76                          int verify)
77 {
78         char *result;
79         u32 len;
80         u32 crc = ~(u32)0;
81
82         result = malloc(csum_size * sizeof(char));
83         if (!result)
84                 return 1;
85
86         len = buf->len - BTRFS_CSUM_SIZE;
87         crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
88         btrfs_csum_final(crc, result);
89
90         if (verify) {
91                 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
92                         printk("checksum verify failed on %llu wanted %X "
93                                "found %X\n", (unsigned long long)buf->start,
94                                *((int *)result), *((char *)buf->data));
95                         free(result);
96                         return 1;
97                 }
98         } else {
99                 write_extent_buffer(buf, result, 0, csum_size);
100         }
101         free(result);
102         return 0;
103 }
104
105 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
106                     int verify)
107 {
108         u16 csum_size =
109                 btrfs_super_csum_size(&root->fs_info->super_copy);
110         return csum_tree_block_size(buf, csum_size, verify);
111 }
112
113 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
114                                             u64 bytenr, u32 blocksize)
115 {
116         return find_extent_buffer(&root->fs_info->extent_cache,
117                                   bytenr, blocksize);
118 }
119
120 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
121                                                  u64 bytenr, u32 blocksize)
122 {
123         return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
124                                    blocksize);
125 }
126
127 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
128                          u64 parent_transid)
129 {
130         int ret;
131         struct extent_buffer *eb;
132         u64 length;
133         struct btrfs_multi_bio *multi = NULL;
134         struct btrfs_device *device;
135
136         eb = btrfs_find_tree_block(root, bytenr, blocksize);
137         if (eb && btrfs_buffer_uptodate(eb, parent_transid)) {
138                 free_extent_buffer(eb);
139                 return 0;
140         }
141
142         length = blocksize;
143         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
144                               bytenr, &length, &multi, 0);
145         BUG_ON(ret);
146         device = multi->stripes[0].dev;
147         device->total_ios++;
148         blocksize = min(blocksize, (u32)(64 * 1024));
149         readahead(device->fd, multi->stripes[0].physical, blocksize);
150         kfree(multi);
151         return 0;
152 }
153
154 static int verify_parent_transid(struct extent_io_tree *io_tree,
155                                  struct extent_buffer *eb, u64 parent_transid,
156                                  int ignore)
157 {
158         int ret;
159
160         if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
161                 return 0;
162
163         if (extent_buffer_uptodate(eb) &&
164             btrfs_header_generation(eb) == parent_transid) {
165                 ret = 0;
166                 goto out;
167         }
168         printk("parent transid verify failed on %llu wanted %llu found %llu\n",
169                (unsigned long long)eb->start,
170                (unsigned long long)parent_transid,
171                (unsigned long long)btrfs_header_generation(eb));
172         if (ignore) {
173                 printk("Ignoring transid failure\n");
174                 return 0;
175         }
176
177         ret = 1;
178 out:
179         clear_extent_buffer_uptodate(io_tree, eb);
180         return ret;
181
182 }
183
184
185 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
186                                      u32 blocksize, u64 parent_transid)
187 {
188         int ret;
189         struct extent_buffer *eb;
190         u64 length;
191         u64 best_transid = 0;
192         struct btrfs_multi_bio *multi = NULL;
193         struct btrfs_device *device;
194         int mirror_num = 0;
195         int good_mirror = 0;
196         int num_copies;
197         int ignore = 0;
198
199         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
200         if (!eb)
201                 return NULL;
202
203         if (btrfs_buffer_uptodate(eb, parent_transid))
204                 return eb;
205
206         length = blocksize;
207         while (1) {
208                 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
209                                       eb->start, &length, &multi, mirror_num);
210                 if (ret) {
211                         printk("Couldn't map the block %Lu\n", bytenr);
212                         break;
213                 }
214                 device = multi->stripes[0].dev;
215                 eb->fd = device->fd;
216                 device->total_ios++;
217                 eb->dev_bytenr = multi->stripes[0].physical;
218                 kfree(multi);
219                 ret = read_extent_from_disk(eb);
220
221                 if (ret == 0 && check_tree_block(root, eb) == 0 &&
222                     csum_tree_block(root, eb, 1) == 0 &&
223                     verify_parent_transid(eb->tree, eb, parent_transid, ignore)
224                     == 0) {
225                         btrfs_set_buffer_uptodate(eb);
226                         return eb;
227                 }
228                 if (ignore) {
229                         if (check_tree_block(root, eb))
230                                 printk("read block failed check_tree_block\n");
231                         else
232                                 printk("Csum didn't match\n");
233                         break;
234                 }
235                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
236                                               eb->start, eb->len);
237                 if (num_copies == 1) {
238                         ignore = 1;
239                         continue;
240                 }
241                 if (btrfs_header_generation(eb) > best_transid) {
242                         best_transid = btrfs_header_generation(eb);
243                         good_mirror = mirror_num;
244                 }
245                 mirror_num++;
246                 if (mirror_num > num_copies) {
247                         mirror_num = good_mirror;
248                         ignore = 1;
249                         continue;
250                 }
251         }
252         free_extent_buffer(eb);
253         return NULL;
254 }
255
256 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
257                      struct extent_buffer *eb)
258 {
259         int ret;
260         int dev_nr;
261         u64 length;
262         struct btrfs_multi_bio *multi = NULL;
263
264         if (check_tree_block(root, eb))
265                 BUG();
266         if (!btrfs_buffer_uptodate(eb, trans->transid))
267                 BUG();
268
269         btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
270         csum_tree_block(root, eb, 0);
271
272         dev_nr = 0;
273         length = eb->len;
274         ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
275                               eb->start, &length, &multi, 0);
276
277         while(dev_nr < multi->num_stripes) {
278                 BUG_ON(ret);
279                 eb->fd = multi->stripes[dev_nr].dev->fd;
280                 eb->dev_bytenr = multi->stripes[dev_nr].physical;
281                 multi->stripes[dev_nr].dev->total_ios++;
282                 dev_nr++;
283                 ret = write_extent_to_disk(eb);
284                 BUG_ON(ret);
285         }
286         kfree(multi);
287         return 0;
288 }
289
290 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
291                         u32 stripesize, struct btrfs_root *root,
292                         struct btrfs_fs_info *fs_info, u64 objectid)
293 {
294         root->node = NULL;
295         root->commit_root = NULL;
296         root->sectorsize = sectorsize;
297         root->nodesize = nodesize;
298         root->leafsize = leafsize;
299         root->stripesize = stripesize;
300         root->ref_cows = 0;
301         root->track_dirty = 0;
302
303         root->fs_info = fs_info;
304         root->objectid = objectid;
305         root->last_trans = 0;
306         root->highest_inode = 0;
307         root->last_inode_alloc = 0;
308
309         INIT_LIST_HEAD(&root->dirty_list);
310         memset(&root->root_key, 0, sizeof(root->root_key));
311         memset(&root->root_item, 0, sizeof(root->root_item));
312         root->root_key.objectid = objectid;
313         return 0;
314 }
315
316 static int update_cowonly_root(struct btrfs_trans_handle *trans,
317                                struct btrfs_root *root)
318 {
319         int ret;
320         u64 old_root_bytenr;
321         struct btrfs_root *tree_root = root->fs_info->tree_root;
322
323         btrfs_write_dirty_block_groups(trans, root);
324         while(1) {
325                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
326                 if (old_root_bytenr == root->node->start)
327                         break;
328                 btrfs_set_root_bytenr(&root->root_item,
329                                        root->node->start);
330                 btrfs_set_root_generation(&root->root_item,
331                                           trans->transid);
332                 root->root_item.level = btrfs_header_level(root->node);
333                 ret = btrfs_update_root(trans, tree_root,
334                                         &root->root_key,
335                                         &root->root_item);
336                 BUG_ON(ret);
337                 btrfs_write_dirty_block_groups(trans, root);
338         }
339         return 0;
340 }
341
342 static int commit_tree_roots(struct btrfs_trans_handle *trans,
343                              struct btrfs_fs_info *fs_info)
344 {
345         struct btrfs_root *root;
346         struct list_head *next;
347         struct extent_buffer *eb;
348         int ret;
349
350         if (fs_info->readonly)
351                 return 0;
352
353         eb = fs_info->tree_root->node;
354         extent_buffer_get(eb);
355         ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
356         free_extent_buffer(eb);
357         if (ret)
358                 return ret;
359
360         while(!list_empty(&fs_info->dirty_cowonly_roots)) {
361                 next = fs_info->dirty_cowonly_roots.next;
362                 list_del_init(next);
363                 root = list_entry(next, struct btrfs_root, dirty_list);
364                 update_cowonly_root(trans, root);
365         }
366         return 0;
367 }
368
369 static int __commit_transaction(struct btrfs_trans_handle *trans,
370                                 struct btrfs_root *root)
371 {
372         u64 start;
373         u64 end;
374         struct extent_buffer *eb;
375         struct extent_io_tree *tree = &root->fs_info->extent_cache;
376         int ret;
377
378         while(1) {
379                 ret = find_first_extent_bit(tree, 0, &start, &end,
380                                             EXTENT_DIRTY);
381                 if (ret)
382                         break;
383                 while(start <= end) {
384                         eb = find_first_extent_buffer(tree, start);
385                         BUG_ON(!eb || eb->start != start);
386                         ret = write_tree_block(trans, root, eb);
387                         BUG_ON(ret);
388                         start += eb->len;
389                         clear_extent_buffer_dirty(eb);
390                         free_extent_buffer(eb);
391                 }
392         }
393         return 0;
394 }
395
396 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
397                              struct btrfs_root *root)
398 {
399         u64 transid = trans->transid;
400         int ret = 0;
401         struct btrfs_fs_info *fs_info = root->fs_info;
402
403         if (root->commit_root == root->node)
404                 goto commit_tree;
405
406         free_extent_buffer(root->commit_root);
407         root->commit_root = NULL;
408
409         btrfs_set_root_bytenr(&root->root_item, root->node->start);
410         btrfs_set_root_generation(&root->root_item, trans->transid);
411         root->root_item.level = btrfs_header_level(root->node);
412         ret = btrfs_update_root(trans, root->fs_info->tree_root,
413                                 &root->root_key, &root->root_item);
414         BUG_ON(ret);
415 commit_tree:
416         ret = commit_tree_roots(trans, fs_info);
417         BUG_ON(ret);
418         ret = __commit_transaction(trans, root);
419         BUG_ON(ret);
420         write_ctree_super(trans, root);
421         btrfs_finish_extent_commit(trans, fs_info->extent_root,
422                                    &fs_info->pinned_extents);
423         btrfs_free_transaction(root, trans);
424         free_extent_buffer(root->commit_root);
425         root->commit_root = NULL;
426         fs_info->running_transaction = NULL;
427         fs_info->last_trans_committed = transid;
428         return 0;
429 }
430
431 static int find_and_setup_root(struct btrfs_root *tree_root,
432                                struct btrfs_fs_info *fs_info,
433                                u64 objectid, struct btrfs_root *root)
434 {
435         int ret;
436         u32 blocksize;
437         u64 generation;
438
439         __setup_root(tree_root->nodesize, tree_root->leafsize,
440                      tree_root->sectorsize, tree_root->stripesize,
441                      root, fs_info, objectid);
442         ret = btrfs_find_last_root(tree_root, objectid,
443                                    &root->root_item, &root->root_key);
444         BUG_ON(ret);
445
446         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
447         generation = btrfs_root_generation(&root->root_item);
448         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
449                                      blocksize, generation);
450         if (!extent_buffer_uptodate(root->node))
451                 return -EIO;
452
453         return 0;
454 }
455
456 static int find_and_setup_log_root(struct btrfs_root *tree_root,
457                                struct btrfs_fs_info *fs_info,
458                                struct btrfs_super_block *disk_super)
459 {
460         u32 blocksize;
461         u64 blocknr = btrfs_super_log_root(disk_super);
462         struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
463
464         if (!log_root)
465                 return -ENOMEM;
466
467         if (blocknr == 0) {
468                 free(log_root);
469                 return 0;
470         }
471
472         blocksize = btrfs_level_size(tree_root,
473                              btrfs_super_log_root_level(disk_super));
474
475         __setup_root(tree_root->nodesize, tree_root->leafsize,
476                      tree_root->sectorsize, tree_root->stripesize,
477                      log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
478
479         log_root->node = read_tree_block(tree_root, blocknr,
480                                      blocksize,
481                                      btrfs_super_generation(disk_super) + 1);
482
483         fs_info->log_root_tree = log_root;
484
485         if (!extent_buffer_uptodate(log_root->node)) {
486                 free(log_root);
487                 return -EIO;
488         }
489
490         free(log_root);
491         return 0;
492 }
493
494
495 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info,
496                        struct btrfs_root *root)
497 {
498         if (root->node)
499                 free_extent_buffer(root->node);
500         if (root->commit_root)
501                 free_extent_buffer(root->commit_root);
502         kfree(root);
503         return 0;
504 }
505
506 static int free_fs_roots(struct btrfs_fs_info *fs_info)
507 {
508         struct cache_extent *cache;
509         struct btrfs_root *root;
510
511         while (1) {
512                 cache = find_first_cache_extent(&fs_info->fs_root_cache, 0);
513                 if (!cache)
514                         break;
515                 root = container_of(cache, struct btrfs_root, cache);
516                 remove_cache_extent(&fs_info->fs_root_cache, cache);
517                 btrfs_free_fs_root(fs_info, root);
518         }
519         return 0;
520 }
521
522 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
523                                                struct btrfs_key *location)
524 {
525         struct btrfs_root *root;
526         struct btrfs_root *tree_root = fs_info->tree_root;
527         struct btrfs_path *path;
528         struct extent_buffer *l;
529         u64 generation;
530         u32 blocksize;
531         int ret = 0;
532
533         root = malloc(sizeof(*root));
534         if (!root)
535                 return ERR_PTR(-ENOMEM);
536         memset(root, 0, sizeof(*root));
537         if (location->offset == (u64)-1) {
538                 ret = find_and_setup_root(tree_root, fs_info,
539                                           location->objectid, root);
540                 if (ret) {
541                         free(root);
542                         return ERR_PTR(ret);
543                 }
544                 goto insert;
545         }
546
547         __setup_root(tree_root->nodesize, tree_root->leafsize,
548                      tree_root->sectorsize, tree_root->stripesize,
549                      root, fs_info, location->objectid);
550
551         path = btrfs_alloc_path();
552         BUG_ON(!path);
553         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
554         if (ret != 0) {
555                 if (ret > 0)
556                         ret = -ENOENT;
557                 goto out;
558         }
559         l = path->nodes[0];
560         read_extent_buffer(l, &root->root_item,
561                btrfs_item_ptr_offset(l, path->slots[0]),
562                sizeof(root->root_item));
563         memcpy(&root->root_key, location, sizeof(*location));
564         ret = 0;
565 out:
566         btrfs_release_path(root, path);
567         btrfs_free_path(path);
568         if (ret) {
569                 free(root);
570                 return ERR_PTR(ret);
571         }
572         generation = btrfs_root_generation(&root->root_item);
573         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
574         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
575                                      blocksize, generation);
576         BUG_ON(!root->node);
577 insert:
578         root->ref_cows = 1;
579         return root;
580 }
581
582 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
583                                       struct btrfs_key *location)
584 {
585         struct btrfs_root *root;
586         struct cache_extent *cache;
587         int ret;
588
589         if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
590                 return fs_info->tree_root;
591         if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
592                 return fs_info->extent_root;
593         if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
594                 return fs_info->chunk_root;
595         if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
596                 return fs_info->dev_root;
597         if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
598                 return fs_info->csum_root;
599
600         BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
601                location->offset != (u64)-1);
602
603         cache = find_cache_extent(&fs_info->fs_root_cache,
604                                   location->objectid, 1);
605         if (cache)
606                 return container_of(cache, struct btrfs_root, cache);
607
608         root = btrfs_read_fs_root_no_cache(fs_info, location);
609         if (IS_ERR(root))
610                 return root;
611
612         root->cache.start = location->objectid;
613         root->cache.size = 1;
614         ret = insert_existing_cache_extent(&fs_info->fs_root_cache,
615                                            &root->cache);
616         BUG_ON(ret);
617         return root;
618 }
619
620 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
621                                              u64 sb_bytenr,
622                                              u64 root_tree_bytenr, int writes,
623                                              int partial)
624 {
625         u32 sectorsize;
626         u32 nodesize;
627         u32 leafsize;
628         u32 blocksize;
629         u32 stripesize;
630         u64 generation;
631         struct btrfs_key key;
632         struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
633         struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
634         struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
635         struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
636         struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
637         struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
638         int ret;
639         struct btrfs_super_block *disk_super;
640         struct btrfs_fs_devices *fs_devices = NULL;
641         u64 total_devs;
642         u64 features;
643
644         if (sb_bytenr == 0)
645                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
646
647         ret = btrfs_scan_one_device(fp, path, &fs_devices,
648                                     &total_devs, sb_bytenr);
649
650         if (ret) {
651                 fprintf(stderr, "No valid Btrfs found on %s\n", path);
652                 goto out;
653         }
654
655         if (total_devs != 1) {
656                 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
657                 if (ret)
658                         goto out;
659         }
660
661         memset(fs_info, 0, sizeof(*fs_info));
662         fs_info->tree_root = tree_root;
663         fs_info->extent_root = extent_root;
664         fs_info->chunk_root = chunk_root;
665         fs_info->dev_root = dev_root;
666         fs_info->csum_root = csum_root;
667
668         if (!writes)
669                 fs_info->readonly = 1;
670
671         extent_io_tree_init(&fs_info->extent_cache);
672         extent_io_tree_init(&fs_info->free_space_cache);
673         extent_io_tree_init(&fs_info->block_group_cache);
674         extent_io_tree_init(&fs_info->pinned_extents);
675         extent_io_tree_init(&fs_info->pending_del);
676         extent_io_tree_init(&fs_info->extent_ins);
677         cache_tree_init(&fs_info->fs_root_cache);
678
679         cache_tree_init(&fs_info->mapping_tree.cache_tree);
680
681         mutex_init(&fs_info->fs_mutex);
682         fs_info->fs_devices = fs_devices;
683         INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
684         INIT_LIST_HEAD(&fs_info->space_info);
685
686         __setup_root(4096, 4096, 4096, 4096, tree_root,
687                      fs_info, BTRFS_ROOT_TREE_OBJECTID);
688
689         if (writes)
690                 ret = btrfs_open_devices(fs_devices, O_RDWR);
691         else
692                 ret = btrfs_open_devices(fs_devices, O_RDONLY);
693         if (ret)
694                 goto out_cleanup;
695
696         fs_info->super_bytenr = sb_bytenr;
697         disk_super = &fs_info->super_copy;
698         ret = btrfs_read_dev_super(fs_devices->latest_bdev,
699                                    disk_super, sb_bytenr);
700         if (ret) {
701                 printk("No valid btrfs found\n");
702                 goto out_devices;
703         }
704
705         memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
706
707
708         features = btrfs_super_incompat_flags(disk_super) &
709                    ~BTRFS_FEATURE_INCOMPAT_SUPP;
710         if (features) {
711                 printk("couldn't open because of unsupported "
712                        "option features (%Lx).\n",
713                        (unsigned long long)features);
714                 goto out_devices;
715         }
716
717         features = btrfs_super_incompat_flags(disk_super);
718         if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
719                 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
720                 btrfs_set_super_incompat_flags(disk_super, features);
721         }
722
723         features = btrfs_super_compat_ro_flags(disk_super) &
724                 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
725         if (writes && features) {
726                 printk("couldn't open RDWR because of unsupported "
727                        "option features (%Lx).\n",
728                        (unsigned long long)features);
729                 goto out_devices;
730         }
731
732         nodesize = btrfs_super_nodesize(disk_super);
733         leafsize = btrfs_super_leafsize(disk_super);
734         sectorsize = btrfs_super_sectorsize(disk_super);
735         stripesize = btrfs_super_stripesize(disk_super);
736         tree_root->nodesize = nodesize;
737         tree_root->leafsize = leafsize;
738         tree_root->sectorsize = sectorsize;
739         tree_root->stripesize = stripesize;
740
741         ret = btrfs_read_sys_array(tree_root);
742         if (ret)
743                 goto out_devices;
744         blocksize = btrfs_level_size(tree_root,
745                                      btrfs_super_chunk_root_level(disk_super));
746         generation = btrfs_super_chunk_root_generation(disk_super);
747
748         __setup_root(nodesize, leafsize, sectorsize, stripesize,
749                      chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
750
751         chunk_root->node = read_tree_block(chunk_root,
752                                            btrfs_super_chunk_root(disk_super),
753                                            blocksize, generation);
754         if (!extent_buffer_uptodate(chunk_root->node)) {
755                 printk("Couldn't read chunk root\n");
756                 goto out_devices;
757         }
758
759         read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
760                  (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
761                  BTRFS_UUID_SIZE);
762
763         if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
764                 ret = btrfs_read_chunk_tree(chunk_root);
765                 if (ret)
766                         goto out_failed;
767         }
768
769         blocksize = btrfs_level_size(tree_root,
770                                      btrfs_super_root_level(disk_super));
771         generation = btrfs_super_generation(disk_super);
772
773         if (!root_tree_bytenr)
774                 root_tree_bytenr = btrfs_super_root(disk_super);
775         tree_root->node = read_tree_block(tree_root,
776                                           root_tree_bytenr,
777                                           blocksize, generation);
778         if (!extent_buffer_uptodate(tree_root->node)) {
779                 printk("Couldn't read tree root\n");
780                 goto out_failed;
781         }
782         ret = find_and_setup_root(tree_root, fs_info,
783                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root);
784         if (ret) {
785                 printk("Couldn't setup extent tree\n");
786                 goto out_failed;
787         }
788         extent_root->track_dirty = 1;
789
790         ret = find_and_setup_root(tree_root, fs_info,
791                                   BTRFS_DEV_TREE_OBJECTID, dev_root);
792         if (ret) {
793                 printk("Couldn't setup device tree\n");
794                 goto out_failed;
795         }
796         dev_root->track_dirty = 1;
797
798         ret = find_and_setup_root(tree_root, fs_info,
799                                   BTRFS_CSUM_TREE_OBJECTID, csum_root);
800         if (ret) {
801                 printk("Couldn't setup csum tree\n");
802                 if (!partial)
803                         goto out_failed;
804         }
805         csum_root->track_dirty = 1;
806
807         find_and_setup_log_root(tree_root, fs_info, disk_super);
808
809         fs_info->generation = generation;
810         fs_info->last_trans_committed = generation;
811         btrfs_read_block_groups(fs_info->tree_root);
812
813         key.objectid = BTRFS_FS_TREE_OBJECTID;
814         key.type = BTRFS_ROOT_ITEM_KEY;
815         key.offset = (u64)-1;
816         fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
817
818         if (!fs_info->fs_root)
819                 goto out_failed;
820
821         fs_info->data_alloc_profile = (u64)-1;
822         fs_info->metadata_alloc_profile = (u64)-1;
823         fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
824
825         return fs_info;
826
827 out_failed:
828         if (partial)
829                 return fs_info;
830
831         if (fs_info->csum_root)
832                 free_extent_buffer(fs_info->csum_root->node);
833         if (fs_info->dev_root)
834                 free_extent_buffer(fs_info->dev_root->node);
835         if (fs_info->extent_root)
836                 free_extent_buffer(fs_info->extent_root->node);
837         if (fs_info->tree_root)
838                 free_extent_buffer(fs_info->tree_root->node);
839         if (fs_info->chunk_root)
840                 free_extent_buffer(fs_info->chunk_root->node);
841 out_devices:
842         close_all_devices(fs_info);
843 out_cleanup:
844         extent_io_tree_cleanup(&fs_info->extent_cache);
845         extent_io_tree_cleanup(&fs_info->free_space_cache);
846         extent_io_tree_cleanup(&fs_info->block_group_cache);
847         extent_io_tree_cleanup(&fs_info->pinned_extents);
848         extent_io_tree_cleanup(&fs_info->pending_del);
849         extent_io_tree_cleanup(&fs_info->extent_ins);
850 out:
851         free(tree_root);
852         free(extent_root);
853         free(chunk_root);
854         free(dev_root);
855         free(csum_root);
856         free(fs_info);
857         return NULL;
858 }
859
860 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
861                                          u64 sb_bytenr, int writes,
862                                          int partial)
863 {
864         int fp;
865         struct btrfs_fs_info *info;
866         int flags = O_CREAT | O_RDWR;
867
868         if (!writes)
869                 flags = O_RDONLY;
870
871         fp = open(filename, flags, 0600);
872         if (fp < 0) {
873                 fprintf (stderr, "Could not open %s\n", filename);
874                 return NULL;
875         }
876         info = __open_ctree_fd(fp, filename, sb_bytenr, 0, writes, partial);
877         close(fp);
878         return info;
879 }
880
881 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
882 {
883         struct btrfs_fs_info *info;
884
885         info = open_ctree_fs_info(filename, sb_bytenr, writes, 0);
886         if (!info)
887                 return NULL;
888         return info->fs_root;
889 }
890
891 struct btrfs_root *open_ctree_recovery(const char *filename, u64 sb_bytenr,
892                                        u64 root_tree_bytenr)
893 {
894         int fp;
895         struct btrfs_fs_info *info;
896
897
898         fp = open(filename, O_RDONLY);
899         if (fp < 0) {
900                 fprintf (stderr, "Could not open %s\n", filename);
901                 return NULL;
902         }
903         info = __open_ctree_fd(fp, filename, sb_bytenr,
904                                root_tree_bytenr, 0, 0);
905         close(fp);
906
907         if (!info)
908                 return NULL;
909         return info->fs_root;
910 }
911
912 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
913                                  int writes)
914 {
915         struct btrfs_fs_info *info;
916         info = __open_ctree_fd(fp, path, sb_bytenr, 0, writes, 0);
917         if (!info)
918                 return NULL;
919         return info->fs_root;
920 }
921
922 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
923 {
924         u8 fsid[BTRFS_FSID_SIZE];
925         int fsid_is_initialized = 0;
926         struct btrfs_super_block buf;
927         int i;
928         int ret;
929         u64 transid = 0;
930         u64 bytenr;
931
932         if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
933                 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
934                 if (ret < sizeof(buf))
935                         return -1;
936
937                 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
938                     buf.magic != cpu_to_le64(BTRFS_MAGIC))
939                         return -1;
940
941                 memcpy(sb, &buf, sizeof(*sb));
942                 return 0;
943         }
944
945         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
946                 bytenr = btrfs_sb_offset(i);
947                 ret = pread64(fd, &buf, sizeof(buf), bytenr);
948                 if (ret < sizeof(buf))
949                         break;
950
951                 if (btrfs_super_bytenr(&buf) != bytenr )
952                         continue;
953                 /* if magic is NULL, the device was removed */
954                 if (buf.magic == 0 && i == 0) 
955                         return -1;
956                 if (buf.magic != cpu_to_le64(BTRFS_MAGIC))
957                         continue;
958
959                 if (!fsid_is_initialized) {
960                         memcpy(fsid, buf.fsid, sizeof(fsid));
961                         fsid_is_initialized = 1;
962                 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
963                         /*
964                          * the superblocks (the original one and
965                          * its backups) contain data of different
966                          * filesystems -> the super cannot be trusted
967                          */
968                         continue;
969                 }
970
971                 if (btrfs_super_generation(&buf) > transid) {
972                         memcpy(sb, &buf, sizeof(*sb));
973                         transid = btrfs_super_generation(&buf);
974                 }
975         }
976
977         return transid > 0 ? 0 : -1;
978 }
979
980 int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb,
981                      struct btrfs_device *device)
982 {
983         u64 bytenr;
984         u32 crc;
985         int i, ret;
986         void *buf;
987
988         buf = calloc(1, BTRFS_SUPER_INFO_SIZE);
989         BUG_ON(!buf);
990
991         if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
992                 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
993                 crc = ~(u32)0;
994                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
995                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
996                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
997
998                 memcpy(buf, sb, sizeof(*sb));
999                 ret = pwrite64(device->fd, buf, BTRFS_SUPER_INFO_SIZE,
1000                                root->fs_info->super_bytenr);
1001                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1002                 goto out;
1003         }
1004
1005         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1006                 bytenr = btrfs_sb_offset(i);
1007                 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1008                         break;
1009
1010                 btrfs_set_super_bytenr(sb, bytenr);
1011
1012                 crc = ~(u32)0;
1013                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1014                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1015                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1016
1017                 memcpy(buf, sb, sizeof(*sb));
1018                 ret = pwrite64(device->fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
1019                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1020         }
1021 out:
1022         free(buf);
1023         return 0;
1024 }
1025
1026 int write_all_supers(struct btrfs_root *root)
1027 {
1028         struct list_head *cur;
1029         struct list_head *head = &root->fs_info->fs_devices->devices;
1030         struct btrfs_device *dev;
1031         struct btrfs_super_block *sb;
1032         struct btrfs_dev_item *dev_item;
1033         int ret;
1034         u64 flags;
1035
1036         sb = &root->fs_info->super_copy;
1037         dev_item = &sb->dev_item;
1038         list_for_each(cur, head) {
1039                 dev = list_entry(cur, struct btrfs_device, dev_list);
1040                 if (!dev->writeable)
1041                         continue;
1042
1043                 btrfs_set_stack_device_generation(dev_item, 0);
1044                 btrfs_set_stack_device_type(dev_item, dev->type);
1045                 btrfs_set_stack_device_id(dev_item, dev->devid);
1046                 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1047                 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1048                 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1049                 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1050                 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1051                 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1052                 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1053
1054                 flags = btrfs_super_flags(sb);
1055                 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1056
1057                 ret = write_dev_supers(root, sb, dev);
1058                 BUG_ON(ret);
1059         }
1060         return 0;
1061 }
1062
1063 int write_ctree_super(struct btrfs_trans_handle *trans,
1064                       struct btrfs_root *root)
1065 {
1066         int ret;
1067         struct btrfs_root *tree_root = root->fs_info->tree_root;
1068         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1069
1070         if (root->fs_info->readonly)
1071                 return 0;
1072
1073         btrfs_set_super_generation(&root->fs_info->super_copy,
1074                                    trans->transid);
1075         btrfs_set_super_root(&root->fs_info->super_copy,
1076                              tree_root->node->start);
1077         btrfs_set_super_root_level(&root->fs_info->super_copy,
1078                                    btrfs_header_level(tree_root->node));
1079         btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1080                                    chunk_root->node->start);
1081         btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1082                                          btrfs_header_level(chunk_root->node));
1083         btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1084                                 btrfs_header_generation(chunk_root->node));
1085
1086         ret = write_all_supers(root);
1087         if (ret)
1088                 fprintf(stderr, "failed to write new super block err %d\n", ret);
1089         return ret;
1090 }
1091
1092 static int close_all_devices(struct btrfs_fs_info *fs_info)
1093 {
1094         struct list_head *list;
1095         struct list_head *next;
1096         struct btrfs_device *device;
1097
1098         return 0;
1099
1100         list = &fs_info->fs_devices->devices;
1101         list_for_each(next, list) {
1102                 device = list_entry(next, struct btrfs_device, dev_list);
1103                 close(device->fd);
1104         }
1105         return 0;
1106 }
1107
1108 int close_ctree(struct btrfs_root *root)
1109 {
1110         int ret;
1111         struct btrfs_trans_handle *trans;
1112         struct btrfs_fs_info *fs_info = root->fs_info;
1113
1114         if (fs_info->last_trans_committed !=
1115             fs_info->generation) {
1116                 trans = btrfs_start_transaction(root, 1);
1117                 btrfs_commit_transaction(trans, root);
1118                 trans = btrfs_start_transaction(root, 1);
1119                 ret = commit_tree_roots(trans, fs_info);
1120                 BUG_ON(ret);
1121                 ret = __commit_transaction(trans, root);
1122                 BUG_ON(ret);
1123                 write_ctree_super(trans, root);
1124                 btrfs_free_transaction(root, trans);
1125         }
1126         btrfs_free_block_groups(fs_info);
1127
1128         free_fs_roots(fs_info);
1129
1130         if (fs_info->extent_root->node)
1131                 free_extent_buffer(fs_info->extent_root->node);
1132         if (fs_info->tree_root->node)
1133                 free_extent_buffer(fs_info->tree_root->node);
1134         if (fs_info->chunk_root->node)
1135                 free_extent_buffer(fs_info->chunk_root->node);
1136         if (fs_info->dev_root->node)
1137                 free_extent_buffer(fs_info->dev_root->node);
1138         if (fs_info->csum_root->node)
1139                 free_extent_buffer(fs_info->csum_root->node);
1140
1141         if (fs_info->log_root_tree) {
1142                 if (fs_info->log_root_tree->node)
1143                         free_extent_buffer(fs_info->log_root_tree->node);
1144                 free(fs_info->log_root_tree);
1145         }
1146
1147         close_all_devices(fs_info);
1148         extent_io_tree_cleanup(&fs_info->extent_cache);
1149         extent_io_tree_cleanup(&fs_info->free_space_cache);
1150         extent_io_tree_cleanup(&fs_info->block_group_cache);
1151         extent_io_tree_cleanup(&fs_info->pinned_extents);
1152         extent_io_tree_cleanup(&fs_info->pending_del);
1153         extent_io_tree_cleanup(&fs_info->extent_ins);
1154
1155         free(fs_info->tree_root);
1156         free(fs_info->extent_root);
1157         free(fs_info->chunk_root);
1158         free(fs_info->dev_root);
1159         free(fs_info->csum_root);
1160         free(fs_info);
1161
1162         return 0;
1163 }
1164
1165 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1166                      struct extent_buffer *eb)
1167 {
1168         return clear_extent_buffer_dirty(eb);
1169 }
1170
1171 int wait_on_tree_block_writeback(struct btrfs_root *root,
1172                                  struct extent_buffer *eb)
1173 {
1174         return 0;
1175 }
1176
1177 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1178 {
1179         set_extent_buffer_dirty(eb);
1180 }
1181
1182 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1183 {
1184         int ret;
1185
1186         ret = extent_buffer_uptodate(buf);
1187         if (!ret)
1188                 return ret;
1189
1190         ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1191         return !ret;
1192 }
1193
1194 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1195 {
1196         return set_extent_buffer_uptodate(eb);
1197 }