xfs_log_recover.c 163 KB

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