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