xfs_log_recover.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_trans.h"
  18. #include "xfs_log.h"
  19. #include "xfs_log_priv.h"
  20. #include "xfs_log_recover.h"
  21. #include "xfs_trans_priv.h"
  22. #include "xfs_alloc.h"
  23. #include "xfs_ialloc.h"
  24. #include "xfs_trace.h"
  25. #include "xfs_icache.h"
  26. #include "xfs_error.h"
  27. #include "xfs_buf_item.h"
  28. #define BLK_AVG(blk1, blk2) ((blk1+blk2) >> 1)
  29. STATIC int
  30. xlog_find_zeroed(
  31. struct xlog *,
  32. xfs_daddr_t *);
  33. STATIC int
  34. xlog_clear_stale_blocks(
  35. struct xlog *,
  36. xfs_lsn_t);
  37. #if defined(DEBUG)
  38. STATIC void
  39. xlog_recover_check_summary(
  40. struct xlog *);
  41. #else
  42. #define xlog_recover_check_summary(log)
  43. #endif
  44. STATIC int
  45. xlog_do_recovery_pass(
  46. struct xlog *, xfs_daddr_t, xfs_daddr_t, int, xfs_daddr_t *);
  47. /*
  48. * Sector aligned buffer routines for buffer create/read/write/access
  49. */
  50. /*
  51. * Verify the log-relative block number and length in basic blocks are valid for
  52. * an operation involving the given XFS log buffer. Returns true if the fields
  53. * are valid, false otherwise.
  54. */
  55. static inline bool
  56. xlog_verify_bno(
  57. struct xlog *log,
  58. xfs_daddr_t blk_no,
  59. int bbcount)
  60. {
  61. if (blk_no < 0 || blk_no >= log->l_logBBsize)
  62. return false;
  63. if (bbcount <= 0 || (blk_no + bbcount) > log->l_logBBsize)
  64. return false;
  65. return true;
  66. }
  67. /*
  68. * Allocate a buffer to hold log data. The buffer needs to be able to map to
  69. * a range of nbblks basic blocks at any valid offset within the log.
  70. */
  71. static char *
  72. xlog_alloc_buffer(
  73. struct xlog *log,
  74. int nbblks)
  75. {
  76. int align_mask = xfs_buftarg_dma_alignment(log->l_targ);
  77. /*
  78. * Pass log block 0 since we don't have an addr yet, buffer will be
  79. * verified on read.
  80. */
  81. if (XFS_IS_CORRUPT(log->l_mp, !xlog_verify_bno(log, 0, nbblks))) {
  82. xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
  83. nbblks);
  84. return NULL;
  85. }
  86. /*
  87. * We do log I/O in units of log sectors (a power-of-2 multiple of the
  88. * basic block size), so we round up the requested size to accommodate
  89. * the basic blocks required for complete log sectors.
  90. *
  91. * In addition, the buffer may be used for a non-sector-aligned block
  92. * offset, in which case an I/O of the requested size could extend
  93. * beyond the end of the buffer. If the requested size is only 1 basic
  94. * block it will never straddle a sector boundary, so this won't be an
  95. * issue. Nor will this be a problem if the log I/O is done in basic
  96. * blocks (sector size 1). But otherwise we extend the buffer by one
  97. * extra log sector to ensure there's space to accommodate this
  98. * possibility.
  99. */
  100. if (nbblks > 1 && log->l_sectBBsize > 1)
  101. nbblks += log->l_sectBBsize;
  102. nbblks = round_up(nbblks, log->l_sectBBsize);
  103. return kmem_alloc_io(BBTOB(nbblks), align_mask, KM_MAYFAIL | KM_ZERO);
  104. }
  105. /*
  106. * Return the address of the start of the given block number's data
  107. * in a log buffer. The buffer covers a log sector-aligned region.
  108. */
  109. static inline unsigned int
  110. xlog_align(
  111. struct xlog *log,
  112. xfs_daddr_t blk_no)
  113. {
  114. return BBTOB(blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1));
  115. }
  116. static int
  117. xlog_do_io(
  118. struct xlog *log,
  119. xfs_daddr_t blk_no,
  120. unsigned int nbblks,
  121. char *data,
  122. unsigned int op)
  123. {
  124. int error;
  125. if (XFS_IS_CORRUPT(log->l_mp, !xlog_verify_bno(log, blk_no, nbblks))) {
  126. xfs_warn(log->l_mp,
  127. "Invalid log block/length (0x%llx, 0x%x) for buffer",
  128. blk_no, nbblks);
  129. return -EFSCORRUPTED;
  130. }
  131. blk_no = round_down(blk_no, log->l_sectBBsize);
  132. nbblks = round_up(nbblks, log->l_sectBBsize);
  133. ASSERT(nbblks > 0);
  134. error = xfs_rw_bdev(log->l_targ->bt_bdev, log->l_logBBstart + blk_no,
  135. BBTOB(nbblks), data, op);
  136. if (error && !XFS_FORCED_SHUTDOWN(log->l_mp)) {
  137. xfs_alert(log->l_mp,
  138. "log recovery %s I/O error at daddr 0x%llx len %d error %d",
  139. op == REQ_OP_WRITE ? "write" : "read",
  140. blk_no, nbblks, error);
  141. }
  142. return error;
  143. }
  144. STATIC int
  145. xlog_bread_noalign(
  146. struct xlog *log,
  147. xfs_daddr_t blk_no,
  148. int nbblks,
  149. char *data)
  150. {
  151. return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
  152. }
  153. STATIC int
  154. xlog_bread(
  155. struct xlog *log,
  156. xfs_daddr_t blk_no,
  157. int nbblks,
  158. char *data,
  159. char **offset)
  160. {
  161. int error;
  162. error = xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
  163. if (!error)
  164. *offset = data + xlog_align(log, blk_no);
  165. return error;
  166. }
  167. STATIC int
  168. xlog_bwrite(
  169. struct xlog *log,
  170. xfs_daddr_t blk_no,
  171. int nbblks,
  172. char *data)
  173. {
  174. return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_WRITE);
  175. }
  176. #ifdef DEBUG
  177. /*
  178. * dump debug superblock and log record information
  179. */
  180. STATIC void
  181. xlog_header_check_dump(
  182. xfs_mount_t *mp,
  183. xlog_rec_header_t *head)
  184. {
  185. xfs_debug(mp, "%s: SB : uuid = %pU, fmt = %d",
  186. __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
  187. xfs_debug(mp, " log : uuid = %pU, fmt = %d",
  188. &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
  189. }
  190. #else
  191. #define xlog_header_check_dump(mp, head)
  192. #endif
  193. /*
  194. * check log record header for recovery
  195. */
  196. STATIC int
  197. xlog_header_check_recover(
  198. xfs_mount_t *mp,
  199. xlog_rec_header_t *head)
  200. {
  201. ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
  202. /*
  203. * IRIX doesn't write the h_fmt field and leaves it zeroed
  204. * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
  205. * a dirty log created in IRIX.
  206. */
  207. if (XFS_IS_CORRUPT(mp, head->h_fmt != cpu_to_be32(XLOG_FMT))) {
  208. xfs_warn(mp,
  209. "dirty log written in incompatible format - can't recover");
  210. xlog_header_check_dump(mp, head);
  211. return -EFSCORRUPTED;
  212. }
  213. if (XFS_IS_CORRUPT(mp, !uuid_equal(&mp->m_sb.sb_uuid,
  214. &head->h_fs_uuid))) {
  215. xfs_warn(mp,
  216. "dirty log entry has mismatched uuid - can't recover");
  217. xlog_header_check_dump(mp, head);
  218. return -EFSCORRUPTED;
  219. }
  220. return 0;
  221. }
  222. /*
  223. * read the head block of the log and check the header
  224. */
  225. STATIC int
  226. xlog_header_check_mount(
  227. xfs_mount_t *mp,
  228. xlog_rec_header_t *head)
  229. {
  230. ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
  231. if (uuid_is_null(&head->h_fs_uuid)) {
  232. /*
  233. * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
  234. * h_fs_uuid is null, we assume this log was last mounted
  235. * by IRIX and continue.
  236. */
  237. xfs_warn(mp, "null uuid in log - IRIX style log");
  238. } else if (XFS_IS_CORRUPT(mp, !uuid_equal(&mp->m_sb.sb_uuid,
  239. &head->h_fs_uuid))) {
  240. xfs_warn(mp, "log has mismatched uuid - can't recover");
  241. xlog_header_check_dump(mp, head);
  242. return -EFSCORRUPTED;
  243. }
  244. return 0;
  245. }
  246. /*
  247. * This routine finds (to an approximation) the first block in the physical
  248. * log which contains the given cycle. It uses a binary search algorithm.
  249. * Note that the algorithm can not be perfect because the disk will not
  250. * necessarily be perfect.
  251. */
  252. STATIC int
  253. xlog_find_cycle_start(
  254. struct xlog *log,
  255. char *buffer,
  256. xfs_daddr_t first_blk,
  257. xfs_daddr_t *last_blk,
  258. uint cycle)
  259. {
  260. char *offset;
  261. xfs_daddr_t mid_blk;
  262. xfs_daddr_t end_blk;
  263. uint mid_cycle;
  264. int error;
  265. end_blk = *last_blk;
  266. mid_blk = BLK_AVG(first_blk, end_blk);
  267. while (mid_blk != first_blk && mid_blk != end_blk) {
  268. error = xlog_bread(log, mid_blk, 1, buffer, &offset);
  269. if (error)
  270. return error;
  271. mid_cycle = xlog_get_cycle(offset);
  272. if (mid_cycle == cycle)
  273. end_blk = mid_blk; /* last_half_cycle == mid_cycle */
  274. else
  275. first_blk = mid_blk; /* first_half_cycle == mid_cycle */
  276. mid_blk = BLK_AVG(first_blk, end_blk);
  277. }
  278. ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
  279. (mid_blk == end_blk && mid_blk-1 == first_blk));
  280. *last_blk = end_blk;
  281. return 0;
  282. }
  283. /*
  284. * Check that a range of blocks does not contain stop_on_cycle_no.
  285. * Fill in *new_blk with the block offset where such a block is
  286. * found, or with -1 (an invalid block number) if there is no such
  287. * block in the range. The scan needs to occur from front to back
  288. * and the pointer into the region must be updated since a later
  289. * routine will need to perform another test.
  290. */
  291. STATIC int
  292. xlog_find_verify_cycle(
  293. struct xlog *log,
  294. xfs_daddr_t start_blk,
  295. int nbblks,
  296. uint stop_on_cycle_no,
  297. xfs_daddr_t *new_blk)
  298. {
  299. xfs_daddr_t i, j;
  300. uint cycle;
  301. char *buffer;
  302. xfs_daddr_t bufblks;
  303. char *buf = NULL;
  304. int error = 0;
  305. /*
  306. * Greedily allocate a buffer big enough to handle the full
  307. * range of basic blocks we'll be examining. If that fails,
  308. * try a smaller size. We need to be able to read at least
  309. * a log sector, or we're out of luck.
  310. */
  311. bufblks = 1 << ffs(nbblks);
  312. while (bufblks > log->l_logBBsize)
  313. bufblks >>= 1;
  314. while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
  315. bufblks >>= 1;
  316. if (bufblks < log->l_sectBBsize)
  317. return -ENOMEM;
  318. }
  319. for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
  320. int bcount;
  321. bcount = min(bufblks, (start_blk + nbblks - i));
  322. error = xlog_bread(log, i, bcount, buffer, &buf);
  323. if (error)
  324. goto out;
  325. for (j = 0; j < bcount; j++) {
  326. cycle = xlog_get_cycle(buf);
  327. if (cycle == stop_on_cycle_no) {
  328. *new_blk = i+j;
  329. goto out;
  330. }
  331. buf += BBSIZE;
  332. }
  333. }
  334. *new_blk = -1;
  335. out:
  336. kmem_free(buffer);
  337. return error;
  338. }
  339. static inline int
  340. xlog_logrec_hblks(struct xlog *log, struct xlog_rec_header *rh)
  341. {
  342. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  343. int h_size = be32_to_cpu(rh->h_size);
  344. if ((be32_to_cpu(rh->h_version) & XLOG_VERSION_2) &&
  345. h_size > XLOG_HEADER_CYCLE_SIZE)
  346. return DIV_ROUND_UP(h_size, XLOG_HEADER_CYCLE_SIZE);
  347. }
  348. return 1;
  349. }
  350. /*
  351. * Potentially backup over partial log record write.
  352. *
  353. * In the typical case, last_blk is the number of the block directly after
  354. * a good log record. Therefore, we subtract one to get the block number
  355. * of the last block in the given buffer. extra_bblks contains the number
  356. * of blocks we would have read on a previous read. This happens when the
  357. * last log record is split over the end of the physical log.
  358. *
  359. * extra_bblks is the number of blocks potentially verified on a previous
  360. * call to this routine.
  361. */
  362. STATIC int
  363. xlog_find_verify_log_record(
  364. struct xlog *log,
  365. xfs_daddr_t start_blk,
  366. xfs_daddr_t *last_blk,
  367. int extra_bblks)
  368. {
  369. xfs_daddr_t i;
  370. char *buffer;
  371. char *offset = NULL;
  372. xlog_rec_header_t *head = NULL;
  373. int error = 0;
  374. int smallmem = 0;
  375. int num_blks = *last_blk - start_blk;
  376. int xhdrs;
  377. ASSERT(start_blk != 0 || *last_blk != start_blk);
  378. buffer = xlog_alloc_buffer(log, num_blks);
  379. if (!buffer) {
  380. buffer = xlog_alloc_buffer(log, 1);
  381. if (!buffer)
  382. return -ENOMEM;
  383. smallmem = 1;
  384. } else {
  385. error = xlog_bread(log, start_blk, num_blks, buffer, &offset);
  386. if (error)
  387. goto out;
  388. offset += ((num_blks - 1) << BBSHIFT);
  389. }
  390. for (i = (*last_blk) - 1; i >= 0; i--) {
  391. if (i < start_blk) {
  392. /* valid log record not found */
  393. xfs_warn(log->l_mp,
  394. "Log inconsistent (didn't find previous header)");
  395. ASSERT(0);
  396. error = -EFSCORRUPTED;
  397. goto out;
  398. }
  399. if (smallmem) {
  400. error = xlog_bread(log, i, 1, buffer, &offset);
  401. if (error)
  402. goto out;
  403. }
  404. head = (xlog_rec_header_t *)offset;
  405. if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
  406. break;
  407. if (!smallmem)
  408. offset -= BBSIZE;
  409. }
  410. /*
  411. * We hit the beginning of the physical log & still no header. Return
  412. * to caller. If caller can handle a return of -1, then this routine
  413. * will be called again for the end of the physical log.
  414. */
  415. if (i == -1) {
  416. error = 1;
  417. goto out;
  418. }
  419. /*
  420. * We have the final block of the good log (the first block
  421. * of the log record _before_ the head. So we check the uuid.
  422. */
  423. if ((error = xlog_header_check_mount(log->l_mp, head)))
  424. goto out;
  425. /*
  426. * We may have found a log record header before we expected one.
  427. * last_blk will be the 1st block # with a given cycle #. We may end
  428. * up reading an entire log record. In this case, we don't want to
  429. * reset last_blk. Only when last_blk points in the middle of a log
  430. * record do we update last_blk.
  431. */
  432. xhdrs = xlog_logrec_hblks(log, head);
  433. if (*last_blk - i + extra_bblks !=
  434. BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
  435. *last_blk = i;
  436. out:
  437. kmem_free(buffer);
  438. return error;
  439. }
  440. /*
  441. * Head is defined to be the point of the log where the next log write
  442. * could go. This means that incomplete LR writes at the end are
  443. * eliminated when calculating the head. We aren't guaranteed that previous
  444. * LR have complete transactions. We only know that a cycle number of
  445. * current cycle number -1 won't be present in the log if we start writing
  446. * from our current block number.
  447. *
  448. * last_blk contains the block number of the first block with a given
  449. * cycle number.
  450. *
  451. * Return: zero if normal, non-zero if error.
  452. */
  453. STATIC int
  454. xlog_find_head(
  455. struct xlog *log,
  456. xfs_daddr_t *return_head_blk)
  457. {
  458. char *buffer;
  459. char *offset;
  460. xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
  461. int num_scan_bblks;
  462. uint first_half_cycle, last_half_cycle;
  463. uint stop_on_cycle;
  464. int error, log_bbnum = log->l_logBBsize;
  465. /* Is the end of the log device zeroed? */
  466. error = xlog_find_zeroed(log, &first_blk);
  467. if (error < 0) {
  468. xfs_warn(log->l_mp, "empty log check failed");
  469. return error;
  470. }
  471. if (error == 1) {
  472. *return_head_blk = first_blk;
  473. /* Is the whole lot zeroed? */
  474. if (!first_blk) {
  475. /* Linux XFS shouldn't generate totally zeroed logs -
  476. * mkfs etc write a dummy unmount record to a fresh
  477. * log so we can store the uuid in there
  478. */
  479. xfs_warn(log->l_mp, "totally zeroed log");
  480. }
  481. return 0;
  482. }
  483. first_blk = 0; /* get cycle # of 1st block */
  484. buffer = xlog_alloc_buffer(log, 1);
  485. if (!buffer)
  486. return -ENOMEM;
  487. error = xlog_bread(log, 0, 1, buffer, &offset);
  488. if (error)
  489. goto out_free_buffer;
  490. first_half_cycle = xlog_get_cycle(offset);
  491. last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
  492. error = xlog_bread(log, last_blk, 1, buffer, &offset);
  493. if (error)
  494. goto out_free_buffer;
  495. last_half_cycle = xlog_get_cycle(offset);
  496. ASSERT(last_half_cycle != 0);
  497. /*
  498. * If the 1st half cycle number is equal to the last half cycle number,
  499. * then the entire log is stamped with the same cycle number. In this
  500. * case, head_blk can't be set to zero (which makes sense). The below
  501. * math doesn't work out properly with head_blk equal to zero. Instead,
  502. * we set it to log_bbnum which is an invalid block number, but this
  503. * value makes the math correct. If head_blk doesn't changed through
  504. * all the tests below, *head_blk is set to zero at the very end rather
  505. * than log_bbnum. In a sense, log_bbnum and zero are the same block
  506. * in a circular file.
  507. */
  508. if (first_half_cycle == last_half_cycle) {
  509. /*
  510. * In this case we believe that the entire log should have
  511. * cycle number last_half_cycle. We need to scan backwards
  512. * from the end verifying that there are no holes still
  513. * containing last_half_cycle - 1. If we find such a hole,
  514. * then the start of that hole will be the new head. The
  515. * simple case looks like
  516. * x | x ... | x - 1 | x
  517. * Another case that fits this picture would be
  518. * x | x + 1 | x ... | x
  519. * In this case the head really is somewhere at the end of the
  520. * log, as one of the latest writes at the beginning was
  521. * incomplete.
  522. * One more case is
  523. * x | x + 1 | x ... | x - 1 | x
  524. * This is really the combination of the above two cases, and
  525. * the head has to end up at the start of the x-1 hole at the
  526. * end of the log.
  527. *
  528. * In the 256k log case, we will read from the beginning to the
  529. * end of the log and search for cycle numbers equal to x-1.
  530. * We don't worry about the x+1 blocks that we encounter,
  531. * because we know that they cannot be the head since the log
  532. * started with x.
  533. */
  534. head_blk = log_bbnum;
  535. stop_on_cycle = last_half_cycle - 1;
  536. } else {
  537. /*
  538. * In this case we want to find the first block with cycle
  539. * number matching last_half_cycle. We expect the log to be
  540. * some variation on
  541. * x + 1 ... | x ... | x
  542. * The first block with cycle number x (last_half_cycle) will
  543. * be where the new head belongs. First we do a binary search
  544. * for the first occurrence of last_half_cycle. The binary
  545. * search may not be totally accurate, so then we scan back
  546. * from there looking for occurrences of last_half_cycle before
  547. * us. If that backwards scan wraps around the beginning of
  548. * the log, then we look for occurrences of last_half_cycle - 1
  549. * at the end of the log. The cases we're looking for look
  550. * like
  551. * v binary search stopped here
  552. * x + 1 ... | x | x + 1 | x ... | x
  553. * ^ but we want to locate this spot
  554. * or
  555. * <---------> less than scan distance
  556. * x + 1 ... | x ... | x - 1 | x
  557. * ^ we want to locate this spot
  558. */
  559. stop_on_cycle = last_half_cycle;
  560. error = xlog_find_cycle_start(log, buffer, first_blk, &head_blk,
  561. last_half_cycle);
  562. if (error)
  563. goto out_free_buffer;
  564. }
  565. /*
  566. * Now validate the answer. Scan back some number of maximum possible
  567. * blocks and make sure each one has the expected cycle number. The
  568. * maximum is determined by the total possible amount of buffering
  569. * in the in-core log. The following number can be made tighter if
  570. * we actually look at the block size of the filesystem.
  571. */
  572. num_scan_bblks = min_t(int, log_bbnum, XLOG_TOTAL_REC_SHIFT(log));
  573. if (head_blk >= num_scan_bblks) {
  574. /*
  575. * We are guaranteed that the entire check can be performed
  576. * in one buffer.
  577. */
  578. start_blk = head_blk - num_scan_bblks;
  579. if ((error = xlog_find_verify_cycle(log,
  580. start_blk, num_scan_bblks,
  581. stop_on_cycle, &new_blk)))
  582. goto out_free_buffer;
  583. if (new_blk != -1)
  584. head_blk = new_blk;
  585. } else { /* need to read 2 parts of log */
  586. /*
  587. * We are going to scan backwards in the log in two parts.
  588. * First we scan the physical end of the log. In this part
  589. * of the log, we are looking for blocks with cycle number
  590. * last_half_cycle - 1.
  591. * If we find one, then we know that the log starts there, as
  592. * we've found a hole that didn't get written in going around
  593. * the end of the physical log. The simple case for this is
  594. * x + 1 ... | x ... | x - 1 | x
  595. * <---------> less than scan distance
  596. * If all of the blocks at the end of the log have cycle number
  597. * last_half_cycle, then we check the blocks at the start of
  598. * the log looking for occurrences of last_half_cycle. If we
  599. * find one, then our current estimate for the location of the
  600. * first occurrence of last_half_cycle is wrong and we move
  601. * back to the hole we've found. This case looks like
  602. * x + 1 ... | x | x + 1 | x ...
  603. * ^ binary search stopped here
  604. * Another case we need to handle that only occurs in 256k
  605. * logs is
  606. * x + 1 ... | x ... | x+1 | x ...
  607. * ^ binary search stops here
  608. * In a 256k log, the scan at the end of the log will see the
  609. * x + 1 blocks. We need to skip past those since that is
  610. * certainly not the head of the log. By searching for
  611. * last_half_cycle-1 we accomplish that.
  612. */
  613. ASSERT(head_blk <= INT_MAX &&
  614. (xfs_daddr_t) num_scan_bblks >= head_blk);
  615. start_blk = log_bbnum - (num_scan_bblks - head_blk);
  616. if ((error = xlog_find_verify_cycle(log, start_blk,
  617. num_scan_bblks - (int)head_blk,
  618. (stop_on_cycle - 1), &new_blk)))
  619. goto out_free_buffer;
  620. if (new_blk != -1) {
  621. head_blk = new_blk;
  622. goto validate_head;
  623. }
  624. /*
  625. * Scan beginning of log now. The last part of the physical
  626. * log is good. This scan needs to verify that it doesn't find
  627. * the last_half_cycle.
  628. */
  629. start_blk = 0;
  630. ASSERT(head_blk <= INT_MAX);
  631. if ((error = xlog_find_verify_cycle(log,
  632. start_blk, (int)head_blk,
  633. stop_on_cycle, &new_blk)))
  634. goto out_free_buffer;
  635. if (new_blk != -1)
  636. head_blk = new_blk;
  637. }
  638. validate_head:
  639. /*
  640. * Now we need to make sure head_blk is not pointing to a block in
  641. * the middle of a log record.
  642. */
  643. num_scan_bblks = XLOG_REC_SHIFT(log);
  644. if (head_blk >= num_scan_bblks) {
  645. start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
  646. /* start ptr at last block ptr before head_blk */
  647. error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  648. if (error == 1)
  649. error = -EIO;
  650. if (error)
  651. goto out_free_buffer;
  652. } else {
  653. start_blk = 0;
  654. ASSERT(head_blk <= INT_MAX);
  655. error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  656. if (error < 0)
  657. goto out_free_buffer;
  658. if (error == 1) {
  659. /* We hit the beginning of the log during our search */
  660. start_blk = log_bbnum - (num_scan_bblks - head_blk);
  661. new_blk = log_bbnum;
  662. ASSERT(start_blk <= INT_MAX &&
  663. (xfs_daddr_t) log_bbnum-start_blk >= 0);
  664. ASSERT(head_blk <= INT_MAX);
  665. error = xlog_find_verify_log_record(log, start_blk,
  666. &new_blk, (int)head_blk);
  667. if (error == 1)
  668. error = -EIO;
  669. if (error)
  670. goto out_free_buffer;
  671. if (new_blk != log_bbnum)
  672. head_blk = new_blk;
  673. } else if (error)
  674. goto out_free_buffer;
  675. }
  676. kmem_free(buffer);
  677. if (head_blk == log_bbnum)
  678. *return_head_blk = 0;
  679. else
  680. *return_head_blk = head_blk;
  681. /*
  682. * When returning here, we have a good block number. Bad block
  683. * means that during a previous crash, we didn't have a clean break
  684. * from cycle number N to cycle number N-1. In this case, we need
  685. * to find the first block with cycle number N-1.
  686. */
  687. return 0;
  688. out_free_buffer:
  689. kmem_free(buffer);
  690. if (error)
  691. xfs_warn(log->l_mp, "failed to find log head");
  692. return error;
  693. }
  694. /*
  695. * Seek backwards in the log for log record headers.
  696. *
  697. * Given a starting log block, walk backwards until we find the provided number
  698. * of records or hit the provided tail block. The return value is the number of
  699. * records encountered or a negative error code. The log block and buffer
  700. * pointer of the last record seen are returned in rblk and rhead respectively.
  701. */
  702. STATIC int
  703. xlog_rseek_logrec_hdr(
  704. struct xlog *log,
  705. xfs_daddr_t head_blk,
  706. xfs_daddr_t tail_blk,
  707. int count,
  708. char *buffer,
  709. xfs_daddr_t *rblk,
  710. struct xlog_rec_header **rhead,
  711. bool *wrapped)
  712. {
  713. int i;
  714. int error;
  715. int found = 0;
  716. char *offset = NULL;
  717. xfs_daddr_t end_blk;
  718. *wrapped = false;
  719. /*
  720. * Walk backwards from the head block until we hit the tail or the first
  721. * block in the log.
  722. */
  723. end_blk = head_blk > tail_blk ? tail_blk : 0;
  724. for (i = (int) head_blk - 1; i >= end_blk; i--) {
  725. error = xlog_bread(log, i, 1, buffer, &offset);
  726. if (error)
  727. goto out_error;
  728. if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  729. *rblk = i;
  730. *rhead = (struct xlog_rec_header *) offset;
  731. if (++found == count)
  732. break;
  733. }
  734. }
  735. /*
  736. * If we haven't hit the tail block or the log record header count,
  737. * start looking again from the end of the physical log. Note that
  738. * callers can pass head == tail if the tail is not yet known.
  739. */
  740. if (tail_blk >= head_blk && found != count) {
  741. for (i = log->l_logBBsize - 1; i >= (int) tail_blk; i--) {
  742. error = xlog_bread(log, i, 1, buffer, &offset);
  743. if (error)
  744. goto out_error;
  745. if (*(__be32 *)offset ==
  746. cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  747. *wrapped = true;
  748. *rblk = i;
  749. *rhead = (struct xlog_rec_header *) offset;
  750. if (++found == count)
  751. break;
  752. }
  753. }
  754. }
  755. return found;
  756. out_error:
  757. return error;
  758. }
  759. /*
  760. * Seek forward in the log for log record headers.
  761. *
  762. * Given head and tail blocks, walk forward from the tail block until we find
  763. * the provided number of records or hit the head block. The return value is the
  764. * number of records encountered or a negative error code. The log block and
  765. * buffer pointer of the last record seen are returned in rblk and rhead
  766. * respectively.
  767. */
  768. STATIC int
  769. xlog_seek_logrec_hdr(
  770. struct xlog *log,
  771. xfs_daddr_t head_blk,
  772. xfs_daddr_t tail_blk,
  773. int count,
  774. char *buffer,
  775. xfs_daddr_t *rblk,
  776. struct xlog_rec_header **rhead,
  777. bool *wrapped)
  778. {
  779. int i;
  780. int error;
  781. int found = 0;
  782. char *offset = NULL;
  783. xfs_daddr_t end_blk;
  784. *wrapped = false;
  785. /*
  786. * Walk forward from the tail block until we hit the head or the last
  787. * block in the log.
  788. */
  789. end_blk = head_blk > tail_blk ? head_blk : log->l_logBBsize - 1;
  790. for (i = (int) tail_blk; i <= end_blk; i++) {
  791. error = xlog_bread(log, i, 1, buffer, &offset);
  792. if (error)
  793. goto out_error;
  794. if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  795. *rblk = i;
  796. *rhead = (struct xlog_rec_header *) offset;
  797. if (++found == count)
  798. break;
  799. }
  800. }
  801. /*
  802. * If we haven't hit the head block or the log record header count,
  803. * start looking again from the start of the physical log.
  804. */
  805. if (tail_blk > head_blk && found != count) {
  806. for (i = 0; i < (int) head_blk; i++) {
  807. error = xlog_bread(log, i, 1, buffer, &offset);
  808. if (error)
  809. goto out_error;
  810. if (*(__be32 *)offset ==
  811. cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  812. *wrapped = true;
  813. *rblk = i;
  814. *rhead = (struct xlog_rec_header *) offset;
  815. if (++found == count)
  816. break;
  817. }
  818. }
  819. }
  820. return found;
  821. out_error:
  822. return error;
  823. }
  824. /*
  825. * Calculate distance from head to tail (i.e., unused space in the log).
  826. */
  827. static inline int
  828. xlog_tail_distance(
  829. struct xlog *log,
  830. xfs_daddr_t head_blk,
  831. xfs_daddr_t tail_blk)
  832. {
  833. if (head_blk < tail_blk)
  834. return tail_blk - head_blk;
  835. return tail_blk + (log->l_logBBsize - head_blk);
  836. }
  837. /*
  838. * Verify the log tail. This is particularly important when torn or incomplete
  839. * writes have been detected near the front of the log and the head has been
  840. * walked back accordingly.
  841. *
  842. * We also have to handle the case where the tail was pinned and the head
  843. * blocked behind the tail right before a crash. If the tail had been pushed
  844. * immediately prior to the crash and the subsequent checkpoint was only
  845. * partially written, it's possible it overwrote the last referenced tail in the
  846. * log with garbage. This is not a coherency problem because the tail must have
  847. * been pushed before it can be overwritten, but appears as log corruption to
  848. * recovery because we have no way to know the tail was updated if the
  849. * subsequent checkpoint didn't write successfully.
  850. *
  851. * Therefore, CRC check the log from tail to head. If a failure occurs and the
  852. * offending record is within max iclog bufs from the head, walk the tail
  853. * forward and retry until a valid tail is found or corruption is detected out
  854. * of the range of a possible overwrite.
  855. */
  856. STATIC int
  857. xlog_verify_tail(
  858. struct xlog *log,
  859. xfs_daddr_t head_blk,
  860. xfs_daddr_t *tail_blk,
  861. int hsize)
  862. {
  863. struct xlog_rec_header *thead;
  864. char *buffer;
  865. xfs_daddr_t first_bad;
  866. int error = 0;
  867. bool wrapped;
  868. xfs_daddr_t tmp_tail;
  869. xfs_daddr_t orig_tail = *tail_blk;
  870. buffer = xlog_alloc_buffer(log, 1);
  871. if (!buffer)
  872. return -ENOMEM;
  873. /*
  874. * Make sure the tail points to a record (returns positive count on
  875. * success).
  876. */
  877. error = xlog_seek_logrec_hdr(log, head_blk, *tail_blk, 1, buffer,
  878. &tmp_tail, &thead, &wrapped);
  879. if (error < 0)
  880. goto out;
  881. if (*tail_blk != tmp_tail)
  882. *tail_blk = tmp_tail;
  883. /*
  884. * Run a CRC check from the tail to the head. We can't just check
  885. * MAX_ICLOGS records past the tail because the tail may point to stale
  886. * blocks cleared during the search for the head/tail. These blocks are
  887. * overwritten with zero-length records and thus record count is not a
  888. * reliable indicator of the iclog state before a crash.
  889. */
  890. first_bad = 0;
  891. error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
  892. XLOG_RECOVER_CRCPASS, &first_bad);
  893. while ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
  894. int tail_distance;
  895. /*
  896. * Is corruption within range of the head? If so, retry from
  897. * the next record. Otherwise return an error.
  898. */
  899. tail_distance = xlog_tail_distance(log, head_blk, first_bad);
  900. if (tail_distance > BTOBB(XLOG_MAX_ICLOGS * hsize))
  901. break;
  902. /* skip to the next record; returns positive count on success */
  903. error = xlog_seek_logrec_hdr(log, head_blk, first_bad, 2,
  904. buffer, &tmp_tail, &thead, &wrapped);
  905. if (error < 0)
  906. goto out;
  907. *tail_blk = tmp_tail;
  908. first_bad = 0;
  909. error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
  910. XLOG_RECOVER_CRCPASS, &first_bad);
  911. }
  912. if (!error && *tail_blk != orig_tail)
  913. xfs_warn(log->l_mp,
  914. "Tail block (0x%llx) overwrite detected. Updated to 0x%llx",
  915. orig_tail, *tail_blk);
  916. out:
  917. kmem_free(buffer);
  918. return error;
  919. }
  920. /*
  921. * Detect and trim torn writes from the head of the log.
  922. *
  923. * Storage without sector atomicity guarantees can result in torn writes in the
  924. * log in the event of a crash. Our only means to detect this scenario is via
  925. * CRC verification. While we can't always be certain that CRC verification
  926. * failure is due to a torn write vs. an unrelated corruption, we do know that
  927. * only a certain number (XLOG_MAX_ICLOGS) of log records can be written out at
  928. * one time. Therefore, CRC verify up to XLOG_MAX_ICLOGS records at the head of
  929. * the log and treat failures in this range as torn writes as a matter of
  930. * policy. In the event of CRC failure, the head is walked back to the last good
  931. * record in the log and the tail is updated from that record and verified.
  932. */
  933. STATIC int
  934. xlog_verify_head(
  935. struct xlog *log,
  936. xfs_daddr_t *head_blk, /* in/out: unverified head */
  937. xfs_daddr_t *tail_blk, /* out: tail block */
  938. char *buffer,
  939. xfs_daddr_t *rhead_blk, /* start blk of last record */
  940. struct xlog_rec_header **rhead, /* ptr to last record */
  941. bool *wrapped) /* last rec. wraps phys. log */
  942. {
  943. struct xlog_rec_header *tmp_rhead;
  944. char *tmp_buffer;
  945. xfs_daddr_t first_bad;
  946. xfs_daddr_t tmp_rhead_blk;
  947. int found;
  948. int error;
  949. bool tmp_wrapped;
  950. /*
  951. * Check the head of the log for torn writes. Search backwards from the
  952. * head until we hit the tail or the maximum number of log record I/Os
  953. * that could have been in flight at one time. Use a temporary buffer so
  954. * we don't trash the rhead/buffer pointers from the caller.
  955. */
  956. tmp_buffer = xlog_alloc_buffer(log, 1);
  957. if (!tmp_buffer)
  958. return -ENOMEM;
  959. error = xlog_rseek_logrec_hdr(log, *head_blk, *tail_blk,
  960. XLOG_MAX_ICLOGS, tmp_buffer,
  961. &tmp_rhead_blk, &tmp_rhead, &tmp_wrapped);
  962. kmem_free(tmp_buffer);
  963. if (error < 0)
  964. return error;
  965. /*
  966. * Now run a CRC verification pass over the records starting at the
  967. * block found above to the current head. If a CRC failure occurs, the
  968. * log block of the first bad record is saved in first_bad.
  969. */
  970. error = xlog_do_recovery_pass(log, *head_blk, tmp_rhead_blk,
  971. XLOG_RECOVER_CRCPASS, &first_bad);
  972. if ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
  973. /*
  974. * We've hit a potential torn write. Reset the error and warn
  975. * about it.
  976. */
  977. error = 0;
  978. xfs_warn(log->l_mp,
  979. "Torn write (CRC failure) detected at log block 0x%llx. Truncating head block from 0x%llx.",
  980. first_bad, *head_blk);
  981. /*
  982. * Get the header block and buffer pointer for the last good
  983. * record before the bad record.
  984. *
  985. * Note that xlog_find_tail() clears the blocks at the new head
  986. * (i.e., the records with invalid CRC) if the cycle number
  987. * matches the current cycle.
  988. */
  989. found = xlog_rseek_logrec_hdr(log, first_bad, *tail_blk, 1,
  990. buffer, rhead_blk, rhead, wrapped);
  991. if (found < 0)
  992. return found;
  993. if (found == 0) /* XXX: right thing to do here? */
  994. return -EIO;
  995. /*
  996. * Reset the head block to the starting block of the first bad
  997. * log record and set the tail block based on the last good
  998. * record.
  999. *
  1000. * Bail out if the updated head/tail match as this indicates
  1001. * possible corruption outside of the acceptable
  1002. * (XLOG_MAX_ICLOGS) range. This is a job for xfs_repair...
  1003. */
  1004. *head_blk = first_bad;
  1005. *tail_blk = BLOCK_LSN(be64_to_cpu((*rhead)->h_tail_lsn));
  1006. if (*head_blk == *tail_blk) {
  1007. ASSERT(0);
  1008. return 0;
  1009. }
  1010. }
  1011. if (error)
  1012. return error;
  1013. return xlog_verify_tail(log, *head_blk, tail_blk,
  1014. be32_to_cpu((*rhead)->h_size));
  1015. }
  1016. /*
  1017. * We need to make sure we handle log wrapping properly, so we can't use the
  1018. * calculated logbno directly. Make sure it wraps to the correct bno inside the
  1019. * log.
  1020. *
  1021. * The log is limited to 32 bit sizes, so we use the appropriate modulus
  1022. * operation here and cast it back to a 64 bit daddr on return.
  1023. */
  1024. static inline xfs_daddr_t
  1025. xlog_wrap_logbno(
  1026. struct xlog *log,
  1027. xfs_daddr_t bno)
  1028. {
  1029. int mod;
  1030. div_s64_rem(bno, log->l_logBBsize, &mod);
  1031. return mod;
  1032. }
  1033. /*
  1034. * Check whether the head of the log points to an unmount record. In other
  1035. * words, determine whether the log is clean. If so, update the in-core state
  1036. * appropriately.
  1037. */
  1038. static int
  1039. xlog_check_unmount_rec(
  1040. struct xlog *log,
  1041. xfs_daddr_t *head_blk,
  1042. xfs_daddr_t *tail_blk,
  1043. struct xlog_rec_header *rhead,
  1044. xfs_daddr_t rhead_blk,
  1045. char *buffer,
  1046. bool *clean)
  1047. {
  1048. struct xlog_op_header *op_head;
  1049. xfs_daddr_t umount_data_blk;
  1050. xfs_daddr_t after_umount_blk;
  1051. int hblks;
  1052. int error;
  1053. char *offset;
  1054. *clean = false;
  1055. /*
  1056. * Look for unmount record. If we find it, then we know there was a
  1057. * clean unmount. Since 'i' could be the last block in the physical
  1058. * log, we convert to a log block before comparing to the head_blk.
  1059. *
  1060. * Save the current tail lsn to use to pass to xlog_clear_stale_blocks()
  1061. * below. We won't want to clear the unmount record if there is one, so
  1062. * we pass the lsn of the unmount record rather than the block after it.
  1063. */
  1064. hblks = xlog_logrec_hblks(log, rhead);
  1065. after_umount_blk = xlog_wrap_logbno(log,
  1066. rhead_blk + hblks + BTOBB(be32_to_cpu(rhead->h_len)));
  1067. if (*head_blk == after_umount_blk &&
  1068. be32_to_cpu(rhead->h_num_logops) == 1) {
  1069. umount_data_blk = xlog_wrap_logbno(log, rhead_blk + hblks);
  1070. error = xlog_bread(log, umount_data_blk, 1, buffer, &offset);
  1071. if (error)
  1072. return error;
  1073. op_head = (struct xlog_op_header *)offset;
  1074. if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
  1075. /*
  1076. * Set tail and last sync so that newly written log
  1077. * records will point recovery to after the current
  1078. * unmount record.
  1079. */
  1080. xlog_assign_atomic_lsn(&log->l_tail_lsn,
  1081. log->l_curr_cycle, after_umount_blk);
  1082. xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
  1083. log->l_curr_cycle, after_umount_blk);
  1084. *tail_blk = after_umount_blk;
  1085. *clean = true;
  1086. }
  1087. }
  1088. return 0;
  1089. }
  1090. static void
  1091. xlog_set_state(
  1092. struct xlog *log,
  1093. xfs_daddr_t head_blk,
  1094. struct xlog_rec_header *rhead,
  1095. xfs_daddr_t rhead_blk,
  1096. bool bump_cycle)
  1097. {
  1098. /*
  1099. * Reset log values according to the state of the log when we
  1100. * crashed. In the case where head_blk == 0, we bump curr_cycle
  1101. * one because the next write starts a new cycle rather than
  1102. * continuing the cycle of the last good log record. At this
  1103. * point we have guaranteed that all partial log records have been
  1104. * accounted for. Therefore, we know that the last good log record
  1105. * written was complete and ended exactly on the end boundary
  1106. * of the physical log.
  1107. */
  1108. log->l_prev_block = rhead_blk;
  1109. log->l_curr_block = (int)head_blk;
  1110. log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
  1111. if (bump_cycle)
  1112. log->l_curr_cycle++;
  1113. atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
  1114. atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
  1115. xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
  1116. BBTOB(log->l_curr_block));
  1117. xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
  1118. BBTOB(log->l_curr_block));
  1119. }
  1120. /*
  1121. * Find the sync block number or the tail of the log.
  1122. *
  1123. * This will be the block number of the last record to have its
  1124. * associated buffers synced to disk. Every log record header has
  1125. * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
  1126. * to get a sync block number. The only concern is to figure out which
  1127. * log record header to believe.
  1128. *
  1129. * The following algorithm uses the log record header with the largest
  1130. * lsn. The entire log record does not need to be valid. We only care
  1131. * that the header is valid.
  1132. *
  1133. * We could speed up search by using current head_blk buffer, but it is not
  1134. * available.
  1135. */
  1136. STATIC int
  1137. xlog_find_tail(
  1138. struct xlog *log,
  1139. xfs_daddr_t *head_blk,
  1140. xfs_daddr_t *tail_blk)
  1141. {
  1142. xlog_rec_header_t *rhead;
  1143. char *offset = NULL;
  1144. char *buffer;
  1145. int error;
  1146. xfs_daddr_t rhead_blk;
  1147. xfs_lsn_t tail_lsn;
  1148. bool wrapped = false;
  1149. bool clean = false;
  1150. /*
  1151. * Find previous log record
  1152. */
  1153. if ((error = xlog_find_head(log, head_blk)))
  1154. return error;
  1155. ASSERT(*head_blk < INT_MAX);
  1156. buffer = xlog_alloc_buffer(log, 1);
  1157. if (!buffer)
  1158. return -ENOMEM;
  1159. if (*head_blk == 0) { /* special case */
  1160. error = xlog_bread(log, 0, 1, buffer, &offset);
  1161. if (error)
  1162. goto done;
  1163. if (xlog_get_cycle(offset) == 0) {
  1164. *tail_blk = 0;
  1165. /* leave all other log inited values alone */
  1166. goto done;
  1167. }
  1168. }
  1169. /*
  1170. * Search backwards through the log looking for the log record header
  1171. * block. This wraps all the way back around to the head so something is
  1172. * seriously wrong if we can't find it.
  1173. */
  1174. error = xlog_rseek_logrec_hdr(log, *head_blk, *head_blk, 1, buffer,
  1175. &rhead_blk, &rhead, &wrapped);
  1176. if (error < 0)
  1177. goto done;
  1178. if (!error) {
  1179. xfs_warn(log->l_mp, "%s: couldn't find sync record", __func__);
  1180. error = -EFSCORRUPTED;
  1181. goto done;
  1182. }
  1183. *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
  1184. /*
  1185. * Set the log state based on the current head record.
  1186. */
  1187. xlog_set_state(log, *head_blk, rhead, rhead_blk, wrapped);
  1188. tail_lsn = atomic64_read(&log->l_tail_lsn);
  1189. /*
  1190. * Look for an unmount record at the head of the log. This sets the log
  1191. * state to determine whether recovery is necessary.
  1192. */
  1193. error = xlog_check_unmount_rec(log, head_blk, tail_blk, rhead,
  1194. rhead_blk, buffer, &clean);
  1195. if (error)
  1196. goto done;
  1197. /*
  1198. * Verify the log head if the log is not clean (e.g., we have anything
  1199. * but an unmount record at the head). This uses CRC verification to
  1200. * detect and trim torn writes. If discovered, CRC failures are
  1201. * considered torn writes and the log head is trimmed accordingly.
  1202. *
  1203. * Note that we can only run CRC verification when the log is dirty
  1204. * because there's no guarantee that the log data behind an unmount
  1205. * record is compatible with the current architecture.
  1206. */
  1207. if (!clean) {
  1208. xfs_daddr_t orig_head = *head_blk;
  1209. error = xlog_verify_head(log, head_blk, tail_blk, buffer,
  1210. &rhead_blk, &rhead, &wrapped);
  1211. if (error)
  1212. goto done;
  1213. /* update in-core state again if the head changed */
  1214. if (*head_blk != orig_head) {
  1215. xlog_set_state(log, *head_blk, rhead, rhead_blk,
  1216. wrapped);
  1217. tail_lsn = atomic64_read(&log->l_tail_lsn);
  1218. error = xlog_check_unmount_rec(log, head_blk, tail_blk,
  1219. rhead, rhead_blk, buffer,
  1220. &clean);
  1221. if (error)
  1222. goto done;
  1223. }
  1224. }
  1225. /*
  1226. * Note that the unmount was clean. If the unmount was not clean, we
  1227. * need to know this to rebuild the superblock counters from the perag
  1228. * headers if we have a filesystem using non-persistent counters.
  1229. */
  1230. if (clean)
  1231. log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
  1232. /*
  1233. * Make sure that there are no blocks in front of the head
  1234. * with the same cycle number as the head. This can happen
  1235. * because we allow multiple outstanding log writes concurrently,
  1236. * and the later writes might make it out before earlier ones.
  1237. *
  1238. * We use the lsn from before modifying it so that we'll never
  1239. * overwrite the unmount record after a clean unmount.
  1240. *
  1241. * Do this only if we are going to recover the filesystem
  1242. *
  1243. * NOTE: This used to say "if (!readonly)"
  1244. * However on Linux, we can & do recover a read-only filesystem.
  1245. * We only skip recovery if NORECOVERY is specified on mount,
  1246. * in which case we would not be here.
  1247. *
  1248. * But... if the -device- itself is readonly, just skip this.
  1249. * We can't recover this device anyway, so it won't matter.
  1250. */
  1251. if (!xfs_readonly_buftarg(log->l_targ))
  1252. error = xlog_clear_stale_blocks(log, tail_lsn);
  1253. done:
  1254. kmem_free(buffer);
  1255. if (error)
  1256. xfs_warn(log->l_mp, "failed to locate log tail");
  1257. return error;
  1258. }
  1259. /*
  1260. * Is the log zeroed at all?
  1261. *
  1262. * The last binary search should be changed to perform an X block read
  1263. * once X becomes small enough. You can then search linearly through
  1264. * the X blocks. This will cut down on the number of reads we need to do.
  1265. *
  1266. * If the log is partially zeroed, this routine will pass back the blkno
  1267. * of the first block with cycle number 0. It won't have a complete LR
  1268. * preceding it.
  1269. *
  1270. * Return:
  1271. * 0 => the log is completely written to
  1272. * 1 => use *blk_no as the first block of the log
  1273. * <0 => error has occurred
  1274. */
  1275. STATIC int
  1276. xlog_find_zeroed(
  1277. struct xlog *log,
  1278. xfs_daddr_t *blk_no)
  1279. {
  1280. char *buffer;
  1281. char *offset;
  1282. uint first_cycle, last_cycle;
  1283. xfs_daddr_t new_blk, last_blk, start_blk;
  1284. xfs_daddr_t num_scan_bblks;
  1285. int error, log_bbnum = log->l_logBBsize;
  1286. *blk_no = 0;
  1287. /* check totally zeroed log */
  1288. buffer = xlog_alloc_buffer(log, 1);
  1289. if (!buffer)
  1290. return -ENOMEM;
  1291. error = xlog_bread(log, 0, 1, buffer, &offset);
  1292. if (error)
  1293. goto out_free_buffer;
  1294. first_cycle = xlog_get_cycle(offset);
  1295. if (first_cycle == 0) { /* completely zeroed log */
  1296. *blk_no = 0;
  1297. kmem_free(buffer);
  1298. return 1;
  1299. }
  1300. /* check partially zeroed log */
  1301. error = xlog_bread(log, log_bbnum-1, 1, buffer, &offset);
  1302. if (error)
  1303. goto out_free_buffer;
  1304. last_cycle = xlog_get_cycle(offset);
  1305. if (last_cycle != 0) { /* log completely written to */
  1306. kmem_free(buffer);
  1307. return 0;
  1308. }
  1309. /* we have a partially zeroed log */
  1310. last_blk = log_bbnum-1;
  1311. error = xlog_find_cycle_start(log, buffer, 0, &last_blk, 0);
  1312. if (error)
  1313. goto out_free_buffer;
  1314. /*
  1315. * Validate the answer. Because there is no way to guarantee that
  1316. * the entire log is made up of log records which are the same size,
  1317. * we scan over the defined maximum blocks. At this point, the maximum
  1318. * is not chosen to mean anything special. XXXmiken
  1319. */
  1320. num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
  1321. ASSERT(num_scan_bblks <= INT_MAX);
  1322. if (last_blk < num_scan_bblks)
  1323. num_scan_bblks = last_blk;
  1324. start_blk = last_blk - num_scan_bblks;
  1325. /*
  1326. * We search for any instances of cycle number 0 that occur before
  1327. * our current estimate of the head. What we're trying to detect is
  1328. * 1 ... | 0 | 1 | 0...
  1329. * ^ binary search ends here
  1330. */
  1331. if ((error = xlog_find_verify_cycle(log, start_blk,
  1332. (int)num_scan_bblks, 0, &new_blk)))
  1333. goto out_free_buffer;
  1334. if (new_blk != -1)
  1335. last_blk = new_blk;
  1336. /*
  1337. * Potentially backup over partial log record write. We don't need
  1338. * to search the end of the log because we know it is zero.
  1339. */
  1340. error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
  1341. if (error == 1)
  1342. error = -EIO;
  1343. if (error)
  1344. goto out_free_buffer;
  1345. *blk_no = last_blk;
  1346. out_free_buffer:
  1347. kmem_free(buffer);
  1348. if (error)
  1349. return error;
  1350. return 1;
  1351. }
  1352. /*
  1353. * These are simple subroutines used by xlog_clear_stale_blocks() below
  1354. * to initialize a buffer full of empty log record headers and write
  1355. * them into the log.
  1356. */
  1357. STATIC void
  1358. xlog_add_record(
  1359. struct xlog *log,
  1360. char *buf,
  1361. int cycle,
  1362. int block,
  1363. int tail_cycle,
  1364. int tail_block)
  1365. {
  1366. xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
  1367. memset(buf, 0, BBSIZE);
  1368. recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
  1369. recp->h_cycle = cpu_to_be32(cycle);
  1370. recp->h_version = cpu_to_be32(
  1371. xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
  1372. recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
  1373. recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
  1374. recp->h_fmt = cpu_to_be32(XLOG_FMT);
  1375. memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
  1376. }
  1377. STATIC int
  1378. xlog_write_log_records(
  1379. struct xlog *log,
  1380. int cycle,
  1381. int start_block,
  1382. int blocks,
  1383. int tail_cycle,
  1384. int tail_block)
  1385. {
  1386. char *offset;
  1387. char *buffer;
  1388. int balign, ealign;
  1389. int sectbb = log->l_sectBBsize;
  1390. int end_block = start_block + blocks;
  1391. int bufblks;
  1392. int error = 0;
  1393. int i, j = 0;
  1394. /*
  1395. * Greedily allocate a buffer big enough to handle the full
  1396. * range of basic blocks to be written. If that fails, try
  1397. * a smaller size. We need to be able to write at least a
  1398. * log sector, or we're out of luck.
  1399. */
  1400. bufblks = 1 << ffs(blocks);
  1401. while (bufblks > log->l_logBBsize)
  1402. bufblks >>= 1;
  1403. while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
  1404. bufblks >>= 1;
  1405. if (bufblks < sectbb)
  1406. return -ENOMEM;
  1407. }
  1408. /* We may need to do a read at the start to fill in part of
  1409. * the buffer in the starting sector not covered by the first
  1410. * write below.
  1411. */
  1412. balign = round_down(start_block, sectbb);
  1413. if (balign != start_block) {
  1414. error = xlog_bread_noalign(log, start_block, 1, buffer);
  1415. if (error)
  1416. goto out_free_buffer;
  1417. j = start_block - balign;
  1418. }
  1419. for (i = start_block; i < end_block; i += bufblks) {
  1420. int bcount, endcount;
  1421. bcount = min(bufblks, end_block - start_block);
  1422. endcount = bcount - j;
  1423. /* We may need to do a read at the end to fill in part of
  1424. * the buffer in the final sector not covered by the write.
  1425. * If this is the same sector as the above read, skip it.
  1426. */
  1427. ealign = round_down(end_block, sectbb);
  1428. if (j == 0 && (start_block + endcount > ealign)) {
  1429. error = xlog_bread_noalign(log, ealign, sectbb,
  1430. buffer + BBTOB(ealign - start_block));
  1431. if (error)
  1432. break;
  1433. }
  1434. offset = buffer + xlog_align(log, start_block);
  1435. for (; j < endcount; j++) {
  1436. xlog_add_record(log, offset, cycle, i+j,
  1437. tail_cycle, tail_block);
  1438. offset += BBSIZE;
  1439. }
  1440. error = xlog_bwrite(log, start_block, endcount, buffer);
  1441. if (error)
  1442. break;
  1443. start_block += endcount;
  1444. j = 0;
  1445. }
  1446. out_free_buffer:
  1447. kmem_free(buffer);
  1448. return error;
  1449. }
  1450. /*
  1451. * This routine is called to blow away any incomplete log writes out
  1452. * in front of the log head. We do this so that we won't become confused
  1453. * if we come up, write only a little bit more, and then crash again.
  1454. * If we leave the partial log records out there, this situation could
  1455. * cause us to think those partial writes are valid blocks since they
  1456. * have the current cycle number. We get rid of them by overwriting them
  1457. * with empty log records with the old cycle number rather than the
  1458. * current one.
  1459. *
  1460. * The tail lsn is passed in rather than taken from
  1461. * the log so that we will not write over the unmount record after a
  1462. * clean unmount in a 512 block log. Doing so would leave the log without
  1463. * any valid log records in it until a new one was written. If we crashed
  1464. * during that time we would not be able to recover.
  1465. */
  1466. STATIC int
  1467. xlog_clear_stale_blocks(
  1468. struct xlog *log,
  1469. xfs_lsn_t tail_lsn)
  1470. {
  1471. int tail_cycle, head_cycle;
  1472. int tail_block, head_block;
  1473. int tail_distance, max_distance;
  1474. int distance;
  1475. int error;
  1476. tail_cycle = CYCLE_LSN(tail_lsn);
  1477. tail_block = BLOCK_LSN(tail_lsn);
  1478. head_cycle = log->l_curr_cycle;
  1479. head_block = log->l_curr_block;
  1480. /*
  1481. * Figure out the distance between the new head of the log
  1482. * and the tail. We want to write over any blocks beyond the
  1483. * head that we may have written just before the crash, but
  1484. * we don't want to overwrite the tail of the log.
  1485. */
  1486. if (head_cycle == tail_cycle) {
  1487. /*
  1488. * The tail is behind the head in the physical log,
  1489. * so the distance from the head to the tail is the
  1490. * distance from the head to the end of the log plus
  1491. * the distance from the beginning of the log to the
  1492. * tail.
  1493. */
  1494. if (XFS_IS_CORRUPT(log->l_mp,
  1495. head_block < tail_block ||
  1496. head_block >= log->l_logBBsize))
  1497. return -EFSCORRUPTED;
  1498. tail_distance = tail_block + (log->l_logBBsize - head_block);
  1499. } else {
  1500. /*
  1501. * The head is behind the tail in the physical log,
  1502. * so the distance from the head to the tail is just
  1503. * the tail block minus the head block.
  1504. */
  1505. if (XFS_IS_CORRUPT(log->l_mp,
  1506. head_block >= tail_block ||
  1507. head_cycle != tail_cycle + 1))
  1508. return -EFSCORRUPTED;
  1509. tail_distance = tail_block - head_block;
  1510. }
  1511. /*
  1512. * If the head is right up against the tail, we can't clear
  1513. * anything.
  1514. */
  1515. if (tail_distance <= 0) {
  1516. ASSERT(tail_distance == 0);
  1517. return 0;
  1518. }
  1519. max_distance = XLOG_TOTAL_REC_SHIFT(log);
  1520. /*
  1521. * Take the smaller of the maximum amount of outstanding I/O
  1522. * we could have and the distance to the tail to clear out.
  1523. * We take the smaller so that we don't overwrite the tail and
  1524. * we don't waste all day writing from the head to the tail
  1525. * for no reason.
  1526. */
  1527. max_distance = min(max_distance, tail_distance);
  1528. if ((head_block + max_distance) <= log->l_logBBsize) {
  1529. /*
  1530. * We can stomp all the blocks we need to without
  1531. * wrapping around the end of the log. Just do it
  1532. * in a single write. Use the cycle number of the
  1533. * current cycle minus one so that the log will look like:
  1534. * n ... | n - 1 ...
  1535. */
  1536. error = xlog_write_log_records(log, (head_cycle - 1),
  1537. head_block, max_distance, tail_cycle,
  1538. tail_block);
  1539. if (error)
  1540. return error;
  1541. } else {
  1542. /*
  1543. * We need to wrap around the end of the physical log in
  1544. * order to clear all the blocks. Do it in two separate
  1545. * I/Os. The first write should be from the head to the
  1546. * end of the physical log, and it should use the current
  1547. * cycle number minus one just like above.
  1548. */
  1549. distance = log->l_logBBsize - head_block;
  1550. error = xlog_write_log_records(log, (head_cycle - 1),
  1551. head_block, distance, tail_cycle,
  1552. tail_block);
  1553. if (error)
  1554. return error;
  1555. /*
  1556. * Now write the blocks at the start of the physical log.
  1557. * This writes the remainder of the blocks we want to clear.
  1558. * It uses the current cycle number since we're now on the
  1559. * same cycle as the head so that we get:
  1560. * n ... n ... | n - 1 ...
  1561. * ^^^^^ blocks we're writing
  1562. */
  1563. distance = max_distance - (log->l_logBBsize - head_block);
  1564. error = xlog_write_log_records(log, head_cycle, 0, distance,
  1565. tail_cycle, tail_block);
  1566. if (error)
  1567. return error;
  1568. }
  1569. return 0;
  1570. }
  1571. /*
  1572. * Release the recovered intent item in the AIL that matches the given intent
  1573. * type and intent id.
  1574. */
  1575. void
  1576. xlog_recover_release_intent(
  1577. struct xlog *log,
  1578. unsigned short intent_type,
  1579. uint64_t intent_id)
  1580. {
  1581. struct xfs_ail_cursor cur;
  1582. struct xfs_log_item *lip;
  1583. struct xfs_ail *ailp = log->l_ailp;
  1584. spin_lock(&ailp->ail_lock);
  1585. for (lip = xfs_trans_ail_cursor_first(ailp, &cur, 0); lip != NULL;
  1586. lip = xfs_trans_ail_cursor_next(ailp, &cur)) {
  1587. if (lip->li_type != intent_type)
  1588. continue;
  1589. if (!lip->li_ops->iop_match(lip, intent_id))
  1590. continue;
  1591. spin_unlock(&ailp->ail_lock);
  1592. lip->li_ops->iop_release(lip);
  1593. spin_lock(&ailp->ail_lock);
  1594. break;
  1595. }
  1596. xfs_trans_ail_cursor_done(&cur);
  1597. spin_unlock(&ailp->ail_lock);
  1598. }
  1599. /******************************************************************************
  1600. *
  1601. * Log recover routines
  1602. *
  1603. ******************************************************************************
  1604. */
  1605. static const struct xlog_recover_item_ops *xlog_recover_item_ops[] = {
  1606. &xlog_buf_item_ops,
  1607. &xlog_inode_item_ops,
  1608. &xlog_dquot_item_ops,
  1609. &xlog_quotaoff_item_ops,
  1610. &xlog_icreate_item_ops,
  1611. &xlog_efi_item_ops,
  1612. &xlog_efd_item_ops,
  1613. &xlog_rui_item_ops,
  1614. &xlog_rud_item_ops,
  1615. &xlog_cui_item_ops,
  1616. &xlog_cud_item_ops,
  1617. &xlog_bui_item_ops,
  1618. &xlog_bud_item_ops,
  1619. };
  1620. static const struct xlog_recover_item_ops *
  1621. xlog_find_item_ops(
  1622. struct xlog_recover_item *item)
  1623. {
  1624. unsigned int i;
  1625. for (i = 0; i < ARRAY_SIZE(xlog_recover_item_ops); i++)
  1626. if (ITEM_TYPE(item) == xlog_recover_item_ops[i]->item_type)
  1627. return xlog_recover_item_ops[i];
  1628. return NULL;
  1629. }
  1630. /*
  1631. * Sort the log items in the transaction.
  1632. *
  1633. * The ordering constraints are defined by the inode allocation and unlink
  1634. * behaviour. The rules are:
  1635. *
  1636. * 1. Every item is only logged once in a given transaction. Hence it
  1637. * represents the last logged state of the item. Hence ordering is
  1638. * dependent on the order in which operations need to be performed so
  1639. * required initial conditions are always met.
  1640. *
  1641. * 2. Cancelled buffers are recorded in pass 1 in a separate table and
  1642. * there's nothing to replay from them so we can simply cull them
  1643. * from the transaction. However, we can't do that until after we've
  1644. * replayed all the other items because they may be dependent on the
  1645. * cancelled buffer and replaying the cancelled buffer can remove it
  1646. * form the cancelled buffer table. Hence they have tobe done last.
  1647. *
  1648. * 3. Inode allocation buffers must be replayed before inode items that
  1649. * read the buffer and replay changes into it. For filesystems using the
  1650. * ICREATE transactions, this means XFS_LI_ICREATE objects need to get
  1651. * treated the same as inode allocation buffers as they create and
  1652. * initialise the buffers directly.
  1653. *
  1654. * 4. Inode unlink buffers must be replayed after inode items are replayed.
  1655. * This ensures that inodes are completely flushed to the inode buffer
  1656. * in a "free" state before we remove the unlinked inode list pointer.
  1657. *
  1658. * Hence the ordering needs to be inode allocation buffers first, inode items
  1659. * second, inode unlink buffers third and cancelled buffers last.
  1660. *
  1661. * But there's a problem with that - we can't tell an inode allocation buffer
  1662. * apart from a regular buffer, so we can't separate them. We can, however,
  1663. * tell an inode unlink buffer from the others, and so we can separate them out
  1664. * from all the other buffers and move them to last.
  1665. *
  1666. * Hence, 4 lists, in order from head to tail:
  1667. * - buffer_list for all buffers except cancelled/inode unlink buffers
  1668. * - item_list for all non-buffer items
  1669. * - inode_buffer_list for inode unlink buffers
  1670. * - cancel_list for the cancelled buffers
  1671. *
  1672. * Note that we add objects to the tail of the lists so that first-to-last
  1673. * ordering is preserved within the lists. Adding objects to the head of the
  1674. * list means when we traverse from the head we walk them in last-to-first
  1675. * order. For cancelled buffers and inode unlink buffers this doesn't matter,
  1676. * but for all other items there may be specific ordering that we need to
  1677. * preserve.
  1678. */
  1679. STATIC int
  1680. xlog_recover_reorder_trans(
  1681. struct xlog *log,
  1682. struct xlog_recover *trans,
  1683. int pass)
  1684. {
  1685. struct xlog_recover_item *item, *n;
  1686. int error = 0;
  1687. LIST_HEAD(sort_list);
  1688. LIST_HEAD(cancel_list);
  1689. LIST_HEAD(buffer_list);
  1690. LIST_HEAD(inode_buffer_list);
  1691. LIST_HEAD(item_list);
  1692. list_splice_init(&trans->r_itemq, &sort_list);
  1693. list_for_each_entry_safe(item, n, &sort_list, ri_list) {
  1694. enum xlog_recover_reorder fate = XLOG_REORDER_ITEM_LIST;
  1695. item->ri_ops = xlog_find_item_ops(item);
  1696. if (!item->ri_ops) {
  1697. xfs_warn(log->l_mp,
  1698. "%s: unrecognized type of log operation (%d)",
  1699. __func__, ITEM_TYPE(item));
  1700. ASSERT(0);
  1701. /*
  1702. * return the remaining items back to the transaction
  1703. * item list so they can be freed in caller.
  1704. */
  1705. if (!list_empty(&sort_list))
  1706. list_splice_init(&sort_list, &trans->r_itemq);
  1707. error = -EFSCORRUPTED;
  1708. break;
  1709. }
  1710. if (item->ri_ops->reorder)
  1711. fate = item->ri_ops->reorder(item);
  1712. switch (fate) {
  1713. case XLOG_REORDER_BUFFER_LIST:
  1714. list_move_tail(&item->ri_list, &buffer_list);
  1715. break;
  1716. case XLOG_REORDER_CANCEL_LIST:
  1717. trace_xfs_log_recover_item_reorder_head(log,
  1718. trans, item, pass);
  1719. list_move(&item->ri_list, &cancel_list);
  1720. break;
  1721. case XLOG_REORDER_INODE_BUFFER_LIST:
  1722. list_move(&item->ri_list, &inode_buffer_list);
  1723. break;
  1724. case XLOG_REORDER_ITEM_LIST:
  1725. trace_xfs_log_recover_item_reorder_tail(log,
  1726. trans, item, pass);
  1727. list_move_tail(&item->ri_list, &item_list);
  1728. break;
  1729. }
  1730. }
  1731. ASSERT(list_empty(&sort_list));
  1732. if (!list_empty(&buffer_list))
  1733. list_splice(&buffer_list, &trans->r_itemq);
  1734. if (!list_empty(&item_list))
  1735. list_splice_tail(&item_list, &trans->r_itemq);
  1736. if (!list_empty(&inode_buffer_list))
  1737. list_splice_tail(&inode_buffer_list, &trans->r_itemq);
  1738. if (!list_empty(&cancel_list))
  1739. list_splice_tail(&cancel_list, &trans->r_itemq);
  1740. return error;
  1741. }
  1742. void
  1743. xlog_buf_readahead(
  1744. struct xlog *log,
  1745. xfs_daddr_t blkno,
  1746. uint len,
  1747. const struct xfs_buf_ops *ops)
  1748. {
  1749. if (!xlog_is_buffer_cancelled(log, blkno, len))
  1750. xfs_buf_readahead(log->l_mp->m_ddev_targp, blkno, len, ops);
  1751. }
  1752. STATIC int
  1753. xlog_recover_items_pass2(
  1754. struct xlog *log,
  1755. struct xlog_recover *trans,
  1756. struct list_head *buffer_list,
  1757. struct list_head *item_list)
  1758. {
  1759. struct xlog_recover_item *item;
  1760. int error = 0;
  1761. list_for_each_entry(item, item_list, ri_list) {
  1762. trace_xfs_log_recover_item_recover(log, trans, item,
  1763. XLOG_RECOVER_PASS2);
  1764. if (item->ri_ops->commit_pass2)
  1765. error = item->ri_ops->commit_pass2(log, buffer_list,
  1766. item, trans->r_lsn);
  1767. if (error)
  1768. return error;
  1769. }
  1770. return error;
  1771. }
  1772. /*
  1773. * Perform the transaction.
  1774. *
  1775. * If the transaction modifies a buffer or inode, do it now. Otherwise,
  1776. * EFIs and EFDs get queued up by adding entries into the AIL for them.
  1777. */
  1778. STATIC int
  1779. xlog_recover_commit_trans(
  1780. struct xlog *log,
  1781. struct xlog_recover *trans,
  1782. int pass,
  1783. struct list_head *buffer_list)
  1784. {
  1785. int error = 0;
  1786. int items_queued = 0;
  1787. struct xlog_recover_item *item;
  1788. struct xlog_recover_item *next;
  1789. LIST_HEAD (ra_list);
  1790. LIST_HEAD (done_list);
  1791. #define XLOG_RECOVER_COMMIT_QUEUE_MAX 100
  1792. hlist_del_init(&trans->r_list);
  1793. error = xlog_recover_reorder_trans(log, trans, pass);
  1794. if (error)
  1795. return error;
  1796. list_for_each_entry_safe(item, next, &trans->r_itemq, ri_list) {
  1797. trace_xfs_log_recover_item_recover(log, trans, item, pass);
  1798. switch (pass) {
  1799. case XLOG_RECOVER_PASS1:
  1800. if (item->ri_ops->commit_pass1)
  1801. error = item->ri_ops->commit_pass1(log, item);
  1802. break;
  1803. case XLOG_RECOVER_PASS2:
  1804. if (item->ri_ops->ra_pass2)
  1805. item->ri_ops->ra_pass2(log, item);
  1806. list_move_tail(&item->ri_list, &ra_list);
  1807. items_queued++;
  1808. if (items_queued >= XLOG_RECOVER_COMMIT_QUEUE_MAX) {
  1809. error = xlog_recover_items_pass2(log, trans,
  1810. buffer_list, &ra_list);
  1811. list_splice_tail_init(&ra_list, &done_list);
  1812. items_queued = 0;
  1813. }
  1814. break;
  1815. default:
  1816. ASSERT(0);
  1817. }
  1818. if (error)
  1819. goto out;
  1820. }
  1821. out:
  1822. if (!list_empty(&ra_list)) {
  1823. if (!error)
  1824. error = xlog_recover_items_pass2(log, trans,
  1825. buffer_list, &ra_list);
  1826. list_splice_tail_init(&ra_list, &done_list);
  1827. }
  1828. if (!list_empty(&done_list))
  1829. list_splice_init(&done_list, &trans->r_itemq);
  1830. return error;
  1831. }
  1832. STATIC void
  1833. xlog_recover_add_item(
  1834. struct list_head *head)
  1835. {
  1836. struct xlog_recover_item *item;
  1837. item = kmem_zalloc(sizeof(struct xlog_recover_item), 0);
  1838. INIT_LIST_HEAD(&item->ri_list);
  1839. list_add_tail(&item->ri_list, head);
  1840. }
  1841. STATIC int
  1842. xlog_recover_add_to_cont_trans(
  1843. struct xlog *log,
  1844. struct xlog_recover *trans,
  1845. char *dp,
  1846. int len)
  1847. {
  1848. struct xlog_recover_item *item;
  1849. char *ptr, *old_ptr;
  1850. int old_len;
  1851. /*
  1852. * If the transaction is empty, the header was split across this and the
  1853. * previous record. Copy the rest of the header.
  1854. */
  1855. if (list_empty(&trans->r_itemq)) {
  1856. ASSERT(len <= sizeof(struct xfs_trans_header));
  1857. if (len > sizeof(struct xfs_trans_header)) {
  1858. xfs_warn(log->l_mp, "%s: bad header length", __func__);
  1859. return -EFSCORRUPTED;
  1860. }
  1861. xlog_recover_add_item(&trans->r_itemq);
  1862. ptr = (char *)&trans->r_theader +
  1863. sizeof(struct xfs_trans_header) - len;
  1864. memcpy(ptr, dp, len);
  1865. return 0;
  1866. }
  1867. /* take the tail entry */
  1868. item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
  1869. ri_list);
  1870. old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
  1871. old_len = item->ri_buf[item->ri_cnt-1].i_len;
  1872. ptr = krealloc(old_ptr, len + old_len, GFP_KERNEL | __GFP_NOFAIL);
  1873. memcpy(&ptr[old_len], dp, len);
  1874. item->ri_buf[item->ri_cnt-1].i_len += len;
  1875. item->ri_buf[item->ri_cnt-1].i_addr = ptr;
  1876. trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
  1877. return 0;
  1878. }
  1879. /*
  1880. * The next region to add is the start of a new region. It could be
  1881. * a whole region or it could be the first part of a new region. Because
  1882. * of this, the assumption here is that the type and size fields of all
  1883. * format structures fit into the first 32 bits of the structure.
  1884. *
  1885. * This works because all regions must be 32 bit aligned. Therefore, we
  1886. * either have both fields or we have neither field. In the case we have
  1887. * neither field, the data part of the region is zero length. We only have
  1888. * a log_op_header and can throw away the header since a new one will appear
  1889. * later. If we have at least 4 bytes, then we can determine how many regions
  1890. * will appear in the current log item.
  1891. */
  1892. STATIC int
  1893. xlog_recover_add_to_trans(
  1894. struct xlog *log,
  1895. struct xlog_recover *trans,
  1896. char *dp,
  1897. int len)
  1898. {
  1899. struct xfs_inode_log_format *in_f; /* any will do */
  1900. struct xlog_recover_item *item;
  1901. char *ptr;
  1902. if (!len)
  1903. return 0;
  1904. if (list_empty(&trans->r_itemq)) {
  1905. /* we need to catch log corruptions here */
  1906. if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
  1907. xfs_warn(log->l_mp, "%s: bad header magic number",
  1908. __func__);
  1909. ASSERT(0);
  1910. return -EFSCORRUPTED;
  1911. }
  1912. if (len > sizeof(struct xfs_trans_header)) {
  1913. xfs_warn(log->l_mp, "%s: bad header length", __func__);
  1914. ASSERT(0);
  1915. return -EFSCORRUPTED;
  1916. }
  1917. /*
  1918. * The transaction header can be arbitrarily split across op
  1919. * records. If we don't have the whole thing here, copy what we
  1920. * do have and handle the rest in the next record.
  1921. */
  1922. if (len == sizeof(struct xfs_trans_header))
  1923. xlog_recover_add_item(&trans->r_itemq);
  1924. memcpy(&trans->r_theader, dp, len);
  1925. return 0;
  1926. }
  1927. ptr = kmem_alloc(len, 0);
  1928. memcpy(ptr, dp, len);
  1929. in_f = (struct xfs_inode_log_format *)ptr;
  1930. /* take the tail entry */
  1931. item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
  1932. ri_list);
  1933. if (item->ri_total != 0 &&
  1934. item->ri_total == item->ri_cnt) {
  1935. /* tail item is in use, get a new one */
  1936. xlog_recover_add_item(&trans->r_itemq);
  1937. item = list_entry(trans->r_itemq.prev,
  1938. struct xlog_recover_item, ri_list);
  1939. }
  1940. if (item->ri_total == 0) { /* first region to be added */
  1941. if (in_f->ilf_size == 0 ||
  1942. in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
  1943. xfs_warn(log->l_mp,
  1944. "bad number of regions (%d) in inode log format",
  1945. in_f->ilf_size);
  1946. ASSERT(0);
  1947. kmem_free(ptr);
  1948. return -EFSCORRUPTED;
  1949. }
  1950. item->ri_total = in_f->ilf_size;
  1951. item->ri_buf =
  1952. kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
  1953. 0);
  1954. }
  1955. if (item->ri_total <= item->ri_cnt) {
  1956. xfs_warn(log->l_mp,
  1957. "log item region count (%d) overflowed size (%d)",
  1958. item->ri_cnt, item->ri_total);
  1959. ASSERT(0);
  1960. kmem_free(ptr);
  1961. return -EFSCORRUPTED;
  1962. }
  1963. /* Description region is ri_buf[0] */
  1964. item->ri_buf[item->ri_cnt].i_addr = ptr;
  1965. item->ri_buf[item->ri_cnt].i_len = len;
  1966. item->ri_cnt++;
  1967. trace_xfs_log_recover_item_add(log, trans, item, 0);
  1968. return 0;
  1969. }
  1970. /*
  1971. * Free up any resources allocated by the transaction
  1972. *
  1973. * Remember that EFIs, EFDs, and IUNLINKs are handled later.
  1974. */
  1975. STATIC void
  1976. xlog_recover_free_trans(
  1977. struct xlog_recover *trans)
  1978. {
  1979. struct xlog_recover_item *item, *n;
  1980. int i;
  1981. hlist_del_init(&trans->r_list);
  1982. list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
  1983. /* Free the regions in the item. */
  1984. list_del(&item->ri_list);
  1985. for (i = 0; i < item->ri_cnt; i++)
  1986. kmem_free(item->ri_buf[i].i_addr);
  1987. /* Free the item itself */
  1988. kmem_free(item->ri_buf);
  1989. kmem_free(item);
  1990. }
  1991. /* Free the transaction recover structure */
  1992. kmem_free(trans);
  1993. }
  1994. /*
  1995. * On error or completion, trans is freed.
  1996. */
  1997. STATIC int
  1998. xlog_recovery_process_trans(
  1999. struct xlog *log,
  2000. struct xlog_recover *trans,
  2001. char *dp,
  2002. unsigned int len,
  2003. unsigned int flags,
  2004. int pass,
  2005. struct list_head *buffer_list)
  2006. {
  2007. int error = 0;
  2008. bool freeit = false;
  2009. /* mask off ophdr transaction container flags */
  2010. flags &= ~XLOG_END_TRANS;
  2011. if (flags & XLOG_WAS_CONT_TRANS)
  2012. flags &= ~XLOG_CONTINUE_TRANS;
  2013. /*
  2014. * Callees must not free the trans structure. We'll decide if we need to
  2015. * free it or not based on the operation being done and it's result.
  2016. */
  2017. switch (flags) {
  2018. /* expected flag values */
  2019. case 0:
  2020. case XLOG_CONTINUE_TRANS:
  2021. error = xlog_recover_add_to_trans(log, trans, dp, len);
  2022. break;
  2023. case XLOG_WAS_CONT_TRANS:
  2024. error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
  2025. break;
  2026. case XLOG_COMMIT_TRANS:
  2027. error = xlog_recover_commit_trans(log, trans, pass,
  2028. buffer_list);
  2029. /* success or fail, we are now done with this transaction. */
  2030. freeit = true;
  2031. break;
  2032. /* unexpected flag values */
  2033. case XLOG_UNMOUNT_TRANS:
  2034. /* just skip trans */
  2035. xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
  2036. freeit = true;
  2037. break;
  2038. case XLOG_START_TRANS:
  2039. default:
  2040. xfs_warn(log->l_mp, "%s: bad flag 0x%x", __func__, flags);
  2041. ASSERT(0);
  2042. error = -EFSCORRUPTED;
  2043. break;
  2044. }
  2045. if (error || freeit)
  2046. xlog_recover_free_trans(trans);
  2047. return error;
  2048. }
  2049. /*
  2050. * Lookup the transaction recovery structure associated with the ID in the
  2051. * current ophdr. If the transaction doesn't exist and the start flag is set in
  2052. * the ophdr, then allocate a new transaction for future ID matches to find.
  2053. * Either way, return what we found during the lookup - an existing transaction
  2054. * or nothing.
  2055. */
  2056. STATIC struct xlog_recover *
  2057. xlog_recover_ophdr_to_trans(
  2058. struct hlist_head rhash[],
  2059. struct xlog_rec_header *rhead,
  2060. struct xlog_op_header *ohead)
  2061. {
  2062. struct xlog_recover *trans;
  2063. xlog_tid_t tid;
  2064. struct hlist_head *rhp;
  2065. tid = be32_to_cpu(ohead->oh_tid);
  2066. rhp = &rhash[XLOG_RHASH(tid)];
  2067. hlist_for_each_entry(trans, rhp, r_list) {
  2068. if (trans->r_log_tid == tid)
  2069. return trans;
  2070. }
  2071. /*
  2072. * skip over non-start transaction headers - we could be
  2073. * processing slack space before the next transaction starts
  2074. */
  2075. if (!(ohead->oh_flags & XLOG_START_TRANS))
  2076. return NULL;
  2077. ASSERT(be32_to_cpu(ohead->oh_len) == 0);
  2078. /*
  2079. * This is a new transaction so allocate a new recovery container to
  2080. * hold the recovery ops that will follow.
  2081. */
  2082. trans = kmem_zalloc(sizeof(struct xlog_recover), 0);
  2083. trans->r_log_tid = tid;
  2084. trans->r_lsn = be64_to_cpu(rhead->h_lsn);
  2085. INIT_LIST_HEAD(&trans->r_itemq);
  2086. INIT_HLIST_NODE(&trans->r_list);
  2087. hlist_add_head(&trans->r_list, rhp);
  2088. /*
  2089. * Nothing more to do for this ophdr. Items to be added to this new
  2090. * transaction will be in subsequent ophdr containers.
  2091. */
  2092. return NULL;
  2093. }
  2094. STATIC int
  2095. xlog_recover_process_ophdr(
  2096. struct xlog *log,
  2097. struct hlist_head rhash[],
  2098. struct xlog_rec_header *rhead,
  2099. struct xlog_op_header *ohead,
  2100. char *dp,
  2101. char *end,
  2102. int pass,
  2103. struct list_head *buffer_list)
  2104. {
  2105. struct xlog_recover *trans;
  2106. unsigned int len;
  2107. int error;
  2108. /* Do we understand who wrote this op? */
  2109. if (ohead->oh_clientid != XFS_TRANSACTION &&
  2110. ohead->oh_clientid != XFS_LOG) {
  2111. xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
  2112. __func__, ohead->oh_clientid);
  2113. ASSERT(0);
  2114. return -EFSCORRUPTED;
  2115. }
  2116. /*
  2117. * Check the ophdr contains all the data it is supposed to contain.
  2118. */
  2119. len = be32_to_cpu(ohead->oh_len);
  2120. if (dp + len > end) {
  2121. xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
  2122. WARN_ON(1);
  2123. return -EFSCORRUPTED;
  2124. }
  2125. trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
  2126. if (!trans) {
  2127. /* nothing to do, so skip over this ophdr */
  2128. return 0;
  2129. }
  2130. /*
  2131. * The recovered buffer queue is drained only once we know that all
  2132. * recovery items for the current LSN have been processed. This is
  2133. * required because:
  2134. *
  2135. * - Buffer write submission updates the metadata LSN of the buffer.
  2136. * - Log recovery skips items with a metadata LSN >= the current LSN of
  2137. * the recovery item.
  2138. * - Separate recovery items against the same metadata buffer can share
  2139. * a current LSN. I.e., consider that the LSN of a recovery item is
  2140. * defined as the starting LSN of the first record in which its
  2141. * transaction appears, that a record can hold multiple transactions,
  2142. * and/or that a transaction can span multiple records.
  2143. *
  2144. * In other words, we are allowed to submit a buffer from log recovery
  2145. * once per current LSN. Otherwise, we may incorrectly skip recovery
  2146. * items and cause corruption.
  2147. *
  2148. * We don't know up front whether buffers are updated multiple times per
  2149. * LSN. Therefore, track the current LSN of each commit log record as it
  2150. * is processed and drain the queue when it changes. Use commit records
  2151. * because they are ordered correctly by the logging code.
  2152. */
  2153. if (log->l_recovery_lsn != trans->r_lsn &&
  2154. ohead->oh_flags & XLOG_COMMIT_TRANS) {
  2155. error = xfs_buf_delwri_submit(buffer_list);
  2156. if (error)
  2157. return error;
  2158. log->l_recovery_lsn = trans->r_lsn;
  2159. }
  2160. return xlog_recovery_process_trans(log, trans, dp, len,
  2161. ohead->oh_flags, pass, buffer_list);
  2162. }
  2163. /*
  2164. * There are two valid states of the r_state field. 0 indicates that the
  2165. * transaction structure is in a normal state. We have either seen the
  2166. * start of the transaction or the last operation we added was not a partial
  2167. * operation. If the last operation we added to the transaction was a
  2168. * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
  2169. *
  2170. * NOTE: skip LRs with 0 data length.
  2171. */
  2172. STATIC int
  2173. xlog_recover_process_data(
  2174. struct xlog *log,
  2175. struct hlist_head rhash[],
  2176. struct xlog_rec_header *rhead,
  2177. char *dp,
  2178. int pass,
  2179. struct list_head *buffer_list)
  2180. {
  2181. struct xlog_op_header *ohead;
  2182. char *end;
  2183. int num_logops;
  2184. int error;
  2185. end = dp + be32_to_cpu(rhead->h_len);
  2186. num_logops = be32_to_cpu(rhead->h_num_logops);
  2187. /* check the log format matches our own - else we can't recover */
  2188. if (xlog_header_check_recover(log->l_mp, rhead))
  2189. return -EIO;
  2190. trace_xfs_log_recover_record(log, rhead, pass);
  2191. while ((dp < end) && num_logops) {
  2192. ohead = (struct xlog_op_header *)dp;
  2193. dp += sizeof(*ohead);
  2194. ASSERT(dp <= end);
  2195. /* errors will abort recovery */
  2196. error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
  2197. dp, end, pass, buffer_list);
  2198. if (error)
  2199. return error;
  2200. dp += be32_to_cpu(ohead->oh_len);
  2201. num_logops--;
  2202. }
  2203. return 0;
  2204. }
  2205. /* Take all the collected deferred ops and finish them in order. */
  2206. static int
  2207. xlog_finish_defer_ops(
  2208. struct xfs_mount *mp,
  2209. struct list_head *capture_list)
  2210. {
  2211. struct xfs_defer_capture *dfc, *next;
  2212. struct xfs_trans *tp;
  2213. struct xfs_inode *ip;
  2214. int error = 0;
  2215. list_for_each_entry_safe(dfc, next, capture_list, dfc_list) {
  2216. struct xfs_trans_res resv;
  2217. /*
  2218. * Create a new transaction reservation from the captured
  2219. * information. Set logcount to 1 to force the new transaction
  2220. * to regrant every roll so that we can make forward progress
  2221. * in recovery no matter how full the log might be.
  2222. */
  2223. resv.tr_logres = dfc->dfc_logres;
  2224. resv.tr_logcount = 1;
  2225. resv.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  2226. error = xfs_trans_alloc(mp, &resv, dfc->dfc_blkres,
  2227. dfc->dfc_rtxres, XFS_TRANS_RESERVE, &tp);
  2228. if (error)
  2229. return error;
  2230. /*
  2231. * Transfer to this new transaction all the dfops we captured
  2232. * from recovering a single intent item.
  2233. */
  2234. list_del_init(&dfc->dfc_list);
  2235. xfs_defer_ops_continue(dfc, tp, &ip);
  2236. error = xfs_trans_commit(tp);
  2237. if (ip) {
  2238. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2239. xfs_irele(ip);
  2240. }
  2241. if (error)
  2242. return error;
  2243. }
  2244. ASSERT(list_empty(capture_list));
  2245. return 0;
  2246. }
  2247. /* Release all the captured defer ops and capture structures in this list. */
  2248. static void
  2249. xlog_abort_defer_ops(
  2250. struct xfs_mount *mp,
  2251. struct list_head *capture_list)
  2252. {
  2253. struct xfs_defer_capture *dfc;
  2254. struct xfs_defer_capture *next;
  2255. list_for_each_entry_safe(dfc, next, capture_list, dfc_list) {
  2256. list_del_init(&dfc->dfc_list);
  2257. xfs_defer_ops_release(mp, dfc);
  2258. }
  2259. }
  2260. /*
  2261. * When this is called, all of the log intent items which did not have
  2262. * corresponding log done items should be in the AIL. What we do now
  2263. * is update the data structures associated with each one.
  2264. *
  2265. * Since we process the log intent items in normal transactions, they
  2266. * will be removed at some point after the commit. This prevents us
  2267. * from just walking down the list processing each one. We'll use a
  2268. * flag in the intent item to skip those that we've already processed
  2269. * and use the AIL iteration mechanism's generation count to try to
  2270. * speed this up at least a bit.
  2271. *
  2272. * When we start, we know that the intents are the only things in the
  2273. * AIL. As we process them, however, other items are added to the
  2274. * AIL.
  2275. */
  2276. STATIC int
  2277. xlog_recover_process_intents(
  2278. struct xlog *log)
  2279. {
  2280. LIST_HEAD(capture_list);
  2281. struct xfs_ail_cursor cur;
  2282. struct xfs_log_item *lip;
  2283. struct xfs_ail *ailp;
  2284. int error = 0;
  2285. #if defined(DEBUG) || defined(XFS_WARN)
  2286. xfs_lsn_t last_lsn;
  2287. #endif
  2288. ailp = log->l_ailp;
  2289. spin_lock(&ailp->ail_lock);
  2290. #if defined(DEBUG) || defined(XFS_WARN)
  2291. last_lsn = xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block);
  2292. #endif
  2293. for (lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  2294. lip != NULL;
  2295. lip = xfs_trans_ail_cursor_next(ailp, &cur)) {
  2296. /*
  2297. * We're done when we see something other than an intent.
  2298. * There should be no intents left in the AIL now.
  2299. */
  2300. if (!xlog_item_is_intent(lip)) {
  2301. #ifdef DEBUG
  2302. for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
  2303. ASSERT(!xlog_item_is_intent(lip));
  2304. #endif
  2305. break;
  2306. }
  2307. /*
  2308. * We should never see a redo item with a LSN higher than
  2309. * the last transaction we found in the log at the start
  2310. * of recovery.
  2311. */
  2312. ASSERT(XFS_LSN_CMP(last_lsn, lip->li_lsn) >= 0);
  2313. /*
  2314. * NOTE: If your intent processing routine can create more
  2315. * deferred ops, you /must/ attach them to the capture list in
  2316. * the recover routine or else those subsequent intents will be
  2317. * replayed in the wrong order!
  2318. */
  2319. spin_unlock(&ailp->ail_lock);
  2320. error = lip->li_ops->iop_recover(lip, &capture_list);
  2321. spin_lock(&ailp->ail_lock);
  2322. if (error)
  2323. break;
  2324. }
  2325. xfs_trans_ail_cursor_done(&cur);
  2326. spin_unlock(&ailp->ail_lock);
  2327. if (error)
  2328. goto err;
  2329. error = xlog_finish_defer_ops(log->l_mp, &capture_list);
  2330. if (error)
  2331. goto err;
  2332. return 0;
  2333. err:
  2334. xlog_abort_defer_ops(log->l_mp, &capture_list);
  2335. return error;
  2336. }
  2337. /*
  2338. * A cancel occurs when the mount has failed and we're bailing out.
  2339. * Release all pending log intent items so they don't pin the AIL.
  2340. */
  2341. STATIC void
  2342. xlog_recover_cancel_intents(
  2343. struct xlog *log)
  2344. {
  2345. struct xfs_log_item *lip;
  2346. struct xfs_ail_cursor cur;
  2347. struct xfs_ail *ailp;
  2348. ailp = log->l_ailp;
  2349. spin_lock(&ailp->ail_lock);
  2350. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  2351. while (lip != NULL) {
  2352. /*
  2353. * We're done when we see something other than an intent.
  2354. * There should be no intents left in the AIL now.
  2355. */
  2356. if (!xlog_item_is_intent(lip)) {
  2357. #ifdef DEBUG
  2358. for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
  2359. ASSERT(!xlog_item_is_intent(lip));
  2360. #endif
  2361. break;
  2362. }
  2363. spin_unlock(&ailp->ail_lock);
  2364. lip->li_ops->iop_release(lip);
  2365. spin_lock(&ailp->ail_lock);
  2366. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  2367. }
  2368. xfs_trans_ail_cursor_done(&cur);
  2369. spin_unlock(&ailp->ail_lock);
  2370. }
  2371. /*
  2372. * This routine performs a transaction to null out a bad inode pointer
  2373. * in an agi unlinked inode hash bucket.
  2374. */
  2375. STATIC void
  2376. xlog_recover_clear_agi_bucket(
  2377. xfs_mount_t *mp,
  2378. xfs_agnumber_t agno,
  2379. int bucket)
  2380. {
  2381. xfs_trans_t *tp;
  2382. xfs_agi_t *agi;
  2383. xfs_buf_t *agibp;
  2384. int offset;
  2385. int error;
  2386. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_clearagi, 0, 0, 0, &tp);
  2387. if (error)
  2388. goto out_error;
  2389. error = xfs_read_agi(mp, tp, agno, &agibp);
  2390. if (error)
  2391. goto out_abort;
  2392. agi = agibp->b_addr;
  2393. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  2394. offset = offsetof(xfs_agi_t, agi_unlinked) +
  2395. (sizeof(xfs_agino_t) * bucket);
  2396. xfs_trans_log_buf(tp, agibp, offset,
  2397. (offset + sizeof(xfs_agino_t) - 1));
  2398. error = xfs_trans_commit(tp);
  2399. if (error)
  2400. goto out_error;
  2401. return;
  2402. out_abort:
  2403. xfs_trans_cancel(tp);
  2404. out_error:
  2405. xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__, agno);
  2406. return;
  2407. }
  2408. STATIC xfs_agino_t
  2409. xlog_recover_process_one_iunlink(
  2410. struct xfs_mount *mp,
  2411. xfs_agnumber_t agno,
  2412. xfs_agino_t agino,
  2413. int bucket)
  2414. {
  2415. struct xfs_buf *ibp;
  2416. struct xfs_dinode *dip;
  2417. struct xfs_inode *ip;
  2418. xfs_ino_t ino;
  2419. int error;
  2420. ino = XFS_AGINO_TO_INO(mp, agno, agino);
  2421. error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
  2422. if (error)
  2423. goto fail;
  2424. /*
  2425. * Get the on disk inode to find the next inode in the bucket.
  2426. */
  2427. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &ibp, 0);
  2428. if (error)
  2429. goto fail_iput;
  2430. xfs_iflags_clear(ip, XFS_IRECOVERY);
  2431. ASSERT(VFS_I(ip)->i_nlink == 0);
  2432. ASSERT(VFS_I(ip)->i_mode != 0);
  2433. /* setup for the next pass */
  2434. agino = be32_to_cpu(dip->di_next_unlinked);
  2435. xfs_buf_relse(ibp);
  2436. /*
  2437. * Prevent any DMAPI event from being sent when the reference on
  2438. * the inode is dropped.
  2439. */
  2440. ip->i_d.di_dmevmask = 0;
  2441. xfs_irele(ip);
  2442. return agino;
  2443. fail_iput:
  2444. xfs_irele(ip);
  2445. fail:
  2446. /*
  2447. * We can't read in the inode this bucket points to, or this inode
  2448. * is messed up. Just ditch this bucket of inodes. We will lose
  2449. * some inodes and space, but at least we won't hang.
  2450. *
  2451. * Call xlog_recover_clear_agi_bucket() to perform a transaction to
  2452. * clear the inode pointer in the bucket.
  2453. */
  2454. xlog_recover_clear_agi_bucket(mp, agno, bucket);
  2455. return NULLAGINO;
  2456. }
  2457. /*
  2458. * Recover AGI unlinked lists
  2459. *
  2460. * This is called during recovery to process any inodes which we unlinked but
  2461. * not freed when the system crashed. These inodes will be on the lists in the
  2462. * AGI blocks. What we do here is scan all the AGIs and fully truncate and free
  2463. * any inodes found on the lists. Each inode is removed from the lists when it
  2464. * has been fully truncated and is freed. The freeing of the inode and its
  2465. * removal from the list must be atomic.
  2466. *
  2467. * If everything we touch in the agi processing loop is already in memory, this
  2468. * loop can hold the cpu for a long time. It runs without lock contention,
  2469. * memory allocation contention, the need wait for IO, etc, and so will run
  2470. * until we either run out of inodes to process, run low on memory or we run out
  2471. * of log space.
  2472. *
  2473. * This behaviour is bad for latency on single CPU and non-preemptible kernels,
  2474. * and can prevent other filesytem work (such as CIL pushes) from running. This
  2475. * can lead to deadlocks if the recovery process runs out of log reservation
  2476. * space. Hence we need to yield the CPU when there is other kernel work
  2477. * scheduled on this CPU to ensure other scheduled work can run without undue
  2478. * latency.
  2479. */
  2480. STATIC void
  2481. xlog_recover_process_iunlinks(
  2482. struct xlog *log)
  2483. {
  2484. xfs_mount_t *mp;
  2485. xfs_agnumber_t agno;
  2486. xfs_agi_t *agi;
  2487. xfs_buf_t *agibp;
  2488. xfs_agino_t agino;
  2489. int bucket;
  2490. int error;
  2491. mp = log->l_mp;
  2492. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  2493. /*
  2494. * Find the agi for this ag.
  2495. */
  2496. error = xfs_read_agi(mp, NULL, agno, &agibp);
  2497. if (error) {
  2498. /*
  2499. * AGI is b0rked. Don't process it.
  2500. *
  2501. * We should probably mark the filesystem as corrupt
  2502. * after we've recovered all the ag's we can....
  2503. */
  2504. continue;
  2505. }
  2506. /*
  2507. * Unlock the buffer so that it can be acquired in the normal
  2508. * course of the transaction to truncate and free each inode.
  2509. * Because we are not racing with anyone else here for the AGI
  2510. * buffer, we don't even need to hold it locked to read the
  2511. * initial unlinked bucket entries out of the buffer. We keep
  2512. * buffer reference though, so that it stays pinned in memory
  2513. * while we need the buffer.
  2514. */
  2515. agi = agibp->b_addr;
  2516. xfs_buf_unlock(agibp);
  2517. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
  2518. agino = be32_to_cpu(agi->agi_unlinked[bucket]);
  2519. while (agino != NULLAGINO) {
  2520. agino = xlog_recover_process_one_iunlink(mp,
  2521. agno, agino, bucket);
  2522. cond_resched();
  2523. }
  2524. }
  2525. xfs_buf_rele(agibp);
  2526. }
  2527. }
  2528. STATIC void
  2529. xlog_unpack_data(
  2530. struct xlog_rec_header *rhead,
  2531. char *dp,
  2532. struct xlog *log)
  2533. {
  2534. int i, j, k;
  2535. for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
  2536. i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
  2537. *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
  2538. dp += BBSIZE;
  2539. }
  2540. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  2541. xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
  2542. for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
  2543. j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  2544. k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  2545. *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
  2546. dp += BBSIZE;
  2547. }
  2548. }
  2549. }
  2550. /*
  2551. * CRC check, unpack and process a log record.
  2552. */
  2553. STATIC int
  2554. xlog_recover_process(
  2555. struct xlog *log,
  2556. struct hlist_head rhash[],
  2557. struct xlog_rec_header *rhead,
  2558. char *dp,
  2559. int pass,
  2560. struct list_head *buffer_list)
  2561. {
  2562. __le32 old_crc = rhead->h_crc;
  2563. __le32 crc;
  2564. crc = xlog_cksum(log, rhead, dp, be32_to_cpu(rhead->h_len));
  2565. /*
  2566. * Nothing else to do if this is a CRC verification pass. Just return
  2567. * if this a record with a non-zero crc. Unfortunately, mkfs always
  2568. * sets old_crc to 0 so we must consider this valid even on v5 supers.
  2569. * Otherwise, return EFSBADCRC on failure so the callers up the stack
  2570. * know precisely what failed.
  2571. */
  2572. if (pass == XLOG_RECOVER_CRCPASS) {
  2573. if (old_crc && crc != old_crc)
  2574. return -EFSBADCRC;
  2575. return 0;
  2576. }
  2577. /*
  2578. * We're in the normal recovery path. Issue a warning if and only if the
  2579. * CRC in the header is non-zero. This is an advisory warning and the
  2580. * zero CRC check prevents warnings from being emitted when upgrading
  2581. * the kernel from one that does not add CRCs by default.
  2582. */
  2583. if (crc != old_crc) {
  2584. if (old_crc || xfs_sb_version_hascrc(&log->l_mp->m_sb)) {
  2585. xfs_alert(log->l_mp,
  2586. "log record CRC mismatch: found 0x%x, expected 0x%x.",
  2587. le32_to_cpu(old_crc),
  2588. le32_to_cpu(crc));
  2589. xfs_hex_dump(dp, 32);
  2590. }
  2591. /*
  2592. * If the filesystem is CRC enabled, this mismatch becomes a
  2593. * fatal log corruption failure.
  2594. */
  2595. if (xfs_sb_version_hascrc(&log->l_mp->m_sb)) {
  2596. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log->l_mp);
  2597. return -EFSCORRUPTED;
  2598. }
  2599. }
  2600. xlog_unpack_data(rhead, dp, log);
  2601. return xlog_recover_process_data(log, rhash, rhead, dp, pass,
  2602. buffer_list);
  2603. }
  2604. STATIC int
  2605. xlog_valid_rec_header(
  2606. struct xlog *log,
  2607. struct xlog_rec_header *rhead,
  2608. xfs_daddr_t blkno,
  2609. int bufsize)
  2610. {
  2611. int hlen;
  2612. if (XFS_IS_CORRUPT(log->l_mp,
  2613. rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM)))
  2614. return -EFSCORRUPTED;
  2615. if (XFS_IS_CORRUPT(log->l_mp,
  2616. (!rhead->h_version ||
  2617. (be32_to_cpu(rhead->h_version) &
  2618. (~XLOG_VERSION_OKBITS))))) {
  2619. xfs_warn(log->l_mp, "%s: unrecognised log version (%d).",
  2620. __func__, be32_to_cpu(rhead->h_version));
  2621. return -EFSCORRUPTED;
  2622. }
  2623. /*
  2624. * LR body must have data (or it wouldn't have been written)
  2625. * and h_len must not be greater than LR buffer size.
  2626. */
  2627. hlen = be32_to_cpu(rhead->h_len);
  2628. if (XFS_IS_CORRUPT(log->l_mp, hlen <= 0 || hlen > bufsize))
  2629. return -EFSCORRUPTED;
  2630. if (XFS_IS_CORRUPT(log->l_mp,
  2631. blkno > log->l_logBBsize || blkno > INT_MAX))
  2632. return -EFSCORRUPTED;
  2633. return 0;
  2634. }
  2635. /*
  2636. * Read the log from tail to head and process the log records found.
  2637. * Handle the two cases where the tail and head are in the same cycle
  2638. * and where the active portion of the log wraps around the end of
  2639. * the physical log separately. The pass parameter is passed through
  2640. * to the routines called to process the data and is not looked at
  2641. * here.
  2642. */
  2643. STATIC int
  2644. xlog_do_recovery_pass(
  2645. struct xlog *log,
  2646. xfs_daddr_t head_blk,
  2647. xfs_daddr_t tail_blk,
  2648. int pass,
  2649. xfs_daddr_t *first_bad) /* out: first bad log rec */
  2650. {
  2651. xlog_rec_header_t *rhead;
  2652. xfs_daddr_t blk_no, rblk_no;
  2653. xfs_daddr_t rhead_blk;
  2654. char *offset;
  2655. char *hbp, *dbp;
  2656. int error = 0, h_size, h_len;
  2657. int error2 = 0;
  2658. int bblks, split_bblks;
  2659. int hblks, split_hblks, wrapped_hblks;
  2660. int i;
  2661. struct hlist_head rhash[XLOG_RHASH_SIZE];
  2662. LIST_HEAD (buffer_list);
  2663. ASSERT(head_blk != tail_blk);
  2664. blk_no = rhead_blk = tail_blk;
  2665. for (i = 0; i < XLOG_RHASH_SIZE; i++)
  2666. INIT_HLIST_HEAD(&rhash[i]);
  2667. /*
  2668. * Read the header of the tail block and get the iclog buffer size from
  2669. * h_size. Use this to tell how many sectors make up the log header.
  2670. */
  2671. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  2672. /*
  2673. * When using variable length iclogs, read first sector of
  2674. * iclog header and extract the header size from it. Get a
  2675. * new hbp that is the correct size.
  2676. */
  2677. hbp = xlog_alloc_buffer(log, 1);
  2678. if (!hbp)
  2679. return -ENOMEM;
  2680. error = xlog_bread(log, tail_blk, 1, hbp, &offset);
  2681. if (error)
  2682. goto bread_err1;
  2683. rhead = (xlog_rec_header_t *)offset;
  2684. /*
  2685. * xfsprogs has a bug where record length is based on lsunit but
  2686. * h_size (iclog size) is hardcoded to 32k. Now that we
  2687. * unconditionally CRC verify the unmount record, this means the
  2688. * log buffer can be too small for the record and cause an
  2689. * overrun.
  2690. *
  2691. * Detect this condition here. Use lsunit for the buffer size as
  2692. * long as this looks like the mkfs case. Otherwise, return an
  2693. * error to avoid a buffer overrun.
  2694. */
  2695. h_size = be32_to_cpu(rhead->h_size);
  2696. h_len = be32_to_cpu(rhead->h_len);
  2697. if (h_len > h_size && h_len <= log->l_mp->m_logbsize &&
  2698. rhead->h_num_logops == cpu_to_be32(1)) {
  2699. xfs_warn(log->l_mp,
  2700. "invalid iclog size (%d bytes), using lsunit (%d bytes)",
  2701. h_size, log->l_mp->m_logbsize);
  2702. h_size = log->l_mp->m_logbsize;
  2703. }
  2704. error = xlog_valid_rec_header(log, rhead, tail_blk, h_size);
  2705. if (error)
  2706. goto bread_err1;
  2707. hblks = xlog_logrec_hblks(log, rhead);
  2708. if (hblks != 1) {
  2709. kmem_free(hbp);
  2710. hbp = xlog_alloc_buffer(log, hblks);
  2711. }
  2712. } else {
  2713. ASSERT(log->l_sectBBsize == 1);
  2714. hblks = 1;
  2715. hbp = xlog_alloc_buffer(log, 1);
  2716. h_size = XLOG_BIG_RECORD_BSIZE;
  2717. }
  2718. if (!hbp)
  2719. return -ENOMEM;
  2720. dbp = xlog_alloc_buffer(log, BTOBB(h_size));
  2721. if (!dbp) {
  2722. kmem_free(hbp);
  2723. return -ENOMEM;
  2724. }
  2725. memset(rhash, 0, sizeof(rhash));
  2726. if (tail_blk > head_blk) {
  2727. /*
  2728. * Perform recovery around the end of the physical log.
  2729. * When the head is not on the same cycle number as the tail,
  2730. * we can't do a sequential recovery.
  2731. */
  2732. while (blk_no < log->l_logBBsize) {
  2733. /*
  2734. * Check for header wrapping around physical end-of-log
  2735. */
  2736. offset = hbp;
  2737. split_hblks = 0;
  2738. wrapped_hblks = 0;
  2739. if (blk_no + hblks <= log->l_logBBsize) {
  2740. /* Read header in one read */
  2741. error = xlog_bread(log, blk_no, hblks, hbp,
  2742. &offset);
  2743. if (error)
  2744. goto bread_err2;
  2745. } else {
  2746. /* This LR is split across physical log end */
  2747. if (blk_no != log->l_logBBsize) {
  2748. /* some data before physical log end */
  2749. ASSERT(blk_no <= INT_MAX);
  2750. split_hblks = log->l_logBBsize - (int)blk_no;
  2751. ASSERT(split_hblks > 0);
  2752. error = xlog_bread(log, blk_no,
  2753. split_hblks, hbp,
  2754. &offset);
  2755. if (error)
  2756. goto bread_err2;
  2757. }
  2758. /*
  2759. * Note: this black magic still works with
  2760. * large sector sizes (non-512) only because:
  2761. * - we increased the buffer size originally
  2762. * by 1 sector giving us enough extra space
  2763. * for the second read;
  2764. * - the log start is guaranteed to be sector
  2765. * aligned;
  2766. * - we read the log end (LR header start)
  2767. * _first_, then the log start (LR header end)
  2768. * - order is important.
  2769. */
  2770. wrapped_hblks = hblks - split_hblks;
  2771. error = xlog_bread_noalign(log, 0,
  2772. wrapped_hblks,
  2773. offset + BBTOB(split_hblks));
  2774. if (error)
  2775. goto bread_err2;
  2776. }
  2777. rhead = (xlog_rec_header_t *)offset;
  2778. error = xlog_valid_rec_header(log, rhead,
  2779. split_hblks ? blk_no : 0, h_size);
  2780. if (error)
  2781. goto bread_err2;
  2782. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  2783. blk_no += hblks;
  2784. /*
  2785. * Read the log record data in multiple reads if it
  2786. * wraps around the end of the log. Note that if the
  2787. * header already wrapped, blk_no could point past the
  2788. * end of the log. The record data is contiguous in
  2789. * that case.
  2790. */
  2791. if (blk_no + bblks <= log->l_logBBsize ||
  2792. blk_no >= log->l_logBBsize) {
  2793. rblk_no = xlog_wrap_logbno(log, blk_no);
  2794. error = xlog_bread(log, rblk_no, bblks, dbp,
  2795. &offset);
  2796. if (error)
  2797. goto bread_err2;
  2798. } else {
  2799. /* This log record is split across the
  2800. * physical end of log */
  2801. offset = dbp;
  2802. split_bblks = 0;
  2803. if (blk_no != log->l_logBBsize) {
  2804. /* some data is before the physical
  2805. * end of log */
  2806. ASSERT(!wrapped_hblks);
  2807. ASSERT(blk_no <= INT_MAX);
  2808. split_bblks =
  2809. log->l_logBBsize - (int)blk_no;
  2810. ASSERT(split_bblks > 0);
  2811. error = xlog_bread(log, blk_no,
  2812. split_bblks, dbp,
  2813. &offset);
  2814. if (error)
  2815. goto bread_err2;
  2816. }
  2817. /*
  2818. * Note: this black magic still works with
  2819. * large sector sizes (non-512) only because:
  2820. * - we increased the buffer size originally
  2821. * by 1 sector giving us enough extra space
  2822. * for the second read;
  2823. * - the log start is guaranteed to be sector
  2824. * aligned;
  2825. * - we read the log end (LR header start)
  2826. * _first_, then the log start (LR header end)
  2827. * - order is important.
  2828. */
  2829. error = xlog_bread_noalign(log, 0,
  2830. bblks - split_bblks,
  2831. offset + BBTOB(split_bblks));
  2832. if (error)
  2833. goto bread_err2;
  2834. }
  2835. error = xlog_recover_process(log, rhash, rhead, offset,
  2836. pass, &buffer_list);
  2837. if (error)
  2838. goto bread_err2;
  2839. blk_no += bblks;
  2840. rhead_blk = blk_no;
  2841. }
  2842. ASSERT(blk_no >= log->l_logBBsize);
  2843. blk_no -= log->l_logBBsize;
  2844. rhead_blk = blk_no;
  2845. }
  2846. /* read first part of physical log */
  2847. while (blk_no < head_blk) {
  2848. error = xlog_bread(log, blk_no, hblks, hbp, &offset);
  2849. if (error)
  2850. goto bread_err2;
  2851. rhead = (xlog_rec_header_t *)offset;
  2852. error = xlog_valid_rec_header(log, rhead, blk_no, h_size);
  2853. if (error)
  2854. goto bread_err2;
  2855. /* blocks in data section */
  2856. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  2857. error = xlog_bread(log, blk_no+hblks, bblks, dbp,
  2858. &offset);
  2859. if (error)
  2860. goto bread_err2;
  2861. error = xlog_recover_process(log, rhash, rhead, offset, pass,
  2862. &buffer_list);
  2863. if (error)
  2864. goto bread_err2;
  2865. blk_no += bblks + hblks;
  2866. rhead_blk = blk_no;
  2867. }
  2868. bread_err2:
  2869. kmem_free(dbp);
  2870. bread_err1:
  2871. kmem_free(hbp);
  2872. /*
  2873. * Submit buffers that have been added from the last record processed,
  2874. * regardless of error status.
  2875. */
  2876. if (!list_empty(&buffer_list))
  2877. error2 = xfs_buf_delwri_submit(&buffer_list);
  2878. if (error && first_bad)
  2879. *first_bad = rhead_blk;
  2880. /*
  2881. * Transactions are freed at commit time but transactions without commit
  2882. * records on disk are never committed. Free any that may be left in the
  2883. * hash table.
  2884. */
  2885. for (i = 0; i < XLOG_RHASH_SIZE; i++) {
  2886. struct hlist_node *tmp;
  2887. struct xlog_recover *trans;
  2888. hlist_for_each_entry_safe(trans, tmp, &rhash[i], r_list)
  2889. xlog_recover_free_trans(trans);
  2890. }
  2891. return error ? error : error2;
  2892. }
  2893. /*
  2894. * Do the recovery of the log. We actually do this in two phases.
  2895. * The two passes are necessary in order to implement the function
  2896. * of cancelling a record written into the log. The first pass
  2897. * determines those things which have been cancelled, and the
  2898. * second pass replays log items normally except for those which
  2899. * have been cancelled. The handling of the replay and cancellations
  2900. * takes place in the log item type specific routines.
  2901. *
  2902. * The table of items which have cancel records in the log is allocated
  2903. * and freed at this level, since only here do we know when all of
  2904. * the log recovery has been completed.
  2905. */
  2906. STATIC int
  2907. xlog_do_log_recovery(
  2908. struct xlog *log,
  2909. xfs_daddr_t head_blk,
  2910. xfs_daddr_t tail_blk)
  2911. {
  2912. int error, i;
  2913. ASSERT(head_blk != tail_blk);
  2914. /*
  2915. * First do a pass to find all of the cancelled buf log items.
  2916. * Store them in the buf_cancel_table for use in the second pass.
  2917. */
  2918. log->l_buf_cancel_table = kmem_zalloc(XLOG_BC_TABLE_SIZE *
  2919. sizeof(struct list_head),
  2920. 0);
  2921. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  2922. INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
  2923. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  2924. XLOG_RECOVER_PASS1, NULL);
  2925. if (error != 0) {
  2926. kmem_free(log->l_buf_cancel_table);
  2927. log->l_buf_cancel_table = NULL;
  2928. return error;
  2929. }
  2930. /*
  2931. * Then do a second pass to actually recover the items in the log.
  2932. * When it is complete free the table of buf cancel items.
  2933. */
  2934. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  2935. XLOG_RECOVER_PASS2, NULL);
  2936. #ifdef DEBUG
  2937. if (!error) {
  2938. int i;
  2939. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  2940. ASSERT(list_empty(&log->l_buf_cancel_table[i]));
  2941. }
  2942. #endif /* DEBUG */
  2943. kmem_free(log->l_buf_cancel_table);
  2944. log->l_buf_cancel_table = NULL;
  2945. return error;
  2946. }
  2947. /*
  2948. * Do the actual recovery
  2949. */
  2950. STATIC int
  2951. xlog_do_recover(
  2952. struct xlog *log,
  2953. xfs_daddr_t head_blk,
  2954. xfs_daddr_t tail_blk)
  2955. {
  2956. struct xfs_mount *mp = log->l_mp;
  2957. struct xfs_buf *bp = mp->m_sb_bp;
  2958. struct xfs_sb *sbp = &mp->m_sb;
  2959. int error;
  2960. trace_xfs_log_recover(log, head_blk, tail_blk);
  2961. /*
  2962. * First replay the images in the log.
  2963. */
  2964. error = xlog_do_log_recovery(log, head_blk, tail_blk);
  2965. if (error)
  2966. return error;
  2967. /*
  2968. * If IO errors happened during recovery, bail out.
  2969. */
  2970. if (XFS_FORCED_SHUTDOWN(mp))
  2971. return -EIO;
  2972. /*
  2973. * We now update the tail_lsn since much of the recovery has completed
  2974. * and there may be space available to use. If there were no extent
  2975. * or iunlinks, we can free up the entire log and set the tail_lsn to
  2976. * be the last_sync_lsn. This was set in xlog_find_tail to be the
  2977. * lsn of the last known good LR on disk. If there are extent frees
  2978. * or iunlinks they will have some entries in the AIL; so we look at
  2979. * the AIL to determine how to set the tail_lsn.
  2980. */
  2981. xlog_assign_tail_lsn(mp);
  2982. /*
  2983. * Now that we've finished replaying all buffer and inode updates,
  2984. * re-read the superblock and reverify it.
  2985. */
  2986. xfs_buf_lock(bp);
  2987. xfs_buf_hold(bp);
  2988. error = _xfs_buf_read(bp, XBF_READ);
  2989. if (error) {
  2990. if (!XFS_FORCED_SHUTDOWN(mp)) {
  2991. xfs_buf_ioerror_alert(bp, __this_address);
  2992. ASSERT(0);
  2993. }
  2994. xfs_buf_relse(bp);
  2995. return error;
  2996. }
  2997. /* Convert superblock from on-disk format */
  2998. xfs_sb_from_disk(sbp, bp->b_addr);
  2999. xfs_buf_relse(bp);
  3000. /* re-initialise in-core superblock and geometry structures */
  3001. xfs_reinit_percpu_counters(mp);
  3002. error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
  3003. if (error) {
  3004. xfs_warn(mp, "Failed post-recovery per-ag init: %d", error);
  3005. return error;
  3006. }
  3007. mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
  3008. xlog_recover_check_summary(log);
  3009. /* Normal transactions can now occur */
  3010. log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
  3011. return 0;
  3012. }
  3013. /*
  3014. * Perform recovery and re-initialize some log variables in xlog_find_tail.
  3015. *
  3016. * Return error or zero.
  3017. */
  3018. int
  3019. xlog_recover(
  3020. struct xlog *log)
  3021. {
  3022. xfs_daddr_t head_blk, tail_blk;
  3023. int error;
  3024. /* find the tail of the log */
  3025. error = xlog_find_tail(log, &head_blk, &tail_blk);
  3026. if (error)
  3027. return error;
  3028. /*
  3029. * The superblock was read before the log was available and thus the LSN
  3030. * could not be verified. Check the superblock LSN against the current
  3031. * LSN now that it's known.
  3032. */
  3033. if (xfs_sb_version_hascrc(&log->l_mp->m_sb) &&
  3034. !xfs_log_check_lsn(log->l_mp, log->l_mp->m_sb.sb_lsn))
  3035. return -EINVAL;
  3036. if (tail_blk != head_blk) {
  3037. /* There used to be a comment here:
  3038. *
  3039. * disallow recovery on read-only mounts. note -- mount
  3040. * checks for ENOSPC and turns it into an intelligent
  3041. * error message.
  3042. * ...but this is no longer true. Now, unless you specify
  3043. * NORECOVERY (in which case this function would never be
  3044. * called), we just go ahead and recover. We do this all
  3045. * under the vfs layer, so we can get away with it unless
  3046. * the device itself is read-only, in which case we fail.
  3047. */
  3048. if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
  3049. return error;
  3050. }
  3051. /*
  3052. * Version 5 superblock log feature mask validation. We know the
  3053. * log is dirty so check if there are any unknown log features
  3054. * in what we need to recover. If there are unknown features
  3055. * (e.g. unsupported transactions, then simply reject the
  3056. * attempt at recovery before touching anything.
  3057. */
  3058. if (XFS_SB_VERSION_NUM(&log->l_mp->m_sb) == XFS_SB_VERSION_5 &&
  3059. xfs_sb_has_incompat_log_feature(&log->l_mp->m_sb,
  3060. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
  3061. xfs_warn(log->l_mp,
  3062. "Superblock has unknown incompatible log features (0x%x) enabled.",
  3063. (log->l_mp->m_sb.sb_features_log_incompat &
  3064. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
  3065. xfs_warn(log->l_mp,
  3066. "The log can not be fully and/or safely recovered by this kernel.");
  3067. xfs_warn(log->l_mp,
  3068. "Please recover the log on a kernel that supports the unknown features.");
  3069. return -EINVAL;
  3070. }
  3071. /*
  3072. * Delay log recovery if the debug hook is set. This is debug
  3073. * instrumention to coordinate simulation of I/O failures with
  3074. * log recovery.
  3075. */
  3076. if (xfs_globals.log_recovery_delay) {
  3077. xfs_notice(log->l_mp,
  3078. "Delaying log recovery for %d seconds.",
  3079. xfs_globals.log_recovery_delay);
  3080. msleep(xfs_globals.log_recovery_delay * 1000);
  3081. }
  3082. xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
  3083. log->l_mp->m_logname ? log->l_mp->m_logname
  3084. : "internal");
  3085. error = xlog_do_recover(log, head_blk, tail_blk);
  3086. log->l_flags |= XLOG_RECOVERY_NEEDED;
  3087. }
  3088. return error;
  3089. }
  3090. /*
  3091. * In the first part of recovery we replay inodes and buffers and build
  3092. * up the list of extent free items which need to be processed. Here
  3093. * we process the extent free items and clean up the on disk unlinked
  3094. * inode lists. This is separated from the first part of recovery so
  3095. * that the root and real-time bitmap inodes can be read in from disk in
  3096. * between the two stages. This is necessary so that we can free space
  3097. * in the real-time portion of the file system.
  3098. */
  3099. int
  3100. xlog_recover_finish(
  3101. struct xlog *log)
  3102. {
  3103. /*
  3104. * Now we're ready to do the transactions needed for the
  3105. * rest of recovery. Start with completing all the extent
  3106. * free intent records and then process the unlinked inode
  3107. * lists. At this point, we essentially run in normal mode
  3108. * except that we're still performing recovery actions
  3109. * rather than accepting new requests.
  3110. */
  3111. if (log->l_flags & XLOG_RECOVERY_NEEDED) {
  3112. int error;
  3113. error = xlog_recover_process_intents(log);
  3114. if (error) {
  3115. /*
  3116. * Cancel all the unprocessed intent items now so that
  3117. * we don't leave them pinned in the AIL. This can
  3118. * cause the AIL to livelock on the pinned item if
  3119. * anyone tries to push the AIL (inode reclaim does
  3120. * this) before we get around to xfs_log_mount_cancel.
  3121. */
  3122. xlog_recover_cancel_intents(log);
  3123. xfs_alert(log->l_mp, "Failed to recover intents");
  3124. return error;
  3125. }
  3126. /*
  3127. * Sync the log to get all the intents out of the AIL.
  3128. * This isn't absolutely necessary, but it helps in
  3129. * case the unlink transactions would have problems
  3130. * pushing the intents out of the way.
  3131. */
  3132. xfs_log_force(log->l_mp, XFS_LOG_SYNC);
  3133. xlog_recover_process_iunlinks(log);
  3134. xlog_recover_check_summary(log);
  3135. xfs_notice(log->l_mp, "Ending recovery (logdev: %s)",
  3136. log->l_mp->m_logname ? log->l_mp->m_logname
  3137. : "internal");
  3138. log->l_flags &= ~XLOG_RECOVERY_NEEDED;
  3139. } else {
  3140. xfs_info(log->l_mp, "Ending clean mount");
  3141. }
  3142. return 0;
  3143. }
  3144. void
  3145. xlog_recover_cancel(
  3146. struct xlog *log)
  3147. {
  3148. if (log->l_flags & XLOG_RECOVERY_NEEDED)
  3149. xlog_recover_cancel_intents(log);
  3150. }
  3151. #if defined(DEBUG)
  3152. /*
  3153. * Read all of the agf and agi counters and check that they
  3154. * are consistent with the superblock counters.
  3155. */
  3156. STATIC void
  3157. xlog_recover_check_summary(
  3158. struct xlog *log)
  3159. {
  3160. xfs_mount_t *mp;
  3161. xfs_buf_t *agfbp;
  3162. xfs_buf_t *agibp;
  3163. xfs_agnumber_t agno;
  3164. uint64_t freeblks;
  3165. uint64_t itotal;
  3166. uint64_t ifree;
  3167. int error;
  3168. mp = log->l_mp;
  3169. freeblks = 0LL;
  3170. itotal = 0LL;
  3171. ifree = 0LL;
  3172. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  3173. error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
  3174. if (error) {
  3175. xfs_alert(mp, "%s agf read failed agno %d error %d",
  3176. __func__, agno, error);
  3177. } else {
  3178. struct xfs_agf *agfp = agfbp->b_addr;
  3179. freeblks += be32_to_cpu(agfp->agf_freeblks) +
  3180. be32_to_cpu(agfp->agf_flcount);
  3181. xfs_buf_relse(agfbp);
  3182. }
  3183. error = xfs_read_agi(mp, NULL, agno, &agibp);
  3184. if (error) {
  3185. xfs_alert(mp, "%s agi read failed agno %d error %d",
  3186. __func__, agno, error);
  3187. } else {
  3188. struct xfs_agi *agi = agibp->b_addr;
  3189. itotal += be32_to_cpu(agi->agi_count);
  3190. ifree += be32_to_cpu(agi->agi_freecount);
  3191. xfs_buf_relse(agibp);
  3192. }
  3193. }
  3194. }
  3195. #endif /* DEBUG */