disk-io.c 17 KB

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