disk-io.c 123 KB

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