xfs_log_recover.c 108 KB

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