disk-io.c 11 KB

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