xfs_log_recover.c 112 KB

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