xfs_log_recover.c 127 KB

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