disk-io.c 49 KB

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