disk-io.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 <linux/buffer_head.h> // for block_sync_page
  26. #include "ctree.h"
  27. #include "disk-io.h"
  28. #include "transaction.h"
  29. #include "btrfs_inode.h"
  30. #if 0
  31. static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
  32. {
  33. if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
  34. printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
  35. (unsigned long long)extent_buffer_blocknr(buf),
  36. (unsigned long long)btrfs_header_blocknr(buf));
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. #endif
  42. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  43. u64 blocknr)
  44. {
  45. struct inode *btree_inode = root->fs_info->btree_inode;
  46. struct extent_buffer *eb;
  47. eb = find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
  48. blocknr * root->sectorsize,
  49. root->sectorsize, GFP_NOFS);
  50. if (eb)
  51. eb->alloc_addr = (unsigned long)__builtin_return_address(0);
  52. return eb;
  53. }
  54. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  55. u64 blocknr)
  56. {
  57. struct inode *btree_inode = root->fs_info->btree_inode;
  58. struct extent_buffer *eb;
  59. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree,
  60. blocknr * root->sectorsize,
  61. root->sectorsize, GFP_NOFS);
  62. eb->alloc_addr = (unsigned long)__builtin_return_address(0);
  63. return eb;
  64. }
  65. struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
  66. size_t page_offset, u64 start, u64 end,
  67. int create)
  68. {
  69. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  70. struct extent_map *em;
  71. int ret;
  72. again:
  73. em = lookup_extent_mapping(em_tree, start, end);
  74. if (em) {
  75. goto out;
  76. }
  77. em = alloc_extent_map(GFP_NOFS);
  78. if (!em) {
  79. em = ERR_PTR(-ENOMEM);
  80. goto out;
  81. }
  82. em->start = 0;
  83. em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1;
  84. em->block_start = 0;
  85. em->block_end = em->end;
  86. em->bdev = inode->i_sb->s_bdev;
  87. ret = add_extent_mapping(em_tree, em);
  88. if (ret == -EEXIST) {
  89. free_extent_map(em);
  90. em = NULL;
  91. goto again;
  92. } else if (ret) {
  93. em = ERR_PTR(ret);
  94. }
  95. out:
  96. return em;
  97. }
  98. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  99. {
  100. struct extent_map_tree *tree;
  101. tree = &BTRFS_I(page->mapping->host)->extent_tree;
  102. return extent_write_full_page(tree, page, btree_get_extent, wbc);
  103. }
  104. int btree_readpage(struct file *file, struct page *page)
  105. {
  106. struct extent_map_tree *tree;
  107. tree = &BTRFS_I(page->mapping->host)->extent_tree;
  108. return extent_read_full_page(tree, page, btree_get_extent);
  109. }
  110. static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags)
  111. {
  112. struct extent_map_tree *tree;
  113. int ret;
  114. BUG_ON(page->private != 1);
  115. tree = &BTRFS_I(page->mapping->host)->extent_tree;
  116. ret = try_release_extent_mapping(tree, page);
  117. if (ret == 1) {
  118. ClearPagePrivate(page);
  119. set_page_private(page, 0);
  120. page_cache_release(page);
  121. }
  122. return ret;
  123. }
  124. static void btree_invalidatepage(struct page *page, unsigned long offset)
  125. {
  126. struct extent_map_tree *tree;
  127. tree = &BTRFS_I(page->mapping->host)->extent_tree;
  128. extent_invalidatepage(tree, page, offset);
  129. btree_releasepage(page, GFP_NOFS);
  130. }
  131. int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
  132. char *result)
  133. {
  134. return 0;
  135. #if 0
  136. u32 crc;
  137. crc = crc32c(0, data, len);
  138. memcpy(result, &crc, BTRFS_CRC32_SIZE);
  139. return 0;
  140. #endif
  141. }
  142. #if 0
  143. static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
  144. int verify)
  145. {
  146. return 0;
  147. char result[BTRFS_CRC32_SIZE];
  148. int ret;
  149. struct btrfs_node *node;
  150. ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
  151. bh->b_size - BTRFS_CSUM_SIZE, result);
  152. if (ret)
  153. return ret;
  154. if (verify) {
  155. if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
  156. printk("btrfs: %s checksum verify failed on %llu\n",
  157. root->fs_info->sb->s_id,
  158. (unsigned long long)bh_blocknr(bh));
  159. return 1;
  160. }
  161. } else {
  162. node = btrfs_buffer_node(bh);
  163. memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
  164. }
  165. return 0;
  166. }
  167. #endif
  168. #if 0
  169. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  170. {
  171. struct buffer_head *bh;
  172. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  173. struct buffer_head *head;
  174. if (!page_has_buffers(page)) {
  175. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  176. (1 << BH_Dirty)|(1 << BH_Uptodate));
  177. }
  178. head = page_buffers(page);
  179. bh = head;
  180. do {
  181. if (buffer_dirty(bh))
  182. csum_tree_block(root, bh, 0);
  183. bh = bh->b_this_page;
  184. } while (bh != head);
  185. return block_write_full_page(page, btree_get_block, wbc);
  186. }
  187. #endif
  188. static struct address_space_operations btree_aops = {
  189. .readpage = btree_readpage,
  190. .writepage = btree_writepage,
  191. .releasepage = btree_releasepage,
  192. .invalidatepage = btree_invalidatepage,
  193. .sync_page = block_sync_page,
  194. };
  195. int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
  196. {
  197. struct extent_buffer *buf = NULL;
  198. struct inode *btree_inode = root->fs_info->btree_inode;
  199. int ret = 0;
  200. buf = btrfs_find_create_tree_block(root, blocknr);
  201. if (!buf)
  202. return 0;
  203. read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
  204. buf, 0);
  205. free_extent_buffer(buf);
  206. return ret;
  207. }
  208. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
  209. {
  210. struct extent_buffer *buf = NULL;
  211. struct inode *btree_inode = root->fs_info->btree_inode;
  212. buf = btrfs_find_create_tree_block(root, blocknr);
  213. if (!buf)
  214. return NULL;
  215. read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree,
  216. buf, 1);
  217. buf->alloc_addr = (unsigned long)__builtin_return_address(0);
  218. return buf;
  219. }
  220. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  221. struct extent_buffer *buf)
  222. {
  223. struct inode *btree_inode = root->fs_info->btree_inode;
  224. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
  225. return 0;
  226. }
  227. int wait_on_tree_block_writeback(struct btrfs_root *root,
  228. struct extent_buffer *buf)
  229. {
  230. struct inode *btree_inode = root->fs_info->btree_inode;
  231. wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree,
  232. buf);
  233. return 0;
  234. }
  235. int set_tree_block_dirty(struct btrfs_root *root, struct extent_buffer *buf)
  236. {
  237. struct inode *btree_inode = root->fs_info->btree_inode;
  238. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
  239. return 0;
  240. }
  241. static int __setup_root(int blocksize,
  242. struct btrfs_root *root,
  243. struct btrfs_fs_info *fs_info,
  244. u64 objectid)
  245. {
  246. root->node = NULL;
  247. root->inode = NULL;
  248. root->commit_root = NULL;
  249. root->sectorsize = blocksize;
  250. root->nodesize = blocksize;
  251. root->leafsize = blocksize;
  252. root->ref_cows = 0;
  253. root->fs_info = fs_info;
  254. root->objectid = objectid;
  255. root->last_trans = 0;
  256. root->highest_inode = 0;
  257. root->last_inode_alloc = 0;
  258. root->name = NULL;
  259. memset(&root->root_key, 0, sizeof(root->root_key));
  260. memset(&root->root_item, 0, sizeof(root->root_item));
  261. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  262. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  263. init_completion(&root->kobj_unregister);
  264. init_rwsem(&root->snap_sem);
  265. root->defrag_running = 0;
  266. root->defrag_level = 0;
  267. root->root_key.objectid = objectid;
  268. return 0;
  269. }
  270. static int find_and_setup_root(int blocksize,
  271. struct btrfs_root *tree_root,
  272. struct btrfs_fs_info *fs_info,
  273. u64 objectid,
  274. struct btrfs_root *root)
  275. {
  276. int ret;
  277. __setup_root(blocksize, root, fs_info, objectid);
  278. ret = btrfs_find_last_root(tree_root, objectid,
  279. &root->root_item, &root->root_key);
  280. BUG_ON(ret);
  281. root->node = read_tree_block(root,
  282. btrfs_root_blocknr(&root->root_item));
  283. BUG_ON(!root->node);
  284. return 0;
  285. }
  286. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
  287. struct btrfs_key *location)
  288. {
  289. struct btrfs_root *root;
  290. struct btrfs_root *tree_root = fs_info->tree_root;
  291. struct btrfs_path *path;
  292. struct extent_buffer *l;
  293. u64 highest_inode;
  294. int ret = 0;
  295. root = kzalloc(sizeof(*root), GFP_NOFS);
  296. if (!root)
  297. return ERR_PTR(-ENOMEM);
  298. if (location->offset == (u64)-1) {
  299. ret = find_and_setup_root(fs_info->sb->s_blocksize,
  300. fs_info->tree_root, fs_info,
  301. location->objectid, root);
  302. if (ret) {
  303. kfree(root);
  304. return ERR_PTR(ret);
  305. }
  306. goto insert;
  307. }
  308. __setup_root(fs_info->sb->s_blocksize, root, fs_info,
  309. location->objectid);
  310. path = btrfs_alloc_path();
  311. BUG_ON(!path);
  312. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  313. if (ret != 0) {
  314. if (ret > 0)
  315. ret = -ENOENT;
  316. goto out;
  317. }
  318. l = path->nodes[0];
  319. read_extent_buffer(l, &root->root_item,
  320. btrfs_item_ptr_offset(l, path->slots[0]),
  321. sizeof(root->root_item));
  322. ret = 0;
  323. out:
  324. btrfs_release_path(root, path);
  325. btrfs_free_path(path);
  326. if (ret) {
  327. kfree(root);
  328. return ERR_PTR(ret);
  329. }
  330. root->node = read_tree_block(root,
  331. btrfs_root_blocknr(&root->root_item));
  332. BUG_ON(!root->node);
  333. insert:
  334. root->ref_cows = 1;
  335. ret = btrfs_find_highest_inode(root, &highest_inode);
  336. if (ret == 0) {
  337. root->highest_inode = highest_inode;
  338. root->last_inode_alloc = highest_inode;
  339. }
  340. return root;
  341. }
  342. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  343. struct btrfs_key *location,
  344. const char *name, int namelen)
  345. {
  346. struct btrfs_root *root;
  347. int ret;
  348. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  349. (unsigned long)location->objectid);
  350. if (root)
  351. return root;
  352. root = btrfs_read_fs_root_no_radix(fs_info, location);
  353. if (IS_ERR(root))
  354. return root;
  355. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  356. (unsigned long)root->root_key.objectid,
  357. root);
  358. if (ret) {
  359. free_extent_buffer(root->node);
  360. kfree(root);
  361. return ERR_PTR(ret);
  362. }
  363. ret = btrfs_set_root_name(root, name, namelen);
  364. if (ret) {
  365. free_extent_buffer(root->node);
  366. kfree(root);
  367. return ERR_PTR(ret);
  368. }
  369. ret = btrfs_sysfs_add_root(root);
  370. if (ret) {
  371. free_extent_buffer(root->node);
  372. kfree(root->name);
  373. kfree(root);
  374. return ERR_PTR(ret);
  375. }
  376. ret = btrfs_find_dead_roots(fs_info->tree_root,
  377. root->root_key.objectid, root);
  378. BUG_ON(ret);
  379. return root;
  380. }
  381. struct btrfs_root *open_ctree(struct super_block *sb)
  382. {
  383. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  384. GFP_NOFS);
  385. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  386. GFP_NOFS);
  387. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  388. GFP_NOFS);
  389. int ret;
  390. int err = -EIO;
  391. struct btrfs_super_block *disk_super;
  392. if (!extent_root || !tree_root || !fs_info) {
  393. err = -ENOMEM;
  394. goto fail;
  395. }
  396. init_bit_radix(&fs_info->pinned_radix);
  397. init_bit_radix(&fs_info->pending_del_radix);
  398. init_bit_radix(&fs_info->extent_ins_radix);
  399. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  400. INIT_LIST_HEAD(&fs_info->trans_list);
  401. INIT_LIST_HEAD(&fs_info->dead_roots);
  402. memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
  403. init_completion(&fs_info->kobj_unregister);
  404. sb_set_blocksize(sb, 4096);
  405. fs_info->running_transaction = NULL;
  406. fs_info->last_trans_committed = 0;
  407. fs_info->tree_root = tree_root;
  408. fs_info->extent_root = extent_root;
  409. fs_info->sb = sb;
  410. fs_info->btree_inode = new_inode(sb);
  411. fs_info->btree_inode->i_ino = 1;
  412. fs_info->btree_inode->i_nlink = 1;
  413. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  414. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  415. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  416. fs_info->btree_inode->i_mapping,
  417. GFP_NOFS);
  418. extent_map_tree_init(&fs_info->free_space_cache,
  419. fs_info->btree_inode->i_mapping, GFP_NOFS);
  420. extent_map_tree_init(&fs_info->block_group_cache,
  421. fs_info->btree_inode->i_mapping, GFP_NOFS);
  422. fs_info->do_barriers = 1;
  423. fs_info->closing = 0;
  424. INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
  425. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  426. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  427. sizeof(struct btrfs_key));
  428. insert_inode_hash(fs_info->btree_inode);
  429. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  430. mutex_init(&fs_info->trans_mutex);
  431. mutex_init(&fs_info->fs_mutex);
  432. __setup_root(sb->s_blocksize, tree_root,
  433. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  434. fs_info->sb_buffer = read_tree_block(tree_root,
  435. BTRFS_SUPER_INFO_OFFSET /
  436. sb->s_blocksize);
  437. if (!fs_info->sb_buffer)
  438. goto fail_iput;
  439. read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
  440. sizeof(fs_info->super_copy));
  441. read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
  442. (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
  443. BTRFS_FSID_SIZE);
  444. disk_super = &fs_info->super_copy;
  445. if (!btrfs_super_root(disk_super))
  446. goto fail_sb_buffer;
  447. i_size_write(fs_info->btree_inode,
  448. btrfs_super_total_blocks(disk_super) <<
  449. fs_info->btree_inode->i_blkbits);
  450. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  451. sizeof(disk_super->magic))) {
  452. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  453. goto fail_sb_buffer;
  454. }
  455. tree_root->node = read_tree_block(tree_root,
  456. btrfs_super_root(disk_super));
  457. if (!tree_root->node)
  458. goto fail_sb_buffer;
  459. mutex_lock(&fs_info->fs_mutex);
  460. ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
  461. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  462. if (ret) {
  463. mutex_unlock(&fs_info->fs_mutex);
  464. goto fail_tree_root;
  465. }
  466. btrfs_read_block_groups(extent_root);
  467. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  468. mutex_unlock(&fs_info->fs_mutex);
  469. return tree_root;
  470. fail_tree_root:
  471. free_extent_buffer(tree_root->node);
  472. fail_sb_buffer:
  473. free_extent_buffer(fs_info->sb_buffer);
  474. fail_iput:
  475. iput(fs_info->btree_inode);
  476. fail:
  477. kfree(extent_root);
  478. kfree(tree_root);
  479. kfree(fs_info);
  480. return ERR_PTR(err);
  481. }
  482. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  483. *root)
  484. {
  485. int ret;
  486. struct extent_buffer *super = root->fs_info->sb_buffer;
  487. struct inode *btree_inode = root->fs_info->btree_inode;
  488. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super);
  489. ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
  490. super->start, super->len);
  491. return ret;
  492. }
  493. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  494. {
  495. radix_tree_delete(&fs_info->fs_roots_radix,
  496. (unsigned long)root->root_key.objectid);
  497. btrfs_sysfs_del_root(root);
  498. if (root->inode)
  499. iput(root->inode);
  500. if (root->node)
  501. free_extent_buffer(root->node);
  502. if (root->commit_root)
  503. free_extent_buffer(root->commit_root);
  504. if (root->name)
  505. kfree(root->name);
  506. kfree(root);
  507. return 0;
  508. }
  509. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  510. {
  511. int ret;
  512. struct btrfs_root *gang[8];
  513. int i;
  514. while(1) {
  515. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  516. (void **)gang, 0,
  517. ARRAY_SIZE(gang));
  518. if (!ret)
  519. break;
  520. for (i = 0; i < ret; i++)
  521. btrfs_free_fs_root(fs_info, gang[i]);
  522. }
  523. return 0;
  524. }
  525. int close_ctree(struct btrfs_root *root)
  526. {
  527. int ret;
  528. struct btrfs_trans_handle *trans;
  529. struct btrfs_fs_info *fs_info = root->fs_info;
  530. fs_info->closing = 1;
  531. btrfs_transaction_flush_work(root);
  532. mutex_lock(&fs_info->fs_mutex);
  533. btrfs_defrag_dirty_roots(root->fs_info);
  534. trans = btrfs_start_transaction(root, 1);
  535. ret = btrfs_commit_transaction(trans, root);
  536. /* run commit again to drop the original snapshot */
  537. trans = btrfs_start_transaction(root, 1);
  538. btrfs_commit_transaction(trans, root);
  539. ret = btrfs_write_and_wait_transaction(NULL, root);
  540. BUG_ON(ret);
  541. write_ctree_super(NULL, root);
  542. mutex_unlock(&fs_info->fs_mutex);
  543. if (fs_info->extent_root->node)
  544. free_extent_buffer(fs_info->extent_root->node);
  545. if (fs_info->tree_root->node)
  546. free_extent_buffer(fs_info->tree_root->node);
  547. free_extent_buffer(fs_info->sb_buffer);
  548. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  549. iput(fs_info->btree_inode);
  550. btrfs_free_block_groups(root->fs_info);
  551. del_fs_roots(fs_info);
  552. kfree(fs_info->extent_root);
  553. kfree(fs_info->tree_root);
  554. return 0;
  555. }
  556. int btrfs_buffer_uptodate(struct extent_buffer *buf)
  557. {
  558. struct inode *btree_inode = buf->first_page->mapping->host;
  559. return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf);
  560. }
  561. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  562. {
  563. struct inode *btree_inode = buf->first_page->mapping->host;
  564. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree,
  565. buf);
  566. }
  567. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  568. {
  569. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  570. u64 transid = btrfs_header_generation(buf);
  571. struct inode *btree_inode = root->fs_info->btree_inode;
  572. if (transid != root->fs_info->generation) {
  573. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  574. (unsigned long long)extent_buffer_blocknr(buf),
  575. transid, root->fs_info->generation);
  576. WARN_ON(1);
  577. }
  578. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf);
  579. }
  580. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  581. {
  582. balance_dirty_pages_ratelimited_nr(
  583. root->fs_info->btree_inode->i_mapping, nr);
  584. }