1 #include <linux/module.h>
5 #include "transaction.h"
7 static int check_tree_block(struct btrfs_root
*root
, struct buffer_head
*buf
)
9 struct btrfs_node
*node
= btrfs_buffer_node(buf
);
10 if (buf
->b_blocknr
!= btrfs_header_blocknr(&node
->header
))
12 if (root
->node
&& btrfs_header_parentid(&node
->header
) !=
13 btrfs_header_parentid(btrfs_buffer_header(root
->node
)))
18 struct buffer_head
*alloc_tree_block(struct btrfs_root
*root
, u64 blocknr
)
20 return sb_getblk(root
->fs_info
->sb
, blocknr
);
23 struct buffer_head
*find_tree_block(struct btrfs_root
*root
, u64 blocknr
)
25 return sb_getblk(root
->fs_info
->sb
, blocknr
);
28 struct buffer_head
*read_tree_block(struct btrfs_root
*root
, u64 blocknr
)
30 struct buffer_head
*buf
= sb_bread(root
->fs_info
->sb
, blocknr
);
34 if (check_tree_block(root
, buf
))
39 int dirty_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
40 struct buffer_head
*buf
)
42 mark_buffer_dirty(buf
);
46 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
47 struct buffer_head
*buf
)
49 clear_buffer_dirty(buf
);
53 int write_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
54 struct buffer_head
*buf
)
56 mark_buffer_dirty(buf
);
60 static int __commit_transaction(struct btrfs_trans_handle
*trans
, struct
63 filemap_write_and_wait(root
->fs_info
->sb
->s_bdev
->bd_inode
->i_mapping
);
67 static int commit_tree_roots(struct btrfs_trans_handle
*trans
,
68 struct btrfs_fs_info
*fs_info
)
72 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
73 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
74 struct btrfs_root
*inode_root
= fs_info
->inode_root
;
76 btrfs_set_root_blocknr(&inode_root
->root_item
,
77 inode_root
->node
->b_blocknr
);
78 ret
= btrfs_update_root(trans
, tree_root
,
79 &inode_root
->root_key
,
80 &inode_root
->root_item
);
83 old_extent_block
= btrfs_root_blocknr(&extent_root
->root_item
);
84 if (old_extent_block
== extent_root
->node
->b_blocknr
)
86 btrfs_set_root_blocknr(&extent_root
->root_item
,
87 extent_root
->node
->b_blocknr
);
88 ret
= btrfs_update_root(trans
, tree_root
,
89 &extent_root
->root_key
,
90 &extent_root
->root_item
);
96 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
, struct
97 btrfs_root
*root
, struct btrfs_super_block
*s
)
100 struct buffer_head
*snap
= root
->commit_root
;
101 struct btrfs_key snap_key
;
103 if (root
->commit_root
== root
->node
)
106 memcpy(&snap_key
, &root
->root_key
, sizeof(snap_key
));
107 root
->root_key
.offset
++;
109 btrfs_set_root_blocknr(&root
->root_item
, root
->node
->b_blocknr
);
110 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
111 &root
->root_key
, &root
->root_item
);
114 ret
= commit_tree_roots(trans
, root
->fs_info
);
117 ret
= __commit_transaction(trans
, root
);
120 write_ctree_super(trans
, root
, s
);
121 btrfs_finish_extent_commit(trans
, root
->fs_info
->extent_root
);
122 btrfs_finish_extent_commit(trans
, root
->fs_info
->tree_root
);
124 root
->commit_root
= root
->node
;
126 ret
= btrfs_drop_snapshot(trans
, root
, snap
);
129 ret
= btrfs_del_root(trans
, root
->fs_info
->tree_root
, &snap_key
);
131 root
->fs_info
->generation
= root
->root_key
.offset
+ 1;
136 static int __setup_root(struct btrfs_super_block
*super
,
137 struct btrfs_root
*root
,
138 struct btrfs_fs_info
*fs_info
,
142 root
->commit_root
= NULL
;
143 root
->blocksize
= btrfs_super_blocksize(super
);
145 root
->fs_info
= fs_info
;
146 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
147 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
151 static int find_and_setup_root(struct btrfs_super_block
*super
,
152 struct btrfs_root
*tree_root
,
153 struct btrfs_fs_info
*fs_info
,
155 struct btrfs_root
*root
)
159 __setup_root(super
, root
, fs_info
, objectid
);
160 ret
= btrfs_find_last_root(tree_root
, objectid
,
161 &root
->root_item
, &root
->root_key
);
164 root
->node
= read_tree_block(root
,
165 btrfs_root_blocknr(&root
->root_item
));
170 struct btrfs_root
*open_ctree(struct super_block
*sb
,
171 struct buffer_head
*sb_buffer
,
172 struct btrfs_super_block
*disk_super
)
174 struct btrfs_root
*root
= kmalloc(sizeof(struct btrfs_root
),
176 struct btrfs_root
*extent_root
= kmalloc(sizeof(struct btrfs_root
),
178 struct btrfs_root
*tree_root
= kmalloc(sizeof(struct btrfs_root
),
180 struct btrfs_root
*inode_root
= kmalloc(sizeof(struct btrfs_root
),
182 struct btrfs_fs_info
*fs_info
= kmalloc(sizeof(*fs_info
),
186 /* FIXME: don't be stupid */
187 if (!btrfs_super_root(disk_super
))
189 INIT_RADIX_TREE(&fs_info
->pinned_radix
, GFP_KERNEL
);
190 fs_info
->running_transaction
= NULL
;
191 fs_info
->fs_root
= root
;
192 fs_info
->tree_root
= tree_root
;
193 fs_info
->extent_root
= extent_root
;
194 fs_info
->inode_root
= inode_root
;
195 fs_info
->last_inode_alloc
= 0;
196 fs_info
->last_inode_alloc_dirid
= 0;
197 fs_info
->disk_super
= disk_super
;
198 fs_info
->sb_buffer
= sb_buffer
;
200 memset(&fs_info
->current_insert
, 0, sizeof(fs_info
->current_insert
));
201 memset(&fs_info
->last_insert
, 0, sizeof(fs_info
->last_insert
));
203 __setup_root(disk_super
, tree_root
, fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
204 tree_root
->node
= read_tree_block(tree_root
,
205 btrfs_super_root(disk_super
));
206 BUG_ON(!tree_root
->node
);
208 ret
= find_and_setup_root(disk_super
, tree_root
, fs_info
,
209 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
212 ret
= find_and_setup_root(disk_super
, tree_root
, fs_info
,
213 BTRFS_INODE_MAP_OBJECTID
, inode_root
);
216 ret
= find_and_setup_root(disk_super
, tree_root
, fs_info
,
217 BTRFS_FS_TREE_OBJECTID
, root
);
220 root
->commit_root
= root
->node
;
223 root
->fs_info
->generation
= root
->root_key
.offset
+ 1;
227 int write_ctree_super(struct btrfs_trans_handle
*trans
, struct btrfs_root
228 *root
, struct btrfs_super_block
*s
)
233 btrfs_set_super_root(s
, root
->fs_info
->tree_root
->node
->b_blocknr
);
235 ret
= pwrite(root
->fs_info
->fp
, s
, sizeof(*s
),
236 BTRFS_SUPER_INFO_OFFSET
);
237 if (ret
!= sizeof(*s
)) {
238 fprintf(stderr
, "failed to write new super block err %d\n", ret
);
245 static int drop_cache(struct btrfs_root
*root
)
249 while(!list_empty(&root
->fs_info
->cache
)) {
250 struct buffer_head
*b
= list_entry(root
->fs_info
->cache
.next
,
253 list_del_init(&b
->cache
);
254 btrfs_block_release(root
, b
);
260 int close_ctree(struct btrfs_root
*root
)
263 struct btrfs_trans_handle
*trans
;
265 trans
= root
->fs_info
->running_transaction
;
266 btrfs_commit_transaction(trans
, root
, root
->fs_info
->disk_super
);
267 ret
= commit_tree_roots(trans
, root
->fs_info
);
269 ret
= __commit_transaction(trans
, root
);
271 write_ctree_super(trans
, root
, root
->fs_info
->disk_super
);
275 btrfs_block_release(root
, root
->node
);
276 if (root
->fs_info
->extent_root
->node
)
277 btrfs_block_release(root
->fs_info
->extent_root
,
278 root
->fs_info
->extent_root
->node
);
279 if (root
->fs_info
->inode_root
->node
)
280 btrfs_block_release(root
->fs_info
->inode_root
,
281 root
->fs_info
->inode_root
->node
);
282 if (root
->fs_info
->tree_root
->node
)
283 btrfs_block_release(root
->fs_info
->tree_root
,
284 root
->fs_info
->tree_root
->node
);
285 btrfs_block_release(root
, root
->commit_root
);
286 btrfs_block_release(root
, root
->fs_info
->sb_buffer
);
287 kfree(root
->fs_info
->extent_root
);
288 kfree(root
->fs_info
->inode_root
);
289 kfree(root
->fs_info
->tree_root
);
290 kfree(root
->fs_info
);
295 void btrfs_block_release(struct btrfs_root
*root
, struct buffer_head
*buf
)