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