xfs_log_recover.c 160 KB

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