disk-io.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472
  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. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  596. struct extent_buffer *eb;
  597. int was_dirty;
  598. tree = &BTRFS_I(page->mapping->host)->io_tree;
  599. if (!(current->flags & PF_MEMALLOC)) {
  600. return extent_write_full_page(tree, page,
  601. btree_get_extent, wbc);
  602. }
  603. redirty_page_for_writepage(wbc, page);
  604. eb = btrfs_find_tree_block(root, page_offset(page),
  605. PAGE_CACHE_SIZE);
  606. WARN_ON(!eb);
  607. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  608. if (!was_dirty) {
  609. spin_lock(&root->fs_info->delalloc_lock);
  610. root->fs_info->dirty_metadata_bytes += PAGE_CACHE_SIZE;
  611. spin_unlock(&root->fs_info->delalloc_lock);
  612. }
  613. free_extent_buffer(eb);
  614. unlock_page(page);
  615. return 0;
  616. }
  617. static int btree_writepages(struct address_space *mapping,
  618. struct writeback_control *wbc)
  619. {
  620. struct extent_io_tree *tree;
  621. tree = &BTRFS_I(mapping->host)->io_tree;
  622. if (wbc->sync_mode == WB_SYNC_NONE) {
  623. struct btrfs_root *root = BTRFS_I(mapping->host)->root;
  624. u64 num_dirty;
  625. unsigned long thresh = 32 * 1024 * 1024;
  626. if (wbc->for_kupdate)
  627. return 0;
  628. /* this is a bit racy, but that's ok */
  629. num_dirty = root->fs_info->dirty_metadata_bytes;
  630. if (num_dirty < thresh)
  631. return 0;
  632. }
  633. return extent_writepages(tree, mapping, btree_get_extent, wbc);
  634. }
  635. static int btree_readpage(struct file *file, struct page *page)
  636. {
  637. struct extent_io_tree *tree;
  638. tree = &BTRFS_I(page->mapping->host)->io_tree;
  639. return extent_read_full_page(tree, page, btree_get_extent);
  640. }
  641. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  642. {
  643. struct extent_io_tree *tree;
  644. struct extent_map_tree *map;
  645. int ret;
  646. if (PageWriteback(page) || PageDirty(page))
  647. return 0;
  648. tree = &BTRFS_I(page->mapping->host)->io_tree;
  649. map = &BTRFS_I(page->mapping->host)->extent_tree;
  650. ret = try_release_extent_state(map, tree, page, gfp_flags);
  651. if (!ret)
  652. return 0;
  653. ret = try_release_extent_buffer(tree, page);
  654. if (ret == 1) {
  655. ClearPagePrivate(page);
  656. set_page_private(page, 0);
  657. page_cache_release(page);
  658. }
  659. return ret;
  660. }
  661. static void btree_invalidatepage(struct page *page, unsigned long offset)
  662. {
  663. struct extent_io_tree *tree;
  664. tree = &BTRFS_I(page->mapping->host)->io_tree;
  665. extent_invalidatepage(tree, page, offset);
  666. btree_releasepage(page, GFP_NOFS);
  667. if (PagePrivate(page)) {
  668. printk(KERN_WARNING "btrfs warning page private not zero "
  669. "on page %llu\n", (unsigned long long)page_offset(page));
  670. ClearPagePrivate(page);
  671. set_page_private(page, 0);
  672. page_cache_release(page);
  673. }
  674. }
  675. #if 0
  676. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  677. {
  678. struct buffer_head *bh;
  679. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  680. struct buffer_head *head;
  681. if (!page_has_buffers(page)) {
  682. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  683. (1 << BH_Dirty)|(1 << BH_Uptodate));
  684. }
  685. head = page_buffers(page);
  686. bh = head;
  687. do {
  688. if (buffer_dirty(bh))
  689. csum_tree_block(root, bh, 0);
  690. bh = bh->b_this_page;
  691. } while (bh != head);
  692. return block_write_full_page(page, btree_get_block, wbc);
  693. }
  694. #endif
  695. static struct address_space_operations btree_aops = {
  696. .readpage = btree_readpage,
  697. .writepage = btree_writepage,
  698. .writepages = btree_writepages,
  699. .releasepage = btree_releasepage,
  700. .invalidatepage = btree_invalidatepage,
  701. .sync_page = block_sync_page,
  702. };
  703. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
  704. u64 parent_transid)
  705. {
  706. struct extent_buffer *buf = NULL;
  707. struct inode *btree_inode = root->fs_info->btree_inode;
  708. int ret = 0;
  709. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  710. if (!buf)
  711. return 0;
  712. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  713. buf, 0, 0, btree_get_extent, 0);
  714. free_extent_buffer(buf);
  715. return ret;
  716. }
  717. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  718. u64 bytenr, u32 blocksize)
  719. {
  720. struct inode *btree_inode = root->fs_info->btree_inode;
  721. struct extent_buffer *eb;
  722. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  723. bytenr, blocksize, GFP_NOFS);
  724. return eb;
  725. }
  726. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  727. u64 bytenr, u32 blocksize)
  728. {
  729. struct inode *btree_inode = root->fs_info->btree_inode;
  730. struct extent_buffer *eb;
  731. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  732. bytenr, blocksize, NULL, GFP_NOFS);
  733. return eb;
  734. }
  735. int btrfs_write_tree_block(struct extent_buffer *buf)
  736. {
  737. return btrfs_fdatawrite_range(buf->first_page->mapping, buf->start,
  738. buf->start + buf->len - 1, WB_SYNC_ALL);
  739. }
  740. int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
  741. {
  742. return btrfs_wait_on_page_writeback_range(buf->first_page->mapping,
  743. buf->start, buf->start + buf->len - 1);
  744. }
  745. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  746. u32 blocksize, u64 parent_transid)
  747. {
  748. struct extent_buffer *buf = NULL;
  749. struct inode *btree_inode = root->fs_info->btree_inode;
  750. struct extent_io_tree *io_tree;
  751. int ret;
  752. io_tree = &BTRFS_I(btree_inode)->io_tree;
  753. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  754. if (!buf)
  755. return NULL;
  756. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  757. if (ret == 0)
  758. set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
  759. else
  760. WARN_ON(1);
  761. return buf;
  762. }
  763. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  764. struct extent_buffer *buf)
  765. {
  766. struct inode *btree_inode = root->fs_info->btree_inode;
  767. if (btrfs_header_generation(buf) ==
  768. root->fs_info->running_transaction->transid) {
  769. btrfs_assert_tree_locked(buf);
  770. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
  771. spin_lock(&root->fs_info->delalloc_lock);
  772. if (root->fs_info->dirty_metadata_bytes >= buf->len)
  773. root->fs_info->dirty_metadata_bytes -= buf->len;
  774. else
  775. WARN_ON(1);
  776. spin_unlock(&root->fs_info->delalloc_lock);
  777. }
  778. /* ugh, clear_extent_buffer_dirty needs to lock the page */
  779. btrfs_set_lock_blocking(buf);
  780. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  781. buf);
  782. }
  783. return 0;
  784. }
  785. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  786. u32 stripesize, struct btrfs_root *root,
  787. struct btrfs_fs_info *fs_info,
  788. u64 objectid)
  789. {
  790. root->node = NULL;
  791. root->commit_root = NULL;
  792. root->ref_tree = NULL;
  793. root->sectorsize = sectorsize;
  794. root->nodesize = nodesize;
  795. root->leafsize = leafsize;
  796. root->stripesize = stripesize;
  797. root->ref_cows = 0;
  798. root->track_dirty = 0;
  799. root->fs_info = fs_info;
  800. root->objectid = objectid;
  801. root->last_trans = 0;
  802. root->highest_inode = 0;
  803. root->last_inode_alloc = 0;
  804. root->name = NULL;
  805. root->in_sysfs = 0;
  806. INIT_LIST_HEAD(&root->dirty_list);
  807. INIT_LIST_HEAD(&root->orphan_list);
  808. INIT_LIST_HEAD(&root->dead_list);
  809. spin_lock_init(&root->node_lock);
  810. spin_lock_init(&root->list_lock);
  811. mutex_init(&root->objectid_mutex);
  812. mutex_init(&root->log_mutex);
  813. init_waitqueue_head(&root->log_writer_wait);
  814. init_waitqueue_head(&root->log_commit_wait[0]);
  815. init_waitqueue_head(&root->log_commit_wait[1]);
  816. atomic_set(&root->log_commit[0], 0);
  817. atomic_set(&root->log_commit[1], 0);
  818. atomic_set(&root->log_writers, 0);
  819. root->log_batch = 0;
  820. root->log_transid = 0;
  821. extent_io_tree_init(&root->dirty_log_pages,
  822. fs_info->btree_inode->i_mapping, GFP_NOFS);
  823. btrfs_leaf_ref_tree_init(&root->ref_tree_struct);
  824. root->ref_tree = &root->ref_tree_struct;
  825. memset(&root->root_key, 0, sizeof(root->root_key));
  826. memset(&root->root_item, 0, sizeof(root->root_item));
  827. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  828. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  829. root->defrag_trans_start = fs_info->generation;
  830. init_completion(&root->kobj_unregister);
  831. root->defrag_running = 0;
  832. root->defrag_level = 0;
  833. root->root_key.objectid = objectid;
  834. root->anon_super.s_root = NULL;
  835. root->anon_super.s_dev = 0;
  836. INIT_LIST_HEAD(&root->anon_super.s_list);
  837. INIT_LIST_HEAD(&root->anon_super.s_instances);
  838. init_rwsem(&root->anon_super.s_umount);
  839. return 0;
  840. }
  841. static int find_and_setup_root(struct btrfs_root *tree_root,
  842. struct btrfs_fs_info *fs_info,
  843. u64 objectid,
  844. struct btrfs_root *root)
  845. {
  846. int ret;
  847. u32 blocksize;
  848. u64 generation;
  849. __setup_root(tree_root->nodesize, tree_root->leafsize,
  850. tree_root->sectorsize, tree_root->stripesize,
  851. root, fs_info, objectid);
  852. ret = btrfs_find_last_root(tree_root, objectid,
  853. &root->root_item, &root->root_key);
  854. BUG_ON(ret);
  855. generation = btrfs_root_generation(&root->root_item);
  856. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  857. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  858. blocksize, generation);
  859. BUG_ON(!root->node);
  860. return 0;
  861. }
  862. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  863. struct btrfs_fs_info *fs_info)
  864. {
  865. struct extent_buffer *eb;
  866. struct btrfs_root *log_root_tree = fs_info->log_root_tree;
  867. u64 start = 0;
  868. u64 end = 0;
  869. int ret;
  870. if (!log_root_tree)
  871. return 0;
  872. while (1) {
  873. ret = find_first_extent_bit(&log_root_tree->dirty_log_pages,
  874. 0, &start, &end, EXTENT_DIRTY);
  875. if (ret)
  876. break;
  877. clear_extent_dirty(&log_root_tree->dirty_log_pages,
  878. start, end, GFP_NOFS);
  879. }
  880. eb = fs_info->log_root_tree->node;
  881. WARN_ON(btrfs_header_level(eb) != 0);
  882. WARN_ON(btrfs_header_nritems(eb) != 0);
  883. ret = btrfs_free_reserved_extent(fs_info->tree_root,
  884. eb->start, eb->len);
  885. BUG_ON(ret);
  886. free_extent_buffer(eb);
  887. kfree(fs_info->log_root_tree);
  888. fs_info->log_root_tree = NULL;
  889. return 0;
  890. }
  891. static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
  892. struct btrfs_fs_info *fs_info)
  893. {
  894. struct btrfs_root *root;
  895. struct btrfs_root *tree_root = fs_info->tree_root;
  896. struct extent_buffer *leaf;
  897. root = kzalloc(sizeof(*root), GFP_NOFS);
  898. if (!root)
  899. return ERR_PTR(-ENOMEM);
  900. __setup_root(tree_root->nodesize, tree_root->leafsize,
  901. tree_root->sectorsize, tree_root->stripesize,
  902. root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  903. root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
  904. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  905. root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
  906. /*
  907. * log trees do not get reference counted because they go away
  908. * before a real commit is actually done. They do store pointers
  909. * to file data extents, and those reference counts still get
  910. * updated (along with back refs to the log tree).
  911. */
  912. root->ref_cows = 0;
  913. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  914. 0, BTRFS_TREE_LOG_OBJECTID,
  915. trans->transid, 0, 0, 0);
  916. if (IS_ERR(leaf)) {
  917. kfree(root);
  918. return ERR_CAST(leaf);
  919. }
  920. root->node = leaf;
  921. btrfs_set_header_nritems(root->node, 0);
  922. btrfs_set_header_level(root->node, 0);
  923. btrfs_set_header_bytenr(root->node, root->node->start);
  924. btrfs_set_header_generation(root->node, trans->transid);
  925. btrfs_set_header_owner(root->node, BTRFS_TREE_LOG_OBJECTID);
  926. write_extent_buffer(root->node, root->fs_info->fsid,
  927. (unsigned long)btrfs_header_fsid(root->node),
  928. BTRFS_FSID_SIZE);
  929. btrfs_mark_buffer_dirty(root->node);
  930. btrfs_tree_unlock(root->node);
  931. return root;
  932. }
  933. int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
  934. struct btrfs_fs_info *fs_info)
  935. {
  936. struct btrfs_root *log_root;
  937. log_root = alloc_log_tree(trans, fs_info);
  938. if (IS_ERR(log_root))
  939. return PTR_ERR(log_root);
  940. WARN_ON(fs_info->log_root_tree);
  941. fs_info->log_root_tree = log_root;
  942. return 0;
  943. }
  944. int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
  945. struct btrfs_root *root)
  946. {
  947. struct btrfs_root *log_root;
  948. struct btrfs_inode_item *inode_item;
  949. log_root = alloc_log_tree(trans, root->fs_info);
  950. if (IS_ERR(log_root))
  951. return PTR_ERR(log_root);
  952. log_root->last_trans = trans->transid;
  953. log_root->root_key.offset = root->root_key.objectid;
  954. inode_item = &log_root->root_item.inode;
  955. inode_item->generation = cpu_to_le64(1);
  956. inode_item->size = cpu_to_le64(3);
  957. inode_item->nlink = cpu_to_le32(1);
  958. inode_item->nbytes = cpu_to_le64(root->leafsize);
  959. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  960. btrfs_set_root_bytenr(&log_root->root_item, log_root->node->start);
  961. btrfs_set_root_generation(&log_root->root_item, trans->transid);
  962. WARN_ON(root->log_root);
  963. root->log_root = log_root;
  964. root->log_transid = 0;
  965. return 0;
  966. }
  967. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
  968. struct btrfs_key *location)
  969. {
  970. struct btrfs_root *root;
  971. struct btrfs_fs_info *fs_info = tree_root->fs_info;
  972. struct btrfs_path *path;
  973. struct extent_buffer *l;
  974. u64 highest_inode;
  975. u64 generation;
  976. u32 blocksize;
  977. int ret = 0;
  978. root = kzalloc(sizeof(*root), GFP_NOFS);
  979. if (!root)
  980. return ERR_PTR(-ENOMEM);
  981. if (location->offset == (u64)-1) {
  982. ret = find_and_setup_root(tree_root, fs_info,
  983. location->objectid, root);
  984. if (ret) {
  985. kfree(root);
  986. return ERR_PTR(ret);
  987. }
  988. goto insert;
  989. }
  990. __setup_root(tree_root->nodesize, tree_root->leafsize,
  991. tree_root->sectorsize, tree_root->stripesize,
  992. root, fs_info, location->objectid);
  993. path = btrfs_alloc_path();
  994. BUG_ON(!path);
  995. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  996. if (ret != 0) {
  997. if (ret > 0)
  998. ret = -ENOENT;
  999. goto out;
  1000. }
  1001. l = path->nodes[0];
  1002. read_extent_buffer(l, &root->root_item,
  1003. btrfs_item_ptr_offset(l, path->slots[0]),
  1004. sizeof(root->root_item));
  1005. memcpy(&root->root_key, location, sizeof(*location));
  1006. ret = 0;
  1007. out:
  1008. btrfs_release_path(root, path);
  1009. btrfs_free_path(path);
  1010. if (ret) {
  1011. kfree(root);
  1012. return ERR_PTR(ret);
  1013. }
  1014. generation = btrfs_root_generation(&root->root_item);
  1015. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  1016. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  1017. blocksize, generation);
  1018. BUG_ON(!root->node);
  1019. insert:
  1020. if (location->objectid != BTRFS_TREE_LOG_OBJECTID) {
  1021. root->ref_cows = 1;
  1022. ret = btrfs_find_highest_inode(root, &highest_inode);
  1023. if (ret == 0) {
  1024. root->highest_inode = highest_inode;
  1025. root->last_inode_alloc = highest_inode;
  1026. }
  1027. }
  1028. return root;
  1029. }
  1030. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  1031. u64 root_objectid)
  1032. {
  1033. struct btrfs_root *root;
  1034. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  1035. return fs_info->tree_root;
  1036. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  1037. return fs_info->extent_root;
  1038. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  1039. (unsigned long)root_objectid);
  1040. return root;
  1041. }
  1042. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  1043. struct btrfs_key *location)
  1044. {
  1045. struct btrfs_root *root;
  1046. int ret;
  1047. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  1048. return fs_info->tree_root;
  1049. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  1050. return fs_info->extent_root;
  1051. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  1052. return fs_info->chunk_root;
  1053. if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
  1054. return fs_info->dev_root;
  1055. if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
  1056. return fs_info->csum_root;
  1057. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  1058. (unsigned long)location->objectid);
  1059. if (root)
  1060. return root;
  1061. root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location);
  1062. if (IS_ERR(root))
  1063. return root;
  1064. set_anon_super(&root->anon_super, NULL);
  1065. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  1066. (unsigned long)root->root_key.objectid,
  1067. root);
  1068. if (ret) {
  1069. free_extent_buffer(root->node);
  1070. kfree(root);
  1071. return ERR_PTR(ret);
  1072. }
  1073. if (!(fs_info->sb->s_flags & MS_RDONLY)) {
  1074. ret = btrfs_find_dead_roots(fs_info->tree_root,
  1075. root->root_key.objectid, root);
  1076. BUG_ON(ret);
  1077. btrfs_orphan_cleanup(root);
  1078. }
  1079. return root;
  1080. }
  1081. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  1082. struct btrfs_key *location,
  1083. const char *name, int namelen)
  1084. {
  1085. struct btrfs_root *root;
  1086. int ret;
  1087. root = btrfs_read_fs_root_no_name(fs_info, location);
  1088. if (!root)
  1089. return NULL;
  1090. if (root->in_sysfs)
  1091. return root;
  1092. ret = btrfs_set_root_name(root, name, namelen);
  1093. if (ret) {
  1094. free_extent_buffer(root->node);
  1095. kfree(root);
  1096. return ERR_PTR(ret);
  1097. }
  1098. #if 0
  1099. ret = btrfs_sysfs_add_root(root);
  1100. if (ret) {
  1101. free_extent_buffer(root->node);
  1102. kfree(root->name);
  1103. kfree(root);
  1104. return ERR_PTR(ret);
  1105. }
  1106. #endif
  1107. root->in_sysfs = 1;
  1108. return root;
  1109. }
  1110. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  1111. {
  1112. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  1113. int ret = 0;
  1114. struct btrfs_device *device;
  1115. struct backing_dev_info *bdi;
  1116. #if 0
  1117. if ((bdi_bits & (1 << BDI_write_congested)) &&
  1118. btrfs_congested_async(info, 0))
  1119. return 1;
  1120. #endif
  1121. list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
  1122. if (!device->bdev)
  1123. continue;
  1124. bdi = blk_get_backing_dev_info(device->bdev);
  1125. if (bdi && bdi_congested(bdi, bdi_bits)) {
  1126. ret = 1;
  1127. break;
  1128. }
  1129. }
  1130. return ret;
  1131. }
  1132. /*
  1133. * this unplugs every device on the box, and it is only used when page
  1134. * is null
  1135. */
  1136. static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  1137. {
  1138. struct btrfs_device *device;
  1139. struct btrfs_fs_info *info;
  1140. info = (struct btrfs_fs_info *)bdi->unplug_io_data;
  1141. list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
  1142. if (!device->bdev)
  1143. continue;
  1144. bdi = blk_get_backing_dev_info(device->bdev);
  1145. if (bdi->unplug_io_fn)
  1146. bdi->unplug_io_fn(bdi, page);
  1147. }
  1148. }
  1149. static void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  1150. {
  1151. struct inode *inode;
  1152. struct extent_map_tree *em_tree;
  1153. struct extent_map *em;
  1154. struct address_space *mapping;
  1155. u64 offset;
  1156. /* the generic O_DIRECT read code does this */
  1157. if (1 || !page) {
  1158. __unplug_io_fn(bdi, page);
  1159. return;
  1160. }
  1161. /*
  1162. * page->mapping may change at any time. Get a consistent copy
  1163. * and use that for everything below
  1164. */
  1165. smp_mb();
  1166. mapping = page->mapping;
  1167. if (!mapping)
  1168. return;
  1169. inode = mapping->host;
  1170. /*
  1171. * don't do the expensive searching for a small number of
  1172. * devices
  1173. */
  1174. if (BTRFS_I(inode)->root->fs_info->fs_devices->open_devices <= 2) {
  1175. __unplug_io_fn(bdi, page);
  1176. return;
  1177. }
  1178. offset = page_offset(page);
  1179. em_tree = &BTRFS_I(inode)->extent_tree;
  1180. spin_lock(&em_tree->lock);
  1181. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  1182. spin_unlock(&em_tree->lock);
  1183. if (!em) {
  1184. __unplug_io_fn(bdi, page);
  1185. return;
  1186. }
  1187. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  1188. free_extent_map(em);
  1189. __unplug_io_fn(bdi, page);
  1190. return;
  1191. }
  1192. offset = offset - em->start;
  1193. btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
  1194. em->block_start + offset, page);
  1195. free_extent_map(em);
  1196. }
  1197. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  1198. {
  1199. bdi_init(bdi);
  1200. bdi->ra_pages = default_backing_dev_info.ra_pages;
  1201. bdi->state = 0;
  1202. bdi->capabilities = default_backing_dev_info.capabilities;
  1203. bdi->unplug_io_fn = btrfs_unplug_io_fn;
  1204. bdi->unplug_io_data = info;
  1205. bdi->congested_fn = btrfs_congested_fn;
  1206. bdi->congested_data = info;
  1207. return 0;
  1208. }
  1209. static int bio_ready_for_csum(struct bio *bio)
  1210. {
  1211. u64 length = 0;
  1212. u64 buf_len = 0;
  1213. u64 start = 0;
  1214. struct page *page;
  1215. struct extent_io_tree *io_tree = NULL;
  1216. struct btrfs_fs_info *info = NULL;
  1217. struct bio_vec *bvec;
  1218. int i;
  1219. int ret;
  1220. bio_for_each_segment(bvec, bio, i) {
  1221. page = bvec->bv_page;
  1222. if (page->private == EXTENT_PAGE_PRIVATE) {
  1223. length += bvec->bv_len;
  1224. continue;
  1225. }
  1226. if (!page->private) {
  1227. length += bvec->bv_len;
  1228. continue;
  1229. }
  1230. length = bvec->bv_len;
  1231. buf_len = page->private >> 2;
  1232. start = page_offset(page) + bvec->bv_offset;
  1233. io_tree = &BTRFS_I(page->mapping->host)->io_tree;
  1234. info = BTRFS_I(page->mapping->host)->root->fs_info;
  1235. }
  1236. /* are we fully contained in this bio? */
  1237. if (buf_len <= length)
  1238. return 1;
  1239. ret = extent_range_uptodate(io_tree, start + length,
  1240. start + buf_len - 1);
  1241. if (ret == 1)
  1242. return ret;
  1243. return ret;
  1244. }
  1245. /*
  1246. * called by the kthread helper functions to finally call the bio end_io
  1247. * functions. This is where read checksum verification actually happens
  1248. */
  1249. static void end_workqueue_fn(struct btrfs_work *work)
  1250. {
  1251. struct bio *bio;
  1252. struct end_io_wq *end_io_wq;
  1253. struct btrfs_fs_info *fs_info;
  1254. int error;
  1255. end_io_wq = container_of(work, struct end_io_wq, work);
  1256. bio = end_io_wq->bio;
  1257. fs_info = end_io_wq->info;
  1258. /* metadata bio reads are special because the whole tree block must
  1259. * be checksummed at once. This makes sure the entire block is in
  1260. * ram and up to date before trying to verify things. For
  1261. * blocksize <= pagesize, it is basically a noop
  1262. */
  1263. if (!(bio->bi_rw & (1 << BIO_RW)) && end_io_wq->metadata &&
  1264. !bio_ready_for_csum(bio)) {
  1265. btrfs_queue_worker(&fs_info->endio_meta_workers,
  1266. &end_io_wq->work);
  1267. return;
  1268. }
  1269. error = end_io_wq->error;
  1270. bio->bi_private = end_io_wq->private;
  1271. bio->bi_end_io = end_io_wq->end_io;
  1272. kfree(end_io_wq);
  1273. bio_endio(bio, error);
  1274. }
  1275. static int cleaner_kthread(void *arg)
  1276. {
  1277. struct btrfs_root *root = arg;
  1278. do {
  1279. smp_mb();
  1280. if (root->fs_info->closing)
  1281. break;
  1282. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1283. mutex_lock(&root->fs_info->cleaner_mutex);
  1284. btrfs_clean_old_snapshots(root);
  1285. mutex_unlock(&root->fs_info->cleaner_mutex);
  1286. if (freezing(current)) {
  1287. refrigerator();
  1288. } else {
  1289. smp_mb();
  1290. if (root->fs_info->closing)
  1291. break;
  1292. set_current_state(TASK_INTERRUPTIBLE);
  1293. schedule();
  1294. __set_current_state(TASK_RUNNING);
  1295. }
  1296. } while (!kthread_should_stop());
  1297. return 0;
  1298. }
  1299. static int transaction_kthread(void *arg)
  1300. {
  1301. struct btrfs_root *root = arg;
  1302. struct btrfs_trans_handle *trans;
  1303. struct btrfs_transaction *cur;
  1304. unsigned long now;
  1305. unsigned long delay;
  1306. int ret;
  1307. do {
  1308. smp_mb();
  1309. if (root->fs_info->closing)
  1310. break;
  1311. delay = HZ * 30;
  1312. vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
  1313. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  1314. mutex_lock(&root->fs_info->trans_mutex);
  1315. cur = root->fs_info->running_transaction;
  1316. if (!cur) {
  1317. mutex_unlock(&root->fs_info->trans_mutex);
  1318. goto sleep;
  1319. }
  1320. now = get_seconds();
  1321. if (now < cur->start_time || now - cur->start_time < 30) {
  1322. mutex_unlock(&root->fs_info->trans_mutex);
  1323. delay = HZ * 5;
  1324. goto sleep;
  1325. }
  1326. mutex_unlock(&root->fs_info->trans_mutex);
  1327. trans = btrfs_start_transaction(root, 1);
  1328. ret = btrfs_commit_transaction(trans, root);
  1329. sleep:
  1330. wake_up_process(root->fs_info->cleaner_kthread);
  1331. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  1332. if (freezing(current)) {
  1333. refrigerator();
  1334. } else {
  1335. if (root->fs_info->closing)
  1336. break;
  1337. set_current_state(TASK_INTERRUPTIBLE);
  1338. schedule_timeout(delay);
  1339. __set_current_state(TASK_RUNNING);
  1340. }
  1341. } while (!kthread_should_stop());
  1342. return 0;
  1343. }
  1344. struct btrfs_root *open_ctree(struct super_block *sb,
  1345. struct btrfs_fs_devices *fs_devices,
  1346. char *options)
  1347. {
  1348. u32 sectorsize;
  1349. u32 nodesize;
  1350. u32 leafsize;
  1351. u32 blocksize;
  1352. u32 stripesize;
  1353. u64 generation;
  1354. u64 features;
  1355. struct btrfs_key location;
  1356. struct buffer_head *bh;
  1357. struct btrfs_root *extent_root = kzalloc(sizeof(struct btrfs_root),
  1358. GFP_NOFS);
  1359. struct btrfs_root *csum_root = kzalloc(sizeof(struct btrfs_root),
  1360. GFP_NOFS);
  1361. struct btrfs_root *tree_root = kzalloc(sizeof(struct btrfs_root),
  1362. GFP_NOFS);
  1363. struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
  1364. GFP_NOFS);
  1365. struct btrfs_root *chunk_root = kzalloc(sizeof(struct btrfs_root),
  1366. GFP_NOFS);
  1367. struct btrfs_root *dev_root = kzalloc(sizeof(struct btrfs_root),
  1368. GFP_NOFS);
  1369. struct btrfs_root *log_tree_root;
  1370. int ret;
  1371. int err = -EINVAL;
  1372. struct btrfs_super_block *disk_super;
  1373. if (!extent_root || !tree_root || !fs_info ||
  1374. !chunk_root || !dev_root || !csum_root) {
  1375. err = -ENOMEM;
  1376. goto fail;
  1377. }
  1378. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  1379. INIT_LIST_HEAD(&fs_info->trans_list);
  1380. INIT_LIST_HEAD(&fs_info->dead_roots);
  1381. INIT_LIST_HEAD(&fs_info->hashers);
  1382. INIT_LIST_HEAD(&fs_info->delalloc_inodes);
  1383. spin_lock_init(&fs_info->delalloc_lock);
  1384. spin_lock_init(&fs_info->new_trans_lock);
  1385. spin_lock_init(&fs_info->ref_cache_lock);
  1386. init_completion(&fs_info->kobj_unregister);
  1387. fs_info->tree_root = tree_root;
  1388. fs_info->extent_root = extent_root;
  1389. fs_info->csum_root = csum_root;
  1390. fs_info->chunk_root = chunk_root;
  1391. fs_info->dev_root = dev_root;
  1392. fs_info->fs_devices = fs_devices;
  1393. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  1394. INIT_LIST_HEAD(&fs_info->space_info);
  1395. btrfs_mapping_init(&fs_info->mapping_tree);
  1396. atomic_set(&fs_info->nr_async_submits, 0);
  1397. atomic_set(&fs_info->async_delalloc_pages, 0);
  1398. atomic_set(&fs_info->async_submit_draining, 0);
  1399. atomic_set(&fs_info->nr_async_bios, 0);
  1400. atomic_set(&fs_info->throttles, 0);
  1401. atomic_set(&fs_info->throttle_gen, 0);
  1402. fs_info->sb = sb;
  1403. fs_info->max_extent = (u64)-1;
  1404. fs_info->max_inline = 8192 * 1024;
  1405. setup_bdi(fs_info, &fs_info->bdi);
  1406. fs_info->btree_inode = new_inode(sb);
  1407. fs_info->btree_inode->i_ino = 1;
  1408. fs_info->btree_inode->i_nlink = 1;
  1409. fs_info->thread_pool_size = min_t(unsigned long,
  1410. num_online_cpus() + 2, 8);
  1411. INIT_LIST_HEAD(&fs_info->ordered_extents);
  1412. spin_lock_init(&fs_info->ordered_extent_lock);
  1413. sb->s_blocksize = 4096;
  1414. sb->s_blocksize_bits = blksize_bits(4096);
  1415. /*
  1416. * we set the i_size on the btree inode to the max possible int.
  1417. * the real end of the address space is determined by all of
  1418. * the devices in the system
  1419. */
  1420. fs_info->btree_inode->i_size = OFFSET_MAX;
  1421. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  1422. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  1423. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  1424. fs_info->btree_inode->i_mapping,
  1425. GFP_NOFS);
  1426. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  1427. GFP_NOFS);
  1428. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  1429. spin_lock_init(&fs_info->block_group_cache_lock);
  1430. fs_info->block_group_cache_tree.rb_node = NULL;
  1431. extent_io_tree_init(&fs_info->pinned_extents,
  1432. fs_info->btree_inode->i_mapping, GFP_NOFS);
  1433. fs_info->do_barriers = 1;
  1434. INIT_LIST_HEAD(&fs_info->dead_reloc_roots);
  1435. btrfs_leaf_ref_tree_init(&fs_info->reloc_ref_tree);
  1436. btrfs_leaf_ref_tree_init(&fs_info->shared_ref_tree);
  1437. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  1438. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  1439. sizeof(struct btrfs_key));
  1440. insert_inode_hash(fs_info->btree_inode);
  1441. mutex_init(&fs_info->trans_mutex);
  1442. mutex_init(&fs_info->tree_log_mutex);
  1443. mutex_init(&fs_info->drop_mutex);
  1444. mutex_init(&fs_info->pinned_mutex);
  1445. mutex_init(&fs_info->chunk_mutex);
  1446. mutex_init(&fs_info->transaction_kthread_mutex);
  1447. mutex_init(&fs_info->cleaner_mutex);
  1448. mutex_init(&fs_info->volume_mutex);
  1449. mutex_init(&fs_info->tree_reloc_mutex);
  1450. init_waitqueue_head(&fs_info->transaction_throttle);
  1451. init_waitqueue_head(&fs_info->transaction_wait);
  1452. init_waitqueue_head(&fs_info->async_submit_wait);
  1453. __setup_root(4096, 4096, 4096, 4096, tree_root,
  1454. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  1455. bh = btrfs_read_dev_super(fs_devices->latest_bdev);
  1456. if (!bh)
  1457. goto fail_iput;
  1458. memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
  1459. memcpy(&fs_info->super_for_commit, &fs_info->super_copy,
  1460. sizeof(fs_info->super_for_commit));
  1461. brelse(bh);
  1462. memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
  1463. disk_super = &fs_info->super_copy;
  1464. if (!btrfs_super_root(disk_super))
  1465. goto fail_iput;
  1466. ret = btrfs_parse_options(tree_root, options);
  1467. if (ret) {
  1468. err = ret;
  1469. goto fail_iput;
  1470. }
  1471. features = btrfs_super_incompat_flags(disk_super) &
  1472. ~BTRFS_FEATURE_INCOMPAT_SUPP;
  1473. if (features) {
  1474. printk(KERN_ERR "BTRFS: couldn't mount because of "
  1475. "unsupported optional features (%Lx).\n",
  1476. features);
  1477. err = -EINVAL;
  1478. goto fail_iput;
  1479. }
  1480. features = btrfs_super_compat_ro_flags(disk_super) &
  1481. ~BTRFS_FEATURE_COMPAT_RO_SUPP;
  1482. if (!(sb->s_flags & MS_RDONLY) && features) {
  1483. printk(KERN_ERR "BTRFS: couldn't mount RDWR because of "
  1484. "unsupported option features (%Lx).\n",
  1485. features);
  1486. err = -EINVAL;
  1487. goto fail_iput;
  1488. }
  1489. /*
  1490. * we need to start all the end_io workers up front because the
  1491. * queue work function gets called at interrupt time, and so it
  1492. * cannot dynamically grow.
  1493. */
  1494. btrfs_init_workers(&fs_info->workers, "worker",
  1495. fs_info->thread_pool_size);
  1496. btrfs_init_workers(&fs_info->delalloc_workers, "delalloc",
  1497. fs_info->thread_pool_size);
  1498. btrfs_init_workers(&fs_info->submit_workers, "submit",
  1499. min_t(u64, fs_devices->num_devices,
  1500. fs_info->thread_pool_size));
  1501. /* a higher idle thresh on the submit workers makes it much more
  1502. * likely that bios will be send down in a sane order to the
  1503. * devices
  1504. */
  1505. fs_info->submit_workers.idle_thresh = 64;
  1506. fs_info->workers.idle_thresh = 16;
  1507. fs_info->workers.ordered = 1;
  1508. fs_info->delalloc_workers.idle_thresh = 2;
  1509. fs_info->delalloc_workers.ordered = 1;
  1510. btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1);
  1511. btrfs_init_workers(&fs_info->endio_workers, "endio",
  1512. fs_info->thread_pool_size);
  1513. btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta",
  1514. fs_info->thread_pool_size);
  1515. btrfs_init_workers(&fs_info->endio_meta_write_workers,
  1516. "endio-meta-write", fs_info->thread_pool_size);
  1517. btrfs_init_workers(&fs_info->endio_write_workers, "endio-write",
  1518. fs_info->thread_pool_size);
  1519. /*
  1520. * endios are largely parallel and should have a very
  1521. * low idle thresh
  1522. */
  1523. fs_info->endio_workers.idle_thresh = 4;
  1524. fs_info->endio_meta_workers.idle_thresh = 4;
  1525. fs_info->endio_write_workers.idle_thresh = 64;
  1526. fs_info->endio_meta_write_workers.idle_thresh = 64;
  1527. btrfs_start_workers(&fs_info->workers, 1);
  1528. btrfs_start_workers(&fs_info->submit_workers, 1);
  1529. btrfs_start_workers(&fs_info->delalloc_workers, 1);
  1530. btrfs_start_workers(&fs_info->fixup_workers, 1);
  1531. btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
  1532. btrfs_start_workers(&fs_info->endio_meta_workers,
  1533. fs_info->thread_pool_size);
  1534. btrfs_start_workers(&fs_info->endio_meta_write_workers,
  1535. fs_info->thread_pool_size);
  1536. btrfs_start_workers(&fs_info->endio_write_workers,
  1537. fs_info->thread_pool_size);
  1538. fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
  1539. fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
  1540. 4 * 1024 * 1024 / PAGE_CACHE_SIZE);
  1541. nodesize = btrfs_super_nodesize(disk_super);
  1542. leafsize = btrfs_super_leafsize(disk_super);
  1543. sectorsize = btrfs_super_sectorsize(disk_super);
  1544. stripesize = btrfs_super_stripesize(disk_super);
  1545. tree_root->nodesize = nodesize;
  1546. tree_root->leafsize = leafsize;
  1547. tree_root->sectorsize = sectorsize;
  1548. tree_root->stripesize = stripesize;
  1549. sb->s_blocksize = sectorsize;
  1550. sb->s_blocksize_bits = blksize_bits(sectorsize);
  1551. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  1552. sizeof(disk_super->magic))) {
  1553. printk(KERN_INFO "btrfs: valid FS not found on %s\n", sb->s_id);
  1554. goto fail_sb_buffer;
  1555. }
  1556. mutex_lock(&fs_info->chunk_mutex);
  1557. ret = btrfs_read_sys_array(tree_root);
  1558. mutex_unlock(&fs_info->chunk_mutex);
  1559. if (ret) {
  1560. printk(KERN_WARNING "btrfs: failed to read the system "
  1561. "array on %s\n", sb->s_id);
  1562. goto fail_sys_array;
  1563. }
  1564. blocksize = btrfs_level_size(tree_root,
  1565. btrfs_super_chunk_root_level(disk_super));
  1566. generation = btrfs_super_chunk_root_generation(disk_super);
  1567. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1568. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  1569. chunk_root->node = read_tree_block(chunk_root,
  1570. btrfs_super_chunk_root(disk_super),
  1571. blocksize, generation);
  1572. BUG_ON(!chunk_root->node);
  1573. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  1574. (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
  1575. BTRFS_UUID_SIZE);
  1576. mutex_lock(&fs_info->chunk_mutex);
  1577. ret = btrfs_read_chunk_tree(chunk_root);
  1578. mutex_unlock(&fs_info->chunk_mutex);
  1579. if (ret) {
  1580. printk(KERN_WARNING "btrfs: failed to read chunk tree on %s\n",
  1581. sb->s_id);
  1582. goto fail_chunk_root;
  1583. }
  1584. btrfs_close_extra_devices(fs_devices);
  1585. blocksize = btrfs_level_size(tree_root,
  1586. btrfs_super_root_level(disk_super));
  1587. generation = btrfs_super_generation(disk_super);
  1588. tree_root->node = read_tree_block(tree_root,
  1589. btrfs_super_root(disk_super),
  1590. blocksize, generation);
  1591. if (!tree_root->node)
  1592. goto fail_chunk_root;
  1593. ret = find_and_setup_root(tree_root, fs_info,
  1594. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  1595. if (ret)
  1596. goto fail_tree_root;
  1597. extent_root->track_dirty = 1;
  1598. ret = find_and_setup_root(tree_root, fs_info,
  1599. BTRFS_DEV_TREE_OBJECTID, dev_root);
  1600. dev_root->track_dirty = 1;
  1601. if (ret)
  1602. goto fail_extent_root;
  1603. ret = find_and_setup_root(tree_root, fs_info,
  1604. BTRFS_CSUM_TREE_OBJECTID, csum_root);
  1605. if (ret)
  1606. goto fail_extent_root;
  1607. csum_root->track_dirty = 1;
  1608. btrfs_read_block_groups(extent_root);
  1609. fs_info->generation = generation;
  1610. fs_info->last_trans_committed = generation;
  1611. fs_info->data_alloc_profile = (u64)-1;
  1612. fs_info->metadata_alloc_profile = (u64)-1;
  1613. fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
  1614. fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
  1615. "btrfs-cleaner");
  1616. if (IS_ERR(fs_info->cleaner_kthread))
  1617. goto fail_csum_root;
  1618. fs_info->transaction_kthread = kthread_run(transaction_kthread,
  1619. tree_root,
  1620. "btrfs-transaction");
  1621. if (IS_ERR(fs_info->transaction_kthread))
  1622. goto fail_cleaner;
  1623. if (btrfs_super_log_root(disk_super) != 0) {
  1624. u64 bytenr = btrfs_super_log_root(disk_super);
  1625. if (fs_devices->rw_devices == 0) {
  1626. printk(KERN_WARNING "Btrfs log replay required "
  1627. "on RO media\n");
  1628. err = -EIO;
  1629. goto fail_trans_kthread;
  1630. }
  1631. blocksize =
  1632. btrfs_level_size(tree_root,
  1633. btrfs_super_log_root_level(disk_super));
  1634. log_tree_root = kzalloc(sizeof(struct btrfs_root),
  1635. GFP_NOFS);
  1636. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  1637. log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  1638. log_tree_root->node = read_tree_block(tree_root, bytenr,
  1639. blocksize,
  1640. generation + 1);
  1641. ret = btrfs_recover_log_trees(log_tree_root);
  1642. BUG_ON(ret);
  1643. if (sb->s_flags & MS_RDONLY) {
  1644. ret = btrfs_commit_super(tree_root);
  1645. BUG_ON(ret);
  1646. }
  1647. }
  1648. if (!(sb->s_flags & MS_RDONLY)) {
  1649. ret = btrfs_cleanup_reloc_trees(tree_root);
  1650. BUG_ON(ret);
  1651. }
  1652. location.objectid = BTRFS_FS_TREE_OBJECTID;
  1653. location.type = BTRFS_ROOT_ITEM_KEY;
  1654. location.offset = (u64)-1;
  1655. fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
  1656. if (!fs_info->fs_root)
  1657. goto fail_trans_kthread;
  1658. return tree_root;
  1659. fail_trans_kthread:
  1660. kthread_stop(fs_info->transaction_kthread);
  1661. fail_cleaner:
  1662. kthread_stop(fs_info->cleaner_kthread);
  1663. /*
  1664. * make sure we're done with the btree inode before we stop our
  1665. * kthreads
  1666. */
  1667. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  1668. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  1669. fail_csum_root:
  1670. free_extent_buffer(csum_root->node);
  1671. fail_extent_root:
  1672. free_extent_buffer(extent_root->node);
  1673. fail_tree_root:
  1674. free_extent_buffer(tree_root->node);
  1675. fail_chunk_root:
  1676. free_extent_buffer(chunk_root->node);
  1677. fail_sys_array:
  1678. free_extent_buffer(dev_root->node);
  1679. fail_sb_buffer:
  1680. btrfs_stop_workers(&fs_info->fixup_workers);
  1681. btrfs_stop_workers(&fs_info->delalloc_workers);
  1682. btrfs_stop_workers(&fs_info->workers);
  1683. btrfs_stop_workers(&fs_info->endio_workers);
  1684. btrfs_stop_workers(&fs_info->endio_meta_workers);
  1685. btrfs_stop_workers(&fs_info->endio_meta_write_workers);
  1686. btrfs_stop_workers(&fs_info->endio_write_workers);
  1687. btrfs_stop_workers(&fs_info->submit_workers);
  1688. fail_iput:
  1689. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  1690. iput(fs_info->btree_inode);
  1691. btrfs_close_devices(fs_info->fs_devices);
  1692. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1693. bdi_destroy(&fs_info->bdi);
  1694. fail:
  1695. kfree(extent_root);
  1696. kfree(tree_root);
  1697. kfree(fs_info);
  1698. kfree(chunk_root);
  1699. kfree(dev_root);
  1700. kfree(csum_root);
  1701. return ERR_PTR(err);
  1702. }
  1703. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  1704. {
  1705. char b[BDEVNAME_SIZE];
  1706. if (uptodate) {
  1707. set_buffer_uptodate(bh);
  1708. } else {
  1709. if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
  1710. printk(KERN_WARNING "lost page write due to "
  1711. "I/O error on %s\n",
  1712. bdevname(bh->b_bdev, b));
  1713. }
  1714. /* note, we dont' set_buffer_write_io_error because we have
  1715. * our own ways of dealing with the IO errors
  1716. */
  1717. clear_buffer_uptodate(bh);
  1718. }
  1719. unlock_buffer(bh);
  1720. put_bh(bh);
  1721. }
  1722. struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
  1723. {
  1724. struct buffer_head *bh;
  1725. struct buffer_head *latest = NULL;
  1726. struct btrfs_super_block *super;
  1727. int i;
  1728. u64 transid = 0;
  1729. u64 bytenr;
  1730. /* we would like to check all the supers, but that would make
  1731. * a btrfs mount succeed after a mkfs from a different FS.
  1732. * So, we need to add a special mount option to scan for
  1733. * later supers, using BTRFS_SUPER_MIRROR_MAX instead
  1734. */
  1735. for (i = 0; i < 1; i++) {
  1736. bytenr = btrfs_sb_offset(i);
  1737. if (bytenr + 4096 >= i_size_read(bdev->bd_inode))
  1738. break;
  1739. bh = __bread(bdev, bytenr / 4096, 4096);
  1740. if (!bh)
  1741. continue;
  1742. super = (struct btrfs_super_block *)bh->b_data;
  1743. if (btrfs_super_bytenr(super) != bytenr ||
  1744. strncmp((char *)(&super->magic), BTRFS_MAGIC,
  1745. sizeof(super->magic))) {
  1746. brelse(bh);
  1747. continue;
  1748. }
  1749. if (!latest || btrfs_super_generation(super) > transid) {
  1750. brelse(latest);
  1751. latest = bh;
  1752. transid = btrfs_super_generation(super);
  1753. } else {
  1754. brelse(bh);
  1755. }
  1756. }
  1757. return latest;
  1758. }
  1759. static int write_dev_supers(struct btrfs_device *device,
  1760. struct btrfs_super_block *sb,
  1761. int do_barriers, int wait, int max_mirrors)
  1762. {
  1763. struct buffer_head *bh;
  1764. int i;
  1765. int ret;
  1766. int errors = 0;
  1767. u32 crc;
  1768. u64 bytenr;
  1769. int last_barrier = 0;
  1770. if (max_mirrors == 0)
  1771. max_mirrors = BTRFS_SUPER_MIRROR_MAX;
  1772. /* make sure only the last submit_bh does a barrier */
  1773. if (do_barriers) {
  1774. for (i = 0; i < max_mirrors; i++) {
  1775. bytenr = btrfs_sb_offset(i);
  1776. if (bytenr + BTRFS_SUPER_INFO_SIZE >=
  1777. device->total_bytes)
  1778. break;
  1779. last_barrier = i;
  1780. }
  1781. }
  1782. for (i = 0; i < max_mirrors; i++) {
  1783. bytenr = btrfs_sb_offset(i);
  1784. if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
  1785. break;
  1786. if (wait) {
  1787. bh = __find_get_block(device->bdev, bytenr / 4096,
  1788. BTRFS_SUPER_INFO_SIZE);
  1789. BUG_ON(!bh);
  1790. brelse(bh);
  1791. wait_on_buffer(bh);
  1792. if (buffer_uptodate(bh)) {
  1793. brelse(bh);
  1794. continue;
  1795. }
  1796. } else {
  1797. btrfs_set_super_bytenr(sb, bytenr);
  1798. crc = ~(u32)0;
  1799. crc = btrfs_csum_data(NULL, (char *)sb +
  1800. BTRFS_CSUM_SIZE, crc,
  1801. BTRFS_SUPER_INFO_SIZE -
  1802. BTRFS_CSUM_SIZE);
  1803. btrfs_csum_final(crc, sb->csum);
  1804. bh = __getblk(device->bdev, bytenr / 4096,
  1805. BTRFS_SUPER_INFO_SIZE);
  1806. memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
  1807. set_buffer_uptodate(bh);
  1808. get_bh(bh);
  1809. lock_buffer(bh);
  1810. bh->b_end_io = btrfs_end_buffer_write_sync;
  1811. }
  1812. if (i == last_barrier && do_barriers && device->barriers) {
  1813. ret = submit_bh(WRITE_BARRIER, bh);
  1814. if (ret == -EOPNOTSUPP) {
  1815. printk("btrfs: disabling barriers on dev %s\n",
  1816. device->name);
  1817. set_buffer_uptodate(bh);
  1818. device->barriers = 0;
  1819. get_bh(bh);
  1820. lock_buffer(bh);
  1821. ret = submit_bh(WRITE, bh);
  1822. }
  1823. } else {
  1824. ret = submit_bh(WRITE, bh);
  1825. }
  1826. if (!ret && wait) {
  1827. wait_on_buffer(bh);
  1828. if (!buffer_uptodate(bh))
  1829. errors++;
  1830. } else if (ret) {
  1831. errors++;
  1832. }
  1833. if (wait)
  1834. brelse(bh);
  1835. }
  1836. return errors < i ? 0 : -1;
  1837. }
  1838. int write_all_supers(struct btrfs_root *root, int max_mirrors)
  1839. {
  1840. struct list_head *head = &root->fs_info->fs_devices->devices;
  1841. struct btrfs_device *dev;
  1842. struct btrfs_super_block *sb;
  1843. struct btrfs_dev_item *dev_item;
  1844. int ret;
  1845. int do_barriers;
  1846. int max_errors;
  1847. int total_errors = 0;
  1848. u64 flags;
  1849. max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
  1850. do_barriers = !btrfs_test_opt(root, NOBARRIER);
  1851. sb = &root->fs_info->super_for_commit;
  1852. dev_item = &sb->dev_item;
  1853. list_for_each_entry(dev, head, dev_list) {
  1854. if (!dev->bdev) {
  1855. total_errors++;
  1856. continue;
  1857. }
  1858. if (!dev->in_fs_metadata || !dev->writeable)
  1859. continue;
  1860. btrfs_set_stack_device_generation(dev_item, 0);
  1861. btrfs_set_stack_device_type(dev_item, dev->type);
  1862. btrfs_set_stack_device_id(dev_item, dev->devid);
  1863. btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
  1864. btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
  1865. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  1866. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  1867. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  1868. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  1869. memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
  1870. flags = btrfs_super_flags(sb);
  1871. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  1872. ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors);
  1873. if (ret)
  1874. total_errors++;
  1875. }
  1876. if (total_errors > max_errors) {
  1877. printk(KERN_ERR "btrfs: %d errors while writing supers\n",
  1878. total_errors);
  1879. BUG();
  1880. }
  1881. total_errors = 0;
  1882. list_for_each_entry(dev, head, dev_list) {
  1883. if (!dev->bdev)
  1884. continue;
  1885. if (!dev->in_fs_metadata || !dev->writeable)
  1886. continue;
  1887. ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors);
  1888. if (ret)
  1889. total_errors++;
  1890. }
  1891. if (total_errors > max_errors) {
  1892. printk(KERN_ERR "btrfs: %d errors while writing supers\n",
  1893. total_errors);
  1894. BUG();
  1895. }
  1896. return 0;
  1897. }
  1898. int write_ctree_super(struct btrfs_trans_handle *trans,
  1899. struct btrfs_root *root, int max_mirrors)
  1900. {
  1901. int ret;
  1902. ret = write_all_supers(root, max_mirrors);
  1903. return ret;
  1904. }
  1905. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  1906. {
  1907. radix_tree_delete(&fs_info->fs_roots_radix,
  1908. (unsigned long)root->root_key.objectid);
  1909. if (root->anon_super.s_dev) {
  1910. down_write(&root->anon_super.s_umount);
  1911. kill_anon_super(&root->anon_super);
  1912. }
  1913. if (root->node)
  1914. free_extent_buffer(root->node);
  1915. if (root->commit_root)
  1916. free_extent_buffer(root->commit_root);
  1917. kfree(root->name);
  1918. kfree(root);
  1919. return 0;
  1920. }
  1921. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  1922. {
  1923. int ret;
  1924. struct btrfs_root *gang[8];
  1925. int i;
  1926. while (1) {
  1927. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1928. (void **)gang, 0,
  1929. ARRAY_SIZE(gang));
  1930. if (!ret)
  1931. break;
  1932. for (i = 0; i < ret; i++)
  1933. btrfs_free_fs_root(fs_info, gang[i]);
  1934. }
  1935. return 0;
  1936. }
  1937. int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
  1938. {
  1939. u64 root_objectid = 0;
  1940. struct btrfs_root *gang[8];
  1941. int i;
  1942. int ret;
  1943. while (1) {
  1944. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1945. (void **)gang, root_objectid,
  1946. ARRAY_SIZE(gang));
  1947. if (!ret)
  1948. break;
  1949. for (i = 0; i < ret; i++) {
  1950. root_objectid = gang[i]->root_key.objectid;
  1951. ret = btrfs_find_dead_roots(fs_info->tree_root,
  1952. root_objectid, gang[i]);
  1953. BUG_ON(ret);
  1954. btrfs_orphan_cleanup(gang[i]);
  1955. }
  1956. root_objectid++;
  1957. }
  1958. return 0;
  1959. }
  1960. int btrfs_commit_super(struct btrfs_root *root)
  1961. {
  1962. struct btrfs_trans_handle *trans;
  1963. int ret;
  1964. mutex_lock(&root->fs_info->cleaner_mutex);
  1965. btrfs_clean_old_snapshots(root);
  1966. mutex_unlock(&root->fs_info->cleaner_mutex);
  1967. trans = btrfs_start_transaction(root, 1);
  1968. ret = btrfs_commit_transaction(trans, root);
  1969. BUG_ON(ret);
  1970. /* run commit again to drop the original snapshot */
  1971. trans = btrfs_start_transaction(root, 1);
  1972. btrfs_commit_transaction(trans, root);
  1973. ret = btrfs_write_and_wait_transaction(NULL, root);
  1974. BUG_ON(ret);
  1975. ret = write_ctree_super(NULL, root, 0);
  1976. return ret;
  1977. }
  1978. int close_ctree(struct btrfs_root *root)
  1979. {
  1980. struct btrfs_fs_info *fs_info = root->fs_info;
  1981. int ret;
  1982. fs_info->closing = 1;
  1983. smp_mb();
  1984. kthread_stop(root->fs_info->transaction_kthread);
  1985. kthread_stop(root->fs_info->cleaner_kthread);
  1986. if (!(fs_info->sb->s_flags & MS_RDONLY)) {
  1987. ret = btrfs_commit_super(root);
  1988. if (ret)
  1989. printk(KERN_ERR "btrfs: commit super ret %d\n", ret);
  1990. }
  1991. if (fs_info->delalloc_bytes) {
  1992. printk(KERN_INFO "btrfs: at unmount delalloc count %llu\n",
  1993. fs_info->delalloc_bytes);
  1994. }
  1995. if (fs_info->total_ref_cache_size) {
  1996. printk(KERN_INFO "btrfs: at umount reference cache size %llu\n",
  1997. (unsigned long long)fs_info->total_ref_cache_size);
  1998. }
  1999. if (fs_info->extent_root->node)
  2000. free_extent_buffer(fs_info->extent_root->node);
  2001. if (fs_info->tree_root->node)
  2002. free_extent_buffer(fs_info->tree_root->node);
  2003. if (root->fs_info->chunk_root->node)
  2004. free_extent_buffer(root->fs_info->chunk_root->node);
  2005. if (root->fs_info->dev_root->node)
  2006. free_extent_buffer(root->fs_info->dev_root->node);
  2007. if (root->fs_info->csum_root->node)
  2008. free_extent_buffer(root->fs_info->csum_root->node);
  2009. btrfs_free_block_groups(root->fs_info);
  2010. del_fs_roots(fs_info);
  2011. iput(fs_info->btree_inode);
  2012. btrfs_stop_workers(&fs_info->fixup_workers);
  2013. btrfs_stop_workers(&fs_info->delalloc_workers);
  2014. btrfs_stop_workers(&fs_info->workers);
  2015. btrfs_stop_workers(&fs_info->endio_workers);
  2016. btrfs_stop_workers(&fs_info->endio_meta_workers);
  2017. btrfs_stop_workers(&fs_info->endio_meta_write_workers);
  2018. btrfs_stop_workers(&fs_info->endio_write_workers);
  2019. btrfs_stop_workers(&fs_info->submit_workers);
  2020. #if 0
  2021. while (!list_empty(&fs_info->hashers)) {
  2022. struct btrfs_hasher *hasher;
  2023. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  2024. hashers);
  2025. list_del(&hasher->hashers);
  2026. crypto_free_hash(&fs_info->hash_tfm);
  2027. kfree(hasher);
  2028. }
  2029. #endif
  2030. btrfs_close_devices(fs_info->fs_devices);
  2031. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  2032. bdi_destroy(&fs_info->bdi);
  2033. kfree(fs_info->extent_root);
  2034. kfree(fs_info->tree_root);
  2035. kfree(fs_info->chunk_root);
  2036. kfree(fs_info->dev_root);
  2037. kfree(fs_info->csum_root);
  2038. return 0;
  2039. }
  2040. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
  2041. {
  2042. int ret;
  2043. struct inode *btree_inode = buf->first_page->mapping->host;
  2044. ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  2045. if (!ret)
  2046. return ret;
  2047. ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
  2048. parent_transid);
  2049. return !ret;
  2050. }
  2051. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  2052. {
  2053. struct inode *btree_inode = buf->first_page->mapping->host;
  2054. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  2055. buf);
  2056. }
  2057. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  2058. {
  2059. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  2060. u64 transid = btrfs_header_generation(buf);
  2061. struct inode *btree_inode = root->fs_info->btree_inode;
  2062. int was_dirty;
  2063. btrfs_assert_tree_locked(buf);
  2064. if (transid != root->fs_info->generation) {
  2065. printk(KERN_CRIT "btrfs transid mismatch buffer %llu, "
  2066. "found %llu running %llu\n",
  2067. (unsigned long long)buf->start,
  2068. (unsigned long long)transid,
  2069. (unsigned long long)root->fs_info->generation);
  2070. WARN_ON(1);
  2071. }
  2072. was_dirty = set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  2073. buf);
  2074. if (!was_dirty) {
  2075. spin_lock(&root->fs_info->delalloc_lock);
  2076. root->fs_info->dirty_metadata_bytes += buf->len;
  2077. spin_unlock(&root->fs_info->delalloc_lock);
  2078. }
  2079. }
  2080. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  2081. {
  2082. /*
  2083. * looks as though older kernels can get into trouble with
  2084. * this code, they end up stuck in balance_dirty_pages forever
  2085. */
  2086. struct extent_io_tree *tree;
  2087. u64 num_dirty;
  2088. u64 start = 0;
  2089. unsigned long thresh = 32 * 1024 * 1024;
  2090. tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  2091. if (current_is_pdflush() || current->flags & PF_MEMALLOC)
  2092. return;
  2093. num_dirty = count_range_bits(tree, &start, (u64)-1,
  2094. thresh, EXTENT_DIRTY);
  2095. if (num_dirty > thresh) {
  2096. balance_dirty_pages_ratelimited_nr(
  2097. root->fs_info->btree_inode->i_mapping, 1);
  2098. }
  2099. return;
  2100. }
  2101. int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
  2102. {
  2103. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  2104. int ret;
  2105. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  2106. if (ret == 0)
  2107. set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
  2108. return ret;
  2109. }
  2110. int btree_lock_page_hook(struct page *page)
  2111. {
  2112. struct inode *inode = page->mapping->host;
  2113. struct btrfs_root *root = BTRFS_I(inode)->root;
  2114. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2115. struct extent_buffer *eb;
  2116. unsigned long len;
  2117. u64 bytenr = page_offset(page);
  2118. if (page->private == EXTENT_PAGE_PRIVATE)
  2119. goto out;
  2120. len = page->private >> 2;
  2121. eb = find_extent_buffer(io_tree, bytenr, len, GFP_NOFS);
  2122. if (!eb)
  2123. goto out;
  2124. btrfs_tree_lock(eb);
  2125. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  2126. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  2127. spin_lock(&root->fs_info->delalloc_lock);
  2128. if (root->fs_info->dirty_metadata_bytes >= eb->len)
  2129. root->fs_info->dirty_metadata_bytes -= eb->len;
  2130. else
  2131. WARN_ON(1);
  2132. spin_unlock(&root->fs_info->delalloc_lock);
  2133. }
  2134. btrfs_tree_unlock(eb);
  2135. free_extent_buffer(eb);
  2136. out:
  2137. lock_page(page);
  2138. return 0;
  2139. }
  2140. static struct extent_io_ops btree_extent_io_ops = {
  2141. .write_cache_pages_lock_hook = btree_lock_page_hook,
  2142. .readpage_end_io_hook = btree_readpage_end_io_hook,
  2143. .submit_bio_hook = btree_submit_bio_hook,
  2144. /* note we're sharing with inode.c for the merge bio hook */
  2145. .merge_bio_hook = btrfs_merge_bio_hook,
  2146. };