disk-io.c 127 KB

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