xfs_log_recover.c 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_trans.h"
  31. #include "xfs_log.h"
  32. #include "xfs_log_priv.h"
  33. #include "xfs_log_recover.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_extfree_item.h"
  36. #include "xfs_trans_priv.h"
  37. #include "xfs_alloc.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_quota.h"
  40. #include "xfs_cksum.h"
  41. #include "xfs_trace.h"
  42. #include "xfs_icache.h"
  43. #include "xfs_bmap_btree.h"
  44. #include "xfs_error.h"
  45. #include "xfs_dir2.h"
  46. #define BLK_AVG(blk1, blk2) ((blk1+blk2) >> 1)
  47. STATIC int
  48. xlog_find_zeroed(
  49. struct xlog *,
  50. xfs_daddr_t *);
  51. STATIC int
  52. xlog_clear_stale_blocks(
  53. struct xlog *,
  54. xfs_lsn_t);
  55. #if defined(DEBUG)
  56. STATIC void
  57. xlog_recover_check_summary(
  58. struct xlog *);
  59. #else
  60. #define xlog_recover_check_summary(log)
  61. #endif
  62. /*
  63. * This structure is used during recovery to record the buf log items which
  64. * have been canceled and should not be replayed.
  65. */
  66. struct xfs_buf_cancel {
  67. xfs_daddr_t bc_blkno;
  68. uint bc_len;
  69. int bc_refcount;
  70. struct list_head bc_list;
  71. };
  72. /*
  73. * Sector aligned buffer routines for buffer create/read/write/access
  74. */
  75. /*
  76. * Verify the given count of basic blocks is valid number of blocks
  77. * to specify for an operation involving the given XFS log buffer.
  78. * Returns nonzero if the count is valid, 0 otherwise.
  79. */
  80. static inline int
  81. xlog_buf_bbcount_valid(
  82. struct xlog *log,
  83. int bbcount)
  84. {
  85. return bbcount > 0 && bbcount <= log->l_logBBsize;
  86. }
  87. /*
  88. * Allocate a buffer to hold log data. The buffer needs to be able
  89. * to map to a range of nbblks basic blocks at any valid (basic
  90. * block) offset within the log.
  91. */
  92. STATIC xfs_buf_t *
  93. xlog_get_bp(
  94. struct xlog *log,
  95. int nbblks)
  96. {
  97. struct xfs_buf *bp;
  98. if (!xlog_buf_bbcount_valid(log, nbblks)) {
  99. xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
  100. nbblks);
  101. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
  102. return NULL;
  103. }
  104. /*
  105. * We do log I/O in units of log sectors (a power-of-2
  106. * multiple of the basic block size), so we round up the
  107. * requested size to accommodate the basic blocks required
  108. * for complete log sectors.
  109. *
  110. * In addition, the buffer may be used for a non-sector-
  111. * aligned block offset, in which case an I/O of the
  112. * requested size could extend beyond the end of the
  113. * buffer. If the requested size is only 1 basic block it
  114. * will never straddle a sector boundary, so this won't be
  115. * an issue. Nor will this be a problem if the log I/O is
  116. * done in basic blocks (sector size 1). But otherwise we
  117. * extend the buffer by one extra log sector to ensure
  118. * there's space to accommodate this possibility.
  119. */
  120. if (nbblks > 1 && log->l_sectBBsize > 1)
  121. nbblks += log->l_sectBBsize;
  122. nbblks = round_up(nbblks, log->l_sectBBsize);
  123. bp = xfs_buf_get_uncached(log->l_mp->m_logdev_targp, nbblks, 0);
  124. if (bp)
  125. xfs_buf_unlock(bp);
  126. return bp;
  127. }
  128. STATIC void
  129. xlog_put_bp(
  130. xfs_buf_t *bp)
  131. {
  132. xfs_buf_free(bp);
  133. }
  134. /*
  135. * Return the address of the start of the given block number's data
  136. * in a log buffer. The buffer covers a log sector-aligned region.
  137. */
  138. STATIC char *
  139. xlog_align(
  140. struct xlog *log,
  141. xfs_daddr_t blk_no,
  142. int nbblks,
  143. struct xfs_buf *bp)
  144. {
  145. xfs_daddr_t offset = blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1);
  146. ASSERT(offset + nbblks <= bp->b_length);
  147. return bp->b_addr + BBTOB(offset);
  148. }
  149. /*
  150. * nbblks should be uint, but oh well. Just want to catch that 32-bit length.
  151. */
  152. STATIC int
  153. xlog_bread_noalign(
  154. struct xlog *log,
  155. xfs_daddr_t blk_no,
  156. int nbblks,
  157. struct xfs_buf *bp)
  158. {
  159. int error;
  160. if (!xlog_buf_bbcount_valid(log, nbblks)) {
  161. xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
  162. nbblks);
  163. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
  164. return -EFSCORRUPTED;
  165. }
  166. blk_no = round_down(blk_no, log->l_sectBBsize);
  167. nbblks = round_up(nbblks, log->l_sectBBsize);
  168. ASSERT(nbblks > 0);
  169. ASSERT(nbblks <= bp->b_length);
  170. XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
  171. XFS_BUF_READ(bp);
  172. bp->b_io_length = nbblks;
  173. bp->b_error = 0;
  174. error = xfs_buf_submit_wait(bp);
  175. if (error && !XFS_FORCED_SHUTDOWN(log->l_mp))
  176. xfs_buf_ioerror_alert(bp, __func__);
  177. return error;
  178. }
  179. STATIC int
  180. xlog_bread(
  181. struct xlog *log,
  182. xfs_daddr_t blk_no,
  183. int nbblks,
  184. struct xfs_buf *bp,
  185. char **offset)
  186. {
  187. int error;
  188. error = xlog_bread_noalign(log, blk_no, nbblks, bp);
  189. if (error)
  190. return error;
  191. *offset = xlog_align(log, blk_no, nbblks, bp);
  192. return 0;
  193. }
  194. /*
  195. * Read at an offset into the buffer. Returns with the buffer in it's original
  196. * state regardless of the result of the read.
  197. */
  198. STATIC int
  199. xlog_bread_offset(
  200. struct xlog *log,
  201. xfs_daddr_t blk_no, /* block to read from */
  202. int nbblks, /* blocks to read */
  203. struct xfs_buf *bp,
  204. char *offset)
  205. {
  206. char *orig_offset = bp->b_addr;
  207. int orig_len = BBTOB(bp->b_length);
  208. int error, error2;
  209. error = xfs_buf_associate_memory(bp, offset, BBTOB(nbblks));
  210. if (error)
  211. return error;
  212. error = xlog_bread_noalign(log, blk_no, nbblks, bp);
  213. /* must reset buffer pointer even on error */
  214. error2 = xfs_buf_associate_memory(bp, orig_offset, orig_len);
  215. if (error)
  216. return error;
  217. return error2;
  218. }
  219. /*
  220. * Write out the buffer at the given block for the given number of blocks.
  221. * The buffer is kept locked across the write and is returned locked.
  222. * This can only be used for synchronous log writes.
  223. */
  224. STATIC int
  225. xlog_bwrite(
  226. struct xlog *log,
  227. xfs_daddr_t blk_no,
  228. int nbblks,
  229. struct xfs_buf *bp)
  230. {
  231. int error;
  232. if (!xlog_buf_bbcount_valid(log, nbblks)) {
  233. xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
  234. nbblks);
  235. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
  236. return -EFSCORRUPTED;
  237. }
  238. blk_no = round_down(blk_no, log->l_sectBBsize);
  239. nbblks = round_up(nbblks, log->l_sectBBsize);
  240. ASSERT(nbblks > 0);
  241. ASSERT(nbblks <= bp->b_length);
  242. XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
  243. XFS_BUF_ZEROFLAGS(bp);
  244. xfs_buf_hold(bp);
  245. xfs_buf_lock(bp);
  246. bp->b_io_length = nbblks;
  247. bp->b_error = 0;
  248. error = xfs_bwrite(bp);
  249. if (error)
  250. xfs_buf_ioerror_alert(bp, __func__);
  251. xfs_buf_relse(bp);
  252. return error;
  253. }
  254. #ifdef DEBUG
  255. /*
  256. * dump debug superblock and log record information
  257. */
  258. STATIC void
  259. xlog_header_check_dump(
  260. xfs_mount_t *mp,
  261. xlog_rec_header_t *head)
  262. {
  263. xfs_debug(mp, "%s: SB : uuid = %pU, fmt = %d",
  264. __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
  265. xfs_debug(mp, " log : uuid = %pU, fmt = %d",
  266. &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
  267. }
  268. #else
  269. #define xlog_header_check_dump(mp, head)
  270. #endif
  271. /*
  272. * check log record header for recovery
  273. */
  274. STATIC int
  275. xlog_header_check_recover(
  276. xfs_mount_t *mp,
  277. xlog_rec_header_t *head)
  278. {
  279. ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
  280. /*
  281. * IRIX doesn't write the h_fmt field and leaves it zeroed
  282. * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
  283. * a dirty log created in IRIX.
  284. */
  285. if (unlikely(head->h_fmt != cpu_to_be32(XLOG_FMT))) {
  286. xfs_warn(mp,
  287. "dirty log written in incompatible format - can't recover");
  288. xlog_header_check_dump(mp, head);
  289. XFS_ERROR_REPORT("xlog_header_check_recover(1)",
  290. XFS_ERRLEVEL_HIGH, mp);
  291. return -EFSCORRUPTED;
  292. } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
  293. xfs_warn(mp,
  294. "dirty log entry has mismatched uuid - can't recover");
  295. xlog_header_check_dump(mp, head);
  296. XFS_ERROR_REPORT("xlog_header_check_recover(2)",
  297. XFS_ERRLEVEL_HIGH, mp);
  298. return -EFSCORRUPTED;
  299. }
  300. return 0;
  301. }
  302. /*
  303. * read the head block of the log and check the header
  304. */
  305. STATIC int
  306. xlog_header_check_mount(
  307. xfs_mount_t *mp,
  308. xlog_rec_header_t *head)
  309. {
  310. ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
  311. if (uuid_is_nil(&head->h_fs_uuid)) {
  312. /*
  313. * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
  314. * h_fs_uuid is nil, we assume this log was last mounted
  315. * by IRIX and continue.
  316. */
  317. xfs_warn(mp, "nil uuid in log - IRIX style log");
  318. } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
  319. xfs_warn(mp, "log has mismatched uuid - can't recover");
  320. xlog_header_check_dump(mp, head);
  321. XFS_ERROR_REPORT("xlog_header_check_mount",
  322. XFS_ERRLEVEL_HIGH, mp);
  323. return -EFSCORRUPTED;
  324. }
  325. return 0;
  326. }
  327. STATIC void
  328. xlog_recover_iodone(
  329. struct xfs_buf *bp)
  330. {
  331. if (bp->b_error) {
  332. /*
  333. * We're not going to bother about retrying
  334. * this during recovery. One strike!
  335. */
  336. if (!XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  337. xfs_buf_ioerror_alert(bp, __func__);
  338. xfs_force_shutdown(bp->b_target->bt_mount,
  339. SHUTDOWN_META_IO_ERROR);
  340. }
  341. }
  342. bp->b_iodone = NULL;
  343. xfs_buf_ioend(bp);
  344. }
  345. /*
  346. * This routine finds (to an approximation) the first block in the physical
  347. * log which contains the given cycle. It uses a binary search algorithm.
  348. * Note that the algorithm can not be perfect because the disk will not
  349. * necessarily be perfect.
  350. */
  351. STATIC int
  352. xlog_find_cycle_start(
  353. struct xlog *log,
  354. struct xfs_buf *bp,
  355. xfs_daddr_t first_blk,
  356. xfs_daddr_t *last_blk,
  357. uint cycle)
  358. {
  359. char *offset;
  360. xfs_daddr_t mid_blk;
  361. xfs_daddr_t end_blk;
  362. uint mid_cycle;
  363. int error;
  364. end_blk = *last_blk;
  365. mid_blk = BLK_AVG(first_blk, end_blk);
  366. while (mid_blk != first_blk && mid_blk != end_blk) {
  367. error = xlog_bread(log, mid_blk, 1, bp, &offset);
  368. if (error)
  369. return error;
  370. mid_cycle = xlog_get_cycle(offset);
  371. if (mid_cycle == cycle)
  372. end_blk = mid_blk; /* last_half_cycle == mid_cycle */
  373. else
  374. first_blk = mid_blk; /* first_half_cycle == mid_cycle */
  375. mid_blk = BLK_AVG(first_blk, end_blk);
  376. }
  377. ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
  378. (mid_blk == end_blk && mid_blk-1 == first_blk));
  379. *last_blk = end_blk;
  380. return 0;
  381. }
  382. /*
  383. * Check that a range of blocks does not contain stop_on_cycle_no.
  384. * Fill in *new_blk with the block offset where such a block is
  385. * found, or with -1 (an invalid block number) if there is no such
  386. * block in the range. The scan needs to occur from front to back
  387. * and the pointer into the region must be updated since a later
  388. * routine will need to perform another test.
  389. */
  390. STATIC int
  391. xlog_find_verify_cycle(
  392. struct xlog *log,
  393. xfs_daddr_t start_blk,
  394. int nbblks,
  395. uint stop_on_cycle_no,
  396. xfs_daddr_t *new_blk)
  397. {
  398. xfs_daddr_t i, j;
  399. uint cycle;
  400. xfs_buf_t *bp;
  401. xfs_daddr_t bufblks;
  402. char *buf = NULL;
  403. int error = 0;
  404. /*
  405. * Greedily allocate a buffer big enough to handle the full
  406. * range of basic blocks we'll be examining. If that fails,
  407. * try a smaller size. We need to be able to read at least
  408. * a log sector, or we're out of luck.
  409. */
  410. bufblks = 1 << ffs(nbblks);
  411. while (bufblks > log->l_logBBsize)
  412. bufblks >>= 1;
  413. while (!(bp = xlog_get_bp(log, bufblks))) {
  414. bufblks >>= 1;
  415. if (bufblks < log->l_sectBBsize)
  416. return -ENOMEM;
  417. }
  418. for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
  419. int bcount;
  420. bcount = min(bufblks, (start_blk + nbblks - i));
  421. error = xlog_bread(log, i, bcount, bp, &buf);
  422. if (error)
  423. goto out;
  424. for (j = 0; j < bcount; j++) {
  425. cycle = xlog_get_cycle(buf);
  426. if (cycle == stop_on_cycle_no) {
  427. *new_blk = i+j;
  428. goto out;
  429. }
  430. buf += BBSIZE;
  431. }
  432. }
  433. *new_blk = -1;
  434. out:
  435. xlog_put_bp(bp);
  436. return error;
  437. }
  438. /*
  439. * Potentially backup over partial log record write.
  440. *
  441. * In the typical case, last_blk is the number of the block directly after
  442. * a good log record. Therefore, we subtract one to get the block number
  443. * of the last block in the given buffer. extra_bblks contains the number
  444. * of blocks we would have read on a previous read. This happens when the
  445. * last log record is split over the end of the physical log.
  446. *
  447. * extra_bblks is the number of blocks potentially verified on a previous
  448. * call to this routine.
  449. */
  450. STATIC int
  451. xlog_find_verify_log_record(
  452. struct xlog *log,
  453. xfs_daddr_t start_blk,
  454. xfs_daddr_t *last_blk,
  455. int extra_bblks)
  456. {
  457. xfs_daddr_t i;
  458. xfs_buf_t *bp;
  459. char *offset = NULL;
  460. xlog_rec_header_t *head = NULL;
  461. int error = 0;
  462. int smallmem = 0;
  463. int num_blks = *last_blk - start_blk;
  464. int xhdrs;
  465. ASSERT(start_blk != 0 || *last_blk != start_blk);
  466. if (!(bp = xlog_get_bp(log, num_blks))) {
  467. if (!(bp = xlog_get_bp(log, 1)))
  468. return -ENOMEM;
  469. smallmem = 1;
  470. } else {
  471. error = xlog_bread(log, start_blk, num_blks, bp, &offset);
  472. if (error)
  473. goto out;
  474. offset += ((num_blks - 1) << BBSHIFT);
  475. }
  476. for (i = (*last_blk) - 1; i >= 0; i--) {
  477. if (i < start_blk) {
  478. /* valid log record not found */
  479. xfs_warn(log->l_mp,
  480. "Log inconsistent (didn't find previous header)");
  481. ASSERT(0);
  482. error = -EIO;
  483. goto out;
  484. }
  485. if (smallmem) {
  486. error = xlog_bread(log, i, 1, bp, &offset);
  487. if (error)
  488. goto out;
  489. }
  490. head = (xlog_rec_header_t *)offset;
  491. if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
  492. break;
  493. if (!smallmem)
  494. offset -= BBSIZE;
  495. }
  496. /*
  497. * We hit the beginning of the physical log & still no header. Return
  498. * to caller. If caller can handle a return of -1, then this routine
  499. * will be called again for the end of the physical log.
  500. */
  501. if (i == -1) {
  502. error = 1;
  503. goto out;
  504. }
  505. /*
  506. * We have the final block of the good log (the first block
  507. * of the log record _before_ the head. So we check the uuid.
  508. */
  509. if ((error = xlog_header_check_mount(log->l_mp, head)))
  510. goto out;
  511. /*
  512. * We may have found a log record header before we expected one.
  513. * last_blk will be the 1st block # with a given cycle #. We may end
  514. * up reading an entire log record. In this case, we don't want to
  515. * reset last_blk. Only when last_blk points in the middle of a log
  516. * record do we update last_blk.
  517. */
  518. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  519. uint h_size = be32_to_cpu(head->h_size);
  520. xhdrs = h_size / XLOG_HEADER_CYCLE_SIZE;
  521. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  522. xhdrs++;
  523. } else {
  524. xhdrs = 1;
  525. }
  526. if (*last_blk - i + extra_bblks !=
  527. BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
  528. *last_blk = i;
  529. out:
  530. xlog_put_bp(bp);
  531. return error;
  532. }
  533. /*
  534. * Head is defined to be the point of the log where the next log write
  535. * could go. This means that incomplete LR writes at the end are
  536. * eliminated when calculating the head. We aren't guaranteed that previous
  537. * LR have complete transactions. We only know that a cycle number of
  538. * current cycle number -1 won't be present in the log if we start writing
  539. * from our current block number.
  540. *
  541. * last_blk contains the block number of the first block with a given
  542. * cycle number.
  543. *
  544. * Return: zero if normal, non-zero if error.
  545. */
  546. STATIC int
  547. xlog_find_head(
  548. struct xlog *log,
  549. xfs_daddr_t *return_head_blk)
  550. {
  551. xfs_buf_t *bp;
  552. char *offset;
  553. xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
  554. int num_scan_bblks;
  555. uint first_half_cycle, last_half_cycle;
  556. uint stop_on_cycle;
  557. int error, log_bbnum = log->l_logBBsize;
  558. /* Is the end of the log device zeroed? */
  559. error = xlog_find_zeroed(log, &first_blk);
  560. if (error < 0) {
  561. xfs_warn(log->l_mp, "empty log check failed");
  562. return error;
  563. }
  564. if (error == 1) {
  565. *return_head_blk = first_blk;
  566. /* Is the whole lot zeroed? */
  567. if (!first_blk) {
  568. /* Linux XFS shouldn't generate totally zeroed logs -
  569. * mkfs etc write a dummy unmount record to a fresh
  570. * log so we can store the uuid in there
  571. */
  572. xfs_warn(log->l_mp, "totally zeroed log");
  573. }
  574. return 0;
  575. }
  576. first_blk = 0; /* get cycle # of 1st block */
  577. bp = xlog_get_bp(log, 1);
  578. if (!bp)
  579. return -ENOMEM;
  580. error = xlog_bread(log, 0, 1, bp, &offset);
  581. if (error)
  582. goto bp_err;
  583. first_half_cycle = xlog_get_cycle(offset);
  584. last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
  585. error = xlog_bread(log, last_blk, 1, bp, &offset);
  586. if (error)
  587. goto bp_err;
  588. last_half_cycle = xlog_get_cycle(offset);
  589. ASSERT(last_half_cycle != 0);
  590. /*
  591. * If the 1st half cycle number is equal to the last half cycle number,
  592. * then the entire log is stamped with the same cycle number. In this
  593. * case, head_blk can't be set to zero (which makes sense). The below
  594. * math doesn't work out properly with head_blk equal to zero. Instead,
  595. * we set it to log_bbnum which is an invalid block number, but this
  596. * value makes the math correct. If head_blk doesn't changed through
  597. * all the tests below, *head_blk is set to zero at the very end rather
  598. * than log_bbnum. In a sense, log_bbnum and zero are the same block
  599. * in a circular file.
  600. */
  601. if (first_half_cycle == last_half_cycle) {
  602. /*
  603. * In this case we believe that the entire log should have
  604. * cycle number last_half_cycle. We need to scan backwards
  605. * from the end verifying that there are no holes still
  606. * containing last_half_cycle - 1. If we find such a hole,
  607. * then the start of that hole will be the new head. The
  608. * simple case looks like
  609. * x | x ... | x - 1 | x
  610. * Another case that fits this picture would be
  611. * x | x + 1 | x ... | x
  612. * In this case the head really is somewhere at the end of the
  613. * log, as one of the latest writes at the beginning was
  614. * incomplete.
  615. * One more case is
  616. * x | x + 1 | x ... | x - 1 | x
  617. * This is really the combination of the above two cases, and
  618. * the head has to end up at the start of the x-1 hole at the
  619. * end of the log.
  620. *
  621. * In the 256k log case, we will read from the beginning to the
  622. * end of the log and search for cycle numbers equal to x-1.
  623. * We don't worry about the x+1 blocks that we encounter,
  624. * because we know that they cannot be the head since the log
  625. * started with x.
  626. */
  627. head_blk = log_bbnum;
  628. stop_on_cycle = last_half_cycle - 1;
  629. } else {
  630. /*
  631. * In this case we want to find the first block with cycle
  632. * number matching last_half_cycle. We expect the log to be
  633. * some variation on
  634. * x + 1 ... | x ... | x
  635. * The first block with cycle number x (last_half_cycle) will
  636. * be where the new head belongs. First we do a binary search
  637. * for the first occurrence of last_half_cycle. The binary
  638. * search may not be totally accurate, so then we scan back
  639. * from there looking for occurrences of last_half_cycle before
  640. * us. If that backwards scan wraps around the beginning of
  641. * the log, then we look for occurrences of last_half_cycle - 1
  642. * at the end of the log. The cases we're looking for look
  643. * like
  644. * v binary search stopped here
  645. * x + 1 ... | x | x + 1 | x ... | x
  646. * ^ but we want to locate this spot
  647. * or
  648. * <---------> less than scan distance
  649. * x + 1 ... | x ... | x - 1 | x
  650. * ^ we want to locate this spot
  651. */
  652. stop_on_cycle = last_half_cycle;
  653. if ((error = xlog_find_cycle_start(log, bp, first_blk,
  654. &head_blk, last_half_cycle)))
  655. goto bp_err;
  656. }
  657. /*
  658. * Now validate the answer. Scan back some number of maximum possible
  659. * blocks and make sure each one has the expected cycle number. The
  660. * maximum is determined by the total possible amount of buffering
  661. * in the in-core log. The following number can be made tighter if
  662. * we actually look at the block size of the filesystem.
  663. */
  664. num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
  665. if (head_blk >= num_scan_bblks) {
  666. /*
  667. * We are guaranteed that the entire check can be performed
  668. * in one buffer.
  669. */
  670. start_blk = head_blk - num_scan_bblks;
  671. if ((error = xlog_find_verify_cycle(log,
  672. start_blk, num_scan_bblks,
  673. stop_on_cycle, &new_blk)))
  674. goto bp_err;
  675. if (new_blk != -1)
  676. head_blk = new_blk;
  677. } else { /* need to read 2 parts of log */
  678. /*
  679. * We are going to scan backwards in the log in two parts.
  680. * First we scan the physical end of the log. In this part
  681. * of the log, we are looking for blocks with cycle number
  682. * last_half_cycle - 1.
  683. * If we find one, then we know that the log starts there, as
  684. * we've found a hole that didn't get written in going around
  685. * the end of the physical log. The simple case for this is
  686. * x + 1 ... | x ... | x - 1 | x
  687. * <---------> less than scan distance
  688. * If all of the blocks at the end of the log have cycle number
  689. * last_half_cycle, then we check the blocks at the start of
  690. * the log looking for occurrences of last_half_cycle. If we
  691. * find one, then our current estimate for the location of the
  692. * first occurrence of last_half_cycle is wrong and we move
  693. * back to the hole we've found. This case looks like
  694. * x + 1 ... | x | x + 1 | x ...
  695. * ^ binary search stopped here
  696. * Another case we need to handle that only occurs in 256k
  697. * logs is
  698. * x + 1 ... | x ... | x+1 | x ...
  699. * ^ binary search stops here
  700. * In a 256k log, the scan at the end of the log will see the
  701. * x + 1 blocks. We need to skip past those since that is
  702. * certainly not the head of the log. By searching for
  703. * last_half_cycle-1 we accomplish that.
  704. */
  705. ASSERT(head_blk <= INT_MAX &&
  706. (xfs_daddr_t) num_scan_bblks >= head_blk);
  707. start_blk = log_bbnum - (num_scan_bblks - head_blk);
  708. if ((error = xlog_find_verify_cycle(log, start_blk,
  709. num_scan_bblks - (int)head_blk,
  710. (stop_on_cycle - 1), &new_blk)))
  711. goto bp_err;
  712. if (new_blk != -1) {
  713. head_blk = new_blk;
  714. goto validate_head;
  715. }
  716. /*
  717. * Scan beginning of log now. The last part of the physical
  718. * log is good. This scan needs to verify that it doesn't find
  719. * the last_half_cycle.
  720. */
  721. start_blk = 0;
  722. ASSERT(head_blk <= INT_MAX);
  723. if ((error = xlog_find_verify_cycle(log,
  724. start_blk, (int)head_blk,
  725. stop_on_cycle, &new_blk)))
  726. goto bp_err;
  727. if (new_blk != -1)
  728. head_blk = new_blk;
  729. }
  730. validate_head:
  731. /*
  732. * Now we need to make sure head_blk is not pointing to a block in
  733. * the middle of a log record.
  734. */
  735. num_scan_bblks = XLOG_REC_SHIFT(log);
  736. if (head_blk >= num_scan_bblks) {
  737. start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
  738. /* start ptr at last block ptr before head_blk */
  739. error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  740. if (error == 1)
  741. error = -EIO;
  742. if (error)
  743. goto bp_err;
  744. } else {
  745. start_blk = 0;
  746. ASSERT(head_blk <= INT_MAX);
  747. error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  748. if (error < 0)
  749. goto bp_err;
  750. if (error == 1) {
  751. /* We hit the beginning of the log during our search */
  752. start_blk = log_bbnum - (num_scan_bblks - head_blk);
  753. new_blk = log_bbnum;
  754. ASSERT(start_blk <= INT_MAX &&
  755. (xfs_daddr_t) log_bbnum-start_blk >= 0);
  756. ASSERT(head_blk <= INT_MAX);
  757. error = xlog_find_verify_log_record(log, start_blk,
  758. &new_blk, (int)head_blk);
  759. if (error == 1)
  760. error = -EIO;
  761. if (error)
  762. goto bp_err;
  763. if (new_blk != log_bbnum)
  764. head_blk = new_blk;
  765. } else if (error)
  766. goto bp_err;
  767. }
  768. xlog_put_bp(bp);
  769. if (head_blk == log_bbnum)
  770. *return_head_blk = 0;
  771. else
  772. *return_head_blk = head_blk;
  773. /*
  774. * When returning here, we have a good block number. Bad block
  775. * means that during a previous crash, we didn't have a clean break
  776. * from cycle number N to cycle number N-1. In this case, we need
  777. * to find the first block with cycle number N-1.
  778. */
  779. return 0;
  780. bp_err:
  781. xlog_put_bp(bp);
  782. if (error)
  783. xfs_warn(log->l_mp, "failed to find log head");
  784. return error;
  785. }
  786. /*
  787. * Find the sync block number or the tail of the log.
  788. *
  789. * This will be the block number of the last record to have its
  790. * associated buffers synced to disk. Every log record header has
  791. * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
  792. * to get a sync block number. The only concern is to figure out which
  793. * log record header to believe.
  794. *
  795. * The following algorithm uses the log record header with the largest
  796. * lsn. The entire log record does not need to be valid. We only care
  797. * that the header is valid.
  798. *
  799. * We could speed up search by using current head_blk buffer, but it is not
  800. * available.
  801. */
  802. STATIC int
  803. xlog_find_tail(
  804. struct xlog *log,
  805. xfs_daddr_t *head_blk,
  806. xfs_daddr_t *tail_blk)
  807. {
  808. xlog_rec_header_t *rhead;
  809. xlog_op_header_t *op_head;
  810. char *offset = NULL;
  811. xfs_buf_t *bp;
  812. int error, i, found;
  813. xfs_daddr_t umount_data_blk;
  814. xfs_daddr_t after_umount_blk;
  815. xfs_lsn_t tail_lsn;
  816. int hblks;
  817. found = 0;
  818. /*
  819. * Find previous log record
  820. */
  821. if ((error = xlog_find_head(log, head_blk)))
  822. return error;
  823. bp = xlog_get_bp(log, 1);
  824. if (!bp)
  825. return -ENOMEM;
  826. if (*head_blk == 0) { /* special case */
  827. error = xlog_bread(log, 0, 1, bp, &offset);
  828. if (error)
  829. goto done;
  830. if (xlog_get_cycle(offset) == 0) {
  831. *tail_blk = 0;
  832. /* leave all other log inited values alone */
  833. goto done;
  834. }
  835. }
  836. /*
  837. * Search backwards looking for log record header block
  838. */
  839. ASSERT(*head_blk < INT_MAX);
  840. for (i = (int)(*head_blk) - 1; i >= 0; i--) {
  841. error = xlog_bread(log, i, 1, bp, &offset);
  842. if (error)
  843. goto done;
  844. if (*(__be32 *)offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  845. found = 1;
  846. break;
  847. }
  848. }
  849. /*
  850. * If we haven't found the log record header block, start looking
  851. * again from the end of the physical log. XXXmiken: There should be
  852. * a check here to make sure we didn't search more than N blocks in
  853. * the previous code.
  854. */
  855. if (!found) {
  856. for (i = log->l_logBBsize - 1; i >= (int)(*head_blk); i--) {
  857. error = xlog_bread(log, i, 1, bp, &offset);
  858. if (error)
  859. goto done;
  860. if (*(__be32 *)offset ==
  861. cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  862. found = 2;
  863. break;
  864. }
  865. }
  866. }
  867. if (!found) {
  868. xfs_warn(log->l_mp, "%s: couldn't find sync record", __func__);
  869. xlog_put_bp(bp);
  870. ASSERT(0);
  871. return -EIO;
  872. }
  873. /* find blk_no of tail of log */
  874. rhead = (xlog_rec_header_t *)offset;
  875. *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
  876. /*
  877. * Reset log values according to the state of the log when we
  878. * crashed. In the case where head_blk == 0, we bump curr_cycle
  879. * one because the next write starts a new cycle rather than
  880. * continuing the cycle of the last good log record. At this
  881. * point we have guaranteed that all partial log records have been
  882. * accounted for. Therefore, we know that the last good log record
  883. * written was complete and ended exactly on the end boundary
  884. * of the physical log.
  885. */
  886. log->l_prev_block = i;
  887. log->l_curr_block = (int)*head_blk;
  888. log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
  889. if (found == 2)
  890. log->l_curr_cycle++;
  891. atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
  892. atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
  893. xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
  894. BBTOB(log->l_curr_block));
  895. xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
  896. BBTOB(log->l_curr_block));
  897. /*
  898. * Look for unmount record. If we find it, then we know there
  899. * was a clean unmount. Since 'i' could be the last block in
  900. * the physical log, we convert to a log block before comparing
  901. * to the head_blk.
  902. *
  903. * Save the current tail lsn to use to pass to
  904. * xlog_clear_stale_blocks() below. We won't want to clear the
  905. * unmount record if there is one, so we pass the lsn of the
  906. * unmount record rather than the block after it.
  907. */
  908. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  909. int h_size = be32_to_cpu(rhead->h_size);
  910. int h_version = be32_to_cpu(rhead->h_version);
  911. if ((h_version & XLOG_VERSION_2) &&
  912. (h_size > XLOG_HEADER_CYCLE_SIZE)) {
  913. hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
  914. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  915. hblks++;
  916. } else {
  917. hblks = 1;
  918. }
  919. } else {
  920. hblks = 1;
  921. }
  922. after_umount_blk = (i + hblks + (int)
  923. BTOBB(be32_to_cpu(rhead->h_len))) % log->l_logBBsize;
  924. tail_lsn = atomic64_read(&log->l_tail_lsn);
  925. if (*head_blk == after_umount_blk &&
  926. be32_to_cpu(rhead->h_num_logops) == 1) {
  927. umount_data_blk = (i + hblks) % log->l_logBBsize;
  928. error = xlog_bread(log, umount_data_blk, 1, bp, &offset);
  929. if (error)
  930. goto done;
  931. op_head = (xlog_op_header_t *)offset;
  932. if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
  933. /*
  934. * Set tail and last sync so that newly written
  935. * log records will point recovery to after the
  936. * current unmount record.
  937. */
  938. xlog_assign_atomic_lsn(&log->l_tail_lsn,
  939. log->l_curr_cycle, after_umount_blk);
  940. xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
  941. log->l_curr_cycle, after_umount_blk);
  942. *tail_blk = after_umount_blk;
  943. /*
  944. * Note that the unmount was clean. If the unmount
  945. * was not clean, we need to know this to rebuild the
  946. * superblock counters from the perag headers if we
  947. * have a filesystem using non-persistent counters.
  948. */
  949. log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
  950. }
  951. }
  952. /*
  953. * Make sure that there are no blocks in front of the head
  954. * with the same cycle number as the head. This can happen
  955. * because we allow multiple outstanding log writes concurrently,
  956. * and the later writes might make it out before earlier ones.
  957. *
  958. * We use the lsn from before modifying it so that we'll never
  959. * overwrite the unmount record after a clean unmount.
  960. *
  961. * Do this only if we are going to recover the filesystem
  962. *
  963. * NOTE: This used to say "if (!readonly)"
  964. * However on Linux, we can & do recover a read-only filesystem.
  965. * We only skip recovery if NORECOVERY is specified on mount,
  966. * in which case we would not be here.
  967. *
  968. * But... if the -device- itself is readonly, just skip this.
  969. * We can't recover this device anyway, so it won't matter.
  970. */
  971. if (!xfs_readonly_buftarg(log->l_mp->m_logdev_targp))
  972. error = xlog_clear_stale_blocks(log, tail_lsn);
  973. done:
  974. xlog_put_bp(bp);
  975. if (error)
  976. xfs_warn(log->l_mp, "failed to locate log tail");
  977. return error;
  978. }
  979. /*
  980. * Is the log zeroed at all?
  981. *
  982. * The last binary search should be changed to perform an X block read
  983. * once X becomes small enough. You can then search linearly through
  984. * the X blocks. This will cut down on the number of reads we need to do.
  985. *
  986. * If the log is partially zeroed, this routine will pass back the blkno
  987. * of the first block with cycle number 0. It won't have a complete LR
  988. * preceding it.
  989. *
  990. * Return:
  991. * 0 => the log is completely written to
  992. * 1 => use *blk_no as the first block of the log
  993. * <0 => error has occurred
  994. */
  995. STATIC int
  996. xlog_find_zeroed(
  997. struct xlog *log,
  998. xfs_daddr_t *blk_no)
  999. {
  1000. xfs_buf_t *bp;
  1001. char *offset;
  1002. uint first_cycle, last_cycle;
  1003. xfs_daddr_t new_blk, last_blk, start_blk;
  1004. xfs_daddr_t num_scan_bblks;
  1005. int error, log_bbnum = log->l_logBBsize;
  1006. *blk_no = 0;
  1007. /* check totally zeroed log */
  1008. bp = xlog_get_bp(log, 1);
  1009. if (!bp)
  1010. return -ENOMEM;
  1011. error = xlog_bread(log, 0, 1, bp, &offset);
  1012. if (error)
  1013. goto bp_err;
  1014. first_cycle = xlog_get_cycle(offset);
  1015. if (first_cycle == 0) { /* completely zeroed log */
  1016. *blk_no = 0;
  1017. xlog_put_bp(bp);
  1018. return 1;
  1019. }
  1020. /* check partially zeroed log */
  1021. error = xlog_bread(log, log_bbnum-1, 1, bp, &offset);
  1022. if (error)
  1023. goto bp_err;
  1024. last_cycle = xlog_get_cycle(offset);
  1025. if (last_cycle != 0) { /* log completely written to */
  1026. xlog_put_bp(bp);
  1027. return 0;
  1028. } else if (first_cycle != 1) {
  1029. /*
  1030. * If the cycle of the last block is zero, the cycle of
  1031. * the first block must be 1. If it's not, maybe we're
  1032. * not looking at a log... Bail out.
  1033. */
  1034. xfs_warn(log->l_mp,
  1035. "Log inconsistent or not a log (last==0, first!=1)");
  1036. error = -EINVAL;
  1037. goto bp_err;
  1038. }
  1039. /* we have a partially zeroed log */
  1040. last_blk = log_bbnum-1;
  1041. if ((error = xlog_find_cycle_start(log, bp, 0, &last_blk, 0)))
  1042. goto bp_err;
  1043. /*
  1044. * Validate the answer. Because there is no way to guarantee that
  1045. * the entire log is made up of log records which are the same size,
  1046. * we scan over the defined maximum blocks. At this point, the maximum
  1047. * is not chosen to mean anything special. XXXmiken
  1048. */
  1049. num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
  1050. ASSERT(num_scan_bblks <= INT_MAX);
  1051. if (last_blk < num_scan_bblks)
  1052. num_scan_bblks = last_blk;
  1053. start_blk = last_blk - num_scan_bblks;
  1054. /*
  1055. * We search for any instances of cycle number 0 that occur before
  1056. * our current estimate of the head. What we're trying to detect is
  1057. * 1 ... | 0 | 1 | 0...
  1058. * ^ binary search ends here
  1059. */
  1060. if ((error = xlog_find_verify_cycle(log, start_blk,
  1061. (int)num_scan_bblks, 0, &new_blk)))
  1062. goto bp_err;
  1063. if (new_blk != -1)
  1064. last_blk = new_blk;
  1065. /*
  1066. * Potentially backup over partial log record write. We don't need
  1067. * to search the end of the log because we know it is zero.
  1068. */
  1069. error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
  1070. if (error == 1)
  1071. error = -EIO;
  1072. if (error)
  1073. goto bp_err;
  1074. *blk_no = last_blk;
  1075. bp_err:
  1076. xlog_put_bp(bp);
  1077. if (error)
  1078. return error;
  1079. return 1;
  1080. }
  1081. /*
  1082. * These are simple subroutines used by xlog_clear_stale_blocks() below
  1083. * to initialize a buffer full of empty log record headers and write
  1084. * them into the log.
  1085. */
  1086. STATIC void
  1087. xlog_add_record(
  1088. struct xlog *log,
  1089. char *buf,
  1090. int cycle,
  1091. int block,
  1092. int tail_cycle,
  1093. int tail_block)
  1094. {
  1095. xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
  1096. memset(buf, 0, BBSIZE);
  1097. recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
  1098. recp->h_cycle = cpu_to_be32(cycle);
  1099. recp->h_version = cpu_to_be32(
  1100. xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
  1101. recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
  1102. recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
  1103. recp->h_fmt = cpu_to_be32(XLOG_FMT);
  1104. memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
  1105. }
  1106. STATIC int
  1107. xlog_write_log_records(
  1108. struct xlog *log,
  1109. int cycle,
  1110. int start_block,
  1111. int blocks,
  1112. int tail_cycle,
  1113. int tail_block)
  1114. {
  1115. char *offset;
  1116. xfs_buf_t *bp;
  1117. int balign, ealign;
  1118. int sectbb = log->l_sectBBsize;
  1119. int end_block = start_block + blocks;
  1120. int bufblks;
  1121. int error = 0;
  1122. int i, j = 0;
  1123. /*
  1124. * Greedily allocate a buffer big enough to handle the full
  1125. * range of basic blocks to be written. If that fails, try
  1126. * a smaller size. We need to be able to write at least a
  1127. * log sector, or we're out of luck.
  1128. */
  1129. bufblks = 1 << ffs(blocks);
  1130. while (bufblks > log->l_logBBsize)
  1131. bufblks >>= 1;
  1132. while (!(bp = xlog_get_bp(log, bufblks))) {
  1133. bufblks >>= 1;
  1134. if (bufblks < sectbb)
  1135. return -ENOMEM;
  1136. }
  1137. /* We may need to do a read at the start to fill in part of
  1138. * the buffer in the starting sector not covered by the first
  1139. * write below.
  1140. */
  1141. balign = round_down(start_block, sectbb);
  1142. if (balign != start_block) {
  1143. error = xlog_bread_noalign(log, start_block, 1, bp);
  1144. if (error)
  1145. goto out_put_bp;
  1146. j = start_block - balign;
  1147. }
  1148. for (i = start_block; i < end_block; i += bufblks) {
  1149. int bcount, endcount;
  1150. bcount = min(bufblks, end_block - start_block);
  1151. endcount = bcount - j;
  1152. /* We may need to do a read at the end to fill in part of
  1153. * the buffer in the final sector not covered by the write.
  1154. * If this is the same sector as the above read, skip it.
  1155. */
  1156. ealign = round_down(end_block, sectbb);
  1157. if (j == 0 && (start_block + endcount > ealign)) {
  1158. offset = bp->b_addr + BBTOB(ealign - start_block);
  1159. error = xlog_bread_offset(log, ealign, sectbb,
  1160. bp, offset);
  1161. if (error)
  1162. break;
  1163. }
  1164. offset = xlog_align(log, start_block, endcount, bp);
  1165. for (; j < endcount; j++) {
  1166. xlog_add_record(log, offset, cycle, i+j,
  1167. tail_cycle, tail_block);
  1168. offset += BBSIZE;
  1169. }
  1170. error = xlog_bwrite(log, start_block, endcount, bp);
  1171. if (error)
  1172. break;
  1173. start_block += endcount;
  1174. j = 0;
  1175. }
  1176. out_put_bp:
  1177. xlog_put_bp(bp);
  1178. return error;
  1179. }
  1180. /*
  1181. * This routine is called to blow away any incomplete log writes out
  1182. * in front of the log head. We do this so that we won't become confused
  1183. * if we come up, write only a little bit more, and then crash again.
  1184. * If we leave the partial log records out there, this situation could
  1185. * cause us to think those partial writes are valid blocks since they
  1186. * have the current cycle number. We get rid of them by overwriting them
  1187. * with empty log records with the old cycle number rather than the
  1188. * current one.
  1189. *
  1190. * The tail lsn is passed in rather than taken from
  1191. * the log so that we will not write over the unmount record after a
  1192. * clean unmount in a 512 block log. Doing so would leave the log without
  1193. * any valid log records in it until a new one was written. If we crashed
  1194. * during that time we would not be able to recover.
  1195. */
  1196. STATIC int
  1197. xlog_clear_stale_blocks(
  1198. struct xlog *log,
  1199. xfs_lsn_t tail_lsn)
  1200. {
  1201. int tail_cycle, head_cycle;
  1202. int tail_block, head_block;
  1203. int tail_distance, max_distance;
  1204. int distance;
  1205. int error;
  1206. tail_cycle = CYCLE_LSN(tail_lsn);
  1207. tail_block = BLOCK_LSN(tail_lsn);
  1208. head_cycle = log->l_curr_cycle;
  1209. head_block = log->l_curr_block;
  1210. /*
  1211. * Figure out the distance between the new head of the log
  1212. * and the tail. We want to write over any blocks beyond the
  1213. * head that we may have written just before the crash, but
  1214. * we don't want to overwrite the tail of the log.
  1215. */
  1216. if (head_cycle == tail_cycle) {
  1217. /*
  1218. * The tail is behind the head in the physical log,
  1219. * so the distance from the head to the tail is the
  1220. * distance from the head to the end of the log plus
  1221. * the distance from the beginning of the log to the
  1222. * tail.
  1223. */
  1224. if (unlikely(head_block < tail_block || head_block >= log->l_logBBsize)) {
  1225. XFS_ERROR_REPORT("xlog_clear_stale_blocks(1)",
  1226. XFS_ERRLEVEL_LOW, log->l_mp);
  1227. return -EFSCORRUPTED;
  1228. }
  1229. tail_distance = tail_block + (log->l_logBBsize - head_block);
  1230. } else {
  1231. /*
  1232. * The head is behind the tail in the physical log,
  1233. * so the distance from the head to the tail is just
  1234. * the tail block minus the head block.
  1235. */
  1236. if (unlikely(head_block >= tail_block || head_cycle != (tail_cycle + 1))){
  1237. XFS_ERROR_REPORT("xlog_clear_stale_blocks(2)",
  1238. XFS_ERRLEVEL_LOW, log->l_mp);
  1239. return -EFSCORRUPTED;
  1240. }
  1241. tail_distance = tail_block - head_block;
  1242. }
  1243. /*
  1244. * If the head is right up against the tail, we can't clear
  1245. * anything.
  1246. */
  1247. if (tail_distance <= 0) {
  1248. ASSERT(tail_distance == 0);
  1249. return 0;
  1250. }
  1251. max_distance = XLOG_TOTAL_REC_SHIFT(log);
  1252. /*
  1253. * Take the smaller of the maximum amount of outstanding I/O
  1254. * we could have and the distance to the tail to clear out.
  1255. * We take the smaller so that we don't overwrite the tail and
  1256. * we don't waste all day writing from the head to the tail
  1257. * for no reason.
  1258. */
  1259. max_distance = MIN(max_distance, tail_distance);
  1260. if ((head_block + max_distance) <= log->l_logBBsize) {
  1261. /*
  1262. * We can stomp all the blocks we need to without
  1263. * wrapping around the end of the log. Just do it
  1264. * in a single write. Use the cycle number of the
  1265. * current cycle minus one so that the log will look like:
  1266. * n ... | n - 1 ...
  1267. */
  1268. error = xlog_write_log_records(log, (head_cycle - 1),
  1269. head_block, max_distance, tail_cycle,
  1270. tail_block);
  1271. if (error)
  1272. return error;
  1273. } else {
  1274. /*
  1275. * We need to wrap around the end of the physical log in
  1276. * order to clear all the blocks. Do it in two separate
  1277. * I/Os. The first write should be from the head to the
  1278. * end of the physical log, and it should use the current
  1279. * cycle number minus one just like above.
  1280. */
  1281. distance = log->l_logBBsize - head_block;
  1282. error = xlog_write_log_records(log, (head_cycle - 1),
  1283. head_block, distance, tail_cycle,
  1284. tail_block);
  1285. if (error)
  1286. return error;
  1287. /*
  1288. * Now write the blocks at the start of the physical log.
  1289. * This writes the remainder of the blocks we want to clear.
  1290. * It uses the current cycle number since we're now on the
  1291. * same cycle as the head so that we get:
  1292. * n ... n ... | n - 1 ...
  1293. * ^^^^^ blocks we're writing
  1294. */
  1295. distance = max_distance - (log->l_logBBsize - head_block);
  1296. error = xlog_write_log_records(log, head_cycle, 0, distance,
  1297. tail_cycle, tail_block);
  1298. if (error)
  1299. return error;
  1300. }
  1301. return 0;
  1302. }
  1303. /******************************************************************************
  1304. *
  1305. * Log recover routines
  1306. *
  1307. ******************************************************************************
  1308. */
  1309. /*
  1310. * Sort the log items in the transaction.
  1311. *
  1312. * The ordering constraints are defined by the inode allocation and unlink
  1313. * behaviour. The rules are:
  1314. *
  1315. * 1. Every item is only logged once in a given transaction. Hence it
  1316. * represents the last logged state of the item. Hence ordering is
  1317. * dependent on the order in which operations need to be performed so
  1318. * required initial conditions are always met.
  1319. *
  1320. * 2. Cancelled buffers are recorded in pass 1 in a separate table and
  1321. * there's nothing to replay from them so we can simply cull them
  1322. * from the transaction. However, we can't do that until after we've
  1323. * replayed all the other items because they may be dependent on the
  1324. * cancelled buffer and replaying the cancelled buffer can remove it
  1325. * form the cancelled buffer table. Hence they have tobe done last.
  1326. *
  1327. * 3. Inode allocation buffers must be replayed before inode items that
  1328. * read the buffer and replay changes into it. For filesystems using the
  1329. * ICREATE transactions, this means XFS_LI_ICREATE objects need to get
  1330. * treated the same as inode allocation buffers as they create and
  1331. * initialise the buffers directly.
  1332. *
  1333. * 4. Inode unlink buffers must be replayed after inode items are replayed.
  1334. * This ensures that inodes are completely flushed to the inode buffer
  1335. * in a "free" state before we remove the unlinked inode list pointer.
  1336. *
  1337. * Hence the ordering needs to be inode allocation buffers first, inode items
  1338. * second, inode unlink buffers third and cancelled buffers last.
  1339. *
  1340. * But there's a problem with that - we can't tell an inode allocation buffer
  1341. * apart from a regular buffer, so we can't separate them. We can, however,
  1342. * tell an inode unlink buffer from the others, and so we can separate them out
  1343. * from all the other buffers and move them to last.
  1344. *
  1345. * Hence, 4 lists, in order from head to tail:
  1346. * - buffer_list for all buffers except cancelled/inode unlink buffers
  1347. * - item_list for all non-buffer items
  1348. * - inode_buffer_list for inode unlink buffers
  1349. * - cancel_list for the cancelled buffers
  1350. *
  1351. * Note that we add objects to the tail of the lists so that first-to-last
  1352. * ordering is preserved within the lists. Adding objects to the head of the
  1353. * list means when we traverse from the head we walk them in last-to-first
  1354. * order. For cancelled buffers and inode unlink buffers this doesn't matter,
  1355. * but for all other items there may be specific ordering that we need to
  1356. * preserve.
  1357. */
  1358. STATIC int
  1359. xlog_recover_reorder_trans(
  1360. struct xlog *log,
  1361. struct xlog_recover *trans,
  1362. int pass)
  1363. {
  1364. xlog_recover_item_t *item, *n;
  1365. int error = 0;
  1366. LIST_HEAD(sort_list);
  1367. LIST_HEAD(cancel_list);
  1368. LIST_HEAD(buffer_list);
  1369. LIST_HEAD(inode_buffer_list);
  1370. LIST_HEAD(inode_list);
  1371. list_splice_init(&trans->r_itemq, &sort_list);
  1372. list_for_each_entry_safe(item, n, &sort_list, ri_list) {
  1373. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  1374. switch (ITEM_TYPE(item)) {
  1375. case XFS_LI_ICREATE:
  1376. list_move_tail(&item->ri_list, &buffer_list);
  1377. break;
  1378. case XFS_LI_BUF:
  1379. if (buf_f->blf_flags & XFS_BLF_CANCEL) {
  1380. trace_xfs_log_recover_item_reorder_head(log,
  1381. trans, item, pass);
  1382. list_move(&item->ri_list, &cancel_list);
  1383. break;
  1384. }
  1385. if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
  1386. list_move(&item->ri_list, &inode_buffer_list);
  1387. break;
  1388. }
  1389. list_move_tail(&item->ri_list, &buffer_list);
  1390. break;
  1391. case XFS_LI_INODE:
  1392. case XFS_LI_DQUOT:
  1393. case XFS_LI_QUOTAOFF:
  1394. case XFS_LI_EFD:
  1395. case XFS_LI_EFI:
  1396. trace_xfs_log_recover_item_reorder_tail(log,
  1397. trans, item, pass);
  1398. list_move_tail(&item->ri_list, &inode_list);
  1399. break;
  1400. default:
  1401. xfs_warn(log->l_mp,
  1402. "%s: unrecognized type of log operation",
  1403. __func__);
  1404. ASSERT(0);
  1405. /*
  1406. * return the remaining items back to the transaction
  1407. * item list so they can be freed in caller.
  1408. */
  1409. if (!list_empty(&sort_list))
  1410. list_splice_init(&sort_list, &trans->r_itemq);
  1411. error = -EIO;
  1412. goto out;
  1413. }
  1414. }
  1415. out:
  1416. ASSERT(list_empty(&sort_list));
  1417. if (!list_empty(&buffer_list))
  1418. list_splice(&buffer_list, &trans->r_itemq);
  1419. if (!list_empty(&inode_list))
  1420. list_splice_tail(&inode_list, &trans->r_itemq);
  1421. if (!list_empty(&inode_buffer_list))
  1422. list_splice_tail(&inode_buffer_list, &trans->r_itemq);
  1423. if (!list_empty(&cancel_list))
  1424. list_splice_tail(&cancel_list, &trans->r_itemq);
  1425. return error;
  1426. }
  1427. /*
  1428. * Build up the table of buf cancel records so that we don't replay
  1429. * cancelled data in the second pass. For buffer records that are
  1430. * not cancel records, there is nothing to do here so we just return.
  1431. *
  1432. * If we get a cancel record which is already in the table, this indicates
  1433. * that the buffer was cancelled multiple times. In order to ensure
  1434. * that during pass 2 we keep the record in the table until we reach its
  1435. * last occurrence in the log, we keep a reference count in the cancel
  1436. * record in the table to tell us how many times we expect to see this
  1437. * record during the second pass.
  1438. */
  1439. STATIC int
  1440. xlog_recover_buffer_pass1(
  1441. struct xlog *log,
  1442. struct xlog_recover_item *item)
  1443. {
  1444. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  1445. struct list_head *bucket;
  1446. struct xfs_buf_cancel *bcp;
  1447. /*
  1448. * If this isn't a cancel buffer item, then just return.
  1449. */
  1450. if (!(buf_f->blf_flags & XFS_BLF_CANCEL)) {
  1451. trace_xfs_log_recover_buf_not_cancel(log, buf_f);
  1452. return 0;
  1453. }
  1454. /*
  1455. * Insert an xfs_buf_cancel record into the hash table of them.
  1456. * If there is already an identical record, bump its reference count.
  1457. */
  1458. bucket = XLOG_BUF_CANCEL_BUCKET(log, buf_f->blf_blkno);
  1459. list_for_each_entry(bcp, bucket, bc_list) {
  1460. if (bcp->bc_blkno == buf_f->blf_blkno &&
  1461. bcp->bc_len == buf_f->blf_len) {
  1462. bcp->bc_refcount++;
  1463. trace_xfs_log_recover_buf_cancel_ref_inc(log, buf_f);
  1464. return 0;
  1465. }
  1466. }
  1467. bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), KM_SLEEP);
  1468. bcp->bc_blkno = buf_f->blf_blkno;
  1469. bcp->bc_len = buf_f->blf_len;
  1470. bcp->bc_refcount = 1;
  1471. list_add_tail(&bcp->bc_list, bucket);
  1472. trace_xfs_log_recover_buf_cancel_add(log, buf_f);
  1473. return 0;
  1474. }
  1475. /*
  1476. * Check to see whether the buffer being recovered has a corresponding
  1477. * entry in the buffer cancel record table. If it is, return the cancel
  1478. * buffer structure to the caller.
  1479. */
  1480. STATIC struct xfs_buf_cancel *
  1481. xlog_peek_buffer_cancelled(
  1482. struct xlog *log,
  1483. xfs_daddr_t blkno,
  1484. uint len,
  1485. ushort flags)
  1486. {
  1487. struct list_head *bucket;
  1488. struct xfs_buf_cancel *bcp;
  1489. if (!log->l_buf_cancel_table) {
  1490. /* empty table means no cancelled buffers in the log */
  1491. ASSERT(!(flags & XFS_BLF_CANCEL));
  1492. return NULL;
  1493. }
  1494. bucket = XLOG_BUF_CANCEL_BUCKET(log, blkno);
  1495. list_for_each_entry(bcp, bucket, bc_list) {
  1496. if (bcp->bc_blkno == blkno && bcp->bc_len == len)
  1497. return bcp;
  1498. }
  1499. /*
  1500. * We didn't find a corresponding entry in the table, so return 0 so
  1501. * that the buffer is NOT cancelled.
  1502. */
  1503. ASSERT(!(flags & XFS_BLF_CANCEL));
  1504. return NULL;
  1505. }
  1506. /*
  1507. * If the buffer is being cancelled then return 1 so that it will be cancelled,
  1508. * otherwise return 0. If the buffer is actually a buffer cancel item
  1509. * (XFS_BLF_CANCEL is set), then decrement the refcount on the entry in the
  1510. * table and remove it from the table if this is the last reference.
  1511. *
  1512. * We remove the cancel record from the table when we encounter its last
  1513. * occurrence in the log so that if the same buffer is re-used again after its
  1514. * last cancellation we actually replay the changes made at that point.
  1515. */
  1516. STATIC int
  1517. xlog_check_buffer_cancelled(
  1518. struct xlog *log,
  1519. xfs_daddr_t blkno,
  1520. uint len,
  1521. ushort flags)
  1522. {
  1523. struct xfs_buf_cancel *bcp;
  1524. bcp = xlog_peek_buffer_cancelled(log, blkno, len, flags);
  1525. if (!bcp)
  1526. return 0;
  1527. /*
  1528. * We've go a match, so return 1 so that the recovery of this buffer
  1529. * is cancelled. If this buffer is actually a buffer cancel log
  1530. * item, then decrement the refcount on the one in the table and
  1531. * remove it if this is the last reference.
  1532. */
  1533. if (flags & XFS_BLF_CANCEL) {
  1534. if (--bcp->bc_refcount == 0) {
  1535. list_del(&bcp->bc_list);
  1536. kmem_free(bcp);
  1537. }
  1538. }
  1539. return 1;
  1540. }
  1541. /*
  1542. * Perform recovery for a buffer full of inodes. In these buffers, the only
  1543. * data which should be recovered is that which corresponds to the
  1544. * di_next_unlinked pointers in the on disk inode structures. The rest of the
  1545. * data for the inodes is always logged through the inodes themselves rather
  1546. * than the inode buffer and is recovered in xlog_recover_inode_pass2().
  1547. *
  1548. * The only time when buffers full of inodes are fully recovered is when the
  1549. * buffer is full of newly allocated inodes. In this case the buffer will
  1550. * not be marked as an inode buffer and so will be sent to
  1551. * xlog_recover_do_reg_buffer() below during recovery.
  1552. */
  1553. STATIC int
  1554. xlog_recover_do_inode_buffer(
  1555. struct xfs_mount *mp,
  1556. xlog_recover_item_t *item,
  1557. struct xfs_buf *bp,
  1558. xfs_buf_log_format_t *buf_f)
  1559. {
  1560. int i;
  1561. int item_index = 0;
  1562. int bit = 0;
  1563. int nbits = 0;
  1564. int reg_buf_offset = 0;
  1565. int reg_buf_bytes = 0;
  1566. int next_unlinked_offset;
  1567. int inodes_per_buf;
  1568. xfs_agino_t *logged_nextp;
  1569. xfs_agino_t *buffer_nextp;
  1570. trace_xfs_log_recover_buf_inode_buf(mp->m_log, buf_f);
  1571. /*
  1572. * Post recovery validation only works properly on CRC enabled
  1573. * filesystems.
  1574. */
  1575. if (xfs_sb_version_hascrc(&mp->m_sb))
  1576. bp->b_ops = &xfs_inode_buf_ops;
  1577. inodes_per_buf = BBTOB(bp->b_io_length) >> mp->m_sb.sb_inodelog;
  1578. for (i = 0; i < inodes_per_buf; i++) {
  1579. next_unlinked_offset = (i * mp->m_sb.sb_inodesize) +
  1580. offsetof(xfs_dinode_t, di_next_unlinked);
  1581. while (next_unlinked_offset >=
  1582. (reg_buf_offset + reg_buf_bytes)) {
  1583. /*
  1584. * The next di_next_unlinked field is beyond
  1585. * the current logged region. Find the next
  1586. * logged region that contains or is beyond
  1587. * the current di_next_unlinked field.
  1588. */
  1589. bit += nbits;
  1590. bit = xfs_next_bit(buf_f->blf_data_map,
  1591. buf_f->blf_map_size, bit);
  1592. /*
  1593. * If there are no more logged regions in the
  1594. * buffer, then we're done.
  1595. */
  1596. if (bit == -1)
  1597. return 0;
  1598. nbits = xfs_contig_bits(buf_f->blf_data_map,
  1599. buf_f->blf_map_size, bit);
  1600. ASSERT(nbits > 0);
  1601. reg_buf_offset = bit << XFS_BLF_SHIFT;
  1602. reg_buf_bytes = nbits << XFS_BLF_SHIFT;
  1603. item_index++;
  1604. }
  1605. /*
  1606. * If the current logged region starts after the current
  1607. * di_next_unlinked field, then move on to the next
  1608. * di_next_unlinked field.
  1609. */
  1610. if (next_unlinked_offset < reg_buf_offset)
  1611. continue;
  1612. ASSERT(item->ri_buf[item_index].i_addr != NULL);
  1613. ASSERT((item->ri_buf[item_index].i_len % XFS_BLF_CHUNK) == 0);
  1614. ASSERT((reg_buf_offset + reg_buf_bytes) <=
  1615. BBTOB(bp->b_io_length));
  1616. /*
  1617. * The current logged region contains a copy of the
  1618. * current di_next_unlinked field. Extract its value
  1619. * and copy it to the buffer copy.
  1620. */
  1621. logged_nextp = item->ri_buf[item_index].i_addr +
  1622. next_unlinked_offset - reg_buf_offset;
  1623. if (unlikely(*logged_nextp == 0)) {
  1624. xfs_alert(mp,
  1625. "Bad inode buffer log record (ptr = 0x%p, bp = 0x%p). "
  1626. "Trying to replay bad (0) inode di_next_unlinked field.",
  1627. item, bp);
  1628. XFS_ERROR_REPORT("xlog_recover_do_inode_buf",
  1629. XFS_ERRLEVEL_LOW, mp);
  1630. return -EFSCORRUPTED;
  1631. }
  1632. buffer_nextp = xfs_buf_offset(bp, next_unlinked_offset);
  1633. *buffer_nextp = *logged_nextp;
  1634. /*
  1635. * If necessary, recalculate the CRC in the on-disk inode. We
  1636. * have to leave the inode in a consistent state for whoever
  1637. * reads it next....
  1638. */
  1639. xfs_dinode_calc_crc(mp,
  1640. xfs_buf_offset(bp, i * mp->m_sb.sb_inodesize));
  1641. }
  1642. return 0;
  1643. }
  1644. /*
  1645. * V5 filesystems know the age of the buffer on disk being recovered. We can
  1646. * have newer objects on disk than we are replaying, and so for these cases we
  1647. * don't want to replay the current change as that will make the buffer contents
  1648. * temporarily invalid on disk.
  1649. *
  1650. * The magic number might not match the buffer type we are going to recover
  1651. * (e.g. reallocated blocks), so we ignore the xfs_buf_log_format flags. Hence
  1652. * extract the LSN of the existing object in the buffer based on it's current
  1653. * magic number. If we don't recognise the magic number in the buffer, then
  1654. * return a LSN of -1 so that the caller knows it was an unrecognised block and
  1655. * so can recover the buffer.
  1656. *
  1657. * Note: we cannot rely solely on magic number matches to determine that the
  1658. * buffer has a valid LSN - we also need to verify that it belongs to this
  1659. * filesystem, so we need to extract the object's LSN and compare it to that
  1660. * which we read from the superblock. If the UUIDs don't match, then we've got a
  1661. * stale metadata block from an old filesystem instance that we need to recover
  1662. * over the top of.
  1663. */
  1664. static xfs_lsn_t
  1665. xlog_recover_get_buf_lsn(
  1666. struct xfs_mount *mp,
  1667. struct xfs_buf *bp)
  1668. {
  1669. __uint32_t magic32;
  1670. __uint16_t magic16;
  1671. __uint16_t magicda;
  1672. void *blk = bp->b_addr;
  1673. uuid_t *uuid;
  1674. xfs_lsn_t lsn = -1;
  1675. /* v4 filesystems always recover immediately */
  1676. if (!xfs_sb_version_hascrc(&mp->m_sb))
  1677. goto recover_immediately;
  1678. magic32 = be32_to_cpu(*(__be32 *)blk);
  1679. switch (magic32) {
  1680. case XFS_ABTB_CRC_MAGIC:
  1681. case XFS_ABTC_CRC_MAGIC:
  1682. case XFS_ABTB_MAGIC:
  1683. case XFS_ABTC_MAGIC:
  1684. case XFS_IBT_CRC_MAGIC:
  1685. case XFS_IBT_MAGIC: {
  1686. struct xfs_btree_block *btb = blk;
  1687. lsn = be64_to_cpu(btb->bb_u.s.bb_lsn);
  1688. uuid = &btb->bb_u.s.bb_uuid;
  1689. break;
  1690. }
  1691. case XFS_BMAP_CRC_MAGIC:
  1692. case XFS_BMAP_MAGIC: {
  1693. struct xfs_btree_block *btb = blk;
  1694. lsn = be64_to_cpu(btb->bb_u.l.bb_lsn);
  1695. uuid = &btb->bb_u.l.bb_uuid;
  1696. break;
  1697. }
  1698. case XFS_AGF_MAGIC:
  1699. lsn = be64_to_cpu(((struct xfs_agf *)blk)->agf_lsn);
  1700. uuid = &((struct xfs_agf *)blk)->agf_uuid;
  1701. break;
  1702. case XFS_AGFL_MAGIC:
  1703. lsn = be64_to_cpu(((struct xfs_agfl *)blk)->agfl_lsn);
  1704. uuid = &((struct xfs_agfl *)blk)->agfl_uuid;
  1705. break;
  1706. case XFS_AGI_MAGIC:
  1707. lsn = be64_to_cpu(((struct xfs_agi *)blk)->agi_lsn);
  1708. uuid = &((struct xfs_agi *)blk)->agi_uuid;
  1709. break;
  1710. case XFS_SYMLINK_MAGIC:
  1711. lsn = be64_to_cpu(((struct xfs_dsymlink_hdr *)blk)->sl_lsn);
  1712. uuid = &((struct xfs_dsymlink_hdr *)blk)->sl_uuid;
  1713. break;
  1714. case XFS_DIR3_BLOCK_MAGIC:
  1715. case XFS_DIR3_DATA_MAGIC:
  1716. case XFS_DIR3_FREE_MAGIC:
  1717. lsn = be64_to_cpu(((struct xfs_dir3_blk_hdr *)blk)->lsn);
  1718. uuid = &((struct xfs_dir3_blk_hdr *)blk)->uuid;
  1719. break;
  1720. case XFS_ATTR3_RMT_MAGIC:
  1721. /*
  1722. * Remote attr blocks are written synchronously, rather than
  1723. * being logged. That means they do not contain a valid LSN
  1724. * (i.e. transactionally ordered) in them, and hence any time we
  1725. * see a buffer to replay over the top of a remote attribute
  1726. * block we should simply do so.
  1727. */
  1728. goto recover_immediately;
  1729. case XFS_SB_MAGIC:
  1730. lsn = be64_to_cpu(((struct xfs_dsb *)blk)->sb_lsn);
  1731. uuid = &((struct xfs_dsb *)blk)->sb_uuid;
  1732. break;
  1733. default:
  1734. break;
  1735. }
  1736. if (lsn != (xfs_lsn_t)-1) {
  1737. if (!uuid_equal(&mp->m_sb.sb_uuid, uuid))
  1738. goto recover_immediately;
  1739. return lsn;
  1740. }
  1741. magicda = be16_to_cpu(((struct xfs_da_blkinfo *)blk)->magic);
  1742. switch (magicda) {
  1743. case XFS_DIR3_LEAF1_MAGIC:
  1744. case XFS_DIR3_LEAFN_MAGIC:
  1745. case XFS_DA3_NODE_MAGIC:
  1746. lsn = be64_to_cpu(((struct xfs_da3_blkinfo *)blk)->lsn);
  1747. uuid = &((struct xfs_da3_blkinfo *)blk)->uuid;
  1748. break;
  1749. default:
  1750. break;
  1751. }
  1752. if (lsn != (xfs_lsn_t)-1) {
  1753. if (!uuid_equal(&mp->m_sb.sb_uuid, uuid))
  1754. goto recover_immediately;
  1755. return lsn;
  1756. }
  1757. /*
  1758. * We do individual object checks on dquot and inode buffers as they
  1759. * have their own individual LSN records. Also, we could have a stale
  1760. * buffer here, so we have to at least recognise these buffer types.
  1761. *
  1762. * A notd complexity here is inode unlinked list processing - it logs
  1763. * the inode directly in the buffer, but we don't know which inodes have
  1764. * been modified, and there is no global buffer LSN. Hence we need to
  1765. * recover all inode buffer types immediately. This problem will be
  1766. * fixed by logical logging of the unlinked list modifications.
  1767. */
  1768. magic16 = be16_to_cpu(*(__be16 *)blk);
  1769. switch (magic16) {
  1770. case XFS_DQUOT_MAGIC:
  1771. case XFS_DINODE_MAGIC:
  1772. goto recover_immediately;
  1773. default:
  1774. break;
  1775. }
  1776. /* unknown buffer contents, recover immediately */
  1777. recover_immediately:
  1778. return (xfs_lsn_t)-1;
  1779. }
  1780. /*
  1781. * Validate the recovered buffer is of the correct type and attach the
  1782. * appropriate buffer operations to them for writeback. Magic numbers are in a
  1783. * few places:
  1784. * the first 16 bits of the buffer (inode buffer, dquot buffer),
  1785. * the first 32 bits of the buffer (most blocks),
  1786. * inside a struct xfs_da_blkinfo at the start of the buffer.
  1787. */
  1788. static void
  1789. xlog_recover_validate_buf_type(
  1790. struct xfs_mount *mp,
  1791. struct xfs_buf *bp,
  1792. xfs_buf_log_format_t *buf_f)
  1793. {
  1794. struct xfs_da_blkinfo *info = bp->b_addr;
  1795. __uint32_t magic32;
  1796. __uint16_t magic16;
  1797. __uint16_t magicda;
  1798. /*
  1799. * We can only do post recovery validation on items on CRC enabled
  1800. * fielsystems as we need to know when the buffer was written to be able
  1801. * to determine if we should have replayed the item. If we replay old
  1802. * metadata over a newer buffer, then it will enter a temporarily
  1803. * inconsistent state resulting in verification failures. Hence for now
  1804. * just avoid the verification stage for non-crc filesystems
  1805. */
  1806. if (!xfs_sb_version_hascrc(&mp->m_sb))
  1807. return;
  1808. magic32 = be32_to_cpu(*(__be32 *)bp->b_addr);
  1809. magic16 = be16_to_cpu(*(__be16*)bp->b_addr);
  1810. magicda = be16_to_cpu(info->magic);
  1811. switch (xfs_blft_from_flags(buf_f)) {
  1812. case XFS_BLFT_BTREE_BUF:
  1813. switch (magic32) {
  1814. case XFS_ABTB_CRC_MAGIC:
  1815. case XFS_ABTC_CRC_MAGIC:
  1816. case XFS_ABTB_MAGIC:
  1817. case XFS_ABTC_MAGIC:
  1818. bp->b_ops = &xfs_allocbt_buf_ops;
  1819. break;
  1820. case XFS_IBT_CRC_MAGIC:
  1821. case XFS_FIBT_CRC_MAGIC:
  1822. case XFS_IBT_MAGIC:
  1823. case XFS_FIBT_MAGIC:
  1824. bp->b_ops = &xfs_inobt_buf_ops;
  1825. break;
  1826. case XFS_BMAP_CRC_MAGIC:
  1827. case XFS_BMAP_MAGIC:
  1828. bp->b_ops = &xfs_bmbt_buf_ops;
  1829. break;
  1830. default:
  1831. xfs_warn(mp, "Bad btree block magic!");
  1832. ASSERT(0);
  1833. break;
  1834. }
  1835. break;
  1836. case XFS_BLFT_AGF_BUF:
  1837. if (magic32 != XFS_AGF_MAGIC) {
  1838. xfs_warn(mp, "Bad AGF block magic!");
  1839. ASSERT(0);
  1840. break;
  1841. }
  1842. bp->b_ops = &xfs_agf_buf_ops;
  1843. break;
  1844. case XFS_BLFT_AGFL_BUF:
  1845. if (magic32 != XFS_AGFL_MAGIC) {
  1846. xfs_warn(mp, "Bad AGFL block magic!");
  1847. ASSERT(0);
  1848. break;
  1849. }
  1850. bp->b_ops = &xfs_agfl_buf_ops;
  1851. break;
  1852. case XFS_BLFT_AGI_BUF:
  1853. if (magic32 != XFS_AGI_MAGIC) {
  1854. xfs_warn(mp, "Bad AGI block magic!");
  1855. ASSERT(0);
  1856. break;
  1857. }
  1858. bp->b_ops = &xfs_agi_buf_ops;
  1859. break;
  1860. case XFS_BLFT_UDQUOT_BUF:
  1861. case XFS_BLFT_PDQUOT_BUF:
  1862. case XFS_BLFT_GDQUOT_BUF:
  1863. #ifdef CONFIG_XFS_QUOTA
  1864. if (magic16 != XFS_DQUOT_MAGIC) {
  1865. xfs_warn(mp, "Bad DQUOT block magic!");
  1866. ASSERT(0);
  1867. break;
  1868. }
  1869. bp->b_ops = &xfs_dquot_buf_ops;
  1870. #else
  1871. xfs_alert(mp,
  1872. "Trying to recover dquots without QUOTA support built in!");
  1873. ASSERT(0);
  1874. #endif
  1875. break;
  1876. case XFS_BLFT_DINO_BUF:
  1877. if (magic16 != XFS_DINODE_MAGIC) {
  1878. xfs_warn(mp, "Bad INODE block magic!");
  1879. ASSERT(0);
  1880. break;
  1881. }
  1882. bp->b_ops = &xfs_inode_buf_ops;
  1883. break;
  1884. case XFS_BLFT_SYMLINK_BUF:
  1885. if (magic32 != XFS_SYMLINK_MAGIC) {
  1886. xfs_warn(mp, "Bad symlink block magic!");
  1887. ASSERT(0);
  1888. break;
  1889. }
  1890. bp->b_ops = &xfs_symlink_buf_ops;
  1891. break;
  1892. case XFS_BLFT_DIR_BLOCK_BUF:
  1893. if (magic32 != XFS_DIR2_BLOCK_MAGIC &&
  1894. magic32 != XFS_DIR3_BLOCK_MAGIC) {
  1895. xfs_warn(mp, "Bad dir block magic!");
  1896. ASSERT(0);
  1897. break;
  1898. }
  1899. bp->b_ops = &xfs_dir3_block_buf_ops;
  1900. break;
  1901. case XFS_BLFT_DIR_DATA_BUF:
  1902. if (magic32 != XFS_DIR2_DATA_MAGIC &&
  1903. magic32 != XFS_DIR3_DATA_MAGIC) {
  1904. xfs_warn(mp, "Bad dir data magic!");
  1905. ASSERT(0);
  1906. break;
  1907. }
  1908. bp->b_ops = &xfs_dir3_data_buf_ops;
  1909. break;
  1910. case XFS_BLFT_DIR_FREE_BUF:
  1911. if (magic32 != XFS_DIR2_FREE_MAGIC &&
  1912. magic32 != XFS_DIR3_FREE_MAGIC) {
  1913. xfs_warn(mp, "Bad dir3 free magic!");
  1914. ASSERT(0);
  1915. break;
  1916. }
  1917. bp->b_ops = &xfs_dir3_free_buf_ops;
  1918. break;
  1919. case XFS_BLFT_DIR_LEAF1_BUF:
  1920. if (magicda != XFS_DIR2_LEAF1_MAGIC &&
  1921. magicda != XFS_DIR3_LEAF1_MAGIC) {
  1922. xfs_warn(mp, "Bad dir leaf1 magic!");
  1923. ASSERT(0);
  1924. break;
  1925. }
  1926. bp->b_ops = &xfs_dir3_leaf1_buf_ops;
  1927. break;
  1928. case XFS_BLFT_DIR_LEAFN_BUF:
  1929. if (magicda != XFS_DIR2_LEAFN_MAGIC &&
  1930. magicda != XFS_DIR3_LEAFN_MAGIC) {
  1931. xfs_warn(mp, "Bad dir leafn magic!");
  1932. ASSERT(0);
  1933. break;
  1934. }
  1935. bp->b_ops = &xfs_dir3_leafn_buf_ops;
  1936. break;
  1937. case XFS_BLFT_DA_NODE_BUF:
  1938. if (magicda != XFS_DA_NODE_MAGIC &&
  1939. magicda != XFS_DA3_NODE_MAGIC) {
  1940. xfs_warn(mp, "Bad da node magic!");
  1941. ASSERT(0);
  1942. break;
  1943. }
  1944. bp->b_ops = &xfs_da3_node_buf_ops;
  1945. break;
  1946. case XFS_BLFT_ATTR_LEAF_BUF:
  1947. if (magicda != XFS_ATTR_LEAF_MAGIC &&
  1948. magicda != XFS_ATTR3_LEAF_MAGIC) {
  1949. xfs_warn(mp, "Bad attr leaf magic!");
  1950. ASSERT(0);
  1951. break;
  1952. }
  1953. bp->b_ops = &xfs_attr3_leaf_buf_ops;
  1954. break;
  1955. case XFS_BLFT_ATTR_RMT_BUF:
  1956. if (magic32 != XFS_ATTR3_RMT_MAGIC) {
  1957. xfs_warn(mp, "Bad attr remote magic!");
  1958. ASSERT(0);
  1959. break;
  1960. }
  1961. bp->b_ops = &xfs_attr3_rmt_buf_ops;
  1962. break;
  1963. case XFS_BLFT_SB_BUF:
  1964. if (magic32 != XFS_SB_MAGIC) {
  1965. xfs_warn(mp, "Bad SB block magic!");
  1966. ASSERT(0);
  1967. break;
  1968. }
  1969. bp->b_ops = &xfs_sb_buf_ops;
  1970. break;
  1971. default:
  1972. xfs_warn(mp, "Unknown buffer type %d!",
  1973. xfs_blft_from_flags(buf_f));
  1974. break;
  1975. }
  1976. }
  1977. /*
  1978. * Perform a 'normal' buffer recovery. Each logged region of the
  1979. * buffer should be copied over the corresponding region in the
  1980. * given buffer. The bitmap in the buf log format structure indicates
  1981. * where to place the logged data.
  1982. */
  1983. STATIC void
  1984. xlog_recover_do_reg_buffer(
  1985. struct xfs_mount *mp,
  1986. xlog_recover_item_t *item,
  1987. struct xfs_buf *bp,
  1988. xfs_buf_log_format_t *buf_f)
  1989. {
  1990. int i;
  1991. int bit;
  1992. int nbits;
  1993. int error;
  1994. trace_xfs_log_recover_buf_reg_buf(mp->m_log, buf_f);
  1995. bit = 0;
  1996. i = 1; /* 0 is the buf format structure */
  1997. while (1) {
  1998. bit = xfs_next_bit(buf_f->blf_data_map,
  1999. buf_f->blf_map_size, bit);
  2000. if (bit == -1)
  2001. break;
  2002. nbits = xfs_contig_bits(buf_f->blf_data_map,
  2003. buf_f->blf_map_size, bit);
  2004. ASSERT(nbits > 0);
  2005. ASSERT(item->ri_buf[i].i_addr != NULL);
  2006. ASSERT(item->ri_buf[i].i_len % XFS_BLF_CHUNK == 0);
  2007. ASSERT(BBTOB(bp->b_io_length) >=
  2008. ((uint)bit << XFS_BLF_SHIFT) + (nbits << XFS_BLF_SHIFT));
  2009. /*
  2010. * The dirty regions logged in the buffer, even though
  2011. * contiguous, may span multiple chunks. This is because the
  2012. * dirty region may span a physical page boundary in a buffer
  2013. * and hence be split into two separate vectors for writing into
  2014. * the log. Hence we need to trim nbits back to the length of
  2015. * the current region being copied out of the log.
  2016. */
  2017. if (item->ri_buf[i].i_len < (nbits << XFS_BLF_SHIFT))
  2018. nbits = item->ri_buf[i].i_len >> XFS_BLF_SHIFT;
  2019. /*
  2020. * Do a sanity check if this is a dquot buffer. Just checking
  2021. * the first dquot in the buffer should do. XXXThis is
  2022. * probably a good thing to do for other buf types also.
  2023. */
  2024. error = 0;
  2025. if (buf_f->blf_flags &
  2026. (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
  2027. if (item->ri_buf[i].i_addr == NULL) {
  2028. xfs_alert(mp,
  2029. "XFS: NULL dquot in %s.", __func__);
  2030. goto next;
  2031. }
  2032. if (item->ri_buf[i].i_len < sizeof(xfs_disk_dquot_t)) {
  2033. xfs_alert(mp,
  2034. "XFS: dquot too small (%d) in %s.",
  2035. item->ri_buf[i].i_len, __func__);
  2036. goto next;
  2037. }
  2038. error = xfs_dqcheck(mp, item->ri_buf[i].i_addr,
  2039. -1, 0, XFS_QMOPT_DOWARN,
  2040. "dquot_buf_recover");
  2041. if (error)
  2042. goto next;
  2043. }
  2044. memcpy(xfs_buf_offset(bp,
  2045. (uint)bit << XFS_BLF_SHIFT), /* dest */
  2046. item->ri_buf[i].i_addr, /* source */
  2047. nbits<<XFS_BLF_SHIFT); /* length */
  2048. next:
  2049. i++;
  2050. bit += nbits;
  2051. }
  2052. /* Shouldn't be any more regions */
  2053. ASSERT(i == item->ri_total);
  2054. xlog_recover_validate_buf_type(mp, bp, buf_f);
  2055. }
  2056. /*
  2057. * Perform a dquot buffer recovery.
  2058. * Simple algorithm: if we have found a QUOTAOFF log item of the same type
  2059. * (ie. USR or GRP), then just toss this buffer away; don't recover it.
  2060. * Else, treat it as a regular buffer and do recovery.
  2061. *
  2062. * Return false if the buffer was tossed and true if we recovered the buffer to
  2063. * indicate to the caller if the buffer needs writing.
  2064. */
  2065. STATIC bool
  2066. xlog_recover_do_dquot_buffer(
  2067. struct xfs_mount *mp,
  2068. struct xlog *log,
  2069. struct xlog_recover_item *item,
  2070. struct xfs_buf *bp,
  2071. struct xfs_buf_log_format *buf_f)
  2072. {
  2073. uint type;
  2074. trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
  2075. /*
  2076. * Filesystems are required to send in quota flags at mount time.
  2077. */
  2078. if (!mp->m_qflags)
  2079. return false;
  2080. type = 0;
  2081. if (buf_f->blf_flags & XFS_BLF_UDQUOT_BUF)
  2082. type |= XFS_DQ_USER;
  2083. if (buf_f->blf_flags & XFS_BLF_PDQUOT_BUF)
  2084. type |= XFS_DQ_PROJ;
  2085. if (buf_f->blf_flags & XFS_BLF_GDQUOT_BUF)
  2086. type |= XFS_DQ_GROUP;
  2087. /*
  2088. * This type of quotas was turned off, so ignore this buffer
  2089. */
  2090. if (log->l_quotaoffs_flag & type)
  2091. return false;
  2092. xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
  2093. return true;
  2094. }
  2095. /*
  2096. * This routine replays a modification made to a buffer at runtime.
  2097. * There are actually two types of buffer, regular and inode, which
  2098. * are handled differently. Inode buffers are handled differently
  2099. * in that we only recover a specific set of data from them, namely
  2100. * the inode di_next_unlinked fields. This is because all other inode
  2101. * data is actually logged via inode records and any data we replay
  2102. * here which overlaps that may be stale.
  2103. *
  2104. * When meta-data buffers are freed at run time we log a buffer item
  2105. * with the XFS_BLF_CANCEL bit set to indicate that previous copies
  2106. * of the buffer in the log should not be replayed at recovery time.
  2107. * This is so that if the blocks covered by the buffer are reused for
  2108. * file data before we crash we don't end up replaying old, freed
  2109. * meta-data into a user's file.
  2110. *
  2111. * To handle the cancellation of buffer log items, we make two passes
  2112. * over the log during recovery. During the first we build a table of
  2113. * those buffers which have been cancelled, and during the second we
  2114. * only replay those buffers which do not have corresponding cancel
  2115. * records in the table. See xlog_recover_buffer_pass[1,2] above
  2116. * for more details on the implementation of the table of cancel records.
  2117. */
  2118. STATIC int
  2119. xlog_recover_buffer_pass2(
  2120. struct xlog *log,
  2121. struct list_head *buffer_list,
  2122. struct xlog_recover_item *item,
  2123. xfs_lsn_t current_lsn)
  2124. {
  2125. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  2126. xfs_mount_t *mp = log->l_mp;
  2127. xfs_buf_t *bp;
  2128. int error;
  2129. uint buf_flags;
  2130. xfs_lsn_t lsn;
  2131. /*
  2132. * In this pass we only want to recover all the buffers which have
  2133. * not been cancelled and are not cancellation buffers themselves.
  2134. */
  2135. if (xlog_check_buffer_cancelled(log, buf_f->blf_blkno,
  2136. buf_f->blf_len, buf_f->blf_flags)) {
  2137. trace_xfs_log_recover_buf_cancel(log, buf_f);
  2138. return 0;
  2139. }
  2140. trace_xfs_log_recover_buf_recover(log, buf_f);
  2141. buf_flags = 0;
  2142. if (buf_f->blf_flags & XFS_BLF_INODE_BUF)
  2143. buf_flags |= XBF_UNMAPPED;
  2144. bp = xfs_buf_read(mp->m_ddev_targp, buf_f->blf_blkno, buf_f->blf_len,
  2145. buf_flags, NULL);
  2146. if (!bp)
  2147. return -ENOMEM;
  2148. error = bp->b_error;
  2149. if (error) {
  2150. xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#1)");
  2151. goto out_release;
  2152. }
  2153. /*
  2154. * Recover the buffer only if we get an LSN from it and it's less than
  2155. * the lsn of the transaction we are replaying.
  2156. *
  2157. * Note that we have to be extremely careful of readahead here.
  2158. * Readahead does not attach verfiers to the buffers so if we don't
  2159. * actually do any replay after readahead because of the LSN we found
  2160. * in the buffer if more recent than that current transaction then we
  2161. * need to attach the verifier directly. Failure to do so can lead to
  2162. * future recovery actions (e.g. EFI and unlinked list recovery) can
  2163. * operate on the buffers and they won't get the verifier attached. This
  2164. * can lead to blocks on disk having the correct content but a stale
  2165. * CRC.
  2166. *
  2167. * It is safe to assume these clean buffers are currently up to date.
  2168. * If the buffer is dirtied by a later transaction being replayed, then
  2169. * the verifier will be reset to match whatever recover turns that
  2170. * buffer into.
  2171. */
  2172. lsn = xlog_recover_get_buf_lsn(mp, bp);
  2173. if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
  2174. xlog_recover_validate_buf_type(mp, bp, buf_f);
  2175. goto out_release;
  2176. }
  2177. if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
  2178. error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
  2179. if (error)
  2180. goto out_release;
  2181. } else if (buf_f->blf_flags &
  2182. (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
  2183. bool dirty;
  2184. dirty = xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
  2185. if (!dirty)
  2186. goto out_release;
  2187. } else {
  2188. xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
  2189. }
  2190. /*
  2191. * Perform delayed write on the buffer. Asynchronous writes will be
  2192. * slower when taking into account all the buffers to be flushed.
  2193. *
  2194. * Also make sure that only inode buffers with good sizes stay in
  2195. * the buffer cache. The kernel moves inodes in buffers of 1 block
  2196. * or mp->m_inode_cluster_size bytes, whichever is bigger. The inode
  2197. * buffers in the log can be a different size if the log was generated
  2198. * by an older kernel using unclustered inode buffers or a newer kernel
  2199. * running with a different inode cluster size. Regardless, if the
  2200. * the inode buffer size isn't MAX(blocksize, mp->m_inode_cluster_size)
  2201. * for *our* value of mp->m_inode_cluster_size, then we need to keep
  2202. * the buffer out of the buffer cache so that the buffer won't
  2203. * overlap with future reads of those inodes.
  2204. */
  2205. if (XFS_DINODE_MAGIC ==
  2206. be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
  2207. (BBTOB(bp->b_io_length) != MAX(log->l_mp->m_sb.sb_blocksize,
  2208. (__uint32_t)log->l_mp->m_inode_cluster_size))) {
  2209. xfs_buf_stale(bp);
  2210. error = xfs_bwrite(bp);
  2211. } else {
  2212. ASSERT(bp->b_target->bt_mount == mp);
  2213. bp->b_iodone = xlog_recover_iodone;
  2214. xfs_buf_delwri_queue(bp, buffer_list);
  2215. }
  2216. out_release:
  2217. xfs_buf_relse(bp);
  2218. return error;
  2219. }
  2220. /*
  2221. * Inode fork owner changes
  2222. *
  2223. * If we have been told that we have to reparent the inode fork, it's because an
  2224. * extent swap operation on a CRC enabled filesystem has been done and we are
  2225. * replaying it. We need to walk the BMBT of the appropriate fork and change the
  2226. * owners of it.
  2227. *
  2228. * The complexity here is that we don't have an inode context to work with, so
  2229. * after we've replayed the inode we need to instantiate one. This is where the
  2230. * fun begins.
  2231. *
  2232. * We are in the middle of log recovery, so we can't run transactions. That
  2233. * means we cannot use cache coherent inode instantiation via xfs_iget(), as
  2234. * that will result in the corresponding iput() running the inode through
  2235. * xfs_inactive(). If we've just replayed an inode core that changes the link
  2236. * count to zero (i.e. it's been unlinked), then xfs_inactive() will run
  2237. * transactions (bad!).
  2238. *
  2239. * So, to avoid this, we instantiate an inode directly from the inode core we've
  2240. * just recovered. We have the buffer still locked, and all we really need to
  2241. * instantiate is the inode core and the forks being modified. We can do this
  2242. * manually, then run the inode btree owner change, and then tear down the
  2243. * xfs_inode without having to run any transactions at all.
  2244. *
  2245. * Also, because we don't have a transaction context available here but need to
  2246. * gather all the buffers we modify for writeback so we pass the buffer_list
  2247. * instead for the operation to use.
  2248. */
  2249. STATIC int
  2250. xfs_recover_inode_owner_change(
  2251. struct xfs_mount *mp,
  2252. struct xfs_dinode *dip,
  2253. struct xfs_inode_log_format *in_f,
  2254. struct list_head *buffer_list)
  2255. {
  2256. struct xfs_inode *ip;
  2257. int error;
  2258. ASSERT(in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER));
  2259. ip = xfs_inode_alloc(mp, in_f->ilf_ino);
  2260. if (!ip)
  2261. return -ENOMEM;
  2262. /* instantiate the inode */
  2263. xfs_dinode_from_disk(&ip->i_d, dip);
  2264. ASSERT(ip->i_d.di_version >= 3);
  2265. error = xfs_iformat_fork(ip, dip);
  2266. if (error)
  2267. goto out_free_ip;
  2268. if (in_f->ilf_fields & XFS_ILOG_DOWNER) {
  2269. ASSERT(in_f->ilf_fields & XFS_ILOG_DBROOT);
  2270. error = xfs_bmbt_change_owner(NULL, ip, XFS_DATA_FORK,
  2271. ip->i_ino, buffer_list);
  2272. if (error)
  2273. goto out_free_ip;
  2274. }
  2275. if (in_f->ilf_fields & XFS_ILOG_AOWNER) {
  2276. ASSERT(in_f->ilf_fields & XFS_ILOG_ABROOT);
  2277. error = xfs_bmbt_change_owner(NULL, ip, XFS_ATTR_FORK,
  2278. ip->i_ino, buffer_list);
  2279. if (error)
  2280. goto out_free_ip;
  2281. }
  2282. out_free_ip:
  2283. xfs_inode_free(ip);
  2284. return error;
  2285. }
  2286. STATIC int
  2287. xlog_recover_inode_pass2(
  2288. struct xlog *log,
  2289. struct list_head *buffer_list,
  2290. struct xlog_recover_item *item,
  2291. xfs_lsn_t current_lsn)
  2292. {
  2293. xfs_inode_log_format_t *in_f;
  2294. xfs_mount_t *mp = log->l_mp;
  2295. xfs_buf_t *bp;
  2296. xfs_dinode_t *dip;
  2297. int len;
  2298. char *src;
  2299. char *dest;
  2300. int error;
  2301. int attr_index;
  2302. uint fields;
  2303. xfs_icdinode_t *dicp;
  2304. uint isize;
  2305. int need_free = 0;
  2306. if (item->ri_buf[0].i_len == sizeof(xfs_inode_log_format_t)) {
  2307. in_f = item->ri_buf[0].i_addr;
  2308. } else {
  2309. in_f = kmem_alloc(sizeof(xfs_inode_log_format_t), KM_SLEEP);
  2310. need_free = 1;
  2311. error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
  2312. if (error)
  2313. goto error;
  2314. }
  2315. /*
  2316. * Inode buffers can be freed, look out for it,
  2317. * and do not replay the inode.
  2318. */
  2319. if (xlog_check_buffer_cancelled(log, in_f->ilf_blkno,
  2320. in_f->ilf_len, 0)) {
  2321. error = 0;
  2322. trace_xfs_log_recover_inode_cancel(log, in_f);
  2323. goto error;
  2324. }
  2325. trace_xfs_log_recover_inode_recover(log, in_f);
  2326. bp = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len, 0,
  2327. &xfs_inode_buf_ops);
  2328. if (!bp) {
  2329. error = -ENOMEM;
  2330. goto error;
  2331. }
  2332. error = bp->b_error;
  2333. if (error) {
  2334. xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#2)");
  2335. goto out_release;
  2336. }
  2337. ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
  2338. dip = xfs_buf_offset(bp, in_f->ilf_boffset);
  2339. /*
  2340. * Make sure the place we're flushing out to really looks
  2341. * like an inode!
  2342. */
  2343. if (unlikely(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))) {
  2344. xfs_alert(mp,
  2345. "%s: Bad inode magic number, dip = 0x%p, dino bp = 0x%p, ino = %Ld",
  2346. __func__, dip, bp, in_f->ilf_ino);
  2347. XFS_ERROR_REPORT("xlog_recover_inode_pass2(1)",
  2348. XFS_ERRLEVEL_LOW, mp);
  2349. error = -EFSCORRUPTED;
  2350. goto out_release;
  2351. }
  2352. dicp = item->ri_buf[1].i_addr;
  2353. if (unlikely(dicp->di_magic != XFS_DINODE_MAGIC)) {
  2354. xfs_alert(mp,
  2355. "%s: Bad inode log record, rec ptr 0x%p, ino %Ld",
  2356. __func__, item, in_f->ilf_ino);
  2357. XFS_ERROR_REPORT("xlog_recover_inode_pass2(2)",
  2358. XFS_ERRLEVEL_LOW, mp);
  2359. error = -EFSCORRUPTED;
  2360. goto out_release;
  2361. }
  2362. /*
  2363. * If the inode has an LSN in it, recover the inode only if it's less
  2364. * than the lsn of the transaction we are replaying. Note: we still
  2365. * need to replay an owner change even though the inode is more recent
  2366. * than the transaction as there is no guarantee that all the btree
  2367. * blocks are more recent than this transaction, too.
  2368. */
  2369. if (dip->di_version >= 3) {
  2370. xfs_lsn_t lsn = be64_to_cpu(dip->di_lsn);
  2371. if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
  2372. trace_xfs_log_recover_inode_skip(log, in_f);
  2373. error = 0;
  2374. goto out_owner_change;
  2375. }
  2376. }
  2377. /*
  2378. * di_flushiter is only valid for v1/2 inodes. All changes for v3 inodes
  2379. * are transactional and if ordering is necessary we can determine that
  2380. * more accurately by the LSN field in the V3 inode core. Don't trust
  2381. * the inode versions we might be changing them here - use the
  2382. * superblock flag to determine whether we need to look at di_flushiter
  2383. * to skip replay when the on disk inode is newer than the log one
  2384. */
  2385. if (!xfs_sb_version_hascrc(&mp->m_sb) &&
  2386. dicp->di_flushiter < be16_to_cpu(dip->di_flushiter)) {
  2387. /*
  2388. * Deal with the wrap case, DI_MAX_FLUSH is less
  2389. * than smaller numbers
  2390. */
  2391. if (be16_to_cpu(dip->di_flushiter) == DI_MAX_FLUSH &&
  2392. dicp->di_flushiter < (DI_MAX_FLUSH >> 1)) {
  2393. /* do nothing */
  2394. } else {
  2395. trace_xfs_log_recover_inode_skip(log, in_f);
  2396. error = 0;
  2397. goto out_release;
  2398. }
  2399. }
  2400. /* Take the opportunity to reset the flush iteration count */
  2401. dicp->di_flushiter = 0;
  2402. if (unlikely(S_ISREG(dicp->di_mode))) {
  2403. if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
  2404. (dicp->di_format != XFS_DINODE_FMT_BTREE)) {
  2405. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(3)",
  2406. XFS_ERRLEVEL_LOW, mp, dicp);
  2407. xfs_alert(mp,
  2408. "%s: Bad regular inode log record, rec ptr 0x%p, "
  2409. "ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
  2410. __func__, item, dip, bp, in_f->ilf_ino);
  2411. error = -EFSCORRUPTED;
  2412. goto out_release;
  2413. }
  2414. } else if (unlikely(S_ISDIR(dicp->di_mode))) {
  2415. if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
  2416. (dicp->di_format != XFS_DINODE_FMT_BTREE) &&
  2417. (dicp->di_format != XFS_DINODE_FMT_LOCAL)) {
  2418. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(4)",
  2419. XFS_ERRLEVEL_LOW, mp, dicp);
  2420. xfs_alert(mp,
  2421. "%s: Bad dir inode log record, rec ptr 0x%p, "
  2422. "ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
  2423. __func__, item, dip, bp, in_f->ilf_ino);
  2424. error = -EFSCORRUPTED;
  2425. goto out_release;
  2426. }
  2427. }
  2428. if (unlikely(dicp->di_nextents + dicp->di_anextents > dicp->di_nblocks)){
  2429. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(5)",
  2430. XFS_ERRLEVEL_LOW, mp, dicp);
  2431. xfs_alert(mp,
  2432. "%s: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, "
  2433. "dino bp 0x%p, ino %Ld, total extents = %d, nblocks = %Ld",
  2434. __func__, item, dip, bp, in_f->ilf_ino,
  2435. dicp->di_nextents + dicp->di_anextents,
  2436. dicp->di_nblocks);
  2437. error = -EFSCORRUPTED;
  2438. goto out_release;
  2439. }
  2440. if (unlikely(dicp->di_forkoff > mp->m_sb.sb_inodesize)) {
  2441. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(6)",
  2442. XFS_ERRLEVEL_LOW, mp, dicp);
  2443. xfs_alert(mp,
  2444. "%s: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, "
  2445. "dino bp 0x%p, ino %Ld, forkoff 0x%x", __func__,
  2446. item, dip, bp, in_f->ilf_ino, dicp->di_forkoff);
  2447. error = -EFSCORRUPTED;
  2448. goto out_release;
  2449. }
  2450. isize = xfs_icdinode_size(dicp->di_version);
  2451. if (unlikely(item->ri_buf[1].i_len > isize)) {
  2452. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(7)",
  2453. XFS_ERRLEVEL_LOW, mp, dicp);
  2454. xfs_alert(mp,
  2455. "%s: Bad inode log record length %d, rec ptr 0x%p",
  2456. __func__, item->ri_buf[1].i_len, item);
  2457. error = -EFSCORRUPTED;
  2458. goto out_release;
  2459. }
  2460. /* The core is in in-core format */
  2461. xfs_dinode_to_disk(dip, dicp);
  2462. /* the rest is in on-disk format */
  2463. if (item->ri_buf[1].i_len > isize) {
  2464. memcpy((char *)dip + isize,
  2465. item->ri_buf[1].i_addr + isize,
  2466. item->ri_buf[1].i_len - isize);
  2467. }
  2468. fields = in_f->ilf_fields;
  2469. switch (fields & (XFS_ILOG_DEV | XFS_ILOG_UUID)) {
  2470. case XFS_ILOG_DEV:
  2471. xfs_dinode_put_rdev(dip, in_f->ilf_u.ilfu_rdev);
  2472. break;
  2473. case XFS_ILOG_UUID:
  2474. memcpy(XFS_DFORK_DPTR(dip),
  2475. &in_f->ilf_u.ilfu_uuid,
  2476. sizeof(uuid_t));
  2477. break;
  2478. }
  2479. if (in_f->ilf_size == 2)
  2480. goto out_owner_change;
  2481. len = item->ri_buf[2].i_len;
  2482. src = item->ri_buf[2].i_addr;
  2483. ASSERT(in_f->ilf_size <= 4);
  2484. ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
  2485. ASSERT(!(fields & XFS_ILOG_DFORK) ||
  2486. (len == in_f->ilf_dsize));
  2487. switch (fields & XFS_ILOG_DFORK) {
  2488. case XFS_ILOG_DDATA:
  2489. case XFS_ILOG_DEXT:
  2490. memcpy(XFS_DFORK_DPTR(dip), src, len);
  2491. break;
  2492. case XFS_ILOG_DBROOT:
  2493. xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src, len,
  2494. (xfs_bmdr_block_t *)XFS_DFORK_DPTR(dip),
  2495. XFS_DFORK_DSIZE(dip, mp));
  2496. break;
  2497. default:
  2498. /*
  2499. * There are no data fork flags set.
  2500. */
  2501. ASSERT((fields & XFS_ILOG_DFORK) == 0);
  2502. break;
  2503. }
  2504. /*
  2505. * If we logged any attribute data, recover it. There may or
  2506. * may not have been any other non-core data logged in this
  2507. * transaction.
  2508. */
  2509. if (in_f->ilf_fields & XFS_ILOG_AFORK) {
  2510. if (in_f->ilf_fields & XFS_ILOG_DFORK) {
  2511. attr_index = 3;
  2512. } else {
  2513. attr_index = 2;
  2514. }
  2515. len = item->ri_buf[attr_index].i_len;
  2516. src = item->ri_buf[attr_index].i_addr;
  2517. ASSERT(len == in_f->ilf_asize);
  2518. switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
  2519. case XFS_ILOG_ADATA:
  2520. case XFS_ILOG_AEXT:
  2521. dest = XFS_DFORK_APTR(dip);
  2522. ASSERT(len <= XFS_DFORK_ASIZE(dip, mp));
  2523. memcpy(dest, src, len);
  2524. break;
  2525. case XFS_ILOG_ABROOT:
  2526. dest = XFS_DFORK_APTR(dip);
  2527. xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src,
  2528. len, (xfs_bmdr_block_t*)dest,
  2529. XFS_DFORK_ASIZE(dip, mp));
  2530. break;
  2531. default:
  2532. xfs_warn(log->l_mp, "%s: Invalid flag", __func__);
  2533. ASSERT(0);
  2534. error = -EIO;
  2535. goto out_release;
  2536. }
  2537. }
  2538. out_owner_change:
  2539. if (in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER))
  2540. error = xfs_recover_inode_owner_change(mp, dip, in_f,
  2541. buffer_list);
  2542. /* re-generate the checksum. */
  2543. xfs_dinode_calc_crc(log->l_mp, dip);
  2544. ASSERT(bp->b_target->bt_mount == mp);
  2545. bp->b_iodone = xlog_recover_iodone;
  2546. xfs_buf_delwri_queue(bp, buffer_list);
  2547. out_release:
  2548. xfs_buf_relse(bp);
  2549. error:
  2550. if (need_free)
  2551. kmem_free(in_f);
  2552. return error;
  2553. }
  2554. /*
  2555. * Recover QUOTAOFF records. We simply make a note of it in the xlog
  2556. * structure, so that we know not to do any dquot item or dquot buffer recovery,
  2557. * of that type.
  2558. */
  2559. STATIC int
  2560. xlog_recover_quotaoff_pass1(
  2561. struct xlog *log,
  2562. struct xlog_recover_item *item)
  2563. {
  2564. xfs_qoff_logformat_t *qoff_f = item->ri_buf[0].i_addr;
  2565. ASSERT(qoff_f);
  2566. /*
  2567. * The logitem format's flag tells us if this was user quotaoff,
  2568. * group/project quotaoff or both.
  2569. */
  2570. if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
  2571. log->l_quotaoffs_flag |= XFS_DQ_USER;
  2572. if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
  2573. log->l_quotaoffs_flag |= XFS_DQ_PROJ;
  2574. if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
  2575. log->l_quotaoffs_flag |= XFS_DQ_GROUP;
  2576. return 0;
  2577. }
  2578. /*
  2579. * Recover a dquot record
  2580. */
  2581. STATIC int
  2582. xlog_recover_dquot_pass2(
  2583. struct xlog *log,
  2584. struct list_head *buffer_list,
  2585. struct xlog_recover_item *item,
  2586. xfs_lsn_t current_lsn)
  2587. {
  2588. xfs_mount_t *mp = log->l_mp;
  2589. xfs_buf_t *bp;
  2590. struct xfs_disk_dquot *ddq, *recddq;
  2591. int error;
  2592. xfs_dq_logformat_t *dq_f;
  2593. uint type;
  2594. /*
  2595. * Filesystems are required to send in quota flags at mount time.
  2596. */
  2597. if (mp->m_qflags == 0)
  2598. return 0;
  2599. recddq = item->ri_buf[1].i_addr;
  2600. if (recddq == NULL) {
  2601. xfs_alert(log->l_mp, "NULL dquot in %s.", __func__);
  2602. return -EIO;
  2603. }
  2604. if (item->ri_buf[1].i_len < sizeof(xfs_disk_dquot_t)) {
  2605. xfs_alert(log->l_mp, "dquot too small (%d) in %s.",
  2606. item->ri_buf[1].i_len, __func__);
  2607. return -EIO;
  2608. }
  2609. /*
  2610. * This type of quotas was turned off, so ignore this record.
  2611. */
  2612. type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
  2613. ASSERT(type);
  2614. if (log->l_quotaoffs_flag & type)
  2615. return 0;
  2616. /*
  2617. * At this point we know that quota was _not_ turned off.
  2618. * Since the mount flags are not indicating to us otherwise, this
  2619. * must mean that quota is on, and the dquot needs to be replayed.
  2620. * Remember that we may not have fully recovered the superblock yet,
  2621. * so we can't do the usual trick of looking at the SB quota bits.
  2622. *
  2623. * The other possibility, of course, is that the quota subsystem was
  2624. * removed since the last mount - ENOSYS.
  2625. */
  2626. dq_f = item->ri_buf[0].i_addr;
  2627. ASSERT(dq_f);
  2628. error = xfs_dqcheck(mp, recddq, dq_f->qlf_id, 0, XFS_QMOPT_DOWARN,
  2629. "xlog_recover_dquot_pass2 (log copy)");
  2630. if (error)
  2631. return -EIO;
  2632. ASSERT(dq_f->qlf_len == 1);
  2633. /*
  2634. * At this point we are assuming that the dquots have been allocated
  2635. * and hence the buffer has valid dquots stamped in it. It should,
  2636. * therefore, pass verifier validation. If the dquot is bad, then the
  2637. * we'll return an error here, so we don't need to specifically check
  2638. * the dquot in the buffer after the verifier has run.
  2639. */
  2640. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dq_f->qlf_blkno,
  2641. XFS_FSB_TO_BB(mp, dq_f->qlf_len), 0, &bp,
  2642. &xfs_dquot_buf_ops);
  2643. if (error)
  2644. return error;
  2645. ASSERT(bp);
  2646. ddq = xfs_buf_offset(bp, dq_f->qlf_boffset);
  2647. /*
  2648. * If the dquot has an LSN in it, recover the dquot only if it's less
  2649. * than the lsn of the transaction we are replaying.
  2650. */
  2651. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  2652. struct xfs_dqblk *dqb = (struct xfs_dqblk *)ddq;
  2653. xfs_lsn_t lsn = be64_to_cpu(dqb->dd_lsn);
  2654. if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
  2655. goto out_release;
  2656. }
  2657. }
  2658. memcpy(ddq, recddq, item->ri_buf[1].i_len);
  2659. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  2660. xfs_update_cksum((char *)ddq, sizeof(struct xfs_dqblk),
  2661. XFS_DQUOT_CRC_OFF);
  2662. }
  2663. ASSERT(dq_f->qlf_size == 2);
  2664. ASSERT(bp->b_target->bt_mount == mp);
  2665. bp->b_iodone = xlog_recover_iodone;
  2666. xfs_buf_delwri_queue(bp, buffer_list);
  2667. out_release:
  2668. xfs_buf_relse(bp);
  2669. return 0;
  2670. }
  2671. /*
  2672. * This routine is called to create an in-core extent free intent
  2673. * item from the efi format structure which was logged on disk.
  2674. * It allocates an in-core efi, copies the extents from the format
  2675. * structure into it, and adds the efi to the AIL with the given
  2676. * LSN.
  2677. */
  2678. STATIC int
  2679. xlog_recover_efi_pass2(
  2680. struct xlog *log,
  2681. struct xlog_recover_item *item,
  2682. xfs_lsn_t lsn)
  2683. {
  2684. int error;
  2685. xfs_mount_t *mp = log->l_mp;
  2686. xfs_efi_log_item_t *efip;
  2687. xfs_efi_log_format_t *efi_formatp;
  2688. efi_formatp = item->ri_buf[0].i_addr;
  2689. efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
  2690. if ((error = xfs_efi_copy_format(&(item->ri_buf[0]),
  2691. &(efip->efi_format)))) {
  2692. xfs_efi_item_free(efip);
  2693. return error;
  2694. }
  2695. atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
  2696. spin_lock(&log->l_ailp->xa_lock);
  2697. /*
  2698. * xfs_trans_ail_update() drops the AIL lock.
  2699. */
  2700. xfs_trans_ail_update(log->l_ailp, &efip->efi_item, lsn);
  2701. return 0;
  2702. }
  2703. /*
  2704. * This routine is called when an efd format structure is found in
  2705. * a committed transaction in the log. It's purpose is to cancel
  2706. * the corresponding efi if it was still in the log. To do this
  2707. * it searches the AIL for the efi with an id equal to that in the
  2708. * efd format structure. If we find it, we remove the efi from the
  2709. * AIL and free it.
  2710. */
  2711. STATIC int
  2712. xlog_recover_efd_pass2(
  2713. struct xlog *log,
  2714. struct xlog_recover_item *item)
  2715. {
  2716. xfs_efd_log_format_t *efd_formatp;
  2717. xfs_efi_log_item_t *efip = NULL;
  2718. xfs_log_item_t *lip;
  2719. __uint64_t efi_id;
  2720. struct xfs_ail_cursor cur;
  2721. struct xfs_ail *ailp = log->l_ailp;
  2722. efd_formatp = item->ri_buf[0].i_addr;
  2723. ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
  2724. ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
  2725. (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
  2726. ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
  2727. efi_id = efd_formatp->efd_efi_id;
  2728. /*
  2729. * Search for the efi with the id in the efd format structure
  2730. * in the AIL.
  2731. */
  2732. spin_lock(&ailp->xa_lock);
  2733. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  2734. while (lip != NULL) {
  2735. if (lip->li_type == XFS_LI_EFI) {
  2736. efip = (xfs_efi_log_item_t *)lip;
  2737. if (efip->efi_format.efi_id == efi_id) {
  2738. /*
  2739. * xfs_trans_ail_delete() drops the
  2740. * AIL lock.
  2741. */
  2742. xfs_trans_ail_delete(ailp, lip,
  2743. SHUTDOWN_CORRUPT_INCORE);
  2744. xfs_efi_item_free(efip);
  2745. spin_lock(&ailp->xa_lock);
  2746. break;
  2747. }
  2748. }
  2749. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  2750. }
  2751. xfs_trans_ail_cursor_done(&cur);
  2752. spin_unlock(&ailp->xa_lock);
  2753. return 0;
  2754. }
  2755. /*
  2756. * This routine is called when an inode create format structure is found in a
  2757. * committed transaction in the log. It's purpose is to initialise the inodes
  2758. * being allocated on disk. This requires us to get inode cluster buffers that
  2759. * match the range to be intialised, stamped with inode templates and written
  2760. * by delayed write so that subsequent modifications will hit the cached buffer
  2761. * and only need writing out at the end of recovery.
  2762. */
  2763. STATIC int
  2764. xlog_recover_do_icreate_pass2(
  2765. struct xlog *log,
  2766. struct list_head *buffer_list,
  2767. xlog_recover_item_t *item)
  2768. {
  2769. struct xfs_mount *mp = log->l_mp;
  2770. struct xfs_icreate_log *icl;
  2771. xfs_agnumber_t agno;
  2772. xfs_agblock_t agbno;
  2773. unsigned int count;
  2774. unsigned int isize;
  2775. xfs_agblock_t length;
  2776. icl = (struct xfs_icreate_log *)item->ri_buf[0].i_addr;
  2777. if (icl->icl_type != XFS_LI_ICREATE) {
  2778. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad type");
  2779. return -EINVAL;
  2780. }
  2781. if (icl->icl_size != 1) {
  2782. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad icl size");
  2783. return -EINVAL;
  2784. }
  2785. agno = be32_to_cpu(icl->icl_ag);
  2786. if (agno >= mp->m_sb.sb_agcount) {
  2787. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agno");
  2788. return -EINVAL;
  2789. }
  2790. agbno = be32_to_cpu(icl->icl_agbno);
  2791. if (!agbno || agbno == NULLAGBLOCK || agbno >= mp->m_sb.sb_agblocks) {
  2792. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agbno");
  2793. return -EINVAL;
  2794. }
  2795. isize = be32_to_cpu(icl->icl_isize);
  2796. if (isize != mp->m_sb.sb_inodesize) {
  2797. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad isize");
  2798. return -EINVAL;
  2799. }
  2800. count = be32_to_cpu(icl->icl_count);
  2801. if (!count) {
  2802. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad count");
  2803. return -EINVAL;
  2804. }
  2805. length = be32_to_cpu(icl->icl_length);
  2806. if (!length || length >= mp->m_sb.sb_agblocks) {
  2807. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad length");
  2808. return -EINVAL;
  2809. }
  2810. /*
  2811. * The inode chunk is either full or sparse and we only support
  2812. * m_ialloc_min_blks sized sparse allocations at this time.
  2813. */
  2814. if (length != mp->m_ialloc_blks &&
  2815. length != mp->m_ialloc_min_blks) {
  2816. xfs_warn(log->l_mp,
  2817. "%s: unsupported chunk length", __FUNCTION__);
  2818. return -EINVAL;
  2819. }
  2820. /* verify inode count is consistent with extent length */
  2821. if ((count >> mp->m_sb.sb_inopblog) != length) {
  2822. xfs_warn(log->l_mp,
  2823. "%s: inconsistent inode count and chunk length",
  2824. __FUNCTION__);
  2825. return -EINVAL;
  2826. }
  2827. /*
  2828. * Inode buffers can be freed. Do not replay the inode initialisation as
  2829. * we could be overwriting something written after this inode buffer was
  2830. * cancelled.
  2831. *
  2832. * XXX: we need to iterate all buffers and only init those that are not
  2833. * cancelled. I think that a more fine grained factoring of
  2834. * xfs_ialloc_inode_init may be appropriate here to enable this to be
  2835. * done easily.
  2836. */
  2837. if (xlog_check_buffer_cancelled(log,
  2838. XFS_AGB_TO_DADDR(mp, agno, agbno), length, 0))
  2839. return 0;
  2840. xfs_ialloc_inode_init(mp, NULL, buffer_list, count, agno, agbno, length,
  2841. be32_to_cpu(icl->icl_gen));
  2842. return 0;
  2843. }
  2844. STATIC void
  2845. xlog_recover_buffer_ra_pass2(
  2846. struct xlog *log,
  2847. struct xlog_recover_item *item)
  2848. {
  2849. struct xfs_buf_log_format *buf_f = item->ri_buf[0].i_addr;
  2850. struct xfs_mount *mp = log->l_mp;
  2851. if (xlog_peek_buffer_cancelled(log, buf_f->blf_blkno,
  2852. buf_f->blf_len, buf_f->blf_flags)) {
  2853. return;
  2854. }
  2855. xfs_buf_readahead(mp->m_ddev_targp, buf_f->blf_blkno,
  2856. buf_f->blf_len, NULL);
  2857. }
  2858. STATIC void
  2859. xlog_recover_inode_ra_pass2(
  2860. struct xlog *log,
  2861. struct xlog_recover_item *item)
  2862. {
  2863. struct xfs_inode_log_format ilf_buf;
  2864. struct xfs_inode_log_format *ilfp;
  2865. struct xfs_mount *mp = log->l_mp;
  2866. int error;
  2867. if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
  2868. ilfp = item->ri_buf[0].i_addr;
  2869. } else {
  2870. ilfp = &ilf_buf;
  2871. memset(ilfp, 0, sizeof(*ilfp));
  2872. error = xfs_inode_item_format_convert(&item->ri_buf[0], ilfp);
  2873. if (error)
  2874. return;
  2875. }
  2876. if (xlog_peek_buffer_cancelled(log, ilfp->ilf_blkno, ilfp->ilf_len, 0))
  2877. return;
  2878. xfs_buf_readahead(mp->m_ddev_targp, ilfp->ilf_blkno,
  2879. ilfp->ilf_len, &xfs_inode_buf_ra_ops);
  2880. }
  2881. STATIC void
  2882. xlog_recover_dquot_ra_pass2(
  2883. struct xlog *log,
  2884. struct xlog_recover_item *item)
  2885. {
  2886. struct xfs_mount *mp = log->l_mp;
  2887. struct xfs_disk_dquot *recddq;
  2888. struct xfs_dq_logformat *dq_f;
  2889. uint type;
  2890. if (mp->m_qflags == 0)
  2891. return;
  2892. recddq = item->ri_buf[1].i_addr;
  2893. if (recddq == NULL)
  2894. return;
  2895. if (item->ri_buf[1].i_len < sizeof(struct xfs_disk_dquot))
  2896. return;
  2897. type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
  2898. ASSERT(type);
  2899. if (log->l_quotaoffs_flag & type)
  2900. return;
  2901. dq_f = item->ri_buf[0].i_addr;
  2902. ASSERT(dq_f);
  2903. ASSERT(dq_f->qlf_len == 1);
  2904. xfs_buf_readahead(mp->m_ddev_targp, dq_f->qlf_blkno,
  2905. XFS_FSB_TO_BB(mp, dq_f->qlf_len), NULL);
  2906. }
  2907. STATIC void
  2908. xlog_recover_ra_pass2(
  2909. struct xlog *log,
  2910. struct xlog_recover_item *item)
  2911. {
  2912. switch (ITEM_TYPE(item)) {
  2913. case XFS_LI_BUF:
  2914. xlog_recover_buffer_ra_pass2(log, item);
  2915. break;
  2916. case XFS_LI_INODE:
  2917. xlog_recover_inode_ra_pass2(log, item);
  2918. break;
  2919. case XFS_LI_DQUOT:
  2920. xlog_recover_dquot_ra_pass2(log, item);
  2921. break;
  2922. case XFS_LI_EFI:
  2923. case XFS_LI_EFD:
  2924. case XFS_LI_QUOTAOFF:
  2925. default:
  2926. break;
  2927. }
  2928. }
  2929. STATIC int
  2930. xlog_recover_commit_pass1(
  2931. struct xlog *log,
  2932. struct xlog_recover *trans,
  2933. struct xlog_recover_item *item)
  2934. {
  2935. trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS1);
  2936. switch (ITEM_TYPE(item)) {
  2937. case XFS_LI_BUF:
  2938. return xlog_recover_buffer_pass1(log, item);
  2939. case XFS_LI_QUOTAOFF:
  2940. return xlog_recover_quotaoff_pass1(log, item);
  2941. case XFS_LI_INODE:
  2942. case XFS_LI_EFI:
  2943. case XFS_LI_EFD:
  2944. case XFS_LI_DQUOT:
  2945. case XFS_LI_ICREATE:
  2946. /* nothing to do in pass 1 */
  2947. return 0;
  2948. default:
  2949. xfs_warn(log->l_mp, "%s: invalid item type (%d)",
  2950. __func__, ITEM_TYPE(item));
  2951. ASSERT(0);
  2952. return -EIO;
  2953. }
  2954. }
  2955. STATIC int
  2956. xlog_recover_commit_pass2(
  2957. struct xlog *log,
  2958. struct xlog_recover *trans,
  2959. struct list_head *buffer_list,
  2960. struct xlog_recover_item *item)
  2961. {
  2962. trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS2);
  2963. switch (ITEM_TYPE(item)) {
  2964. case XFS_LI_BUF:
  2965. return xlog_recover_buffer_pass2(log, buffer_list, item,
  2966. trans->r_lsn);
  2967. case XFS_LI_INODE:
  2968. return xlog_recover_inode_pass2(log, buffer_list, item,
  2969. trans->r_lsn);
  2970. case XFS_LI_EFI:
  2971. return xlog_recover_efi_pass2(log, item, trans->r_lsn);
  2972. case XFS_LI_EFD:
  2973. return xlog_recover_efd_pass2(log, item);
  2974. case XFS_LI_DQUOT:
  2975. return xlog_recover_dquot_pass2(log, buffer_list, item,
  2976. trans->r_lsn);
  2977. case XFS_LI_ICREATE:
  2978. return xlog_recover_do_icreate_pass2(log, buffer_list, item);
  2979. case XFS_LI_QUOTAOFF:
  2980. /* nothing to do in pass2 */
  2981. return 0;
  2982. default:
  2983. xfs_warn(log->l_mp, "%s: invalid item type (%d)",
  2984. __func__, ITEM_TYPE(item));
  2985. ASSERT(0);
  2986. return -EIO;
  2987. }
  2988. }
  2989. STATIC int
  2990. xlog_recover_items_pass2(
  2991. struct xlog *log,
  2992. struct xlog_recover *trans,
  2993. struct list_head *buffer_list,
  2994. struct list_head *item_list)
  2995. {
  2996. struct xlog_recover_item *item;
  2997. int error = 0;
  2998. list_for_each_entry(item, item_list, ri_list) {
  2999. error = xlog_recover_commit_pass2(log, trans,
  3000. buffer_list, item);
  3001. if (error)
  3002. return error;
  3003. }
  3004. return error;
  3005. }
  3006. /*
  3007. * Perform the transaction.
  3008. *
  3009. * If the transaction modifies a buffer or inode, do it now. Otherwise,
  3010. * EFIs and EFDs get queued up by adding entries into the AIL for them.
  3011. */
  3012. STATIC int
  3013. xlog_recover_commit_trans(
  3014. struct xlog *log,
  3015. struct xlog_recover *trans,
  3016. int pass)
  3017. {
  3018. int error = 0;
  3019. int error2;
  3020. int items_queued = 0;
  3021. struct xlog_recover_item *item;
  3022. struct xlog_recover_item *next;
  3023. LIST_HEAD (buffer_list);
  3024. LIST_HEAD (ra_list);
  3025. LIST_HEAD (done_list);
  3026. #define XLOG_RECOVER_COMMIT_QUEUE_MAX 100
  3027. hlist_del(&trans->r_list);
  3028. error = xlog_recover_reorder_trans(log, trans, pass);
  3029. if (error)
  3030. return error;
  3031. list_for_each_entry_safe(item, next, &trans->r_itemq, ri_list) {
  3032. switch (pass) {
  3033. case XLOG_RECOVER_PASS1:
  3034. error = xlog_recover_commit_pass1(log, trans, item);
  3035. break;
  3036. case XLOG_RECOVER_PASS2:
  3037. xlog_recover_ra_pass2(log, item);
  3038. list_move_tail(&item->ri_list, &ra_list);
  3039. items_queued++;
  3040. if (items_queued >= XLOG_RECOVER_COMMIT_QUEUE_MAX) {
  3041. error = xlog_recover_items_pass2(log, trans,
  3042. &buffer_list, &ra_list);
  3043. list_splice_tail_init(&ra_list, &done_list);
  3044. items_queued = 0;
  3045. }
  3046. break;
  3047. default:
  3048. ASSERT(0);
  3049. }
  3050. if (error)
  3051. goto out;
  3052. }
  3053. out:
  3054. if (!list_empty(&ra_list)) {
  3055. if (!error)
  3056. error = xlog_recover_items_pass2(log, trans,
  3057. &buffer_list, &ra_list);
  3058. list_splice_tail_init(&ra_list, &done_list);
  3059. }
  3060. if (!list_empty(&done_list))
  3061. list_splice_init(&done_list, &trans->r_itemq);
  3062. error2 = xfs_buf_delwri_submit(&buffer_list);
  3063. return error ? error : error2;
  3064. }
  3065. STATIC void
  3066. xlog_recover_add_item(
  3067. struct list_head *head)
  3068. {
  3069. xlog_recover_item_t *item;
  3070. item = kmem_zalloc(sizeof(xlog_recover_item_t), KM_SLEEP);
  3071. INIT_LIST_HEAD(&item->ri_list);
  3072. list_add_tail(&item->ri_list, head);
  3073. }
  3074. STATIC int
  3075. xlog_recover_add_to_cont_trans(
  3076. struct xlog *log,
  3077. struct xlog_recover *trans,
  3078. char *dp,
  3079. int len)
  3080. {
  3081. xlog_recover_item_t *item;
  3082. char *ptr, *old_ptr;
  3083. int old_len;
  3084. if (list_empty(&trans->r_itemq)) {
  3085. /* finish copying rest of trans header */
  3086. xlog_recover_add_item(&trans->r_itemq);
  3087. ptr = (char *)&trans->r_theader +
  3088. sizeof(xfs_trans_header_t) - len;
  3089. memcpy(ptr, dp, len);
  3090. return 0;
  3091. }
  3092. /* take the tail entry */
  3093. item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
  3094. old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
  3095. old_len = item->ri_buf[item->ri_cnt-1].i_len;
  3096. ptr = kmem_realloc(old_ptr, len+old_len, old_len, KM_SLEEP);
  3097. memcpy(&ptr[old_len], dp, len);
  3098. item->ri_buf[item->ri_cnt-1].i_len += len;
  3099. item->ri_buf[item->ri_cnt-1].i_addr = ptr;
  3100. trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
  3101. return 0;
  3102. }
  3103. /*
  3104. * The next region to add is the start of a new region. It could be
  3105. * a whole region or it could be the first part of a new region. Because
  3106. * of this, the assumption here is that the type and size fields of all
  3107. * format structures fit into the first 32 bits of the structure.
  3108. *
  3109. * This works because all regions must be 32 bit aligned. Therefore, we
  3110. * either have both fields or we have neither field. In the case we have
  3111. * neither field, the data part of the region is zero length. We only have
  3112. * a log_op_header and can throw away the header since a new one will appear
  3113. * later. If we have at least 4 bytes, then we can determine how many regions
  3114. * will appear in the current log item.
  3115. */
  3116. STATIC int
  3117. xlog_recover_add_to_trans(
  3118. struct xlog *log,
  3119. struct xlog_recover *trans,
  3120. char *dp,
  3121. int len)
  3122. {
  3123. xfs_inode_log_format_t *in_f; /* any will do */
  3124. xlog_recover_item_t *item;
  3125. char *ptr;
  3126. if (!len)
  3127. return 0;
  3128. if (list_empty(&trans->r_itemq)) {
  3129. /* we need to catch log corruptions here */
  3130. if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
  3131. xfs_warn(log->l_mp, "%s: bad header magic number",
  3132. __func__);
  3133. ASSERT(0);
  3134. return -EIO;
  3135. }
  3136. if (len == sizeof(xfs_trans_header_t))
  3137. xlog_recover_add_item(&trans->r_itemq);
  3138. memcpy(&trans->r_theader, dp, len);
  3139. return 0;
  3140. }
  3141. ptr = kmem_alloc(len, KM_SLEEP);
  3142. memcpy(ptr, dp, len);
  3143. in_f = (xfs_inode_log_format_t *)ptr;
  3144. /* take the tail entry */
  3145. item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
  3146. if (item->ri_total != 0 &&
  3147. item->ri_total == item->ri_cnt) {
  3148. /* tail item is in use, get a new one */
  3149. xlog_recover_add_item(&trans->r_itemq);
  3150. item = list_entry(trans->r_itemq.prev,
  3151. xlog_recover_item_t, ri_list);
  3152. }
  3153. if (item->ri_total == 0) { /* first region to be added */
  3154. if (in_f->ilf_size == 0 ||
  3155. in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
  3156. xfs_warn(log->l_mp,
  3157. "bad number of regions (%d) in inode log format",
  3158. in_f->ilf_size);
  3159. ASSERT(0);
  3160. kmem_free(ptr);
  3161. return -EIO;
  3162. }
  3163. item->ri_total = in_f->ilf_size;
  3164. item->ri_buf =
  3165. kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
  3166. KM_SLEEP);
  3167. }
  3168. ASSERT(item->ri_total > item->ri_cnt);
  3169. /* Description region is ri_buf[0] */
  3170. item->ri_buf[item->ri_cnt].i_addr = ptr;
  3171. item->ri_buf[item->ri_cnt].i_len = len;
  3172. item->ri_cnt++;
  3173. trace_xfs_log_recover_item_add(log, trans, item, 0);
  3174. return 0;
  3175. }
  3176. /*
  3177. * Free up any resources allocated by the transaction
  3178. *
  3179. * Remember that EFIs, EFDs, and IUNLINKs are handled later.
  3180. */
  3181. STATIC void
  3182. xlog_recover_free_trans(
  3183. struct xlog_recover *trans)
  3184. {
  3185. xlog_recover_item_t *item, *n;
  3186. int i;
  3187. list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
  3188. /* Free the regions in the item. */
  3189. list_del(&item->ri_list);
  3190. for (i = 0; i < item->ri_cnt; i++)
  3191. kmem_free(item->ri_buf[i].i_addr);
  3192. /* Free the item itself */
  3193. kmem_free(item->ri_buf);
  3194. kmem_free(item);
  3195. }
  3196. /* Free the transaction recover structure */
  3197. kmem_free(trans);
  3198. }
  3199. /*
  3200. * On error or completion, trans is freed.
  3201. */
  3202. STATIC int
  3203. xlog_recovery_process_trans(
  3204. struct xlog *log,
  3205. struct xlog_recover *trans,
  3206. char *dp,
  3207. unsigned int len,
  3208. unsigned int flags,
  3209. int pass)
  3210. {
  3211. int error = 0;
  3212. bool freeit = false;
  3213. /* mask off ophdr transaction container flags */
  3214. flags &= ~XLOG_END_TRANS;
  3215. if (flags & XLOG_WAS_CONT_TRANS)
  3216. flags &= ~XLOG_CONTINUE_TRANS;
  3217. /*
  3218. * Callees must not free the trans structure. We'll decide if we need to
  3219. * free it or not based on the operation being done and it's result.
  3220. */
  3221. switch (flags) {
  3222. /* expected flag values */
  3223. case 0:
  3224. case XLOG_CONTINUE_TRANS:
  3225. error = xlog_recover_add_to_trans(log, trans, dp, len);
  3226. break;
  3227. case XLOG_WAS_CONT_TRANS:
  3228. error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
  3229. break;
  3230. case XLOG_COMMIT_TRANS:
  3231. error = xlog_recover_commit_trans(log, trans, pass);
  3232. /* success or fail, we are now done with this transaction. */
  3233. freeit = true;
  3234. break;
  3235. /* unexpected flag values */
  3236. case XLOG_UNMOUNT_TRANS:
  3237. /* just skip trans */
  3238. xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
  3239. freeit = true;
  3240. break;
  3241. case XLOG_START_TRANS:
  3242. default:
  3243. xfs_warn(log->l_mp, "%s: bad flag 0x%x", __func__, flags);
  3244. ASSERT(0);
  3245. error = -EIO;
  3246. break;
  3247. }
  3248. if (error || freeit)
  3249. xlog_recover_free_trans(trans);
  3250. return error;
  3251. }
  3252. /*
  3253. * Lookup the transaction recovery structure associated with the ID in the
  3254. * current ophdr. If the transaction doesn't exist and the start flag is set in
  3255. * the ophdr, then allocate a new transaction for future ID matches to find.
  3256. * Either way, return what we found during the lookup - an existing transaction
  3257. * or nothing.
  3258. */
  3259. STATIC struct xlog_recover *
  3260. xlog_recover_ophdr_to_trans(
  3261. struct hlist_head rhash[],
  3262. struct xlog_rec_header *rhead,
  3263. struct xlog_op_header *ohead)
  3264. {
  3265. struct xlog_recover *trans;
  3266. xlog_tid_t tid;
  3267. struct hlist_head *rhp;
  3268. tid = be32_to_cpu(ohead->oh_tid);
  3269. rhp = &rhash[XLOG_RHASH(tid)];
  3270. hlist_for_each_entry(trans, rhp, r_list) {
  3271. if (trans->r_log_tid == tid)
  3272. return trans;
  3273. }
  3274. /*
  3275. * skip over non-start transaction headers - we could be
  3276. * processing slack space before the next transaction starts
  3277. */
  3278. if (!(ohead->oh_flags & XLOG_START_TRANS))
  3279. return NULL;
  3280. ASSERT(be32_to_cpu(ohead->oh_len) == 0);
  3281. /*
  3282. * This is a new transaction so allocate a new recovery container to
  3283. * hold the recovery ops that will follow.
  3284. */
  3285. trans = kmem_zalloc(sizeof(struct xlog_recover), KM_SLEEP);
  3286. trans->r_log_tid = tid;
  3287. trans->r_lsn = be64_to_cpu(rhead->h_lsn);
  3288. INIT_LIST_HEAD(&trans->r_itemq);
  3289. INIT_HLIST_NODE(&trans->r_list);
  3290. hlist_add_head(&trans->r_list, rhp);
  3291. /*
  3292. * Nothing more to do for this ophdr. Items to be added to this new
  3293. * transaction will be in subsequent ophdr containers.
  3294. */
  3295. return NULL;
  3296. }
  3297. STATIC int
  3298. xlog_recover_process_ophdr(
  3299. struct xlog *log,
  3300. struct hlist_head rhash[],
  3301. struct xlog_rec_header *rhead,
  3302. struct xlog_op_header *ohead,
  3303. char *dp,
  3304. char *end,
  3305. int pass)
  3306. {
  3307. struct xlog_recover *trans;
  3308. unsigned int len;
  3309. /* Do we understand who wrote this op? */
  3310. if (ohead->oh_clientid != XFS_TRANSACTION &&
  3311. ohead->oh_clientid != XFS_LOG) {
  3312. xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
  3313. __func__, ohead->oh_clientid);
  3314. ASSERT(0);
  3315. return -EIO;
  3316. }
  3317. /*
  3318. * Check the ophdr contains all the data it is supposed to contain.
  3319. */
  3320. len = be32_to_cpu(ohead->oh_len);
  3321. if (dp + len > end) {
  3322. xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
  3323. WARN_ON(1);
  3324. return -EIO;
  3325. }
  3326. trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
  3327. if (!trans) {
  3328. /* nothing to do, so skip over this ophdr */
  3329. return 0;
  3330. }
  3331. return xlog_recovery_process_trans(log, trans, dp, len,
  3332. ohead->oh_flags, pass);
  3333. }
  3334. /*
  3335. * There are two valid states of the r_state field. 0 indicates that the
  3336. * transaction structure is in a normal state. We have either seen the
  3337. * start of the transaction or the last operation we added was not a partial
  3338. * operation. If the last operation we added to the transaction was a
  3339. * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
  3340. *
  3341. * NOTE: skip LRs with 0 data length.
  3342. */
  3343. STATIC int
  3344. xlog_recover_process_data(
  3345. struct xlog *log,
  3346. struct hlist_head rhash[],
  3347. struct xlog_rec_header *rhead,
  3348. char *dp,
  3349. int pass)
  3350. {
  3351. struct xlog_op_header *ohead;
  3352. char *end;
  3353. int num_logops;
  3354. int error;
  3355. end = dp + be32_to_cpu(rhead->h_len);
  3356. num_logops = be32_to_cpu(rhead->h_num_logops);
  3357. /* check the log format matches our own - else we can't recover */
  3358. if (xlog_header_check_recover(log->l_mp, rhead))
  3359. return -EIO;
  3360. while ((dp < end) && num_logops) {
  3361. ohead = (struct xlog_op_header *)dp;
  3362. dp += sizeof(*ohead);
  3363. ASSERT(dp <= end);
  3364. /* errors will abort recovery */
  3365. error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
  3366. dp, end, pass);
  3367. if (error)
  3368. return error;
  3369. dp += be32_to_cpu(ohead->oh_len);
  3370. num_logops--;
  3371. }
  3372. return 0;
  3373. }
  3374. /*
  3375. * Process an extent free intent item that was recovered from
  3376. * the log. We need to free the extents that it describes.
  3377. */
  3378. STATIC int
  3379. xlog_recover_process_efi(
  3380. xfs_mount_t *mp,
  3381. xfs_efi_log_item_t *efip)
  3382. {
  3383. xfs_efd_log_item_t *efdp;
  3384. xfs_trans_t *tp;
  3385. int i;
  3386. int error = 0;
  3387. xfs_extent_t *extp;
  3388. xfs_fsblock_t startblock_fsb;
  3389. ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
  3390. /*
  3391. * First check the validity of the extents described by the
  3392. * EFI. If any are bad, then assume that all are bad and
  3393. * just toss the EFI.
  3394. */
  3395. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  3396. extp = &(efip->efi_format.efi_extents[i]);
  3397. startblock_fsb = XFS_BB_TO_FSB(mp,
  3398. XFS_FSB_TO_DADDR(mp, extp->ext_start));
  3399. if ((startblock_fsb == 0) ||
  3400. (extp->ext_len == 0) ||
  3401. (startblock_fsb >= mp->m_sb.sb_dblocks) ||
  3402. (extp->ext_len >= mp->m_sb.sb_agblocks)) {
  3403. /*
  3404. * This will pull the EFI from the AIL and
  3405. * free the memory associated with it.
  3406. */
  3407. set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
  3408. xfs_efi_release(efip, efip->efi_format.efi_nextents);
  3409. return -EIO;
  3410. }
  3411. }
  3412. tp = xfs_trans_alloc(mp, 0);
  3413. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  3414. if (error)
  3415. goto abort_error;
  3416. efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
  3417. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  3418. extp = &(efip->efi_format.efi_extents[i]);
  3419. error = xfs_free_extent(tp, extp->ext_start, extp->ext_len);
  3420. if (error)
  3421. goto abort_error;
  3422. xfs_trans_log_efd_extent(tp, efdp, extp->ext_start,
  3423. extp->ext_len);
  3424. }
  3425. set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
  3426. error = xfs_trans_commit(tp);
  3427. return error;
  3428. abort_error:
  3429. xfs_trans_cancel(tp);
  3430. return error;
  3431. }
  3432. /*
  3433. * When this is called, all of the EFIs which did not have
  3434. * corresponding EFDs should be in the AIL. What we do now
  3435. * is free the extents associated with each one.
  3436. *
  3437. * Since we process the EFIs in normal transactions, they
  3438. * will be removed at some point after the commit. This prevents
  3439. * us from just walking down the list processing each one.
  3440. * We'll use a flag in the EFI to skip those that we've already
  3441. * processed and use the AIL iteration mechanism's generation
  3442. * count to try to speed this up at least a bit.
  3443. *
  3444. * When we start, we know that the EFIs are the only things in
  3445. * the AIL. As we process them, however, other items are added
  3446. * to the AIL. Since everything added to the AIL must come after
  3447. * everything already in the AIL, we stop processing as soon as
  3448. * we see something other than an EFI in the AIL.
  3449. */
  3450. STATIC int
  3451. xlog_recover_process_efis(
  3452. struct xlog *log)
  3453. {
  3454. xfs_log_item_t *lip;
  3455. xfs_efi_log_item_t *efip;
  3456. int error = 0;
  3457. struct xfs_ail_cursor cur;
  3458. struct xfs_ail *ailp;
  3459. ailp = log->l_ailp;
  3460. spin_lock(&ailp->xa_lock);
  3461. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  3462. while (lip != NULL) {
  3463. /*
  3464. * We're done when we see something other than an EFI.
  3465. * There should be no EFIs left in the AIL now.
  3466. */
  3467. if (lip->li_type != XFS_LI_EFI) {
  3468. #ifdef DEBUG
  3469. for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
  3470. ASSERT(lip->li_type != XFS_LI_EFI);
  3471. #endif
  3472. break;
  3473. }
  3474. /*
  3475. * Skip EFIs that we've already processed.
  3476. */
  3477. efip = (xfs_efi_log_item_t *)lip;
  3478. if (test_bit(XFS_EFI_RECOVERED, &efip->efi_flags)) {
  3479. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  3480. continue;
  3481. }
  3482. spin_unlock(&ailp->xa_lock);
  3483. error = xlog_recover_process_efi(log->l_mp, efip);
  3484. spin_lock(&ailp->xa_lock);
  3485. if (error)
  3486. goto out;
  3487. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  3488. }
  3489. out:
  3490. xfs_trans_ail_cursor_done(&cur);
  3491. spin_unlock(&ailp->xa_lock);
  3492. return error;
  3493. }
  3494. /*
  3495. * This routine performs a transaction to null out a bad inode pointer
  3496. * in an agi unlinked inode hash bucket.
  3497. */
  3498. STATIC void
  3499. xlog_recover_clear_agi_bucket(
  3500. xfs_mount_t *mp,
  3501. xfs_agnumber_t agno,
  3502. int bucket)
  3503. {
  3504. xfs_trans_t *tp;
  3505. xfs_agi_t *agi;
  3506. xfs_buf_t *agibp;
  3507. int offset;
  3508. int error;
  3509. tp = xfs_trans_alloc(mp, XFS_TRANS_CLEAR_AGI_BUCKET);
  3510. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_clearagi, 0, 0);
  3511. if (error)
  3512. goto out_abort;
  3513. error = xfs_read_agi(mp, tp, agno, &agibp);
  3514. if (error)
  3515. goto out_abort;
  3516. agi = XFS_BUF_TO_AGI(agibp);
  3517. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  3518. offset = offsetof(xfs_agi_t, agi_unlinked) +
  3519. (sizeof(xfs_agino_t) * bucket);
  3520. xfs_trans_log_buf(tp, agibp, offset,
  3521. (offset + sizeof(xfs_agino_t) - 1));
  3522. error = xfs_trans_commit(tp);
  3523. if (error)
  3524. goto out_error;
  3525. return;
  3526. out_abort:
  3527. xfs_trans_cancel(tp);
  3528. out_error:
  3529. xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__, agno);
  3530. return;
  3531. }
  3532. STATIC xfs_agino_t
  3533. xlog_recover_process_one_iunlink(
  3534. struct xfs_mount *mp,
  3535. xfs_agnumber_t agno,
  3536. xfs_agino_t agino,
  3537. int bucket)
  3538. {
  3539. struct xfs_buf *ibp;
  3540. struct xfs_dinode *dip;
  3541. struct xfs_inode *ip;
  3542. xfs_ino_t ino;
  3543. int error;
  3544. ino = XFS_AGINO_TO_INO(mp, agno, agino);
  3545. error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
  3546. if (error)
  3547. goto fail;
  3548. /*
  3549. * Get the on disk inode to find the next inode in the bucket.
  3550. */
  3551. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &ibp, 0, 0);
  3552. if (error)
  3553. goto fail_iput;
  3554. ASSERT(ip->i_d.di_nlink == 0);
  3555. ASSERT(ip->i_d.di_mode != 0);
  3556. /* setup for the next pass */
  3557. agino = be32_to_cpu(dip->di_next_unlinked);
  3558. xfs_buf_relse(ibp);
  3559. /*
  3560. * Prevent any DMAPI event from being sent when the reference on
  3561. * the inode is dropped.
  3562. */
  3563. ip->i_d.di_dmevmask = 0;
  3564. IRELE(ip);
  3565. return agino;
  3566. fail_iput:
  3567. IRELE(ip);
  3568. fail:
  3569. /*
  3570. * We can't read in the inode this bucket points to, or this inode
  3571. * is messed up. Just ditch this bucket of inodes. We will lose
  3572. * some inodes and space, but at least we won't hang.
  3573. *
  3574. * Call xlog_recover_clear_agi_bucket() to perform a transaction to
  3575. * clear the inode pointer in the bucket.
  3576. */
  3577. xlog_recover_clear_agi_bucket(mp, agno, bucket);
  3578. return NULLAGINO;
  3579. }
  3580. /*
  3581. * xlog_iunlink_recover
  3582. *
  3583. * This is called during recovery to process any inodes which
  3584. * we unlinked but not freed when the system crashed. These
  3585. * inodes will be on the lists in the AGI blocks. What we do
  3586. * here is scan all the AGIs and fully truncate and free any
  3587. * inodes found on the lists. Each inode is removed from the
  3588. * lists when it has been fully truncated and is freed. The
  3589. * freeing of the inode and its removal from the list must be
  3590. * atomic.
  3591. */
  3592. STATIC void
  3593. xlog_recover_process_iunlinks(
  3594. struct xlog *log)
  3595. {
  3596. xfs_mount_t *mp;
  3597. xfs_agnumber_t agno;
  3598. xfs_agi_t *agi;
  3599. xfs_buf_t *agibp;
  3600. xfs_agino_t agino;
  3601. int bucket;
  3602. int error;
  3603. uint mp_dmevmask;
  3604. mp = log->l_mp;
  3605. /*
  3606. * Prevent any DMAPI event from being sent while in this function.
  3607. */
  3608. mp_dmevmask = mp->m_dmevmask;
  3609. mp->m_dmevmask = 0;
  3610. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  3611. /*
  3612. * Find the agi for this ag.
  3613. */
  3614. error = xfs_read_agi(mp, NULL, agno, &agibp);
  3615. if (error) {
  3616. /*
  3617. * AGI is b0rked. Don't process it.
  3618. *
  3619. * We should probably mark the filesystem as corrupt
  3620. * after we've recovered all the ag's we can....
  3621. */
  3622. continue;
  3623. }
  3624. /*
  3625. * Unlock the buffer so that it can be acquired in the normal
  3626. * course of the transaction to truncate and free each inode.
  3627. * Because we are not racing with anyone else here for the AGI
  3628. * buffer, we don't even need to hold it locked to read the
  3629. * initial unlinked bucket entries out of the buffer. We keep
  3630. * buffer reference though, so that it stays pinned in memory
  3631. * while we need the buffer.
  3632. */
  3633. agi = XFS_BUF_TO_AGI(agibp);
  3634. xfs_buf_unlock(agibp);
  3635. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
  3636. agino = be32_to_cpu(agi->agi_unlinked[bucket]);
  3637. while (agino != NULLAGINO) {
  3638. agino = xlog_recover_process_one_iunlink(mp,
  3639. agno, agino, bucket);
  3640. }
  3641. }
  3642. xfs_buf_rele(agibp);
  3643. }
  3644. mp->m_dmevmask = mp_dmevmask;
  3645. }
  3646. /*
  3647. * Upack the log buffer data and crc check it. If the check fails, issue a
  3648. * warning if and only if the CRC in the header is non-zero. This makes the
  3649. * check an advisory warning, and the zero CRC check will prevent failure
  3650. * warnings from being emitted when upgrading the kernel from one that does not
  3651. * add CRCs by default.
  3652. *
  3653. * When filesystems are CRC enabled, this CRC mismatch becomes a fatal log
  3654. * corruption failure
  3655. */
  3656. STATIC int
  3657. xlog_unpack_data_crc(
  3658. struct xlog_rec_header *rhead,
  3659. char *dp,
  3660. struct xlog *log)
  3661. {
  3662. __le32 crc;
  3663. crc = xlog_cksum(log, rhead, dp, be32_to_cpu(rhead->h_len));
  3664. if (crc != rhead->h_crc) {
  3665. if (rhead->h_crc || xfs_sb_version_hascrc(&log->l_mp->m_sb)) {
  3666. xfs_alert(log->l_mp,
  3667. "log record CRC mismatch: found 0x%x, expected 0x%x.",
  3668. le32_to_cpu(rhead->h_crc),
  3669. le32_to_cpu(crc));
  3670. xfs_hex_dump(dp, 32);
  3671. }
  3672. /*
  3673. * If we've detected a log record corruption, then we can't
  3674. * recover past this point. Abort recovery if we are enforcing
  3675. * CRC protection by punting an error back up the stack.
  3676. */
  3677. if (xfs_sb_version_hascrc(&log->l_mp->m_sb))
  3678. return -EFSCORRUPTED;
  3679. }
  3680. return 0;
  3681. }
  3682. STATIC int
  3683. xlog_unpack_data(
  3684. struct xlog_rec_header *rhead,
  3685. char *dp,
  3686. struct xlog *log)
  3687. {
  3688. int i, j, k;
  3689. int error;
  3690. error = xlog_unpack_data_crc(rhead, dp, log);
  3691. if (error)
  3692. return error;
  3693. for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
  3694. i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
  3695. *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
  3696. dp += BBSIZE;
  3697. }
  3698. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  3699. xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
  3700. for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
  3701. j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3702. k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3703. *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
  3704. dp += BBSIZE;
  3705. }
  3706. }
  3707. return 0;
  3708. }
  3709. STATIC int
  3710. xlog_valid_rec_header(
  3711. struct xlog *log,
  3712. struct xlog_rec_header *rhead,
  3713. xfs_daddr_t blkno)
  3714. {
  3715. int hlen;
  3716. if (unlikely(rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))) {
  3717. XFS_ERROR_REPORT("xlog_valid_rec_header(1)",
  3718. XFS_ERRLEVEL_LOW, log->l_mp);
  3719. return -EFSCORRUPTED;
  3720. }
  3721. if (unlikely(
  3722. (!rhead->h_version ||
  3723. (be32_to_cpu(rhead->h_version) & (~XLOG_VERSION_OKBITS))))) {
  3724. xfs_warn(log->l_mp, "%s: unrecognised log version (%d).",
  3725. __func__, be32_to_cpu(rhead->h_version));
  3726. return -EIO;
  3727. }
  3728. /* LR body must have data or it wouldn't have been written */
  3729. hlen = be32_to_cpu(rhead->h_len);
  3730. if (unlikely( hlen <= 0 || hlen > INT_MAX )) {
  3731. XFS_ERROR_REPORT("xlog_valid_rec_header(2)",
  3732. XFS_ERRLEVEL_LOW, log->l_mp);
  3733. return -EFSCORRUPTED;
  3734. }
  3735. if (unlikely( blkno > log->l_logBBsize || blkno > INT_MAX )) {
  3736. XFS_ERROR_REPORT("xlog_valid_rec_header(3)",
  3737. XFS_ERRLEVEL_LOW, log->l_mp);
  3738. return -EFSCORRUPTED;
  3739. }
  3740. return 0;
  3741. }
  3742. /*
  3743. * Read the log from tail to head and process the log records found.
  3744. * Handle the two cases where the tail and head are in the same cycle
  3745. * and where the active portion of the log wraps around the end of
  3746. * the physical log separately. The pass parameter is passed through
  3747. * to the routines called to process the data and is not looked at
  3748. * here.
  3749. */
  3750. STATIC int
  3751. xlog_do_recovery_pass(
  3752. struct xlog *log,
  3753. xfs_daddr_t head_blk,
  3754. xfs_daddr_t tail_blk,
  3755. int pass)
  3756. {
  3757. xlog_rec_header_t *rhead;
  3758. xfs_daddr_t blk_no;
  3759. char *offset;
  3760. xfs_buf_t *hbp, *dbp;
  3761. int error = 0, h_size;
  3762. int bblks, split_bblks;
  3763. int hblks, split_hblks, wrapped_hblks;
  3764. struct hlist_head rhash[XLOG_RHASH_SIZE];
  3765. ASSERT(head_blk != tail_blk);
  3766. /*
  3767. * Read the header of the tail block and get the iclog buffer size from
  3768. * h_size. Use this to tell how many sectors make up the log header.
  3769. */
  3770. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  3771. /*
  3772. * When using variable length iclogs, read first sector of
  3773. * iclog header and extract the header size from it. Get a
  3774. * new hbp that is the correct size.
  3775. */
  3776. hbp = xlog_get_bp(log, 1);
  3777. if (!hbp)
  3778. return -ENOMEM;
  3779. error = xlog_bread(log, tail_blk, 1, hbp, &offset);
  3780. if (error)
  3781. goto bread_err1;
  3782. rhead = (xlog_rec_header_t *)offset;
  3783. error = xlog_valid_rec_header(log, rhead, tail_blk);
  3784. if (error)
  3785. goto bread_err1;
  3786. h_size = be32_to_cpu(rhead->h_size);
  3787. if ((be32_to_cpu(rhead->h_version) & XLOG_VERSION_2) &&
  3788. (h_size > XLOG_HEADER_CYCLE_SIZE)) {
  3789. hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
  3790. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  3791. hblks++;
  3792. xlog_put_bp(hbp);
  3793. hbp = xlog_get_bp(log, hblks);
  3794. } else {
  3795. hblks = 1;
  3796. }
  3797. } else {
  3798. ASSERT(log->l_sectBBsize == 1);
  3799. hblks = 1;
  3800. hbp = xlog_get_bp(log, 1);
  3801. h_size = XLOG_BIG_RECORD_BSIZE;
  3802. }
  3803. if (!hbp)
  3804. return -ENOMEM;
  3805. dbp = xlog_get_bp(log, BTOBB(h_size));
  3806. if (!dbp) {
  3807. xlog_put_bp(hbp);
  3808. return -ENOMEM;
  3809. }
  3810. memset(rhash, 0, sizeof(rhash));
  3811. blk_no = tail_blk;
  3812. if (tail_blk > head_blk) {
  3813. /*
  3814. * Perform recovery around the end of the physical log.
  3815. * When the head is not on the same cycle number as the tail,
  3816. * we can't do a sequential recovery.
  3817. */
  3818. while (blk_no < log->l_logBBsize) {
  3819. /*
  3820. * Check for header wrapping around physical end-of-log
  3821. */
  3822. offset = hbp->b_addr;
  3823. split_hblks = 0;
  3824. wrapped_hblks = 0;
  3825. if (blk_no + hblks <= log->l_logBBsize) {
  3826. /* Read header in one read */
  3827. error = xlog_bread(log, blk_no, hblks, hbp,
  3828. &offset);
  3829. if (error)
  3830. goto bread_err2;
  3831. } else {
  3832. /* This LR is split across physical log end */
  3833. if (blk_no != log->l_logBBsize) {
  3834. /* some data before physical log end */
  3835. ASSERT(blk_no <= INT_MAX);
  3836. split_hblks = log->l_logBBsize - (int)blk_no;
  3837. ASSERT(split_hblks > 0);
  3838. error = xlog_bread(log, blk_no,
  3839. split_hblks, hbp,
  3840. &offset);
  3841. if (error)
  3842. goto bread_err2;
  3843. }
  3844. /*
  3845. * Note: this black magic still works with
  3846. * large sector sizes (non-512) only because:
  3847. * - we increased the buffer size originally
  3848. * by 1 sector giving us enough extra space
  3849. * for the second read;
  3850. * - the log start is guaranteed to be sector
  3851. * aligned;
  3852. * - we read the log end (LR header start)
  3853. * _first_, then the log start (LR header end)
  3854. * - order is important.
  3855. */
  3856. wrapped_hblks = hblks - split_hblks;
  3857. error = xlog_bread_offset(log, 0,
  3858. wrapped_hblks, hbp,
  3859. offset + BBTOB(split_hblks));
  3860. if (error)
  3861. goto bread_err2;
  3862. }
  3863. rhead = (xlog_rec_header_t *)offset;
  3864. error = xlog_valid_rec_header(log, rhead,
  3865. split_hblks ? blk_no : 0);
  3866. if (error)
  3867. goto bread_err2;
  3868. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  3869. blk_no += hblks;
  3870. /* Read in data for log record */
  3871. if (blk_no + bblks <= log->l_logBBsize) {
  3872. error = xlog_bread(log, blk_no, bblks, dbp,
  3873. &offset);
  3874. if (error)
  3875. goto bread_err2;
  3876. } else {
  3877. /* This log record is split across the
  3878. * physical end of log */
  3879. offset = dbp->b_addr;
  3880. split_bblks = 0;
  3881. if (blk_no != log->l_logBBsize) {
  3882. /* some data is before the physical
  3883. * end of log */
  3884. ASSERT(!wrapped_hblks);
  3885. ASSERT(blk_no <= INT_MAX);
  3886. split_bblks =
  3887. log->l_logBBsize - (int)blk_no;
  3888. ASSERT(split_bblks > 0);
  3889. error = xlog_bread(log, blk_no,
  3890. split_bblks, dbp,
  3891. &offset);
  3892. if (error)
  3893. goto bread_err2;
  3894. }
  3895. /*
  3896. * Note: this black magic still works with
  3897. * large sector sizes (non-512) only because:
  3898. * - we increased the buffer size originally
  3899. * by 1 sector giving us enough extra space
  3900. * for the second read;
  3901. * - the log start is guaranteed to be sector
  3902. * aligned;
  3903. * - we read the log end (LR header start)
  3904. * _first_, then the log start (LR header end)
  3905. * - order is important.
  3906. */
  3907. error = xlog_bread_offset(log, 0,
  3908. bblks - split_bblks, dbp,
  3909. offset + BBTOB(split_bblks));
  3910. if (error)
  3911. goto bread_err2;
  3912. }
  3913. error = xlog_unpack_data(rhead, offset, log);
  3914. if (error)
  3915. goto bread_err2;
  3916. error = xlog_recover_process_data(log, rhash,
  3917. rhead, offset, pass);
  3918. if (error)
  3919. goto bread_err2;
  3920. blk_no += bblks;
  3921. }
  3922. ASSERT(blk_no >= log->l_logBBsize);
  3923. blk_no -= log->l_logBBsize;
  3924. }
  3925. /* read first part of physical log */
  3926. while (blk_no < head_blk) {
  3927. error = xlog_bread(log, blk_no, hblks, hbp, &offset);
  3928. if (error)
  3929. goto bread_err2;
  3930. rhead = (xlog_rec_header_t *)offset;
  3931. error = xlog_valid_rec_header(log, rhead, blk_no);
  3932. if (error)
  3933. goto bread_err2;
  3934. /* blocks in data section */
  3935. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  3936. error = xlog_bread(log, blk_no+hblks, bblks, dbp,
  3937. &offset);
  3938. if (error)
  3939. goto bread_err2;
  3940. error = xlog_unpack_data(rhead, offset, log);
  3941. if (error)
  3942. goto bread_err2;
  3943. error = xlog_recover_process_data(log, rhash,
  3944. rhead, offset, pass);
  3945. if (error)
  3946. goto bread_err2;
  3947. blk_no += bblks + hblks;
  3948. }
  3949. bread_err2:
  3950. xlog_put_bp(dbp);
  3951. bread_err1:
  3952. xlog_put_bp(hbp);
  3953. return error;
  3954. }
  3955. /*
  3956. * Do the recovery of the log. We actually do this in two phases.
  3957. * The two passes are necessary in order to implement the function
  3958. * of cancelling a record written into the log. The first pass
  3959. * determines those things which have been cancelled, and the
  3960. * second pass replays log items normally except for those which
  3961. * have been cancelled. The handling of the replay and cancellations
  3962. * takes place in the log item type specific routines.
  3963. *
  3964. * The table of items which have cancel records in the log is allocated
  3965. * and freed at this level, since only here do we know when all of
  3966. * the log recovery has been completed.
  3967. */
  3968. STATIC int
  3969. xlog_do_log_recovery(
  3970. struct xlog *log,
  3971. xfs_daddr_t head_blk,
  3972. xfs_daddr_t tail_blk)
  3973. {
  3974. int error, i;
  3975. ASSERT(head_blk != tail_blk);
  3976. /*
  3977. * First do a pass to find all of the cancelled buf log items.
  3978. * Store them in the buf_cancel_table for use in the second pass.
  3979. */
  3980. log->l_buf_cancel_table = kmem_zalloc(XLOG_BC_TABLE_SIZE *
  3981. sizeof(struct list_head),
  3982. KM_SLEEP);
  3983. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  3984. INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
  3985. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  3986. XLOG_RECOVER_PASS1);
  3987. if (error != 0) {
  3988. kmem_free(log->l_buf_cancel_table);
  3989. log->l_buf_cancel_table = NULL;
  3990. return error;
  3991. }
  3992. /*
  3993. * Then do a second pass to actually recover the items in the log.
  3994. * When it is complete free the table of buf cancel items.
  3995. */
  3996. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  3997. XLOG_RECOVER_PASS2);
  3998. #ifdef DEBUG
  3999. if (!error) {
  4000. int i;
  4001. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  4002. ASSERT(list_empty(&log->l_buf_cancel_table[i]));
  4003. }
  4004. #endif /* DEBUG */
  4005. kmem_free(log->l_buf_cancel_table);
  4006. log->l_buf_cancel_table = NULL;
  4007. return error;
  4008. }
  4009. /*
  4010. * Do the actual recovery
  4011. */
  4012. STATIC int
  4013. xlog_do_recover(
  4014. struct xlog *log,
  4015. xfs_daddr_t head_blk,
  4016. xfs_daddr_t tail_blk)
  4017. {
  4018. int error;
  4019. xfs_buf_t *bp;
  4020. xfs_sb_t *sbp;
  4021. /*
  4022. * First replay the images in the log.
  4023. */
  4024. error = xlog_do_log_recovery(log, head_blk, tail_blk);
  4025. if (error)
  4026. return error;
  4027. /*
  4028. * If IO errors happened during recovery, bail out.
  4029. */
  4030. if (XFS_FORCED_SHUTDOWN(log->l_mp)) {
  4031. return -EIO;
  4032. }
  4033. /*
  4034. * We now update the tail_lsn since much of the recovery has completed
  4035. * and there may be space available to use. If there were no extent
  4036. * or iunlinks, we can free up the entire log and set the tail_lsn to
  4037. * be the last_sync_lsn. This was set in xlog_find_tail to be the
  4038. * lsn of the last known good LR on disk. If there are extent frees
  4039. * or iunlinks they will have some entries in the AIL; so we look at
  4040. * the AIL to determine how to set the tail_lsn.
  4041. */
  4042. xlog_assign_tail_lsn(log->l_mp);
  4043. /*
  4044. * Now that we've finished replaying all buffer and inode
  4045. * updates, re-read in the superblock and reverify it.
  4046. */
  4047. bp = xfs_getsb(log->l_mp, 0);
  4048. XFS_BUF_UNDONE(bp);
  4049. ASSERT(!(XFS_BUF_ISWRITE(bp)));
  4050. XFS_BUF_READ(bp);
  4051. XFS_BUF_UNASYNC(bp);
  4052. bp->b_ops = &xfs_sb_buf_ops;
  4053. error = xfs_buf_submit_wait(bp);
  4054. if (error) {
  4055. if (!XFS_FORCED_SHUTDOWN(log->l_mp)) {
  4056. xfs_buf_ioerror_alert(bp, __func__);
  4057. ASSERT(0);
  4058. }
  4059. xfs_buf_relse(bp);
  4060. return error;
  4061. }
  4062. /* Convert superblock from on-disk format */
  4063. sbp = &log->l_mp->m_sb;
  4064. xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
  4065. ASSERT(sbp->sb_magicnum == XFS_SB_MAGIC);
  4066. ASSERT(xfs_sb_good_version(sbp));
  4067. xfs_reinit_percpu_counters(log->l_mp);
  4068. xfs_buf_relse(bp);
  4069. xlog_recover_check_summary(log);
  4070. /* Normal transactions can now occur */
  4071. log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
  4072. return 0;
  4073. }
  4074. /*
  4075. * Perform recovery and re-initialize some log variables in xlog_find_tail.
  4076. *
  4077. * Return error or zero.
  4078. */
  4079. int
  4080. xlog_recover(
  4081. struct xlog *log)
  4082. {
  4083. xfs_daddr_t head_blk, tail_blk;
  4084. int error;
  4085. /* find the tail of the log */
  4086. if ((error = xlog_find_tail(log, &head_blk, &tail_blk)))
  4087. return error;
  4088. if (tail_blk != head_blk) {
  4089. /* There used to be a comment here:
  4090. *
  4091. * disallow recovery on read-only mounts. note -- mount
  4092. * checks for ENOSPC and turns it into an intelligent
  4093. * error message.
  4094. * ...but this is no longer true. Now, unless you specify
  4095. * NORECOVERY (in which case this function would never be
  4096. * called), we just go ahead and recover. We do this all
  4097. * under the vfs layer, so we can get away with it unless
  4098. * the device itself is read-only, in which case we fail.
  4099. */
  4100. if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
  4101. return error;
  4102. }
  4103. /*
  4104. * Version 5 superblock log feature mask validation. We know the
  4105. * log is dirty so check if there are any unknown log features
  4106. * in what we need to recover. If there are unknown features
  4107. * (e.g. unsupported transactions, then simply reject the
  4108. * attempt at recovery before touching anything.
  4109. */
  4110. if (XFS_SB_VERSION_NUM(&log->l_mp->m_sb) == XFS_SB_VERSION_5 &&
  4111. xfs_sb_has_incompat_log_feature(&log->l_mp->m_sb,
  4112. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
  4113. xfs_warn(log->l_mp,
  4114. "Superblock has unknown incompatible log features (0x%x) enabled.\n"
  4115. "The log can not be fully and/or safely recovered by this kernel.\n"
  4116. "Please recover the log on a kernel that supports the unknown features.",
  4117. (log->l_mp->m_sb.sb_features_log_incompat &
  4118. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
  4119. return -EINVAL;
  4120. }
  4121. /*
  4122. * Delay log recovery if the debug hook is set. This is debug
  4123. * instrumention to coordinate simulation of I/O failures with
  4124. * log recovery.
  4125. */
  4126. if (xfs_globals.log_recovery_delay) {
  4127. xfs_notice(log->l_mp,
  4128. "Delaying log recovery for %d seconds.",
  4129. xfs_globals.log_recovery_delay);
  4130. msleep(xfs_globals.log_recovery_delay * 1000);
  4131. }
  4132. xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
  4133. log->l_mp->m_logname ? log->l_mp->m_logname
  4134. : "internal");
  4135. error = xlog_do_recover(log, head_blk, tail_blk);
  4136. log->l_flags |= XLOG_RECOVERY_NEEDED;
  4137. }
  4138. return error;
  4139. }
  4140. /*
  4141. * In the first part of recovery we replay inodes and buffers and build
  4142. * up the list of extent free items which need to be processed. Here
  4143. * we process the extent free items and clean up the on disk unlinked
  4144. * inode lists. This is separated from the first part of recovery so
  4145. * that the root and real-time bitmap inodes can be read in from disk in
  4146. * between the two stages. This is necessary so that we can free space
  4147. * in the real-time portion of the file system.
  4148. */
  4149. int
  4150. xlog_recover_finish(
  4151. struct xlog *log)
  4152. {
  4153. /*
  4154. * Now we're ready to do the transactions needed for the
  4155. * rest of recovery. Start with completing all the extent
  4156. * free intent records and then process the unlinked inode
  4157. * lists. At this point, we essentially run in normal mode
  4158. * except that we're still performing recovery actions
  4159. * rather than accepting new requests.
  4160. */
  4161. if (log->l_flags & XLOG_RECOVERY_NEEDED) {
  4162. int error;
  4163. error = xlog_recover_process_efis(log);
  4164. if (error) {
  4165. xfs_alert(log->l_mp, "Failed to recover EFIs");
  4166. return error;
  4167. }
  4168. /*
  4169. * Sync the log to get all the EFIs out of the AIL.
  4170. * This isn't absolutely necessary, but it helps in
  4171. * case the unlink transactions would have problems
  4172. * pushing the EFIs out of the way.
  4173. */
  4174. xfs_log_force(log->l_mp, XFS_LOG_SYNC);
  4175. xlog_recover_process_iunlinks(log);
  4176. xlog_recover_check_summary(log);
  4177. xfs_notice(log->l_mp, "Ending recovery (logdev: %s)",
  4178. log->l_mp->m_logname ? log->l_mp->m_logname
  4179. : "internal");
  4180. log->l_flags &= ~XLOG_RECOVERY_NEEDED;
  4181. } else {
  4182. xfs_info(log->l_mp, "Ending clean mount");
  4183. }
  4184. return 0;
  4185. }
  4186. #if defined(DEBUG)
  4187. /*
  4188. * Read all of the agf and agi counters and check that they
  4189. * are consistent with the superblock counters.
  4190. */
  4191. void
  4192. xlog_recover_check_summary(
  4193. struct xlog *log)
  4194. {
  4195. xfs_mount_t *mp;
  4196. xfs_agf_t *agfp;
  4197. xfs_buf_t *agfbp;
  4198. xfs_buf_t *agibp;
  4199. xfs_agnumber_t agno;
  4200. __uint64_t freeblks;
  4201. __uint64_t itotal;
  4202. __uint64_t ifree;
  4203. int error;
  4204. mp = log->l_mp;
  4205. freeblks = 0LL;
  4206. itotal = 0LL;
  4207. ifree = 0LL;
  4208. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  4209. error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
  4210. if (error) {
  4211. xfs_alert(mp, "%s agf read failed agno %d error %d",
  4212. __func__, agno, error);
  4213. } else {
  4214. agfp = XFS_BUF_TO_AGF(agfbp);
  4215. freeblks += be32_to_cpu(agfp->agf_freeblks) +
  4216. be32_to_cpu(agfp->agf_flcount);
  4217. xfs_buf_relse(agfbp);
  4218. }
  4219. error = xfs_read_agi(mp, NULL, agno, &agibp);
  4220. if (error) {
  4221. xfs_alert(mp, "%s agi read failed agno %d error %d",
  4222. __func__, agno, error);
  4223. } else {
  4224. struct xfs_agi *agi = XFS_BUF_TO_AGI(agibp);
  4225. itotal += be32_to_cpu(agi->agi_count);
  4226. ifree += be32_to_cpu(agi->agi_freecount);
  4227. xfs_buf_relse(agibp);
  4228. }
  4229. }
  4230. }
  4231. #endif /* DEBUG */