disk-io.c 78 KB

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