disk-io.c 69 KB

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