xfs_log_recover.c 128 KB

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