disk-io.c 64 KB

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