xfs_log_recover.c 118 KB

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