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