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