disk-io.c 124 KB

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