xfs_log_recover.c 107 KB

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