disk-io.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. return 0;
  148. ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
  149. bh->b_size - BTRFS_CSUM_SIZE, result);
  150. if (ret)
  151. return ret;
  152. if (verify) {
  153. if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
  154. printk("checksum verify failed on %lu\n",
  155. bh->b_blocknr);
  156. return 1;
  157. }
  158. } else {
  159. node = btrfs_buffer_node(bh);
  160. memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
  161. }
  162. return 0;
  163. }
  164. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  165. {
  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. return block_write_full_page(page, btree_get_block, wbc);
  181. }
  182. static int btree_readpage(struct file * file, struct page * page)
  183. {
  184. return block_read_full_page(page, btree_get_block);
  185. }
  186. static struct address_space_operations btree_aops = {
  187. .readpage = btree_readpage,
  188. .writepage = btree_writepage,
  189. .sync_page = block_sync_page,
  190. };
  191. struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
  192. {
  193. struct buffer_head *bh = NULL;
  194. bh = btrfs_find_create_tree_block(root, blocknr);
  195. if (!bh)
  196. return bh;
  197. lock_buffer(bh);
  198. if (!buffer_uptodate(bh)) {
  199. get_bh(bh);
  200. bh->b_end_io = end_buffer_read_sync;
  201. submit_bh(READ, bh);
  202. wait_on_buffer(bh);
  203. if (!buffer_uptodate(bh))
  204. goto fail;
  205. csum_tree_block(root, bh, 1);
  206. } else {
  207. unlock_buffer(bh);
  208. }
  209. if (check_tree_block(root, bh))
  210. BUG();
  211. return bh;
  212. fail:
  213. brelse(bh);
  214. return NULL;
  215. }
  216. int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  217. struct buffer_head *buf)
  218. {
  219. WARN_ON(atomic_read(&buf->b_count) == 0);
  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. WARN_ON(atomic_read(&buf->b_count) == 0);
  227. clear_buffer_dirty(buf);
  228. return 0;
  229. }
  230. static int __setup_root(int blocksize,
  231. struct btrfs_root *root,
  232. struct btrfs_fs_info *fs_info,
  233. u64 objectid)
  234. {
  235. root->node = NULL;
  236. root->commit_root = NULL;
  237. root->blocksize = blocksize;
  238. root->ref_cows = 0;
  239. root->fs_info = fs_info;
  240. memset(&root->root_key, 0, sizeof(root->root_key));
  241. memset(&root->root_item, 0, sizeof(root->root_item));
  242. return 0;
  243. }
  244. static int find_and_setup_root(int blocksize,
  245. struct btrfs_root *tree_root,
  246. struct btrfs_fs_info *fs_info,
  247. u64 objectid,
  248. struct btrfs_root *root)
  249. {
  250. int ret;
  251. __setup_root(blocksize, root, fs_info, objectid);
  252. ret = btrfs_find_last_root(tree_root, objectid,
  253. &root->root_item, &root->root_key);
  254. BUG_ON(ret);
  255. root->node = read_tree_block(root,
  256. btrfs_root_blocknr(&root->root_item));
  257. BUG_ON(!root->node);
  258. return 0;
  259. }
  260. struct btrfs_root *open_ctree(struct super_block *sb)
  261. {
  262. struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
  263. GFP_NOFS);
  264. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  265. GFP_NOFS);
  266. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  267. GFP_NOFS);
  268. struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
  269. GFP_NOFS);
  270. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  271. GFP_NOFS);
  272. int ret;
  273. struct btrfs_super_block *disk_super;
  274. init_bit_radix(&fs_info->pinned_radix);
  275. init_bit_radix(&fs_info->pending_del_radix);
  276. sb_set_blocksize(sb, 4096);
  277. fs_info->running_transaction = NULL;
  278. fs_info->fs_root = root;
  279. fs_info->tree_root = tree_root;
  280. fs_info->extent_root = extent_root;
  281. fs_info->inode_root = inode_root;
  282. fs_info->last_inode_alloc = 0;
  283. fs_info->last_inode_alloc_dirid = 0;
  284. fs_info->sb = sb;
  285. fs_info->btree_inode = new_inode(sb);
  286. fs_info->btree_inode->i_ino = 1;
  287. fs_info->btree_inode->i_nlink = 1;
  288. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  289. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  290. insert_inode_hash(fs_info->btree_inode);
  291. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  292. fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  293. spin_lock_init(&fs_info->hash_lock);
  294. if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
  295. printk("failed to allocate sha256 hash\n");
  296. return NULL;
  297. }
  298. mutex_init(&fs_info->trans_mutex);
  299. mutex_init(&fs_info->fs_mutex);
  300. memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
  301. memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
  302. __setup_root(sb->s_blocksize, tree_root,
  303. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  304. fs_info->sb_buffer = read_tree_block(tree_root,
  305. BTRFS_SUPER_INFO_OFFSET /
  306. sb->s_blocksize);
  307. if (!fs_info->sb_buffer) {
  308. printk("failed2\n");
  309. return NULL;
  310. }
  311. disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
  312. if (!btrfs_super_root(disk_super)) {
  313. return NULL;
  314. }
  315. fs_info->disk_super = disk_super;
  316. tree_root->node = read_tree_block(tree_root,
  317. btrfs_super_root(disk_super));
  318. BUG_ON(!tree_root->node);
  319. mutex_lock(&fs_info->fs_mutex);
  320. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  321. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  322. BUG_ON(ret);
  323. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  324. BTRFS_INODE_MAP_OBJECTID, inode_root);
  325. BUG_ON(ret);
  326. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  327. BTRFS_FS_TREE_OBJECTID, root);
  328. mutex_unlock(&fs_info->fs_mutex);
  329. BUG_ON(ret);
  330. root->commit_root = root->node;
  331. get_bh(root->node);
  332. root->ref_cows = 1;
  333. root->fs_info->generation = root->root_key.offset + 1;
  334. return root;
  335. }
  336. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  337. *root)
  338. {
  339. struct buffer_head *bh = root->fs_info->sb_buffer;
  340. btrfs_set_super_root(root->fs_info->disk_super,
  341. root->fs_info->tree_root->node->b_blocknr);
  342. lock_buffer(bh);
  343. WARN_ON(atomic_read(&bh->b_count) < 1);
  344. clear_buffer_dirty(bh);
  345. csum_tree_block(root, bh, 0);
  346. bh->b_end_io = end_buffer_write_sync;
  347. get_bh(bh);
  348. submit_bh(WRITE, bh);
  349. wait_on_buffer(bh);
  350. if (!buffer_uptodate(bh)) {
  351. WARN_ON(1);
  352. return -EIO;
  353. }
  354. return 0;
  355. }
  356. int close_ctree(struct btrfs_root *root)
  357. {
  358. int ret;
  359. struct btrfs_trans_handle *trans;
  360. mutex_lock(&root->fs_info->fs_mutex);
  361. trans = btrfs_start_transaction(root, 1);
  362. btrfs_commit_transaction(trans, root);
  363. /* run commit again to drop the original snapshot */
  364. trans = btrfs_start_transaction(root, 1);
  365. btrfs_commit_transaction(trans, root);
  366. ret = btrfs_write_and_wait_transaction(NULL, root);
  367. BUG_ON(ret);
  368. write_ctree_super(NULL, root);
  369. mutex_unlock(&root->fs_info->fs_mutex);
  370. if (root->node)
  371. btrfs_block_release(root, root->node);
  372. if (root->fs_info->extent_root->node)
  373. btrfs_block_release(root->fs_info->extent_root,
  374. root->fs_info->extent_root->node);
  375. if (root->fs_info->inode_root->node)
  376. btrfs_block_release(root->fs_info->inode_root,
  377. root->fs_info->inode_root->node);
  378. if (root->fs_info->tree_root->node)
  379. btrfs_block_release(root->fs_info->tree_root,
  380. root->fs_info->tree_root->node);
  381. btrfs_block_release(root, root->commit_root);
  382. btrfs_block_release(root, root->fs_info->sb_buffer);
  383. crypto_free_hash(root->fs_info->hash_tfm);
  384. truncate_inode_pages(root->fs_info->btree_inode->i_mapping, 0);
  385. iput(root->fs_info->btree_inode);
  386. kfree(root->fs_info->extent_root);
  387. kfree(root->fs_info->inode_root);
  388. kfree(root->fs_info->tree_root);
  389. kfree(root->fs_info);
  390. kfree(root);
  391. return 0;
  392. }
  393. void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
  394. {
  395. brelse(buf);
  396. }