disk-io.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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. #include "volumes.h"
  31. #include "print-tree.h"
  32. #if 0
  33. static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
  34. {
  35. if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
  36. printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
  37. (unsigned long long)extent_buffer_blocknr(buf),
  38. (unsigned long long)btrfs_header_blocknr(buf));
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. #endif
  44. static struct extent_io_ops btree_extent_io_ops;
  45. struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
  46. size_t page_offset, u64 start, u64 len,
  47. int create)
  48. {
  49. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  50. struct extent_map *em;
  51. int ret;
  52. again:
  53. spin_lock(&em_tree->lock);
  54. em = lookup_extent_mapping(em_tree, start, len);
  55. spin_unlock(&em_tree->lock);
  56. if (em) {
  57. goto out;
  58. }
  59. em = alloc_extent_map(GFP_NOFS);
  60. if (!em) {
  61. em = ERR_PTR(-ENOMEM);
  62. goto out;
  63. }
  64. em->start = 0;
  65. em->len = i_size_read(inode);
  66. em->block_start = 0;
  67. em->bdev = inode->i_sb->s_bdev;
  68. spin_lock(&em_tree->lock);
  69. ret = add_extent_mapping(em_tree, em);
  70. spin_unlock(&em_tree->lock);
  71. if (ret == -EEXIST) {
  72. free_extent_map(em);
  73. em = NULL;
  74. goto again;
  75. } else if (ret) {
  76. em = ERR_PTR(ret);
  77. }
  78. out:
  79. return em;
  80. }
  81. u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
  82. {
  83. return crc32c(seed, data, len);
  84. }
  85. void btrfs_csum_final(u32 crc, char *result)
  86. {
  87. *(__le32 *)result = ~cpu_to_le32(crc);
  88. }
  89. static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
  90. int verify)
  91. {
  92. char result[BTRFS_CRC32_SIZE];
  93. unsigned long len;
  94. unsigned long cur_len;
  95. unsigned long offset = BTRFS_CSUM_SIZE;
  96. char *map_token = NULL;
  97. char *kaddr;
  98. unsigned long map_start;
  99. unsigned long map_len;
  100. int err;
  101. u32 crc = ~(u32)0;
  102. len = buf->len - offset;
  103. while(len > 0) {
  104. err = map_private_extent_buffer(buf, offset, 32,
  105. &map_token, &kaddr,
  106. &map_start, &map_len, KM_USER0);
  107. if (err) {
  108. printk("failed to map extent buffer! %lu\n",
  109. offset);
  110. return 1;
  111. }
  112. cur_len = min(len, map_len - (offset - map_start));
  113. crc = btrfs_csum_data(root, kaddr + offset - map_start,
  114. crc, cur_len);
  115. len -= cur_len;
  116. offset += cur_len;
  117. unmap_extent_buffer(buf, map_token, KM_USER0);
  118. }
  119. btrfs_csum_final(crc, result);
  120. if (verify) {
  121. int from_this_trans = 0;
  122. if (root->fs_info->running_transaction &&
  123. btrfs_header_generation(buf) ==
  124. root->fs_info->running_transaction->transid)
  125. from_this_trans = 1;
  126. /* FIXME, this is not good */
  127. if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
  128. u32 val;
  129. u32 found = 0;
  130. memcpy(&found, result, BTRFS_CRC32_SIZE);
  131. read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
  132. WARN_ON(1);
  133. printk("btrfs: %s checksum verify failed on %llu "
  134. "wanted %X found %X from_this_trans %d "
  135. "level %d\n",
  136. root->fs_info->sb->s_id,
  137. buf->start, val, found, from_this_trans,
  138. btrfs_header_level(buf));
  139. return 1;
  140. }
  141. } else {
  142. write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
  143. }
  144. return 0;
  145. }
  146. int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
  147. {
  148. struct extent_io_tree *tree;
  149. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  150. u64 found_start;
  151. int found_level;
  152. unsigned long len;
  153. struct extent_buffer *eb;
  154. tree = &BTRFS_I(page->mapping->host)->io_tree;
  155. if (page->private == EXTENT_PAGE_PRIVATE)
  156. goto out;
  157. if (!page->private)
  158. goto out;
  159. len = page->private >> 2;
  160. if (len == 0) {
  161. WARN_ON(1);
  162. }
  163. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  164. read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
  165. btree_get_extent);
  166. btrfs_clear_buffer_defrag(eb);
  167. found_start = btrfs_header_bytenr(eb);
  168. if (found_start != start) {
  169. printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
  170. start, found_start, len);
  171. WARN_ON(1);
  172. goto err;
  173. }
  174. if (eb->first_page != page) {
  175. printk("bad first page %lu %lu\n", eb->first_page->index,
  176. page->index);
  177. WARN_ON(1);
  178. goto err;
  179. }
  180. if (!PageUptodate(page)) {
  181. printk("csum not up to date page %lu\n", page->index);
  182. WARN_ON(1);
  183. goto err;
  184. }
  185. found_level = btrfs_header_level(eb);
  186. spin_lock(&root->fs_info->hash_lock);
  187. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  188. spin_unlock(&root->fs_info->hash_lock);
  189. csum_tree_block(root, eb, 0);
  190. err:
  191. free_extent_buffer(eb);
  192. out:
  193. return 0;
  194. }
  195. static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
  196. {
  197. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  198. csum_dirty_buffer(root, page);
  199. return 0;
  200. }
  201. static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
  202. {
  203. struct btrfs_root *root = BTRFS_I(inode)->root;
  204. u64 offset;
  205. offset = bio->bi_sector << 9;
  206. if (offset == BTRFS_SUPER_INFO_OFFSET) {
  207. bio->bi_bdev = root->fs_info->sb->s_bdev;
  208. submit_bio(rw, bio);
  209. return 0;
  210. }
  211. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
  212. }
  213. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  214. {
  215. struct extent_io_tree *tree;
  216. tree = &BTRFS_I(page->mapping->host)->io_tree;
  217. return extent_write_full_page(tree, page, btree_get_extent, wbc);
  218. }
  219. static int btree_writepages(struct address_space *mapping,
  220. struct writeback_control *wbc)
  221. {
  222. struct extent_io_tree *tree;
  223. tree = &BTRFS_I(mapping->host)->io_tree;
  224. if (wbc->sync_mode == WB_SYNC_NONE) {
  225. u64 num_dirty;
  226. u64 start = 0;
  227. unsigned long thresh = 96 * 1024 * 1024;
  228. if (wbc->for_kupdate)
  229. return 0;
  230. if (current_is_pdflush()) {
  231. thresh = 96 * 1024 * 1024;
  232. } else {
  233. thresh = 8 * 1024 * 1024;
  234. }
  235. num_dirty = count_range_bits(tree, &start, (u64)-1,
  236. thresh, EXTENT_DIRTY);
  237. if (num_dirty < thresh) {
  238. return 0;
  239. }
  240. }
  241. return extent_writepages(tree, mapping, btree_get_extent, wbc);
  242. }
  243. int btree_readpage(struct file *file, struct page *page)
  244. {
  245. struct extent_io_tree *tree;
  246. tree = &BTRFS_I(page->mapping->host)->io_tree;
  247. return extent_read_full_page(tree, page, btree_get_extent);
  248. }
  249. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  250. {
  251. struct extent_io_tree *tree;
  252. struct extent_map_tree *map;
  253. int ret;
  254. tree = &BTRFS_I(page->mapping->host)->io_tree;
  255. map = &BTRFS_I(page->mapping->host)->extent_tree;
  256. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  257. if (ret == 1) {
  258. ClearPagePrivate(page);
  259. set_page_private(page, 0);
  260. page_cache_release(page);
  261. }
  262. return ret;
  263. }
  264. static void btree_invalidatepage(struct page *page, unsigned long offset)
  265. {
  266. struct extent_io_tree *tree;
  267. tree = &BTRFS_I(page->mapping->host)->io_tree;
  268. extent_invalidatepage(tree, page, offset);
  269. btree_releasepage(page, GFP_NOFS);
  270. }
  271. #if 0
  272. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  273. {
  274. struct buffer_head *bh;
  275. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  276. struct buffer_head *head;
  277. if (!page_has_buffers(page)) {
  278. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  279. (1 << BH_Dirty)|(1 << BH_Uptodate));
  280. }
  281. head = page_buffers(page);
  282. bh = head;
  283. do {
  284. if (buffer_dirty(bh))
  285. csum_tree_block(root, bh, 0);
  286. bh = bh->b_this_page;
  287. } while (bh != head);
  288. return block_write_full_page(page, btree_get_block, wbc);
  289. }
  290. #endif
  291. static struct address_space_operations btree_aops = {
  292. .readpage = btree_readpage,
  293. .writepage = btree_writepage,
  294. .writepages = btree_writepages,
  295. .releasepage = btree_releasepage,
  296. .invalidatepage = btree_invalidatepage,
  297. .sync_page = block_sync_page,
  298. };
  299. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
  300. {
  301. struct extent_buffer *buf = NULL;
  302. struct inode *btree_inode = root->fs_info->btree_inode;
  303. int ret = 0;
  304. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  305. if (!buf)
  306. return 0;
  307. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  308. buf, 0, 0, btree_get_extent);
  309. free_extent_buffer(buf);
  310. return ret;
  311. }
  312. static int close_all_devices(struct btrfs_fs_info *fs_info)
  313. {
  314. struct list_head *list;
  315. struct list_head *next;
  316. struct btrfs_device *device;
  317. list = &fs_info->fs_devices->devices;
  318. list_for_each(next, list) {
  319. device = list_entry(next, struct btrfs_device, dev_list);
  320. if (device->bdev && device->bdev != fs_info->sb->s_bdev)
  321. close_bdev_excl(device->bdev);
  322. device->bdev = NULL;
  323. }
  324. return 0;
  325. }
  326. int btrfs_verify_block_csum(struct btrfs_root *root,
  327. struct extent_buffer *buf)
  328. {
  329. struct extent_io_tree *io_tree;
  330. u64 end;
  331. int ret;
  332. io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  333. if (buf->flags & EXTENT_CSUM)
  334. return 0;
  335. end = min_t(u64, buf->len, PAGE_CACHE_SIZE);
  336. end = buf->start + end - 1;
  337. if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
  338. buf->flags |= EXTENT_CSUM;
  339. return 0;
  340. }
  341. lock_extent(io_tree, buf->start, end, GFP_NOFS);
  342. if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
  343. buf->flags |= EXTENT_CSUM;
  344. ret = 0;
  345. goto out_unlock;
  346. }
  347. ret = csum_tree_block(root, buf, 1);
  348. set_extent_bits(io_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS);
  349. buf->flags |= EXTENT_CSUM;
  350. out_unlock:
  351. unlock_extent(io_tree, buf->start, end, GFP_NOFS);
  352. return ret;
  353. }
  354. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  355. u64 bytenr, u32 blocksize)
  356. {
  357. struct inode *btree_inode = root->fs_info->btree_inode;
  358. struct extent_buffer *eb;
  359. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  360. bytenr, blocksize, GFP_NOFS);
  361. return eb;
  362. }
  363. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  364. u64 bytenr, u32 blocksize)
  365. {
  366. struct inode *btree_inode = root->fs_info->btree_inode;
  367. struct extent_buffer *eb;
  368. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  369. bytenr, blocksize, NULL, GFP_NOFS);
  370. return eb;
  371. }
  372. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  373. u32 blocksize)
  374. {
  375. struct extent_buffer *buf = NULL;
  376. struct inode *btree_inode = root->fs_info->btree_inode;
  377. struct extent_io_tree *io_tree;
  378. int ret;
  379. io_tree = &BTRFS_I(btree_inode)->io_tree;
  380. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  381. if (!buf)
  382. return NULL;
  383. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0, 1,
  384. btree_get_extent);
  385. ret = btrfs_verify_block_csum(root, buf);
  386. return buf;
  387. }
  388. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  389. struct extent_buffer *buf)
  390. {
  391. struct inode *btree_inode = root->fs_info->btree_inode;
  392. if (btrfs_header_generation(buf) ==
  393. root->fs_info->running_transaction->transid)
  394. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  395. buf);
  396. return 0;
  397. }
  398. int wait_on_tree_block_writeback(struct btrfs_root *root,
  399. struct extent_buffer *buf)
  400. {
  401. struct inode *btree_inode = root->fs_info->btree_inode;
  402. wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
  403. buf);
  404. return 0;
  405. }
  406. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  407. u32 stripesize, struct btrfs_root *root,
  408. struct btrfs_fs_info *fs_info,
  409. u64 objectid)
  410. {
  411. root->node = NULL;
  412. root->inode = NULL;
  413. root->commit_root = NULL;
  414. root->sectorsize = sectorsize;
  415. root->nodesize = nodesize;
  416. root->leafsize = leafsize;
  417. root->stripesize = stripesize;
  418. root->ref_cows = 0;
  419. root->track_dirty = 0;
  420. root->fs_info = fs_info;
  421. root->objectid = objectid;
  422. root->last_trans = 0;
  423. root->highest_inode = 0;
  424. root->last_inode_alloc = 0;
  425. root->name = NULL;
  426. root->in_sysfs = 0;
  427. INIT_LIST_HEAD(&root->dirty_list);
  428. memset(&root->root_key, 0, sizeof(root->root_key));
  429. memset(&root->root_item, 0, sizeof(root->root_item));
  430. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  431. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  432. init_completion(&root->kobj_unregister);
  433. root->defrag_running = 0;
  434. root->defrag_level = 0;
  435. root->root_key.objectid = objectid;
  436. return 0;
  437. }
  438. static int find_and_setup_root(struct btrfs_root *tree_root,
  439. struct btrfs_fs_info *fs_info,
  440. u64 objectid,
  441. struct btrfs_root *root)
  442. {
  443. int ret;
  444. u32 blocksize;
  445. __setup_root(tree_root->nodesize, tree_root->leafsize,
  446. tree_root->sectorsize, tree_root->stripesize,
  447. root, fs_info, objectid);
  448. ret = btrfs_find_last_root(tree_root, objectid,
  449. &root->root_item, &root->root_key);
  450. BUG_ON(ret);
  451. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  452. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  453. blocksize);
  454. BUG_ON(!root->node);
  455. return 0;
  456. }
  457. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
  458. struct btrfs_key *location)
  459. {
  460. struct btrfs_root *root;
  461. struct btrfs_root *tree_root = fs_info->tree_root;
  462. struct btrfs_path *path;
  463. struct extent_buffer *l;
  464. u64 highest_inode;
  465. u32 blocksize;
  466. int ret = 0;
  467. root = kzalloc(sizeof(*root), GFP_NOFS);
  468. if (!root)
  469. return ERR_PTR(-ENOMEM);
  470. if (location->offset == (u64)-1) {
  471. ret = find_and_setup_root(tree_root, fs_info,
  472. location->objectid, root);
  473. if (ret) {
  474. kfree(root);
  475. return ERR_PTR(ret);
  476. }
  477. goto insert;
  478. }
  479. __setup_root(tree_root->nodesize, tree_root->leafsize,
  480. tree_root->sectorsize, tree_root->stripesize,
  481. root, fs_info, location->objectid);
  482. path = btrfs_alloc_path();
  483. BUG_ON(!path);
  484. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  485. if (ret != 0) {
  486. if (ret > 0)
  487. ret = -ENOENT;
  488. goto out;
  489. }
  490. l = path->nodes[0];
  491. read_extent_buffer(l, &root->root_item,
  492. btrfs_item_ptr_offset(l, path->slots[0]),
  493. sizeof(root->root_item));
  494. memcpy(&root->root_key, location, sizeof(*location));
  495. ret = 0;
  496. out:
  497. btrfs_release_path(root, path);
  498. btrfs_free_path(path);
  499. if (ret) {
  500. kfree(root);
  501. return ERR_PTR(ret);
  502. }
  503. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  504. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  505. blocksize);
  506. BUG_ON(!root->node);
  507. insert:
  508. root->ref_cows = 1;
  509. ret = btrfs_find_highest_inode(root, &highest_inode);
  510. if (ret == 0) {
  511. root->highest_inode = highest_inode;
  512. root->last_inode_alloc = highest_inode;
  513. }
  514. return root;
  515. }
  516. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  517. u64 root_objectid)
  518. {
  519. struct btrfs_root *root;
  520. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  521. return fs_info->tree_root;
  522. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  523. return fs_info->extent_root;
  524. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  525. (unsigned long)root_objectid);
  526. return root;
  527. }
  528. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  529. struct btrfs_key *location)
  530. {
  531. struct btrfs_root *root;
  532. int ret;
  533. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  534. return fs_info->tree_root;
  535. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  536. return fs_info->extent_root;
  537. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  538. (unsigned long)location->objectid);
  539. if (root)
  540. return root;
  541. root = btrfs_read_fs_root_no_radix(fs_info, location);
  542. if (IS_ERR(root))
  543. return root;
  544. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  545. (unsigned long)root->root_key.objectid,
  546. root);
  547. if (ret) {
  548. free_extent_buffer(root->node);
  549. kfree(root);
  550. return ERR_PTR(ret);
  551. }
  552. ret = btrfs_find_dead_roots(fs_info->tree_root,
  553. root->root_key.objectid, root);
  554. BUG_ON(ret);
  555. return root;
  556. }
  557. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  558. struct btrfs_key *location,
  559. const char *name, int namelen)
  560. {
  561. struct btrfs_root *root;
  562. int ret;
  563. root = btrfs_read_fs_root_no_name(fs_info, location);
  564. if (!root)
  565. return NULL;
  566. if (root->in_sysfs)
  567. return root;
  568. ret = btrfs_set_root_name(root, name, namelen);
  569. if (ret) {
  570. free_extent_buffer(root->node);
  571. kfree(root);
  572. return ERR_PTR(ret);
  573. }
  574. ret = btrfs_sysfs_add_root(root);
  575. if (ret) {
  576. free_extent_buffer(root->node);
  577. kfree(root->name);
  578. kfree(root);
  579. return ERR_PTR(ret);
  580. }
  581. root->in_sysfs = 1;
  582. return root;
  583. }
  584. #if 0
  585. static int add_hasher(struct btrfs_fs_info *info, char *type) {
  586. struct btrfs_hasher *hasher;
  587. hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
  588. if (!hasher)
  589. return -ENOMEM;
  590. hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
  591. if (!hasher->hash_tfm) {
  592. kfree(hasher);
  593. return -EINVAL;
  594. }
  595. spin_lock(&info->hash_lock);
  596. list_add(&hasher->list, &info->hashers);
  597. spin_unlock(&info->hash_lock);
  598. return 0;
  599. }
  600. #endif
  601. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  602. {
  603. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  604. int ret = 0;
  605. struct list_head *cur;
  606. struct btrfs_device *device;
  607. struct backing_dev_info *bdi;
  608. list_for_each(cur, &info->fs_devices->devices) {
  609. device = list_entry(cur, struct btrfs_device, dev_list);
  610. bdi = blk_get_backing_dev_info(device->bdev);
  611. if (bdi && bdi_congested(bdi, bdi_bits)) {
  612. ret = 1;
  613. break;
  614. }
  615. }
  616. return ret;
  617. }
  618. void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  619. {
  620. struct list_head *cur;
  621. struct btrfs_device *device;
  622. struct btrfs_fs_info *info;
  623. info = (struct btrfs_fs_info *)bdi->unplug_io_data;
  624. list_for_each(cur, &info->fs_devices->devices) {
  625. device = list_entry(cur, struct btrfs_device, dev_list);
  626. bdi = blk_get_backing_dev_info(device->bdev);
  627. if (bdi->unplug_io_fn) {
  628. bdi->unplug_io_fn(bdi, page);
  629. }
  630. }
  631. }
  632. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  633. {
  634. bdi_init(bdi);
  635. bdi->ra_pages = default_backing_dev_info.ra_pages * 4;
  636. bdi->state = 0;
  637. bdi->capabilities = default_backing_dev_info.capabilities;
  638. bdi->unplug_io_fn = btrfs_unplug_io_fn;
  639. bdi->unplug_io_data = info;
  640. bdi->congested_fn = btrfs_congested_fn;
  641. bdi->congested_data = info;
  642. return 0;
  643. }
  644. struct btrfs_root *open_ctree(struct super_block *sb,
  645. struct btrfs_fs_devices *fs_devices)
  646. {
  647. u32 sectorsize;
  648. u32 nodesize;
  649. u32 leafsize;
  650. u32 blocksize;
  651. u32 stripesize;
  652. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  653. GFP_NOFS);
  654. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  655. GFP_NOFS);
  656. struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
  657. GFP_NOFS);
  658. struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
  659. GFP_NOFS);
  660. struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
  661. GFP_NOFS);
  662. int ret;
  663. int err = -EINVAL;
  664. struct btrfs_super_block *disk_super;
  665. if (!extent_root || !tree_root || !fs_info) {
  666. err = -ENOMEM;
  667. goto fail;
  668. }
  669. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  670. INIT_LIST_HEAD(&fs_info->trans_list);
  671. INIT_LIST_HEAD(&fs_info->dead_roots);
  672. INIT_LIST_HEAD(&fs_info->hashers);
  673. spin_lock_init(&fs_info->hash_lock);
  674. spin_lock_init(&fs_info->delalloc_lock);
  675. spin_lock_init(&fs_info->new_trans_lock);
  676. init_completion(&fs_info->kobj_unregister);
  677. sb_set_blocksize(sb, 4096);
  678. fs_info->tree_root = tree_root;
  679. fs_info->extent_root = extent_root;
  680. fs_info->chunk_root = chunk_root;
  681. fs_info->dev_root = dev_root;
  682. fs_info->fs_devices = fs_devices;
  683. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  684. INIT_LIST_HEAD(&fs_info->space_info);
  685. btrfs_mapping_init(&fs_info->mapping_tree);
  686. fs_info->sb = sb;
  687. fs_info->max_extent = (u64)-1;
  688. fs_info->max_inline = 8192 * 1024;
  689. setup_bdi(fs_info, &fs_info->bdi);
  690. fs_info->btree_inode = new_inode(sb);
  691. fs_info->btree_inode->i_ino = 1;
  692. fs_info->btree_inode->i_nlink = 1;
  693. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  694. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  695. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  696. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  697. fs_info->btree_inode->i_mapping,
  698. GFP_NOFS);
  699. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  700. GFP_NOFS);
  701. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  702. extent_io_tree_init(&fs_info->free_space_cache,
  703. fs_info->btree_inode->i_mapping, GFP_NOFS);
  704. extent_io_tree_init(&fs_info->block_group_cache,
  705. fs_info->btree_inode->i_mapping, GFP_NOFS);
  706. extent_io_tree_init(&fs_info->pinned_extents,
  707. fs_info->btree_inode->i_mapping, GFP_NOFS);
  708. extent_io_tree_init(&fs_info->pending_del,
  709. fs_info->btree_inode->i_mapping, GFP_NOFS);
  710. extent_io_tree_init(&fs_info->extent_ins,
  711. fs_info->btree_inode->i_mapping, GFP_NOFS);
  712. fs_info->do_barriers = 1;
  713. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  714. INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
  715. #else
  716. INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
  717. #endif
  718. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  719. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  720. sizeof(struct btrfs_key));
  721. insert_inode_hash(fs_info->btree_inode);
  722. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  723. mutex_init(&fs_info->trans_mutex);
  724. mutex_init(&fs_info->fs_mutex);
  725. #if 0
  726. ret = add_hasher(fs_info, "crc32c");
  727. if (ret) {
  728. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  729. err = -ENOMEM;
  730. goto fail_iput;
  731. }
  732. #endif
  733. __setup_root(4096, 4096, 4096, 4096, tree_root,
  734. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  735. fs_info->sb_buffer = read_tree_block(tree_root,
  736. BTRFS_SUPER_INFO_OFFSET,
  737. 4096);
  738. if (!fs_info->sb_buffer)
  739. goto fail_iput;
  740. read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
  741. sizeof(fs_info->super_copy));
  742. read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
  743. (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
  744. BTRFS_FSID_SIZE);
  745. disk_super = &fs_info->super_copy;
  746. if (!btrfs_super_root(disk_super))
  747. goto fail_sb_buffer;
  748. if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
  749. printk("Btrfs: wanted %llu devices, but found %llu\n",
  750. (unsigned long long)btrfs_super_num_devices(disk_super),
  751. (unsigned long long)fs_devices->num_devices);
  752. goto fail_sb_buffer;
  753. }
  754. nodesize = btrfs_super_nodesize(disk_super);
  755. leafsize = btrfs_super_leafsize(disk_super);
  756. sectorsize = btrfs_super_sectorsize(disk_super);
  757. stripesize = btrfs_super_stripesize(disk_super);
  758. tree_root->nodesize = nodesize;
  759. tree_root->leafsize = leafsize;
  760. tree_root->sectorsize = sectorsize;
  761. tree_root->stripesize = stripesize;
  762. sb_set_blocksize(sb, sectorsize);
  763. i_size_write(fs_info->btree_inode,
  764. btrfs_super_total_bytes(disk_super));
  765. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  766. sizeof(disk_super->magic))) {
  767. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  768. goto fail_sb_buffer;
  769. }
  770. mutex_lock(&fs_info->fs_mutex);
  771. ret = btrfs_read_sys_array(tree_root);
  772. BUG_ON(ret);
  773. blocksize = btrfs_level_size(tree_root,
  774. btrfs_super_chunk_root_level(disk_super));
  775. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  776. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  777. chunk_root->node = read_tree_block(chunk_root,
  778. btrfs_super_chunk_root(disk_super),
  779. blocksize);
  780. BUG_ON(!chunk_root->node);
  781. ret = btrfs_read_chunk_tree(chunk_root);
  782. BUG_ON(ret);
  783. blocksize = btrfs_level_size(tree_root,
  784. btrfs_super_root_level(disk_super));
  785. tree_root->node = read_tree_block(tree_root,
  786. btrfs_super_root(disk_super),
  787. blocksize);
  788. if (!tree_root->node)
  789. goto fail_sb_buffer;
  790. ret = find_and_setup_root(tree_root, fs_info,
  791. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  792. if (ret)
  793. goto fail_tree_root;
  794. extent_root->track_dirty = 1;
  795. ret = find_and_setup_root(tree_root, fs_info,
  796. BTRFS_DEV_TREE_OBJECTID, dev_root);
  797. dev_root->track_dirty = 1;
  798. if (ret)
  799. goto fail_extent_root;
  800. btrfs_read_block_groups(extent_root);
  801. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  802. if (btrfs_super_num_devices(disk_super) > 0) {
  803. fs_info->data_alloc_profile = BTRFS_BLOCK_GROUP_RAID0;
  804. fs_info->metadata_alloc_profile = BTRFS_BLOCK_GROUP_RAID1;
  805. fs_info->system_alloc_profile = BTRFS_BLOCK_GROUP_RAID0;
  806. }
  807. mutex_unlock(&fs_info->fs_mutex);
  808. return tree_root;
  809. fail_extent_root:
  810. free_extent_buffer(extent_root->node);
  811. fail_tree_root:
  812. mutex_unlock(&fs_info->fs_mutex);
  813. free_extent_buffer(tree_root->node);
  814. fail_sb_buffer:
  815. free_extent_buffer(fs_info->sb_buffer);
  816. extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
  817. fail_iput:
  818. iput(fs_info->btree_inode);
  819. fail:
  820. close_all_devices(fs_info);
  821. kfree(extent_root);
  822. kfree(tree_root);
  823. bdi_destroy(&fs_info->bdi);
  824. kfree(fs_info);
  825. return ERR_PTR(err);
  826. }
  827. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  828. *root)
  829. {
  830. int ret;
  831. struct extent_buffer *super = root->fs_info->sb_buffer;
  832. struct inode *btree_inode = root->fs_info->btree_inode;
  833. struct super_block *sb = root->fs_info->sb;
  834. if (!btrfs_test_opt(root, NOBARRIER))
  835. blkdev_issue_flush(sb->s_bdev, NULL);
  836. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
  837. ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
  838. super->start, super->len);
  839. if (!btrfs_test_opt(root, NOBARRIER))
  840. blkdev_issue_flush(sb->s_bdev, NULL);
  841. return ret;
  842. }
  843. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  844. {
  845. radix_tree_delete(&fs_info->fs_roots_radix,
  846. (unsigned long)root->root_key.objectid);
  847. if (root->in_sysfs)
  848. btrfs_sysfs_del_root(root);
  849. if (root->inode)
  850. iput(root->inode);
  851. if (root->node)
  852. free_extent_buffer(root->node);
  853. if (root->commit_root)
  854. free_extent_buffer(root->commit_root);
  855. if (root->name)
  856. kfree(root->name);
  857. kfree(root);
  858. return 0;
  859. }
  860. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  861. {
  862. int ret;
  863. struct btrfs_root *gang[8];
  864. int i;
  865. while(1) {
  866. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  867. (void **)gang, 0,
  868. ARRAY_SIZE(gang));
  869. if (!ret)
  870. break;
  871. for (i = 0; i < ret; i++)
  872. btrfs_free_fs_root(fs_info, gang[i]);
  873. }
  874. return 0;
  875. }
  876. int close_ctree(struct btrfs_root *root)
  877. {
  878. int ret;
  879. struct btrfs_trans_handle *trans;
  880. struct btrfs_fs_info *fs_info = root->fs_info;
  881. fs_info->closing = 1;
  882. btrfs_transaction_flush_work(root);
  883. mutex_lock(&fs_info->fs_mutex);
  884. btrfs_defrag_dirty_roots(root->fs_info);
  885. trans = btrfs_start_transaction(root, 1);
  886. ret = btrfs_commit_transaction(trans, root);
  887. /* run commit again to drop the original snapshot */
  888. trans = btrfs_start_transaction(root, 1);
  889. btrfs_commit_transaction(trans, root);
  890. ret = btrfs_write_and_wait_transaction(NULL, root);
  891. BUG_ON(ret);
  892. write_ctree_super(NULL, root);
  893. mutex_unlock(&fs_info->fs_mutex);
  894. if (fs_info->delalloc_bytes) {
  895. printk("btrfs: at unmount delalloc count %Lu\n",
  896. fs_info->delalloc_bytes);
  897. }
  898. if (fs_info->extent_root->node)
  899. free_extent_buffer(fs_info->extent_root->node);
  900. if (fs_info->tree_root->node)
  901. free_extent_buffer(fs_info->tree_root->node);
  902. if (root->fs_info->chunk_root->node);
  903. free_extent_buffer(root->fs_info->chunk_root->node);
  904. if (root->fs_info->dev_root->node);
  905. free_extent_buffer(root->fs_info->dev_root->node);
  906. free_extent_buffer(fs_info->sb_buffer);
  907. btrfs_free_block_groups(root->fs_info);
  908. del_fs_roots(fs_info);
  909. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  910. extent_io_tree_empty_lru(&fs_info->free_space_cache);
  911. extent_io_tree_empty_lru(&fs_info->block_group_cache);
  912. extent_io_tree_empty_lru(&fs_info->pinned_extents);
  913. extent_io_tree_empty_lru(&fs_info->pending_del);
  914. extent_io_tree_empty_lru(&fs_info->extent_ins);
  915. extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
  916. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  917. iput(fs_info->btree_inode);
  918. #if 0
  919. while(!list_empty(&fs_info->hashers)) {
  920. struct btrfs_hasher *hasher;
  921. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  922. hashers);
  923. list_del(&hasher->hashers);
  924. crypto_free_hash(&fs_info->hash_tfm);
  925. kfree(hasher);
  926. }
  927. #endif
  928. close_all_devices(fs_info);
  929. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  930. bdi_destroy(&fs_info->bdi);
  931. kfree(fs_info->extent_root);
  932. kfree(fs_info->tree_root);
  933. kfree(fs_info->chunk_root);
  934. kfree(fs_info->dev_root);
  935. return 0;
  936. }
  937. int btrfs_buffer_uptodate(struct extent_buffer *buf)
  938. {
  939. struct inode *btree_inode = buf->first_page->mapping->host;
  940. return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  941. }
  942. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  943. {
  944. struct inode *btree_inode = buf->first_page->mapping->host;
  945. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  946. buf);
  947. }
  948. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  949. {
  950. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  951. u64 transid = btrfs_header_generation(buf);
  952. struct inode *btree_inode = root->fs_info->btree_inode;
  953. if (transid != root->fs_info->generation) {
  954. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  955. (unsigned long long)buf->start,
  956. transid, root->fs_info->generation);
  957. WARN_ON(1);
  958. }
  959. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  960. }
  961. void btrfs_throttle(struct btrfs_root *root)
  962. {
  963. struct backing_dev_info *bdi;
  964. bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
  965. if (root->fs_info->throttles && bdi_write_congested(bdi)) {
  966. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
  967. congestion_wait(WRITE, HZ/20);
  968. #else
  969. blk_congestion_wait(WRITE, HZ/20);
  970. #endif
  971. }
  972. }
  973. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  974. {
  975. balance_dirty_pages_ratelimited_nr(
  976. root->fs_info->btree_inode->i_mapping, 1);
  977. }
  978. void btrfs_set_buffer_defrag(struct extent_buffer *buf)
  979. {
  980. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  981. struct inode *btree_inode = root->fs_info->btree_inode;
  982. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  983. buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
  984. }
  985. void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
  986. {
  987. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  988. struct inode *btree_inode = root->fs_info->btree_inode;
  989. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  990. buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
  991. GFP_NOFS);
  992. }
  993. int btrfs_buffer_defrag(struct extent_buffer *buf)
  994. {
  995. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  996. struct inode *btree_inode = root->fs_info->btree_inode;
  997. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  998. buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
  999. }
  1000. int btrfs_buffer_defrag_done(struct extent_buffer *buf)
  1001. {
  1002. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1003. struct inode *btree_inode = root->fs_info->btree_inode;
  1004. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  1005. buf->start, buf->start + buf->len - 1,
  1006. EXTENT_DEFRAG_DONE, 0);
  1007. }
  1008. int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
  1009. {
  1010. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1011. struct inode *btree_inode = root->fs_info->btree_inode;
  1012. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  1013. buf->start, buf->start + buf->len - 1,
  1014. EXTENT_DEFRAG_DONE, GFP_NOFS);
  1015. }
  1016. int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
  1017. {
  1018. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1019. struct inode *btree_inode = root->fs_info->btree_inode;
  1020. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  1021. buf->start, buf->start + buf->len - 1,
  1022. EXTENT_DEFRAG, GFP_NOFS);
  1023. }
  1024. int btrfs_read_buffer(struct extent_buffer *buf)
  1025. {
  1026. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1027. struct inode *btree_inode = root->fs_info->btree_inode;
  1028. return read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  1029. buf, 0, 1, btree_get_extent);
  1030. }
  1031. static struct extent_io_ops btree_extent_io_ops = {
  1032. .writepage_io_hook = btree_writepage_io_hook,
  1033. .submit_bio_hook = btree_submit_bio_hook,
  1034. /* note we're sharing with inode.c for the merge bio hook */
  1035. .merge_bio_hook = btrfs_merge_bio_hook,
  1036. };