Add rollback support for the converter
[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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include "kerncompat.h"
28 #include "radix-tree.h"
29 #include "ctree.h"
30 #include "disk-io.h"
31 #include "transaction.h"
32 #include "crc32c.h"
33
34 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *buf,
35                             u64 logical)
36 {
37         buf->fd = root->fs_info->fp;
38         buf->dev_bytenr = logical;
39         return 0;
40 }
41
42 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
43 {
44         if (buf->start != btrfs_header_bytenr(buf))
45                 BUG();
46
47         if (memcmp_extent_buffer(buf, root->fs_info->fsid,
48                                  (unsigned long)btrfs_header_fsid(buf),
49                                  BTRFS_FSID_SIZE))
50                 BUG();
51         return 0;
52 }
53
54 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
55 {
56         return crc32c(seed, data, len);
57 }
58
59 void btrfs_csum_final(u32 crc, char *result)
60 {
61         *(__le32 *)result = ~cpu_to_le32(crc);
62 }
63
64 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
65                            int verify)
66 {
67         char result[BTRFS_CRC32_SIZE];
68         u32 len;
69         u32 crc = ~(u32)0;
70
71         len = buf->len - BTRFS_CSUM_SIZE;
72         crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
73         btrfs_csum_final(crc, result);
74
75         if (verify) {
76                 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
77                         printk("checksum verify failed on %llu\n", buf->start);
78                         return 1;
79                 }
80         } else {
81                 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
82         }
83         return 0;
84 }
85
86 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
87                                             u64 bytenr, u32 blocksize)
88 {
89         return find_extent_buffer(&root->fs_info->extent_cache,
90                                   bytenr, blocksize);
91 }
92
93 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
94                                                  u64 bytenr, u32 blocksize)
95 {
96         return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
97                                    blocksize);
98 }
99
100 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
101 {
102         return 0;
103 }
104
105 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
106                                      u32 blocksize)
107 {
108         int ret;
109         struct extent_buffer *eb;
110
111         eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
112         if (!eb)
113                 return NULL;
114         if (!btrfs_buffer_uptodate(eb)) {
115                 btrfs_map_bh_to_logical(root, eb, eb->start);
116                 ret = read_extent_from_disk(eb);
117                 if (ret) {
118                         free_extent_buffer(eb);
119                         return NULL;
120                 }
121                 btrfs_set_buffer_uptodate(eb);
122         }
123         return eb;
124 }
125
126 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
127                      struct extent_buffer *eb)
128 {
129         if (check_tree_block(root, eb))
130                 BUG();
131         if (!btrfs_buffer_uptodate(eb))
132                 BUG();
133         btrfs_map_bh_to_logical(root, eb, eb->start);
134         csum_tree_block(root, eb, 0);
135         return write_extent_to_disk(eb);
136 }
137
138 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
139                         u32 stripesize, struct btrfs_root *root,
140                         struct btrfs_fs_info *fs_info, u64 objectid)
141 {
142         root->node = NULL;
143         root->commit_root = NULL;
144         root->sectorsize = sectorsize;
145         root->nodesize = nodesize;
146         root->leafsize = leafsize;
147         root->stripesize = stripesize;
148         root->ref_cows = 0;
149         root->fs_info = fs_info;
150         root->objectid = objectid;
151         root->last_trans = 0;
152         root->highest_inode = 0;
153         root->last_inode_alloc = 0;
154         memset(&root->root_key, 0, sizeof(root->root_key));
155         memset(&root->root_item, 0, sizeof(root->root_item));
156         root->root_key.objectid = objectid;
157         return 0;
158 }
159
160 static int commit_tree_roots(struct btrfs_trans_handle *trans,
161                              struct btrfs_fs_info *fs_info)
162 {
163         int ret;
164         u64 old_extent_bytenr;
165         struct btrfs_root *tree_root = fs_info->tree_root;
166         struct btrfs_root *extent_root = fs_info->extent_root;
167
168         btrfs_write_dirty_block_groups(trans, fs_info->extent_root);
169         while(1) {
170                 old_extent_bytenr = btrfs_root_bytenr(&extent_root->root_item);
171                 if (old_extent_bytenr == extent_root->node->start)
172                         break;
173                 btrfs_set_root_bytenr(&extent_root->root_item,
174                                        extent_root->node->start);
175                 extent_root->root_item.level =
176                         btrfs_header_level(extent_root->node);
177                 ret = btrfs_update_root(trans, tree_root,
178                                         &extent_root->root_key,
179                                         &extent_root->root_item);
180                 BUG_ON(ret);
181                 btrfs_write_dirty_block_groups(trans, fs_info->extent_root);
182         }
183         return 0;
184 }
185
186 static int __commit_transaction(struct btrfs_trans_handle *trans,
187                                 struct btrfs_root *root)
188 {
189         u64 start;
190         u64 end;
191         struct extent_buffer *eb;
192         struct extent_map_tree *tree = &root->fs_info->extent_cache;
193         int ret;
194
195         while(1) {
196                 ret = find_first_extent_bit(tree, 0, &start, &end,
197                                             EXTENT_DIRTY);
198                 if (ret)
199                         break;
200                 while(start <= end) {
201                         eb = find_first_extent_buffer(tree, start);
202                         BUG_ON(!eb || eb->start != start);
203                         ret = write_tree_block(trans, root, eb);
204                         BUG_ON(ret);
205                         start += eb->len;
206                         clear_extent_buffer_dirty(eb);
207                         free_extent_buffer(eb);
208                 }
209         }
210         return 0;
211 }
212
213 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
214                              struct btrfs_root *root)
215 {
216         int ret = 0;
217         struct btrfs_root *new_root = NULL;
218         struct btrfs_fs_info *fs_info = root->fs_info;
219
220         if (root->commit_root == root->node)
221                 goto commit_tree;
222
223         new_root = malloc(sizeof(*new_root));
224         if (!new_root)
225                 return -ENOMEM;
226         memcpy(new_root, root, sizeof(*new_root));
227         new_root->node = root->commit_root;
228         root->commit_root = NULL;
229
230         root->root_key.offset = trans->transid;
231         btrfs_set_root_bytenr(&root->root_item, root->node->start);
232         root->root_item.level = btrfs_header_level(root->node);
233         ret = btrfs_insert_root(trans, fs_info->tree_root,
234                                 &root->root_key, &root->root_item);
235         BUG_ON(ret);
236
237         btrfs_set_root_refs(&new_root->root_item, 0);
238         ret = btrfs_update_root(trans, root->fs_info->tree_root,
239                                 &new_root->root_key, &new_root->root_item);
240         BUG_ON(ret);
241
242         ret = commit_tree_roots(trans, fs_info);
243         BUG_ON(ret);
244         ret = __commit_transaction(trans, root);
245         BUG_ON(ret);
246         write_ctree_super(trans, root);
247         btrfs_finish_extent_commit(trans, fs_info->extent_root,
248                                    &fs_info->pinned_extents);
249         btrfs_free_transaction(root, trans);
250         fs_info->running_transaction = NULL;
251
252         trans = btrfs_start_transaction(root, 1);
253         ret = btrfs_drop_snapshot(trans, new_root);
254         BUG_ON(ret);
255         ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
256         BUG_ON(ret);
257 commit_tree:
258         ret = commit_tree_roots(trans, fs_info);
259         BUG_ON(ret);
260         ret = __commit_transaction(trans, root);
261         BUG_ON(ret);
262         write_ctree_super(trans, root);
263         btrfs_finish_extent_commit(trans, fs_info->extent_root,
264                                    &fs_info->pinned_extents);
265         btrfs_free_transaction(root, trans);
266         free_extent_buffer(root->commit_root);
267         root->commit_root = NULL;
268         fs_info->running_transaction = NULL;
269         if (new_root) {
270                 free_extent_buffer(new_root->node);
271                 free(new_root);
272         }
273         return 0;
274 }
275
276 static int find_and_setup_root(struct btrfs_root *tree_root,
277                                struct btrfs_fs_info *fs_info,
278                                u64 objectid, struct btrfs_root *root)
279 {
280         int ret;
281         u32 blocksize;
282
283         __setup_root(tree_root->nodesize, tree_root->leafsize,
284                      tree_root->sectorsize, tree_root->stripesize,
285                      root, fs_info, objectid);
286         ret = btrfs_find_last_root(tree_root, objectid,
287                                    &root->root_item, &root->root_key);
288         BUG_ON(ret);
289
290         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
291         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
292                                      blocksize);
293         BUG_ON(!root->node);
294         return 0;
295 }
296
297 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
298 {
299         if (root->node)
300                 free_extent_buffer(root->node);
301         if (root->commit_root)
302                 free_extent_buffer(root->commit_root);
303
304         free(root);
305         return 0;
306 }
307
308 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
309                                       struct btrfs_key *location)
310 {
311         struct btrfs_root *root;
312         struct btrfs_root *tree_root = fs_info->tree_root;
313         struct btrfs_path *path;
314         struct extent_buffer *l;
315         u32 blocksize;
316         int ret = 0;
317
318         root = malloc(sizeof(*root));
319         if (!root)
320                 return ERR_PTR(-ENOMEM);
321         memset(root, 0, sizeof(*root));
322         if (location->offset == (u64)-1) {
323                 ret = find_and_setup_root(tree_root, fs_info,
324                                           location->objectid, root);
325                 if (ret) {
326                         free(root);
327                         return ERR_PTR(ret);
328                 }
329                 goto insert;
330         }
331
332         __setup_root(tree_root->nodesize, tree_root->leafsize,
333                      tree_root->sectorsize, tree_root->stripesize,
334                      root, fs_info, location->objectid);
335
336         path = btrfs_alloc_path();
337         BUG_ON(!path);
338         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
339         if (ret != 0) {
340                 if (ret > 0)
341                         ret = -ENOENT;
342                 goto out;
343         }
344         l = path->nodes[0];
345         read_extent_buffer(l, &root->root_item,
346                btrfs_item_ptr_offset(l, path->slots[0]),
347                sizeof(root->root_item));
348         memcpy(&root->root_key, location, sizeof(*location));
349         ret = 0;
350 out:
351         btrfs_release_path(root, path);
352         btrfs_free_path(path);
353         if (ret) {
354                 free(root);
355                 return ERR_PTR(ret);
356         }
357         blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
358         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
359                                      blocksize);
360         BUG_ON(!root->node);
361 insert:
362         root->ref_cows = 1;
363         return root;
364 }
365
366 struct btrfs_root *open_ctree(char *filename, u64 sb_bytenr)
367 {
368         int fp;
369
370         fp = open(filename, O_CREAT | O_RDWR, 0600);
371         if (fp < 0) {
372                 return NULL;
373         }
374         return open_ctree_fd(fp, sb_bytenr);
375 }
376
377 struct btrfs_root *open_ctree_fd(int fp, u64 sb_bytenr)
378 {
379         u32 sectorsize;
380         u32 nodesize;
381         u32 leafsize;
382         u32 blocksize;
383         u32 stripesize;
384         struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
385         struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
386         struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
387         struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
388         int ret;
389         struct btrfs_super_block *disk_super;
390
391         if (sb_bytenr == 0)
392                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
393
394         fs_info->fp = fp;
395         fs_info->running_transaction = NULL;
396         fs_info->fs_root = root;
397         fs_info->tree_root = tree_root;
398         fs_info->extent_root = extent_root;
399         fs_info->extent_ops = NULL;
400         fs_info->priv_data = NULL;
401         extent_map_tree_init(&fs_info->extent_cache);
402         extent_map_tree_init(&fs_info->free_space_cache);
403         extent_map_tree_init(&fs_info->pending_tree);
404         extent_map_tree_init(&fs_info->pinned_extents);
405         extent_map_tree_init(&fs_info->del_pending);
406         extent_map_tree_init(&fs_info->block_group_cache);
407
408         mutex_init(&fs_info->fs_mutex);
409
410         __setup_root(512, 512, 512, 512, tree_root,
411                      fs_info, BTRFS_ROOT_TREE_OBJECTID);
412
413         fs_info->sb_buffer = read_tree_block(tree_root, sb_bytenr, 512);
414         BUG_ON(!fs_info->sb_buffer);
415         read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
416                            sizeof(fs_info->super_copy));
417         read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
418                            (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
419                            BTRFS_FSID_SIZE);
420
421         disk_super = &fs_info->super_copy;
422         if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
423                     sizeof(disk_super->magic))) {
424                 printk("No valid btrfs found\n");
425                 BUG_ON(1);
426         }
427         nodesize = btrfs_super_nodesize(disk_super);
428         leafsize = btrfs_super_leafsize(disk_super);
429         sectorsize = btrfs_super_sectorsize(disk_super);
430         stripesize = btrfs_super_stripesize(disk_super);
431         tree_root->nodesize = nodesize;
432         tree_root->leafsize = leafsize;
433         tree_root->sectorsize = sectorsize;
434         tree_root->stripesize = stripesize;
435
436         blocksize = btrfs_level_size(tree_root,
437                                      btrfs_super_root_level(disk_super));
438         tree_root->node = read_tree_block(tree_root,
439                                           btrfs_super_root(disk_super),
440                                           blocksize);
441         BUG_ON(!tree_root->node);
442         ret = find_and_setup_root(tree_root, fs_info,
443                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root);
444         BUG_ON(ret);
445         ret = find_and_setup_root(tree_root, fs_info,
446                                   BTRFS_FS_TREE_OBJECTID, root);
447         BUG_ON(ret);
448         root->ref_cows = 1;
449         fs_info->generation = btrfs_super_generation(disk_super) + 1;
450         btrfs_read_block_groups(root);
451         return root;
452 }
453
454 int write_ctree_super(struct btrfs_trans_handle *trans,
455                       struct btrfs_root *root)
456 {
457         int ret;
458         struct btrfs_root *tree_root = root->fs_info->tree_root;
459         btrfs_set_super_generation(&root->fs_info->super_copy,
460                                    trans->transid);
461         btrfs_set_super_root(&root->fs_info->super_copy,
462                              tree_root->node->start);
463         btrfs_set_super_root_level(&root->fs_info->super_copy,
464                                    btrfs_header_level(tree_root->node));
465         write_extent_buffer(root->fs_info->sb_buffer,
466                             &root->fs_info->super_copy, 0,
467                             sizeof(root->fs_info->super_copy));
468         ret = write_tree_block(trans, root, root->fs_info->sb_buffer);
469         if (ret)
470                 fprintf(stderr, "failed to write new super block err %d\n", ret);
471         return ret;
472 }
473
474 int close_ctree(struct btrfs_root *root)
475 {
476         int ret;
477         struct btrfs_trans_handle *trans;
478         struct btrfs_fs_info *fs_info = root->fs_info;
479
480         trans = btrfs_start_transaction(root, 1);
481         btrfs_commit_transaction(trans, root);
482         trans = btrfs_start_transaction(root, 1);
483         ret = commit_tree_roots(trans, root->fs_info);
484         BUG_ON(ret);
485         ret = __commit_transaction(trans, root);
486         BUG_ON(ret);
487         write_ctree_super(trans, root);
488         btrfs_free_transaction(root, trans);
489         btrfs_free_block_groups(root->fs_info);
490         close(root->fs_info->fp);
491         if (root->node)
492                 free_extent_buffer(root->node);
493         if (root->fs_info->extent_root->node)
494                 free_extent_buffer(root->fs_info->extent_root->node);
495         if (root->fs_info->tree_root->node)
496                 free_extent_buffer(root->fs_info->tree_root->node);
497         free_extent_buffer(root->commit_root);
498         free_extent_buffer(root->fs_info->sb_buffer);
499
500         extent_map_tree_cleanup(&fs_info->extent_cache);
501         extent_map_tree_cleanup(&fs_info->free_space_cache);
502         extent_map_tree_cleanup(&fs_info->pending_tree);
503         extent_map_tree_cleanup(&fs_info->pinned_extents);
504         extent_map_tree_cleanup(&fs_info->del_pending);
505         extent_map_tree_cleanup(&fs_info->block_group_cache);
506
507         free(fs_info->tree_root);
508         free(fs_info->extent_root);
509         free(fs_info->fs_root);
510         free(fs_info);
511
512         return 0;
513 }
514
515 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
516                      struct extent_buffer *eb)
517 {
518         return clear_extent_buffer_dirty(eb);
519 }
520
521 int wait_on_tree_block_writeback(struct btrfs_root *root,
522                                  struct extent_buffer *eb)
523 {
524         return 0;
525 }
526
527 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
528 {
529         set_extent_buffer_dirty(eb);
530 }
531
532 int btrfs_buffer_uptodate(struct extent_buffer *eb)
533 {
534         return extent_buffer_uptodate(eb);
535 }
536
537 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
538 {
539         return set_extent_buffer_uptodate(eb);
540 }