disk-io.c 15 KB

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