disk-io.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  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. atomic_inc(&fs_info->nr_async_submits);
  408. spin_unlock(&fs_info->async_submit_work_lock);
  409. queue_work(async_submit_workqueue, &fs_info->async_submit_work);
  410. return 0;
  411. }
  412. static int __btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  413. int mirror_num)
  414. {
  415. struct btrfs_root *root = BTRFS_I(inode)->root;
  416. u64 offset;
  417. int ret;
  418. offset = bio->bi_sector << 9;
  419. if (rw & (1 << BIO_RW)) {
  420. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num);
  421. }
  422. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 1);
  423. BUG_ON(ret);
  424. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num);
  425. }
  426. static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  427. int mirror_num)
  428. {
  429. if (!(rw & (1 << BIO_RW))) {
  430. return __btree_submit_bio_hook(inode, rw, bio, mirror_num);
  431. }
  432. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  433. inode, rw, bio, mirror_num,
  434. __btree_submit_bio_hook);
  435. }
  436. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  437. {
  438. struct extent_io_tree *tree;
  439. tree = &BTRFS_I(page->mapping->host)->io_tree;
  440. return extent_write_full_page(tree, page, btree_get_extent, wbc);
  441. }
  442. static int btree_writepages(struct address_space *mapping,
  443. struct writeback_control *wbc)
  444. {
  445. struct extent_io_tree *tree;
  446. tree = &BTRFS_I(mapping->host)->io_tree;
  447. if (wbc->sync_mode == WB_SYNC_NONE) {
  448. u64 num_dirty;
  449. u64 start = 0;
  450. unsigned long thresh = 96 * 1024 * 1024;
  451. if (wbc->for_kupdate)
  452. return 0;
  453. if (current_is_pdflush()) {
  454. thresh = 96 * 1024 * 1024;
  455. } else {
  456. thresh = 8 * 1024 * 1024;
  457. }
  458. num_dirty = count_range_bits(tree, &start, (u64)-1,
  459. thresh, EXTENT_DIRTY);
  460. if (num_dirty < thresh) {
  461. return 0;
  462. }
  463. }
  464. return extent_writepages(tree, mapping, btree_get_extent, wbc);
  465. }
  466. int btree_readpage(struct file *file, struct page *page)
  467. {
  468. struct extent_io_tree *tree;
  469. tree = &BTRFS_I(page->mapping->host)->io_tree;
  470. return extent_read_full_page(tree, page, btree_get_extent);
  471. }
  472. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  473. {
  474. struct extent_io_tree *tree;
  475. struct extent_map_tree *map;
  476. int ret;
  477. if (page_count(page) > 3) {
  478. /* once for page->private, once for the caller, once
  479. * once for the page cache
  480. */
  481. return 0;
  482. }
  483. tree = &BTRFS_I(page->mapping->host)->io_tree;
  484. map = &BTRFS_I(page->mapping->host)->extent_tree;
  485. ret = try_release_extent_state(map, tree, page, gfp_flags);
  486. if (ret == 1) {
  487. invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
  488. ClearPagePrivate(page);
  489. set_page_private(page, 0);
  490. page_cache_release(page);
  491. }
  492. return ret;
  493. }
  494. static void btree_invalidatepage(struct page *page, unsigned long offset)
  495. {
  496. struct extent_io_tree *tree;
  497. tree = &BTRFS_I(page->mapping->host)->io_tree;
  498. extent_invalidatepage(tree, page, offset);
  499. btree_releasepage(page, GFP_NOFS);
  500. if (PagePrivate(page)) {
  501. invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
  502. ClearPagePrivate(page);
  503. set_page_private(page, 0);
  504. page_cache_release(page);
  505. }
  506. }
  507. #if 0
  508. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  509. {
  510. struct buffer_head *bh;
  511. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  512. struct buffer_head *head;
  513. if (!page_has_buffers(page)) {
  514. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  515. (1 << BH_Dirty)|(1 << BH_Uptodate));
  516. }
  517. head = page_buffers(page);
  518. bh = head;
  519. do {
  520. if (buffer_dirty(bh))
  521. csum_tree_block(root, bh, 0);
  522. bh = bh->b_this_page;
  523. } while (bh != head);
  524. return block_write_full_page(page, btree_get_block, wbc);
  525. }
  526. #endif
  527. static struct address_space_operations btree_aops = {
  528. .readpage = btree_readpage,
  529. .writepage = btree_writepage,
  530. .writepages = btree_writepages,
  531. .releasepage = btree_releasepage,
  532. .invalidatepage = btree_invalidatepage,
  533. .sync_page = block_sync_page,
  534. };
  535. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
  536. u64 parent_transid)
  537. {
  538. struct extent_buffer *buf = NULL;
  539. struct inode *btree_inode = root->fs_info->btree_inode;
  540. int ret = 0;
  541. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  542. if (!buf)
  543. return 0;
  544. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  545. buf, 0, 0, btree_get_extent, 0);
  546. free_extent_buffer(buf);
  547. return ret;
  548. }
  549. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  550. u64 bytenr, u32 blocksize)
  551. {
  552. struct inode *btree_inode = root->fs_info->btree_inode;
  553. struct extent_buffer *eb;
  554. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  555. bytenr, blocksize, GFP_NOFS);
  556. return eb;
  557. }
  558. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  559. u64 bytenr, u32 blocksize)
  560. {
  561. struct inode *btree_inode = root->fs_info->btree_inode;
  562. struct extent_buffer *eb;
  563. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  564. bytenr, blocksize, NULL, GFP_NOFS);
  565. return eb;
  566. }
  567. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  568. u32 blocksize, u64 parent_transid)
  569. {
  570. struct extent_buffer *buf = NULL;
  571. struct inode *btree_inode = root->fs_info->btree_inode;
  572. struct extent_io_tree *io_tree;
  573. int ret;
  574. io_tree = &BTRFS_I(btree_inode)->io_tree;
  575. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  576. if (!buf)
  577. return NULL;
  578. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  579. if (ret == 0) {
  580. buf->flags |= EXTENT_UPTODATE;
  581. }
  582. return buf;
  583. }
  584. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  585. struct extent_buffer *buf)
  586. {
  587. struct inode *btree_inode = root->fs_info->btree_inode;
  588. if (btrfs_header_generation(buf) ==
  589. root->fs_info->running_transaction->transid)
  590. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  591. buf);
  592. return 0;
  593. }
  594. int wait_on_tree_block_writeback(struct btrfs_root *root,
  595. struct extent_buffer *buf)
  596. {
  597. struct inode *btree_inode = root->fs_info->btree_inode;
  598. wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
  599. buf);
  600. return 0;
  601. }
  602. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  603. u32 stripesize, struct btrfs_root *root,
  604. struct btrfs_fs_info *fs_info,
  605. u64 objectid)
  606. {
  607. root->node = NULL;
  608. root->inode = NULL;
  609. root->commit_root = NULL;
  610. root->sectorsize = sectorsize;
  611. root->nodesize = nodesize;
  612. root->leafsize = leafsize;
  613. root->stripesize = stripesize;
  614. root->ref_cows = 0;
  615. root->track_dirty = 0;
  616. root->fs_info = fs_info;
  617. root->objectid = objectid;
  618. root->last_trans = 0;
  619. root->highest_inode = 0;
  620. root->last_inode_alloc = 0;
  621. root->name = NULL;
  622. root->in_sysfs = 0;
  623. INIT_LIST_HEAD(&root->dirty_list);
  624. memset(&root->root_key, 0, sizeof(root->root_key));
  625. memset(&root->root_item, 0, sizeof(root->root_item));
  626. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  627. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  628. init_completion(&root->kobj_unregister);
  629. root->defrag_running = 0;
  630. root->defrag_level = 0;
  631. root->root_key.objectid = objectid;
  632. return 0;
  633. }
  634. static int find_and_setup_root(struct btrfs_root *tree_root,
  635. struct btrfs_fs_info *fs_info,
  636. u64 objectid,
  637. struct btrfs_root *root)
  638. {
  639. int ret;
  640. u32 blocksize;
  641. __setup_root(tree_root->nodesize, tree_root->leafsize,
  642. tree_root->sectorsize, tree_root->stripesize,
  643. root, fs_info, objectid);
  644. ret = btrfs_find_last_root(tree_root, objectid,
  645. &root->root_item, &root->root_key);
  646. BUG_ON(ret);
  647. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  648. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  649. blocksize, 0);
  650. BUG_ON(!root->node);
  651. return 0;
  652. }
  653. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
  654. struct btrfs_key *location)
  655. {
  656. struct btrfs_root *root;
  657. struct btrfs_root *tree_root = fs_info->tree_root;
  658. struct btrfs_path *path;
  659. struct extent_buffer *l;
  660. u64 highest_inode;
  661. u32 blocksize;
  662. int ret = 0;
  663. root = kzalloc(sizeof(*root), GFP_NOFS);
  664. if (!root)
  665. return ERR_PTR(-ENOMEM);
  666. if (location->offset == (u64)-1) {
  667. ret = find_and_setup_root(tree_root, fs_info,
  668. location->objectid, root);
  669. if (ret) {
  670. kfree(root);
  671. return ERR_PTR(ret);
  672. }
  673. goto insert;
  674. }
  675. __setup_root(tree_root->nodesize, tree_root->leafsize,
  676. tree_root->sectorsize, tree_root->stripesize,
  677. root, fs_info, location->objectid);
  678. path = btrfs_alloc_path();
  679. BUG_ON(!path);
  680. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  681. if (ret != 0) {
  682. if (ret > 0)
  683. ret = -ENOENT;
  684. goto out;
  685. }
  686. l = path->nodes[0];
  687. read_extent_buffer(l, &root->root_item,
  688. btrfs_item_ptr_offset(l, path->slots[0]),
  689. sizeof(root->root_item));
  690. memcpy(&root->root_key, location, sizeof(*location));
  691. ret = 0;
  692. out:
  693. btrfs_release_path(root, path);
  694. btrfs_free_path(path);
  695. if (ret) {
  696. kfree(root);
  697. return ERR_PTR(ret);
  698. }
  699. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  700. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  701. blocksize, 0);
  702. BUG_ON(!root->node);
  703. insert:
  704. root->ref_cows = 1;
  705. ret = btrfs_find_highest_inode(root, &highest_inode);
  706. if (ret == 0) {
  707. root->highest_inode = highest_inode;
  708. root->last_inode_alloc = highest_inode;
  709. }
  710. return root;
  711. }
  712. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  713. u64 root_objectid)
  714. {
  715. struct btrfs_root *root;
  716. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  717. return fs_info->tree_root;
  718. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  719. return fs_info->extent_root;
  720. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  721. (unsigned long)root_objectid);
  722. return root;
  723. }
  724. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  725. struct btrfs_key *location)
  726. {
  727. struct btrfs_root *root;
  728. int ret;
  729. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  730. return fs_info->tree_root;
  731. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  732. return fs_info->extent_root;
  733. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  734. return fs_info->chunk_root;
  735. if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
  736. return fs_info->dev_root;
  737. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  738. (unsigned long)location->objectid);
  739. if (root)
  740. return root;
  741. root = btrfs_read_fs_root_no_radix(fs_info, location);
  742. if (IS_ERR(root))
  743. return root;
  744. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  745. (unsigned long)root->root_key.objectid,
  746. root);
  747. if (ret) {
  748. free_extent_buffer(root->node);
  749. kfree(root);
  750. return ERR_PTR(ret);
  751. }
  752. ret = btrfs_find_dead_roots(fs_info->tree_root,
  753. root->root_key.objectid, root);
  754. BUG_ON(ret);
  755. return root;
  756. }
  757. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  758. struct btrfs_key *location,
  759. const char *name, int namelen)
  760. {
  761. struct btrfs_root *root;
  762. int ret;
  763. root = btrfs_read_fs_root_no_name(fs_info, location);
  764. if (!root)
  765. return NULL;
  766. if (root->in_sysfs)
  767. return root;
  768. ret = btrfs_set_root_name(root, name, namelen);
  769. if (ret) {
  770. free_extent_buffer(root->node);
  771. kfree(root);
  772. return ERR_PTR(ret);
  773. }
  774. ret = btrfs_sysfs_add_root(root);
  775. if (ret) {
  776. free_extent_buffer(root->node);
  777. kfree(root->name);
  778. kfree(root);
  779. return ERR_PTR(ret);
  780. }
  781. root->in_sysfs = 1;
  782. return root;
  783. }
  784. #if 0
  785. static int add_hasher(struct btrfs_fs_info *info, char *type) {
  786. struct btrfs_hasher *hasher;
  787. hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
  788. if (!hasher)
  789. return -ENOMEM;
  790. hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
  791. if (!hasher->hash_tfm) {
  792. kfree(hasher);
  793. return -EINVAL;
  794. }
  795. spin_lock(&info->hash_lock);
  796. list_add(&hasher->list, &info->hashers);
  797. spin_unlock(&info->hash_lock);
  798. return 0;
  799. }
  800. #endif
  801. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  802. {
  803. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  804. int ret = 0;
  805. int limit = 256 * info->fs_devices->open_devices;
  806. struct list_head *cur;
  807. struct btrfs_device *device;
  808. struct backing_dev_info *bdi;
  809. if ((bdi_bits & (1 << BDI_write_congested)) &&
  810. atomic_read(&info->nr_async_submits) > limit) {
  811. return 1;
  812. }
  813. list_for_each(cur, &info->fs_devices->devices) {
  814. device = list_entry(cur, struct btrfs_device, dev_list);
  815. if (!device->bdev)
  816. continue;
  817. bdi = blk_get_backing_dev_info(device->bdev);
  818. if (bdi && bdi_congested(bdi, bdi_bits)) {
  819. ret = 1;
  820. break;
  821. }
  822. }
  823. return ret;
  824. }
  825. /*
  826. * this unplugs every device on the box, and it is only used when page
  827. * is null
  828. */
  829. static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  830. {
  831. struct list_head *cur;
  832. struct btrfs_device *device;
  833. struct btrfs_fs_info *info;
  834. info = (struct btrfs_fs_info *)bdi->unplug_io_data;
  835. list_for_each(cur, &info->fs_devices->devices) {
  836. device = list_entry(cur, struct btrfs_device, dev_list);
  837. bdi = blk_get_backing_dev_info(device->bdev);
  838. if (bdi->unplug_io_fn) {
  839. bdi->unplug_io_fn(bdi, page);
  840. }
  841. }
  842. }
  843. void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  844. {
  845. struct inode *inode;
  846. struct extent_map_tree *em_tree;
  847. struct extent_map *em;
  848. struct address_space *mapping;
  849. u64 offset;
  850. /* the generic O_DIRECT read code does this */
  851. if (!page) {
  852. __unplug_io_fn(bdi, page);
  853. return;
  854. }
  855. /*
  856. * page->mapping may change at any time. Get a consistent copy
  857. * and use that for everything below
  858. */
  859. smp_mb();
  860. mapping = page->mapping;
  861. if (!mapping)
  862. return;
  863. inode = mapping->host;
  864. offset = page_offset(page);
  865. em_tree = &BTRFS_I(inode)->extent_tree;
  866. spin_lock(&em_tree->lock);
  867. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  868. spin_unlock(&em_tree->lock);
  869. if (!em)
  870. return;
  871. offset = offset - em->start;
  872. btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
  873. em->block_start + offset, page);
  874. free_extent_map(em);
  875. }
  876. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  877. {
  878. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
  879. bdi_init(bdi);
  880. #endif
  881. bdi->ra_pages = default_backing_dev_info.ra_pages;
  882. bdi->state = 0;
  883. bdi->capabilities = default_backing_dev_info.capabilities;
  884. bdi->unplug_io_fn = btrfs_unplug_io_fn;
  885. bdi->unplug_io_data = info;
  886. bdi->congested_fn = btrfs_congested_fn;
  887. bdi->congested_data = info;
  888. return 0;
  889. }
  890. static int bio_ready_for_csum(struct bio *bio)
  891. {
  892. u64 length = 0;
  893. u64 buf_len = 0;
  894. u64 start = 0;
  895. struct page *page;
  896. struct extent_io_tree *io_tree = NULL;
  897. struct btrfs_fs_info *info = NULL;
  898. struct bio_vec *bvec;
  899. int i;
  900. int ret;
  901. bio_for_each_segment(bvec, bio, i) {
  902. page = bvec->bv_page;
  903. if (page->private == EXTENT_PAGE_PRIVATE) {
  904. length += bvec->bv_len;
  905. continue;
  906. }
  907. if (!page->private) {
  908. length += bvec->bv_len;
  909. continue;
  910. }
  911. length = bvec->bv_len;
  912. buf_len = page->private >> 2;
  913. start = page_offset(page) + bvec->bv_offset;
  914. io_tree = &BTRFS_I(page->mapping->host)->io_tree;
  915. info = BTRFS_I(page->mapping->host)->root->fs_info;
  916. }
  917. /* are we fully contained in this bio? */
  918. if (buf_len <= length)
  919. return 1;
  920. ret = extent_range_uptodate(io_tree, start + length,
  921. start + buf_len - 1);
  922. if (ret == 1)
  923. return ret;
  924. return ret;
  925. }
  926. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  927. static void btrfs_end_io_csum(void *p)
  928. #else
  929. static void btrfs_end_io_csum(struct work_struct *work)
  930. #endif
  931. {
  932. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  933. struct btrfs_fs_info *fs_info = p;
  934. #else
  935. struct btrfs_fs_info *fs_info = container_of(work,
  936. struct btrfs_fs_info,
  937. end_io_work);
  938. #endif
  939. unsigned long flags;
  940. struct end_io_wq *end_io_wq;
  941. struct bio *bio;
  942. struct list_head *next;
  943. int error;
  944. int was_empty;
  945. while(1) {
  946. spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
  947. if (list_empty(&fs_info->end_io_work_list)) {
  948. spin_unlock_irqrestore(&fs_info->end_io_work_lock,
  949. flags);
  950. return;
  951. }
  952. next = fs_info->end_io_work_list.next;
  953. list_del(next);
  954. spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
  955. end_io_wq = list_entry(next, struct end_io_wq, list);
  956. bio = end_io_wq->bio;
  957. if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
  958. spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
  959. was_empty = list_empty(&fs_info->end_io_work_list);
  960. list_add_tail(&end_io_wq->list,
  961. &fs_info->end_io_work_list);
  962. spin_unlock_irqrestore(&fs_info->end_io_work_lock,
  963. flags);
  964. if (was_empty)
  965. return;
  966. continue;
  967. }
  968. error = end_io_wq->error;
  969. bio->bi_private = end_io_wq->private;
  970. bio->bi_end_io = end_io_wq->end_io;
  971. kfree(end_io_wq);
  972. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  973. bio_endio(bio, bio->bi_size, error);
  974. #else
  975. bio_endio(bio, error);
  976. #endif
  977. }
  978. }
  979. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  980. static void btrfs_async_submit_work(void *p)
  981. #else
  982. static void btrfs_async_submit_work(struct work_struct *work)
  983. #endif
  984. {
  985. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  986. struct btrfs_fs_info *fs_info = p;
  987. #else
  988. struct btrfs_fs_info *fs_info = container_of(work,
  989. struct btrfs_fs_info,
  990. async_submit_work);
  991. #endif
  992. struct async_submit_bio *async;
  993. struct list_head *next;
  994. while(1) {
  995. spin_lock(&fs_info->async_submit_work_lock);
  996. if (list_empty(&fs_info->async_submit_work_list)) {
  997. spin_unlock(&fs_info->async_submit_work_lock);
  998. return;
  999. }
  1000. next = fs_info->async_submit_work_list.next;
  1001. list_del(next);
  1002. atomic_dec(&fs_info->nr_async_submits);
  1003. spin_unlock(&fs_info->async_submit_work_lock);
  1004. async = list_entry(next, struct async_submit_bio, list);
  1005. async->submit_bio_hook(async->inode, async->rw, async->bio,
  1006. async->mirror_num);
  1007. kfree(async);
  1008. }
  1009. }
  1010. struct btrfs_root *open_ctree(struct super_block *sb,
  1011. struct btrfs_fs_devices *fs_devices,
  1012. char *options)
  1013. {
  1014. u32 sectorsize;
  1015. u32 nodesize;
  1016. u32 leafsize;
  1017. u32 blocksize;
  1018. u32 stripesize;
  1019. struct buffer_head *bh;
  1020. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  1021. GFP_NOFS);
  1022. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  1023. GFP_NOFS);
  1024. struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
  1025. GFP_NOFS);
  1026. struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
  1027. GFP_NOFS);
  1028. struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
  1029. GFP_NOFS);
  1030. int ret;
  1031. int err = -EINVAL;
  1032. struct btrfs_super_block *disk_super;
  1033. if (!extent_root || !tree_root || !fs_info) {
  1034. err = -ENOMEM;
  1035. goto fail;
  1036. }
  1037. end_io_workqueue = create_workqueue("btrfs-end-io");
  1038. BUG_ON(!end_io_workqueue);
  1039. async_submit_workqueue = create_workqueue("btrfs-async-submit");
  1040. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  1041. INIT_LIST_HEAD(&fs_info->trans_list);
  1042. INIT_LIST_HEAD(&fs_info->dead_roots);
  1043. INIT_LIST_HEAD(&fs_info->hashers);
  1044. INIT_LIST_HEAD(&fs_info->end_io_work_list);
  1045. INIT_LIST_HEAD(&fs_info->async_submit_work_list);
  1046. spin_lock_init(&fs_info->hash_lock);
  1047. spin_lock_init(&fs_info->end_io_work_lock);
  1048. spin_lock_init(&fs_info->async_submit_work_lock);
  1049. spin_lock_init(&fs_info->delalloc_lock);
  1050. spin_lock_init(&fs_info->new_trans_lock);
  1051. init_completion(&fs_info->kobj_unregister);
  1052. fs_info->tree_root = tree_root;
  1053. fs_info->extent_root = extent_root;
  1054. fs_info->chunk_root = chunk_root;
  1055. fs_info->dev_root = dev_root;
  1056. fs_info->fs_devices = fs_devices;
  1057. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  1058. INIT_LIST_HEAD(&fs_info->space_info);
  1059. btrfs_mapping_init(&fs_info->mapping_tree);
  1060. atomic_set(&fs_info->nr_async_submits, 0);
  1061. fs_info->sb = sb;
  1062. fs_info->max_extent = (u64)-1;
  1063. fs_info->max_inline = 8192 * 1024;
  1064. setup_bdi(fs_info, &fs_info->bdi);
  1065. fs_info->btree_inode = new_inode(sb);
  1066. fs_info->btree_inode->i_ino = 1;
  1067. fs_info->btree_inode->i_nlink = 1;
  1068. sb->s_blocksize = 4096;
  1069. sb->s_blocksize_bits = blksize_bits(4096);
  1070. /*
  1071. * we set the i_size on the btree inode to the max possible int.
  1072. * the real end of the address space is determined by all of
  1073. * the devices in the system
  1074. */
  1075. fs_info->btree_inode->i_size = OFFSET_MAX;
  1076. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  1077. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  1078. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  1079. fs_info->btree_inode->i_mapping,
  1080. GFP_NOFS);
  1081. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  1082. GFP_NOFS);
  1083. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  1084. extent_io_tree_init(&fs_info->free_space_cache,
  1085. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1086. extent_io_tree_init(&fs_info->block_group_cache,
  1087. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1088. extent_io_tree_init(&fs_info->pinned_extents,
  1089. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1090. extent_io_tree_init(&fs_info->pending_del,
  1091. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1092. extent_io_tree_init(&fs_info->extent_ins,
  1093. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1094. fs_info->do_barriers = 1;
  1095. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  1096. INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum, fs_info);
  1097. INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work,
  1098. fs_info);
  1099. INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
  1100. #else
  1101. INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum);
  1102. INIT_WORK(&fs_info->async_submit_work, btrfs_async_submit_work);
  1103. INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
  1104. #endif
  1105. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  1106. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  1107. sizeof(struct btrfs_key));
  1108. insert_inode_hash(fs_info->btree_inode);
  1109. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  1110. mutex_init(&fs_info->trans_mutex);
  1111. mutex_init(&fs_info->fs_mutex);
  1112. #if 0
  1113. ret = add_hasher(fs_info, "crc32c");
  1114. if (ret) {
  1115. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  1116. err = -ENOMEM;
  1117. goto fail_iput;
  1118. }
  1119. #endif
  1120. __setup_root(4096, 4096, 4096, 4096, tree_root,
  1121. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  1122. bh = __bread(fs_devices->latest_bdev,
  1123. BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  1124. if (!bh)
  1125. goto fail_iput;
  1126. memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
  1127. brelse(bh);
  1128. memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
  1129. disk_super = &fs_info->super_copy;
  1130. if (!btrfs_super_root(disk_super))
  1131. goto fail_sb_buffer;
  1132. btrfs_parse_options(options, tree_root, NULL);
  1133. if (btrfs_super_num_devices(disk_super) > fs_devices->open_devices) {
  1134. printk("Btrfs: wanted %llu devices, but found %llu\n",
  1135. (unsigned long long)btrfs_super_num_devices(disk_super),
  1136. (unsigned long long)fs_devices->open_devices);
  1137. if (btrfs_test_opt(tree_root, DEGRADED))
  1138. printk("continuing in degraded mode\n");
  1139. else {
  1140. goto fail_sb_buffer;
  1141. }
  1142. }
  1143. fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
  1144. nodesize = btrfs_super_nodesize(disk_super);
  1145. leafsize = btrfs_super_leafsize(disk_super);
  1146. sectorsize = btrfs_super_sectorsize(disk_super);
  1147. stripesize = btrfs_super_stripesize(disk_super);
  1148. tree_root->nodesize = nodesize;
  1149. tree_root->leafsize = leafsize;
  1150. tree_root->sectorsize = sectorsize;
  1151. tree_root->stripesize = stripesize;
  1152. sb->s_blocksize = sectorsize;
  1153. sb->s_blocksize_bits = blksize_bits(sectorsize);
  1154. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  1155. sizeof(disk_super->magic))) {
  1156. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  1157. goto fail_sb_buffer;
  1158. }
  1159. mutex_lock(&fs_info->fs_mutex);
  1160. ret = btrfs_read_sys_array(tree_root);
  1161. if (ret) {
  1162. printk("btrfs: failed to read the system array on %s\n",
  1163. sb->s_id);
  1164. goto fail_sys_array;
  1165. }
  1166. blocksize = btrfs_level_size(tree_root,
  1167. btrfs_super_chunk_root_level(disk_super));
  1168. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1169. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  1170. chunk_root->node = read_tree_block(chunk_root,
  1171. btrfs_super_chunk_root(disk_super),
  1172. blocksize, 0);
  1173. BUG_ON(!chunk_root->node);
  1174. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  1175. (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
  1176. BTRFS_UUID_SIZE);
  1177. ret = btrfs_read_chunk_tree(chunk_root);
  1178. BUG_ON(ret);
  1179. btrfs_close_extra_devices(fs_devices);
  1180. blocksize = btrfs_level_size(tree_root,
  1181. btrfs_super_root_level(disk_super));
  1182. tree_root->node = read_tree_block(tree_root,
  1183. btrfs_super_root(disk_super),
  1184. blocksize, 0);
  1185. if (!tree_root->node)
  1186. goto fail_sb_buffer;
  1187. ret = find_and_setup_root(tree_root, fs_info,
  1188. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  1189. if (ret)
  1190. goto fail_tree_root;
  1191. extent_root->track_dirty = 1;
  1192. ret = find_and_setup_root(tree_root, fs_info,
  1193. BTRFS_DEV_TREE_OBJECTID, dev_root);
  1194. dev_root->track_dirty = 1;
  1195. if (ret)
  1196. goto fail_extent_root;
  1197. btrfs_read_block_groups(extent_root);
  1198. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  1199. fs_info->data_alloc_profile = (u64)-1;
  1200. fs_info->metadata_alloc_profile = (u64)-1;
  1201. fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
  1202. mutex_unlock(&fs_info->fs_mutex);
  1203. return tree_root;
  1204. fail_extent_root:
  1205. free_extent_buffer(extent_root->node);
  1206. fail_tree_root:
  1207. free_extent_buffer(tree_root->node);
  1208. fail_sys_array:
  1209. mutex_unlock(&fs_info->fs_mutex);
  1210. fail_sb_buffer:
  1211. extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
  1212. fail_iput:
  1213. iput(fs_info->btree_inode);
  1214. fail:
  1215. btrfs_close_devices(fs_info->fs_devices);
  1216. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1217. kfree(extent_root);
  1218. kfree(tree_root);
  1219. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
  1220. bdi_destroy(&fs_info->bdi);
  1221. #endif
  1222. kfree(fs_info);
  1223. return ERR_PTR(err);
  1224. }
  1225. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  1226. {
  1227. char b[BDEVNAME_SIZE];
  1228. if (uptodate) {
  1229. set_buffer_uptodate(bh);
  1230. } else {
  1231. if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
  1232. printk(KERN_WARNING "lost page write due to "
  1233. "I/O error on %s\n",
  1234. bdevname(bh->b_bdev, b));
  1235. }
  1236. /* note, we dont' set_buffer_write_io_error because we have
  1237. * our own ways of dealing with the IO errors
  1238. */
  1239. clear_buffer_uptodate(bh);
  1240. }
  1241. unlock_buffer(bh);
  1242. put_bh(bh);
  1243. }
  1244. int write_all_supers(struct btrfs_root *root)
  1245. {
  1246. struct list_head *cur;
  1247. struct list_head *head = &root->fs_info->fs_devices->devices;
  1248. struct btrfs_device *dev;
  1249. struct btrfs_super_block *sb;
  1250. struct btrfs_dev_item *dev_item;
  1251. struct buffer_head *bh;
  1252. int ret;
  1253. int do_barriers;
  1254. int max_errors;
  1255. int total_errors = 0;
  1256. u32 crc;
  1257. u64 flags;
  1258. max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
  1259. do_barriers = !btrfs_test_opt(root, NOBARRIER);
  1260. sb = &root->fs_info->super_for_commit;
  1261. dev_item = &sb->dev_item;
  1262. list_for_each(cur, head) {
  1263. dev = list_entry(cur, struct btrfs_device, dev_list);
  1264. if (!dev->bdev) {
  1265. total_errors++;
  1266. continue;
  1267. }
  1268. if (!dev->in_fs_metadata)
  1269. continue;
  1270. btrfs_set_stack_device_type(dev_item, dev->type);
  1271. btrfs_set_stack_device_id(dev_item, dev->devid);
  1272. btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
  1273. btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
  1274. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  1275. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  1276. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  1277. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  1278. flags = btrfs_super_flags(sb);
  1279. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  1280. crc = ~(u32)0;
  1281. crc = btrfs_csum_data(root, (char *)sb + BTRFS_CSUM_SIZE, crc,
  1282. BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  1283. btrfs_csum_final(crc, sb->csum);
  1284. bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET / 4096,
  1285. BTRFS_SUPER_INFO_SIZE);
  1286. memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
  1287. dev->pending_io = bh;
  1288. get_bh(bh);
  1289. set_buffer_uptodate(bh);
  1290. lock_buffer(bh);
  1291. bh->b_end_io = btrfs_end_buffer_write_sync;
  1292. if (do_barriers && dev->barriers) {
  1293. ret = submit_bh(WRITE_BARRIER, bh);
  1294. if (ret == -EOPNOTSUPP) {
  1295. printk("btrfs: disabling barriers on dev %s\n",
  1296. dev->name);
  1297. set_buffer_uptodate(bh);
  1298. dev->barriers = 0;
  1299. get_bh(bh);
  1300. lock_buffer(bh);
  1301. ret = submit_bh(WRITE, bh);
  1302. }
  1303. } else {
  1304. ret = submit_bh(WRITE, bh);
  1305. }
  1306. if (ret)
  1307. total_errors++;
  1308. }
  1309. if (total_errors > max_errors) {
  1310. printk("btrfs: %d errors while writing supers\n", total_errors);
  1311. BUG();
  1312. }
  1313. total_errors = 0;
  1314. list_for_each(cur, head) {
  1315. dev = list_entry(cur, struct btrfs_device, dev_list);
  1316. if (!dev->bdev)
  1317. continue;
  1318. if (!dev->in_fs_metadata)
  1319. continue;
  1320. BUG_ON(!dev->pending_io);
  1321. bh = dev->pending_io;
  1322. wait_on_buffer(bh);
  1323. if (!buffer_uptodate(dev->pending_io)) {
  1324. if (do_barriers && dev->barriers) {
  1325. printk("btrfs: disabling barriers on dev %s\n",
  1326. dev->name);
  1327. set_buffer_uptodate(bh);
  1328. get_bh(bh);
  1329. lock_buffer(bh);
  1330. dev->barriers = 0;
  1331. ret = submit_bh(WRITE, bh);
  1332. BUG_ON(ret);
  1333. wait_on_buffer(bh);
  1334. if (!buffer_uptodate(bh))
  1335. total_errors++;
  1336. } else {
  1337. total_errors++;
  1338. }
  1339. }
  1340. dev->pending_io = NULL;
  1341. brelse(bh);
  1342. }
  1343. if (total_errors > max_errors) {
  1344. printk("btrfs: %d errors while writing supers\n", total_errors);
  1345. BUG();
  1346. }
  1347. return 0;
  1348. }
  1349. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  1350. *root)
  1351. {
  1352. int ret;
  1353. ret = write_all_supers(root);
  1354. return ret;
  1355. }
  1356. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  1357. {
  1358. radix_tree_delete(&fs_info->fs_roots_radix,
  1359. (unsigned long)root->root_key.objectid);
  1360. if (root->in_sysfs)
  1361. btrfs_sysfs_del_root(root);
  1362. if (root->inode)
  1363. iput(root->inode);
  1364. if (root->node)
  1365. free_extent_buffer(root->node);
  1366. if (root->commit_root)
  1367. free_extent_buffer(root->commit_root);
  1368. if (root->name)
  1369. kfree(root->name);
  1370. kfree(root);
  1371. return 0;
  1372. }
  1373. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  1374. {
  1375. int ret;
  1376. struct btrfs_root *gang[8];
  1377. int i;
  1378. while(1) {
  1379. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1380. (void **)gang, 0,
  1381. ARRAY_SIZE(gang));
  1382. if (!ret)
  1383. break;
  1384. for (i = 0; i < ret; i++)
  1385. btrfs_free_fs_root(fs_info, gang[i]);
  1386. }
  1387. return 0;
  1388. }
  1389. int close_ctree(struct btrfs_root *root)
  1390. {
  1391. int ret;
  1392. struct btrfs_trans_handle *trans;
  1393. struct btrfs_fs_info *fs_info = root->fs_info;
  1394. fs_info->closing = 1;
  1395. btrfs_transaction_flush_work(root);
  1396. mutex_lock(&fs_info->fs_mutex);
  1397. btrfs_defrag_dirty_roots(root->fs_info);
  1398. trans = btrfs_start_transaction(root, 1);
  1399. ret = btrfs_commit_transaction(trans, root);
  1400. /* run commit again to drop the original snapshot */
  1401. trans = btrfs_start_transaction(root, 1);
  1402. btrfs_commit_transaction(trans, root);
  1403. ret = btrfs_write_and_wait_transaction(NULL, root);
  1404. BUG_ON(ret);
  1405. write_ctree_super(NULL, root);
  1406. mutex_unlock(&fs_info->fs_mutex);
  1407. btrfs_transaction_flush_work(root);
  1408. if (fs_info->delalloc_bytes) {
  1409. printk("btrfs: at unmount delalloc count %Lu\n",
  1410. fs_info->delalloc_bytes);
  1411. }
  1412. if (fs_info->extent_root->node)
  1413. free_extent_buffer(fs_info->extent_root->node);
  1414. if (fs_info->tree_root->node)
  1415. free_extent_buffer(fs_info->tree_root->node);
  1416. if (root->fs_info->chunk_root->node);
  1417. free_extent_buffer(root->fs_info->chunk_root->node);
  1418. if (root->fs_info->dev_root->node);
  1419. free_extent_buffer(root->fs_info->dev_root->node);
  1420. btrfs_free_block_groups(root->fs_info);
  1421. del_fs_roots(fs_info);
  1422. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  1423. extent_io_tree_empty_lru(&fs_info->free_space_cache);
  1424. extent_io_tree_empty_lru(&fs_info->block_group_cache);
  1425. extent_io_tree_empty_lru(&fs_info->pinned_extents);
  1426. extent_io_tree_empty_lru(&fs_info->pending_del);
  1427. extent_io_tree_empty_lru(&fs_info->extent_ins);
  1428. extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
  1429. flush_workqueue(async_submit_workqueue);
  1430. flush_workqueue(end_io_workqueue);
  1431. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  1432. flush_workqueue(async_submit_workqueue);
  1433. destroy_workqueue(async_submit_workqueue);
  1434. flush_workqueue(end_io_workqueue);
  1435. destroy_workqueue(end_io_workqueue);
  1436. iput(fs_info->btree_inode);
  1437. #if 0
  1438. while(!list_empty(&fs_info->hashers)) {
  1439. struct btrfs_hasher *hasher;
  1440. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  1441. hashers);
  1442. list_del(&hasher->hashers);
  1443. crypto_free_hash(&fs_info->hash_tfm);
  1444. kfree(hasher);
  1445. }
  1446. #endif
  1447. btrfs_close_devices(fs_info->fs_devices);
  1448. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1449. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
  1450. bdi_destroy(&fs_info->bdi);
  1451. #endif
  1452. kfree(fs_info->extent_root);
  1453. kfree(fs_info->tree_root);
  1454. kfree(fs_info->chunk_root);
  1455. kfree(fs_info->dev_root);
  1456. return 0;
  1457. }
  1458. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
  1459. {
  1460. int ret;
  1461. struct inode *btree_inode = buf->first_page->mapping->host;
  1462. ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  1463. if (!ret)
  1464. return ret;
  1465. ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
  1466. parent_transid);
  1467. return !ret;
  1468. }
  1469. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  1470. {
  1471. struct inode *btree_inode = buf->first_page->mapping->host;
  1472. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  1473. buf);
  1474. }
  1475. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  1476. {
  1477. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1478. u64 transid = btrfs_header_generation(buf);
  1479. struct inode *btree_inode = root->fs_info->btree_inode;
  1480. if (transid != root->fs_info->generation) {
  1481. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  1482. (unsigned long long)buf->start,
  1483. transid, root->fs_info->generation);
  1484. WARN_ON(1);
  1485. }
  1486. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  1487. }
  1488. void btrfs_throttle(struct btrfs_root *root)
  1489. {
  1490. struct backing_dev_info *bdi;
  1491. bdi = &root->fs_info->bdi;
  1492. if (root->fs_info->throttles && bdi_write_congested(bdi)) {
  1493. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
  1494. congestion_wait(WRITE, HZ/20);
  1495. #else
  1496. blk_congestion_wait(WRITE, HZ/20);
  1497. #endif
  1498. }
  1499. }
  1500. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  1501. {
  1502. /*
  1503. * looks as though older kernels can get into trouble with
  1504. * this code, they end up stuck in balance_dirty_pages forever
  1505. */
  1506. struct extent_io_tree *tree;
  1507. u64 num_dirty;
  1508. u64 start = 0;
  1509. unsigned long thresh = 16 * 1024 * 1024;
  1510. tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  1511. if (current_is_pdflush())
  1512. return;
  1513. num_dirty = count_range_bits(tree, &start, (u64)-1,
  1514. thresh, EXTENT_DIRTY);
  1515. if (num_dirty > thresh) {
  1516. balance_dirty_pages_ratelimited_nr(
  1517. root->fs_info->btree_inode->i_mapping, 1);
  1518. }
  1519. return;
  1520. }
  1521. void btrfs_set_buffer_defrag(struct extent_buffer *buf)
  1522. {
  1523. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1524. struct inode *btree_inode = root->fs_info->btree_inode;
  1525. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  1526. buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
  1527. }
  1528. void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
  1529. {
  1530. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1531. struct inode *btree_inode = root->fs_info->btree_inode;
  1532. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  1533. buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
  1534. GFP_NOFS);
  1535. }
  1536. int btrfs_buffer_defrag(struct extent_buffer *buf)
  1537. {
  1538. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1539. struct inode *btree_inode = root->fs_info->btree_inode;
  1540. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  1541. buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
  1542. }
  1543. int btrfs_buffer_defrag_done(struct extent_buffer *buf)
  1544. {
  1545. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1546. struct inode *btree_inode = root->fs_info->btree_inode;
  1547. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  1548. buf->start, buf->start + buf->len - 1,
  1549. EXTENT_DEFRAG_DONE, 0);
  1550. }
  1551. int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
  1552. {
  1553. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1554. struct inode *btree_inode = root->fs_info->btree_inode;
  1555. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  1556. buf->start, buf->start + buf->len - 1,
  1557. EXTENT_DEFRAG_DONE, GFP_NOFS);
  1558. }
  1559. int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
  1560. {
  1561. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1562. struct inode *btree_inode = root->fs_info->btree_inode;
  1563. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  1564. buf->start, buf->start + buf->len - 1,
  1565. EXTENT_DEFRAG, GFP_NOFS);
  1566. }
  1567. int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
  1568. {
  1569. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1570. int ret;
  1571. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  1572. if (ret == 0) {
  1573. buf->flags |= EXTENT_UPTODATE;
  1574. }
  1575. return ret;
  1576. }
  1577. static struct extent_io_ops btree_extent_io_ops = {
  1578. .writepage_io_hook = btree_writepage_io_hook,
  1579. .readpage_end_io_hook = btree_readpage_end_io_hook,
  1580. .submit_bio_hook = btree_submit_bio_hook,
  1581. /* note we're sharing with inode.c for the merge bio hook */
  1582. .merge_bio_hook = btrfs_merge_bio_hook,
  1583. };