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