disk-io.c 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218
  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/slab.h>
  29. #include <linux/migrate.h>
  30. #include <linux/ratelimit.h>
  31. #include <linux/uuid.h>
  32. #include <linux/semaphore.h>
  33. #include <asm/unaligned.h>
  34. #include "ctree.h"
  35. #include "disk-io.h"
  36. #include "hash.h"
  37. #include "transaction.h"
  38. #include "btrfs_inode.h"
  39. #include "volumes.h"
  40. #include "print-tree.h"
  41. #include "locking.h"
  42. #include "tree-log.h"
  43. #include "free-space-cache.h"
  44. #include "inode-map.h"
  45. #include "check-integrity.h"
  46. #include "rcu-string.h"
  47. #include "dev-replace.h"
  48. #include "raid56.h"
  49. #include "sysfs.h"
  50. #include "qgroup.h"
  51. #ifdef CONFIG_X86
  52. #include <asm/cpufeature.h>
  53. #endif
  54. static struct extent_io_ops btree_extent_io_ops;
  55. static void end_workqueue_fn(struct btrfs_work *work);
  56. static void free_fs_root(struct btrfs_root *root);
  57. static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
  58. int read_only);
  59. static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
  60. static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
  61. struct btrfs_root *root);
  62. static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root);
  63. static int btrfs_destroy_marked_extents(struct btrfs_root *root,
  64. struct extent_io_tree *dirty_pages,
  65. int mark);
  66. static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
  67. struct extent_io_tree *pinned_extents);
  68. static int btrfs_cleanup_transaction(struct btrfs_root *root);
  69. static void btrfs_error_commit_super(struct btrfs_root *root);
  70. /*
  71. * btrfs_end_io_wq structs are used to do processing in task context when an IO
  72. * is complete. This is used during reads to verify checksums, and it is used
  73. * by writes to insert metadata for new file extents after IO is complete.
  74. */
  75. struct btrfs_end_io_wq {
  76. struct bio *bio;
  77. bio_end_io_t *end_io;
  78. void *private;
  79. struct btrfs_fs_info *info;
  80. int error;
  81. enum btrfs_wq_endio_type metadata;
  82. struct list_head list;
  83. struct btrfs_work work;
  84. };
  85. static struct kmem_cache *btrfs_end_io_wq_cache;
  86. int __init btrfs_end_io_wq_init(void)
  87. {
  88. btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq",
  89. sizeof(struct btrfs_end_io_wq),
  90. 0,
  91. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  92. NULL);
  93. if (!btrfs_end_io_wq_cache)
  94. return -ENOMEM;
  95. return 0;
  96. }
  97. void btrfs_end_io_wq_exit(void)
  98. {
  99. if (btrfs_end_io_wq_cache)
  100. kmem_cache_destroy(btrfs_end_io_wq_cache);
  101. }
  102. /*
  103. * async submit bios are used to offload expensive checksumming
  104. * onto the worker threads. They checksum file and metadata bios
  105. * just before they are sent down the IO stack.
  106. */
  107. struct async_submit_bio {
  108. struct inode *inode;
  109. struct bio *bio;
  110. struct list_head list;
  111. extent_submit_bio_hook_t *submit_bio_start;
  112. extent_submit_bio_hook_t *submit_bio_done;
  113. int rw;
  114. int mirror_num;
  115. unsigned long bio_flags;
  116. /*
  117. * bio_offset is optional, can be used if the pages in the bio
  118. * can't tell us where in the file the bio should go
  119. */
  120. u64 bio_offset;
  121. struct btrfs_work work;
  122. int error;
  123. };
  124. /*
  125. * Lockdep class keys for extent_buffer->lock's in this root. For a given
  126. * eb, the lockdep key is determined by the btrfs_root it belongs to and
  127. * the level the eb occupies in the tree.
  128. *
  129. * Different roots are used for different purposes and may nest inside each
  130. * other and they require separate keysets. As lockdep keys should be
  131. * static, assign keysets according to the purpose of the root as indicated
  132. * by btrfs_root->objectid. This ensures that all special purpose roots
  133. * have separate keysets.
  134. *
  135. * Lock-nesting across peer nodes is always done with the immediate parent
  136. * node locked thus preventing deadlock. As lockdep doesn't know this, use
  137. * subclass to avoid triggering lockdep warning in such cases.
  138. *
  139. * The key is set by the readpage_end_io_hook after the buffer has passed
  140. * csum validation but before the pages are unlocked. It is also set by
  141. * btrfs_init_new_buffer on freshly allocated blocks.
  142. *
  143. * We also add a check to make sure the highest level of the tree is the
  144. * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code
  145. * needs update as well.
  146. */
  147. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  148. # if BTRFS_MAX_LEVEL != 8
  149. # error
  150. # endif
  151. static struct btrfs_lockdep_keyset {
  152. u64 id; /* root objectid */
  153. const char *name_stem; /* lock name stem */
  154. char names[BTRFS_MAX_LEVEL + 1][20];
  155. struct lock_class_key keys[BTRFS_MAX_LEVEL + 1];
  156. } btrfs_lockdep_keysets[] = {
  157. { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" },
  158. { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" },
  159. { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" },
  160. { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" },
  161. { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" },
  162. { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" },
  163. { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" },
  164. { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" },
  165. { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" },
  166. { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" },
  167. { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" },
  168. { .id = 0, .name_stem = "tree" },
  169. };
  170. void __init btrfs_init_lockdep(void)
  171. {
  172. int i, j;
  173. /* initialize lockdep class names */
  174. for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) {
  175. struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i];
  176. for (j = 0; j < ARRAY_SIZE(ks->names); j++)
  177. snprintf(ks->names[j], sizeof(ks->names[j]),
  178. "btrfs-%s-%02d", ks->name_stem, j);
  179. }
  180. }
  181. void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb,
  182. int level)
  183. {
  184. struct btrfs_lockdep_keyset *ks;
  185. BUG_ON(level >= ARRAY_SIZE(ks->keys));
  186. /* find the matching keyset, id 0 is the default entry */
  187. for (ks = btrfs_lockdep_keysets; ks->id; ks++)
  188. if (ks->id == objectid)
  189. break;
  190. lockdep_set_class_and_name(&eb->lock,
  191. &ks->keys[level], ks->names[level]);
  192. }
  193. #endif
  194. /*
  195. * extents on the btree inode are pretty simple, there's one extent
  196. * that covers the entire device
  197. */
  198. static struct extent_map *btree_get_extent(struct inode *inode,
  199. struct page *page, size_t pg_offset, u64 start, u64 len,
  200. int create)
  201. {
  202. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  203. struct extent_map *em;
  204. int ret;
  205. read_lock(&em_tree->lock);
  206. em = lookup_extent_mapping(em_tree, start, len);
  207. if (em) {
  208. em->bdev =
  209. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  210. read_unlock(&em_tree->lock);
  211. goto out;
  212. }
  213. read_unlock(&em_tree->lock);
  214. em = alloc_extent_map();
  215. if (!em) {
  216. em = ERR_PTR(-ENOMEM);
  217. goto out;
  218. }
  219. em->start = 0;
  220. em->len = (u64)-1;
  221. em->block_len = (u64)-1;
  222. em->block_start = 0;
  223. em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  224. write_lock(&em_tree->lock);
  225. ret = add_extent_mapping(em_tree, em, 0);
  226. if (ret == -EEXIST) {
  227. free_extent_map(em);
  228. em = lookup_extent_mapping(em_tree, start, len);
  229. if (!em)
  230. em = ERR_PTR(-EIO);
  231. } else if (ret) {
  232. free_extent_map(em);
  233. em = ERR_PTR(ret);
  234. }
  235. write_unlock(&em_tree->lock);
  236. out:
  237. return em;
  238. }
  239. u32 btrfs_csum_data(char *data, u32 seed, size_t len)
  240. {
  241. return btrfs_crc32c(seed, data, len);
  242. }
  243. void btrfs_csum_final(u32 crc, char *result)
  244. {
  245. put_unaligned_le32(~crc, result);
  246. }
  247. /*
  248. * compute the csum for a btree block, and either verify it or write it
  249. * into the csum field of the block.
  250. */
  251. static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
  252. int verify)
  253. {
  254. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  255. char *result = NULL;
  256. unsigned long len;
  257. unsigned long cur_len;
  258. unsigned long offset = BTRFS_CSUM_SIZE;
  259. char *kaddr;
  260. unsigned long map_start;
  261. unsigned long map_len;
  262. int err;
  263. u32 crc = ~(u32)0;
  264. unsigned long inline_result;
  265. len = buf->len - offset;
  266. while (len > 0) {
  267. err = map_private_extent_buffer(buf, offset, 32,
  268. &kaddr, &map_start, &map_len);
  269. if (err)
  270. return 1;
  271. cur_len = min(len, map_len - (offset - map_start));
  272. crc = btrfs_csum_data(kaddr + offset - map_start,
  273. crc, cur_len);
  274. len -= cur_len;
  275. offset += cur_len;
  276. }
  277. if (csum_size > sizeof(inline_result)) {
  278. result = kzalloc(csum_size * sizeof(char), GFP_NOFS);
  279. if (!result)
  280. return 1;
  281. } else {
  282. result = (char *)&inline_result;
  283. }
  284. btrfs_csum_final(crc, result);
  285. if (verify) {
  286. if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
  287. u32 val;
  288. u32 found = 0;
  289. memcpy(&found, result, csum_size);
  290. read_extent_buffer(buf, &val, 0, csum_size);
  291. printk_ratelimited(KERN_INFO
  292. "BTRFS: %s checksum verify failed on %llu wanted %X found %X "
  293. "level %d\n",
  294. root->fs_info->sb->s_id, buf->start,
  295. val, found, btrfs_header_level(buf));
  296. if (result != (char *)&inline_result)
  297. kfree(result);
  298. return 1;
  299. }
  300. } else {
  301. write_extent_buffer(buf, result, 0, csum_size);
  302. }
  303. if (result != (char *)&inline_result)
  304. kfree(result);
  305. return 0;
  306. }
  307. /*
  308. * we can't consider a given block up to date unless the transid of the
  309. * block matches the transid in the parent node's pointer. This is how we
  310. * detect blocks that either didn't get written at all or got written
  311. * in the wrong place.
  312. */
  313. static int verify_parent_transid(struct extent_io_tree *io_tree,
  314. struct extent_buffer *eb, u64 parent_transid,
  315. int atomic)
  316. {
  317. struct extent_state *cached_state = NULL;
  318. int ret;
  319. bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB);
  320. if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
  321. return 0;
  322. if (atomic)
  323. return -EAGAIN;
  324. if (need_lock) {
  325. btrfs_tree_read_lock(eb);
  326. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  327. }
  328. lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
  329. 0, &cached_state);
  330. if (extent_buffer_uptodate(eb) &&
  331. btrfs_header_generation(eb) == parent_transid) {
  332. ret = 0;
  333. goto out;
  334. }
  335. printk_ratelimited(KERN_INFO "BTRFS (device %s): parent transid verify failed on %llu wanted %llu found %llu\n",
  336. eb->fs_info->sb->s_id, eb->start,
  337. parent_transid, btrfs_header_generation(eb));
  338. ret = 1;
  339. /*
  340. * Things reading via commit roots that don't have normal protection,
  341. * like send, can have a really old block in cache that may point at a
  342. * block that has been free'd and re-allocated. So don't clear uptodate
  343. * if we find an eb that is under IO (dirty/writeback) because we could
  344. * end up reading in the stale data and then writing it back out and
  345. * making everybody very sad.
  346. */
  347. if (!extent_buffer_under_io(eb))
  348. clear_extent_buffer_uptodate(eb);
  349. out:
  350. unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1,
  351. &cached_state, GFP_NOFS);
  352. if (need_lock)
  353. btrfs_tree_read_unlock_blocking(eb);
  354. return ret;
  355. }
  356. /*
  357. * Return 0 if the superblock checksum type matches the checksum value of that
  358. * algorithm. Pass the raw disk superblock data.
  359. */
  360. static int btrfs_check_super_csum(char *raw_disk_sb)
  361. {
  362. struct btrfs_super_block *disk_sb =
  363. (struct btrfs_super_block *)raw_disk_sb;
  364. u16 csum_type = btrfs_super_csum_type(disk_sb);
  365. int ret = 0;
  366. if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
  367. u32 crc = ~(u32)0;
  368. const int csum_size = sizeof(crc);
  369. char result[csum_size];
  370. /*
  371. * The super_block structure does not span the whole
  372. * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
  373. * is filled with zeros and is included in the checkum.
  374. */
  375. crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
  376. crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  377. btrfs_csum_final(crc, result);
  378. if (memcmp(raw_disk_sb, result, csum_size))
  379. ret = 1;
  380. if (ret && btrfs_super_generation(disk_sb) < 10) {
  381. printk(KERN_WARNING
  382. "BTRFS: super block crcs don't match, older mkfs detected\n");
  383. ret = 0;
  384. }
  385. }
  386. if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
  387. printk(KERN_ERR "BTRFS: unsupported checksum algorithm %u\n",
  388. csum_type);
  389. ret = 1;
  390. }
  391. return ret;
  392. }
  393. /*
  394. * helper to read a given tree block, doing retries as required when
  395. * the checksums don't match and we have alternate mirrors to try.
  396. */
  397. static int btree_read_extent_buffer_pages(struct btrfs_root *root,
  398. struct extent_buffer *eb,
  399. u64 start, u64 parent_transid)
  400. {
  401. struct extent_io_tree *io_tree;
  402. int failed = 0;
  403. int ret;
  404. int num_copies = 0;
  405. int mirror_num = 0;
  406. int failed_mirror = 0;
  407. clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
  408. io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
  409. while (1) {
  410. ret = read_extent_buffer_pages(io_tree, eb, start,
  411. WAIT_COMPLETE,
  412. btree_get_extent, mirror_num);
  413. if (!ret) {
  414. if (!verify_parent_transid(io_tree, eb,
  415. parent_transid, 0))
  416. break;
  417. else
  418. ret = -EIO;
  419. }
  420. /*
  421. * This buffer's crc is fine, but its contents are corrupted, so
  422. * there is no reason to read the other copies, they won't be
  423. * any less wrong.
  424. */
  425. if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags))
  426. break;
  427. num_copies = btrfs_num_copies(root->fs_info,
  428. eb->start, eb->len);
  429. if (num_copies == 1)
  430. break;
  431. if (!failed_mirror) {
  432. failed = 1;
  433. failed_mirror = eb->read_mirror;
  434. }
  435. mirror_num++;
  436. if (mirror_num == failed_mirror)
  437. mirror_num++;
  438. if (mirror_num > num_copies)
  439. break;
  440. }
  441. if (failed && !ret && failed_mirror)
  442. repair_eb_io_failure(root, eb, failed_mirror);
  443. return ret;
  444. }
  445. /*
  446. * checksum a dirty tree block before IO. This has extra checks to make sure
  447. * we only fill in the checksum field in the first page of a multi-page block
  448. */
  449. static int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
  450. {
  451. u64 start = page_offset(page);
  452. u64 found_start;
  453. struct extent_buffer *eb;
  454. eb = (struct extent_buffer *)page->private;
  455. if (page != eb->pages[0])
  456. return 0;
  457. found_start = btrfs_header_bytenr(eb);
  458. if (WARN_ON(found_start != start || !PageUptodate(page)))
  459. return 0;
  460. csum_tree_block(root, eb, 0);
  461. return 0;
  462. }
  463. static int check_tree_block_fsid(struct btrfs_root *root,
  464. struct extent_buffer *eb)
  465. {
  466. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  467. u8 fsid[BTRFS_UUID_SIZE];
  468. int ret = 1;
  469. read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE);
  470. while (fs_devices) {
  471. if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) {
  472. ret = 0;
  473. break;
  474. }
  475. fs_devices = fs_devices->seed;
  476. }
  477. return ret;
  478. }
  479. #define CORRUPT(reason, eb, root, slot) \
  480. btrfs_crit(root->fs_info, "corrupt leaf, %s: block=%llu," \
  481. "root=%llu, slot=%d", reason, \
  482. btrfs_header_bytenr(eb), root->objectid, slot)
  483. static noinline int check_leaf(struct btrfs_root *root,
  484. struct extent_buffer *leaf)
  485. {
  486. struct btrfs_key key;
  487. struct btrfs_key leaf_key;
  488. u32 nritems = btrfs_header_nritems(leaf);
  489. int slot;
  490. if (nritems == 0)
  491. return 0;
  492. /* Check the 0 item */
  493. if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) !=
  494. BTRFS_LEAF_DATA_SIZE(root)) {
  495. CORRUPT("invalid item offset size pair", leaf, root, 0);
  496. return -EIO;
  497. }
  498. /*
  499. * Check to make sure each items keys are in the correct order and their
  500. * offsets make sense. We only have to loop through nritems-1 because
  501. * we check the current slot against the next slot, which verifies the
  502. * next slot's offset+size makes sense and that the current's slot
  503. * offset is correct.
  504. */
  505. for (slot = 0; slot < nritems - 1; slot++) {
  506. btrfs_item_key_to_cpu(leaf, &leaf_key, slot);
  507. btrfs_item_key_to_cpu(leaf, &key, slot + 1);
  508. /* Make sure the keys are in the right order */
  509. if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) {
  510. CORRUPT("bad key order", leaf, root, slot);
  511. return -EIO;
  512. }
  513. /*
  514. * Make sure the offset and ends are right, remember that the
  515. * item data starts at the end of the leaf and grows towards the
  516. * front.
  517. */
  518. if (btrfs_item_offset_nr(leaf, slot) !=
  519. btrfs_item_end_nr(leaf, slot + 1)) {
  520. CORRUPT("slot offset bad", leaf, root, slot);
  521. return -EIO;
  522. }
  523. /*
  524. * Check to make sure that we don't point outside of the leaf,
  525. * just incase all the items are consistent to eachother, but
  526. * all point outside of the leaf.
  527. */
  528. if (btrfs_item_end_nr(leaf, slot) >
  529. BTRFS_LEAF_DATA_SIZE(root)) {
  530. CORRUPT("slot end outside of leaf", leaf, root, slot);
  531. return -EIO;
  532. }
  533. }
  534. return 0;
  535. }
  536. static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
  537. u64 phy_offset, struct page *page,
  538. u64 start, u64 end, int mirror)
  539. {
  540. u64 found_start;
  541. int found_level;
  542. struct extent_buffer *eb;
  543. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  544. int ret = 0;
  545. int reads_done;
  546. if (!page->private)
  547. goto out;
  548. eb = (struct extent_buffer *)page->private;
  549. /* the pending IO might have been the only thing that kept this buffer
  550. * in memory. Make sure we have a ref for all this other checks
  551. */
  552. extent_buffer_get(eb);
  553. reads_done = atomic_dec_and_test(&eb->io_pages);
  554. if (!reads_done)
  555. goto err;
  556. eb->read_mirror = mirror;
  557. if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) {
  558. ret = -EIO;
  559. goto err;
  560. }
  561. found_start = btrfs_header_bytenr(eb);
  562. if (found_start != eb->start) {
  563. printk_ratelimited(KERN_INFO "BTRFS (device %s): bad tree block start "
  564. "%llu %llu\n",
  565. eb->fs_info->sb->s_id, found_start, eb->start);
  566. ret = -EIO;
  567. goto err;
  568. }
  569. if (check_tree_block_fsid(root, eb)) {
  570. printk_ratelimited(KERN_INFO "BTRFS (device %s): bad fsid on block %llu\n",
  571. eb->fs_info->sb->s_id, eb->start);
  572. ret = -EIO;
  573. goto err;
  574. }
  575. found_level = btrfs_header_level(eb);
  576. if (found_level >= BTRFS_MAX_LEVEL) {
  577. btrfs_info(root->fs_info, "bad tree block level %d",
  578. (int)btrfs_header_level(eb));
  579. ret = -EIO;
  580. goto err;
  581. }
  582. btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb),
  583. eb, found_level);
  584. ret = csum_tree_block(root, eb, 1);
  585. if (ret) {
  586. ret = -EIO;
  587. goto err;
  588. }
  589. /*
  590. * If this is a leaf block and it is corrupt, set the corrupt bit so
  591. * that we don't try and read the other copies of this block, just
  592. * return -EIO.
  593. */
  594. if (found_level == 0 && check_leaf(root, eb)) {
  595. set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
  596. ret = -EIO;
  597. }
  598. if (!ret)
  599. set_extent_buffer_uptodate(eb);
  600. err:
  601. if (reads_done &&
  602. test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags))
  603. btree_readahead_hook(root, eb, eb->start, ret);
  604. if (ret) {
  605. /*
  606. * our io error hook is going to dec the io pages
  607. * again, we have to make sure it has something
  608. * to decrement
  609. */
  610. atomic_inc(&eb->io_pages);
  611. clear_extent_buffer_uptodate(eb);
  612. }
  613. free_extent_buffer(eb);
  614. out:
  615. return ret;
  616. }
  617. static int btree_io_failed_hook(struct page *page, int failed_mirror)
  618. {
  619. struct extent_buffer *eb;
  620. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  621. eb = (struct extent_buffer *)page->private;
  622. set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
  623. eb->read_mirror = failed_mirror;
  624. atomic_dec(&eb->io_pages);
  625. if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags))
  626. btree_readahead_hook(root, eb, eb->start, -EIO);
  627. return -EIO; /* we fixed nothing */
  628. }
  629. static void end_workqueue_bio(struct bio *bio, int err)
  630. {
  631. struct btrfs_end_io_wq *end_io_wq = bio->bi_private;
  632. struct btrfs_fs_info *fs_info;
  633. struct btrfs_workqueue *wq;
  634. btrfs_work_func_t func;
  635. fs_info = end_io_wq->info;
  636. end_io_wq->error = err;
  637. if (bio->bi_rw & REQ_WRITE) {
  638. if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) {
  639. wq = fs_info->endio_meta_write_workers;
  640. func = btrfs_endio_meta_write_helper;
  641. } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) {
  642. wq = fs_info->endio_freespace_worker;
  643. func = btrfs_freespace_write_helper;
  644. } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) {
  645. wq = fs_info->endio_raid56_workers;
  646. func = btrfs_endio_raid56_helper;
  647. } else {
  648. wq = fs_info->endio_write_workers;
  649. func = btrfs_endio_write_helper;
  650. }
  651. } else {
  652. if (unlikely(end_io_wq->metadata ==
  653. BTRFS_WQ_ENDIO_DIO_REPAIR)) {
  654. wq = fs_info->endio_repair_workers;
  655. func = btrfs_endio_repair_helper;
  656. } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) {
  657. wq = fs_info->endio_raid56_workers;
  658. func = btrfs_endio_raid56_helper;
  659. } else if (end_io_wq->metadata) {
  660. wq = fs_info->endio_meta_workers;
  661. func = btrfs_endio_meta_helper;
  662. } else {
  663. wq = fs_info->endio_workers;
  664. func = btrfs_endio_helper;
  665. }
  666. }
  667. btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL);
  668. btrfs_queue_work(wq, &end_io_wq->work);
  669. }
  670. int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
  671. enum btrfs_wq_endio_type metadata)
  672. {
  673. struct btrfs_end_io_wq *end_io_wq;
  674. end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS);
  675. if (!end_io_wq)
  676. return -ENOMEM;
  677. end_io_wq->private = bio->bi_private;
  678. end_io_wq->end_io = bio->bi_end_io;
  679. end_io_wq->info = info;
  680. end_io_wq->error = 0;
  681. end_io_wq->bio = bio;
  682. end_io_wq->metadata = metadata;
  683. bio->bi_private = end_io_wq;
  684. bio->bi_end_io = end_workqueue_bio;
  685. return 0;
  686. }
  687. unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info)
  688. {
  689. unsigned long limit = min_t(unsigned long,
  690. info->thread_pool_size,
  691. info->fs_devices->open_devices);
  692. return 256 * limit;
  693. }
  694. static void run_one_async_start(struct btrfs_work *work)
  695. {
  696. struct async_submit_bio *async;
  697. int ret;
  698. async = container_of(work, struct async_submit_bio, work);
  699. ret = async->submit_bio_start(async->inode, async->rw, async->bio,
  700. async->mirror_num, async->bio_flags,
  701. async->bio_offset);
  702. if (ret)
  703. async->error = ret;
  704. }
  705. static void run_one_async_done(struct btrfs_work *work)
  706. {
  707. struct btrfs_fs_info *fs_info;
  708. struct async_submit_bio *async;
  709. int limit;
  710. async = container_of(work, struct async_submit_bio, work);
  711. fs_info = BTRFS_I(async->inode)->root->fs_info;
  712. limit = btrfs_async_submit_limit(fs_info);
  713. limit = limit * 2 / 3;
  714. if (atomic_dec_return(&fs_info->nr_async_submits) < limit &&
  715. waitqueue_active(&fs_info->async_submit_wait))
  716. wake_up(&fs_info->async_submit_wait);
  717. /* If an error occured we just want to clean up the bio and move on */
  718. if (async->error) {
  719. bio_endio(async->bio, async->error);
  720. return;
  721. }
  722. async->submit_bio_done(async->inode, async->rw, async->bio,
  723. async->mirror_num, async->bio_flags,
  724. async->bio_offset);
  725. }
  726. static void run_one_async_free(struct btrfs_work *work)
  727. {
  728. struct async_submit_bio *async;
  729. async = container_of(work, struct async_submit_bio, work);
  730. kfree(async);
  731. }
  732. int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
  733. int rw, struct bio *bio, int mirror_num,
  734. unsigned long bio_flags,
  735. u64 bio_offset,
  736. extent_submit_bio_hook_t *submit_bio_start,
  737. extent_submit_bio_hook_t *submit_bio_done)
  738. {
  739. struct async_submit_bio *async;
  740. async = kmalloc(sizeof(*async), GFP_NOFS);
  741. if (!async)
  742. return -ENOMEM;
  743. async->inode = inode;
  744. async->rw = rw;
  745. async->bio = bio;
  746. async->mirror_num = mirror_num;
  747. async->submit_bio_start = submit_bio_start;
  748. async->submit_bio_done = submit_bio_done;
  749. btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start,
  750. run_one_async_done, run_one_async_free);
  751. async->bio_flags = bio_flags;
  752. async->bio_offset = bio_offset;
  753. async->error = 0;
  754. atomic_inc(&fs_info->nr_async_submits);
  755. if (rw & REQ_SYNC)
  756. btrfs_set_work_high_priority(&async->work);
  757. btrfs_queue_work(fs_info->workers, &async->work);
  758. while (atomic_read(&fs_info->async_submit_draining) &&
  759. atomic_read(&fs_info->nr_async_submits)) {
  760. wait_event(fs_info->async_submit_wait,
  761. (atomic_read(&fs_info->nr_async_submits) == 0));
  762. }
  763. return 0;
  764. }
  765. static int btree_csum_one_bio(struct bio *bio)
  766. {
  767. struct bio_vec *bvec;
  768. struct btrfs_root *root;
  769. int i, ret = 0;
  770. bio_for_each_segment_all(bvec, bio, i) {
  771. root = BTRFS_I(bvec->bv_page->mapping->host)->root;
  772. ret = csum_dirty_buffer(root, bvec->bv_page);
  773. if (ret)
  774. break;
  775. }
  776. return ret;
  777. }
  778. static int __btree_submit_bio_start(struct inode *inode, int rw,
  779. struct bio *bio, int mirror_num,
  780. unsigned long bio_flags,
  781. u64 bio_offset)
  782. {
  783. /*
  784. * when we're called for a write, we're already in the async
  785. * submission context. Just jump into btrfs_map_bio
  786. */
  787. return btree_csum_one_bio(bio);
  788. }
  789. static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  790. int mirror_num, unsigned long bio_flags,
  791. u64 bio_offset)
  792. {
  793. int ret;
  794. /*
  795. * when we're called for a write, we're already in the async
  796. * submission context. Just jump into btrfs_map_bio
  797. */
  798. ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1);
  799. if (ret)
  800. bio_endio(bio, ret);
  801. return ret;
  802. }
  803. static int check_async_write(struct inode *inode, unsigned long bio_flags)
  804. {
  805. if (bio_flags & EXTENT_BIO_TREE_LOG)
  806. return 0;
  807. #ifdef CONFIG_X86
  808. if (cpu_has_xmm4_2)
  809. return 0;
  810. #endif
  811. return 1;
  812. }
  813. static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  814. int mirror_num, unsigned long bio_flags,
  815. u64 bio_offset)
  816. {
  817. int async = check_async_write(inode, bio_flags);
  818. int ret;
  819. if (!(rw & REQ_WRITE)) {
  820. /*
  821. * called for a read, do the setup so that checksum validation
  822. * can happen in the async kernel threads
  823. */
  824. ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info,
  825. bio, BTRFS_WQ_ENDIO_METADATA);
  826. if (ret)
  827. goto out_w_error;
  828. ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
  829. mirror_num, 0);
  830. } else if (!async) {
  831. ret = btree_csum_one_bio(bio);
  832. if (ret)
  833. goto out_w_error;
  834. ret = btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
  835. mirror_num, 0);
  836. } else {
  837. /*
  838. * kthread helpers are used to submit writes so that
  839. * checksumming can happen in parallel across all CPUs
  840. */
  841. ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  842. inode, rw, bio, mirror_num, 0,
  843. bio_offset,
  844. __btree_submit_bio_start,
  845. __btree_submit_bio_done);
  846. }
  847. if (ret) {
  848. out_w_error:
  849. bio_endio(bio, ret);
  850. }
  851. return ret;
  852. }
  853. #ifdef CONFIG_MIGRATION
  854. static int btree_migratepage(struct address_space *mapping,
  855. struct page *newpage, struct page *page,
  856. enum migrate_mode mode)
  857. {
  858. /*
  859. * we can't safely write a btree page from here,
  860. * we haven't done the locking hook
  861. */
  862. if (PageDirty(page))
  863. return -EAGAIN;
  864. /*
  865. * Buffers may be managed in a filesystem specific way.
  866. * We must have no buffers or drop them.
  867. */
  868. if (page_has_private(page) &&
  869. !try_to_release_page(page, GFP_KERNEL))
  870. return -EAGAIN;
  871. return migrate_page(mapping, newpage, page, mode);
  872. }
  873. #endif
  874. static int btree_writepages(struct address_space *mapping,
  875. struct writeback_control *wbc)
  876. {
  877. struct btrfs_fs_info *fs_info;
  878. int ret;
  879. if (wbc->sync_mode == WB_SYNC_NONE) {
  880. if (wbc->for_kupdate)
  881. return 0;
  882. fs_info = BTRFS_I(mapping->host)->root->fs_info;
  883. /* this is a bit racy, but that's ok */
  884. ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes,
  885. BTRFS_DIRTY_METADATA_THRESH);
  886. if (ret < 0)
  887. return 0;
  888. }
  889. return btree_write_cache_pages(mapping, wbc);
  890. }
  891. static int btree_readpage(struct file *file, struct page *page)
  892. {
  893. struct extent_io_tree *tree;
  894. tree = &BTRFS_I(page->mapping->host)->io_tree;
  895. return extent_read_full_page(tree, page, btree_get_extent, 0);
  896. }
  897. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  898. {
  899. if (PageWriteback(page) || PageDirty(page))
  900. return 0;
  901. return try_release_extent_buffer(page);
  902. }
  903. static void btree_invalidatepage(struct page *page, unsigned int offset,
  904. unsigned int length)
  905. {
  906. struct extent_io_tree *tree;
  907. tree = &BTRFS_I(page->mapping->host)->io_tree;
  908. extent_invalidatepage(tree, page, offset);
  909. btree_releasepage(page, GFP_NOFS);
  910. if (PagePrivate(page)) {
  911. btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info,
  912. "page private not zero on page %llu",
  913. (unsigned long long)page_offset(page));
  914. ClearPagePrivate(page);
  915. set_page_private(page, 0);
  916. page_cache_release(page);
  917. }
  918. }
  919. static int btree_set_page_dirty(struct page *page)
  920. {
  921. #ifdef DEBUG
  922. struct extent_buffer *eb;
  923. BUG_ON(!PagePrivate(page));
  924. eb = (struct extent_buffer *)page->private;
  925. BUG_ON(!eb);
  926. BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  927. BUG_ON(!atomic_read(&eb->refs));
  928. btrfs_assert_tree_locked(eb);
  929. #endif
  930. return __set_page_dirty_nobuffers(page);
  931. }
  932. static const struct address_space_operations btree_aops = {
  933. .readpage = btree_readpage,
  934. .writepages = btree_writepages,
  935. .releasepage = btree_releasepage,
  936. .invalidatepage = btree_invalidatepage,
  937. #ifdef CONFIG_MIGRATION
  938. .migratepage = btree_migratepage,
  939. #endif
  940. .set_page_dirty = btree_set_page_dirty,
  941. };
  942. void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
  943. {
  944. struct extent_buffer *buf = NULL;
  945. struct inode *btree_inode = root->fs_info->btree_inode;
  946. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  947. if (!buf)
  948. return;
  949. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  950. buf, 0, WAIT_NONE, btree_get_extent, 0);
  951. free_extent_buffer(buf);
  952. }
  953. int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, u32 blocksize,
  954. int mirror_num, struct extent_buffer **eb)
  955. {
  956. struct extent_buffer *buf = NULL;
  957. struct inode *btree_inode = root->fs_info->btree_inode;
  958. struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree;
  959. int ret;
  960. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  961. if (!buf)
  962. return 0;
  963. set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags);
  964. ret = read_extent_buffer_pages(io_tree, buf, 0, WAIT_PAGE_LOCK,
  965. btree_get_extent, mirror_num);
  966. if (ret) {
  967. free_extent_buffer(buf);
  968. return ret;
  969. }
  970. if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) {
  971. free_extent_buffer(buf);
  972. return -EIO;
  973. } else if (extent_buffer_uptodate(buf)) {
  974. *eb = buf;
  975. } else {
  976. free_extent_buffer(buf);
  977. }
  978. return 0;
  979. }
  980. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  981. u64 bytenr)
  982. {
  983. return find_extent_buffer(root->fs_info, bytenr);
  984. }
  985. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  986. u64 bytenr, u32 blocksize)
  987. {
  988. if (btrfs_test_is_dummy_root(root))
  989. return alloc_test_extent_buffer(root->fs_info, bytenr,
  990. blocksize);
  991. return alloc_extent_buffer(root->fs_info, bytenr, blocksize);
  992. }
  993. int btrfs_write_tree_block(struct extent_buffer *buf)
  994. {
  995. return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start,
  996. buf->start + buf->len - 1);
  997. }
  998. int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
  999. {
  1000. return filemap_fdatawait_range(buf->pages[0]->mapping,
  1001. buf->start, buf->start + buf->len - 1);
  1002. }
  1003. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  1004. u64 parent_transid)
  1005. {
  1006. struct extent_buffer *buf = NULL;
  1007. int ret;
  1008. buf = btrfs_find_create_tree_block(root, bytenr, root->nodesize);
  1009. if (!buf)
  1010. return NULL;
  1011. ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  1012. if (ret) {
  1013. free_extent_buffer(buf);
  1014. return NULL;
  1015. }
  1016. return buf;
  1017. }
  1018. void clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  1019. struct extent_buffer *buf)
  1020. {
  1021. struct btrfs_fs_info *fs_info = root->fs_info;
  1022. if (btrfs_header_generation(buf) ==
  1023. fs_info->running_transaction->transid) {
  1024. btrfs_assert_tree_locked(buf);
  1025. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
  1026. __percpu_counter_add(&fs_info->dirty_metadata_bytes,
  1027. -buf->len,
  1028. fs_info->dirty_metadata_batch);
  1029. /* ugh, clear_extent_buffer_dirty needs to lock the page */
  1030. btrfs_set_lock_blocking(buf);
  1031. clear_extent_buffer_dirty(buf);
  1032. }
  1033. }
  1034. }
  1035. static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void)
  1036. {
  1037. struct btrfs_subvolume_writers *writers;
  1038. int ret;
  1039. writers = kmalloc(sizeof(*writers), GFP_NOFS);
  1040. if (!writers)
  1041. return ERR_PTR(-ENOMEM);
  1042. ret = percpu_counter_init(&writers->counter, 0, GFP_KERNEL);
  1043. if (ret < 0) {
  1044. kfree(writers);
  1045. return ERR_PTR(ret);
  1046. }
  1047. init_waitqueue_head(&writers->wait);
  1048. return writers;
  1049. }
  1050. static void
  1051. btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers)
  1052. {
  1053. percpu_counter_destroy(&writers->counter);
  1054. kfree(writers);
  1055. }
  1056. static void __setup_root(u32 nodesize, u32 sectorsize, u32 stripesize,
  1057. struct btrfs_root *root, struct btrfs_fs_info *fs_info,
  1058. u64 objectid)
  1059. {
  1060. root->node = NULL;
  1061. root->commit_root = NULL;
  1062. root->sectorsize = sectorsize;
  1063. root->nodesize = nodesize;
  1064. root->stripesize = stripesize;
  1065. root->state = 0;
  1066. root->orphan_cleanup_state = 0;
  1067. root->objectid = objectid;
  1068. root->last_trans = 0;
  1069. root->highest_objectid = 0;
  1070. root->nr_delalloc_inodes = 0;
  1071. root->nr_ordered_extents = 0;
  1072. root->name = NULL;
  1073. root->inode_tree = RB_ROOT;
  1074. INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC);
  1075. root->block_rsv = NULL;
  1076. root->orphan_block_rsv = NULL;
  1077. INIT_LIST_HEAD(&root->dirty_list);
  1078. INIT_LIST_HEAD(&root->root_list);
  1079. INIT_LIST_HEAD(&root->delalloc_inodes);
  1080. INIT_LIST_HEAD(&root->delalloc_root);
  1081. INIT_LIST_HEAD(&root->ordered_extents);
  1082. INIT_LIST_HEAD(&root->ordered_root);
  1083. INIT_LIST_HEAD(&root->logged_list[0]);
  1084. INIT_LIST_HEAD(&root->logged_list[1]);
  1085. spin_lock_init(&root->orphan_lock);
  1086. spin_lock_init(&root->inode_lock);
  1087. spin_lock_init(&root->delalloc_lock);
  1088. spin_lock_init(&root->ordered_extent_lock);
  1089. spin_lock_init(&root->accounting_lock);
  1090. spin_lock_init(&root->log_extents_lock[0]);
  1091. spin_lock_init(&root->log_extents_lock[1]);
  1092. mutex_init(&root->objectid_mutex);
  1093. mutex_init(&root->log_mutex);
  1094. mutex_init(&root->ordered_extent_mutex);
  1095. mutex_init(&root->delalloc_mutex);
  1096. init_waitqueue_head(&root->log_writer_wait);
  1097. init_waitqueue_head(&root->log_commit_wait[0]);
  1098. init_waitqueue_head(&root->log_commit_wait[1]);
  1099. INIT_LIST_HEAD(&root->log_ctxs[0]);
  1100. INIT_LIST_HEAD(&root->log_ctxs[1]);
  1101. atomic_set(&root->log_commit[0], 0);
  1102. atomic_set(&root->log_commit[1], 0);
  1103. atomic_set(&root->log_writers, 0);
  1104. atomic_set(&root->log_batch, 0);
  1105. atomic_set(&root->orphan_inodes, 0);
  1106. atomic_set(&root->refs, 1);
  1107. atomic_set(&root->will_be_snapshoted, 0);
  1108. root->log_transid = 0;
  1109. root->log_transid_committed = -1;
  1110. root->last_log_commit = 0;
  1111. if (fs_info)
  1112. extent_io_tree_init(&root->dirty_log_pages,
  1113. fs_info->btree_inode->i_mapping);
  1114. memset(&root->root_key, 0, sizeof(root->root_key));
  1115. memset(&root->root_item, 0, sizeof(root->root_item));
  1116. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  1117. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  1118. if (fs_info)
  1119. root->defrag_trans_start = fs_info->generation;
  1120. else
  1121. root->defrag_trans_start = 0;
  1122. init_completion(&root->kobj_unregister);
  1123. root->root_key.objectid = objectid;
  1124. root->anon_dev = 0;
  1125. spin_lock_init(&root->root_item_lock);
  1126. }
  1127. static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info)
  1128. {
  1129. struct btrfs_root *root = kzalloc(sizeof(*root), GFP_NOFS);
  1130. if (root)
  1131. root->fs_info = fs_info;
  1132. return root;
  1133. }
  1134. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  1135. /* Should only be used by the testing infrastructure */
  1136. struct btrfs_root *btrfs_alloc_dummy_root(void)
  1137. {
  1138. struct btrfs_root *root;
  1139. root = btrfs_alloc_root(NULL);
  1140. if (!root)
  1141. return ERR_PTR(-ENOMEM);
  1142. __setup_root(4096, 4096, 4096, root, NULL, 1);
  1143. set_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state);
  1144. root->alloc_bytenr = 0;
  1145. return root;
  1146. }
  1147. #endif
  1148. struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
  1149. struct btrfs_fs_info *fs_info,
  1150. u64 objectid)
  1151. {
  1152. struct extent_buffer *leaf;
  1153. struct btrfs_root *tree_root = fs_info->tree_root;
  1154. struct btrfs_root *root;
  1155. struct btrfs_key key;
  1156. int ret = 0;
  1157. uuid_le uuid;
  1158. root = btrfs_alloc_root(fs_info);
  1159. if (!root)
  1160. return ERR_PTR(-ENOMEM);
  1161. __setup_root(tree_root->nodesize, tree_root->sectorsize,
  1162. tree_root->stripesize, root, fs_info, objectid);
  1163. root->root_key.objectid = objectid;
  1164. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  1165. root->root_key.offset = 0;
  1166. leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
  1167. if (IS_ERR(leaf)) {
  1168. ret = PTR_ERR(leaf);
  1169. leaf = NULL;
  1170. goto fail;
  1171. }
  1172. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  1173. btrfs_set_header_bytenr(leaf, leaf->start);
  1174. btrfs_set_header_generation(leaf, trans->transid);
  1175. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  1176. btrfs_set_header_owner(leaf, objectid);
  1177. root->node = leaf;
  1178. write_extent_buffer(leaf, fs_info->fsid, btrfs_header_fsid(),
  1179. BTRFS_FSID_SIZE);
  1180. write_extent_buffer(leaf, fs_info->chunk_tree_uuid,
  1181. btrfs_header_chunk_tree_uuid(leaf),
  1182. BTRFS_UUID_SIZE);
  1183. btrfs_mark_buffer_dirty(leaf);
  1184. root->commit_root = btrfs_root_node(root);
  1185. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  1186. root->root_item.flags = 0;
  1187. root->root_item.byte_limit = 0;
  1188. btrfs_set_root_bytenr(&root->root_item, leaf->start);
  1189. btrfs_set_root_generation(&root->root_item, trans->transid);
  1190. btrfs_set_root_level(&root->root_item, 0);
  1191. btrfs_set_root_refs(&root->root_item, 1);
  1192. btrfs_set_root_used(&root->root_item, leaf->len);
  1193. btrfs_set_root_last_snapshot(&root->root_item, 0);
  1194. btrfs_set_root_dirid(&root->root_item, 0);
  1195. uuid_le_gen(&uuid);
  1196. memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE);
  1197. root->root_item.drop_level = 0;
  1198. key.objectid = objectid;
  1199. key.type = BTRFS_ROOT_ITEM_KEY;
  1200. key.offset = 0;
  1201. ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item);
  1202. if (ret)
  1203. goto fail;
  1204. btrfs_tree_unlock(leaf);
  1205. return root;
  1206. fail:
  1207. if (leaf) {
  1208. btrfs_tree_unlock(leaf);
  1209. free_extent_buffer(root->commit_root);
  1210. free_extent_buffer(leaf);
  1211. }
  1212. kfree(root);
  1213. return ERR_PTR(ret);
  1214. }
  1215. static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
  1216. struct btrfs_fs_info *fs_info)
  1217. {
  1218. struct btrfs_root *root;
  1219. struct btrfs_root *tree_root = fs_info->tree_root;
  1220. struct extent_buffer *leaf;
  1221. root = btrfs_alloc_root(fs_info);
  1222. if (!root)
  1223. return ERR_PTR(-ENOMEM);
  1224. __setup_root(tree_root->nodesize, tree_root->sectorsize,
  1225. tree_root->stripesize, root, fs_info,
  1226. BTRFS_TREE_LOG_OBJECTID);
  1227. root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
  1228. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  1229. root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
  1230. /*
  1231. * DON'T set REF_COWS for log trees
  1232. *
  1233. * log trees do not get reference counted because they go away
  1234. * before a real commit is actually done. They do store pointers
  1235. * to file data extents, and those reference counts still get
  1236. * updated (along with back refs to the log tree).
  1237. */
  1238. leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
  1239. NULL, 0, 0, 0);
  1240. if (IS_ERR(leaf)) {
  1241. kfree(root);
  1242. return ERR_CAST(leaf);
  1243. }
  1244. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  1245. btrfs_set_header_bytenr(leaf, leaf->start);
  1246. btrfs_set_header_generation(leaf, trans->transid);
  1247. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  1248. btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID);
  1249. root->node = leaf;
  1250. write_extent_buffer(root->node, root->fs_info->fsid,
  1251. btrfs_header_fsid(), BTRFS_FSID_SIZE);
  1252. btrfs_mark_buffer_dirty(root->node);
  1253. btrfs_tree_unlock(root->node);
  1254. return root;
  1255. }
  1256. int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
  1257. struct btrfs_fs_info *fs_info)
  1258. {
  1259. struct btrfs_root *log_root;
  1260. log_root = alloc_log_tree(trans, fs_info);
  1261. if (IS_ERR(log_root))
  1262. return PTR_ERR(log_root);
  1263. WARN_ON(fs_info->log_root_tree);
  1264. fs_info->log_root_tree = log_root;
  1265. return 0;
  1266. }
  1267. int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
  1268. struct btrfs_root *root)
  1269. {
  1270. struct btrfs_root *log_root;
  1271. struct btrfs_inode_item *inode_item;
  1272. log_root = alloc_log_tree(trans, root->fs_info);
  1273. if (IS_ERR(log_root))
  1274. return PTR_ERR(log_root);
  1275. log_root->last_trans = trans->transid;
  1276. log_root->root_key.offset = root->root_key.objectid;
  1277. inode_item = &log_root->root_item.inode;
  1278. btrfs_set_stack_inode_generation(inode_item, 1);
  1279. btrfs_set_stack_inode_size(inode_item, 3);
  1280. btrfs_set_stack_inode_nlink(inode_item, 1);
  1281. btrfs_set_stack_inode_nbytes(inode_item, root->nodesize);
  1282. btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
  1283. btrfs_set_root_node(&log_root->root_item, log_root->node);
  1284. WARN_ON(root->log_root);
  1285. root->log_root = log_root;
  1286. root->log_transid = 0;
  1287. root->log_transid_committed = -1;
  1288. root->last_log_commit = 0;
  1289. return 0;
  1290. }
  1291. static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
  1292. struct btrfs_key *key)
  1293. {
  1294. struct btrfs_root *root;
  1295. struct btrfs_fs_info *fs_info = tree_root->fs_info;
  1296. struct btrfs_path *path;
  1297. u64 generation;
  1298. int ret;
  1299. path = btrfs_alloc_path();
  1300. if (!path)
  1301. return ERR_PTR(-ENOMEM);
  1302. root = btrfs_alloc_root(fs_info);
  1303. if (!root) {
  1304. ret = -ENOMEM;
  1305. goto alloc_fail;
  1306. }
  1307. __setup_root(tree_root->nodesize, tree_root->sectorsize,
  1308. tree_root->stripesize, root, fs_info, key->objectid);
  1309. ret = btrfs_find_root(tree_root, key, path,
  1310. &root->root_item, &root->root_key);
  1311. if (ret) {
  1312. if (ret > 0)
  1313. ret = -ENOENT;
  1314. goto find_fail;
  1315. }
  1316. generation = btrfs_root_generation(&root->root_item);
  1317. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  1318. generation);
  1319. if (!root->node) {
  1320. ret = -ENOMEM;
  1321. goto find_fail;
  1322. } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
  1323. ret = -EIO;
  1324. goto read_fail;
  1325. }
  1326. root->commit_root = btrfs_root_node(root);
  1327. out:
  1328. btrfs_free_path(path);
  1329. return root;
  1330. read_fail:
  1331. free_extent_buffer(root->node);
  1332. find_fail:
  1333. kfree(root);
  1334. alloc_fail:
  1335. root = ERR_PTR(ret);
  1336. goto out;
  1337. }
  1338. struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,
  1339. struct btrfs_key *location)
  1340. {
  1341. struct btrfs_root *root;
  1342. root = btrfs_read_tree_root(tree_root, location);
  1343. if (IS_ERR(root))
  1344. return root;
  1345. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  1346. set_bit(BTRFS_ROOT_REF_COWS, &root->state);
  1347. btrfs_check_and_init_root_item(&root->root_item);
  1348. }
  1349. return root;
  1350. }
  1351. int btrfs_init_fs_root(struct btrfs_root *root)
  1352. {
  1353. int ret;
  1354. struct btrfs_subvolume_writers *writers;
  1355. root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS);
  1356. root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned),
  1357. GFP_NOFS);
  1358. if (!root->free_ino_pinned || !root->free_ino_ctl) {
  1359. ret = -ENOMEM;
  1360. goto fail;
  1361. }
  1362. writers = btrfs_alloc_subvolume_writers();
  1363. if (IS_ERR(writers)) {
  1364. ret = PTR_ERR(writers);
  1365. goto fail;
  1366. }
  1367. root->subv_writers = writers;
  1368. btrfs_init_free_ino_ctl(root);
  1369. spin_lock_init(&root->ino_cache_lock);
  1370. init_waitqueue_head(&root->ino_cache_wait);
  1371. ret = get_anon_bdev(&root->anon_dev);
  1372. if (ret)
  1373. goto free_writers;
  1374. return 0;
  1375. free_writers:
  1376. btrfs_free_subvolume_writers(root->subv_writers);
  1377. fail:
  1378. kfree(root->free_ino_ctl);
  1379. kfree(root->free_ino_pinned);
  1380. return ret;
  1381. }
  1382. static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  1383. u64 root_id)
  1384. {
  1385. struct btrfs_root *root;
  1386. spin_lock(&fs_info->fs_roots_radix_lock);
  1387. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  1388. (unsigned long)root_id);
  1389. spin_unlock(&fs_info->fs_roots_radix_lock);
  1390. return root;
  1391. }
  1392. int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
  1393. struct btrfs_root *root)
  1394. {
  1395. int ret;
  1396. ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
  1397. if (ret)
  1398. return ret;
  1399. spin_lock(&fs_info->fs_roots_radix_lock);
  1400. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  1401. (unsigned long)root->root_key.objectid,
  1402. root);
  1403. if (ret == 0)
  1404. set_bit(BTRFS_ROOT_IN_RADIX, &root->state);
  1405. spin_unlock(&fs_info->fs_roots_radix_lock);
  1406. radix_tree_preload_end();
  1407. return ret;
  1408. }
  1409. struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
  1410. struct btrfs_key *location,
  1411. bool check_ref)
  1412. {
  1413. struct btrfs_root *root;
  1414. int ret;
  1415. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  1416. return fs_info->tree_root;
  1417. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  1418. return fs_info->extent_root;
  1419. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  1420. return fs_info->chunk_root;
  1421. if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
  1422. return fs_info->dev_root;
  1423. if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
  1424. return fs_info->csum_root;
  1425. if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
  1426. return fs_info->quota_root ? fs_info->quota_root :
  1427. ERR_PTR(-ENOENT);
  1428. if (location->objectid == BTRFS_UUID_TREE_OBJECTID)
  1429. return fs_info->uuid_root ? fs_info->uuid_root :
  1430. ERR_PTR(-ENOENT);
  1431. again:
  1432. root = btrfs_lookup_fs_root(fs_info, location->objectid);
  1433. if (root) {
  1434. if (check_ref && btrfs_root_refs(&root->root_item) == 0)
  1435. return ERR_PTR(-ENOENT);
  1436. return root;
  1437. }
  1438. root = btrfs_read_fs_root(fs_info->tree_root, location);
  1439. if (IS_ERR(root))
  1440. return root;
  1441. if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
  1442. ret = -ENOENT;
  1443. goto fail;
  1444. }
  1445. ret = btrfs_init_fs_root(root);
  1446. if (ret)
  1447. goto fail;
  1448. ret = btrfs_find_item(fs_info->tree_root, NULL, BTRFS_ORPHAN_OBJECTID,
  1449. location->objectid, BTRFS_ORPHAN_ITEM_KEY, NULL);
  1450. if (ret < 0)
  1451. goto fail;
  1452. if (ret == 0)
  1453. set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
  1454. ret = btrfs_insert_fs_root(fs_info, root);
  1455. if (ret) {
  1456. if (ret == -EEXIST) {
  1457. free_fs_root(root);
  1458. goto again;
  1459. }
  1460. goto fail;
  1461. }
  1462. return root;
  1463. fail:
  1464. free_fs_root(root);
  1465. return ERR_PTR(ret);
  1466. }
  1467. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  1468. {
  1469. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  1470. int ret = 0;
  1471. struct btrfs_device *device;
  1472. struct backing_dev_info *bdi;
  1473. rcu_read_lock();
  1474. list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) {
  1475. if (!device->bdev)
  1476. continue;
  1477. bdi = blk_get_backing_dev_info(device->bdev);
  1478. if (bdi_congested(bdi, bdi_bits)) {
  1479. ret = 1;
  1480. break;
  1481. }
  1482. }
  1483. rcu_read_unlock();
  1484. return ret;
  1485. }
  1486. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  1487. {
  1488. int err;
  1489. bdi->capabilities = BDI_CAP_MAP_COPY;
  1490. err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY);
  1491. if (err)
  1492. return err;
  1493. bdi->ra_pages = default_backing_dev_info.ra_pages;
  1494. bdi->congested_fn = btrfs_congested_fn;
  1495. bdi->congested_data = info;
  1496. return 0;
  1497. }
  1498. /*
  1499. * called by the kthread helper functions to finally call the bio end_io
  1500. * functions. This is where read checksum verification actually happens
  1501. */
  1502. static void end_workqueue_fn(struct btrfs_work *work)
  1503. {
  1504. struct bio *bio;
  1505. struct btrfs_end_io_wq *end_io_wq;
  1506. int error;
  1507. end_io_wq = container_of(work, struct btrfs_end_io_wq, work);
  1508. bio = end_io_wq->bio;
  1509. error = end_io_wq->error;
  1510. bio->bi_private = end_io_wq->private;
  1511. bio->bi_end_io = end_io_wq->end_io;
  1512. kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq);
  1513. bio_endio_nodec(bio, error);
  1514. }
  1515. static int cleaner_kthread(void *arg)
  1516. {
  1517. struct btrfs_root *root = arg;
  1518. int again;
  1519. do {
  1520. again = 0;
  1521. /* Make the cleaner go to sleep early. */
  1522. if (btrfs_need_cleaner_sleep(root))
  1523. goto sleep;
  1524. if (!mutex_trylock(&root->fs_info->cleaner_mutex))
  1525. goto sleep;
  1526. /*
  1527. * Avoid the problem that we change the status of the fs
  1528. * during the above check and trylock.
  1529. */
  1530. if (btrfs_need_cleaner_sleep(root)) {
  1531. mutex_unlock(&root->fs_info->cleaner_mutex);
  1532. goto sleep;
  1533. }
  1534. btrfs_run_delayed_iputs(root);
  1535. btrfs_delete_unused_bgs(root->fs_info);
  1536. again = btrfs_clean_one_deleted_snapshot(root);
  1537. mutex_unlock(&root->fs_info->cleaner_mutex);
  1538. /*
  1539. * The defragger has dealt with the R/O remount and umount,
  1540. * needn't do anything special here.
  1541. */
  1542. btrfs_run_defrag_inodes(root->fs_info);
  1543. sleep:
  1544. if (!try_to_freeze() && !again) {
  1545. set_current_state(TASK_INTERRUPTIBLE);
  1546. if (!kthread_should_stop())
  1547. schedule();
  1548. __set_current_state(TASK_RUNNING);
  1549. }
  1550. } while (!kthread_should_stop());
  1551. return 0;
  1552. }
  1553. static int transaction_kthread(void *arg)
  1554. {
  1555. struct btrfs_root *root = arg;
  1556. struct btrfs_trans_handle *trans;
  1557. struct btrfs_transaction *cur;
  1558. u64 transid;
  1559. unsigned long now;
  1560. unsigned long delay;
  1561. bool cannot_commit;
  1562. do {
  1563. cannot_commit = false;
  1564. delay = HZ * root->fs_info->commit_interval;
  1565. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  1566. spin_lock(&root->fs_info->trans_lock);
  1567. cur = root->fs_info->running_transaction;
  1568. if (!cur) {
  1569. spin_unlock(&root->fs_info->trans_lock);
  1570. goto sleep;
  1571. }
  1572. now = get_seconds();
  1573. if (cur->state < TRANS_STATE_BLOCKED &&
  1574. (now < cur->start_time ||
  1575. now - cur->start_time < root->fs_info->commit_interval)) {
  1576. spin_unlock(&root->fs_info->trans_lock);
  1577. delay = HZ * 5;
  1578. goto sleep;
  1579. }
  1580. transid = cur->transid;
  1581. spin_unlock(&root->fs_info->trans_lock);
  1582. /* If the file system is aborted, this will always fail. */
  1583. trans = btrfs_attach_transaction(root);
  1584. if (IS_ERR(trans)) {
  1585. if (PTR_ERR(trans) != -ENOENT)
  1586. cannot_commit = true;
  1587. goto sleep;
  1588. }
  1589. if (transid == trans->transid) {
  1590. btrfs_commit_transaction(trans, root);
  1591. } else {
  1592. btrfs_end_transaction(trans, root);
  1593. }
  1594. sleep:
  1595. wake_up_process(root->fs_info->cleaner_kthread);
  1596. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  1597. if (unlikely(test_bit(BTRFS_FS_STATE_ERROR,
  1598. &root->fs_info->fs_state)))
  1599. btrfs_cleanup_transaction(root);
  1600. if (!try_to_freeze()) {
  1601. set_current_state(TASK_INTERRUPTIBLE);
  1602. if (!kthread_should_stop() &&
  1603. (!btrfs_transaction_blocked(root->fs_info) ||
  1604. cannot_commit))
  1605. schedule_timeout(delay);
  1606. __set_current_state(TASK_RUNNING);
  1607. }
  1608. } while (!kthread_should_stop());
  1609. return 0;
  1610. }
  1611. /*
  1612. * this will find the highest generation in the array of
  1613. * root backups. The index of the highest array is returned,
  1614. * or -1 if we can't find anything.
  1615. *
  1616. * We check to make sure the array is valid by comparing the
  1617. * generation of the latest root in the array with the generation
  1618. * in the super block. If they don't match we pitch it.
  1619. */
  1620. static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen)
  1621. {
  1622. u64 cur;
  1623. int newest_index = -1;
  1624. struct btrfs_root_backup *root_backup;
  1625. int i;
  1626. for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
  1627. root_backup = info->super_copy->super_roots + i;
  1628. cur = btrfs_backup_tree_root_gen(root_backup);
  1629. if (cur == newest_gen)
  1630. newest_index = i;
  1631. }
  1632. /* check to see if we actually wrapped around */
  1633. if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) {
  1634. root_backup = info->super_copy->super_roots;
  1635. cur = btrfs_backup_tree_root_gen(root_backup);
  1636. if (cur == newest_gen)
  1637. newest_index = 0;
  1638. }
  1639. return newest_index;
  1640. }
  1641. /*
  1642. * find the oldest backup so we know where to store new entries
  1643. * in the backup array. This will set the backup_root_index
  1644. * field in the fs_info struct
  1645. */
  1646. static void find_oldest_super_backup(struct btrfs_fs_info *info,
  1647. u64 newest_gen)
  1648. {
  1649. int newest_index = -1;
  1650. newest_index = find_newest_super_backup(info, newest_gen);
  1651. /* if there was garbage in there, just move along */
  1652. if (newest_index == -1) {
  1653. info->backup_root_index = 0;
  1654. } else {
  1655. info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS;
  1656. }
  1657. }
  1658. /*
  1659. * copy all the root pointers into the super backup array.
  1660. * this will bump the backup pointer by one when it is
  1661. * done
  1662. */
  1663. static void backup_super_roots(struct btrfs_fs_info *info)
  1664. {
  1665. int next_backup;
  1666. struct btrfs_root_backup *root_backup;
  1667. int last_backup;
  1668. next_backup = info->backup_root_index;
  1669. last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) %
  1670. BTRFS_NUM_BACKUP_ROOTS;
  1671. /*
  1672. * just overwrite the last backup if we're at the same generation
  1673. * this happens only at umount
  1674. */
  1675. root_backup = info->super_for_commit->super_roots + last_backup;
  1676. if (btrfs_backup_tree_root_gen(root_backup) ==
  1677. btrfs_header_generation(info->tree_root->node))
  1678. next_backup = last_backup;
  1679. root_backup = info->super_for_commit->super_roots + next_backup;
  1680. /*
  1681. * make sure all of our padding and empty slots get zero filled
  1682. * regardless of which ones we use today
  1683. */
  1684. memset(root_backup, 0, sizeof(*root_backup));
  1685. info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS;
  1686. btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start);
  1687. btrfs_set_backup_tree_root_gen(root_backup,
  1688. btrfs_header_generation(info->tree_root->node));
  1689. btrfs_set_backup_tree_root_level(root_backup,
  1690. btrfs_header_level(info->tree_root->node));
  1691. btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start);
  1692. btrfs_set_backup_chunk_root_gen(root_backup,
  1693. btrfs_header_generation(info->chunk_root->node));
  1694. btrfs_set_backup_chunk_root_level(root_backup,
  1695. btrfs_header_level(info->chunk_root->node));
  1696. btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start);
  1697. btrfs_set_backup_extent_root_gen(root_backup,
  1698. btrfs_header_generation(info->extent_root->node));
  1699. btrfs_set_backup_extent_root_level(root_backup,
  1700. btrfs_header_level(info->extent_root->node));
  1701. /*
  1702. * we might commit during log recovery, which happens before we set
  1703. * the fs_root. Make sure it is valid before we fill it in.
  1704. */
  1705. if (info->fs_root && info->fs_root->node) {
  1706. btrfs_set_backup_fs_root(root_backup,
  1707. info->fs_root->node->start);
  1708. btrfs_set_backup_fs_root_gen(root_backup,
  1709. btrfs_header_generation(info->fs_root->node));
  1710. btrfs_set_backup_fs_root_level(root_backup,
  1711. btrfs_header_level(info->fs_root->node));
  1712. }
  1713. btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start);
  1714. btrfs_set_backup_dev_root_gen(root_backup,
  1715. btrfs_header_generation(info->dev_root->node));
  1716. btrfs_set_backup_dev_root_level(root_backup,
  1717. btrfs_header_level(info->dev_root->node));
  1718. btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start);
  1719. btrfs_set_backup_csum_root_gen(root_backup,
  1720. btrfs_header_generation(info->csum_root->node));
  1721. btrfs_set_backup_csum_root_level(root_backup,
  1722. btrfs_header_level(info->csum_root->node));
  1723. btrfs_set_backup_total_bytes(root_backup,
  1724. btrfs_super_total_bytes(info->super_copy));
  1725. btrfs_set_backup_bytes_used(root_backup,
  1726. btrfs_super_bytes_used(info->super_copy));
  1727. btrfs_set_backup_num_devices(root_backup,
  1728. btrfs_super_num_devices(info->super_copy));
  1729. /*
  1730. * if we don't copy this out to the super_copy, it won't get remembered
  1731. * for the next commit
  1732. */
  1733. memcpy(&info->super_copy->super_roots,
  1734. &info->super_for_commit->super_roots,
  1735. sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS);
  1736. }
  1737. /*
  1738. * this copies info out of the root backup array and back into
  1739. * the in-memory super block. It is meant to help iterate through
  1740. * the array, so you send it the number of backups you've already
  1741. * tried and the last backup index you used.
  1742. *
  1743. * this returns -1 when it has tried all the backups
  1744. */
  1745. static noinline int next_root_backup(struct btrfs_fs_info *info,
  1746. struct btrfs_super_block *super,
  1747. int *num_backups_tried, int *backup_index)
  1748. {
  1749. struct btrfs_root_backup *root_backup;
  1750. int newest = *backup_index;
  1751. if (*num_backups_tried == 0) {
  1752. u64 gen = btrfs_super_generation(super);
  1753. newest = find_newest_super_backup(info, gen);
  1754. if (newest == -1)
  1755. return -1;
  1756. *backup_index = newest;
  1757. *num_backups_tried = 1;
  1758. } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) {
  1759. /* we've tried all the backups, all done */
  1760. return -1;
  1761. } else {
  1762. /* jump to the next oldest backup */
  1763. newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) %
  1764. BTRFS_NUM_BACKUP_ROOTS;
  1765. *backup_index = newest;
  1766. *num_backups_tried += 1;
  1767. }
  1768. root_backup = super->super_roots + newest;
  1769. btrfs_set_super_generation(super,
  1770. btrfs_backup_tree_root_gen(root_backup));
  1771. btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup));
  1772. btrfs_set_super_root_level(super,
  1773. btrfs_backup_tree_root_level(root_backup));
  1774. btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup));
  1775. /*
  1776. * fixme: the total bytes and num_devices need to match or we should
  1777. * need a fsck
  1778. */
  1779. btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup));
  1780. btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup));
  1781. return 0;
  1782. }
  1783. /* helper to cleanup workers */
  1784. static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
  1785. {
  1786. btrfs_destroy_workqueue(fs_info->fixup_workers);
  1787. btrfs_destroy_workqueue(fs_info->delalloc_workers);
  1788. btrfs_destroy_workqueue(fs_info->workers);
  1789. btrfs_destroy_workqueue(fs_info->endio_workers);
  1790. btrfs_destroy_workqueue(fs_info->endio_meta_workers);
  1791. btrfs_destroy_workqueue(fs_info->endio_raid56_workers);
  1792. btrfs_destroy_workqueue(fs_info->endio_repair_workers);
  1793. btrfs_destroy_workqueue(fs_info->rmw_workers);
  1794. btrfs_destroy_workqueue(fs_info->endio_meta_write_workers);
  1795. btrfs_destroy_workqueue(fs_info->endio_write_workers);
  1796. btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
  1797. btrfs_destroy_workqueue(fs_info->submit_workers);
  1798. btrfs_destroy_workqueue(fs_info->delayed_workers);
  1799. btrfs_destroy_workqueue(fs_info->caching_workers);
  1800. btrfs_destroy_workqueue(fs_info->readahead_workers);
  1801. btrfs_destroy_workqueue(fs_info->flush_workers);
  1802. btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers);
  1803. btrfs_destroy_workqueue(fs_info->extent_workers);
  1804. }
  1805. static void free_root_extent_buffers(struct btrfs_root *root)
  1806. {
  1807. if (root) {
  1808. free_extent_buffer(root->node);
  1809. free_extent_buffer(root->commit_root);
  1810. root->node = NULL;
  1811. root->commit_root = NULL;
  1812. }
  1813. }
  1814. /* helper to cleanup tree roots */
  1815. static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root)
  1816. {
  1817. free_root_extent_buffers(info->tree_root);
  1818. free_root_extent_buffers(info->dev_root);
  1819. free_root_extent_buffers(info->extent_root);
  1820. free_root_extent_buffers(info->csum_root);
  1821. free_root_extent_buffers(info->quota_root);
  1822. free_root_extent_buffers(info->uuid_root);
  1823. if (chunk_root)
  1824. free_root_extent_buffers(info->chunk_root);
  1825. }
  1826. void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
  1827. {
  1828. int ret;
  1829. struct btrfs_root *gang[8];
  1830. int i;
  1831. while (!list_empty(&fs_info->dead_roots)) {
  1832. gang[0] = list_entry(fs_info->dead_roots.next,
  1833. struct btrfs_root, root_list);
  1834. list_del(&gang[0]->root_list);
  1835. if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) {
  1836. btrfs_drop_and_free_fs_root(fs_info, gang[0]);
  1837. } else {
  1838. free_extent_buffer(gang[0]->node);
  1839. free_extent_buffer(gang[0]->commit_root);
  1840. btrfs_put_fs_root(gang[0]);
  1841. }
  1842. }
  1843. while (1) {
  1844. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1845. (void **)gang, 0,
  1846. ARRAY_SIZE(gang));
  1847. if (!ret)
  1848. break;
  1849. for (i = 0; i < ret; i++)
  1850. btrfs_drop_and_free_fs_root(fs_info, gang[i]);
  1851. }
  1852. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  1853. btrfs_free_log_root_tree(NULL, fs_info);
  1854. btrfs_destroy_pinned_extent(fs_info->tree_root,
  1855. fs_info->pinned_extents);
  1856. }
  1857. }
  1858. int open_ctree(struct super_block *sb,
  1859. struct btrfs_fs_devices *fs_devices,
  1860. char *options)
  1861. {
  1862. u32 sectorsize;
  1863. u32 nodesize;
  1864. u32 stripesize;
  1865. u64 generation;
  1866. u64 features;
  1867. struct btrfs_key location;
  1868. struct buffer_head *bh;
  1869. struct btrfs_super_block *disk_super;
  1870. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1871. struct btrfs_root *tree_root;
  1872. struct btrfs_root *extent_root;
  1873. struct btrfs_root *csum_root;
  1874. struct btrfs_root *chunk_root;
  1875. struct btrfs_root *dev_root;
  1876. struct btrfs_root *quota_root;
  1877. struct btrfs_root *uuid_root;
  1878. struct btrfs_root *log_tree_root;
  1879. int ret;
  1880. int err = -EINVAL;
  1881. int num_backups_tried = 0;
  1882. int backup_index = 0;
  1883. int max_active;
  1884. int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND;
  1885. bool create_uuid_tree;
  1886. bool check_uuid_tree;
  1887. tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info);
  1888. chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info);
  1889. if (!tree_root || !chunk_root) {
  1890. err = -ENOMEM;
  1891. goto fail;
  1892. }
  1893. ret = init_srcu_struct(&fs_info->subvol_srcu);
  1894. if (ret) {
  1895. err = ret;
  1896. goto fail;
  1897. }
  1898. ret = setup_bdi(fs_info, &fs_info->bdi);
  1899. if (ret) {
  1900. err = ret;
  1901. goto fail_srcu;
  1902. }
  1903. ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL);
  1904. if (ret) {
  1905. err = ret;
  1906. goto fail_bdi;
  1907. }
  1908. fs_info->dirty_metadata_batch = PAGE_CACHE_SIZE *
  1909. (1 + ilog2(nr_cpu_ids));
  1910. ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL);
  1911. if (ret) {
  1912. err = ret;
  1913. goto fail_dirty_metadata_bytes;
  1914. }
  1915. ret = percpu_counter_init(&fs_info->bio_counter, 0, GFP_KERNEL);
  1916. if (ret) {
  1917. err = ret;
  1918. goto fail_delalloc_bytes;
  1919. }
  1920. fs_info->btree_inode = new_inode(sb);
  1921. if (!fs_info->btree_inode) {
  1922. err = -ENOMEM;
  1923. goto fail_bio_counter;
  1924. }
  1925. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  1926. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
  1927. INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
  1928. INIT_LIST_HEAD(&fs_info->trans_list);
  1929. INIT_LIST_HEAD(&fs_info->dead_roots);
  1930. INIT_LIST_HEAD(&fs_info->delayed_iputs);
  1931. INIT_LIST_HEAD(&fs_info->delalloc_roots);
  1932. INIT_LIST_HEAD(&fs_info->caching_block_groups);
  1933. spin_lock_init(&fs_info->delalloc_root_lock);
  1934. spin_lock_init(&fs_info->trans_lock);
  1935. spin_lock_init(&fs_info->fs_roots_radix_lock);
  1936. spin_lock_init(&fs_info->delayed_iput_lock);
  1937. spin_lock_init(&fs_info->defrag_inodes_lock);
  1938. spin_lock_init(&fs_info->free_chunk_lock);
  1939. spin_lock_init(&fs_info->tree_mod_seq_lock);
  1940. spin_lock_init(&fs_info->super_lock);
  1941. spin_lock_init(&fs_info->qgroup_op_lock);
  1942. spin_lock_init(&fs_info->buffer_lock);
  1943. spin_lock_init(&fs_info->unused_bgs_lock);
  1944. rwlock_init(&fs_info->tree_mod_log_lock);
  1945. mutex_init(&fs_info->reloc_mutex);
  1946. mutex_init(&fs_info->delalloc_root_mutex);
  1947. seqlock_init(&fs_info->profiles_lock);
  1948. init_completion(&fs_info->kobj_unregister);
  1949. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  1950. INIT_LIST_HEAD(&fs_info->space_info);
  1951. INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
  1952. INIT_LIST_HEAD(&fs_info->unused_bgs);
  1953. btrfs_mapping_init(&fs_info->mapping_tree);
  1954. btrfs_init_block_rsv(&fs_info->global_block_rsv,
  1955. BTRFS_BLOCK_RSV_GLOBAL);
  1956. btrfs_init_block_rsv(&fs_info->delalloc_block_rsv,
  1957. BTRFS_BLOCK_RSV_DELALLOC);
  1958. btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
  1959. btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
  1960. btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
  1961. btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
  1962. BTRFS_BLOCK_RSV_DELOPS);
  1963. atomic_set(&fs_info->nr_async_submits, 0);
  1964. atomic_set(&fs_info->async_delalloc_pages, 0);
  1965. atomic_set(&fs_info->async_submit_draining, 0);
  1966. atomic_set(&fs_info->nr_async_bios, 0);
  1967. atomic_set(&fs_info->defrag_running, 0);
  1968. atomic_set(&fs_info->qgroup_op_seq, 0);
  1969. atomic64_set(&fs_info->tree_mod_seq, 0);
  1970. fs_info->sb = sb;
  1971. fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
  1972. fs_info->metadata_ratio = 0;
  1973. fs_info->defrag_inodes = RB_ROOT;
  1974. fs_info->free_chunk_space = 0;
  1975. fs_info->tree_mod_log = RB_ROOT;
  1976. fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
  1977. fs_info->avg_delayed_ref_runtime = div64_u64(NSEC_PER_SEC, 64);
  1978. /* readahead state */
  1979. INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT);
  1980. spin_lock_init(&fs_info->reada_lock);
  1981. fs_info->thread_pool_size = min_t(unsigned long,
  1982. num_online_cpus() + 2, 8);
  1983. INIT_LIST_HEAD(&fs_info->ordered_roots);
  1984. spin_lock_init(&fs_info->ordered_root_lock);
  1985. fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root),
  1986. GFP_NOFS);
  1987. if (!fs_info->delayed_root) {
  1988. err = -ENOMEM;
  1989. goto fail_iput;
  1990. }
  1991. btrfs_init_delayed_root(fs_info->delayed_root);
  1992. mutex_init(&fs_info->scrub_lock);
  1993. atomic_set(&fs_info->scrubs_running, 0);
  1994. atomic_set(&fs_info->scrub_pause_req, 0);
  1995. atomic_set(&fs_info->scrubs_paused, 0);
  1996. atomic_set(&fs_info->scrub_cancel_req, 0);
  1997. init_waitqueue_head(&fs_info->replace_wait);
  1998. init_waitqueue_head(&fs_info->scrub_pause_wait);
  1999. fs_info->scrub_workers_refcnt = 0;
  2000. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  2001. fs_info->check_integrity_print_mask = 0;
  2002. #endif
  2003. spin_lock_init(&fs_info->balance_lock);
  2004. mutex_init(&fs_info->balance_mutex);
  2005. atomic_set(&fs_info->balance_running, 0);
  2006. atomic_set(&fs_info->balance_pause_req, 0);
  2007. atomic_set(&fs_info->balance_cancel_req, 0);
  2008. fs_info->balance_ctl = NULL;
  2009. init_waitqueue_head(&fs_info->balance_wait_q);
  2010. btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work);
  2011. sb->s_blocksize = 4096;
  2012. sb->s_blocksize_bits = blksize_bits(4096);
  2013. sb->s_bdi = &fs_info->bdi;
  2014. fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
  2015. set_nlink(fs_info->btree_inode, 1);
  2016. /*
  2017. * we set the i_size on the btree inode to the max possible int.
  2018. * the real end of the address space is determined by all of
  2019. * the devices in the system
  2020. */
  2021. fs_info->btree_inode->i_size = OFFSET_MAX;
  2022. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  2023. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  2024. RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node);
  2025. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  2026. fs_info->btree_inode->i_mapping);
  2027. BTRFS_I(fs_info->btree_inode)->io_tree.track_uptodate = 0;
  2028. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree);
  2029. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  2030. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  2031. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  2032. sizeof(struct btrfs_key));
  2033. set_bit(BTRFS_INODE_DUMMY,
  2034. &BTRFS_I(fs_info->btree_inode)->runtime_flags);
  2035. btrfs_insert_inode_hash(fs_info->btree_inode);
  2036. spin_lock_init(&fs_info->block_group_cache_lock);
  2037. fs_info->block_group_cache_tree = RB_ROOT;
  2038. fs_info->first_logical_byte = (u64)-1;
  2039. extent_io_tree_init(&fs_info->freed_extents[0],
  2040. fs_info->btree_inode->i_mapping);
  2041. extent_io_tree_init(&fs_info->freed_extents[1],
  2042. fs_info->btree_inode->i_mapping);
  2043. fs_info->pinned_extents = &fs_info->freed_extents[0];
  2044. fs_info->do_barriers = 1;
  2045. mutex_init(&fs_info->ordered_operations_mutex);
  2046. mutex_init(&fs_info->ordered_extent_flush_mutex);
  2047. mutex_init(&fs_info->tree_log_mutex);
  2048. mutex_init(&fs_info->chunk_mutex);
  2049. mutex_init(&fs_info->transaction_kthread_mutex);
  2050. mutex_init(&fs_info->cleaner_mutex);
  2051. mutex_init(&fs_info->volume_mutex);
  2052. init_rwsem(&fs_info->commit_root_sem);
  2053. init_rwsem(&fs_info->cleanup_work_sem);
  2054. init_rwsem(&fs_info->subvol_sem);
  2055. sema_init(&fs_info->uuid_tree_rescan_sem, 1);
  2056. fs_info->dev_replace.lock_owner = 0;
  2057. atomic_set(&fs_info->dev_replace.nesting_level, 0);
  2058. mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
  2059. mutex_init(&fs_info->dev_replace.lock_management_lock);
  2060. mutex_init(&fs_info->dev_replace.lock);
  2061. spin_lock_init(&fs_info->qgroup_lock);
  2062. mutex_init(&fs_info->qgroup_ioctl_lock);
  2063. fs_info->qgroup_tree = RB_ROOT;
  2064. fs_info->qgroup_op_tree = RB_ROOT;
  2065. INIT_LIST_HEAD(&fs_info->dirty_qgroups);
  2066. fs_info->qgroup_seq = 1;
  2067. fs_info->quota_enabled = 0;
  2068. fs_info->pending_quota_state = 0;
  2069. fs_info->qgroup_ulist = NULL;
  2070. mutex_init(&fs_info->qgroup_rescan_lock);
  2071. btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
  2072. btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
  2073. init_waitqueue_head(&fs_info->transaction_throttle);
  2074. init_waitqueue_head(&fs_info->transaction_wait);
  2075. init_waitqueue_head(&fs_info->transaction_blocked_wait);
  2076. init_waitqueue_head(&fs_info->async_submit_wait);
  2077. ret = btrfs_alloc_stripe_hash_table(fs_info);
  2078. if (ret) {
  2079. err = ret;
  2080. goto fail_alloc;
  2081. }
  2082. __setup_root(4096, 4096, 4096, tree_root,
  2083. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  2084. invalidate_bdev(fs_devices->latest_bdev);
  2085. /*
  2086. * Read super block and check the signature bytes only
  2087. */
  2088. bh = btrfs_read_dev_super(fs_devices->latest_bdev);
  2089. if (!bh) {
  2090. err = -EINVAL;
  2091. goto fail_alloc;
  2092. }
  2093. /*
  2094. * We want to check superblock checksum, the type is stored inside.
  2095. * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
  2096. */
  2097. if (btrfs_check_super_csum(bh->b_data)) {
  2098. printk(KERN_ERR "BTRFS: superblock checksum mismatch\n");
  2099. err = -EINVAL;
  2100. goto fail_alloc;
  2101. }
  2102. /*
  2103. * super_copy is zeroed at allocation time and we never touch the
  2104. * following bytes up to INFO_SIZE, the checksum is calculated from
  2105. * the whole block of INFO_SIZE
  2106. */
  2107. memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy));
  2108. memcpy(fs_info->super_for_commit, fs_info->super_copy,
  2109. sizeof(*fs_info->super_for_commit));
  2110. brelse(bh);
  2111. memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
  2112. ret = btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY);
  2113. if (ret) {
  2114. printk(KERN_ERR "BTRFS: superblock contains fatal errors\n");
  2115. err = -EINVAL;
  2116. goto fail_alloc;
  2117. }
  2118. disk_super = fs_info->super_copy;
  2119. if (!btrfs_super_root(disk_super))
  2120. goto fail_alloc;
  2121. /* check FS state, whether FS is broken. */
  2122. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
  2123. set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
  2124. /*
  2125. * run through our array of backup supers and setup
  2126. * our ring pointer to the oldest one
  2127. */
  2128. generation = btrfs_super_generation(disk_super);
  2129. find_oldest_super_backup(fs_info, generation);
  2130. /*
  2131. * In the long term, we'll store the compression type in the super
  2132. * block, and it'll be used for per file compression control.
  2133. */
  2134. fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
  2135. ret = btrfs_parse_options(tree_root, options);
  2136. if (ret) {
  2137. err = ret;
  2138. goto fail_alloc;
  2139. }
  2140. features = btrfs_super_incompat_flags(disk_super) &
  2141. ~BTRFS_FEATURE_INCOMPAT_SUPP;
  2142. if (features) {
  2143. printk(KERN_ERR "BTRFS: couldn't mount because of "
  2144. "unsupported optional features (%Lx).\n",
  2145. features);
  2146. err = -EINVAL;
  2147. goto fail_alloc;
  2148. }
  2149. /*
  2150. * Leafsize and nodesize were always equal, this is only a sanity check.
  2151. */
  2152. if (le32_to_cpu(disk_super->__unused_leafsize) !=
  2153. btrfs_super_nodesize(disk_super)) {
  2154. printk(KERN_ERR "BTRFS: couldn't mount because metadata "
  2155. "blocksizes don't match. node %d leaf %d\n",
  2156. btrfs_super_nodesize(disk_super),
  2157. le32_to_cpu(disk_super->__unused_leafsize));
  2158. err = -EINVAL;
  2159. goto fail_alloc;
  2160. }
  2161. if (btrfs_super_nodesize(disk_super) > BTRFS_MAX_METADATA_BLOCKSIZE) {
  2162. printk(KERN_ERR "BTRFS: couldn't mount because metadata "
  2163. "blocksize (%d) was too large\n",
  2164. btrfs_super_nodesize(disk_super));
  2165. err = -EINVAL;
  2166. goto fail_alloc;
  2167. }
  2168. features = btrfs_super_incompat_flags(disk_super);
  2169. features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
  2170. if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
  2171. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  2172. if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)
  2173. printk(KERN_ERR "BTRFS: has skinny extents\n");
  2174. /*
  2175. * flag our filesystem as having big metadata blocks if
  2176. * they are bigger than the page size
  2177. */
  2178. if (btrfs_super_nodesize(disk_super) > PAGE_CACHE_SIZE) {
  2179. if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA))
  2180. printk(KERN_INFO "BTRFS: flagging fs with big metadata feature\n");
  2181. features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA;
  2182. }
  2183. nodesize = btrfs_super_nodesize(disk_super);
  2184. sectorsize = btrfs_super_sectorsize(disk_super);
  2185. stripesize = btrfs_super_stripesize(disk_super);
  2186. fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids));
  2187. fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids));
  2188. /*
  2189. * mixed block groups end up with duplicate but slightly offset
  2190. * extent buffers for the same range. It leads to corruptions
  2191. */
  2192. if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
  2193. (sectorsize != nodesize)) {
  2194. printk(KERN_WARNING "BTRFS: unequal leaf/node/sector sizes "
  2195. "are not allowed for mixed block groups on %s\n",
  2196. sb->s_id);
  2197. goto fail_alloc;
  2198. }
  2199. /*
  2200. * Needn't use the lock because there is no other task which will
  2201. * update the flag.
  2202. */
  2203. btrfs_set_super_incompat_flags(disk_super, features);
  2204. features = btrfs_super_compat_ro_flags(disk_super) &
  2205. ~BTRFS_FEATURE_COMPAT_RO_SUPP;
  2206. if (!(sb->s_flags & MS_RDONLY) && features) {
  2207. printk(KERN_ERR "BTRFS: couldn't mount RDWR because of "
  2208. "unsupported option features (%Lx).\n",
  2209. features);
  2210. err = -EINVAL;
  2211. goto fail_alloc;
  2212. }
  2213. max_active = fs_info->thread_pool_size;
  2214. fs_info->workers =
  2215. btrfs_alloc_workqueue("worker", flags | WQ_HIGHPRI,
  2216. max_active, 16);
  2217. fs_info->delalloc_workers =
  2218. btrfs_alloc_workqueue("delalloc", flags, max_active, 2);
  2219. fs_info->flush_workers =
  2220. btrfs_alloc_workqueue("flush_delalloc", flags, max_active, 0);
  2221. fs_info->caching_workers =
  2222. btrfs_alloc_workqueue("cache", flags, max_active, 0);
  2223. /*
  2224. * a higher idle thresh on the submit workers makes it much more
  2225. * likely that bios will be send down in a sane order to the
  2226. * devices
  2227. */
  2228. fs_info->submit_workers =
  2229. btrfs_alloc_workqueue("submit", flags,
  2230. min_t(u64, fs_devices->num_devices,
  2231. max_active), 64);
  2232. fs_info->fixup_workers =
  2233. btrfs_alloc_workqueue("fixup", flags, 1, 0);
  2234. /*
  2235. * endios are largely parallel and should have a very
  2236. * low idle thresh
  2237. */
  2238. fs_info->endio_workers =
  2239. btrfs_alloc_workqueue("endio", flags, max_active, 4);
  2240. fs_info->endio_meta_workers =
  2241. btrfs_alloc_workqueue("endio-meta", flags, max_active, 4);
  2242. fs_info->endio_meta_write_workers =
  2243. btrfs_alloc_workqueue("endio-meta-write", flags, max_active, 2);
  2244. fs_info->endio_raid56_workers =
  2245. btrfs_alloc_workqueue("endio-raid56", flags, max_active, 4);
  2246. fs_info->endio_repair_workers =
  2247. btrfs_alloc_workqueue("endio-repair", flags, 1, 0);
  2248. fs_info->rmw_workers =
  2249. btrfs_alloc_workqueue("rmw", flags, max_active, 2);
  2250. fs_info->endio_write_workers =
  2251. btrfs_alloc_workqueue("endio-write", flags, max_active, 2);
  2252. fs_info->endio_freespace_worker =
  2253. btrfs_alloc_workqueue("freespace-write", flags, max_active, 0);
  2254. fs_info->delayed_workers =
  2255. btrfs_alloc_workqueue("delayed-meta", flags, max_active, 0);
  2256. fs_info->readahead_workers =
  2257. btrfs_alloc_workqueue("readahead", flags, max_active, 2);
  2258. fs_info->qgroup_rescan_workers =
  2259. btrfs_alloc_workqueue("qgroup-rescan", flags, 1, 0);
  2260. fs_info->extent_workers =
  2261. btrfs_alloc_workqueue("extent-refs", flags,
  2262. min_t(u64, fs_devices->num_devices,
  2263. max_active), 8);
  2264. if (!(fs_info->workers && fs_info->delalloc_workers &&
  2265. fs_info->submit_workers && fs_info->flush_workers &&
  2266. fs_info->endio_workers && fs_info->endio_meta_workers &&
  2267. fs_info->endio_meta_write_workers &&
  2268. fs_info->endio_repair_workers &&
  2269. fs_info->endio_write_workers && fs_info->endio_raid56_workers &&
  2270. fs_info->endio_freespace_worker && fs_info->rmw_workers &&
  2271. fs_info->caching_workers && fs_info->readahead_workers &&
  2272. fs_info->fixup_workers && fs_info->delayed_workers &&
  2273. fs_info->extent_workers &&
  2274. fs_info->qgroup_rescan_workers)) {
  2275. err = -ENOMEM;
  2276. goto fail_sb_buffer;
  2277. }
  2278. fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
  2279. fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
  2280. 4 * 1024 * 1024 / PAGE_CACHE_SIZE);
  2281. tree_root->nodesize = nodesize;
  2282. tree_root->sectorsize = sectorsize;
  2283. tree_root->stripesize = stripesize;
  2284. sb->s_blocksize = sectorsize;
  2285. sb->s_blocksize_bits = blksize_bits(sectorsize);
  2286. if (btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
  2287. printk(KERN_INFO "BTRFS: valid FS not found on %s\n", sb->s_id);
  2288. goto fail_sb_buffer;
  2289. }
  2290. if (sectorsize != PAGE_SIZE) {
  2291. printk(KERN_WARNING "BTRFS: Incompatible sector size(%lu) "
  2292. "found on %s\n", (unsigned long)sectorsize, sb->s_id);
  2293. goto fail_sb_buffer;
  2294. }
  2295. mutex_lock(&fs_info->chunk_mutex);
  2296. ret = btrfs_read_sys_array(tree_root);
  2297. mutex_unlock(&fs_info->chunk_mutex);
  2298. if (ret) {
  2299. printk(KERN_WARNING "BTRFS: failed to read the system "
  2300. "array on %s\n", sb->s_id);
  2301. goto fail_sb_buffer;
  2302. }
  2303. generation = btrfs_super_chunk_root_generation(disk_super);
  2304. __setup_root(nodesize, sectorsize, stripesize, chunk_root,
  2305. fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  2306. chunk_root->node = read_tree_block(chunk_root,
  2307. btrfs_super_chunk_root(disk_super),
  2308. generation);
  2309. if (!chunk_root->node ||
  2310. !test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) {
  2311. printk(KERN_WARNING "BTRFS: failed to read chunk root on %s\n",
  2312. sb->s_id);
  2313. goto fail_tree_roots;
  2314. }
  2315. btrfs_set_root_node(&chunk_root->root_item, chunk_root->node);
  2316. chunk_root->commit_root = btrfs_root_node(chunk_root);
  2317. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  2318. btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE);
  2319. ret = btrfs_read_chunk_tree(chunk_root);
  2320. if (ret) {
  2321. printk(KERN_WARNING "BTRFS: failed to read chunk tree on %s\n",
  2322. sb->s_id);
  2323. goto fail_tree_roots;
  2324. }
  2325. /*
  2326. * keep the device that is marked to be the target device for the
  2327. * dev_replace procedure
  2328. */
  2329. btrfs_close_extra_devices(fs_info, fs_devices, 0);
  2330. if (!fs_devices->latest_bdev) {
  2331. printk(KERN_CRIT "BTRFS: failed to read devices on %s\n",
  2332. sb->s_id);
  2333. goto fail_tree_roots;
  2334. }
  2335. retry_root_backup:
  2336. generation = btrfs_super_generation(disk_super);
  2337. tree_root->node = read_tree_block(tree_root,
  2338. btrfs_super_root(disk_super),
  2339. generation);
  2340. if (!tree_root->node ||
  2341. !test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) {
  2342. printk(KERN_WARNING "BTRFS: failed to read tree root on %s\n",
  2343. sb->s_id);
  2344. goto recovery_tree_root;
  2345. }
  2346. btrfs_set_root_node(&tree_root->root_item, tree_root->node);
  2347. tree_root->commit_root = btrfs_root_node(tree_root);
  2348. btrfs_set_root_refs(&tree_root->root_item, 1);
  2349. location.objectid = BTRFS_EXTENT_TREE_OBJECTID;
  2350. location.type = BTRFS_ROOT_ITEM_KEY;
  2351. location.offset = 0;
  2352. extent_root = btrfs_read_tree_root(tree_root, &location);
  2353. if (IS_ERR(extent_root)) {
  2354. ret = PTR_ERR(extent_root);
  2355. goto recovery_tree_root;
  2356. }
  2357. set_bit(BTRFS_ROOT_TRACK_DIRTY, &extent_root->state);
  2358. fs_info->extent_root = extent_root;
  2359. location.objectid = BTRFS_DEV_TREE_OBJECTID;
  2360. dev_root = btrfs_read_tree_root(tree_root, &location);
  2361. if (IS_ERR(dev_root)) {
  2362. ret = PTR_ERR(dev_root);
  2363. goto recovery_tree_root;
  2364. }
  2365. set_bit(BTRFS_ROOT_TRACK_DIRTY, &dev_root->state);
  2366. fs_info->dev_root = dev_root;
  2367. btrfs_init_devices_late(fs_info);
  2368. location.objectid = BTRFS_CSUM_TREE_OBJECTID;
  2369. csum_root = btrfs_read_tree_root(tree_root, &location);
  2370. if (IS_ERR(csum_root)) {
  2371. ret = PTR_ERR(csum_root);
  2372. goto recovery_tree_root;
  2373. }
  2374. set_bit(BTRFS_ROOT_TRACK_DIRTY, &csum_root->state);
  2375. fs_info->csum_root = csum_root;
  2376. location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
  2377. quota_root = btrfs_read_tree_root(tree_root, &location);
  2378. if (!IS_ERR(quota_root)) {
  2379. set_bit(BTRFS_ROOT_TRACK_DIRTY, &quota_root->state);
  2380. fs_info->quota_enabled = 1;
  2381. fs_info->pending_quota_state = 1;
  2382. fs_info->quota_root = quota_root;
  2383. }
  2384. location.objectid = BTRFS_UUID_TREE_OBJECTID;
  2385. uuid_root = btrfs_read_tree_root(tree_root, &location);
  2386. if (IS_ERR(uuid_root)) {
  2387. ret = PTR_ERR(uuid_root);
  2388. if (ret != -ENOENT)
  2389. goto recovery_tree_root;
  2390. create_uuid_tree = true;
  2391. check_uuid_tree = false;
  2392. } else {
  2393. set_bit(BTRFS_ROOT_TRACK_DIRTY, &uuid_root->state);
  2394. fs_info->uuid_root = uuid_root;
  2395. create_uuid_tree = false;
  2396. check_uuid_tree =
  2397. generation != btrfs_super_uuid_tree_generation(disk_super);
  2398. }
  2399. fs_info->generation = generation;
  2400. fs_info->last_trans_committed = generation;
  2401. ret = btrfs_recover_balance(fs_info);
  2402. if (ret) {
  2403. printk(KERN_WARNING "BTRFS: failed to recover balance\n");
  2404. goto fail_block_groups;
  2405. }
  2406. ret = btrfs_init_dev_stats(fs_info);
  2407. if (ret) {
  2408. printk(KERN_ERR "BTRFS: failed to init dev_stats: %d\n",
  2409. ret);
  2410. goto fail_block_groups;
  2411. }
  2412. ret = btrfs_init_dev_replace(fs_info);
  2413. if (ret) {
  2414. pr_err("BTRFS: failed to init dev_replace: %d\n", ret);
  2415. goto fail_block_groups;
  2416. }
  2417. btrfs_close_extra_devices(fs_info, fs_devices, 1);
  2418. ret = btrfs_sysfs_add_one(fs_info);
  2419. if (ret) {
  2420. pr_err("BTRFS: failed to init sysfs interface: %d\n", ret);
  2421. goto fail_block_groups;
  2422. }
  2423. ret = btrfs_init_space_info(fs_info);
  2424. if (ret) {
  2425. printk(KERN_ERR "BTRFS: Failed to initial space info: %d\n", ret);
  2426. goto fail_sysfs;
  2427. }
  2428. ret = btrfs_read_block_groups(extent_root);
  2429. if (ret) {
  2430. printk(KERN_ERR "BTRFS: Failed to read block groups: %d\n", ret);
  2431. goto fail_sysfs;
  2432. }
  2433. fs_info->num_tolerated_disk_barrier_failures =
  2434. btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
  2435. if (fs_info->fs_devices->missing_devices >
  2436. fs_info->num_tolerated_disk_barrier_failures &&
  2437. !(sb->s_flags & MS_RDONLY)) {
  2438. printk(KERN_WARNING "BTRFS: "
  2439. "too many missing devices, writeable mount is not allowed\n");
  2440. goto fail_sysfs;
  2441. }
  2442. fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
  2443. "btrfs-cleaner");
  2444. if (IS_ERR(fs_info->cleaner_kthread))
  2445. goto fail_sysfs;
  2446. fs_info->transaction_kthread = kthread_run(transaction_kthread,
  2447. tree_root,
  2448. "btrfs-transaction");
  2449. if (IS_ERR(fs_info->transaction_kthread))
  2450. goto fail_cleaner;
  2451. if (!btrfs_test_opt(tree_root, SSD) &&
  2452. !btrfs_test_opt(tree_root, NOSSD) &&
  2453. !fs_info->fs_devices->rotating) {
  2454. printk(KERN_INFO "BTRFS: detected SSD devices, enabling SSD "
  2455. "mode\n");
  2456. btrfs_set_opt(fs_info->mount_opt, SSD);
  2457. }
  2458. /* Set the real inode map cache flag */
  2459. if (btrfs_test_opt(tree_root, CHANGE_INODE_CACHE))
  2460. btrfs_set_opt(tree_root->fs_info->mount_opt, INODE_MAP_CACHE);
  2461. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  2462. if (btrfs_test_opt(tree_root, CHECK_INTEGRITY)) {
  2463. ret = btrfsic_mount(tree_root, fs_devices,
  2464. btrfs_test_opt(tree_root,
  2465. CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ?
  2466. 1 : 0,
  2467. fs_info->check_integrity_print_mask);
  2468. if (ret)
  2469. printk(KERN_WARNING "BTRFS: failed to initialize"
  2470. " integrity check module %s\n", sb->s_id);
  2471. }
  2472. #endif
  2473. ret = btrfs_read_qgroup_config(fs_info);
  2474. if (ret)
  2475. goto fail_trans_kthread;
  2476. /* do not make disk changes in broken FS */
  2477. if (btrfs_super_log_root(disk_super) != 0) {
  2478. u64 bytenr = btrfs_super_log_root(disk_super);
  2479. if (fs_devices->rw_devices == 0) {
  2480. printk(KERN_WARNING "BTRFS: log replay required "
  2481. "on RO media\n");
  2482. err = -EIO;
  2483. goto fail_qgroup;
  2484. }
  2485. log_tree_root = btrfs_alloc_root(fs_info);
  2486. if (!log_tree_root) {
  2487. err = -ENOMEM;
  2488. goto fail_qgroup;
  2489. }
  2490. __setup_root(nodesize, sectorsize, stripesize,
  2491. log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  2492. log_tree_root->node = read_tree_block(tree_root, bytenr,
  2493. generation + 1);
  2494. if (!log_tree_root->node ||
  2495. !extent_buffer_uptodate(log_tree_root->node)) {
  2496. printk(KERN_ERR "BTRFS: failed to read log tree\n");
  2497. free_extent_buffer(log_tree_root->node);
  2498. kfree(log_tree_root);
  2499. goto fail_qgroup;
  2500. }
  2501. /* returns with log_tree_root freed on success */
  2502. ret = btrfs_recover_log_trees(log_tree_root);
  2503. if (ret) {
  2504. btrfs_error(tree_root->fs_info, ret,
  2505. "Failed to recover log tree");
  2506. free_extent_buffer(log_tree_root->node);
  2507. kfree(log_tree_root);
  2508. goto fail_qgroup;
  2509. }
  2510. if (sb->s_flags & MS_RDONLY) {
  2511. ret = btrfs_commit_super(tree_root);
  2512. if (ret)
  2513. goto fail_qgroup;
  2514. }
  2515. }
  2516. ret = btrfs_find_orphan_roots(tree_root);
  2517. if (ret)
  2518. goto fail_qgroup;
  2519. if (!(sb->s_flags & MS_RDONLY)) {
  2520. ret = btrfs_cleanup_fs_roots(fs_info);
  2521. if (ret)
  2522. goto fail_qgroup;
  2523. mutex_lock(&fs_info->cleaner_mutex);
  2524. ret = btrfs_recover_relocation(tree_root);
  2525. mutex_unlock(&fs_info->cleaner_mutex);
  2526. if (ret < 0) {
  2527. printk(KERN_WARNING
  2528. "BTRFS: failed to recover relocation\n");
  2529. err = -EINVAL;
  2530. goto fail_qgroup;
  2531. }
  2532. }
  2533. location.objectid = BTRFS_FS_TREE_OBJECTID;
  2534. location.type = BTRFS_ROOT_ITEM_KEY;
  2535. location.offset = 0;
  2536. fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
  2537. if (IS_ERR(fs_info->fs_root)) {
  2538. err = PTR_ERR(fs_info->fs_root);
  2539. goto fail_qgroup;
  2540. }
  2541. if (sb->s_flags & MS_RDONLY)
  2542. return 0;
  2543. down_read(&fs_info->cleanup_work_sem);
  2544. if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
  2545. (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
  2546. up_read(&fs_info->cleanup_work_sem);
  2547. close_ctree(tree_root);
  2548. return ret;
  2549. }
  2550. up_read(&fs_info->cleanup_work_sem);
  2551. ret = btrfs_resume_balance_async(fs_info);
  2552. if (ret) {
  2553. printk(KERN_WARNING "BTRFS: failed to resume balance\n");
  2554. close_ctree(tree_root);
  2555. return ret;
  2556. }
  2557. ret = btrfs_resume_dev_replace_async(fs_info);
  2558. if (ret) {
  2559. pr_warn("BTRFS: failed to resume dev_replace\n");
  2560. close_ctree(tree_root);
  2561. return ret;
  2562. }
  2563. btrfs_qgroup_rescan_resume(fs_info);
  2564. if (create_uuid_tree) {
  2565. pr_info("BTRFS: creating UUID tree\n");
  2566. ret = btrfs_create_uuid_tree(fs_info);
  2567. if (ret) {
  2568. pr_warn("BTRFS: failed to create the UUID tree %d\n",
  2569. ret);
  2570. close_ctree(tree_root);
  2571. return ret;
  2572. }
  2573. } else if (check_uuid_tree ||
  2574. btrfs_test_opt(tree_root, RESCAN_UUID_TREE)) {
  2575. pr_info("BTRFS: checking UUID tree\n");
  2576. ret = btrfs_check_uuid_tree(fs_info);
  2577. if (ret) {
  2578. pr_warn("BTRFS: failed to check the UUID tree %d\n",
  2579. ret);
  2580. close_ctree(tree_root);
  2581. return ret;
  2582. }
  2583. } else {
  2584. fs_info->update_uuid_tree_gen = 1;
  2585. }
  2586. fs_info->open = 1;
  2587. return 0;
  2588. fail_qgroup:
  2589. btrfs_free_qgroup_config(fs_info);
  2590. fail_trans_kthread:
  2591. kthread_stop(fs_info->transaction_kthread);
  2592. btrfs_cleanup_transaction(fs_info->tree_root);
  2593. btrfs_free_fs_roots(fs_info);
  2594. fail_cleaner:
  2595. kthread_stop(fs_info->cleaner_kthread);
  2596. /*
  2597. * make sure we're done with the btree inode before we stop our
  2598. * kthreads
  2599. */
  2600. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  2601. fail_sysfs:
  2602. btrfs_sysfs_remove_one(fs_info);
  2603. fail_block_groups:
  2604. btrfs_put_block_group_cache(fs_info);
  2605. btrfs_free_block_groups(fs_info);
  2606. fail_tree_roots:
  2607. free_root_pointers(fs_info, 1);
  2608. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  2609. fail_sb_buffer:
  2610. btrfs_stop_all_workers(fs_info);
  2611. fail_alloc:
  2612. fail_iput:
  2613. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  2614. iput(fs_info->btree_inode);
  2615. fail_bio_counter:
  2616. percpu_counter_destroy(&fs_info->bio_counter);
  2617. fail_delalloc_bytes:
  2618. percpu_counter_destroy(&fs_info->delalloc_bytes);
  2619. fail_dirty_metadata_bytes:
  2620. percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
  2621. fail_bdi:
  2622. bdi_destroy(&fs_info->bdi);
  2623. fail_srcu:
  2624. cleanup_srcu_struct(&fs_info->subvol_srcu);
  2625. fail:
  2626. btrfs_free_stripe_hash_table(fs_info);
  2627. btrfs_close_devices(fs_info->fs_devices);
  2628. return err;
  2629. recovery_tree_root:
  2630. if (!btrfs_test_opt(tree_root, RECOVERY))
  2631. goto fail_tree_roots;
  2632. free_root_pointers(fs_info, 0);
  2633. /* don't use the log in recovery mode, it won't be valid */
  2634. btrfs_set_super_log_root(disk_super, 0);
  2635. /* we can't trust the free space cache either */
  2636. btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE);
  2637. ret = next_root_backup(fs_info, fs_info->super_copy,
  2638. &num_backups_tried, &backup_index);
  2639. if (ret == -1)
  2640. goto fail_block_groups;
  2641. goto retry_root_backup;
  2642. }
  2643. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  2644. {
  2645. if (uptodate) {
  2646. set_buffer_uptodate(bh);
  2647. } else {
  2648. struct btrfs_device *device = (struct btrfs_device *)
  2649. bh->b_private;
  2650. printk_ratelimited_in_rcu(KERN_WARNING "BTRFS: lost page write due to "
  2651. "I/O error on %s\n",
  2652. rcu_str_deref(device->name));
  2653. /* note, we dont' set_buffer_write_io_error because we have
  2654. * our own ways of dealing with the IO errors
  2655. */
  2656. clear_buffer_uptodate(bh);
  2657. btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS);
  2658. }
  2659. unlock_buffer(bh);
  2660. put_bh(bh);
  2661. }
  2662. struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
  2663. {
  2664. struct buffer_head *bh;
  2665. struct buffer_head *latest = NULL;
  2666. struct btrfs_super_block *super;
  2667. int i;
  2668. u64 transid = 0;
  2669. u64 bytenr;
  2670. /* we would like to check all the supers, but that would make
  2671. * a btrfs mount succeed after a mkfs from a different FS.
  2672. * So, we need to add a special mount option to scan for
  2673. * later supers, using BTRFS_SUPER_MIRROR_MAX instead
  2674. */
  2675. for (i = 0; i < 1; i++) {
  2676. bytenr = btrfs_sb_offset(i);
  2677. if (bytenr + BTRFS_SUPER_INFO_SIZE >=
  2678. i_size_read(bdev->bd_inode))
  2679. break;
  2680. bh = __bread(bdev, bytenr / 4096,
  2681. BTRFS_SUPER_INFO_SIZE);
  2682. if (!bh)
  2683. continue;
  2684. super = (struct btrfs_super_block *)bh->b_data;
  2685. if (btrfs_super_bytenr(super) != bytenr ||
  2686. btrfs_super_magic(super) != BTRFS_MAGIC) {
  2687. brelse(bh);
  2688. continue;
  2689. }
  2690. if (!latest || btrfs_super_generation(super) > transid) {
  2691. brelse(latest);
  2692. latest = bh;
  2693. transid = btrfs_super_generation(super);
  2694. } else {
  2695. brelse(bh);
  2696. }
  2697. }
  2698. return latest;
  2699. }
  2700. /*
  2701. * this should be called twice, once with wait == 0 and
  2702. * once with wait == 1. When wait == 0 is done, all the buffer heads
  2703. * we write are pinned.
  2704. *
  2705. * They are released when wait == 1 is done.
  2706. * max_mirrors must be the same for both runs, and it indicates how
  2707. * many supers on this one device should be written.
  2708. *
  2709. * max_mirrors == 0 means to write them all.
  2710. */
  2711. static int write_dev_supers(struct btrfs_device *device,
  2712. struct btrfs_super_block *sb,
  2713. int do_barriers, int wait, int max_mirrors)
  2714. {
  2715. struct buffer_head *bh;
  2716. int i;
  2717. int ret;
  2718. int errors = 0;
  2719. u32 crc;
  2720. u64 bytenr;
  2721. if (max_mirrors == 0)
  2722. max_mirrors = BTRFS_SUPER_MIRROR_MAX;
  2723. for (i = 0; i < max_mirrors; i++) {
  2724. bytenr = btrfs_sb_offset(i);
  2725. if (bytenr + BTRFS_SUPER_INFO_SIZE >=
  2726. device->commit_total_bytes)
  2727. break;
  2728. if (wait) {
  2729. bh = __find_get_block(device->bdev, bytenr / 4096,
  2730. BTRFS_SUPER_INFO_SIZE);
  2731. if (!bh) {
  2732. errors++;
  2733. continue;
  2734. }
  2735. wait_on_buffer(bh);
  2736. if (!buffer_uptodate(bh))
  2737. errors++;
  2738. /* drop our reference */
  2739. brelse(bh);
  2740. /* drop the reference from the wait == 0 run */
  2741. brelse(bh);
  2742. continue;
  2743. } else {
  2744. btrfs_set_super_bytenr(sb, bytenr);
  2745. crc = ~(u32)0;
  2746. crc = btrfs_csum_data((char *)sb +
  2747. BTRFS_CSUM_SIZE, crc,
  2748. BTRFS_SUPER_INFO_SIZE -
  2749. BTRFS_CSUM_SIZE);
  2750. btrfs_csum_final(crc, sb->csum);
  2751. /*
  2752. * one reference for us, and we leave it for the
  2753. * caller
  2754. */
  2755. bh = __getblk(device->bdev, bytenr / 4096,
  2756. BTRFS_SUPER_INFO_SIZE);
  2757. if (!bh) {
  2758. printk(KERN_ERR "BTRFS: couldn't get super "
  2759. "buffer head for bytenr %Lu\n", bytenr);
  2760. errors++;
  2761. continue;
  2762. }
  2763. memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
  2764. /* one reference for submit_bh */
  2765. get_bh(bh);
  2766. set_buffer_uptodate(bh);
  2767. lock_buffer(bh);
  2768. bh->b_end_io = btrfs_end_buffer_write_sync;
  2769. bh->b_private = device;
  2770. }
  2771. /*
  2772. * we fua the first super. The others we allow
  2773. * to go down lazy.
  2774. */
  2775. if (i == 0)
  2776. ret = btrfsic_submit_bh(WRITE_FUA, bh);
  2777. else
  2778. ret = btrfsic_submit_bh(WRITE_SYNC, bh);
  2779. if (ret)
  2780. errors++;
  2781. }
  2782. return errors < i ? 0 : -1;
  2783. }
  2784. /*
  2785. * endio for the write_dev_flush, this will wake anyone waiting
  2786. * for the barrier when it is done
  2787. */
  2788. static void btrfs_end_empty_barrier(struct bio *bio, int err)
  2789. {
  2790. if (err) {
  2791. if (err == -EOPNOTSUPP)
  2792. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  2793. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2794. }
  2795. if (bio->bi_private)
  2796. complete(bio->bi_private);
  2797. bio_put(bio);
  2798. }
  2799. /*
  2800. * trigger flushes for one the devices. If you pass wait == 0, the flushes are
  2801. * sent down. With wait == 1, it waits for the previous flush.
  2802. *
  2803. * any device where the flush fails with eopnotsupp are flagged as not-barrier
  2804. * capable
  2805. */
  2806. static int write_dev_flush(struct btrfs_device *device, int wait)
  2807. {
  2808. struct bio *bio;
  2809. int ret = 0;
  2810. if (device->nobarriers)
  2811. return 0;
  2812. if (wait) {
  2813. bio = device->flush_bio;
  2814. if (!bio)
  2815. return 0;
  2816. wait_for_completion(&device->flush_wait);
  2817. if (bio_flagged(bio, BIO_EOPNOTSUPP)) {
  2818. printk_in_rcu("BTRFS: disabling barriers on dev %s\n",
  2819. rcu_str_deref(device->name));
  2820. device->nobarriers = 1;
  2821. } else if (!bio_flagged(bio, BIO_UPTODATE)) {
  2822. ret = -EIO;
  2823. btrfs_dev_stat_inc_and_print(device,
  2824. BTRFS_DEV_STAT_FLUSH_ERRS);
  2825. }
  2826. /* drop the reference from the wait == 0 run */
  2827. bio_put(bio);
  2828. device->flush_bio = NULL;
  2829. return ret;
  2830. }
  2831. /*
  2832. * one reference for us, and we leave it for the
  2833. * caller
  2834. */
  2835. device->flush_bio = NULL;
  2836. bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
  2837. if (!bio)
  2838. return -ENOMEM;
  2839. bio->bi_end_io = btrfs_end_empty_barrier;
  2840. bio->bi_bdev = device->bdev;
  2841. init_completion(&device->flush_wait);
  2842. bio->bi_private = &device->flush_wait;
  2843. device->flush_bio = bio;
  2844. bio_get(bio);
  2845. btrfsic_submit_bio(WRITE_FLUSH, bio);
  2846. return 0;
  2847. }
  2848. /*
  2849. * send an empty flush down to each device in parallel,
  2850. * then wait for them
  2851. */
  2852. static int barrier_all_devices(struct btrfs_fs_info *info)
  2853. {
  2854. struct list_head *head;
  2855. struct btrfs_device *dev;
  2856. int errors_send = 0;
  2857. int errors_wait = 0;
  2858. int ret;
  2859. /* send down all the barriers */
  2860. head = &info->fs_devices->devices;
  2861. list_for_each_entry_rcu(dev, head, dev_list) {
  2862. if (dev->missing)
  2863. continue;
  2864. if (!dev->bdev) {
  2865. errors_send++;
  2866. continue;
  2867. }
  2868. if (!dev->in_fs_metadata || !dev->writeable)
  2869. continue;
  2870. ret = write_dev_flush(dev, 0);
  2871. if (ret)
  2872. errors_send++;
  2873. }
  2874. /* wait for all the barriers */
  2875. list_for_each_entry_rcu(dev, head, dev_list) {
  2876. if (dev->missing)
  2877. continue;
  2878. if (!dev->bdev) {
  2879. errors_wait++;
  2880. continue;
  2881. }
  2882. if (!dev->in_fs_metadata || !dev->writeable)
  2883. continue;
  2884. ret = write_dev_flush(dev, 1);
  2885. if (ret)
  2886. errors_wait++;
  2887. }
  2888. if (errors_send > info->num_tolerated_disk_barrier_failures ||
  2889. errors_wait > info->num_tolerated_disk_barrier_failures)
  2890. return -EIO;
  2891. return 0;
  2892. }
  2893. int btrfs_calc_num_tolerated_disk_barrier_failures(
  2894. struct btrfs_fs_info *fs_info)
  2895. {
  2896. struct btrfs_ioctl_space_info space;
  2897. struct btrfs_space_info *sinfo;
  2898. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  2899. BTRFS_BLOCK_GROUP_SYSTEM,
  2900. BTRFS_BLOCK_GROUP_METADATA,
  2901. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  2902. int num_types = 4;
  2903. int i;
  2904. int c;
  2905. int num_tolerated_disk_barrier_failures =
  2906. (int)fs_info->fs_devices->num_devices;
  2907. for (i = 0; i < num_types; i++) {
  2908. struct btrfs_space_info *tmp;
  2909. sinfo = NULL;
  2910. rcu_read_lock();
  2911. list_for_each_entry_rcu(tmp, &fs_info->space_info, list) {
  2912. if (tmp->flags == types[i]) {
  2913. sinfo = tmp;
  2914. break;
  2915. }
  2916. }
  2917. rcu_read_unlock();
  2918. if (!sinfo)
  2919. continue;
  2920. down_read(&sinfo->groups_sem);
  2921. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2922. if (!list_empty(&sinfo->block_groups[c])) {
  2923. u64 flags;
  2924. btrfs_get_block_group_info(
  2925. &sinfo->block_groups[c], &space);
  2926. if (space.total_bytes == 0 ||
  2927. space.used_bytes == 0)
  2928. continue;
  2929. flags = space.flags;
  2930. /*
  2931. * return
  2932. * 0: if dup, single or RAID0 is configured for
  2933. * any of metadata, system or data, else
  2934. * 1: if RAID5 is configured, or if RAID1 or
  2935. * RAID10 is configured and only two mirrors
  2936. * are used, else
  2937. * 2: if RAID6 is configured, else
  2938. * num_mirrors - 1: if RAID1 or RAID10 is
  2939. * configured and more than
  2940. * 2 mirrors are used.
  2941. */
  2942. if (num_tolerated_disk_barrier_failures > 0 &&
  2943. ((flags & (BTRFS_BLOCK_GROUP_DUP |
  2944. BTRFS_BLOCK_GROUP_RAID0)) ||
  2945. ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK)
  2946. == 0)))
  2947. num_tolerated_disk_barrier_failures = 0;
  2948. else if (num_tolerated_disk_barrier_failures > 1) {
  2949. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  2950. BTRFS_BLOCK_GROUP_RAID5 |
  2951. BTRFS_BLOCK_GROUP_RAID10)) {
  2952. num_tolerated_disk_barrier_failures = 1;
  2953. } else if (flags &
  2954. BTRFS_BLOCK_GROUP_RAID6) {
  2955. num_tolerated_disk_barrier_failures = 2;
  2956. }
  2957. }
  2958. }
  2959. }
  2960. up_read(&sinfo->groups_sem);
  2961. }
  2962. return num_tolerated_disk_barrier_failures;
  2963. }
  2964. static int write_all_supers(struct btrfs_root *root, int max_mirrors)
  2965. {
  2966. struct list_head *head;
  2967. struct btrfs_device *dev;
  2968. struct btrfs_super_block *sb;
  2969. struct btrfs_dev_item *dev_item;
  2970. int ret;
  2971. int do_barriers;
  2972. int max_errors;
  2973. int total_errors = 0;
  2974. u64 flags;
  2975. do_barriers = !btrfs_test_opt(root, NOBARRIER);
  2976. backup_super_roots(root->fs_info);
  2977. sb = root->fs_info->super_for_commit;
  2978. dev_item = &sb->dev_item;
  2979. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  2980. head = &root->fs_info->fs_devices->devices;
  2981. max_errors = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
  2982. if (do_barriers) {
  2983. ret = barrier_all_devices(root->fs_info);
  2984. if (ret) {
  2985. mutex_unlock(
  2986. &root->fs_info->fs_devices->device_list_mutex);
  2987. btrfs_error(root->fs_info, ret,
  2988. "errors while submitting device barriers.");
  2989. return ret;
  2990. }
  2991. }
  2992. list_for_each_entry_rcu(dev, head, dev_list) {
  2993. if (!dev->bdev) {
  2994. total_errors++;
  2995. continue;
  2996. }
  2997. if (!dev->in_fs_metadata || !dev->writeable)
  2998. continue;
  2999. btrfs_set_stack_device_generation(dev_item, 0);
  3000. btrfs_set_stack_device_type(dev_item, dev->type);
  3001. btrfs_set_stack_device_id(dev_item, dev->devid);
  3002. btrfs_set_stack_device_total_bytes(dev_item,
  3003. dev->commit_total_bytes);
  3004. btrfs_set_stack_device_bytes_used(dev_item,
  3005. dev->commit_bytes_used);
  3006. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  3007. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  3008. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  3009. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  3010. memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
  3011. flags = btrfs_super_flags(sb);
  3012. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  3013. ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors);
  3014. if (ret)
  3015. total_errors++;
  3016. }
  3017. if (total_errors > max_errors) {
  3018. btrfs_err(root->fs_info, "%d errors while writing supers",
  3019. total_errors);
  3020. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  3021. /* FUA is masked off if unsupported and can't be the reason */
  3022. btrfs_error(root->fs_info, -EIO,
  3023. "%d errors while writing supers", total_errors);
  3024. return -EIO;
  3025. }
  3026. total_errors = 0;
  3027. list_for_each_entry_rcu(dev, head, dev_list) {
  3028. if (!dev->bdev)
  3029. continue;
  3030. if (!dev->in_fs_metadata || !dev->writeable)
  3031. continue;
  3032. ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors);
  3033. if (ret)
  3034. total_errors++;
  3035. }
  3036. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  3037. if (total_errors > max_errors) {
  3038. btrfs_error(root->fs_info, -EIO,
  3039. "%d errors while writing supers", total_errors);
  3040. return -EIO;
  3041. }
  3042. return 0;
  3043. }
  3044. int write_ctree_super(struct btrfs_trans_handle *trans,
  3045. struct btrfs_root *root, int max_mirrors)
  3046. {
  3047. return write_all_supers(root, max_mirrors);
  3048. }
  3049. /* Drop a fs root from the radix tree and free it. */
  3050. void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
  3051. struct btrfs_root *root)
  3052. {
  3053. spin_lock(&fs_info->fs_roots_radix_lock);
  3054. radix_tree_delete(&fs_info->fs_roots_radix,
  3055. (unsigned long)root->root_key.objectid);
  3056. spin_unlock(&fs_info->fs_roots_radix_lock);
  3057. if (btrfs_root_refs(&root->root_item) == 0)
  3058. synchronize_srcu(&fs_info->subvol_srcu);
  3059. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  3060. btrfs_free_log(NULL, root);
  3061. if (root->free_ino_pinned)
  3062. __btrfs_remove_free_space_cache(root->free_ino_pinned);
  3063. if (root->free_ino_ctl)
  3064. __btrfs_remove_free_space_cache(root->free_ino_ctl);
  3065. free_fs_root(root);
  3066. }
  3067. static void free_fs_root(struct btrfs_root *root)
  3068. {
  3069. iput(root->ino_cache_inode);
  3070. WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
  3071. btrfs_free_block_rsv(root, root->orphan_block_rsv);
  3072. root->orphan_block_rsv = NULL;
  3073. if (root->anon_dev)
  3074. free_anon_bdev(root->anon_dev);
  3075. if (root->subv_writers)
  3076. btrfs_free_subvolume_writers(root->subv_writers);
  3077. free_extent_buffer(root->node);
  3078. free_extent_buffer(root->commit_root);
  3079. kfree(root->free_ino_ctl);
  3080. kfree(root->free_ino_pinned);
  3081. kfree(root->name);
  3082. btrfs_put_fs_root(root);
  3083. }
  3084. void btrfs_free_fs_root(struct btrfs_root *root)
  3085. {
  3086. free_fs_root(root);
  3087. }
  3088. int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
  3089. {
  3090. u64 root_objectid = 0;
  3091. struct btrfs_root *gang[8];
  3092. int i = 0;
  3093. int err = 0;
  3094. unsigned int ret = 0;
  3095. int index;
  3096. while (1) {
  3097. index = srcu_read_lock(&fs_info->subvol_srcu);
  3098. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  3099. (void **)gang, root_objectid,
  3100. ARRAY_SIZE(gang));
  3101. if (!ret) {
  3102. srcu_read_unlock(&fs_info->subvol_srcu, index);
  3103. break;
  3104. }
  3105. root_objectid = gang[ret - 1]->root_key.objectid + 1;
  3106. for (i = 0; i < ret; i++) {
  3107. /* Avoid to grab roots in dead_roots */
  3108. if (btrfs_root_refs(&gang[i]->root_item) == 0) {
  3109. gang[i] = NULL;
  3110. continue;
  3111. }
  3112. /* grab all the search result for later use */
  3113. gang[i] = btrfs_grab_fs_root(gang[i]);
  3114. }
  3115. srcu_read_unlock(&fs_info->subvol_srcu, index);
  3116. for (i = 0; i < ret; i++) {
  3117. if (!gang[i])
  3118. continue;
  3119. root_objectid = gang[i]->root_key.objectid;
  3120. err = btrfs_orphan_cleanup(gang[i]);
  3121. if (err)
  3122. break;
  3123. btrfs_put_fs_root(gang[i]);
  3124. }
  3125. root_objectid++;
  3126. }
  3127. /* release the uncleaned roots due to error */
  3128. for (; i < ret; i++) {
  3129. if (gang[i])
  3130. btrfs_put_fs_root(gang[i]);
  3131. }
  3132. return err;
  3133. }
  3134. int btrfs_commit_super(struct btrfs_root *root)
  3135. {
  3136. struct btrfs_trans_handle *trans;
  3137. mutex_lock(&root->fs_info->cleaner_mutex);
  3138. btrfs_run_delayed_iputs(root);
  3139. mutex_unlock(&root->fs_info->cleaner_mutex);
  3140. wake_up_process(root->fs_info->cleaner_kthread);
  3141. /* wait until ongoing cleanup work done */
  3142. down_write(&root->fs_info->cleanup_work_sem);
  3143. up_write(&root->fs_info->cleanup_work_sem);
  3144. trans = btrfs_join_transaction(root);
  3145. if (IS_ERR(trans))
  3146. return PTR_ERR(trans);
  3147. return btrfs_commit_transaction(trans, root);
  3148. }
  3149. void close_ctree(struct btrfs_root *root)
  3150. {
  3151. struct btrfs_fs_info *fs_info = root->fs_info;
  3152. int ret;
  3153. fs_info->closing = 1;
  3154. smp_mb();
  3155. /* wait for the uuid_scan task to finish */
  3156. down(&fs_info->uuid_tree_rescan_sem);
  3157. /* avoid complains from lockdep et al., set sem back to initial state */
  3158. up(&fs_info->uuid_tree_rescan_sem);
  3159. /* pause restriper - we want to resume on mount */
  3160. btrfs_pause_balance(fs_info);
  3161. btrfs_dev_replace_suspend_for_unmount(fs_info);
  3162. btrfs_scrub_cancel(fs_info);
  3163. /* wait for any defraggers to finish */
  3164. wait_event(fs_info->transaction_wait,
  3165. (atomic_read(&fs_info->defrag_running) == 0));
  3166. /* clear out the rbtree of defraggable inodes */
  3167. btrfs_cleanup_defrag_inodes(fs_info);
  3168. cancel_work_sync(&fs_info->async_reclaim_work);
  3169. if (!(fs_info->sb->s_flags & MS_RDONLY)) {
  3170. ret = btrfs_commit_super(root);
  3171. if (ret)
  3172. btrfs_err(root->fs_info, "commit super ret %d", ret);
  3173. }
  3174. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  3175. btrfs_error_commit_super(root);
  3176. kthread_stop(fs_info->transaction_kthread);
  3177. kthread_stop(fs_info->cleaner_kthread);
  3178. fs_info->closing = 2;
  3179. smp_mb();
  3180. btrfs_free_qgroup_config(root->fs_info);
  3181. if (percpu_counter_sum(&fs_info->delalloc_bytes)) {
  3182. btrfs_info(root->fs_info, "at unmount delalloc count %lld",
  3183. percpu_counter_sum(&fs_info->delalloc_bytes));
  3184. }
  3185. btrfs_sysfs_remove_one(fs_info);
  3186. btrfs_free_fs_roots(fs_info);
  3187. btrfs_put_block_group_cache(fs_info);
  3188. btrfs_free_block_groups(fs_info);
  3189. /*
  3190. * we must make sure there is not any read request to
  3191. * submit after we stopping all workers.
  3192. */
  3193. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  3194. btrfs_stop_all_workers(fs_info);
  3195. fs_info->open = 0;
  3196. free_root_pointers(fs_info, 1);
  3197. iput(fs_info->btree_inode);
  3198. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  3199. if (btrfs_test_opt(root, CHECK_INTEGRITY))
  3200. btrfsic_unmount(root, fs_info->fs_devices);
  3201. #endif
  3202. btrfs_close_devices(fs_info->fs_devices);
  3203. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  3204. percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
  3205. percpu_counter_destroy(&fs_info->delalloc_bytes);
  3206. percpu_counter_destroy(&fs_info->bio_counter);
  3207. bdi_destroy(&fs_info->bdi);
  3208. cleanup_srcu_struct(&fs_info->subvol_srcu);
  3209. btrfs_free_stripe_hash_table(fs_info);
  3210. btrfs_free_block_rsv(root, root->orphan_block_rsv);
  3211. root->orphan_block_rsv = NULL;
  3212. }
  3213. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
  3214. int atomic)
  3215. {
  3216. int ret;
  3217. struct inode *btree_inode = buf->pages[0]->mapping->host;
  3218. ret = extent_buffer_uptodate(buf);
  3219. if (!ret)
  3220. return ret;
  3221. ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
  3222. parent_transid, atomic);
  3223. if (ret == -EAGAIN)
  3224. return ret;
  3225. return !ret;
  3226. }
  3227. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  3228. {
  3229. return set_extent_buffer_uptodate(buf);
  3230. }
  3231. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  3232. {
  3233. struct btrfs_root *root;
  3234. u64 transid = btrfs_header_generation(buf);
  3235. int was_dirty;
  3236. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  3237. /*
  3238. * This is a fast path so only do this check if we have sanity tests
  3239. * enabled. Normal people shouldn't be marking dummy buffers as dirty
  3240. * outside of the sanity tests.
  3241. */
  3242. if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &buf->bflags)))
  3243. return;
  3244. #endif
  3245. root = BTRFS_I(buf->pages[0]->mapping->host)->root;
  3246. btrfs_assert_tree_locked(buf);
  3247. if (transid != root->fs_info->generation)
  3248. WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, "
  3249. "found %llu running %llu\n",
  3250. buf->start, transid, root->fs_info->generation);
  3251. was_dirty = set_extent_buffer_dirty(buf);
  3252. if (!was_dirty)
  3253. __percpu_counter_add(&root->fs_info->dirty_metadata_bytes,
  3254. buf->len,
  3255. root->fs_info->dirty_metadata_batch);
  3256. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  3257. if (btrfs_header_level(buf) == 0 && check_leaf(root, buf)) {
  3258. btrfs_print_leaf(root, buf);
  3259. ASSERT(0);
  3260. }
  3261. #endif
  3262. }
  3263. static void __btrfs_btree_balance_dirty(struct btrfs_root *root,
  3264. int flush_delayed)
  3265. {
  3266. /*
  3267. * looks as though older kernels can get into trouble with
  3268. * this code, they end up stuck in balance_dirty_pages forever
  3269. */
  3270. int ret;
  3271. if (current->flags & PF_MEMALLOC)
  3272. return;
  3273. if (flush_delayed)
  3274. btrfs_balance_delayed_items(root);
  3275. ret = percpu_counter_compare(&root->fs_info->dirty_metadata_bytes,
  3276. BTRFS_DIRTY_METADATA_THRESH);
  3277. if (ret > 0) {
  3278. balance_dirty_pages_ratelimited(
  3279. root->fs_info->btree_inode->i_mapping);
  3280. }
  3281. return;
  3282. }
  3283. void btrfs_btree_balance_dirty(struct btrfs_root *root)
  3284. {
  3285. __btrfs_btree_balance_dirty(root, 1);
  3286. }
  3287. void btrfs_btree_balance_dirty_nodelay(struct btrfs_root *root)
  3288. {
  3289. __btrfs_btree_balance_dirty(root, 0);
  3290. }
  3291. int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
  3292. {
  3293. struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
  3294. return btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
  3295. }
  3296. static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
  3297. int read_only)
  3298. {
  3299. struct btrfs_super_block *sb = fs_info->super_copy;
  3300. int ret = 0;
  3301. if (sb->root_level > BTRFS_MAX_LEVEL) {
  3302. printk(KERN_ERR "BTRFS: tree_root level too big: %d > %d\n",
  3303. sb->root_level, BTRFS_MAX_LEVEL);
  3304. ret = -EINVAL;
  3305. }
  3306. if (sb->chunk_root_level > BTRFS_MAX_LEVEL) {
  3307. printk(KERN_ERR "BTRFS: chunk_root level too big: %d > %d\n",
  3308. sb->chunk_root_level, BTRFS_MAX_LEVEL);
  3309. ret = -EINVAL;
  3310. }
  3311. if (sb->log_root_level > BTRFS_MAX_LEVEL) {
  3312. printk(KERN_ERR "BTRFS: log_root level too big: %d > %d\n",
  3313. sb->log_root_level, BTRFS_MAX_LEVEL);
  3314. ret = -EINVAL;
  3315. }
  3316. /*
  3317. * The common minimum, we don't know if we can trust the nodesize/sectorsize
  3318. * items yet, they'll be verified later. Issue just a warning.
  3319. */
  3320. if (!IS_ALIGNED(sb->root, 4096))
  3321. printk(KERN_WARNING "BTRFS: tree_root block unaligned: %llu\n",
  3322. sb->root);
  3323. if (!IS_ALIGNED(sb->chunk_root, 4096))
  3324. printk(KERN_WARNING "BTRFS: tree_root block unaligned: %llu\n",
  3325. sb->chunk_root);
  3326. if (!IS_ALIGNED(sb->log_root, 4096))
  3327. printk(KERN_WARNING "BTRFS: tree_root block unaligned: %llu\n",
  3328. sb->log_root);
  3329. if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
  3330. printk(KERN_ERR "BTRFS: dev_item UUID does not match fsid: %pU != %pU\n",
  3331. fs_info->fsid, sb->dev_item.fsid);
  3332. ret = -EINVAL;
  3333. }
  3334. /*
  3335. * Hint to catch really bogus numbers, bitflips or so, more exact checks are
  3336. * done later
  3337. */
  3338. if (sb->num_devices > (1UL << 31))
  3339. printk(KERN_WARNING "BTRFS: suspicious number of devices: %llu\n",
  3340. sb->num_devices);
  3341. if (sb->bytenr != BTRFS_SUPER_INFO_OFFSET) {
  3342. printk(KERN_ERR "BTRFS: super offset mismatch %llu != %u\n",
  3343. sb->bytenr, BTRFS_SUPER_INFO_OFFSET);
  3344. ret = -EINVAL;
  3345. }
  3346. /*
  3347. * The generation is a global counter, we'll trust it more than the others
  3348. * but it's still possible that it's the one that's wrong.
  3349. */
  3350. if (sb->generation < sb->chunk_root_generation)
  3351. printk(KERN_WARNING
  3352. "BTRFS: suspicious: generation < chunk_root_generation: %llu < %llu\n",
  3353. sb->generation, sb->chunk_root_generation);
  3354. if (sb->generation < sb->cache_generation && sb->cache_generation != (u64)-1)
  3355. printk(KERN_WARNING
  3356. "BTRFS: suspicious: generation < cache_generation: %llu < %llu\n",
  3357. sb->generation, sb->cache_generation);
  3358. return ret;
  3359. }
  3360. static void btrfs_error_commit_super(struct btrfs_root *root)
  3361. {
  3362. mutex_lock(&root->fs_info->cleaner_mutex);
  3363. btrfs_run_delayed_iputs(root);
  3364. mutex_unlock(&root->fs_info->cleaner_mutex);
  3365. down_write(&root->fs_info->cleanup_work_sem);
  3366. up_write(&root->fs_info->cleanup_work_sem);
  3367. /* cleanup FS via transaction */
  3368. btrfs_cleanup_transaction(root);
  3369. }
  3370. static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
  3371. {
  3372. struct btrfs_ordered_extent *ordered;
  3373. spin_lock(&root->ordered_extent_lock);
  3374. /*
  3375. * This will just short circuit the ordered completion stuff which will
  3376. * make sure the ordered extent gets properly cleaned up.
  3377. */
  3378. list_for_each_entry(ordered, &root->ordered_extents,
  3379. root_extent_list)
  3380. set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
  3381. spin_unlock(&root->ordered_extent_lock);
  3382. }
  3383. static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
  3384. {
  3385. struct btrfs_root *root;
  3386. struct list_head splice;
  3387. INIT_LIST_HEAD(&splice);
  3388. spin_lock(&fs_info->ordered_root_lock);
  3389. list_splice_init(&fs_info->ordered_roots, &splice);
  3390. while (!list_empty(&splice)) {
  3391. root = list_first_entry(&splice, struct btrfs_root,
  3392. ordered_root);
  3393. list_move_tail(&root->ordered_root,
  3394. &fs_info->ordered_roots);
  3395. spin_unlock(&fs_info->ordered_root_lock);
  3396. btrfs_destroy_ordered_extents(root);
  3397. cond_resched();
  3398. spin_lock(&fs_info->ordered_root_lock);
  3399. }
  3400. spin_unlock(&fs_info->ordered_root_lock);
  3401. }
  3402. static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
  3403. struct btrfs_root *root)
  3404. {
  3405. struct rb_node *node;
  3406. struct btrfs_delayed_ref_root *delayed_refs;
  3407. struct btrfs_delayed_ref_node *ref;
  3408. int ret = 0;
  3409. delayed_refs = &trans->delayed_refs;
  3410. spin_lock(&delayed_refs->lock);
  3411. if (atomic_read(&delayed_refs->num_entries) == 0) {
  3412. spin_unlock(&delayed_refs->lock);
  3413. btrfs_info(root->fs_info, "delayed_refs has NO entry");
  3414. return ret;
  3415. }
  3416. while ((node = rb_first(&delayed_refs->href_root)) != NULL) {
  3417. struct btrfs_delayed_ref_head *head;
  3418. bool pin_bytes = false;
  3419. head = rb_entry(node, struct btrfs_delayed_ref_head,
  3420. href_node);
  3421. if (!mutex_trylock(&head->mutex)) {
  3422. atomic_inc(&head->node.refs);
  3423. spin_unlock(&delayed_refs->lock);
  3424. mutex_lock(&head->mutex);
  3425. mutex_unlock(&head->mutex);
  3426. btrfs_put_delayed_ref(&head->node);
  3427. spin_lock(&delayed_refs->lock);
  3428. continue;
  3429. }
  3430. spin_lock(&head->lock);
  3431. while ((node = rb_first(&head->ref_root)) != NULL) {
  3432. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  3433. rb_node);
  3434. ref->in_tree = 0;
  3435. rb_erase(&ref->rb_node, &head->ref_root);
  3436. atomic_dec(&delayed_refs->num_entries);
  3437. btrfs_put_delayed_ref(ref);
  3438. }
  3439. if (head->must_insert_reserved)
  3440. pin_bytes = true;
  3441. btrfs_free_delayed_extent_op(head->extent_op);
  3442. delayed_refs->num_heads--;
  3443. if (head->processing == 0)
  3444. delayed_refs->num_heads_ready--;
  3445. atomic_dec(&delayed_refs->num_entries);
  3446. head->node.in_tree = 0;
  3447. rb_erase(&head->href_node, &delayed_refs->href_root);
  3448. spin_unlock(&head->lock);
  3449. spin_unlock(&delayed_refs->lock);
  3450. mutex_unlock(&head->mutex);
  3451. if (pin_bytes)
  3452. btrfs_pin_extent(root, head->node.bytenr,
  3453. head->node.num_bytes, 1);
  3454. btrfs_put_delayed_ref(&head->node);
  3455. cond_resched();
  3456. spin_lock(&delayed_refs->lock);
  3457. }
  3458. spin_unlock(&delayed_refs->lock);
  3459. return ret;
  3460. }
  3461. static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
  3462. {
  3463. struct btrfs_inode *btrfs_inode;
  3464. struct list_head splice;
  3465. INIT_LIST_HEAD(&splice);
  3466. spin_lock(&root->delalloc_lock);
  3467. list_splice_init(&root->delalloc_inodes, &splice);
  3468. while (!list_empty(&splice)) {
  3469. btrfs_inode = list_first_entry(&splice, struct btrfs_inode,
  3470. delalloc_inodes);
  3471. list_del_init(&btrfs_inode->delalloc_inodes);
  3472. clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  3473. &btrfs_inode->runtime_flags);
  3474. spin_unlock(&root->delalloc_lock);
  3475. btrfs_invalidate_inodes(btrfs_inode->root);
  3476. spin_lock(&root->delalloc_lock);
  3477. }
  3478. spin_unlock(&root->delalloc_lock);
  3479. }
  3480. static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
  3481. {
  3482. struct btrfs_root *root;
  3483. struct list_head splice;
  3484. INIT_LIST_HEAD(&splice);
  3485. spin_lock(&fs_info->delalloc_root_lock);
  3486. list_splice_init(&fs_info->delalloc_roots, &splice);
  3487. while (!list_empty(&splice)) {
  3488. root = list_first_entry(&splice, struct btrfs_root,
  3489. delalloc_root);
  3490. list_del_init(&root->delalloc_root);
  3491. root = btrfs_grab_fs_root(root);
  3492. BUG_ON(!root);
  3493. spin_unlock(&fs_info->delalloc_root_lock);
  3494. btrfs_destroy_delalloc_inodes(root);
  3495. btrfs_put_fs_root(root);
  3496. spin_lock(&fs_info->delalloc_root_lock);
  3497. }
  3498. spin_unlock(&fs_info->delalloc_root_lock);
  3499. }
  3500. static int btrfs_destroy_marked_extents(struct btrfs_root *root,
  3501. struct extent_io_tree *dirty_pages,
  3502. int mark)
  3503. {
  3504. int ret;
  3505. struct extent_buffer *eb;
  3506. u64 start = 0;
  3507. u64 end;
  3508. while (1) {
  3509. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  3510. mark, NULL);
  3511. if (ret)
  3512. break;
  3513. clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
  3514. while (start <= end) {
  3515. eb = btrfs_find_tree_block(root, start);
  3516. start += root->nodesize;
  3517. if (!eb)
  3518. continue;
  3519. wait_on_extent_buffer_writeback(eb);
  3520. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY,
  3521. &eb->bflags))
  3522. clear_extent_buffer_dirty(eb);
  3523. free_extent_buffer_stale(eb);
  3524. }
  3525. }
  3526. return ret;
  3527. }
  3528. static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
  3529. struct extent_io_tree *pinned_extents)
  3530. {
  3531. struct extent_io_tree *unpin;
  3532. u64 start;
  3533. u64 end;
  3534. int ret;
  3535. bool loop = true;
  3536. unpin = pinned_extents;
  3537. again:
  3538. while (1) {
  3539. ret = find_first_extent_bit(unpin, 0, &start, &end,
  3540. EXTENT_DIRTY, NULL);
  3541. if (ret)
  3542. break;
  3543. /* opt_discard */
  3544. if (btrfs_test_opt(root, DISCARD))
  3545. ret = btrfs_error_discard_extent(root, start,
  3546. end + 1 - start,
  3547. NULL);
  3548. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  3549. btrfs_error_unpin_extent_range(root, start, end);
  3550. cond_resched();
  3551. }
  3552. if (loop) {
  3553. if (unpin == &root->fs_info->freed_extents[0])
  3554. unpin = &root->fs_info->freed_extents[1];
  3555. else
  3556. unpin = &root->fs_info->freed_extents[0];
  3557. loop = false;
  3558. goto again;
  3559. }
  3560. return 0;
  3561. }
  3562. void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
  3563. struct btrfs_root *root)
  3564. {
  3565. btrfs_destroy_delayed_refs(cur_trans, root);
  3566. cur_trans->state = TRANS_STATE_COMMIT_START;
  3567. wake_up(&root->fs_info->transaction_blocked_wait);
  3568. cur_trans->state = TRANS_STATE_UNBLOCKED;
  3569. wake_up(&root->fs_info->transaction_wait);
  3570. btrfs_destroy_delayed_inodes(root);
  3571. btrfs_assert_delayed_root_empty(root);
  3572. btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages,
  3573. EXTENT_DIRTY);
  3574. btrfs_destroy_pinned_extent(root,
  3575. root->fs_info->pinned_extents);
  3576. cur_trans->state =TRANS_STATE_COMPLETED;
  3577. wake_up(&cur_trans->commit_wait);
  3578. /*
  3579. memset(cur_trans, 0, sizeof(*cur_trans));
  3580. kmem_cache_free(btrfs_transaction_cachep, cur_trans);
  3581. */
  3582. }
  3583. static int btrfs_cleanup_transaction(struct btrfs_root *root)
  3584. {
  3585. struct btrfs_transaction *t;
  3586. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  3587. spin_lock(&root->fs_info->trans_lock);
  3588. while (!list_empty(&root->fs_info->trans_list)) {
  3589. t = list_first_entry(&root->fs_info->trans_list,
  3590. struct btrfs_transaction, list);
  3591. if (t->state >= TRANS_STATE_COMMIT_START) {
  3592. atomic_inc(&t->use_count);
  3593. spin_unlock(&root->fs_info->trans_lock);
  3594. btrfs_wait_for_commit(root, t->transid);
  3595. btrfs_put_transaction(t);
  3596. spin_lock(&root->fs_info->trans_lock);
  3597. continue;
  3598. }
  3599. if (t == root->fs_info->running_transaction) {
  3600. t->state = TRANS_STATE_COMMIT_DOING;
  3601. spin_unlock(&root->fs_info->trans_lock);
  3602. /*
  3603. * We wait for 0 num_writers since we don't hold a trans
  3604. * handle open currently for this transaction.
  3605. */
  3606. wait_event(t->writer_wait,
  3607. atomic_read(&t->num_writers) == 0);
  3608. } else {
  3609. spin_unlock(&root->fs_info->trans_lock);
  3610. }
  3611. btrfs_cleanup_one_transaction(t, root);
  3612. spin_lock(&root->fs_info->trans_lock);
  3613. if (t == root->fs_info->running_transaction)
  3614. root->fs_info->running_transaction = NULL;
  3615. list_del_init(&t->list);
  3616. spin_unlock(&root->fs_info->trans_lock);
  3617. btrfs_put_transaction(t);
  3618. trace_btrfs_transaction_commit(root);
  3619. spin_lock(&root->fs_info->trans_lock);
  3620. }
  3621. spin_unlock(&root->fs_info->trans_lock);
  3622. btrfs_destroy_all_ordered_extents(root->fs_info);
  3623. btrfs_destroy_delayed_inodes(root);
  3624. btrfs_assert_delayed_root_empty(root);
  3625. btrfs_destroy_pinned_extent(root, root->fs_info->pinned_extents);
  3626. btrfs_destroy_all_delalloc_inodes(root->fs_info);
  3627. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  3628. return 0;
  3629. }
  3630. static struct extent_io_ops btree_extent_io_ops = {
  3631. .readpage_end_io_hook = btree_readpage_end_io_hook,
  3632. .readpage_io_failed_hook = btree_io_failed_hook,
  3633. .submit_bio_hook = btree_submit_bio_hook,
  3634. /* note we're sharing with inode.c for the merge bio hook */
  3635. .merge_bio_hook = btrfs_merge_bio_hook,
  3636. };