disk-io.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/crc32c.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/swap.h>
  23. #include <linux/radix-tree.h>
  24. #include <linux/writeback.h>
  25. #include "ctree.h"
  26. #include "disk-io.h"
  27. #include "transaction.h"
  28. #include "btrfs_inode.h"
  29. u64 bh_blocknr(struct buffer_head *bh)
  30. {
  31. return bh->b_blocknr;
  32. }
  33. static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
  34. {
  35. struct btrfs_node *node = btrfs_buffer_node(buf);
  36. if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
  37. printk(KERN_CRIT "bh_blocknr(buf) is %llu, header is %llu\n",
  38. (unsigned long long)bh_blocknr(buf),
  39. (unsigned long long)btrfs_header_blocknr(&node->header));
  40. return 1;
  41. }
  42. return 0;
  43. }
  44. struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
  45. {
  46. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  47. int blockbits = root->fs_info->sb->s_blocksize_bits;
  48. unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
  49. struct page *page;
  50. struct buffer_head *bh;
  51. struct buffer_head *head;
  52. struct buffer_head *ret = NULL;
  53. page = find_lock_page(mapping, index);
  54. if (!page)
  55. return NULL;
  56. if (!page_has_buffers(page))
  57. goto out_unlock;
  58. head = page_buffers(page);
  59. bh = head;
  60. do {
  61. if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
  62. ret = bh;
  63. get_bh(bh);
  64. goto out_unlock;
  65. }
  66. bh = bh->b_this_page;
  67. } while (bh != head);
  68. out_unlock:
  69. unlock_page(page);
  70. page_cache_release(page);
  71. return ret;
  72. }
  73. int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
  74. u64 logical)
  75. {
  76. if (logical == 0) {
  77. bh->b_bdev = NULL;
  78. bh->b_blocknr = 0;
  79. set_buffer_mapped(bh);
  80. } else {
  81. map_bh(bh, root->fs_info->sb, logical);
  82. }
  83. return 0;
  84. }
  85. struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
  86. u64 blocknr)
  87. {
  88. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  89. int blockbits = root->fs_info->sb->s_blocksize_bits;
  90. unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
  91. struct page *page;
  92. struct buffer_head *bh;
  93. struct buffer_head *head;
  94. struct buffer_head *ret = NULL;
  95. int err;
  96. u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
  97. page = find_or_create_page(mapping, index, GFP_NOFS);
  98. if (!page)
  99. return NULL;
  100. if (!page_has_buffers(page))
  101. create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
  102. head = page_buffers(page);
  103. bh = head;
  104. do {
  105. if (!buffer_mapped(bh)) {
  106. err = btrfs_map_bh_to_logical(root, bh, first_block);
  107. BUG_ON(err);
  108. }
  109. if (bh_blocknr(bh) == blocknr) {
  110. ret = bh;
  111. get_bh(bh);
  112. goto out_unlock;
  113. }
  114. bh = bh->b_this_page;
  115. first_block++;
  116. } while (bh != head);
  117. out_unlock:
  118. unlock_page(page);
  119. if (ret)
  120. touch_buffer(ret);
  121. page_cache_release(page);
  122. return ret;
  123. }
  124. static int btree_get_block(struct inode *inode, sector_t iblock,
  125. struct buffer_head *bh, int create)
  126. {
  127. int err;
  128. struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
  129. err = btrfs_map_bh_to_logical(root, bh, iblock);
  130. return err;
  131. }
  132. int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
  133. char *result)
  134. {
  135. u32 crc;
  136. crc = crc32c(0, data, len);
  137. memcpy(result, &crc, BTRFS_CRC32_SIZE);
  138. return 0;
  139. }
  140. static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
  141. int verify)
  142. {
  143. char result[BTRFS_CRC32_SIZE];
  144. int ret;
  145. struct btrfs_node *node;
  146. ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
  147. bh->b_size - BTRFS_CSUM_SIZE, result);
  148. if (ret)
  149. return ret;
  150. if (verify) {
  151. if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
  152. printk("btrfs: %s checksum verify failed on %llu\n",
  153. root->fs_info->sb->s_id,
  154. (unsigned long long)bh_blocknr(bh));
  155. return 1;
  156. }
  157. } else {
  158. node = btrfs_buffer_node(bh);
  159. memcpy(node->header.csum, result, BTRFS_CRC32_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_I(page->mapping->host)->root;
  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. int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
  191. {
  192. struct buffer_head *bh = NULL;
  193. int ret = 0;
  194. bh = btrfs_find_create_tree_block(root, blocknr);
  195. if (!bh)
  196. return 0;
  197. if (buffer_uptodate(bh)) {
  198. ret = 1;
  199. goto done;
  200. }
  201. if (test_set_buffer_locked(bh)) {
  202. ret = 1;
  203. goto done;
  204. }
  205. if (!buffer_uptodate(bh)) {
  206. get_bh(bh);
  207. bh->b_end_io = end_buffer_read_sync;
  208. submit_bh(READ, bh);
  209. } else {
  210. unlock_buffer(bh);
  211. ret = 1;
  212. }
  213. done:
  214. brelse(bh);
  215. return ret;
  216. }
  217. struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
  218. {
  219. struct buffer_head *bh = NULL;
  220. bh = btrfs_find_create_tree_block(root, blocknr);
  221. if (!bh)
  222. return bh;
  223. if (buffer_uptodate(bh))
  224. goto uptodate;
  225. lock_buffer(bh);
  226. if (!buffer_uptodate(bh)) {
  227. get_bh(bh);
  228. bh->b_end_io = end_buffer_read_sync;
  229. submit_bh(READ, bh);
  230. wait_on_buffer(bh);
  231. if (!buffer_uptodate(bh))
  232. goto fail;
  233. } else {
  234. unlock_buffer(bh);
  235. }
  236. uptodate:
  237. if (!buffer_checked(bh)) {
  238. csum_tree_block(root, bh, 1);
  239. set_buffer_checked(bh);
  240. }
  241. if (check_tree_block(root, bh))
  242. goto fail;
  243. return bh;
  244. fail:
  245. brelse(bh);
  246. return NULL;
  247. }
  248. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  249. struct buffer_head *buf)
  250. {
  251. WARN_ON(atomic_read(&buf->b_count) == 0);
  252. lock_buffer(buf);
  253. clear_buffer_dirty(buf);
  254. unlock_buffer(buf);
  255. return 0;
  256. }
  257. static int __setup_root(int blocksize,
  258. struct btrfs_root *root,
  259. struct btrfs_fs_info *fs_info,
  260. u64 objectid)
  261. {
  262. root->node = NULL;
  263. root->inode = NULL;
  264. root->commit_root = NULL;
  265. root->blocksize = blocksize;
  266. root->ref_cows = 0;
  267. root->fs_info = fs_info;
  268. root->objectid = objectid;
  269. root->last_trans = 0;
  270. root->highest_inode = 0;
  271. root->last_inode_alloc = 0;
  272. root->name = NULL;
  273. memset(&root->root_key, 0, sizeof(root->root_key));
  274. memset(&root->root_item, 0, sizeof(root->root_item));
  275. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  276. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  277. init_completion(&root->kobj_unregister);
  278. init_rwsem(&root->snap_sem);
  279. root->defrag_running = 0;
  280. root->defrag_level = 0;
  281. root->root_key.objectid = objectid;
  282. return 0;
  283. }
  284. static int find_and_setup_root(int blocksize,
  285. struct btrfs_root *tree_root,
  286. struct btrfs_fs_info *fs_info,
  287. u64 objectid,
  288. struct btrfs_root *root)
  289. {
  290. int ret;
  291. __setup_root(blocksize, root, fs_info, objectid);
  292. ret = btrfs_find_last_root(tree_root, objectid,
  293. &root->root_item, &root->root_key);
  294. BUG_ON(ret);
  295. root->node = read_tree_block(root,
  296. btrfs_root_blocknr(&root->root_item));
  297. BUG_ON(!root->node);
  298. return 0;
  299. }
  300. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
  301. struct btrfs_key *location)
  302. {
  303. struct btrfs_root *root;
  304. struct btrfs_root *tree_root = fs_info->tree_root;
  305. struct btrfs_path *path;
  306. struct btrfs_leaf *l;
  307. u64 highest_inode;
  308. int ret = 0;
  309. root = kzalloc(sizeof(*root), GFP_NOFS);
  310. if (!root)
  311. return ERR_PTR(-ENOMEM);
  312. if (location->offset == (u64)-1) {
  313. ret = find_and_setup_root(fs_info->sb->s_blocksize,
  314. fs_info->tree_root, fs_info,
  315. location->objectid, root);
  316. if (ret) {
  317. kfree(root);
  318. return ERR_PTR(ret);
  319. }
  320. goto insert;
  321. }
  322. __setup_root(fs_info->sb->s_blocksize, root, fs_info,
  323. location->objectid);
  324. path = btrfs_alloc_path();
  325. BUG_ON(!path);
  326. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  327. if (ret != 0) {
  328. if (ret > 0)
  329. ret = -ENOENT;
  330. goto out;
  331. }
  332. l = btrfs_buffer_leaf(path->nodes[0]);
  333. memcpy(&root->root_item,
  334. btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
  335. sizeof(root->root_item));
  336. memcpy(&root->root_key, location, sizeof(*location));
  337. ret = 0;
  338. out:
  339. btrfs_release_path(root, path);
  340. btrfs_free_path(path);
  341. if (ret) {
  342. kfree(root);
  343. return ERR_PTR(ret);
  344. }
  345. root->node = read_tree_block(root,
  346. btrfs_root_blocknr(&root->root_item));
  347. BUG_ON(!root->node);
  348. insert:
  349. root->ref_cows = 1;
  350. ret = btrfs_find_highest_inode(root, &highest_inode);
  351. if (ret == 0) {
  352. root->highest_inode = highest_inode;
  353. root->last_inode_alloc = highest_inode;
  354. }
  355. return root;
  356. }
  357. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  358. struct btrfs_key *location,
  359. const char *name, int namelen)
  360. {
  361. struct btrfs_root *root;
  362. int ret;
  363. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  364. (unsigned long)location->objectid);
  365. if (root)
  366. return root;
  367. root = btrfs_read_fs_root_no_radix(fs_info, location);
  368. if (IS_ERR(root))
  369. return root;
  370. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  371. (unsigned long)root->root_key.objectid,
  372. root);
  373. if (ret) {
  374. brelse(root->node);
  375. kfree(root);
  376. return ERR_PTR(ret);
  377. }
  378. ret = btrfs_set_root_name(root, name, namelen);
  379. if (ret) {
  380. brelse(root->node);
  381. kfree(root);
  382. return ERR_PTR(ret);
  383. }
  384. ret = btrfs_sysfs_add_root(root);
  385. if (ret) {
  386. brelse(root->node);
  387. kfree(root->name);
  388. kfree(root);
  389. return ERR_PTR(ret);
  390. }
  391. ret = btrfs_find_dead_roots(fs_info->tree_root,
  392. root->root_key.objectid, root);
  393. BUG_ON(ret);
  394. return root;
  395. }
  396. struct btrfs_root *open_ctree(struct super_block *sb)
  397. {
  398. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  399. GFP_NOFS);
  400. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  401. GFP_NOFS);
  402. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  403. GFP_NOFS);
  404. int ret;
  405. int err = -EIO;
  406. struct btrfs_super_block *disk_super;
  407. if (!extent_root || !tree_root || !fs_info) {
  408. err = -ENOMEM;
  409. goto fail;
  410. }
  411. init_bit_radix(&fs_info->pinned_radix);
  412. init_bit_radix(&fs_info->pending_del_radix);
  413. init_bit_radix(&fs_info->extent_map_radix);
  414. init_bit_radix(&fs_info->extent_ins_radix);
  415. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  416. INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
  417. INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
  418. INIT_LIST_HEAD(&fs_info->trans_list);
  419. INIT_LIST_HEAD(&fs_info->dead_roots);
  420. memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
  421. init_completion(&fs_info->kobj_unregister);
  422. sb_set_blocksize(sb, 4096);
  423. fs_info->running_transaction = NULL;
  424. fs_info->last_trans_committed = 0;
  425. fs_info->tree_root = tree_root;
  426. fs_info->extent_root = extent_root;
  427. fs_info->sb = sb;
  428. fs_info->btree_inode = new_inode(sb);
  429. fs_info->btree_inode->i_ino = 1;
  430. fs_info->btree_inode->i_nlink = 1;
  431. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  432. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  433. fs_info->do_barriers = 1;
  434. fs_info->closing = 0;
  435. INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
  436. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  437. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  438. sizeof(struct btrfs_key));
  439. insert_inode_hash(fs_info->btree_inode);
  440. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  441. mutex_init(&fs_info->trans_mutex);
  442. mutex_init(&fs_info->fs_mutex);
  443. __setup_root(sb->s_blocksize, tree_root,
  444. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  445. fs_info->sb_buffer = read_tree_block(tree_root,
  446. BTRFS_SUPER_INFO_OFFSET /
  447. sb->s_blocksize);
  448. if (!fs_info->sb_buffer)
  449. goto fail_iput;
  450. disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
  451. fs_info->disk_super = disk_super;
  452. memcpy(&fs_info->super_copy, disk_super, sizeof(fs_info->super_copy));
  453. if (!btrfs_super_root(disk_super))
  454. goto fail_sb_buffer;
  455. i_size_write(fs_info->btree_inode,
  456. btrfs_super_total_blocks(disk_super) <<
  457. fs_info->btree_inode->i_blkbits);
  458. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  459. sizeof(disk_super->magic))) {
  460. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  461. goto fail_sb_buffer;
  462. }
  463. tree_root->node = read_tree_block(tree_root,
  464. btrfs_super_root(disk_super));
  465. if (!tree_root->node)
  466. goto fail_sb_buffer;
  467. mutex_lock(&fs_info->fs_mutex);
  468. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  469. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  470. if (ret) {
  471. mutex_unlock(&fs_info->fs_mutex);
  472. goto fail_tree_root;
  473. }
  474. btrfs_read_block_groups(extent_root);
  475. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  476. mutex_unlock(&fs_info->fs_mutex);
  477. return tree_root;
  478. fail_tree_root:
  479. btrfs_block_release(tree_root, tree_root->node);
  480. fail_sb_buffer:
  481. btrfs_block_release(tree_root, fs_info->sb_buffer);
  482. fail_iput:
  483. iput(fs_info->btree_inode);
  484. fail:
  485. kfree(extent_root);
  486. kfree(tree_root);
  487. kfree(fs_info);
  488. return ERR_PTR(err);
  489. }
  490. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  491. *root)
  492. {
  493. int ret;
  494. struct buffer_head *bh = root->fs_info->sb_buffer;
  495. lock_buffer(bh);
  496. WARN_ON(atomic_read(&bh->b_count) < 1);
  497. clear_buffer_dirty(bh);
  498. csum_tree_block(root, bh, 0);
  499. bh->b_end_io = end_buffer_write_sync;
  500. get_bh(bh);
  501. if (root->fs_info->do_barriers)
  502. ret = submit_bh(WRITE_BARRIER, bh);
  503. else
  504. ret = submit_bh(WRITE, bh);
  505. if (ret == -EOPNOTSUPP) {
  506. get_bh(bh);
  507. lock_buffer(bh);
  508. set_buffer_uptodate(bh);
  509. root->fs_info->do_barriers = 0;
  510. ret = submit_bh(WRITE, bh);
  511. }
  512. wait_on_buffer(bh);
  513. if (!buffer_uptodate(bh)) {
  514. WARN_ON(1);
  515. return -EIO;
  516. }
  517. return 0;
  518. }
  519. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  520. {
  521. radix_tree_delete(&fs_info->fs_roots_radix,
  522. (unsigned long)root->root_key.objectid);
  523. btrfs_sysfs_del_root(root);
  524. if (root->inode)
  525. iput(root->inode);
  526. if (root->node)
  527. brelse(root->node);
  528. if (root->commit_root)
  529. brelse(root->commit_root);
  530. if (root->name)
  531. kfree(root->name);
  532. kfree(root);
  533. return 0;
  534. }
  535. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  536. {
  537. int ret;
  538. struct btrfs_root *gang[8];
  539. int i;
  540. while(1) {
  541. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  542. (void **)gang, 0,
  543. ARRAY_SIZE(gang));
  544. if (!ret)
  545. break;
  546. for (i = 0; i < ret; i++)
  547. btrfs_free_fs_root(fs_info, gang[i]);
  548. }
  549. return 0;
  550. }
  551. int close_ctree(struct btrfs_root *root)
  552. {
  553. int ret;
  554. struct btrfs_trans_handle *trans;
  555. struct btrfs_fs_info *fs_info = root->fs_info;
  556. fs_info->closing = 1;
  557. btrfs_transaction_flush_work(root);
  558. mutex_lock(&fs_info->fs_mutex);
  559. btrfs_defrag_dirty_roots(root->fs_info);
  560. trans = btrfs_start_transaction(root, 1);
  561. ret = btrfs_commit_transaction(trans, root);
  562. /* run commit again to drop the original snapshot */
  563. trans = btrfs_start_transaction(root, 1);
  564. btrfs_commit_transaction(trans, root);
  565. ret = btrfs_write_and_wait_transaction(NULL, root);
  566. BUG_ON(ret);
  567. write_ctree_super(NULL, root);
  568. mutex_unlock(&fs_info->fs_mutex);
  569. if (fs_info->extent_root->node)
  570. btrfs_block_release(fs_info->extent_root,
  571. fs_info->extent_root->node);
  572. if (fs_info->tree_root->node)
  573. btrfs_block_release(fs_info->tree_root,
  574. fs_info->tree_root->node);
  575. btrfs_block_release(root, fs_info->sb_buffer);
  576. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  577. iput(fs_info->btree_inode);
  578. btrfs_free_block_groups(root->fs_info);
  579. del_fs_roots(fs_info);
  580. kfree(fs_info->extent_root);
  581. kfree(fs_info->tree_root);
  582. return 0;
  583. }
  584. void btrfs_mark_buffer_dirty(struct buffer_head *bh)
  585. {
  586. struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
  587. u64 transid = btrfs_header_generation(btrfs_buffer_header(bh));
  588. WARN_ON(!atomic_read(&bh->b_count));
  589. if (transid != root->fs_info->generation) {
  590. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  591. (unsigned long long)bh->b_blocknr,
  592. transid, root->fs_info->generation);
  593. WARN_ON(1);
  594. }
  595. mark_buffer_dirty(bh);
  596. }
  597. void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
  598. {
  599. brelse(buf);
  600. }
  601. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  602. {
  603. balance_dirty_pages_ratelimited_nr(
  604. root->fs_info->btree_inode->i_mapping, nr);
  605. }