Commit | Line | Data |
---|---|---|
e20d96d6 CM |
1 | #include <linux/module.h> |
2 | #include <linux/fs.h> | |
d98237b3 | 3 | #include <linux/blkdev.h> |
87cbda5c CM |
4 | #include <linux/crypto.h> |
5 | #include <linux/scatterlist.h> | |
22b0ebda | 6 | #include <linux/swap.h> |
0f7d52f4 | 7 | #include <linux/radix-tree.h> |
eb60ceac CM |
8 | #include "ctree.h" |
9 | #include "disk-io.h" | |
e089f05c | 10 | #include "transaction.h" |
0f7d52f4 | 11 | #include "btrfs_inode.h" |
eb60ceac | 12 | |
e20d96d6 | 13 | static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf) |
eb60ceac | 14 | { |
e20d96d6 | 15 | struct btrfs_node *node = btrfs_buffer_node(buf); |
d98237b3 | 16 | if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) { |
9a8dd150 | 17 | BUG(); |
d98237b3 | 18 | } |
9a8dd150 | 19 | return 0; |
eb60ceac CM |
20 | } |
21 | ||
d98237b3 CM |
22 | struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr) |
23 | { | |
24 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; | |
25 | int blockbits = root->fs_info->sb->s_blocksize_bits; | |
26 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); | |
27 | struct page *page; | |
28 | struct buffer_head *bh; | |
29 | struct buffer_head *head; | |
30 | struct buffer_head *ret = NULL; | |
31 | ||
2c90e5d6 | 32 | |
d98237b3 CM |
33 | page = find_lock_page(mapping, index); |
34 | if (!page) | |
35 | return NULL; | |
36 | ||
37 | if (!page_has_buffers(page)) | |
38 | goto out_unlock; | |
39 | ||
40 | head = page_buffers(page); | |
41 | bh = head; | |
42 | do { | |
43 | if (buffer_mapped(bh) && bh->b_blocknr == blocknr) { | |
44 | ret = bh; | |
45 | get_bh(bh); | |
46 | goto out_unlock; | |
47 | } | |
48 | bh = bh->b_this_page; | |
49 | } while (bh != head); | |
50 | out_unlock: | |
51 | unlock_page(page); | |
d6025579 | 52 | if (ret) { |
22b0ebda | 53 | touch_buffer(ret); |
d6025579 | 54 | } |
d98237b3 CM |
55 | page_cache_release(page); |
56 | return ret; | |
57 | } | |
58 | ||
59 | struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root, | |
60 | u64 blocknr) | |
61 | { | |
62 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; | |
63 | int blockbits = root->fs_info->sb->s_blocksize_bits; | |
64 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); | |
65 | struct page *page; | |
66 | struct buffer_head *bh; | |
67 | struct buffer_head *head; | |
68 | struct buffer_head *ret = NULL; | |
69 | u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits); | |
22b0ebda | 70 | |
d98237b3 CM |
71 | page = grab_cache_page(mapping, index); |
72 | if (!page) | |
73 | return NULL; | |
74 | ||
d98237b3 CM |
75 | if (!page_has_buffers(page)) |
76 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0); | |
77 | head = page_buffers(page); | |
78 | bh = head; | |
79 | do { | |
80 | if (!buffer_mapped(bh)) { | |
81 | bh->b_bdev = root->fs_info->sb->s_bdev; | |
82 | bh->b_blocknr = first_block; | |
83 | set_buffer_mapped(bh); | |
84 | } | |
85 | if (bh->b_blocknr == blocknr) { | |
86 | ret = bh; | |
87 | get_bh(bh); | |
88 | goto out_unlock; | |
89 | } | |
90 | bh = bh->b_this_page; | |
91 | first_block++; | |
92 | } while (bh != head); | |
93 | out_unlock: | |
94 | unlock_page(page); | |
22b0ebda CM |
95 | if (ret) |
96 | touch_buffer(ret); | |
d98237b3 CM |
97 | page_cache_release(page); |
98 | return ret; | |
99 | } | |
100 | ||
101 | static sector_t max_block(struct block_device *bdev) | |
102 | { | |
103 | sector_t retval = ~((sector_t)0); | |
104 | loff_t sz = i_size_read(bdev->bd_inode); | |
105 | ||
106 | if (sz) { | |
107 | unsigned int size = block_size(bdev); | |
108 | unsigned int sizebits = blksize_bits(size); | |
109 | retval = (sz >> sizebits); | |
110 | } | |
111 | return retval; | |
112 | } | |
113 | ||
114 | static int btree_get_block(struct inode *inode, sector_t iblock, | |
115 | struct buffer_head *bh, int create) | |
116 | { | |
117 | if (iblock >= max_block(inode->i_sb->s_bdev)) { | |
118 | if (create) | |
119 | return -EIO; | |
120 | ||
121 | /* | |
122 | * for reads, we're just trying to fill a partial page. | |
123 | * return a hole, they will have to call get_block again | |
124 | * before they can fill it, and they will get -EIO at that | |
125 | * time | |
126 | */ | |
127 | return 0; | |
128 | } | |
129 | bh->b_bdev = inode->i_sb->s_bdev; | |
130 | bh->b_blocknr = iblock; | |
131 | set_buffer_mapped(bh); | |
132 | return 0; | |
133 | } | |
134 | ||
f254e52c CM |
135 | int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, |
136 | char *result) | |
87cbda5c | 137 | { |
87cbda5c CM |
138 | struct scatterlist sg; |
139 | struct crypto_hash *tfm = root->fs_info->hash_tfm; | |
140 | struct hash_desc desc; | |
141 | int ret; | |
87cbda5c CM |
142 | |
143 | desc.tfm = tfm; | |
144 | desc.flags = 0; | |
f254e52c | 145 | sg_init_one(&sg, data, len); |
87cbda5c | 146 | spin_lock(&root->fs_info->hash_lock); |
22b0ebda | 147 | ret = crypto_hash_digest(&desc, &sg, 1, result); |
87cbda5c CM |
148 | spin_unlock(&root->fs_info->hash_lock); |
149 | if (ret) { | |
150 | printk("sha256 digest failed\n"); | |
151 | } | |
f254e52c CM |
152 | return ret; |
153 | } | |
154 | static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, | |
155 | int verify) | |
156 | { | |
157 | char result[BTRFS_CSUM_SIZE]; | |
158 | int ret; | |
159 | struct btrfs_node *node; | |
160 | ||
161 | ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE, | |
162 | bh->b_size - BTRFS_CSUM_SIZE, result); | |
163 | if (ret) | |
164 | return ret; | |
87cbda5c | 165 | if (verify) { |
f254e52c CM |
166 | if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) { |
167 | printk("checksum verify failed on %lu\n", | |
168 | bh->b_blocknr); | |
169 | return 1; | |
170 | } | |
171 | } else { | |
172 | node = btrfs_buffer_node(bh); | |
22b0ebda | 173 | memcpy(node->header.csum, result, BTRFS_CSUM_SIZE); |
f254e52c | 174 | } |
87cbda5c CM |
175 | return 0; |
176 | } | |
177 | ||
d98237b3 | 178 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
ed2ff2cb | 179 | { |
87cbda5c | 180 | struct buffer_head *bh; |
0f7d52f4 | 181 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
87cbda5c | 182 | struct buffer_head *head; |
87cbda5c CM |
183 | if (!page_has_buffers(page)) { |
184 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, | |
185 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | |
186 | } | |
187 | head = page_buffers(page); | |
188 | bh = head; | |
189 | do { | |
190 | if (buffer_dirty(bh)) | |
191 | csum_tree_block(root, bh, 0); | |
192 | bh = bh->b_this_page; | |
193 | } while (bh != head); | |
d98237b3 | 194 | return block_write_full_page(page, btree_get_block, wbc); |
ed2ff2cb CM |
195 | } |
196 | ||
d98237b3 | 197 | static int btree_readpage(struct file * file, struct page * page) |
eb60ceac | 198 | { |
d98237b3 | 199 | return block_read_full_page(page, btree_get_block); |
eb60ceac CM |
200 | } |
201 | ||
d98237b3 CM |
202 | static struct address_space_operations btree_aops = { |
203 | .readpage = btree_readpage, | |
204 | .writepage = btree_writepage, | |
205 | .sync_page = block_sync_page, | |
206 | }; | |
207 | ||
e20d96d6 | 208 | struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr) |
eb60ceac | 209 | { |
d98237b3 | 210 | struct buffer_head *bh = NULL; |
eb60ceac | 211 | |
d98237b3 CM |
212 | bh = btrfs_find_create_tree_block(root, blocknr); |
213 | if (!bh) | |
214 | return bh; | |
9d64272c CM |
215 | if (buffer_uptodate(bh)) |
216 | goto uptodate; | |
d98237b3 CM |
217 | lock_buffer(bh); |
218 | if (!buffer_uptodate(bh)) { | |
219 | get_bh(bh); | |
220 | bh->b_end_io = end_buffer_read_sync; | |
221 | submit_bh(READ, bh); | |
222 | wait_on_buffer(bh); | |
223 | if (!buffer_uptodate(bh)) | |
224 | goto fail; | |
87cbda5c | 225 | csum_tree_block(root, bh, 1); |
d98237b3 CM |
226 | } else { |
227 | unlock_buffer(bh); | |
228 | } | |
9d64272c | 229 | uptodate: |
d98237b3 | 230 | if (check_tree_block(root, bh)) |
cfaa7295 | 231 | BUG(); |
d98237b3 CM |
232 | return bh; |
233 | fail: | |
234 | brelse(bh); | |
235 | return NULL; | |
eb60ceac CM |
236 | } |
237 | ||
e089f05c | 238 | int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
e20d96d6 | 239 | struct buffer_head *buf) |
ed2ff2cb | 240 | { |
d6025579 | 241 | WARN_ON(atomic_read(&buf->b_count) == 0); |
e20d96d6 | 242 | mark_buffer_dirty(buf); |
ed2ff2cb CM |
243 | return 0; |
244 | } | |
245 | ||
e089f05c | 246 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
e20d96d6 | 247 | struct buffer_head *buf) |
ed2ff2cb | 248 | { |
d6025579 | 249 | WARN_ON(atomic_read(&buf->b_count) == 0); |
e20d96d6 | 250 | clear_buffer_dirty(buf); |
ed2ff2cb CM |
251 | return 0; |
252 | } | |
253 | ||
2c90e5d6 | 254 | static int __setup_root(int blocksize, |
9f5fae2f CM |
255 | struct btrfs_root *root, |
256 | struct btrfs_fs_info *fs_info, | |
e20d96d6 | 257 | u64 objectid) |
d97e63b6 | 258 | { |
cfaa7295 | 259 | root->node = NULL; |
0f7d52f4 | 260 | root->inode = NULL; |
a28ec197 | 261 | root->commit_root = NULL; |
2c90e5d6 | 262 | root->blocksize = blocksize; |
123abc88 | 263 | root->ref_cows = 0; |
9f5fae2f | 264 | root->fs_info = fs_info; |
0f7d52f4 CM |
265 | root->objectid = objectid; |
266 | root->last_trans = 0; | |
3768f368 CM |
267 | memset(&root->root_key, 0, sizeof(root->root_key)); |
268 | memset(&root->root_item, 0, sizeof(root->root_item)); | |
269 | return 0; | |
270 | } | |
271 | ||
2c90e5d6 | 272 | static int find_and_setup_root(int blocksize, |
9f5fae2f CM |
273 | struct btrfs_root *tree_root, |
274 | struct btrfs_fs_info *fs_info, | |
275 | u64 objectid, | |
e20d96d6 | 276 | struct btrfs_root *root) |
3768f368 CM |
277 | { |
278 | int ret; | |
279 | ||
2c90e5d6 | 280 | __setup_root(blocksize, root, fs_info, objectid); |
3768f368 CM |
281 | ret = btrfs_find_last_root(tree_root, objectid, |
282 | &root->root_item, &root->root_key); | |
283 | BUG_ON(ret); | |
284 | ||
285 | root->node = read_tree_block(root, | |
286 | btrfs_root_blocknr(&root->root_item)); | |
3768f368 | 287 | BUG_ON(!root->node); |
d97e63b6 CM |
288 | return 0; |
289 | } | |
290 | ||
0f7d52f4 CM |
291 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, |
292 | struct btrfs_key *location) | |
293 | { | |
294 | struct btrfs_root *root; | |
295 | struct btrfs_root *tree_root = fs_info->tree_root; | |
296 | struct btrfs_path *path; | |
297 | struct btrfs_leaf *l; | |
298 | int ret = 0; | |
299 | ||
300 | printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags); | |
301 | root = kmalloc(sizeof(*root), GFP_NOFS); | |
302 | if (!root) { | |
303 | printk("failed1\n"); | |
304 | return ERR_PTR(-ENOMEM); | |
305 | } | |
306 | if (location->offset == (u64)-1) { | |
307 | ret = find_and_setup_root(fs_info->sb->s_blocksize, | |
308 | fs_info->tree_root, fs_info, | |
309 | location->objectid, root); | |
310 | if (ret) { | |
311 | printk("failed2\n"); | |
312 | kfree(root); | |
313 | return ERR_PTR(ret); | |
314 | } | |
315 | goto insert; | |
316 | } | |
317 | ||
318 | __setup_root(fs_info->sb->s_blocksize, root, fs_info, | |
319 | location->objectid); | |
320 | ||
321 | path = btrfs_alloc_path(); | |
322 | BUG_ON(!path); | |
323 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); | |
324 | if (ret != 0) { | |
325 | printk("internal search_slot gives us %d\n", ret); | |
326 | if (ret > 0) | |
327 | ret = -ENOENT; | |
328 | goto out; | |
329 | } | |
330 | l = btrfs_buffer_leaf(path->nodes[0]); | |
331 | memcpy(&root->root_item, | |
332 | btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item), | |
333 | sizeof(root->root_item)); | |
334 | memcpy(&root->root_key, location, sizeof(*location)); | |
335 | ret = 0; | |
336 | out: | |
337 | btrfs_release_path(root, path); | |
338 | btrfs_free_path(path); | |
339 | if (ret) { | |
340 | kfree(root); | |
341 | return ERR_PTR(ret); | |
342 | } | |
343 | root->node = read_tree_block(root, | |
344 | btrfs_root_blocknr(&root->root_item)); | |
345 | BUG_ON(!root->node); | |
346 | insert: | |
347 | printk("inserting %p\n", root); | |
348 | root->ref_cows = 1; | |
349 | ret = radix_tree_insert(&fs_info->fs_roots_radix, (unsigned long)root, | |
350 | root); | |
351 | if (ret) { | |
352 | printk("radix_tree_insert gives us %d\n", ret); | |
353 | brelse(root->node); | |
354 | kfree(root); | |
355 | return ERR_PTR(ret); | |
356 | } | |
357 | printk("all worked\n"); | |
358 | return root; | |
359 | } | |
360 | ||
2c90e5d6 | 361 | struct btrfs_root *open_ctree(struct super_block *sb) |
2e635a27 | 362 | { |
e20d96d6 CM |
363 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
364 | GFP_NOFS); | |
365 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), | |
366 | GFP_NOFS); | |
367 | struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root), | |
368 | GFP_NOFS); | |
369 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), | |
370 | GFP_NOFS); | |
eb60ceac | 371 | int ret; |
2c90e5d6 | 372 | struct btrfs_super_block *disk_super; |
eb60ceac | 373 | |
8ef97622 CM |
374 | init_bit_radix(&fs_info->pinned_radix); |
375 | init_bit_radix(&fs_info->pending_del_radix); | |
0f7d52f4 | 376 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
2c90e5d6 | 377 | sb_set_blocksize(sb, 4096); |
9f5fae2f | 378 | fs_info->running_transaction = NULL; |
9f5fae2f CM |
379 | fs_info->tree_root = tree_root; |
380 | fs_info->extent_root = extent_root; | |
381 | fs_info->inode_root = inode_root; | |
382 | fs_info->last_inode_alloc = 0; | |
5be6f7f1 | 383 | fs_info->highest_inode = 0; |
e20d96d6 | 384 | fs_info->sb = sb; |
d98237b3 CM |
385 | fs_info->btree_inode = new_inode(sb); |
386 | fs_info->btree_inode->i_ino = 1; | |
2c90e5d6 | 387 | fs_info->btree_inode->i_nlink = 1; |
d98237b3 CM |
388 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
389 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; | |
0f7d52f4 CM |
390 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
391 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, | |
392 | sizeof(struct btrfs_key)); | |
22b0ebda | 393 | insert_inode_hash(fs_info->btree_inode); |
d98237b3 | 394 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
87cbda5c | 395 | fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC); |
30ae8467 | 396 | spin_lock_init(&fs_info->hash_lock); |
30ae8467 | 397 | if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) { |
87cbda5c CM |
398 | printk("failed to allocate sha256 hash\n"); |
399 | return NULL; | |
400 | } | |
79154b1b | 401 | mutex_init(&fs_info->trans_mutex); |
d561c025 | 402 | mutex_init(&fs_info->fs_mutex); |
9f5fae2f CM |
403 | memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert)); |
404 | memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert)); | |
3768f368 | 405 | |
2c90e5d6 CM |
406 | __setup_root(sb->s_blocksize, tree_root, |
407 | fs_info, BTRFS_ROOT_TREE_OBJECTID); | |
408 | fs_info->sb_buffer = read_tree_block(tree_root, | |
409 | BTRFS_SUPER_INFO_OFFSET / | |
410 | sb->s_blocksize); | |
d98237b3 | 411 | |
0f7d52f4 | 412 | if (!fs_info->sb_buffer) |
d98237b3 | 413 | return NULL; |
d98237b3 | 414 | disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data; |
0f7d52f4 | 415 | if (!btrfs_super_root(disk_super)) |
2c90e5d6 | 416 | return NULL; |
0f7d52f4 | 417 | |
d98237b3 | 418 | fs_info->disk_super = disk_super; |
e20d96d6 CM |
419 | tree_root->node = read_tree_block(tree_root, |
420 | btrfs_super_root(disk_super)); | |
3768f368 CM |
421 | BUG_ON(!tree_root->node); |
422 | ||
2c90e5d6 CM |
423 | mutex_lock(&fs_info->fs_mutex); |
424 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, | |
e20d96d6 | 425 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
3768f368 CM |
426 | BUG_ON(ret); |
427 | ||
2c90e5d6 | 428 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, |
e20d96d6 | 429 | BTRFS_INODE_MAP_OBJECTID, inode_root); |
9f5fae2f CM |
430 | BUG_ON(ret); |
431 | ||
0f7d52f4 CM |
432 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
433 | ret = btrfs_find_highest_inode(tree_root, &fs_info->last_inode_alloc); | |
5be6f7f1 CM |
434 | if (ret == 0) |
435 | fs_info->highest_inode = fs_info->last_inode_alloc; | |
d6e4a428 CM |
436 | memset(&fs_info->kobj, 0, sizeof(fs_info->kobj)); |
437 | kobj_set_kset_s(fs_info, btrfs_subsys); | |
438 | kobject_set_name(&fs_info->kobj, "%s", sb->s_id); | |
439 | kobject_register(&fs_info->kobj); | |
5be6f7f1 | 440 | mutex_unlock(&fs_info->fs_mutex); |
0f7d52f4 | 441 | return tree_root; |
eb60ceac CM |
442 | } |
443 | ||
e089f05c | 444 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
79154b1b | 445 | *root) |
eb60ceac | 446 | { |
d5719762 | 447 | struct buffer_head *bh = root->fs_info->sb_buffer; |
2c90e5d6 | 448 | |
d5719762 CM |
449 | btrfs_set_super_root(root->fs_info->disk_super, |
450 | root->fs_info->tree_root->node->b_blocknr); | |
451 | lock_buffer(bh); | |
2c90e5d6 | 452 | WARN_ON(atomic_read(&bh->b_count) < 1); |
d5719762 | 453 | clear_buffer_dirty(bh); |
87cbda5c | 454 | csum_tree_block(root, bh, 0); |
d5719762 CM |
455 | bh->b_end_io = end_buffer_write_sync; |
456 | get_bh(bh); | |
457 | submit_bh(WRITE, bh); | |
458 | wait_on_buffer(bh); | |
459 | if (!buffer_uptodate(bh)) { | |
460 | WARN_ON(1); | |
461 | return -EIO; | |
cfaa7295 CM |
462 | } |
463 | return 0; | |
464 | } | |
465 | ||
0f7d52f4 CM |
466 | int del_fs_roots(struct btrfs_fs_info *fs_info) |
467 | { | |
468 | int ret; | |
469 | struct btrfs_root *gang[8]; | |
470 | int i; | |
471 | ||
472 | while(1) { | |
473 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, | |
474 | (void **)gang, 0, | |
475 | ARRAY_SIZE(gang)); | |
476 | if (!ret) | |
477 | break; | |
478 | for (i = 0; i < ret; i++) { | |
479 | radix_tree_delete(&fs_info->fs_roots_radix, | |
480 | (unsigned long)gang[i]); | |
481 | if (gang[i]->inode) | |
482 | iput(gang[i]->inode); | |
483 | else | |
484 | printk("no inode for root %p\n", gang[i]); | |
485 | if (gang[i]->node) | |
486 | brelse(gang[i]->node); | |
487 | if (gang[i]->commit_root) | |
488 | brelse(gang[i]->commit_root); | |
489 | kfree(gang[i]); | |
490 | } | |
491 | } | |
492 | return 0; | |
493 | } | |
494 | ||
e20d96d6 | 495 | int close_ctree(struct btrfs_root *root) |
cfaa7295 | 496 | { |
3768f368 | 497 | int ret; |
e089f05c | 498 | struct btrfs_trans_handle *trans; |
0f7d52f4 | 499 | struct btrfs_fs_info *fs_info = root->fs_info; |
e089f05c | 500 | |
0f7d52f4 | 501 | mutex_lock(&fs_info->fs_mutex); |
79154b1b CM |
502 | trans = btrfs_start_transaction(root, 1); |
503 | btrfs_commit_transaction(trans, root); | |
504 | /* run commit again to drop the original snapshot */ | |
505 | trans = btrfs_start_transaction(root, 1); | |
506 | btrfs_commit_transaction(trans, root); | |
507 | ret = btrfs_write_and_wait_transaction(NULL, root); | |
3768f368 | 508 | BUG_ON(ret); |
79154b1b | 509 | write_ctree_super(NULL, root); |
0f7d52f4 CM |
510 | mutex_unlock(&fs_info->fs_mutex); |
511 | ||
512 | if (fs_info->extent_root->node) | |
513 | btrfs_block_release(fs_info->extent_root, | |
514 | fs_info->extent_root->node); | |
515 | if (fs_info->inode_root->node) | |
516 | btrfs_block_release(fs_info->inode_root, | |
517 | fs_info->inode_root->node); | |
518 | if (fs_info->tree_root->node) | |
519 | btrfs_block_release(fs_info->tree_root, | |
520 | fs_info->tree_root->node); | |
521 | btrfs_block_release(root, fs_info->sb_buffer); | |
522 | crypto_free_hash(fs_info->hash_tfm); | |
523 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); | |
524 | iput(fs_info->btree_inode); | |
525 | del_fs_roots(fs_info); | |
526 | kfree(fs_info->extent_root); | |
527 | kfree(fs_info->inode_root); | |
528 | kfree(fs_info->tree_root); | |
529 | kobject_unregister(&fs_info->kobj); | |
eb60ceac CM |
530 | return 0; |
531 | } | |
532 | ||
e20d96d6 | 533 | void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf) |
eb60ceac | 534 | { |
7cfcc17e | 535 | brelse(buf); |
eb60ceac CM |
536 | } |
537 |