xfs_log_recover.c 161 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_trans.h"
  31. #include "xfs_log.h"
  32. #include "xfs_log_priv.h"
  33. #include "xfs_log_recover.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_extfree_item.h"
  36. #include "xfs_trans_priv.h"
  37. #include "xfs_alloc.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_quota.h"
  40. #include "xfs_cksum.h"
  41. #include "xfs_trace.h"
  42. #include "xfs_icache.h"
  43. #include "xfs_bmap_btree.h"
  44. #include "xfs_error.h"
  45. #include "xfs_dir2.h"
  46. #include "xfs_rmap_item.h"
  47. #include "xfs_buf_item.h"
  48. #include "xfs_refcount_item.h"
  49. #include "xfs_bmap_item.h"
  50. #define BLK_AVG(blk1, blk2) ((blk1+blk2) >> 1)
  51. STATIC int
  52. xlog_find_zeroed(
  53. struct xlog *,
  54. xfs_daddr_t *);
  55. STATIC int
  56. xlog_clear_stale_blocks(
  57. struct xlog *,
  58. xfs_lsn_t);
  59. #if defined(DEBUG)
  60. STATIC void
  61. xlog_recover_check_summary(
  62. struct xlog *);
  63. #else
  64. #define xlog_recover_check_summary(log)
  65. #endif
  66. STATIC int
  67. xlog_do_recovery_pass(
  68. struct xlog *, xfs_daddr_t, xfs_daddr_t, int, xfs_daddr_t *);
  69. /*
  70. * This structure is used during recovery to record the buf log items which
  71. * have been canceled and should not be replayed.
  72. */
  73. struct xfs_buf_cancel {
  74. xfs_daddr_t bc_blkno;
  75. uint bc_len;
  76. int bc_refcount;
  77. struct list_head bc_list;
  78. };
  79. /*
  80. * Sector aligned buffer routines for buffer create/read/write/access
  81. */
  82. /*
  83. * Verify the given count of basic blocks is valid number of blocks
  84. * to specify for an operation involving the given XFS log buffer.
  85. * Returns nonzero if the count is valid, 0 otherwise.
  86. */
  87. static inline int
  88. xlog_buf_bbcount_valid(
  89. struct xlog *log,
  90. int bbcount)
  91. {
  92. return bbcount > 0 && bbcount <= log->l_logBBsize;
  93. }
  94. /*
  95. * Allocate a buffer to hold log data. The buffer needs to be able
  96. * to map to a range of nbblks basic blocks at any valid (basic
  97. * block) offset within the log.
  98. */
  99. STATIC xfs_buf_t *
  100. xlog_get_bp(
  101. struct xlog *log,
  102. int nbblks)
  103. {
  104. struct xfs_buf *bp;
  105. if (!xlog_buf_bbcount_valid(log, nbblks)) {
  106. xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
  107. nbblks);
  108. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
  109. return NULL;
  110. }
  111. /*
  112. * We do log I/O in units of log sectors (a power-of-2
  113. * multiple of the basic block size), so we round up the
  114. * requested size to accommodate the basic blocks required
  115. * for complete log sectors.
  116. *
  117. * In addition, the buffer may be used for a non-sector-
  118. * aligned block offset, in which case an I/O of the
  119. * requested size could extend beyond the end of the
  120. * buffer. If the requested size is only 1 basic block it
  121. * will never straddle a sector boundary, so this won't be
  122. * an issue. Nor will this be a problem if the log I/O is
  123. * done in basic blocks (sector size 1). But otherwise we
  124. * extend the buffer by one extra log sector to ensure
  125. * there's space to accommodate this possibility.
  126. */
  127. if (nbblks > 1 && log->l_sectBBsize > 1)
  128. nbblks += log->l_sectBBsize;
  129. nbblks = round_up(nbblks, log->l_sectBBsize);
  130. bp = xfs_buf_get_uncached(log->l_mp->m_logdev_targp, nbblks, 0);
  131. if (bp)
  132. xfs_buf_unlock(bp);
  133. return bp;
  134. }
  135. STATIC void
  136. xlog_put_bp(
  137. xfs_buf_t *bp)
  138. {
  139. xfs_buf_free(bp);
  140. }
  141. /*
  142. * Return the address of the start of the given block number's data
  143. * in a log buffer. The buffer covers a log sector-aligned region.
  144. */
  145. STATIC char *
  146. xlog_align(
  147. struct xlog *log,
  148. xfs_daddr_t blk_no,
  149. int nbblks,
  150. struct xfs_buf *bp)
  151. {
  152. xfs_daddr_t offset = blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1);
  153. ASSERT(offset + nbblks <= bp->b_length);
  154. return bp->b_addr + BBTOB(offset);
  155. }
  156. /*
  157. * nbblks should be uint, but oh well. Just want to catch that 32-bit length.
  158. */
  159. STATIC int
  160. xlog_bread_noalign(
  161. struct xlog *log,
  162. xfs_daddr_t blk_no,
  163. int nbblks,
  164. struct xfs_buf *bp)
  165. {
  166. int error;
  167. if (!xlog_buf_bbcount_valid(log, nbblks)) {
  168. xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
  169. nbblks);
  170. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
  171. return -EFSCORRUPTED;
  172. }
  173. blk_no = round_down(blk_no, log->l_sectBBsize);
  174. nbblks = round_up(nbblks, log->l_sectBBsize);
  175. ASSERT(nbblks > 0);
  176. ASSERT(nbblks <= bp->b_length);
  177. XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
  178. bp->b_flags |= XBF_READ;
  179. bp->b_io_length = nbblks;
  180. bp->b_error = 0;
  181. error = xfs_buf_submit_wait(bp);
  182. if (error && !XFS_FORCED_SHUTDOWN(log->l_mp))
  183. xfs_buf_ioerror_alert(bp, __func__);
  184. return error;
  185. }
  186. STATIC int
  187. xlog_bread(
  188. struct xlog *log,
  189. xfs_daddr_t blk_no,
  190. int nbblks,
  191. struct xfs_buf *bp,
  192. char **offset)
  193. {
  194. int error;
  195. error = xlog_bread_noalign(log, blk_no, nbblks, bp);
  196. if (error)
  197. return error;
  198. *offset = xlog_align(log, blk_no, nbblks, bp);
  199. return 0;
  200. }
  201. /*
  202. * Read at an offset into the buffer. Returns with the buffer in it's original
  203. * state regardless of the result of the read.
  204. */
  205. STATIC int
  206. xlog_bread_offset(
  207. struct xlog *log,
  208. xfs_daddr_t blk_no, /* block to read from */
  209. int nbblks, /* blocks to read */
  210. struct xfs_buf *bp,
  211. char *offset)
  212. {
  213. char *orig_offset = bp->b_addr;
  214. int orig_len = BBTOB(bp->b_length);
  215. int error, error2;
  216. error = xfs_buf_associate_memory(bp, offset, BBTOB(nbblks));
  217. if (error)
  218. return error;
  219. error = xlog_bread_noalign(log, blk_no, nbblks, bp);
  220. /* must reset buffer pointer even on error */
  221. error2 = xfs_buf_associate_memory(bp, orig_offset, orig_len);
  222. if (error)
  223. return error;
  224. return error2;
  225. }
  226. /*
  227. * Write out the buffer at the given block for the given number of blocks.
  228. * The buffer is kept locked across the write and is returned locked.
  229. * This can only be used for synchronous log writes.
  230. */
  231. STATIC int
  232. xlog_bwrite(
  233. struct xlog *log,
  234. xfs_daddr_t blk_no,
  235. int nbblks,
  236. struct xfs_buf *bp)
  237. {
  238. int error;
  239. if (!xlog_buf_bbcount_valid(log, nbblks)) {
  240. xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
  241. nbblks);
  242. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
  243. return -EFSCORRUPTED;
  244. }
  245. blk_no = round_down(blk_no, log->l_sectBBsize);
  246. nbblks = round_up(nbblks, log->l_sectBBsize);
  247. ASSERT(nbblks > 0);
  248. ASSERT(nbblks <= bp->b_length);
  249. XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
  250. xfs_buf_hold(bp);
  251. xfs_buf_lock(bp);
  252. bp->b_io_length = nbblks;
  253. bp->b_error = 0;
  254. error = xfs_bwrite(bp);
  255. if (error)
  256. xfs_buf_ioerror_alert(bp, __func__);
  257. xfs_buf_relse(bp);
  258. return error;
  259. }
  260. #ifdef DEBUG
  261. /*
  262. * dump debug superblock and log record information
  263. */
  264. STATIC void
  265. xlog_header_check_dump(
  266. xfs_mount_t *mp,
  267. xlog_rec_header_t *head)
  268. {
  269. xfs_debug(mp, "%s: SB : uuid = %pU, fmt = %d",
  270. __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
  271. xfs_debug(mp, " log : uuid = %pU, fmt = %d",
  272. &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
  273. }
  274. #else
  275. #define xlog_header_check_dump(mp, head)
  276. #endif
  277. /*
  278. * check log record header for recovery
  279. */
  280. STATIC int
  281. xlog_header_check_recover(
  282. xfs_mount_t *mp,
  283. xlog_rec_header_t *head)
  284. {
  285. ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
  286. /*
  287. * IRIX doesn't write the h_fmt field and leaves it zeroed
  288. * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
  289. * a dirty log created in IRIX.
  290. */
  291. if (unlikely(head->h_fmt != cpu_to_be32(XLOG_FMT))) {
  292. xfs_warn(mp,
  293. "dirty log written in incompatible format - can't recover");
  294. xlog_header_check_dump(mp, head);
  295. XFS_ERROR_REPORT("xlog_header_check_recover(1)",
  296. XFS_ERRLEVEL_HIGH, mp);
  297. return -EFSCORRUPTED;
  298. } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
  299. xfs_warn(mp,
  300. "dirty log entry has mismatched uuid - can't recover");
  301. xlog_header_check_dump(mp, head);
  302. XFS_ERROR_REPORT("xlog_header_check_recover(2)",
  303. XFS_ERRLEVEL_HIGH, mp);
  304. return -EFSCORRUPTED;
  305. }
  306. return 0;
  307. }
  308. /*
  309. * read the head block of the log and check the header
  310. */
  311. STATIC int
  312. xlog_header_check_mount(
  313. xfs_mount_t *mp,
  314. xlog_rec_header_t *head)
  315. {
  316. ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
  317. if (uuid_is_null(&head->h_fs_uuid)) {
  318. /*
  319. * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
  320. * h_fs_uuid is null, we assume this log was last mounted
  321. * by IRIX and continue.
  322. */
  323. xfs_warn(mp, "null uuid in log - IRIX style log");
  324. } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
  325. xfs_warn(mp, "log has mismatched uuid - can't recover");
  326. xlog_header_check_dump(mp, head);
  327. XFS_ERROR_REPORT("xlog_header_check_mount",
  328. XFS_ERRLEVEL_HIGH, mp);
  329. return -EFSCORRUPTED;
  330. }
  331. return 0;
  332. }
  333. STATIC void
  334. xlog_recover_iodone(
  335. struct xfs_buf *bp)
  336. {
  337. if (bp->b_error) {
  338. /*
  339. * We're not going to bother about retrying
  340. * this during recovery. One strike!
  341. */
  342. if (!XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  343. xfs_buf_ioerror_alert(bp, __func__);
  344. xfs_force_shutdown(bp->b_target->bt_mount,
  345. SHUTDOWN_META_IO_ERROR);
  346. }
  347. }
  348. /*
  349. * On v5 supers, a bli could be attached to update the metadata LSN.
  350. * Clean it up.
  351. */
  352. if (bp->b_fspriv)
  353. xfs_buf_item_relse(bp);
  354. ASSERT(bp->b_fspriv == NULL);
  355. bp->b_iodone = NULL;
  356. xfs_buf_ioend(bp);
  357. }
  358. /*
  359. * This routine finds (to an approximation) the first block in the physical
  360. * log which contains the given cycle. It uses a binary search algorithm.
  361. * Note that the algorithm can not be perfect because the disk will not
  362. * necessarily be perfect.
  363. */
  364. STATIC int
  365. xlog_find_cycle_start(
  366. struct xlog *log,
  367. struct xfs_buf *bp,
  368. xfs_daddr_t first_blk,
  369. xfs_daddr_t *last_blk,
  370. uint cycle)
  371. {
  372. char *offset;
  373. xfs_daddr_t mid_blk;
  374. xfs_daddr_t end_blk;
  375. uint mid_cycle;
  376. int error;
  377. end_blk = *last_blk;
  378. mid_blk = BLK_AVG(first_blk, end_blk);
  379. while (mid_blk != first_blk && mid_blk != end_blk) {
  380. error = xlog_bread(log, mid_blk, 1, bp, &offset);
  381. if (error)
  382. return error;
  383. mid_cycle = xlog_get_cycle(offset);
  384. if (mid_cycle == cycle)
  385. end_blk = mid_blk; /* last_half_cycle == mid_cycle */
  386. else
  387. first_blk = mid_blk; /* first_half_cycle == mid_cycle */
  388. mid_blk = BLK_AVG(first_blk, end_blk);
  389. }
  390. ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
  391. (mid_blk == end_blk && mid_blk-1 == first_blk));
  392. *last_blk = end_blk;
  393. return 0;
  394. }
  395. /*
  396. * Check that a range of blocks does not contain stop_on_cycle_no.
  397. * Fill in *new_blk with the block offset where such a block is
  398. * found, or with -1 (an invalid block number) if there is no such
  399. * block in the range. The scan needs to occur from front to back
  400. * and the pointer into the region must be updated since a later
  401. * routine will need to perform another test.
  402. */
  403. STATIC int
  404. xlog_find_verify_cycle(
  405. struct xlog *log,
  406. xfs_daddr_t start_blk,
  407. int nbblks,
  408. uint stop_on_cycle_no,
  409. xfs_daddr_t *new_blk)
  410. {
  411. xfs_daddr_t i, j;
  412. uint cycle;
  413. xfs_buf_t *bp;
  414. xfs_daddr_t bufblks;
  415. char *buf = NULL;
  416. int error = 0;
  417. /*
  418. * Greedily allocate a buffer big enough to handle the full
  419. * range of basic blocks we'll be examining. If that fails,
  420. * try a smaller size. We need to be able to read at least
  421. * a log sector, or we're out of luck.
  422. */
  423. bufblks = 1 << ffs(nbblks);
  424. while (bufblks > log->l_logBBsize)
  425. bufblks >>= 1;
  426. while (!(bp = xlog_get_bp(log, bufblks))) {
  427. bufblks >>= 1;
  428. if (bufblks < log->l_sectBBsize)
  429. return -ENOMEM;
  430. }
  431. for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
  432. int bcount;
  433. bcount = min(bufblks, (start_blk + nbblks - i));
  434. error = xlog_bread(log, i, bcount, bp, &buf);
  435. if (error)
  436. goto out;
  437. for (j = 0; j < bcount; j++) {
  438. cycle = xlog_get_cycle(buf);
  439. if (cycle == stop_on_cycle_no) {
  440. *new_blk = i+j;
  441. goto out;
  442. }
  443. buf += BBSIZE;
  444. }
  445. }
  446. *new_blk = -1;
  447. out:
  448. xlog_put_bp(bp);
  449. return error;
  450. }
  451. /*
  452. * Potentially backup over partial log record write.
  453. *
  454. * In the typical case, last_blk is the number of the block directly after
  455. * a good log record. Therefore, we subtract one to get the block number
  456. * of the last block in the given buffer. extra_bblks contains the number
  457. * of blocks we would have read on a previous read. This happens when the
  458. * last log record is split over the end of the physical log.
  459. *
  460. * extra_bblks is the number of blocks potentially verified on a previous
  461. * call to this routine.
  462. */
  463. STATIC int
  464. xlog_find_verify_log_record(
  465. struct xlog *log,
  466. xfs_daddr_t start_blk,
  467. xfs_daddr_t *last_blk,
  468. int extra_bblks)
  469. {
  470. xfs_daddr_t i;
  471. xfs_buf_t *bp;
  472. char *offset = NULL;
  473. xlog_rec_header_t *head = NULL;
  474. int error = 0;
  475. int smallmem = 0;
  476. int num_blks = *last_blk - start_blk;
  477. int xhdrs;
  478. ASSERT(start_blk != 0 || *last_blk != start_blk);
  479. if (!(bp = xlog_get_bp(log, num_blks))) {
  480. if (!(bp = xlog_get_bp(log, 1)))
  481. return -ENOMEM;
  482. smallmem = 1;
  483. } else {
  484. error = xlog_bread(log, start_blk, num_blks, bp, &offset);
  485. if (error)
  486. goto out;
  487. offset += ((num_blks - 1) << BBSHIFT);
  488. }
  489. for (i = (*last_blk) - 1; i >= 0; i--) {
  490. if (i < start_blk) {
  491. /* valid log record not found */
  492. xfs_warn(log->l_mp,
  493. "Log inconsistent (didn't find previous header)");
  494. ASSERT(0);
  495. error = -EIO;
  496. goto out;
  497. }
  498. if (smallmem) {
  499. error = xlog_bread(log, i, 1, bp, &offset);
  500. if (error)
  501. goto out;
  502. }
  503. head = (xlog_rec_header_t *)offset;
  504. if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
  505. break;
  506. if (!smallmem)
  507. offset -= BBSIZE;
  508. }
  509. /*
  510. * We hit the beginning of the physical log & still no header. Return
  511. * to caller. If caller can handle a return of -1, then this routine
  512. * will be called again for the end of the physical log.
  513. */
  514. if (i == -1) {
  515. error = 1;
  516. goto out;
  517. }
  518. /*
  519. * We have the final block of the good log (the first block
  520. * of the log record _before_ the head. So we check the uuid.
  521. */
  522. if ((error = xlog_header_check_mount(log->l_mp, head)))
  523. goto out;
  524. /*
  525. * We may have found a log record header before we expected one.
  526. * last_blk will be the 1st block # with a given cycle #. We may end
  527. * up reading an entire log record. In this case, we don't want to
  528. * reset last_blk. Only when last_blk points in the middle of a log
  529. * record do we update last_blk.
  530. */
  531. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  532. uint h_size = be32_to_cpu(head->h_size);
  533. xhdrs = h_size / XLOG_HEADER_CYCLE_SIZE;
  534. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  535. xhdrs++;
  536. } else {
  537. xhdrs = 1;
  538. }
  539. if (*last_blk - i + extra_bblks !=
  540. BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
  541. *last_blk = i;
  542. out:
  543. xlog_put_bp(bp);
  544. return error;
  545. }
  546. /*
  547. * Head is defined to be the point of the log where the next log write
  548. * could go. This means that incomplete LR writes at the end are
  549. * eliminated when calculating the head. We aren't guaranteed that previous
  550. * LR have complete transactions. We only know that a cycle number of
  551. * current cycle number -1 won't be present in the log if we start writing
  552. * from our current block number.
  553. *
  554. * last_blk contains the block number of the first block with a given
  555. * cycle number.
  556. *
  557. * Return: zero if normal, non-zero if error.
  558. */
  559. STATIC int
  560. xlog_find_head(
  561. struct xlog *log,
  562. xfs_daddr_t *return_head_blk)
  563. {
  564. xfs_buf_t *bp;
  565. char *offset;
  566. xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
  567. int num_scan_bblks;
  568. uint first_half_cycle, last_half_cycle;
  569. uint stop_on_cycle;
  570. int error, log_bbnum = log->l_logBBsize;
  571. /* Is the end of the log device zeroed? */
  572. error = xlog_find_zeroed(log, &first_blk);
  573. if (error < 0) {
  574. xfs_warn(log->l_mp, "empty log check failed");
  575. return error;
  576. }
  577. if (error == 1) {
  578. *return_head_blk = first_blk;
  579. /* Is the whole lot zeroed? */
  580. if (!first_blk) {
  581. /* Linux XFS shouldn't generate totally zeroed logs -
  582. * mkfs etc write a dummy unmount record to a fresh
  583. * log so we can store the uuid in there
  584. */
  585. xfs_warn(log->l_mp, "totally zeroed log");
  586. }
  587. return 0;
  588. }
  589. first_blk = 0; /* get cycle # of 1st block */
  590. bp = xlog_get_bp(log, 1);
  591. if (!bp)
  592. return -ENOMEM;
  593. error = xlog_bread(log, 0, 1, bp, &offset);
  594. if (error)
  595. goto bp_err;
  596. first_half_cycle = xlog_get_cycle(offset);
  597. last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
  598. error = xlog_bread(log, last_blk, 1, bp, &offset);
  599. if (error)
  600. goto bp_err;
  601. last_half_cycle = xlog_get_cycle(offset);
  602. ASSERT(last_half_cycle != 0);
  603. /*
  604. * If the 1st half cycle number is equal to the last half cycle number,
  605. * then the entire log is stamped with the same cycle number. In this
  606. * case, head_blk can't be set to zero (which makes sense). The below
  607. * math doesn't work out properly with head_blk equal to zero. Instead,
  608. * we set it to log_bbnum which is an invalid block number, but this
  609. * value makes the math correct. If head_blk doesn't changed through
  610. * all the tests below, *head_blk is set to zero at the very end rather
  611. * than log_bbnum. In a sense, log_bbnum and zero are the same block
  612. * in a circular file.
  613. */
  614. if (first_half_cycle == last_half_cycle) {
  615. /*
  616. * In this case we believe that the entire log should have
  617. * cycle number last_half_cycle. We need to scan backwards
  618. * from the end verifying that there are no holes still
  619. * containing last_half_cycle - 1. If we find such a hole,
  620. * then the start of that hole will be the new head. The
  621. * simple case looks like
  622. * x | x ... | x - 1 | x
  623. * Another case that fits this picture would be
  624. * x | x + 1 | x ... | x
  625. * In this case the head really is somewhere at the end of the
  626. * log, as one of the latest writes at the beginning was
  627. * incomplete.
  628. * One more case is
  629. * x | x + 1 | x ... | x - 1 | x
  630. * This is really the combination of the above two cases, and
  631. * the head has to end up at the start of the x-1 hole at the
  632. * end of the log.
  633. *
  634. * In the 256k log case, we will read from the beginning to the
  635. * end of the log and search for cycle numbers equal to x-1.
  636. * We don't worry about the x+1 blocks that we encounter,
  637. * because we know that they cannot be the head since the log
  638. * started with x.
  639. */
  640. head_blk = log_bbnum;
  641. stop_on_cycle = last_half_cycle - 1;
  642. } else {
  643. /*
  644. * In this case we want to find the first block with cycle
  645. * number matching last_half_cycle. We expect the log to be
  646. * some variation on
  647. * x + 1 ... | x ... | x
  648. * The first block with cycle number x (last_half_cycle) will
  649. * be where the new head belongs. First we do a binary search
  650. * for the first occurrence of last_half_cycle. The binary
  651. * search may not be totally accurate, so then we scan back
  652. * from there looking for occurrences of last_half_cycle before
  653. * us. If that backwards scan wraps around the beginning of
  654. * the log, then we look for occurrences of last_half_cycle - 1
  655. * at the end of the log. The cases we're looking for look
  656. * like
  657. * v binary search stopped here
  658. * x + 1 ... | x | x + 1 | x ... | x
  659. * ^ but we want to locate this spot
  660. * or
  661. * <---------> less than scan distance
  662. * x + 1 ... | x ... | x - 1 | x
  663. * ^ we want to locate this spot
  664. */
  665. stop_on_cycle = last_half_cycle;
  666. if ((error = xlog_find_cycle_start(log, bp, first_blk,
  667. &head_blk, last_half_cycle)))
  668. goto bp_err;
  669. }
  670. /*
  671. * Now validate the answer. Scan back some number of maximum possible
  672. * blocks and make sure each one has the expected cycle number. The
  673. * maximum is determined by the total possible amount of buffering
  674. * in the in-core log. The following number can be made tighter if
  675. * we actually look at the block size of the filesystem.
  676. */
  677. num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
  678. if (head_blk >= num_scan_bblks) {
  679. /*
  680. * We are guaranteed that the entire check can be performed
  681. * in one buffer.
  682. */
  683. start_blk = head_blk - num_scan_bblks;
  684. if ((error = xlog_find_verify_cycle(log,
  685. start_blk, num_scan_bblks,
  686. stop_on_cycle, &new_blk)))
  687. goto bp_err;
  688. if (new_blk != -1)
  689. head_blk = new_blk;
  690. } else { /* need to read 2 parts of log */
  691. /*
  692. * We are going to scan backwards in the log in two parts.
  693. * First we scan the physical end of the log. In this part
  694. * of the log, we are looking for blocks with cycle number
  695. * last_half_cycle - 1.
  696. * If we find one, then we know that the log starts there, as
  697. * we've found a hole that didn't get written in going around
  698. * the end of the physical log. The simple case for this is
  699. * x + 1 ... | x ... | x - 1 | x
  700. * <---------> less than scan distance
  701. * If all of the blocks at the end of the log have cycle number
  702. * last_half_cycle, then we check the blocks at the start of
  703. * the log looking for occurrences of last_half_cycle. If we
  704. * find one, then our current estimate for the location of the
  705. * first occurrence of last_half_cycle is wrong and we move
  706. * back to the hole we've found. This case looks like
  707. * x + 1 ... | x | x + 1 | x ...
  708. * ^ binary search stopped here
  709. * Another case we need to handle that only occurs in 256k
  710. * logs is
  711. * x + 1 ... | x ... | x+1 | x ...
  712. * ^ binary search stops here
  713. * In a 256k log, the scan at the end of the log will see the
  714. * x + 1 blocks. We need to skip past those since that is
  715. * certainly not the head of the log. By searching for
  716. * last_half_cycle-1 we accomplish that.
  717. */
  718. ASSERT(head_blk <= INT_MAX &&
  719. (xfs_daddr_t) num_scan_bblks >= head_blk);
  720. start_blk = log_bbnum - (num_scan_bblks - head_blk);
  721. if ((error = xlog_find_verify_cycle(log, start_blk,
  722. num_scan_bblks - (int)head_blk,
  723. (stop_on_cycle - 1), &new_blk)))
  724. goto bp_err;
  725. if (new_blk != -1) {
  726. head_blk = new_blk;
  727. goto validate_head;
  728. }
  729. /*
  730. * Scan beginning of log now. The last part of the physical
  731. * log is good. This scan needs to verify that it doesn't find
  732. * the last_half_cycle.
  733. */
  734. start_blk = 0;
  735. ASSERT(head_blk <= INT_MAX);
  736. if ((error = xlog_find_verify_cycle(log,
  737. start_blk, (int)head_blk,
  738. stop_on_cycle, &new_blk)))
  739. goto bp_err;
  740. if (new_blk != -1)
  741. head_blk = new_blk;
  742. }
  743. validate_head:
  744. /*
  745. * Now we need to make sure head_blk is not pointing to a block in
  746. * the middle of a log record.
  747. */
  748. num_scan_bblks = XLOG_REC_SHIFT(log);
  749. if (head_blk >= num_scan_bblks) {
  750. start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
  751. /* start ptr at last block ptr before head_blk */
  752. error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  753. if (error == 1)
  754. error = -EIO;
  755. if (error)
  756. goto bp_err;
  757. } else {
  758. start_blk = 0;
  759. ASSERT(head_blk <= INT_MAX);
  760. error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  761. if (error < 0)
  762. goto bp_err;
  763. if (error == 1) {
  764. /* We hit the beginning of the log during our search */
  765. start_blk = log_bbnum - (num_scan_bblks - head_blk);
  766. new_blk = log_bbnum;
  767. ASSERT(start_blk <= INT_MAX &&
  768. (xfs_daddr_t) log_bbnum-start_blk >= 0);
  769. ASSERT(head_blk <= INT_MAX);
  770. error = xlog_find_verify_log_record(log, start_blk,
  771. &new_blk, (int)head_blk);
  772. if (error == 1)
  773. error = -EIO;
  774. if (error)
  775. goto bp_err;
  776. if (new_blk != log_bbnum)
  777. head_blk = new_blk;
  778. } else if (error)
  779. goto bp_err;
  780. }
  781. xlog_put_bp(bp);
  782. if (head_blk == log_bbnum)
  783. *return_head_blk = 0;
  784. else
  785. *return_head_blk = head_blk;
  786. /*
  787. * When returning here, we have a good block number. Bad block
  788. * means that during a previous crash, we didn't have a clean break
  789. * from cycle number N to cycle number N-1. In this case, we need
  790. * to find the first block with cycle number N-1.
  791. */
  792. return 0;
  793. bp_err:
  794. xlog_put_bp(bp);
  795. if (error)
  796. xfs_warn(log->l_mp, "failed to find log head");
  797. return error;
  798. }
  799. /*
  800. * Seek backwards in the log for log record headers.
  801. *
  802. * Given a starting log block, walk backwards until we find the provided number
  803. * of records or hit the provided tail block. The return value is the number of
  804. * records encountered or a negative error code. The log block and buffer
  805. * pointer of the last record seen are returned in rblk and rhead respectively.
  806. */
  807. STATIC int
  808. xlog_rseek_logrec_hdr(
  809. struct xlog *log,
  810. xfs_daddr_t head_blk,
  811. xfs_daddr_t tail_blk,
  812. int count,
  813. struct xfs_buf *bp,
  814. xfs_daddr_t *rblk,
  815. struct xlog_rec_header **rhead,
  816. bool *wrapped)
  817. {
  818. int i;
  819. int error;
  820. int found = 0;
  821. char *offset = NULL;
  822. xfs_daddr_t end_blk;
  823. *wrapped = false;
  824. /*
  825. * Walk backwards from the head block until we hit the tail or the first
  826. * block in the log.
  827. */
  828. end_blk = head_blk > tail_blk ? tail_blk : 0;
  829. for (i = (int) head_blk - 1; i >= end_blk; i--) {
  830. error = xlog_bread(log, i, 1, bp, &offset);
  831. if (error)
  832. goto out_error;
  833. if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  834. *rblk = i;
  835. *rhead = (struct xlog_rec_header *) offset;
  836. if (++found == count)
  837. break;
  838. }
  839. }
  840. /*
  841. * If we haven't hit the tail block or the log record header count,
  842. * start looking again from the end of the physical log. Note that
  843. * callers can pass head == tail if the tail is not yet known.
  844. */
  845. if (tail_blk >= head_blk && found != count) {
  846. for (i = log->l_logBBsize - 1; i >= (int) tail_blk; i--) {
  847. error = xlog_bread(log, i, 1, bp, &offset);
  848. if (error)
  849. goto out_error;
  850. if (*(__be32 *)offset ==
  851. cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  852. *wrapped = true;
  853. *rblk = i;
  854. *rhead = (struct xlog_rec_header *) offset;
  855. if (++found == count)
  856. break;
  857. }
  858. }
  859. }
  860. return found;
  861. out_error:
  862. return error;
  863. }
  864. /*
  865. * Seek forward in the log for log record headers.
  866. *
  867. * Given head and tail blocks, walk forward from the tail block until we find
  868. * the provided number of records or hit the head block. The return value is the
  869. * number of records encountered or a negative error code. The log block and
  870. * buffer pointer of the last record seen are returned in rblk and rhead
  871. * respectively.
  872. */
  873. STATIC int
  874. xlog_seek_logrec_hdr(
  875. struct xlog *log,
  876. xfs_daddr_t head_blk,
  877. xfs_daddr_t tail_blk,
  878. int count,
  879. struct xfs_buf *bp,
  880. xfs_daddr_t *rblk,
  881. struct xlog_rec_header **rhead,
  882. bool *wrapped)
  883. {
  884. int i;
  885. int error;
  886. int found = 0;
  887. char *offset = NULL;
  888. xfs_daddr_t end_blk;
  889. *wrapped = false;
  890. /*
  891. * Walk forward from the tail block until we hit the head or the last
  892. * block in the log.
  893. */
  894. end_blk = head_blk > tail_blk ? head_blk : log->l_logBBsize - 1;
  895. for (i = (int) tail_blk; i <= end_blk; i++) {
  896. error = xlog_bread(log, i, 1, bp, &offset);
  897. if (error)
  898. goto out_error;
  899. if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  900. *rblk = i;
  901. *rhead = (struct xlog_rec_header *) offset;
  902. if (++found == count)
  903. break;
  904. }
  905. }
  906. /*
  907. * If we haven't hit the head block or the log record header count,
  908. * start looking again from the start of the physical log.
  909. */
  910. if (tail_blk > head_blk && found != count) {
  911. for (i = 0; i < (int) head_blk; i++) {
  912. error = xlog_bread(log, i, 1, bp, &offset);
  913. if (error)
  914. goto out_error;
  915. if (*(__be32 *)offset ==
  916. cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  917. *wrapped = true;
  918. *rblk = i;
  919. *rhead = (struct xlog_rec_header *) offset;
  920. if (++found == count)
  921. break;
  922. }
  923. }
  924. }
  925. return found;
  926. out_error:
  927. return error;
  928. }
  929. /*
  930. * Calculate distance from head to tail (i.e., unused space in the log).
  931. */
  932. static inline int
  933. xlog_tail_distance(
  934. struct xlog *log,
  935. xfs_daddr_t head_blk,
  936. xfs_daddr_t tail_blk)
  937. {
  938. if (head_blk < tail_blk)
  939. return tail_blk - head_blk;
  940. return tail_blk + (log->l_logBBsize - head_blk);
  941. }
  942. /*
  943. * Verify the log tail. This is particularly important when torn or incomplete
  944. * writes have been detected near the front of the log and the head has been
  945. * walked back accordingly.
  946. *
  947. * We also have to handle the case where the tail was pinned and the head
  948. * blocked behind the tail right before a crash. If the tail had been pushed
  949. * immediately prior to the crash and the subsequent checkpoint was only
  950. * partially written, it's possible it overwrote the last referenced tail in the
  951. * log with garbage. This is not a coherency problem because the tail must have
  952. * been pushed before it can be overwritten, but appears as log corruption to
  953. * recovery because we have no way to know the tail was updated if the
  954. * subsequent checkpoint didn't write successfully.
  955. *
  956. * Therefore, CRC check the log from tail to head. If a failure occurs and the
  957. * offending record is within max iclog bufs from the head, walk the tail
  958. * forward and retry until a valid tail is found or corruption is detected out
  959. * of the range of a possible overwrite.
  960. */
  961. STATIC int
  962. xlog_verify_tail(
  963. struct xlog *log,
  964. xfs_daddr_t head_blk,
  965. xfs_daddr_t *tail_blk,
  966. int hsize)
  967. {
  968. struct xlog_rec_header *thead;
  969. struct xfs_buf *bp;
  970. xfs_daddr_t first_bad;
  971. int error = 0;
  972. bool wrapped;
  973. xfs_daddr_t tmp_tail;
  974. xfs_daddr_t orig_tail = *tail_blk;
  975. bp = xlog_get_bp(log, 1);
  976. if (!bp)
  977. return -ENOMEM;
  978. /*
  979. * Make sure the tail points to a record (returns positive count on
  980. * success).
  981. */
  982. error = xlog_seek_logrec_hdr(log, head_blk, *tail_blk, 1, bp,
  983. &tmp_tail, &thead, &wrapped);
  984. if (error < 0)
  985. goto out;
  986. if (*tail_blk != tmp_tail)
  987. *tail_blk = tmp_tail;
  988. /*
  989. * Run a CRC check from the tail to the head. We can't just check
  990. * MAX_ICLOGS records past the tail because the tail may point to stale
  991. * blocks cleared during the search for the head/tail. These blocks are
  992. * overwritten with zero-length records and thus record count is not a
  993. * reliable indicator of the iclog state before a crash.
  994. */
  995. first_bad = 0;
  996. error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
  997. XLOG_RECOVER_CRCPASS, &first_bad);
  998. while ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
  999. int tail_distance;
  1000. /*
  1001. * Is corruption within range of the head? If so, retry from
  1002. * the next record. Otherwise return an error.
  1003. */
  1004. tail_distance = xlog_tail_distance(log, head_blk, first_bad);
  1005. if (tail_distance > BTOBB(XLOG_MAX_ICLOGS * hsize))
  1006. break;
  1007. /* skip to the next record; returns positive count on success */
  1008. error = xlog_seek_logrec_hdr(log, head_blk, first_bad, 2, bp,
  1009. &tmp_tail, &thead, &wrapped);
  1010. if (error < 0)
  1011. goto out;
  1012. *tail_blk = tmp_tail;
  1013. first_bad = 0;
  1014. error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
  1015. XLOG_RECOVER_CRCPASS, &first_bad);
  1016. }
  1017. if (!error && *tail_blk != orig_tail)
  1018. xfs_warn(log->l_mp,
  1019. "Tail block (0x%llx) overwrite detected. Updated to 0x%llx",
  1020. orig_tail, *tail_blk);
  1021. out:
  1022. xlog_put_bp(bp);
  1023. return error;
  1024. }
  1025. /*
  1026. * Detect and trim torn writes from the head of the log.
  1027. *
  1028. * Storage without sector atomicity guarantees can result in torn writes in the
  1029. * log in the event of a crash. Our only means to detect this scenario is via
  1030. * CRC verification. While we can't always be certain that CRC verification
  1031. * failure is due to a torn write vs. an unrelated corruption, we do know that
  1032. * only a certain number (XLOG_MAX_ICLOGS) of log records can be written out at
  1033. * one time. Therefore, CRC verify up to XLOG_MAX_ICLOGS records at the head of
  1034. * the log and treat failures in this range as torn writes as a matter of
  1035. * policy. In the event of CRC failure, the head is walked back to the last good
  1036. * record in the log and the tail is updated from that record and verified.
  1037. */
  1038. STATIC int
  1039. xlog_verify_head(
  1040. struct xlog *log,
  1041. xfs_daddr_t *head_blk, /* in/out: unverified head */
  1042. xfs_daddr_t *tail_blk, /* out: tail block */
  1043. struct xfs_buf *bp,
  1044. xfs_daddr_t *rhead_blk, /* start blk of last record */
  1045. struct xlog_rec_header **rhead, /* ptr to last record */
  1046. bool *wrapped) /* last rec. wraps phys. log */
  1047. {
  1048. struct xlog_rec_header *tmp_rhead;
  1049. struct xfs_buf *tmp_bp;
  1050. xfs_daddr_t first_bad;
  1051. xfs_daddr_t tmp_rhead_blk;
  1052. int found;
  1053. int error;
  1054. bool tmp_wrapped;
  1055. /*
  1056. * Check the head of the log for torn writes. Search backwards from the
  1057. * head until we hit the tail or the maximum number of log record I/Os
  1058. * that could have been in flight at one time. Use a temporary buffer so
  1059. * we don't trash the rhead/bp pointers from the caller.
  1060. */
  1061. tmp_bp = xlog_get_bp(log, 1);
  1062. if (!tmp_bp)
  1063. return -ENOMEM;
  1064. error = xlog_rseek_logrec_hdr(log, *head_blk, *tail_blk,
  1065. XLOG_MAX_ICLOGS, tmp_bp, &tmp_rhead_blk,
  1066. &tmp_rhead, &tmp_wrapped);
  1067. xlog_put_bp(tmp_bp);
  1068. if (error < 0)
  1069. return error;
  1070. /*
  1071. * Now run a CRC verification pass over the records starting at the
  1072. * block found above to the current head. If a CRC failure occurs, the
  1073. * log block of the first bad record is saved in first_bad.
  1074. */
  1075. error = xlog_do_recovery_pass(log, *head_blk, tmp_rhead_blk,
  1076. XLOG_RECOVER_CRCPASS, &first_bad);
  1077. if ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
  1078. /*
  1079. * We've hit a potential torn write. Reset the error and warn
  1080. * about it.
  1081. */
  1082. error = 0;
  1083. xfs_warn(log->l_mp,
  1084. "Torn write (CRC failure) detected at log block 0x%llx. Truncating head block from 0x%llx.",
  1085. first_bad, *head_blk);
  1086. /*
  1087. * Get the header block and buffer pointer for the last good
  1088. * record before the bad record.
  1089. *
  1090. * Note that xlog_find_tail() clears the blocks at the new head
  1091. * (i.e., the records with invalid CRC) if the cycle number
  1092. * matches the the current cycle.
  1093. */
  1094. found = xlog_rseek_logrec_hdr(log, first_bad, *tail_blk, 1, bp,
  1095. rhead_blk, rhead, wrapped);
  1096. if (found < 0)
  1097. return found;
  1098. if (found == 0) /* XXX: right thing to do here? */
  1099. return -EIO;
  1100. /*
  1101. * Reset the head block to the starting block of the first bad
  1102. * log record and set the tail block based on the last good
  1103. * record.
  1104. *
  1105. * Bail out if the updated head/tail match as this indicates
  1106. * possible corruption outside of the acceptable
  1107. * (XLOG_MAX_ICLOGS) range. This is a job for xfs_repair...
  1108. */
  1109. *head_blk = first_bad;
  1110. *tail_blk = BLOCK_LSN(be64_to_cpu((*rhead)->h_tail_lsn));
  1111. if (*head_blk == *tail_blk) {
  1112. ASSERT(0);
  1113. return 0;
  1114. }
  1115. }
  1116. if (error)
  1117. return error;
  1118. return xlog_verify_tail(log, *head_blk, tail_blk,
  1119. be32_to_cpu((*rhead)->h_size));
  1120. }
  1121. /*
  1122. * Check whether the head of the log points to an unmount record. In other
  1123. * words, determine whether the log is clean. If so, update the in-core state
  1124. * appropriately.
  1125. */
  1126. static int
  1127. xlog_check_unmount_rec(
  1128. struct xlog *log,
  1129. xfs_daddr_t *head_blk,
  1130. xfs_daddr_t *tail_blk,
  1131. struct xlog_rec_header *rhead,
  1132. xfs_daddr_t rhead_blk,
  1133. struct xfs_buf *bp,
  1134. bool *clean)
  1135. {
  1136. struct xlog_op_header *op_head;
  1137. xfs_daddr_t umount_data_blk;
  1138. xfs_daddr_t after_umount_blk;
  1139. int hblks;
  1140. int error;
  1141. char *offset;
  1142. *clean = false;
  1143. /*
  1144. * Look for unmount record. If we find it, then we know there was a
  1145. * clean unmount. Since 'i' could be the last block in the physical
  1146. * log, we convert to a log block before comparing to the head_blk.
  1147. *
  1148. * Save the current tail lsn to use to pass to xlog_clear_stale_blocks()
  1149. * below. We won't want to clear the unmount record if there is one, so
  1150. * we pass the lsn of the unmount record rather than the block after it.
  1151. */
  1152. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  1153. int h_size = be32_to_cpu(rhead->h_size);
  1154. int h_version = be32_to_cpu(rhead->h_version);
  1155. if ((h_version & XLOG_VERSION_2) &&
  1156. (h_size > XLOG_HEADER_CYCLE_SIZE)) {
  1157. hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
  1158. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  1159. hblks++;
  1160. } else {
  1161. hblks = 1;
  1162. }
  1163. } else {
  1164. hblks = 1;
  1165. }
  1166. after_umount_blk = rhead_blk + hblks + BTOBB(be32_to_cpu(rhead->h_len));
  1167. after_umount_blk = do_mod(after_umount_blk, log->l_logBBsize);
  1168. if (*head_blk == after_umount_blk &&
  1169. be32_to_cpu(rhead->h_num_logops) == 1) {
  1170. umount_data_blk = rhead_blk + hblks;
  1171. umount_data_blk = do_mod(umount_data_blk, log->l_logBBsize);
  1172. error = xlog_bread(log, umount_data_blk, 1, bp, &offset);
  1173. if (error)
  1174. return error;
  1175. op_head = (struct xlog_op_header *)offset;
  1176. if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
  1177. /*
  1178. * Set tail and last sync so that newly written log
  1179. * records will point recovery to after the current
  1180. * unmount record.
  1181. */
  1182. xlog_assign_atomic_lsn(&log->l_tail_lsn,
  1183. log->l_curr_cycle, after_umount_blk);
  1184. xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
  1185. log->l_curr_cycle, after_umount_blk);
  1186. *tail_blk = after_umount_blk;
  1187. *clean = true;
  1188. }
  1189. }
  1190. return 0;
  1191. }
  1192. static void
  1193. xlog_set_state(
  1194. struct xlog *log,
  1195. xfs_daddr_t head_blk,
  1196. struct xlog_rec_header *rhead,
  1197. xfs_daddr_t rhead_blk,
  1198. bool bump_cycle)
  1199. {
  1200. /*
  1201. * Reset log values according to the state of the log when we
  1202. * crashed. In the case where head_blk == 0, we bump curr_cycle
  1203. * one because the next write starts a new cycle rather than
  1204. * continuing the cycle of the last good log record. At this
  1205. * point we have guaranteed that all partial log records have been
  1206. * accounted for. Therefore, we know that the last good log record
  1207. * written was complete and ended exactly on the end boundary
  1208. * of the physical log.
  1209. */
  1210. log->l_prev_block = rhead_blk;
  1211. log->l_curr_block = (int)head_blk;
  1212. log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
  1213. if (bump_cycle)
  1214. log->l_curr_cycle++;
  1215. atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
  1216. atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
  1217. xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
  1218. BBTOB(log->l_curr_block));
  1219. xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
  1220. BBTOB(log->l_curr_block));
  1221. }
  1222. /*
  1223. * Find the sync block number or the tail of the log.
  1224. *
  1225. * This will be the block number of the last record to have its
  1226. * associated buffers synced to disk. Every log record header has
  1227. * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
  1228. * to get a sync block number. The only concern is to figure out which
  1229. * log record header to believe.
  1230. *
  1231. * The following algorithm uses the log record header with the largest
  1232. * lsn. The entire log record does not need to be valid. We only care
  1233. * that the header is valid.
  1234. *
  1235. * We could speed up search by using current head_blk buffer, but it is not
  1236. * available.
  1237. */
  1238. STATIC int
  1239. xlog_find_tail(
  1240. struct xlog *log,
  1241. xfs_daddr_t *head_blk,
  1242. xfs_daddr_t *tail_blk)
  1243. {
  1244. xlog_rec_header_t *rhead;
  1245. char *offset = NULL;
  1246. xfs_buf_t *bp;
  1247. int error;
  1248. xfs_daddr_t rhead_blk;
  1249. xfs_lsn_t tail_lsn;
  1250. bool wrapped = false;
  1251. bool clean = false;
  1252. /*
  1253. * Find previous log record
  1254. */
  1255. if ((error = xlog_find_head(log, head_blk)))
  1256. return error;
  1257. ASSERT(*head_blk < INT_MAX);
  1258. bp = xlog_get_bp(log, 1);
  1259. if (!bp)
  1260. return -ENOMEM;
  1261. if (*head_blk == 0) { /* special case */
  1262. error = xlog_bread(log, 0, 1, bp, &offset);
  1263. if (error)
  1264. goto done;
  1265. if (xlog_get_cycle(offset) == 0) {
  1266. *tail_blk = 0;
  1267. /* leave all other log inited values alone */
  1268. goto done;
  1269. }
  1270. }
  1271. /*
  1272. * Search backwards through the log looking for the log record header
  1273. * block. This wraps all the way back around to the head so something is
  1274. * seriously wrong if we can't find it.
  1275. */
  1276. error = xlog_rseek_logrec_hdr(log, *head_blk, *head_blk, 1, bp,
  1277. &rhead_blk, &rhead, &wrapped);
  1278. if (error < 0)
  1279. return error;
  1280. if (!error) {
  1281. xfs_warn(log->l_mp, "%s: couldn't find sync record", __func__);
  1282. return -EIO;
  1283. }
  1284. *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
  1285. /*
  1286. * Set the log state based on the current head record.
  1287. */
  1288. xlog_set_state(log, *head_blk, rhead, rhead_blk, wrapped);
  1289. tail_lsn = atomic64_read(&log->l_tail_lsn);
  1290. /*
  1291. * Look for an unmount record at the head of the log. This sets the log
  1292. * state to determine whether recovery is necessary.
  1293. */
  1294. error = xlog_check_unmount_rec(log, head_blk, tail_blk, rhead,
  1295. rhead_blk, bp, &clean);
  1296. if (error)
  1297. goto done;
  1298. /*
  1299. * Verify the log head if the log is not clean (e.g., we have anything
  1300. * but an unmount record at the head). This uses CRC verification to
  1301. * detect and trim torn writes. If discovered, CRC failures are
  1302. * considered torn writes and the log head is trimmed accordingly.
  1303. *
  1304. * Note that we can only run CRC verification when the log is dirty
  1305. * because there's no guarantee that the log data behind an unmount
  1306. * record is compatible with the current architecture.
  1307. */
  1308. if (!clean) {
  1309. xfs_daddr_t orig_head = *head_blk;
  1310. error = xlog_verify_head(log, head_blk, tail_blk, bp,
  1311. &rhead_blk, &rhead, &wrapped);
  1312. if (error)
  1313. goto done;
  1314. /* update in-core state again if the head changed */
  1315. if (*head_blk != orig_head) {
  1316. xlog_set_state(log, *head_blk, rhead, rhead_blk,
  1317. wrapped);
  1318. tail_lsn = atomic64_read(&log->l_tail_lsn);
  1319. error = xlog_check_unmount_rec(log, head_blk, tail_blk,
  1320. rhead, rhead_blk, bp,
  1321. &clean);
  1322. if (error)
  1323. goto done;
  1324. }
  1325. }
  1326. /*
  1327. * Note that the unmount was clean. If the unmount was not clean, we
  1328. * need to know this to rebuild the superblock counters from the perag
  1329. * headers if we have a filesystem using non-persistent counters.
  1330. */
  1331. if (clean)
  1332. log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
  1333. /*
  1334. * Make sure that there are no blocks in front of the head
  1335. * with the same cycle number as the head. This can happen
  1336. * because we allow multiple outstanding log writes concurrently,
  1337. * and the later writes might make it out before earlier ones.
  1338. *
  1339. * We use the lsn from before modifying it so that we'll never
  1340. * overwrite the unmount record after a clean unmount.
  1341. *
  1342. * Do this only if we are going to recover the filesystem
  1343. *
  1344. * NOTE: This used to say "if (!readonly)"
  1345. * However on Linux, we can & do recover a read-only filesystem.
  1346. * We only skip recovery if NORECOVERY is specified on mount,
  1347. * in which case we would not be here.
  1348. *
  1349. * But... if the -device- itself is readonly, just skip this.
  1350. * We can't recover this device anyway, so it won't matter.
  1351. */
  1352. if (!xfs_readonly_buftarg(log->l_mp->m_logdev_targp))
  1353. error = xlog_clear_stale_blocks(log, tail_lsn);
  1354. done:
  1355. xlog_put_bp(bp);
  1356. if (error)
  1357. xfs_warn(log->l_mp, "failed to locate log tail");
  1358. return error;
  1359. }
  1360. /*
  1361. * Is the log zeroed at all?
  1362. *
  1363. * The last binary search should be changed to perform an X block read
  1364. * once X becomes small enough. You can then search linearly through
  1365. * the X blocks. This will cut down on the number of reads we need to do.
  1366. *
  1367. * If the log is partially zeroed, this routine will pass back the blkno
  1368. * of the first block with cycle number 0. It won't have a complete LR
  1369. * preceding it.
  1370. *
  1371. * Return:
  1372. * 0 => the log is completely written to
  1373. * 1 => use *blk_no as the first block of the log
  1374. * <0 => error has occurred
  1375. */
  1376. STATIC int
  1377. xlog_find_zeroed(
  1378. struct xlog *log,
  1379. xfs_daddr_t *blk_no)
  1380. {
  1381. xfs_buf_t *bp;
  1382. char *offset;
  1383. uint first_cycle, last_cycle;
  1384. xfs_daddr_t new_blk, last_blk, start_blk;
  1385. xfs_daddr_t num_scan_bblks;
  1386. int error, log_bbnum = log->l_logBBsize;
  1387. *blk_no = 0;
  1388. /* check totally zeroed log */
  1389. bp = xlog_get_bp(log, 1);
  1390. if (!bp)
  1391. return -ENOMEM;
  1392. error = xlog_bread(log, 0, 1, bp, &offset);
  1393. if (error)
  1394. goto bp_err;
  1395. first_cycle = xlog_get_cycle(offset);
  1396. if (first_cycle == 0) { /* completely zeroed log */
  1397. *blk_no = 0;
  1398. xlog_put_bp(bp);
  1399. return 1;
  1400. }
  1401. /* check partially zeroed log */
  1402. error = xlog_bread(log, log_bbnum-1, 1, bp, &offset);
  1403. if (error)
  1404. goto bp_err;
  1405. last_cycle = xlog_get_cycle(offset);
  1406. if (last_cycle != 0) { /* log completely written to */
  1407. xlog_put_bp(bp);
  1408. return 0;
  1409. } else if (first_cycle != 1) {
  1410. /*
  1411. * If the cycle of the last block is zero, the cycle of
  1412. * the first block must be 1. If it's not, maybe we're
  1413. * not looking at a log... Bail out.
  1414. */
  1415. xfs_warn(log->l_mp,
  1416. "Log inconsistent or not a log (last==0, first!=1)");
  1417. error = -EINVAL;
  1418. goto bp_err;
  1419. }
  1420. /* we have a partially zeroed log */
  1421. last_blk = log_bbnum-1;
  1422. if ((error = xlog_find_cycle_start(log, bp, 0, &last_blk, 0)))
  1423. goto bp_err;
  1424. /*
  1425. * Validate the answer. Because there is no way to guarantee that
  1426. * the entire log is made up of log records which are the same size,
  1427. * we scan over the defined maximum blocks. At this point, the maximum
  1428. * is not chosen to mean anything special. XXXmiken
  1429. */
  1430. num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
  1431. ASSERT(num_scan_bblks <= INT_MAX);
  1432. if (last_blk < num_scan_bblks)
  1433. num_scan_bblks = last_blk;
  1434. start_blk = last_blk - num_scan_bblks;
  1435. /*
  1436. * We search for any instances of cycle number 0 that occur before
  1437. * our current estimate of the head. What we're trying to detect is
  1438. * 1 ... | 0 | 1 | 0...
  1439. * ^ binary search ends here
  1440. */
  1441. if ((error = xlog_find_verify_cycle(log, start_blk,
  1442. (int)num_scan_bblks, 0, &new_blk)))
  1443. goto bp_err;
  1444. if (new_blk != -1)
  1445. last_blk = new_blk;
  1446. /*
  1447. * Potentially backup over partial log record write. We don't need
  1448. * to search the end of the log because we know it is zero.
  1449. */
  1450. error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
  1451. if (error == 1)
  1452. error = -EIO;
  1453. if (error)
  1454. goto bp_err;
  1455. *blk_no = last_blk;
  1456. bp_err:
  1457. xlog_put_bp(bp);
  1458. if (error)
  1459. return error;
  1460. return 1;
  1461. }
  1462. /*
  1463. * These are simple subroutines used by xlog_clear_stale_blocks() below
  1464. * to initialize a buffer full of empty log record headers and write
  1465. * them into the log.
  1466. */
  1467. STATIC void
  1468. xlog_add_record(
  1469. struct xlog *log,
  1470. char *buf,
  1471. int cycle,
  1472. int block,
  1473. int tail_cycle,
  1474. int tail_block)
  1475. {
  1476. xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
  1477. memset(buf, 0, BBSIZE);
  1478. recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
  1479. recp->h_cycle = cpu_to_be32(cycle);
  1480. recp->h_version = cpu_to_be32(
  1481. xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
  1482. recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
  1483. recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
  1484. recp->h_fmt = cpu_to_be32(XLOG_FMT);
  1485. memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
  1486. }
  1487. STATIC int
  1488. xlog_write_log_records(
  1489. struct xlog *log,
  1490. int cycle,
  1491. int start_block,
  1492. int blocks,
  1493. int tail_cycle,
  1494. int tail_block)
  1495. {
  1496. char *offset;
  1497. xfs_buf_t *bp;
  1498. int balign, ealign;
  1499. int sectbb = log->l_sectBBsize;
  1500. int end_block = start_block + blocks;
  1501. int bufblks;
  1502. int error = 0;
  1503. int i, j = 0;
  1504. /*
  1505. * Greedily allocate a buffer big enough to handle the full
  1506. * range of basic blocks to be written. If that fails, try
  1507. * a smaller size. We need to be able to write at least a
  1508. * log sector, or we're out of luck.
  1509. */
  1510. bufblks = 1 << ffs(blocks);
  1511. while (bufblks > log->l_logBBsize)
  1512. bufblks >>= 1;
  1513. while (!(bp = xlog_get_bp(log, bufblks))) {
  1514. bufblks >>= 1;
  1515. if (bufblks < sectbb)
  1516. return -ENOMEM;
  1517. }
  1518. /* We may need to do a read at the start to fill in part of
  1519. * the buffer in the starting sector not covered by the first
  1520. * write below.
  1521. */
  1522. balign = round_down(start_block, sectbb);
  1523. if (balign != start_block) {
  1524. error = xlog_bread_noalign(log, start_block, 1, bp);
  1525. if (error)
  1526. goto out_put_bp;
  1527. j = start_block - balign;
  1528. }
  1529. for (i = start_block; i < end_block; i += bufblks) {
  1530. int bcount, endcount;
  1531. bcount = min(bufblks, end_block - start_block);
  1532. endcount = bcount - j;
  1533. /* We may need to do a read at the end to fill in part of
  1534. * the buffer in the final sector not covered by the write.
  1535. * If this is the same sector as the above read, skip it.
  1536. */
  1537. ealign = round_down(end_block, sectbb);
  1538. if (j == 0 && (start_block + endcount > ealign)) {
  1539. offset = bp->b_addr + BBTOB(ealign - start_block);
  1540. error = xlog_bread_offset(log, ealign, sectbb,
  1541. bp, offset);
  1542. if (error)
  1543. break;
  1544. }
  1545. offset = xlog_align(log, start_block, endcount, bp);
  1546. for (; j < endcount; j++) {
  1547. xlog_add_record(log, offset, cycle, i+j,
  1548. tail_cycle, tail_block);
  1549. offset += BBSIZE;
  1550. }
  1551. error = xlog_bwrite(log, start_block, endcount, bp);
  1552. if (error)
  1553. break;
  1554. start_block += endcount;
  1555. j = 0;
  1556. }
  1557. out_put_bp:
  1558. xlog_put_bp(bp);
  1559. return error;
  1560. }
  1561. /*
  1562. * This routine is called to blow away any incomplete log writes out
  1563. * in front of the log head. We do this so that we won't become confused
  1564. * if we come up, write only a little bit more, and then crash again.
  1565. * If we leave the partial log records out there, this situation could
  1566. * cause us to think those partial writes are valid blocks since they
  1567. * have the current cycle number. We get rid of them by overwriting them
  1568. * with empty log records with the old cycle number rather than the
  1569. * current one.
  1570. *
  1571. * The tail lsn is passed in rather than taken from
  1572. * the log so that we will not write over the unmount record after a
  1573. * clean unmount in a 512 block log. Doing so would leave the log without
  1574. * any valid log records in it until a new one was written. If we crashed
  1575. * during that time we would not be able to recover.
  1576. */
  1577. STATIC int
  1578. xlog_clear_stale_blocks(
  1579. struct xlog *log,
  1580. xfs_lsn_t tail_lsn)
  1581. {
  1582. int tail_cycle, head_cycle;
  1583. int tail_block, head_block;
  1584. int tail_distance, max_distance;
  1585. int distance;
  1586. int error;
  1587. tail_cycle = CYCLE_LSN(tail_lsn);
  1588. tail_block = BLOCK_LSN(tail_lsn);
  1589. head_cycle = log->l_curr_cycle;
  1590. head_block = log->l_curr_block;
  1591. /*
  1592. * Figure out the distance between the new head of the log
  1593. * and the tail. We want to write over any blocks beyond the
  1594. * head that we may have written just before the crash, but
  1595. * we don't want to overwrite the tail of the log.
  1596. */
  1597. if (head_cycle == tail_cycle) {
  1598. /*
  1599. * The tail is behind the head in the physical log,
  1600. * so the distance from the head to the tail is the
  1601. * distance from the head to the end of the log plus
  1602. * the distance from the beginning of the log to the
  1603. * tail.
  1604. */
  1605. if (unlikely(head_block < tail_block || head_block >= log->l_logBBsize)) {
  1606. XFS_ERROR_REPORT("xlog_clear_stale_blocks(1)",
  1607. XFS_ERRLEVEL_LOW, log->l_mp);
  1608. return -EFSCORRUPTED;
  1609. }
  1610. tail_distance = tail_block + (log->l_logBBsize - head_block);
  1611. } else {
  1612. /*
  1613. * The head is behind the tail in the physical log,
  1614. * so the distance from the head to the tail is just
  1615. * the tail block minus the head block.
  1616. */
  1617. if (unlikely(head_block >= tail_block || head_cycle != (tail_cycle + 1))){
  1618. XFS_ERROR_REPORT("xlog_clear_stale_blocks(2)",
  1619. XFS_ERRLEVEL_LOW, log->l_mp);
  1620. return -EFSCORRUPTED;
  1621. }
  1622. tail_distance = tail_block - head_block;
  1623. }
  1624. /*
  1625. * If the head is right up against the tail, we can't clear
  1626. * anything.
  1627. */
  1628. if (tail_distance <= 0) {
  1629. ASSERT(tail_distance == 0);
  1630. return 0;
  1631. }
  1632. max_distance = XLOG_TOTAL_REC_SHIFT(log);
  1633. /*
  1634. * Take the smaller of the maximum amount of outstanding I/O
  1635. * we could have and the distance to the tail to clear out.
  1636. * We take the smaller so that we don't overwrite the tail and
  1637. * we don't waste all day writing from the head to the tail
  1638. * for no reason.
  1639. */
  1640. max_distance = MIN(max_distance, tail_distance);
  1641. if ((head_block + max_distance) <= log->l_logBBsize) {
  1642. /*
  1643. * We can stomp all the blocks we need to without
  1644. * wrapping around the end of the log. Just do it
  1645. * in a single write. Use the cycle number of the
  1646. * current cycle minus one so that the log will look like:
  1647. * n ... | n - 1 ...
  1648. */
  1649. error = xlog_write_log_records(log, (head_cycle - 1),
  1650. head_block, max_distance, tail_cycle,
  1651. tail_block);
  1652. if (error)
  1653. return error;
  1654. } else {
  1655. /*
  1656. * We need to wrap around the end of the physical log in
  1657. * order to clear all the blocks. Do it in two separate
  1658. * I/Os. The first write should be from the head to the
  1659. * end of the physical log, and it should use the current
  1660. * cycle number minus one just like above.
  1661. */
  1662. distance = log->l_logBBsize - head_block;
  1663. error = xlog_write_log_records(log, (head_cycle - 1),
  1664. head_block, distance, tail_cycle,
  1665. tail_block);
  1666. if (error)
  1667. return error;
  1668. /*
  1669. * Now write the blocks at the start of the physical log.
  1670. * This writes the remainder of the blocks we want to clear.
  1671. * It uses the current cycle number since we're now on the
  1672. * same cycle as the head so that we get:
  1673. * n ... n ... | n - 1 ...
  1674. * ^^^^^ blocks we're writing
  1675. */
  1676. distance = max_distance - (log->l_logBBsize - head_block);
  1677. error = xlog_write_log_records(log, head_cycle, 0, distance,
  1678. tail_cycle, tail_block);
  1679. if (error)
  1680. return error;
  1681. }
  1682. return 0;
  1683. }
  1684. /******************************************************************************
  1685. *
  1686. * Log recover routines
  1687. *
  1688. ******************************************************************************
  1689. */
  1690. /*
  1691. * Sort the log items in the transaction.
  1692. *
  1693. * The ordering constraints are defined by the inode allocation and unlink
  1694. * behaviour. The rules are:
  1695. *
  1696. * 1. Every item is only logged once in a given transaction. Hence it
  1697. * represents the last logged state of the item. Hence ordering is
  1698. * dependent on the order in which operations need to be performed so
  1699. * required initial conditions are always met.
  1700. *
  1701. * 2. Cancelled buffers are recorded in pass 1 in a separate table and
  1702. * there's nothing to replay from them so we can simply cull them
  1703. * from the transaction. However, we can't do that until after we've
  1704. * replayed all the other items because they may be dependent on the
  1705. * cancelled buffer and replaying the cancelled buffer can remove it
  1706. * form the cancelled buffer table. Hence they have tobe done last.
  1707. *
  1708. * 3. Inode allocation buffers must be replayed before inode items that
  1709. * read the buffer and replay changes into it. For filesystems using the
  1710. * ICREATE transactions, this means XFS_LI_ICREATE objects need to get
  1711. * treated the same as inode allocation buffers as they create and
  1712. * initialise the buffers directly.
  1713. *
  1714. * 4. Inode unlink buffers must be replayed after inode items are replayed.
  1715. * This ensures that inodes are completely flushed to the inode buffer
  1716. * in a "free" state before we remove the unlinked inode list pointer.
  1717. *
  1718. * Hence the ordering needs to be inode allocation buffers first, inode items
  1719. * second, inode unlink buffers third and cancelled buffers last.
  1720. *
  1721. * But there's a problem with that - we can't tell an inode allocation buffer
  1722. * apart from a regular buffer, so we can't separate them. We can, however,
  1723. * tell an inode unlink buffer from the others, and so we can separate them out
  1724. * from all the other buffers and move them to last.
  1725. *
  1726. * Hence, 4 lists, in order from head to tail:
  1727. * - buffer_list for all buffers except cancelled/inode unlink buffers
  1728. * - item_list for all non-buffer items
  1729. * - inode_buffer_list for inode unlink buffers
  1730. * - cancel_list for the cancelled buffers
  1731. *
  1732. * Note that we add objects to the tail of the lists so that first-to-last
  1733. * ordering is preserved within the lists. Adding objects to the head of the
  1734. * list means when we traverse from the head we walk them in last-to-first
  1735. * order. For cancelled buffers and inode unlink buffers this doesn't matter,
  1736. * but for all other items there may be specific ordering that we need to
  1737. * preserve.
  1738. */
  1739. STATIC int
  1740. xlog_recover_reorder_trans(
  1741. struct xlog *log,
  1742. struct xlog_recover *trans,
  1743. int pass)
  1744. {
  1745. xlog_recover_item_t *item, *n;
  1746. int error = 0;
  1747. LIST_HEAD(sort_list);
  1748. LIST_HEAD(cancel_list);
  1749. LIST_HEAD(buffer_list);
  1750. LIST_HEAD(inode_buffer_list);
  1751. LIST_HEAD(inode_list);
  1752. list_splice_init(&trans->r_itemq, &sort_list);
  1753. list_for_each_entry_safe(item, n, &sort_list, ri_list) {
  1754. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  1755. switch (ITEM_TYPE(item)) {
  1756. case XFS_LI_ICREATE:
  1757. list_move_tail(&item->ri_list, &buffer_list);
  1758. break;
  1759. case XFS_LI_BUF:
  1760. if (buf_f->blf_flags & XFS_BLF_CANCEL) {
  1761. trace_xfs_log_recover_item_reorder_head(log,
  1762. trans, item, pass);
  1763. list_move(&item->ri_list, &cancel_list);
  1764. break;
  1765. }
  1766. if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
  1767. list_move(&item->ri_list, &inode_buffer_list);
  1768. break;
  1769. }
  1770. list_move_tail(&item->ri_list, &buffer_list);
  1771. break;
  1772. case XFS_LI_INODE:
  1773. case XFS_LI_DQUOT:
  1774. case XFS_LI_QUOTAOFF:
  1775. case XFS_LI_EFD:
  1776. case XFS_LI_EFI:
  1777. case XFS_LI_RUI:
  1778. case XFS_LI_RUD:
  1779. case XFS_LI_CUI:
  1780. case XFS_LI_CUD:
  1781. case XFS_LI_BUI:
  1782. case XFS_LI_BUD:
  1783. trace_xfs_log_recover_item_reorder_tail(log,
  1784. trans, item, pass);
  1785. list_move_tail(&item->ri_list, &inode_list);
  1786. break;
  1787. default:
  1788. xfs_warn(log->l_mp,
  1789. "%s: unrecognized type of log operation",
  1790. __func__);
  1791. ASSERT(0);
  1792. /*
  1793. * return the remaining items back to the transaction
  1794. * item list so they can be freed in caller.
  1795. */
  1796. if (!list_empty(&sort_list))
  1797. list_splice_init(&sort_list, &trans->r_itemq);
  1798. error = -EIO;
  1799. goto out;
  1800. }
  1801. }
  1802. out:
  1803. ASSERT(list_empty(&sort_list));
  1804. if (!list_empty(&buffer_list))
  1805. list_splice(&buffer_list, &trans->r_itemq);
  1806. if (!list_empty(&inode_list))
  1807. list_splice_tail(&inode_list, &trans->r_itemq);
  1808. if (!list_empty(&inode_buffer_list))
  1809. list_splice_tail(&inode_buffer_list, &trans->r_itemq);
  1810. if (!list_empty(&cancel_list))
  1811. list_splice_tail(&cancel_list, &trans->r_itemq);
  1812. return error;
  1813. }
  1814. /*
  1815. * Build up the table of buf cancel records so that we don't replay
  1816. * cancelled data in the second pass. For buffer records that are
  1817. * not cancel records, there is nothing to do here so we just return.
  1818. *
  1819. * If we get a cancel record which is already in the table, this indicates
  1820. * that the buffer was cancelled multiple times. In order to ensure
  1821. * that during pass 2 we keep the record in the table until we reach its
  1822. * last occurrence in the log, we keep a reference count in the cancel
  1823. * record in the table to tell us how many times we expect to see this
  1824. * record during the second pass.
  1825. */
  1826. STATIC int
  1827. xlog_recover_buffer_pass1(
  1828. struct xlog *log,
  1829. struct xlog_recover_item *item)
  1830. {
  1831. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  1832. struct list_head *bucket;
  1833. struct xfs_buf_cancel *bcp;
  1834. /*
  1835. * If this isn't a cancel buffer item, then just return.
  1836. */
  1837. if (!(buf_f->blf_flags & XFS_BLF_CANCEL)) {
  1838. trace_xfs_log_recover_buf_not_cancel(log, buf_f);
  1839. return 0;
  1840. }
  1841. /*
  1842. * Insert an xfs_buf_cancel record into the hash table of them.
  1843. * If there is already an identical record, bump its reference count.
  1844. */
  1845. bucket = XLOG_BUF_CANCEL_BUCKET(log, buf_f->blf_blkno);
  1846. list_for_each_entry(bcp, bucket, bc_list) {
  1847. if (bcp->bc_blkno == buf_f->blf_blkno &&
  1848. bcp->bc_len == buf_f->blf_len) {
  1849. bcp->bc_refcount++;
  1850. trace_xfs_log_recover_buf_cancel_ref_inc(log, buf_f);
  1851. return 0;
  1852. }
  1853. }
  1854. bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), KM_SLEEP);
  1855. bcp->bc_blkno = buf_f->blf_blkno;
  1856. bcp->bc_len = buf_f->blf_len;
  1857. bcp->bc_refcount = 1;
  1858. list_add_tail(&bcp->bc_list, bucket);
  1859. trace_xfs_log_recover_buf_cancel_add(log, buf_f);
  1860. return 0;
  1861. }
  1862. /*
  1863. * Check to see whether the buffer being recovered has a corresponding
  1864. * entry in the buffer cancel record table. If it is, return the cancel
  1865. * buffer structure to the caller.
  1866. */
  1867. STATIC struct xfs_buf_cancel *
  1868. xlog_peek_buffer_cancelled(
  1869. struct xlog *log,
  1870. xfs_daddr_t blkno,
  1871. uint len,
  1872. unsigned short flags)
  1873. {
  1874. struct list_head *bucket;
  1875. struct xfs_buf_cancel *bcp;
  1876. if (!log->l_buf_cancel_table) {
  1877. /* empty table means no cancelled buffers in the log */
  1878. ASSERT(!(flags & XFS_BLF_CANCEL));
  1879. return NULL;
  1880. }
  1881. bucket = XLOG_BUF_CANCEL_BUCKET(log, blkno);
  1882. list_for_each_entry(bcp, bucket, bc_list) {
  1883. if (bcp->bc_blkno == blkno && bcp->bc_len == len)
  1884. return bcp;
  1885. }
  1886. /*
  1887. * We didn't find a corresponding entry in the table, so return 0 so
  1888. * that the buffer is NOT cancelled.
  1889. */
  1890. ASSERT(!(flags & XFS_BLF_CANCEL));
  1891. return NULL;
  1892. }
  1893. /*
  1894. * If the buffer is being cancelled then return 1 so that it will be cancelled,
  1895. * otherwise return 0. If the buffer is actually a buffer cancel item
  1896. * (XFS_BLF_CANCEL is set), then decrement the refcount on the entry in the
  1897. * table and remove it from the table if this is the last reference.
  1898. *
  1899. * We remove the cancel record from the table when we encounter its last
  1900. * occurrence in the log so that if the same buffer is re-used again after its
  1901. * last cancellation we actually replay the changes made at that point.
  1902. */
  1903. STATIC int
  1904. xlog_check_buffer_cancelled(
  1905. struct xlog *log,
  1906. xfs_daddr_t blkno,
  1907. uint len,
  1908. unsigned short flags)
  1909. {
  1910. struct xfs_buf_cancel *bcp;
  1911. bcp = xlog_peek_buffer_cancelled(log, blkno, len, flags);
  1912. if (!bcp)
  1913. return 0;
  1914. /*
  1915. * We've go a match, so return 1 so that the recovery of this buffer
  1916. * is cancelled. If this buffer is actually a buffer cancel log
  1917. * item, then decrement the refcount on the one in the table and
  1918. * remove it if this is the last reference.
  1919. */
  1920. if (flags & XFS_BLF_CANCEL) {
  1921. if (--bcp->bc_refcount == 0) {
  1922. list_del(&bcp->bc_list);
  1923. kmem_free(bcp);
  1924. }
  1925. }
  1926. return 1;
  1927. }
  1928. /*
  1929. * Perform recovery for a buffer full of inodes. In these buffers, the only
  1930. * data which should be recovered is that which corresponds to the
  1931. * di_next_unlinked pointers in the on disk inode structures. The rest of the
  1932. * data for the inodes is always logged through the inodes themselves rather
  1933. * than the inode buffer and is recovered in xlog_recover_inode_pass2().
  1934. *
  1935. * The only time when buffers full of inodes are fully recovered is when the
  1936. * buffer is full of newly allocated inodes. In this case the buffer will
  1937. * not be marked as an inode buffer and so will be sent to
  1938. * xlog_recover_do_reg_buffer() below during recovery.
  1939. */
  1940. STATIC int
  1941. xlog_recover_do_inode_buffer(
  1942. struct xfs_mount *mp,
  1943. xlog_recover_item_t *item,
  1944. struct xfs_buf *bp,
  1945. xfs_buf_log_format_t *buf_f)
  1946. {
  1947. int i;
  1948. int item_index = 0;
  1949. int bit = 0;
  1950. int nbits = 0;
  1951. int reg_buf_offset = 0;
  1952. int reg_buf_bytes = 0;
  1953. int next_unlinked_offset;
  1954. int inodes_per_buf;
  1955. xfs_agino_t *logged_nextp;
  1956. xfs_agino_t *buffer_nextp;
  1957. trace_xfs_log_recover_buf_inode_buf(mp->m_log, buf_f);
  1958. /*
  1959. * Post recovery validation only works properly on CRC enabled
  1960. * filesystems.
  1961. */
  1962. if (xfs_sb_version_hascrc(&mp->m_sb))
  1963. bp->b_ops = &xfs_inode_buf_ops;
  1964. inodes_per_buf = BBTOB(bp->b_io_length) >> mp->m_sb.sb_inodelog;
  1965. for (i = 0; i < inodes_per_buf; i++) {
  1966. next_unlinked_offset = (i * mp->m_sb.sb_inodesize) +
  1967. offsetof(xfs_dinode_t, di_next_unlinked);
  1968. while (next_unlinked_offset >=
  1969. (reg_buf_offset + reg_buf_bytes)) {
  1970. /*
  1971. * The next di_next_unlinked field is beyond
  1972. * the current logged region. Find the next
  1973. * logged region that contains or is beyond
  1974. * the current di_next_unlinked field.
  1975. */
  1976. bit += nbits;
  1977. bit = xfs_next_bit(buf_f->blf_data_map,
  1978. buf_f->blf_map_size, bit);
  1979. /*
  1980. * If there are no more logged regions in the
  1981. * buffer, then we're done.
  1982. */
  1983. if (bit == -1)
  1984. return 0;
  1985. nbits = xfs_contig_bits(buf_f->blf_data_map,
  1986. buf_f->blf_map_size, bit);
  1987. ASSERT(nbits > 0);
  1988. reg_buf_offset = bit << XFS_BLF_SHIFT;
  1989. reg_buf_bytes = nbits << XFS_BLF_SHIFT;
  1990. item_index++;
  1991. }
  1992. /*
  1993. * If the current logged region starts after the current
  1994. * di_next_unlinked field, then move on to the next
  1995. * di_next_unlinked field.
  1996. */
  1997. if (next_unlinked_offset < reg_buf_offset)
  1998. continue;
  1999. ASSERT(item->ri_buf[item_index].i_addr != NULL);
  2000. ASSERT((item->ri_buf[item_index].i_len % XFS_BLF_CHUNK) == 0);
  2001. ASSERT((reg_buf_offset + reg_buf_bytes) <=
  2002. BBTOB(bp->b_io_length));
  2003. /*
  2004. * The current logged region contains a copy of the
  2005. * current di_next_unlinked field. Extract its value
  2006. * and copy it to the buffer copy.
  2007. */
  2008. logged_nextp = item->ri_buf[item_index].i_addr +
  2009. next_unlinked_offset - reg_buf_offset;
  2010. if (unlikely(*logged_nextp == 0)) {
  2011. xfs_alert(mp,
  2012. "Bad inode buffer log record (ptr = 0x%p, bp = 0x%p). "
  2013. "Trying to replay bad (0) inode di_next_unlinked field.",
  2014. item, bp);
  2015. XFS_ERROR_REPORT("xlog_recover_do_inode_buf",
  2016. XFS_ERRLEVEL_LOW, mp);
  2017. return -EFSCORRUPTED;
  2018. }
  2019. buffer_nextp = xfs_buf_offset(bp, next_unlinked_offset);
  2020. *buffer_nextp = *logged_nextp;
  2021. /*
  2022. * If necessary, recalculate the CRC in the on-disk inode. We
  2023. * have to leave the inode in a consistent state for whoever
  2024. * reads it next....
  2025. */
  2026. xfs_dinode_calc_crc(mp,
  2027. xfs_buf_offset(bp, i * mp->m_sb.sb_inodesize));
  2028. }
  2029. return 0;
  2030. }
  2031. /*
  2032. * V5 filesystems know the age of the buffer on disk being recovered. We can
  2033. * have newer objects on disk than we are replaying, and so for these cases we
  2034. * don't want to replay the current change as that will make the buffer contents
  2035. * temporarily invalid on disk.
  2036. *
  2037. * The magic number might not match the buffer type we are going to recover
  2038. * (e.g. reallocated blocks), so we ignore the xfs_buf_log_format flags. Hence
  2039. * extract the LSN of the existing object in the buffer based on it's current
  2040. * magic number. If we don't recognise the magic number in the buffer, then
  2041. * return a LSN of -1 so that the caller knows it was an unrecognised block and
  2042. * so can recover the buffer.
  2043. *
  2044. * Note: we cannot rely solely on magic number matches to determine that the
  2045. * buffer has a valid LSN - we also need to verify that it belongs to this
  2046. * filesystem, so we need to extract the object's LSN and compare it to that
  2047. * which we read from the superblock. If the UUIDs don't match, then we've got a
  2048. * stale metadata block from an old filesystem instance that we need to recover
  2049. * over the top of.
  2050. */
  2051. static xfs_lsn_t
  2052. xlog_recover_get_buf_lsn(
  2053. struct xfs_mount *mp,
  2054. struct xfs_buf *bp)
  2055. {
  2056. uint32_t magic32;
  2057. uint16_t magic16;
  2058. uint16_t magicda;
  2059. void *blk = bp->b_addr;
  2060. uuid_t *uuid;
  2061. xfs_lsn_t lsn = -1;
  2062. /* v4 filesystems always recover immediately */
  2063. if (!xfs_sb_version_hascrc(&mp->m_sb))
  2064. goto recover_immediately;
  2065. magic32 = be32_to_cpu(*(__be32 *)blk);
  2066. switch (magic32) {
  2067. case XFS_ABTB_CRC_MAGIC:
  2068. case XFS_ABTC_CRC_MAGIC:
  2069. case XFS_ABTB_MAGIC:
  2070. case XFS_ABTC_MAGIC:
  2071. case XFS_RMAP_CRC_MAGIC:
  2072. case XFS_REFC_CRC_MAGIC:
  2073. case XFS_IBT_CRC_MAGIC:
  2074. case XFS_IBT_MAGIC: {
  2075. struct xfs_btree_block *btb = blk;
  2076. lsn = be64_to_cpu(btb->bb_u.s.bb_lsn);
  2077. uuid = &btb->bb_u.s.bb_uuid;
  2078. break;
  2079. }
  2080. case XFS_BMAP_CRC_MAGIC:
  2081. case XFS_BMAP_MAGIC: {
  2082. struct xfs_btree_block *btb = blk;
  2083. lsn = be64_to_cpu(btb->bb_u.l.bb_lsn);
  2084. uuid = &btb->bb_u.l.bb_uuid;
  2085. break;
  2086. }
  2087. case XFS_AGF_MAGIC:
  2088. lsn = be64_to_cpu(((struct xfs_agf *)blk)->agf_lsn);
  2089. uuid = &((struct xfs_agf *)blk)->agf_uuid;
  2090. break;
  2091. case XFS_AGFL_MAGIC:
  2092. lsn = be64_to_cpu(((struct xfs_agfl *)blk)->agfl_lsn);
  2093. uuid = &((struct xfs_agfl *)blk)->agfl_uuid;
  2094. break;
  2095. case XFS_AGI_MAGIC:
  2096. lsn = be64_to_cpu(((struct xfs_agi *)blk)->agi_lsn);
  2097. uuid = &((struct xfs_agi *)blk)->agi_uuid;
  2098. break;
  2099. case XFS_SYMLINK_MAGIC:
  2100. lsn = be64_to_cpu(((struct xfs_dsymlink_hdr *)blk)->sl_lsn);
  2101. uuid = &((struct xfs_dsymlink_hdr *)blk)->sl_uuid;
  2102. break;
  2103. case XFS_DIR3_BLOCK_MAGIC:
  2104. case XFS_DIR3_DATA_MAGIC:
  2105. case XFS_DIR3_FREE_MAGIC:
  2106. lsn = be64_to_cpu(((struct xfs_dir3_blk_hdr *)blk)->lsn);
  2107. uuid = &((struct xfs_dir3_blk_hdr *)blk)->uuid;
  2108. break;
  2109. case XFS_ATTR3_RMT_MAGIC:
  2110. /*
  2111. * Remote attr blocks are written synchronously, rather than
  2112. * being logged. That means they do not contain a valid LSN
  2113. * (i.e. transactionally ordered) in them, and hence any time we
  2114. * see a buffer to replay over the top of a remote attribute
  2115. * block we should simply do so.
  2116. */
  2117. goto recover_immediately;
  2118. case XFS_SB_MAGIC:
  2119. /*
  2120. * superblock uuids are magic. We may or may not have a
  2121. * sb_meta_uuid on disk, but it will be set in the in-core
  2122. * superblock. We set the uuid pointer for verification
  2123. * according to the superblock feature mask to ensure we check
  2124. * the relevant UUID in the superblock.
  2125. */
  2126. lsn = be64_to_cpu(((struct xfs_dsb *)blk)->sb_lsn);
  2127. if (xfs_sb_version_hasmetauuid(&mp->m_sb))
  2128. uuid = &((struct xfs_dsb *)blk)->sb_meta_uuid;
  2129. else
  2130. uuid = &((struct xfs_dsb *)blk)->sb_uuid;
  2131. break;
  2132. default:
  2133. break;
  2134. }
  2135. if (lsn != (xfs_lsn_t)-1) {
  2136. if (!uuid_equal(&mp->m_sb.sb_meta_uuid, uuid))
  2137. goto recover_immediately;
  2138. return lsn;
  2139. }
  2140. magicda = be16_to_cpu(((struct xfs_da_blkinfo *)blk)->magic);
  2141. switch (magicda) {
  2142. case XFS_DIR3_LEAF1_MAGIC:
  2143. case XFS_DIR3_LEAFN_MAGIC:
  2144. case XFS_DA3_NODE_MAGIC:
  2145. lsn = be64_to_cpu(((struct xfs_da3_blkinfo *)blk)->lsn);
  2146. uuid = &((struct xfs_da3_blkinfo *)blk)->uuid;
  2147. break;
  2148. default:
  2149. break;
  2150. }
  2151. if (lsn != (xfs_lsn_t)-1) {
  2152. if (!uuid_equal(&mp->m_sb.sb_uuid, uuid))
  2153. goto recover_immediately;
  2154. return lsn;
  2155. }
  2156. /*
  2157. * We do individual object checks on dquot and inode buffers as they
  2158. * have their own individual LSN records. Also, we could have a stale
  2159. * buffer here, so we have to at least recognise these buffer types.
  2160. *
  2161. * A notd complexity here is inode unlinked list processing - it logs
  2162. * the inode directly in the buffer, but we don't know which inodes have
  2163. * been modified, and there is no global buffer LSN. Hence we need to
  2164. * recover all inode buffer types immediately. This problem will be
  2165. * fixed by logical logging of the unlinked list modifications.
  2166. */
  2167. magic16 = be16_to_cpu(*(__be16 *)blk);
  2168. switch (magic16) {
  2169. case XFS_DQUOT_MAGIC:
  2170. case XFS_DINODE_MAGIC:
  2171. goto recover_immediately;
  2172. default:
  2173. break;
  2174. }
  2175. /* unknown buffer contents, recover immediately */
  2176. recover_immediately:
  2177. return (xfs_lsn_t)-1;
  2178. }
  2179. /*
  2180. * Validate the recovered buffer is of the correct type and attach the
  2181. * appropriate buffer operations to them for writeback. Magic numbers are in a
  2182. * few places:
  2183. * the first 16 bits of the buffer (inode buffer, dquot buffer),
  2184. * the first 32 bits of the buffer (most blocks),
  2185. * inside a struct xfs_da_blkinfo at the start of the buffer.
  2186. */
  2187. static void
  2188. xlog_recover_validate_buf_type(
  2189. struct xfs_mount *mp,
  2190. struct xfs_buf *bp,
  2191. xfs_buf_log_format_t *buf_f,
  2192. xfs_lsn_t current_lsn)
  2193. {
  2194. struct xfs_da_blkinfo *info = bp->b_addr;
  2195. uint32_t magic32;
  2196. uint16_t magic16;
  2197. uint16_t magicda;
  2198. char *warnmsg = NULL;
  2199. /*
  2200. * We can only do post recovery validation on items on CRC enabled
  2201. * fielsystems as we need to know when the buffer was written to be able
  2202. * to determine if we should have replayed the item. If we replay old
  2203. * metadata over a newer buffer, then it will enter a temporarily
  2204. * inconsistent state resulting in verification failures. Hence for now
  2205. * just avoid the verification stage for non-crc filesystems
  2206. */
  2207. if (!xfs_sb_version_hascrc(&mp->m_sb))
  2208. return;
  2209. magic32 = be32_to_cpu(*(__be32 *)bp->b_addr);
  2210. magic16 = be16_to_cpu(*(__be16*)bp->b_addr);
  2211. magicda = be16_to_cpu(info->magic);
  2212. switch (xfs_blft_from_flags(buf_f)) {
  2213. case XFS_BLFT_BTREE_BUF:
  2214. switch (magic32) {
  2215. case XFS_ABTB_CRC_MAGIC:
  2216. case XFS_ABTC_CRC_MAGIC:
  2217. case XFS_ABTB_MAGIC:
  2218. case XFS_ABTC_MAGIC:
  2219. bp->b_ops = &xfs_allocbt_buf_ops;
  2220. break;
  2221. case XFS_IBT_CRC_MAGIC:
  2222. case XFS_FIBT_CRC_MAGIC:
  2223. case XFS_IBT_MAGIC:
  2224. case XFS_FIBT_MAGIC:
  2225. bp->b_ops = &xfs_inobt_buf_ops;
  2226. break;
  2227. case XFS_BMAP_CRC_MAGIC:
  2228. case XFS_BMAP_MAGIC:
  2229. bp->b_ops = &xfs_bmbt_buf_ops;
  2230. break;
  2231. case XFS_RMAP_CRC_MAGIC:
  2232. bp->b_ops = &xfs_rmapbt_buf_ops;
  2233. break;
  2234. case XFS_REFC_CRC_MAGIC:
  2235. bp->b_ops = &xfs_refcountbt_buf_ops;
  2236. break;
  2237. default:
  2238. warnmsg = "Bad btree block magic!";
  2239. break;
  2240. }
  2241. break;
  2242. case XFS_BLFT_AGF_BUF:
  2243. if (magic32 != XFS_AGF_MAGIC) {
  2244. warnmsg = "Bad AGF block magic!";
  2245. break;
  2246. }
  2247. bp->b_ops = &xfs_agf_buf_ops;
  2248. break;
  2249. case XFS_BLFT_AGFL_BUF:
  2250. if (magic32 != XFS_AGFL_MAGIC) {
  2251. warnmsg = "Bad AGFL block magic!";
  2252. break;
  2253. }
  2254. bp->b_ops = &xfs_agfl_buf_ops;
  2255. break;
  2256. case XFS_BLFT_AGI_BUF:
  2257. if (magic32 != XFS_AGI_MAGIC) {
  2258. warnmsg = "Bad AGI block magic!";
  2259. break;
  2260. }
  2261. bp->b_ops = &xfs_agi_buf_ops;
  2262. break;
  2263. case XFS_BLFT_UDQUOT_BUF:
  2264. case XFS_BLFT_PDQUOT_BUF:
  2265. case XFS_BLFT_GDQUOT_BUF:
  2266. #ifdef CONFIG_XFS_QUOTA
  2267. if (magic16 != XFS_DQUOT_MAGIC) {
  2268. warnmsg = "Bad DQUOT block magic!";
  2269. break;
  2270. }
  2271. bp->b_ops = &xfs_dquot_buf_ops;
  2272. #else
  2273. xfs_alert(mp,
  2274. "Trying to recover dquots without QUOTA support built in!");
  2275. ASSERT(0);
  2276. #endif
  2277. break;
  2278. case XFS_BLFT_DINO_BUF:
  2279. if (magic16 != XFS_DINODE_MAGIC) {
  2280. warnmsg = "Bad INODE block magic!";
  2281. break;
  2282. }
  2283. bp->b_ops = &xfs_inode_buf_ops;
  2284. break;
  2285. case XFS_BLFT_SYMLINK_BUF:
  2286. if (magic32 != XFS_SYMLINK_MAGIC) {
  2287. warnmsg = "Bad symlink block magic!";
  2288. break;
  2289. }
  2290. bp->b_ops = &xfs_symlink_buf_ops;
  2291. break;
  2292. case XFS_BLFT_DIR_BLOCK_BUF:
  2293. if (magic32 != XFS_DIR2_BLOCK_MAGIC &&
  2294. magic32 != XFS_DIR3_BLOCK_MAGIC) {
  2295. warnmsg = "Bad dir block magic!";
  2296. break;
  2297. }
  2298. bp->b_ops = &xfs_dir3_block_buf_ops;
  2299. break;
  2300. case XFS_BLFT_DIR_DATA_BUF:
  2301. if (magic32 != XFS_DIR2_DATA_MAGIC &&
  2302. magic32 != XFS_DIR3_DATA_MAGIC) {
  2303. warnmsg = "Bad dir data magic!";
  2304. break;
  2305. }
  2306. bp->b_ops = &xfs_dir3_data_buf_ops;
  2307. break;
  2308. case XFS_BLFT_DIR_FREE_BUF:
  2309. if (magic32 != XFS_DIR2_FREE_MAGIC &&
  2310. magic32 != XFS_DIR3_FREE_MAGIC) {
  2311. warnmsg = "Bad dir3 free magic!";
  2312. break;
  2313. }
  2314. bp->b_ops = &xfs_dir3_free_buf_ops;
  2315. break;
  2316. case XFS_BLFT_DIR_LEAF1_BUF:
  2317. if (magicda != XFS_DIR2_LEAF1_MAGIC &&
  2318. magicda != XFS_DIR3_LEAF1_MAGIC) {
  2319. warnmsg = "Bad dir leaf1 magic!";
  2320. break;
  2321. }
  2322. bp->b_ops = &xfs_dir3_leaf1_buf_ops;
  2323. break;
  2324. case XFS_BLFT_DIR_LEAFN_BUF:
  2325. if (magicda != XFS_DIR2_LEAFN_MAGIC &&
  2326. magicda != XFS_DIR3_LEAFN_MAGIC) {
  2327. warnmsg = "Bad dir leafn magic!";
  2328. break;
  2329. }
  2330. bp->b_ops = &xfs_dir3_leafn_buf_ops;
  2331. break;
  2332. case XFS_BLFT_DA_NODE_BUF:
  2333. if (magicda != XFS_DA_NODE_MAGIC &&
  2334. magicda != XFS_DA3_NODE_MAGIC) {
  2335. warnmsg = "Bad da node magic!";
  2336. break;
  2337. }
  2338. bp->b_ops = &xfs_da3_node_buf_ops;
  2339. break;
  2340. case XFS_BLFT_ATTR_LEAF_BUF:
  2341. if (magicda != XFS_ATTR_LEAF_MAGIC &&
  2342. magicda != XFS_ATTR3_LEAF_MAGIC) {
  2343. warnmsg = "Bad attr leaf magic!";
  2344. break;
  2345. }
  2346. bp->b_ops = &xfs_attr3_leaf_buf_ops;
  2347. break;
  2348. case XFS_BLFT_ATTR_RMT_BUF:
  2349. if (magic32 != XFS_ATTR3_RMT_MAGIC) {
  2350. warnmsg = "Bad attr remote magic!";
  2351. break;
  2352. }
  2353. bp->b_ops = &xfs_attr3_rmt_buf_ops;
  2354. break;
  2355. case XFS_BLFT_SB_BUF:
  2356. if (magic32 != XFS_SB_MAGIC) {
  2357. warnmsg = "Bad SB block magic!";
  2358. break;
  2359. }
  2360. bp->b_ops = &xfs_sb_buf_ops;
  2361. break;
  2362. #ifdef CONFIG_XFS_RT
  2363. case XFS_BLFT_RTBITMAP_BUF:
  2364. case XFS_BLFT_RTSUMMARY_BUF:
  2365. /* no magic numbers for verification of RT buffers */
  2366. bp->b_ops = &xfs_rtbuf_ops;
  2367. break;
  2368. #endif /* CONFIG_XFS_RT */
  2369. default:
  2370. xfs_warn(mp, "Unknown buffer type %d!",
  2371. xfs_blft_from_flags(buf_f));
  2372. break;
  2373. }
  2374. /*
  2375. * Nothing else to do in the case of a NULL current LSN as this means
  2376. * the buffer is more recent than the change in the log and will be
  2377. * skipped.
  2378. */
  2379. if (current_lsn == NULLCOMMITLSN)
  2380. return;
  2381. if (warnmsg) {
  2382. xfs_warn(mp, warnmsg);
  2383. ASSERT(0);
  2384. }
  2385. /*
  2386. * We must update the metadata LSN of the buffer as it is written out to
  2387. * ensure that older transactions never replay over this one and corrupt
  2388. * the buffer. This can occur if log recovery is interrupted at some
  2389. * point after the current transaction completes, at which point a
  2390. * subsequent mount starts recovery from the beginning.
  2391. *
  2392. * Write verifiers update the metadata LSN from log items attached to
  2393. * the buffer. Therefore, initialize a bli purely to carry the LSN to
  2394. * the verifier. We'll clean it up in our ->iodone() callback.
  2395. */
  2396. if (bp->b_ops) {
  2397. struct xfs_buf_log_item *bip;
  2398. ASSERT(!bp->b_iodone || bp->b_iodone == xlog_recover_iodone);
  2399. bp->b_iodone = xlog_recover_iodone;
  2400. xfs_buf_item_init(bp, mp);
  2401. bip = bp->b_fspriv;
  2402. bip->bli_item.li_lsn = current_lsn;
  2403. }
  2404. }
  2405. /*
  2406. * Perform a 'normal' buffer recovery. Each logged region of the
  2407. * buffer should be copied over the corresponding region in the
  2408. * given buffer. The bitmap in the buf log format structure indicates
  2409. * where to place the logged data.
  2410. */
  2411. STATIC void
  2412. xlog_recover_do_reg_buffer(
  2413. struct xfs_mount *mp,
  2414. xlog_recover_item_t *item,
  2415. struct xfs_buf *bp,
  2416. xfs_buf_log_format_t *buf_f,
  2417. xfs_lsn_t current_lsn)
  2418. {
  2419. int i;
  2420. int bit;
  2421. int nbits;
  2422. int error;
  2423. trace_xfs_log_recover_buf_reg_buf(mp->m_log, buf_f);
  2424. bit = 0;
  2425. i = 1; /* 0 is the buf format structure */
  2426. while (1) {
  2427. bit = xfs_next_bit(buf_f->blf_data_map,
  2428. buf_f->blf_map_size, bit);
  2429. if (bit == -1)
  2430. break;
  2431. nbits = xfs_contig_bits(buf_f->blf_data_map,
  2432. buf_f->blf_map_size, bit);
  2433. ASSERT(nbits > 0);
  2434. ASSERT(item->ri_buf[i].i_addr != NULL);
  2435. ASSERT(item->ri_buf[i].i_len % XFS_BLF_CHUNK == 0);
  2436. ASSERT(BBTOB(bp->b_io_length) >=
  2437. ((uint)bit << XFS_BLF_SHIFT) + (nbits << XFS_BLF_SHIFT));
  2438. /*
  2439. * The dirty regions logged in the buffer, even though
  2440. * contiguous, may span multiple chunks. This is because the
  2441. * dirty region may span a physical page boundary in a buffer
  2442. * and hence be split into two separate vectors for writing into
  2443. * the log. Hence we need to trim nbits back to the length of
  2444. * the current region being copied out of the log.
  2445. */
  2446. if (item->ri_buf[i].i_len < (nbits << XFS_BLF_SHIFT))
  2447. nbits = item->ri_buf[i].i_len >> XFS_BLF_SHIFT;
  2448. /*
  2449. * Do a sanity check if this is a dquot buffer. Just checking
  2450. * the first dquot in the buffer should do. XXXThis is
  2451. * probably a good thing to do for other buf types also.
  2452. */
  2453. error = 0;
  2454. if (buf_f->blf_flags &
  2455. (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
  2456. if (item->ri_buf[i].i_addr == NULL) {
  2457. xfs_alert(mp,
  2458. "XFS: NULL dquot in %s.", __func__);
  2459. goto next;
  2460. }
  2461. if (item->ri_buf[i].i_len < sizeof(xfs_disk_dquot_t)) {
  2462. xfs_alert(mp,
  2463. "XFS: dquot too small (%d) in %s.",
  2464. item->ri_buf[i].i_len, __func__);
  2465. goto next;
  2466. }
  2467. error = xfs_dqcheck(mp, item->ri_buf[i].i_addr,
  2468. -1, 0, XFS_QMOPT_DOWARN,
  2469. "dquot_buf_recover");
  2470. if (error)
  2471. goto next;
  2472. }
  2473. memcpy(xfs_buf_offset(bp,
  2474. (uint)bit << XFS_BLF_SHIFT), /* dest */
  2475. item->ri_buf[i].i_addr, /* source */
  2476. nbits<<XFS_BLF_SHIFT); /* length */
  2477. next:
  2478. i++;
  2479. bit += nbits;
  2480. }
  2481. /* Shouldn't be any more regions */
  2482. ASSERT(i == item->ri_total);
  2483. xlog_recover_validate_buf_type(mp, bp, buf_f, current_lsn);
  2484. }
  2485. /*
  2486. * Perform a dquot buffer recovery.
  2487. * Simple algorithm: if we have found a QUOTAOFF log item of the same type
  2488. * (ie. USR or GRP), then just toss this buffer away; don't recover it.
  2489. * Else, treat it as a regular buffer and do recovery.
  2490. *
  2491. * Return false if the buffer was tossed and true if we recovered the buffer to
  2492. * indicate to the caller if the buffer needs writing.
  2493. */
  2494. STATIC bool
  2495. xlog_recover_do_dquot_buffer(
  2496. struct xfs_mount *mp,
  2497. struct xlog *log,
  2498. struct xlog_recover_item *item,
  2499. struct xfs_buf *bp,
  2500. struct xfs_buf_log_format *buf_f)
  2501. {
  2502. uint type;
  2503. trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
  2504. /*
  2505. * Filesystems are required to send in quota flags at mount time.
  2506. */
  2507. if (!mp->m_qflags)
  2508. return false;
  2509. type = 0;
  2510. if (buf_f->blf_flags & XFS_BLF_UDQUOT_BUF)
  2511. type |= XFS_DQ_USER;
  2512. if (buf_f->blf_flags & XFS_BLF_PDQUOT_BUF)
  2513. type |= XFS_DQ_PROJ;
  2514. if (buf_f->blf_flags & XFS_BLF_GDQUOT_BUF)
  2515. type |= XFS_DQ_GROUP;
  2516. /*
  2517. * This type of quotas was turned off, so ignore this buffer
  2518. */
  2519. if (log->l_quotaoffs_flag & type)
  2520. return false;
  2521. xlog_recover_do_reg_buffer(mp, item, bp, buf_f, NULLCOMMITLSN);
  2522. return true;
  2523. }
  2524. /*
  2525. * This routine replays a modification made to a buffer at runtime.
  2526. * There are actually two types of buffer, regular and inode, which
  2527. * are handled differently. Inode buffers are handled differently
  2528. * in that we only recover a specific set of data from them, namely
  2529. * the inode di_next_unlinked fields. This is because all other inode
  2530. * data is actually logged via inode records and any data we replay
  2531. * here which overlaps that may be stale.
  2532. *
  2533. * When meta-data buffers are freed at run time we log a buffer item
  2534. * with the XFS_BLF_CANCEL bit set to indicate that previous copies
  2535. * of the buffer in the log should not be replayed at recovery time.
  2536. * This is so that if the blocks covered by the buffer are reused for
  2537. * file data before we crash we don't end up replaying old, freed
  2538. * meta-data into a user's file.
  2539. *
  2540. * To handle the cancellation of buffer log items, we make two passes
  2541. * over the log during recovery. During the first we build a table of
  2542. * those buffers which have been cancelled, and during the second we
  2543. * only replay those buffers which do not have corresponding cancel
  2544. * records in the table. See xlog_recover_buffer_pass[1,2] above
  2545. * for more details on the implementation of the table of cancel records.
  2546. */
  2547. STATIC int
  2548. xlog_recover_buffer_pass2(
  2549. struct xlog *log,
  2550. struct list_head *buffer_list,
  2551. struct xlog_recover_item *item,
  2552. xfs_lsn_t current_lsn)
  2553. {
  2554. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  2555. xfs_mount_t *mp = log->l_mp;
  2556. xfs_buf_t *bp;
  2557. int error;
  2558. uint buf_flags;
  2559. xfs_lsn_t lsn;
  2560. /*
  2561. * In this pass we only want to recover all the buffers which have
  2562. * not been cancelled and are not cancellation buffers themselves.
  2563. */
  2564. if (xlog_check_buffer_cancelled(log, buf_f->blf_blkno,
  2565. buf_f->blf_len, buf_f->blf_flags)) {
  2566. trace_xfs_log_recover_buf_cancel(log, buf_f);
  2567. return 0;
  2568. }
  2569. trace_xfs_log_recover_buf_recover(log, buf_f);
  2570. buf_flags = 0;
  2571. if (buf_f->blf_flags & XFS_BLF_INODE_BUF)
  2572. buf_flags |= XBF_UNMAPPED;
  2573. bp = xfs_buf_read(mp->m_ddev_targp, buf_f->blf_blkno, buf_f->blf_len,
  2574. buf_flags, NULL);
  2575. if (!bp)
  2576. return -ENOMEM;
  2577. error = bp->b_error;
  2578. if (error) {
  2579. xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#1)");
  2580. goto out_release;
  2581. }
  2582. /*
  2583. * Recover the buffer only if we get an LSN from it and it's less than
  2584. * the lsn of the transaction we are replaying.
  2585. *
  2586. * Note that we have to be extremely careful of readahead here.
  2587. * Readahead does not attach verfiers to the buffers so if we don't
  2588. * actually do any replay after readahead because of the LSN we found
  2589. * in the buffer if more recent than that current transaction then we
  2590. * need to attach the verifier directly. Failure to do so can lead to
  2591. * future recovery actions (e.g. EFI and unlinked list recovery) can
  2592. * operate on the buffers and they won't get the verifier attached. This
  2593. * can lead to blocks on disk having the correct content but a stale
  2594. * CRC.
  2595. *
  2596. * It is safe to assume these clean buffers are currently up to date.
  2597. * If the buffer is dirtied by a later transaction being replayed, then
  2598. * the verifier will be reset to match whatever recover turns that
  2599. * buffer into.
  2600. */
  2601. lsn = xlog_recover_get_buf_lsn(mp, bp);
  2602. if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
  2603. trace_xfs_log_recover_buf_skip(log, buf_f);
  2604. xlog_recover_validate_buf_type(mp, bp, buf_f, NULLCOMMITLSN);
  2605. goto out_release;
  2606. }
  2607. if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
  2608. error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
  2609. if (error)
  2610. goto out_release;
  2611. } else if (buf_f->blf_flags &
  2612. (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
  2613. bool dirty;
  2614. dirty = xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
  2615. if (!dirty)
  2616. goto out_release;
  2617. } else {
  2618. xlog_recover_do_reg_buffer(mp, item, bp, buf_f, current_lsn);
  2619. }
  2620. /*
  2621. * Perform delayed write on the buffer. Asynchronous writes will be
  2622. * slower when taking into account all the buffers to be flushed.
  2623. *
  2624. * Also make sure that only inode buffers with good sizes stay in
  2625. * the buffer cache. The kernel moves inodes in buffers of 1 block
  2626. * or mp->m_inode_cluster_size bytes, whichever is bigger. The inode
  2627. * buffers in the log can be a different size if the log was generated
  2628. * by an older kernel using unclustered inode buffers or a newer kernel
  2629. * running with a different inode cluster size. Regardless, if the
  2630. * the inode buffer size isn't MAX(blocksize, mp->m_inode_cluster_size)
  2631. * for *our* value of mp->m_inode_cluster_size, then we need to keep
  2632. * the buffer out of the buffer cache so that the buffer won't
  2633. * overlap with future reads of those inodes.
  2634. */
  2635. if (XFS_DINODE_MAGIC ==
  2636. be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
  2637. (BBTOB(bp->b_io_length) != MAX(log->l_mp->m_sb.sb_blocksize,
  2638. (uint32_t)log->l_mp->m_inode_cluster_size))) {
  2639. xfs_buf_stale(bp);
  2640. error = xfs_bwrite(bp);
  2641. } else {
  2642. ASSERT(bp->b_target->bt_mount == mp);
  2643. bp->b_iodone = xlog_recover_iodone;
  2644. xfs_buf_delwri_queue(bp, buffer_list);
  2645. }
  2646. out_release:
  2647. xfs_buf_relse(bp);
  2648. return error;
  2649. }
  2650. /*
  2651. * Inode fork owner changes
  2652. *
  2653. * If we have been told that we have to reparent the inode fork, it's because an
  2654. * extent swap operation on a CRC enabled filesystem has been done and we are
  2655. * replaying it. We need to walk the BMBT of the appropriate fork and change the
  2656. * owners of it.
  2657. *
  2658. * The complexity here is that we don't have an inode context to work with, so
  2659. * after we've replayed the inode we need to instantiate one. This is where the
  2660. * fun begins.
  2661. *
  2662. * We are in the middle of log recovery, so we can't run transactions. That
  2663. * means we cannot use cache coherent inode instantiation via xfs_iget(), as
  2664. * that will result in the corresponding iput() running the inode through
  2665. * xfs_inactive(). If we've just replayed an inode core that changes the link
  2666. * count to zero (i.e. it's been unlinked), then xfs_inactive() will run
  2667. * transactions (bad!).
  2668. *
  2669. * So, to avoid this, we instantiate an inode directly from the inode core we've
  2670. * just recovered. We have the buffer still locked, and all we really need to
  2671. * instantiate is the inode core and the forks being modified. We can do this
  2672. * manually, then run the inode btree owner change, and then tear down the
  2673. * xfs_inode without having to run any transactions at all.
  2674. *
  2675. * Also, because we don't have a transaction context available here but need to
  2676. * gather all the buffers we modify for writeback so we pass the buffer_list
  2677. * instead for the operation to use.
  2678. */
  2679. STATIC int
  2680. xfs_recover_inode_owner_change(
  2681. struct xfs_mount *mp,
  2682. struct xfs_dinode *dip,
  2683. struct xfs_inode_log_format *in_f,
  2684. struct list_head *buffer_list)
  2685. {
  2686. struct xfs_inode *ip;
  2687. int error;
  2688. ASSERT(in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER));
  2689. ip = xfs_inode_alloc(mp, in_f->ilf_ino);
  2690. if (!ip)
  2691. return -ENOMEM;
  2692. /* instantiate the inode */
  2693. xfs_inode_from_disk(ip, dip);
  2694. ASSERT(ip->i_d.di_version >= 3);
  2695. error = xfs_iformat_fork(ip, dip);
  2696. if (error)
  2697. goto out_free_ip;
  2698. if (in_f->ilf_fields & XFS_ILOG_DOWNER) {
  2699. ASSERT(in_f->ilf_fields & XFS_ILOG_DBROOT);
  2700. error = xfs_bmbt_change_owner(NULL, ip, XFS_DATA_FORK,
  2701. ip->i_ino, buffer_list);
  2702. if (error)
  2703. goto out_free_ip;
  2704. }
  2705. if (in_f->ilf_fields & XFS_ILOG_AOWNER) {
  2706. ASSERT(in_f->ilf_fields & XFS_ILOG_ABROOT);
  2707. error = xfs_bmbt_change_owner(NULL, ip, XFS_ATTR_FORK,
  2708. ip->i_ino, buffer_list);
  2709. if (error)
  2710. goto out_free_ip;
  2711. }
  2712. out_free_ip:
  2713. xfs_inode_free(ip);
  2714. return error;
  2715. }
  2716. STATIC int
  2717. xlog_recover_inode_pass2(
  2718. struct xlog *log,
  2719. struct list_head *buffer_list,
  2720. struct xlog_recover_item *item,
  2721. xfs_lsn_t current_lsn)
  2722. {
  2723. xfs_inode_log_format_t *in_f;
  2724. xfs_mount_t *mp = log->l_mp;
  2725. xfs_buf_t *bp;
  2726. xfs_dinode_t *dip;
  2727. int len;
  2728. char *src;
  2729. char *dest;
  2730. int error;
  2731. int attr_index;
  2732. uint fields;
  2733. struct xfs_log_dinode *ldip;
  2734. uint isize;
  2735. int need_free = 0;
  2736. if (item->ri_buf[0].i_len == sizeof(xfs_inode_log_format_t)) {
  2737. in_f = item->ri_buf[0].i_addr;
  2738. } else {
  2739. in_f = kmem_alloc(sizeof(xfs_inode_log_format_t), KM_SLEEP);
  2740. need_free = 1;
  2741. error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
  2742. if (error)
  2743. goto error;
  2744. }
  2745. /*
  2746. * Inode buffers can be freed, look out for it,
  2747. * and do not replay the inode.
  2748. */
  2749. if (xlog_check_buffer_cancelled(log, in_f->ilf_blkno,
  2750. in_f->ilf_len, 0)) {
  2751. error = 0;
  2752. trace_xfs_log_recover_inode_cancel(log, in_f);
  2753. goto error;
  2754. }
  2755. trace_xfs_log_recover_inode_recover(log, in_f);
  2756. bp = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len, 0,
  2757. &xfs_inode_buf_ops);
  2758. if (!bp) {
  2759. error = -ENOMEM;
  2760. goto error;
  2761. }
  2762. error = bp->b_error;
  2763. if (error) {
  2764. xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#2)");
  2765. goto out_release;
  2766. }
  2767. ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
  2768. dip = xfs_buf_offset(bp, in_f->ilf_boffset);
  2769. /*
  2770. * Make sure the place we're flushing out to really looks
  2771. * like an inode!
  2772. */
  2773. if (unlikely(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))) {
  2774. xfs_alert(mp,
  2775. "%s: Bad inode magic number, dip = 0x%p, dino bp = 0x%p, ino = %Ld",
  2776. __func__, dip, bp, in_f->ilf_ino);
  2777. XFS_ERROR_REPORT("xlog_recover_inode_pass2(1)",
  2778. XFS_ERRLEVEL_LOW, mp);
  2779. error = -EFSCORRUPTED;
  2780. goto out_release;
  2781. }
  2782. ldip = item->ri_buf[1].i_addr;
  2783. if (unlikely(ldip->di_magic != XFS_DINODE_MAGIC)) {
  2784. xfs_alert(mp,
  2785. "%s: Bad inode log record, rec ptr 0x%p, ino %Ld",
  2786. __func__, item, in_f->ilf_ino);
  2787. XFS_ERROR_REPORT("xlog_recover_inode_pass2(2)",
  2788. XFS_ERRLEVEL_LOW, mp);
  2789. error = -EFSCORRUPTED;
  2790. goto out_release;
  2791. }
  2792. /*
  2793. * If the inode has an LSN in it, recover the inode only if it's less
  2794. * than the lsn of the transaction we are replaying. Note: we still
  2795. * need to replay an owner change even though the inode is more recent
  2796. * than the transaction as there is no guarantee that all the btree
  2797. * blocks are more recent than this transaction, too.
  2798. */
  2799. if (dip->di_version >= 3) {
  2800. xfs_lsn_t lsn = be64_to_cpu(dip->di_lsn);
  2801. if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
  2802. trace_xfs_log_recover_inode_skip(log, in_f);
  2803. error = 0;
  2804. goto out_owner_change;
  2805. }
  2806. }
  2807. /*
  2808. * di_flushiter is only valid for v1/2 inodes. All changes for v3 inodes
  2809. * are transactional and if ordering is necessary we can determine that
  2810. * more accurately by the LSN field in the V3 inode core. Don't trust
  2811. * the inode versions we might be changing them here - use the
  2812. * superblock flag to determine whether we need to look at di_flushiter
  2813. * to skip replay when the on disk inode is newer than the log one
  2814. */
  2815. if (!xfs_sb_version_hascrc(&mp->m_sb) &&
  2816. ldip->di_flushiter < be16_to_cpu(dip->di_flushiter)) {
  2817. /*
  2818. * Deal with the wrap case, DI_MAX_FLUSH is less
  2819. * than smaller numbers
  2820. */
  2821. if (be16_to_cpu(dip->di_flushiter) == DI_MAX_FLUSH &&
  2822. ldip->di_flushiter < (DI_MAX_FLUSH >> 1)) {
  2823. /* do nothing */
  2824. } else {
  2825. trace_xfs_log_recover_inode_skip(log, in_f);
  2826. error = 0;
  2827. goto out_release;
  2828. }
  2829. }
  2830. /* Take the opportunity to reset the flush iteration count */
  2831. ldip->di_flushiter = 0;
  2832. if (unlikely(S_ISREG(ldip->di_mode))) {
  2833. if ((ldip->di_format != XFS_DINODE_FMT_EXTENTS) &&
  2834. (ldip->di_format != XFS_DINODE_FMT_BTREE)) {
  2835. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(3)",
  2836. XFS_ERRLEVEL_LOW, mp, ldip);
  2837. xfs_alert(mp,
  2838. "%s: Bad regular inode log record, rec ptr 0x%p, "
  2839. "ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
  2840. __func__, item, dip, bp, in_f->ilf_ino);
  2841. error = -EFSCORRUPTED;
  2842. goto out_release;
  2843. }
  2844. } else if (unlikely(S_ISDIR(ldip->di_mode))) {
  2845. if ((ldip->di_format != XFS_DINODE_FMT_EXTENTS) &&
  2846. (ldip->di_format != XFS_DINODE_FMT_BTREE) &&
  2847. (ldip->di_format != XFS_DINODE_FMT_LOCAL)) {
  2848. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(4)",
  2849. XFS_ERRLEVEL_LOW, mp, ldip);
  2850. xfs_alert(mp,
  2851. "%s: Bad dir inode log record, rec ptr 0x%p, "
  2852. "ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
  2853. __func__, item, dip, bp, in_f->ilf_ino);
  2854. error = -EFSCORRUPTED;
  2855. goto out_release;
  2856. }
  2857. }
  2858. if (unlikely(ldip->di_nextents + ldip->di_anextents > ldip->di_nblocks)){
  2859. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(5)",
  2860. XFS_ERRLEVEL_LOW, mp, ldip);
  2861. xfs_alert(mp,
  2862. "%s: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, "
  2863. "dino bp 0x%p, ino %Ld, total extents = %d, nblocks = %Ld",
  2864. __func__, item, dip, bp, in_f->ilf_ino,
  2865. ldip->di_nextents + ldip->di_anextents,
  2866. ldip->di_nblocks);
  2867. error = -EFSCORRUPTED;
  2868. goto out_release;
  2869. }
  2870. if (unlikely(ldip->di_forkoff > mp->m_sb.sb_inodesize)) {
  2871. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(6)",
  2872. XFS_ERRLEVEL_LOW, mp, ldip);
  2873. xfs_alert(mp,
  2874. "%s: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, "
  2875. "dino bp 0x%p, ino %Ld, forkoff 0x%x", __func__,
  2876. item, dip, bp, in_f->ilf_ino, ldip->di_forkoff);
  2877. error = -EFSCORRUPTED;
  2878. goto out_release;
  2879. }
  2880. isize = xfs_log_dinode_size(ldip->di_version);
  2881. if (unlikely(item->ri_buf[1].i_len > isize)) {
  2882. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(7)",
  2883. XFS_ERRLEVEL_LOW, mp, ldip);
  2884. xfs_alert(mp,
  2885. "%s: Bad inode log record length %d, rec ptr 0x%p",
  2886. __func__, item->ri_buf[1].i_len, item);
  2887. error = -EFSCORRUPTED;
  2888. goto out_release;
  2889. }
  2890. /* recover the log dinode inode into the on disk inode */
  2891. xfs_log_dinode_to_disk(ldip, dip);
  2892. /* the rest is in on-disk format */
  2893. if (item->ri_buf[1].i_len > isize) {
  2894. memcpy((char *)dip + isize,
  2895. item->ri_buf[1].i_addr + isize,
  2896. item->ri_buf[1].i_len - isize);
  2897. }
  2898. fields = in_f->ilf_fields;
  2899. switch (fields & (XFS_ILOG_DEV | XFS_ILOG_UUID)) {
  2900. case XFS_ILOG_DEV:
  2901. xfs_dinode_put_rdev(dip, in_f->ilf_u.ilfu_rdev);
  2902. break;
  2903. case XFS_ILOG_UUID:
  2904. memcpy(XFS_DFORK_DPTR(dip),
  2905. &in_f->ilf_u.ilfu_uuid,
  2906. sizeof(uuid_t));
  2907. break;
  2908. }
  2909. if (in_f->ilf_size == 2)
  2910. goto out_owner_change;
  2911. len = item->ri_buf[2].i_len;
  2912. src = item->ri_buf[2].i_addr;
  2913. ASSERT(in_f->ilf_size <= 4);
  2914. ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
  2915. ASSERT(!(fields & XFS_ILOG_DFORK) ||
  2916. (len == in_f->ilf_dsize));
  2917. switch (fields & XFS_ILOG_DFORK) {
  2918. case XFS_ILOG_DDATA:
  2919. case XFS_ILOG_DEXT:
  2920. memcpy(XFS_DFORK_DPTR(dip), src, len);
  2921. break;
  2922. case XFS_ILOG_DBROOT:
  2923. xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src, len,
  2924. (xfs_bmdr_block_t *)XFS_DFORK_DPTR(dip),
  2925. XFS_DFORK_DSIZE(dip, mp));
  2926. break;
  2927. default:
  2928. /*
  2929. * There are no data fork flags set.
  2930. */
  2931. ASSERT((fields & XFS_ILOG_DFORK) == 0);
  2932. break;
  2933. }
  2934. /*
  2935. * If we logged any attribute data, recover it. There may or
  2936. * may not have been any other non-core data logged in this
  2937. * transaction.
  2938. */
  2939. if (in_f->ilf_fields & XFS_ILOG_AFORK) {
  2940. if (in_f->ilf_fields & XFS_ILOG_DFORK) {
  2941. attr_index = 3;
  2942. } else {
  2943. attr_index = 2;
  2944. }
  2945. len = item->ri_buf[attr_index].i_len;
  2946. src = item->ri_buf[attr_index].i_addr;
  2947. ASSERT(len == in_f->ilf_asize);
  2948. switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
  2949. case XFS_ILOG_ADATA:
  2950. case XFS_ILOG_AEXT:
  2951. dest = XFS_DFORK_APTR(dip);
  2952. ASSERT(len <= XFS_DFORK_ASIZE(dip, mp));
  2953. memcpy(dest, src, len);
  2954. break;
  2955. case XFS_ILOG_ABROOT:
  2956. dest = XFS_DFORK_APTR(dip);
  2957. xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src,
  2958. len, (xfs_bmdr_block_t*)dest,
  2959. XFS_DFORK_ASIZE(dip, mp));
  2960. break;
  2961. default:
  2962. xfs_warn(log->l_mp, "%s: Invalid flag", __func__);
  2963. ASSERT(0);
  2964. error = -EIO;
  2965. goto out_release;
  2966. }
  2967. }
  2968. out_owner_change:
  2969. if (in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER))
  2970. error = xfs_recover_inode_owner_change(mp, dip, in_f,
  2971. buffer_list);
  2972. /* re-generate the checksum. */
  2973. xfs_dinode_calc_crc(log->l_mp, dip);
  2974. ASSERT(bp->b_target->bt_mount == mp);
  2975. bp->b_iodone = xlog_recover_iodone;
  2976. xfs_buf_delwri_queue(bp, buffer_list);
  2977. out_release:
  2978. xfs_buf_relse(bp);
  2979. error:
  2980. if (need_free)
  2981. kmem_free(in_f);
  2982. return error;
  2983. }
  2984. /*
  2985. * Recover QUOTAOFF records. We simply make a note of it in the xlog
  2986. * structure, so that we know not to do any dquot item or dquot buffer recovery,
  2987. * of that type.
  2988. */
  2989. STATIC int
  2990. xlog_recover_quotaoff_pass1(
  2991. struct xlog *log,
  2992. struct xlog_recover_item *item)
  2993. {
  2994. xfs_qoff_logformat_t *qoff_f = item->ri_buf[0].i_addr;
  2995. ASSERT(qoff_f);
  2996. /*
  2997. * The logitem format's flag tells us if this was user quotaoff,
  2998. * group/project quotaoff or both.
  2999. */
  3000. if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
  3001. log->l_quotaoffs_flag |= XFS_DQ_USER;
  3002. if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
  3003. log->l_quotaoffs_flag |= XFS_DQ_PROJ;
  3004. if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
  3005. log->l_quotaoffs_flag |= XFS_DQ_GROUP;
  3006. return 0;
  3007. }
  3008. /*
  3009. * Recover a dquot record
  3010. */
  3011. STATIC int
  3012. xlog_recover_dquot_pass2(
  3013. struct xlog *log,
  3014. struct list_head *buffer_list,
  3015. struct xlog_recover_item *item,
  3016. xfs_lsn_t current_lsn)
  3017. {
  3018. xfs_mount_t *mp = log->l_mp;
  3019. xfs_buf_t *bp;
  3020. struct xfs_disk_dquot *ddq, *recddq;
  3021. int error;
  3022. xfs_dq_logformat_t *dq_f;
  3023. uint type;
  3024. /*
  3025. * Filesystems are required to send in quota flags at mount time.
  3026. */
  3027. if (mp->m_qflags == 0)
  3028. return 0;
  3029. recddq = item->ri_buf[1].i_addr;
  3030. if (recddq == NULL) {
  3031. xfs_alert(log->l_mp, "NULL dquot in %s.", __func__);
  3032. return -EIO;
  3033. }
  3034. if (item->ri_buf[1].i_len < sizeof(xfs_disk_dquot_t)) {
  3035. xfs_alert(log->l_mp, "dquot too small (%d) in %s.",
  3036. item->ri_buf[1].i_len, __func__);
  3037. return -EIO;
  3038. }
  3039. /*
  3040. * This type of quotas was turned off, so ignore this record.
  3041. */
  3042. type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
  3043. ASSERT(type);
  3044. if (log->l_quotaoffs_flag & type)
  3045. return 0;
  3046. /*
  3047. * At this point we know that quota was _not_ turned off.
  3048. * Since the mount flags are not indicating to us otherwise, this
  3049. * must mean that quota is on, and the dquot needs to be replayed.
  3050. * Remember that we may not have fully recovered the superblock yet,
  3051. * so we can't do the usual trick of looking at the SB quota bits.
  3052. *
  3053. * The other possibility, of course, is that the quota subsystem was
  3054. * removed since the last mount - ENOSYS.
  3055. */
  3056. dq_f = item->ri_buf[0].i_addr;
  3057. ASSERT(dq_f);
  3058. error = xfs_dqcheck(mp, recddq, dq_f->qlf_id, 0, XFS_QMOPT_DOWARN,
  3059. "xlog_recover_dquot_pass2 (log copy)");
  3060. if (error)
  3061. return -EIO;
  3062. ASSERT(dq_f->qlf_len == 1);
  3063. /*
  3064. * At this point we are assuming that the dquots have been allocated
  3065. * and hence the buffer has valid dquots stamped in it. It should,
  3066. * therefore, pass verifier validation. If the dquot is bad, then the
  3067. * we'll return an error here, so we don't need to specifically check
  3068. * the dquot in the buffer after the verifier has run.
  3069. */
  3070. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dq_f->qlf_blkno,
  3071. XFS_FSB_TO_BB(mp, dq_f->qlf_len), 0, &bp,
  3072. &xfs_dquot_buf_ops);
  3073. if (error)
  3074. return error;
  3075. ASSERT(bp);
  3076. ddq = xfs_buf_offset(bp, dq_f->qlf_boffset);
  3077. /*
  3078. * If the dquot has an LSN in it, recover the dquot only if it's less
  3079. * than the lsn of the transaction we are replaying.
  3080. */
  3081. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  3082. struct xfs_dqblk *dqb = (struct xfs_dqblk *)ddq;
  3083. xfs_lsn_t lsn = be64_to_cpu(dqb->dd_lsn);
  3084. if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
  3085. goto out_release;
  3086. }
  3087. }
  3088. memcpy(ddq, recddq, item->ri_buf[1].i_len);
  3089. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  3090. xfs_update_cksum((char *)ddq, sizeof(struct xfs_dqblk),
  3091. XFS_DQUOT_CRC_OFF);
  3092. }
  3093. ASSERT(dq_f->qlf_size == 2);
  3094. ASSERT(bp->b_target->bt_mount == mp);
  3095. bp->b_iodone = xlog_recover_iodone;
  3096. xfs_buf_delwri_queue(bp, buffer_list);
  3097. out_release:
  3098. xfs_buf_relse(bp);
  3099. return 0;
  3100. }
  3101. /*
  3102. * This routine is called to create an in-core extent free intent
  3103. * item from the efi format structure which was logged on disk.
  3104. * It allocates an in-core efi, copies the extents from the format
  3105. * structure into it, and adds the efi to the AIL with the given
  3106. * LSN.
  3107. */
  3108. STATIC int
  3109. xlog_recover_efi_pass2(
  3110. struct xlog *log,
  3111. struct xlog_recover_item *item,
  3112. xfs_lsn_t lsn)
  3113. {
  3114. int error;
  3115. struct xfs_mount *mp = log->l_mp;
  3116. struct xfs_efi_log_item *efip;
  3117. struct xfs_efi_log_format *efi_formatp;
  3118. efi_formatp = item->ri_buf[0].i_addr;
  3119. efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
  3120. error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
  3121. if (error) {
  3122. xfs_efi_item_free(efip);
  3123. return error;
  3124. }
  3125. atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
  3126. spin_lock(&log->l_ailp->xa_lock);
  3127. /*
  3128. * The EFI has two references. One for the EFD and one for EFI to ensure
  3129. * it makes it into the AIL. Insert the EFI into the AIL directly and
  3130. * drop the EFI reference. Note that xfs_trans_ail_update() drops the
  3131. * AIL lock.
  3132. */
  3133. xfs_trans_ail_update(log->l_ailp, &efip->efi_item, lsn);
  3134. xfs_efi_release(efip);
  3135. return 0;
  3136. }
  3137. /*
  3138. * This routine is called when an EFD format structure is found in a committed
  3139. * transaction in the log. Its purpose is to cancel the corresponding EFI if it
  3140. * was still in the log. To do this it searches the AIL for the EFI with an id
  3141. * equal to that in the EFD format structure. If we find it we drop the EFD
  3142. * reference, which removes the EFI from the AIL and frees it.
  3143. */
  3144. STATIC int
  3145. xlog_recover_efd_pass2(
  3146. struct xlog *log,
  3147. struct xlog_recover_item *item)
  3148. {
  3149. xfs_efd_log_format_t *efd_formatp;
  3150. xfs_efi_log_item_t *efip = NULL;
  3151. xfs_log_item_t *lip;
  3152. uint64_t efi_id;
  3153. struct xfs_ail_cursor cur;
  3154. struct xfs_ail *ailp = log->l_ailp;
  3155. efd_formatp = item->ri_buf[0].i_addr;
  3156. ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
  3157. ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
  3158. (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
  3159. ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
  3160. efi_id = efd_formatp->efd_efi_id;
  3161. /*
  3162. * Search for the EFI with the id in the EFD format structure in the
  3163. * AIL.
  3164. */
  3165. spin_lock(&ailp->xa_lock);
  3166. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  3167. while (lip != NULL) {
  3168. if (lip->li_type == XFS_LI_EFI) {
  3169. efip = (xfs_efi_log_item_t *)lip;
  3170. if (efip->efi_format.efi_id == efi_id) {
  3171. /*
  3172. * Drop the EFD reference to the EFI. This
  3173. * removes the EFI from the AIL and frees it.
  3174. */
  3175. spin_unlock(&ailp->xa_lock);
  3176. xfs_efi_release(efip);
  3177. spin_lock(&ailp->xa_lock);
  3178. break;
  3179. }
  3180. }
  3181. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  3182. }
  3183. xfs_trans_ail_cursor_done(&cur);
  3184. spin_unlock(&ailp->xa_lock);
  3185. return 0;
  3186. }
  3187. /*
  3188. * This routine is called to create an in-core extent rmap update
  3189. * item from the rui format structure which was logged on disk.
  3190. * It allocates an in-core rui, copies the extents from the format
  3191. * structure into it, and adds the rui to the AIL with the given
  3192. * LSN.
  3193. */
  3194. STATIC int
  3195. xlog_recover_rui_pass2(
  3196. struct xlog *log,
  3197. struct xlog_recover_item *item,
  3198. xfs_lsn_t lsn)
  3199. {
  3200. int error;
  3201. struct xfs_mount *mp = log->l_mp;
  3202. struct xfs_rui_log_item *ruip;
  3203. struct xfs_rui_log_format *rui_formatp;
  3204. rui_formatp = item->ri_buf[0].i_addr;
  3205. ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
  3206. error = xfs_rui_copy_format(&item->ri_buf[0], &ruip->rui_format);
  3207. if (error) {
  3208. xfs_rui_item_free(ruip);
  3209. return error;
  3210. }
  3211. atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
  3212. spin_lock(&log->l_ailp->xa_lock);
  3213. /*
  3214. * The RUI has two references. One for the RUD and one for RUI to ensure
  3215. * it makes it into the AIL. Insert the RUI into the AIL directly and
  3216. * drop the RUI reference. Note that xfs_trans_ail_update() drops the
  3217. * AIL lock.
  3218. */
  3219. xfs_trans_ail_update(log->l_ailp, &ruip->rui_item, lsn);
  3220. xfs_rui_release(ruip);
  3221. return 0;
  3222. }
  3223. /*
  3224. * This routine is called when an RUD format structure is found in a committed
  3225. * transaction in the log. Its purpose is to cancel the corresponding RUI if it
  3226. * was still in the log. To do this it searches the AIL for the RUI with an id
  3227. * equal to that in the RUD format structure. If we find it we drop the RUD
  3228. * reference, which removes the RUI from the AIL and frees it.
  3229. */
  3230. STATIC int
  3231. xlog_recover_rud_pass2(
  3232. struct xlog *log,
  3233. struct xlog_recover_item *item)
  3234. {
  3235. struct xfs_rud_log_format *rud_formatp;
  3236. struct xfs_rui_log_item *ruip = NULL;
  3237. struct xfs_log_item *lip;
  3238. uint64_t rui_id;
  3239. struct xfs_ail_cursor cur;
  3240. struct xfs_ail *ailp = log->l_ailp;
  3241. rud_formatp = item->ri_buf[0].i_addr;
  3242. ASSERT(item->ri_buf[0].i_len == sizeof(struct xfs_rud_log_format));
  3243. rui_id = rud_formatp->rud_rui_id;
  3244. /*
  3245. * Search for the RUI with the id in the RUD format structure in the
  3246. * AIL.
  3247. */
  3248. spin_lock(&ailp->xa_lock);
  3249. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  3250. while (lip != NULL) {
  3251. if (lip->li_type == XFS_LI_RUI) {
  3252. ruip = (struct xfs_rui_log_item *)lip;
  3253. if (ruip->rui_format.rui_id == rui_id) {
  3254. /*
  3255. * Drop the RUD reference to the RUI. This
  3256. * removes the RUI from the AIL and frees it.
  3257. */
  3258. spin_unlock(&ailp->xa_lock);
  3259. xfs_rui_release(ruip);
  3260. spin_lock(&ailp->xa_lock);
  3261. break;
  3262. }
  3263. }
  3264. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  3265. }
  3266. xfs_trans_ail_cursor_done(&cur);
  3267. spin_unlock(&ailp->xa_lock);
  3268. return 0;
  3269. }
  3270. /*
  3271. * Copy an CUI format buffer from the given buf, and into the destination
  3272. * CUI format structure. The CUI/CUD items were designed not to need any
  3273. * special alignment handling.
  3274. */
  3275. static int
  3276. xfs_cui_copy_format(
  3277. struct xfs_log_iovec *buf,
  3278. struct xfs_cui_log_format *dst_cui_fmt)
  3279. {
  3280. struct xfs_cui_log_format *src_cui_fmt;
  3281. uint len;
  3282. src_cui_fmt = buf->i_addr;
  3283. len = xfs_cui_log_format_sizeof(src_cui_fmt->cui_nextents);
  3284. if (buf->i_len == len) {
  3285. memcpy(dst_cui_fmt, src_cui_fmt, len);
  3286. return 0;
  3287. }
  3288. return -EFSCORRUPTED;
  3289. }
  3290. /*
  3291. * This routine is called to create an in-core extent refcount update
  3292. * item from the cui format structure which was logged on disk.
  3293. * It allocates an in-core cui, copies the extents from the format
  3294. * structure into it, and adds the cui to the AIL with the given
  3295. * LSN.
  3296. */
  3297. STATIC int
  3298. xlog_recover_cui_pass2(
  3299. struct xlog *log,
  3300. struct xlog_recover_item *item,
  3301. xfs_lsn_t lsn)
  3302. {
  3303. int error;
  3304. struct xfs_mount *mp = log->l_mp;
  3305. struct xfs_cui_log_item *cuip;
  3306. struct xfs_cui_log_format *cui_formatp;
  3307. cui_formatp = item->ri_buf[0].i_addr;
  3308. cuip = xfs_cui_init(mp, cui_formatp->cui_nextents);
  3309. error = xfs_cui_copy_format(&item->ri_buf[0], &cuip->cui_format);
  3310. if (error) {
  3311. xfs_cui_item_free(cuip);
  3312. return error;
  3313. }
  3314. atomic_set(&cuip->cui_next_extent, cui_formatp->cui_nextents);
  3315. spin_lock(&log->l_ailp->xa_lock);
  3316. /*
  3317. * The CUI has two references. One for the CUD and one for CUI to ensure
  3318. * it makes it into the AIL. Insert the CUI into the AIL directly and
  3319. * drop the CUI reference. Note that xfs_trans_ail_update() drops the
  3320. * AIL lock.
  3321. */
  3322. xfs_trans_ail_update(log->l_ailp, &cuip->cui_item, lsn);
  3323. xfs_cui_release(cuip);
  3324. return 0;
  3325. }
  3326. /*
  3327. * This routine is called when an CUD format structure is found in a committed
  3328. * transaction in the log. Its purpose is to cancel the corresponding CUI if it
  3329. * was still in the log. To do this it searches the AIL for the CUI with an id
  3330. * equal to that in the CUD format structure. If we find it we drop the CUD
  3331. * reference, which removes the CUI from the AIL and frees it.
  3332. */
  3333. STATIC int
  3334. xlog_recover_cud_pass2(
  3335. struct xlog *log,
  3336. struct xlog_recover_item *item)
  3337. {
  3338. struct xfs_cud_log_format *cud_formatp;
  3339. struct xfs_cui_log_item *cuip = NULL;
  3340. struct xfs_log_item *lip;
  3341. uint64_t cui_id;
  3342. struct xfs_ail_cursor cur;
  3343. struct xfs_ail *ailp = log->l_ailp;
  3344. cud_formatp = item->ri_buf[0].i_addr;
  3345. if (item->ri_buf[0].i_len != sizeof(struct xfs_cud_log_format))
  3346. return -EFSCORRUPTED;
  3347. cui_id = cud_formatp->cud_cui_id;
  3348. /*
  3349. * Search for the CUI with the id in the CUD format structure in the
  3350. * AIL.
  3351. */
  3352. spin_lock(&ailp->xa_lock);
  3353. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  3354. while (lip != NULL) {
  3355. if (lip->li_type == XFS_LI_CUI) {
  3356. cuip = (struct xfs_cui_log_item *)lip;
  3357. if (cuip->cui_format.cui_id == cui_id) {
  3358. /*
  3359. * Drop the CUD reference to the CUI. This
  3360. * removes the CUI from the AIL and frees it.
  3361. */
  3362. spin_unlock(&ailp->xa_lock);
  3363. xfs_cui_release(cuip);
  3364. spin_lock(&ailp->xa_lock);
  3365. break;
  3366. }
  3367. }
  3368. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  3369. }
  3370. xfs_trans_ail_cursor_done(&cur);
  3371. spin_unlock(&ailp->xa_lock);
  3372. return 0;
  3373. }
  3374. /*
  3375. * Copy an BUI format buffer from the given buf, and into the destination
  3376. * BUI format structure. The BUI/BUD items were designed not to need any
  3377. * special alignment handling.
  3378. */
  3379. static int
  3380. xfs_bui_copy_format(
  3381. struct xfs_log_iovec *buf,
  3382. struct xfs_bui_log_format *dst_bui_fmt)
  3383. {
  3384. struct xfs_bui_log_format *src_bui_fmt;
  3385. uint len;
  3386. src_bui_fmt = buf->i_addr;
  3387. len = xfs_bui_log_format_sizeof(src_bui_fmt->bui_nextents);
  3388. if (buf->i_len == len) {
  3389. memcpy(dst_bui_fmt, src_bui_fmt, len);
  3390. return 0;
  3391. }
  3392. return -EFSCORRUPTED;
  3393. }
  3394. /*
  3395. * This routine is called to create an in-core extent bmap update
  3396. * item from the bui format structure which was logged on disk.
  3397. * It allocates an in-core bui, copies the extents from the format
  3398. * structure into it, and adds the bui to the AIL with the given
  3399. * LSN.
  3400. */
  3401. STATIC int
  3402. xlog_recover_bui_pass2(
  3403. struct xlog *log,
  3404. struct xlog_recover_item *item,
  3405. xfs_lsn_t lsn)
  3406. {
  3407. int error;
  3408. struct xfs_mount *mp = log->l_mp;
  3409. struct xfs_bui_log_item *buip;
  3410. struct xfs_bui_log_format *bui_formatp;
  3411. bui_formatp = item->ri_buf[0].i_addr;
  3412. if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
  3413. return -EFSCORRUPTED;
  3414. buip = xfs_bui_init(mp);
  3415. error = xfs_bui_copy_format(&item->ri_buf[0], &buip->bui_format);
  3416. if (error) {
  3417. xfs_bui_item_free(buip);
  3418. return error;
  3419. }
  3420. atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
  3421. spin_lock(&log->l_ailp->xa_lock);
  3422. /*
  3423. * The RUI has two references. One for the RUD and one for RUI to ensure
  3424. * it makes it into the AIL. Insert the RUI into the AIL directly and
  3425. * drop the RUI reference. Note that xfs_trans_ail_update() drops the
  3426. * AIL lock.
  3427. */
  3428. xfs_trans_ail_update(log->l_ailp, &buip->bui_item, lsn);
  3429. xfs_bui_release(buip);
  3430. return 0;
  3431. }
  3432. /*
  3433. * This routine is called when an BUD format structure is found in a committed
  3434. * transaction in the log. Its purpose is to cancel the corresponding BUI if it
  3435. * was still in the log. To do this it searches the AIL for the BUI with an id
  3436. * equal to that in the BUD format structure. If we find it we drop the BUD
  3437. * reference, which removes the BUI from the AIL and frees it.
  3438. */
  3439. STATIC int
  3440. xlog_recover_bud_pass2(
  3441. struct xlog *log,
  3442. struct xlog_recover_item *item)
  3443. {
  3444. struct xfs_bud_log_format *bud_formatp;
  3445. struct xfs_bui_log_item *buip = NULL;
  3446. struct xfs_log_item *lip;
  3447. uint64_t bui_id;
  3448. struct xfs_ail_cursor cur;
  3449. struct xfs_ail *ailp = log->l_ailp;
  3450. bud_formatp = item->ri_buf[0].i_addr;
  3451. if (item->ri_buf[0].i_len != sizeof(struct xfs_bud_log_format))
  3452. return -EFSCORRUPTED;
  3453. bui_id = bud_formatp->bud_bui_id;
  3454. /*
  3455. * Search for the BUI with the id in the BUD format structure in the
  3456. * AIL.
  3457. */
  3458. spin_lock(&ailp->xa_lock);
  3459. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  3460. while (lip != NULL) {
  3461. if (lip->li_type == XFS_LI_BUI) {
  3462. buip = (struct xfs_bui_log_item *)lip;
  3463. if (buip->bui_format.bui_id == bui_id) {
  3464. /*
  3465. * Drop the BUD reference to the BUI. This
  3466. * removes the BUI from the AIL and frees it.
  3467. */
  3468. spin_unlock(&ailp->xa_lock);
  3469. xfs_bui_release(buip);
  3470. spin_lock(&ailp->xa_lock);
  3471. break;
  3472. }
  3473. }
  3474. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  3475. }
  3476. xfs_trans_ail_cursor_done(&cur);
  3477. spin_unlock(&ailp->xa_lock);
  3478. return 0;
  3479. }
  3480. /*
  3481. * This routine is called when an inode create format structure is found in a
  3482. * committed transaction in the log. It's purpose is to initialise the inodes
  3483. * being allocated on disk. This requires us to get inode cluster buffers that
  3484. * match the range to be initialised, stamped with inode templates and written
  3485. * by delayed write so that subsequent modifications will hit the cached buffer
  3486. * and only need writing out at the end of recovery.
  3487. */
  3488. STATIC int
  3489. xlog_recover_do_icreate_pass2(
  3490. struct xlog *log,
  3491. struct list_head *buffer_list,
  3492. xlog_recover_item_t *item)
  3493. {
  3494. struct xfs_mount *mp = log->l_mp;
  3495. struct xfs_icreate_log *icl;
  3496. xfs_agnumber_t agno;
  3497. xfs_agblock_t agbno;
  3498. unsigned int count;
  3499. unsigned int isize;
  3500. xfs_agblock_t length;
  3501. int blks_per_cluster;
  3502. int bb_per_cluster;
  3503. int cancel_count;
  3504. int nbufs;
  3505. int i;
  3506. icl = (struct xfs_icreate_log *)item->ri_buf[0].i_addr;
  3507. if (icl->icl_type != XFS_LI_ICREATE) {
  3508. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad type");
  3509. return -EINVAL;
  3510. }
  3511. if (icl->icl_size != 1) {
  3512. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad icl size");
  3513. return -EINVAL;
  3514. }
  3515. agno = be32_to_cpu(icl->icl_ag);
  3516. if (agno >= mp->m_sb.sb_agcount) {
  3517. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agno");
  3518. return -EINVAL;
  3519. }
  3520. agbno = be32_to_cpu(icl->icl_agbno);
  3521. if (!agbno || agbno == NULLAGBLOCK || agbno >= mp->m_sb.sb_agblocks) {
  3522. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agbno");
  3523. return -EINVAL;
  3524. }
  3525. isize = be32_to_cpu(icl->icl_isize);
  3526. if (isize != mp->m_sb.sb_inodesize) {
  3527. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad isize");
  3528. return -EINVAL;
  3529. }
  3530. count = be32_to_cpu(icl->icl_count);
  3531. if (!count) {
  3532. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad count");
  3533. return -EINVAL;
  3534. }
  3535. length = be32_to_cpu(icl->icl_length);
  3536. if (!length || length >= mp->m_sb.sb_agblocks) {
  3537. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad length");
  3538. return -EINVAL;
  3539. }
  3540. /*
  3541. * The inode chunk is either full or sparse and we only support
  3542. * m_ialloc_min_blks sized sparse allocations at this time.
  3543. */
  3544. if (length != mp->m_ialloc_blks &&
  3545. length != mp->m_ialloc_min_blks) {
  3546. xfs_warn(log->l_mp,
  3547. "%s: unsupported chunk length", __FUNCTION__);
  3548. return -EINVAL;
  3549. }
  3550. /* verify inode count is consistent with extent length */
  3551. if ((count >> mp->m_sb.sb_inopblog) != length) {
  3552. xfs_warn(log->l_mp,
  3553. "%s: inconsistent inode count and chunk length",
  3554. __FUNCTION__);
  3555. return -EINVAL;
  3556. }
  3557. /*
  3558. * The icreate transaction can cover multiple cluster buffers and these
  3559. * buffers could have been freed and reused. Check the individual
  3560. * buffers for cancellation so we don't overwrite anything written after
  3561. * a cancellation.
  3562. */
  3563. blks_per_cluster = xfs_icluster_size_fsb(mp);
  3564. bb_per_cluster = XFS_FSB_TO_BB(mp, blks_per_cluster);
  3565. nbufs = length / blks_per_cluster;
  3566. for (i = 0, cancel_count = 0; i < nbufs; i++) {
  3567. xfs_daddr_t daddr;
  3568. daddr = XFS_AGB_TO_DADDR(mp, agno,
  3569. agbno + i * blks_per_cluster);
  3570. if (xlog_check_buffer_cancelled(log, daddr, bb_per_cluster, 0))
  3571. cancel_count++;
  3572. }
  3573. /*
  3574. * We currently only use icreate for a single allocation at a time. This
  3575. * means we should expect either all or none of the buffers to be
  3576. * cancelled. Be conservative and skip replay if at least one buffer is
  3577. * cancelled, but warn the user that something is awry if the buffers
  3578. * are not consistent.
  3579. *
  3580. * XXX: This must be refined to only skip cancelled clusters once we use
  3581. * icreate for multiple chunk allocations.
  3582. */
  3583. ASSERT(!cancel_count || cancel_count == nbufs);
  3584. if (cancel_count) {
  3585. if (cancel_count != nbufs)
  3586. xfs_warn(mp,
  3587. "WARNING: partial inode chunk cancellation, skipped icreate.");
  3588. trace_xfs_log_recover_icreate_cancel(log, icl);
  3589. return 0;
  3590. }
  3591. trace_xfs_log_recover_icreate_recover(log, icl);
  3592. return xfs_ialloc_inode_init(mp, NULL, buffer_list, count, agno, agbno,
  3593. length, be32_to_cpu(icl->icl_gen));
  3594. }
  3595. STATIC void
  3596. xlog_recover_buffer_ra_pass2(
  3597. struct xlog *log,
  3598. struct xlog_recover_item *item)
  3599. {
  3600. struct xfs_buf_log_format *buf_f = item->ri_buf[0].i_addr;
  3601. struct xfs_mount *mp = log->l_mp;
  3602. if (xlog_peek_buffer_cancelled(log, buf_f->blf_blkno,
  3603. buf_f->blf_len, buf_f->blf_flags)) {
  3604. return;
  3605. }
  3606. xfs_buf_readahead(mp->m_ddev_targp, buf_f->blf_blkno,
  3607. buf_f->blf_len, NULL);
  3608. }
  3609. STATIC void
  3610. xlog_recover_inode_ra_pass2(
  3611. struct xlog *log,
  3612. struct xlog_recover_item *item)
  3613. {
  3614. struct xfs_inode_log_format ilf_buf;
  3615. struct xfs_inode_log_format *ilfp;
  3616. struct xfs_mount *mp = log->l_mp;
  3617. int error;
  3618. if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
  3619. ilfp = item->ri_buf[0].i_addr;
  3620. } else {
  3621. ilfp = &ilf_buf;
  3622. memset(ilfp, 0, sizeof(*ilfp));
  3623. error = xfs_inode_item_format_convert(&item->ri_buf[0], ilfp);
  3624. if (error)
  3625. return;
  3626. }
  3627. if (xlog_peek_buffer_cancelled(log, ilfp->ilf_blkno, ilfp->ilf_len, 0))
  3628. return;
  3629. xfs_buf_readahead(mp->m_ddev_targp, ilfp->ilf_blkno,
  3630. ilfp->ilf_len, &xfs_inode_buf_ra_ops);
  3631. }
  3632. STATIC void
  3633. xlog_recover_dquot_ra_pass2(
  3634. struct xlog *log,
  3635. struct xlog_recover_item *item)
  3636. {
  3637. struct xfs_mount *mp = log->l_mp;
  3638. struct xfs_disk_dquot *recddq;
  3639. struct xfs_dq_logformat *dq_f;
  3640. uint type;
  3641. int len;
  3642. if (mp->m_qflags == 0)
  3643. return;
  3644. recddq = item->ri_buf[1].i_addr;
  3645. if (recddq == NULL)
  3646. return;
  3647. if (item->ri_buf[1].i_len < sizeof(struct xfs_disk_dquot))
  3648. return;
  3649. type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
  3650. ASSERT(type);
  3651. if (log->l_quotaoffs_flag & type)
  3652. return;
  3653. dq_f = item->ri_buf[0].i_addr;
  3654. ASSERT(dq_f);
  3655. ASSERT(dq_f->qlf_len == 1);
  3656. len = XFS_FSB_TO_BB(mp, dq_f->qlf_len);
  3657. if (xlog_peek_buffer_cancelled(log, dq_f->qlf_blkno, len, 0))
  3658. return;
  3659. xfs_buf_readahead(mp->m_ddev_targp, dq_f->qlf_blkno, len,
  3660. &xfs_dquot_buf_ra_ops);
  3661. }
  3662. STATIC void
  3663. xlog_recover_ra_pass2(
  3664. struct xlog *log,
  3665. struct xlog_recover_item *item)
  3666. {
  3667. switch (ITEM_TYPE(item)) {
  3668. case XFS_LI_BUF:
  3669. xlog_recover_buffer_ra_pass2(log, item);
  3670. break;
  3671. case XFS_LI_INODE:
  3672. xlog_recover_inode_ra_pass2(log, item);
  3673. break;
  3674. case XFS_LI_DQUOT:
  3675. xlog_recover_dquot_ra_pass2(log, item);
  3676. break;
  3677. case XFS_LI_EFI:
  3678. case XFS_LI_EFD:
  3679. case XFS_LI_QUOTAOFF:
  3680. case XFS_LI_RUI:
  3681. case XFS_LI_RUD:
  3682. case XFS_LI_CUI:
  3683. case XFS_LI_CUD:
  3684. case XFS_LI_BUI:
  3685. case XFS_LI_BUD:
  3686. default:
  3687. break;
  3688. }
  3689. }
  3690. STATIC int
  3691. xlog_recover_commit_pass1(
  3692. struct xlog *log,
  3693. struct xlog_recover *trans,
  3694. struct xlog_recover_item *item)
  3695. {
  3696. trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS1);
  3697. switch (ITEM_TYPE(item)) {
  3698. case XFS_LI_BUF:
  3699. return xlog_recover_buffer_pass1(log, item);
  3700. case XFS_LI_QUOTAOFF:
  3701. return xlog_recover_quotaoff_pass1(log, item);
  3702. case XFS_LI_INODE:
  3703. case XFS_LI_EFI:
  3704. case XFS_LI_EFD:
  3705. case XFS_LI_DQUOT:
  3706. case XFS_LI_ICREATE:
  3707. case XFS_LI_RUI:
  3708. case XFS_LI_RUD:
  3709. case XFS_LI_CUI:
  3710. case XFS_LI_CUD:
  3711. case XFS_LI_BUI:
  3712. case XFS_LI_BUD:
  3713. /* nothing to do in pass 1 */
  3714. return 0;
  3715. default:
  3716. xfs_warn(log->l_mp, "%s: invalid item type (%d)",
  3717. __func__, ITEM_TYPE(item));
  3718. ASSERT(0);
  3719. return -EIO;
  3720. }
  3721. }
  3722. STATIC int
  3723. xlog_recover_commit_pass2(
  3724. struct xlog *log,
  3725. struct xlog_recover *trans,
  3726. struct list_head *buffer_list,
  3727. struct xlog_recover_item *item)
  3728. {
  3729. trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS2);
  3730. switch (ITEM_TYPE(item)) {
  3731. case XFS_LI_BUF:
  3732. return xlog_recover_buffer_pass2(log, buffer_list, item,
  3733. trans->r_lsn);
  3734. case XFS_LI_INODE:
  3735. return xlog_recover_inode_pass2(log, buffer_list, item,
  3736. trans->r_lsn);
  3737. case XFS_LI_EFI:
  3738. return xlog_recover_efi_pass2(log, item, trans->r_lsn);
  3739. case XFS_LI_EFD:
  3740. return xlog_recover_efd_pass2(log, item);
  3741. case XFS_LI_RUI:
  3742. return xlog_recover_rui_pass2(log, item, trans->r_lsn);
  3743. case XFS_LI_RUD:
  3744. return xlog_recover_rud_pass2(log, item);
  3745. case XFS_LI_CUI:
  3746. return xlog_recover_cui_pass2(log, item, trans->r_lsn);
  3747. case XFS_LI_CUD:
  3748. return xlog_recover_cud_pass2(log, item);
  3749. case XFS_LI_BUI:
  3750. return xlog_recover_bui_pass2(log, item, trans->r_lsn);
  3751. case XFS_LI_BUD:
  3752. return xlog_recover_bud_pass2(log, item);
  3753. case XFS_LI_DQUOT:
  3754. return xlog_recover_dquot_pass2(log, buffer_list, item,
  3755. trans->r_lsn);
  3756. case XFS_LI_ICREATE:
  3757. return xlog_recover_do_icreate_pass2(log, buffer_list, item);
  3758. case XFS_LI_QUOTAOFF:
  3759. /* nothing to do in pass2 */
  3760. return 0;
  3761. default:
  3762. xfs_warn(log->l_mp, "%s: invalid item type (%d)",
  3763. __func__, ITEM_TYPE(item));
  3764. ASSERT(0);
  3765. return -EIO;
  3766. }
  3767. }
  3768. STATIC int
  3769. xlog_recover_items_pass2(
  3770. struct xlog *log,
  3771. struct xlog_recover *trans,
  3772. struct list_head *buffer_list,
  3773. struct list_head *item_list)
  3774. {
  3775. struct xlog_recover_item *item;
  3776. int error = 0;
  3777. list_for_each_entry(item, item_list, ri_list) {
  3778. error = xlog_recover_commit_pass2(log, trans,
  3779. buffer_list, item);
  3780. if (error)
  3781. return error;
  3782. }
  3783. return error;
  3784. }
  3785. /*
  3786. * Perform the transaction.
  3787. *
  3788. * If the transaction modifies a buffer or inode, do it now. Otherwise,
  3789. * EFIs and EFDs get queued up by adding entries into the AIL for them.
  3790. */
  3791. STATIC int
  3792. xlog_recover_commit_trans(
  3793. struct xlog *log,
  3794. struct xlog_recover *trans,
  3795. int pass,
  3796. struct list_head *buffer_list)
  3797. {
  3798. int error = 0;
  3799. int items_queued = 0;
  3800. struct xlog_recover_item *item;
  3801. struct xlog_recover_item *next;
  3802. LIST_HEAD (ra_list);
  3803. LIST_HEAD (done_list);
  3804. #define XLOG_RECOVER_COMMIT_QUEUE_MAX 100
  3805. hlist_del_init(&trans->r_list);
  3806. error = xlog_recover_reorder_trans(log, trans, pass);
  3807. if (error)
  3808. return error;
  3809. list_for_each_entry_safe(item, next, &trans->r_itemq, ri_list) {
  3810. switch (pass) {
  3811. case XLOG_RECOVER_PASS1:
  3812. error = xlog_recover_commit_pass1(log, trans, item);
  3813. break;
  3814. case XLOG_RECOVER_PASS2:
  3815. xlog_recover_ra_pass2(log, item);
  3816. list_move_tail(&item->ri_list, &ra_list);
  3817. items_queued++;
  3818. if (items_queued >= XLOG_RECOVER_COMMIT_QUEUE_MAX) {
  3819. error = xlog_recover_items_pass2(log, trans,
  3820. buffer_list, &ra_list);
  3821. list_splice_tail_init(&ra_list, &done_list);
  3822. items_queued = 0;
  3823. }
  3824. break;
  3825. default:
  3826. ASSERT(0);
  3827. }
  3828. if (error)
  3829. goto out;
  3830. }
  3831. out:
  3832. if (!list_empty(&ra_list)) {
  3833. if (!error)
  3834. error = xlog_recover_items_pass2(log, trans,
  3835. buffer_list, &ra_list);
  3836. list_splice_tail_init(&ra_list, &done_list);
  3837. }
  3838. if (!list_empty(&done_list))
  3839. list_splice_init(&done_list, &trans->r_itemq);
  3840. return error;
  3841. }
  3842. STATIC void
  3843. xlog_recover_add_item(
  3844. struct list_head *head)
  3845. {
  3846. xlog_recover_item_t *item;
  3847. item = kmem_zalloc(sizeof(xlog_recover_item_t), KM_SLEEP);
  3848. INIT_LIST_HEAD(&item->ri_list);
  3849. list_add_tail(&item->ri_list, head);
  3850. }
  3851. STATIC int
  3852. xlog_recover_add_to_cont_trans(
  3853. struct xlog *log,
  3854. struct xlog_recover *trans,
  3855. char *dp,
  3856. int len)
  3857. {
  3858. xlog_recover_item_t *item;
  3859. char *ptr, *old_ptr;
  3860. int old_len;
  3861. /*
  3862. * If the transaction is empty, the header was split across this and the
  3863. * previous record. Copy the rest of the header.
  3864. */
  3865. if (list_empty(&trans->r_itemq)) {
  3866. ASSERT(len <= sizeof(struct xfs_trans_header));
  3867. if (len > sizeof(struct xfs_trans_header)) {
  3868. xfs_warn(log->l_mp, "%s: bad header length", __func__);
  3869. return -EIO;
  3870. }
  3871. xlog_recover_add_item(&trans->r_itemq);
  3872. ptr = (char *)&trans->r_theader +
  3873. sizeof(struct xfs_trans_header) - len;
  3874. memcpy(ptr, dp, len);
  3875. return 0;
  3876. }
  3877. /* take the tail entry */
  3878. item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
  3879. old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
  3880. old_len = item->ri_buf[item->ri_cnt-1].i_len;
  3881. ptr = kmem_realloc(old_ptr, len + old_len, KM_SLEEP);
  3882. memcpy(&ptr[old_len], dp, len);
  3883. item->ri_buf[item->ri_cnt-1].i_len += len;
  3884. item->ri_buf[item->ri_cnt-1].i_addr = ptr;
  3885. trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
  3886. return 0;
  3887. }
  3888. /*
  3889. * The next region to add is the start of a new region. It could be
  3890. * a whole region or it could be the first part of a new region. Because
  3891. * of this, the assumption here is that the type and size fields of all
  3892. * format structures fit into the first 32 bits of the structure.
  3893. *
  3894. * This works because all regions must be 32 bit aligned. Therefore, we
  3895. * either have both fields or we have neither field. In the case we have
  3896. * neither field, the data part of the region is zero length. We only have
  3897. * a log_op_header and can throw away the header since a new one will appear
  3898. * later. If we have at least 4 bytes, then we can determine how many regions
  3899. * will appear in the current log item.
  3900. */
  3901. STATIC int
  3902. xlog_recover_add_to_trans(
  3903. struct xlog *log,
  3904. struct xlog_recover *trans,
  3905. char *dp,
  3906. int len)
  3907. {
  3908. xfs_inode_log_format_t *in_f; /* any will do */
  3909. xlog_recover_item_t *item;
  3910. char *ptr;
  3911. if (!len)
  3912. return 0;
  3913. if (list_empty(&trans->r_itemq)) {
  3914. /* we need to catch log corruptions here */
  3915. if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
  3916. xfs_warn(log->l_mp, "%s: bad header magic number",
  3917. __func__);
  3918. ASSERT(0);
  3919. return -EIO;
  3920. }
  3921. if (len > sizeof(struct xfs_trans_header)) {
  3922. xfs_warn(log->l_mp, "%s: bad header length", __func__);
  3923. ASSERT(0);
  3924. return -EIO;
  3925. }
  3926. /*
  3927. * The transaction header can be arbitrarily split across op
  3928. * records. If we don't have the whole thing here, copy what we
  3929. * do have and handle the rest in the next record.
  3930. */
  3931. if (len == sizeof(struct xfs_trans_header))
  3932. xlog_recover_add_item(&trans->r_itemq);
  3933. memcpy(&trans->r_theader, dp, len);
  3934. return 0;
  3935. }
  3936. ptr = kmem_alloc(len, KM_SLEEP);
  3937. memcpy(ptr, dp, len);
  3938. in_f = (xfs_inode_log_format_t *)ptr;
  3939. /* take the tail entry */
  3940. item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
  3941. if (item->ri_total != 0 &&
  3942. item->ri_total == item->ri_cnt) {
  3943. /* tail item is in use, get a new one */
  3944. xlog_recover_add_item(&trans->r_itemq);
  3945. item = list_entry(trans->r_itemq.prev,
  3946. xlog_recover_item_t, ri_list);
  3947. }
  3948. if (item->ri_total == 0) { /* first region to be added */
  3949. if (in_f->ilf_size == 0 ||
  3950. in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
  3951. xfs_warn(log->l_mp,
  3952. "bad number of regions (%d) in inode log format",
  3953. in_f->ilf_size);
  3954. ASSERT(0);
  3955. kmem_free(ptr);
  3956. return -EIO;
  3957. }
  3958. item->ri_total = in_f->ilf_size;
  3959. item->ri_buf =
  3960. kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
  3961. KM_SLEEP);
  3962. }
  3963. ASSERT(item->ri_total > item->ri_cnt);
  3964. /* Description region is ri_buf[0] */
  3965. item->ri_buf[item->ri_cnt].i_addr = ptr;
  3966. item->ri_buf[item->ri_cnt].i_len = len;
  3967. item->ri_cnt++;
  3968. trace_xfs_log_recover_item_add(log, trans, item, 0);
  3969. return 0;
  3970. }
  3971. /*
  3972. * Free up any resources allocated by the transaction
  3973. *
  3974. * Remember that EFIs, EFDs, and IUNLINKs are handled later.
  3975. */
  3976. STATIC void
  3977. xlog_recover_free_trans(
  3978. struct xlog_recover *trans)
  3979. {
  3980. xlog_recover_item_t *item, *n;
  3981. int i;
  3982. hlist_del_init(&trans->r_list);
  3983. list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
  3984. /* Free the regions in the item. */
  3985. list_del(&item->ri_list);
  3986. for (i = 0; i < item->ri_cnt; i++)
  3987. kmem_free(item->ri_buf[i].i_addr);
  3988. /* Free the item itself */
  3989. kmem_free(item->ri_buf);
  3990. kmem_free(item);
  3991. }
  3992. /* Free the transaction recover structure */
  3993. kmem_free(trans);
  3994. }
  3995. /*
  3996. * On error or completion, trans is freed.
  3997. */
  3998. STATIC int
  3999. xlog_recovery_process_trans(
  4000. struct xlog *log,
  4001. struct xlog_recover *trans,
  4002. char *dp,
  4003. unsigned int len,
  4004. unsigned int flags,
  4005. int pass,
  4006. struct list_head *buffer_list)
  4007. {
  4008. int error = 0;
  4009. bool freeit = false;
  4010. /* mask off ophdr transaction container flags */
  4011. flags &= ~XLOG_END_TRANS;
  4012. if (flags & XLOG_WAS_CONT_TRANS)
  4013. flags &= ~XLOG_CONTINUE_TRANS;
  4014. /*
  4015. * Callees must not free the trans structure. We'll decide if we need to
  4016. * free it or not based on the operation being done and it's result.
  4017. */
  4018. switch (flags) {
  4019. /* expected flag values */
  4020. case 0:
  4021. case XLOG_CONTINUE_TRANS:
  4022. error = xlog_recover_add_to_trans(log, trans, dp, len);
  4023. break;
  4024. case XLOG_WAS_CONT_TRANS:
  4025. error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
  4026. break;
  4027. case XLOG_COMMIT_TRANS:
  4028. error = xlog_recover_commit_trans(log, trans, pass,
  4029. buffer_list);
  4030. /* success or fail, we are now done with this transaction. */
  4031. freeit = true;
  4032. break;
  4033. /* unexpected flag values */
  4034. case XLOG_UNMOUNT_TRANS:
  4035. /* just skip trans */
  4036. xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
  4037. freeit = true;
  4038. break;
  4039. case XLOG_START_TRANS:
  4040. default:
  4041. xfs_warn(log->l_mp, "%s: bad flag 0x%x", __func__, flags);
  4042. ASSERT(0);
  4043. error = -EIO;
  4044. break;
  4045. }
  4046. if (error || freeit)
  4047. xlog_recover_free_trans(trans);
  4048. return error;
  4049. }
  4050. /*
  4051. * Lookup the transaction recovery structure associated with the ID in the
  4052. * current ophdr. If the transaction doesn't exist and the start flag is set in
  4053. * the ophdr, then allocate a new transaction for future ID matches to find.
  4054. * Either way, return what we found during the lookup - an existing transaction
  4055. * or nothing.
  4056. */
  4057. STATIC struct xlog_recover *
  4058. xlog_recover_ophdr_to_trans(
  4059. struct hlist_head rhash[],
  4060. struct xlog_rec_header *rhead,
  4061. struct xlog_op_header *ohead)
  4062. {
  4063. struct xlog_recover *trans;
  4064. xlog_tid_t tid;
  4065. struct hlist_head *rhp;
  4066. tid = be32_to_cpu(ohead->oh_tid);
  4067. rhp = &rhash[XLOG_RHASH(tid)];
  4068. hlist_for_each_entry(trans, rhp, r_list) {
  4069. if (trans->r_log_tid == tid)
  4070. return trans;
  4071. }
  4072. /*
  4073. * skip over non-start transaction headers - we could be
  4074. * processing slack space before the next transaction starts
  4075. */
  4076. if (!(ohead->oh_flags & XLOG_START_TRANS))
  4077. return NULL;
  4078. ASSERT(be32_to_cpu(ohead->oh_len) == 0);
  4079. /*
  4080. * This is a new transaction so allocate a new recovery container to
  4081. * hold the recovery ops that will follow.
  4082. */
  4083. trans = kmem_zalloc(sizeof(struct xlog_recover), KM_SLEEP);
  4084. trans->r_log_tid = tid;
  4085. trans->r_lsn = be64_to_cpu(rhead->h_lsn);
  4086. INIT_LIST_HEAD(&trans->r_itemq);
  4087. INIT_HLIST_NODE(&trans->r_list);
  4088. hlist_add_head(&trans->r_list, rhp);
  4089. /*
  4090. * Nothing more to do for this ophdr. Items to be added to this new
  4091. * transaction will be in subsequent ophdr containers.
  4092. */
  4093. return NULL;
  4094. }
  4095. STATIC int
  4096. xlog_recover_process_ophdr(
  4097. struct xlog *log,
  4098. struct hlist_head rhash[],
  4099. struct xlog_rec_header *rhead,
  4100. struct xlog_op_header *ohead,
  4101. char *dp,
  4102. char *end,
  4103. int pass,
  4104. struct list_head *buffer_list)
  4105. {
  4106. struct xlog_recover *trans;
  4107. unsigned int len;
  4108. int error;
  4109. /* Do we understand who wrote this op? */
  4110. if (ohead->oh_clientid != XFS_TRANSACTION &&
  4111. ohead->oh_clientid != XFS_LOG) {
  4112. xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
  4113. __func__, ohead->oh_clientid);
  4114. ASSERT(0);
  4115. return -EIO;
  4116. }
  4117. /*
  4118. * Check the ophdr contains all the data it is supposed to contain.
  4119. */
  4120. len = be32_to_cpu(ohead->oh_len);
  4121. if (dp + len > end) {
  4122. xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
  4123. WARN_ON(1);
  4124. return -EIO;
  4125. }
  4126. trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
  4127. if (!trans) {
  4128. /* nothing to do, so skip over this ophdr */
  4129. return 0;
  4130. }
  4131. /*
  4132. * The recovered buffer queue is drained only once we know that all
  4133. * recovery items for the current LSN have been processed. This is
  4134. * required because:
  4135. *
  4136. * - Buffer write submission updates the metadata LSN of the buffer.
  4137. * - Log recovery skips items with a metadata LSN >= the current LSN of
  4138. * the recovery item.
  4139. * - Separate recovery items against the same metadata buffer can share
  4140. * a current LSN. I.e., consider that the LSN of a recovery item is
  4141. * defined as the starting LSN of the first record in which its
  4142. * transaction appears, that a record can hold multiple transactions,
  4143. * and/or that a transaction can span multiple records.
  4144. *
  4145. * In other words, we are allowed to submit a buffer from log recovery
  4146. * once per current LSN. Otherwise, we may incorrectly skip recovery
  4147. * items and cause corruption.
  4148. *
  4149. * We don't know up front whether buffers are updated multiple times per
  4150. * LSN. Therefore, track the current LSN of each commit log record as it
  4151. * is processed and drain the queue when it changes. Use commit records
  4152. * because they are ordered correctly by the logging code.
  4153. */
  4154. if (log->l_recovery_lsn != trans->r_lsn &&
  4155. ohead->oh_flags & XLOG_COMMIT_TRANS) {
  4156. error = xfs_buf_delwri_submit(buffer_list);
  4157. if (error)
  4158. return error;
  4159. log->l_recovery_lsn = trans->r_lsn;
  4160. }
  4161. return xlog_recovery_process_trans(log, trans, dp, len,
  4162. ohead->oh_flags, pass, buffer_list);
  4163. }
  4164. /*
  4165. * There are two valid states of the r_state field. 0 indicates that the
  4166. * transaction structure is in a normal state. We have either seen the
  4167. * start of the transaction or the last operation we added was not a partial
  4168. * operation. If the last operation we added to the transaction was a
  4169. * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
  4170. *
  4171. * NOTE: skip LRs with 0 data length.
  4172. */
  4173. STATIC int
  4174. xlog_recover_process_data(
  4175. struct xlog *log,
  4176. struct hlist_head rhash[],
  4177. struct xlog_rec_header *rhead,
  4178. char *dp,
  4179. int pass,
  4180. struct list_head *buffer_list)
  4181. {
  4182. struct xlog_op_header *ohead;
  4183. char *end;
  4184. int num_logops;
  4185. int error;
  4186. end = dp + be32_to_cpu(rhead->h_len);
  4187. num_logops = be32_to_cpu(rhead->h_num_logops);
  4188. /* check the log format matches our own - else we can't recover */
  4189. if (xlog_header_check_recover(log->l_mp, rhead))
  4190. return -EIO;
  4191. trace_xfs_log_recover_record(log, rhead, pass);
  4192. while ((dp < end) && num_logops) {
  4193. ohead = (struct xlog_op_header *)dp;
  4194. dp += sizeof(*ohead);
  4195. ASSERT(dp <= end);
  4196. /* errors will abort recovery */
  4197. error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
  4198. dp, end, pass, buffer_list);
  4199. if (error)
  4200. return error;
  4201. dp += be32_to_cpu(ohead->oh_len);
  4202. num_logops--;
  4203. }
  4204. return 0;
  4205. }
  4206. /* Recover the EFI if necessary. */
  4207. STATIC int
  4208. xlog_recover_process_efi(
  4209. struct xfs_mount *mp,
  4210. struct xfs_ail *ailp,
  4211. struct xfs_log_item *lip)
  4212. {
  4213. struct xfs_efi_log_item *efip;
  4214. int error;
  4215. /*
  4216. * Skip EFIs that we've already processed.
  4217. */
  4218. efip = container_of(lip, struct xfs_efi_log_item, efi_item);
  4219. if (test_bit(XFS_EFI_RECOVERED, &efip->efi_flags))
  4220. return 0;
  4221. spin_unlock(&ailp->xa_lock);
  4222. error = xfs_efi_recover(mp, efip);
  4223. spin_lock(&ailp->xa_lock);
  4224. return error;
  4225. }
  4226. /* Release the EFI since we're cancelling everything. */
  4227. STATIC void
  4228. xlog_recover_cancel_efi(
  4229. struct xfs_mount *mp,
  4230. struct xfs_ail *ailp,
  4231. struct xfs_log_item *lip)
  4232. {
  4233. struct xfs_efi_log_item *efip;
  4234. efip = container_of(lip, struct xfs_efi_log_item, efi_item);
  4235. spin_unlock(&ailp->xa_lock);
  4236. xfs_efi_release(efip);
  4237. spin_lock(&ailp->xa_lock);
  4238. }
  4239. /* Recover the RUI if necessary. */
  4240. STATIC int
  4241. xlog_recover_process_rui(
  4242. struct xfs_mount *mp,
  4243. struct xfs_ail *ailp,
  4244. struct xfs_log_item *lip)
  4245. {
  4246. struct xfs_rui_log_item *ruip;
  4247. int error;
  4248. /*
  4249. * Skip RUIs that we've already processed.
  4250. */
  4251. ruip = container_of(lip, struct xfs_rui_log_item, rui_item);
  4252. if (test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags))
  4253. return 0;
  4254. spin_unlock(&ailp->xa_lock);
  4255. error = xfs_rui_recover(mp, ruip);
  4256. spin_lock(&ailp->xa_lock);
  4257. return error;
  4258. }
  4259. /* Release the RUI since we're cancelling everything. */
  4260. STATIC void
  4261. xlog_recover_cancel_rui(
  4262. struct xfs_mount *mp,
  4263. struct xfs_ail *ailp,
  4264. struct xfs_log_item *lip)
  4265. {
  4266. struct xfs_rui_log_item *ruip;
  4267. ruip = container_of(lip, struct xfs_rui_log_item, rui_item);
  4268. spin_unlock(&ailp->xa_lock);
  4269. xfs_rui_release(ruip);
  4270. spin_lock(&ailp->xa_lock);
  4271. }
  4272. /* Recover the CUI if necessary. */
  4273. STATIC int
  4274. xlog_recover_process_cui(
  4275. struct xfs_mount *mp,
  4276. struct xfs_ail *ailp,
  4277. struct xfs_log_item *lip)
  4278. {
  4279. struct xfs_cui_log_item *cuip;
  4280. int error;
  4281. /*
  4282. * Skip CUIs that we've already processed.
  4283. */
  4284. cuip = container_of(lip, struct xfs_cui_log_item, cui_item);
  4285. if (test_bit(XFS_CUI_RECOVERED, &cuip->cui_flags))
  4286. return 0;
  4287. spin_unlock(&ailp->xa_lock);
  4288. error = xfs_cui_recover(mp, cuip);
  4289. spin_lock(&ailp->xa_lock);
  4290. return error;
  4291. }
  4292. /* Release the CUI since we're cancelling everything. */
  4293. STATIC void
  4294. xlog_recover_cancel_cui(
  4295. struct xfs_mount *mp,
  4296. struct xfs_ail *ailp,
  4297. struct xfs_log_item *lip)
  4298. {
  4299. struct xfs_cui_log_item *cuip;
  4300. cuip = container_of(lip, struct xfs_cui_log_item, cui_item);
  4301. spin_unlock(&ailp->xa_lock);
  4302. xfs_cui_release(cuip);
  4303. spin_lock(&ailp->xa_lock);
  4304. }
  4305. /* Recover the BUI if necessary. */
  4306. STATIC int
  4307. xlog_recover_process_bui(
  4308. struct xfs_mount *mp,
  4309. struct xfs_ail *ailp,
  4310. struct xfs_log_item *lip)
  4311. {
  4312. struct xfs_bui_log_item *buip;
  4313. int error;
  4314. /*
  4315. * Skip BUIs that we've already processed.
  4316. */
  4317. buip = container_of(lip, struct xfs_bui_log_item, bui_item);
  4318. if (test_bit(XFS_BUI_RECOVERED, &buip->bui_flags))
  4319. return 0;
  4320. spin_unlock(&ailp->xa_lock);
  4321. error = xfs_bui_recover(mp, buip);
  4322. spin_lock(&ailp->xa_lock);
  4323. return error;
  4324. }
  4325. /* Release the BUI since we're cancelling everything. */
  4326. STATIC void
  4327. xlog_recover_cancel_bui(
  4328. struct xfs_mount *mp,
  4329. struct xfs_ail *ailp,
  4330. struct xfs_log_item *lip)
  4331. {
  4332. struct xfs_bui_log_item *buip;
  4333. buip = container_of(lip, struct xfs_bui_log_item, bui_item);
  4334. spin_unlock(&ailp->xa_lock);
  4335. xfs_bui_release(buip);
  4336. spin_lock(&ailp->xa_lock);
  4337. }
  4338. /* Is this log item a deferred action intent? */
  4339. static inline bool xlog_item_is_intent(struct xfs_log_item *lip)
  4340. {
  4341. switch (lip->li_type) {
  4342. case XFS_LI_EFI:
  4343. case XFS_LI_RUI:
  4344. case XFS_LI_CUI:
  4345. case XFS_LI_BUI:
  4346. return true;
  4347. default:
  4348. return false;
  4349. }
  4350. }
  4351. /*
  4352. * When this is called, all of the log intent items which did not have
  4353. * corresponding log done items should be in the AIL. What we do now
  4354. * is update the data structures associated with each one.
  4355. *
  4356. * Since we process the log intent items in normal transactions, they
  4357. * will be removed at some point after the commit. This prevents us
  4358. * from just walking down the list processing each one. We'll use a
  4359. * flag in the intent item to skip those that we've already processed
  4360. * and use the AIL iteration mechanism's generation count to try to
  4361. * speed this up at least a bit.
  4362. *
  4363. * When we start, we know that the intents are the only things in the
  4364. * AIL. As we process them, however, other items are added to the
  4365. * AIL.
  4366. */
  4367. STATIC int
  4368. xlog_recover_process_intents(
  4369. struct xlog *log)
  4370. {
  4371. struct xfs_log_item *lip;
  4372. int error = 0;
  4373. struct xfs_ail_cursor cur;
  4374. struct xfs_ail *ailp;
  4375. #if defined(DEBUG) || defined(XFS_WARN)
  4376. xfs_lsn_t last_lsn;
  4377. #endif
  4378. ailp = log->l_ailp;
  4379. spin_lock(&ailp->xa_lock);
  4380. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  4381. #if defined(DEBUG) || defined(XFS_WARN)
  4382. last_lsn = xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block);
  4383. #endif
  4384. while (lip != NULL) {
  4385. /*
  4386. * We're done when we see something other than an intent.
  4387. * There should be no intents left in the AIL now.
  4388. */
  4389. if (!xlog_item_is_intent(lip)) {
  4390. #ifdef DEBUG
  4391. for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
  4392. ASSERT(!xlog_item_is_intent(lip));
  4393. #endif
  4394. break;
  4395. }
  4396. /*
  4397. * We should never see a redo item with a LSN higher than
  4398. * the last transaction we found in the log at the start
  4399. * of recovery.
  4400. */
  4401. ASSERT(XFS_LSN_CMP(last_lsn, lip->li_lsn) >= 0);
  4402. switch (lip->li_type) {
  4403. case XFS_LI_EFI:
  4404. error = xlog_recover_process_efi(log->l_mp, ailp, lip);
  4405. break;
  4406. case XFS_LI_RUI:
  4407. error = xlog_recover_process_rui(log->l_mp, ailp, lip);
  4408. break;
  4409. case XFS_LI_CUI:
  4410. error = xlog_recover_process_cui(log->l_mp, ailp, lip);
  4411. break;
  4412. case XFS_LI_BUI:
  4413. error = xlog_recover_process_bui(log->l_mp, ailp, lip);
  4414. break;
  4415. }
  4416. if (error)
  4417. goto out;
  4418. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  4419. }
  4420. out:
  4421. xfs_trans_ail_cursor_done(&cur);
  4422. spin_unlock(&ailp->xa_lock);
  4423. return error;
  4424. }
  4425. /*
  4426. * A cancel occurs when the mount has failed and we're bailing out.
  4427. * Release all pending log intent items so they don't pin the AIL.
  4428. */
  4429. STATIC int
  4430. xlog_recover_cancel_intents(
  4431. struct xlog *log)
  4432. {
  4433. struct xfs_log_item *lip;
  4434. int error = 0;
  4435. struct xfs_ail_cursor cur;
  4436. struct xfs_ail *ailp;
  4437. ailp = log->l_ailp;
  4438. spin_lock(&ailp->xa_lock);
  4439. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  4440. while (lip != NULL) {
  4441. /*
  4442. * We're done when we see something other than an intent.
  4443. * There should be no intents left in the AIL now.
  4444. */
  4445. if (!xlog_item_is_intent(lip)) {
  4446. #ifdef DEBUG
  4447. for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
  4448. ASSERT(!xlog_item_is_intent(lip));
  4449. #endif
  4450. break;
  4451. }
  4452. switch (lip->li_type) {
  4453. case XFS_LI_EFI:
  4454. xlog_recover_cancel_efi(log->l_mp, ailp, lip);
  4455. break;
  4456. case XFS_LI_RUI:
  4457. xlog_recover_cancel_rui(log->l_mp, ailp, lip);
  4458. break;
  4459. case XFS_LI_CUI:
  4460. xlog_recover_cancel_cui(log->l_mp, ailp, lip);
  4461. break;
  4462. case XFS_LI_BUI:
  4463. xlog_recover_cancel_bui(log->l_mp, ailp, lip);
  4464. break;
  4465. }
  4466. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  4467. }
  4468. xfs_trans_ail_cursor_done(&cur);
  4469. spin_unlock(&ailp->xa_lock);
  4470. return error;
  4471. }
  4472. /*
  4473. * This routine performs a transaction to null out a bad inode pointer
  4474. * in an agi unlinked inode hash bucket.
  4475. */
  4476. STATIC void
  4477. xlog_recover_clear_agi_bucket(
  4478. xfs_mount_t *mp,
  4479. xfs_agnumber_t agno,
  4480. int bucket)
  4481. {
  4482. xfs_trans_t *tp;
  4483. xfs_agi_t *agi;
  4484. xfs_buf_t *agibp;
  4485. int offset;
  4486. int error;
  4487. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_clearagi, 0, 0, 0, &tp);
  4488. if (error)
  4489. goto out_error;
  4490. error = xfs_read_agi(mp, tp, agno, &agibp);
  4491. if (error)
  4492. goto out_abort;
  4493. agi = XFS_BUF_TO_AGI(agibp);
  4494. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  4495. offset = offsetof(xfs_agi_t, agi_unlinked) +
  4496. (sizeof(xfs_agino_t) * bucket);
  4497. xfs_trans_log_buf(tp, agibp, offset,
  4498. (offset + sizeof(xfs_agino_t) - 1));
  4499. error = xfs_trans_commit(tp);
  4500. if (error)
  4501. goto out_error;
  4502. return;
  4503. out_abort:
  4504. xfs_trans_cancel(tp);
  4505. out_error:
  4506. xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__, agno);
  4507. return;
  4508. }
  4509. STATIC xfs_agino_t
  4510. xlog_recover_process_one_iunlink(
  4511. struct xfs_mount *mp,
  4512. xfs_agnumber_t agno,
  4513. xfs_agino_t agino,
  4514. int bucket)
  4515. {
  4516. struct xfs_buf *ibp;
  4517. struct xfs_dinode *dip;
  4518. struct xfs_inode *ip;
  4519. xfs_ino_t ino;
  4520. int error;
  4521. ino = XFS_AGINO_TO_INO(mp, agno, agino);
  4522. error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
  4523. if (error)
  4524. goto fail;
  4525. /*
  4526. * Get the on disk inode to find the next inode in the bucket.
  4527. */
  4528. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &ibp, 0, 0);
  4529. if (error)
  4530. goto fail_iput;
  4531. xfs_iflags_clear(ip, XFS_IRECOVERY);
  4532. ASSERT(VFS_I(ip)->i_nlink == 0);
  4533. ASSERT(VFS_I(ip)->i_mode != 0);
  4534. /* setup for the next pass */
  4535. agino = be32_to_cpu(dip->di_next_unlinked);
  4536. xfs_buf_relse(ibp);
  4537. /*
  4538. * Prevent any DMAPI event from being sent when the reference on
  4539. * the inode is dropped.
  4540. */
  4541. ip->i_d.di_dmevmask = 0;
  4542. IRELE(ip);
  4543. return agino;
  4544. fail_iput:
  4545. IRELE(ip);
  4546. fail:
  4547. /*
  4548. * We can't read in the inode this bucket points to, or this inode
  4549. * is messed up. Just ditch this bucket of inodes. We will lose
  4550. * some inodes and space, but at least we won't hang.
  4551. *
  4552. * Call xlog_recover_clear_agi_bucket() to perform a transaction to
  4553. * clear the inode pointer in the bucket.
  4554. */
  4555. xlog_recover_clear_agi_bucket(mp, agno, bucket);
  4556. return NULLAGINO;
  4557. }
  4558. /*
  4559. * xlog_iunlink_recover
  4560. *
  4561. * This is called during recovery to process any inodes which
  4562. * we unlinked but not freed when the system crashed. These
  4563. * inodes will be on the lists in the AGI blocks. What we do
  4564. * here is scan all the AGIs and fully truncate and free any
  4565. * inodes found on the lists. Each inode is removed from the
  4566. * lists when it has been fully truncated and is freed. The
  4567. * freeing of the inode and its removal from the list must be
  4568. * atomic.
  4569. */
  4570. STATIC void
  4571. xlog_recover_process_iunlinks(
  4572. struct xlog *log)
  4573. {
  4574. xfs_mount_t *mp;
  4575. xfs_agnumber_t agno;
  4576. xfs_agi_t *agi;
  4577. xfs_buf_t *agibp;
  4578. xfs_agino_t agino;
  4579. int bucket;
  4580. int error;
  4581. uint mp_dmevmask;
  4582. mp = log->l_mp;
  4583. /*
  4584. * Prevent any DMAPI event from being sent while in this function.
  4585. */
  4586. mp_dmevmask = mp->m_dmevmask;
  4587. mp->m_dmevmask = 0;
  4588. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  4589. /*
  4590. * Find the agi for this ag.
  4591. */
  4592. error = xfs_read_agi(mp, NULL, agno, &agibp);
  4593. if (error) {
  4594. /*
  4595. * AGI is b0rked. Don't process it.
  4596. *
  4597. * We should probably mark the filesystem as corrupt
  4598. * after we've recovered all the ag's we can....
  4599. */
  4600. continue;
  4601. }
  4602. /*
  4603. * Unlock the buffer so that it can be acquired in the normal
  4604. * course of the transaction to truncate and free each inode.
  4605. * Because we are not racing with anyone else here for the AGI
  4606. * buffer, we don't even need to hold it locked to read the
  4607. * initial unlinked bucket entries out of the buffer. We keep
  4608. * buffer reference though, so that it stays pinned in memory
  4609. * while we need the buffer.
  4610. */
  4611. agi = XFS_BUF_TO_AGI(agibp);
  4612. xfs_buf_unlock(agibp);
  4613. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
  4614. agino = be32_to_cpu(agi->agi_unlinked[bucket]);
  4615. while (agino != NULLAGINO) {
  4616. agino = xlog_recover_process_one_iunlink(mp,
  4617. agno, agino, bucket);
  4618. }
  4619. }
  4620. xfs_buf_rele(agibp);
  4621. }
  4622. mp->m_dmevmask = mp_dmevmask;
  4623. }
  4624. STATIC int
  4625. xlog_unpack_data(
  4626. struct xlog_rec_header *rhead,
  4627. char *dp,
  4628. struct xlog *log)
  4629. {
  4630. int i, j, k;
  4631. for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
  4632. i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
  4633. *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
  4634. dp += BBSIZE;
  4635. }
  4636. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  4637. xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
  4638. for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
  4639. j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  4640. k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  4641. *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
  4642. dp += BBSIZE;
  4643. }
  4644. }
  4645. return 0;
  4646. }
  4647. /*
  4648. * CRC check, unpack and process a log record.
  4649. */
  4650. STATIC int
  4651. xlog_recover_process(
  4652. struct xlog *log,
  4653. struct hlist_head rhash[],
  4654. struct xlog_rec_header *rhead,
  4655. char *dp,
  4656. int pass,
  4657. struct list_head *buffer_list)
  4658. {
  4659. int error;
  4660. __le32 old_crc = rhead->h_crc;
  4661. __le32 crc;
  4662. crc = xlog_cksum(log, rhead, dp, be32_to_cpu(rhead->h_len));
  4663. /*
  4664. * Nothing else to do if this is a CRC verification pass. Just return
  4665. * if this a record with a non-zero crc. Unfortunately, mkfs always
  4666. * sets old_crc to 0 so we must consider this valid even on v5 supers.
  4667. * Otherwise, return EFSBADCRC on failure so the callers up the stack
  4668. * know precisely what failed.
  4669. */
  4670. if (pass == XLOG_RECOVER_CRCPASS) {
  4671. if (old_crc && crc != old_crc)
  4672. return -EFSBADCRC;
  4673. return 0;
  4674. }
  4675. /*
  4676. * We're in the normal recovery path. Issue a warning if and only if the
  4677. * CRC in the header is non-zero. This is an advisory warning and the
  4678. * zero CRC check prevents warnings from being emitted when upgrading
  4679. * the kernel from one that does not add CRCs by default.
  4680. */
  4681. if (crc != old_crc) {
  4682. if (old_crc || xfs_sb_version_hascrc(&log->l_mp->m_sb)) {
  4683. xfs_alert(log->l_mp,
  4684. "log record CRC mismatch: found 0x%x, expected 0x%x.",
  4685. le32_to_cpu(old_crc),
  4686. le32_to_cpu(crc));
  4687. xfs_hex_dump(dp, 32);
  4688. }
  4689. /*
  4690. * If the filesystem is CRC enabled, this mismatch becomes a
  4691. * fatal log corruption failure.
  4692. */
  4693. if (xfs_sb_version_hascrc(&log->l_mp->m_sb))
  4694. return -EFSCORRUPTED;
  4695. }
  4696. error = xlog_unpack_data(rhead, dp, log);
  4697. if (error)
  4698. return error;
  4699. return xlog_recover_process_data(log, rhash, rhead, dp, pass,
  4700. buffer_list);
  4701. }
  4702. STATIC int
  4703. xlog_valid_rec_header(
  4704. struct xlog *log,
  4705. struct xlog_rec_header *rhead,
  4706. xfs_daddr_t blkno)
  4707. {
  4708. int hlen;
  4709. if (unlikely(rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))) {
  4710. XFS_ERROR_REPORT("xlog_valid_rec_header(1)",
  4711. XFS_ERRLEVEL_LOW, log->l_mp);
  4712. return -EFSCORRUPTED;
  4713. }
  4714. if (unlikely(
  4715. (!rhead->h_version ||
  4716. (be32_to_cpu(rhead->h_version) & (~XLOG_VERSION_OKBITS))))) {
  4717. xfs_warn(log->l_mp, "%s: unrecognised log version (%d).",
  4718. __func__, be32_to_cpu(rhead->h_version));
  4719. return -EIO;
  4720. }
  4721. /* LR body must have data or it wouldn't have been written */
  4722. hlen = be32_to_cpu(rhead->h_len);
  4723. if (unlikely( hlen <= 0 || hlen > INT_MAX )) {
  4724. XFS_ERROR_REPORT("xlog_valid_rec_header(2)",
  4725. XFS_ERRLEVEL_LOW, log->l_mp);
  4726. return -EFSCORRUPTED;
  4727. }
  4728. if (unlikely( blkno > log->l_logBBsize || blkno > INT_MAX )) {
  4729. XFS_ERROR_REPORT("xlog_valid_rec_header(3)",
  4730. XFS_ERRLEVEL_LOW, log->l_mp);
  4731. return -EFSCORRUPTED;
  4732. }
  4733. return 0;
  4734. }
  4735. /*
  4736. * Read the log from tail to head and process the log records found.
  4737. * Handle the two cases where the tail and head are in the same cycle
  4738. * and where the active portion of the log wraps around the end of
  4739. * the physical log separately. The pass parameter is passed through
  4740. * to the routines called to process the data and is not looked at
  4741. * here.
  4742. */
  4743. STATIC int
  4744. xlog_do_recovery_pass(
  4745. struct xlog *log,
  4746. xfs_daddr_t head_blk,
  4747. xfs_daddr_t tail_blk,
  4748. int pass,
  4749. xfs_daddr_t *first_bad) /* out: first bad log rec */
  4750. {
  4751. xlog_rec_header_t *rhead;
  4752. xfs_daddr_t blk_no, rblk_no;
  4753. xfs_daddr_t rhead_blk;
  4754. char *offset;
  4755. xfs_buf_t *hbp, *dbp;
  4756. int error = 0, h_size, h_len;
  4757. int error2 = 0;
  4758. int bblks, split_bblks;
  4759. int hblks, split_hblks, wrapped_hblks;
  4760. int i;
  4761. struct hlist_head rhash[XLOG_RHASH_SIZE];
  4762. LIST_HEAD (buffer_list);
  4763. ASSERT(head_blk != tail_blk);
  4764. blk_no = rhead_blk = tail_blk;
  4765. for (i = 0; i < XLOG_RHASH_SIZE; i++)
  4766. INIT_HLIST_HEAD(&rhash[i]);
  4767. /*
  4768. * Read the header of the tail block and get the iclog buffer size from
  4769. * h_size. Use this to tell how many sectors make up the log header.
  4770. */
  4771. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  4772. /*
  4773. * When using variable length iclogs, read first sector of
  4774. * iclog header and extract the header size from it. Get a
  4775. * new hbp that is the correct size.
  4776. */
  4777. hbp = xlog_get_bp(log, 1);
  4778. if (!hbp)
  4779. return -ENOMEM;
  4780. error = xlog_bread(log, tail_blk, 1, hbp, &offset);
  4781. if (error)
  4782. goto bread_err1;
  4783. rhead = (xlog_rec_header_t *)offset;
  4784. error = xlog_valid_rec_header(log, rhead, tail_blk);
  4785. if (error)
  4786. goto bread_err1;
  4787. /*
  4788. * xfsprogs has a bug where record length is based on lsunit but
  4789. * h_size (iclog size) is hardcoded to 32k. Now that we
  4790. * unconditionally CRC verify the unmount record, this means the
  4791. * log buffer can be too small for the record and cause an
  4792. * overrun.
  4793. *
  4794. * Detect this condition here. Use lsunit for the buffer size as
  4795. * long as this looks like the mkfs case. Otherwise, return an
  4796. * error to avoid a buffer overrun.
  4797. */
  4798. h_size = be32_to_cpu(rhead->h_size);
  4799. h_len = be32_to_cpu(rhead->h_len);
  4800. if (h_len > h_size) {
  4801. if (h_len <= log->l_mp->m_logbsize &&
  4802. be32_to_cpu(rhead->h_num_logops) == 1) {
  4803. xfs_warn(log->l_mp,
  4804. "invalid iclog size (%d bytes), using lsunit (%d bytes)",
  4805. h_size, log->l_mp->m_logbsize);
  4806. h_size = log->l_mp->m_logbsize;
  4807. } else
  4808. return -EFSCORRUPTED;
  4809. }
  4810. if ((be32_to_cpu(rhead->h_version) & XLOG_VERSION_2) &&
  4811. (h_size > XLOG_HEADER_CYCLE_SIZE)) {
  4812. hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
  4813. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  4814. hblks++;
  4815. xlog_put_bp(hbp);
  4816. hbp = xlog_get_bp(log, hblks);
  4817. } else {
  4818. hblks = 1;
  4819. }
  4820. } else {
  4821. ASSERT(log->l_sectBBsize == 1);
  4822. hblks = 1;
  4823. hbp = xlog_get_bp(log, 1);
  4824. h_size = XLOG_BIG_RECORD_BSIZE;
  4825. }
  4826. if (!hbp)
  4827. return -ENOMEM;
  4828. dbp = xlog_get_bp(log, BTOBB(h_size));
  4829. if (!dbp) {
  4830. xlog_put_bp(hbp);
  4831. return -ENOMEM;
  4832. }
  4833. memset(rhash, 0, sizeof(rhash));
  4834. if (tail_blk > head_blk) {
  4835. /*
  4836. * Perform recovery around the end of the physical log.
  4837. * When the head is not on the same cycle number as the tail,
  4838. * we can't do a sequential recovery.
  4839. */
  4840. while (blk_no < log->l_logBBsize) {
  4841. /*
  4842. * Check for header wrapping around physical end-of-log
  4843. */
  4844. offset = hbp->b_addr;
  4845. split_hblks = 0;
  4846. wrapped_hblks = 0;
  4847. if (blk_no + hblks <= log->l_logBBsize) {
  4848. /* Read header in one read */
  4849. error = xlog_bread(log, blk_no, hblks, hbp,
  4850. &offset);
  4851. if (error)
  4852. goto bread_err2;
  4853. } else {
  4854. /* This LR is split across physical log end */
  4855. if (blk_no != log->l_logBBsize) {
  4856. /* some data before physical log end */
  4857. ASSERT(blk_no <= INT_MAX);
  4858. split_hblks = log->l_logBBsize - (int)blk_no;
  4859. ASSERT(split_hblks > 0);
  4860. error = xlog_bread(log, blk_no,
  4861. split_hblks, hbp,
  4862. &offset);
  4863. if (error)
  4864. goto bread_err2;
  4865. }
  4866. /*
  4867. * Note: this black magic still works with
  4868. * large sector sizes (non-512) only because:
  4869. * - we increased the buffer size originally
  4870. * by 1 sector giving us enough extra space
  4871. * for the second read;
  4872. * - the log start is guaranteed to be sector
  4873. * aligned;
  4874. * - we read the log end (LR header start)
  4875. * _first_, then the log start (LR header end)
  4876. * - order is important.
  4877. */
  4878. wrapped_hblks = hblks - split_hblks;
  4879. error = xlog_bread_offset(log, 0,
  4880. wrapped_hblks, hbp,
  4881. offset + BBTOB(split_hblks));
  4882. if (error)
  4883. goto bread_err2;
  4884. }
  4885. rhead = (xlog_rec_header_t *)offset;
  4886. error = xlog_valid_rec_header(log, rhead,
  4887. split_hblks ? blk_no : 0);
  4888. if (error)
  4889. goto bread_err2;
  4890. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  4891. blk_no += hblks;
  4892. /*
  4893. * Read the log record data in multiple reads if it
  4894. * wraps around the end of the log. Note that if the
  4895. * header already wrapped, blk_no could point past the
  4896. * end of the log. The record data is contiguous in
  4897. * that case.
  4898. */
  4899. if (blk_no + bblks <= log->l_logBBsize ||
  4900. blk_no >= log->l_logBBsize) {
  4901. /* mod blk_no in case the header wrapped and
  4902. * pushed it beyond the end of the log */
  4903. rblk_no = do_mod(blk_no, log->l_logBBsize);
  4904. error = xlog_bread(log, rblk_no, bblks, dbp,
  4905. &offset);
  4906. if (error)
  4907. goto bread_err2;
  4908. } else {
  4909. /* This log record is split across the
  4910. * physical end of log */
  4911. offset = dbp->b_addr;
  4912. split_bblks = 0;
  4913. if (blk_no != log->l_logBBsize) {
  4914. /* some data is before the physical
  4915. * end of log */
  4916. ASSERT(!wrapped_hblks);
  4917. ASSERT(blk_no <= INT_MAX);
  4918. split_bblks =
  4919. log->l_logBBsize - (int)blk_no;
  4920. ASSERT(split_bblks > 0);
  4921. error = xlog_bread(log, blk_no,
  4922. split_bblks, dbp,
  4923. &offset);
  4924. if (error)
  4925. goto bread_err2;
  4926. }
  4927. /*
  4928. * Note: this black magic still works with
  4929. * large sector sizes (non-512) only because:
  4930. * - we increased the buffer size originally
  4931. * by 1 sector giving us enough extra space
  4932. * for the second read;
  4933. * - the log start is guaranteed to be sector
  4934. * aligned;
  4935. * - we read the log end (LR header start)
  4936. * _first_, then the log start (LR header end)
  4937. * - order is important.
  4938. */
  4939. error = xlog_bread_offset(log, 0,
  4940. bblks - split_bblks, dbp,
  4941. offset + BBTOB(split_bblks));
  4942. if (error)
  4943. goto bread_err2;
  4944. }
  4945. error = xlog_recover_process(log, rhash, rhead, offset,
  4946. pass, &buffer_list);
  4947. if (error)
  4948. goto bread_err2;
  4949. blk_no += bblks;
  4950. rhead_blk = blk_no;
  4951. }
  4952. ASSERT(blk_no >= log->l_logBBsize);
  4953. blk_no -= log->l_logBBsize;
  4954. rhead_blk = blk_no;
  4955. }
  4956. /* read first part of physical log */
  4957. while (blk_no < head_blk) {
  4958. error = xlog_bread(log, blk_no, hblks, hbp, &offset);
  4959. if (error)
  4960. goto bread_err2;
  4961. rhead = (xlog_rec_header_t *)offset;
  4962. error = xlog_valid_rec_header(log, rhead, blk_no);
  4963. if (error)
  4964. goto bread_err2;
  4965. /* blocks in data section */
  4966. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  4967. error = xlog_bread(log, blk_no+hblks, bblks, dbp,
  4968. &offset);
  4969. if (error)
  4970. goto bread_err2;
  4971. error = xlog_recover_process(log, rhash, rhead, offset, pass,
  4972. &buffer_list);
  4973. if (error)
  4974. goto bread_err2;
  4975. blk_no += bblks + hblks;
  4976. rhead_blk = blk_no;
  4977. }
  4978. bread_err2:
  4979. xlog_put_bp(dbp);
  4980. bread_err1:
  4981. xlog_put_bp(hbp);
  4982. /*
  4983. * Submit buffers that have been added from the last record processed,
  4984. * regardless of error status.
  4985. */
  4986. if (!list_empty(&buffer_list))
  4987. error2 = xfs_buf_delwri_submit(&buffer_list);
  4988. if (error && first_bad)
  4989. *first_bad = rhead_blk;
  4990. /*
  4991. * Transactions are freed at commit time but transactions without commit
  4992. * records on disk are never committed. Free any that may be left in the
  4993. * hash table.
  4994. */
  4995. for (i = 0; i < XLOG_RHASH_SIZE; i++) {
  4996. struct hlist_node *tmp;
  4997. struct xlog_recover *trans;
  4998. hlist_for_each_entry_safe(trans, tmp, &rhash[i], r_list)
  4999. xlog_recover_free_trans(trans);
  5000. }
  5001. return error ? error : error2;
  5002. }
  5003. /*
  5004. * Do the recovery of the log. We actually do this in two phases.
  5005. * The two passes are necessary in order to implement the function
  5006. * of cancelling a record written into the log. The first pass
  5007. * determines those things which have been cancelled, and the
  5008. * second pass replays log items normally except for those which
  5009. * have been cancelled. The handling of the replay and cancellations
  5010. * takes place in the log item type specific routines.
  5011. *
  5012. * The table of items which have cancel records in the log is allocated
  5013. * and freed at this level, since only here do we know when all of
  5014. * the log recovery has been completed.
  5015. */
  5016. STATIC int
  5017. xlog_do_log_recovery(
  5018. struct xlog *log,
  5019. xfs_daddr_t head_blk,
  5020. xfs_daddr_t tail_blk)
  5021. {
  5022. int error, i;
  5023. ASSERT(head_blk != tail_blk);
  5024. /*
  5025. * First do a pass to find all of the cancelled buf log items.
  5026. * Store them in the buf_cancel_table for use in the second pass.
  5027. */
  5028. log->l_buf_cancel_table = kmem_zalloc(XLOG_BC_TABLE_SIZE *
  5029. sizeof(struct list_head),
  5030. KM_SLEEP);
  5031. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  5032. INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
  5033. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  5034. XLOG_RECOVER_PASS1, NULL);
  5035. if (error != 0) {
  5036. kmem_free(log->l_buf_cancel_table);
  5037. log->l_buf_cancel_table = NULL;
  5038. return error;
  5039. }
  5040. /*
  5041. * Then do a second pass to actually recover the items in the log.
  5042. * When it is complete free the table of buf cancel items.
  5043. */
  5044. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  5045. XLOG_RECOVER_PASS2, NULL);
  5046. #ifdef DEBUG
  5047. if (!error) {
  5048. int i;
  5049. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  5050. ASSERT(list_empty(&log->l_buf_cancel_table[i]));
  5051. }
  5052. #endif /* DEBUG */
  5053. kmem_free(log->l_buf_cancel_table);
  5054. log->l_buf_cancel_table = NULL;
  5055. return error;
  5056. }
  5057. /*
  5058. * Do the actual recovery
  5059. */
  5060. STATIC int
  5061. xlog_do_recover(
  5062. struct xlog *log,
  5063. xfs_daddr_t head_blk,
  5064. xfs_daddr_t tail_blk)
  5065. {
  5066. struct xfs_mount *mp = log->l_mp;
  5067. int error;
  5068. xfs_buf_t *bp;
  5069. xfs_sb_t *sbp;
  5070. trace_xfs_log_recover(log, head_blk, tail_blk);
  5071. /*
  5072. * First replay the images in the log.
  5073. */
  5074. error = xlog_do_log_recovery(log, head_blk, tail_blk);
  5075. if (error)
  5076. return error;
  5077. /*
  5078. * If IO errors happened during recovery, bail out.
  5079. */
  5080. if (XFS_FORCED_SHUTDOWN(mp)) {
  5081. return -EIO;
  5082. }
  5083. /*
  5084. * We now update the tail_lsn since much of the recovery has completed
  5085. * and there may be space available to use. If there were no extent
  5086. * or iunlinks, we can free up the entire log and set the tail_lsn to
  5087. * be the last_sync_lsn. This was set in xlog_find_tail to be the
  5088. * lsn of the last known good LR on disk. If there are extent frees
  5089. * or iunlinks they will have some entries in the AIL; so we look at
  5090. * the AIL to determine how to set the tail_lsn.
  5091. */
  5092. xlog_assign_tail_lsn(mp);
  5093. /*
  5094. * Now that we've finished replaying all buffer and inode
  5095. * updates, re-read in the superblock and reverify it.
  5096. */
  5097. bp = xfs_getsb(mp, 0);
  5098. bp->b_flags &= ~(XBF_DONE | XBF_ASYNC);
  5099. ASSERT(!(bp->b_flags & XBF_WRITE));
  5100. bp->b_flags |= XBF_READ;
  5101. bp->b_ops = &xfs_sb_buf_ops;
  5102. error = xfs_buf_submit_wait(bp);
  5103. if (error) {
  5104. if (!XFS_FORCED_SHUTDOWN(mp)) {
  5105. xfs_buf_ioerror_alert(bp, __func__);
  5106. ASSERT(0);
  5107. }
  5108. xfs_buf_relse(bp);
  5109. return error;
  5110. }
  5111. /* Convert superblock from on-disk format */
  5112. sbp = &mp->m_sb;
  5113. xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
  5114. xfs_buf_relse(bp);
  5115. /* re-initialise in-core superblock and geometry structures */
  5116. xfs_reinit_percpu_counters(mp);
  5117. error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
  5118. if (error) {
  5119. xfs_warn(mp, "Failed post-recovery per-ag init: %d", error);
  5120. return error;
  5121. }
  5122. mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
  5123. xlog_recover_check_summary(log);
  5124. /* Normal transactions can now occur */
  5125. log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
  5126. return 0;
  5127. }
  5128. /*
  5129. * Perform recovery and re-initialize some log variables in xlog_find_tail.
  5130. *
  5131. * Return error or zero.
  5132. */
  5133. int
  5134. xlog_recover(
  5135. struct xlog *log)
  5136. {
  5137. xfs_daddr_t head_blk, tail_blk;
  5138. int error;
  5139. /* find the tail of the log */
  5140. error = xlog_find_tail(log, &head_blk, &tail_blk);
  5141. if (error)
  5142. return error;
  5143. /*
  5144. * The superblock was read before the log was available and thus the LSN
  5145. * could not be verified. Check the superblock LSN against the current
  5146. * LSN now that it's known.
  5147. */
  5148. if (xfs_sb_version_hascrc(&log->l_mp->m_sb) &&
  5149. !xfs_log_check_lsn(log->l_mp, log->l_mp->m_sb.sb_lsn))
  5150. return -EINVAL;
  5151. if (tail_blk != head_blk) {
  5152. /* There used to be a comment here:
  5153. *
  5154. * disallow recovery on read-only mounts. note -- mount
  5155. * checks for ENOSPC and turns it into an intelligent
  5156. * error message.
  5157. * ...but this is no longer true. Now, unless you specify
  5158. * NORECOVERY (in which case this function would never be
  5159. * called), we just go ahead and recover. We do this all
  5160. * under the vfs layer, so we can get away with it unless
  5161. * the device itself is read-only, in which case we fail.
  5162. */
  5163. if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
  5164. return error;
  5165. }
  5166. /*
  5167. * Version 5 superblock log feature mask validation. We know the
  5168. * log is dirty so check if there are any unknown log features
  5169. * in what we need to recover. If there are unknown features
  5170. * (e.g. unsupported transactions, then simply reject the
  5171. * attempt at recovery before touching anything.
  5172. */
  5173. if (XFS_SB_VERSION_NUM(&log->l_mp->m_sb) == XFS_SB_VERSION_5 &&
  5174. xfs_sb_has_incompat_log_feature(&log->l_mp->m_sb,
  5175. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
  5176. xfs_warn(log->l_mp,
  5177. "Superblock has unknown incompatible log features (0x%x) enabled.",
  5178. (log->l_mp->m_sb.sb_features_log_incompat &
  5179. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
  5180. xfs_warn(log->l_mp,
  5181. "The log can not be fully and/or safely recovered by this kernel.");
  5182. xfs_warn(log->l_mp,
  5183. "Please recover the log on a kernel that supports the unknown features.");
  5184. return -EINVAL;
  5185. }
  5186. /*
  5187. * Delay log recovery if the debug hook is set. This is debug
  5188. * instrumention to coordinate simulation of I/O failures with
  5189. * log recovery.
  5190. */
  5191. if (xfs_globals.log_recovery_delay) {
  5192. xfs_notice(log->l_mp,
  5193. "Delaying log recovery for %d seconds.",
  5194. xfs_globals.log_recovery_delay);
  5195. msleep(xfs_globals.log_recovery_delay * 1000);
  5196. }
  5197. xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
  5198. log->l_mp->m_logname ? log->l_mp->m_logname
  5199. : "internal");
  5200. error = xlog_do_recover(log, head_blk, tail_blk);
  5201. log->l_flags |= XLOG_RECOVERY_NEEDED;
  5202. }
  5203. return error;
  5204. }
  5205. /*
  5206. * In the first part of recovery we replay inodes and buffers and build
  5207. * up the list of extent free items which need to be processed. Here
  5208. * we process the extent free items and clean up the on disk unlinked
  5209. * inode lists. This is separated from the first part of recovery so
  5210. * that the root and real-time bitmap inodes can be read in from disk in
  5211. * between the two stages. This is necessary so that we can free space
  5212. * in the real-time portion of the file system.
  5213. */
  5214. int
  5215. xlog_recover_finish(
  5216. struct xlog *log)
  5217. {
  5218. /*
  5219. * Now we're ready to do the transactions needed for the
  5220. * rest of recovery. Start with completing all the extent
  5221. * free intent records and then process the unlinked inode
  5222. * lists. At this point, we essentially run in normal mode
  5223. * except that we're still performing recovery actions
  5224. * rather than accepting new requests.
  5225. */
  5226. if (log->l_flags & XLOG_RECOVERY_NEEDED) {
  5227. int error;
  5228. error = xlog_recover_process_intents(log);
  5229. if (error) {
  5230. xfs_alert(log->l_mp, "Failed to recover intents");
  5231. return error;
  5232. }
  5233. /*
  5234. * Sync the log to get all the intents out of the AIL.
  5235. * This isn't absolutely necessary, but it helps in
  5236. * case the unlink transactions would have problems
  5237. * pushing the intents out of the way.
  5238. */
  5239. xfs_log_force(log->l_mp, XFS_LOG_SYNC);
  5240. xlog_recover_process_iunlinks(log);
  5241. xlog_recover_check_summary(log);
  5242. xfs_notice(log->l_mp, "Ending recovery (logdev: %s)",
  5243. log->l_mp->m_logname ? log->l_mp->m_logname
  5244. : "internal");
  5245. log->l_flags &= ~XLOG_RECOVERY_NEEDED;
  5246. } else {
  5247. xfs_info(log->l_mp, "Ending clean mount");
  5248. }
  5249. return 0;
  5250. }
  5251. int
  5252. xlog_recover_cancel(
  5253. struct xlog *log)
  5254. {
  5255. int error = 0;
  5256. if (log->l_flags & XLOG_RECOVERY_NEEDED)
  5257. error = xlog_recover_cancel_intents(log);
  5258. return error;
  5259. }
  5260. #if defined(DEBUG)
  5261. /*
  5262. * Read all of the agf and agi counters and check that they
  5263. * are consistent with the superblock counters.
  5264. */
  5265. void
  5266. xlog_recover_check_summary(
  5267. struct xlog *log)
  5268. {
  5269. xfs_mount_t *mp;
  5270. xfs_agf_t *agfp;
  5271. xfs_buf_t *agfbp;
  5272. xfs_buf_t *agibp;
  5273. xfs_agnumber_t agno;
  5274. uint64_t freeblks;
  5275. uint64_t itotal;
  5276. uint64_t ifree;
  5277. int error;
  5278. mp = log->l_mp;
  5279. freeblks = 0LL;
  5280. itotal = 0LL;
  5281. ifree = 0LL;
  5282. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  5283. error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
  5284. if (error) {
  5285. xfs_alert(mp, "%s agf read failed agno %d error %d",
  5286. __func__, agno, error);
  5287. } else {
  5288. agfp = XFS_BUF_TO_AGF(agfbp);
  5289. freeblks += be32_to_cpu(agfp->agf_freeblks) +
  5290. be32_to_cpu(agfp->agf_flcount);
  5291. xfs_buf_relse(agfbp);
  5292. }
  5293. error = xfs_read_agi(mp, NULL, agno, &agibp);
  5294. if (error) {
  5295. xfs_alert(mp, "%s agi read failed agno %d error %d",
  5296. __func__, agno, error);
  5297. } else {
  5298. struct xfs_agi *agi = XFS_BUF_TO_AGI(agibp);
  5299. itotal += be32_to_cpu(agi->agi_count);
  5300. ifree += be32_to_cpu(agi->agi_freecount);
  5301. xfs_buf_relse(agibp);
  5302. }
  5303. }
  5304. }
  5305. #endif /* DEBUG */