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