]>
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> |
eb60ceac CM |
7 | #include "ctree.h" |
8 | #include "disk-io.h" | |
e089f05c | 9 | #include "transaction.h" |
eb60ceac | 10 | |
e20d96d6 | 11 | static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf) |
eb60ceac | 12 | { |
e20d96d6 | 13 | struct btrfs_node *node = btrfs_buffer_node(buf); |
d98237b3 | 14 | if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) { |
9a8dd150 | 15 | BUG(); |
d98237b3 | 16 | } |
e20d96d6 | 17 | if (root->node && btrfs_header_parentid(&node->header) != |
df2ce34c | 18 | btrfs_header_parentid(btrfs_buffer_header(root->node))) { |
7f5c1516 | 19 | BUG(); |
df2ce34c | 20 | } |
9a8dd150 | 21 | return 0; |
eb60ceac CM |
22 | } |
23 | ||
d98237b3 CM |
24 | struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr) |
25 | { | |
26 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; | |
27 | int blockbits = root->fs_info->sb->s_blocksize_bits; | |
28 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); | |
29 | struct page *page; | |
30 | struct buffer_head *bh; | |
31 | struct buffer_head *head; | |
32 | struct buffer_head *ret = NULL; | |
33 | ||
2c90e5d6 | 34 | |
d98237b3 CM |
35 | page = find_lock_page(mapping, index); |
36 | if (!page) | |
37 | return NULL; | |
38 | ||
39 | if (!page_has_buffers(page)) | |
40 | goto out_unlock; | |
41 | ||
42 | head = page_buffers(page); | |
43 | bh = head; | |
44 | do { | |
45 | if (buffer_mapped(bh) && bh->b_blocknr == blocknr) { | |
46 | ret = bh; | |
47 | get_bh(bh); | |
48 | goto out_unlock; | |
49 | } | |
50 | bh = bh->b_this_page; | |
51 | } while (bh != head); | |
52 | out_unlock: | |
53 | unlock_page(page); | |
d6025579 | 54 | if (ret) { |
22b0ebda | 55 | touch_buffer(ret); |
d6025579 | 56 | } |
d98237b3 CM |
57 | page_cache_release(page); |
58 | return ret; | |
59 | } | |
60 | ||
61 | struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root, | |
62 | u64 blocknr) | |
63 | { | |
64 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; | |
65 | int blockbits = root->fs_info->sb->s_blocksize_bits; | |
66 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); | |
67 | struct page *page; | |
68 | struct buffer_head *bh; | |
69 | struct buffer_head *head; | |
70 | struct buffer_head *ret = NULL; | |
71 | u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits); | |
22b0ebda | 72 | |
d98237b3 CM |
73 | page = grab_cache_page(mapping, index); |
74 | if (!page) | |
75 | return NULL; | |
76 | ||
d98237b3 CM |
77 | if (!page_has_buffers(page)) |
78 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0); | |
79 | head = page_buffers(page); | |
80 | bh = head; | |
81 | do { | |
82 | if (!buffer_mapped(bh)) { | |
83 | bh->b_bdev = root->fs_info->sb->s_bdev; | |
84 | bh->b_blocknr = first_block; | |
85 | set_buffer_mapped(bh); | |
86 | } | |
87 | if (bh->b_blocknr == blocknr) { | |
88 | ret = bh; | |
89 | get_bh(bh); | |
90 | goto out_unlock; | |
91 | } | |
92 | bh = bh->b_this_page; | |
93 | first_block++; | |
94 | } while (bh != head); | |
95 | out_unlock: | |
96 | unlock_page(page); | |
22b0ebda CM |
97 | if (ret) |
98 | touch_buffer(ret); | |
d98237b3 CM |
99 | page_cache_release(page); |
100 | return ret; | |
101 | } | |
102 | ||
103 | static sector_t max_block(struct block_device *bdev) | |
104 | { | |
105 | sector_t retval = ~((sector_t)0); | |
106 | loff_t sz = i_size_read(bdev->bd_inode); | |
107 | ||
108 | if (sz) { | |
109 | unsigned int size = block_size(bdev); | |
110 | unsigned int sizebits = blksize_bits(size); | |
111 | retval = (sz >> sizebits); | |
112 | } | |
113 | return retval; | |
114 | } | |
115 | ||
116 | static int btree_get_block(struct inode *inode, sector_t iblock, | |
117 | struct buffer_head *bh, int create) | |
118 | { | |
119 | if (iblock >= max_block(inode->i_sb->s_bdev)) { | |
120 | if (create) | |
121 | return -EIO; | |
122 | ||
123 | /* | |
124 | * for reads, we're just trying to fill a partial page. | |
125 | * return a hole, they will have to call get_block again | |
126 | * before they can fill it, and they will get -EIO at that | |
127 | * time | |
128 | */ | |
129 | return 0; | |
130 | } | |
131 | bh->b_bdev = inode->i_sb->s_bdev; | |
132 | bh->b_blocknr = iblock; | |
133 | set_buffer_mapped(bh); | |
134 | return 0; | |
135 | } | |
136 | ||
f254e52c CM |
137 | int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, |
138 | char *result) | |
87cbda5c | 139 | { |
87cbda5c CM |
140 | struct scatterlist sg; |
141 | struct crypto_hash *tfm = root->fs_info->hash_tfm; | |
142 | struct hash_desc desc; | |
143 | int ret; | |
87cbda5c CM |
144 | |
145 | desc.tfm = tfm; | |
146 | desc.flags = 0; | |
f254e52c | 147 | sg_init_one(&sg, data, len); |
87cbda5c | 148 | spin_lock(&root->fs_info->hash_lock); |
22b0ebda | 149 | ret = crypto_hash_digest(&desc, &sg, 1, result); |
87cbda5c CM |
150 | spin_unlock(&root->fs_info->hash_lock); |
151 | if (ret) { | |
152 | printk("sha256 digest failed\n"); | |
153 | } | |
f254e52c CM |
154 | return ret; |
155 | } | |
156 | static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, | |
157 | int verify) | |
158 | { | |
159 | char result[BTRFS_CSUM_SIZE]; | |
160 | int ret; | |
161 | struct btrfs_node *node; | |
162 | ||
163 | ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE, | |
164 | bh->b_size - BTRFS_CSUM_SIZE, result); | |
165 | if (ret) | |
166 | return ret; | |
87cbda5c | 167 | if (verify) { |
f254e52c CM |
168 | if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) { |
169 | printk("checksum verify failed on %lu\n", | |
170 | bh->b_blocknr); | |
171 | return 1; | |
172 | } | |
173 | } else { | |
174 | node = btrfs_buffer_node(bh); | |
22b0ebda | 175 | memcpy(node->header.csum, result, BTRFS_CSUM_SIZE); |
f254e52c | 176 | } |
87cbda5c CM |
177 | return 0; |
178 | } | |
179 | ||
d98237b3 | 180 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
ed2ff2cb | 181 | { |
87cbda5c CM |
182 | struct buffer_head *bh; |
183 | struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb); | |
184 | struct buffer_head *head; | |
87cbda5c CM |
185 | if (!page_has_buffers(page)) { |
186 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, | |
187 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | |
188 | } | |
189 | head = page_buffers(page); | |
190 | bh = head; | |
191 | do { | |
192 | if (buffer_dirty(bh)) | |
193 | csum_tree_block(root, bh, 0); | |
194 | bh = bh->b_this_page; | |
195 | } while (bh != head); | |
d98237b3 | 196 | return block_write_full_page(page, btree_get_block, wbc); |
ed2ff2cb CM |
197 | } |
198 | ||
d98237b3 | 199 | static int btree_readpage(struct file * file, struct page * page) |
eb60ceac | 200 | { |
d98237b3 | 201 | return block_read_full_page(page, btree_get_block); |
eb60ceac CM |
202 | } |
203 | ||
d98237b3 CM |
204 | static struct address_space_operations btree_aops = { |
205 | .readpage = btree_readpage, | |
206 | .writepage = btree_writepage, | |
207 | .sync_page = block_sync_page, | |
208 | }; | |
209 | ||
e20d96d6 | 210 | struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr) |
eb60ceac | 211 | { |
d98237b3 | 212 | struct buffer_head *bh = NULL; |
eb60ceac | 213 | |
d98237b3 CM |
214 | bh = btrfs_find_create_tree_block(root, blocknr); |
215 | if (!bh) | |
216 | return bh; | |
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 | } | |
229 | if (check_tree_block(root, bh)) | |
cfaa7295 | 230 | BUG(); |
d98237b3 CM |
231 | return bh; |
232 | fail: | |
233 | brelse(bh); | |
234 | return NULL; | |
eb60ceac CM |
235 | } |
236 | ||
e089f05c | 237 | int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
e20d96d6 | 238 | struct buffer_head *buf) |
ed2ff2cb | 239 | { |
d6025579 | 240 | WARN_ON(atomic_read(&buf->b_count) == 0); |
e20d96d6 | 241 | mark_buffer_dirty(buf); |
ed2ff2cb CM |
242 | return 0; |
243 | } | |
244 | ||
e089f05c | 245 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
e20d96d6 | 246 | struct buffer_head *buf) |
ed2ff2cb | 247 | { |
d6025579 | 248 | WARN_ON(atomic_read(&buf->b_count) == 0); |
e20d96d6 | 249 | clear_buffer_dirty(buf); |
ed2ff2cb CM |
250 | return 0; |
251 | } | |
252 | ||
2c90e5d6 | 253 | static int __setup_root(int blocksize, |
9f5fae2f CM |
254 | struct btrfs_root *root, |
255 | struct btrfs_fs_info *fs_info, | |
e20d96d6 | 256 | u64 objectid) |
d97e63b6 | 257 | { |
cfaa7295 | 258 | root->node = NULL; |
a28ec197 | 259 | root->commit_root = NULL; |
2c90e5d6 | 260 | root->blocksize = blocksize; |
123abc88 | 261 | root->ref_cows = 0; |
9f5fae2f | 262 | root->fs_info = fs_info; |
3768f368 CM |
263 | memset(&root->root_key, 0, sizeof(root->root_key)); |
264 | memset(&root->root_item, 0, sizeof(root->root_item)); | |
265 | return 0; | |
266 | } | |
267 | ||
2c90e5d6 | 268 | static int find_and_setup_root(int blocksize, |
9f5fae2f CM |
269 | struct btrfs_root *tree_root, |
270 | struct btrfs_fs_info *fs_info, | |
271 | u64 objectid, | |
e20d96d6 | 272 | struct btrfs_root *root) |
3768f368 CM |
273 | { |
274 | int ret; | |
275 | ||
2c90e5d6 | 276 | __setup_root(blocksize, root, fs_info, objectid); |
3768f368 CM |
277 | ret = btrfs_find_last_root(tree_root, objectid, |
278 | &root->root_item, &root->root_key); | |
279 | BUG_ON(ret); | |
280 | ||
281 | root->node = read_tree_block(root, | |
282 | btrfs_root_blocknr(&root->root_item)); | |
3768f368 | 283 | BUG_ON(!root->node); |
d97e63b6 CM |
284 | return 0; |
285 | } | |
286 | ||
2c90e5d6 | 287 | struct btrfs_root *open_ctree(struct super_block *sb) |
2e635a27 | 288 | { |
e20d96d6 CM |
289 | struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root), |
290 | GFP_NOFS); | |
291 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), | |
292 | GFP_NOFS); | |
293 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), | |
294 | GFP_NOFS); | |
295 | struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root), | |
296 | GFP_NOFS); | |
297 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), | |
298 | GFP_NOFS); | |
eb60ceac | 299 | int ret; |
2c90e5d6 | 300 | struct btrfs_super_block *disk_super; |
eb60ceac | 301 | |
8ef97622 CM |
302 | init_bit_radix(&fs_info->pinned_radix); |
303 | init_bit_radix(&fs_info->pending_del_radix); | |
2c90e5d6 | 304 | sb_set_blocksize(sb, 4096); |
9f5fae2f CM |
305 | fs_info->running_transaction = NULL; |
306 | fs_info->fs_root = root; | |
307 | fs_info->tree_root = tree_root; | |
308 | fs_info->extent_root = extent_root; | |
309 | fs_info->inode_root = inode_root; | |
310 | fs_info->last_inode_alloc = 0; | |
311 | fs_info->last_inode_alloc_dirid = 0; | |
e20d96d6 | 312 | fs_info->sb = sb; |
d98237b3 CM |
313 | fs_info->btree_inode = new_inode(sb); |
314 | fs_info->btree_inode->i_ino = 1; | |
2c90e5d6 | 315 | fs_info->btree_inode->i_nlink = 1; |
d98237b3 CM |
316 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
317 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; | |
22b0ebda | 318 | insert_inode_hash(fs_info->btree_inode); |
d98237b3 | 319 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
87cbda5c | 320 | fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC); |
30ae8467 | 321 | spin_lock_init(&fs_info->hash_lock); |
30ae8467 | 322 | if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) { |
87cbda5c CM |
323 | printk("failed to allocate sha256 hash\n"); |
324 | return NULL; | |
325 | } | |
79154b1b | 326 | mutex_init(&fs_info->trans_mutex); |
d561c025 | 327 | mutex_init(&fs_info->fs_mutex); |
9f5fae2f CM |
328 | memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert)); |
329 | memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert)); | |
3768f368 | 330 | |
2c90e5d6 CM |
331 | __setup_root(sb->s_blocksize, tree_root, |
332 | fs_info, BTRFS_ROOT_TREE_OBJECTID); | |
333 | fs_info->sb_buffer = read_tree_block(tree_root, | |
334 | BTRFS_SUPER_INFO_OFFSET / | |
335 | sb->s_blocksize); | |
d98237b3 | 336 | |
87cbda5c CM |
337 | if (!fs_info->sb_buffer) { |
338 | printk("failed2\n"); | |
d98237b3 | 339 | return NULL; |
87cbda5c | 340 | } |
d98237b3 | 341 | disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data; |
2c90e5d6 CM |
342 | if (!btrfs_super_root(disk_super)) { |
343 | return NULL; | |
344 | } | |
d98237b3 | 345 | fs_info->disk_super = disk_super; |
e20d96d6 CM |
346 | tree_root->node = read_tree_block(tree_root, |
347 | btrfs_super_root(disk_super)); | |
3768f368 CM |
348 | BUG_ON(!tree_root->node); |
349 | ||
2c90e5d6 CM |
350 | mutex_lock(&fs_info->fs_mutex); |
351 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, | |
e20d96d6 | 352 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
3768f368 CM |
353 | BUG_ON(ret); |
354 | ||
2c90e5d6 | 355 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, |
e20d96d6 | 356 | BTRFS_INODE_MAP_OBJECTID, inode_root); |
9f5fae2f CM |
357 | BUG_ON(ret); |
358 | ||
2c90e5d6 | 359 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, |
e20d96d6 | 360 | BTRFS_FS_TREE_OBJECTID, root); |
2c90e5d6 | 361 | mutex_unlock(&fs_info->fs_mutex); |
3768f368 | 362 | BUG_ON(ret); |
a28ec197 | 363 | root->commit_root = root->node; |
e20d96d6 | 364 | get_bh(root->node); |
3768f368 | 365 | root->ref_cows = 1; |
293ffd5f | 366 | root->fs_info->generation = root->root_key.offset + 1; |
eb60ceac CM |
367 | return root; |
368 | } | |
369 | ||
e089f05c | 370 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
79154b1b | 371 | *root) |
eb60ceac | 372 | { |
d5719762 | 373 | struct buffer_head *bh = root->fs_info->sb_buffer; |
2c90e5d6 | 374 | |
d5719762 CM |
375 | btrfs_set_super_root(root->fs_info->disk_super, |
376 | root->fs_info->tree_root->node->b_blocknr); | |
377 | lock_buffer(bh); | |
2c90e5d6 | 378 | WARN_ON(atomic_read(&bh->b_count) < 1); |
d5719762 | 379 | clear_buffer_dirty(bh); |
87cbda5c | 380 | csum_tree_block(root, bh, 0); |
d5719762 CM |
381 | bh->b_end_io = end_buffer_write_sync; |
382 | get_bh(bh); | |
383 | submit_bh(WRITE, bh); | |
384 | wait_on_buffer(bh); | |
385 | if (!buffer_uptodate(bh)) { | |
386 | WARN_ON(1); | |
387 | return -EIO; | |
cfaa7295 CM |
388 | } |
389 | return 0; | |
390 | } | |
391 | ||
e20d96d6 | 392 | int close_ctree(struct btrfs_root *root) |
cfaa7295 | 393 | { |
3768f368 | 394 | int ret; |
e089f05c CM |
395 | struct btrfs_trans_handle *trans; |
396 | ||
2c90e5d6 | 397 | mutex_lock(&root->fs_info->fs_mutex); |
79154b1b CM |
398 | trans = btrfs_start_transaction(root, 1); |
399 | btrfs_commit_transaction(trans, root); | |
400 | /* run commit again to drop the original snapshot */ | |
401 | trans = btrfs_start_transaction(root, 1); | |
402 | btrfs_commit_transaction(trans, root); | |
403 | ret = btrfs_write_and_wait_transaction(NULL, root); | |
3768f368 | 404 | BUG_ON(ret); |
79154b1b | 405 | write_ctree_super(NULL, root); |
2c90e5d6 | 406 | mutex_unlock(&root->fs_info->fs_mutex); |
ed2ff2cb | 407 | |
cfaa7295 | 408 | if (root->node) |
234b63a0 | 409 | btrfs_block_release(root, root->node); |
9f5fae2f CM |
410 | if (root->fs_info->extent_root->node) |
411 | btrfs_block_release(root->fs_info->extent_root, | |
412 | root->fs_info->extent_root->node); | |
413 | if (root->fs_info->inode_root->node) | |
414 | btrfs_block_release(root->fs_info->inode_root, | |
415 | root->fs_info->inode_root->node); | |
416 | if (root->fs_info->tree_root->node) | |
417 | btrfs_block_release(root->fs_info->tree_root, | |
418 | root->fs_info->tree_root->node); | |
234b63a0 | 419 | btrfs_block_release(root, root->commit_root); |
e20d96d6 | 420 | btrfs_block_release(root, root->fs_info->sb_buffer); |
87cbda5c | 421 | crypto_free_hash(root->fs_info->hash_tfm); |
7cfcc17e CM |
422 | truncate_inode_pages(root->fs_info->btree_inode->i_mapping, 0); |
423 | iput(root->fs_info->btree_inode); | |
e20d96d6 CM |
424 | kfree(root->fs_info->extent_root); |
425 | kfree(root->fs_info->inode_root); | |
426 | kfree(root->fs_info->tree_root); | |
427 | kfree(root->fs_info); | |
428 | kfree(root); | |
eb60ceac CM |
429 | return 0; |
430 | } | |
431 | ||
e20d96d6 | 432 | void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf) |
eb60ceac | 433 | { |
7cfcc17e | 434 | brelse(buf); |
eb60ceac CM |
435 | } |
436 |