disk-io.c 64 KB

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