Test the additional ioctl.
[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 #define _XOPEN_SOURCE 600
20 #define __USE_XOPEN2K
21 #define _GNU_SOURCE 1
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include "kerncompat.h"
29 #include "radix-tree.h"
30 #include "ctree.h"
31 #include "disk-io.h"
32 #include "volumes.h"
33 #include "transaction.h"
34 #include "crc32c.h"
35 #include "utils.h"
36 #include "print-tree.h"
37
38 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
39 {
40
41         struct btrfs_fs_devices *fs_devices;
42         int ret = 1;
43
44         if (buf->start != btrfs_header_bytenr(buf))
45                 return ret;
46
47         fs_devices = root->fs_info->fs_devices;
48         while (fs_devices) {
49                 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
50                                           (unsigned long)btrfs_header_fsid(buf),
51                                           BTRFS_FSID_SIZE)) {
52                         ret = 0;
53                         break;
54                 }
55                 fs_devices = fs_devices->seed;
56         }
57         return ret;
58 }
59
60 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
61 {
62         return crc32c(seed, data, len);
63 }
64
65 void btrfs_csum_final(u32 crc, char *result)
66 {
67         *(__le32 *)result = ~cpu_to_le32(crc);
68 }
69
70 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
71                          int verify)
72 {
73         char *result;
74         u32 len;
75         u32 crc = ~(u32)0;
76
77         result = malloc(csum_size * sizeof(char));
78         if (!result)
79                 return 1;
80
81         len = buf->len - BTRFS_CSUM_SIZE;
82         crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
83         btrfs_csum_final(crc, result);
84
85         if (verify) {
86                 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
87                         printk("checksum verify failed on %llu wanted %X "
88                                "found %X\n", (unsigned long long)buf->start,
89                                *((int *)result), *((char *)buf->data));
90                         free(result);
91                         return 1;
92                 }
93         } else {
94                 write_extent_buffer(buf, result, 0, csum_size);
95         }
96         free(result);
97         return 0;
98 }
99
100 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
101                     int verify)
102 {
103         u16 csum_size =
104                 btrfs_super_csum_size(&root->fs_info->super_copy);
105         return csum_tree_block_size(buf, csum_size, verify);
106 }
107
108 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
109                                             u64 bytenr, u32 blocksize)
110 {
111         return find_extent_buffer(&root->fs_info->extent_cache,
112                                   bytenr, blocksize);
113 }
114
115 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
116                                                  u64 bytenr, u32 blocksize)
117 {
118         return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
119                                    blocksize);
120 }
121
122 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
123                          u64 parent_transid)
124 {
125         int ret;
126         struct extent_buffer *eb;
127         u64 length;
128         struct btrfs_multi_bio *multi = NULL;
129         struct btrfs_device *device;
130
131         eb = btrfs_find_tree_block(root, bytenr, blocksize);
132         if (eb && btrfs_buffer_uptodate(eb, parent_transid)) {
133                 free_extent_buffer(eb);
134                 return 0;
135         }
136
137         length = blocksize;
138         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
139                               bytenr, &length, &multi, 0);
140         BUG_ON(ret);
141         device = multi->stripes[0].dev;
142         device->total_ios++;
143         blocksize = min(blocksize, (u32)(64 * 1024));
144         readahead(device->fd, multi->stripes[0].physical, blocksize);
145         kfree(multi);
146         return 0;
147 }
148
149 static int verify_parent_transid(struct extent_io_tree *io_tree,
150                                  struct extent_buffer *eb, u64 parent_transid)
151 {
152         int ret;
153
154         if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
155                 return 0;
156
157         if (extent_buffer_uptodate(eb) &&
158             btrfs_header_generation(eb) == parent_transid) {
159                 ret = 0;
160                 goto out;
161         }
162         printk("parent transid verify failed on %llu wanted %llu found %llu\n",
163                (unsigned long long)eb->start,
164                (unsigned long long)parent_transid,
165                (unsigned long long)btrfs_header_generation(eb));
166         ret = 1;
167 out:
168         clear_extent_buffer_uptodate(io_tree, eb);
169         return ret;
170
171 }
172
173
174 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
175                                      u32 blocksize, u64 parent_transid)
176 {
177         int ret;
178         struct extent_buffer *eb;
179         u64 length;
180         struct btrfs_multi_bio *multi = NULL;
181         struct btrfs_device *device;
182         int mirror_num = 0;
183         int num_copies;
184
185         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
186         if (!eb)
187                 return NULL;
188
189         if (btrfs_buffer_uptodate(eb, parent_transid))
190                 return eb;
191
192         length = blocksize;
193         while (1) {
194                 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
195                                       eb->start, &length, &multi, mirror_num);
196                 BUG_ON(ret);
197                 device = multi->stripes[0].dev;
198                 eb->fd = device->fd;
199                 device->total_ios++;
200                 eb->dev_bytenr = multi->stripes[0].physical;
201                 kfree(multi);
202                 ret = read_extent_from_disk(eb);
203
204                 if (ret == 0 && check_tree_block(root, eb) == 0 &&
205                     csum_tree_block(root, eb, 1) == 0 &&
206                     verify_parent_transid(eb->tree, eb, parent_transid) == 0) {
207                         btrfs_set_buffer_uptodate(eb);
208                         return eb;
209                 }
210                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
211                                               eb->start, eb->len);
212                 if (num_copies == 1) {
213                         break;
214                 }
215                 mirror_num++;
216                 if (mirror_num > num_copies) {
217                         break;
218                 }
219         }
220         free_extent_buffer(eb);
221         return NULL;
222 }
223
224 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
225                      struct extent_buffer *eb)
226 {
227         int ret;
228         int dev_nr;
229         u64 length;
230         struct btrfs_multi_bio *multi = NULL;
231
232         if (check_tree_block(root, eb))
233                 BUG();
234         if (!btrfs_buffer_uptodate(eb, trans->transid))
235                 BUG();
236
237         btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
238         csum_tree_block(root, eb, 0);
239
240         dev_nr = 0;
241         length = eb->len;
242         ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
243                               eb->start, &length, &multi, 0);
244
245         while(dev_nr < multi->num_stripes) {
246                 BUG_ON(ret);
247                 eb->fd = multi->stripes[dev_nr].dev->fd;
248                 eb->dev_bytenr = multi->stripes[dev_nr].physical;
249                 multi->stripes[dev_nr].dev->total_ios++;
250                 dev_nr++;
251                 ret = write_extent_to_disk(eb);
252                 BUG_ON(ret);
253         }
254         kfree(multi);
255         return 0;
256 }
257
258 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
259                         u32 stripesize, struct btrfs_root *root,
260                         struct btrfs_fs_info *fs_info, u64 objectid)
261 {
262         root->node = NULL;
263         root->commit_root = NULL;
264         root->sectorsize = sectorsize;
265         root->nodesize = nodesize;
266         root->leafsize = leafsize;
267         root->stripesize = stripesize;
268         root->ref_cows = 0;
269         root->track_dirty = 0;
270
271         root->fs_info = fs_info;
272         root->objectid = objectid;
273         root->last_trans = 0;
274         root->highest_inode = 0;
275         root->last_inode_alloc = 0;
276
277         INIT_LIST_HEAD(&root->dirty_list);
278         memset(&root->root_key, 0, sizeof(root->root_key));
279         memset(&root->root_item, 0, sizeof(root->root_item));
280         root->root_key.objectid = objectid;
281         return 0;
282 }
283
284 static int update_cowonly_root(struct btrfs_trans_handle *trans,
285                                struct btrfs_root *root)
286 {
287         int ret;
288         u64 old_root_bytenr;
289         struct btrfs_root *tree_root = root->fs_info->tree_root;
290
291         btrfs_write_dirty_block_groups(trans, root);
292         while(1) {
293                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
294                 if (old_root_bytenr == root->node->start)
295                         break;
296                 btrfs_set_root_bytenr(&root->root_item,
297                                        root->node->start);
298                 btrfs_set_root_generation(&root->root_item,
299                                           trans->transid);
300                 root->root_item.level = btrfs_header_level(root->node);
301                 ret = btrfs_update_root(trans, tree_root,
302                                         &root->root_key,
303                                         &root->root_item);
304                 BUG_ON(ret);
305                 btrfs_write_dirty_block_groups(trans, root);
306         }
307         return 0;
308 }
309
310 static int commit_tree_roots(struct btrfs_trans_handle *trans,
311                              struct btrfs_fs_info *fs_info)
312 {
313         struct btrfs_root *root;
314         struct list_head *next;
315         struct extent_buffer *eb;
316
317         if (fs_info->readonly)
318                 return 0;
319
320         eb = fs_info->tree_root->node;
321         extent_buffer_get(eb);
322         btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
323         free_extent_buffer(eb);
324
325         while(!list_empty(&fs_info->dirty_cowonly_roots)) {
326                 next = fs_info->dirty_cowonly_roots.next;
327                 list_del_init(next);
328                 root = list_entry(next, struct btrfs_root, dirty_list);
329                 update_cowonly_root(trans, root);
330         }
331         return 0;
332 }
333
334 static int __commit_transaction(struct btrfs_trans_handle *trans,
335                                 struct btrfs_root *root)
336 {
337         u64 start;
338         u64 end;
339         struct extent_buffer *eb;
340         struct extent_io_tree *tree = &root->fs_info->extent_cache;
341         int ret;
342
343         while(1) {
344                 ret = find_first_extent_bit(tree, 0, &start, &end,
345                                             EXTENT_DIRTY);
346                 if (ret)
347                         break;
348                 while(start <= end) {
349                         eb = find_first_extent_buffer(tree, start);
350                         BUG_ON(!eb || eb->start != start);
351                         ret = write_tree_block(trans, root, eb);
352                         BUG_ON(ret);
353                         start += eb->len;
354                         clear_extent_buffer_dirty(eb);
355                         free_extent_buffer(eb);
356                 }
357         }
358         return 0;
359 }
360
361 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
362                              struct btrfs_root *root)
363 {
364         int ret = 0;
365         struct btrfs_fs_info *fs_info = root->fs_info;
366
367         if (root->commit_root == root->node)
368                 goto commit_tree;
369
370         free_extent_buffer(root->commit_root);
371         root->commit_root = NULL;
372
373         btrfs_set_root_bytenr(&root->root_item, root->node->start);
374         btrfs_set_root_generation(&root->root_item, trans->transid);
375         root->root_item.level = btrfs_header_level(root->node);
376         ret = btrfs_update_root(trans, root->fs_info->tree_root,
377                                 &root->root_key, &root->root_item);
378         BUG_ON(ret);
379 commit_tree:
380         ret = commit_tree_roots(trans, fs_info);
381         BUG_ON(ret);
382         ret = __commit_transaction(trans, root);
383         BUG_ON(ret);
384         write_ctree_super(trans, root);
385         btrfs_finish_extent_commit(trans, fs_info->extent_root,
386                                    &fs_info->pinned_extents);
387         btrfs_free_transaction(root, trans);
388         free_extent_buffer(root->commit_root);
389         root->commit_root = NULL;
390         fs_info->running_transaction = NULL;
391         return 0;
392 }
393
394 static int find_and_setup_root(struct btrfs_root *tree_root,
395                                struct btrfs_fs_info *fs_info,
396                                u64 objectid, struct btrfs_root *root)
397 {
398         int ret;
399         u32 blocksize;
400         u64 generation;
401
402         __setup_root(tree_root->nodesize, tree_root->leafsize,
403                      tree_root->sectorsize, tree_root->stripesize,
404                      root, fs_info, objectid);
405         ret = btrfs_find_last_root(tree_root, objectid,
406                                    &root->root_item, &root->root_key);
407         BUG_ON(ret);
408
409         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
410         generation = btrfs_root_generation(&root->root_item);
411         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
412                                      blocksize, generation);
413         BUG_ON(!root->node);
414         return 0;
415 }
416
417 static int find_and_setup_log_root(struct btrfs_root *tree_root,
418                                struct btrfs_fs_info *fs_info,
419                                struct btrfs_super_block *disk_super)
420 {
421         u32 blocksize;
422         u64 blocknr = btrfs_super_log_root(disk_super);
423         struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
424
425         if (blocknr == 0)
426                 return 0;
427
428         blocksize = btrfs_level_size(tree_root,
429                              btrfs_super_log_root_level(disk_super));
430
431         __setup_root(tree_root->nodesize, tree_root->leafsize,
432                      tree_root->sectorsize, tree_root->stripesize,
433                      log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
434
435         log_root->node = read_tree_block(tree_root, blocknr,
436                                      blocksize,
437                                      btrfs_super_generation(disk_super) + 1);
438
439         fs_info->log_root_tree = log_root;
440         BUG_ON(!log_root->node);
441         return 0;
442 }
443
444
445 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info,
446                        struct btrfs_root *root)
447 {
448         if (root->node)
449                 free_extent_buffer(root->node);
450         if (root->commit_root)
451                 free_extent_buffer(root->commit_root);
452         kfree(root);
453         return 0;
454 }
455
456 static int free_fs_roots(struct btrfs_fs_info *fs_info)
457 {
458         struct cache_extent *cache;
459         struct btrfs_root *root;
460
461         while (1) {
462                 cache = find_first_cache_extent(&fs_info->fs_root_cache, 0);
463                 if (!cache)
464                         break;
465                 root = container_of(cache, struct btrfs_root, cache);
466                 remove_cache_extent(&fs_info->fs_root_cache, cache);
467                 btrfs_free_fs_root(fs_info, root);
468         }
469         return 0;
470 }
471
472 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
473                                                struct btrfs_key *location)
474 {
475         struct btrfs_root *root;
476         struct btrfs_root *tree_root = fs_info->tree_root;
477         struct btrfs_path *path;
478         struct extent_buffer *l;
479         u64 generation;
480         u32 blocksize;
481         int ret = 0;
482
483         root = malloc(sizeof(*root));
484         if (!root)
485                 return ERR_PTR(-ENOMEM);
486         memset(root, 0, sizeof(*root));
487         if (location->offset == (u64)-1) {
488                 ret = find_and_setup_root(tree_root, fs_info,
489                                           location->objectid, root);
490                 if (ret) {
491                         free(root);
492                         return ERR_PTR(ret);
493                 }
494                 goto insert;
495         }
496
497         __setup_root(tree_root->nodesize, tree_root->leafsize,
498                      tree_root->sectorsize, tree_root->stripesize,
499                      root, fs_info, location->objectid);
500
501         path = btrfs_alloc_path();
502         BUG_ON(!path);
503         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
504         if (ret != 0) {
505                 if (ret > 0)
506                         ret = -ENOENT;
507                 goto out;
508         }
509         l = path->nodes[0];
510         read_extent_buffer(l, &root->root_item,
511                btrfs_item_ptr_offset(l, path->slots[0]),
512                sizeof(root->root_item));
513         memcpy(&root->root_key, location, sizeof(*location));
514         ret = 0;
515 out:
516         btrfs_release_path(root, path);
517         btrfs_free_path(path);
518         if (ret) {
519                 free(root);
520                 return ERR_PTR(ret);
521         }
522         generation = btrfs_root_generation(&root->root_item);
523         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
524         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
525                                      blocksize, generation);
526         BUG_ON(!root->node);
527 insert:
528         root->ref_cows = 1;
529         return root;
530 }
531
532 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
533                                       struct btrfs_key *location)
534 {
535         struct btrfs_root *root;
536         struct cache_extent *cache;
537         int ret;
538
539         if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
540                 return fs_info->tree_root;
541         if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
542                 return fs_info->extent_root;
543         if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
544                 return fs_info->chunk_root;
545         if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
546                 return fs_info->dev_root;
547         if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
548                 return fs_info->csum_root;
549         
550         BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
551                location->offset != (u64)-1);
552
553         cache = find_cache_extent(&fs_info->fs_root_cache,
554                                   location->objectid, 1);
555         if (cache)
556                 return container_of(cache, struct btrfs_root, cache);
557
558         root = btrfs_read_fs_root_no_cache(fs_info, location);
559         if (IS_ERR(root))
560                 return root;
561
562         root->cache.start = location->objectid;
563         root->cache.size = 1;
564         ret = insert_existing_cache_extent(&fs_info->fs_root_cache,
565                                            &root->cache);
566         BUG_ON(ret);
567         return root;
568 }
569
570 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
571 {
572         int fp;
573         struct btrfs_root *root;
574         int flags = O_CREAT | O_RDWR;
575
576         if (!writes)
577                 flags = O_RDONLY;
578
579         fp = open(filename, flags, 0600);
580         if (fp < 0) {
581                 fprintf (stderr, "Could not open %s\n", filename);
582                 return NULL;
583         }
584         root = open_ctree_fd(fp, filename, sb_bytenr, writes);
585         close(fp);
586
587         return root;
588 }
589
590 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
591                                  int writes)
592 {
593         u32 sectorsize;
594         u32 nodesize;
595         u32 leafsize;
596         u32 blocksize;
597         u32 stripesize;
598         u64 generation;
599         struct btrfs_key key;
600         struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
601         struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
602         struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
603         struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
604         struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
605         struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
606         int ret;
607         struct btrfs_super_block *disk_super;
608         struct btrfs_fs_devices *fs_devices = NULL;
609         u64 total_devs;
610         u64 features;
611
612         if (sb_bytenr == 0)
613                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
614
615         ret = btrfs_scan_one_device(fp, path, &fs_devices,
616                                     &total_devs, sb_bytenr);
617
618         if (ret) {
619                 fprintf(stderr, "No valid Btrfs found on %s\n", path);
620                 return NULL;
621         }
622
623         if (total_devs != 1) {
624                 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
625                 BUG_ON(ret);
626         }
627
628         memset(fs_info, 0, sizeof(*fs_info));
629         fs_info->tree_root = tree_root;
630         fs_info->extent_root = extent_root;
631         fs_info->chunk_root = chunk_root;
632         fs_info->dev_root = dev_root;
633         fs_info->csum_root = csum_root;
634
635         if (!writes)
636                 fs_info->readonly = 1;
637
638         extent_io_tree_init(&fs_info->extent_cache);
639         extent_io_tree_init(&fs_info->free_space_cache);
640         extent_io_tree_init(&fs_info->block_group_cache);
641         extent_io_tree_init(&fs_info->pinned_extents);
642         extent_io_tree_init(&fs_info->pending_del);
643         extent_io_tree_init(&fs_info->extent_ins);
644         cache_tree_init(&fs_info->fs_root_cache);
645
646         cache_tree_init(&fs_info->mapping_tree.cache_tree);
647
648         mutex_init(&fs_info->fs_mutex);
649         fs_info->fs_devices = fs_devices;
650         INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
651         INIT_LIST_HEAD(&fs_info->space_info);
652
653         __setup_root(4096, 4096, 4096, 4096, tree_root,
654                      fs_info, BTRFS_ROOT_TREE_OBJECTID);
655
656         if (writes)
657                 ret = btrfs_open_devices(fs_devices, O_RDWR);
658         else
659                 ret = btrfs_open_devices(fs_devices, O_RDONLY);
660         BUG_ON(ret);
661
662         fs_info->super_bytenr = sb_bytenr;
663         disk_super = &fs_info->super_copy;
664         ret = btrfs_read_dev_super(fs_devices->latest_bdev,
665                                    disk_super, sb_bytenr);
666         if (ret) {
667                 printk("No valid btrfs found\n");
668                 BUG_ON(1);
669         }
670
671         memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
672
673
674         features = btrfs_super_incompat_flags(disk_super) &
675                    ~BTRFS_FEATURE_INCOMPAT_SUPP;
676         if (features) {
677                 printk("couldn't open because of unsupported "
678                        "option features (%Lx).\n",
679                        (unsigned long long)features);
680                 BUG_ON(1);
681         }
682
683         features = btrfs_super_incompat_flags(disk_super);
684         if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
685                 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
686                 btrfs_set_super_incompat_flags(disk_super, features);
687         }
688
689         features = btrfs_super_compat_ro_flags(disk_super) &
690                 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
691         if (writes && features) {
692                 printk("couldn't open RDWR because of unsupported "
693                        "option features (%Lx).\n",
694                        (unsigned long long)features);
695                 BUG_ON(1);
696         }
697
698         nodesize = btrfs_super_nodesize(disk_super);
699         leafsize = btrfs_super_leafsize(disk_super);
700         sectorsize = btrfs_super_sectorsize(disk_super);
701         stripesize = btrfs_super_stripesize(disk_super);
702         tree_root->nodesize = nodesize;
703         tree_root->leafsize = leafsize;
704         tree_root->sectorsize = sectorsize;
705         tree_root->stripesize = stripesize;
706
707         ret = btrfs_read_sys_array(tree_root);
708         BUG_ON(ret);
709         blocksize = btrfs_level_size(tree_root,
710                                      btrfs_super_chunk_root_level(disk_super));
711         generation = btrfs_super_chunk_root_generation(disk_super);
712
713         __setup_root(nodesize, leafsize, sectorsize, stripesize,
714                      chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
715
716         chunk_root->node = read_tree_block(chunk_root,
717                                            btrfs_super_chunk_root(disk_super),
718                                            blocksize, generation);
719
720         BUG_ON(!chunk_root->node);
721
722         read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
723                  (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
724                  BTRFS_UUID_SIZE);
725
726         if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
727                 ret = btrfs_read_chunk_tree(chunk_root);
728                 BUG_ON(ret);
729         }
730
731         blocksize = btrfs_level_size(tree_root,
732                                      btrfs_super_root_level(disk_super));
733         generation = btrfs_super_generation(disk_super);
734
735         tree_root->node = read_tree_block(tree_root,
736                                           btrfs_super_root(disk_super),
737                                           blocksize, generation);
738         BUG_ON(!tree_root->node);
739         ret = find_and_setup_root(tree_root, fs_info,
740                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root);
741         BUG_ON(ret);
742         extent_root->track_dirty = 1;
743
744         ret = find_and_setup_root(tree_root, fs_info,
745                                   BTRFS_DEV_TREE_OBJECTID, dev_root);
746         BUG_ON(ret);
747         dev_root->track_dirty = 1;
748
749         ret = find_and_setup_root(tree_root, fs_info,
750                                   BTRFS_CSUM_TREE_OBJECTID, csum_root);
751         BUG_ON(ret);
752         csum_root->track_dirty = 1;
753
754         BUG_ON(ret);
755
756         find_and_setup_log_root(tree_root, fs_info, disk_super);
757
758         fs_info->generation = generation + 1;
759         btrfs_read_block_groups(fs_info->tree_root);
760
761         key.objectid = BTRFS_FS_TREE_OBJECTID;
762         key.type = BTRFS_ROOT_ITEM_KEY;
763         key.offset = (u64)-1;
764         fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
765
766         fs_info->data_alloc_profile = (u64)-1;
767         fs_info->metadata_alloc_profile = (u64)-1;
768         fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
769
770         return fs_info->fs_root;
771 }
772
773 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
774 {
775         u8 fsid[BTRFS_FSID_SIZE];
776         struct btrfs_super_block buf;
777         int i;
778         int ret;
779         u64 transid = 0;
780         u64 bytenr;
781
782         if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
783                 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
784                 if (ret < sizeof(buf))
785                         return -1;
786
787                 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
788                     strncmp((char *)(&buf.magic), BTRFS_MAGIC,
789                             sizeof(buf.magic)))
790                         return -1;
791
792                 memcpy(sb, &buf, sizeof(*sb));
793                 return 0;
794         }
795
796         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
797                 bytenr = btrfs_sb_offset(i);
798                 ret = pread64(fd, &buf, sizeof(buf), bytenr);
799                 if (ret < sizeof(buf))
800                         break;
801
802                 if (btrfs_super_bytenr(&buf) != bytenr ||
803                     strncmp((char *)(&buf.magic), BTRFS_MAGIC,
804                             sizeof(buf.magic)))
805                         continue;
806
807                 if (i == 0)
808                         memcpy(fsid, buf.fsid, sizeof(fsid));
809                 else if (memcmp(fsid, buf.fsid, sizeof(fsid)))
810                         continue;
811
812                 if (btrfs_super_generation(&buf) > transid) {
813                         memcpy(sb, &buf, sizeof(*sb));
814                         transid = btrfs_super_generation(&buf);
815                 }
816         }
817
818         return transid > 0 ? 0 : -1;
819 }
820
821 int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb,
822                      struct btrfs_device *device)
823 {
824         u64 bytenr;
825         u32 crc;
826         int i, ret;
827
828         if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
829                 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
830 printk("speiiiiiiiiiiiiiiiiiiiiiiiiiiiii\n");
831                 crc = ~(u32)0;
832                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
833                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
834                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
835
836                 ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE,
837                                root->fs_info->super_bytenr);
838                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
839                 return 0;
840         }
841
842         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
843                 bytenr = btrfs_sb_offset(i);
844                 if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
845                         break;
846
847                 btrfs_set_super_bytenr(sb, bytenr);
848
849                 crc = ~(u32)0;
850                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
851                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
852                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
853
854                 ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE, bytenr);
855                 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
856         }
857         return 0;
858 }
859
860 int write_all_supers(struct btrfs_root *root)
861 {
862         struct list_head *cur;
863         struct list_head *head = &root->fs_info->fs_devices->devices;
864         struct btrfs_device *dev;
865         struct btrfs_super_block *sb;
866         struct btrfs_dev_item *dev_item;
867         int ret;
868         u64 flags;
869
870         sb = &root->fs_info->super_copy;
871         dev_item = &sb->dev_item;
872         list_for_each(cur, head) {
873                 dev = list_entry(cur, struct btrfs_device, dev_list);
874                 if (!dev->writeable)
875                         continue;
876
877                 btrfs_set_stack_device_generation(dev_item, 0);
878                 btrfs_set_stack_device_type(dev_item, dev->type);
879                 btrfs_set_stack_device_id(dev_item, dev->devid);
880                 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
881                 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
882                 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
883                 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
884                 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
885                 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
886                 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
887
888                 flags = btrfs_super_flags(sb);
889                 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
890
891                 ret = write_dev_supers(root, sb, dev);
892                 BUG_ON(ret);
893         }
894         return 0;
895 }
896
897 int write_ctree_super(struct btrfs_trans_handle *trans,
898                       struct btrfs_root *root)
899 {
900         int ret;
901         struct btrfs_root *tree_root = root->fs_info->tree_root;
902         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
903
904         if (root->fs_info->readonly)
905                 return 0;
906
907         btrfs_set_super_generation(&root->fs_info->super_copy,
908                                    trans->transid);
909         btrfs_set_super_root(&root->fs_info->super_copy,
910                              tree_root->node->start);
911         btrfs_set_super_root_level(&root->fs_info->super_copy,
912                                    btrfs_header_level(tree_root->node));
913         btrfs_set_super_chunk_root(&root->fs_info->super_copy,
914                                    chunk_root->node->start);
915         btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
916                                          btrfs_header_level(chunk_root->node));
917         btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
918                                 btrfs_header_generation(chunk_root->node));
919
920         ret = write_all_supers(root);
921         if (ret)
922                 fprintf(stderr, "failed to write new super block err %d\n", ret);
923         return ret;
924 }
925
926 static int close_all_devices(struct btrfs_fs_info *fs_info)
927 {
928         struct list_head *list;
929         struct list_head *next;
930         struct btrfs_device *device;
931
932         return 0;
933
934         list = &fs_info->fs_devices->devices;
935         list_for_each(next, list) {
936                 device = list_entry(next, struct btrfs_device, dev_list);
937                 close(device->fd);
938         }
939         return 0;
940 }
941
942 int close_ctree(struct btrfs_root *root)
943 {
944         int ret;
945         struct btrfs_trans_handle *trans;
946         struct btrfs_fs_info *fs_info = root->fs_info;
947
948         trans = btrfs_start_transaction(root, 1);
949         btrfs_commit_transaction(trans, root);
950         trans = btrfs_start_transaction(root, 1);
951         ret = commit_tree_roots(trans, fs_info);
952         BUG_ON(ret);
953         ret = __commit_transaction(trans, root);
954         BUG_ON(ret);
955         write_ctree_super(trans, root);
956         btrfs_free_transaction(root, trans);
957         btrfs_free_block_groups(fs_info);
958
959         free_fs_roots(fs_info);
960
961         if (fs_info->extent_root->node)
962                 free_extent_buffer(fs_info->extent_root->node);
963         if (fs_info->tree_root->node)
964                 free_extent_buffer(fs_info->tree_root->node);
965         if (fs_info->chunk_root->node)
966                 free_extent_buffer(fs_info->chunk_root->node);
967         if (fs_info->dev_root->node)
968                 free_extent_buffer(fs_info->dev_root->node);
969         if (fs_info->csum_root->node)
970                 free_extent_buffer(fs_info->csum_root->node);
971
972         if (fs_info->log_root_tree) {
973                 if (fs_info->log_root_tree->node)
974                         free_extent_buffer(fs_info->log_root_tree->node);
975                 free(fs_info->log_root_tree);
976         }
977
978         close_all_devices(fs_info);
979         extent_io_tree_cleanup(&fs_info->extent_cache);
980         extent_io_tree_cleanup(&fs_info->free_space_cache);
981         extent_io_tree_cleanup(&fs_info->block_group_cache);
982         extent_io_tree_cleanup(&fs_info->pinned_extents);
983         extent_io_tree_cleanup(&fs_info->pending_del);
984         extent_io_tree_cleanup(&fs_info->extent_ins);
985
986         free(fs_info->tree_root);
987         free(fs_info->extent_root);
988         free(fs_info->chunk_root);
989         free(fs_info->dev_root);
990         free(fs_info->csum_root);
991         free(fs_info);
992
993         return 0;
994 }
995
996 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
997                      struct extent_buffer *eb)
998 {
999         return clear_extent_buffer_dirty(eb);
1000 }
1001
1002 int wait_on_tree_block_writeback(struct btrfs_root *root,
1003                                  struct extent_buffer *eb)
1004 {
1005         return 0;
1006 }
1007
1008 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1009 {
1010         set_extent_buffer_dirty(eb);
1011 }
1012
1013 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1014 {
1015         int ret;
1016
1017         ret = extent_buffer_uptodate(buf);
1018         if (!ret)
1019                 return ret;
1020
1021         ret = verify_parent_transid(buf->tree, buf, parent_transid);
1022         return !ret;
1023 }
1024
1025 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1026 {
1027         return set_extent_buffer_uptodate(eb);
1028 }