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