xfs_log_recover.c 127 KB

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