ioctls to scan for btrfs filesystems
[btrfs-progs-unstable.git] / disk-io.c
blobce801bf16ea228359e911f473698471a14515cb4
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
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 "volumes.h"
32 #include "transaction.h"
33 #include "crc32c.h"
35 int btrfs_open_device(struct btrfs_device *dev)
37 return 0;
40 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *buf,
41 u64 logical)
43 u64 physical;
44 u64 length;
45 struct btrfs_device *device;
46 int ret;
48 ret = btrfs_map_block(&root->fs_info->mapping_tree, logical, &physical,
49 &length, &device);
50 BUG_ON(ret);
51 buf->fd = device->fd;
52 buf->dev_bytenr = physical;
53 return 0;
56 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
58 if (buf->start != btrfs_header_bytenr(buf))
59 BUG();
61 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
62 (unsigned long)btrfs_header_fsid(buf),
63 BTRFS_FSID_SIZE))
64 BUG();
65 return 0;
68 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
70 return crc32c(seed, data, len);
73 void btrfs_csum_final(u32 crc, char *result)
75 *(__le32 *)result = ~cpu_to_le32(crc);
78 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
79 int verify)
81 char result[BTRFS_CRC32_SIZE];
82 u32 len;
83 u32 crc = ~(u32)0;
85 len = buf->len - BTRFS_CSUM_SIZE;
86 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
87 btrfs_csum_final(crc, result);
89 if (verify) {
90 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
91 printk("checksum verify failed on %llu\n", buf->start);
92 return 1;
94 } else {
95 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
97 return 0;
100 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
101 u64 bytenr, u32 blocksize)
103 return find_extent_buffer(&root->fs_info->extent_cache,
104 bytenr, blocksize);
107 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
108 u64 bytenr, u32 blocksize)
110 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
111 blocksize);
114 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
116 return 0;
119 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
120 u32 blocksize)
122 int ret;
123 struct extent_buffer *eb;
125 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
126 if (!eb)
127 return NULL;
128 if (!btrfs_buffer_uptodate(eb)) {
129 btrfs_map_bh_to_logical(root, eb, eb->start);
130 ret = read_extent_from_disk(eb);
131 if (ret) {
132 free_extent_buffer(eb);
133 return NULL;
135 btrfs_set_buffer_uptodate(eb);
137 return eb;
140 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
141 struct extent_buffer *eb)
143 if (check_tree_block(root, eb))
144 BUG();
145 if (!btrfs_buffer_uptodate(eb))
146 BUG();
147 btrfs_map_bh_to_logical(root, eb, eb->start);
148 csum_tree_block(root, eb, 0);
149 return write_extent_to_disk(eb);
152 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
153 u32 stripesize, struct btrfs_root *root,
154 struct btrfs_fs_info *fs_info, u64 objectid)
156 root->node = NULL;
157 root->commit_root = NULL;
158 root->sectorsize = sectorsize;
159 root->nodesize = nodesize;
160 root->leafsize = leafsize;
161 root->stripesize = stripesize;
162 root->ref_cows = 0;
163 root->track_dirty = 0;
165 root->fs_info = fs_info;
166 root->objectid = objectid;
167 root->last_trans = 0;
168 root->highest_inode = 0;
169 root->last_inode_alloc = 0;
171 INIT_LIST_HEAD(&root->dirty_list);
172 memset(&root->root_key, 0, sizeof(root->root_key));
173 memset(&root->root_item, 0, sizeof(root->root_item));
174 root->root_key.objectid = objectid;
175 return 0;
178 static int update_cowonly_root(struct btrfs_trans_handle *trans,
179 struct btrfs_root *root)
181 int ret;
182 u64 old_root_bytenr;
183 struct btrfs_root *tree_root = root->fs_info->tree_root;
185 btrfs_write_dirty_block_groups(trans, root);
186 while(1) {
187 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
188 if (old_root_bytenr == root->node->start)
189 break;
190 btrfs_set_root_bytenr(&root->root_item,
191 root->node->start);
192 root->root_item.level = btrfs_header_level(root->node);
193 ret = btrfs_update_root(trans, tree_root,
194 &root->root_key,
195 &root->root_item);
196 BUG_ON(ret);
197 btrfs_write_dirty_block_groups(trans, root);
199 return 0;
202 static int commit_tree_roots(struct btrfs_trans_handle *trans,
203 struct btrfs_fs_info *fs_info)
205 struct btrfs_root *root;
206 struct list_head *next;
208 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
209 next = fs_info->dirty_cowonly_roots.next;
210 list_del_init(next);
211 root = list_entry(next, struct btrfs_root, dirty_list);
212 update_cowonly_root(trans, root);
214 return 0;
217 static int __commit_transaction(struct btrfs_trans_handle *trans,
218 struct btrfs_root *root)
220 u64 start;
221 u64 end;
222 struct extent_buffer *eb;
223 struct extent_io_tree *tree = &root->fs_info->extent_cache;
224 int ret;
226 while(1) {
227 ret = find_first_extent_bit(tree, 0, &start, &end,
228 EXTENT_DIRTY);
229 if (ret)
230 break;
231 while(start <= end) {
232 eb = find_first_extent_buffer(tree, start);
233 BUG_ON(!eb || eb->start != start);
234 ret = write_tree_block(trans, root, eb);
235 BUG_ON(ret);
236 start += eb->len;
237 clear_extent_buffer_dirty(eb);
238 free_extent_buffer(eb);
241 return 0;
244 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
245 struct btrfs_root *root)
247 int ret = 0;
248 struct btrfs_root *new_root = NULL;
249 struct btrfs_fs_info *fs_info = root->fs_info;
251 if (root->commit_root == root->node)
252 goto commit_tree;
254 new_root = malloc(sizeof(*new_root));
255 if (!new_root)
256 return -ENOMEM;
257 memcpy(new_root, root, sizeof(*new_root));
258 new_root->node = root->commit_root;
259 root->commit_root = NULL;
261 root->root_key.offset = trans->transid;
262 btrfs_set_root_bytenr(&root->root_item, root->node->start);
263 root->root_item.level = btrfs_header_level(root->node);
264 ret = btrfs_insert_root(trans, fs_info->tree_root,
265 &root->root_key, &root->root_item);
266 BUG_ON(ret);
268 btrfs_set_root_refs(&new_root->root_item, 0);
269 ret = btrfs_update_root(trans, root->fs_info->tree_root,
270 &new_root->root_key, &new_root->root_item);
271 BUG_ON(ret);
273 ret = commit_tree_roots(trans, fs_info);
274 BUG_ON(ret);
275 ret = __commit_transaction(trans, root);
276 BUG_ON(ret);
277 write_ctree_super(trans, root);
278 btrfs_finish_extent_commit(trans, fs_info->extent_root,
279 &fs_info->pinned_extents);
280 btrfs_free_transaction(root, trans);
281 fs_info->running_transaction = NULL;
283 trans = btrfs_start_transaction(root, 1);
284 ret = btrfs_drop_snapshot(trans, new_root);
285 BUG_ON(ret);
286 ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key);
287 BUG_ON(ret);
288 commit_tree:
289 ret = commit_tree_roots(trans, fs_info);
290 BUG_ON(ret);
291 ret = __commit_transaction(trans, root);
292 BUG_ON(ret);
293 write_ctree_super(trans, root);
294 btrfs_finish_extent_commit(trans, fs_info->extent_root,
295 &fs_info->pinned_extents);
296 btrfs_free_transaction(root, trans);
297 free_extent_buffer(root->commit_root);
298 root->commit_root = NULL;
299 fs_info->running_transaction = NULL;
300 if (new_root) {
301 free_extent_buffer(new_root->node);
302 free(new_root);
304 return 0;
307 static int find_and_setup_root(struct btrfs_root *tree_root,
308 struct btrfs_fs_info *fs_info,
309 u64 objectid, struct btrfs_root *root)
311 int ret;
312 u32 blocksize;
314 __setup_root(tree_root->nodesize, tree_root->leafsize,
315 tree_root->sectorsize, tree_root->stripesize,
316 root, fs_info, objectid);
317 ret = btrfs_find_last_root(tree_root, objectid,
318 &root->root_item, &root->root_key);
319 BUG_ON(ret);
321 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
322 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
323 blocksize);
324 BUG_ON(!root->node);
325 return 0;
328 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
330 if (root->node)
331 free_extent_buffer(root->node);
332 if (root->commit_root)
333 free_extent_buffer(root->commit_root);
335 free(root);
336 return 0;
339 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
340 struct btrfs_key *location)
342 struct btrfs_root *root;
343 struct btrfs_root *tree_root = fs_info->tree_root;
344 struct btrfs_path *path;
345 struct extent_buffer *l;
346 u32 blocksize;
347 int ret = 0;
349 root = malloc(sizeof(*root));
350 if (!root)
351 return ERR_PTR(-ENOMEM);
352 memset(root, 0, sizeof(*root));
353 if (location->offset == (u64)-1) {
354 ret = find_and_setup_root(tree_root, fs_info,
355 location->objectid, root);
356 if (ret) {
357 free(root);
358 return ERR_PTR(ret);
360 goto insert;
363 __setup_root(tree_root->nodesize, tree_root->leafsize,
364 tree_root->sectorsize, tree_root->stripesize,
365 root, fs_info, location->objectid);
367 path = btrfs_alloc_path();
368 BUG_ON(!path);
369 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
370 if (ret != 0) {
371 if (ret > 0)
372 ret = -ENOENT;
373 goto out;
375 l = path->nodes[0];
376 read_extent_buffer(l, &root->root_item,
377 btrfs_item_ptr_offset(l, path->slots[0]),
378 sizeof(root->root_item));
379 memcpy(&root->root_key, location, sizeof(*location));
380 ret = 0;
381 out:
382 btrfs_release_path(root, path);
383 btrfs_free_path(path);
384 if (ret) {
385 free(root);
386 return ERR_PTR(ret);
388 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
389 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
390 blocksize);
391 BUG_ON(!root->node);
392 insert:
393 root->ref_cows = 1;
394 return root;
397 struct btrfs_root *open_ctree(char *filename, u64 sb_bytenr)
399 int fp;
401 fp = open(filename, O_CREAT | O_RDWR, 0600);
402 if (fp < 0) {
403 return NULL;
405 return open_ctree_fd(fp, sb_bytenr);
408 struct btrfs_root *open_ctree_fd(int fp, u64 sb_bytenr)
410 u32 sectorsize;
411 u32 nodesize;
412 u32 leafsize;
413 u32 blocksize;
414 u32 stripesize;
415 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
416 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
417 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
418 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
419 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
420 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
421 int ret;
422 struct btrfs_super_block *disk_super;
424 if (sb_bytenr == 0)
425 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
427 fs_info->fp = fp;
428 fs_info->running_transaction = NULL;
429 fs_info->fs_root = root;
430 fs_info->tree_root = tree_root;
431 fs_info->extent_root = extent_root;
432 fs_info->extent_ops = NULL;
433 fs_info->priv_data = NULL;
434 fs_info->chunk_root = chunk_root;
435 fs_info->dev_root = dev_root;
436 fs_info->force_system_allocs = 0;
438 extent_io_tree_init(&fs_info->extent_cache);
439 extent_io_tree_init(&fs_info->free_space_cache);
440 extent_io_tree_init(&fs_info->block_group_cache);
441 extent_io_tree_init(&fs_info->pinned_extents);
442 extent_io_tree_init(&fs_info->pending_del);
443 extent_io_tree_init(&fs_info->extent_ins);
445 cache_tree_init(&fs_info->mapping_tree.cache_tree);
447 mutex_init(&fs_info->fs_mutex);
448 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
449 INIT_LIST_HEAD(&fs_info->devices);
450 INIT_LIST_HEAD(&fs_info->space_info);
452 __setup_root(4096, 4096, 4096, 4096, tree_root,
453 fs_info, BTRFS_ROOT_TREE_OBJECTID);
455 fs_info->sb_buffer = btrfs_find_create_tree_block(tree_root, sb_bytenr,
456 4096);
457 BUG_ON(!fs_info->sb_buffer);
458 fs_info->sb_buffer->fd = fp;
459 fs_info->sb_buffer->dev_bytenr = sb_bytenr;
460 ret = read_extent_from_disk(fs_info->sb_buffer);
461 BUG_ON(ret);
462 btrfs_set_buffer_uptodate(fs_info->sb_buffer);
464 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
465 sizeof(fs_info->super_copy));
466 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
467 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
468 BTRFS_FSID_SIZE);
470 disk_super = &fs_info->super_copy;
471 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
472 sizeof(disk_super->magic))) {
473 printk("No valid btrfs found\n");
474 BUG_ON(1);
476 nodesize = btrfs_super_nodesize(disk_super);
477 leafsize = btrfs_super_leafsize(disk_super);
478 sectorsize = btrfs_super_sectorsize(disk_super);
479 stripesize = btrfs_super_stripesize(disk_super);
480 tree_root->nodesize = nodesize;
481 tree_root->leafsize = leafsize;
482 tree_root->sectorsize = sectorsize;
483 tree_root->stripesize = stripesize;
485 ret = btrfs_read_super_device(tree_root, fs_info->sb_buffer);
486 BUG_ON(ret);
488 ret = btrfs_read_sys_array(tree_root);
489 BUG_ON(ret);
490 blocksize = btrfs_level_size(tree_root,
491 btrfs_super_chunk_root_level(disk_super));
493 __setup_root(nodesize, leafsize, sectorsize, stripesize,
494 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
495 chunk_root->node = read_tree_block(chunk_root,
496 btrfs_super_chunk_root(disk_super),
497 blocksize);
499 BUG_ON(!chunk_root->node);
500 ret = btrfs_read_chunk_tree(chunk_root);
501 BUG_ON(ret);
503 blocksize = btrfs_level_size(tree_root,
504 btrfs_super_root_level(disk_super));
506 tree_root->node = read_tree_block(tree_root,
507 btrfs_super_root(disk_super),
508 blocksize);
509 BUG_ON(!tree_root->node);
510 ret = find_and_setup_root(tree_root, fs_info,
511 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
512 BUG_ON(ret);
513 extent_root->track_dirty = 1;
515 ret = find_and_setup_root(tree_root, fs_info,
516 BTRFS_DEV_TREE_OBJECTID, dev_root);
517 BUG_ON(ret);
518 dev_root->track_dirty = 1;
520 ret = find_and_setup_root(tree_root, fs_info,
521 BTRFS_FS_TREE_OBJECTID, root);
522 BUG_ON(ret);
523 root->ref_cows = 1;
524 fs_info->generation = btrfs_super_generation(disk_super) + 1;
525 btrfs_read_block_groups(root);
526 return root;
529 int write_ctree_super(struct btrfs_trans_handle *trans,
530 struct btrfs_root *root)
532 int ret;
533 struct btrfs_root *tree_root = root->fs_info->tree_root;
534 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
535 btrfs_set_super_generation(&root->fs_info->super_copy,
536 trans->transid);
537 btrfs_set_super_root(&root->fs_info->super_copy,
538 tree_root->node->start);
539 btrfs_set_super_root_level(&root->fs_info->super_copy,
540 btrfs_header_level(tree_root->node));
541 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
542 chunk_root->node->start);
543 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
544 btrfs_header_level(chunk_root->node));
545 write_extent_buffer(root->fs_info->sb_buffer,
546 &root->fs_info->super_copy, 0,
547 sizeof(root->fs_info->super_copy));
548 ret = write_tree_block(trans, root, root->fs_info->sb_buffer);
549 if (ret)
550 fprintf(stderr, "failed to write new super block err %d\n", ret);
551 return ret;
554 static int close_all_devices(struct btrfs_fs_info *fs_info)
556 struct list_head *list;
557 struct list_head *next;
558 struct btrfs_device *device;
560 list = &fs_info->devices;
561 while(!list_empty(list)) {
562 next = list->next;
563 list_del(next);
564 device = list_entry(next, struct btrfs_device, dev_list);
565 // close(device->fd);
566 kfree(device);
568 return 0;
571 int close_ctree(struct btrfs_root *root)
573 int ret;
574 struct btrfs_trans_handle *trans;
575 struct btrfs_fs_info *fs_info = root->fs_info;
577 trans = btrfs_start_transaction(root, 1);
578 btrfs_commit_transaction(trans, root);
579 trans = btrfs_start_transaction(root, 1);
580 ret = commit_tree_roots(trans, root->fs_info);
581 BUG_ON(ret);
582 ret = __commit_transaction(trans, root);
583 BUG_ON(ret);
584 write_ctree_super(trans, root);
585 btrfs_free_transaction(root, trans);
586 btrfs_free_block_groups(root->fs_info);
587 close(root->fs_info->fp);
588 if (root->node)
589 free_extent_buffer(root->node);
590 if (root->fs_info->extent_root->node)
591 free_extent_buffer(root->fs_info->extent_root->node);
592 if (root->fs_info->tree_root->node)
593 free_extent_buffer(root->fs_info->tree_root->node);
594 free_extent_buffer(root->commit_root);
595 free_extent_buffer(root->fs_info->sb_buffer);
597 if (root->fs_info->chunk_root->node);
598 free_extent_buffer(root->fs_info->chunk_root->node);
600 if (root->fs_info->dev_root->node);
601 free_extent_buffer(root->fs_info->dev_root->node);
603 close_all_devices(root->fs_info);
604 extent_io_tree_cleanup(&fs_info->extent_cache);
605 extent_io_tree_cleanup(&fs_info->free_space_cache);
606 extent_io_tree_cleanup(&fs_info->block_group_cache);
607 extent_io_tree_cleanup(&fs_info->pinned_extents);
608 extent_io_tree_cleanup(&fs_info->pending_del);
609 extent_io_tree_cleanup(&fs_info->extent_ins);
611 free(fs_info->tree_root);
612 free(fs_info->extent_root);
613 free(fs_info->fs_root);
614 free(fs_info->chunk_root);
615 free(fs_info->dev_root);
616 free(fs_info);
618 return 0;
621 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
622 struct extent_buffer *eb)
624 return clear_extent_buffer_dirty(eb);
627 int wait_on_tree_block_writeback(struct btrfs_root *root,
628 struct extent_buffer *eb)
630 return 0;
633 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
635 set_extent_buffer_dirty(eb);
638 int btrfs_buffer_uptodate(struct extent_buffer *eb)
640 return extent_buffer_uptodate(eb);
643 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
645 return set_extent_buffer_uptodate(eb);