xfs_log_recover.c 111 KB

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