disk-io.c 11 KB

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