xfs_log_recover.c 108 KB

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