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. }
  51. page_cache_release(page);
  52. return ret;
  53. }
  54. struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
  55. u64 blocknr)
  56. {
  57. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  58. int blockbits = root->fs_info->sb->s_blocksize_bits;
  59. unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
  60. struct page *page;
  61. struct buffer_head *bh;
  62. struct buffer_head *head;
  63. struct buffer_head *ret = NULL;
  64. u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
  65. page = grab_cache_page(mapping, index);
  66. if (!page)
  67. return NULL;
  68. if (!page_has_buffers(page))
  69. create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
  70. head = page_buffers(page);
  71. bh = head;
  72. do {
  73. if (!buffer_mapped(bh)) {
  74. bh->b_bdev = root->fs_info->sb->s_bdev;
  75. bh->b_blocknr = first_block;
  76. set_buffer_mapped(bh);
  77. }
  78. if (bh->b_blocknr == blocknr) {
  79. ret = bh;
  80. get_bh(bh);
  81. goto out_unlock;
  82. }
  83. bh = bh->b_this_page;
  84. first_block++;
  85. } while (bh != head);
  86. out_unlock:
  87. unlock_page(page);
  88. if (ret)
  89. touch_buffer(ret);
  90. page_cache_release(page);
  91. return ret;
  92. }
  93. static sector_t max_block(struct block_device *bdev)
  94. {
  95. sector_t retval = ~((sector_t)0);
  96. loff_t sz = i_size_read(bdev->bd_inode);
  97. if (sz) {
  98. unsigned int size = block_size(bdev);
  99. unsigned int sizebits = blksize_bits(size);
  100. retval = (sz >> sizebits);
  101. }
  102. return retval;
  103. }
  104. static int btree_get_block(struct inode *inode, sector_t iblock,
  105. struct buffer_head *bh, int create)
  106. {
  107. if (iblock >= max_block(inode->i_sb->s_bdev)) {
  108. if (create)
  109. return -EIO;
  110. /*
  111. * for reads, we're just trying to fill a partial page.
  112. * return a hole, they will have to call get_block again
  113. * before they can fill it, and they will get -EIO at that
  114. * time
  115. */
  116. return 0;
  117. }
  118. bh->b_bdev = inode->i_sb->s_bdev;
  119. bh->b_blocknr = iblock;
  120. set_buffer_mapped(bh);
  121. return 0;
  122. }
  123. int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
  124. char *result)
  125. {
  126. struct scatterlist sg;
  127. struct crypto_hash *tfm = root->fs_info->hash_tfm;
  128. struct hash_desc desc;
  129. int ret;
  130. desc.tfm = tfm;
  131. desc.flags = 0;
  132. sg_init_one(&sg, data, len);
  133. spin_lock(&root->fs_info->hash_lock);
  134. ret = crypto_hash_digest(&desc, &sg, 1, result);
  135. spin_unlock(&root->fs_info->hash_lock);
  136. if (ret) {
  137. printk("sha256 digest failed\n");
  138. }
  139. return ret;
  140. }
  141. static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
  142. int verify)
  143. {
  144. char result[BTRFS_CSUM_SIZE];
  145. int ret;
  146. struct btrfs_node *node;
  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. struct buffer_head *bh;
  166. struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
  167. struct buffer_head *head;
  168. if (!page_has_buffers(page)) {
  169. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  170. (1 << BH_Dirty)|(1 << BH_Uptodate));
  171. }
  172. head = page_buffers(page);
  173. bh = head;
  174. do {
  175. if (buffer_dirty(bh))
  176. csum_tree_block(root, bh, 0);
  177. bh = bh->b_this_page;
  178. } while (bh != head);
  179. return block_write_full_page(page, btree_get_block, wbc);
  180. }
  181. static int btree_readpage(struct file * file, struct page * page)
  182. {
  183. return block_read_full_page(page, btree_get_block);
  184. }
  185. static struct address_space_operations btree_aops = {
  186. .readpage = btree_readpage,
  187. .writepage = btree_writepage,
  188. .sync_page = block_sync_page,
  189. };
  190. struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
  191. {
  192. struct buffer_head *bh = NULL;
  193. bh = btrfs_find_create_tree_block(root, blocknr);
  194. if (!bh)
  195. return bh;
  196. lock_buffer(bh);
  197. if (!buffer_uptodate(bh)) {
  198. get_bh(bh);
  199. bh->b_end_io = end_buffer_read_sync;
  200. submit_bh(READ, bh);
  201. wait_on_buffer(bh);
  202. if (!buffer_uptodate(bh))
  203. goto fail;
  204. csum_tree_block(root, bh, 1);
  205. } else {
  206. unlock_buffer(bh);
  207. }
  208. if (check_tree_block(root, bh))
  209. BUG();
  210. return bh;
  211. fail:
  212. brelse(bh);
  213. return NULL;
  214. }
  215. int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  216. struct buffer_head *buf)
  217. {
  218. WARN_ON(atomic_read(&buf->b_count) == 0);
  219. mark_buffer_dirty(buf);
  220. return 0;
  221. }
  222. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  223. struct buffer_head *buf)
  224. {
  225. WARN_ON(atomic_read(&buf->b_count) == 0);
  226. clear_buffer_dirty(buf);
  227. return 0;
  228. }
  229. static int __setup_root(int blocksize,
  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 = blocksize;
  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(int blocksize,
  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(blocksize, 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. {
  261. struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
  262. GFP_NOFS);
  263. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  264. GFP_NOFS);
  265. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  266. GFP_NOFS);
  267. struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
  268. GFP_NOFS);
  269. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  270. GFP_NOFS);
  271. int ret;
  272. struct btrfs_super_block *disk_super;
  273. init_bit_radix(&fs_info->pinned_radix);
  274. init_bit_radix(&fs_info->pending_del_radix);
  275. sb_set_blocksize(sb, 4096);
  276. fs_info->running_transaction = NULL;
  277. fs_info->fs_root = root;
  278. fs_info->tree_root = tree_root;
  279. fs_info->extent_root = extent_root;
  280. fs_info->inode_root = inode_root;
  281. fs_info->last_inode_alloc = 0;
  282. fs_info->last_inode_alloc_dirid = 0;
  283. fs_info->sb = sb;
  284. fs_info->btree_inode = new_inode(sb);
  285. fs_info->btree_inode->i_ino = 1;
  286. fs_info->btree_inode->i_nlink = 1;
  287. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  288. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  289. insert_inode_hash(fs_info->btree_inode);
  290. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  291. fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  292. spin_lock_init(&fs_info->hash_lock);
  293. if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
  294. printk("failed to allocate sha256 hash\n");
  295. return NULL;
  296. }
  297. mutex_init(&fs_info->trans_mutex);
  298. mutex_init(&fs_info->fs_mutex);
  299. memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
  300. memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
  301. __setup_root(sb->s_blocksize, tree_root,
  302. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  303. fs_info->sb_buffer = read_tree_block(tree_root,
  304. BTRFS_SUPER_INFO_OFFSET /
  305. sb->s_blocksize);
  306. if (!fs_info->sb_buffer) {
  307. printk("failed2\n");
  308. return NULL;
  309. }
  310. disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
  311. if (!btrfs_super_root(disk_super)) {
  312. return NULL;
  313. }
  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. mutex_lock(&fs_info->fs_mutex);
  319. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  320. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  321. BUG_ON(ret);
  322. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  323. BTRFS_INODE_MAP_OBJECTID, inode_root);
  324. BUG_ON(ret);
  325. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  326. BTRFS_FS_TREE_OBJECTID, root);
  327. mutex_unlock(&fs_info->fs_mutex);
  328. BUG_ON(ret);
  329. root->commit_root = root->node;
  330. get_bh(root->node);
  331. root->ref_cows = 1;
  332. root->fs_info->generation = root->root_key.offset + 1;
  333. return root;
  334. }
  335. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  336. *root)
  337. {
  338. struct buffer_head *bh = root->fs_info->sb_buffer;
  339. btrfs_set_super_root(root->fs_info->disk_super,
  340. root->fs_info->tree_root->node->b_blocknr);
  341. lock_buffer(bh);
  342. WARN_ON(atomic_read(&bh->b_count) < 1);
  343. clear_buffer_dirty(bh);
  344. csum_tree_block(root, bh, 0);
  345. bh->b_end_io = end_buffer_write_sync;
  346. get_bh(bh);
  347. submit_bh(WRITE, bh);
  348. wait_on_buffer(bh);
  349. if (!buffer_uptodate(bh)) {
  350. WARN_ON(1);
  351. return -EIO;
  352. }
  353. return 0;
  354. }
  355. int close_ctree(struct btrfs_root *root)
  356. {
  357. int ret;
  358. struct btrfs_trans_handle *trans;
  359. mutex_lock(&root->fs_info->fs_mutex);
  360. trans = btrfs_start_transaction(root, 1);
  361. btrfs_commit_transaction(trans, root);
  362. /* run commit again to drop the original snapshot */
  363. trans = btrfs_start_transaction(root, 1);
  364. btrfs_commit_transaction(trans, root);
  365. ret = btrfs_write_and_wait_transaction(NULL, root);
  366. BUG_ON(ret);
  367. write_ctree_super(NULL, root);
  368. mutex_unlock(&root->fs_info->fs_mutex);
  369. if (root->node)
  370. btrfs_block_release(root, root->node);
  371. if (root->fs_info->extent_root->node)
  372. btrfs_block_release(root->fs_info->extent_root,
  373. root->fs_info->extent_root->node);
  374. if (root->fs_info->inode_root->node)
  375. btrfs_block_release(root->fs_info->inode_root,
  376. root->fs_info->inode_root->node);
  377. if (root->fs_info->tree_root->node)
  378. btrfs_block_release(root->fs_info->tree_root,
  379. root->fs_info->tree_root->node);
  380. btrfs_block_release(root, root->commit_root);
  381. btrfs_block_release(root, root->fs_info->sb_buffer);
  382. crypto_free_hash(root->fs_info->hash_tfm);
  383. truncate_inode_pages(root->fs_info->btree_inode->i_mapping, 0);
  384. iput(root->fs_info->btree_inode);
  385. kfree(root->fs_info->extent_root);
  386. kfree(root->fs_info->inode_root);
  387. kfree(root->fs_info->tree_root);
  388. kfree(root->fs_info);
  389. kfree(root);
  390. return 0;
  391. }
  392. void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
  393. {
  394. brelse(buf);
  395. }