disk-io.c 124 KB

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