disk-io.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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/scatterlist.h>
  21. #include <linux/swap.h>
  22. #include <linux/radix-tree.h>
  23. #include <linux/writeback.h>
  24. #include <linux/buffer_head.h> // for block_sync_page
  25. #include <linux/workqueue.h>
  26. #include "crc32c.h"
  27. #include "ctree.h"
  28. #include "disk-io.h"
  29. #include "transaction.h"
  30. #include "btrfs_inode.h"
  31. #include "volumes.h"
  32. #include "print-tree.h"
  33. #if 0
  34. static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
  35. {
  36. if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
  37. printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
  38. (unsigned long long)extent_buffer_blocknr(buf),
  39. (unsigned long long)btrfs_header_blocknr(buf));
  40. return 1;
  41. }
  42. return 0;
  43. }
  44. #endif
  45. static struct extent_io_ops btree_extent_io_ops;
  46. static struct workqueue_struct *end_io_workqueue;
  47. static struct workqueue_struct *async_submit_workqueue;
  48. struct end_io_wq {
  49. struct bio *bio;
  50. bio_end_io_t *end_io;
  51. void *private;
  52. struct btrfs_fs_info *info;
  53. int error;
  54. int metadata;
  55. struct list_head list;
  56. };
  57. struct async_submit_bio {
  58. struct inode *inode;
  59. struct bio *bio;
  60. struct list_head list;
  61. extent_submit_bio_hook_t *submit_bio_hook;
  62. int rw;
  63. int mirror_num;
  64. };
  65. struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
  66. size_t page_offset, u64 start, u64 len,
  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. spin_lock(&em_tree->lock);
  73. em = lookup_extent_mapping(em_tree, start, len);
  74. if (em) {
  75. em->bdev =
  76. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  77. spin_unlock(&em_tree->lock);
  78. goto out;
  79. }
  80. spin_unlock(&em_tree->lock);
  81. em = alloc_extent_map(GFP_NOFS);
  82. if (!em) {
  83. em = ERR_PTR(-ENOMEM);
  84. goto out;
  85. }
  86. em->start = 0;
  87. em->len = (u64)-1;
  88. em->block_start = 0;
  89. em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  90. spin_lock(&em_tree->lock);
  91. ret = add_extent_mapping(em_tree, em);
  92. if (ret == -EEXIST) {
  93. u64 failed_start = em->start;
  94. u64 failed_len = em->len;
  95. printk("failed to insert %Lu %Lu -> %Lu into tree\n",
  96. em->start, em->len, em->block_start);
  97. free_extent_map(em);
  98. em = lookup_extent_mapping(em_tree, start, len);
  99. if (em) {
  100. printk("after failing, found %Lu %Lu %Lu\n",
  101. em->start, em->len, em->block_start);
  102. ret = 0;
  103. } else {
  104. em = lookup_extent_mapping(em_tree, failed_start,
  105. failed_len);
  106. if (em) {
  107. printk("double failure lookup gives us "
  108. "%Lu %Lu -> %Lu\n", em->start,
  109. em->len, em->block_start);
  110. free_extent_map(em);
  111. }
  112. ret = -EIO;
  113. }
  114. } else if (ret) {
  115. free_extent_map(em);
  116. em = NULL;
  117. }
  118. spin_unlock(&em_tree->lock);
  119. if (ret)
  120. em = ERR_PTR(ret);
  121. out:
  122. return em;
  123. }
  124. u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
  125. {
  126. return btrfs_crc32c(seed, data, len);
  127. }
  128. void btrfs_csum_final(u32 crc, char *result)
  129. {
  130. *(__le32 *)result = ~cpu_to_le32(crc);
  131. }
  132. static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
  133. int verify)
  134. {
  135. char result[BTRFS_CRC32_SIZE];
  136. unsigned long len;
  137. unsigned long cur_len;
  138. unsigned long offset = BTRFS_CSUM_SIZE;
  139. char *map_token = NULL;
  140. char *kaddr;
  141. unsigned long map_start;
  142. unsigned long map_len;
  143. int err;
  144. u32 crc = ~(u32)0;
  145. len = buf->len - offset;
  146. while(len > 0) {
  147. err = map_private_extent_buffer(buf, offset, 32,
  148. &map_token, &kaddr,
  149. &map_start, &map_len, KM_USER0);
  150. if (err) {
  151. printk("failed to map extent buffer! %lu\n",
  152. offset);
  153. return 1;
  154. }
  155. cur_len = min(len, map_len - (offset - map_start));
  156. crc = btrfs_csum_data(root, kaddr + offset - map_start,
  157. crc, cur_len);
  158. len -= cur_len;
  159. offset += cur_len;
  160. unmap_extent_buffer(buf, map_token, KM_USER0);
  161. }
  162. btrfs_csum_final(crc, result);
  163. if (verify) {
  164. int from_this_trans = 0;
  165. if (root->fs_info->running_transaction &&
  166. btrfs_header_generation(buf) ==
  167. root->fs_info->running_transaction->transid)
  168. from_this_trans = 1;
  169. /* FIXME, this is not good */
  170. if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
  171. u32 val;
  172. u32 found = 0;
  173. memcpy(&found, result, BTRFS_CRC32_SIZE);
  174. read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
  175. printk("btrfs: %s checksum verify failed on %llu "
  176. "wanted %X found %X from_this_trans %d "
  177. "level %d\n",
  178. root->fs_info->sb->s_id,
  179. buf->start, val, found, from_this_trans,
  180. btrfs_header_level(buf));
  181. return 1;
  182. }
  183. } else {
  184. write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
  185. }
  186. return 0;
  187. }
  188. static int verify_parent_transid(struct extent_io_tree *io_tree,
  189. struct extent_buffer *eb, u64 parent_transid)
  190. {
  191. int ret;
  192. if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
  193. return 0;
  194. lock_extent(io_tree, eb->start, eb->start + eb->len - 1, GFP_NOFS);
  195. if (extent_buffer_uptodate(io_tree, eb) &&
  196. btrfs_header_generation(eb) == parent_transid) {
  197. ret = 0;
  198. goto out;
  199. }
  200. printk("parent transid verify failed on %llu wanted %llu found %llu\n",
  201. (unsigned long long)eb->start,
  202. (unsigned long long)parent_transid,
  203. (unsigned long long)btrfs_header_generation(eb));
  204. ret = 1;
  205. out:
  206. clear_extent_buffer_uptodate(io_tree, eb);
  207. unlock_extent(io_tree, eb->start, eb->start + eb->len - 1,
  208. GFP_NOFS);
  209. return ret;
  210. }
  211. static int btree_read_extent_buffer_pages(struct btrfs_root *root,
  212. struct extent_buffer *eb,
  213. u64 start, u64 parent_transid)
  214. {
  215. struct extent_io_tree *io_tree;
  216. int ret;
  217. int num_copies = 0;
  218. int mirror_num = 0;
  219. io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  220. while (1) {
  221. ret = read_extent_buffer_pages(io_tree, eb, start, 1,
  222. btree_get_extent, mirror_num);
  223. if (!ret &&
  224. !verify_parent_transid(io_tree, eb, parent_transid))
  225. return ret;
  226. num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
  227. eb->start, eb->len);
  228. if (num_copies == 1)
  229. return ret;
  230. mirror_num++;
  231. if (mirror_num > num_copies)
  232. return ret;
  233. }
  234. return -EIO;
  235. }
  236. int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
  237. {
  238. struct extent_io_tree *tree;
  239. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  240. u64 found_start;
  241. int found_level;
  242. unsigned long len;
  243. struct extent_buffer *eb;
  244. int ret;
  245. tree = &BTRFS_I(page->mapping->host)->io_tree;
  246. if (page->private == EXTENT_PAGE_PRIVATE)
  247. goto out;
  248. if (!page->private)
  249. goto out;
  250. len = page->private >> 2;
  251. if (len == 0) {
  252. WARN_ON(1);
  253. }
  254. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  255. ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
  256. btrfs_header_generation(eb));
  257. BUG_ON(ret);
  258. btrfs_clear_buffer_defrag(eb);
  259. found_start = btrfs_header_bytenr(eb);
  260. if (found_start != start) {
  261. printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
  262. start, found_start, len);
  263. WARN_ON(1);
  264. goto err;
  265. }
  266. if (eb->first_page != page) {
  267. printk("bad first page %lu %lu\n", eb->first_page->index,
  268. page->index);
  269. WARN_ON(1);
  270. goto err;
  271. }
  272. if (!PageUptodate(page)) {
  273. printk("csum not up to date page %lu\n", page->index);
  274. WARN_ON(1);
  275. goto err;
  276. }
  277. found_level = btrfs_header_level(eb);
  278. spin_lock(&root->fs_info->hash_lock);
  279. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  280. spin_unlock(&root->fs_info->hash_lock);
  281. csum_tree_block(root, eb, 0);
  282. err:
  283. free_extent_buffer(eb);
  284. out:
  285. return 0;
  286. }
  287. static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
  288. {
  289. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  290. csum_dirty_buffer(root, page);
  291. return 0;
  292. }
  293. int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  294. struct extent_state *state)
  295. {
  296. struct extent_io_tree *tree;
  297. u64 found_start;
  298. int found_level;
  299. unsigned long len;
  300. struct extent_buffer *eb;
  301. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  302. int ret = 0;
  303. tree = &BTRFS_I(page->mapping->host)->io_tree;
  304. if (page->private == EXTENT_PAGE_PRIVATE)
  305. goto out;
  306. if (!page->private)
  307. goto out;
  308. len = page->private >> 2;
  309. if (len == 0) {
  310. WARN_ON(1);
  311. }
  312. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  313. btrfs_clear_buffer_defrag(eb);
  314. found_start = btrfs_header_bytenr(eb);
  315. if (found_start != start) {
  316. ret = -EIO;
  317. goto err;
  318. }
  319. if (eb->first_page != page) {
  320. printk("bad first page %lu %lu\n", eb->first_page->index,
  321. page->index);
  322. WARN_ON(1);
  323. ret = -EIO;
  324. goto err;
  325. }
  326. if (memcmp_extent_buffer(eb, root->fs_info->fsid,
  327. (unsigned long)btrfs_header_fsid(eb),
  328. BTRFS_FSID_SIZE)) {
  329. printk("bad fsid on block %Lu\n", eb->start);
  330. ret = -EIO;
  331. goto err;
  332. }
  333. found_level = btrfs_header_level(eb);
  334. ret = csum_tree_block(root, eb, 1);
  335. if (ret)
  336. ret = -EIO;
  337. end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
  338. end = eb->start + end - 1;
  339. release_extent_buffer_tail_pages(eb);
  340. err:
  341. free_extent_buffer(eb);
  342. out:
  343. return ret;
  344. }
  345. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  346. static void end_workqueue_bio(struct bio *bio, int err)
  347. #else
  348. static int end_workqueue_bio(struct bio *bio,
  349. unsigned int bytes_done, int err)
  350. #endif
  351. {
  352. struct end_io_wq *end_io_wq = bio->bi_private;
  353. struct btrfs_fs_info *fs_info;
  354. unsigned long flags;
  355. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  356. if (bio->bi_size)
  357. return 1;
  358. #endif
  359. fs_info = end_io_wq->info;
  360. spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
  361. end_io_wq->error = err;
  362. list_add_tail(&end_io_wq->list, &fs_info->end_io_work_list);
  363. spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
  364. queue_work(end_io_workqueue, &fs_info->end_io_work);
  365. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  366. return 0;
  367. #endif
  368. }
  369. int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
  370. int metadata)
  371. {
  372. struct end_io_wq *end_io_wq;
  373. end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
  374. if (!end_io_wq)
  375. return -ENOMEM;
  376. end_io_wq->private = bio->bi_private;
  377. end_io_wq->end_io = bio->bi_end_io;
  378. end_io_wq->info = info;
  379. end_io_wq->error = 0;
  380. end_io_wq->bio = bio;
  381. end_io_wq->metadata = metadata;
  382. bio->bi_private = end_io_wq;
  383. bio->bi_end_io = end_workqueue_bio;
  384. return 0;
  385. }
  386. int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
  387. int rw, struct bio *bio, int mirror_num,
  388. extent_submit_bio_hook_t *submit_bio_hook)
  389. {
  390. struct async_submit_bio *async;
  391. /*
  392. * inline writerback should stay inline, only hop to the async
  393. * queue if we're pdflush
  394. */
  395. if (!current_is_pdflush())
  396. return submit_bio_hook(inode, rw, bio, mirror_num);
  397. async = kmalloc(sizeof(*async), GFP_NOFS);
  398. if (!async)
  399. return -ENOMEM;
  400. async->inode = inode;
  401. async->rw = rw;
  402. async->bio = bio;
  403. async->mirror_num = mirror_num;
  404. async->submit_bio_hook = submit_bio_hook;
  405. spin_lock(&fs_info->async_submit_work_lock);
  406. list_add_tail(&async->list, &fs_info->async_submit_work_list);
  407. spin_unlock(&fs_info->async_submit_work_lock);
  408. queue_work(async_submit_workqueue, &fs_info->async_submit_work);
  409. return 0;
  410. }
  411. static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  412. int mirror_num)
  413. {
  414. struct btrfs_root *root = BTRFS_I(inode)->root;
  415. u64 offset;
  416. int ret;
  417. offset = bio->bi_sector << 9;
  418. if (rw & (1 << BIO_RW)) {
  419. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num);
  420. }
  421. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
  422. BUG_ON(ret);
  423. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num);
  424. }
  425. static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  426. int mirror_num)
  427. {
  428. if (!(rw & (1 << BIO_RW))) {
  429. return __btree_submit_bio_hook(inode, rw, bio, mirror_num);
  430. }
  431. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  432. inode, rw, bio, mirror_num,
  433. __btree_submit_bio_hook);
  434. }
  435. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  436. {
  437. struct extent_io_tree *tree;
  438. tree = &BTRFS_I(page->mapping->host)->io_tree;
  439. return extent_write_full_page(tree, page, btree_get_extent, wbc);
  440. }
  441. static int btree_writepages(struct address_space *mapping,
  442. struct writeback_control *wbc)
  443. {
  444. struct extent_io_tree *tree;
  445. tree = &BTRFS_I(mapping->host)->io_tree;
  446. if (wbc->sync_mode == WB_SYNC_NONE) {
  447. u64 num_dirty;
  448. u64 start = 0;
  449. unsigned long thresh = 96 * 1024 * 1024;
  450. if (wbc->for_kupdate)
  451. return 0;
  452. if (current_is_pdflush()) {
  453. thresh = 96 * 1024 * 1024;
  454. } else {
  455. thresh = 8 * 1024 * 1024;
  456. }
  457. num_dirty = count_range_bits(tree, &start, (u64)-1,
  458. thresh, EXTENT_DIRTY);
  459. if (num_dirty < thresh) {
  460. return 0;
  461. }
  462. }
  463. return extent_writepages(tree, mapping, btree_get_extent, wbc);
  464. }
  465. int btree_readpage(struct file *file, struct page *page)
  466. {
  467. struct extent_io_tree *tree;
  468. tree = &BTRFS_I(page->mapping->host)->io_tree;
  469. return extent_read_full_page(tree, page, btree_get_extent);
  470. }
  471. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  472. {
  473. struct extent_io_tree *tree;
  474. struct extent_map_tree *map;
  475. int ret;
  476. if (page_count(page) > 3) {
  477. /* once for page->private, once for the caller, once
  478. * once for the page cache
  479. */
  480. return 0;
  481. }
  482. tree = &BTRFS_I(page->mapping->host)->io_tree;
  483. map = &BTRFS_I(page->mapping->host)->extent_tree;
  484. ret = try_release_extent_state(map, tree, page, gfp_flags);
  485. if (ret == 1) {
  486. invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
  487. ClearPagePrivate(page);
  488. set_page_private(page, 0);
  489. page_cache_release(page);
  490. }
  491. return ret;
  492. }
  493. static void btree_invalidatepage(struct page *page, unsigned long offset)
  494. {
  495. struct extent_io_tree *tree;
  496. tree = &BTRFS_I(page->mapping->host)->io_tree;
  497. extent_invalidatepage(tree, page, offset);
  498. btree_releasepage(page, GFP_NOFS);
  499. if (PagePrivate(page)) {
  500. invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
  501. ClearPagePrivate(page);
  502. set_page_private(page, 0);
  503. page_cache_release(page);
  504. }
  505. }
  506. #if 0
  507. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  508. {
  509. struct buffer_head *bh;
  510. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  511. struct buffer_head *head;
  512. if (!page_has_buffers(page)) {
  513. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  514. (1 << BH_Dirty)|(1 << BH_Uptodate));
  515. }
  516. head = page_buffers(page);
  517. bh = head;
  518. do {
  519. if (buffer_dirty(bh))
  520. csum_tree_block(root, bh, 0);
  521. bh = bh->b_this_page;
  522. } while (bh != head);
  523. return block_write_full_page(page, btree_get_block, wbc);
  524. }
  525. #endif
  526. static struct address_space_operations btree_aops = {
  527. .readpage = btree_readpage,
  528. .writepage = btree_writepage,
  529. .writepages = btree_writepages,
  530. .releasepage = btree_releasepage,
  531. .invalidatepage = btree_invalidatepage,
  532. .sync_page = block_sync_page,
  533. };
  534. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
  535. u64 parent_transid)
  536. {
  537. struct extent_buffer *buf = NULL;
  538. struct inode *btree_inode = root->fs_info->btree_inode;
  539. int ret = 0;
  540. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  541. if (!buf)
  542. return 0;
  543. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  544. buf, 0, 0, btree_get_extent, 0);
  545. free_extent_buffer(buf);
  546. return ret;
  547. }
  548. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  549. u64 bytenr, u32 blocksize)
  550. {
  551. struct inode *btree_inode = root->fs_info->btree_inode;
  552. struct extent_buffer *eb;
  553. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  554. bytenr, blocksize, GFP_NOFS);
  555. return eb;
  556. }
  557. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  558. u64 bytenr, u32 blocksize)
  559. {
  560. struct inode *btree_inode = root->fs_info->btree_inode;
  561. struct extent_buffer *eb;
  562. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  563. bytenr, blocksize, NULL, GFP_NOFS);
  564. return eb;
  565. }
  566. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  567. u32 blocksize, u64 parent_transid)
  568. {
  569. struct extent_buffer *buf = NULL;
  570. struct inode *btree_inode = root->fs_info->btree_inode;
  571. struct extent_io_tree *io_tree;
  572. int ret;
  573. io_tree = &BTRFS_I(btree_inode)->io_tree;
  574. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  575. if (!buf)
  576. return NULL;
  577. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  578. if (ret == 0) {
  579. buf->flags |= EXTENT_UPTODATE;
  580. }
  581. return buf;
  582. }
  583. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  584. struct extent_buffer *buf)
  585. {
  586. struct inode *btree_inode = root->fs_info->btree_inode;
  587. if (btrfs_header_generation(buf) ==
  588. root->fs_info->running_transaction->transid)
  589. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  590. buf);
  591. return 0;
  592. }
  593. int wait_on_tree_block_writeback(struct btrfs_root *root,
  594. struct extent_buffer *buf)
  595. {
  596. struct inode *btree_inode = root->fs_info->btree_inode;
  597. wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
  598. buf);
  599. return 0;
  600. }
  601. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  602. u32 stripesize, struct btrfs_root *root,
  603. struct btrfs_fs_info *fs_info,
  604. u64 objectid)
  605. {
  606. root->node = NULL;
  607. root->inode = NULL;
  608. root->commit_root = NULL;
  609. root->sectorsize = sectorsize;
  610. root->nodesize = nodesize;
  611. root->leafsize = leafsize;
  612. root->stripesize = stripesize;
  613. root->ref_cows = 0;
  614. root->track_dirty = 0;
  615. root->fs_info = fs_info;
  616. root->objectid = objectid;
  617. root->last_trans = 0;
  618. root->highest_inode = 0;
  619. root->last_inode_alloc = 0;
  620. root->name = NULL;
  621. root->in_sysfs = 0;
  622. INIT_LIST_HEAD(&root->dirty_list);
  623. memset(&root->root_key, 0, sizeof(root->root_key));
  624. memset(&root->root_item, 0, sizeof(root->root_item));
  625. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  626. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  627. init_completion(&root->kobj_unregister);
  628. root->defrag_running = 0;
  629. root->defrag_level = 0;
  630. root->root_key.objectid = objectid;
  631. return 0;
  632. }
  633. static int find_and_setup_root(struct btrfs_root *tree_root,
  634. struct btrfs_fs_info *fs_info,
  635. u64 objectid,
  636. struct btrfs_root *root)
  637. {
  638. int ret;
  639. u32 blocksize;
  640. __setup_root(tree_root->nodesize, tree_root->leafsize,
  641. tree_root->sectorsize, tree_root->stripesize,
  642. root, fs_info, objectid);
  643. ret = btrfs_find_last_root(tree_root, objectid,
  644. &root->root_item, &root->root_key);
  645. BUG_ON(ret);
  646. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  647. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  648. blocksize, 0);
  649. BUG_ON(!root->node);
  650. return 0;
  651. }
  652. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
  653. struct btrfs_key *location)
  654. {
  655. struct btrfs_root *root;
  656. struct btrfs_root *tree_root = fs_info->tree_root;
  657. struct btrfs_path *path;
  658. struct extent_buffer *l;
  659. u64 highest_inode;
  660. u32 blocksize;
  661. int ret = 0;
  662. root = kzalloc(sizeof(*root), GFP_NOFS);
  663. if (!root)
  664. return ERR_PTR(-ENOMEM);
  665. if (location->offset == (u64)-1) {
  666. ret = find_and_setup_root(tree_root, fs_info,
  667. location->objectid, root);
  668. if (ret) {
  669. kfree(root);
  670. return ERR_PTR(ret);
  671. }
  672. goto insert;
  673. }
  674. __setup_root(tree_root->nodesize, tree_root->leafsize,
  675. tree_root->sectorsize, tree_root->stripesize,
  676. root, fs_info, location->objectid);
  677. path = btrfs_alloc_path();
  678. BUG_ON(!path);
  679. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  680. if (ret != 0) {
  681. if (ret > 0)
  682. ret = -ENOENT;
  683. goto out;
  684. }
  685. l = path->nodes[0];
  686. read_extent_buffer(l, &root->root_item,
  687. btrfs_item_ptr_offset(l, path->slots[0]),
  688. sizeof(root->root_item));
  689. memcpy(&root->root_key, location, sizeof(*location));
  690. ret = 0;
  691. out:
  692. btrfs_release_path(root, path);
  693. btrfs_free_path(path);
  694. if (ret) {
  695. kfree(root);
  696. return ERR_PTR(ret);
  697. }
  698. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  699. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  700. blocksize, 0);
  701. BUG_ON(!root->node);
  702. insert:
  703. root->ref_cows = 1;
  704. ret = btrfs_find_highest_inode(root, &highest_inode);
  705. if (ret == 0) {
  706. root->highest_inode = highest_inode;
  707. root->last_inode_alloc = highest_inode;
  708. }
  709. return root;
  710. }
  711. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  712. u64 root_objectid)
  713. {
  714. struct btrfs_root *root;
  715. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  716. return fs_info->tree_root;
  717. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  718. return fs_info->extent_root;
  719. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  720. (unsigned long)root_objectid);
  721. return root;
  722. }
  723. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  724. struct btrfs_key *location)
  725. {
  726. struct btrfs_root *root;
  727. int ret;
  728. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  729. return fs_info->tree_root;
  730. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  731. return fs_info->extent_root;
  732. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  733. return fs_info->chunk_root;
  734. if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
  735. return fs_info->dev_root;
  736. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  737. (unsigned long)location->objectid);
  738. if (root)
  739. return root;
  740. root = btrfs_read_fs_root_no_radix(fs_info, location);
  741. if (IS_ERR(root))
  742. return root;
  743. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  744. (unsigned long)root->root_key.objectid,
  745. root);
  746. if (ret) {
  747. free_extent_buffer(root->node);
  748. kfree(root);
  749. return ERR_PTR(ret);
  750. }
  751. ret = btrfs_find_dead_roots(fs_info->tree_root,
  752. root->root_key.objectid, root);
  753. BUG_ON(ret);
  754. return root;
  755. }
  756. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  757. struct btrfs_key *location,
  758. const char *name, int namelen)
  759. {
  760. struct btrfs_root *root;
  761. int ret;
  762. root = btrfs_read_fs_root_no_name(fs_info, location);
  763. if (!root)
  764. return NULL;
  765. if (root->in_sysfs)
  766. return root;
  767. ret = btrfs_set_root_name(root, name, namelen);
  768. if (ret) {
  769. free_extent_buffer(root->node);
  770. kfree(root);
  771. return ERR_PTR(ret);
  772. }
  773. ret = btrfs_sysfs_add_root(root);
  774. if (ret) {
  775. free_extent_buffer(root->node);
  776. kfree(root->name);
  777. kfree(root);
  778. return ERR_PTR(ret);
  779. }
  780. root->in_sysfs = 1;
  781. return root;
  782. }
  783. #if 0
  784. static int add_hasher(struct btrfs_fs_info *info, char *type) {
  785. struct btrfs_hasher *hasher;
  786. hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
  787. if (!hasher)
  788. return -ENOMEM;
  789. hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
  790. if (!hasher->hash_tfm) {
  791. kfree(hasher);
  792. return -EINVAL;
  793. }
  794. spin_lock(&info->hash_lock);
  795. list_add(&hasher->list, &info->hashers);
  796. spin_unlock(&info->hash_lock);
  797. return 0;
  798. }
  799. #endif
  800. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  801. {
  802. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  803. int ret = 0;
  804. struct list_head *cur;
  805. struct btrfs_device *device;
  806. struct backing_dev_info *bdi;
  807. list_for_each(cur, &info->fs_devices->devices) {
  808. device = list_entry(cur, struct btrfs_device, dev_list);
  809. if (!device->bdev)
  810. continue;
  811. bdi = blk_get_backing_dev_info(device->bdev);
  812. if (bdi && bdi_congested(bdi, bdi_bits)) {
  813. ret = 1;
  814. break;
  815. }
  816. }
  817. return ret;
  818. }
  819. /*
  820. * this unplugs every device on the box, and it is only used when page
  821. * is null
  822. */
  823. static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  824. {
  825. struct list_head *cur;
  826. struct btrfs_device *device;
  827. struct btrfs_fs_info *info;
  828. info = (struct btrfs_fs_info *)bdi->unplug_io_data;
  829. list_for_each(cur, &info->fs_devices->devices) {
  830. device = list_entry(cur, struct btrfs_device, dev_list);
  831. bdi = blk_get_backing_dev_info(device->bdev);
  832. if (bdi->unplug_io_fn) {
  833. bdi->unplug_io_fn(bdi, page);
  834. }
  835. }
  836. }
  837. void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  838. {
  839. struct inode *inode;
  840. struct extent_map_tree *em_tree;
  841. struct extent_map *em;
  842. struct address_space *mapping;
  843. u64 offset;
  844. /* the generic O_DIRECT read code does this */
  845. if (!page) {
  846. __unplug_io_fn(bdi, page);
  847. return;
  848. }
  849. /*
  850. * page->mapping may change at any time. Get a consistent copy
  851. * and use that for everything below
  852. */
  853. smp_mb();
  854. mapping = page->mapping;
  855. if (!mapping)
  856. return;
  857. inode = mapping->host;
  858. offset = page_offset(page);
  859. em_tree = &BTRFS_I(inode)->extent_tree;
  860. spin_lock(&em_tree->lock);
  861. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  862. spin_unlock(&em_tree->lock);
  863. if (!em)
  864. return;
  865. offset = offset - em->start;
  866. btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
  867. em->block_start + offset, page);
  868. free_extent_map(em);
  869. }
  870. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  871. {
  872. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
  873. bdi_init(bdi);
  874. #endif
  875. bdi->ra_pages = default_backing_dev_info.ra_pages;
  876. bdi->state = 0;
  877. bdi->capabilities = default_backing_dev_info.capabilities;
  878. bdi->unplug_io_fn = btrfs_unplug_io_fn;
  879. bdi->unplug_io_data = info;
  880. bdi->congested_fn = btrfs_congested_fn;
  881. bdi->congested_data = info;
  882. return 0;
  883. }
  884. static int bio_ready_for_csum(struct bio *bio)
  885. {
  886. u64 length = 0;
  887. u64 buf_len = 0;
  888. u64 start = 0;
  889. struct page *page;
  890. struct extent_io_tree *io_tree = NULL;
  891. struct btrfs_fs_info *info = NULL;
  892. struct bio_vec *bvec;
  893. int i;
  894. int ret;
  895. bio_for_each_segment(bvec, bio, i) {
  896. page = bvec->bv_page;
  897. if (page->private == EXTENT_PAGE_PRIVATE) {
  898. length += bvec->bv_len;
  899. continue;
  900. }
  901. if (!page->private) {
  902. length += bvec->bv_len;
  903. continue;
  904. }
  905. length = bvec->bv_len;
  906. buf_len = page->private >> 2;
  907. start = page_offset(page) + bvec->bv_offset;
  908. io_tree = &BTRFS_I(page->mapping->host)->io_tree;
  909. info = BTRFS_I(page->mapping->host)->root->fs_info;
  910. }
  911. /* are we fully contained in this bio? */
  912. if (buf_len <= length)
  913. return 1;
  914. ret = extent_range_uptodate(io_tree, start + length,
  915. start + buf_len - 1);
  916. if (ret == 1)
  917. return ret;
  918. return ret;
  919. }
  920. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  921. static void btrfs_end_io_csum(void *p)
  922. #else
  923. static void btrfs_end_io_csum(struct work_struct *work)
  924. #endif
  925. {
  926. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  927. struct btrfs_fs_info *fs_info = p;
  928. #else
  929. struct btrfs_fs_info *fs_info = container_of(work,
  930. struct btrfs_fs_info,
  931. end_io_work);
  932. #endif
  933. unsigned long flags;
  934. struct end_io_wq *end_io_wq;
  935. struct bio *bio;
  936. struct list_head *next;
  937. int error;
  938. int was_empty;
  939. while(1) {
  940. spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
  941. if (list_empty(&fs_info->end_io_work_list)) {
  942. spin_unlock_irqrestore(&fs_info->end_io_work_lock,
  943. flags);
  944. return;
  945. }
  946. next = fs_info->end_io_work_list.next;
  947. list_del(next);
  948. spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
  949. end_io_wq = list_entry(next, struct end_io_wq, list);
  950. bio = end_io_wq->bio;
  951. if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
  952. spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
  953. was_empty = list_empty(&fs_info->end_io_work_list);
  954. list_add_tail(&end_io_wq->list,
  955. &fs_info->end_io_work_list);
  956. spin_unlock_irqrestore(&fs_info->end_io_work_lock,
  957. flags);
  958. if (was_empty)
  959. return;
  960. continue;
  961. }
  962. error = end_io_wq->error;
  963. bio->bi_private = end_io_wq->private;
  964. bio->bi_end_io = end_io_wq->end_io;
  965. kfree(end_io_wq);
  966. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  967. bio_endio(bio, bio->bi_size, error);
  968. #else
  969. bio_endio(bio, error);
  970. #endif
  971. }
  972. }
  973. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  974. static void btrfs_async_submit_work(void *p)
  975. #else
  976. static void btrfs_async_submit_work(struct work_struct *work)
  977. #endif
  978. {
  979. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  980. struct btrfs_fs_info *fs_info = p;
  981. #else
  982. struct btrfs_fs_info *fs_info = container_of(work,
  983. struct btrfs_fs_info,
  984. async_submit_work);
  985. #endif
  986. struct async_submit_bio *async;
  987. struct list_head *next;
  988. while(1) {
  989. spin_lock(&fs_info->async_submit_work_lock);
  990. if (list_empty(&fs_info->async_submit_work_list)) {
  991. spin_unlock(&fs_info->async_submit_work_lock);
  992. return;
  993. }
  994. next = fs_info->async_submit_work_list.next;
  995. list_del(next);
  996. spin_unlock(&fs_info->async_submit_work_lock);
  997. async = list_entry(next, struct async_submit_bio, list);
  998. async->submit_bio_hook(async->inode, async->rw, async->bio,
  999. async->mirror_num);
  1000. kfree(async);
  1001. }
  1002. }
  1003. struct btrfs_root *open_ctree(struct super_block *sb,
  1004. struct btrfs_fs_devices *fs_devices,
  1005. char *options)
  1006. {
  1007. u32 sectorsize;
  1008. u32 nodesize;
  1009. u32 leafsize;
  1010. u32 blocksize;
  1011. u32 stripesize;
  1012. struct buffer_head *bh;
  1013. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  1014. GFP_NOFS);
  1015. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  1016. GFP_NOFS);
  1017. struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
  1018. GFP_NOFS);
  1019. struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
  1020. GFP_NOFS);
  1021. struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
  1022. GFP_NOFS);
  1023. int ret;
  1024. int err = -EINVAL;
  1025. struct btrfs_super_block *disk_super;
  1026. if (!extent_root || !tree_root || !fs_info) {
  1027. err = -ENOMEM;
  1028. goto fail;
  1029. }
  1030. end_io_workqueue = create_workqueue("btrfs-end-io");
  1031. BUG_ON(!end_io_workqueue);
  1032. async_submit_workqueue = create_workqueue("btrfs-async-submit");
  1033. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  1034. INIT_LIST_HEAD(&fs_info->trans_list);
  1035. INIT_LIST_HEAD(&fs_info->dead_roots);
  1036. INIT_LIST_HEAD(&fs_info->hashers);
  1037. INIT_LIST_HEAD(&fs_info->end_io_work_list);
  1038. INIT_LIST_HEAD(&fs_info->async_submit_work_list);
  1039. spin_lock_init(&fs_info->hash_lock);
  1040. spin_lock_init(&fs_info->end_io_work_lock);
  1041. spin_lock_init(&fs_info->async_submit_work_lock);
  1042. spin_lock_init(&fs_info->delalloc_lock);
  1043. spin_lock_init(&fs_info->new_trans_lock);
  1044. init_completion(&fs_info->kobj_unregister);
  1045. fs_info->tree_root = tree_root;
  1046. fs_info->extent_root = extent_root;
  1047. fs_info->chunk_root = chunk_root;
  1048. fs_info->dev_root = dev_root;
  1049. fs_info->fs_devices = fs_devices;
  1050. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  1051. INIT_LIST_HEAD(&fs_info->space_info);
  1052. btrfs_mapping_init(&fs_info->mapping_tree);
  1053. fs_info->sb = sb;
  1054. fs_info->max_extent = (u64)-1;
  1055. fs_info->max_inline = 8192 * 1024;
  1056. setup_bdi(fs_info, &fs_info->bdi);
  1057. fs_info->btree_inode = new_inode(sb);
  1058. fs_info->btree_inode->i_ino = 1;
  1059. fs_info->btree_inode->i_nlink = 1;
  1060. sb->s_blocksize = 4096;
  1061. sb->s_blocksize_bits = blksize_bits(4096);
  1062. /*
  1063. * we set the i_size on the btree inode to the max possible int.
  1064. * the real end of the address space is determined by all of
  1065. * the devices in the system
  1066. */
  1067. fs_info->btree_inode->i_size = OFFSET_MAX;
  1068. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  1069. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  1070. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  1071. fs_info->btree_inode->i_mapping,
  1072. GFP_NOFS);
  1073. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  1074. GFP_NOFS);
  1075. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  1076. extent_io_tree_init(&fs_info->free_space_cache,
  1077. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1078. extent_io_tree_init(&fs_info->block_group_cache,
  1079. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1080. extent_io_tree_init(&fs_info->pinned_extents,
  1081. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1082. extent_io_tree_init(&fs_info->pending_del,
  1083. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1084. extent_io_tree_init(&fs_info->extent_ins,
  1085. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1086. fs_info->do_barriers = 1;
  1087. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  1088. INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum, fs_info);
  1089. INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work,
  1090. fs_info);
  1091. INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
  1092. #else
  1093. INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum);
  1094. INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work);
  1095. INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
  1096. #endif
  1097. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  1098. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  1099. sizeof(struct btrfs_key));
  1100. insert_inode_hash(fs_info->btree_inode);
  1101. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  1102. mutex_init(&fs_info->trans_mutex);
  1103. mutex_init(&fs_info->fs_mutex);
  1104. #if 0
  1105. ret = add_hasher(fs_info, "crc32c");
  1106. if (ret) {
  1107. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  1108. err = -ENOMEM;
  1109. goto fail_iput;
  1110. }
  1111. #endif
  1112. __setup_root(4096, 4096, 4096, 4096, tree_root,
  1113. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  1114. bh = __bread(fs_devices->latest_bdev,
  1115. BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  1116. if (!bh)
  1117. goto fail_iput;
  1118. memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
  1119. brelse(bh);
  1120. memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
  1121. disk_super = &fs_info->super_copy;
  1122. if (!btrfs_super_root(disk_super))
  1123. goto fail_sb_buffer;
  1124. btrfs_parse_options(options, tree_root, NULL);
  1125. if (btrfs_super_num_devices(disk_super) > fs_devices->num_devices) {
  1126. printk("Btrfs: wanted %llu devices, but found %llu\n",
  1127. (unsigned long long)btrfs_super_num_devices(disk_super),
  1128. (unsigned long long)fs_devices->num_devices);
  1129. if (btrfs_test_opt(tree_root, DEGRADED))
  1130. printk("continuing in degraded mode\n");
  1131. else {
  1132. goto fail_sb_buffer;
  1133. }
  1134. }
  1135. fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
  1136. nodesize = btrfs_super_nodesize(disk_super);
  1137. leafsize = btrfs_super_leafsize(disk_super);
  1138. sectorsize = btrfs_super_sectorsize(disk_super);
  1139. stripesize = btrfs_super_stripesize(disk_super);
  1140. tree_root->nodesize = nodesize;
  1141. tree_root->leafsize = leafsize;
  1142. tree_root->sectorsize = sectorsize;
  1143. tree_root->stripesize = stripesize;
  1144. sb->s_blocksize = sectorsize;
  1145. sb->s_blocksize_bits = blksize_bits(sectorsize);
  1146. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  1147. sizeof(disk_super->magic))) {
  1148. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  1149. goto fail_sb_buffer;
  1150. }
  1151. mutex_lock(&fs_info->fs_mutex);
  1152. ret = btrfs_read_sys_array(tree_root);
  1153. if (ret) {
  1154. printk("btrfs: failed to read the system array on %s\n",
  1155. sb->s_id);
  1156. goto fail_sys_array;
  1157. }
  1158. blocksize = btrfs_level_size(tree_root,
  1159. btrfs_super_chunk_root_level(disk_super));
  1160. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1161. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  1162. chunk_root->node = read_tree_block(chunk_root,
  1163. btrfs_super_chunk_root(disk_super),
  1164. blocksize, 0);
  1165. BUG_ON(!chunk_root->node);
  1166. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  1167. (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
  1168. BTRFS_UUID_SIZE);
  1169. ret = btrfs_read_chunk_tree(chunk_root);
  1170. BUG_ON(ret);
  1171. btrfs_close_extra_devices(fs_devices);
  1172. blocksize = btrfs_level_size(tree_root,
  1173. btrfs_super_root_level(disk_super));
  1174. tree_root->node = read_tree_block(tree_root,
  1175. btrfs_super_root(disk_super),
  1176. blocksize, 0);
  1177. if (!tree_root->node)
  1178. goto fail_sb_buffer;
  1179. ret = find_and_setup_root(tree_root, fs_info,
  1180. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  1181. if (ret)
  1182. goto fail_tree_root;
  1183. extent_root->track_dirty = 1;
  1184. ret = find_and_setup_root(tree_root, fs_info,
  1185. BTRFS_DEV_TREE_OBJECTID, dev_root);
  1186. dev_root->track_dirty = 1;
  1187. if (ret)
  1188. goto fail_extent_root;
  1189. btrfs_read_block_groups(extent_root);
  1190. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  1191. fs_info->data_alloc_profile = (u64)-1;
  1192. fs_info->metadata_alloc_profile = (u64)-1;
  1193. fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
  1194. mutex_unlock(&fs_info->fs_mutex);
  1195. return tree_root;
  1196. fail_extent_root:
  1197. free_extent_buffer(extent_root->node);
  1198. fail_tree_root:
  1199. free_extent_buffer(tree_root->node);
  1200. fail_sys_array:
  1201. mutex_unlock(&fs_info->fs_mutex);
  1202. fail_sb_buffer:
  1203. extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
  1204. fail_iput:
  1205. iput(fs_info->btree_inode);
  1206. fail:
  1207. btrfs_close_devices(fs_info->fs_devices);
  1208. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1209. kfree(extent_root);
  1210. kfree(tree_root);
  1211. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
  1212. bdi_destroy(&fs_info->bdi);
  1213. #endif
  1214. kfree(fs_info);
  1215. return ERR_PTR(err);
  1216. }
  1217. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  1218. {
  1219. char b[BDEVNAME_SIZE];
  1220. if (uptodate) {
  1221. set_buffer_uptodate(bh);
  1222. } else {
  1223. if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
  1224. printk(KERN_WARNING "lost page write due to "
  1225. "I/O error on %s\n",
  1226. bdevname(bh->b_bdev, b));
  1227. }
  1228. /* note, we dont' set_buffer_write_io_error because we have
  1229. * our own ways of dealing with the IO errors
  1230. */
  1231. clear_buffer_uptodate(bh);
  1232. }
  1233. unlock_buffer(bh);
  1234. put_bh(bh);
  1235. }
  1236. int write_all_supers(struct btrfs_root *root)
  1237. {
  1238. struct list_head *cur;
  1239. struct list_head *head = &root->fs_info->fs_devices->devices;
  1240. struct btrfs_device *dev;
  1241. struct btrfs_super_block *sb;
  1242. struct btrfs_dev_item *dev_item;
  1243. struct buffer_head *bh;
  1244. int ret;
  1245. int do_barriers;
  1246. int max_errors;
  1247. int total_errors = 0;
  1248. u32 crc;
  1249. u64 flags;
  1250. max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
  1251. do_barriers = !btrfs_test_opt(root, NOBARRIER);
  1252. sb = &root->fs_info->super_for_commit;
  1253. dev_item = &sb->dev_item;
  1254. list_for_each(cur, head) {
  1255. dev = list_entry(cur, struct btrfs_device, dev_list);
  1256. if (!dev->bdev) {
  1257. total_errors++;
  1258. continue;
  1259. }
  1260. if (!dev->in_fs_metadata)
  1261. continue;
  1262. btrfs_set_stack_device_type(dev_item, dev->type);
  1263. btrfs_set_stack_device_id(dev_item, dev->devid);
  1264. btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
  1265. btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
  1266. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  1267. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  1268. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  1269. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  1270. flags = btrfs_super_flags(sb);
  1271. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  1272. crc = ~(u32)0;
  1273. crc = btrfs_csum_data(root, (char *)sb + BTRFS_CSUM_SIZE, crc,
  1274. BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  1275. btrfs_csum_final(crc, sb->csum);
  1276. bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET / 4096,
  1277. BTRFS_SUPER_INFO_SIZE);
  1278. memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
  1279. dev->pending_io = bh;
  1280. get_bh(bh);
  1281. set_buffer_uptodate(bh);
  1282. lock_buffer(bh);
  1283. bh->b_end_io = btrfs_end_buffer_write_sync;
  1284. if (do_barriers && dev->barriers) {
  1285. ret = submit_bh(WRITE_BARRIER, bh);
  1286. if (ret == -EOPNOTSUPP) {
  1287. printk("btrfs: disabling barriers on dev %s\n",
  1288. dev->name);
  1289. set_buffer_uptodate(bh);
  1290. dev->barriers = 0;
  1291. get_bh(bh);
  1292. lock_buffer(bh);
  1293. ret = submit_bh(WRITE, bh);
  1294. }
  1295. } else {
  1296. ret = submit_bh(WRITE, bh);
  1297. }
  1298. if (ret)
  1299. total_errors++;
  1300. }
  1301. if (total_errors > max_errors) {
  1302. printk("btrfs: %d errors while writing supers\n", total_errors);
  1303. BUG();
  1304. }
  1305. total_errors = 0;
  1306. list_for_each(cur, head) {
  1307. dev = list_entry(cur, struct btrfs_device, dev_list);
  1308. if (!dev->bdev)
  1309. continue;
  1310. if (!dev->in_fs_metadata)
  1311. continue;
  1312. BUG_ON(!dev->pending_io);
  1313. bh = dev->pending_io;
  1314. wait_on_buffer(bh);
  1315. if (!buffer_uptodate(dev->pending_io)) {
  1316. if (do_barriers && dev->barriers) {
  1317. printk("btrfs: disabling barriers on dev %s\n",
  1318. dev->name);
  1319. set_buffer_uptodate(bh);
  1320. get_bh(bh);
  1321. lock_buffer(bh);
  1322. dev->barriers = 0;
  1323. ret = submit_bh(WRITE, bh);
  1324. BUG_ON(ret);
  1325. wait_on_buffer(bh);
  1326. if (!buffer_uptodate(bh))
  1327. total_errors++;
  1328. } else {
  1329. total_errors++;
  1330. }
  1331. }
  1332. dev->pending_io = NULL;
  1333. brelse(bh);
  1334. }
  1335. if (total_errors > max_errors) {
  1336. printk("btrfs: %d errors while writing supers\n", total_errors);
  1337. BUG();
  1338. }
  1339. return 0;
  1340. }
  1341. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  1342. *root)
  1343. {
  1344. int ret;
  1345. ret = write_all_supers(root);
  1346. return ret;
  1347. }
  1348. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  1349. {
  1350. radix_tree_delete(&fs_info->fs_roots_radix,
  1351. (unsigned long)root->root_key.objectid);
  1352. if (root->in_sysfs)
  1353. btrfs_sysfs_del_root(root);
  1354. if (root->inode)
  1355. iput(root->inode);
  1356. if (root->node)
  1357. free_extent_buffer(root->node);
  1358. if (root->commit_root)
  1359. free_extent_buffer(root->commit_root);
  1360. if (root->name)
  1361. kfree(root->name);
  1362. kfree(root);
  1363. return 0;
  1364. }
  1365. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  1366. {
  1367. int ret;
  1368. struct btrfs_root *gang[8];
  1369. int i;
  1370. while(1) {
  1371. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1372. (void **)gang, 0,
  1373. ARRAY_SIZE(gang));
  1374. if (!ret)
  1375. break;
  1376. for (i = 0; i < ret; i++)
  1377. btrfs_free_fs_root(fs_info, gang[i]);
  1378. }
  1379. return 0;
  1380. }
  1381. int close_ctree(struct btrfs_root *root)
  1382. {
  1383. int ret;
  1384. struct btrfs_trans_handle *trans;
  1385. struct btrfs_fs_info *fs_info = root->fs_info;
  1386. fs_info->closing = 1;
  1387. btrfs_transaction_flush_work(root);
  1388. mutex_lock(&fs_info->fs_mutex);
  1389. btrfs_defrag_dirty_roots(root->fs_info);
  1390. trans = btrfs_start_transaction(root, 1);
  1391. ret = btrfs_commit_transaction(trans, root);
  1392. /* run commit again to drop the original snapshot */
  1393. trans = btrfs_start_transaction(root, 1);
  1394. btrfs_commit_transaction(trans, root);
  1395. ret = btrfs_write_and_wait_transaction(NULL, root);
  1396. BUG_ON(ret);
  1397. write_ctree_super(NULL, root);
  1398. mutex_unlock(&fs_info->fs_mutex);
  1399. btrfs_transaction_flush_work(root);
  1400. if (fs_info->delalloc_bytes) {
  1401. printk("btrfs: at unmount delalloc count %Lu\n",
  1402. fs_info->delalloc_bytes);
  1403. }
  1404. if (fs_info->extent_root->node)
  1405. free_extent_buffer(fs_info->extent_root->node);
  1406. if (fs_info->tree_root->node)
  1407. free_extent_buffer(fs_info->tree_root->node);
  1408. if (root->fs_info->chunk_root->node);
  1409. free_extent_buffer(root->fs_info->chunk_root->node);
  1410. if (root->fs_info->dev_root->node);
  1411. free_extent_buffer(root->fs_info->dev_root->node);
  1412. btrfs_free_block_groups(root->fs_info);
  1413. del_fs_roots(fs_info);
  1414. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  1415. extent_io_tree_empty_lru(&fs_info->free_space_cache);
  1416. extent_io_tree_empty_lru(&fs_info->block_group_cache);
  1417. extent_io_tree_empty_lru(&fs_info->pinned_extents);
  1418. extent_io_tree_empty_lru(&fs_info->pending_del);
  1419. extent_io_tree_empty_lru(&fs_info->extent_ins);
  1420. extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
  1421. flush_workqueue(async_submit_workqueue);
  1422. flush_workqueue(end_io_workqueue);
  1423. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  1424. flush_workqueue(async_submit_workqueue);
  1425. destroy_workqueue(async_submit_workqueue);
  1426. flush_workqueue(end_io_workqueue);
  1427. destroy_workqueue(end_io_workqueue);
  1428. iput(fs_info->btree_inode);
  1429. #if 0
  1430. while(!list_empty(&fs_info->hashers)) {
  1431. struct btrfs_hasher *hasher;
  1432. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  1433. hashers);
  1434. list_del(&hasher->hashers);
  1435. crypto_free_hash(&fs_info->hash_tfm);
  1436. kfree(hasher);
  1437. }
  1438. #endif
  1439. btrfs_close_devices(fs_info->fs_devices);
  1440. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1441. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
  1442. bdi_destroy(&fs_info->bdi);
  1443. #endif
  1444. kfree(fs_info->extent_root);
  1445. kfree(fs_info->tree_root);
  1446. kfree(fs_info->chunk_root);
  1447. kfree(fs_info->dev_root);
  1448. return 0;
  1449. }
  1450. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
  1451. {
  1452. int ret;
  1453. struct inode *btree_inode = buf->first_page->mapping->host;
  1454. ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  1455. if (!ret)
  1456. return ret;
  1457. ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
  1458. parent_transid);
  1459. return !ret;
  1460. }
  1461. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  1462. {
  1463. struct inode *btree_inode = buf->first_page->mapping->host;
  1464. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  1465. buf);
  1466. }
  1467. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  1468. {
  1469. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1470. u64 transid = btrfs_header_generation(buf);
  1471. struct inode *btree_inode = root->fs_info->btree_inode;
  1472. if (transid != root->fs_info->generation) {
  1473. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  1474. (unsigned long long)buf->start,
  1475. transid, root->fs_info->generation);
  1476. WARN_ON(1);
  1477. }
  1478. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  1479. }
  1480. void btrfs_throttle(struct btrfs_root *root)
  1481. {
  1482. struct backing_dev_info *bdi;
  1483. bdi = &root->fs_info->bdi;
  1484. if (root->fs_info->throttles && bdi_write_congested(bdi)) {
  1485. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
  1486. congestion_wait(WRITE, HZ/20);
  1487. #else
  1488. blk_congestion_wait(WRITE, HZ/20);
  1489. #endif
  1490. }
  1491. }
  1492. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  1493. {
  1494. /*
  1495. * looks as though older kernels can get into trouble with
  1496. * this code, they end up stuck in balance_dirty_pages forever
  1497. */
  1498. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
  1499. struct extent_io_tree *tree;
  1500. u64 num_dirty;
  1501. u64 start = 0;
  1502. unsigned long thresh = 16 * 1024 * 1024;
  1503. tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  1504. if (current_is_pdflush())
  1505. return;
  1506. num_dirty = count_range_bits(tree, &start, (u64)-1,
  1507. thresh, EXTENT_DIRTY);
  1508. if (num_dirty > thresh) {
  1509. balance_dirty_pages_ratelimited_nr(
  1510. root->fs_info->btree_inode->i_mapping, 1);
  1511. }
  1512. #else
  1513. return;
  1514. #endif
  1515. }
  1516. void btrfs_set_buffer_defrag(struct extent_buffer *buf)
  1517. {
  1518. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1519. struct inode *btree_inode = root->fs_info->btree_inode;
  1520. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  1521. buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
  1522. }
  1523. void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
  1524. {
  1525. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1526. struct inode *btree_inode = root->fs_info->btree_inode;
  1527. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  1528. buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
  1529. GFP_NOFS);
  1530. }
  1531. int btrfs_buffer_defrag(struct extent_buffer *buf)
  1532. {
  1533. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1534. struct inode *btree_inode = root->fs_info->btree_inode;
  1535. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  1536. buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
  1537. }
  1538. int btrfs_buffer_defrag_done(struct extent_buffer *buf)
  1539. {
  1540. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1541. struct inode *btree_inode = root->fs_info->btree_inode;
  1542. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  1543. buf->start, buf->start + buf->len - 1,
  1544. EXTENT_DEFRAG_DONE, 0);
  1545. }
  1546. int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
  1547. {
  1548. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1549. struct inode *btree_inode = root->fs_info->btree_inode;
  1550. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  1551. buf->start, buf->start + buf->len - 1,
  1552. EXTENT_DEFRAG_DONE, GFP_NOFS);
  1553. }
  1554. int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
  1555. {
  1556. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1557. struct inode *btree_inode = root->fs_info->btree_inode;
  1558. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  1559. buf->start, buf->start + buf->len - 1,
  1560. EXTENT_DEFRAG, GFP_NOFS);
  1561. }
  1562. int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
  1563. {
  1564. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1565. int ret;
  1566. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  1567. if (ret == 0) {
  1568. buf->flags |= EXTENT_UPTODATE;
  1569. }
  1570. return ret;
  1571. }
  1572. static struct extent_io_ops btree_extent_io_ops = {
  1573. .writepage_io_hook = btree_writepage_io_hook,
  1574. .readpage_end_io_hook = btree_readpage_end_io_hook,
  1575. .submit_bio_hook = btree_submit_bio_hook,
  1576. /* note we're sharing with inode.c for the merge bio hook */
  1577. .merge_bio_hook = btrfs_merge_bio_hook,
  1578. };