disk-io.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  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. #include <linux/freezer.h>
  29. #include "compat.h"
  30. #include "crc32c.h"
  31. #include "ctree.h"
  32. #include "disk-io.h"
  33. #include "transaction.h"
  34. #include "btrfs_inode.h"
  35. #include "volumes.h"
  36. #include "print-tree.h"
  37. #include "async-thread.h"
  38. #include "locking.h"
  39. #include "ref-cache.h"
  40. #include "tree-log.h"
  41. #if 0
  42. static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
  43. {
  44. if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
  45. printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
  46. (unsigned long long)extent_buffer_blocknr(buf),
  47. (unsigned long long)btrfs_header_blocknr(buf));
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. #endif
  53. static struct extent_io_ops btree_extent_io_ops;
  54. static void end_workqueue_fn(struct btrfs_work *work);
  55. /*
  56. * end_io_wq structs are used to do processing in task context when an IO is
  57. * complete. This is used during reads to verify checksums, and it is used
  58. * by writes to insert metadata for new file extents after IO is complete.
  59. */
  60. struct end_io_wq {
  61. struct bio *bio;
  62. bio_end_io_t *end_io;
  63. void *private;
  64. struct btrfs_fs_info *info;
  65. int error;
  66. int metadata;
  67. struct list_head list;
  68. struct btrfs_work work;
  69. };
  70. /*
  71. * async submit bios are used to offload expensive checksumming
  72. * onto the worker threads. They checksum file and metadata bios
  73. * just before they are sent down the IO stack.
  74. */
  75. struct async_submit_bio {
  76. struct inode *inode;
  77. struct bio *bio;
  78. struct list_head list;
  79. extent_submit_bio_hook_t *submit_bio_start;
  80. extent_submit_bio_hook_t *submit_bio_done;
  81. int rw;
  82. int mirror_num;
  83. unsigned long bio_flags;
  84. struct btrfs_work work;
  85. };
  86. /*
  87. * extents on the btree inode are pretty simple, there's one extent
  88. * that covers the entire device
  89. */
  90. static struct extent_map *btree_get_extent(struct inode *inode,
  91. struct page *page, size_t page_offset, u64 start, u64 len,
  92. int create)
  93. {
  94. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  95. struct extent_map *em;
  96. int ret;
  97. spin_lock(&em_tree->lock);
  98. em = lookup_extent_mapping(em_tree, start, len);
  99. if (em) {
  100. em->bdev =
  101. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  102. spin_unlock(&em_tree->lock);
  103. goto out;
  104. }
  105. spin_unlock(&em_tree->lock);
  106. em = alloc_extent_map(GFP_NOFS);
  107. if (!em) {
  108. em = ERR_PTR(-ENOMEM);
  109. goto out;
  110. }
  111. em->start = 0;
  112. em->len = (u64)-1;
  113. em->block_len = (u64)-1;
  114. em->block_start = 0;
  115. em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  116. spin_lock(&em_tree->lock);
  117. ret = add_extent_mapping(em_tree, em);
  118. if (ret == -EEXIST) {
  119. u64 failed_start = em->start;
  120. u64 failed_len = em->len;
  121. printk("failed to insert %Lu %Lu -> %Lu into tree\n",
  122. em->start, em->len, em->block_start);
  123. free_extent_map(em);
  124. em = lookup_extent_mapping(em_tree, start, len);
  125. if (em) {
  126. printk("after failing, found %Lu %Lu %Lu\n",
  127. em->start, em->len, em->block_start);
  128. ret = 0;
  129. } else {
  130. em = lookup_extent_mapping(em_tree, failed_start,
  131. failed_len);
  132. if (em) {
  133. printk("double failure lookup gives us "
  134. "%Lu %Lu -> %Lu\n", em->start,
  135. em->len, em->block_start);
  136. free_extent_map(em);
  137. }
  138. ret = -EIO;
  139. }
  140. } else if (ret) {
  141. free_extent_map(em);
  142. em = NULL;
  143. }
  144. spin_unlock(&em_tree->lock);
  145. if (ret)
  146. em = ERR_PTR(ret);
  147. out:
  148. return em;
  149. }
  150. u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
  151. {
  152. return btrfs_crc32c(seed, data, len);
  153. }
  154. void btrfs_csum_final(u32 crc, char *result)
  155. {
  156. *(__le32 *)result = ~cpu_to_le32(crc);
  157. }
  158. /*
  159. * compute the csum for a btree block, and either verify it or write it
  160. * into the csum field of the block.
  161. */
  162. static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
  163. int verify)
  164. {
  165. u16 csum_size =
  166. btrfs_super_csum_size(&root->fs_info->super_copy);
  167. char *result = NULL;
  168. unsigned long len;
  169. unsigned long cur_len;
  170. unsigned long offset = BTRFS_CSUM_SIZE;
  171. char *map_token = NULL;
  172. char *kaddr;
  173. unsigned long map_start;
  174. unsigned long map_len;
  175. int err;
  176. u32 crc = ~(u32)0;
  177. unsigned long inline_result;
  178. len = buf->len - offset;
  179. while(len > 0) {
  180. err = map_private_extent_buffer(buf, offset, 32,
  181. &map_token, &kaddr,
  182. &map_start, &map_len, KM_USER0);
  183. if (err) {
  184. printk("failed to map extent buffer! %lu\n",
  185. offset);
  186. return 1;
  187. }
  188. cur_len = min(len, map_len - (offset - map_start));
  189. crc = btrfs_csum_data(root, kaddr + offset - map_start,
  190. crc, cur_len);
  191. len -= cur_len;
  192. offset += cur_len;
  193. unmap_extent_buffer(buf, map_token, KM_USER0);
  194. }
  195. if (csum_size > sizeof(inline_result)) {
  196. result = kzalloc(csum_size * sizeof(char), GFP_NOFS);
  197. if (!result)
  198. return 1;
  199. } else {
  200. result = (char *)&inline_result;
  201. }
  202. btrfs_csum_final(crc, result);
  203. if (verify) {
  204. /* FIXME, this is not good */
  205. if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
  206. u32 val;
  207. u32 found = 0;
  208. memcpy(&found, result, csum_size);
  209. read_extent_buffer(buf, &val, 0, csum_size);
  210. printk("btrfs: %s checksum verify failed on %llu "
  211. "wanted %X found %X level %d\n",
  212. root->fs_info->sb->s_id,
  213. buf->start, val, found, btrfs_header_level(buf));
  214. if (result != (char *)&inline_result)
  215. kfree(result);
  216. return 1;
  217. }
  218. } else {
  219. write_extent_buffer(buf, result, 0, csum_size);
  220. }
  221. if (result != (char *)&inline_result)
  222. kfree(result);
  223. return 0;
  224. }
  225. /*
  226. * we can't consider a given block up to date unless the transid of the
  227. * block matches the transid in the parent node's pointer. This is how we
  228. * detect blocks that either didn't get written at all or got written
  229. * in the wrong place.
  230. */
  231. static int verify_parent_transid(struct extent_io_tree *io_tree,
  232. struct extent_buffer *eb, u64 parent_transid)
  233. {
  234. int ret;
  235. if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
  236. return 0;
  237. lock_extent(io_tree, eb->start, eb->start + eb->len - 1, GFP_NOFS);
  238. if (extent_buffer_uptodate(io_tree, eb) &&
  239. btrfs_header_generation(eb) == parent_transid) {
  240. ret = 0;
  241. goto out;
  242. }
  243. printk("parent transid verify failed on %llu wanted %llu found %llu\n",
  244. (unsigned long long)eb->start,
  245. (unsigned long long)parent_transid,
  246. (unsigned long long)btrfs_header_generation(eb));
  247. ret = 1;
  248. clear_extent_buffer_uptodate(io_tree, eb);
  249. out:
  250. unlock_extent(io_tree, eb->start, eb->start + eb->len - 1,
  251. GFP_NOFS);
  252. return ret;
  253. }
  254. /*
  255. * helper to read a given tree block, doing retries as required when
  256. * the checksums don't match and we have alternate mirrors to try.
  257. */
  258. static int btree_read_extent_buffer_pages(struct btrfs_root *root,
  259. struct extent_buffer *eb,
  260. u64 start, u64 parent_transid)
  261. {
  262. struct extent_io_tree *io_tree;
  263. int ret;
  264. int num_copies = 0;
  265. int mirror_num = 0;
  266. io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  267. while (1) {
  268. ret = read_extent_buffer_pages(io_tree, eb, start, 1,
  269. btree_get_extent, mirror_num);
  270. if (!ret &&
  271. !verify_parent_transid(io_tree, eb, parent_transid))
  272. return ret;
  273. printk("read extent buffer pages failed with ret %d mirror no %d\n", ret, mirror_num);
  274. num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
  275. eb->start, eb->len);
  276. if (num_copies == 1)
  277. return ret;
  278. mirror_num++;
  279. if (mirror_num > num_copies)
  280. return ret;
  281. }
  282. return -EIO;
  283. }
  284. /*
  285. * checksum a dirty tree block before IO. This has extra checks to make
  286. * sure we only fill in the checksum field in the first page of a multi-page block
  287. */
  288. static int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
  289. {
  290. struct extent_io_tree *tree;
  291. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  292. u64 found_start;
  293. int found_level;
  294. unsigned long len;
  295. struct extent_buffer *eb;
  296. int ret;
  297. tree = &BTRFS_I(page->mapping->host)->io_tree;
  298. if (page->private == EXTENT_PAGE_PRIVATE)
  299. goto out;
  300. if (!page->private)
  301. goto out;
  302. len = page->private >> 2;
  303. if (len == 0) {
  304. WARN_ON(1);
  305. }
  306. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  307. ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
  308. btrfs_header_generation(eb));
  309. BUG_ON(ret);
  310. found_start = btrfs_header_bytenr(eb);
  311. if (found_start != start) {
  312. printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
  313. start, found_start, len);
  314. WARN_ON(1);
  315. goto err;
  316. }
  317. if (eb->first_page != page) {
  318. printk("bad first page %lu %lu\n", eb->first_page->index,
  319. page->index);
  320. WARN_ON(1);
  321. goto err;
  322. }
  323. if (!PageUptodate(page)) {
  324. printk("csum not up to date page %lu\n", page->index);
  325. WARN_ON(1);
  326. goto err;
  327. }
  328. found_level = btrfs_header_level(eb);
  329. csum_tree_block(root, eb, 0);
  330. err:
  331. free_extent_buffer(eb);
  332. out:
  333. return 0;
  334. }
  335. static int check_tree_block_fsid(struct btrfs_root *root,
  336. struct extent_buffer *eb)
  337. {
  338. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  339. u8 fsid[BTRFS_UUID_SIZE];
  340. int ret = 1;
  341. read_extent_buffer(eb, fsid, (unsigned long)btrfs_header_fsid(eb),
  342. BTRFS_FSID_SIZE);
  343. while (fs_devices) {
  344. if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) {
  345. ret = 0;
  346. break;
  347. }
  348. fs_devices = fs_devices->seed;
  349. }
  350. return ret;
  351. }
  352. static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  353. struct extent_state *state)
  354. {
  355. struct extent_io_tree *tree;
  356. u64 found_start;
  357. int found_level;
  358. unsigned long len;
  359. struct extent_buffer *eb;
  360. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  361. int ret = 0;
  362. tree = &BTRFS_I(page->mapping->host)->io_tree;
  363. if (page->private == EXTENT_PAGE_PRIVATE)
  364. goto out;
  365. if (!page->private)
  366. goto out;
  367. len = page->private >> 2;
  368. if (len == 0) {
  369. WARN_ON(1);
  370. }
  371. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  372. found_start = btrfs_header_bytenr(eb);
  373. if (found_start != start) {
  374. printk("bad tree block start %llu %llu\n",
  375. (unsigned long long)found_start,
  376. (unsigned long long)eb->start);
  377. ret = -EIO;
  378. goto err;
  379. }
  380. if (eb->first_page != page) {
  381. printk("bad first page %lu %lu\n", eb->first_page->index,
  382. page->index);
  383. WARN_ON(1);
  384. ret = -EIO;
  385. goto err;
  386. }
  387. if (check_tree_block_fsid(root, eb)) {
  388. printk("bad fsid on block %Lu\n", eb->start);
  389. ret = -EIO;
  390. goto err;
  391. }
  392. found_level = btrfs_header_level(eb);
  393. ret = csum_tree_block(root, eb, 1);
  394. if (ret)
  395. ret = -EIO;
  396. end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
  397. end = eb->start + end - 1;
  398. err:
  399. free_extent_buffer(eb);
  400. out:
  401. return ret;
  402. }
  403. static void end_workqueue_bio(struct bio *bio, int err)
  404. {
  405. struct end_io_wq *end_io_wq = bio->bi_private;
  406. struct btrfs_fs_info *fs_info;
  407. fs_info = end_io_wq->info;
  408. end_io_wq->error = err;
  409. end_io_wq->work.func = end_workqueue_fn;
  410. end_io_wq->work.flags = 0;
  411. if (bio->bi_rw & (1 << BIO_RW)) {
  412. btrfs_queue_worker(&fs_info->endio_write_workers,
  413. &end_io_wq->work);
  414. } else {
  415. if (end_io_wq->metadata)
  416. btrfs_queue_worker(&fs_info->endio_meta_workers,
  417. &end_io_wq->work);
  418. else
  419. btrfs_queue_worker(&fs_info->endio_workers,
  420. &end_io_wq->work);
  421. }
  422. }
  423. int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
  424. int metadata)
  425. {
  426. struct end_io_wq *end_io_wq;
  427. end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
  428. if (!end_io_wq)
  429. return -ENOMEM;
  430. end_io_wq->private = bio->bi_private;
  431. end_io_wq->end_io = bio->bi_end_io;
  432. end_io_wq->info = info;
  433. end_io_wq->error = 0;
  434. end_io_wq->bio = bio;
  435. end_io_wq->metadata = metadata;
  436. bio->bi_private = end_io_wq;
  437. bio->bi_end_io = end_workqueue_bio;
  438. return 0;
  439. }
  440. unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info)
  441. {
  442. unsigned long limit = min_t(unsigned long,
  443. info->workers.max_workers,
  444. info->fs_devices->open_devices);
  445. return 256 * limit;
  446. }
  447. int btrfs_congested_async(struct btrfs_fs_info *info, int iodone)
  448. {
  449. return atomic_read(&info->nr_async_bios) >
  450. btrfs_async_submit_limit(info);
  451. }
  452. static void run_one_async_start(struct btrfs_work *work)
  453. {
  454. struct btrfs_fs_info *fs_info;
  455. struct async_submit_bio *async;
  456. async = container_of(work, struct async_submit_bio, work);
  457. fs_info = BTRFS_I(async->inode)->root->fs_info;
  458. async->submit_bio_start(async->inode, async->rw, async->bio,
  459. async->mirror_num, async->bio_flags);
  460. }
  461. static void run_one_async_done(struct btrfs_work *work)
  462. {
  463. struct btrfs_fs_info *fs_info;
  464. struct async_submit_bio *async;
  465. int limit;
  466. async = container_of(work, struct async_submit_bio, work);
  467. fs_info = BTRFS_I(async->inode)->root->fs_info;
  468. limit = btrfs_async_submit_limit(fs_info);
  469. limit = limit * 2 / 3;
  470. atomic_dec(&fs_info->nr_async_submits);
  471. if (atomic_read(&fs_info->nr_async_submits) < limit &&
  472. waitqueue_active(&fs_info->async_submit_wait))
  473. wake_up(&fs_info->async_submit_wait);
  474. async->submit_bio_done(async->inode, async->rw, async->bio,
  475. async->mirror_num, async->bio_flags);
  476. }
  477. static void run_one_async_free(struct btrfs_work *work)
  478. {
  479. struct async_submit_bio *async;
  480. async = container_of(work, struct async_submit_bio, work);
  481. kfree(async);
  482. }
  483. int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
  484. int rw, struct bio *bio, int mirror_num,
  485. unsigned long bio_flags,
  486. extent_submit_bio_hook_t *submit_bio_start,
  487. extent_submit_bio_hook_t *submit_bio_done)
  488. {
  489. struct async_submit_bio *async;
  490. async = kmalloc(sizeof(*async), GFP_NOFS);
  491. if (!async)
  492. return -ENOMEM;
  493. async->inode = inode;
  494. async->rw = rw;
  495. async->bio = bio;
  496. async->mirror_num = mirror_num;
  497. async->submit_bio_start = submit_bio_start;
  498. async->submit_bio_done = submit_bio_done;
  499. async->work.func = run_one_async_start;
  500. async->work.ordered_func = run_one_async_done;
  501. async->work.ordered_free = run_one_async_free;
  502. async->work.flags = 0;
  503. async->bio_flags = bio_flags;
  504. atomic_inc(&fs_info->nr_async_submits);
  505. btrfs_queue_worker(&fs_info->workers, &async->work);
  506. #if 0
  507. int limit = btrfs_async_submit_limit(fs_info);
  508. if (atomic_read(&fs_info->nr_async_submits) > limit) {
  509. wait_event_timeout(fs_info->async_submit_wait,
  510. (atomic_read(&fs_info->nr_async_submits) < limit),
  511. HZ/10);
  512. wait_event_timeout(fs_info->async_submit_wait,
  513. (atomic_read(&fs_info->nr_async_bios) < limit),
  514. HZ/10);
  515. }
  516. #endif
  517. while(atomic_read(&fs_info->async_submit_draining) &&
  518. atomic_read(&fs_info->nr_async_submits)) {
  519. wait_event(fs_info->async_submit_wait,
  520. (atomic_read(&fs_info->nr_async_submits) == 0));
  521. }
  522. return 0;
  523. }
  524. static int btree_csum_one_bio(struct bio *bio)
  525. {
  526. struct bio_vec *bvec = bio->bi_io_vec;
  527. int bio_index = 0;
  528. struct btrfs_root *root;
  529. WARN_ON(bio->bi_vcnt <= 0);
  530. while(bio_index < bio->bi_vcnt) {
  531. root = BTRFS_I(bvec->bv_page->mapping->host)->root;
  532. csum_dirty_buffer(root, bvec->bv_page);
  533. bio_index++;
  534. bvec++;
  535. }
  536. return 0;
  537. }
  538. static int __btree_submit_bio_start(struct inode *inode, int rw,
  539. struct bio *bio, int mirror_num,
  540. unsigned long bio_flags)
  541. {
  542. /*
  543. * when we're called for a write, we're already in the async
  544. * submission context. Just jump into btrfs_map_bio
  545. */
  546. btree_csum_one_bio(bio);
  547. return 0;
  548. }
  549. static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  550. int mirror_num, unsigned long bio_flags)
  551. {
  552. /*
  553. * when we're called for a write, we're already in the async
  554. * submission context. Just jump into btrfs_map_bio
  555. */
  556. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1);
  557. }
  558. static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  559. int mirror_num, unsigned long bio_flags)
  560. {
  561. /*
  562. * kthread helpers are used to submit writes so that checksumming
  563. * can happen in parallel across all CPUs
  564. */
  565. if (!(rw & (1 << BIO_RW))) {
  566. int ret;
  567. /*
  568. * called for a read, do the setup so that checksum validation
  569. * can happen in the async kernel threads
  570. */
  571. ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info,
  572. bio, 1);
  573. BUG_ON(ret);
  574. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
  575. mirror_num, 0);
  576. }
  577. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  578. inode, rw, bio, mirror_num, 0,
  579. __btree_submit_bio_start,
  580. __btree_submit_bio_done);
  581. }
  582. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  583. {
  584. struct extent_io_tree *tree;
  585. tree = &BTRFS_I(page->mapping->host)->io_tree;
  586. if (current->flags & PF_MEMALLOC) {
  587. redirty_page_for_writepage(wbc, page);
  588. unlock_page(page);
  589. return 0;
  590. }
  591. return extent_write_full_page(tree, page, btree_get_extent, wbc);
  592. }
  593. static int btree_writepages(struct address_space *mapping,
  594. struct writeback_control *wbc)
  595. {
  596. struct extent_io_tree *tree;
  597. tree = &BTRFS_I(mapping->host)->io_tree;
  598. if (wbc->sync_mode == WB_SYNC_NONE) {
  599. u64 num_dirty;
  600. u64 start = 0;
  601. unsigned long thresh = 32 * 1024 * 1024;
  602. if (wbc->for_kupdate)
  603. return 0;
  604. num_dirty = count_range_bits(tree, &start, (u64)-1,
  605. thresh, EXTENT_DIRTY);
  606. if (num_dirty < thresh) {
  607. return 0;
  608. }
  609. }
  610. return extent_writepages(tree, mapping, btree_get_extent, wbc);
  611. }
  612. static int btree_readpage(struct file *file, struct page *page)
  613. {
  614. struct extent_io_tree *tree;
  615. tree = &BTRFS_I(page->mapping->host)->io_tree;
  616. return extent_read_full_page(tree, page, btree_get_extent);
  617. }
  618. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  619. {
  620. struct extent_io_tree *tree;
  621. struct extent_map_tree *map;
  622. int ret;
  623. if (PageWriteback(page) || PageDirty(page))
  624. return 0;
  625. tree = &BTRFS_I(page->mapping->host)->io_tree;
  626. map = &BTRFS_I(page->mapping->host)->extent_tree;
  627. ret = try_release_extent_state(map, tree, page, gfp_flags);
  628. if (!ret) {
  629. return 0;
  630. }
  631. ret = try_release_extent_buffer(tree, page);
  632. if (ret == 1) {
  633. ClearPagePrivate(page);
  634. set_page_private(page, 0);
  635. page_cache_release(page);
  636. }
  637. return ret;
  638. }
  639. static void btree_invalidatepage(struct page *page, unsigned long offset)
  640. {
  641. struct extent_io_tree *tree;
  642. tree = &BTRFS_I(page->mapping->host)->io_tree;
  643. extent_invalidatepage(tree, page, offset);
  644. btree_releasepage(page, GFP_NOFS);
  645. if (PagePrivate(page)) {
  646. printk("warning page private not zero on page %Lu\n",
  647. page_offset(page));
  648. ClearPagePrivate(page);
  649. set_page_private(page, 0);
  650. page_cache_release(page);
  651. }
  652. }
  653. #if 0
  654. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  655. {
  656. struct buffer_head *bh;
  657. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  658. struct buffer_head *head;
  659. if (!page_has_buffers(page)) {
  660. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  661. (1 << BH_Dirty)|(1 << BH_Uptodate));
  662. }
  663. head = page_buffers(page);
  664. bh = head;
  665. do {
  666. if (buffer_dirty(bh))
  667. csum_tree_block(root, bh, 0);
  668. bh = bh->b_this_page;
  669. } while (bh != head);
  670. return block_write_full_page(page, btree_get_block, wbc);
  671. }
  672. #endif
  673. static struct address_space_operations btree_aops = {
  674. .readpage = btree_readpage,
  675. .writepage = btree_writepage,
  676. .writepages = btree_writepages,
  677. .releasepage = btree_releasepage,
  678. .invalidatepage = btree_invalidatepage,
  679. .sync_page = block_sync_page,
  680. };
  681. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
  682. u64 parent_transid)
  683. {
  684. struct extent_buffer *buf = NULL;
  685. struct inode *btree_inode = root->fs_info->btree_inode;
  686. int ret = 0;
  687. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  688. if (!buf)
  689. return 0;
  690. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  691. buf, 0, 0, btree_get_extent, 0);
  692. free_extent_buffer(buf);
  693. return ret;
  694. }
  695. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  696. u64 bytenr, u32 blocksize)
  697. {
  698. struct inode *btree_inode = root->fs_info->btree_inode;
  699. struct extent_buffer *eb;
  700. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  701. bytenr, blocksize, GFP_NOFS);
  702. return eb;
  703. }
  704. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  705. u64 bytenr, u32 blocksize)
  706. {
  707. struct inode *btree_inode = root->fs_info->btree_inode;
  708. struct extent_buffer *eb;
  709. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  710. bytenr, blocksize, NULL, GFP_NOFS);
  711. return eb;
  712. }
  713. int btrfs_write_tree_block(struct extent_buffer *buf)
  714. {
  715. return btrfs_fdatawrite_range(buf->first_page->mapping, buf->start,
  716. buf->start + buf->len - 1, WB_SYNC_ALL);
  717. }
  718. int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
  719. {
  720. return btrfs_wait_on_page_writeback_range(buf->first_page->mapping,
  721. buf->start, buf->start + buf->len -1);
  722. }
  723. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  724. u32 blocksize, u64 parent_transid)
  725. {
  726. struct extent_buffer *buf = NULL;
  727. struct inode *btree_inode = root->fs_info->btree_inode;
  728. struct extent_io_tree *io_tree;
  729. int ret;
  730. io_tree = &BTRFS_I(btree_inode)->io_tree;
  731. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  732. if (!buf)
  733. return NULL;
  734. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  735. if (ret == 0) {
  736. buf->flags |= EXTENT_UPTODATE;
  737. } else {
  738. WARN_ON(1);
  739. }
  740. return buf;
  741. }
  742. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  743. struct extent_buffer *buf)
  744. {
  745. struct inode *btree_inode = root->fs_info->btree_inode;
  746. if (btrfs_header_generation(buf) ==
  747. root->fs_info->running_transaction->transid) {
  748. WARN_ON(!btrfs_tree_locked(buf));
  749. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  750. buf);
  751. }
  752. return 0;
  753. }
  754. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  755. u32 stripesize, struct btrfs_root *root,
  756. struct btrfs_fs_info *fs_info,
  757. u64 objectid)
  758. {
  759. root->node = NULL;
  760. root->commit_root = NULL;
  761. root->ref_tree = NULL;
  762. root->sectorsize = sectorsize;
  763. root->nodesize = nodesize;
  764. root->leafsize = leafsize;
  765. root->stripesize = stripesize;
  766. root->ref_cows = 0;
  767. root->track_dirty = 0;
  768. root->fs_info = fs_info;
  769. root->objectid = objectid;
  770. root->last_trans = 0;
  771. root->highest_inode = 0;
  772. root->last_inode_alloc = 0;
  773. root->name = NULL;
  774. root->in_sysfs = 0;
  775. INIT_LIST_HEAD(&root->dirty_list);
  776. INIT_LIST_HEAD(&root->orphan_list);
  777. INIT_LIST_HEAD(&root->dead_list);
  778. spin_lock_init(&root->node_lock);
  779. spin_lock_init(&root->list_lock);
  780. mutex_init(&root->objectid_mutex);
  781. mutex_init(&root->log_mutex);
  782. extent_io_tree_init(&root->dirty_log_pages,
  783. fs_info->btree_inode->i_mapping, GFP_NOFS);
  784. btrfs_leaf_ref_tree_init(&root->ref_tree_struct);
  785. root->ref_tree = &root->ref_tree_struct;
  786. memset(&root->root_key, 0, sizeof(root->root_key));
  787. memset(&root->root_item, 0, sizeof(root->root_item));
  788. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  789. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  790. root->defrag_trans_start = fs_info->generation;
  791. init_completion(&root->kobj_unregister);
  792. root->defrag_running = 0;
  793. root->defrag_level = 0;
  794. root->root_key.objectid = objectid;
  795. root->anon_super.s_root = NULL;
  796. root->anon_super.s_dev = 0;
  797. INIT_LIST_HEAD(&root->anon_super.s_list);
  798. INIT_LIST_HEAD(&root->anon_super.s_instances);
  799. init_rwsem(&root->anon_super.s_umount);
  800. return 0;
  801. }
  802. static int find_and_setup_root(struct btrfs_root *tree_root,
  803. struct btrfs_fs_info *fs_info,
  804. u64 objectid,
  805. struct btrfs_root *root)
  806. {
  807. int ret;
  808. u32 blocksize;
  809. u64 generation;
  810. __setup_root(tree_root->nodesize, tree_root->leafsize,
  811. tree_root->sectorsize, tree_root->stripesize,
  812. root, fs_info, objectid);
  813. ret = btrfs_find_last_root(tree_root, objectid,
  814. &root->root_item, &root->root_key);
  815. BUG_ON(ret);
  816. generation = btrfs_root_generation(&root->root_item);
  817. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  818. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  819. blocksize, generation);
  820. BUG_ON(!root->node);
  821. return 0;
  822. }
  823. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  824. struct btrfs_fs_info *fs_info)
  825. {
  826. struct extent_buffer *eb;
  827. struct btrfs_root *log_root_tree = fs_info->log_root_tree;
  828. u64 start = 0;
  829. u64 end = 0;
  830. int ret;
  831. if (!log_root_tree)
  832. return 0;
  833. while(1) {
  834. ret = find_first_extent_bit(&log_root_tree->dirty_log_pages,
  835. 0, &start, &end, EXTENT_DIRTY);
  836. if (ret)
  837. break;
  838. clear_extent_dirty(&log_root_tree->dirty_log_pages,
  839. start, end, GFP_NOFS);
  840. }
  841. eb = fs_info->log_root_tree->node;
  842. WARN_ON(btrfs_header_level(eb) != 0);
  843. WARN_ON(btrfs_header_nritems(eb) != 0);
  844. ret = btrfs_free_reserved_extent(fs_info->tree_root,
  845. eb->start, eb->len);
  846. BUG_ON(ret);
  847. free_extent_buffer(eb);
  848. kfree(fs_info->log_root_tree);
  849. fs_info->log_root_tree = NULL;
  850. return 0;
  851. }
  852. int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
  853. struct btrfs_fs_info *fs_info)
  854. {
  855. struct btrfs_root *root;
  856. struct btrfs_root *tree_root = fs_info->tree_root;
  857. root = kzalloc(sizeof(*root), GFP_NOFS);
  858. if (!root)
  859. return -ENOMEM;
  860. __setup_root(tree_root->nodesize, tree_root->leafsize,
  861. tree_root->sectorsize, tree_root->stripesize,
  862. root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  863. root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
  864. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  865. root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
  866. root->ref_cows = 0;
  867. root->node = btrfs_alloc_free_block(trans, root, root->leafsize,
  868. 0, BTRFS_TREE_LOG_OBJECTID,
  869. trans->transid, 0, 0, 0);
  870. btrfs_set_header_nritems(root->node, 0);
  871. btrfs_set_header_level(root->node, 0);
  872. btrfs_set_header_bytenr(root->node, root->node->start);
  873. btrfs_set_header_generation(root->node, trans->transid);
  874. btrfs_set_header_owner(root->node, BTRFS_TREE_LOG_OBJECTID);
  875. write_extent_buffer(root->node, root->fs_info->fsid,
  876. (unsigned long)btrfs_header_fsid(root->node),
  877. BTRFS_FSID_SIZE);
  878. btrfs_mark_buffer_dirty(root->node);
  879. btrfs_tree_unlock(root->node);
  880. fs_info->log_root_tree = root;
  881. return 0;
  882. }
  883. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
  884. struct btrfs_key *location)
  885. {
  886. struct btrfs_root *root;
  887. struct btrfs_fs_info *fs_info = tree_root->fs_info;
  888. struct btrfs_path *path;
  889. struct extent_buffer *l;
  890. u64 highest_inode;
  891. u64 generation;
  892. u32 blocksize;
  893. int ret = 0;
  894. root = kzalloc(sizeof(*root), GFP_NOFS);
  895. if (!root)
  896. return ERR_PTR(-ENOMEM);
  897. if (location->offset == (u64)-1) {
  898. ret = find_and_setup_root(tree_root, fs_info,
  899. location->objectid, root);
  900. if (ret) {
  901. kfree(root);
  902. return ERR_PTR(ret);
  903. }
  904. goto insert;
  905. }
  906. __setup_root(tree_root->nodesize, tree_root->leafsize,
  907. tree_root->sectorsize, tree_root->stripesize,
  908. root, fs_info, location->objectid);
  909. path = btrfs_alloc_path();
  910. BUG_ON(!path);
  911. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  912. if (ret != 0) {
  913. if (ret > 0)
  914. ret = -ENOENT;
  915. goto out;
  916. }
  917. l = path->nodes[0];
  918. read_extent_buffer(l, &root->root_item,
  919. btrfs_item_ptr_offset(l, path->slots[0]),
  920. sizeof(root->root_item));
  921. memcpy(&root->root_key, location, sizeof(*location));
  922. ret = 0;
  923. out:
  924. btrfs_release_path(root, path);
  925. btrfs_free_path(path);
  926. if (ret) {
  927. kfree(root);
  928. return ERR_PTR(ret);
  929. }
  930. generation = btrfs_root_generation(&root->root_item);
  931. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  932. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  933. blocksize, generation);
  934. BUG_ON(!root->node);
  935. insert:
  936. if (location->objectid != BTRFS_TREE_LOG_OBJECTID) {
  937. root->ref_cows = 1;
  938. ret = btrfs_find_highest_inode(root, &highest_inode);
  939. if (ret == 0) {
  940. root->highest_inode = highest_inode;
  941. root->last_inode_alloc = highest_inode;
  942. }
  943. }
  944. return root;
  945. }
  946. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  947. u64 root_objectid)
  948. {
  949. struct btrfs_root *root;
  950. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  951. return fs_info->tree_root;
  952. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  953. return fs_info->extent_root;
  954. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  955. (unsigned long)root_objectid);
  956. return root;
  957. }
  958. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  959. struct btrfs_key *location)
  960. {
  961. struct btrfs_root *root;
  962. int ret;
  963. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  964. return fs_info->tree_root;
  965. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  966. return fs_info->extent_root;
  967. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  968. return fs_info->chunk_root;
  969. if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
  970. return fs_info->dev_root;
  971. if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
  972. return fs_info->csum_root;
  973. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  974. (unsigned long)location->objectid);
  975. if (root)
  976. return root;
  977. root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location);
  978. if (IS_ERR(root))
  979. return root;
  980. set_anon_super(&root->anon_super, NULL);
  981. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  982. (unsigned long)root->root_key.objectid,
  983. root);
  984. if (ret) {
  985. free_extent_buffer(root->node);
  986. kfree(root);
  987. return ERR_PTR(ret);
  988. }
  989. if (!(fs_info->sb->s_flags & MS_RDONLY)) {
  990. ret = btrfs_find_dead_roots(fs_info->tree_root,
  991. root->root_key.objectid, root);
  992. BUG_ON(ret);
  993. btrfs_orphan_cleanup(root);
  994. }
  995. return root;
  996. }
  997. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  998. struct btrfs_key *location,
  999. const char *name, int namelen)
  1000. {
  1001. struct btrfs_root *root;
  1002. int ret;
  1003. root = btrfs_read_fs_root_no_name(fs_info, location);
  1004. if (!root)
  1005. return NULL;
  1006. if (root->in_sysfs)
  1007. return root;
  1008. ret = btrfs_set_root_name(root, name, namelen);
  1009. if (ret) {
  1010. free_extent_buffer(root->node);
  1011. kfree(root);
  1012. return ERR_PTR(ret);
  1013. }
  1014. #if 0
  1015. ret = btrfs_sysfs_add_root(root);
  1016. if (ret) {
  1017. free_extent_buffer(root->node);
  1018. kfree(root->name);
  1019. kfree(root);
  1020. return ERR_PTR(ret);
  1021. }
  1022. #endif
  1023. root->in_sysfs = 1;
  1024. return root;
  1025. }
  1026. #if 0
  1027. static int add_hasher(struct btrfs_fs_info *info, char *type) {
  1028. struct btrfs_hasher *hasher;
  1029. hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
  1030. if (!hasher)
  1031. return -ENOMEM;
  1032. hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
  1033. if (!hasher->hash_tfm) {
  1034. kfree(hasher);
  1035. return -EINVAL;
  1036. }
  1037. spin_lock(&info->hash_lock);
  1038. list_add(&hasher->list, &info->hashers);
  1039. spin_unlock(&info->hash_lock);
  1040. return 0;
  1041. }
  1042. #endif
  1043. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  1044. {
  1045. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  1046. int ret = 0;
  1047. struct list_head *cur;
  1048. struct btrfs_device *device;
  1049. struct backing_dev_info *bdi;
  1050. #if 0
  1051. if ((bdi_bits & (1 << BDI_write_congested)) &&
  1052. btrfs_congested_async(info, 0))
  1053. return 1;
  1054. #endif
  1055. list_for_each(cur, &info->fs_devices->devices) {
  1056. device = list_entry(cur, struct btrfs_device, dev_list);
  1057. if (!device->bdev)
  1058. continue;
  1059. bdi = blk_get_backing_dev_info(device->bdev);
  1060. if (bdi && bdi_congested(bdi, bdi_bits)) {
  1061. ret = 1;
  1062. break;
  1063. }
  1064. }
  1065. return ret;
  1066. }
  1067. /*
  1068. * this unplugs every device on the box, and it is only used when page
  1069. * is null
  1070. */
  1071. static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  1072. {
  1073. struct list_head *cur;
  1074. struct btrfs_device *device;
  1075. struct btrfs_fs_info *info;
  1076. info = (struct btrfs_fs_info *)bdi->unplug_io_data;
  1077. list_for_each(cur, &info->fs_devices->devices) {
  1078. device = list_entry(cur, struct btrfs_device, dev_list);
  1079. if (!device->bdev)
  1080. continue;
  1081. bdi = blk_get_backing_dev_info(device->bdev);
  1082. if (bdi->unplug_io_fn) {
  1083. bdi->unplug_io_fn(bdi, page);
  1084. }
  1085. }
  1086. }
  1087. static void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  1088. {
  1089. struct inode *inode;
  1090. struct extent_map_tree *em_tree;
  1091. struct extent_map *em;
  1092. struct address_space *mapping;
  1093. u64 offset;
  1094. /* the generic O_DIRECT read code does this */
  1095. if (1 || !page) {
  1096. __unplug_io_fn(bdi, page);
  1097. return;
  1098. }
  1099. /*
  1100. * page->mapping may change at any time. Get a consistent copy
  1101. * and use that for everything below
  1102. */
  1103. smp_mb();
  1104. mapping = page->mapping;
  1105. if (!mapping)
  1106. return;
  1107. inode = mapping->host;
  1108. /*
  1109. * don't do the expensive searching for a small number of
  1110. * devices
  1111. */
  1112. if (BTRFS_I(inode)->root->fs_info->fs_devices->open_devices <= 2) {
  1113. __unplug_io_fn(bdi, page);
  1114. return;
  1115. }
  1116. offset = page_offset(page);
  1117. em_tree = &BTRFS_I(inode)->extent_tree;
  1118. spin_lock(&em_tree->lock);
  1119. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  1120. spin_unlock(&em_tree->lock);
  1121. if (!em) {
  1122. __unplug_io_fn(bdi, page);
  1123. return;
  1124. }
  1125. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  1126. free_extent_map(em);
  1127. __unplug_io_fn(bdi, page);
  1128. return;
  1129. }
  1130. offset = offset - em->start;
  1131. btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
  1132. em->block_start + offset, page);
  1133. free_extent_map(em);
  1134. }
  1135. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  1136. {
  1137. bdi_init(bdi);
  1138. bdi->ra_pages = default_backing_dev_info.ra_pages;
  1139. bdi->state = 0;
  1140. bdi->capabilities = default_backing_dev_info.capabilities;
  1141. bdi->unplug_io_fn = btrfs_unplug_io_fn;
  1142. bdi->unplug_io_data = info;
  1143. bdi->congested_fn = btrfs_congested_fn;
  1144. bdi->congested_data = info;
  1145. return 0;
  1146. }
  1147. static int bio_ready_for_csum(struct bio *bio)
  1148. {
  1149. u64 length = 0;
  1150. u64 buf_len = 0;
  1151. u64 start = 0;
  1152. struct page *page;
  1153. struct extent_io_tree *io_tree = NULL;
  1154. struct btrfs_fs_info *info = NULL;
  1155. struct bio_vec *bvec;
  1156. int i;
  1157. int ret;
  1158. bio_for_each_segment(bvec, bio, i) {
  1159. page = bvec->bv_page;
  1160. if (page->private == EXTENT_PAGE_PRIVATE) {
  1161. length += bvec->bv_len;
  1162. continue;
  1163. }
  1164. if (!page->private) {
  1165. length += bvec->bv_len;
  1166. continue;
  1167. }
  1168. length = bvec->bv_len;
  1169. buf_len = page->private >> 2;
  1170. start = page_offset(page) + bvec->bv_offset;
  1171. io_tree = &BTRFS_I(page->mapping->host)->io_tree;
  1172. info = BTRFS_I(page->mapping->host)->root->fs_info;
  1173. }
  1174. /* are we fully contained in this bio? */
  1175. if (buf_len <= length)
  1176. return 1;
  1177. ret = extent_range_uptodate(io_tree, start + length,
  1178. start + buf_len - 1);
  1179. if (ret == 1)
  1180. return ret;
  1181. return ret;
  1182. }
  1183. /*
  1184. * called by the kthread helper functions to finally call the bio end_io
  1185. * functions. This is where read checksum verification actually happens
  1186. */
  1187. static void end_workqueue_fn(struct btrfs_work *work)
  1188. {
  1189. struct bio *bio;
  1190. struct end_io_wq *end_io_wq;
  1191. struct btrfs_fs_info *fs_info;
  1192. int error;
  1193. end_io_wq = container_of(work, struct end_io_wq, work);
  1194. bio = end_io_wq->bio;
  1195. fs_info = end_io_wq->info;
  1196. /* metadata bios are special because the whole tree block must
  1197. * be checksummed at once. This makes sure the entire block is in
  1198. * ram and up to date before trying to verify things. For
  1199. * blocksize <= pagesize, it is basically a noop
  1200. */
  1201. if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
  1202. btrfs_queue_worker(&fs_info->endio_meta_workers,
  1203. &end_io_wq->work);
  1204. return;
  1205. }
  1206. error = end_io_wq->error;
  1207. bio->bi_private = end_io_wq->private;
  1208. bio->bi_end_io = end_io_wq->end_io;
  1209. kfree(end_io_wq);
  1210. bio_endio(bio, error);
  1211. }
  1212. static int cleaner_kthread(void *arg)
  1213. {
  1214. struct btrfs_root *root = arg;
  1215. do {
  1216. smp_mb();
  1217. if (root->fs_info->closing)
  1218. break;
  1219. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1220. mutex_lock(&root->fs_info->cleaner_mutex);
  1221. btrfs_clean_old_snapshots(root);
  1222. mutex_unlock(&root->fs_info->cleaner_mutex);
  1223. if (freezing(current)) {
  1224. refrigerator();
  1225. } else {
  1226. smp_mb();
  1227. if (root->fs_info->closing)
  1228. break;
  1229. set_current_state(TASK_INTERRUPTIBLE);
  1230. schedule();
  1231. __set_current_state(TASK_RUNNING);
  1232. }
  1233. } while (!kthread_should_stop());
  1234. return 0;
  1235. }
  1236. static int transaction_kthread(void *arg)
  1237. {
  1238. struct btrfs_root *root = arg;
  1239. struct btrfs_trans_handle *trans;
  1240. struct btrfs_transaction *cur;
  1241. unsigned long now;
  1242. unsigned long delay;
  1243. int ret;
  1244. do {
  1245. smp_mb();
  1246. if (root->fs_info->closing)
  1247. break;
  1248. delay = HZ * 30;
  1249. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1250. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  1251. if (root->fs_info->total_ref_cache_size > 20 * 1024 * 1024) {
  1252. printk("btrfs: total reference cache size %Lu\n",
  1253. root->fs_info->total_ref_cache_size);
  1254. }
  1255. mutex_lock(&root->fs_info->trans_mutex);
  1256. cur = root->fs_info->running_transaction;
  1257. if (!cur) {
  1258. mutex_unlock(&root->fs_info->trans_mutex);
  1259. goto sleep;
  1260. }
  1261. now = get_seconds();
  1262. if (now < cur->start_time || now - cur->start_time < 30) {
  1263. mutex_unlock(&root->fs_info->trans_mutex);
  1264. delay = HZ * 5;
  1265. goto sleep;
  1266. }
  1267. mutex_unlock(&root->fs_info->trans_mutex);
  1268. trans = btrfs_start_transaction(root, 1);
  1269. ret = btrfs_commit_transaction(trans, root);
  1270. sleep:
  1271. wake_up_process(root->fs_info->cleaner_kthread);
  1272. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  1273. if (freezing(current)) {
  1274. refrigerator();
  1275. } else {
  1276. if (root->fs_info->closing)
  1277. break;
  1278. set_current_state(TASK_INTERRUPTIBLE);
  1279. schedule_timeout(delay);
  1280. __set_current_state(TASK_RUNNING);
  1281. }
  1282. } while (!kthread_should_stop());
  1283. return 0;
  1284. }
  1285. struct btrfs_root *open_ctree(struct super_block *sb,
  1286. struct btrfs_fs_devices *fs_devices,
  1287. char *options)
  1288. {
  1289. u32 sectorsize;
  1290. u32 nodesize;
  1291. u32 leafsize;
  1292. u32 blocksize;
  1293. u32 stripesize;
  1294. u64 generation;
  1295. u64 features;
  1296. struct btrfs_key location;
  1297. struct buffer_head *bh;
  1298. struct btrfs_root *extent_root = kzalloc(sizeof(struct btrfs_root),
  1299. GFP_NOFS);
  1300. struct btrfs_root *csum_root = kzalloc(sizeof(struct btrfs_root),
  1301. GFP_NOFS);
  1302. struct btrfs_root *tree_root = kzalloc(sizeof(struct btrfs_root),
  1303. GFP_NOFS);
  1304. struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
  1305. GFP_NOFS);
  1306. struct btrfs_root *chunk_root = kzalloc(sizeof(struct btrfs_root),
  1307. GFP_NOFS);
  1308. struct btrfs_root *dev_root = kzalloc(sizeof(struct btrfs_root),
  1309. GFP_NOFS);
  1310. struct btrfs_root *log_tree_root;
  1311. int ret;
  1312. int err = -EINVAL;
  1313. struct btrfs_super_block *disk_super;
  1314. if (!extent_root || !tree_root || !fs_info ||
  1315. !chunk_root || !dev_root || !csum_root) {
  1316. err = -ENOMEM;
  1317. goto fail;
  1318. }
  1319. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  1320. INIT_LIST_HEAD(&fs_info->trans_list);
  1321. INIT_LIST_HEAD(&fs_info->dead_roots);
  1322. INIT_LIST_HEAD(&fs_info->hashers);
  1323. INIT_LIST_HEAD(&fs_info->delalloc_inodes);
  1324. spin_lock_init(&fs_info->hash_lock);
  1325. spin_lock_init(&fs_info->delalloc_lock);
  1326. spin_lock_init(&fs_info->new_trans_lock);
  1327. spin_lock_init(&fs_info->ref_cache_lock);
  1328. init_completion(&fs_info->kobj_unregister);
  1329. fs_info->tree_root = tree_root;
  1330. fs_info->extent_root = extent_root;
  1331. fs_info->csum_root = csum_root;
  1332. fs_info->chunk_root = chunk_root;
  1333. fs_info->dev_root = dev_root;
  1334. fs_info->fs_devices = fs_devices;
  1335. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  1336. INIT_LIST_HEAD(&fs_info->space_info);
  1337. btrfs_mapping_init(&fs_info->mapping_tree);
  1338. atomic_set(&fs_info->nr_async_submits, 0);
  1339. atomic_set(&fs_info->async_delalloc_pages, 0);
  1340. atomic_set(&fs_info->async_submit_draining, 0);
  1341. atomic_set(&fs_info->nr_async_bios, 0);
  1342. atomic_set(&fs_info->throttles, 0);
  1343. atomic_set(&fs_info->throttle_gen, 0);
  1344. fs_info->sb = sb;
  1345. fs_info->max_extent = (u64)-1;
  1346. fs_info->max_inline = 8192 * 1024;
  1347. setup_bdi(fs_info, &fs_info->bdi);
  1348. fs_info->btree_inode = new_inode(sb);
  1349. fs_info->btree_inode->i_ino = 1;
  1350. fs_info->btree_inode->i_nlink = 1;
  1351. fs_info->thread_pool_size = min(num_online_cpus() + 2, 8);
  1352. INIT_LIST_HEAD(&fs_info->ordered_extents);
  1353. spin_lock_init(&fs_info->ordered_extent_lock);
  1354. sb->s_blocksize = 4096;
  1355. sb->s_blocksize_bits = blksize_bits(4096);
  1356. /*
  1357. * we set the i_size on the btree inode to the max possible int.
  1358. * the real end of the address space is determined by all of
  1359. * the devices in the system
  1360. */
  1361. fs_info->btree_inode->i_size = OFFSET_MAX;
  1362. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  1363. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  1364. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  1365. fs_info->btree_inode->i_mapping,
  1366. GFP_NOFS);
  1367. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  1368. GFP_NOFS);
  1369. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  1370. spin_lock_init(&fs_info->block_group_cache_lock);
  1371. fs_info->block_group_cache_tree.rb_node = NULL;
  1372. extent_io_tree_init(&fs_info->pinned_extents,
  1373. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1374. extent_io_tree_init(&fs_info->pending_del,
  1375. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1376. extent_io_tree_init(&fs_info->extent_ins,
  1377. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1378. fs_info->do_barriers = 1;
  1379. INIT_LIST_HEAD(&fs_info->dead_reloc_roots);
  1380. btrfs_leaf_ref_tree_init(&fs_info->reloc_ref_tree);
  1381. btrfs_leaf_ref_tree_init(&fs_info->shared_ref_tree);
  1382. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  1383. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  1384. sizeof(struct btrfs_key));
  1385. insert_inode_hash(fs_info->btree_inode);
  1386. mutex_init(&fs_info->trans_mutex);
  1387. mutex_init(&fs_info->tree_log_mutex);
  1388. mutex_init(&fs_info->drop_mutex);
  1389. mutex_init(&fs_info->extent_ins_mutex);
  1390. mutex_init(&fs_info->pinned_mutex);
  1391. mutex_init(&fs_info->chunk_mutex);
  1392. mutex_init(&fs_info->transaction_kthread_mutex);
  1393. mutex_init(&fs_info->cleaner_mutex);
  1394. mutex_init(&fs_info->volume_mutex);
  1395. mutex_init(&fs_info->tree_reloc_mutex);
  1396. init_waitqueue_head(&fs_info->transaction_throttle);
  1397. init_waitqueue_head(&fs_info->transaction_wait);
  1398. init_waitqueue_head(&fs_info->async_submit_wait);
  1399. init_waitqueue_head(&fs_info->tree_log_wait);
  1400. atomic_set(&fs_info->tree_log_commit, 0);
  1401. atomic_set(&fs_info->tree_log_writers, 0);
  1402. fs_info->tree_log_transid = 0;
  1403. #if 0
  1404. ret = add_hasher(fs_info, "crc32c");
  1405. if (ret) {
  1406. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  1407. err = -ENOMEM;
  1408. goto fail_iput;
  1409. }
  1410. #endif
  1411. __setup_root(4096, 4096, 4096, 4096, tree_root,
  1412. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  1413. bh = btrfs_read_dev_super(fs_devices->latest_bdev);
  1414. if (!bh)
  1415. goto fail_iput;
  1416. memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
  1417. brelse(bh);
  1418. memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
  1419. disk_super = &fs_info->super_copy;
  1420. if (!btrfs_super_root(disk_super))
  1421. goto fail_iput;
  1422. ret = btrfs_parse_options(tree_root, options);
  1423. if (ret) {
  1424. err = ret;
  1425. goto fail_iput;
  1426. }
  1427. features = btrfs_super_incompat_flags(disk_super) &
  1428. ~BTRFS_FEATURE_INCOMPAT_SUPP;
  1429. if (features) {
  1430. printk(KERN_ERR "BTRFS: couldn't mount because of "
  1431. "unsupported optional features (%Lx).\n",
  1432. features);
  1433. err = -EINVAL;
  1434. goto fail_iput;
  1435. }
  1436. features = btrfs_super_compat_ro_flags(disk_super) &
  1437. ~BTRFS_FEATURE_COMPAT_RO_SUPP;
  1438. if (!(sb->s_flags & MS_RDONLY) && features) {
  1439. printk(KERN_ERR "BTRFS: couldn't mount RDWR because of "
  1440. "unsupported option features (%Lx).\n",
  1441. features);
  1442. err = -EINVAL;
  1443. goto fail_iput;
  1444. }
  1445. /*
  1446. * we need to start all the end_io workers up front because the
  1447. * queue work function gets called at interrupt time, and so it
  1448. * cannot dynamically grow.
  1449. */
  1450. btrfs_init_workers(&fs_info->workers, "worker",
  1451. fs_info->thread_pool_size);
  1452. btrfs_init_workers(&fs_info->delalloc_workers, "delalloc",
  1453. fs_info->thread_pool_size);
  1454. btrfs_init_workers(&fs_info->submit_workers, "submit",
  1455. min_t(u64, fs_devices->num_devices,
  1456. fs_info->thread_pool_size));
  1457. /* a higher idle thresh on the submit workers makes it much more
  1458. * likely that bios will be send down in a sane order to the
  1459. * devices
  1460. */
  1461. fs_info->submit_workers.idle_thresh = 64;
  1462. fs_info->workers.idle_thresh = 16;
  1463. fs_info->workers.ordered = 1;
  1464. fs_info->delalloc_workers.idle_thresh = 2;
  1465. fs_info->delalloc_workers.ordered = 1;
  1466. btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1);
  1467. btrfs_init_workers(&fs_info->endio_workers, "endio",
  1468. fs_info->thread_pool_size);
  1469. btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta",
  1470. fs_info->thread_pool_size);
  1471. btrfs_init_workers(&fs_info->endio_write_workers, "endio-write",
  1472. fs_info->thread_pool_size);
  1473. /*
  1474. * endios are largely parallel and should have a very
  1475. * low idle thresh
  1476. */
  1477. fs_info->endio_workers.idle_thresh = 4;
  1478. fs_info->endio_write_workers.idle_thresh = 64;
  1479. btrfs_start_workers(&fs_info->workers, 1);
  1480. btrfs_start_workers(&fs_info->submit_workers, 1);
  1481. btrfs_start_workers(&fs_info->delalloc_workers, 1);
  1482. btrfs_start_workers(&fs_info->fixup_workers, 1);
  1483. btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
  1484. btrfs_start_workers(&fs_info->endio_meta_workers,
  1485. fs_info->thread_pool_size);
  1486. btrfs_start_workers(&fs_info->endio_write_workers,
  1487. fs_info->thread_pool_size);
  1488. fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
  1489. fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
  1490. 4 * 1024 * 1024 / PAGE_CACHE_SIZE);
  1491. nodesize = btrfs_super_nodesize(disk_super);
  1492. leafsize = btrfs_super_leafsize(disk_super);
  1493. sectorsize = btrfs_super_sectorsize(disk_super);
  1494. stripesize = btrfs_super_stripesize(disk_super);
  1495. tree_root->nodesize = nodesize;
  1496. tree_root->leafsize = leafsize;
  1497. tree_root->sectorsize = sectorsize;
  1498. tree_root->stripesize = stripesize;
  1499. sb->s_blocksize = sectorsize;
  1500. sb->s_blocksize_bits = blksize_bits(sectorsize);
  1501. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  1502. sizeof(disk_super->magic))) {
  1503. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  1504. goto fail_sb_buffer;
  1505. }
  1506. mutex_lock(&fs_info->chunk_mutex);
  1507. ret = btrfs_read_sys_array(tree_root);
  1508. mutex_unlock(&fs_info->chunk_mutex);
  1509. if (ret) {
  1510. printk("btrfs: failed to read the system array on %s\n",
  1511. sb->s_id);
  1512. goto fail_sys_array;
  1513. }
  1514. blocksize = btrfs_level_size(tree_root,
  1515. btrfs_super_chunk_root_level(disk_super));
  1516. generation = btrfs_super_chunk_root_generation(disk_super);
  1517. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1518. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  1519. chunk_root->node = read_tree_block(chunk_root,
  1520. btrfs_super_chunk_root(disk_super),
  1521. blocksize, generation);
  1522. BUG_ON(!chunk_root->node);
  1523. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  1524. (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
  1525. BTRFS_UUID_SIZE);
  1526. mutex_lock(&fs_info->chunk_mutex);
  1527. ret = btrfs_read_chunk_tree(chunk_root);
  1528. mutex_unlock(&fs_info->chunk_mutex);
  1529. if (ret) {
  1530. printk("btrfs: failed to read chunk tree on %s\n", sb->s_id);
  1531. goto fail_chunk_root;
  1532. }
  1533. btrfs_close_extra_devices(fs_devices);
  1534. blocksize = btrfs_level_size(tree_root,
  1535. btrfs_super_root_level(disk_super));
  1536. generation = btrfs_super_generation(disk_super);
  1537. tree_root->node = read_tree_block(tree_root,
  1538. btrfs_super_root(disk_super),
  1539. blocksize, generation);
  1540. if (!tree_root->node)
  1541. goto fail_chunk_root;
  1542. ret = find_and_setup_root(tree_root, fs_info,
  1543. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  1544. if (ret)
  1545. goto fail_tree_root;
  1546. extent_root->track_dirty = 1;
  1547. ret = find_and_setup_root(tree_root, fs_info,
  1548. BTRFS_DEV_TREE_OBJECTID, dev_root);
  1549. dev_root->track_dirty = 1;
  1550. if (ret)
  1551. goto fail_extent_root;
  1552. ret = find_and_setup_root(tree_root, fs_info,
  1553. BTRFS_CSUM_TREE_OBJECTID, csum_root);
  1554. if (ret)
  1555. goto fail_extent_root;
  1556. csum_root->track_dirty = 1;
  1557. btrfs_read_block_groups(extent_root);
  1558. fs_info->generation = generation + 1;
  1559. fs_info->last_trans_committed = generation;
  1560. fs_info->data_alloc_profile = (u64)-1;
  1561. fs_info->metadata_alloc_profile = (u64)-1;
  1562. fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
  1563. fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
  1564. "btrfs-cleaner");
  1565. if (!fs_info->cleaner_kthread)
  1566. goto fail_csum_root;
  1567. fs_info->transaction_kthread = kthread_run(transaction_kthread,
  1568. tree_root,
  1569. "btrfs-transaction");
  1570. if (!fs_info->transaction_kthread)
  1571. goto fail_cleaner;
  1572. if (btrfs_super_log_root(disk_super) != 0) {
  1573. u64 bytenr = btrfs_super_log_root(disk_super);
  1574. if (fs_devices->rw_devices == 0) {
  1575. printk("Btrfs log replay required on RO media\n");
  1576. err = -EIO;
  1577. goto fail_trans_kthread;
  1578. }
  1579. blocksize =
  1580. btrfs_level_size(tree_root,
  1581. btrfs_super_log_root_level(disk_super));
  1582. log_tree_root = kzalloc(sizeof(struct btrfs_root),
  1583. GFP_NOFS);
  1584. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1585. log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  1586. log_tree_root->node = read_tree_block(tree_root, bytenr,
  1587. blocksize,
  1588. generation + 1);
  1589. ret = btrfs_recover_log_trees(log_tree_root);
  1590. BUG_ON(ret);
  1591. if (sb->s_flags & MS_RDONLY) {
  1592. ret = btrfs_commit_super(tree_root);
  1593. BUG_ON(ret);
  1594. }
  1595. }
  1596. if (!(sb->s_flags & MS_RDONLY)) {
  1597. ret = btrfs_cleanup_reloc_trees(tree_root);
  1598. BUG_ON(ret);
  1599. }
  1600. location.objectid = BTRFS_FS_TREE_OBJECTID;
  1601. location.type = BTRFS_ROOT_ITEM_KEY;
  1602. location.offset = (u64)-1;
  1603. fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
  1604. if (!fs_info->fs_root)
  1605. goto fail_trans_kthread;
  1606. return tree_root;
  1607. fail_trans_kthread:
  1608. kthread_stop(fs_info->transaction_kthread);
  1609. fail_cleaner:
  1610. kthread_stop(fs_info->cleaner_kthread);
  1611. /*
  1612. * make sure we're done with the btree inode before we stop our
  1613. * kthreads
  1614. */
  1615. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  1616. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  1617. fail_csum_root:
  1618. free_extent_buffer(csum_root->node);
  1619. fail_extent_root:
  1620. free_extent_buffer(extent_root->node);
  1621. fail_tree_root:
  1622. free_extent_buffer(tree_root->node);
  1623. fail_chunk_root:
  1624. free_extent_buffer(chunk_root->node);
  1625. fail_sys_array:
  1626. free_extent_buffer(dev_root->node);
  1627. fail_sb_buffer:
  1628. btrfs_stop_workers(&fs_info->fixup_workers);
  1629. btrfs_stop_workers(&fs_info->delalloc_workers);
  1630. btrfs_stop_workers(&fs_info->workers);
  1631. btrfs_stop_workers(&fs_info->endio_workers);
  1632. btrfs_stop_workers(&fs_info->endio_meta_workers);
  1633. btrfs_stop_workers(&fs_info->endio_write_workers);
  1634. btrfs_stop_workers(&fs_info->submit_workers);
  1635. fail_iput:
  1636. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  1637. iput(fs_info->btree_inode);
  1638. fail:
  1639. btrfs_close_devices(fs_info->fs_devices);
  1640. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1641. kfree(extent_root);
  1642. kfree(tree_root);
  1643. bdi_destroy(&fs_info->bdi);
  1644. kfree(fs_info);
  1645. kfree(chunk_root);
  1646. kfree(dev_root);
  1647. kfree(csum_root);
  1648. return ERR_PTR(err);
  1649. }
  1650. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  1651. {
  1652. char b[BDEVNAME_SIZE];
  1653. if (uptodate) {
  1654. set_buffer_uptodate(bh);
  1655. } else {
  1656. if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
  1657. printk(KERN_WARNING "lost page write due to "
  1658. "I/O error on %s\n",
  1659. bdevname(bh->b_bdev, b));
  1660. }
  1661. /* note, we dont' set_buffer_write_io_error because we have
  1662. * our own ways of dealing with the IO errors
  1663. */
  1664. clear_buffer_uptodate(bh);
  1665. }
  1666. unlock_buffer(bh);
  1667. put_bh(bh);
  1668. }
  1669. struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
  1670. {
  1671. struct buffer_head *bh;
  1672. struct buffer_head *latest = NULL;
  1673. struct btrfs_super_block *super;
  1674. int i;
  1675. u64 transid = 0;
  1676. u64 bytenr;
  1677. /* we would like to check all the supers, but that would make
  1678. * a btrfs mount succeed after a mkfs from a different FS.
  1679. * So, we need to add a special mount option to scan for
  1680. * later supers, using BTRFS_SUPER_MIRROR_MAX instead
  1681. */
  1682. for (i = 0; i < 1; i++) {
  1683. bytenr = btrfs_sb_offset(i);
  1684. if (bytenr + 4096 >= i_size_read(bdev->bd_inode))
  1685. break;
  1686. bh = __bread(bdev, bytenr / 4096, 4096);
  1687. if (!bh)
  1688. continue;
  1689. super = (struct btrfs_super_block *)bh->b_data;
  1690. if (btrfs_super_bytenr(super) != bytenr ||
  1691. strncmp((char *)(&super->magic), BTRFS_MAGIC,
  1692. sizeof(super->magic))) {
  1693. brelse(bh);
  1694. continue;
  1695. }
  1696. if (!latest || btrfs_super_generation(super) > transid) {
  1697. brelse(latest);
  1698. latest = bh;
  1699. transid = btrfs_super_generation(super);
  1700. } else {
  1701. brelse(bh);
  1702. }
  1703. }
  1704. return latest;
  1705. }
  1706. static int write_dev_supers(struct btrfs_device *device,
  1707. struct btrfs_super_block *sb,
  1708. int do_barriers, int wait, int max_mirrors)
  1709. {
  1710. struct buffer_head *bh;
  1711. int i;
  1712. int ret;
  1713. int errors = 0;
  1714. u32 crc;
  1715. u64 bytenr;
  1716. int last_barrier = 0;
  1717. if (max_mirrors == 0)
  1718. max_mirrors = BTRFS_SUPER_MIRROR_MAX;
  1719. /* make sure only the last submit_bh does a barrier */
  1720. if (do_barriers) {
  1721. for (i = 0; i < max_mirrors; i++) {
  1722. bytenr = btrfs_sb_offset(i);
  1723. if (bytenr + BTRFS_SUPER_INFO_SIZE >=
  1724. device->total_bytes)
  1725. break;
  1726. last_barrier = i;
  1727. }
  1728. }
  1729. for (i = 0; i < max_mirrors; i++) {
  1730. bytenr = btrfs_sb_offset(i);
  1731. if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
  1732. break;
  1733. if (wait) {
  1734. bh = __find_get_block(device->bdev, bytenr / 4096,
  1735. BTRFS_SUPER_INFO_SIZE);
  1736. BUG_ON(!bh);
  1737. brelse(bh);
  1738. wait_on_buffer(bh);
  1739. if (buffer_uptodate(bh)) {
  1740. brelse(bh);
  1741. continue;
  1742. }
  1743. } else {
  1744. btrfs_set_super_bytenr(sb, bytenr);
  1745. crc = ~(u32)0;
  1746. crc = btrfs_csum_data(NULL, (char *)sb +
  1747. BTRFS_CSUM_SIZE, crc,
  1748. BTRFS_SUPER_INFO_SIZE -
  1749. BTRFS_CSUM_SIZE);
  1750. btrfs_csum_final(crc, sb->csum);
  1751. bh = __getblk(device->bdev, bytenr / 4096,
  1752. BTRFS_SUPER_INFO_SIZE);
  1753. memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
  1754. set_buffer_uptodate(bh);
  1755. get_bh(bh);
  1756. lock_buffer(bh);
  1757. bh->b_end_io = btrfs_end_buffer_write_sync;
  1758. }
  1759. if (i == last_barrier && do_barriers && device->barriers) {
  1760. ret = submit_bh(WRITE_BARRIER, bh);
  1761. if (ret == -EOPNOTSUPP) {
  1762. printk("btrfs: disabling barriers on dev %s\n",
  1763. device->name);
  1764. set_buffer_uptodate(bh);
  1765. device->barriers = 0;
  1766. get_bh(bh);
  1767. lock_buffer(bh);
  1768. ret = submit_bh(WRITE, bh);
  1769. }
  1770. } else {
  1771. ret = submit_bh(WRITE, bh);
  1772. }
  1773. if (!ret && wait) {
  1774. wait_on_buffer(bh);
  1775. if (!buffer_uptodate(bh))
  1776. errors++;
  1777. } else if (ret) {
  1778. errors++;
  1779. }
  1780. if (wait)
  1781. brelse(bh);
  1782. }
  1783. return errors < i ? 0 : -1;
  1784. }
  1785. int write_all_supers(struct btrfs_root *root, int max_mirrors)
  1786. {
  1787. struct list_head *cur;
  1788. struct list_head *head = &root->fs_info->fs_devices->devices;
  1789. struct btrfs_device *dev;
  1790. struct btrfs_super_block *sb;
  1791. struct btrfs_dev_item *dev_item;
  1792. int ret;
  1793. int do_barriers;
  1794. int max_errors;
  1795. int total_errors = 0;
  1796. u64 flags;
  1797. max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
  1798. do_barriers = !btrfs_test_opt(root, NOBARRIER);
  1799. sb = &root->fs_info->super_for_commit;
  1800. dev_item = &sb->dev_item;
  1801. list_for_each(cur, head) {
  1802. dev = list_entry(cur, struct btrfs_device, dev_list);
  1803. if (!dev->bdev) {
  1804. total_errors++;
  1805. continue;
  1806. }
  1807. if (!dev->in_fs_metadata || !dev->writeable)
  1808. continue;
  1809. btrfs_set_stack_device_generation(dev_item, 0);
  1810. btrfs_set_stack_device_type(dev_item, dev->type);
  1811. btrfs_set_stack_device_id(dev_item, dev->devid);
  1812. btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
  1813. btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
  1814. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  1815. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  1816. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  1817. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  1818. memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
  1819. flags = btrfs_super_flags(sb);
  1820. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  1821. ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors);
  1822. if (ret)
  1823. total_errors++;
  1824. }
  1825. if (total_errors > max_errors) {
  1826. printk("btrfs: %d errors while writing supers\n", total_errors);
  1827. BUG();
  1828. }
  1829. total_errors = 0;
  1830. list_for_each(cur, head) {
  1831. dev = list_entry(cur, struct btrfs_device, dev_list);
  1832. if (!dev->bdev)
  1833. continue;
  1834. if (!dev->in_fs_metadata || !dev->writeable)
  1835. continue;
  1836. ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors);
  1837. if (ret)
  1838. total_errors++;
  1839. }
  1840. if (total_errors > max_errors) {
  1841. printk("btrfs: %d errors while writing supers\n", total_errors);
  1842. BUG();
  1843. }
  1844. return 0;
  1845. }
  1846. int write_ctree_super(struct btrfs_trans_handle *trans,
  1847. struct btrfs_root *root, int max_mirrors)
  1848. {
  1849. int ret;
  1850. ret = write_all_supers(root, max_mirrors);
  1851. return ret;
  1852. }
  1853. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  1854. {
  1855. radix_tree_delete(&fs_info->fs_roots_radix,
  1856. (unsigned long)root->root_key.objectid);
  1857. if (root->anon_super.s_dev) {
  1858. down_write(&root->anon_super.s_umount);
  1859. kill_anon_super(&root->anon_super);
  1860. }
  1861. #if 0
  1862. if (root->in_sysfs)
  1863. btrfs_sysfs_del_root(root);
  1864. #endif
  1865. if (root->node)
  1866. free_extent_buffer(root->node);
  1867. if (root->commit_root)
  1868. free_extent_buffer(root->commit_root);
  1869. if (root->name)
  1870. kfree(root->name);
  1871. kfree(root);
  1872. return 0;
  1873. }
  1874. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  1875. {
  1876. int ret;
  1877. struct btrfs_root *gang[8];
  1878. int i;
  1879. while(1) {
  1880. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1881. (void **)gang, 0,
  1882. ARRAY_SIZE(gang));
  1883. if (!ret)
  1884. break;
  1885. for (i = 0; i < ret; i++)
  1886. btrfs_free_fs_root(fs_info, gang[i]);
  1887. }
  1888. return 0;
  1889. }
  1890. int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
  1891. {
  1892. u64 root_objectid = 0;
  1893. struct btrfs_root *gang[8];
  1894. int i;
  1895. int ret;
  1896. while (1) {
  1897. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1898. (void **)gang, root_objectid,
  1899. ARRAY_SIZE(gang));
  1900. if (!ret)
  1901. break;
  1902. for (i = 0; i < ret; i++) {
  1903. root_objectid = gang[i]->root_key.objectid;
  1904. ret = btrfs_find_dead_roots(fs_info->tree_root,
  1905. root_objectid, gang[i]);
  1906. BUG_ON(ret);
  1907. btrfs_orphan_cleanup(gang[i]);
  1908. }
  1909. root_objectid++;
  1910. }
  1911. return 0;
  1912. }
  1913. int btrfs_commit_super(struct btrfs_root *root)
  1914. {
  1915. struct btrfs_trans_handle *trans;
  1916. int ret;
  1917. mutex_lock(&root->fs_info->cleaner_mutex);
  1918. btrfs_clean_old_snapshots(root);
  1919. mutex_unlock(&root->fs_info->cleaner_mutex);
  1920. trans = btrfs_start_transaction(root, 1);
  1921. ret = btrfs_commit_transaction(trans, root);
  1922. BUG_ON(ret);
  1923. /* run commit again to drop the original snapshot */
  1924. trans = btrfs_start_transaction(root, 1);
  1925. btrfs_commit_transaction(trans, root);
  1926. ret = btrfs_write_and_wait_transaction(NULL, root);
  1927. BUG_ON(ret);
  1928. ret = write_ctree_super(NULL, root, 0);
  1929. return ret;
  1930. }
  1931. int close_ctree(struct btrfs_root *root)
  1932. {
  1933. struct btrfs_fs_info *fs_info = root->fs_info;
  1934. int ret;
  1935. fs_info->closing = 1;
  1936. smp_mb();
  1937. kthread_stop(root->fs_info->transaction_kthread);
  1938. kthread_stop(root->fs_info->cleaner_kthread);
  1939. if (!(fs_info->sb->s_flags & MS_RDONLY)) {
  1940. ret = btrfs_commit_super(root);
  1941. if (ret) {
  1942. printk("btrfs: commit super returns %d\n", ret);
  1943. }
  1944. }
  1945. if (fs_info->delalloc_bytes) {
  1946. printk("btrfs: at unmount delalloc count %Lu\n",
  1947. fs_info->delalloc_bytes);
  1948. }
  1949. if (fs_info->total_ref_cache_size) {
  1950. printk("btrfs: at umount reference cache size %Lu\n",
  1951. fs_info->total_ref_cache_size);
  1952. }
  1953. if (fs_info->extent_root->node)
  1954. free_extent_buffer(fs_info->extent_root->node);
  1955. if (fs_info->tree_root->node)
  1956. free_extent_buffer(fs_info->tree_root->node);
  1957. if (root->fs_info->chunk_root->node);
  1958. free_extent_buffer(root->fs_info->chunk_root->node);
  1959. if (root->fs_info->dev_root->node);
  1960. free_extent_buffer(root->fs_info->dev_root->node);
  1961. if (root->fs_info->csum_root->node);
  1962. free_extent_buffer(root->fs_info->csum_root->node);
  1963. btrfs_free_block_groups(root->fs_info);
  1964. del_fs_roots(fs_info);
  1965. iput(fs_info->btree_inode);
  1966. btrfs_stop_workers(&fs_info->fixup_workers);
  1967. btrfs_stop_workers(&fs_info->delalloc_workers);
  1968. btrfs_stop_workers(&fs_info->workers);
  1969. btrfs_stop_workers(&fs_info->endio_workers);
  1970. btrfs_stop_workers(&fs_info->endio_meta_workers);
  1971. btrfs_stop_workers(&fs_info->endio_write_workers);
  1972. btrfs_stop_workers(&fs_info->submit_workers);
  1973. #if 0
  1974. while(!list_empty(&fs_info->hashers)) {
  1975. struct btrfs_hasher *hasher;
  1976. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  1977. hashers);
  1978. list_del(&hasher->hashers);
  1979. crypto_free_hash(&fs_info->hash_tfm);
  1980. kfree(hasher);
  1981. }
  1982. #endif
  1983. btrfs_close_devices(fs_info->fs_devices);
  1984. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1985. bdi_destroy(&fs_info->bdi);
  1986. kfree(fs_info->extent_root);
  1987. kfree(fs_info->tree_root);
  1988. kfree(fs_info->chunk_root);
  1989. kfree(fs_info->dev_root);
  1990. kfree(fs_info->csum_root);
  1991. return 0;
  1992. }
  1993. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
  1994. {
  1995. int ret;
  1996. struct inode *btree_inode = buf->first_page->mapping->host;
  1997. ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  1998. if (!ret)
  1999. return ret;
  2000. ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
  2001. parent_transid);
  2002. return !ret;
  2003. }
  2004. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  2005. {
  2006. struct inode *btree_inode = buf->first_page->mapping->host;
  2007. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  2008. buf);
  2009. }
  2010. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  2011. {
  2012. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  2013. u64 transid = btrfs_header_generation(buf);
  2014. struct inode *btree_inode = root->fs_info->btree_inode;
  2015. WARN_ON(!btrfs_tree_locked(buf));
  2016. if (transid != root->fs_info->generation) {
  2017. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  2018. (unsigned long long)buf->start,
  2019. transid, root->fs_info->generation);
  2020. WARN_ON(1);
  2021. }
  2022. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  2023. }
  2024. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  2025. {
  2026. /*
  2027. * looks as though older kernels can get into trouble with
  2028. * this code, they end up stuck in balance_dirty_pages forever
  2029. */
  2030. struct extent_io_tree *tree;
  2031. u64 num_dirty;
  2032. u64 start = 0;
  2033. unsigned long thresh = 32 * 1024 * 1024;
  2034. tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  2035. if (current_is_pdflush() || current->flags & PF_MEMALLOC)
  2036. return;
  2037. num_dirty = count_range_bits(tree, &start, (u64)-1,
  2038. thresh, EXTENT_DIRTY);
  2039. if (num_dirty > thresh) {
  2040. balance_dirty_pages_ratelimited_nr(
  2041. root->fs_info->btree_inode->i_mapping, 1);
  2042. }
  2043. return;
  2044. }
  2045. int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
  2046. {
  2047. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  2048. int ret;
  2049. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  2050. if (ret == 0) {
  2051. buf->flags |= EXTENT_UPTODATE;
  2052. }
  2053. return ret;
  2054. }
  2055. int btree_lock_page_hook(struct page *page)
  2056. {
  2057. struct inode *inode = page->mapping->host;
  2058. struct btrfs_root *root = BTRFS_I(inode)->root;
  2059. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2060. struct extent_buffer *eb;
  2061. unsigned long len;
  2062. u64 bytenr = page_offset(page);
  2063. if (page->private == EXTENT_PAGE_PRIVATE)
  2064. goto out;
  2065. len = page->private >> 2;
  2066. eb = find_extent_buffer(io_tree, bytenr, len, GFP_NOFS);
  2067. if (!eb)
  2068. goto out;
  2069. btrfs_tree_lock(eb);
  2070. spin_lock(&root->fs_info->hash_lock);
  2071. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  2072. spin_unlock(&root->fs_info->hash_lock);
  2073. btrfs_tree_unlock(eb);
  2074. free_extent_buffer(eb);
  2075. out:
  2076. lock_page(page);
  2077. return 0;
  2078. }
  2079. static struct extent_io_ops btree_extent_io_ops = {
  2080. .write_cache_pages_lock_hook = btree_lock_page_hook,
  2081. .readpage_end_io_hook = btree_readpage_end_io_hook,
  2082. .submit_bio_hook = btree_submit_bio_hook,
  2083. /* note we're sharing with inode.c for the merge bio hook */
  2084. .merge_bio_hook = btrfs_merge_bio_hook,
  2085. };