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