disk-io.c 121 KB

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