disk-io.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 <linux/radix-tree.h>
  8. #include <linux/writeback.h>
  9. #include "ctree.h"
  10. #include "disk-io.h"
  11. #include "transaction.h"
  12. #include "btrfs_inode.h"
  13. u64 bh_blocknr(struct buffer_head *bh)
  14. {
  15. return bh->b_blocknr;
  16. }
  17. static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
  18. {
  19. struct btrfs_node *node = btrfs_buffer_node(buf);
  20. if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
  21. printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n",
  22. bh_blocknr(buf), btrfs_header_blocknr(&node->header));
  23. return 1;
  24. }
  25. return 0;
  26. }
  27. struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
  28. {
  29. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  30. int blockbits = root->fs_info->sb->s_blocksize_bits;
  31. unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
  32. struct page *page;
  33. struct buffer_head *bh;
  34. struct buffer_head *head;
  35. struct buffer_head *ret = NULL;
  36. page = find_lock_page(mapping, index);
  37. if (!page)
  38. return NULL;
  39. if (!page_has_buffers(page))
  40. goto out_unlock;
  41. head = page_buffers(page);
  42. bh = head;
  43. do {
  44. if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
  45. ret = bh;
  46. get_bh(bh);
  47. goto out_unlock;
  48. }
  49. bh = bh->b_this_page;
  50. } while (bh != head);
  51. out_unlock:
  52. unlock_page(page);
  53. page_cache_release(page);
  54. return ret;
  55. }
  56. int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
  57. u64 logical)
  58. {
  59. if (logical == 0) {
  60. bh->b_bdev = NULL;
  61. bh->b_blocknr = 0;
  62. set_buffer_mapped(bh);
  63. } else {
  64. map_bh(bh, root->fs_info->sb, logical);
  65. }
  66. return 0;
  67. }
  68. struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
  69. u64 blocknr)
  70. {
  71. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  72. int blockbits = root->fs_info->sb->s_blocksize_bits;
  73. unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
  74. struct page *page;
  75. struct buffer_head *bh;
  76. struct buffer_head *head;
  77. struct buffer_head *ret = NULL;
  78. int err;
  79. u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
  80. page = grab_cache_page(mapping, index);
  81. if (!page)
  82. return NULL;
  83. if (!page_has_buffers(page))
  84. create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
  85. head = page_buffers(page);
  86. bh = head;
  87. do {
  88. if (!buffer_mapped(bh)) {
  89. err = btrfs_map_bh_to_logical(root, bh, first_block);
  90. BUG_ON(err);
  91. }
  92. if (bh_blocknr(bh) == blocknr) {
  93. ret = bh;
  94. get_bh(bh);
  95. goto out_unlock;
  96. }
  97. bh = bh->b_this_page;
  98. first_block++;
  99. } while (bh != head);
  100. out_unlock:
  101. unlock_page(page);
  102. if (ret)
  103. touch_buffer(ret);
  104. page_cache_release(page);
  105. return ret;
  106. }
  107. static int btree_get_block(struct inode *inode, sector_t iblock,
  108. struct buffer_head *bh, int create)
  109. {
  110. int err;
  111. struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
  112. err = btrfs_map_bh_to_logical(root, bh, iblock);
  113. return err;
  114. }
  115. int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
  116. char *result)
  117. {
  118. struct scatterlist sg;
  119. struct crypto_hash *tfm = root->fs_info->hash_tfm;
  120. struct hash_desc desc;
  121. int ret;
  122. desc.tfm = tfm;
  123. desc.flags = 0;
  124. sg_init_one(&sg, data, len);
  125. spin_lock(&root->fs_info->hash_lock);
  126. ret = crypto_hash_digest(&desc, &sg, 1, result);
  127. spin_unlock(&root->fs_info->hash_lock);
  128. if (ret) {
  129. printk("digest failed\n");
  130. }
  131. return ret;
  132. }
  133. static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
  134. int verify)
  135. {
  136. char result[BTRFS_CRC32_SIZE];
  137. int ret;
  138. struct btrfs_node *node;
  139. ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
  140. bh->b_size - BTRFS_CSUM_SIZE, result);
  141. if (ret)
  142. return ret;
  143. if (verify) {
  144. if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
  145. printk("checksum verify failed on %Lu\n",
  146. bh_blocknr(bh));
  147. return 1;
  148. }
  149. } else {
  150. node = btrfs_buffer_node(bh);
  151. memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
  152. }
  153. return 0;
  154. }
  155. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  156. {
  157. struct buffer_head *bh;
  158. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  159. struct buffer_head *head;
  160. if (!page_has_buffers(page)) {
  161. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  162. (1 << BH_Dirty)|(1 << BH_Uptodate));
  163. }
  164. head = page_buffers(page);
  165. bh = head;
  166. do {
  167. if (buffer_dirty(bh))
  168. csum_tree_block(root, bh, 0);
  169. bh = bh->b_this_page;
  170. } while (bh != head);
  171. return block_write_full_page(page, btree_get_block, wbc);
  172. }
  173. static int btree_readpage(struct file * file, struct page * page)
  174. {
  175. return block_read_full_page(page, btree_get_block);
  176. }
  177. static struct address_space_operations btree_aops = {
  178. .readpage = btree_readpage,
  179. .writepage = btree_writepage,
  180. .sync_page = block_sync_page,
  181. };
  182. int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
  183. {
  184. struct buffer_head *bh = NULL;
  185. int ret = 0;
  186. bh = btrfs_find_create_tree_block(root, blocknr);
  187. if (!bh)
  188. return 0;
  189. if (buffer_uptodate(bh)) {
  190. ret = 1;
  191. goto done;
  192. }
  193. if (test_set_buffer_locked(bh)) {
  194. ret = 1;
  195. goto done;
  196. }
  197. if (!buffer_uptodate(bh)) {
  198. get_bh(bh);
  199. bh->b_end_io = end_buffer_read_sync;
  200. submit_bh(READ, bh);
  201. } else {
  202. unlock_buffer(bh);
  203. ret = 1;
  204. }
  205. done:
  206. brelse(bh);
  207. return ret;
  208. }
  209. struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
  210. {
  211. struct buffer_head *bh = NULL;
  212. bh = btrfs_find_create_tree_block(root, blocknr);
  213. if (!bh)
  214. return bh;
  215. if (buffer_uptodate(bh))
  216. goto uptodate;
  217. lock_buffer(bh);
  218. if (!buffer_uptodate(bh)) {
  219. get_bh(bh);
  220. bh->b_end_io = end_buffer_read_sync;
  221. submit_bh(READ, bh);
  222. wait_on_buffer(bh);
  223. if (!buffer_uptodate(bh))
  224. goto fail;
  225. } else {
  226. unlock_buffer(bh);
  227. }
  228. uptodate:
  229. if (!buffer_checked(bh)) {
  230. csum_tree_block(root, bh, 1);
  231. set_buffer_checked(bh);
  232. }
  233. if (check_tree_block(root, bh))
  234. goto fail;
  235. return bh;
  236. fail:
  237. brelse(bh);
  238. return NULL;
  239. }
  240. int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  241. struct buffer_head *buf)
  242. {
  243. WARN_ON(atomic_read(&buf->b_count) == 0);
  244. mark_buffer_dirty(buf);
  245. return 0;
  246. }
  247. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  248. struct buffer_head *buf)
  249. {
  250. WARN_ON(atomic_read(&buf->b_count) == 0);
  251. clear_buffer_dirty(buf);
  252. return 0;
  253. }
  254. static int __setup_root(int blocksize,
  255. struct btrfs_root *root,
  256. struct btrfs_fs_info *fs_info,
  257. u64 objectid)
  258. {
  259. root->node = NULL;
  260. root->inode = NULL;
  261. root->commit_root = NULL;
  262. root->blocksize = blocksize;
  263. root->ref_cows = 0;
  264. root->fs_info = fs_info;
  265. root->objectid = objectid;
  266. root->last_trans = 0;
  267. root->highest_inode = 0;
  268. root->last_inode_alloc = 0;
  269. memset(&root->root_key, 0, sizeof(root->root_key));
  270. memset(&root->root_item, 0, sizeof(root->root_item));
  271. root->root_key.objectid = objectid;
  272. return 0;
  273. }
  274. static int find_and_setup_root(int blocksize,
  275. struct btrfs_root *tree_root,
  276. struct btrfs_fs_info *fs_info,
  277. u64 objectid,
  278. struct btrfs_root *root)
  279. {
  280. int ret;
  281. __setup_root(blocksize, root, fs_info, objectid);
  282. ret = btrfs_find_last_root(tree_root, objectid,
  283. &root->root_item, &root->root_key);
  284. BUG_ON(ret);
  285. root->node = read_tree_block(root,
  286. btrfs_root_blocknr(&root->root_item));
  287. BUG_ON(!root->node);
  288. return 0;
  289. }
  290. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  291. struct btrfs_key *location)
  292. {
  293. struct btrfs_root *root;
  294. struct btrfs_root *tree_root = fs_info->tree_root;
  295. struct btrfs_path *path;
  296. struct btrfs_leaf *l;
  297. u64 highest_inode;
  298. int ret = 0;
  299. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  300. (unsigned long)location->objectid);
  301. if (root)
  302. return root;
  303. root = kmalloc(sizeof(*root), GFP_NOFS);
  304. if (!root)
  305. return ERR_PTR(-ENOMEM);
  306. if (location->offset == (u64)-1) {
  307. ret = find_and_setup_root(fs_info->sb->s_blocksize,
  308. fs_info->tree_root, fs_info,
  309. location->objectid, root);
  310. if (ret) {
  311. kfree(root);
  312. return ERR_PTR(ret);
  313. }
  314. goto insert;
  315. }
  316. __setup_root(fs_info->sb->s_blocksize, root, fs_info,
  317. location->objectid);
  318. path = btrfs_alloc_path();
  319. BUG_ON(!path);
  320. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  321. if (ret != 0) {
  322. if (ret > 0)
  323. ret = -ENOENT;
  324. goto out;
  325. }
  326. l = btrfs_buffer_leaf(path->nodes[0]);
  327. memcpy(&root->root_item,
  328. btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
  329. sizeof(root->root_item));
  330. memcpy(&root->root_key, location, sizeof(*location));
  331. ret = 0;
  332. out:
  333. btrfs_release_path(root, path);
  334. btrfs_free_path(path);
  335. if (ret) {
  336. kfree(root);
  337. return ERR_PTR(ret);
  338. }
  339. root->node = read_tree_block(root,
  340. btrfs_root_blocknr(&root->root_item));
  341. BUG_ON(!root->node);
  342. insert:
  343. root->ref_cows = 1;
  344. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  345. (unsigned long)root->root_key.objectid,
  346. root);
  347. if (ret) {
  348. brelse(root->node);
  349. kfree(root);
  350. return ERR_PTR(ret);
  351. }
  352. ret = btrfs_find_highest_inode(root, &highest_inode);
  353. if (ret == 0) {
  354. root->highest_inode = highest_inode;
  355. root->last_inode_alloc = highest_inode;
  356. }
  357. return root;
  358. }
  359. struct btrfs_root *open_ctree(struct super_block *sb)
  360. {
  361. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  362. GFP_NOFS);
  363. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  364. GFP_NOFS);
  365. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  366. GFP_NOFS);
  367. int ret;
  368. int err = -EIO;
  369. struct btrfs_super_block *disk_super;
  370. if (!extent_root || !tree_root || !fs_info) {
  371. err = -ENOMEM;
  372. goto fail;
  373. }
  374. init_bit_radix(&fs_info->pinned_radix);
  375. init_bit_radix(&fs_info->pending_del_radix);
  376. init_bit_radix(&fs_info->extent_map_radix);
  377. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  378. INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
  379. INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
  380. INIT_LIST_HEAD(&fs_info->trans_list);
  381. INIT_LIST_HEAD(&fs_info->dead_roots);
  382. sb_set_blocksize(sb, 4096);
  383. fs_info->running_transaction = NULL;
  384. fs_info->tree_root = tree_root;
  385. fs_info->extent_root = extent_root;
  386. fs_info->sb = sb;
  387. fs_info->btree_inode = new_inode(sb);
  388. fs_info->btree_inode->i_ino = 1;
  389. fs_info->btree_inode->i_nlink = 1;
  390. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  391. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  392. fs_info->do_barriers = 1;
  393. fs_info->extent_tree_insert_nr = 0;
  394. fs_info->extent_tree_prealloc_nr = 0;
  395. fs_info->closing = 0;
  396. INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
  397. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  398. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  399. sizeof(struct btrfs_key));
  400. insert_inode_hash(fs_info->btree_inode);
  401. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  402. fs_info->hash_tfm = crypto_alloc_hash("crc32c", 0, CRYPTO_ALG_ASYNC);
  403. spin_lock_init(&fs_info->hash_lock);
  404. if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
  405. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  406. err = -ENOMEM;
  407. goto fail_iput;
  408. }
  409. mutex_init(&fs_info->trans_mutex);
  410. mutex_init(&fs_info->fs_mutex);
  411. __setup_root(sb->s_blocksize, tree_root,
  412. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  413. fs_info->sb_buffer = read_tree_block(tree_root,
  414. BTRFS_SUPER_INFO_OFFSET /
  415. sb->s_blocksize);
  416. if (!fs_info->sb_buffer)
  417. goto fail_iput;
  418. disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
  419. if (!btrfs_super_root(disk_super))
  420. goto fail_sb_buffer;
  421. i_size_write(fs_info->btree_inode,
  422. btrfs_super_total_blocks(disk_super) <<
  423. fs_info->btree_inode->i_blkbits);
  424. fs_info->disk_super = disk_super;
  425. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  426. sizeof(disk_super->magic))) {
  427. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  428. goto fail_sb_buffer;
  429. }
  430. tree_root->node = read_tree_block(tree_root,
  431. btrfs_super_root(disk_super));
  432. if (!tree_root->node)
  433. goto fail_sb_buffer;
  434. mutex_lock(&fs_info->fs_mutex);
  435. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  436. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  437. if (ret) {
  438. mutex_unlock(&fs_info->fs_mutex);
  439. goto fail_tree_root;
  440. }
  441. btrfs_read_block_groups(extent_root);
  442. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  443. mutex_unlock(&fs_info->fs_mutex);
  444. return tree_root;
  445. fail_tree_root:
  446. btrfs_block_release(tree_root, tree_root->node);
  447. fail_sb_buffer:
  448. btrfs_block_release(tree_root, fs_info->sb_buffer);
  449. fail_iput:
  450. iput(fs_info->btree_inode);
  451. fail:
  452. kfree(extent_root);
  453. kfree(tree_root);
  454. kfree(fs_info);
  455. return ERR_PTR(err);
  456. }
  457. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  458. *root)
  459. {
  460. int ret;
  461. struct buffer_head *bh = root->fs_info->sb_buffer;
  462. btrfs_set_super_root(root->fs_info->disk_super,
  463. bh_blocknr(root->fs_info->tree_root->node));
  464. lock_buffer(bh);
  465. WARN_ON(atomic_read(&bh->b_count) < 1);
  466. clear_buffer_dirty(bh);
  467. csum_tree_block(root, bh, 0);
  468. bh->b_end_io = end_buffer_write_sync;
  469. get_bh(bh);
  470. if (root->fs_info->do_barriers)
  471. ret = submit_bh(WRITE_BARRIER, bh);
  472. else
  473. ret = submit_bh(WRITE, bh);
  474. if (ret == -EOPNOTSUPP) {
  475. set_buffer_uptodate(bh);
  476. root->fs_info->do_barriers = 0;
  477. ret = submit_bh(WRITE, bh);
  478. }
  479. wait_on_buffer(bh);
  480. if (!buffer_uptodate(bh)) {
  481. WARN_ON(1);
  482. return -EIO;
  483. }
  484. return 0;
  485. }
  486. static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  487. {
  488. radix_tree_delete(&fs_info->fs_roots_radix,
  489. (unsigned long)root->root_key.objectid);
  490. if (root->inode)
  491. iput(root->inode);
  492. if (root->node)
  493. brelse(root->node);
  494. if (root->commit_root)
  495. brelse(root->commit_root);
  496. kfree(root);
  497. return 0;
  498. }
  499. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  500. {
  501. int ret;
  502. struct btrfs_root *gang[8];
  503. int i;
  504. while(1) {
  505. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  506. (void **)gang, 0,
  507. ARRAY_SIZE(gang));
  508. if (!ret)
  509. break;
  510. for (i = 0; i < ret; i++)
  511. free_fs_root(fs_info, gang[i]);
  512. }
  513. return 0;
  514. }
  515. int close_ctree(struct btrfs_root *root)
  516. {
  517. int ret;
  518. struct btrfs_trans_handle *trans;
  519. struct btrfs_fs_info *fs_info = root->fs_info;
  520. fs_info->closing = 1;
  521. btrfs_transaction_flush_work(root);
  522. mutex_lock(&fs_info->fs_mutex);
  523. trans = btrfs_start_transaction(root, 1);
  524. btrfs_commit_transaction(trans, root);
  525. /* run commit again to drop the original snapshot */
  526. trans = btrfs_start_transaction(root, 1);
  527. btrfs_commit_transaction(trans, root);
  528. ret = btrfs_write_and_wait_transaction(NULL, root);
  529. BUG_ON(ret);
  530. write_ctree_super(NULL, root);
  531. mutex_unlock(&fs_info->fs_mutex);
  532. if (fs_info->extent_root->node)
  533. btrfs_block_release(fs_info->extent_root,
  534. fs_info->extent_root->node);
  535. if (fs_info->tree_root->node)
  536. btrfs_block_release(fs_info->tree_root,
  537. fs_info->tree_root->node);
  538. btrfs_block_release(root, fs_info->sb_buffer);
  539. crypto_free_hash(fs_info->hash_tfm);
  540. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  541. iput(fs_info->btree_inode);
  542. btrfs_free_block_groups(root->fs_info);
  543. del_fs_roots(fs_info);
  544. kfree(fs_info->extent_root);
  545. kfree(fs_info->tree_root);
  546. return 0;
  547. }
  548. void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
  549. {
  550. brelse(buf);
  551. }
  552. void btrfs_btree_balance_dirty(struct btrfs_root *root)
  553. {
  554. balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping);
  555. }