disk-io.c 62 KB

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