disk-io.c 70 KB

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