xfs_log_recover.c 105 KB

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