Remove extent back refs in batches, and avoid duplicate searches
[btrfs-progs-unstable/devel.git] / disk-io.c
blob0957c633a0df39af75328ead97b90111f6cb455a
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 "transaction.h"
32 #include "crc32c.h"
34 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *buf,
35 u64 logical)
37 buf->fd = root->fs_info->fp;
38 buf->dev_bytenr = logical;
39 return 0;
42 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
44 if (buf->start != btrfs_header_bytenr(buf))
45 BUG();
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;
54 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
56 return crc32c(seed, data, len);
59 void btrfs_csum_final(u32 crc, char *result)
61 *(__le32 *)result = ~cpu_to_le32(crc);
64 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
65 int verify)
67 char result[BTRFS_CRC32_SIZE];
68 u32 len;
69 u32 crc = ~(u32)0;
71 len = buf->len - BTRFS_CSUM_SIZE;
72 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
73 btrfs_csum_final(crc, result);
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;
80 } else {
81 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
83 return 0;
86 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
87 u64 bytenr, u32 blocksize)
89 return find_extent_buffer(&root->fs_info->extent_cache,
90 bytenr, blocksize);
93 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
94 u64 bytenr, u32 blocksize)
96 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
97 blocksize);
100 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
102 return 0;
105 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
106 u32 blocksize)
108 int ret;
109 struct extent_buffer *eb;
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;
121 btrfs_set_buffer_uptodate(eb);
123 return eb;
126 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
127 struct extent_buffer *eb)
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);
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)
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;
160 static int commit_tree_roots(struct btrfs_trans_handle *trans,
161 struct btrfs_fs_info *fs_info)
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;
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);
183 return 0;
186 static int __commit_transaction(struct btrfs_trans_handle *trans,
187 struct btrfs_root *root)
189 u64 start;
190 u64 end;
191 struct extent_buffer *eb;
192 struct extent_io_tree *tree = &root->fs_info->extent_cache;
193 int ret;
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);
210 return 0;
213 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
214 struct btrfs_root *root)
216 int ret = 0;
217 struct btrfs_root *new_root = NULL;
218 struct btrfs_fs_info *fs_info = root->fs_info;
220 if (root->commit_root == root->node)
221 goto commit_tree;
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;
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);
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);
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;
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);
273 return 0;
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)
280 int ret;
281 u32 blocksize;
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);
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;
297 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
299 if (root->node)
300 free_extent_buffer(root->node);
301 if (root->commit_root)
302 free_extent_buffer(root->commit_root);
304 free(root);
305 return 0;
308 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
309 struct btrfs_key *location)
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;
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);
329 goto insert;
332 __setup_root(tree_root->nodesize, tree_root->leafsize,
333 tree_root->sectorsize, tree_root->stripesize,
334 root, fs_info, location->objectid);
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;
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);
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;
366 struct btrfs_root *open_ctree(char *filename, u64 sb_bytenr)
368 int fp;
370 fp = open(filename, O_CREAT | O_RDWR, 0600);
371 if (fp < 0) {
372 return NULL;
374 return open_ctree_fd(fp, sb_bytenr);
377 struct btrfs_root *open_ctree_fd(int fp, u64 sb_bytenr)
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;
391 if (sb_bytenr == 0)
392 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
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_io_tree_init(&fs_info->extent_cache);
402 extent_io_tree_init(&fs_info->free_space_cache);
403 extent_io_tree_init(&fs_info->block_group_cache);
404 extent_io_tree_init(&fs_info->pinned_extents);
405 extent_io_tree_init(&fs_info->pending_del);
406 extent_io_tree_init(&fs_info->extent_ins);
408 mutex_init(&fs_info->fs_mutex);
410 __setup_root(512, 512, 512, 512, tree_root,
411 fs_info, BTRFS_ROOT_TREE_OBJECTID);
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);
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);
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;
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;
454 int write_ctree_super(struct btrfs_trans_handle *trans,
455 struct btrfs_root *root)
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;
474 int close_ctree(struct btrfs_root *root)
476 int ret;
477 struct btrfs_trans_handle *trans;
478 struct btrfs_fs_info *fs_info = root->fs_info;
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);
500 extent_io_tree_cleanup(&fs_info->extent_cache);
501 extent_io_tree_cleanup(&fs_info->free_space_cache);
502 extent_io_tree_cleanup(&fs_info->block_group_cache);
503 extent_io_tree_cleanup(&fs_info->pinned_extents);
504 extent_io_tree_cleanup(&fs_info->pending_del);
505 extent_io_tree_cleanup(&fs_info->extent_ins);
507 free(fs_info->tree_root);
508 free(fs_info->extent_root);
509 free(fs_info->fs_root);
510 free(fs_info);
512 return 0;
515 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
516 struct extent_buffer *eb)
518 return clear_extent_buffer_dirty(eb);
521 int wait_on_tree_block_writeback(struct btrfs_root *root,
522 struct extent_buffer *eb)
524 return 0;
527 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
529 set_extent_buffer_dirty(eb);
532 int btrfs_buffer_uptodate(struct extent_buffer *eb)
534 return extent_buffer_uptodate(eb);
537 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
539 return set_extent_buffer_uptodate(eb);