xfs_log_recover.c 148 KB

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