disk-io.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. if (buffer_uptodate(bh))
  197. goto uptodate;
  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. uptodate:
  211. if (check_tree_block(root, bh))
  212. BUG();
  213. return bh;
  214. fail:
  215. brelse(bh);
  216. return NULL;
  217. }
  218. int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  219. struct buffer_head *buf)
  220. {
  221. WARN_ON(atomic_read(&buf->b_count) == 0);
  222. mark_buffer_dirty(buf);
  223. return 0;
  224. }
  225. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  226. struct buffer_head *buf)
  227. {
  228. WARN_ON(atomic_read(&buf->b_count) == 0);
  229. clear_buffer_dirty(buf);
  230. return 0;
  231. }
  232. static int __setup_root(int blocksize,
  233. struct btrfs_root *root,
  234. struct btrfs_fs_info *fs_info,
  235. u64 objectid)
  236. {
  237. root->node = NULL;
  238. root->commit_root = NULL;
  239. root->blocksize = blocksize;
  240. root->ref_cows = 0;
  241. root->fs_info = fs_info;
  242. memset(&root->root_key, 0, sizeof(root->root_key));
  243. memset(&root->root_item, 0, sizeof(root->root_item));
  244. return 0;
  245. }
  246. static int find_and_setup_root(int blocksize,
  247. struct btrfs_root *tree_root,
  248. struct btrfs_fs_info *fs_info,
  249. u64 objectid,
  250. struct btrfs_root *root)
  251. {
  252. int ret;
  253. __setup_root(blocksize, root, fs_info, objectid);
  254. ret = btrfs_find_last_root(tree_root, objectid,
  255. &root->root_item, &root->root_key);
  256. BUG_ON(ret);
  257. root->node = read_tree_block(root,
  258. btrfs_root_blocknr(&root->root_item));
  259. BUG_ON(!root->node);
  260. return 0;
  261. }
  262. struct btrfs_root *open_ctree(struct super_block *sb)
  263. {
  264. struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
  265. GFP_NOFS);
  266. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  267. GFP_NOFS);
  268. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  269. GFP_NOFS);
  270. struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
  271. GFP_NOFS);
  272. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  273. GFP_NOFS);
  274. int ret;
  275. struct btrfs_super_block *disk_super;
  276. init_bit_radix(&fs_info->pinned_radix);
  277. init_bit_radix(&fs_info->pending_del_radix);
  278. sb_set_blocksize(sb, 4096);
  279. fs_info->running_transaction = NULL;
  280. fs_info->fs_root = root;
  281. fs_info->tree_root = tree_root;
  282. fs_info->extent_root = extent_root;
  283. fs_info->inode_root = inode_root;
  284. fs_info->last_inode_alloc = 0;
  285. fs_info->sb = sb;
  286. fs_info->btree_inode = new_inode(sb);
  287. fs_info->btree_inode->i_ino = 1;
  288. fs_info->btree_inode->i_nlink = 1;
  289. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  290. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  291. insert_inode_hash(fs_info->btree_inode);
  292. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  293. fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  294. spin_lock_init(&fs_info->hash_lock);
  295. if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
  296. printk("failed to allocate sha256 hash\n");
  297. return NULL;
  298. }
  299. mutex_init(&fs_info->trans_mutex);
  300. mutex_init(&fs_info->fs_mutex);
  301. memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
  302. memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
  303. __setup_root(sb->s_blocksize, tree_root,
  304. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  305. fs_info->sb_buffer = read_tree_block(tree_root,
  306. BTRFS_SUPER_INFO_OFFSET /
  307. sb->s_blocksize);
  308. if (!fs_info->sb_buffer) {
  309. printk("failed2\n");
  310. return NULL;
  311. }
  312. disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
  313. if (!btrfs_super_root(disk_super)) {
  314. return NULL;
  315. }
  316. fs_info->disk_super = disk_super;
  317. tree_root->node = read_tree_block(tree_root,
  318. btrfs_super_root(disk_super));
  319. BUG_ON(!tree_root->node);
  320. mutex_lock(&fs_info->fs_mutex);
  321. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  322. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  323. BUG_ON(ret);
  324. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  325. BTRFS_INODE_MAP_OBJECTID, inode_root);
  326. BUG_ON(ret);
  327. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  328. BTRFS_FS_TREE_OBJECTID, root);
  329. mutex_unlock(&fs_info->fs_mutex);
  330. BUG_ON(ret);
  331. root->commit_root = root->node;
  332. get_bh(root->node);
  333. root->ref_cows = 1;
  334. root->fs_info->generation = root->root_key.offset + 1;
  335. return root;
  336. }
  337. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  338. *root)
  339. {
  340. struct buffer_head *bh = root->fs_info->sb_buffer;
  341. btrfs_set_super_root(root->fs_info->disk_super,
  342. root->fs_info->tree_root->node->b_blocknr);
  343. lock_buffer(bh);
  344. WARN_ON(atomic_read(&bh->b_count) < 1);
  345. clear_buffer_dirty(bh);
  346. csum_tree_block(root, bh, 0);
  347. bh->b_end_io = end_buffer_write_sync;
  348. get_bh(bh);
  349. submit_bh(WRITE, bh);
  350. wait_on_buffer(bh);
  351. if (!buffer_uptodate(bh)) {
  352. WARN_ON(1);
  353. return -EIO;
  354. }
  355. return 0;
  356. }
  357. int close_ctree(struct btrfs_root *root)
  358. {
  359. int ret;
  360. struct btrfs_trans_handle *trans;
  361. mutex_lock(&root->fs_info->fs_mutex);
  362. trans = btrfs_start_transaction(root, 1);
  363. btrfs_commit_transaction(trans, root);
  364. /* run commit again to drop the original snapshot */
  365. trans = btrfs_start_transaction(root, 1);
  366. btrfs_commit_transaction(trans, root);
  367. ret = btrfs_write_and_wait_transaction(NULL, root);
  368. BUG_ON(ret);
  369. write_ctree_super(NULL, root);
  370. mutex_unlock(&root->fs_info->fs_mutex);
  371. if (root->node)
  372. btrfs_block_release(root, root->node);
  373. if (root->fs_info->extent_root->node)
  374. btrfs_block_release(root->fs_info->extent_root,
  375. root->fs_info->extent_root->node);
  376. if (root->fs_info->inode_root->node)
  377. btrfs_block_release(root->fs_info->inode_root,
  378. root->fs_info->inode_root->node);
  379. if (root->fs_info->tree_root->node)
  380. btrfs_block_release(root->fs_info->tree_root,
  381. root->fs_info->tree_root->node);
  382. btrfs_block_release(root, root->commit_root);
  383. btrfs_block_release(root, root->fs_info->sb_buffer);
  384. crypto_free_hash(root->fs_info->hash_tfm);
  385. truncate_inode_pages(root->fs_info->btree_inode->i_mapping, 0);
  386. iput(root->fs_info->btree_inode);
  387. kfree(root->fs_info->extent_root);
  388. kfree(root->fs_info->inode_root);
  389. kfree(root->fs_info->tree_root);
  390. kfree(root->fs_info);
  391. kfree(root);
  392. return 0;
  393. }
  394. void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
  395. {
  396. brelse(buf);
  397. }