disk-io.c 48 KB

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