12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/blkdev.h>
#include <linux/list_sort.h>
#include <linux/iversion.h>
#include "misc.h"
#include "ctree.h"
#include "tree-log.h"
#include "disk-io.h"
#include "locking.h"
#include "backref.h"
#include "compression.h"
#include "qgroup.h"
#include "block-group.h"
#include "space-info.h"
#include "inode-item.h"
#include "fs.h"
#include "accessors.h"
#include "extent-tree.h"
#include "root-tree.h"
#include "dir-item.h"
#include "file-item.h"
#include "file.h"
#include "orphan.h"
#include "print-tree.h"
#include "tree-checker.h"
#include "delayed-inode.h"
#define MAX_CONFLICT_INODES 10
enum {
LOG_WALK_PIN_ONLY,
LOG_WALK_REPLAY_INODES,
LOG_WALK_REPLAY_DIR_INDEX,
LOG_WALK_REPLAY_ALL,
};
struct walk_control {
bool free;
bool pin;
int stage;
bool ignore_cur_inode;
struct btrfs_root *root;
struct btrfs_root *log;
struct btrfs_trans_handle *trans;
int (*process_func)(struct extent_buffer *eb,
struct walk_control *wc, u64 gen, int level);
struct extent_buffer *log_leaf;
struct btrfs_key log_key;
int log_slot;
struct btrfs_path *subvol_path;
};
static void do_abort_log_replay(struct walk_control *wc, const char *function,
unsigned int line, int error, const char *fmt, ...)
{
struct btrfs_fs_info *fs_info = wc->trans->fs_info;
struct va_format vaf;
va_list args;
if (test_and_set_bit(BTRFS_FS_STATE_LOG_REPLAY_ABORTED, &fs_info->fs_state))
return;
btrfs_abort_transaction(wc->trans, error);
if (wc->subvol_path && wc->subvol_path->nodes[0]) {
btrfs_crit(fs_info,
"subvolume (root %llu) leaf currently being processed:",
btrfs_root_id(wc->root));
btrfs_print_leaf(wc->subvol_path->nodes[0]);
}
if (wc->log_leaf) {
btrfs_crit(fs_info,
"log tree (for root %llu) leaf currently being processed (slot %d key " BTRFS_KEY_FMT "):",
btrfs_root_id(wc->root), wc->log_slot,
BTRFS_KEY_FMT_VALUE(&wc->log_key));
btrfs_print_leaf(wc->log_leaf);
}
va_start(args, fmt);
vaf.fmt = fmt;
vaf.va = &args;
btrfs_crit(fs_info,
"log replay failed in %s:%u for root %llu, stage %d, with error %d: %pV",
function, line, btrfs_root_id(wc->root), wc->stage, error, &vaf);
va_end(args);
}
#define btrfs_abort_log_replay(wc, error, fmt, args...) \
do_abort_log_replay((wc), __func__, __LINE__, (error), fmt, ##args)
static int btrfs_log_inode(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
enum btrfs_log_mode log_mode,
struct btrfs_log_ctx *ctx);
static int link_to_fixup_dir(struct walk_control *wc, u64 objectid);
static noinline int replay_dir_deletes(struct walk_control *wc,
u64 dirid, bool del_all);
static bool wait_log_commit(struct btrfs_root *root, int transid);
static struct btrfs_inode *btrfs_iget_logging(u64 objectid, struct btrfs_root *root)
{
unsigned int nofs_flag;
struct btrfs_inode *inode;
ASSERT(btrfs_is_fstree(btrfs_root_id(root)), "root_id=%llu", btrfs_root_id(root));
nofs_flag = memalloc_nofs_save();
inode = btrfs_iget(objectid, root);
memalloc_nofs_restore(nofs_flag);
return inode;
}
static int start_log_trans(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_log_ctx *ctx)
{
struct btrfs_fs_info *fs_info = root->fs_info;
struct btrfs_root *tree_root = fs_info->tree_root;
const bool zoned = btrfs_is_zoned(fs_info);
int ret = 0;
bool created = false;
if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state)) {
mutex_lock(&tree_root->log_mutex);
if (!fs_info->log_root_tree) {
ret = btrfs_init_log_root_tree(trans, fs_info);
if (!ret) {
set_bit(BTRFS_ROOT_HAS_LOG_TREE, &tree_root->state);
created = true;
}
}
mutex_unlock(&tree_root->log_mutex);
if (ret)
return ret;
}
mutex_lock(&root->log_mutex);
again:
if (root->log_root) {
if (btrfs_need_log_full_commit(trans)) {
ret = BTRFS_LOG_FORCE_COMMIT;
goto out;
}
if (zoned && wait_log_commit(root, root->log_transid - 1))
goto again;
} else {
if (zoned && !created) {
ret = BTRFS_LOG_FORCE_COMMIT;
goto out;
}
ret = btrfs_add_log_tree(trans, root);
if (ret)
goto out;
set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
}
atomic_inc(&root->log_writers);
if (!ctx->logging_new_name) {
int index = root->log_transid % 2;
list_add_tail(&ctx->list, &root->log_ctxs[index]);
ctx->log_transid = root->log_transid;
}
out:
mutex_unlock(&root->log_mutex);
return ret;
}
static int join_running_log_trans(struct btrfs_root *root)
{
const bool zoned = btrfs_is_zoned(root->fs_info);
int ret = -ENOENT;
if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
return ret;
mutex_lock(&root->log_mutex);
again:
if (root->log_root) {
ret = 0;
if (zoned && wait_log_commit(root, root->log_transid - 1))
goto again;
atomic_inc(&root->log_writers);
}
mutex_unlock(&root->log_mutex);
return ret;
}
void btrfs_pin_log_trans(struct btrfs_root *root)
{
atomic_inc(&root->log_writers);
}
void btrfs_end_log_trans(struct btrfs_root *root)
{
if (atomic_dec_and_test(&root->log_writers)) {
cond_wake_up_nomb(&root->log_writer_wait);
}
}
static int process_one_buffer(struct extent_buffer *eb,
struct walk_control *wc, u64 gen, int level)
{
struct btrfs_root *log = wc->log;
struct btrfs_trans_handle *trans = wc->trans;
struct btrfs_fs_info *fs_info = log->fs_info;
int ret = 0;
if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
struct btrfs_tree_parent_check check = {
.level = level,
.transid = gen
};
ret = btrfs_read_extent_buffer(eb, &check);
if (unlikely(ret)) {
if (trans)
btrfs_abort_transaction(trans, ret);
else
btrfs_handle_fs_error(fs_info, ret, NULL);
return ret;
}
}
if (wc->pin) {
ASSERT(trans != NULL);
ret = btrfs_pin_extent_for_log_replay(trans, eb);
if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
return ret;
}
if (btrfs_buffer_uptodate(eb, gen, NULL) && level == 0) {
ret = btrfs_exclude_logged_extents(eb);
if (ret)
btrfs_abort_transaction(trans, ret);
}
}
return ret;
}
static int overwrite_item(struct walk_control *wc)
{
struct btrfs_trans_handle *trans = wc->trans;
struct btrfs_root *root = wc->root;
int ret;
u32 item_size;
u64 saved_i_size = 0;
int save_old_i_size = 0;
unsigned long src_ptr;
unsigned long dst_ptr;
struct extent_buffer *dst_eb;
int dst_slot;
const bool is_inode_item = (wc->log_key.type == BTRFS_INODE_ITEM_KEY);
ASSERT(btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID);
item_size = btrfs_item_size(wc->log_leaf, wc->log_slot);
src_ptr = btrfs_item_ptr_offset(wc->log_leaf, wc->log_slot);
ret = btrfs_search_slot(NULL, root, &wc->log_key, wc->subvol_path, 0, 0);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to search subvolume tree for key " BTRFS_KEY_FMT " root %llu",
BTRFS_KEY_FMT_VALUE(&wc->log_key),
btrfs_root_id(root));
return ret;
}
dst_eb = wc->subvol_path->nodes[0];
dst_slot = wc->subvol_path->slots[0];
if (ret == 0) {
char *src_copy;
const u32 dst_size = btrfs_item_size(dst_eb, dst_slot);
if (dst_size != item_size)
goto insert;
if (item_size == 0) {
btrfs_release_path(wc->subvol_path);
return 0;
}
src_copy = kvmalloc(item_size, GFP_NOFS);
if (!src_copy) {
btrfs_abort_log_replay(wc, -ENOMEM,
"failed to allocate memory for log leaf item");
return -ENOMEM;
}
read_extent_buffer(wc->log_leaf, src_copy, src_ptr, item_size);
dst_ptr = btrfs_item_ptr_offset(dst_eb, dst_slot);
ret = memcmp_extent_buffer(dst_eb, src_copy, dst_ptr, item_size);
kvfree(src_copy);
if (ret == 0) {
btrfs_release_path(wc->subvol_path);
return 0;
}
if (is_inode_item) {
struct btrfs_inode_item *item;
u64 nbytes;
u32 mode;
item = btrfs_item_ptr(dst_eb, dst_slot,
struct btrfs_inode_item);
nbytes = btrfs_inode_nbytes(dst_eb, item);
item = btrfs_item_ptr(wc->log_leaf, wc->log_slot,
struct btrfs_inode_item);
btrfs_set_inode_nbytes(wc->log_leaf, item, nbytes);
mode = btrfs_inode_mode(wc->log_leaf, item);
if (S_ISDIR(mode))
btrfs_set_inode_size(wc->log_leaf, item, 0);
}
} else if (is_inode_item) {
struct btrfs_inode_item *item;
u32 mode;
item = btrfs_item_ptr(wc->log_leaf, wc->log_slot, struct btrfs_inode_item);
btrfs_set_inode_nbytes(wc->log_leaf, item, 0);
mode = btrfs_inode_mode(wc->log_leaf, item);
if (S_ISDIR(mode))
btrfs_set_inode_size(wc->log_leaf, item, 0);
}
insert:
btrfs_release_path(wc->subvol_path);
wc->subvol_path->skip_release_on_error = true;
ret = btrfs_insert_empty_item(trans, root, wc->subvol_path, &wc->log_key, item_size);
wc->subvol_path->skip_release_on_error = false;
dst_eb = wc->subvol_path->nodes[0];
dst_slot = wc->subvol_path->slots[0];
if (ret == -EEXIST || ret == -EOVERFLOW) {
const u32 found_size = btrfs_item_size(dst_eb, dst_slot);
if (found_size > item_size)
btrfs_truncate_item(trans, wc->subvol_path, item_size, 1);
else if (found_size < item_size)
btrfs_extend_item(trans, wc->subvol_path, item_size - found_size);
} else if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to insert item for key " BTRFS_KEY_FMT,
BTRFS_KEY_FMT_VALUE(&wc->log_key));
return ret;
}
dst_ptr = btrfs_item_ptr_offset(dst_eb, dst_slot);
if (is_inode_item && ret == -EEXIST) {
struct btrfs_inode_item *src_item;
struct btrfs_inode_item *dst_item;
src_item = (struct btrfs_inode_item *)src_ptr;
dst_item = (struct btrfs_inode_item *)dst_ptr;
if (btrfs_inode_generation(wc->log_leaf, src_item) == 0) {
const u64 ino_size = btrfs_inode_size(wc->log_leaf, src_item);
if (S_ISREG(btrfs_inode_mode(wc->log_leaf, src_item)) &&
S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
ino_size != 0)
btrfs_set_inode_size(dst_eb, dst_item, ino_size);
goto no_copy;
}
if (S_ISDIR(btrfs_inode_mode(wc->log_leaf, src_item)) &&
S_ISDIR(btrfs_inode_mode(dst_eb, dst_item))) {
save_old_i_size = 1;
saved_i_size = btrfs_inode_size(dst_eb, dst_item);
}
}
copy_extent_buffer(dst_eb, wc->log_leaf, dst_ptr, src_ptr, item_size);
if (save_old_i_size) {
struct btrfs_inode_item *dst_item;
dst_item = (struct btrfs_inode_item *)dst_ptr;
btrfs_set_inode_size(dst_eb, dst_item, saved_i_size);
}
if (is_inode_item) {
struct btrfs_inode_item *dst_item;
dst_item = (struct btrfs_inode_item *)dst_ptr;
if (btrfs_inode_generation(dst_eb, dst_item) == 0)
btrfs_set_inode_generation(dst_eb, dst_item, trans->transid);
}
no_copy:
btrfs_release_path(wc->subvol_path);
return 0;
}
static int read_alloc_one_name(struct extent_buffer *eb, void *start, int len,
struct fscrypt_str *name)
{
char *buf;
buf = kmalloc(len, GFP_NOFS);
if (!buf)
return -ENOMEM;
read_extent_buffer(eb, buf, (unsigned long)start, len);
name->name = buf;
name->len = len;
return 0;
}
static noinline int replay_one_extent(struct walk_control *wc)
{
struct btrfs_trans_handle *trans = wc->trans;
struct btrfs_root *root = wc->root;
struct btrfs_drop_extents_args drop_args = { 0 };
struct btrfs_fs_info *fs_info = root->fs_info;
int found_type;
u64 extent_end;
const u64 start = wc->log_key.offset;
u64 nbytes = 0;
u64 csum_start;
u64 csum_end;
LIST_HEAD(ordered_sums);
u64 offset;
unsigned long dest_offset;
struct btrfs_key ins;
struct btrfs_file_extent_item *item;
struct btrfs_inode *inode = NULL;
int ret = 0;
item = btrfs_item_ptr(wc->log_leaf, wc->log_slot, struct btrfs_file_extent_item);
found_type = btrfs_file_extent_type(wc->log_leaf, item);
if (found_type == BTRFS_FILE_EXTENT_REG ||
found_type == BTRFS_FILE_EXTENT_PREALLOC) {
extent_end = start + btrfs_file_extent_num_bytes(wc->log_leaf, item);
if (btrfs_file_extent_disk_bytenr(wc->log_leaf, item) != 0)
nbytes = btrfs_file_extent_num_bytes(wc->log_leaf, item);
} else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
nbytes = btrfs_file_extent_ram_bytes(wc->log_leaf, item);
extent_end = ALIGN(start + nbytes, fs_info->sectorsize);
} else {
btrfs_abort_log_replay(wc, -EUCLEAN,
"unexpected extent type=%d root=%llu inode=%llu offset=%llu",
found_type, btrfs_root_id(root),
wc->log_key.objectid, wc->log_key.offset);
return -EUCLEAN;
}
inode = btrfs_iget_logging(wc->log_key.objectid, root);
if (IS_ERR(inode)) {
ret = PTR_ERR(inode);
btrfs_abort_log_replay(wc, ret,
"failed to get inode %llu for root %llu",
wc->log_key.objectid, btrfs_root_id(root));
return ret;
}
ret = btrfs_lookup_file_extent(trans, root, wc->subvol_path,
btrfs_ino(inode), start, 0);
if (ret == 0 &&
(found_type == BTRFS_FILE_EXTENT_REG ||
found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
struct extent_buffer *leaf = wc->subvol_path->nodes[0];
struct btrfs_file_extent_item existing;
unsigned long ptr;
ptr = btrfs_item_ptr_offset(leaf, wc->subvol_path->slots[0]);
read_extent_buffer(leaf, &existing, ptr, sizeof(existing));
if (memcmp_extent_buffer(wc->log_leaf, &existing, (unsigned long)item,
sizeof(existing)) == 0) {
btrfs_release_path(wc->subvol_path);
goto out;
}
}
btrfs_release_path(wc->subvol_path);
drop_args.start = start;
drop_args.end = extent_end;
drop_args.drop_cache = true;
drop_args.path = wc->subvol_path;
ret = btrfs_drop_extents(trans, root, inode, &drop_args);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to drop extents for inode %llu range [%llu, %llu) root %llu",
wc->log_key.objectid, start, extent_end,
btrfs_root_id(root));
goto out;
}
if (found_type == BTRFS_FILE_EXTENT_INLINE) {
ret = overwrite_item(wc);
if (ret)
goto out;
goto update_inode;
}
if (btrfs_file_extent_disk_bytenr(wc->log_leaf, item) == 0 &&
btrfs_fs_incompat(fs_info, NO_HOLES))
goto update_inode;
ret = btrfs_insert_empty_item(trans, root, wc->subvol_path,
&wc->log_key, sizeof(*item));
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to insert item with key " BTRFS_KEY_FMT " root %llu",
BTRFS_KEY_FMT_VALUE(&wc->log_key),
btrfs_root_id(root));
goto out;
}
dest_offset = btrfs_item_ptr_offset(wc->subvol_path->nodes[0],
wc->subvol_path->slots[0]);
copy_extent_buffer(wc->subvol_path->nodes[0], wc->log_leaf, dest_offset,
(unsigned long)item, sizeof(*item));
if (btrfs_file_extent_disk_bytenr(wc->log_leaf, item) == 0) {
btrfs_release_path(wc->subvol_path);
goto update_inode;
}
ins.objectid = btrfs_file_extent_disk_bytenr(wc->log_leaf, item);
ins.type = BTRFS_EXTENT_ITEM_KEY;
ins.offset = btrfs_file_extent_disk_num_bytes(wc->log_leaf, item);
offset = wc->log_key.offset - btrfs_file_extent_offset(wc->log_leaf, item);
ret = btrfs_qgroup_trace_extent(trans, ins.objectid, ins.offset);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to trace extent for bytenr %llu disk_num_bytes %llu inode %llu root %llu",
ins.objectid, ins.offset,
wc->log_key.objectid, btrfs_root_id(root));
goto out;
}
ret = btrfs_lookup_data_extent(fs_info, ins.objectid, ins.offset);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to lookup data extent for bytenr %llu disk_num_bytes %llu inode %llu root %llu",
ins.objectid, ins.offset,
wc->log_key.objectid, btrfs_root_id(root));
goto out;
} else if (ret == 0) {
struct btrfs_ref ref = {
.action = BTRFS_ADD_DELAYED_REF,
.bytenr = ins.objectid,
.num_bytes = ins.offset,
.owning_root = btrfs_root_id(root),
.ref_root = btrfs_root_id(root),
};
btrfs_init_data_ref(&ref, wc->log_key.objectid, offset, 0, false);
ret = btrfs_inc_extent_ref(trans, &ref);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to increment data extent for bytenr %llu disk_num_bytes %llu inode %llu root %llu",
ins.objectid, ins.offset,
wc->log_key.objectid,
btrfs_root_id(root));
goto out;
}
} else {
ret = btrfs_alloc_logged_file_extent(trans, btrfs_root_id(root),
wc->log_key.objectid, offset, &ins);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to allocate logged data extent for bytenr %llu disk_num_bytes %llu offset %llu inode %llu root %llu",
ins.objectid, ins.offset, offset,
wc->log_key.objectid, btrfs_root_id(root));
goto out;
}
}
btrfs_release_path(wc->subvol_path);
if (btrfs_file_extent_compression(wc->log_leaf, item)) {
csum_start = ins.objectid;
csum_end = csum_start + ins.offset;
} else {
csum_start = ins.objectid + btrfs_file_extent_offset(wc->log_leaf, item);
csum_end = csum_start + btrfs_file_extent_num_bytes(wc->log_leaf, item);
}
ret = btrfs_lookup_csums_list(root->log_root, csum_start, csum_end - 1,
&ordered_sums, false);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to lookups csums for range [%llu, %llu) inode %llu root %llu",
csum_start, csum_end, wc->log_key.objectid,
btrfs_root_id(root));
goto out;
}
ret = 0;
while (!list_empty(&ordered_sums)) {
struct btrfs_ordered_sum *sums;
struct btrfs_root *csum_root;
sums = list_first_entry(&ordered_sums, struct btrfs_ordered_sum, list);
csum_root = btrfs_csum_root(fs_info, sums->logical);
if (unlikely(!csum_root)) {
btrfs_err(fs_info,
"missing csum root for extent at bytenr %llu",
sums->logical);
ret = -EUCLEAN;
}
if (!ret) {
ret = btrfs_del_csums(trans, csum_root, sums->logical,
sums->len);
if (ret)
btrfs_abort_log_replay(wc, ret,
"failed to delete csums for range [%llu, %llu) inode %llu root %llu",
sums->logical,
sums->logical + sums->len,
wc->log_key.objectid,
btrfs_root_id(root));
}
if (!ret) {
ret = btrfs_insert_data_csums(trans, csum_root, sums);
if (ret)
btrfs_abort_log_replay(wc, ret,
"failed to add csums for range [%llu, %llu) inode %llu root %llu",
sums->logical,
sums->logical + sums->len,
wc->log_key.objectid,
btrfs_root_id(root));
}
list_del(&sums->list);
kfree(sums);
}
if (ret)
goto out;
update_inode:
ret = btrfs_inode_set_file_extent_range(inode, start, extent_end - start);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to set file extent range [%llu, %llu) inode %llu root %llu",
start, extent_end, wc->log_key.objectid,
btrfs_root_id(root));
goto out;
}
btrfs_update_inode_bytes(inode, nbytes, drop_args.bytes_found);
ret = btrfs_update_inode(trans, inode);
if (ret)
btrfs_abort_log_replay(wc, ret,
"failed to update inode %llu root %llu",
wc->log_key.objectid, btrfs_root_id(root));
out:
iput(&inode->vfs_inode);
return ret;
}
static int unlink_inode_for_log_replay(struct walk_control *wc,
struct btrfs_inode *dir,
struct btrfs_inode *inode,
const struct fscrypt_str *name)
{
struct btrfs_trans_handle *trans = wc->trans;
int ret;
ret = btrfs_unlink_inode(trans, dir, inode, name);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to unlink inode %llu parent dir %llu name %.*s root %llu",
btrfs_ino(inode), btrfs_ino(dir), name->len,
name->name, btrfs_root_id(inode->root));
return ret;
}
ret = btrfs_run_delayed_items(trans);
if (ret)
btrfs_abort_log_replay(wc, ret,
"failed to run delayed items current inode %llu parent dir %llu name %.*s root %llu",
btrfs_ino(inode), btrfs_ino(dir), name->len,
name->name, btrfs_root_id(inode->root));
return ret;
}
static noinline int drop_one_dir_item(struct walk_control *wc,
struct btrfs_inode *dir,
struct btrfs_dir_item *di)
{
struct btrfs_root *root = dir->root;
struct btrfs_inode *inode;
struct fscrypt_str name;
struct extent_buffer *leaf = wc->subvol_path->nodes[0];
struct btrfs_key location;
int ret;
btrfs_dir_item_key_to_cpu(leaf, di, &location);
ret = read_alloc_one_name(leaf, di + 1, btrfs_dir_name_len(leaf, di), &name);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to allocate name for dir %llu root %llu",
btrfs_ino(dir), btrfs_root_id(root));
return ret;
}
btrfs_release_path(wc->subvol_path);
inode = btrfs_iget_logging(location.objectid, root);
if (IS_ERR(inode)) {
ret = PTR_ERR(inode);
btrfs_abort_log_replay(wc, ret,
"failed to open inode %llu parent dir %llu name %.*s root %llu",
location.objectid, btrfs_ino(dir),
name.len, name.name, btrfs_root_id(root));
inode = NULL;
goto out;
}
ret = link_to_fixup_dir(wc, location.objectid);
if (ret)
goto out;
ret = unlink_inode_for_log_replay(wc, dir, inode, &name);
out:
kfree(name.name);
if (inode)
iput(&inode->vfs_inode);
return ret;
}
static noinline int inode_in_dir(struct btrfs_root *root,
struct btrfs_path *path,
u64 dirid, u64 objectid, u64 index,
struct fscrypt_str *name)
{
struct btrfs_dir_item *di;
struct btrfs_key location;
int ret = 0;
di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
index, name, 0);
if (IS_ERR(di)) {
ret = PTR_ERR(di);
goto out;
} else if (di) {
btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
if (location.objectid != objectid)
goto out;
} else {
goto out;
}
btrfs_release_path(path);
di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, 0);
if (IS_ERR(di)) {
ret = PTR_ERR(di);
goto out;
} else if (di) {
btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
if (location.objectid == objectid)
ret = 1;
}
out:
btrfs_release_path(path);
return ret;
}
static noinline int backref_in_log(struct btrfs_root *log,
struct btrfs_key *key,
u64 ref_objectid,
const struct fscrypt_str *name)
{
BTRFS_PATH_AUTO_FREE(path);
int ret;
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
if (ret < 0)
return ret;
if (ret == 1)
return 0;
if (key->type == BTRFS_INODE_EXTREF_KEY)
ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
path->slots[0],
ref_objectid, name);
else
ret = !!btrfs_find_name_in_backref(path->nodes[0],
path->slots[0], name);
return ret;
}
static int unlink_refs_not_in_log(struct walk_control *wc,
struct btrfs_key *search_key,
struct btrfs_inode *dir,
struct btrfs_inode *inode)
{
struct extent_buffer *leaf = wc->subvol_path->nodes[0];
unsigned long ptr;
unsigned long ptr_end;
ptr = btrfs_item_ptr_offset(leaf, wc->subvol_path->slots[0]);
ptr_end = ptr + btrfs_item_size(leaf, wc->subvol_path->slots[0]);
while (ptr < ptr_end) {
struct fscrypt_str victim_name;
struct btrfs_inode_ref *victim_ref;
int ret;
victim_ref = (struct btrfs_inode_ref *)ptr;
ret = read_alloc_one_name(leaf, (victim_ref + 1),
btrfs_inode_ref_name_len(leaf, victim_ref),
&victim_name);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to allocate name for inode %llu parent dir %llu root %llu",
btrfs_ino(inode), btrfs_ino(dir),
btrfs_root_id(inode->root));
return ret;
}
ret = backref_in_log(wc->log, search_key, btrfs_ino(dir), &victim_name);
if (ret) {
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to check if backref is in log tree for inode %llu parent dir %llu name %.*s root %llu",
btrfs_ino(inode), btrfs_ino(dir),
victim_name.len, victim_name.name,
btrfs_root_id(inode->root));
kfree(victim_name.name);
return ret;
}
kfree(victim_name.name);
ptr = (unsigned long)(victim_ref + 1) + victim_name.len;
continue;
}
inc_nlink(&inode->vfs_inode);
btrfs_release_path(wc->subvol_path);
ret = unlink_inode_for_log_replay(wc, dir, inode, &victim_name);
kfree(victim_name.name);
if (ret)
return ret;
return -EAGAIN;
}
return 0;
}
static int unlink_extrefs_not_in_log(struct walk_control *wc,
struct btrfs_key *search_key,
struct btrfs_inode *dir,
struct btrfs_inode *inode)
{
struct extent_buffer *leaf = wc->subvol_path->nodes[0];
const unsigned long base = btrfs_item_ptr_offset(leaf, wc->subvol_path->slots[0]);
const u32 item_size = btrfs_item_size(leaf, wc->subvol_path->slots[0]);
u32 cur_offset = 0;
while (cur_offset < item_size) {
struct btrfs_root *log_root = wc->log;
struct btrfs_inode_extref *extref;
struct fscrypt_str victim_name;
int ret;
extref = (struct btrfs_inode_extref *)(base + cur_offset);
victim_name.len = btrfs_inode_extref_name_len(leaf, extref);
if (btrfs_inode_extref_parent(leaf, extref) != btrfs_ino(dir))
goto next;
ret = read_alloc_one_name(leaf, &extref->name, victim_name.len,
&victim_name);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to allocate name for inode %llu parent dir %llu root %llu",
btrfs_ino(inode), btrfs_ino(dir),
btrfs_root_id(inode->root));
return ret;
}
search_key->objectid = btrfs_ino(inode);
search_key->type = BTRFS_INODE_EXTREF_KEY;
search_key->offset = btrfs_extref_hash(btrfs_ino(dir),
victim_name.name,
victim_name.len);
ret = backref_in_log(log_root, search_key, btrfs_ino(dir), &victim_name);
if (ret) {
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to check if backref is in log tree for inode %llu parent dir %llu name %.*s root %llu",
btrfs_ino(inode), btrfs_ino(dir),
victim_name.len, victim_name.name,
btrfs_root_id(inode->root));
kfree(victim_name.name);
return ret;
}
kfree(victim_name.name);
next:
cur_offset += victim_name.len + sizeof(*extref);
continue;
}
inc_nlink(&inode->vfs_inode);
btrfs_release_path(wc->subvol_path);
ret = unlink_inode_for_log_replay(wc, dir, inode, &victim_name);
kfree(victim_name.name);
if (ret)
return ret;
return -EAGAIN;
}
return 0;
}
static inline int __add_inode_ref(struct walk_control *wc,
struct btrfs_inode *dir,
struct btrfs_inode *inode,
u64 ref_index, struct fscrypt_str *name)
{
int ret;
struct btrfs_trans_handle *trans = wc->trans;
struct btrfs_root *root = wc->root;
struct btrfs_dir_item *di;
struct btrfs_key search_key;
struct btrfs_inode_extref *extref;
again:
search_key.objectid = btrfs_ino(inode);
search_key.type = BTRFS_INODE_REF_KEY;
search_key.offset = btrfs_ino(dir);
ret = btrfs_search_slot(NULL, root, &search_key, wc->subvol_path, 0, 0);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to search subvolume tree for key " BTRFS_KEY_FMT " root %llu",
BTRFS_KEY_FMT_VALUE(&search_key),
btrfs_root_id(root));
return ret;
} else if (ret == 0) {
if (search_key.objectid == search_key.offset)
return 1;
ret = unlink_refs_not_in_log(wc, &search_key, dir, inode);
if (ret == -EAGAIN)
goto again;
else if (ret)
return ret;
}
btrfs_release_path(wc->subvol_path);
extref = btrfs_lookup_inode_extref(root, wc->subvol_path, name,
btrfs_ino(inode), btrfs_ino(dir));
if (IS_ERR(extref)) {
return PTR_ERR(extref);
} else if (extref) {
ret = unlink_extrefs_not_in_log(wc, &search_key, dir, inode);
if (ret == -EAGAIN)
goto again;
else if (ret)
return ret;
}
btrfs_release_path(wc->subvol_path);
di = btrfs_lookup_dir_index_item(trans, root, wc->subvol_path, btrfs_ino(dir),
ref_index, name, 0);
if (IS_ERR(di)) {
ret = PTR_ERR(di);
btrfs_abort_log_replay(wc, ret,
"failed to lookup dir index item for dir %llu ref_index %llu name %.*s root %llu",
btrfs_ino(dir), ref_index, name->len,
name->name, btrfs_root_id(root));
return ret;
} else if (di) {
ret = drop_one_dir_item(wc, dir, di);
if (ret)
return ret;
}
btrfs_release_path(wc->subvol_path);
di = btrfs_lookup_dir_item(trans, root, wc->subvol_path, btrfs_ino(dir), name, 0);
if (IS_ERR(di)) {
ret = PTR_ERR(di);
btrfs_abort_log_replay(wc, ret,
"failed to lookup dir item for dir %llu name %.*s root %llu",
btrfs_ino(dir), name->len, name->name,
btrfs_root_id(root));
return ret;
} else if (di) {
ret = drop_one_dir_item(wc, dir, di);
if (ret)
return ret;
}
btrfs_release_path(wc->subvol_path);
return 0;
}
static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
struct fscrypt_str *name, u64 *index,
u64 *parent_objectid)
{
struct btrfs_inode_extref *extref;
int ret;
extref = (struct btrfs_inode_extref *)ref_ptr;
ret = read_alloc_one_name(eb, &extref->name,
btrfs_inode_extref_name_len(eb, extref), name);
if (ret)
return ret;
if (index)
*index = btrfs_inode_extref_index(eb, extref);
if (parent_objectid)
*parent_objectid = btrfs_inode_extref_parent(eb, extref);
return 0;
}
static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
struct fscrypt_str *name, u64 *index)
{
struct btrfs_inode_ref *ref;
int ret;
ref = (struct btrfs_inode_ref *)ref_ptr;
ret = read_alloc_one_name(eb, ref + 1, btrfs_inode_ref_name_len(eb, ref),
name);
if (ret)
return ret;
if (index)
*index = btrfs_inode_ref_index(eb, ref);
return 0;
}
static int unlink_old_inode_refs(struct walk_control *wc, struct btrfs_inode *inode)
{
struct btrfs_root *root = wc->root;
int ret;
unsigned long ref_ptr;
unsigned long ref_end;
struct extent_buffer *eb;
again:
btrfs_release_path(wc->subvol_path);
ret = btrfs_search_slot(NULL, root, &wc->log_key, wc->subvol_path, 0, 0);
if (ret > 0) {
ret = 0;
goto out;
}
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to search subvolume tree for key " BTRFS_KEY_FMT " root %llu",
BTRFS_KEY_FMT_VALUE(&wc->log_key),
btrfs_root_id(root));
goto out;
}
eb = wc->subvol_path->nodes[0];
ref_ptr = btrfs_item_ptr_offset(eb, wc->subvol_path->slots[0]);
ref_end = ref_ptr + btrfs_item_size(eb, wc->subvol_path->slots[0]);
while (ref_ptr < ref_end) {
struct fscrypt_str name;
u64 parent_id;
if (wc->log_key.type == BTRFS_INODE_EXTREF_KEY) {
ret = extref_get_fields(eb, ref_ptr, &name,
NULL, &parent_id);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to get extref details for inode %llu root %llu",
btrfs_ino(inode),
btrfs_root_id(root));
goto out;
}
} else {
parent_id = wc->log_key.offset;
ret = ref_get_fields(eb, ref_ptr, &name, NULL);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to get ref details for inode %llu parent_id %llu root %llu",
btrfs_ino(inode), parent_id,
btrfs_root_id(root));
goto out;
}
}
if (wc->log_key.type == BTRFS_INODE_EXTREF_KEY)
ret = !!btrfs_find_name_in_ext_backref(wc->log_leaf, wc->log_slot,
parent_id, &name);
else
ret = !!btrfs_find_name_in_backref(wc->log_leaf, wc->log_slot,
&name);
if (!ret) {
struct btrfs_inode *dir;
btrfs_release_path(wc->subvol_path);
dir = btrfs_iget_logging(parent_id, root);
if (IS_ERR(dir)) {
ret = PTR_ERR(dir);
kfree(name.name);
btrfs_abort_log_replay(wc, ret,
"failed to lookup dir inode %llu root %llu",
parent_id, btrfs_root_id(root));
goto out;
}
ret = unlink_inode_for_log_replay(wc, dir, inode, &name);
kfree(name.name);
iput(&dir->vfs_inode);
if (ret)
goto out;
goto again;
}
kfree(name.name);
ref_ptr += name.len;
if (wc->log_key.type == BTRFS_INODE_EXTREF_KEY)
ref_ptr += sizeof(struct btrfs_inode_extref);
else
ref_ptr += sizeof(struct btrfs_inode_ref);
}
ret = 0;
out:
btrfs_release_path(wc->subvol_path);
return ret;
}
static noinline int add_inode_ref(struct walk_control *wc)
{
struct btrfs_trans_handle *trans = wc->trans;
struct btrfs_root *root = wc->root;
struct btrfs_inode *dir = NULL;
struct btrfs_inode *inode = NULL;
unsigned long ref_ptr;
unsigned long ref_end;
struct fscrypt_str name = { 0 };
int ret;
const bool is_extref_item = (wc->log_key.type == BTRFS_INODE_EXTREF_KEY);
u64 parent_objectid;
u64 inode_objectid;
u64 ref_index = 0;
int ref_struct_size;
ref_ptr = btrfs_item_ptr_offset(wc->log_leaf, wc->log_slot);
ref_end = ref_ptr + btrfs_item_size(wc->log_leaf, wc->log_slot);
if (is_extref_item) {
struct btrfs_inode_extref *r;
ref_struct_size = sizeof(struct btrfs_inode_extref);
r = (struct btrfs_inode_extref *)ref_ptr;
parent_objectid = btrfs_inode_extref_parent(wc->log_leaf, r);
} else {
ref_struct_size = sizeof(struct btrfs_inode_ref);
parent_objectid = wc->log_key.offset;
}
inode_objectid = wc->log_key.objectid;
dir = btrfs_iget_logging(parent_objectid, root);
if (IS_ERR(dir)) {
ret = PTR_ERR(dir);
if (ret == -ENOENT)
ret = 0;
else
btrfs_abort_log_replay(wc, ret,
"failed to lookup dir inode %llu root %llu",
parent_objectid, btrfs_root_id(root));
dir = NULL;
goto out;
}
inode = btrfs_iget_logging(inode_objectid, root);
if (IS_ERR(inode)) {
ret = PTR_ERR(inode);
btrfs_abort_log_replay(wc, ret,
"failed to lookup inode %llu root %llu",
inode_objectid, btrfs_root_id(root));
inode = NULL;
goto out;
}
while (ref_ptr < ref_end) {
if (is_extref_item) {
ret = extref_get_fields(wc->log_leaf, ref_ptr, &name,
&ref_index, &parent_objectid);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to get extref details for inode %llu root %llu",
btrfs_ino(inode),
btrfs_root_id(root));
goto out;
}
if (!dir) {
dir = btrfs_iget_logging(parent_objectid, root);
if (IS_ERR(dir)) {
ret = PTR_ERR(dir);
dir = NULL;
if (ret == -ENOENT) {
ret = 0;
goto next;
} else {
btrfs_abort_log_replay(wc, ret,
"failed to lookup dir inode %llu root %llu",
parent_objectid,
btrfs_root_id(root));
}
goto out;
}
}
} else {
ret = ref_get_fields(wc->log_leaf, ref_ptr, &name, &ref_index);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to get ref details for inode %llu parent_objectid %llu root %llu",
btrfs_ino(inode),
parent_objectid,
btrfs_root_id(root));
goto out;
}
}
ret = inode_in_dir(root, wc->subvol_path, btrfs_ino(dir),
btrfs_ino(inode), ref_index, &name);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to check if inode %llu is in dir %llu ref_index %llu name %.*s root %llu",
btrfs_ino(inode), btrfs_ino(dir),
ref_index, name.len, name.name,
btrfs_root_id(root));
goto out;
} else if (ret == 0) {
ret = __add_inode_ref(wc, dir, inode, ref_index, &name);
if (ret) {
if (ret == 1)
ret = 0;
goto out;
}
ret = btrfs_add_link(trans, dir, inode, &name, false, ref_index, NULL);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to add link for inode %llu in dir %llu ref_index %llu name %.*s root %llu",
btrfs_ino(inode),
btrfs_ino(dir), ref_index,
name.len, name.name,
btrfs_root_id(root));
goto out;
}
ret = btrfs_update_inode(trans, inode);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to update inode %llu root %llu",
btrfs_ino(inode),
btrfs_root_id(root));
goto out;
}
}
next:
ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + name.len;
kfree(name.name);
name.name = NULL;
if (is_extref_item && dir) {
iput(&dir->vfs_inode);
dir = NULL;
}
}
ret = unlink_old_inode_refs(wc, inode);
if (ret)
goto out;
ret = overwrite_item(wc);
out:
btrfs_release_path(wc->subvol_path);
kfree(name.name);
if (dir)
iput(&dir->vfs_inode);
if (inode)
iput(&inode->vfs_inode);
return ret;
}
static int count_inode_extrefs(struct btrfs_inode *inode, struct btrfs_path *path)
{
int ret = 0;
int name_len;
unsigned int nlink = 0;
u32 item_size;
u32 cur_offset = 0;
u64 inode_objectid = btrfs_ino(inode);
u64 offset = 0;
unsigned long ptr;
struct btrfs_inode_extref *extref;
struct extent_buffer *leaf;
while (1) {
ret = btrfs_find_one_extref(inode->root, inode_objectid, offset,
path, &extref, &offset);
if (ret)
break;
leaf = path->nodes[0];
item_size = btrfs_item_size(leaf, path->slots[0]);
ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
cur_offset = 0;
while (cur_offset < item_size) {
extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
name_len = btrfs_inode_extref_name_len(leaf, extref);
nlink++;
cur_offset += name_len + sizeof(*extref);
}
offset++;
btrfs_release_path(path);
}
btrfs_release_path(path);
if (ret < 0 && ret != -ENOENT)
return ret;
return nlink;
}
static int count_inode_refs(struct btrfs_inode *inode, struct btrfs_path *path)
{
int ret;
struct btrfs_key key;
unsigned int nlink = 0;
unsigned long ptr;
unsigned long ptr_end;
int name_len;
u64 ino = btrfs_ino(inode);
key.objectid = ino;
key.type = BTRFS_INODE_REF_KEY;
key.offset = (u64)-1;
while (1) {
ret = btrfs_search_slot(NULL, inode->root, &key, path, 0, 0);
if (ret < 0)
break;
if (ret > 0) {
if (path->slots[0] == 0)
break;
path->slots[0]--;
}
process_slot:
btrfs_item_key_to_cpu(path->nodes[0], &key,
path->slots[0]);
if (key.objectid != ino ||
key.type != BTRFS_INODE_REF_KEY)
break;
ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
ptr_end = ptr + btrfs_item_size(path->nodes[0],
path->slots[0]);
while (ptr < ptr_end) {
struct btrfs_inode_ref *ref;
ref = (struct btrfs_inode_ref *)ptr;
name_len = btrfs_inode_ref_name_len(path->nodes[0],
ref);
ptr = (unsigned long)(ref + 1) + name_len;
nlink++;
}
if (key.offset == 0)
break;
if (path->slots[0] > 0) {
path->slots[0]--;
goto process_slot;
}
key.offset--;
btrfs_release_path(path);
}
btrfs_release_path(path);
return nlink;
}
static noinline int fixup_inode_link_count(struct walk_control *wc,
struct btrfs_inode *inode)
{
struct btrfs_trans_handle *trans = wc->trans;
struct btrfs_root *root = inode->root;
int ret;
u64 nlink = 0;
const u64 ino = btrfs_ino(inode);
ret = count_inode_refs(inode, wc->subvol_path);
if (ret < 0)
goto out;
nlink = ret;
ret = count_inode_extrefs(inode, wc->subvol_path);
if (ret < 0)
goto out;
nlink += ret;
ret = 0;
if (nlink != inode->vfs_inode.i_nlink) {
set_nlink(&inode->vfs_inode, nlink);
ret = btrfs_update_inode(trans, inode);
if (ret)
goto out;
}
if (S_ISDIR(inode->vfs_inode.i_mode))
inode->index_cnt = (u64)-1;
if (inode->vfs_inode.i_nlink == 0) {
if (S_ISDIR(inode->vfs_inode.i_mode)) {
ret = replay_dir_deletes(wc, ino, true);
if (ret)
goto out;
}
ret = btrfs_insert_orphan_item(trans, root, ino);
if (ret == -EEXIST)
ret = 0;
}
out:
btrfs_release_path(wc->subvol_path);
return ret;
}
static noinline int fixup_inode_link_counts(struct walk_control *wc)
{
int ret;
struct btrfs_key key;
key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
key.type = BTRFS_ORPHAN_ITEM_KEY;
key.offset = (u64)-1;
while (1) {
struct btrfs_trans_handle *trans = wc->trans;
struct btrfs_root *root = wc->root;
struct btrfs_inode *inode;
ret = btrfs_search_slot(trans, root, &key, wc->subvol_path, -1, 1);
if (ret < 0)
break;
if (ret == 1) {
ret = 0;
if (wc->subvol_path->slots[0] == 0)
break;
wc->subvol_path->slots[0]--;
}
btrfs_item_key_to_cpu(wc->subvol_path->nodes[0], &key, wc->subvol_path->slots[0]);
if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
key.type != BTRFS_ORPHAN_ITEM_KEY)
break;
ret = btrfs_del_item(trans, root, wc->subvol_path);
if (ret)
break;
btrfs_release_path(wc->subvol_path);
inode = btrfs_iget_logging(key.offset, root);
if (IS_ERR(inode)) {
ret = PTR_ERR(inode);
break;
}
ret = fixup_inode_link_count(wc, inode);
iput(&inode->vfs_inode);
if (ret)
break;
key.offset = (u64)-1;
}
btrfs_release_path(wc->subvol_path);
return ret;
}
static noinline int link_to_fixup_dir(struct walk_control *wc, u64 objectid)
{
struct btrfs_trans_handle *trans = wc->trans;
struct btrfs_root *root = wc->root;
struct btrfs_key key;
int ret = 0;
struct btrfs_inode *inode;
struct inode *vfs_inode;
inode = btrfs_iget_logging(objectid, root);
if (IS_ERR(inode)) {
ret = PTR_ERR(inode);
btrfs_abort_log_replay(wc, ret,
"failed to lookup inode %llu root %llu",
objectid, btrfs_root_id(root));
return ret;
}
vfs_inode = &inode->vfs_inode;
key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
key.type = BTRFS_ORPHAN_ITEM_KEY;
key.offset = objectid;
ret = btrfs_insert_empty_item(trans, root, wc->subvol_path, &key, 0);
btrfs_release_path(wc->subvol_path);
if (ret == 0) {
if (!vfs_inode->i_nlink)
set_nlink(vfs_inode, 1);
else
inc_nlink(vfs_inode);
ret = btrfs_update_inode(trans, inode);
if (ret)
btrfs_abort_log_replay(wc, ret,
"failed to update inode %llu root %llu",
objectid, btrfs_root_id(root));
} else if (ret == -EEXIST) {
ret = 0;
} else {
btrfs_abort_log_replay(wc, ret,
"failed to insert fixup item for inode %llu root %llu",
objectid, btrfs_root_id(root));
}
iput(vfs_inode);
return ret;
}
static noinline int insert_one_name(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
u64 dirid, u64 index,
const struct fscrypt_str *name,
struct btrfs_key *location)
{
struct btrfs_inode *inode;
struct btrfs_inode *dir;
int ret;
inode = btrfs_iget_logging(location->objectid, root);
if (IS_ERR(inode))
return PTR_ERR(inode);
dir = btrfs_iget_logging(dirid, root);
if (IS_ERR(dir)) {
iput(&inode->vfs_inode);
return PTR_ERR(dir);
}
ret = btrfs_add_link(trans, dir, inode, name, true, index, NULL);
iput(&inode->vfs_inode);
iput(&dir->vfs_inode);
return ret;
}
static int delete_conflicting_dir_entry(struct walk_control *wc,
struct btrfs_inode *dir,
struct btrfs_dir_item *dst_di,
const struct btrfs_key *log_key,
u8 log_flags,
bool exists)
{
struct btrfs_key found_key;
btrfs_dir_item_key_to_cpu(wc->subvol_path->nodes[0], dst_di, &found_key);
if (found_key.objectid == log_key->objectid &&
found_key.type == log_key->type &&
found_key.offset == log_key->offset &&
btrfs_dir_flags(wc->subvol_path->nodes[0], dst_di) == log_flags)
return 1;
if (!exists)
return 0;
return drop_one_dir_item(wc, dir, dst_di);
}
static noinline int replay_one_name(struct walk_control *wc, struct btrfs_dir_item *di)
{
struct btrfs_trans_handle *trans = wc->trans;
struct btrfs_root *root = wc->root;
struct fscrypt_str name = { 0 };
struct btrfs_dir_item *dir_dst_di;
struct btrfs_dir_item *index_dst_di;
bool dir_dst_matches = false;
bool index_dst_matches = false;
struct btrfs_key log_key;
struct btrfs_key search_key;
struct btrfs_inode *dir;
u8 log_flags;
bool exists;
int ret;
bool update_size = true;
bool name_added = false;
dir = btrfs_iget_logging(wc->log_key.objectid, root);
if (IS_ERR(dir)) {
ret = PTR_ERR(dir);
btrfs_abort_log_replay(wc, ret,
"failed to lookup dir inode %llu root %llu",
wc->log_key.objectid, btrfs_root_id(root));
return ret;
}
ret = read_alloc_one_name(wc->log_leaf, di + 1,
btrfs_dir_name_len(wc->log_leaf, di), &name);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to allocate name for dir %llu root %llu",
btrfs_ino(dir), btrfs_root_id(root));
goto out;
}
log_flags = btrfs_dir_flags(wc->log_leaf, di);
btrfs_dir_item_key_to_cpu(wc->log_leaf, di, &log_key);
ret = btrfs_lookup_inode(trans, root, wc->subvol_path, &log_key, 0);
btrfs_release_path(wc->subvol_path);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to lookup inode %llu root %llu",
log_key.objectid, btrfs_root_id(root));
goto out;
}
exists = (ret == 0);
ret = 0;
dir_dst_di = btrfs_lookup_dir_item(trans, root, wc->subvol_path,
wc->log_key.objectid, &name, 1);
if (IS_ERR(dir_dst_di)) {
ret = PTR_ERR(dir_dst_di);
btrfs_abort_log_replay(wc, ret,
"failed to lookup dir item for dir %llu name %.*s root %llu",
wc->log_key.objectid, name.len, name.name,
btrfs_root_id(root));
goto out;
} else if (dir_dst_di) {
ret = delete_conflicting_dir_entry(wc, dir, dir_dst_di,
&log_key, log_flags, exists);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to delete conflicting entry for dir %llu name %.*s root %llu",
btrfs_ino(dir), name.len, name.name,
btrfs_root_id(root));
goto out;
}
dir_dst_matches = (ret == 1);
}
btrfs_release_path(wc->subvol_path);
index_dst_di = btrfs_lookup_dir_index_item(trans, root, wc->subvol_path,
wc->log_key.objectid,
wc->log_key.offset, &name, 1);
if (IS_ERR(index_dst_di)) {
ret = PTR_ERR(index_dst_di);
btrfs_abort_log_replay(wc, ret,
"failed to lookup dir index item for dir %llu name %.*s root %llu",
wc->log_key.objectid, name.len, name.name,
btrfs_root_id(root));
goto out;
} else if (index_dst_di) {
ret = delete_conflicting_dir_entry(wc, dir, index_dst_di,
&log_key, log_flags, exists);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to delete conflicting entry for dir %llu name %.*s root %llu",
btrfs_ino(dir), name.len, name.name,
btrfs_root_id(root));
goto out;
}
index_dst_matches = (ret == 1);
}
btrfs_release_path(wc->subvol_path);
if (dir_dst_matches && index_dst_matches) {
ret = 0;
update_size = false;
goto out;
}
search_key.objectid = log_key.objectid;
search_key.type = BTRFS_INODE_REF_KEY;
search_key.offset = wc->log_key.objectid;
ret = backref_in_log(root->log_root, &search_key, 0, &name);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to check if ref item is logged for inode %llu dir %llu name %.*s root %llu",
search_key.objectid, btrfs_ino(dir),
name.len, name.name, btrfs_root_id(root));
goto out;
} else if (ret) {
ret = 0;
update_size = false;
goto out;
}
search_key.objectid = log_key.objectid;
search_key.type = BTRFS_INODE_EXTREF_KEY;
search_key.offset = btrfs_extref_hash(wc->log_key.objectid, name.name, name.len);
ret = backref_in_log(root->log_root, &search_key, wc->log_key.objectid, &name);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to check if extref item is logged for inode %llu dir %llu name %.*s root %llu",
search_key.objectid, btrfs_ino(dir),
name.len, name.name, btrfs_root_id(root));
goto out;
} else if (ret) {
ret = 0;
update_size = false;
goto out;
}
ret = insert_one_name(trans, root, wc->log_key.objectid, wc->log_key.offset,
&name, &log_key);
if (ret && ret != -ENOENT && ret != -EEXIST) {
btrfs_abort_log_replay(wc, ret,
"failed to insert name %.*s for inode %llu dir %llu root %llu",
name.len, name.name, log_key.objectid,
btrfs_ino(dir), btrfs_root_id(root));
goto out;
}
if (!ret)
name_added = true;
update_size = false;
ret = 0;
out:
if (!ret && update_size) {
btrfs_i_size_write(dir, dir->vfs_inode.i_size + name.len * 2);
ret = btrfs_update_inode(trans, dir);
if (ret)
btrfs_abort_log_replay(wc, ret,
"failed to update dir inode %llu root %llu",
btrfs_ino(dir), btrfs_root_id(root));
}
kfree(name.name);
iput(&dir->vfs_inode);
if (!ret && name_added)
ret = 1;
return ret;
}
static noinline int replay_one_dir_item(struct walk_control *wc)
{
int ret;
struct btrfs_dir_item *di;
ASSERT(wc->log_key.type == BTRFS_DIR_INDEX_KEY,
"wc->log_key.type=%u", wc->log_key.type);
di = btrfs_item_ptr(wc->log_leaf, wc->log_slot, struct btrfs_dir_item);
ret = replay_one_name(wc, di);
if (ret < 0)
return ret;
if (ret == 1 && btrfs_dir_ftype(wc->log_leaf, di) != BTRFS_FT_DIR) {
struct btrfs_key di_key;
btrfs_dir_item_key_to_cpu(wc->log_leaf, di, &di_key);
ret = link_to_fixup_dir(wc, di_key.objectid);
}
return ret;
}
static noinline int find_dir_range(struct btrfs_root *root,
struct btrfs_path *path,
u64 dirid,
u64 *start_ret, u64 *end_ret)
{
struct btrfs_key key;
u64 found_end;
struct btrfs_dir_log_item *item;
int ret;
int nritems;
if (*start_ret == (u64)-1)
return 1;
key.objectid = dirid;
key.type = BTRFS_DIR_LOG_INDEX_KEY;
key.offset = *start_ret;
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0)
goto out;
if (ret > 0) {
if (path->slots[0] == 0)
goto out;
path->slots[0]--;
}
if (ret != 0)
btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) {
ret = 1;
goto next;
}
item = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_dir_log_item);
found_end = btrfs_dir_log_end(path->nodes[0], item);
if (*start_ret >= key.offset && *start_ret <= found_end) {
ret = 0;
*start_ret = key.offset;
*end_ret = found_end;
goto out;
}
ret = 1;
next:
nritems = btrfs_header_nritems(path->nodes[0]);
path->slots[0]++;
if (path->slots[0] >= nritems) {
ret = btrfs_next_leaf(root, path);
if (ret)
goto out;
}
btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
if (key.type != BTRFS_DIR_LOG_INDEX_KEY || key.objectid != dirid) {
ret = 1;
goto out;
}
item = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_dir_log_item);
found_end = btrfs_dir_log_end(path->nodes[0], item);
*start_ret = key.offset;
*end_ret = found_end;
ret = 0;
out:
btrfs_release_path(path);
return ret;
}
static noinline int check_item_in_log(struct walk_control *wc,
struct btrfs_path *log_path,
struct btrfs_inode *dir,
struct btrfs_key *dir_key,
bool force_remove)
{
struct btrfs_trans_handle *trans = wc->trans;
struct btrfs_root *root = dir->root;
int ret;
struct extent_buffer *eb;
int slot;
struct btrfs_dir_item *di;
struct fscrypt_str name = { 0 };
struct btrfs_inode *inode = NULL;
struct btrfs_key location;
ASSERT(dir_key->type == BTRFS_DIR_INDEX_KEY, "dir_key->type=%u", dir_key->type);
eb = wc->subvol_path->nodes[0];
slot = wc->subvol_path->slots[0];
di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
ret = read_alloc_one_name(eb, di + 1, btrfs_dir_name_len(eb, di), &name);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to allocate name for dir %llu index %llu root %llu",
btrfs_ino(dir), dir_key->offset,
btrfs_root_id(root));
goto out;
}
if (!force_remove) {
struct btrfs_dir_item *log_di;
log_di = btrfs_lookup_dir_index_item(trans, wc->log, log_path,
dir_key->objectid,
dir_key->offset, &name, 0);
if (IS_ERR(log_di)) {
ret = PTR_ERR(log_di);
btrfs_abort_log_replay(wc, ret,
"failed to lookup dir index item for dir %llu index %llu name %.*s root %llu",
btrfs_ino(dir), dir_key->offset,
name.len, name.name,
btrfs_root_id(root));
goto out;
} else if (log_di) {
ret = 0;
goto out;
}
}
btrfs_dir_item_key_to_cpu(eb, di, &location);
btrfs_release_path(wc->subvol_path);
btrfs_release_path(log_path);
inode = btrfs_iget_logging(location.objectid, root);
if (IS_ERR(inode)) {
ret = PTR_ERR(inode);
inode = NULL;
btrfs_abort_log_replay(wc, ret,
"failed to lookup inode %llu root %llu",
location.objectid, btrfs_root_id(root));
goto out;
}
ret = link_to_fixup_dir(wc, location.objectid);
if (ret)
goto out;
inc_nlink(&inode->vfs_inode);
ret = unlink_inode_for_log_replay(wc, dir, inode, &name);
out:
btrfs_release_path(wc->subvol_path);
btrfs_release_path(log_path);
kfree(name.name);
if (inode)
iput(&inode->vfs_inode);
return ret;
}
static int replay_xattr_deletes(struct walk_control *wc)
{
struct btrfs_trans_handle *trans = wc->trans;
struct btrfs_root *root = wc->root;
struct btrfs_root *log = wc->log;
struct btrfs_key search_key;
BTRFS_PATH_AUTO_FREE(log_path);
const u64 ino = wc->log_key.objectid;
int nritems;
int ret;
log_path = btrfs_alloc_path();
if (!log_path) {
btrfs_abort_log_replay(wc, -ENOMEM, "failed to allocate path");
return -ENOMEM;
}
search_key.objectid = ino;
search_key.type = BTRFS_XATTR_ITEM_KEY;
search_key.offset = 0;
again:
ret = btrfs_search_slot(NULL, root, &search_key, wc->subvol_path, 0, 0);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to search xattrs for inode %llu root %llu",
ino, btrfs_root_id(root));
goto out;
}
process_leaf:
nritems = btrfs_header_nritems(wc->subvol_path->nodes[0]);
for (int i = wc->subvol_path->slots[0]; i < nritems; i++) {
struct btrfs_key key;
struct btrfs_dir_item *di;
struct btrfs_dir_item *log_di;
u32 total_size;
u32 cur;
btrfs_item_key_to_cpu(wc->subvol_path->nodes[0], &key, i);
if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
ret = 0;
goto out;
}
di = btrfs_item_ptr(wc->subvol_path->nodes[0], i, struct btrfs_dir_item);
total_size = btrfs_item_size(wc->subvol_path->nodes[0], i);
cur = 0;
while (cur < total_size) {
u16 name_len = btrfs_dir_name_len(wc->subvol_path->nodes[0], di);
u16 data_len = btrfs_dir_data_len(wc->subvol_path->nodes[0], di);
u32 this_len = sizeof(*di) + name_len + data_len;
char *name;
name = kmalloc(name_len, GFP_NOFS);
if (!name) {
ret = -ENOMEM;
btrfs_abort_log_replay(wc, ret,
"failed to allocate memory for name of length %u",
name_len);
goto out;
}
read_extent_buffer(wc->subvol_path->nodes[0], name,
(unsigned long)(di + 1), name_len);
log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
name, name_len, 0);
btrfs_release_path(log_path);
if (!log_di) {
btrfs_release_path(wc->subvol_path);
di = btrfs_lookup_xattr(trans, root, wc->subvol_path, ino,
name, name_len, -1);
if (IS_ERR(di)) {
ret = PTR_ERR(di);
btrfs_abort_log_replay(wc, ret,
"failed to lookup xattr with name %.*s for inode %llu root %llu",
name_len, name, ino,
btrfs_root_id(root));
kfree(name);
goto out;
}
ASSERT(di);
ret = btrfs_delete_one_dir_name(trans, root,
wc->subvol_path, di);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to delete xattr with name %.*s for inode %llu root %llu",
name_len, name, ino,
btrfs_root_id(root));
kfree(name);
goto out;
}
btrfs_release_path(wc->subvol_path);
kfree(name);
search_key = key;
goto again;
}
if (IS_ERR(log_di)) {
ret = PTR_ERR(log_di);
btrfs_abort_log_replay(wc, ret,
"failed to lookup xattr in log tree with name %.*s for inode %llu root %llu",
name_len, name, ino,
btrfs_root_id(root));
kfree(name);
goto out;
}
kfree(name);
cur += this_len;
di = (struct btrfs_dir_item *)((char *)di + this_len);
}
}
ret = btrfs_next_leaf(root, wc->subvol_path);
if (ret > 0)
ret = 0;
else if (ret == 0)
goto process_leaf;
else
btrfs_abort_log_replay(wc, ret,
"failed to get next leaf in subvolume root %llu",
btrfs_root_id(root));
out:
btrfs_release_path(wc->subvol_path);
return ret;
}
static noinline int replay_dir_deletes(struct walk_control *wc,
u64 dirid, bool del_all)
{
struct btrfs_root *root = wc->root;
struct btrfs_root *log = (del_all ? NULL : wc->log);
u64 range_start;
u64 range_end;
int ret = 0;
struct btrfs_key dir_key;
struct btrfs_key found_key;
BTRFS_PATH_AUTO_FREE(log_path);
struct btrfs_inode *dir;
dir_key.objectid = dirid;
dir_key.type = BTRFS_DIR_INDEX_KEY;
log_path = btrfs_alloc_path();
if (!log_path) {
btrfs_abort_log_replay(wc, -ENOMEM, "failed to allocate path");
return -ENOMEM;
}
dir = btrfs_iget_logging(dirid, root);
if (IS_ERR(dir)) {
ret = PTR_ERR(dir);
if (ret == -ENOENT)
ret = 0;
else
btrfs_abort_log_replay(wc, ret,
"failed to lookup dir inode %llu root %llu",
dirid, btrfs_root_id(root));
return ret;
}
range_start = 0;
range_end = 0;
while (1) {
if (del_all)
range_end = (u64)-1;
else {
ret = find_dir_range(log, wc->subvol_path, dirid,
&range_start, &range_end);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to find range for dir %llu in log tree root %llu",
dirid, btrfs_root_id(root));
goto out;
} else if (ret > 0) {
break;
}
}
dir_key.offset = range_start;
while (1) {
int nritems;
ret = btrfs_search_slot(NULL, root, &dir_key,
wc->subvol_path, 0, 0);
if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to search root %llu for key " BTRFS_KEY_FMT,
btrfs_root_id(root),
BTRFS_KEY_FMT_VALUE(&dir_key));
goto out;
}
nritems = btrfs_header_nritems(wc->subvol_path->nodes[0]);
if (wc->subvol_path->slots[0] >= nritems) {
ret = btrfs_next_leaf(root, wc->subvol_path);
if (ret == 1) {
break;
} else if (ret < 0) {
btrfs_abort_log_replay(wc, ret,
"failed to get next leaf in subvolume root %llu",
btrfs_root_id(root));
goto out;
}
}
btrfs_item_key_to_cpu(wc->subvol_path->nodes[0], &found_key,
wc->subvol_path->slots[0]);
if (found_key.objectid != dirid ||
found_key.type != dir_key.type) {
ret = 0;
goto out;
}
if (found_key.offset > range_end)
break;
ret = check_item_in_log(wc, log_path, dir, &found_key, del_all);
if (ret)
goto out;
if (found_key.offset == (u64)-1)
break;
dir_key.offset = found_key.offset + 1;
}
btrfs_release_path(wc->subvol_path);
if (range_end == (u64)-1)
break;
range_start = range_end + 1;
}
ret = 0;
out:
btrfs_release_path(wc->subvol_path);
iput(&dir->vfs_inode);
return ret;
}
static int replay_one_buffer(struct extent_buffer *eb,
struct walk_control *wc, u64 gen, int level)
{
int nritems;
struct btrfs_tree_parent_check check = {
.transid = gen,
.level = level
};
struct btrfs_root *root = wc->root;
struct btrfs_trans_handle *trans = wc->trans;
int ret;
if (level != 0)
return 0;
wc->log_leaf = NULL;
ret = btrfs_read_extent_buffer(eb, &check);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to read log tree leaf %llu for root %llu",
eb->start, btrfs_root_id(root));
return ret;
}
ASSERT(wc->subvol_path == NULL);
wc->subvol_path = btrfs_alloc_path();
if (!wc->subvol_path) {
btrfs_abort_log_replay(wc, -ENOMEM, "failed to allocate path");
return -ENOMEM;
}
wc->log_leaf = eb;
nritems = btrfs_header_nritems(eb);
for (wc->log_slot = 0; wc->log_slot < nritems; wc->log_slot++) {
struct btrfs_inode_item *inode_item = NULL;
btrfs_item_key_to_cpu(eb, &wc->log_key, wc->log_slot);
if (wc->log_key.type == BTRFS_INODE_ITEM_KEY) {
inode_item = btrfs_item_ptr(eb, wc->log_slot,
struct btrfs_inode_item);
if (btrfs_inode_nlink(eb, inode_item) == 0) {
wc->ignore_cur_inode = true;
continue;
} else {
wc->ignore_cur_inode = false;
}
}
if (wc->log_key.type == BTRFS_INODE_ITEM_KEY &&
wc->stage == LOG_WALK_REPLAY_INODES) {
u32 mode;
ret = replay_xattr_deletes(wc);
if (ret)
break;
mode = btrfs_inode_mode(eb, inode_item);
if (S_ISDIR(mode)) {
ret = replay_dir_deletes(wc, wc->log_key.objectid, false);
if (ret)
break;
}
ret = overwrite_item(wc);
if (ret)
break;
if (S_ISREG(mode)) {
struct btrfs_drop_extents_args drop_args = { 0 };
struct btrfs_inode *inode;
u64 from;
inode = btrfs_iget_logging(wc->log_key.objectid, root);
if (IS_ERR(inode)) {
ret = PTR_ERR(inode);
btrfs_abort_log_replay(wc, ret,
"failed to lookup inode %llu root %llu",
wc->log_key.objectid,
btrfs_root_id(root));
break;
}
from = ALIGN(i_size_read(&inode->vfs_inode),
root->fs_info->sectorsize);
drop_args.start = from;
drop_args.end = (u64)-1;
drop_args.drop_cache = true;
drop_args.path = wc->subvol_path;
ret = btrfs_drop_extents(trans, root, inode, &drop_args);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to drop extents for inode %llu root %llu offset %llu",
btrfs_ino(inode),
btrfs_root_id(root),
from);
} else {
inode_sub_bytes(&inode->vfs_inode,
drop_args.bytes_found);
ret = btrfs_update_inode(trans, inode);
if (ret)
btrfs_abort_log_replay(wc, ret,
"failed to update inode %llu root %llu",
btrfs_ino(inode),
btrfs_root_id(root));
}
iput(&inode->vfs_inode);
if (ret)
break;
}
ret = link_to_fixup_dir(wc, wc->log_key.objectid);
if (ret)
break;
}
if (wc->ignore_cur_inode)
continue;
if (wc->log_key.type == BTRFS_DIR_INDEX_KEY &&
wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
ret = replay_one_dir_item(wc);
if (ret)
break;
}
if (wc->stage < LOG_WALK_REPLAY_ALL)
continue;
if (wc->log_key.type == BTRFS_XATTR_ITEM_KEY) {
ret = overwrite_item(wc);
if (ret)
break;
} else if (wc->log_key.type == BTRFS_INODE_REF_KEY ||
wc->log_key.type == BTRFS_INODE_EXTREF_KEY) {
ret = add_inode_ref(wc);
if (ret)
break;
} else if (wc->log_key.type == BTRFS_EXTENT_DATA_KEY) {
ret = replay_one_extent(wc);
if (ret)
break;
}
}
btrfs_free_path(wc->subvol_path);
wc->subvol_path = NULL;
return ret;
}
static int clean_log_buffer(struct btrfs_trans_handle *trans,
struct extent_buffer *eb)
{
struct btrfs_fs_info *fs_info = eb->fs_info;
struct btrfs_block_group *bg;
btrfs_tree_lock(eb);
btrfs_clear_buffer_dirty(trans, eb);
wait_on_extent_buffer_writeback(eb);
btrfs_tree_unlock(eb);
if (trans) {
int ret;
ret = btrfs_pin_reserved_extent(trans, eb);
if (ret)
btrfs_abort_transaction(trans, ret);
return ret;
}
bg = btrfs_lookup_block_group(fs_info, eb->start);
if (!bg) {
btrfs_err(fs_info, "unable to find block group for %llu", eb->start);
btrfs_handle_fs_error(fs_info, -ENOENT, NULL);
return -ENOENT;
}
spin_lock(&bg->space_info->lock);
spin_lock(&bg->lock);
bg->reserved -= fs_info->nodesize;
bg->space_info->bytes_reserved -= fs_info->nodesize;
spin_unlock(&bg->lock);
spin_unlock(&bg->space_info->lock);
btrfs_put_block_group(bg);
return 0;
}
static noinline int walk_down_log_tree(struct btrfs_path *path, int *level,
struct walk_control *wc)
{
struct btrfs_trans_handle *trans = wc->trans;
struct btrfs_fs_info *fs_info = wc->log->fs_info;
struct btrfs_eb_prealloc pa = { 0 };
u64 bytenr;
u64 ptr_gen;
struct extent_buffer *next;
struct extent_buffer *cur;
int ret = 0;
while (*level > 0) {
struct btrfs_tree_parent_check check = { 0 };
cur = path->nodes[*level];
WARN_ON(btrfs_header_level(cur) != *level);
if (path->slots[*level] >=
btrfs_header_nritems(cur))
break;
bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
check.transid = ptr_gen;
check.level = *level - 1;
check.has_first_key = true;
btrfs_node_key_to_cpu(cur, &check.first_key, path->slots[*level]);
next = btrfs_find_create_tree_block(fs_info, &pa, bytenr,
btrfs_header_owner(cur),
*level - 1);
if (IS_ERR(next)) {
ret = PTR_ERR(next);
if (trans)
btrfs_abort_transaction(trans, ret);
else
btrfs_handle_fs_error(fs_info, ret, NULL);
return ret;
}
if (*level == 1) {
ret = wc->process_func(next, wc, ptr_gen, *level - 1);
if (ret) {
free_extent_buffer(next);
return ret;
}
path->slots[*level]++;
if (wc->free) {
ret = btrfs_read_extent_buffer(next, &check);
if (ret) {
free_extent_buffer(next);
if (trans)
btrfs_abort_transaction(trans, ret);
else
btrfs_handle_fs_error(fs_info, ret, NULL);
return ret;
}
ret = clean_log_buffer(trans, next);
if (ret) {
free_extent_buffer(next);
return ret;
}
}
free_extent_buffer(next);
continue;
}
ret = btrfs_read_extent_buffer(next, &check);
if (ret) {
free_extent_buffer(next);
if (trans)
btrfs_abort_transaction(trans, ret);
else
btrfs_handle_fs_error(fs_info, ret, NULL);
return ret;
}
if (path->nodes[*level-1])
free_extent_buffer(path->nodes[*level-1]);
path->nodes[*level-1] = next;
*level = btrfs_header_level(next);
path->slots[*level] = 0;
cond_resched();
}
path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
cond_resched();
return 0;
}
static noinline int walk_up_log_tree(struct btrfs_path *path, int *level,
struct walk_control *wc)
{
int i;
int slot;
int ret;
for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
slot = path->slots[i];
if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
path->slots[i]++;
*level = i;
WARN_ON(*level == 0);
return 0;
} else {
ret = wc->process_func(path->nodes[*level], wc,
btrfs_header_generation(path->nodes[*level]),
*level);
if (ret)
return ret;
if (wc->free) {
ret = clean_log_buffer(wc->trans, path->nodes[*level]);
if (ret)
return ret;
}
free_extent_buffer(path->nodes[*level]);
path->nodes[*level] = NULL;
*level = i + 1;
}
}
return 1;
}
static int walk_log_tree(struct walk_control *wc)
{
struct btrfs_root *log = wc->log;
int ret = 0;
int wret;
int level;
BTRFS_PATH_AUTO_FREE(path);
int orig_level;
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
level = btrfs_header_level(log->node);
orig_level = level;
path->nodes[level] = log->node;
refcount_inc(&log->node->refs);
path->slots[level] = 0;
while (1) {
wret = walk_down_log_tree(path, &level, wc);
if (wret > 0)
break;
if (wret < 0)
return wret;
wret = walk_up_log_tree(path, &level, wc);
if (wret > 0)
break;
if (wret < 0)
return wret;
}
if (path->nodes[orig_level]) {
ret = wc->process_func(path->nodes[orig_level], wc,
btrfs_header_generation(path->nodes[orig_level]),
orig_level);
if (ret)
return ret;
if (wc->free)
ret = clean_log_buffer(wc->trans, path->nodes[orig_level]);
}
return ret;
}
static int update_log_root(struct btrfs_trans_handle *trans,
struct btrfs_root *log,
struct btrfs_root_item *root_item)
{
struct btrfs_fs_info *fs_info = log->fs_info;
int ret;
if (log->log_transid == 1) {
ret = btrfs_insert_root(trans, fs_info->log_root_tree,
&log->root_key, root_item);
} else {
ret = btrfs_update_root(trans, fs_info->log_root_tree,
&log->root_key, root_item);
}
return ret;
}
static bool wait_log_commit(struct btrfs_root *root, int transid)
{
DEFINE_WAIT(wait);
const int index = (transid >= 0 ? transid % 2 : -transid % 2);
if (!root->log_commit[index])
return false;
for (;;) {
prepare_to_wait(&root->log_commit_wait[index],
&wait, TASK_UNINTERRUPTIBLE);
mutex_unlock(&root->log_mutex);
schedule();
mutex_lock(&root->log_mutex);
if (!(root->log_transid_committed < transid &&
root->log_commit[index]))
break;
}
finish_wait(&root->log_commit_wait[index], &wait);
return true;
}
static void wait_for_writer(struct btrfs_root *root)
{
DEFINE_WAIT(wait);
for (;;) {
prepare_to_wait(&root->log_writer_wait, &wait,
TASK_UNINTERRUPTIBLE);
if (!atomic_read(&root->log_writers))
break;
mutex_unlock(&root->log_mutex);
schedule();
mutex_lock(&root->log_mutex);
}
finish_wait(&root->log_writer_wait, &wait);
}
void btrfs_init_log_ctx(struct btrfs_log_ctx *ctx, struct btrfs_inode *inode)
{
ctx->log_ret = 0;
ctx->log_transid = 0;
ctx->log_new_dentries = false;
ctx->logging_new_name = false;
ctx->logging_new_delayed_dentries = false;
ctx->logged_before = false;
ctx->inode = inode;
INIT_LIST_HEAD(&ctx->list);
INIT_LIST_HEAD(&ctx->ordered_extents);
INIT_LIST_HEAD(&ctx->conflict_inodes);
ctx->num_conflict_inodes = 0;
ctx->logging_conflict_inodes = false;
ctx->scratch_eb = NULL;
}
void btrfs_init_log_ctx_scratch_eb(struct btrfs_log_ctx *ctx)
{
struct btrfs_inode *inode = ctx->inode;
if (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) &&
!test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags))
return;
ctx->scratch_eb = alloc_dummy_extent_buffer(inode->root->fs_info, 0);
}
void btrfs_release_log_ctx_extents(struct btrfs_log_ctx *ctx)
{
struct btrfs_ordered_extent *ordered;
struct btrfs_ordered_extent *tmp;
btrfs_assert_inode_locked(ctx->inode);
list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
list_del_init(&ordered->log_list);
btrfs_put_ordered_extent(ordered);
}
}
static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
struct btrfs_log_ctx *ctx)
{
mutex_lock(&root->log_mutex);
list_del_init(&ctx->list);
mutex_unlock(&root->log_mutex);
}
static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
int index, int error)
{
struct btrfs_log_ctx *ctx;
struct btrfs_log_ctx *safe;
list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
list_del_init(&ctx->list);
ctx->log_ret = error;
}
}
int btrfs_sync_log(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_log_ctx *ctx)
{
int mark;
int ret;
struct btrfs_fs_info *fs_info = root->fs_info;
struct btrfs_root *log = root->log_root;
struct btrfs_root *log_root_tree = fs_info->log_root_tree;
struct btrfs_root_item new_root_item;
int log_transid = ctx->log_transid;
int index1 = log_transid % 2;
int index2;
struct btrfs_log_ctx root_log_ctx;
struct blk_plug plug;
u64 log_root_start;
u64 log_root_level;
mutex_lock(&root->log_mutex);
trace_btrfs_sync_log_enter(trans, root, ctx);
if (root->log_transid_committed >= log_transid) {
trace_btrfs_sync_log_exit(trans, root, ctx, ctx->log_ret);
mutex_unlock(&root->log_mutex);
return ctx->log_ret;
}
if (wait_log_commit(root, log_transid)) {
trace_btrfs_sync_log_exit(trans, root, ctx, ctx->log_ret);
mutex_unlock(&root->log_mutex);
return ctx->log_ret;
}
ASSERT(log_transid == root->log_transid,
"log_transid=%d root->log_transid=%d", log_transid, root->log_transid);
root->log_commit[index1] = true;
wait_log_commit(root, log_transid - 1);
wait_for_writer(root);
if (btrfs_need_log_full_commit(trans)) {
ret = BTRFS_LOG_FORCE_COMMIT;
trace_btrfs_sync_log_exit(trans, root, ctx, ret);
mutex_unlock(&root->log_mutex);
goto out;
}
if (log_transid % 2 == 0)
mark = EXTENT_DIRTY_LOG1;
else
mark = EXTENT_DIRTY_LOG2;
blk_start_plug(&plug);
ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
if (ret == -EAGAIN && btrfs_is_zoned(fs_info))
ret = 0;
if (ret) {
trace_btrfs_sync_log_exit(trans, root, ctx, ret);
blk_finish_plug(&plug);
btrfs_set_log_full_commit(trans);
mutex_unlock(&root->log_mutex);
goto out;
}
btrfs_set_root_node(&log->root_item, log->node);
memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
btrfs_set_root_log_transid(root, root->log_transid + 1);
log->log_transid = root->log_transid;
mutex_unlock(&root->log_mutex);
if (btrfs_is_zoned(fs_info)) {
mutex_lock(&fs_info->tree_root->log_mutex);
if (!log_root_tree->node) {
ret = btrfs_alloc_log_tree_node(trans, log_root_tree);
if (ret) {
trace_btrfs_sync_log_exit(trans, root, ctx, ret);
mutex_unlock(&fs_info->tree_root->log_mutex);
blk_finish_plug(&plug);
goto out;
}
}
mutex_unlock(&fs_info->tree_root->log_mutex);
}
btrfs_init_log_ctx(&root_log_ctx, NULL);
mutex_lock(&log_root_tree->log_mutex);
index2 = log_root_tree->log_transid % 2;
list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
root_log_ctx.log_transid = log_root_tree->log_transid;
ret = update_log_root(trans, log, &new_root_item);
if (ret) {
trace_btrfs_sync_log_exit(trans, root, ctx, ret);
list_del_init(&root_log_ctx.list);
blk_finish_plug(&plug);
btrfs_set_log_full_commit(trans);
if (ret != -ENOSPC)
btrfs_err(fs_info,
"failed to update log for root %llu ret %d",
btrfs_root_id(root), ret);
btrfs_wait_tree_log_extents(log, mark);
mutex_unlock(&log_root_tree->log_mutex);
goto out;
}
if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
blk_finish_plug(&plug);
list_del_init(&root_log_ctx.list);
mutex_unlock(&log_root_tree->log_mutex);
ret = root_log_ctx.log_ret;
trace_btrfs_sync_log_exit(trans, root, ctx, ret);
goto out;
}
if (log_root_tree->log_commit[index2]) {
blk_finish_plug(&plug);
ret = btrfs_wait_tree_log_extents(log, mark);
wait_log_commit(log_root_tree,
root_log_ctx.log_transid);
mutex_unlock(&log_root_tree->log_mutex);
if (!ret)
ret = root_log_ctx.log_ret;
trace_btrfs_sync_log_exit(trans, root, ctx, ret);
goto out;
}
ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid,
"root_log_ctx.log_transid=%d log_root_tree->log_transid=%d",
root_log_ctx.log_transid, log_root_tree->log_transid);
log_root_tree->log_commit[index2] = true;
wait_log_commit(log_root_tree, root_log_ctx.log_transid - 1);
if (btrfs_need_log_full_commit(trans)) {
blk_finish_plug(&plug);
btrfs_wait_tree_log_extents(log, mark);
mutex_unlock(&log_root_tree->log_mutex);
ret = BTRFS_LOG_FORCE_COMMIT;
trace_btrfs_sync_log_exit(trans, root, ctx, ret);
goto out_wake_log_root;
}
ret = btrfs_write_marked_extents(fs_info,
&log_root_tree->dirty_log_pages,
EXTENT_DIRTY_LOG1 | EXTENT_DIRTY_LOG2);
blk_finish_plug(&plug);
if (ret == -EAGAIN && btrfs_is_zoned(fs_info)) {
trace_btrfs_sync_log_exit(trans, root, ctx, ret);
btrfs_set_log_full_commit(trans);
btrfs_wait_tree_log_extents(log, mark);
mutex_unlock(&log_root_tree->log_mutex);
goto out_wake_log_root;
} else if (ret) {
trace_btrfs_sync_log_exit(trans, root, ctx, ret);
btrfs_set_log_full_commit(trans);
mutex_unlock(&log_root_tree->log_mutex);
goto out_wake_log_root;
}
ret = btrfs_wait_tree_log_extents(log, mark);
if (!ret)
ret = btrfs_wait_tree_log_extents(log_root_tree,
EXTENT_DIRTY_LOG1 | EXTENT_DIRTY_LOG2);
if (ret) {
trace_btrfs_sync_log_exit(trans, root, ctx, ret);
btrfs_set_log_full_commit(trans);
mutex_unlock(&log_root_tree->log_mutex);
goto out_wake_log_root;
}
log_root_start = log_root_tree->node->start;
log_root_level = btrfs_header_level(log_root_tree->node);
log_root_tree->log_transid++;
mutex_unlock(&log_root_tree->log_mutex);
mutex_lock(&fs_info->tree_log_mutex);
if (unlikely(BTRFS_FS_ERROR(fs_info))) {
ret = -EIO;
trace_btrfs_sync_log_exit(trans, root, ctx, ret);
btrfs_set_log_full_commit(trans);
btrfs_abort_transaction(trans, ret);
mutex_unlock(&fs_info->tree_log_mutex);
goto out_wake_log_root;
}
btrfs_set_super_log_root(fs_info->super_for_commit, log_root_start);
btrfs_set_super_log_root_level(fs_info->super_for_commit, log_root_level);
ret = write_all_supers(trans);
mutex_unlock(&fs_info->tree_log_mutex);
if (unlikely(ret)) {
trace_btrfs_sync_log_exit(trans, root, ctx, ret);
btrfs_set_log_full_commit(trans);
btrfs_abort_transaction(trans, ret);
goto out_wake_log_root;
}
ASSERT(btrfs_get_root_last_log_commit(root) <= log_transid,
"last_log_commit(root)=%d log_transid=%d",
btrfs_get_root_last_log_commit(root), log_transid);
btrfs_set_root_last_log_commit(root, log_transid);
out_wake_log_root:
mutex_lock(&log_root_tree->log_mutex);
btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
log_root_tree->log_transid_committed++;
log_root_tree->log_commit[index2] = false;
mutex_unlock(&log_root_tree->log_mutex);
cond_wake_up(&log_root_tree->log_commit_wait[index2]);
out:
mutex_lock(&root->log_mutex);
btrfs_remove_all_log_ctxs(root, index1, ret);
root->log_transid_committed++;
root->log_commit[index1] = false;
mutex_unlock(&root->log_mutex);
cond_wake_up(&root->log_commit_wait[index1]);
return ret;
}
static void free_log_tree(struct btrfs_trans_handle *trans,
struct btrfs_root *log)
{
int ret;
struct walk_control wc = {
.free = true,
.process_func = process_one_buffer,
.log = log,
.trans = trans,
};
if (log->node) {
ret = walk_log_tree(&wc);
if (ret) {
set_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR,
&log->fs_info->fs_state);
btrfs_write_marked_extents(log->fs_info,
&log->dirty_log_pages,
EXTENT_DIRTY_LOG1 | EXTENT_DIRTY_LOG2);
btrfs_wait_tree_log_extents(log,
EXTENT_DIRTY_LOG1 | EXTENT_DIRTY_LOG2);
if (trans)
btrfs_abort_transaction(trans, ret);
else
btrfs_handle_fs_error(log->fs_info, ret, NULL);
}
}
btrfs_extent_io_tree_release(&log->dirty_log_pages);
btrfs_extent_io_tree_release(&log->log_csum_range);
btrfs_put_root(log);
}
void btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
{
if (root->log_root) {
free_log_tree(trans, root->log_root);
root->log_root = NULL;
clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
}
}
void btrfs_free_log_root_tree(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info)
{
if (fs_info->log_root_tree) {
free_log_tree(trans, fs_info->log_root_tree);
fs_info->log_root_tree = NULL;
clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &fs_info->tree_root->state);
}
}
static bool mark_inode_as_not_logged(const struct btrfs_trans_handle *trans,
struct btrfs_inode *inode)
{
bool ret = false;
spin_lock(&inode->lock);
if (inode->logged_trans == 0)
inode->logged_trans = trans->transid - 1;
else if (inode->logged_trans == trans->transid)
ret = true;
spin_unlock(&inode->lock);
return ret;
}
static int inode_logged(const struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path_in)
{
struct btrfs_path *path = path_in;
struct btrfs_key key;
int ret;
if (data_race(inode->logged_trans) == trans->transid)
return 1;
spin_lock(&inode->lock);
if (inode->logged_trans == trans->transid) {
spin_unlock(&inode->lock);
return 1;
} else if (inode->logged_trans > 0) {
spin_unlock(&inode->lock);
return 0;
}
spin_unlock(&inode->lock);
if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state))
return mark_inode_as_not_logged(trans, inode);
key.objectid = btrfs_ino(inode);
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
if (!path) {
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
}
ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0);
if (path_in)
btrfs_release_path(path);
else
btrfs_free_path(path);
if (ret < 0) {
return ret;
} else if (ret > 0) {
return mark_inode_as_not_logged(trans, inode);
}
spin_lock(&inode->lock);
inode->logged_trans = trans->transid;
spin_unlock(&inode->lock);
return 1;
}
static int del_logged_dentry(struct btrfs_trans_handle *trans,
struct btrfs_root *log,
struct btrfs_path *path,
u64 dir_ino,
const struct fscrypt_str *name,
u64 index)
{
struct btrfs_dir_item *di;
di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
index, name, -1);
if (IS_ERR(di))
return PTR_ERR(di);
else if (!di)
return 1;
return btrfs_del_item(trans, log, path);
}
void btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
const struct fscrypt_str *name,
struct btrfs_inode *dir, u64 index)
{
struct btrfs_root *root = dir->root;
BTRFS_PATH_AUTO_FREE(path);
int ret;
ret = inode_logged(trans, dir, NULL);
if (ret == 0)
return;
if (ret < 0) {
btrfs_set_log_full_commit(trans);
return;
}
path = btrfs_alloc_path();
if (!path) {
btrfs_set_log_full_commit(trans);
return;
}
ret = join_running_log_trans(root);
ASSERT(ret == 0, "join_running_log_trans() ret=%d", ret);
if (WARN_ON(ret))
return;
mutex_lock(&dir->log_mutex);
ret = del_logged_dentry(trans, root->log_root, path, btrfs_ino(dir),
name, index);
mutex_unlock(&dir->log_mutex);
if (ret < 0)
btrfs_set_log_full_commit(trans);
btrfs_end_log_trans(root);
}
void btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
const struct fscrypt_str *name,
struct btrfs_inode *inode,
struct btrfs_inode *dir)
{
struct btrfs_root *root = dir->root;
int ret;
ret = inode_logged(trans, inode, NULL);
if (ret == 0)
return;
else if (ret < 0) {
btrfs_set_log_full_commit(trans);
return;
}
ret = join_running_log_trans(root);
ASSERT(ret == 0, "join_running_log_trans() ret=%d", ret);
if (WARN_ON(ret))
return;
mutex_lock(&inode->log_mutex);
ret = btrfs_del_inode_ref(trans, root->log_root, name, btrfs_ino(inode),
btrfs_ino(dir), NULL);
mutex_unlock(&inode->log_mutex);
if (ret < 0 && ret != -ENOENT)
btrfs_set_log_full_commit(trans);
btrfs_end_log_trans(root);
}
static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
struct btrfs_root *log,
struct btrfs_path *path,
u64 dirid,
u64 first_offset, u64 last_offset)
{
int ret;
struct btrfs_key key;
struct btrfs_dir_log_item *item;
key.objectid = dirid;
key.type = BTRFS_DIR_LOG_INDEX_KEY;
key.offset = first_offset;
ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
if (ret && ret != -EEXIST)
return ret;
item = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_dir_log_item);
if (ret == -EEXIST) {
const u64 curr_end = btrfs_dir_log_end(path->nodes[0], item);
last_offset = max(last_offset, curr_end);
}
btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
btrfs_release_path(path);
return 0;
}
static int flush_dir_items_batch(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct extent_buffer *src,
struct btrfs_path *dst_path,
int start_slot,
int count)
{
struct btrfs_root *log = inode->root->log_root;
char AUTO_KFREE(ins_data);
struct btrfs_item_batch batch;
struct extent_buffer *dst;
unsigned long src_offset;
unsigned long dst_offset;
u64 last_index;
struct btrfs_key key;
u32 item_size;
int ret;
int i;
ASSERT(count > 0, "count=%d", count);
batch.nr = count;
if (count == 1) {
btrfs_item_key_to_cpu(src, &key, start_slot);
item_size = btrfs_item_size(src, start_slot);
batch.keys = &key;
batch.data_sizes = &item_size;
batch.total_data_size = item_size;
} else {
struct btrfs_key *ins_keys;
u32 *ins_sizes;
ins_data = kmalloc_array(count, sizeof(u32) + sizeof(struct btrfs_key), GFP_NOFS);
if (!ins_data)
return -ENOMEM;
ins_sizes = (u32 *)ins_data;
ins_keys = (struct btrfs_key *)(ins_data + count * sizeof(u32));
batch.keys = ins_keys;
batch.data_sizes = ins_sizes;
batch.total_data_size = 0;
for (i = 0; i < count; i++) {
const int slot = start_slot + i;
btrfs_item_key_to_cpu(src, &ins_keys[i], slot);
ins_sizes[i] = btrfs_item_size(src, slot);
batch.total_data_size += ins_sizes[i];
}
}
ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
if (ret)
return ret;
dst = dst_path->nodes[0];
dst_offset = btrfs_item_ptr_offset(dst, dst_path->slots[0] + count - 1);
src_offset = btrfs_item_ptr_offset(src, start_slot + count - 1);
copy_extent_buffer(dst, src, dst_offset, src_offset, batch.total_data_size);
btrfs_release_path(dst_path);
last_index = batch.keys[count - 1].offset;
ASSERT(last_index > inode->last_dir_index_offset,
"last_index=%llu inode->last_dir_index_offset=%llu",
last_index, inode->last_dir_index_offset);
if (WARN_ON(last_index <= inode->last_dir_index_offset))
ret = BTRFS_LOG_FORCE_COMMIT;
else
inode->last_dir_index_offset = last_index;
if (btrfs_get_first_dir_index_to_log(inode) == 0)
btrfs_set_first_dir_index_to_log(inode, batch.keys[0].offset);
return ret;
}
static int clone_leaf(struct btrfs_path *path, struct btrfs_log_ctx *ctx)
{
const int slot = path->slots[0];
if (ctx->scratch_eb) {
copy_extent_buffer_full(ctx->scratch_eb, path->nodes[0]);
} else {
ctx->scratch_eb = btrfs_clone_extent_buffer(path->nodes[0]);
if (!ctx->scratch_eb)
return -ENOMEM;
}
btrfs_release_path(path);
path->nodes[0] = ctx->scratch_eb;
path->slots[0] = slot;
refcount_inc(&ctx->scratch_eb->refs);
return 0;
}
static int process_dir_items_leaf(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path,
struct btrfs_path *dst_path,
struct btrfs_log_ctx *ctx,
u64 *last_old_dentry_offset)
{
struct btrfs_root *log = inode->root->log_root;
struct extent_buffer *src;
const int nritems = btrfs_header_nritems(path->nodes[0]);
const u64 ino = btrfs_ino(inode);
bool last_found = false;
int batch_start = 0;
int batch_size = 0;
int ret;
ret = clone_leaf(path, ctx);
if (ret < 0)
return ret;
src = path->nodes[0];
for (int i = path->slots[0]; i < nritems; i++) {
struct btrfs_dir_item *di;
struct btrfs_key key;
btrfs_item_key_to_cpu(src, &key, i);
if (key.objectid != ino || key.type != BTRFS_DIR_INDEX_KEY) {
last_found = true;
break;
}
di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
if (btrfs_dir_transid(src, di) < trans->transid) {
if (key.offset > *last_old_dentry_offset + 1) {
ret = insert_dir_log_key(trans, log, dst_path,
ino, *last_old_dentry_offset + 1,
key.offset - 1);
if (ret < 0)
return ret;
}
*last_old_dentry_offset = key.offset;
continue;
}
if (key.offset <= inode->last_dir_index_offset)
continue;
if (!ctx->log_new_dentries) {
struct btrfs_key di_key;
btrfs_dir_item_key_to_cpu(src, di, &di_key);
if (di_key.type != BTRFS_ROOT_ITEM_KEY)
ctx->log_new_dentries = true;
}
if (batch_size == 0)
batch_start = i;
batch_size++;
}
if (batch_size > 0) {
ret = flush_dir_items_batch(trans, inode, src, dst_path,
batch_start, batch_size);
if (ret < 0)
return ret;
}
return last_found ? 1 : 0;
}
static noinline int log_dir_items(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path,
struct btrfs_path *dst_path,
struct btrfs_log_ctx *ctx,
u64 min_offset, u64 *last_offset_ret)
{
struct btrfs_key min_key;
struct btrfs_root *root = inode->root;
struct btrfs_root *log = root->log_root;
int ret;
u64 last_old_dentry_offset = min_offset - 1;
u64 last_offset = (u64)-1;
u64 ino = btrfs_ino(inode);
min_key.objectid = ino;
min_key.type = BTRFS_DIR_INDEX_KEY;
min_key.offset = min_offset;
ret = btrfs_search_forward(root, &min_key, path, trans->transid);
if (ret != 0 || min_key.objectid != ino ||
min_key.type != BTRFS_DIR_INDEX_KEY) {
min_key.objectid = ino;
min_key.type = BTRFS_DIR_INDEX_KEY;
min_key.offset = (u64)-1;
btrfs_release_path(path);
ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
if (ret < 0) {
btrfs_release_path(path);
return ret;
}
ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY);
if (ret == 0) {
struct btrfs_key tmp;
btrfs_item_key_to_cpu(path->nodes[0], &tmp,
path->slots[0]);
if (tmp.type == BTRFS_DIR_INDEX_KEY)
last_old_dentry_offset = tmp.offset;
} else if (ret > 0) {
ret = 0;
}
goto done;
}
ret = btrfs_previous_item(root, path, ino, BTRFS_DIR_INDEX_KEY);
if (ret == 0) {
struct btrfs_key tmp;
btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
if (tmp.type == BTRFS_DIR_INDEX_KEY)
last_old_dentry_offset = tmp.offset;
} else if (ret < 0) {
goto done;
}
btrfs_release_path(path);
search:
ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
if (ret > 0) {
ret = btrfs_next_item(root, path);
if (ret > 0) {
ret = 0;
goto done;
}
}
if (ret < 0)
goto done;
while (1) {
ret = process_dir_items_leaf(trans, inode, path, dst_path, ctx,
&last_old_dentry_offset);
if (ret != 0) {
if (ret > 0)
ret = 0;
goto done;
}
path->slots[0] = btrfs_header_nritems(path->nodes[0]);
ret = btrfs_next_leaf(root, path);
if (ret) {
if (ret == 1) {
last_offset = (u64)-1;
ret = 0;
}
goto done;
}
btrfs_item_key_to_cpu(path->nodes[0], &min_key, path->slots[0]);
if (min_key.objectid != ino || min_key.type != BTRFS_DIR_INDEX_KEY) {
last_offset = (u64)-1;
goto done;
}
if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
last_offset = min_key.offset - 1;
goto done;
}
if (need_resched()) {
btrfs_release_path(path);
cond_resched();
goto search;
}
}
done:
btrfs_release_path(path);
btrfs_release_path(dst_path);
if (ret == 0) {
*last_offset_ret = last_offset;
ASSERT(last_old_dentry_offset <= last_offset,
"last_old_dentry_offset=%llu last_offset=%llu",
last_old_dentry_offset, last_offset);
if (last_old_dentry_offset < last_offset)
ret = insert_dir_log_key(trans, log, path, ino,
last_old_dentry_offset + 1,
last_offset);
}
return ret;
}
static int update_last_dir_index_offset(struct btrfs_inode *inode,
struct btrfs_path *path,
const struct btrfs_log_ctx *ctx)
{
const u64 ino = btrfs_ino(inode);
struct btrfs_key key;
int ret;
lockdep_assert_held(&inode->log_mutex);
if (inode->last_dir_index_offset != 0)
return 0;
if (!ctx->logged_before) {
inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1;
return 0;
}
key.objectid = ino;
key.type = BTRFS_DIR_INDEX_KEY;
key.offset = (u64)-1;
ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0);
if (ret <= 0)
goto out;
ret = 0;
inode->last_dir_index_offset = BTRFS_DIR_START_INDEX - 1;
if (path->slots[0] == 0)
goto out;
btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
if (key.objectid == ino && key.type == BTRFS_DIR_INDEX_KEY)
inode->last_dir_index_offset = key.offset;
out:
btrfs_release_path(path);
return ret;
}
static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path,
struct btrfs_path *dst_path,
struct btrfs_log_ctx *ctx)
{
u64 min_key;
u64 max_key;
int ret;
ret = update_last_dir_index_offset(inode, path, ctx);
if (ret)
return ret;
min_key = BTRFS_DIR_START_INDEX;
max_key = 0;
while (1) {
ret = log_dir_items(trans, inode, path, dst_path,
ctx, min_key, &max_key);
if (ret)
return ret;
if (max_key == (u64)-1)
break;
min_key = max_key + 1;
}
return 0;
}
static int drop_inode_items(struct btrfs_trans_handle *trans,
struct btrfs_root *log,
struct btrfs_path *path,
struct btrfs_inode *inode,
int max_key_type)
{
int ret;
struct btrfs_key key;
struct btrfs_key found_key;
int start_slot;
key.objectid = btrfs_ino(inode);
key.type = max_key_type;
key.offset = (u64)-1;
while (1) {
ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
if (ret < 0) {
break;
} else if (ret > 0) {
if (path->slots[0] == 0)
break;
path->slots[0]--;
}
btrfs_item_key_to_cpu(path->nodes[0], &found_key,
path->slots[0]);
if (found_key.objectid != key.objectid)
break;
found_key.offset = 0;
found_key.type = 0;
ret = btrfs_bin_search(path->nodes[0], 0, &found_key, &start_slot);
if (ret < 0)
break;
ret = btrfs_del_items(trans, log, path, start_slot,
path->slots[0] - start_slot + 1);
if (ret || start_slot != 0)
break;
btrfs_release_path(path);
}
btrfs_release_path(path);
if (ret > 0)
ret = 0;
return ret;
}
static int truncate_inode_items(struct btrfs_trans_handle *trans,
struct btrfs_root *log_root,
struct btrfs_inode *inode,
u64 new_size, u32 min_type)
{
struct btrfs_truncate_control control = {
.new_size = new_size,
.ino = btrfs_ino(inode),
.min_type = min_type,
.skip_ref_updates = true,
};
return btrfs_truncate_inode_items(trans, log_root, &control);
}
static void fill_inode_item(struct btrfs_trans_handle *trans,
struct extent_buffer *leaf,
struct btrfs_inode_item *item,
struct btrfs_inode *inode, bool log_inode_only,
u64 logged_isize)
{
struct inode *vfs_inode = &inode->vfs_inode;
u64 gen = inode->generation;
u64 flags;
if (log_inode_only) {
ASSERT(data_race(inode->logged_trans) > 0);
if (data_race(inode->logged_trans) < trans->transid)
gen = 0;
btrfs_set_inode_size(leaf, item, logged_isize);
} else {
btrfs_set_inode_size(leaf, item, vfs_inode->i_size);
}
btrfs_set_inode_generation(leaf, item, gen);
btrfs_set_inode_uid(leaf, item, i_uid_read(vfs_inode));
btrfs_set_inode_gid(leaf, item, i_gid_read(vfs_inode));
btrfs_set_inode_mode(leaf, item, vfs_inode->i_mode);
btrfs_set_inode_nlink(leaf, item, vfs_inode->i_nlink);
btrfs_set_timespec_sec(leaf, &item->atime, inode_get_atime_sec(vfs_inode));
btrfs_set_timespec_nsec(leaf, &item->atime, inode_get_atime_nsec(vfs_inode));
btrfs_set_timespec_sec(leaf, &item->mtime, inode_get_mtime_sec(vfs_inode));
btrfs_set_timespec_nsec(leaf, &item->mtime, inode_get_mtime_nsec(vfs_inode));
btrfs_set_timespec_sec(leaf, &item->ctime, inode_get_ctime_sec(vfs_inode));
btrfs_set_timespec_nsec(leaf, &item->ctime, inode_get_ctime_nsec(vfs_inode));
btrfs_set_timespec_sec(leaf, &item->otime, inode->i_otime_sec);
btrfs_set_timespec_nsec(leaf, &item->otime, inode->i_otime_nsec);
btrfs_set_inode_sequence(leaf, item, inode_peek_iversion(vfs_inode));
btrfs_set_inode_transid(leaf, item, trans->transid);
btrfs_set_inode_rdev(leaf, item, vfs_inode->i_rdev);
flags = btrfs_inode_combine_flags(inode->flags, inode->ro_flags);
btrfs_set_inode_flags(leaf, item, flags);
btrfs_set_inode_block_group(leaf, item, 0);
}
static int log_inode_item(struct btrfs_trans_handle *trans,
struct btrfs_root *log, struct btrfs_path *path,
struct btrfs_inode *inode, bool inode_item_dropped)
{
struct btrfs_inode_item *inode_item;
struct btrfs_key key;
int ret;
btrfs_get_inode_key(inode, &key);
if (!inode_item_dropped && inode->logged_trans == trans->transid) {
ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
ASSERT(ret <= 0);
if (ret > 0)
ret = -ENOENT;
} else {
ret = btrfs_insert_empty_item(trans, log, path, &key,
sizeof(*inode_item));
ASSERT(ret != -EEXIST);
}
if (ret)
return ret;
inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_inode_item);
fill_inode_item(trans, path->nodes[0], inode_item, inode, false, 0);
btrfs_release_path(path);
return 0;
}
static int log_csums(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_root *log_root,
struct btrfs_ordered_sum *sums)
{
const u64 lock_end = sums->logical + sums->len - 1;
struct extent_state *cached_state = NULL;
int ret;
if (inode->last_reflink_trans < trans->transid)
return btrfs_insert_data_csums(trans, log_root, sums);
ret = btrfs_lock_extent(&log_root->log_csum_range, sums->logical, lock_end,
&cached_state);
if (ret)
return ret;
ret = btrfs_del_csums(trans, log_root, sums->logical, sums->len);
if (!ret)
ret = btrfs_insert_data_csums(trans, log_root, sums);
btrfs_unlock_extent(&log_root->log_csum_range, sums->logical, lock_end,
&cached_state);
return ret;
}
static noinline int copy_items(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *dst_path,
struct btrfs_path *src_path,
int start_slot, int nr, enum btrfs_log_mode log_mode,
u64 logged_isize, struct btrfs_log_ctx *ctx)
{
struct btrfs_root *log = inode->root->log_root;
struct btrfs_file_extent_item *extent;
struct extent_buffer *src;
int ret;
struct btrfs_key *ins_keys;
u32 *ins_sizes;
struct btrfs_item_batch batch;
char AUTO_KFREE(ins_data);
int dst_index;
const bool skip_csum = (inode->flags & BTRFS_INODE_NODATASUM);
const u64 i_size = i_size_read(&inode->vfs_inode);
ret = clone_leaf(src_path, ctx);
if (ret < 0)
return ret;
src = src_path->nodes[0];
ins_data = kmalloc_array(nr, sizeof(struct btrfs_key) + sizeof(u32), GFP_NOFS);
if (!ins_data)
return -ENOMEM;
ins_sizes = (u32 *)ins_data;
ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
batch.keys = ins_keys;
batch.data_sizes = ins_sizes;
batch.total_data_size = 0;
batch.nr = 0;
dst_index = 0;
for (int i = 0; i < nr; i++) {
const int src_slot = start_slot + i;
struct btrfs_root *csum_root;
struct btrfs_ordered_sum *sums;
struct btrfs_ordered_sum *sums_next;
LIST_HEAD(ordered_sums);
u64 disk_bytenr;
u64 disk_num_bytes;
u64 extent_offset;
u64 extent_num_bytes;
bool is_old_extent;
btrfs_item_key_to_cpu(src, &ins_keys[dst_index], src_slot);
if (ins_keys[dst_index].type != BTRFS_EXTENT_DATA_KEY)
goto add_to_batch;
extent = btrfs_item_ptr(src, src_slot,
struct btrfs_file_extent_item);
is_old_extent = (btrfs_file_extent_generation(src, extent) <
trans->transid);
if (is_old_extent &&
ins_keys[dst_index].offset < i_size &&
inode->last_reflink_trans < trans->transid)
continue;
if (skip_csum)
goto add_to_batch;
if (btrfs_file_extent_type(src, extent) != BTRFS_FILE_EXTENT_REG)
goto add_to_batch;
if (is_old_extent)
goto add_to_batch;
disk_bytenr = btrfs_file_extent_disk_bytenr(src, extent);
if (disk_bytenr == 0)
goto add_to_batch;
disk_num_bytes = btrfs_file_extent_disk_num_bytes(src, extent);
if (btrfs_file_extent_compression(src, extent)) {
extent_offset = 0;
extent_num_bytes = disk_num_bytes;
} else {
extent_offset = btrfs_file_extent_offset(src, extent);
extent_num_bytes = btrfs_file_extent_num_bytes(src, extent);
}
csum_root = btrfs_csum_root(trans->fs_info, disk_bytenr);
if (unlikely(!csum_root)) {
btrfs_err(trans->fs_info,
"missing csum root for extent at bytenr %llu",
disk_bytenr);
return -EUCLEAN;
}
disk_bytenr += extent_offset;
ret = btrfs_lookup_csums_list(csum_root, disk_bytenr,
disk_bytenr + extent_num_bytes - 1,
&ordered_sums, false);
if (ret < 0)
return ret;
ret = 0;
list_for_each_entry_safe(sums, sums_next, &ordered_sums, list) {
if (!ret)
ret = log_csums(trans, inode, log, sums);
list_del(&sums->list);
kfree(sums);
}
if (ret)
return ret;
add_to_batch:
ins_sizes[dst_index] = btrfs_item_size(src, src_slot);
batch.total_data_size += ins_sizes[dst_index];
batch.nr++;
dst_index++;
}
if (batch.nr == 0)
return 0;
ret = btrfs_insert_empty_items(trans, log, dst_path, &batch);
if (ret)
return ret;
dst_index = 0;
for (int i = 0; i < nr; i++) {
const int src_slot = start_slot + i;
const int dst_slot = dst_path->slots[0] + dst_index;
struct btrfs_key key;
unsigned long src_offset;
unsigned long dst_offset;
if (dst_index >= batch.nr)
break;
btrfs_item_key_to_cpu(src, &key, src_slot);
if (key.type != BTRFS_EXTENT_DATA_KEY)
goto copy_item;
extent = btrfs_item_ptr(src, src_slot,
struct btrfs_file_extent_item);
if (btrfs_file_extent_generation(src, extent) < trans->transid &&
key.offset < i_size &&
inode->last_reflink_trans < trans->transid)
continue;
copy_item:
dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0], dst_slot);
src_offset = btrfs_item_ptr_offset(src, src_slot);
if (key.type == BTRFS_INODE_ITEM_KEY) {
struct btrfs_inode_item *inode_item;
inode_item = btrfs_item_ptr(dst_path->nodes[0], dst_slot,
struct btrfs_inode_item);
fill_inode_item(trans, dst_path->nodes[0], inode_item,
inode, log_mode == LOG_INODE_EXISTS,
logged_isize);
} else {
copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
src_offset, ins_sizes[dst_index]);
}
dst_index++;
}
btrfs_release_path(dst_path);
return ret;
}
static int extent_cmp(void *priv, const struct list_head *a,
const struct list_head *b)
{
const struct extent_map *em1, *em2;
em1 = list_entry(a, struct extent_map, list);
em2 = list_entry(b, struct extent_map, list);
if (em1->start < em2->start)
return -1;
else if (em1->start > em2->start)
return 1;
return 0;
}
static int log_extent_csums(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_root *log_root,
const struct extent_map *em,
struct btrfs_log_ctx *ctx)
{
struct btrfs_ordered_extent *ordered;
struct btrfs_root *csum_root;
u64 block_start;
u64 csum_offset;
u64 csum_len;
u64 mod_start = em->start;
u64 mod_len = em->len;
LIST_HEAD(ordered_sums);
int ret = 0;
if (inode->flags & BTRFS_INODE_NODATASUM ||
(em->flags & EXTENT_FLAG_PREALLOC) ||
em->disk_bytenr == EXTENT_MAP_HOLE)
return 0;
list_for_each_entry(ordered, &ctx->ordered_extents, log_list) {
const u64 ordered_end = ordered->file_offset + ordered->num_bytes;
const u64 mod_end = mod_start + mod_len;
struct btrfs_ordered_sum *sums;
if (mod_len == 0)
break;
if (ordered_end <= mod_start)
continue;
if (mod_end <= ordered->file_offset)
break;
if (ordered->file_offset > mod_start) {
if (ordered_end >= mod_end)
mod_len = ordered->file_offset - mod_start;
} else {
if (ordered_end < mod_end) {
mod_len = mod_end - ordered_end;
mod_start = ordered_end;
} else {
mod_len = 0;
}
}
if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags))
continue;
list_for_each_entry(sums, &ordered->csum_list, list) {
ret = log_csums(trans, inode, log_root, sums);
if (ret)
return ret;
}
}
if (mod_len == 0)
return 0;
if (btrfs_extent_map_is_compressed(em)) {
csum_offset = 0;
csum_len = em->disk_num_bytes;
} else {
csum_offset = mod_start - em->start;
csum_len = mod_len;
}
block_start = btrfs_extent_map_block_start(em);
csum_root = btrfs_csum_root(trans->fs_info, block_start);
if (unlikely(!csum_root)) {
btrfs_err(trans->fs_info,
"missing csum root for extent at bytenr %llu",
block_start);
return -EUCLEAN;
}
ret = btrfs_lookup_csums_list(csum_root, block_start + csum_offset,
block_start + csum_offset + csum_len - 1,
&ordered_sums, false);
if (ret < 0)
return ret;
ret = 0;
while (!list_empty(&ordered_sums)) {
struct btrfs_ordered_sum *sums = list_first_entry(&ordered_sums,
struct btrfs_ordered_sum,
list);
if (!ret)
ret = log_csums(trans, inode, log_root, sums);
list_del(&sums->list);
kfree(sums);
}
return ret;
}
static int log_one_extent(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
const struct extent_map *em,
struct btrfs_path *path,
struct btrfs_log_ctx *ctx)
{
struct btrfs_drop_extents_args drop_args = { 0 };
struct btrfs_root *log = inode->root->log_root;
struct btrfs_file_extent_item fi = { 0 };
struct extent_buffer *leaf;
struct btrfs_key key;
enum btrfs_compression_type compress_type;
u64 extent_offset = em->offset;
u64 block_start = btrfs_extent_map_block_start(em);
u64 block_len;
int ret;
btrfs_set_stack_file_extent_generation(&fi, trans->transid);
if (em->flags & EXTENT_FLAG_PREALLOC)
btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_PREALLOC);
else
btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_REG);
block_len = em->disk_num_bytes;
compress_type = btrfs_extent_map_compression(em);
if (compress_type != BTRFS_COMPRESS_NONE) {
btrfs_set_stack_file_extent_disk_bytenr(&fi, block_start);
btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);
} else if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE) {
btrfs_set_stack_file_extent_disk_bytenr(&fi, block_start - extent_offset);
btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);
}
btrfs_set_stack_file_extent_offset(&fi, extent_offset);
btrfs_set_stack_file_extent_num_bytes(&fi, em->len);
btrfs_set_stack_file_extent_ram_bytes(&fi, em->ram_bytes);
btrfs_set_stack_file_extent_compression(&fi, compress_type);
ret = log_extent_csums(trans, inode, log, em, ctx);
if (ret)
return ret;
if (ctx->logged_before) {
drop_args.path = path;
drop_args.start = em->start;
drop_args.end = btrfs_extent_map_end(em);
drop_args.replace_extent = true;
drop_args.extent_item_size = sizeof(fi);
ret = btrfs_drop_extents(trans, log, inode, &drop_args);
if (ret)
return ret;
}
if (!drop_args.extent_inserted) {
key.objectid = btrfs_ino(inode);
key.type = BTRFS_EXTENT_DATA_KEY;
key.offset = em->start;
ret = btrfs_insert_empty_item(trans, log, path, &key,
sizeof(fi));
if (ret)
return ret;
}
leaf = path->nodes[0];
write_extent_buffer(leaf, &fi,
btrfs_item_ptr_offset(leaf, path->slots[0]),
sizeof(fi));
btrfs_release_path(path);
return ret;
}
static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path,
struct btrfs_log_ctx *ctx)
{
struct btrfs_root *root = inode->root;
struct btrfs_key key;
const u64 i_size = i_size_read(&inode->vfs_inode);
const u64 ino = btrfs_ino(inode);
BTRFS_PATH_AUTO_FREE(dst_path);
bool dropped_extents = false;
u64 truncate_offset = i_size;
struct extent_buffer *leaf;
int slot;
int ins_nr = 0;
int start_slot = 0;
int ret;
if (!(inode->flags & BTRFS_INODE_PREALLOC))
return 0;
key.objectid = ino;
key.type = BTRFS_EXTENT_DATA_KEY;
key.offset = i_size;
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0)
goto out;
ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
if (ret < 0)
goto out;
if (ret == 0) {
struct btrfs_file_extent_item *ei;
leaf = path->nodes[0];
slot = path->slots[0];
ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
if (btrfs_file_extent_type(leaf, ei) ==
BTRFS_FILE_EXTENT_PREALLOC) {
u64 extent_end;
btrfs_item_key_to_cpu(leaf, &key, slot);
extent_end = key.offset +
btrfs_file_extent_num_bytes(leaf, ei);
if (extent_end > i_size)
truncate_offset = extent_end;
}
} else {
ret = 0;
}
while (true) {
leaf = path->nodes[0];
slot = path->slots[0];
if (slot >= btrfs_header_nritems(leaf)) {
if (ins_nr > 0) {
ret = copy_items(trans, inode, dst_path, path,
start_slot, ins_nr, 1, 0, ctx);
if (ret < 0)
goto out;
ins_nr = 0;
}
ret = btrfs_next_leaf(root, path);
if (ret < 0)
goto out;
if (ret > 0) {
ret = 0;
break;
}
continue;
}
btrfs_item_key_to_cpu(leaf, &key, slot);
if (key.objectid > ino)
break;
if (WARN_ON_ONCE(key.objectid < ino) ||
key.type < BTRFS_EXTENT_DATA_KEY ||
key.offset < i_size) {
path->slots[0]++;
continue;
}
if (!dropped_extents || key.offset < truncate_offset) {
ret = truncate_inode_items(trans, root->log_root, inode,
min(key.offset, truncate_offset),
BTRFS_EXTENT_DATA_KEY);
if (ret)
goto out;
dropped_extents = true;
}
truncate_offset = btrfs_file_extent_end(path);
if (ins_nr == 0)
start_slot = slot;
ins_nr++;
path->slots[0]++;
if (!dst_path) {
dst_path = btrfs_alloc_path();
if (!dst_path) {
ret = -ENOMEM;
goto out;
}
}
}
if (ins_nr > 0)
ret = copy_items(trans, inode, dst_path, path,
start_slot, ins_nr, 1, 0, ctx);
out:
btrfs_release_path(path);
return ret;
}
static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path,
struct btrfs_log_ctx *ctx)
{
struct btrfs_ordered_extent *ordered;
struct btrfs_ordered_extent *tmp;
struct extent_map *em, *n;
LIST_HEAD(extents);
struct extent_map_tree *tree = &inode->extent_tree;
int ret = 0;
int num = 0;
write_lock(&tree->lock);
list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
list_del_init(&em->list);
if (++num > SZ_16K) {
list_del_init(&tree->modified_extents);
ret = -EFBIG;
goto process;
}
if (em->generation < trans->transid)
continue;
if ((em->flags & EXTENT_FLAG_PREALLOC) &&
em->start >= i_size_read(&inode->vfs_inode))
continue;
refcount_inc(&em->refs);
em->flags |= EXTENT_FLAG_LOGGING;
list_add_tail(&em->list, &extents);
}
list_sort(NULL, &extents, extent_cmp);
process:
while (!list_empty(&extents)) {
em = list_first_entry(&extents, struct extent_map, list);
list_del_init(&em->list);
if (ret) {
btrfs_clear_em_logging(inode, em);
btrfs_free_extent_map(em);
continue;
}
write_unlock(&tree->lock);
ret = log_one_extent(trans, inode, em, path, ctx);
write_lock(&tree->lock);
btrfs_clear_em_logging(inode, em);
btrfs_free_extent_map(em);
}
WARN_ON(!list_empty(&extents));
write_unlock(&tree->lock);
if (!ret)
ret = btrfs_log_prealloc_extents(trans, inode, path, ctx);
if (ret)
return ret;
list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
list_del_init(&ordered->log_list);
set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
spin_lock(&inode->ordered_tree_lock);
if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
atomic_inc(&trans->transaction->pending_ordered);
}
spin_unlock(&inode->ordered_tree_lock);
}
btrfs_put_ordered_extent(ordered);
}
return 0;
}
static int get_inode_size_to_log(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path, u64 *size_ret)
{
struct btrfs_key key;
struct btrfs_inode_item *item;
int ret;
key.objectid = btrfs_ino(inode);
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
if (data_race(inode->logged_trans) == trans->transid) {
ret = btrfs_search_slot(NULL, inode->root->log_root, &key, path, 0, 0);
} else if (inode->generation < trans->transid) {
path->search_commit_root = true;
path->skip_locking = true;
ret = btrfs_search_slot(NULL, inode->root, &key, path, 0, 0);
path->search_commit_root = false;
path->skip_locking = false;
} else {
*size_ret = 0;
return 0;
}
ASSERT(ret <= 0);
if (WARN_ON_ONCE(ret > 0))
ret = -ENOENT;
if (ret < 0)
return ret;
item = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_inode_item);
*size_ret = btrfs_inode_size(path->nodes[0], item);
if (*size_ret > inode->vfs_inode.i_size)
*size_ret = inode->vfs_inode.i_size;
btrfs_release_path(path);
return 0;
}
static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path,
struct btrfs_path *dst_path,
struct btrfs_log_ctx *ctx)
{
struct btrfs_root *root = inode->root;
int ret;
struct btrfs_key key;
const u64 ino = btrfs_ino(inode);
int ins_nr = 0;
int start_slot = 0;
bool found_xattrs = false;
if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags))
return 0;
key.objectid = ino;
key.type = BTRFS_XATTR_ITEM_KEY;
key.offset = 0;
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0)
return ret;
while (true) {
int slot = path->slots[0];
struct extent_buffer *leaf = path->nodes[0];
int nritems = btrfs_header_nritems(leaf);
if (slot >= nritems) {
if (ins_nr > 0) {
ret = copy_items(trans, inode, dst_path, path,
start_slot, ins_nr, 1, 0, ctx);
if (ret < 0)
return ret;
ins_nr = 0;
}
ret = btrfs_next_leaf(root, path);
if (ret < 0)
return ret;
else if (ret > 0)
break;
continue;
}
btrfs_item_key_to_cpu(leaf, &key, slot);
if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
break;
if (ins_nr == 0)
start_slot = slot;
ins_nr++;
path->slots[0]++;
found_xattrs = true;
cond_resched();
}
if (ins_nr > 0) {
ret = copy_items(trans, inode, dst_path, path,
start_slot, ins_nr, 1, 0, ctx);
if (ret < 0)
return ret;
}
if (!found_xattrs)
set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags);
return 0;
}
static int btrfs_log_holes(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path)
{
struct btrfs_root *root = inode->root;
struct btrfs_fs_info *fs_info = root->fs_info;
struct btrfs_key key;
const u64 ino = btrfs_ino(inode);
const u64 i_size = i_size_read(&inode->vfs_inode);
u64 prev_extent_end = 0;
int ret;
if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
return 0;
if (!(inode->flags & BTRFS_INODE_PREALLOC) &&
i_size <= inode_get_bytes(&inode->vfs_inode))
return 0;
key.objectid = ino;
key.type = BTRFS_EXTENT_DATA_KEY;
key.offset = 0;
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0)
return ret;
while (true) {
struct extent_buffer *leaf = path->nodes[0];
if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
ret = btrfs_next_leaf(root, path);
if (ret < 0)
return ret;
if (ret > 0) {
ret = 0;
break;
}
leaf = path->nodes[0];
}
btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
break;
if (prev_extent_end < key.offset) {
const u64 hole_len = key.offset - prev_extent_end;
btrfs_release_path(path);
ret = btrfs_insert_hole_extent(trans, root->log_root,
ino, prev_extent_end,
hole_len);
if (ret < 0)
return ret;
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0)
return ret;
if (WARN_ON(ret > 0))
return -ENOENT;
leaf = path->nodes[0];
}
prev_extent_end = btrfs_file_extent_end(path);
path->slots[0]++;
cond_resched();
}
if (prev_extent_end < i_size) {
u64 hole_len;
btrfs_release_path(path);
hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
ret = btrfs_insert_hole_extent(trans, root->log_root, ino,
prev_extent_end, hole_len);
if (ret < 0)
return ret;
}
return 0;
}
static int btrfs_check_ref_name_override(struct extent_buffer *eb,
const int slot,
const struct btrfs_key *key,
struct btrfs_inode *inode,
u64 *other_ino, u64 *other_parent)
{
BTRFS_PATH_AUTO_FREE(search_path);
char AUTO_KFREE(name);
u32 name_len = 0;
u32 item_size = btrfs_item_size(eb, slot);
u32 cur_offset = 0;
unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
search_path = btrfs_alloc_path();
if (!search_path)
return -ENOMEM;
search_path->search_commit_root = true;
search_path->skip_locking = true;
while (cur_offset < item_size) {
u64 parent;
u32 this_name_len;
u32 this_len;
unsigned long name_ptr;
struct btrfs_dir_item *di;
struct fscrypt_str name_str;
if (key->type == BTRFS_INODE_REF_KEY) {
struct btrfs_inode_ref *iref;
iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
parent = key->offset;
this_name_len = btrfs_inode_ref_name_len(eb, iref);
name_ptr = (unsigned long)(iref + 1);
this_len = sizeof(*iref) + this_name_len;
} else {
struct btrfs_inode_extref *extref;
extref = (struct btrfs_inode_extref *)(ptr +
cur_offset);
parent = btrfs_inode_extref_parent(eb, extref);
this_name_len = btrfs_inode_extref_name_len(eb, extref);
name_ptr = (unsigned long)&extref->name;
this_len = sizeof(*extref) + this_name_len;
}
if (this_name_len > name_len) {
char *new_name;
new_name = krealloc(name, this_name_len, GFP_NOFS);
if (!new_name)
return -ENOMEM;
name_len = this_name_len;
name = new_name;
}
read_extent_buffer(eb, name, name_ptr, this_name_len);
name_str.name = name;
name_str.len = this_name_len;
di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
parent, &name_str, 0);
if (!IS_ERR_OR_NULL(di)) {
struct btrfs_key di_key;
btrfs_dir_item_key_to_cpu(search_path->nodes[0],
di, &di_key);
if (di_key.type == BTRFS_INODE_ITEM_KEY) {
if (di_key.objectid != key->objectid) {
*other_ino = di_key.objectid;
*other_parent = parent;
return 1;
} else {
return 0;
}
} else {
return -EAGAIN;
}
} else if (IS_ERR(di)) {
return PTR_ERR(di);
}
btrfs_release_path(search_path);
cur_offset += this_len;
}
return 0;
}
static bool need_log_inode(const struct btrfs_trans_handle *trans,
struct btrfs_inode *inode)
{
if (S_ISDIR(inode->vfs_inode.i_mode) && inode->last_trans < trans->transid)
return false;
if (inode_logged(trans, inode, NULL) == 1 &&
!test_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags))
return false;
return true;
}
struct btrfs_dir_list {
u64 ino;
struct list_head list;
};
static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
struct btrfs_inode *start_inode,
struct btrfs_log_ctx *ctx)
{
struct btrfs_root *root = start_inode->root;
struct btrfs_path *path;
LIST_HEAD(dir_list);
struct btrfs_dir_list *dir_elem;
u64 ino = btrfs_ino(start_inode);
struct btrfs_inode *curr_inode = start_inode;
int ret = 0;
trace_btrfs_log_new_dir_dentries_enter(trans, start_inode);
path = btrfs_alloc_path();
if (!path) {
ret = -ENOMEM;
goto out;
}
ihold(&curr_inode->vfs_inode);
while (true) {
struct btrfs_key key;
struct btrfs_key found_key;
u64 next_index;
bool continue_curr_inode = true;
int iter_ret;
key.objectid = ino;
key.type = BTRFS_DIR_INDEX_KEY;
key.offset = btrfs_get_first_dir_index_to_log(curr_inode);
next_index = key.offset;
again:
btrfs_for_each_slot(root->log_root, &key, &found_key, path, iter_ret) {
struct extent_buffer *leaf = path->nodes[0];
struct btrfs_dir_item *di;
struct btrfs_key di_key;
struct btrfs_inode *di_inode;
int log_mode = LOG_INODE_EXISTS;
int type;
if (found_key.objectid != ino ||
found_key.type != BTRFS_DIR_INDEX_KEY) {
continue_curr_inode = false;
break;
}
next_index = found_key.offset + 1;
di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
type = btrfs_dir_ftype(leaf, di);
if (btrfs_dir_transid(leaf, di) < trans->transid)
continue;
btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
if (di_key.type == BTRFS_ROOT_ITEM_KEY)
continue;
btrfs_release_path(path);
di_inode = btrfs_iget_logging(di_key.objectid, root);
if (IS_ERR(di_inode)) {
ret = PTR_ERR(di_inode);
goto out;
}
if (!need_log_inode(trans, di_inode)) {
btrfs_add_delayed_iput(di_inode);
break;
}
ctx->log_new_dentries = false;
if (type == BTRFS_FT_DIR)
log_mode = LOG_INODE_ALL;
ret = btrfs_log_inode(trans, di_inode, log_mode, ctx);
btrfs_add_delayed_iput(di_inode);
if (ret)
goto out;
if (ctx->log_new_dentries) {
dir_elem = kmalloc_obj(*dir_elem, GFP_NOFS);
if (!dir_elem) {
ret = -ENOMEM;
goto out;
}
dir_elem->ino = di_key.objectid;
list_add_tail(&dir_elem->list, &dir_list);
}
break;
}
btrfs_release_path(path);
if (iter_ret < 0) {
ret = iter_ret;
goto out;
} else if (iter_ret > 0) {
continue_curr_inode = false;
} else {
key = found_key;
}
if (continue_curr_inode && key.offset < (u64)-1) {
key.offset++;
goto again;
}
btrfs_set_first_dir_index_to_log(curr_inode, next_index);
if (list_empty(&dir_list))
break;
dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, list);
ino = dir_elem->ino;
list_del(&dir_elem->list);
kfree(dir_elem);
btrfs_add_delayed_iput(curr_inode);
curr_inode = btrfs_iget_logging(ino, root);
if (IS_ERR(curr_inode)) {
ret = PTR_ERR(curr_inode);
curr_inode = NULL;
break;
}
}
out:
btrfs_free_path(path);
if (curr_inode)
btrfs_add_delayed_iput(curr_inode);
if (ret) {
struct btrfs_dir_list *next;
list_for_each_entry_safe(dir_elem, next, &dir_list, list)
kfree(dir_elem);
}
trace_btrfs_log_new_dir_dentries_exit(trans, start_inode, ret);
return ret;
}
struct btrfs_ino_list {
u64 ino;
u64 parent;
struct list_head list;
};
static void free_conflicting_inodes(struct btrfs_log_ctx *ctx)
{
struct btrfs_ino_list *curr;
struct btrfs_ino_list *next;
list_for_each_entry_safe(curr, next, &ctx->conflict_inodes, list) {
list_del(&curr->list);
kfree(curr);
}
}
static int conflicting_inode_is_dir(struct btrfs_root *root, u64 ino,
struct btrfs_path *path)
{
struct btrfs_key key;
int ret;
key.objectid = ino;
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
path->search_commit_root = true;
path->skip_locking = true;
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (WARN_ON_ONCE(ret > 0)) {
ret = -ENOENT;
} else if (ret == 0) {
struct btrfs_inode_item *item;
item = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_inode_item);
if (S_ISDIR(btrfs_inode_mode(path->nodes[0], item)))
ret = 1;
}
btrfs_release_path(path);
path->search_commit_root = false;
path->skip_locking = false;
return ret;
}
static bool can_log_conflicting_inode(const struct btrfs_trans_handle *trans,
const struct btrfs_inode *inode)
{
if (!S_ISDIR(inode->vfs_inode.i_mode))
return true;
if (inode->last_unlink_trans < trans->transid)
return true;
return false;
}
static int add_conflicting_inode(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_path *path,
u64 ino, u64 parent,
struct btrfs_log_ctx *ctx)
{
struct btrfs_ino_list *ino_elem;
struct btrfs_inode *inode;
int ret = 0;
trace_btrfs_add_conflicting_inode_enter(trans, ctx, ino, parent);
if (ctx->num_conflict_inodes >= MAX_CONFLICT_INODES) {
ret = BTRFS_LOG_FORCE_COMMIT;
goto out;
}
inode = btrfs_iget_logging(ino, root);
if (IS_ERR(inode)) {
ret = PTR_ERR(inode);
if (ret != -ENOENT)
goto out;
ret = conflicting_inode_is_dir(root, ino, path);
if (ret <= 0)
goto out;
ino_elem = kmalloc_obj(*ino_elem, GFP_NOFS);
if (!ino_elem) {
ret = -ENOMEM;
goto out;
}
ino_elem->ino = ino;
ino_elem->parent = parent;
list_add_tail(&ino_elem->list, &ctx->conflict_inodes);
ctx->num_conflict_inodes++;
ret = 0;
goto out;
}
if (!need_log_inode(trans, inode)) {
btrfs_add_delayed_iput(inode);
goto out;
}
if (!can_log_conflicting_inode(trans, inode)) {
btrfs_add_delayed_iput(inode);
ret = BTRFS_LOG_FORCE_COMMIT;
goto out;
}
btrfs_add_delayed_iput(inode);
ino_elem = kmalloc_obj(*ino_elem, GFP_NOFS);
if (!ino_elem) {
ret = -ENOMEM;
goto out;
}
ino_elem->ino = ino;
ino_elem->parent = parent;
list_add_tail(&ino_elem->list, &ctx->conflict_inodes);
ctx->num_conflict_inodes++;
out:
trace_btrfs_add_conflicting_inode_exit(trans, ctx, ino, parent, ret);
return ret;
}
static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_log_ctx *ctx)
{
const bool orig_log_new_dentries = ctx->log_new_dentries;
int ret = 0;
if (ctx->logging_conflict_inodes)
return 0;
if (list_empty(&ctx->conflict_inodes))
return 0;
ctx->logging_conflict_inodes = true;
trace_btrfs_log_conflicting_inodes_enter(trans, ctx);
while (!list_empty(&ctx->conflict_inodes)) {
struct btrfs_ino_list *curr;
struct btrfs_inode *inode;
u64 ino;
u64 parent;
curr = list_first_entry(&ctx->conflict_inodes,
struct btrfs_ino_list, list);
ino = curr->ino;
parent = curr->parent;
list_del(&curr->list);
kfree(curr);
inode = btrfs_iget_logging(ino, root);
if (IS_ERR(inode)) {
ret = PTR_ERR(inode);
if (ret != -ENOENT)
break;
inode = btrfs_iget_logging(parent, root);
if (IS_ERR(inode)) {
ret = PTR_ERR(inode);
break;
}
if (!can_log_conflicting_inode(trans, inode)) {
btrfs_add_delayed_iput(inode);
ret = BTRFS_LOG_FORCE_COMMIT;
break;
}
ctx->log_new_dentries = false;
ret = btrfs_log_inode(trans, inode, LOG_INODE_ALL, ctx);
if (!ret && ctx->log_new_dentries)
ret = log_new_dir_dentries(trans, inode, ctx);
btrfs_add_delayed_iput(inode);
if (ret)
break;
continue;
}
if (!need_log_inode(trans, inode)) {
btrfs_add_delayed_iput(inode);
continue;
}
ret = btrfs_log_inode(trans, inode, LOG_INODE_EXISTS, ctx);
btrfs_add_delayed_iput(inode);
if (ret)
break;
}
ctx->log_new_dentries = orig_log_new_dentries;
ctx->logging_conflict_inodes = false;
if (ret)
free_conflicting_inodes(ctx);
trace_btrfs_log_conflicting_inodes_exit(trans, ctx, ret);
return ret;
}
static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_key *min_key,
const struct btrfs_key *max_key,
struct btrfs_path *path,
struct btrfs_path *dst_path,
const u64 logged_isize,
const enum btrfs_log_mode log_mode,
struct btrfs_log_ctx *ctx,
bool *need_log_inode_item)
{
const u64 i_size = i_size_read(&inode->vfs_inode);
struct btrfs_root *root = inode->root;
int ins_start_slot = 0;
int ins_nr = 0;
int ret;
while (1) {
ret = btrfs_search_forward(root, min_key, path, trans->transid);
if (ret < 0)
return ret;
if (ret > 0) {
ret = 0;
break;
}
again:
if (min_key->objectid != max_key->objectid)
break;
if (min_key->type > max_key->type)
break;
if (min_key->type == BTRFS_INODE_ITEM_KEY) {
*need_log_inode_item = false;
} else if (min_key->type == BTRFS_EXTENT_DATA_KEY &&
min_key->offset >= i_size) {
break;
} else if (min_key->type == BTRFS_INODE_REF_KEY ||
min_key->type == BTRFS_INODE_EXTREF_KEY) {
u64 other_ino = 0;
u64 other_parent = 0;
ret = btrfs_check_ref_name_override(path->nodes[0],
path->slots[0], min_key, inode,
&other_ino, &other_parent);
if (ret < 0) {
return ret;
} else if (ret > 0 &&
other_ino != btrfs_ino(ctx->inode)) {
if (ins_nr > 0) {
ins_nr++;
} else {
ins_nr = 1;
ins_start_slot = path->slots[0];
}
ret = copy_items(trans, inode, dst_path, path,
ins_start_slot, ins_nr,
log_mode, logged_isize, ctx);
if (ret < 0)
return ret;
ins_nr = 0;
btrfs_release_path(path);
ret = add_conflicting_inode(trans, root, path,
other_ino,
other_parent, ctx);
if (ret)
return ret;
goto next_key;
}
} else if (min_key->type == BTRFS_XATTR_ITEM_KEY) {
if (ins_nr == 0)
goto next_slot;
ret = copy_items(trans, inode, dst_path, path,
ins_start_slot,
ins_nr, log_mode, logged_isize, ctx);
if (ret < 0)
return ret;
ins_nr = 0;
goto next_slot;
}
if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
ins_nr++;
goto next_slot;
} else if (!ins_nr) {
ins_start_slot = path->slots[0];
ins_nr = 1;
goto next_slot;
}
ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
ins_nr, log_mode, logged_isize, ctx);
if (ret < 0)
return ret;
ins_nr = 1;
ins_start_slot = path->slots[0];
next_slot:
path->slots[0]++;
if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
btrfs_item_key_to_cpu(path->nodes[0], min_key,
path->slots[0]);
goto again;
}
if (ins_nr) {
ret = copy_items(trans, inode, dst_path, path,
ins_start_slot, ins_nr, log_mode,
logged_isize, ctx);
if (ret < 0)
return ret;
ins_nr = 0;
}
btrfs_release_path(path);
next_key:
if (min_key->offset < (u64)-1) {
min_key->offset++;
} else if (min_key->type < max_key->type) {
min_key->type++;
min_key->offset = 0;
} else {
break;
}
cond_resched();
}
if (ins_nr) {
ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
ins_nr, log_mode, logged_isize, ctx);
if (ret)
return ret;
}
if (log_mode == LOG_INODE_ALL && S_ISREG(inode->vfs_inode.i_mode)) {
btrfs_release_path(path);
ret = btrfs_log_prealloc_extents(trans, inode, dst_path, ctx);
}
return ret;
}
static int insert_delayed_items_batch(struct btrfs_trans_handle *trans,
struct btrfs_root *log,
struct btrfs_path *path,
const struct btrfs_item_batch *batch,
const struct btrfs_delayed_item *first_item)
{
const struct btrfs_delayed_item *curr = first_item;
int ret;
ret = btrfs_insert_empty_items(trans, log, path, batch);
if (ret)
return ret;
for (int i = 0; i < batch->nr; i++) {
char *data_ptr;
data_ptr = btrfs_item_ptr(path->nodes[0], path->slots[0], char);
write_extent_buffer(path->nodes[0], &curr->data,
(unsigned long)data_ptr, curr->data_len);
curr = list_next_entry(curr, log_list);
path->slots[0]++;
}
btrfs_release_path(path);
return 0;
}
static int log_delayed_insertion_items(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path,
const struct list_head *delayed_ins_list,
struct btrfs_log_ctx *ctx)
{
const int max_batch_size = 195;
const int leaf_data_size = BTRFS_LEAF_DATA_SIZE(trans->fs_info);
const u64 ino = btrfs_ino(inode);
struct btrfs_root *log = inode->root->log_root;
struct btrfs_item_batch batch = {
.nr = 0,
.total_data_size = 0,
};
const struct btrfs_delayed_item *first = NULL;
const struct btrfs_delayed_item *curr;
char *ins_data;
struct btrfs_key *ins_keys;
u32 *ins_sizes;
u64 curr_batch_size = 0;
int batch_idx = 0;
int ret;
lockdep_assert_held(&inode->log_mutex);
list_for_each_entry(curr, delayed_ins_list, log_list) {
if (curr->index > inode->last_dir_index_offset) {
first = curr;
break;
}
}
if (!first)
return 0;
ins_data = kmalloc_array(max_batch_size, sizeof(u32) + sizeof(struct btrfs_key), GFP_NOFS);
if (!ins_data)
return -ENOMEM;
ins_sizes = (u32 *)ins_data;
batch.data_sizes = ins_sizes;
ins_keys = (struct btrfs_key *)(ins_data + max_batch_size * sizeof(u32));
batch.keys = ins_keys;
curr = first;
while (!list_entry_is_head(curr, delayed_ins_list, log_list)) {
const u32 curr_size = curr->data_len + sizeof(struct btrfs_item);
if (curr_batch_size + curr_size > leaf_data_size ||
batch.nr == max_batch_size) {
ret = insert_delayed_items_batch(trans, log, path,
&batch, first);
if (ret)
goto out;
batch_idx = 0;
batch.nr = 0;
batch.total_data_size = 0;
curr_batch_size = 0;
first = curr;
}
ins_sizes[batch_idx] = curr->data_len;
ins_keys[batch_idx].objectid = ino;
ins_keys[batch_idx].type = BTRFS_DIR_INDEX_KEY;
ins_keys[batch_idx].offset = curr->index;
curr_batch_size += curr_size;
batch.total_data_size += curr->data_len;
batch.nr++;
batch_idx++;
curr = list_next_entry(curr, log_list);
}
ASSERT(batch.nr >= 1, "batch.nr=%d", batch.nr);
ret = insert_delayed_items_batch(trans, log, path, &batch, first);
curr = list_last_entry(delayed_ins_list, struct btrfs_delayed_item,
log_list);
inode->last_dir_index_offset = curr->index;
out:
kfree(ins_data);
return ret;
}
static int log_delayed_deletions_full(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path,
const struct list_head *delayed_del_list,
struct btrfs_log_ctx *ctx)
{
const u64 ino = btrfs_ino(inode);
const struct btrfs_delayed_item *curr;
curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item,
log_list);
while (!list_entry_is_head(curr, delayed_del_list, log_list)) {
u64 first_dir_index = curr->index;
u64 last_dir_index;
const struct btrfs_delayed_item *next;
int ret;
next = list_next_entry(curr, log_list);
while (!list_entry_is_head(next, delayed_del_list, log_list)) {
if (next->index != curr->index + 1)
break;
curr = next;
next = list_next_entry(next, log_list);
}
last_dir_index = curr->index;
ASSERT(last_dir_index >= first_dir_index,
"last_dir_index=%llu first_dir_index=%llu",
last_dir_index, first_dir_index);
ret = insert_dir_log_key(trans, inode->root->log_root, path,
ino, first_dir_index, last_dir_index);
if (ret)
return ret;
curr = list_next_entry(curr, log_list);
}
return 0;
}
static int batch_delete_dir_index_items(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path,
const struct list_head *delayed_del_list,
const struct btrfs_delayed_item *first,
const struct btrfs_delayed_item **last_ret)
{
const struct btrfs_delayed_item *next;
struct extent_buffer *leaf = path->nodes[0];
const int last_slot = btrfs_header_nritems(leaf) - 1;
int slot = path->slots[0] + 1;
const u64 ino = btrfs_ino(inode);
next = list_next_entry(first, log_list);
while (slot < last_slot &&
!list_entry_is_head(next, delayed_del_list, log_list)) {
struct btrfs_key key;
btrfs_item_key_to_cpu(leaf, &key, slot);
if (key.objectid != ino ||
key.type != BTRFS_DIR_INDEX_KEY ||
key.offset != next->index)
break;
slot++;
*last_ret = next;
next = list_next_entry(next, log_list);
}
return btrfs_del_items(trans, inode->root->log_root, path,
path->slots[0], slot - path->slots[0]);
}
static int log_delayed_deletions_incremental(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path,
const struct list_head *delayed_del_list,
struct btrfs_log_ctx *ctx)
{
struct btrfs_root *log = inode->root->log_root;
const struct btrfs_delayed_item *curr;
u64 last_range_start = 0;
u64 last_range_end = 0;
struct btrfs_key key;
key.objectid = btrfs_ino(inode);
key.type = BTRFS_DIR_INDEX_KEY;
curr = list_first_entry(delayed_del_list, struct btrfs_delayed_item,
log_list);
while (!list_entry_is_head(curr, delayed_del_list, log_list)) {
const struct btrfs_delayed_item *last = curr;
u64 first_dir_index = curr->index;
u64 last_dir_index;
bool deleted_items = false;
int ret;
key.offset = curr->index;
ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
if (ret < 0) {
return ret;
} else if (ret == 0) {
ret = batch_delete_dir_index_items(trans, inode, path,
delayed_del_list, curr,
&last);
if (ret)
return ret;
deleted_items = true;
}
btrfs_release_path(path);
if (deleted_items)
goto next_batch;
last_dir_index = last->index;
ASSERT(last_dir_index >= first_dir_index,
"last_dir_index=%llu first_dir_index=%llu",
last_dir_index, first_dir_index);
if (last_range_end != 0 && first_dir_index == last_range_end + 1)
first_dir_index = last_range_start;
ret = insert_dir_log_key(trans, log, path, key.objectid,
first_dir_index, last_dir_index);
if (ret)
return ret;
last_range_start = first_dir_index;
last_range_end = last_dir_index;
next_batch:
curr = list_next_entry(last, log_list);
}
return 0;
}
static int log_delayed_deletion_items(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_path *path,
const struct list_head *delayed_del_list,
struct btrfs_log_ctx *ctx)
{
lockdep_assert_held(&inode->log_mutex);
if (list_empty(delayed_del_list))
return 0;
if (ctx->logged_before)
return log_delayed_deletions_incremental(trans, inode, path,
delayed_del_list, ctx);
return log_delayed_deletions_full(trans, inode, path, delayed_del_list,
ctx);
}
static int log_new_delayed_dentries(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
const struct list_head *delayed_ins_list,
struct btrfs_log_ctx *ctx)
{
const bool orig_log_new_dentries = ctx->log_new_dentries;
struct btrfs_delayed_item *item;
int ret = 0;
lockdep_assert_not_held(&inode->log_mutex);
ASSERT(!ctx->logging_new_delayed_dentries);
if (list_empty(delayed_ins_list))
return 0;
trace_btrfs_log_new_delayed_dentries_enter(trans, inode);
ctx->logging_new_delayed_dentries = true;
list_for_each_entry(item, delayed_ins_list, log_list) {
struct btrfs_dir_item *dir_item;
struct btrfs_inode *di_inode;
struct btrfs_key key;
int log_mode = LOG_INODE_EXISTS;
dir_item = (struct btrfs_dir_item *)item->data;
btrfs_disk_key_to_cpu(&key, &dir_item->location);
if (key.type == BTRFS_ROOT_ITEM_KEY)
continue;
di_inode = btrfs_iget_logging(key.objectid, inode->root);
if (IS_ERR(di_inode)) {
ret = PTR_ERR(di_inode);
break;
}
if (!need_log_inode(trans, di_inode)) {
btrfs_add_delayed_iput(di_inode);
continue;
}
if (btrfs_stack_dir_ftype(dir_item) == BTRFS_FT_DIR)
log_mode = LOG_INODE_ALL;
ctx->log_new_dentries = false;
ret = btrfs_log_inode(trans, di_inode, log_mode, ctx);
if (!ret && ctx->log_new_dentries)
ret = log_new_dir_dentries(trans, di_inode, ctx);
btrfs_add_delayed_iput(di_inode);
if (ret)
break;
}
ctx->log_new_dentries = orig_log_new_dentries;
ctx->logging_new_delayed_dentries = false;
trace_btrfs_log_new_delayed_dentries_exit(trans, inode, ret);
return ret;
}
static int btrfs_log_inode(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
enum btrfs_log_mode log_mode,
struct btrfs_log_ctx *ctx)
{
struct btrfs_path *path;
struct btrfs_path *dst_path = NULL;
struct btrfs_key min_key;
struct btrfs_key max_key;
struct btrfs_root *log = inode->root->log_root;
int ret;
bool fast_search = false;
u64 ino = btrfs_ino(inode);
struct extent_map_tree *em_tree = &inode->extent_tree;
u64 logged_isize = 0;
bool need_log_inode_item = true;
bool xattrs_logged = false;
bool inode_item_dropped = true;
bool full_dir_logging = false;
LIST_HEAD(delayed_ins_list);
LIST_HEAD(delayed_del_list);
trace_btrfs_log_inode_enter(trans, inode, ctx, log_mode);
path = btrfs_alloc_path();
if (!path) {
ret = -ENOMEM;
goto out;
}
dst_path = btrfs_alloc_path();
if (!dst_path) {
ret = -ENOMEM;
goto out;
}
min_key.objectid = ino;
min_key.type = BTRFS_INODE_ITEM_KEY;
min_key.offset = 0;
max_key.objectid = ino;
if (S_ISDIR(inode->vfs_inode.i_mode) ||
(!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
&inode->runtime_flags) &&
log_mode >= LOG_INODE_EXISTS))
max_key.type = BTRFS_XATTR_ITEM_KEY;
else
max_key.type = (u8)-1;
max_key.offset = (u64)-1;
if (S_ISDIR(inode->vfs_inode.i_mode) && log_mode == LOG_INODE_ALL)
full_dir_logging = true;
if (full_dir_logging && ctx->logging_new_delayed_dentries) {
ret = btrfs_commit_inode_delayed_items(trans, inode);
if (ret)
goto out;
}
mutex_lock(&inode->log_mutex);
if (S_ISLNK(inode->vfs_inode.i_mode))
log_mode = LOG_INODE_ALL;
ret = inode_logged(trans, inode, path);
if (ret < 0)
goto out_unlock;
ctx->logged_before = (ret == 1);
ret = 0;
if (full_dir_logging && inode->last_unlink_trans >= trans->transid) {
ret = BTRFS_LOG_FORCE_COMMIT;
goto out_unlock;
}
if (S_ISDIR(inode->vfs_inode.i_mode)) {
clear_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags);
if (ctx->logged_before)
ret = drop_inode_items(trans, log, path, inode,
BTRFS_XATTR_ITEM_KEY);
} else {
if (log_mode == LOG_INODE_EXISTS) {
ret = get_inode_size_to_log(trans, inode, path, &logged_isize);
if (ret)
goto out_unlock;
}
if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
&inode->runtime_flags)) {
if (log_mode == LOG_INODE_EXISTS) {
max_key.type = BTRFS_XATTR_ITEM_KEY;
if (ctx->logged_before)
ret = drop_inode_items(trans, log, path,
inode, max_key.type);
} else {
clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
&inode->runtime_flags);
clear_bit(BTRFS_INODE_COPY_EVERYTHING,
&inode->runtime_flags);
if (ctx->logged_before)
ret = truncate_inode_items(trans, log,
inode, 0, 0);
}
} else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
&inode->runtime_flags) ||
log_mode == LOG_INODE_EXISTS) {
if (log_mode == LOG_INODE_ALL)
fast_search = true;
max_key.type = BTRFS_XATTR_ITEM_KEY;
if (ctx->logged_before)
ret = drop_inode_items(trans, log, path, inode,
max_key.type);
} else {
if (log_mode == LOG_INODE_ALL)
fast_search = true;
inode_item_dropped = false;
goto log_extents;
}
}
if (ret)
goto out_unlock;
if (full_dir_logging && !ctx->logging_new_delayed_dentries)
btrfs_log_get_delayed_items(inode, &delayed_ins_list,
&delayed_del_list);
if (inode->vfs_inode.i_nlink == 0) {
ret = btrfs_commit_inode_delayed_inode(inode);
if (ret)
goto out_unlock;
}
ret = copy_inode_items_to_log(trans, inode, &min_key, &max_key,
path, dst_path, logged_isize,
log_mode, ctx, &need_log_inode_item);
if (ret)
goto out_unlock;
btrfs_release_path(path);
btrfs_release_path(dst_path);
ret = btrfs_log_all_xattrs(trans, inode, path, dst_path, ctx);
if (ret)
goto out_unlock;
xattrs_logged = true;
if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
btrfs_release_path(path);
btrfs_release_path(dst_path);
ret = btrfs_log_holes(trans, inode, path);
if (ret)
goto out_unlock;
}
log_extents:
btrfs_release_path(path);
btrfs_release_path(dst_path);
if (need_log_inode_item) {
ret = log_inode_item(trans, log, dst_path, inode, inode_item_dropped);
if (ret)
goto out_unlock;
if (!xattrs_logged && inode->logged_trans < trans->transid) {
ret = btrfs_log_all_xattrs(trans, inode, path, dst_path, ctx);
if (ret)
goto out_unlock;
btrfs_release_path(path);
}
}
if (fast_search) {
ret = btrfs_log_changed_extents(trans, inode, dst_path, ctx);
if (ret)
goto out_unlock;
} else if (log_mode == LOG_INODE_ALL) {
struct extent_map *em, *n;
write_lock(&em_tree->lock);
list_for_each_entry_safe(em, n, &em_tree->modified_extents, list)
list_del_init(&em->list);
write_unlock(&em_tree->lock);
}
if (full_dir_logging) {
ret = log_directory_changes(trans, inode, path, dst_path, ctx);
if (ret)
goto out_unlock;
ret = log_delayed_insertion_items(trans, inode, path,
&delayed_ins_list, ctx);
if (ret)
goto out_unlock;
ret = log_delayed_deletion_items(trans, inode, path,
&delayed_del_list, ctx);
if (ret)
goto out_unlock;
}
spin_lock(&inode->lock);
inode->logged_trans = trans->transid;
if (!ctx->logging_new_name && log_mode != LOG_INODE_EXISTS)
inode->last_log_commit = inode->last_sub_trans;
spin_unlock(&inode->lock);
if (log_mode == LOG_INODE_ALL)
inode->last_reflink_trans = 0;
out_unlock:
mutex_unlock(&inode->log_mutex);
out:
btrfs_free_path(path);
btrfs_free_path(dst_path);
if (ret)
free_conflicting_inodes(ctx);
else
ret = log_conflicting_inodes(trans, inode->root, ctx);
if (full_dir_logging && !ctx->logging_new_delayed_dentries) {
if (!ret)
ret = log_new_delayed_dentries(trans, inode,
&delayed_ins_list, ctx);
btrfs_log_put_delayed_items(inode, &delayed_ins_list,
&delayed_del_list);
}
trace_btrfs_log_inode_exit(trans, inode, ret);
return ret;
}
static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct btrfs_log_ctx *ctx)
{
int ret;
BTRFS_PATH_AUTO_FREE(path);
struct btrfs_key key;
struct btrfs_root *root = inode->root;
const u64 ino = btrfs_ino(inode);
trace_btrfs_log_all_parents_enter(trans, inode);
path = btrfs_alloc_path();
if (!path) {
ret = -ENOMEM;
goto out;
}
path->skip_locking = true;
path->search_commit_root = true;
key.objectid = ino;
key.type = BTRFS_INODE_REF_KEY;
key.offset = 0;
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0)
goto out;
if (WARN_ON_ONCE(ret == 0)) {
btrfs_err(trans->fs_info,
"found inode ref key with offset 0 for root %llu inode %llu",
btrfs_root_id(root), ino);
ret = BTRFS_LOG_FORCE_COMMIT;
goto out;
}
ret = 0;
while (true) {
struct extent_buffer *leaf = path->nodes[0];
int slot = path->slots[0];
u32 cur_offset = 0;
u32 item_size;
unsigned long ptr;
if (slot >= btrfs_header_nritems(leaf)) {
ret = btrfs_next_leaf(root, path);
if (ret < 0)
goto out;
if (ret > 0) {
ret = 0;
break;
}
continue;
}
btrfs_item_key_to_cpu(leaf, &key, slot);
if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
break;
item_size = btrfs_item_size(leaf, slot);
ptr = btrfs_item_ptr_offset(leaf, slot);
while (cur_offset < item_size) {
u64 dir_id;
struct btrfs_inode *dir_inode;
if (key.type == BTRFS_INODE_EXTREF_KEY) {
struct btrfs_inode_extref *extref;
extref = (struct btrfs_inode_extref *)
(ptr + cur_offset);
dir_id = btrfs_inode_extref_parent(leaf, extref);
cur_offset += sizeof(*extref);
cur_offset += btrfs_inode_extref_name_len(leaf,
extref);
} else {
dir_id = key.offset;
cur_offset = item_size;
}
dir_inode = btrfs_iget_logging(dir_id, root);
if (IS_ERR(dir_inode)) {
ret = PTR_ERR(dir_inode);
goto out;
}
if (!need_log_inode(trans, dir_inode)) {
btrfs_add_delayed_iput(dir_inode);
continue;
}
ctx->log_new_dentries = false;
ret = btrfs_log_inode(trans, dir_inode, LOG_INODE_ALL, ctx);
if (!ret && ctx->log_new_dentries)
ret = log_new_dir_dentries(trans, dir_inode, ctx);
btrfs_add_delayed_iput(dir_inode);
if (ret)
goto out;
}
path->slots[0]++;
}
out:
trace_btrfs_log_all_parents_exit(trans, inode, ret);
return ret;
}
static int log_new_ancestors(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_path *path,
struct btrfs_log_ctx *ctx)
{
struct btrfs_key found_key;
btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
while (true) {
struct extent_buffer *leaf;
int slot;
struct btrfs_key search_key;
struct btrfs_inode *inode;
u64 ino;
int ret = 0;
btrfs_release_path(path);
ino = found_key.offset;
search_key.objectid = found_key.offset;
search_key.type = BTRFS_INODE_ITEM_KEY;
search_key.offset = 0;
inode = btrfs_iget_logging(ino, root);
if (IS_ERR(inode))
return PTR_ERR(inode);
if (inode->generation >= trans->transid &&
need_log_inode(trans, inode))
ret = btrfs_log_inode(trans, inode, LOG_INODE_EXISTS, ctx);
btrfs_add_delayed_iput(inode);
if (ret)
return ret;
if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
break;
search_key.type = BTRFS_INODE_REF_KEY;
ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
if (ret < 0)
return ret;
leaf = path->nodes[0];
slot = path->slots[0];
if (slot >= btrfs_header_nritems(leaf)) {
ret = btrfs_next_leaf(root, path);
if (ret < 0)
return ret;
else if (ret > 0)
return -ENOENT;
leaf = path->nodes[0];
slot = path->slots[0];
}
btrfs_item_key_to_cpu(leaf, &found_key, slot);
if (found_key.objectid != search_key.objectid ||
found_key.type != BTRFS_INODE_REF_KEY)
return -ENOENT;
}
return 0;
}
static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct dentry *parent,
struct btrfs_log_ctx *ctx)
{
struct btrfs_root *root = inode->root;
struct dentry *old_parent = NULL;
struct super_block *sb = inode->vfs_inode.i_sb;
int ret = 0;
while (true) {
if (!parent || d_really_is_negative(parent) ||
sb != parent->d_sb)
break;
inode = BTRFS_I(d_inode(parent));
if (root != inode->root)
break;
if (inode->generation >= trans->transid &&
need_log_inode(trans, inode)) {
ret = btrfs_log_inode(trans, inode,
LOG_INODE_EXISTS, ctx);
if (ret)
break;
}
if (IS_ROOT(parent))
break;
parent = dget_parent(parent);
dput(old_parent);
old_parent = parent;
}
dput(old_parent);
return ret;
}
static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct dentry *parent,
struct btrfs_log_ctx *ctx)
{
struct btrfs_root *root = inode->root;
const u64 ino = btrfs_ino(inode);
BTRFS_PATH_AUTO_FREE(path);
struct btrfs_key search_key;
int ret;
trace_btrfs_log_all_new_ancestors_enter(trans, inode);
if (inode->vfs_inode.i_nlink < 2) {
ret = log_new_ancestors_fast(trans, inode, parent, ctx);
goto out;
}
path = btrfs_alloc_path();
if (!path) {
ret = -ENOMEM;
goto out;
}
search_key.objectid = ino;
search_key.type = BTRFS_INODE_REF_KEY;
search_key.offset = 0;
again:
ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
if (ret < 0)
goto out;
if (ret == 0)
path->slots[0]++;
while (true) {
struct extent_buffer *leaf = path->nodes[0];
int slot = path->slots[0];
struct btrfs_key found_key;
if (slot >= btrfs_header_nritems(leaf)) {
ret = btrfs_next_leaf(root, path);
if (ret < 0)
goto out;
if (ret > 0) {
ret = 0;
break;
}
continue;
}
btrfs_item_key_to_cpu(leaf, &found_key, slot);
if (found_key.objectid != ino ||
found_key.type > BTRFS_INODE_EXTREF_KEY)
break;
if (found_key.type == BTRFS_INODE_EXTREF_KEY) {
ret = -EMLINK;
goto out;
}
memcpy(&search_key, &found_key, sizeof(search_key));
ret = log_new_ancestors(trans, root, path, ctx);
if (ret)
goto out;
btrfs_release_path(path);
goto again;
}
out:
trace_btrfs_log_all_new_ancestors_exit(trans, inode, ret);
return ret;
}
static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
struct btrfs_inode *inode,
struct dentry *parent,
enum btrfs_log_mode log_mode,
struct btrfs_log_ctx *ctx)
{
struct btrfs_root *root = inode->root;
struct btrfs_fs_info *fs_info = root->fs_info;
int ret = 0;
bool log_dentries;
trace_btrfs_log_inode_parent_enter(trans, inode);
if (btrfs_test_opt(fs_info, NOTREELOG)) {
ret = BTRFS_LOG_FORCE_COMMIT;
goto out;
}
if (btrfs_root_refs(&root->root_item) == 0) {
ret = BTRFS_LOG_FORCE_COMMIT;
goto out;
}
if (btrfs_root_generation(&root->root_item) == trans->transid) {
ret = BTRFS_LOG_FORCE_COMMIT;
goto out;
}
if (btrfs_inode_in_log(inode, trans->transid) &&
list_empty(&ctx->ordered_extents)) {
ret = BTRFS_NO_LOG_SYNC;
goto out;
}
ret = start_log_trans(trans, root, ctx);
if (ret)
goto out;
ret = btrfs_log_inode(trans, inode, log_mode, ctx);
if (ret)
goto end_trans;
if (S_ISREG(inode->vfs_inode.i_mode) &&
inode->generation < trans->transid &&
inode->last_unlink_trans < trans->transid) {
ret = 0;
goto end_trans;
}
log_dentries = ctx->log_new_dentries;
if (inode->last_unlink_trans >= trans->transid) {
ret = btrfs_log_all_parents(trans, inode, ctx);
if (ret)
goto end_trans;
}
ret = log_all_new_ancestors(trans, inode, parent, ctx);
if (ret)
goto end_trans;
if (log_dentries)
ret = log_new_dir_dentries(trans, inode, ctx);
end_trans:
if (ret < 0) {
btrfs_set_log_full_commit(trans);
ret = BTRFS_LOG_FORCE_COMMIT;
}
if (ret)
btrfs_remove_log_ctx(root, ctx);
btrfs_end_log_trans(root);
out:
trace_btrfs_log_inode_parent_exit(trans, inode, ret);
return ret;
}
int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
struct dentry *dentry,
struct btrfs_log_ctx *ctx)
{
struct dentry *parent = dget_parent(dentry);
int ret;
ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
LOG_INODE_ALL, ctx);
dput(parent);
return ret;
}
int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
{
int ret;
struct btrfs_path *path;
struct btrfs_trans_handle *trans;
struct btrfs_key key;
struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
struct walk_control wc = {
.process_func = process_one_buffer,
.stage = LOG_WALK_PIN_ONLY,
};
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
trans = btrfs_start_transaction(fs_info->tree_root, 0);
if (IS_ERR(trans)) {
ret = PTR_ERR(trans);
goto error;
}
wc.trans = trans;
wc.pin = true;
wc.log = log_root_tree;
ret = walk_log_tree(&wc);
wc.log = NULL;
if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
goto error;
}
again:
key.objectid = BTRFS_TREE_LOG_OBJECTID;
key.type = BTRFS_ROOT_ITEM_KEY;
key.offset = (u64)-1;
while (1) {
struct btrfs_key found_key;
ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
if (unlikely(ret < 0)) {
btrfs_abort_transaction(trans, ret);
goto error;
}
if (ret > 0) {
if (path->slots[0] == 0)
break;
path->slots[0]--;
}
btrfs_item_key_to_cpu(path->nodes[0], &found_key,
path->slots[0]);
btrfs_release_path(path);
if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
break;
wc.log = btrfs_read_tree_root(log_root_tree, &found_key);
if (IS_ERR(wc.log)) {
ret = PTR_ERR(wc.log);
wc.log = NULL;
btrfs_abort_transaction(trans, ret);
goto error;
}
wc.root = btrfs_get_fs_root(fs_info, found_key.offset, true);
if (IS_ERR(wc.root)) {
ret = PTR_ERR(wc.root);
wc.root = NULL;
if (unlikely(ret != -ENOENT)) {
btrfs_abort_transaction(trans, ret);
goto error;
}
ret = btrfs_pin_extent_for_log_replay(trans, wc.log->node);
if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
goto error;
}
goto next;
}
wc.root->log_root = wc.log;
ret = btrfs_record_root_in_trans(trans, wc.root);
if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
goto next;
}
ret = walk_log_tree(&wc);
if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
goto next;
}
if (wc.stage == LOG_WALK_REPLAY_ALL) {
struct btrfs_root *root = wc.root;
wc.subvol_path = path;
ret = fixup_inode_link_counts(&wc);
wc.subvol_path = NULL;
if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
goto next;
}
ret = btrfs_init_root_free_objectid(root);
if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
goto next;
}
}
next:
if (wc.root) {
wc.root->log_root = NULL;
btrfs_put_root(wc.root);
}
btrfs_put_root(wc.log);
wc.log = NULL;
if (ret)
goto error;
if (found_key.offset == 0)
break;
key.offset = found_key.offset - 1;
}
btrfs_release_path(path);
if (wc.pin) {
wc.pin = false;
wc.process_func = replay_one_buffer;
wc.stage = LOG_WALK_REPLAY_INODES;
goto again;
}
if (wc.stage < LOG_WALK_REPLAY_ALL) {
wc.stage++;
goto again;
}
btrfs_free_path(path);
ret = btrfs_commit_transaction(trans);
if (ret)
return ret;
clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
return 0;
error:
if (wc.trans)
btrfs_end_transaction(wc.trans);
btrfs_put_root(wc.log);
clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
btrfs_free_path(path);
return ret;
}
void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
struct btrfs_inode *dir, struct btrfs_inode *inode,
bool for_rename)
{
trace_btrfs_record_unlink_dir(trans, dir, inode, for_rename);
mutex_lock(&inode->log_mutex);
inode->last_unlink_trans = trans->transid;
mutex_unlock(&inode->log_mutex);
if (!for_rename)
return;
if (inode_logged(trans, dir, NULL) == 1)
return;
if (inode_logged(trans, inode, NULL) == 1)
return;
mutex_lock(&dir->log_mutex);
dir->last_unlink_trans = trans->transid;
mutex_unlock(&dir->log_mutex);
}
void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
struct btrfs_inode *dir)
{
trace_btrfs_record_snapshot_destroy(trans, dir);
mutex_lock(&dir->log_mutex);
dir->last_unlink_trans = trans->transid;
mutex_unlock(&dir->log_mutex);
}
void btrfs_record_new_subvolume(const struct btrfs_trans_handle *trans,
struct btrfs_inode *dir)
{
trace_btrfs_record_new_subvolume(trans, dir);
mutex_lock(&dir->log_mutex);
dir->last_unlink_trans = trans->transid;
mutex_unlock(&dir->log_mutex);
}
void btrfs_log_new_name(struct btrfs_trans_handle *trans,
struct dentry *old_dentry, struct btrfs_inode *old_dir,
u64 old_dir_index, struct dentry *parent)
{
struct btrfs_inode *inode = BTRFS_I(d_inode(old_dentry));
struct btrfs_root *root = inode->root;
struct btrfs_log_ctx ctx;
bool log_pinned = false;
int ret;
trace_btrfs_log_new_name_enter(trans, inode, old_dir, old_dir_index);
set_bit(BTRFS_INODE_COPY_EVERYTHING, &inode->runtime_flags);
btrfs_init_log_ctx(&ctx, inode);
ctx.logging_new_name = true;
if (!S_ISDIR(inode->vfs_inode.i_mode))
inode->last_unlink_trans = trans->transid;
ret = inode_logged(trans, inode, NULL);
if (ret < 0) {
goto out;
} else if (ret == 0) {
if (!old_dir)
goto out;
ret = inode_logged(trans, old_dir, NULL);
if (ret < 0)
goto out;
else if (ret == 0)
goto out;
}
ret = 0;
btrfs_init_log_ctx_scratch_eb(&ctx);
if (old_dir && old_dir->logged_trans == trans->transid) {
struct btrfs_root *log = old_dir->root->log_root;
struct btrfs_path *path;
struct fscrypt_name fname;
ASSERT(old_dir_index >= BTRFS_DIR_START_INDEX,
"old_dir_index=%llu", old_dir_index);
ret = fscrypt_setup_filename(&old_dir->vfs_inode,
&old_dentry->d_name, 0, &fname);
if (ret)
goto out;
path = btrfs_alloc_path();
if (!path) {
ret = -ENOMEM;
fscrypt_free_filename(&fname);
goto out;
}
ret = join_running_log_trans(root);
if (WARN_ON_ONCE(ret < 0)) {
btrfs_free_path(path);
fscrypt_free_filename(&fname);
goto out;
}
log_pinned = true;
mutex_lock(&old_dir->log_mutex);
ret = del_logged_dentry(trans, log, path, btrfs_ino(old_dir),
&fname.disk_name, old_dir_index);
if (ret > 0) {
btrfs_release_path(path);
ret = insert_dir_log_key(trans, log, path,
btrfs_ino(old_dir),
old_dir_index, old_dir_index);
}
mutex_unlock(&old_dir->log_mutex);
btrfs_free_path(path);
fscrypt_free_filename(&fname);
if (ret < 0)
goto out;
}
btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx);
ASSERT(list_empty(&ctx.conflict_inodes));
out:
trace_btrfs_log_new_name_exit(trans, inode, old_dir, ret);
if (ret < 0)
btrfs_set_log_full_commit(trans);
if (log_pinned)
btrfs_end_log_trans(root);
free_extent_buffer(ctx.scratch_eb);
}