disk-io.c 49 KB

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