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
23 #include <sys/types.h>
27 #include "kerncompat.h"
28 #include "radix-tree.h"
31 #include "transaction.h"
34 static int allocated_blocks = 0;
35 int cache_max = 10000;
37 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct btrfs_buffer *bh,
40 bh->fd = root->fs_info->fp;
41 bh->dev_blocknr = logical;
45 static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
47 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
49 if (memcmp(root->fs_info->disk_super->fsid, buf->node.header.fsid,
50 sizeof(buf->node.header.fsid)))
55 static int free_some_buffers(struct btrfs_root *root)
57 struct list_head *node, *next;
58 struct btrfs_buffer *b;
59 if (root->fs_info->cache_size < cache_max)
61 list_for_each_safe(node, next, &root->fs_info->cache) {
62 b = list_entry(node, struct btrfs_buffer, cache);
64 BUG_ON(!list_empty(&b->dirty));
65 list_del_init(&b->cache);
66 btrfs_block_release(root, b);
67 if (root->fs_info->cache_size < cache_max)
74 struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
76 struct btrfs_buffer *buf;
79 buf = malloc(sizeof(struct btrfs_buffer) + root->sectorsize);
83 buf->blocknr = blocknr;
85 INIT_LIST_HEAD(&buf->dirty);
86 free_some_buffers(root);
87 radix_tree_preload(GFP_KERNEL);
88 ret = radix_tree_insert(&root->fs_info->cache_radix, blocknr, buf);
89 radix_tree_preload_end();
90 list_add_tail(&buf->cache, &root->fs_info->cache);
91 root->fs_info->cache_size++;
99 struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)
101 struct btrfs_buffer *buf;
102 buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
106 buf = alloc_tree_block(root, blocknr);
115 struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
117 struct btrfs_buffer *buf;
119 buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
122 if (check_tree_block(root, buf))
125 buf = alloc_tree_block(root, blocknr);
128 btrfs_map_bh_to_logical(root, buf, blocknr);
129 ret = pread(buf->fd, &buf->node, root->sectorsize,
130 buf->dev_blocknr * root->sectorsize);
131 if (ret != root->sectorsize) {
135 if (check_tree_block(root, buf))
141 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
142 struct btrfs_buffer *buf)
144 if (!list_empty(&buf->dirty))
146 list_add_tail(&buf->dirty, &root->fs_info->trans);
148 if (check_tree_block(root, buf))
153 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
154 struct btrfs_buffer *buf)
156 if (!list_empty(&buf->dirty)) {
157 list_del_init(&buf->dirty);
158 btrfs_block_release(root, buf);
163 int btrfs_csum_node(struct btrfs_root *root, struct btrfs_node *node)
166 size_t len = root->sectorsize - BTRFS_CSUM_SIZE;
168 crc = crc32c(0, (char *)(node) + BTRFS_CSUM_SIZE, len);
169 memcpy(node->header.csum, &crc, BTRFS_CRC32_SIZE);
173 int btrfs_csum_super(struct btrfs_root *root, struct btrfs_super_block *super)
176 char block[root->sectorsize];
177 size_t len = root->sectorsize - BTRFS_CSUM_SIZE;
179 memset(block, 0, root->sectorsize);
180 memcpy(block, super, sizeof(*super));
182 crc = crc32c(0, block + BTRFS_CSUM_SIZE, len);
183 memcpy(super->csum, &crc, BTRFS_CRC32_SIZE);
187 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
188 struct btrfs_buffer *buf)
192 if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
194 btrfs_map_bh_to_logical(root, buf, buf->blocknr);
195 if (check_tree_block(root, buf))
198 btrfs_csum_node(root, &buf->node);
200 ret = pwrite(buf->fd, &buf->node, root->sectorsize,
201 buf->dev_blocknr * root->sectorsize);
202 if (ret != root->sectorsize)
207 static int __commit_transaction(struct btrfs_trans_handle *trans, struct
210 struct btrfs_buffer *b;
213 while(!list_empty(&root->fs_info->trans)) {
214 b = list_entry(root->fs_info->trans.next, struct btrfs_buffer,
216 list_del_init(&b->dirty);
217 wret = write_tree_block(trans, root, b);
220 btrfs_block_release(root, b);
225 static int commit_tree_roots(struct btrfs_trans_handle *trans,
226 struct btrfs_fs_info *fs_info)
229 u64 old_extent_block;
230 struct btrfs_root *tree_root = fs_info->tree_root;
231 struct btrfs_root *extent_root = fs_info->extent_root;
233 btrfs_write_dirty_block_groups(trans, fs_info->extent_root);
235 old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
236 if (old_extent_block == extent_root->node->blocknr)
238 btrfs_set_root_blocknr(&extent_root->root_item,
239 extent_root->node->blocknr);
240 ret = btrfs_update_root(trans, tree_root,
241 &extent_root->root_key,
242 &extent_root->root_item);
244 btrfs_write_dirty_block_groups(trans, fs_info->extent_root);
249 int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
250 btrfs_root *root, struct btrfs_super_block *s)
253 struct btrfs_buffer *snap = root->commit_root;
254 struct btrfs_key snap_key;
256 if (root->commit_root == root->node)
259 memcpy(&snap_key, &root->root_key, sizeof(snap_key));
260 root->root_key.offset++;
262 btrfs_set_root_blocknr(&root->root_item, root->node->blocknr);
263 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
264 &root->root_key, &root->root_item);
267 ret = commit_tree_roots(trans, root->fs_info);
270 ret = __commit_transaction(trans, root);
273 write_ctree_super(trans, root, s);
274 btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
275 btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
277 root->commit_root = root->node;
279 ret = btrfs_drop_snapshot(trans, root, snap);
282 ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
284 root->fs_info->generation = root->root_key.offset + 1;
289 static int __setup_root(struct btrfs_super_block *super,
290 struct btrfs_root *root,
291 struct btrfs_fs_info *fs_info,
292 u64 objectid, int fp)
295 root->commit_root = NULL;
296 root->sectorsize = btrfs_super_sectorsize(super);
297 root->nodesize = btrfs_super_nodesize(super);
298 root->leafsize = btrfs_super_leafsize(super);
300 root->fs_info = fs_info;
301 memset(&root->root_key, 0, sizeof(root->root_key));
302 memset(&root->root_item, 0, sizeof(root->root_item));
303 root->root_key.objectid = objectid;
307 static int find_and_setup_root(struct btrfs_super_block *super,
308 struct btrfs_root *tree_root,
309 struct btrfs_fs_info *fs_info,
311 struct btrfs_root *root, int fp)
315 __setup_root(super, root, fs_info, objectid, fp);
316 ret = btrfs_find_last_root(tree_root, objectid,
317 &root->root_item, &root->root_key);
320 root->node = read_tree_block(root,
321 btrfs_root_blocknr(&root->root_item));
326 struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
330 fp = open(filename, O_CREAT | O_RDWR, 0600);
334 return open_ctree_fd(fp, super);
337 struct btrfs_root *open_ctree_fd(int fp, struct btrfs_super_block *super)
339 struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
340 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
341 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
342 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
345 INIT_RADIX_TREE(&fs_info->cache_radix, GFP_KERNEL);
346 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
347 INIT_LIST_HEAD(&fs_info->trans);
348 INIT_LIST_HEAD(&fs_info->cache);
349 pending_tree_init(&fs_info->pending_tree);
350 pending_tree_init(&fs_info->pinned_tree);
351 pending_tree_init(&fs_info->del_pending);
352 fs_info->cache_size = 0;
354 fs_info->running_transaction = NULL;
355 fs_info->fs_root = root;
356 fs_info->tree_root = tree_root;
357 fs_info->extent_root = extent_root;
358 fs_info->last_inode_alloc = 0;
359 fs_info->last_inode_alloc_dirid = 0;
360 fs_info->disk_super = super;
361 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
363 ret = pread(fp, super, sizeof(struct btrfs_super_block),
364 BTRFS_SUPER_INFO_OFFSET);
365 if (ret == 0 || btrfs_super_root(super) == 0) {
371 __setup_root(super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID, fp);
372 tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
373 BUG_ON(!tree_root->node);
375 ret = find_and_setup_root(super, tree_root, fs_info,
376 BTRFS_EXTENT_TREE_OBJECTID, extent_root, fp);
379 ret = find_and_setup_root(super, tree_root, fs_info,
380 BTRFS_FS_TREE_OBJECTID, root, fp);
383 root->commit_root = root->node;
386 root->fs_info->generation = root->root_key.offset + 1;
387 btrfs_read_block_groups(root);
391 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
392 *root, struct btrfs_super_block *s)
396 btrfs_set_super_root(s, root->fs_info->tree_root->node->blocknr);
397 btrfs_csum_super(root, s);
399 ret = pwrite(root->fs_info->fp, s, sizeof(*s),
400 BTRFS_SUPER_INFO_OFFSET);
401 if (ret != sizeof(*s)) {
402 fprintf(stderr, "failed to write new super block err %d\n", ret);
408 static int drop_cache(struct btrfs_root *root)
410 while(!list_empty(&root->fs_info->cache)) {
411 struct btrfs_buffer *b = list_entry(root->fs_info->cache.next,
414 list_del_init(&b->cache);
415 btrfs_block_release(root, b);
420 int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
423 struct btrfs_trans_handle *trans;
425 trans = root->fs_info->running_transaction;
426 btrfs_commit_transaction(trans, root, s);
427 ret = commit_tree_roots(trans, root->fs_info);
429 ret = __commit_transaction(trans, root);
431 write_ctree_super(trans, root, s);
433 BUG_ON(!list_empty(&root->fs_info->trans));
435 btrfs_free_block_groups(root->fs_info);
436 close(root->fs_info->fp);
438 btrfs_block_release(root, root->node);
439 if (root->fs_info->extent_root->node)
440 btrfs_block_release(root->fs_info->extent_root,
441 root->fs_info->extent_root->node);
442 if (root->fs_info->tree_root->node)
443 btrfs_block_release(root->fs_info->tree_root,
444 root->fs_info->tree_root->node);
445 btrfs_block_release(root, root->commit_root);
447 printf("on close %d blocks are allocated\n", allocated_blocks);
451 void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
456 if (buf->count == 0) {
457 BUG_ON(!list_empty(&buf->cache));
458 BUG_ON(!list_empty(&buf->dirty));
459 if (!radix_tree_lookup(&root->fs_info->cache_radix,
462 radix_tree_delete(&root->fs_info->cache_radix, buf->blocknr);
463 memset(buf, 0, sizeof(*buf));
465 BUG_ON(allocated_blocks == 0);
467 BUG_ON(root->fs_info->cache_size == 0);
468 root->fs_info->cache_size--;