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