tree-log.c 175 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2008 Oracle. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/slab.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/list_sort.h>
  9. #include <linux/iversion.h>
  10. #include "misc.h"
  11. #include "ctree.h"
  12. #include "tree-log.h"
  13. #include "disk-io.h"
  14. #include "locking.h"
  15. #include "print-tree.h"
  16. #include "backref.h"
  17. #include "compression.h"
  18. #include "qgroup.h"
  19. #include "inode-map.h"
  20. #include "block-group.h"
  21. #include "space-info.h"
  22. /* magic values for the inode_only field in btrfs_log_inode:
  23. *
  24. * LOG_INODE_ALL means to log everything
  25. * LOG_INODE_EXISTS means to log just enough to recreate the inode
  26. * during log replay
  27. */
  28. enum {
  29. LOG_INODE_ALL,
  30. LOG_INODE_EXISTS,
  31. LOG_OTHER_INODE,
  32. LOG_OTHER_INODE_ALL,
  33. };
  34. /*
  35. * directory trouble cases
  36. *
  37. * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
  38. * log, we must force a full commit before doing an fsync of the directory
  39. * where the unlink was done.
  40. * ---> record transid of last unlink/rename per directory
  41. *
  42. * mkdir foo/some_dir
  43. * normal commit
  44. * rename foo/some_dir foo2/some_dir
  45. * mkdir foo/some_dir
  46. * fsync foo/some_dir/some_file
  47. *
  48. * The fsync above will unlink the original some_dir without recording
  49. * it in its new location (foo2). After a crash, some_dir will be gone
  50. * unless the fsync of some_file forces a full commit
  51. *
  52. * 2) we must log any new names for any file or dir that is in the fsync
  53. * log. ---> check inode while renaming/linking.
  54. *
  55. * 2a) we must log any new names for any file or dir during rename
  56. * when the directory they are being removed from was logged.
  57. * ---> check inode and old parent dir during rename
  58. *
  59. * 2a is actually the more important variant. With the extra logging
  60. * a crash might unlink the old name without recreating the new one
  61. *
  62. * 3) after a crash, we must go through any directories with a link count
  63. * of zero and redo the rm -rf
  64. *
  65. * mkdir f1/foo
  66. * normal commit
  67. * rm -rf f1/foo
  68. * fsync(f1)
  69. *
  70. * The directory f1 was fully removed from the FS, but fsync was never
  71. * called on f1, only its parent dir. After a crash the rm -rf must
  72. * be replayed. This must be able to recurse down the entire
  73. * directory tree. The inode link count fixup code takes care of the
  74. * ugly details.
  75. */
  76. /*
  77. * stages for the tree walking. The first
  78. * stage (0) is to only pin down the blocks we find
  79. * the second stage (1) is to make sure that all the inodes
  80. * we find in the log are created in the subvolume.
  81. *
  82. * The last stage is to deal with directories and links and extents
  83. * and all the other fun semantics
  84. */
  85. enum {
  86. LOG_WALK_PIN_ONLY,
  87. LOG_WALK_REPLAY_INODES,
  88. LOG_WALK_REPLAY_DIR_INDEX,
  89. LOG_WALK_REPLAY_ALL,
  90. };
  91. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  92. struct btrfs_root *root, struct btrfs_inode *inode,
  93. int inode_only,
  94. struct btrfs_log_ctx *ctx);
  95. static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  96. struct btrfs_root *root,
  97. struct btrfs_path *path, u64 objectid);
  98. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  99. struct btrfs_root *root,
  100. struct btrfs_root *log,
  101. struct btrfs_path *path,
  102. u64 dirid, int del_all);
  103. /*
  104. * tree logging is a special write ahead log used to make sure that
  105. * fsyncs and O_SYNCs can happen without doing full tree commits.
  106. *
  107. * Full tree commits are expensive because they require commonly
  108. * modified blocks to be recowed, creating many dirty pages in the
  109. * extent tree an 4x-6x higher write load than ext3.
  110. *
  111. * Instead of doing a tree commit on every fsync, we use the
  112. * key ranges and transaction ids to find items for a given file or directory
  113. * that have changed in this transaction. Those items are copied into
  114. * a special tree (one per subvolume root), that tree is written to disk
  115. * and then the fsync is considered complete.
  116. *
  117. * After a crash, items are copied out of the log-tree back into the
  118. * subvolume tree. Any file data extents found are recorded in the extent
  119. * allocation tree, and the log-tree freed.
  120. *
  121. * The log tree is read three times, once to pin down all the extents it is
  122. * using in ram and once, once to create all the inodes logged in the tree
  123. * and once to do all the other items.
  124. */
  125. /*
  126. * start a sub transaction and setup the log tree
  127. * this increments the log tree writer count to make the people
  128. * syncing the tree wait for us to finish
  129. */
  130. static int start_log_trans(struct btrfs_trans_handle *trans,
  131. struct btrfs_root *root,
  132. struct btrfs_log_ctx *ctx)
  133. {
  134. struct btrfs_fs_info *fs_info = root->fs_info;
  135. int ret = 0;
  136. mutex_lock(&root->log_mutex);
  137. if (root->log_root) {
  138. if (btrfs_need_log_full_commit(trans)) {
  139. ret = -EAGAIN;
  140. goto out;
  141. }
  142. if (!root->log_start_pid) {
  143. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  144. root->log_start_pid = current->pid;
  145. } else if (root->log_start_pid != current->pid) {
  146. set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  147. }
  148. } else {
  149. mutex_lock(&fs_info->tree_log_mutex);
  150. if (!fs_info->log_root_tree)
  151. ret = btrfs_init_log_root_tree(trans, fs_info);
  152. mutex_unlock(&fs_info->tree_log_mutex);
  153. if (ret)
  154. goto out;
  155. ret = btrfs_add_log_tree(trans, root);
  156. if (ret)
  157. goto out;
  158. set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
  159. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  160. root->log_start_pid = current->pid;
  161. }
  162. atomic_inc(&root->log_batch);
  163. atomic_inc(&root->log_writers);
  164. if (ctx && !ctx->logging_new_name) {
  165. int index = root->log_transid % 2;
  166. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  167. ctx->log_transid = root->log_transid;
  168. }
  169. out:
  170. mutex_unlock(&root->log_mutex);
  171. return ret;
  172. }
  173. /*
  174. * returns 0 if there was a log transaction running and we were able
  175. * to join, or returns -ENOENT if there were not transactions
  176. * in progress
  177. */
  178. static int join_running_log_trans(struct btrfs_root *root)
  179. {
  180. int ret = -ENOENT;
  181. if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
  182. return ret;
  183. mutex_lock(&root->log_mutex);
  184. if (root->log_root) {
  185. ret = 0;
  186. atomic_inc(&root->log_writers);
  187. }
  188. mutex_unlock(&root->log_mutex);
  189. return ret;
  190. }
  191. /*
  192. * This either makes the current running log transaction wait
  193. * until you call btrfs_end_log_trans() or it makes any future
  194. * log transactions wait until you call btrfs_end_log_trans()
  195. */
  196. void btrfs_pin_log_trans(struct btrfs_root *root)
  197. {
  198. atomic_inc(&root->log_writers);
  199. }
  200. /*
  201. * indicate we're done making changes to the log tree
  202. * and wake up anyone waiting to do a sync
  203. */
  204. void btrfs_end_log_trans(struct btrfs_root *root)
  205. {
  206. if (atomic_dec_and_test(&root->log_writers)) {
  207. /* atomic_dec_and_test implies a barrier */
  208. cond_wake_up_nomb(&root->log_writer_wait);
  209. }
  210. }
  211. static int btrfs_write_tree_block(struct extent_buffer *buf)
  212. {
  213. return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start,
  214. buf->start + buf->len - 1);
  215. }
  216. static void btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
  217. {
  218. filemap_fdatawait_range(buf->pages[0]->mapping,
  219. buf->start, buf->start + buf->len - 1);
  220. }
  221. /*
  222. * the walk control struct is used to pass state down the chain when
  223. * processing the log tree. The stage field tells us which part
  224. * of the log tree processing we are currently doing. The others
  225. * are state fields used for that specific part
  226. */
  227. struct walk_control {
  228. /* should we free the extent on disk when done? This is used
  229. * at transaction commit time while freeing a log tree
  230. */
  231. int free;
  232. /* should we write out the extent buffer? This is used
  233. * while flushing the log tree to disk during a sync
  234. */
  235. int write;
  236. /* should we wait for the extent buffer io to finish? Also used
  237. * while flushing the log tree to disk for a sync
  238. */
  239. int wait;
  240. /* pin only walk, we record which extents on disk belong to the
  241. * log trees
  242. */
  243. int pin;
  244. /* what stage of the replay code we're currently in */
  245. int stage;
  246. /*
  247. * Ignore any items from the inode currently being processed. Needs
  248. * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
  249. * the LOG_WALK_REPLAY_INODES stage.
  250. */
  251. bool ignore_cur_inode;
  252. /* the root we are currently replaying */
  253. struct btrfs_root *replay_dest;
  254. /* the trans handle for the current replay */
  255. struct btrfs_trans_handle *trans;
  256. /* the function that gets used to process blocks we find in the
  257. * tree. Note the extent_buffer might not be up to date when it is
  258. * passed in, and it must be checked or read if you need the data
  259. * inside it
  260. */
  261. int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
  262. struct walk_control *wc, u64 gen, int level);
  263. };
  264. /*
  265. * process_func used to pin down extents, write them or wait on them
  266. */
  267. static int process_one_buffer(struct btrfs_root *log,
  268. struct extent_buffer *eb,
  269. struct walk_control *wc, u64 gen, int level)
  270. {
  271. struct btrfs_fs_info *fs_info = log->fs_info;
  272. int ret = 0;
  273. /*
  274. * If this fs is mixed then we need to be able to process the leaves to
  275. * pin down any logged extents, so we have to read the block.
  276. */
  277. if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
  278. ret = btrfs_read_buffer(eb, gen, level, NULL);
  279. if (ret)
  280. return ret;
  281. }
  282. if (wc->pin)
  283. ret = btrfs_pin_extent_for_log_replay(wc->trans, eb->start,
  284. eb->len);
  285. if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
  286. if (wc->pin && btrfs_header_level(eb) == 0)
  287. ret = btrfs_exclude_logged_extents(eb);
  288. if (wc->write)
  289. btrfs_write_tree_block(eb);
  290. if (wc->wait)
  291. btrfs_wait_tree_block_writeback(eb);
  292. }
  293. return ret;
  294. }
  295. /*
  296. * Item overwrite used by replay and tree logging. eb, slot and key all refer
  297. * to the src data we are copying out.
  298. *
  299. * root is the tree we are copying into, and path is a scratch
  300. * path for use in this function (it should be released on entry and
  301. * will be released on exit).
  302. *
  303. * If the key is already in the destination tree the existing item is
  304. * overwritten. If the existing item isn't big enough, it is extended.
  305. * If it is too large, it is truncated.
  306. *
  307. * If the key isn't in the destination yet, a new item is inserted.
  308. */
  309. static noinline int overwrite_item(struct btrfs_trans_handle *trans,
  310. struct btrfs_root *root,
  311. struct btrfs_path *path,
  312. struct extent_buffer *eb, int slot,
  313. struct btrfs_key *key)
  314. {
  315. int ret;
  316. u32 item_size;
  317. u64 saved_i_size = 0;
  318. int save_old_i_size = 0;
  319. unsigned long src_ptr;
  320. unsigned long dst_ptr;
  321. int overwrite_root = 0;
  322. bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
  323. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  324. overwrite_root = 1;
  325. item_size = btrfs_item_size_nr(eb, slot);
  326. src_ptr = btrfs_item_ptr_offset(eb, slot);
  327. /* look for the key in the destination tree */
  328. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  329. if (ret < 0)
  330. return ret;
  331. if (ret == 0) {
  332. char *src_copy;
  333. char *dst_copy;
  334. u32 dst_size = btrfs_item_size_nr(path->nodes[0],
  335. path->slots[0]);
  336. if (dst_size != item_size)
  337. goto insert;
  338. if (item_size == 0) {
  339. btrfs_release_path(path);
  340. return 0;
  341. }
  342. dst_copy = kmalloc(item_size, GFP_NOFS);
  343. src_copy = kmalloc(item_size, GFP_NOFS);
  344. if (!dst_copy || !src_copy) {
  345. btrfs_release_path(path);
  346. kfree(dst_copy);
  347. kfree(src_copy);
  348. return -ENOMEM;
  349. }
  350. read_extent_buffer(eb, src_copy, src_ptr, item_size);
  351. dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  352. read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
  353. item_size);
  354. ret = memcmp(dst_copy, src_copy, item_size);
  355. kfree(dst_copy);
  356. kfree(src_copy);
  357. /*
  358. * they have the same contents, just return, this saves
  359. * us from cowing blocks in the destination tree and doing
  360. * extra writes that may not have been done by a previous
  361. * sync
  362. */
  363. if (ret == 0) {
  364. btrfs_release_path(path);
  365. return 0;
  366. }
  367. /*
  368. * We need to load the old nbytes into the inode so when we
  369. * replay the extents we've logged we get the right nbytes.
  370. */
  371. if (inode_item) {
  372. struct btrfs_inode_item *item;
  373. u64 nbytes;
  374. u32 mode;
  375. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  376. struct btrfs_inode_item);
  377. nbytes = btrfs_inode_nbytes(path->nodes[0], item);
  378. item = btrfs_item_ptr(eb, slot,
  379. struct btrfs_inode_item);
  380. btrfs_set_inode_nbytes(eb, item, nbytes);
  381. /*
  382. * If this is a directory we need to reset the i_size to
  383. * 0 so that we can set it up properly when replaying
  384. * the rest of the items in this log.
  385. */
  386. mode = btrfs_inode_mode(eb, item);
  387. if (S_ISDIR(mode))
  388. btrfs_set_inode_size(eb, item, 0);
  389. }
  390. } else if (inode_item) {
  391. struct btrfs_inode_item *item;
  392. u32 mode;
  393. /*
  394. * New inode, set nbytes to 0 so that the nbytes comes out
  395. * properly when we replay the extents.
  396. */
  397. item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  398. btrfs_set_inode_nbytes(eb, item, 0);
  399. /*
  400. * If this is a directory we need to reset the i_size to 0 so
  401. * that we can set it up properly when replaying the rest of
  402. * the items in this log.
  403. */
  404. mode = btrfs_inode_mode(eb, item);
  405. if (S_ISDIR(mode))
  406. btrfs_set_inode_size(eb, item, 0);
  407. }
  408. insert:
  409. btrfs_release_path(path);
  410. /* try to insert the key into the destination tree */
  411. path->skip_release_on_error = 1;
  412. ret = btrfs_insert_empty_item(trans, root, path,
  413. key, item_size);
  414. path->skip_release_on_error = 0;
  415. /* make sure any existing item is the correct size */
  416. if (ret == -EEXIST || ret == -EOVERFLOW) {
  417. u32 found_size;
  418. found_size = btrfs_item_size_nr(path->nodes[0],
  419. path->slots[0]);
  420. if (found_size > item_size)
  421. btrfs_truncate_item(path, item_size, 1);
  422. else if (found_size < item_size)
  423. btrfs_extend_item(path, item_size - found_size);
  424. } else if (ret) {
  425. return ret;
  426. }
  427. dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
  428. path->slots[0]);
  429. /* don't overwrite an existing inode if the generation number
  430. * was logged as zero. This is done when the tree logging code
  431. * is just logging an inode to make sure it exists after recovery.
  432. *
  433. * Also, don't overwrite i_size on directories during replay.
  434. * log replay inserts and removes directory items based on the
  435. * state of the tree found in the subvolume, and i_size is modified
  436. * as it goes
  437. */
  438. if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
  439. struct btrfs_inode_item *src_item;
  440. struct btrfs_inode_item *dst_item;
  441. src_item = (struct btrfs_inode_item *)src_ptr;
  442. dst_item = (struct btrfs_inode_item *)dst_ptr;
  443. if (btrfs_inode_generation(eb, src_item) == 0) {
  444. struct extent_buffer *dst_eb = path->nodes[0];
  445. const u64 ino_size = btrfs_inode_size(eb, src_item);
  446. /*
  447. * For regular files an ino_size == 0 is used only when
  448. * logging that an inode exists, as part of a directory
  449. * fsync, and the inode wasn't fsynced before. In this
  450. * case don't set the size of the inode in the fs/subvol
  451. * tree, otherwise we would be throwing valid data away.
  452. */
  453. if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
  454. S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
  455. ino_size != 0)
  456. btrfs_set_inode_size(dst_eb, dst_item, ino_size);
  457. goto no_copy;
  458. }
  459. if (overwrite_root &&
  460. S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
  461. S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
  462. save_old_i_size = 1;
  463. saved_i_size = btrfs_inode_size(path->nodes[0],
  464. dst_item);
  465. }
  466. }
  467. copy_extent_buffer(path->nodes[0], eb, dst_ptr,
  468. src_ptr, item_size);
  469. if (save_old_i_size) {
  470. struct btrfs_inode_item *dst_item;
  471. dst_item = (struct btrfs_inode_item *)dst_ptr;
  472. btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
  473. }
  474. /* make sure the generation is filled in */
  475. if (key->type == BTRFS_INODE_ITEM_KEY) {
  476. struct btrfs_inode_item *dst_item;
  477. dst_item = (struct btrfs_inode_item *)dst_ptr;
  478. if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
  479. btrfs_set_inode_generation(path->nodes[0], dst_item,
  480. trans->transid);
  481. }
  482. }
  483. no_copy:
  484. btrfs_mark_buffer_dirty(path->nodes[0]);
  485. btrfs_release_path(path);
  486. return 0;
  487. }
  488. /*
  489. * simple helper to read an inode off the disk from a given root
  490. * This can only be called for subvolume roots and not for the log
  491. */
  492. static noinline struct inode *read_one_inode(struct btrfs_root *root,
  493. u64 objectid)
  494. {
  495. struct inode *inode;
  496. inode = btrfs_iget(root->fs_info->sb, objectid, root);
  497. if (IS_ERR(inode))
  498. inode = NULL;
  499. return inode;
  500. }
  501. /* replays a single extent in 'eb' at 'slot' with 'key' into the
  502. * subvolume 'root'. path is released on entry and should be released
  503. * on exit.
  504. *
  505. * extents in the log tree have not been allocated out of the extent
  506. * tree yet. So, this completes the allocation, taking a reference
  507. * as required if the extent already exists or creating a new extent
  508. * if it isn't in the extent allocation tree yet.
  509. *
  510. * The extent is inserted into the file, dropping any existing extents
  511. * from the file that overlap the new one.
  512. */
  513. static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
  514. struct btrfs_root *root,
  515. struct btrfs_path *path,
  516. struct extent_buffer *eb, int slot,
  517. struct btrfs_key *key)
  518. {
  519. struct btrfs_fs_info *fs_info = root->fs_info;
  520. int found_type;
  521. u64 extent_end;
  522. u64 start = key->offset;
  523. u64 nbytes = 0;
  524. struct btrfs_file_extent_item *item;
  525. struct inode *inode = NULL;
  526. unsigned long size;
  527. int ret = 0;
  528. item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  529. found_type = btrfs_file_extent_type(eb, item);
  530. if (found_type == BTRFS_FILE_EXTENT_REG ||
  531. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  532. nbytes = btrfs_file_extent_num_bytes(eb, item);
  533. extent_end = start + nbytes;
  534. /*
  535. * We don't add to the inodes nbytes if we are prealloc or a
  536. * hole.
  537. */
  538. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  539. nbytes = 0;
  540. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  541. size = btrfs_file_extent_ram_bytes(eb, item);
  542. nbytes = btrfs_file_extent_ram_bytes(eb, item);
  543. extent_end = ALIGN(start + size,
  544. fs_info->sectorsize);
  545. } else {
  546. ret = 0;
  547. goto out;
  548. }
  549. inode = read_one_inode(root, key->objectid);
  550. if (!inode) {
  551. ret = -EIO;
  552. goto out;
  553. }
  554. /*
  555. * first check to see if we already have this extent in the
  556. * file. This must be done before the btrfs_drop_extents run
  557. * so we don't try to drop this extent.
  558. */
  559. ret = btrfs_lookup_file_extent(trans, root, path,
  560. btrfs_ino(BTRFS_I(inode)), start, 0);
  561. if (ret == 0 &&
  562. (found_type == BTRFS_FILE_EXTENT_REG ||
  563. found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
  564. struct btrfs_file_extent_item cmp1;
  565. struct btrfs_file_extent_item cmp2;
  566. struct btrfs_file_extent_item *existing;
  567. struct extent_buffer *leaf;
  568. leaf = path->nodes[0];
  569. existing = btrfs_item_ptr(leaf, path->slots[0],
  570. struct btrfs_file_extent_item);
  571. read_extent_buffer(eb, &cmp1, (unsigned long)item,
  572. sizeof(cmp1));
  573. read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
  574. sizeof(cmp2));
  575. /*
  576. * we already have a pointer to this exact extent,
  577. * we don't have to do anything
  578. */
  579. if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
  580. btrfs_release_path(path);
  581. goto out;
  582. }
  583. }
  584. btrfs_release_path(path);
  585. /* drop any overlapping extents */
  586. ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
  587. if (ret)
  588. goto out;
  589. if (found_type == BTRFS_FILE_EXTENT_REG ||
  590. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  591. u64 offset;
  592. unsigned long dest_offset;
  593. struct btrfs_key ins;
  594. if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
  595. btrfs_fs_incompat(fs_info, NO_HOLES))
  596. goto update_inode;
  597. ret = btrfs_insert_empty_item(trans, root, path, key,
  598. sizeof(*item));
  599. if (ret)
  600. goto out;
  601. dest_offset = btrfs_item_ptr_offset(path->nodes[0],
  602. path->slots[0]);
  603. copy_extent_buffer(path->nodes[0], eb, dest_offset,
  604. (unsigned long)item, sizeof(*item));
  605. ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  606. ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  607. ins.type = BTRFS_EXTENT_ITEM_KEY;
  608. offset = key->offset - btrfs_file_extent_offset(eb, item);
  609. /*
  610. * Manually record dirty extent, as here we did a shallow
  611. * file extent item copy and skip normal backref update,
  612. * but modifying extent tree all by ourselves.
  613. * So need to manually record dirty extent for qgroup,
  614. * as the owner of the file extent changed from log tree
  615. * (doesn't affect qgroup) to fs/file tree(affects qgroup)
  616. */
  617. ret = btrfs_qgroup_trace_extent(trans,
  618. btrfs_file_extent_disk_bytenr(eb, item),
  619. btrfs_file_extent_disk_num_bytes(eb, item),
  620. GFP_NOFS);
  621. if (ret < 0)
  622. goto out;
  623. if (ins.objectid > 0) {
  624. struct btrfs_ref ref = { 0 };
  625. u64 csum_start;
  626. u64 csum_end;
  627. LIST_HEAD(ordered_sums);
  628. /*
  629. * is this extent already allocated in the extent
  630. * allocation tree? If so, just add a reference
  631. */
  632. ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
  633. ins.offset);
  634. if (ret < 0) {
  635. goto out;
  636. } else if (ret == 0) {
  637. btrfs_init_generic_ref(&ref,
  638. BTRFS_ADD_DELAYED_REF,
  639. ins.objectid, ins.offset, 0);
  640. btrfs_init_data_ref(&ref,
  641. root->root_key.objectid,
  642. key->objectid, offset);
  643. ret = btrfs_inc_extent_ref(trans, &ref);
  644. if (ret)
  645. goto out;
  646. } else {
  647. /*
  648. * insert the extent pointer in the extent
  649. * allocation tree
  650. */
  651. ret = btrfs_alloc_logged_file_extent(trans,
  652. root->root_key.objectid,
  653. key->objectid, offset, &ins);
  654. if (ret)
  655. goto out;
  656. }
  657. btrfs_release_path(path);
  658. if (btrfs_file_extent_compression(eb, item)) {
  659. csum_start = ins.objectid;
  660. csum_end = csum_start + ins.offset;
  661. } else {
  662. csum_start = ins.objectid +
  663. btrfs_file_extent_offset(eb, item);
  664. csum_end = csum_start +
  665. btrfs_file_extent_num_bytes(eb, item);
  666. }
  667. ret = btrfs_lookup_csums_range(root->log_root,
  668. csum_start, csum_end - 1,
  669. &ordered_sums, 0);
  670. if (ret)
  671. goto out;
  672. /*
  673. * Now delete all existing cums in the csum root that
  674. * cover our range. We do this because we can have an
  675. * extent that is completely referenced by one file
  676. * extent item and partially referenced by another
  677. * file extent item (like after using the clone or
  678. * extent_same ioctls). In this case if we end up doing
  679. * the replay of the one that partially references the
  680. * extent first, and we do not do the csum deletion
  681. * below, we can get 2 csum items in the csum tree that
  682. * overlap each other. For example, imagine our log has
  683. * the two following file extent items:
  684. *
  685. * key (257 EXTENT_DATA 409600)
  686. * extent data disk byte 12845056 nr 102400
  687. * extent data offset 20480 nr 20480 ram 102400
  688. *
  689. * key (257 EXTENT_DATA 819200)
  690. * extent data disk byte 12845056 nr 102400
  691. * extent data offset 0 nr 102400 ram 102400
  692. *
  693. * Where the second one fully references the 100K extent
  694. * that starts at disk byte 12845056, and the log tree
  695. * has a single csum item that covers the entire range
  696. * of the extent:
  697. *
  698. * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
  699. *
  700. * After the first file extent item is replayed, the
  701. * csum tree gets the following csum item:
  702. *
  703. * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
  704. *
  705. * Which covers the 20K sub-range starting at offset 20K
  706. * of our extent. Now when we replay the second file
  707. * extent item, if we do not delete existing csum items
  708. * that cover any of its blocks, we end up getting two
  709. * csum items in our csum tree that overlap each other:
  710. *
  711. * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
  712. * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
  713. *
  714. * Which is a problem, because after this anyone trying
  715. * to lookup up for the checksum of any block of our
  716. * extent starting at an offset of 40K or higher, will
  717. * end up looking at the second csum item only, which
  718. * does not contain the checksum for any block starting
  719. * at offset 40K or higher of our extent.
  720. */
  721. while (!list_empty(&ordered_sums)) {
  722. struct btrfs_ordered_sum *sums;
  723. sums = list_entry(ordered_sums.next,
  724. struct btrfs_ordered_sum,
  725. list);
  726. if (!ret)
  727. ret = btrfs_del_csums(trans,
  728. fs_info->csum_root,
  729. sums->bytenr,
  730. sums->len);
  731. if (!ret)
  732. ret = btrfs_csum_file_blocks(trans,
  733. fs_info->csum_root, sums);
  734. list_del(&sums->list);
  735. kfree(sums);
  736. }
  737. if (ret)
  738. goto out;
  739. } else {
  740. btrfs_release_path(path);
  741. }
  742. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  743. /* inline extents are easy, we just overwrite them */
  744. ret = overwrite_item(trans, root, path, eb, slot, key);
  745. if (ret)
  746. goto out;
  747. }
  748. ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start,
  749. extent_end - start);
  750. if (ret)
  751. goto out;
  752. inode_add_bytes(inode, nbytes);
  753. update_inode:
  754. ret = btrfs_update_inode(trans, root, inode);
  755. out:
  756. if (inode)
  757. iput(inode);
  758. return ret;
  759. }
  760. /*
  761. * when cleaning up conflicts between the directory names in the
  762. * subvolume, directory names in the log and directory names in the
  763. * inode back references, we may have to unlink inodes from directories.
  764. *
  765. * This is a helper function to do the unlink of a specific directory
  766. * item
  767. */
  768. static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
  769. struct btrfs_root *root,
  770. struct btrfs_path *path,
  771. struct btrfs_inode *dir,
  772. struct btrfs_dir_item *di)
  773. {
  774. struct inode *inode;
  775. char *name;
  776. int name_len;
  777. struct extent_buffer *leaf;
  778. struct btrfs_key location;
  779. int ret;
  780. leaf = path->nodes[0];
  781. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  782. name_len = btrfs_dir_name_len(leaf, di);
  783. name = kmalloc(name_len, GFP_NOFS);
  784. if (!name)
  785. return -ENOMEM;
  786. read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
  787. btrfs_release_path(path);
  788. inode = read_one_inode(root, location.objectid);
  789. if (!inode) {
  790. ret = -EIO;
  791. goto out;
  792. }
  793. ret = link_to_fixup_dir(trans, root, path, location.objectid);
  794. if (ret)
  795. goto out;
  796. ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
  797. name_len);
  798. if (ret)
  799. goto out;
  800. else
  801. ret = btrfs_run_delayed_items(trans);
  802. out:
  803. kfree(name);
  804. iput(inode);
  805. return ret;
  806. }
  807. /*
  808. * See if a given name and sequence number found in an inode back reference are
  809. * already in a directory and correctly point to this inode.
  810. *
  811. * Returns: < 0 on error, 0 if the directory entry does not exists and 1 if it
  812. * exists.
  813. */
  814. static noinline int inode_in_dir(struct btrfs_root *root,
  815. struct btrfs_path *path,
  816. u64 dirid, u64 objectid, u64 index,
  817. const char *name, int name_len)
  818. {
  819. struct btrfs_dir_item *di;
  820. struct btrfs_key location;
  821. int ret = 0;
  822. di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
  823. index, name, name_len, 0);
  824. if (IS_ERR(di)) {
  825. if (PTR_ERR(di) != -ENOENT)
  826. ret = PTR_ERR(di);
  827. goto out;
  828. } else if (di) {
  829. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  830. if (location.objectid != objectid)
  831. goto out;
  832. } else {
  833. goto out;
  834. }
  835. btrfs_release_path(path);
  836. di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
  837. if (IS_ERR(di)) {
  838. ret = PTR_ERR(di);
  839. goto out;
  840. } else if (di) {
  841. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  842. if (location.objectid == objectid)
  843. ret = 1;
  844. }
  845. out:
  846. btrfs_release_path(path);
  847. return ret;
  848. }
  849. /*
  850. * helper function to check a log tree for a named back reference in
  851. * an inode. This is used to decide if a back reference that is
  852. * found in the subvolume conflicts with what we find in the log.
  853. *
  854. * inode backreferences may have multiple refs in a single item,
  855. * during replay we process one reference at a time, and we don't
  856. * want to delete valid links to a file from the subvolume if that
  857. * link is also in the log.
  858. */
  859. static noinline int backref_in_log(struct btrfs_root *log,
  860. struct btrfs_key *key,
  861. u64 ref_objectid,
  862. const char *name, int namelen)
  863. {
  864. struct btrfs_path *path;
  865. int ret;
  866. path = btrfs_alloc_path();
  867. if (!path)
  868. return -ENOMEM;
  869. ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
  870. if (ret < 0) {
  871. goto out;
  872. } else if (ret == 1) {
  873. ret = 0;
  874. goto out;
  875. }
  876. if (key->type == BTRFS_INODE_EXTREF_KEY)
  877. ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
  878. path->slots[0],
  879. ref_objectid,
  880. name, namelen);
  881. else
  882. ret = !!btrfs_find_name_in_backref(path->nodes[0],
  883. path->slots[0],
  884. name, namelen);
  885. out:
  886. btrfs_free_path(path);
  887. return ret;
  888. }
  889. static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
  890. struct btrfs_root *root,
  891. struct btrfs_path *path,
  892. struct btrfs_root *log_root,
  893. struct btrfs_inode *dir,
  894. struct btrfs_inode *inode,
  895. u64 inode_objectid, u64 parent_objectid,
  896. u64 ref_index, char *name, int namelen,
  897. int *search_done)
  898. {
  899. int ret;
  900. char *victim_name;
  901. int victim_name_len;
  902. struct extent_buffer *leaf;
  903. struct btrfs_dir_item *di;
  904. struct btrfs_key search_key;
  905. struct btrfs_inode_extref *extref;
  906. again:
  907. /* Search old style refs */
  908. search_key.objectid = inode_objectid;
  909. search_key.type = BTRFS_INODE_REF_KEY;
  910. search_key.offset = parent_objectid;
  911. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  912. if (ret == 0) {
  913. struct btrfs_inode_ref *victim_ref;
  914. unsigned long ptr;
  915. unsigned long ptr_end;
  916. leaf = path->nodes[0];
  917. /* are we trying to overwrite a back ref for the root directory
  918. * if so, just jump out, we're done
  919. */
  920. if (search_key.objectid == search_key.offset)
  921. return 1;
  922. /* check all the names in this back reference to see
  923. * if they are in the log. if so, we allow them to stay
  924. * otherwise they must be unlinked as a conflict
  925. */
  926. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  927. ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
  928. while (ptr < ptr_end) {
  929. victim_ref = (struct btrfs_inode_ref *)ptr;
  930. victim_name_len = btrfs_inode_ref_name_len(leaf,
  931. victim_ref);
  932. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  933. if (!victim_name)
  934. return -ENOMEM;
  935. read_extent_buffer(leaf, victim_name,
  936. (unsigned long)(victim_ref + 1),
  937. victim_name_len);
  938. ret = backref_in_log(log_root, &search_key,
  939. parent_objectid, victim_name,
  940. victim_name_len);
  941. if (ret < 0) {
  942. kfree(victim_name);
  943. return ret;
  944. } else if (!ret) {
  945. inc_nlink(&inode->vfs_inode);
  946. btrfs_release_path(path);
  947. ret = btrfs_unlink_inode(trans, root, dir, inode,
  948. victim_name, victim_name_len);
  949. kfree(victim_name);
  950. if (ret)
  951. return ret;
  952. ret = btrfs_run_delayed_items(trans);
  953. if (ret)
  954. return ret;
  955. *search_done = 1;
  956. goto again;
  957. }
  958. kfree(victim_name);
  959. ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
  960. }
  961. /*
  962. * NOTE: we have searched root tree and checked the
  963. * corresponding ref, it does not need to check again.
  964. */
  965. *search_done = 1;
  966. }
  967. btrfs_release_path(path);
  968. /* Same search but for extended refs */
  969. extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
  970. inode_objectid, parent_objectid, 0,
  971. 0);
  972. if (!IS_ERR_OR_NULL(extref)) {
  973. u32 item_size;
  974. u32 cur_offset = 0;
  975. unsigned long base;
  976. struct inode *victim_parent;
  977. leaf = path->nodes[0];
  978. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  979. base = btrfs_item_ptr_offset(leaf, path->slots[0]);
  980. while (cur_offset < item_size) {
  981. extref = (struct btrfs_inode_extref *)(base + cur_offset);
  982. victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
  983. if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
  984. goto next;
  985. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  986. if (!victim_name)
  987. return -ENOMEM;
  988. read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
  989. victim_name_len);
  990. search_key.objectid = inode_objectid;
  991. search_key.type = BTRFS_INODE_EXTREF_KEY;
  992. search_key.offset = btrfs_extref_hash(parent_objectid,
  993. victim_name,
  994. victim_name_len);
  995. ret = backref_in_log(log_root, &search_key,
  996. parent_objectid, victim_name,
  997. victim_name_len);
  998. if (ret < 0) {
  999. kfree(victim_name);
  1000. return ret;
  1001. } else if (!ret) {
  1002. ret = -ENOENT;
  1003. victim_parent = read_one_inode(root,
  1004. parent_objectid);
  1005. if (victim_parent) {
  1006. inc_nlink(&inode->vfs_inode);
  1007. btrfs_release_path(path);
  1008. ret = btrfs_unlink_inode(trans, root,
  1009. BTRFS_I(victim_parent),
  1010. inode,
  1011. victim_name,
  1012. victim_name_len);
  1013. if (!ret)
  1014. ret = btrfs_run_delayed_items(
  1015. trans);
  1016. }
  1017. iput(victim_parent);
  1018. kfree(victim_name);
  1019. if (ret)
  1020. return ret;
  1021. *search_done = 1;
  1022. goto again;
  1023. }
  1024. kfree(victim_name);
  1025. next:
  1026. cur_offset += victim_name_len + sizeof(*extref);
  1027. }
  1028. *search_done = 1;
  1029. }
  1030. btrfs_release_path(path);
  1031. /* look for a conflicting sequence number */
  1032. di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
  1033. ref_index, name, namelen, 0);
  1034. if (IS_ERR(di)) {
  1035. if (PTR_ERR(di) != -ENOENT)
  1036. return PTR_ERR(di);
  1037. } else if (di) {
  1038. ret = drop_one_dir_item(trans, root, path, dir, di);
  1039. if (ret)
  1040. return ret;
  1041. }
  1042. btrfs_release_path(path);
  1043. /* look for a conflicting name */
  1044. di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
  1045. name, namelen, 0);
  1046. if (IS_ERR(di)) {
  1047. return PTR_ERR(di);
  1048. } else if (di) {
  1049. ret = drop_one_dir_item(trans, root, path, dir, di);
  1050. if (ret)
  1051. return ret;
  1052. }
  1053. btrfs_release_path(path);
  1054. return 0;
  1055. }
  1056. static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  1057. u32 *namelen, char **name, u64 *index,
  1058. u64 *parent_objectid)
  1059. {
  1060. struct btrfs_inode_extref *extref;
  1061. extref = (struct btrfs_inode_extref *)ref_ptr;
  1062. *namelen = btrfs_inode_extref_name_len(eb, extref);
  1063. *name = kmalloc(*namelen, GFP_NOFS);
  1064. if (*name == NULL)
  1065. return -ENOMEM;
  1066. read_extent_buffer(eb, *name, (unsigned long)&extref->name,
  1067. *namelen);
  1068. if (index)
  1069. *index = btrfs_inode_extref_index(eb, extref);
  1070. if (parent_objectid)
  1071. *parent_objectid = btrfs_inode_extref_parent(eb, extref);
  1072. return 0;
  1073. }
  1074. static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  1075. u32 *namelen, char **name, u64 *index)
  1076. {
  1077. struct btrfs_inode_ref *ref;
  1078. ref = (struct btrfs_inode_ref *)ref_ptr;
  1079. *namelen = btrfs_inode_ref_name_len(eb, ref);
  1080. *name = kmalloc(*namelen, GFP_NOFS);
  1081. if (*name == NULL)
  1082. return -ENOMEM;
  1083. read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
  1084. if (index)
  1085. *index = btrfs_inode_ref_index(eb, ref);
  1086. return 0;
  1087. }
  1088. /*
  1089. * Take an inode reference item from the log tree and iterate all names from the
  1090. * inode reference item in the subvolume tree with the same key (if it exists).
  1091. * For any name that is not in the inode reference item from the log tree, do a
  1092. * proper unlink of that name (that is, remove its entry from the inode
  1093. * reference item and both dir index keys).
  1094. */
  1095. static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
  1096. struct btrfs_root *root,
  1097. struct btrfs_path *path,
  1098. struct btrfs_inode *inode,
  1099. struct extent_buffer *log_eb,
  1100. int log_slot,
  1101. struct btrfs_key *key)
  1102. {
  1103. int ret;
  1104. unsigned long ref_ptr;
  1105. unsigned long ref_end;
  1106. struct extent_buffer *eb;
  1107. again:
  1108. btrfs_release_path(path);
  1109. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  1110. if (ret > 0) {
  1111. ret = 0;
  1112. goto out;
  1113. }
  1114. if (ret < 0)
  1115. goto out;
  1116. eb = path->nodes[0];
  1117. ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
  1118. ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
  1119. while (ref_ptr < ref_end) {
  1120. char *name = NULL;
  1121. int namelen;
  1122. u64 parent_id;
  1123. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1124. ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
  1125. NULL, &parent_id);
  1126. } else {
  1127. parent_id = key->offset;
  1128. ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
  1129. NULL);
  1130. }
  1131. if (ret)
  1132. goto out;
  1133. if (key->type == BTRFS_INODE_EXTREF_KEY)
  1134. ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot,
  1135. parent_id, name,
  1136. namelen);
  1137. else
  1138. ret = !!btrfs_find_name_in_backref(log_eb, log_slot,
  1139. name, namelen);
  1140. if (!ret) {
  1141. struct inode *dir;
  1142. btrfs_release_path(path);
  1143. dir = read_one_inode(root, parent_id);
  1144. if (!dir) {
  1145. ret = -ENOENT;
  1146. kfree(name);
  1147. goto out;
  1148. }
  1149. ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
  1150. inode, name, namelen);
  1151. kfree(name);
  1152. iput(dir);
  1153. /*
  1154. * Whenever we need to check if a name exists or not, we
  1155. * check the subvolume tree. So after an unlink we must
  1156. * run delayed items, so that future checks for a name
  1157. * during log replay see that the name does not exists
  1158. * anymore.
  1159. */
  1160. if (!ret)
  1161. ret = btrfs_run_delayed_items(trans);
  1162. if (ret)
  1163. goto out;
  1164. goto again;
  1165. }
  1166. kfree(name);
  1167. ref_ptr += namelen;
  1168. if (key->type == BTRFS_INODE_EXTREF_KEY)
  1169. ref_ptr += sizeof(struct btrfs_inode_extref);
  1170. else
  1171. ref_ptr += sizeof(struct btrfs_inode_ref);
  1172. }
  1173. ret = 0;
  1174. out:
  1175. btrfs_release_path(path);
  1176. return ret;
  1177. }
  1178. static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
  1179. const u8 ref_type, const char *name,
  1180. const int namelen)
  1181. {
  1182. struct btrfs_key key;
  1183. struct btrfs_path *path;
  1184. const u64 parent_id = btrfs_ino(BTRFS_I(dir));
  1185. int ret;
  1186. path = btrfs_alloc_path();
  1187. if (!path)
  1188. return -ENOMEM;
  1189. key.objectid = btrfs_ino(BTRFS_I(inode));
  1190. key.type = ref_type;
  1191. if (key.type == BTRFS_INODE_REF_KEY)
  1192. key.offset = parent_id;
  1193. else
  1194. key.offset = btrfs_extref_hash(parent_id, name, namelen);
  1195. ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
  1196. if (ret < 0)
  1197. goto out;
  1198. if (ret > 0) {
  1199. ret = 0;
  1200. goto out;
  1201. }
  1202. if (key.type == BTRFS_INODE_EXTREF_KEY)
  1203. ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
  1204. path->slots[0], parent_id, name, namelen);
  1205. else
  1206. ret = !!btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
  1207. name, namelen);
  1208. out:
  1209. btrfs_free_path(path);
  1210. return ret;
  1211. }
  1212. static int add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  1213. struct inode *dir, struct inode *inode, const char *name,
  1214. int namelen, u64 ref_index)
  1215. {
  1216. struct btrfs_dir_item *dir_item;
  1217. struct btrfs_key key;
  1218. struct btrfs_path *path;
  1219. struct inode *other_inode = NULL;
  1220. int ret;
  1221. path = btrfs_alloc_path();
  1222. if (!path)
  1223. return -ENOMEM;
  1224. dir_item = btrfs_lookup_dir_item(NULL, root, path,
  1225. btrfs_ino(BTRFS_I(dir)),
  1226. name, namelen, 0);
  1227. if (!dir_item) {
  1228. btrfs_release_path(path);
  1229. goto add_link;
  1230. } else if (IS_ERR(dir_item)) {
  1231. ret = PTR_ERR(dir_item);
  1232. goto out;
  1233. }
  1234. /*
  1235. * Our inode's dentry collides with the dentry of another inode which is
  1236. * in the log but not yet processed since it has a higher inode number.
  1237. * So delete that other dentry.
  1238. */
  1239. btrfs_dir_item_key_to_cpu(path->nodes[0], dir_item, &key);
  1240. btrfs_release_path(path);
  1241. other_inode = read_one_inode(root, key.objectid);
  1242. if (!other_inode) {
  1243. ret = -ENOENT;
  1244. goto out;
  1245. }
  1246. ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), BTRFS_I(other_inode),
  1247. name, namelen);
  1248. if (ret)
  1249. goto out;
  1250. /*
  1251. * If we dropped the link count to 0, bump it so that later the iput()
  1252. * on the inode will not free it. We will fixup the link count later.
  1253. */
  1254. if (other_inode->i_nlink == 0)
  1255. inc_nlink(other_inode);
  1256. ret = btrfs_run_delayed_items(trans);
  1257. if (ret)
  1258. goto out;
  1259. add_link:
  1260. ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
  1261. name, namelen, 0, ref_index);
  1262. out:
  1263. iput(other_inode);
  1264. btrfs_free_path(path);
  1265. return ret;
  1266. }
  1267. /*
  1268. * replay one inode back reference item found in the log tree.
  1269. * eb, slot and key refer to the buffer and key found in the log tree.
  1270. * root is the destination we are replaying into, and path is for temp
  1271. * use by this function. (it should be released on return).
  1272. */
  1273. static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
  1274. struct btrfs_root *root,
  1275. struct btrfs_root *log,
  1276. struct btrfs_path *path,
  1277. struct extent_buffer *eb, int slot,
  1278. struct btrfs_key *key)
  1279. {
  1280. struct inode *dir = NULL;
  1281. struct inode *inode = NULL;
  1282. unsigned long ref_ptr;
  1283. unsigned long ref_end;
  1284. char *name = NULL;
  1285. int namelen;
  1286. int ret;
  1287. int search_done = 0;
  1288. int log_ref_ver = 0;
  1289. u64 parent_objectid;
  1290. u64 inode_objectid;
  1291. u64 ref_index = 0;
  1292. int ref_struct_size;
  1293. ref_ptr = btrfs_item_ptr_offset(eb, slot);
  1294. ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
  1295. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1296. struct btrfs_inode_extref *r;
  1297. ref_struct_size = sizeof(struct btrfs_inode_extref);
  1298. log_ref_ver = 1;
  1299. r = (struct btrfs_inode_extref *)ref_ptr;
  1300. parent_objectid = btrfs_inode_extref_parent(eb, r);
  1301. } else {
  1302. ref_struct_size = sizeof(struct btrfs_inode_ref);
  1303. parent_objectid = key->offset;
  1304. }
  1305. inode_objectid = key->objectid;
  1306. /*
  1307. * it is possible that we didn't log all the parent directories
  1308. * for a given inode. If we don't find the dir, just don't
  1309. * copy the back ref in. The link count fixup code will take
  1310. * care of the rest
  1311. */
  1312. dir = read_one_inode(root, parent_objectid);
  1313. if (!dir) {
  1314. ret = -ENOENT;
  1315. goto out;
  1316. }
  1317. inode = read_one_inode(root, inode_objectid);
  1318. if (!inode) {
  1319. ret = -EIO;
  1320. goto out;
  1321. }
  1322. while (ref_ptr < ref_end) {
  1323. if (log_ref_ver) {
  1324. ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
  1325. &ref_index, &parent_objectid);
  1326. /*
  1327. * parent object can change from one array
  1328. * item to another.
  1329. */
  1330. if (!dir)
  1331. dir = read_one_inode(root, parent_objectid);
  1332. if (!dir) {
  1333. ret = -ENOENT;
  1334. goto out;
  1335. }
  1336. } else {
  1337. ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
  1338. &ref_index);
  1339. }
  1340. if (ret)
  1341. goto out;
  1342. ret = inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
  1343. btrfs_ino(BTRFS_I(inode)), ref_index,
  1344. name, namelen);
  1345. if (ret < 0) {
  1346. goto out;
  1347. } else if (ret == 0) {
  1348. /*
  1349. * look for a conflicting back reference in the
  1350. * metadata. if we find one we have to unlink that name
  1351. * of the file before we add our new link. Later on, we
  1352. * overwrite any existing back reference, and we don't
  1353. * want to create dangling pointers in the directory.
  1354. */
  1355. if (!search_done) {
  1356. ret = __add_inode_ref(trans, root, path, log,
  1357. BTRFS_I(dir),
  1358. BTRFS_I(inode),
  1359. inode_objectid,
  1360. parent_objectid,
  1361. ref_index, name, namelen,
  1362. &search_done);
  1363. if (ret) {
  1364. if (ret == 1)
  1365. ret = 0;
  1366. goto out;
  1367. }
  1368. }
  1369. /*
  1370. * If a reference item already exists for this inode
  1371. * with the same parent and name, but different index,
  1372. * drop it and the corresponding directory index entries
  1373. * from the parent before adding the new reference item
  1374. * and dir index entries, otherwise we would fail with
  1375. * -EEXIST returned from btrfs_add_link() below.
  1376. */
  1377. ret = btrfs_inode_ref_exists(inode, dir, key->type,
  1378. name, namelen);
  1379. if (ret > 0) {
  1380. ret = btrfs_unlink_inode(trans, root,
  1381. BTRFS_I(dir),
  1382. BTRFS_I(inode),
  1383. name, namelen);
  1384. /*
  1385. * If we dropped the link count to 0, bump it so
  1386. * that later the iput() on the inode will not
  1387. * free it. We will fixup the link count later.
  1388. */
  1389. if (!ret && inode->i_nlink == 0)
  1390. inc_nlink(inode);
  1391. /*
  1392. * Whenever we need to check if a name exists or
  1393. * not, we check the subvolume tree. So after an
  1394. * unlink we must run delayed items, so that future
  1395. * checks for a name during log replay see that the
  1396. * name does not exists anymore.
  1397. */
  1398. if (!ret)
  1399. ret = btrfs_run_delayed_items(trans);
  1400. }
  1401. if (ret < 0)
  1402. goto out;
  1403. /* insert our name */
  1404. ret = add_link(trans, root, dir, inode, name, namelen,
  1405. ref_index);
  1406. if (ret)
  1407. goto out;
  1408. btrfs_update_inode(trans, root, inode);
  1409. }
  1410. /* Else, ret == 1, we already have a perfect match, we're done. */
  1411. ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
  1412. kfree(name);
  1413. name = NULL;
  1414. if (log_ref_ver) {
  1415. iput(dir);
  1416. dir = NULL;
  1417. }
  1418. }
  1419. /*
  1420. * Before we overwrite the inode reference item in the subvolume tree
  1421. * with the item from the log tree, we must unlink all names from the
  1422. * parent directory that are in the subvolume's tree inode reference
  1423. * item, otherwise we end up with an inconsistent subvolume tree where
  1424. * dir index entries exist for a name but there is no inode reference
  1425. * item with the same name.
  1426. */
  1427. ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
  1428. key);
  1429. if (ret)
  1430. goto out;
  1431. /* finally write the back reference in the inode */
  1432. ret = overwrite_item(trans, root, path, eb, slot, key);
  1433. out:
  1434. btrfs_release_path(path);
  1435. kfree(name);
  1436. iput(dir);
  1437. iput(inode);
  1438. return ret;
  1439. }
  1440. static int insert_orphan_item(struct btrfs_trans_handle *trans,
  1441. struct btrfs_root *root, u64 ino)
  1442. {
  1443. int ret;
  1444. ret = btrfs_insert_orphan_item(trans, root, ino);
  1445. if (ret == -EEXIST)
  1446. ret = 0;
  1447. return ret;
  1448. }
  1449. static int count_inode_extrefs(struct btrfs_root *root,
  1450. struct btrfs_inode *inode, struct btrfs_path *path)
  1451. {
  1452. int ret = 0;
  1453. int name_len;
  1454. unsigned int nlink = 0;
  1455. u32 item_size;
  1456. u32 cur_offset = 0;
  1457. u64 inode_objectid = btrfs_ino(inode);
  1458. u64 offset = 0;
  1459. unsigned long ptr;
  1460. struct btrfs_inode_extref *extref;
  1461. struct extent_buffer *leaf;
  1462. while (1) {
  1463. ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
  1464. &extref, &offset);
  1465. if (ret)
  1466. break;
  1467. leaf = path->nodes[0];
  1468. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1469. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1470. cur_offset = 0;
  1471. while (cur_offset < item_size) {
  1472. extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
  1473. name_len = btrfs_inode_extref_name_len(leaf, extref);
  1474. nlink++;
  1475. cur_offset += name_len + sizeof(*extref);
  1476. }
  1477. offset++;
  1478. btrfs_release_path(path);
  1479. }
  1480. btrfs_release_path(path);
  1481. if (ret < 0 && ret != -ENOENT)
  1482. return ret;
  1483. return nlink;
  1484. }
  1485. static int count_inode_refs(struct btrfs_root *root,
  1486. struct btrfs_inode *inode, struct btrfs_path *path)
  1487. {
  1488. int ret;
  1489. struct btrfs_key key;
  1490. unsigned int nlink = 0;
  1491. unsigned long ptr;
  1492. unsigned long ptr_end;
  1493. int name_len;
  1494. u64 ino = btrfs_ino(inode);
  1495. key.objectid = ino;
  1496. key.type = BTRFS_INODE_REF_KEY;
  1497. key.offset = (u64)-1;
  1498. while (1) {
  1499. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1500. if (ret < 0)
  1501. break;
  1502. if (ret > 0) {
  1503. if (path->slots[0] == 0)
  1504. break;
  1505. path->slots[0]--;
  1506. }
  1507. process_slot:
  1508. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1509. path->slots[0]);
  1510. if (key.objectid != ino ||
  1511. key.type != BTRFS_INODE_REF_KEY)
  1512. break;
  1513. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  1514. ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
  1515. path->slots[0]);
  1516. while (ptr < ptr_end) {
  1517. struct btrfs_inode_ref *ref;
  1518. ref = (struct btrfs_inode_ref *)ptr;
  1519. name_len = btrfs_inode_ref_name_len(path->nodes[0],
  1520. ref);
  1521. ptr = (unsigned long)(ref + 1) + name_len;
  1522. nlink++;
  1523. }
  1524. if (key.offset == 0)
  1525. break;
  1526. if (path->slots[0] > 0) {
  1527. path->slots[0]--;
  1528. goto process_slot;
  1529. }
  1530. key.offset--;
  1531. btrfs_release_path(path);
  1532. }
  1533. btrfs_release_path(path);
  1534. return nlink;
  1535. }
  1536. /*
  1537. * There are a few corners where the link count of the file can't
  1538. * be properly maintained during replay. So, instead of adding
  1539. * lots of complexity to the log code, we just scan the backrefs
  1540. * for any file that has been through replay.
  1541. *
  1542. * The scan will update the link count on the inode to reflect the
  1543. * number of back refs found. If it goes down to zero, the iput
  1544. * will free the inode.
  1545. */
  1546. static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
  1547. struct btrfs_root *root,
  1548. struct inode *inode)
  1549. {
  1550. struct btrfs_path *path;
  1551. int ret;
  1552. u64 nlink = 0;
  1553. u64 ino = btrfs_ino(BTRFS_I(inode));
  1554. path = btrfs_alloc_path();
  1555. if (!path)
  1556. return -ENOMEM;
  1557. ret = count_inode_refs(root, BTRFS_I(inode), path);
  1558. if (ret < 0)
  1559. goto out;
  1560. nlink = ret;
  1561. ret = count_inode_extrefs(root, BTRFS_I(inode), path);
  1562. if (ret < 0)
  1563. goto out;
  1564. nlink += ret;
  1565. ret = 0;
  1566. if (nlink != inode->i_nlink) {
  1567. set_nlink(inode, nlink);
  1568. btrfs_update_inode(trans, root, inode);
  1569. }
  1570. BTRFS_I(inode)->index_cnt = (u64)-1;
  1571. if (inode->i_nlink == 0) {
  1572. if (S_ISDIR(inode->i_mode)) {
  1573. ret = replay_dir_deletes(trans, root, NULL, path,
  1574. ino, 1);
  1575. if (ret)
  1576. goto out;
  1577. }
  1578. ret = insert_orphan_item(trans, root, ino);
  1579. }
  1580. out:
  1581. btrfs_free_path(path);
  1582. return ret;
  1583. }
  1584. static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
  1585. struct btrfs_root *root,
  1586. struct btrfs_path *path)
  1587. {
  1588. int ret;
  1589. struct btrfs_key key;
  1590. struct inode *inode;
  1591. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1592. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1593. key.offset = (u64)-1;
  1594. while (1) {
  1595. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1596. if (ret < 0)
  1597. break;
  1598. if (ret == 1) {
  1599. ret = 0;
  1600. if (path->slots[0] == 0)
  1601. break;
  1602. path->slots[0]--;
  1603. }
  1604. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1605. if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
  1606. key.type != BTRFS_ORPHAN_ITEM_KEY)
  1607. break;
  1608. ret = btrfs_del_item(trans, root, path);
  1609. if (ret)
  1610. break;
  1611. btrfs_release_path(path);
  1612. inode = read_one_inode(root, key.offset);
  1613. if (!inode) {
  1614. ret = -EIO;
  1615. break;
  1616. }
  1617. ret = fixup_inode_link_count(trans, root, inode);
  1618. iput(inode);
  1619. if (ret)
  1620. break;
  1621. /*
  1622. * fixup on a directory may create new entries,
  1623. * make sure we always look for the highset possible
  1624. * offset
  1625. */
  1626. key.offset = (u64)-1;
  1627. }
  1628. btrfs_release_path(path);
  1629. return ret;
  1630. }
  1631. /*
  1632. * record a given inode in the fixup dir so we can check its link
  1633. * count when replay is done. The link count is incremented here
  1634. * so the inode won't go away until we check it
  1635. */
  1636. static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  1637. struct btrfs_root *root,
  1638. struct btrfs_path *path,
  1639. u64 objectid)
  1640. {
  1641. struct btrfs_key key;
  1642. int ret = 0;
  1643. struct inode *inode;
  1644. inode = read_one_inode(root, objectid);
  1645. if (!inode)
  1646. return -EIO;
  1647. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1648. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1649. key.offset = objectid;
  1650. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  1651. btrfs_release_path(path);
  1652. if (ret == 0) {
  1653. if (!inode->i_nlink)
  1654. set_nlink(inode, 1);
  1655. else
  1656. inc_nlink(inode);
  1657. ret = btrfs_update_inode(trans, root, inode);
  1658. } else if (ret == -EEXIST) {
  1659. ret = 0;
  1660. }
  1661. iput(inode);
  1662. return ret;
  1663. }
  1664. /*
  1665. * when replaying the log for a directory, we only insert names
  1666. * for inodes that actually exist. This means an fsync on a directory
  1667. * does not implicitly fsync all the new files in it
  1668. */
  1669. static noinline int insert_one_name(struct btrfs_trans_handle *trans,
  1670. struct btrfs_root *root,
  1671. u64 dirid, u64 index,
  1672. char *name, int name_len,
  1673. struct btrfs_key *location)
  1674. {
  1675. struct inode *inode;
  1676. struct inode *dir;
  1677. int ret;
  1678. inode = read_one_inode(root, location->objectid);
  1679. if (!inode)
  1680. return -ENOENT;
  1681. dir = read_one_inode(root, dirid);
  1682. if (!dir) {
  1683. iput(inode);
  1684. return -EIO;
  1685. }
  1686. ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
  1687. name_len, 1, index);
  1688. /* FIXME, put inode into FIXUP list */
  1689. iput(inode);
  1690. iput(dir);
  1691. return ret;
  1692. }
  1693. /*
  1694. * take a single entry in a log directory item and replay it into
  1695. * the subvolume.
  1696. *
  1697. * if a conflicting item exists in the subdirectory already,
  1698. * the inode it points to is unlinked and put into the link count
  1699. * fix up tree.
  1700. *
  1701. * If a name from the log points to a file or directory that does
  1702. * not exist in the FS, it is skipped. fsyncs on directories
  1703. * do not force down inodes inside that directory, just changes to the
  1704. * names or unlinks in a directory.
  1705. *
  1706. * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
  1707. * non-existing inode) and 1 if the name was replayed.
  1708. */
  1709. static noinline int replay_one_name(struct btrfs_trans_handle *trans,
  1710. struct btrfs_root *root,
  1711. struct btrfs_path *path,
  1712. struct extent_buffer *eb,
  1713. struct btrfs_dir_item *di,
  1714. struct btrfs_key *key)
  1715. {
  1716. char *name;
  1717. int name_len;
  1718. struct btrfs_dir_item *dst_di;
  1719. struct btrfs_key found_key;
  1720. struct btrfs_key log_key;
  1721. struct inode *dir;
  1722. u8 log_type;
  1723. bool exists;
  1724. int ret;
  1725. bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
  1726. bool name_added = false;
  1727. dir = read_one_inode(root, key->objectid);
  1728. if (!dir)
  1729. return -EIO;
  1730. name_len = btrfs_dir_name_len(eb, di);
  1731. name = kmalloc(name_len, GFP_NOFS);
  1732. if (!name) {
  1733. ret = -ENOMEM;
  1734. goto out;
  1735. }
  1736. log_type = btrfs_dir_type(eb, di);
  1737. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1738. name_len);
  1739. btrfs_dir_item_key_to_cpu(eb, di, &log_key);
  1740. ret = btrfs_lookup_inode(trans, root, path, &log_key, 0);
  1741. btrfs_release_path(path);
  1742. if (ret < 0)
  1743. goto out;
  1744. exists = (ret == 0);
  1745. ret = 0;
  1746. if (key->type == BTRFS_DIR_ITEM_KEY) {
  1747. dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
  1748. name, name_len, 1);
  1749. } else if (key->type == BTRFS_DIR_INDEX_KEY) {
  1750. dst_di = btrfs_lookup_dir_index_item(trans, root, path,
  1751. key->objectid,
  1752. key->offset, name,
  1753. name_len, 1);
  1754. } else {
  1755. /* Corruption */
  1756. ret = -EINVAL;
  1757. goto out;
  1758. }
  1759. if (dst_di == ERR_PTR(-ENOENT))
  1760. dst_di = NULL;
  1761. if (IS_ERR(dst_di)) {
  1762. ret = PTR_ERR(dst_di);
  1763. goto out;
  1764. } else if (!dst_di) {
  1765. /* we need a sequence number to insert, so we only
  1766. * do inserts for the BTRFS_DIR_INDEX_KEY types
  1767. */
  1768. if (key->type != BTRFS_DIR_INDEX_KEY)
  1769. goto out;
  1770. goto insert;
  1771. }
  1772. btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
  1773. /* the existing item matches the logged item */
  1774. if (found_key.objectid == log_key.objectid &&
  1775. found_key.type == log_key.type &&
  1776. found_key.offset == log_key.offset &&
  1777. btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
  1778. update_size = false;
  1779. goto out;
  1780. }
  1781. /*
  1782. * don't drop the conflicting directory entry if the inode
  1783. * for the new entry doesn't exist
  1784. */
  1785. if (!exists)
  1786. goto out;
  1787. ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
  1788. if (ret)
  1789. goto out;
  1790. if (key->type == BTRFS_DIR_INDEX_KEY)
  1791. goto insert;
  1792. out:
  1793. btrfs_release_path(path);
  1794. if (!ret && update_size) {
  1795. btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
  1796. ret = btrfs_update_inode(trans, root, dir);
  1797. }
  1798. kfree(name);
  1799. iput(dir);
  1800. if (!ret && name_added)
  1801. ret = 1;
  1802. return ret;
  1803. insert:
  1804. /*
  1805. * Check if the inode reference exists in the log for the given name,
  1806. * inode and parent inode
  1807. */
  1808. found_key.objectid = log_key.objectid;
  1809. found_key.type = BTRFS_INODE_REF_KEY;
  1810. found_key.offset = key->objectid;
  1811. ret = backref_in_log(root->log_root, &found_key, 0, name, name_len);
  1812. if (ret < 0) {
  1813. goto out;
  1814. } else if (ret) {
  1815. /* The dentry will be added later. */
  1816. ret = 0;
  1817. update_size = false;
  1818. goto out;
  1819. }
  1820. found_key.objectid = log_key.objectid;
  1821. found_key.type = BTRFS_INODE_EXTREF_KEY;
  1822. found_key.offset = key->objectid;
  1823. ret = backref_in_log(root->log_root, &found_key, key->objectid, name,
  1824. name_len);
  1825. if (ret < 0) {
  1826. goto out;
  1827. } else if (ret) {
  1828. /* The dentry will be added later. */
  1829. ret = 0;
  1830. update_size = false;
  1831. goto out;
  1832. }
  1833. btrfs_release_path(path);
  1834. ret = insert_one_name(trans, root, key->objectid, key->offset,
  1835. name, name_len, &log_key);
  1836. if (ret && ret != -ENOENT && ret != -EEXIST)
  1837. goto out;
  1838. if (!ret)
  1839. name_added = true;
  1840. update_size = false;
  1841. ret = 0;
  1842. goto out;
  1843. }
  1844. /*
  1845. * find all the names in a directory item and reconcile them into
  1846. * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
  1847. * one name in a directory item, but the same code gets used for
  1848. * both directory index types
  1849. */
  1850. static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
  1851. struct btrfs_root *root,
  1852. struct btrfs_path *path,
  1853. struct extent_buffer *eb, int slot,
  1854. struct btrfs_key *key)
  1855. {
  1856. int ret = 0;
  1857. u32 item_size = btrfs_item_size_nr(eb, slot);
  1858. struct btrfs_dir_item *di;
  1859. int name_len;
  1860. unsigned long ptr;
  1861. unsigned long ptr_end;
  1862. struct btrfs_path *fixup_path = NULL;
  1863. ptr = btrfs_item_ptr_offset(eb, slot);
  1864. ptr_end = ptr + item_size;
  1865. while (ptr < ptr_end) {
  1866. di = (struct btrfs_dir_item *)ptr;
  1867. name_len = btrfs_dir_name_len(eb, di);
  1868. ret = replay_one_name(trans, root, path, eb, di, key);
  1869. if (ret < 0)
  1870. break;
  1871. ptr = (unsigned long)(di + 1);
  1872. ptr += name_len;
  1873. /*
  1874. * If this entry refers to a non-directory (directories can not
  1875. * have a link count > 1) and it was added in the transaction
  1876. * that was not committed, make sure we fixup the link count of
  1877. * the inode it the entry points to. Otherwise something like
  1878. * the following would result in a directory pointing to an
  1879. * inode with a wrong link that does not account for this dir
  1880. * entry:
  1881. *
  1882. * mkdir testdir
  1883. * touch testdir/foo
  1884. * touch testdir/bar
  1885. * sync
  1886. *
  1887. * ln testdir/bar testdir/bar_link
  1888. * ln testdir/foo testdir/foo_link
  1889. * xfs_io -c "fsync" testdir/bar
  1890. *
  1891. * <power failure>
  1892. *
  1893. * mount fs, log replay happens
  1894. *
  1895. * File foo would remain with a link count of 1 when it has two
  1896. * entries pointing to it in the directory testdir. This would
  1897. * make it impossible to ever delete the parent directory has
  1898. * it would result in stale dentries that can never be deleted.
  1899. */
  1900. if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
  1901. struct btrfs_key di_key;
  1902. if (!fixup_path) {
  1903. fixup_path = btrfs_alloc_path();
  1904. if (!fixup_path) {
  1905. ret = -ENOMEM;
  1906. break;
  1907. }
  1908. }
  1909. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  1910. ret = link_to_fixup_dir(trans, root, fixup_path,
  1911. di_key.objectid);
  1912. if (ret)
  1913. break;
  1914. }
  1915. ret = 0;
  1916. }
  1917. btrfs_free_path(fixup_path);
  1918. return ret;
  1919. }
  1920. /*
  1921. * directory replay has two parts. There are the standard directory
  1922. * items in the log copied from the subvolume, and range items
  1923. * created in the log while the subvolume was logged.
  1924. *
  1925. * The range items tell us which parts of the key space the log
  1926. * is authoritative for. During replay, if a key in the subvolume
  1927. * directory is in a logged range item, but not actually in the log
  1928. * that means it was deleted from the directory before the fsync
  1929. * and should be removed.
  1930. */
  1931. static noinline int find_dir_range(struct btrfs_root *root,
  1932. struct btrfs_path *path,
  1933. u64 dirid, int key_type,
  1934. u64 *start_ret, u64 *end_ret)
  1935. {
  1936. struct btrfs_key key;
  1937. u64 found_end;
  1938. struct btrfs_dir_log_item *item;
  1939. int ret;
  1940. int nritems;
  1941. if (*start_ret == (u64)-1)
  1942. return 1;
  1943. key.objectid = dirid;
  1944. key.type = key_type;
  1945. key.offset = *start_ret;
  1946. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1947. if (ret < 0)
  1948. goto out;
  1949. if (ret > 0) {
  1950. if (path->slots[0] == 0)
  1951. goto out;
  1952. path->slots[0]--;
  1953. }
  1954. if (ret != 0)
  1955. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1956. if (key.type != key_type || key.objectid != dirid) {
  1957. ret = 1;
  1958. goto next;
  1959. }
  1960. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1961. struct btrfs_dir_log_item);
  1962. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1963. if (*start_ret >= key.offset && *start_ret <= found_end) {
  1964. ret = 0;
  1965. *start_ret = key.offset;
  1966. *end_ret = found_end;
  1967. goto out;
  1968. }
  1969. ret = 1;
  1970. next:
  1971. /* check the next slot in the tree to see if it is a valid item */
  1972. nritems = btrfs_header_nritems(path->nodes[0]);
  1973. path->slots[0]++;
  1974. if (path->slots[0] >= nritems) {
  1975. ret = btrfs_next_leaf(root, path);
  1976. if (ret)
  1977. goto out;
  1978. }
  1979. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1980. if (key.type != key_type || key.objectid != dirid) {
  1981. ret = 1;
  1982. goto out;
  1983. }
  1984. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1985. struct btrfs_dir_log_item);
  1986. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1987. *start_ret = key.offset;
  1988. *end_ret = found_end;
  1989. ret = 0;
  1990. out:
  1991. btrfs_release_path(path);
  1992. return ret;
  1993. }
  1994. /*
  1995. * this looks for a given directory item in the log. If the directory
  1996. * item is not in the log, the item is removed and the inode it points
  1997. * to is unlinked
  1998. */
  1999. static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
  2000. struct btrfs_root *root,
  2001. struct btrfs_root *log,
  2002. struct btrfs_path *path,
  2003. struct btrfs_path *log_path,
  2004. struct inode *dir,
  2005. struct btrfs_key *dir_key)
  2006. {
  2007. int ret;
  2008. struct extent_buffer *eb;
  2009. int slot;
  2010. u32 item_size;
  2011. struct btrfs_dir_item *di;
  2012. struct btrfs_dir_item *log_di;
  2013. int name_len;
  2014. unsigned long ptr;
  2015. unsigned long ptr_end;
  2016. char *name;
  2017. struct inode *inode;
  2018. struct btrfs_key location;
  2019. again:
  2020. eb = path->nodes[0];
  2021. slot = path->slots[0];
  2022. item_size = btrfs_item_size_nr(eb, slot);
  2023. ptr = btrfs_item_ptr_offset(eb, slot);
  2024. ptr_end = ptr + item_size;
  2025. while (ptr < ptr_end) {
  2026. di = (struct btrfs_dir_item *)ptr;
  2027. name_len = btrfs_dir_name_len(eb, di);
  2028. name = kmalloc(name_len, GFP_NOFS);
  2029. if (!name) {
  2030. ret = -ENOMEM;
  2031. goto out;
  2032. }
  2033. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  2034. name_len);
  2035. log_di = NULL;
  2036. if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
  2037. log_di = btrfs_lookup_dir_item(trans, log, log_path,
  2038. dir_key->objectid,
  2039. name, name_len, 0);
  2040. } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
  2041. log_di = btrfs_lookup_dir_index_item(trans, log,
  2042. log_path,
  2043. dir_key->objectid,
  2044. dir_key->offset,
  2045. name, name_len, 0);
  2046. }
  2047. if (!log_di || log_di == ERR_PTR(-ENOENT)) {
  2048. btrfs_dir_item_key_to_cpu(eb, di, &location);
  2049. btrfs_release_path(path);
  2050. btrfs_release_path(log_path);
  2051. inode = read_one_inode(root, location.objectid);
  2052. if (!inode) {
  2053. kfree(name);
  2054. return -EIO;
  2055. }
  2056. ret = link_to_fixup_dir(trans, root,
  2057. path, location.objectid);
  2058. if (ret) {
  2059. kfree(name);
  2060. iput(inode);
  2061. goto out;
  2062. }
  2063. inc_nlink(inode);
  2064. ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
  2065. BTRFS_I(inode), name, name_len);
  2066. if (!ret)
  2067. ret = btrfs_run_delayed_items(trans);
  2068. kfree(name);
  2069. iput(inode);
  2070. if (ret)
  2071. goto out;
  2072. /* there might still be more names under this key
  2073. * check and repeat if required
  2074. */
  2075. ret = btrfs_search_slot(NULL, root, dir_key, path,
  2076. 0, 0);
  2077. if (ret == 0)
  2078. goto again;
  2079. ret = 0;
  2080. goto out;
  2081. } else if (IS_ERR(log_di)) {
  2082. kfree(name);
  2083. return PTR_ERR(log_di);
  2084. }
  2085. btrfs_release_path(log_path);
  2086. kfree(name);
  2087. ptr = (unsigned long)(di + 1);
  2088. ptr += name_len;
  2089. }
  2090. ret = 0;
  2091. out:
  2092. btrfs_release_path(path);
  2093. btrfs_release_path(log_path);
  2094. return ret;
  2095. }
  2096. static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
  2097. struct btrfs_root *root,
  2098. struct btrfs_root *log,
  2099. struct btrfs_path *path,
  2100. const u64 ino)
  2101. {
  2102. struct btrfs_key search_key;
  2103. struct btrfs_path *log_path;
  2104. int i;
  2105. int nritems;
  2106. int ret;
  2107. log_path = btrfs_alloc_path();
  2108. if (!log_path)
  2109. return -ENOMEM;
  2110. search_key.objectid = ino;
  2111. search_key.type = BTRFS_XATTR_ITEM_KEY;
  2112. search_key.offset = 0;
  2113. again:
  2114. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  2115. if (ret < 0)
  2116. goto out;
  2117. process_leaf:
  2118. nritems = btrfs_header_nritems(path->nodes[0]);
  2119. for (i = path->slots[0]; i < nritems; i++) {
  2120. struct btrfs_key key;
  2121. struct btrfs_dir_item *di;
  2122. struct btrfs_dir_item *log_di;
  2123. u32 total_size;
  2124. u32 cur;
  2125. btrfs_item_key_to_cpu(path->nodes[0], &key, i);
  2126. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
  2127. ret = 0;
  2128. goto out;
  2129. }
  2130. di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
  2131. total_size = btrfs_item_size_nr(path->nodes[0], i);
  2132. cur = 0;
  2133. while (cur < total_size) {
  2134. u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
  2135. u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
  2136. u32 this_len = sizeof(*di) + name_len + data_len;
  2137. char *name;
  2138. name = kmalloc(name_len, GFP_NOFS);
  2139. if (!name) {
  2140. ret = -ENOMEM;
  2141. goto out;
  2142. }
  2143. read_extent_buffer(path->nodes[0], name,
  2144. (unsigned long)(di + 1), name_len);
  2145. log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
  2146. name, name_len, 0);
  2147. btrfs_release_path(log_path);
  2148. if (!log_di) {
  2149. /* Doesn't exist in log tree, so delete it. */
  2150. btrfs_release_path(path);
  2151. di = btrfs_lookup_xattr(trans, root, path, ino,
  2152. name, name_len, -1);
  2153. kfree(name);
  2154. if (IS_ERR(di)) {
  2155. ret = PTR_ERR(di);
  2156. goto out;
  2157. }
  2158. ASSERT(di);
  2159. ret = btrfs_delete_one_dir_name(trans, root,
  2160. path, di);
  2161. if (ret)
  2162. goto out;
  2163. btrfs_release_path(path);
  2164. search_key = key;
  2165. goto again;
  2166. }
  2167. kfree(name);
  2168. if (IS_ERR(log_di)) {
  2169. ret = PTR_ERR(log_di);
  2170. goto out;
  2171. }
  2172. cur += this_len;
  2173. di = (struct btrfs_dir_item *)((char *)di + this_len);
  2174. }
  2175. }
  2176. ret = btrfs_next_leaf(root, path);
  2177. if (ret > 0)
  2178. ret = 0;
  2179. else if (ret == 0)
  2180. goto process_leaf;
  2181. out:
  2182. btrfs_free_path(log_path);
  2183. btrfs_release_path(path);
  2184. return ret;
  2185. }
  2186. /*
  2187. * deletion replay happens before we copy any new directory items
  2188. * out of the log or out of backreferences from inodes. It
  2189. * scans the log to find ranges of keys that log is authoritative for,
  2190. * and then scans the directory to find items in those ranges that are
  2191. * not present in the log.
  2192. *
  2193. * Anything we don't find in the log is unlinked and removed from the
  2194. * directory.
  2195. */
  2196. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  2197. struct btrfs_root *root,
  2198. struct btrfs_root *log,
  2199. struct btrfs_path *path,
  2200. u64 dirid, int del_all)
  2201. {
  2202. u64 range_start;
  2203. u64 range_end;
  2204. int key_type = BTRFS_DIR_LOG_ITEM_KEY;
  2205. int ret = 0;
  2206. struct btrfs_key dir_key;
  2207. struct btrfs_key found_key;
  2208. struct btrfs_path *log_path;
  2209. struct inode *dir;
  2210. dir_key.objectid = dirid;
  2211. dir_key.type = BTRFS_DIR_ITEM_KEY;
  2212. log_path = btrfs_alloc_path();
  2213. if (!log_path)
  2214. return -ENOMEM;
  2215. dir = read_one_inode(root, dirid);
  2216. /* it isn't an error if the inode isn't there, that can happen
  2217. * because we replay the deletes before we copy in the inode item
  2218. * from the log
  2219. */
  2220. if (!dir) {
  2221. btrfs_free_path(log_path);
  2222. return 0;
  2223. }
  2224. again:
  2225. range_start = 0;
  2226. range_end = 0;
  2227. while (1) {
  2228. if (del_all)
  2229. range_end = (u64)-1;
  2230. else {
  2231. ret = find_dir_range(log, path, dirid, key_type,
  2232. &range_start, &range_end);
  2233. if (ret < 0)
  2234. goto out;
  2235. else if (ret > 0)
  2236. break;
  2237. }
  2238. dir_key.offset = range_start;
  2239. while (1) {
  2240. int nritems;
  2241. ret = btrfs_search_slot(NULL, root, &dir_key, path,
  2242. 0, 0);
  2243. if (ret < 0)
  2244. goto out;
  2245. nritems = btrfs_header_nritems(path->nodes[0]);
  2246. if (path->slots[0] >= nritems) {
  2247. ret = btrfs_next_leaf(root, path);
  2248. if (ret == 1)
  2249. break;
  2250. else if (ret < 0)
  2251. goto out;
  2252. }
  2253. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2254. path->slots[0]);
  2255. if (found_key.objectid != dirid ||
  2256. found_key.type != dir_key.type)
  2257. goto next_type;
  2258. if (found_key.offset > range_end)
  2259. break;
  2260. ret = check_item_in_log(trans, root, log, path,
  2261. log_path, dir,
  2262. &found_key);
  2263. if (ret)
  2264. goto out;
  2265. if (found_key.offset == (u64)-1)
  2266. break;
  2267. dir_key.offset = found_key.offset + 1;
  2268. }
  2269. btrfs_release_path(path);
  2270. if (range_end == (u64)-1)
  2271. break;
  2272. range_start = range_end + 1;
  2273. }
  2274. next_type:
  2275. ret = 0;
  2276. if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
  2277. key_type = BTRFS_DIR_LOG_INDEX_KEY;
  2278. dir_key.type = BTRFS_DIR_INDEX_KEY;
  2279. btrfs_release_path(path);
  2280. goto again;
  2281. }
  2282. out:
  2283. btrfs_release_path(path);
  2284. btrfs_free_path(log_path);
  2285. iput(dir);
  2286. return ret;
  2287. }
  2288. /*
  2289. * the process_func used to replay items from the log tree. This
  2290. * gets called in two different stages. The first stage just looks
  2291. * for inodes and makes sure they are all copied into the subvolume.
  2292. *
  2293. * The second stage copies all the other item types from the log into
  2294. * the subvolume. The two stage approach is slower, but gets rid of
  2295. * lots of complexity around inodes referencing other inodes that exist
  2296. * only in the log (references come from either directory items or inode
  2297. * back refs).
  2298. */
  2299. static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
  2300. struct walk_control *wc, u64 gen, int level)
  2301. {
  2302. int nritems;
  2303. struct btrfs_path *path;
  2304. struct btrfs_root *root = wc->replay_dest;
  2305. struct btrfs_key key;
  2306. int i;
  2307. int ret;
  2308. ret = btrfs_read_buffer(eb, gen, level, NULL);
  2309. if (ret)
  2310. return ret;
  2311. level = btrfs_header_level(eb);
  2312. if (level != 0)
  2313. return 0;
  2314. path = btrfs_alloc_path();
  2315. if (!path)
  2316. return -ENOMEM;
  2317. nritems = btrfs_header_nritems(eb);
  2318. for (i = 0; i < nritems; i++) {
  2319. btrfs_item_key_to_cpu(eb, &key, i);
  2320. /* inode keys are done during the first stage */
  2321. if (key.type == BTRFS_INODE_ITEM_KEY &&
  2322. wc->stage == LOG_WALK_REPLAY_INODES) {
  2323. struct btrfs_inode_item *inode_item;
  2324. u32 mode;
  2325. inode_item = btrfs_item_ptr(eb, i,
  2326. struct btrfs_inode_item);
  2327. /*
  2328. * If we have a tmpfile (O_TMPFILE) that got fsync'ed
  2329. * and never got linked before the fsync, skip it, as
  2330. * replaying it is pointless since it would be deleted
  2331. * later. We skip logging tmpfiles, but it's always
  2332. * possible we are replaying a log created with a kernel
  2333. * that used to log tmpfiles.
  2334. */
  2335. if (btrfs_inode_nlink(eb, inode_item) == 0) {
  2336. wc->ignore_cur_inode = true;
  2337. continue;
  2338. } else {
  2339. wc->ignore_cur_inode = false;
  2340. }
  2341. ret = replay_xattr_deletes(wc->trans, root, log,
  2342. path, key.objectid);
  2343. if (ret)
  2344. break;
  2345. mode = btrfs_inode_mode(eb, inode_item);
  2346. if (S_ISDIR(mode)) {
  2347. ret = replay_dir_deletes(wc->trans,
  2348. root, log, path, key.objectid, 0);
  2349. if (ret)
  2350. break;
  2351. }
  2352. ret = overwrite_item(wc->trans, root, path,
  2353. eb, i, &key);
  2354. if (ret)
  2355. break;
  2356. /*
  2357. * Before replaying extents, truncate the inode to its
  2358. * size. We need to do it now and not after log replay
  2359. * because before an fsync we can have prealloc extents
  2360. * added beyond the inode's i_size. If we did it after,
  2361. * through orphan cleanup for example, we would drop
  2362. * those prealloc extents just after replaying them.
  2363. */
  2364. if (S_ISREG(mode)) {
  2365. struct inode *inode;
  2366. u64 from;
  2367. inode = read_one_inode(root, key.objectid);
  2368. if (!inode) {
  2369. ret = -EIO;
  2370. break;
  2371. }
  2372. from = ALIGN(i_size_read(inode),
  2373. root->fs_info->sectorsize);
  2374. ret = btrfs_drop_extents(wc->trans, root, inode,
  2375. from, (u64)-1, 1);
  2376. if (!ret) {
  2377. /* Update the inode's nbytes. */
  2378. ret = btrfs_update_inode(wc->trans,
  2379. root, inode);
  2380. }
  2381. iput(inode);
  2382. if (ret)
  2383. break;
  2384. }
  2385. ret = link_to_fixup_dir(wc->trans, root,
  2386. path, key.objectid);
  2387. if (ret)
  2388. break;
  2389. }
  2390. if (wc->ignore_cur_inode)
  2391. continue;
  2392. if (key.type == BTRFS_DIR_INDEX_KEY &&
  2393. wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
  2394. ret = replay_one_dir_item(wc->trans, root, path,
  2395. eb, i, &key);
  2396. if (ret)
  2397. break;
  2398. }
  2399. if (wc->stage < LOG_WALK_REPLAY_ALL)
  2400. continue;
  2401. /* these keys are simply copied */
  2402. if (key.type == BTRFS_XATTR_ITEM_KEY) {
  2403. ret = overwrite_item(wc->trans, root, path,
  2404. eb, i, &key);
  2405. if (ret)
  2406. break;
  2407. } else if (key.type == BTRFS_INODE_REF_KEY ||
  2408. key.type == BTRFS_INODE_EXTREF_KEY) {
  2409. ret = add_inode_ref(wc->trans, root, log, path,
  2410. eb, i, &key);
  2411. if (ret && ret != -ENOENT)
  2412. break;
  2413. ret = 0;
  2414. } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
  2415. ret = replay_one_extent(wc->trans, root, path,
  2416. eb, i, &key);
  2417. if (ret)
  2418. break;
  2419. } else if (key.type == BTRFS_DIR_ITEM_KEY) {
  2420. ret = replay_one_dir_item(wc->trans, root, path,
  2421. eb, i, &key);
  2422. if (ret)
  2423. break;
  2424. }
  2425. }
  2426. btrfs_free_path(path);
  2427. return ret;
  2428. }
  2429. /*
  2430. * Correctly adjust the reserved bytes occupied by a log tree extent buffer
  2431. */
  2432. static void unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start)
  2433. {
  2434. struct btrfs_block_group *cache;
  2435. cache = btrfs_lookup_block_group(fs_info, start);
  2436. if (!cache) {
  2437. btrfs_err(fs_info, "unable to find block group for %llu", start);
  2438. return;
  2439. }
  2440. spin_lock(&cache->space_info->lock);
  2441. spin_lock(&cache->lock);
  2442. cache->reserved -= fs_info->nodesize;
  2443. cache->space_info->bytes_reserved -= fs_info->nodesize;
  2444. spin_unlock(&cache->lock);
  2445. spin_unlock(&cache->space_info->lock);
  2446. btrfs_put_block_group(cache);
  2447. }
  2448. static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
  2449. struct btrfs_root *root,
  2450. struct btrfs_path *path, int *level,
  2451. struct walk_control *wc)
  2452. {
  2453. struct btrfs_fs_info *fs_info = root->fs_info;
  2454. u64 bytenr;
  2455. u64 ptr_gen;
  2456. struct extent_buffer *next;
  2457. struct extent_buffer *cur;
  2458. u32 blocksize;
  2459. int ret = 0;
  2460. while (*level > 0) {
  2461. struct btrfs_key first_key;
  2462. cur = path->nodes[*level];
  2463. WARN_ON(btrfs_header_level(cur) != *level);
  2464. if (path->slots[*level] >=
  2465. btrfs_header_nritems(cur))
  2466. break;
  2467. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  2468. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  2469. btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
  2470. blocksize = fs_info->nodesize;
  2471. next = btrfs_find_create_tree_block(fs_info, bytenr);
  2472. if (IS_ERR(next))
  2473. return PTR_ERR(next);
  2474. if (*level == 1) {
  2475. ret = wc->process_func(root, next, wc, ptr_gen,
  2476. *level - 1);
  2477. if (ret) {
  2478. free_extent_buffer(next);
  2479. return ret;
  2480. }
  2481. path->slots[*level]++;
  2482. if (wc->free) {
  2483. ret = btrfs_read_buffer(next, ptr_gen,
  2484. *level - 1, &first_key);
  2485. if (ret) {
  2486. free_extent_buffer(next);
  2487. return ret;
  2488. }
  2489. if (trans) {
  2490. btrfs_tree_lock(next);
  2491. btrfs_set_lock_blocking_write(next);
  2492. btrfs_clean_tree_block(next);
  2493. btrfs_wait_tree_block_writeback(next);
  2494. btrfs_tree_unlock(next);
  2495. ret = btrfs_pin_reserved_extent(trans,
  2496. bytenr, blocksize);
  2497. if (ret) {
  2498. free_extent_buffer(next);
  2499. return ret;
  2500. }
  2501. } else {
  2502. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
  2503. clear_extent_buffer_dirty(next);
  2504. unaccount_log_buffer(fs_info, bytenr);
  2505. }
  2506. }
  2507. free_extent_buffer(next);
  2508. continue;
  2509. }
  2510. ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
  2511. if (ret) {
  2512. free_extent_buffer(next);
  2513. return ret;
  2514. }
  2515. if (path->nodes[*level-1])
  2516. free_extent_buffer(path->nodes[*level-1]);
  2517. path->nodes[*level-1] = next;
  2518. *level = btrfs_header_level(next);
  2519. path->slots[*level] = 0;
  2520. cond_resched();
  2521. }
  2522. path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
  2523. cond_resched();
  2524. return 0;
  2525. }
  2526. static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
  2527. struct btrfs_root *root,
  2528. struct btrfs_path *path, int *level,
  2529. struct walk_control *wc)
  2530. {
  2531. struct btrfs_fs_info *fs_info = root->fs_info;
  2532. int i;
  2533. int slot;
  2534. int ret;
  2535. for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  2536. slot = path->slots[i];
  2537. if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
  2538. path->slots[i]++;
  2539. *level = i;
  2540. WARN_ON(*level == 0);
  2541. return 0;
  2542. } else {
  2543. ret = wc->process_func(root, path->nodes[*level], wc,
  2544. btrfs_header_generation(path->nodes[*level]),
  2545. *level);
  2546. if (ret)
  2547. return ret;
  2548. if (wc->free) {
  2549. struct extent_buffer *next;
  2550. next = path->nodes[*level];
  2551. if (trans) {
  2552. btrfs_tree_lock(next);
  2553. btrfs_set_lock_blocking_write(next);
  2554. btrfs_clean_tree_block(next);
  2555. btrfs_wait_tree_block_writeback(next);
  2556. btrfs_tree_unlock(next);
  2557. ret = btrfs_pin_reserved_extent(trans,
  2558. path->nodes[*level]->start,
  2559. path->nodes[*level]->len);
  2560. if (ret)
  2561. return ret;
  2562. } else {
  2563. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
  2564. clear_extent_buffer_dirty(next);
  2565. unaccount_log_buffer(fs_info,
  2566. path->nodes[*level]->start);
  2567. }
  2568. }
  2569. free_extent_buffer(path->nodes[*level]);
  2570. path->nodes[*level] = NULL;
  2571. *level = i + 1;
  2572. }
  2573. }
  2574. return 1;
  2575. }
  2576. /*
  2577. * drop the reference count on the tree rooted at 'snap'. This traverses
  2578. * the tree freeing any blocks that have a ref count of zero after being
  2579. * decremented.
  2580. */
  2581. static int walk_log_tree(struct btrfs_trans_handle *trans,
  2582. struct btrfs_root *log, struct walk_control *wc)
  2583. {
  2584. struct btrfs_fs_info *fs_info = log->fs_info;
  2585. int ret = 0;
  2586. int wret;
  2587. int level;
  2588. struct btrfs_path *path;
  2589. int orig_level;
  2590. path = btrfs_alloc_path();
  2591. if (!path)
  2592. return -ENOMEM;
  2593. level = btrfs_header_level(log->node);
  2594. orig_level = level;
  2595. path->nodes[level] = log->node;
  2596. atomic_inc(&log->node->refs);
  2597. path->slots[level] = 0;
  2598. while (1) {
  2599. wret = walk_down_log_tree(trans, log, path, &level, wc);
  2600. if (wret > 0)
  2601. break;
  2602. if (wret < 0) {
  2603. ret = wret;
  2604. goto out;
  2605. }
  2606. wret = walk_up_log_tree(trans, log, path, &level, wc);
  2607. if (wret > 0)
  2608. break;
  2609. if (wret < 0) {
  2610. ret = wret;
  2611. goto out;
  2612. }
  2613. }
  2614. /* was the root node processed? if not, catch it here */
  2615. if (path->nodes[orig_level]) {
  2616. ret = wc->process_func(log, path->nodes[orig_level], wc,
  2617. btrfs_header_generation(path->nodes[orig_level]),
  2618. orig_level);
  2619. if (ret)
  2620. goto out;
  2621. if (wc->free) {
  2622. struct extent_buffer *next;
  2623. next = path->nodes[orig_level];
  2624. if (trans) {
  2625. btrfs_tree_lock(next);
  2626. btrfs_set_lock_blocking_write(next);
  2627. btrfs_clean_tree_block(next);
  2628. btrfs_wait_tree_block_writeback(next);
  2629. btrfs_tree_unlock(next);
  2630. ret = btrfs_pin_reserved_extent(trans,
  2631. next->start, next->len);
  2632. if (ret)
  2633. goto out;
  2634. } else {
  2635. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
  2636. clear_extent_buffer_dirty(next);
  2637. unaccount_log_buffer(fs_info, next->start);
  2638. }
  2639. }
  2640. }
  2641. out:
  2642. btrfs_free_path(path);
  2643. return ret;
  2644. }
  2645. /*
  2646. * helper function to update the item for a given subvolumes log root
  2647. * in the tree of log roots
  2648. */
  2649. static int update_log_root(struct btrfs_trans_handle *trans,
  2650. struct btrfs_root *log,
  2651. struct btrfs_root_item *root_item)
  2652. {
  2653. struct btrfs_fs_info *fs_info = log->fs_info;
  2654. int ret;
  2655. if (log->log_transid == 1) {
  2656. /* insert root item on the first sync */
  2657. ret = btrfs_insert_root(trans, fs_info->log_root_tree,
  2658. &log->root_key, root_item);
  2659. } else {
  2660. ret = btrfs_update_root(trans, fs_info->log_root_tree,
  2661. &log->root_key, root_item);
  2662. }
  2663. return ret;
  2664. }
  2665. static void wait_log_commit(struct btrfs_root *root, int transid)
  2666. {
  2667. DEFINE_WAIT(wait);
  2668. int index = transid % 2;
  2669. /*
  2670. * we only allow two pending log transactions at a time,
  2671. * so we know that if ours is more than 2 older than the
  2672. * current transaction, we're done
  2673. */
  2674. for (;;) {
  2675. prepare_to_wait(&root->log_commit_wait[index],
  2676. &wait, TASK_UNINTERRUPTIBLE);
  2677. if (!(root->log_transid_committed < transid &&
  2678. atomic_read(&root->log_commit[index])))
  2679. break;
  2680. mutex_unlock(&root->log_mutex);
  2681. schedule();
  2682. mutex_lock(&root->log_mutex);
  2683. }
  2684. finish_wait(&root->log_commit_wait[index], &wait);
  2685. }
  2686. static void wait_for_writer(struct btrfs_root *root)
  2687. {
  2688. DEFINE_WAIT(wait);
  2689. for (;;) {
  2690. prepare_to_wait(&root->log_writer_wait, &wait,
  2691. TASK_UNINTERRUPTIBLE);
  2692. if (!atomic_read(&root->log_writers))
  2693. break;
  2694. mutex_unlock(&root->log_mutex);
  2695. schedule();
  2696. mutex_lock(&root->log_mutex);
  2697. }
  2698. finish_wait(&root->log_writer_wait, &wait);
  2699. }
  2700. static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
  2701. struct btrfs_log_ctx *ctx)
  2702. {
  2703. if (!ctx)
  2704. return;
  2705. mutex_lock(&root->log_mutex);
  2706. list_del_init(&ctx->list);
  2707. mutex_unlock(&root->log_mutex);
  2708. }
  2709. /*
  2710. * Invoked in log mutex context, or be sure there is no other task which
  2711. * can access the list.
  2712. */
  2713. static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
  2714. int index, int error)
  2715. {
  2716. struct btrfs_log_ctx *ctx;
  2717. struct btrfs_log_ctx *safe;
  2718. list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
  2719. list_del_init(&ctx->list);
  2720. ctx->log_ret = error;
  2721. }
  2722. INIT_LIST_HEAD(&root->log_ctxs[index]);
  2723. }
  2724. /*
  2725. * btrfs_sync_log does sends a given tree log down to the disk and
  2726. * updates the super blocks to record it. When this call is done,
  2727. * you know that any inodes previously logged are safely on disk only
  2728. * if it returns 0.
  2729. *
  2730. * Any other return value means you need to call btrfs_commit_transaction.
  2731. * Some of the edge cases for fsyncing directories that have had unlinks
  2732. * or renames done in the past mean that sometimes the only safe
  2733. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  2734. * that has happened.
  2735. */
  2736. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  2737. struct btrfs_root *root, struct btrfs_log_ctx *ctx)
  2738. {
  2739. int index1;
  2740. int index2;
  2741. int mark;
  2742. int ret;
  2743. struct btrfs_fs_info *fs_info = root->fs_info;
  2744. struct btrfs_root *log = root->log_root;
  2745. struct btrfs_root *log_root_tree = fs_info->log_root_tree;
  2746. struct btrfs_root_item new_root_item;
  2747. int log_transid = 0;
  2748. struct btrfs_log_ctx root_log_ctx;
  2749. struct blk_plug plug;
  2750. mutex_lock(&root->log_mutex);
  2751. log_transid = ctx->log_transid;
  2752. if (root->log_transid_committed >= log_transid) {
  2753. mutex_unlock(&root->log_mutex);
  2754. return ctx->log_ret;
  2755. }
  2756. index1 = log_transid % 2;
  2757. if (atomic_read(&root->log_commit[index1])) {
  2758. wait_log_commit(root, log_transid);
  2759. mutex_unlock(&root->log_mutex);
  2760. return ctx->log_ret;
  2761. }
  2762. ASSERT(log_transid == root->log_transid);
  2763. atomic_set(&root->log_commit[index1], 1);
  2764. /* wait for previous tree log sync to complete */
  2765. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  2766. wait_log_commit(root, log_transid - 1);
  2767. while (1) {
  2768. int batch = atomic_read(&root->log_batch);
  2769. /* when we're on an ssd, just kick the log commit out */
  2770. if (!btrfs_test_opt(fs_info, SSD) &&
  2771. test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
  2772. mutex_unlock(&root->log_mutex);
  2773. schedule_timeout_uninterruptible(1);
  2774. mutex_lock(&root->log_mutex);
  2775. }
  2776. wait_for_writer(root);
  2777. if (batch == atomic_read(&root->log_batch))
  2778. break;
  2779. }
  2780. /* bail out if we need to do a full commit */
  2781. if (btrfs_need_log_full_commit(trans)) {
  2782. ret = -EAGAIN;
  2783. mutex_unlock(&root->log_mutex);
  2784. goto out;
  2785. }
  2786. if (log_transid % 2 == 0)
  2787. mark = EXTENT_DIRTY;
  2788. else
  2789. mark = EXTENT_NEW;
  2790. /* we start IO on all the marked extents here, but we don't actually
  2791. * wait for them until later.
  2792. */
  2793. blk_start_plug(&plug);
  2794. ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
  2795. if (ret) {
  2796. blk_finish_plug(&plug);
  2797. btrfs_abort_transaction(trans, ret);
  2798. btrfs_set_log_full_commit(trans);
  2799. mutex_unlock(&root->log_mutex);
  2800. goto out;
  2801. }
  2802. /*
  2803. * We _must_ update under the root->log_mutex in order to make sure we
  2804. * have a consistent view of the log root we are trying to commit at
  2805. * this moment.
  2806. *
  2807. * We _must_ copy this into a local copy, because we are not holding the
  2808. * log_root_tree->log_mutex yet. This is important because when we
  2809. * commit the log_root_tree we must have a consistent view of the
  2810. * log_root_tree when we update the super block to point at the
  2811. * log_root_tree bytenr. If we update the log_root_tree here we'll race
  2812. * with the commit and possibly point at the new block which we may not
  2813. * have written out.
  2814. */
  2815. btrfs_set_root_node(&log->root_item, log->node);
  2816. memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
  2817. root->log_transid++;
  2818. log->log_transid = root->log_transid;
  2819. root->log_start_pid = 0;
  2820. /*
  2821. * IO has been started, blocks of the log tree have WRITTEN flag set
  2822. * in their headers. new modifications of the log will be written to
  2823. * new positions. so it's safe to allow log writers to go in.
  2824. */
  2825. mutex_unlock(&root->log_mutex);
  2826. btrfs_init_log_ctx(&root_log_ctx, NULL);
  2827. mutex_lock(&log_root_tree->log_mutex);
  2828. index2 = log_root_tree->log_transid % 2;
  2829. list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
  2830. root_log_ctx.log_transid = log_root_tree->log_transid;
  2831. /*
  2832. * Now we are safe to update the log_root_tree because we're under the
  2833. * log_mutex, and we're a current writer so we're holding the commit
  2834. * open until we drop the log_mutex.
  2835. */
  2836. ret = update_log_root(trans, log, &new_root_item);
  2837. if (ret) {
  2838. if (!list_empty(&root_log_ctx.list))
  2839. list_del_init(&root_log_ctx.list);
  2840. blk_finish_plug(&plug);
  2841. btrfs_set_log_full_commit(trans);
  2842. if (ret != -ENOSPC) {
  2843. btrfs_abort_transaction(trans, ret);
  2844. mutex_unlock(&log_root_tree->log_mutex);
  2845. goto out;
  2846. }
  2847. btrfs_wait_tree_log_extents(log, mark);
  2848. mutex_unlock(&log_root_tree->log_mutex);
  2849. ret = -EAGAIN;
  2850. goto out;
  2851. }
  2852. if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
  2853. blk_finish_plug(&plug);
  2854. list_del_init(&root_log_ctx.list);
  2855. mutex_unlock(&log_root_tree->log_mutex);
  2856. ret = root_log_ctx.log_ret;
  2857. goto out;
  2858. }
  2859. index2 = root_log_ctx.log_transid % 2;
  2860. if (atomic_read(&log_root_tree->log_commit[index2])) {
  2861. blk_finish_plug(&plug);
  2862. ret = btrfs_wait_tree_log_extents(log, mark);
  2863. wait_log_commit(log_root_tree,
  2864. root_log_ctx.log_transid);
  2865. mutex_unlock(&log_root_tree->log_mutex);
  2866. if (!ret)
  2867. ret = root_log_ctx.log_ret;
  2868. goto out;
  2869. }
  2870. ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
  2871. atomic_set(&log_root_tree->log_commit[index2], 1);
  2872. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  2873. wait_log_commit(log_root_tree,
  2874. root_log_ctx.log_transid - 1);
  2875. }
  2876. /*
  2877. * now that we've moved on to the tree of log tree roots,
  2878. * check the full commit flag again
  2879. */
  2880. if (btrfs_need_log_full_commit(trans)) {
  2881. blk_finish_plug(&plug);
  2882. btrfs_wait_tree_log_extents(log, mark);
  2883. mutex_unlock(&log_root_tree->log_mutex);
  2884. ret = -EAGAIN;
  2885. goto out_wake_log_root;
  2886. }
  2887. ret = btrfs_write_marked_extents(fs_info,
  2888. &log_root_tree->dirty_log_pages,
  2889. EXTENT_DIRTY | EXTENT_NEW);
  2890. blk_finish_plug(&plug);
  2891. if (ret) {
  2892. btrfs_set_log_full_commit(trans);
  2893. btrfs_abort_transaction(trans, ret);
  2894. mutex_unlock(&log_root_tree->log_mutex);
  2895. goto out_wake_log_root;
  2896. }
  2897. ret = btrfs_wait_tree_log_extents(log, mark);
  2898. if (!ret)
  2899. ret = btrfs_wait_tree_log_extents(log_root_tree,
  2900. EXTENT_NEW | EXTENT_DIRTY);
  2901. if (ret) {
  2902. btrfs_set_log_full_commit(trans);
  2903. mutex_unlock(&log_root_tree->log_mutex);
  2904. goto out_wake_log_root;
  2905. }
  2906. btrfs_set_super_log_root(fs_info->super_for_commit,
  2907. log_root_tree->node->start);
  2908. btrfs_set_super_log_root_level(fs_info->super_for_commit,
  2909. btrfs_header_level(log_root_tree->node));
  2910. log_root_tree->log_transid++;
  2911. mutex_unlock(&log_root_tree->log_mutex);
  2912. /*
  2913. * Nobody else is going to jump in and write the ctree
  2914. * super here because the log_commit atomic below is protecting
  2915. * us. We must be called with a transaction handle pinning
  2916. * the running transaction open, so a full commit can't hop
  2917. * in and cause problems either.
  2918. */
  2919. ret = write_all_supers(fs_info, 1);
  2920. if (ret) {
  2921. btrfs_set_log_full_commit(trans);
  2922. btrfs_abort_transaction(trans, ret);
  2923. goto out_wake_log_root;
  2924. }
  2925. mutex_lock(&root->log_mutex);
  2926. if (root->last_log_commit < log_transid)
  2927. root->last_log_commit = log_transid;
  2928. mutex_unlock(&root->log_mutex);
  2929. out_wake_log_root:
  2930. mutex_lock(&log_root_tree->log_mutex);
  2931. btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
  2932. log_root_tree->log_transid_committed++;
  2933. atomic_set(&log_root_tree->log_commit[index2], 0);
  2934. mutex_unlock(&log_root_tree->log_mutex);
  2935. /*
  2936. * The barrier before waitqueue_active (in cond_wake_up) is needed so
  2937. * all the updates above are seen by the woken threads. It might not be
  2938. * necessary, but proving that seems to be hard.
  2939. */
  2940. cond_wake_up(&log_root_tree->log_commit_wait[index2]);
  2941. out:
  2942. mutex_lock(&root->log_mutex);
  2943. btrfs_remove_all_log_ctxs(root, index1, ret);
  2944. root->log_transid_committed++;
  2945. atomic_set(&root->log_commit[index1], 0);
  2946. mutex_unlock(&root->log_mutex);
  2947. /*
  2948. * The barrier before waitqueue_active (in cond_wake_up) is needed so
  2949. * all the updates above are seen by the woken threads. It might not be
  2950. * necessary, but proving that seems to be hard.
  2951. */
  2952. cond_wake_up(&root->log_commit_wait[index1]);
  2953. return ret;
  2954. }
  2955. static void free_log_tree(struct btrfs_trans_handle *trans,
  2956. struct btrfs_root *log)
  2957. {
  2958. int ret;
  2959. struct walk_control wc = {
  2960. .free = 1,
  2961. .process_func = process_one_buffer
  2962. };
  2963. ret = walk_log_tree(trans, log, &wc);
  2964. if (ret) {
  2965. if (trans)
  2966. btrfs_abort_transaction(trans, ret);
  2967. else
  2968. btrfs_handle_fs_error(log->fs_info, ret, NULL);
  2969. }
  2970. clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
  2971. EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
  2972. extent_io_tree_release(&log->log_csum_range);
  2973. btrfs_put_root(log);
  2974. }
  2975. /*
  2976. * free all the extents used by the tree log. This should be called
  2977. * at commit time of the full transaction
  2978. */
  2979. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  2980. {
  2981. if (root->log_root) {
  2982. free_log_tree(trans, root->log_root);
  2983. root->log_root = NULL;
  2984. clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
  2985. }
  2986. return 0;
  2987. }
  2988. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  2989. struct btrfs_fs_info *fs_info)
  2990. {
  2991. if (fs_info->log_root_tree) {
  2992. free_log_tree(trans, fs_info->log_root_tree);
  2993. fs_info->log_root_tree = NULL;
  2994. }
  2995. return 0;
  2996. }
  2997. /*
  2998. * Check if an inode was logged in the current transaction. We can't always rely
  2999. * on an inode's logged_trans value, because it's an in-memory only field and
  3000. * therefore not persisted. This means that its value is lost if the inode gets
  3001. * evicted and loaded again from disk (in which case it has a value of 0, and
  3002. * certainly it is smaller then any possible transaction ID), when that happens
  3003. * the full_sync flag is set in the inode's runtime flags, so on that case we
  3004. * assume eviction happened and ignore the logged_trans value, assuming the
  3005. * worst case, that the inode was logged before in the current transaction.
  3006. */
  3007. static bool inode_logged(struct btrfs_trans_handle *trans,
  3008. struct btrfs_inode *inode)
  3009. {
  3010. if (inode->logged_trans == trans->transid)
  3011. return true;
  3012. if (inode->last_trans == trans->transid &&
  3013. test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) &&
  3014. !test_bit(BTRFS_FS_LOG_RECOVERING, &trans->fs_info->flags))
  3015. return true;
  3016. return false;
  3017. }
  3018. /*
  3019. * If both a file and directory are logged, and unlinks or renames are
  3020. * mixed in, we have a few interesting corners:
  3021. *
  3022. * create file X in dir Y
  3023. * link file X to X.link in dir Y
  3024. * fsync file X
  3025. * unlink file X but leave X.link
  3026. * fsync dir Y
  3027. *
  3028. * After a crash we would expect only X.link to exist. But file X
  3029. * didn't get fsync'd again so the log has back refs for X and X.link.
  3030. *
  3031. * We solve this by removing directory entries and inode backrefs from the
  3032. * log when a file that was logged in the current transaction is
  3033. * unlinked. Any later fsync will include the updated log entries, and
  3034. * we'll be able to reconstruct the proper directory items from backrefs.
  3035. *
  3036. * This optimizations allows us to avoid relogging the entire inode
  3037. * or the entire directory.
  3038. */
  3039. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  3040. struct btrfs_root *root,
  3041. const char *name, int name_len,
  3042. struct btrfs_inode *dir, u64 index)
  3043. {
  3044. struct btrfs_root *log;
  3045. struct btrfs_dir_item *di;
  3046. struct btrfs_path *path;
  3047. int ret;
  3048. int err = 0;
  3049. int bytes_del = 0;
  3050. u64 dir_ino = btrfs_ino(dir);
  3051. if (!inode_logged(trans, dir))
  3052. return 0;
  3053. ret = join_running_log_trans(root);
  3054. if (ret)
  3055. return 0;
  3056. mutex_lock(&dir->log_mutex);
  3057. log = root->log_root;
  3058. path = btrfs_alloc_path();
  3059. if (!path) {
  3060. err = -ENOMEM;
  3061. goto out_unlock;
  3062. }
  3063. di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
  3064. name, name_len, -1);
  3065. if (IS_ERR(di)) {
  3066. err = PTR_ERR(di);
  3067. goto fail;
  3068. }
  3069. if (di) {
  3070. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  3071. bytes_del += name_len;
  3072. if (ret) {
  3073. err = ret;
  3074. goto fail;
  3075. }
  3076. }
  3077. btrfs_release_path(path);
  3078. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  3079. index, name, name_len, -1);
  3080. if (IS_ERR(di)) {
  3081. err = PTR_ERR(di);
  3082. goto fail;
  3083. }
  3084. if (di) {
  3085. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  3086. bytes_del += name_len;
  3087. if (ret) {
  3088. err = ret;
  3089. goto fail;
  3090. }
  3091. }
  3092. /* update the directory size in the log to reflect the names
  3093. * we have removed
  3094. */
  3095. if (bytes_del) {
  3096. struct btrfs_key key;
  3097. key.objectid = dir_ino;
  3098. key.offset = 0;
  3099. key.type = BTRFS_INODE_ITEM_KEY;
  3100. btrfs_release_path(path);
  3101. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  3102. if (ret < 0) {
  3103. err = ret;
  3104. goto fail;
  3105. }
  3106. if (ret == 0) {
  3107. struct btrfs_inode_item *item;
  3108. u64 i_size;
  3109. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3110. struct btrfs_inode_item);
  3111. i_size = btrfs_inode_size(path->nodes[0], item);
  3112. if (i_size > bytes_del)
  3113. i_size -= bytes_del;
  3114. else
  3115. i_size = 0;
  3116. btrfs_set_inode_size(path->nodes[0], item, i_size);
  3117. btrfs_mark_buffer_dirty(path->nodes[0]);
  3118. } else
  3119. ret = 0;
  3120. btrfs_release_path(path);
  3121. }
  3122. fail:
  3123. btrfs_free_path(path);
  3124. out_unlock:
  3125. mutex_unlock(&dir->log_mutex);
  3126. if (err == -ENOSPC) {
  3127. btrfs_set_log_full_commit(trans);
  3128. err = 0;
  3129. } else if (err < 0 && err != -ENOENT) {
  3130. /* ENOENT can be returned if the entry hasn't been fsynced yet */
  3131. btrfs_abort_transaction(trans, err);
  3132. }
  3133. btrfs_end_log_trans(root);
  3134. return err;
  3135. }
  3136. /* see comments for btrfs_del_dir_entries_in_log */
  3137. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  3138. struct btrfs_root *root,
  3139. const char *name, int name_len,
  3140. struct btrfs_inode *inode, u64 dirid)
  3141. {
  3142. struct btrfs_root *log;
  3143. u64 index;
  3144. int ret;
  3145. if (!inode_logged(trans, inode))
  3146. return 0;
  3147. ret = join_running_log_trans(root);
  3148. if (ret)
  3149. return 0;
  3150. log = root->log_root;
  3151. mutex_lock(&inode->log_mutex);
  3152. ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
  3153. dirid, &index);
  3154. mutex_unlock(&inode->log_mutex);
  3155. if (ret == -ENOSPC) {
  3156. btrfs_set_log_full_commit(trans);
  3157. ret = 0;
  3158. } else if (ret < 0 && ret != -ENOENT)
  3159. btrfs_abort_transaction(trans, ret);
  3160. btrfs_end_log_trans(root);
  3161. return ret;
  3162. }
  3163. /*
  3164. * creates a range item in the log for 'dirid'. first_offset and
  3165. * last_offset tell us which parts of the key space the log should
  3166. * be considered authoritative for.
  3167. */
  3168. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  3169. struct btrfs_root *log,
  3170. struct btrfs_path *path,
  3171. int key_type, u64 dirid,
  3172. u64 first_offset, u64 last_offset)
  3173. {
  3174. int ret;
  3175. struct btrfs_key key;
  3176. struct btrfs_dir_log_item *item;
  3177. key.objectid = dirid;
  3178. key.offset = first_offset;
  3179. if (key_type == BTRFS_DIR_ITEM_KEY)
  3180. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  3181. else
  3182. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  3183. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  3184. if (ret)
  3185. return ret;
  3186. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3187. struct btrfs_dir_log_item);
  3188. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  3189. btrfs_mark_buffer_dirty(path->nodes[0]);
  3190. btrfs_release_path(path);
  3191. return 0;
  3192. }
  3193. /*
  3194. * log all the items included in the current transaction for a given
  3195. * directory. This also creates the range items in the log tree required
  3196. * to replay anything deleted before the fsync
  3197. */
  3198. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  3199. struct btrfs_root *root, struct btrfs_inode *inode,
  3200. struct btrfs_path *path,
  3201. struct btrfs_path *dst_path, int key_type,
  3202. struct btrfs_log_ctx *ctx,
  3203. u64 min_offset, u64 *last_offset_ret)
  3204. {
  3205. struct btrfs_key min_key;
  3206. struct btrfs_root *log = root->log_root;
  3207. struct extent_buffer *src;
  3208. int err = 0;
  3209. int ret;
  3210. int i;
  3211. int nritems;
  3212. u64 first_offset = min_offset;
  3213. u64 last_offset = (u64)-1;
  3214. u64 ino = btrfs_ino(inode);
  3215. log = root->log_root;
  3216. min_key.objectid = ino;
  3217. min_key.type = key_type;
  3218. min_key.offset = min_offset;
  3219. ret = btrfs_search_forward(root, &min_key, path, trans->transid);
  3220. /*
  3221. * we didn't find anything from this transaction, see if there
  3222. * is anything at all
  3223. */
  3224. if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
  3225. min_key.objectid = ino;
  3226. min_key.type = key_type;
  3227. min_key.offset = (u64)-1;
  3228. btrfs_release_path(path);
  3229. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  3230. if (ret < 0) {
  3231. btrfs_release_path(path);
  3232. return ret;
  3233. }
  3234. ret = btrfs_previous_item(root, path, ino, key_type);
  3235. /* if ret == 0 there are items for this type,
  3236. * create a range to tell us the last key of this type.
  3237. * otherwise, there are no items in this directory after
  3238. * *min_offset, and we create a range to indicate that.
  3239. */
  3240. if (ret == 0) {
  3241. struct btrfs_key tmp;
  3242. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  3243. path->slots[0]);
  3244. if (key_type == tmp.type)
  3245. first_offset = max(min_offset, tmp.offset) + 1;
  3246. }
  3247. goto done;
  3248. }
  3249. /* go backward to find any previous key */
  3250. ret = btrfs_previous_item(root, path, ino, key_type);
  3251. if (ret == 0) {
  3252. struct btrfs_key tmp;
  3253. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  3254. if (key_type == tmp.type) {
  3255. first_offset = tmp.offset;
  3256. ret = overwrite_item(trans, log, dst_path,
  3257. path->nodes[0], path->slots[0],
  3258. &tmp);
  3259. if (ret) {
  3260. err = ret;
  3261. goto done;
  3262. }
  3263. }
  3264. }
  3265. btrfs_release_path(path);
  3266. /*
  3267. * Find the first key from this transaction again. See the note for
  3268. * log_new_dir_dentries, if we're logging a directory recursively we
  3269. * won't be holding its i_mutex, which means we can modify the directory
  3270. * while we're logging it. If we remove an entry between our first
  3271. * search and this search we'll not find the key again and can just
  3272. * bail.
  3273. */
  3274. search:
  3275. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  3276. if (ret != 0)
  3277. goto done;
  3278. /*
  3279. * we have a block from this transaction, log every item in it
  3280. * from our directory
  3281. */
  3282. while (1) {
  3283. struct btrfs_key tmp;
  3284. src = path->nodes[0];
  3285. nritems = btrfs_header_nritems(src);
  3286. for (i = path->slots[0]; i < nritems; i++) {
  3287. struct btrfs_dir_item *di;
  3288. btrfs_item_key_to_cpu(src, &min_key, i);
  3289. if (min_key.objectid != ino || min_key.type != key_type)
  3290. goto done;
  3291. if (need_resched()) {
  3292. btrfs_release_path(path);
  3293. cond_resched();
  3294. goto search;
  3295. }
  3296. ret = overwrite_item(trans, log, dst_path, src, i,
  3297. &min_key);
  3298. if (ret) {
  3299. err = ret;
  3300. goto done;
  3301. }
  3302. /*
  3303. * We must make sure that when we log a directory entry,
  3304. * the corresponding inode, after log replay, has a
  3305. * matching link count. For example:
  3306. *
  3307. * touch foo
  3308. * mkdir mydir
  3309. * sync
  3310. * ln foo mydir/bar
  3311. * xfs_io -c "fsync" mydir
  3312. * <crash>
  3313. * <mount fs and log replay>
  3314. *
  3315. * Would result in a fsync log that when replayed, our
  3316. * file inode would have a link count of 1, but we get
  3317. * two directory entries pointing to the same inode.
  3318. * After removing one of the names, it would not be
  3319. * possible to remove the other name, which resulted
  3320. * always in stale file handle errors, and would not
  3321. * be possible to rmdir the parent directory, since
  3322. * its i_size could never decrement to the value
  3323. * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
  3324. */
  3325. di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
  3326. btrfs_dir_item_key_to_cpu(src, di, &tmp);
  3327. if (ctx &&
  3328. (btrfs_dir_transid(src, di) == trans->transid ||
  3329. btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
  3330. tmp.type != BTRFS_ROOT_ITEM_KEY)
  3331. ctx->log_new_dentries = true;
  3332. }
  3333. path->slots[0] = nritems;
  3334. /*
  3335. * look ahead to the next item and see if it is also
  3336. * from this directory and from this transaction
  3337. */
  3338. ret = btrfs_next_leaf(root, path);
  3339. if (ret) {
  3340. if (ret == 1)
  3341. last_offset = (u64)-1;
  3342. else
  3343. err = ret;
  3344. goto done;
  3345. }
  3346. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  3347. if (tmp.objectid != ino || tmp.type != key_type) {
  3348. last_offset = (u64)-1;
  3349. goto done;
  3350. }
  3351. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  3352. ret = overwrite_item(trans, log, dst_path,
  3353. path->nodes[0], path->slots[0],
  3354. &tmp);
  3355. if (ret)
  3356. err = ret;
  3357. else
  3358. last_offset = tmp.offset;
  3359. goto done;
  3360. }
  3361. }
  3362. done:
  3363. btrfs_release_path(path);
  3364. btrfs_release_path(dst_path);
  3365. if (err == 0) {
  3366. *last_offset_ret = last_offset;
  3367. /*
  3368. * insert the log range keys to indicate where the log
  3369. * is valid
  3370. */
  3371. ret = insert_dir_log_key(trans, log, path, key_type,
  3372. ino, first_offset, last_offset);
  3373. if (ret)
  3374. err = ret;
  3375. }
  3376. return err;
  3377. }
  3378. /*
  3379. * logging directories is very similar to logging inodes, We find all the items
  3380. * from the current transaction and write them to the log.
  3381. *
  3382. * The recovery code scans the directory in the subvolume, and if it finds a
  3383. * key in the range logged that is not present in the log tree, then it means
  3384. * that dir entry was unlinked during the transaction.
  3385. *
  3386. * In order for that scan to work, we must include one key smaller than
  3387. * the smallest logged by this transaction and one key larger than the largest
  3388. * key logged by this transaction.
  3389. */
  3390. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  3391. struct btrfs_root *root, struct btrfs_inode *inode,
  3392. struct btrfs_path *path,
  3393. struct btrfs_path *dst_path,
  3394. struct btrfs_log_ctx *ctx)
  3395. {
  3396. u64 min_key;
  3397. u64 max_key;
  3398. int ret;
  3399. int key_type = BTRFS_DIR_ITEM_KEY;
  3400. again:
  3401. min_key = 0;
  3402. max_key = 0;
  3403. while (1) {
  3404. ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
  3405. ctx, min_key, &max_key);
  3406. if (ret)
  3407. return ret;
  3408. if (max_key == (u64)-1)
  3409. break;
  3410. min_key = max_key + 1;
  3411. }
  3412. if (key_type == BTRFS_DIR_ITEM_KEY) {
  3413. key_type = BTRFS_DIR_INDEX_KEY;
  3414. goto again;
  3415. }
  3416. return 0;
  3417. }
  3418. /*
  3419. * a helper function to drop items from the log before we relog an
  3420. * inode. max_key_type indicates the highest item type to remove.
  3421. * This cannot be run for file data extents because it does not
  3422. * free the extents they point to.
  3423. */
  3424. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  3425. struct btrfs_root *log,
  3426. struct btrfs_path *path,
  3427. u64 objectid, int max_key_type)
  3428. {
  3429. int ret;
  3430. struct btrfs_key key;
  3431. struct btrfs_key found_key;
  3432. int start_slot;
  3433. key.objectid = objectid;
  3434. key.type = max_key_type;
  3435. key.offset = (u64)-1;
  3436. while (1) {
  3437. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  3438. BUG_ON(ret == 0); /* Logic error */
  3439. if (ret < 0)
  3440. break;
  3441. if (path->slots[0] == 0)
  3442. break;
  3443. path->slots[0]--;
  3444. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  3445. path->slots[0]);
  3446. if (found_key.objectid != objectid)
  3447. break;
  3448. found_key.offset = 0;
  3449. found_key.type = 0;
  3450. ret = btrfs_bin_search(path->nodes[0], &found_key, &start_slot);
  3451. if (ret < 0)
  3452. break;
  3453. ret = btrfs_del_items(trans, log, path, start_slot,
  3454. path->slots[0] - start_slot + 1);
  3455. /*
  3456. * If start slot isn't 0 then we don't need to re-search, we've
  3457. * found the last guy with the objectid in this tree.
  3458. */
  3459. if (ret || start_slot != 0)
  3460. break;
  3461. btrfs_release_path(path);
  3462. }
  3463. btrfs_release_path(path);
  3464. if (ret > 0)
  3465. ret = 0;
  3466. return ret;
  3467. }
  3468. static void fill_inode_item(struct btrfs_trans_handle *trans,
  3469. struct extent_buffer *leaf,
  3470. struct btrfs_inode_item *item,
  3471. struct inode *inode, int log_inode_only,
  3472. u64 logged_isize)
  3473. {
  3474. struct btrfs_map_token token;
  3475. btrfs_init_map_token(&token, leaf);
  3476. if (log_inode_only) {
  3477. /* set the generation to zero so the recover code
  3478. * can tell the difference between an logging
  3479. * just to say 'this inode exists' and a logging
  3480. * to say 'update this inode with these values'
  3481. */
  3482. btrfs_set_token_inode_generation(&token, item, 0);
  3483. btrfs_set_token_inode_size(&token, item, logged_isize);
  3484. } else {
  3485. btrfs_set_token_inode_generation(&token, item,
  3486. BTRFS_I(inode)->generation);
  3487. btrfs_set_token_inode_size(&token, item, inode->i_size);
  3488. }
  3489. btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
  3490. btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
  3491. btrfs_set_token_inode_mode(&token, item, inode->i_mode);
  3492. btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
  3493. btrfs_set_token_timespec_sec(&token, &item->atime,
  3494. inode->i_atime.tv_sec);
  3495. btrfs_set_token_timespec_nsec(&token, &item->atime,
  3496. inode->i_atime.tv_nsec);
  3497. btrfs_set_token_timespec_sec(&token, &item->mtime,
  3498. inode->i_mtime.tv_sec);
  3499. btrfs_set_token_timespec_nsec(&token, &item->mtime,
  3500. inode->i_mtime.tv_nsec);
  3501. btrfs_set_token_timespec_sec(&token, &item->ctime,
  3502. inode->i_ctime.tv_sec);
  3503. btrfs_set_token_timespec_nsec(&token, &item->ctime,
  3504. inode->i_ctime.tv_nsec);
  3505. btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
  3506. btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
  3507. btrfs_set_token_inode_transid(&token, item, trans->transid);
  3508. btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
  3509. btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
  3510. btrfs_set_token_inode_block_group(&token, item, 0);
  3511. }
  3512. static int log_inode_item(struct btrfs_trans_handle *trans,
  3513. struct btrfs_root *log, struct btrfs_path *path,
  3514. struct btrfs_inode *inode)
  3515. {
  3516. struct btrfs_inode_item *inode_item;
  3517. int ret;
  3518. ret = btrfs_insert_empty_item(trans, log, path,
  3519. &inode->location, sizeof(*inode_item));
  3520. if (ret && ret != -EEXIST)
  3521. return ret;
  3522. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3523. struct btrfs_inode_item);
  3524. fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
  3525. 0, 0);
  3526. btrfs_release_path(path);
  3527. return 0;
  3528. }
  3529. static int log_csums(struct btrfs_trans_handle *trans,
  3530. struct btrfs_inode *inode,
  3531. struct btrfs_root *log_root,
  3532. struct btrfs_ordered_sum *sums)
  3533. {
  3534. const u64 lock_end = sums->bytenr + sums->len - 1;
  3535. struct extent_state *cached_state = NULL;
  3536. int ret;
  3537. /*
  3538. * If this inode was not used for reflink operations in the current
  3539. * transaction with new extents, then do the fast path, no need to
  3540. * worry about logging checksum items with overlapping ranges.
  3541. */
  3542. if (inode->last_reflink_trans < trans->transid)
  3543. return btrfs_csum_file_blocks(trans, log_root, sums);
  3544. /*
  3545. * Serialize logging for checksums. This is to avoid racing with the
  3546. * same checksum being logged by another task that is logging another
  3547. * file which happens to refer to the same extent as well. Such races
  3548. * can leave checksum items in the log with overlapping ranges.
  3549. */
  3550. ret = lock_extent_bits(&log_root->log_csum_range, sums->bytenr,
  3551. lock_end, &cached_state);
  3552. if (ret)
  3553. return ret;
  3554. /*
  3555. * Due to extent cloning, we might have logged a csum item that covers a
  3556. * subrange of a cloned extent, and later we can end up logging a csum
  3557. * item for a larger subrange of the same extent or the entire range.
  3558. * This would leave csum items in the log tree that cover the same range
  3559. * and break the searches for checksums in the log tree, resulting in
  3560. * some checksums missing in the fs/subvolume tree. So just delete (or
  3561. * trim and adjust) any existing csum items in the log for this range.
  3562. */
  3563. ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len);
  3564. if (!ret)
  3565. ret = btrfs_csum_file_blocks(trans, log_root, sums);
  3566. unlock_extent_cached(&log_root->log_csum_range, sums->bytenr, lock_end,
  3567. &cached_state);
  3568. return ret;
  3569. }
  3570. static noinline int copy_items(struct btrfs_trans_handle *trans,
  3571. struct btrfs_inode *inode,
  3572. struct btrfs_path *dst_path,
  3573. struct btrfs_path *src_path,
  3574. int start_slot, int nr, int inode_only,
  3575. u64 logged_isize)
  3576. {
  3577. struct btrfs_fs_info *fs_info = trans->fs_info;
  3578. unsigned long src_offset;
  3579. unsigned long dst_offset;
  3580. struct btrfs_root *log = inode->root->log_root;
  3581. struct btrfs_file_extent_item *extent;
  3582. struct btrfs_inode_item *inode_item;
  3583. struct extent_buffer *src = src_path->nodes[0];
  3584. int ret;
  3585. struct btrfs_key *ins_keys;
  3586. u32 *ins_sizes;
  3587. char *ins_data;
  3588. int i;
  3589. struct list_head ordered_sums;
  3590. int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
  3591. INIT_LIST_HEAD(&ordered_sums);
  3592. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  3593. nr * sizeof(u32), GFP_NOFS);
  3594. if (!ins_data)
  3595. return -ENOMEM;
  3596. ins_sizes = (u32 *)ins_data;
  3597. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  3598. for (i = 0; i < nr; i++) {
  3599. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  3600. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  3601. }
  3602. ret = btrfs_insert_empty_items(trans, log, dst_path,
  3603. ins_keys, ins_sizes, nr);
  3604. if (ret) {
  3605. kfree(ins_data);
  3606. return ret;
  3607. }
  3608. for (i = 0; i < nr; i++, dst_path->slots[0]++) {
  3609. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  3610. dst_path->slots[0]);
  3611. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  3612. if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  3613. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  3614. dst_path->slots[0],
  3615. struct btrfs_inode_item);
  3616. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  3617. &inode->vfs_inode,
  3618. inode_only == LOG_INODE_EXISTS,
  3619. logged_isize);
  3620. } else {
  3621. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  3622. src_offset, ins_sizes[i]);
  3623. }
  3624. /* take a reference on file data extents so that truncates
  3625. * or deletes of this inode don't have to relog the inode
  3626. * again
  3627. */
  3628. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
  3629. !skip_csum) {
  3630. int found_type;
  3631. extent = btrfs_item_ptr(src, start_slot + i,
  3632. struct btrfs_file_extent_item);
  3633. if (btrfs_file_extent_generation(src, extent) < trans->transid)
  3634. continue;
  3635. found_type = btrfs_file_extent_type(src, extent);
  3636. if (found_type == BTRFS_FILE_EXTENT_REG) {
  3637. u64 ds, dl, cs, cl;
  3638. ds = btrfs_file_extent_disk_bytenr(src,
  3639. extent);
  3640. /* ds == 0 is a hole */
  3641. if (ds == 0)
  3642. continue;
  3643. dl = btrfs_file_extent_disk_num_bytes(src,
  3644. extent);
  3645. cs = btrfs_file_extent_offset(src, extent);
  3646. cl = btrfs_file_extent_num_bytes(src,
  3647. extent);
  3648. if (btrfs_file_extent_compression(src,
  3649. extent)) {
  3650. cs = 0;
  3651. cl = dl;
  3652. }
  3653. ret = btrfs_lookup_csums_range(
  3654. fs_info->csum_root,
  3655. ds + cs, ds + cs + cl - 1,
  3656. &ordered_sums, 0);
  3657. if (ret)
  3658. break;
  3659. }
  3660. }
  3661. }
  3662. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  3663. btrfs_release_path(dst_path);
  3664. kfree(ins_data);
  3665. /*
  3666. * we have to do this after the loop above to avoid changing the
  3667. * log tree while trying to change the log tree.
  3668. */
  3669. while (!list_empty(&ordered_sums)) {
  3670. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3671. struct btrfs_ordered_sum,
  3672. list);
  3673. if (!ret)
  3674. ret = log_csums(trans, inode, log, sums);
  3675. list_del(&sums->list);
  3676. kfree(sums);
  3677. }
  3678. return ret;
  3679. }
  3680. static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
  3681. {
  3682. struct extent_map *em1, *em2;
  3683. em1 = list_entry(a, struct extent_map, list);
  3684. em2 = list_entry(b, struct extent_map, list);
  3685. if (em1->start < em2->start)
  3686. return -1;
  3687. else if (em1->start > em2->start)
  3688. return 1;
  3689. return 0;
  3690. }
  3691. static int log_extent_csums(struct btrfs_trans_handle *trans,
  3692. struct btrfs_inode *inode,
  3693. struct btrfs_root *log_root,
  3694. const struct extent_map *em,
  3695. struct btrfs_log_ctx *ctx)
  3696. {
  3697. struct btrfs_ordered_extent *ordered;
  3698. u64 csum_offset;
  3699. u64 csum_len;
  3700. u64 mod_start = em->mod_start;
  3701. u64 mod_len = em->mod_len;
  3702. LIST_HEAD(ordered_sums);
  3703. int ret = 0;
  3704. if (inode->flags & BTRFS_INODE_NODATASUM ||
  3705. test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  3706. em->block_start == EXTENT_MAP_HOLE)
  3707. return 0;
  3708. list_for_each_entry(ordered, &ctx->ordered_extents, log_list) {
  3709. const u64 ordered_end = ordered->file_offset + ordered->num_bytes;
  3710. const u64 mod_end = mod_start + mod_len;
  3711. struct btrfs_ordered_sum *sums;
  3712. if (mod_len == 0)
  3713. break;
  3714. if (ordered_end <= mod_start)
  3715. continue;
  3716. if (mod_end <= ordered->file_offset)
  3717. break;
  3718. /*
  3719. * We are going to copy all the csums on this ordered extent, so
  3720. * go ahead and adjust mod_start and mod_len in case this ordered
  3721. * extent has already been logged.
  3722. */
  3723. if (ordered->file_offset > mod_start) {
  3724. if (ordered_end >= mod_end)
  3725. mod_len = ordered->file_offset - mod_start;
  3726. /*
  3727. * If we have this case
  3728. *
  3729. * |--------- logged extent ---------|
  3730. * |----- ordered extent ----|
  3731. *
  3732. * Just don't mess with mod_start and mod_len, we'll
  3733. * just end up logging more csums than we need and it
  3734. * will be ok.
  3735. */
  3736. } else {
  3737. if (ordered_end < mod_end) {
  3738. mod_len = mod_end - ordered_end;
  3739. mod_start = ordered_end;
  3740. } else {
  3741. mod_len = 0;
  3742. }
  3743. }
  3744. /*
  3745. * To keep us from looping for the above case of an ordered
  3746. * extent that falls inside of the logged extent.
  3747. */
  3748. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags))
  3749. continue;
  3750. list_for_each_entry(sums, &ordered->list, list) {
  3751. ret = log_csums(trans, inode, log_root, sums);
  3752. if (ret)
  3753. return ret;
  3754. }
  3755. }
  3756. /* We're done, found all csums in the ordered extents. */
  3757. if (mod_len == 0)
  3758. return 0;
  3759. /* If we're compressed we have to save the entire range of csums. */
  3760. if (em->compress_type) {
  3761. csum_offset = 0;
  3762. csum_len = max(em->block_len, em->orig_block_len);
  3763. } else {
  3764. csum_offset = mod_start - em->start;
  3765. csum_len = mod_len;
  3766. }
  3767. /* block start is already adjusted for the file extent offset. */
  3768. ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
  3769. em->block_start + csum_offset,
  3770. em->block_start + csum_offset +
  3771. csum_len - 1, &ordered_sums, 0);
  3772. if (ret)
  3773. return ret;
  3774. while (!list_empty(&ordered_sums)) {
  3775. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3776. struct btrfs_ordered_sum,
  3777. list);
  3778. if (!ret)
  3779. ret = log_csums(trans, inode, log_root, sums);
  3780. list_del(&sums->list);
  3781. kfree(sums);
  3782. }
  3783. return ret;
  3784. }
  3785. static int log_one_extent(struct btrfs_trans_handle *trans,
  3786. struct btrfs_inode *inode, struct btrfs_root *root,
  3787. const struct extent_map *em,
  3788. struct btrfs_path *path,
  3789. struct btrfs_log_ctx *ctx)
  3790. {
  3791. struct btrfs_root *log = root->log_root;
  3792. struct btrfs_file_extent_item *fi;
  3793. struct extent_buffer *leaf;
  3794. struct btrfs_map_token token;
  3795. struct btrfs_key key;
  3796. u64 extent_offset = em->start - em->orig_start;
  3797. u64 block_len;
  3798. int ret;
  3799. int extent_inserted = 0;
  3800. ret = log_extent_csums(trans, inode, log, em, ctx);
  3801. if (ret)
  3802. return ret;
  3803. ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
  3804. em->start + em->len, NULL, 0, 1,
  3805. sizeof(*fi), &extent_inserted);
  3806. if (ret)
  3807. return ret;
  3808. if (!extent_inserted) {
  3809. key.objectid = btrfs_ino(inode);
  3810. key.type = BTRFS_EXTENT_DATA_KEY;
  3811. key.offset = em->start;
  3812. ret = btrfs_insert_empty_item(trans, log, path, &key,
  3813. sizeof(*fi));
  3814. if (ret)
  3815. return ret;
  3816. }
  3817. leaf = path->nodes[0];
  3818. btrfs_init_map_token(&token, leaf);
  3819. fi = btrfs_item_ptr(leaf, path->slots[0],
  3820. struct btrfs_file_extent_item);
  3821. btrfs_set_token_file_extent_generation(&token, fi, trans->transid);
  3822. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  3823. btrfs_set_token_file_extent_type(&token, fi,
  3824. BTRFS_FILE_EXTENT_PREALLOC);
  3825. else
  3826. btrfs_set_token_file_extent_type(&token, fi,
  3827. BTRFS_FILE_EXTENT_REG);
  3828. block_len = max(em->block_len, em->orig_block_len);
  3829. if (em->compress_type != BTRFS_COMPRESS_NONE) {
  3830. btrfs_set_token_file_extent_disk_bytenr(&token, fi,
  3831. em->block_start);
  3832. btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
  3833. } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  3834. btrfs_set_token_file_extent_disk_bytenr(&token, fi,
  3835. em->block_start -
  3836. extent_offset);
  3837. btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
  3838. } else {
  3839. btrfs_set_token_file_extent_disk_bytenr(&token, fi, 0);
  3840. btrfs_set_token_file_extent_disk_num_bytes(&token, fi, 0);
  3841. }
  3842. btrfs_set_token_file_extent_offset(&token, fi, extent_offset);
  3843. btrfs_set_token_file_extent_num_bytes(&token, fi, em->len);
  3844. btrfs_set_token_file_extent_ram_bytes(&token, fi, em->ram_bytes);
  3845. btrfs_set_token_file_extent_compression(&token, fi, em->compress_type);
  3846. btrfs_set_token_file_extent_encryption(&token, fi, 0);
  3847. btrfs_set_token_file_extent_other_encoding(&token, fi, 0);
  3848. btrfs_mark_buffer_dirty(leaf);
  3849. btrfs_release_path(path);
  3850. return ret;
  3851. }
  3852. /*
  3853. * Log all prealloc extents beyond the inode's i_size to make sure we do not
  3854. * lose them after doing a full/fast fsync and replaying the log. We scan the
  3855. * subvolume's root instead of iterating the inode's extent map tree because
  3856. * otherwise we can log incorrect extent items based on extent map conversion.
  3857. * That can happen due to the fact that extent maps are merged when they
  3858. * are not in the extent map tree's list of modified extents.
  3859. */
  3860. static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
  3861. struct btrfs_inode *inode,
  3862. struct btrfs_path *path)
  3863. {
  3864. struct btrfs_root *root = inode->root;
  3865. struct btrfs_key key;
  3866. const u64 i_size = i_size_read(&inode->vfs_inode);
  3867. const u64 ino = btrfs_ino(inode);
  3868. struct btrfs_path *dst_path = NULL;
  3869. bool dropped_extents = false;
  3870. u64 truncate_offset = i_size;
  3871. struct extent_buffer *leaf;
  3872. int slot;
  3873. int ins_nr = 0;
  3874. int start_slot;
  3875. int ret;
  3876. if (!(inode->flags & BTRFS_INODE_PREALLOC))
  3877. return 0;
  3878. key.objectid = ino;
  3879. key.type = BTRFS_EXTENT_DATA_KEY;
  3880. key.offset = i_size;
  3881. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3882. if (ret < 0)
  3883. goto out;
  3884. /*
  3885. * We must check if there is a prealloc extent that starts before the
  3886. * i_size and crosses the i_size boundary. This is to ensure later we
  3887. * truncate down to the end of that extent and not to the i_size, as
  3888. * otherwise we end up losing part of the prealloc extent after a log
  3889. * replay and with an implicit hole if there is another prealloc extent
  3890. * that starts at an offset beyond i_size.
  3891. */
  3892. ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
  3893. if (ret < 0)
  3894. goto out;
  3895. if (ret == 0) {
  3896. struct btrfs_file_extent_item *ei;
  3897. leaf = path->nodes[0];
  3898. slot = path->slots[0];
  3899. ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  3900. if (btrfs_file_extent_type(leaf, ei) ==
  3901. BTRFS_FILE_EXTENT_PREALLOC) {
  3902. u64 extent_end;
  3903. btrfs_item_key_to_cpu(leaf, &key, slot);
  3904. extent_end = key.offset +
  3905. btrfs_file_extent_num_bytes(leaf, ei);
  3906. if (extent_end > i_size)
  3907. truncate_offset = extent_end;
  3908. }
  3909. } else {
  3910. ret = 0;
  3911. }
  3912. while (true) {
  3913. leaf = path->nodes[0];
  3914. slot = path->slots[0];
  3915. if (slot >= btrfs_header_nritems(leaf)) {
  3916. if (ins_nr > 0) {
  3917. ret = copy_items(trans, inode, dst_path, path,
  3918. start_slot, ins_nr, 1, 0);
  3919. if (ret < 0)
  3920. goto out;
  3921. ins_nr = 0;
  3922. }
  3923. ret = btrfs_next_leaf(root, path);
  3924. if (ret < 0)
  3925. goto out;
  3926. if (ret > 0) {
  3927. ret = 0;
  3928. break;
  3929. }
  3930. continue;
  3931. }
  3932. btrfs_item_key_to_cpu(leaf, &key, slot);
  3933. if (key.objectid > ino)
  3934. break;
  3935. if (WARN_ON_ONCE(key.objectid < ino) ||
  3936. key.type < BTRFS_EXTENT_DATA_KEY ||
  3937. key.offset < i_size) {
  3938. path->slots[0]++;
  3939. continue;
  3940. }
  3941. if (!dropped_extents) {
  3942. /*
  3943. * Avoid logging extent items logged in past fsync calls
  3944. * and leading to duplicate keys in the log tree.
  3945. */
  3946. do {
  3947. ret = btrfs_truncate_inode_items(trans,
  3948. root->log_root,
  3949. &inode->vfs_inode,
  3950. truncate_offset,
  3951. BTRFS_EXTENT_DATA_KEY);
  3952. } while (ret == -EAGAIN);
  3953. if (ret)
  3954. goto out;
  3955. dropped_extents = true;
  3956. }
  3957. if (ins_nr == 0)
  3958. start_slot = slot;
  3959. ins_nr++;
  3960. path->slots[0]++;
  3961. if (!dst_path) {
  3962. dst_path = btrfs_alloc_path();
  3963. if (!dst_path) {
  3964. ret = -ENOMEM;
  3965. goto out;
  3966. }
  3967. }
  3968. }
  3969. if (ins_nr > 0)
  3970. ret = copy_items(trans, inode, dst_path, path,
  3971. start_slot, ins_nr, 1, 0);
  3972. out:
  3973. btrfs_release_path(path);
  3974. btrfs_free_path(dst_path);
  3975. return ret;
  3976. }
  3977. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  3978. struct btrfs_root *root,
  3979. struct btrfs_inode *inode,
  3980. struct btrfs_path *path,
  3981. struct btrfs_log_ctx *ctx)
  3982. {
  3983. struct btrfs_ordered_extent *ordered;
  3984. struct btrfs_ordered_extent *tmp;
  3985. struct extent_map *em, *n;
  3986. struct list_head extents;
  3987. struct extent_map_tree *tree = &inode->extent_tree;
  3988. u64 test_gen;
  3989. int ret = 0;
  3990. int num = 0;
  3991. INIT_LIST_HEAD(&extents);
  3992. write_lock(&tree->lock);
  3993. test_gen = root->fs_info->last_trans_committed;
  3994. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  3995. list_del_init(&em->list);
  3996. /*
  3997. * Just an arbitrary number, this can be really CPU intensive
  3998. * once we start getting a lot of extents, and really once we
  3999. * have a bunch of extents we just want to commit since it will
  4000. * be faster.
  4001. */
  4002. if (++num > 32768) {
  4003. list_del_init(&tree->modified_extents);
  4004. ret = -EFBIG;
  4005. goto process;
  4006. }
  4007. if (em->generation <= test_gen)
  4008. continue;
  4009. /* We log prealloc extents beyond eof later. */
  4010. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
  4011. em->start >= i_size_read(&inode->vfs_inode))
  4012. continue;
  4013. /* Need a ref to keep it from getting evicted from cache */
  4014. refcount_inc(&em->refs);
  4015. set_bit(EXTENT_FLAG_LOGGING, &em->flags);
  4016. list_add_tail(&em->list, &extents);
  4017. num++;
  4018. }
  4019. list_sort(NULL, &extents, extent_cmp);
  4020. process:
  4021. while (!list_empty(&extents)) {
  4022. em = list_entry(extents.next, struct extent_map, list);
  4023. list_del_init(&em->list);
  4024. /*
  4025. * If we had an error we just need to delete everybody from our
  4026. * private list.
  4027. */
  4028. if (ret) {
  4029. clear_em_logging(tree, em);
  4030. free_extent_map(em);
  4031. continue;
  4032. }
  4033. write_unlock(&tree->lock);
  4034. ret = log_one_extent(trans, inode, root, em, path, ctx);
  4035. write_lock(&tree->lock);
  4036. clear_em_logging(tree, em);
  4037. free_extent_map(em);
  4038. }
  4039. WARN_ON(!list_empty(&extents));
  4040. write_unlock(&tree->lock);
  4041. btrfs_release_path(path);
  4042. if (!ret)
  4043. ret = btrfs_log_prealloc_extents(trans, inode, path);
  4044. if (ret)
  4045. return ret;
  4046. /*
  4047. * We have logged all extents successfully, now make sure the commit of
  4048. * the current transaction waits for the ordered extents to complete
  4049. * before it commits and wipes out the log trees, otherwise we would
  4050. * lose data if an ordered extents completes after the transaction
  4051. * commits and a power failure happens after the transaction commit.
  4052. */
  4053. list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
  4054. list_del_init(&ordered->log_list);
  4055. set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
  4056. if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
  4057. spin_lock_irq(&inode->ordered_tree.lock);
  4058. if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
  4059. set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
  4060. atomic_inc(&trans->transaction->pending_ordered);
  4061. }
  4062. spin_unlock_irq(&inode->ordered_tree.lock);
  4063. }
  4064. btrfs_put_ordered_extent(ordered);
  4065. }
  4066. return 0;
  4067. }
  4068. static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
  4069. struct btrfs_path *path, u64 *size_ret)
  4070. {
  4071. struct btrfs_key key;
  4072. int ret;
  4073. key.objectid = btrfs_ino(inode);
  4074. key.type = BTRFS_INODE_ITEM_KEY;
  4075. key.offset = 0;
  4076. ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
  4077. if (ret < 0) {
  4078. return ret;
  4079. } else if (ret > 0) {
  4080. *size_ret = 0;
  4081. } else {
  4082. struct btrfs_inode_item *item;
  4083. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4084. struct btrfs_inode_item);
  4085. *size_ret = btrfs_inode_size(path->nodes[0], item);
  4086. /*
  4087. * If the in-memory inode's i_size is smaller then the inode
  4088. * size stored in the btree, return the inode's i_size, so
  4089. * that we get a correct inode size after replaying the log
  4090. * when before a power failure we had a shrinking truncate
  4091. * followed by addition of a new name (rename / new hard link).
  4092. * Otherwise return the inode size from the btree, to avoid
  4093. * data loss when replaying a log due to previously doing a
  4094. * write that expands the inode's size and logging a new name
  4095. * immediately after.
  4096. */
  4097. if (*size_ret > inode->vfs_inode.i_size)
  4098. *size_ret = inode->vfs_inode.i_size;
  4099. }
  4100. btrfs_release_path(path);
  4101. return 0;
  4102. }
  4103. /*
  4104. * At the moment we always log all xattrs. This is to figure out at log replay
  4105. * time which xattrs must have their deletion replayed. If a xattr is missing
  4106. * in the log tree and exists in the fs/subvol tree, we delete it. This is
  4107. * because if a xattr is deleted, the inode is fsynced and a power failure
  4108. * happens, causing the log to be replayed the next time the fs is mounted,
  4109. * we want the xattr to not exist anymore (same behaviour as other filesystems
  4110. * with a journal, ext3/4, xfs, f2fs, etc).
  4111. */
  4112. static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
  4113. struct btrfs_root *root,
  4114. struct btrfs_inode *inode,
  4115. struct btrfs_path *path,
  4116. struct btrfs_path *dst_path)
  4117. {
  4118. int ret;
  4119. struct btrfs_key key;
  4120. const u64 ino = btrfs_ino(inode);
  4121. int ins_nr = 0;
  4122. int start_slot = 0;
  4123. bool found_xattrs = false;
  4124. if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags))
  4125. return 0;
  4126. key.objectid = ino;
  4127. key.type = BTRFS_XATTR_ITEM_KEY;
  4128. key.offset = 0;
  4129. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4130. if (ret < 0)
  4131. return ret;
  4132. while (true) {
  4133. int slot = path->slots[0];
  4134. struct extent_buffer *leaf = path->nodes[0];
  4135. int nritems = btrfs_header_nritems(leaf);
  4136. if (slot >= nritems) {
  4137. if (ins_nr > 0) {
  4138. ret = copy_items(trans, inode, dst_path, path,
  4139. start_slot, ins_nr, 1, 0);
  4140. if (ret < 0)
  4141. return ret;
  4142. ins_nr = 0;
  4143. }
  4144. ret = btrfs_next_leaf(root, path);
  4145. if (ret < 0)
  4146. return ret;
  4147. else if (ret > 0)
  4148. break;
  4149. continue;
  4150. }
  4151. btrfs_item_key_to_cpu(leaf, &key, slot);
  4152. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
  4153. break;
  4154. if (ins_nr == 0)
  4155. start_slot = slot;
  4156. ins_nr++;
  4157. path->slots[0]++;
  4158. found_xattrs = true;
  4159. cond_resched();
  4160. }
  4161. if (ins_nr > 0) {
  4162. ret = copy_items(trans, inode, dst_path, path,
  4163. start_slot, ins_nr, 1, 0);
  4164. if (ret < 0)
  4165. return ret;
  4166. }
  4167. if (!found_xattrs)
  4168. set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags);
  4169. return 0;
  4170. }
  4171. /*
  4172. * When using the NO_HOLES feature if we punched a hole that causes the
  4173. * deletion of entire leafs or all the extent items of the first leaf (the one
  4174. * that contains the inode item and references) we may end up not processing
  4175. * any extents, because there are no leafs with a generation matching the
  4176. * current transaction that have extent items for our inode. So we need to find
  4177. * if any holes exist and then log them. We also need to log holes after any
  4178. * truncate operation that changes the inode's size.
  4179. */
  4180. static int btrfs_log_holes(struct btrfs_trans_handle *trans,
  4181. struct btrfs_root *root,
  4182. struct btrfs_inode *inode,
  4183. struct btrfs_path *path)
  4184. {
  4185. struct btrfs_fs_info *fs_info = root->fs_info;
  4186. struct btrfs_key key;
  4187. const u64 ino = btrfs_ino(inode);
  4188. const u64 i_size = i_size_read(&inode->vfs_inode);
  4189. u64 prev_extent_end = 0;
  4190. int ret;
  4191. if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
  4192. return 0;
  4193. key.objectid = ino;
  4194. key.type = BTRFS_EXTENT_DATA_KEY;
  4195. key.offset = 0;
  4196. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4197. if (ret < 0)
  4198. return ret;
  4199. while (true) {
  4200. struct extent_buffer *leaf = path->nodes[0];
  4201. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  4202. ret = btrfs_next_leaf(root, path);
  4203. if (ret < 0)
  4204. return ret;
  4205. if (ret > 0) {
  4206. ret = 0;
  4207. break;
  4208. }
  4209. leaf = path->nodes[0];
  4210. }
  4211. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4212. if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
  4213. break;
  4214. /* We have a hole, log it. */
  4215. if (prev_extent_end < key.offset) {
  4216. const u64 hole_len = key.offset - prev_extent_end;
  4217. /*
  4218. * Release the path to avoid deadlocks with other code
  4219. * paths that search the root while holding locks on
  4220. * leafs from the log root.
  4221. */
  4222. btrfs_release_path(path);
  4223. ret = btrfs_insert_file_extent(trans, root->log_root,
  4224. ino, prev_extent_end, 0,
  4225. 0, hole_len, 0, hole_len,
  4226. 0, 0, 0);
  4227. if (ret < 0)
  4228. return ret;
  4229. /*
  4230. * Search for the same key again in the root. Since it's
  4231. * an extent item and we are holding the inode lock, the
  4232. * key must still exist. If it doesn't just emit warning
  4233. * and return an error to fall back to a transaction
  4234. * commit.
  4235. */
  4236. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4237. if (ret < 0)
  4238. return ret;
  4239. if (WARN_ON(ret > 0))
  4240. return -ENOENT;
  4241. leaf = path->nodes[0];
  4242. }
  4243. prev_extent_end = btrfs_file_extent_end(path);
  4244. path->slots[0]++;
  4245. cond_resched();
  4246. }
  4247. if (prev_extent_end < i_size) {
  4248. u64 hole_len;
  4249. btrfs_release_path(path);
  4250. hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
  4251. ret = btrfs_insert_file_extent(trans, root->log_root,
  4252. ino, prev_extent_end, 0, 0,
  4253. hole_len, 0, hole_len,
  4254. 0, 0, 0);
  4255. if (ret < 0)
  4256. return ret;
  4257. }
  4258. return 0;
  4259. }
  4260. /*
  4261. * When we are logging a new inode X, check if it doesn't have a reference that
  4262. * matches the reference from some other inode Y created in a past transaction
  4263. * and that was renamed in the current transaction. If we don't do this, then at
  4264. * log replay time we can lose inode Y (and all its files if it's a directory):
  4265. *
  4266. * mkdir /mnt/x
  4267. * echo "hello world" > /mnt/x/foobar
  4268. * sync
  4269. * mv /mnt/x /mnt/y
  4270. * mkdir /mnt/x # or touch /mnt/x
  4271. * xfs_io -c fsync /mnt/x
  4272. * <power fail>
  4273. * mount fs, trigger log replay
  4274. *
  4275. * After the log replay procedure, we would lose the first directory and all its
  4276. * files (file foobar).
  4277. * For the case where inode Y is not a directory we simply end up losing it:
  4278. *
  4279. * echo "123" > /mnt/foo
  4280. * sync
  4281. * mv /mnt/foo /mnt/bar
  4282. * echo "abc" > /mnt/foo
  4283. * xfs_io -c fsync /mnt/foo
  4284. * <power fail>
  4285. *
  4286. * We also need this for cases where a snapshot entry is replaced by some other
  4287. * entry (file or directory) otherwise we end up with an unreplayable log due to
  4288. * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
  4289. * if it were a regular entry:
  4290. *
  4291. * mkdir /mnt/x
  4292. * btrfs subvolume snapshot /mnt /mnt/x/snap
  4293. * btrfs subvolume delete /mnt/x/snap
  4294. * rmdir /mnt/x
  4295. * mkdir /mnt/x
  4296. * fsync /mnt/x or fsync some new file inside it
  4297. * <power fail>
  4298. *
  4299. * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
  4300. * the same transaction.
  4301. */
  4302. static int btrfs_check_ref_name_override(struct extent_buffer *eb,
  4303. const int slot,
  4304. const struct btrfs_key *key,
  4305. struct btrfs_inode *inode,
  4306. u64 *other_ino, u64 *other_parent)
  4307. {
  4308. int ret;
  4309. struct btrfs_path *search_path;
  4310. char *name = NULL;
  4311. u32 name_len = 0;
  4312. u32 item_size = btrfs_item_size_nr(eb, slot);
  4313. u32 cur_offset = 0;
  4314. unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
  4315. search_path = btrfs_alloc_path();
  4316. if (!search_path)
  4317. return -ENOMEM;
  4318. search_path->search_commit_root = 1;
  4319. search_path->skip_locking = 1;
  4320. while (cur_offset < item_size) {
  4321. u64 parent;
  4322. u32 this_name_len;
  4323. u32 this_len;
  4324. unsigned long name_ptr;
  4325. struct btrfs_dir_item *di;
  4326. if (key->type == BTRFS_INODE_REF_KEY) {
  4327. struct btrfs_inode_ref *iref;
  4328. iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
  4329. parent = key->offset;
  4330. this_name_len = btrfs_inode_ref_name_len(eb, iref);
  4331. name_ptr = (unsigned long)(iref + 1);
  4332. this_len = sizeof(*iref) + this_name_len;
  4333. } else {
  4334. struct btrfs_inode_extref *extref;
  4335. extref = (struct btrfs_inode_extref *)(ptr +
  4336. cur_offset);
  4337. parent = btrfs_inode_extref_parent(eb, extref);
  4338. this_name_len = btrfs_inode_extref_name_len(eb, extref);
  4339. name_ptr = (unsigned long)&extref->name;
  4340. this_len = sizeof(*extref) + this_name_len;
  4341. }
  4342. if (this_name_len > name_len) {
  4343. char *new_name;
  4344. new_name = krealloc(name, this_name_len, GFP_NOFS);
  4345. if (!new_name) {
  4346. ret = -ENOMEM;
  4347. goto out;
  4348. }
  4349. name_len = this_name_len;
  4350. name = new_name;
  4351. }
  4352. read_extent_buffer(eb, name, name_ptr, this_name_len);
  4353. di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
  4354. parent, name, this_name_len, 0);
  4355. if (di && !IS_ERR(di)) {
  4356. struct btrfs_key di_key;
  4357. btrfs_dir_item_key_to_cpu(search_path->nodes[0],
  4358. di, &di_key);
  4359. if (di_key.type == BTRFS_INODE_ITEM_KEY) {
  4360. if (di_key.objectid != key->objectid) {
  4361. ret = 1;
  4362. *other_ino = di_key.objectid;
  4363. *other_parent = parent;
  4364. } else {
  4365. ret = 0;
  4366. }
  4367. } else {
  4368. ret = -EAGAIN;
  4369. }
  4370. goto out;
  4371. } else if (IS_ERR(di)) {
  4372. ret = PTR_ERR(di);
  4373. goto out;
  4374. }
  4375. btrfs_release_path(search_path);
  4376. cur_offset += this_len;
  4377. }
  4378. ret = 0;
  4379. out:
  4380. btrfs_free_path(search_path);
  4381. kfree(name);
  4382. return ret;
  4383. }
  4384. struct btrfs_ino_list {
  4385. u64 ino;
  4386. u64 parent;
  4387. struct list_head list;
  4388. };
  4389. static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
  4390. struct btrfs_root *root,
  4391. struct btrfs_path *path,
  4392. struct btrfs_log_ctx *ctx,
  4393. u64 ino, u64 parent)
  4394. {
  4395. struct btrfs_ino_list *ino_elem;
  4396. LIST_HEAD(inode_list);
  4397. int ret = 0;
  4398. ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
  4399. if (!ino_elem)
  4400. return -ENOMEM;
  4401. ino_elem->ino = ino;
  4402. ino_elem->parent = parent;
  4403. list_add_tail(&ino_elem->list, &inode_list);
  4404. while (!list_empty(&inode_list)) {
  4405. struct btrfs_fs_info *fs_info = root->fs_info;
  4406. struct btrfs_key key;
  4407. struct inode *inode;
  4408. ino_elem = list_first_entry(&inode_list, struct btrfs_ino_list,
  4409. list);
  4410. ino = ino_elem->ino;
  4411. parent = ino_elem->parent;
  4412. list_del(&ino_elem->list);
  4413. kfree(ino_elem);
  4414. if (ret)
  4415. continue;
  4416. btrfs_release_path(path);
  4417. inode = btrfs_iget(fs_info->sb, ino, root);
  4418. /*
  4419. * If the other inode that had a conflicting dir entry was
  4420. * deleted in the current transaction, we need to log its parent
  4421. * directory.
  4422. */
  4423. if (IS_ERR(inode)) {
  4424. ret = PTR_ERR(inode);
  4425. if (ret == -ENOENT) {
  4426. inode = btrfs_iget(fs_info->sb, parent, root);
  4427. if (IS_ERR(inode)) {
  4428. ret = PTR_ERR(inode);
  4429. } else {
  4430. ret = btrfs_log_inode(trans, root,
  4431. BTRFS_I(inode),
  4432. LOG_OTHER_INODE_ALL,
  4433. ctx);
  4434. btrfs_add_delayed_iput(inode);
  4435. }
  4436. }
  4437. continue;
  4438. }
  4439. /*
  4440. * If the inode was already logged skip it - otherwise we can
  4441. * hit an infinite loop. Example:
  4442. *
  4443. * From the commit root (previous transaction) we have the
  4444. * following inodes:
  4445. *
  4446. * inode 257 a directory
  4447. * inode 258 with references "zz" and "zz_link" on inode 257
  4448. * inode 259 with reference "a" on inode 257
  4449. *
  4450. * And in the current (uncommitted) transaction we have:
  4451. *
  4452. * inode 257 a directory, unchanged
  4453. * inode 258 with references "a" and "a2" on inode 257
  4454. * inode 259 with reference "zz_link" on inode 257
  4455. * inode 261 with reference "zz" on inode 257
  4456. *
  4457. * When logging inode 261 the following infinite loop could
  4458. * happen if we don't skip already logged inodes:
  4459. *
  4460. * - we detect inode 258 as a conflicting inode, with inode 261
  4461. * on reference "zz", and log it;
  4462. *
  4463. * - we detect inode 259 as a conflicting inode, with inode 258
  4464. * on reference "a", and log it;
  4465. *
  4466. * - we detect inode 258 as a conflicting inode, with inode 259
  4467. * on reference "zz_link", and log it - again! After this we
  4468. * repeat the above steps forever.
  4469. */
  4470. spin_lock(&BTRFS_I(inode)->lock);
  4471. /*
  4472. * Check the inode's logged_trans only instead of
  4473. * btrfs_inode_in_log(). This is because the last_log_commit of
  4474. * the inode is not updated when we only log that it exists and
  4475. * it has the full sync bit set (see btrfs_log_inode()).
  4476. */
  4477. if (BTRFS_I(inode)->logged_trans == trans->transid) {
  4478. spin_unlock(&BTRFS_I(inode)->lock);
  4479. btrfs_add_delayed_iput(inode);
  4480. continue;
  4481. }
  4482. spin_unlock(&BTRFS_I(inode)->lock);
  4483. /*
  4484. * We are safe logging the other inode without acquiring its
  4485. * lock as long as we log with the LOG_INODE_EXISTS mode. We
  4486. * are safe against concurrent renames of the other inode as
  4487. * well because during a rename we pin the log and update the
  4488. * log with the new name before we unpin it.
  4489. */
  4490. ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
  4491. LOG_OTHER_INODE, ctx);
  4492. if (ret) {
  4493. btrfs_add_delayed_iput(inode);
  4494. continue;
  4495. }
  4496. key.objectid = ino;
  4497. key.type = BTRFS_INODE_REF_KEY;
  4498. key.offset = 0;
  4499. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4500. if (ret < 0) {
  4501. btrfs_add_delayed_iput(inode);
  4502. continue;
  4503. }
  4504. while (true) {
  4505. struct extent_buffer *leaf = path->nodes[0];
  4506. int slot = path->slots[0];
  4507. u64 other_ino = 0;
  4508. u64 other_parent = 0;
  4509. if (slot >= btrfs_header_nritems(leaf)) {
  4510. ret = btrfs_next_leaf(root, path);
  4511. if (ret < 0) {
  4512. break;
  4513. } else if (ret > 0) {
  4514. ret = 0;
  4515. break;
  4516. }
  4517. continue;
  4518. }
  4519. btrfs_item_key_to_cpu(leaf, &key, slot);
  4520. if (key.objectid != ino ||
  4521. (key.type != BTRFS_INODE_REF_KEY &&
  4522. key.type != BTRFS_INODE_EXTREF_KEY)) {
  4523. ret = 0;
  4524. break;
  4525. }
  4526. ret = btrfs_check_ref_name_override(leaf, slot, &key,
  4527. BTRFS_I(inode), &other_ino,
  4528. &other_parent);
  4529. if (ret < 0)
  4530. break;
  4531. if (ret > 0) {
  4532. ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
  4533. if (!ino_elem) {
  4534. ret = -ENOMEM;
  4535. break;
  4536. }
  4537. ino_elem->ino = other_ino;
  4538. ino_elem->parent = other_parent;
  4539. list_add_tail(&ino_elem->list, &inode_list);
  4540. ret = 0;
  4541. }
  4542. path->slots[0]++;
  4543. }
  4544. btrfs_add_delayed_iput(inode);
  4545. }
  4546. return ret;
  4547. }
  4548. static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,
  4549. struct btrfs_inode *inode,
  4550. struct btrfs_key *min_key,
  4551. const struct btrfs_key *max_key,
  4552. struct btrfs_path *path,
  4553. struct btrfs_path *dst_path,
  4554. const u64 logged_isize,
  4555. const bool recursive_logging,
  4556. const int inode_only,
  4557. struct btrfs_log_ctx *ctx,
  4558. bool *need_log_inode_item)
  4559. {
  4560. const u64 i_size = i_size_read(&inode->vfs_inode);
  4561. struct btrfs_root *root = inode->root;
  4562. int ins_start_slot = 0;
  4563. int ins_nr = 0;
  4564. int ret;
  4565. while (1) {
  4566. ret = btrfs_search_forward(root, min_key, path, trans->transid);
  4567. if (ret < 0)
  4568. return ret;
  4569. if (ret > 0) {
  4570. ret = 0;
  4571. break;
  4572. }
  4573. again:
  4574. /* Note, ins_nr might be > 0 here, cleanup outside the loop */
  4575. if (min_key->objectid != max_key->objectid)
  4576. break;
  4577. if (min_key->type > max_key->type)
  4578. break;
  4579. if (min_key->type == BTRFS_INODE_ITEM_KEY) {
  4580. *need_log_inode_item = false;
  4581. } else if (min_key->type == BTRFS_EXTENT_DATA_KEY &&
  4582. min_key->offset >= i_size) {
  4583. /*
  4584. * Extents at and beyond eof are logged with
  4585. * btrfs_log_prealloc_extents().
  4586. * Only regular files have BTRFS_EXTENT_DATA_KEY keys,
  4587. * and no keys greater than that, so bail out.
  4588. */
  4589. break;
  4590. } else if ((min_key->type == BTRFS_INODE_REF_KEY ||
  4591. min_key->type == BTRFS_INODE_EXTREF_KEY) &&
  4592. inode->generation == trans->transid &&
  4593. !recursive_logging) {
  4594. u64 other_ino = 0;
  4595. u64 other_parent = 0;
  4596. ret = btrfs_check_ref_name_override(path->nodes[0],
  4597. path->slots[0], min_key, inode,
  4598. &other_ino, &other_parent);
  4599. if (ret < 0) {
  4600. return ret;
  4601. } else if (ret > 0 && ctx &&
  4602. other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
  4603. if (ins_nr > 0) {
  4604. ins_nr++;
  4605. } else {
  4606. ins_nr = 1;
  4607. ins_start_slot = path->slots[0];
  4608. }
  4609. ret = copy_items(trans, inode, dst_path, path,
  4610. ins_start_slot, ins_nr,
  4611. inode_only, logged_isize);
  4612. if (ret < 0)
  4613. return ret;
  4614. ins_nr = 0;
  4615. ret = log_conflicting_inodes(trans, root, path,
  4616. ctx, other_ino, other_parent);
  4617. if (ret)
  4618. return ret;
  4619. btrfs_release_path(path);
  4620. goto next_key;
  4621. }
  4622. } else if (min_key->type == BTRFS_XATTR_ITEM_KEY) {
  4623. /* Skip xattrs, logged later with btrfs_log_all_xattrs() */
  4624. if (ins_nr == 0)
  4625. goto next_slot;
  4626. ret = copy_items(trans, inode, dst_path, path,
  4627. ins_start_slot,
  4628. ins_nr, inode_only, logged_isize);
  4629. if (ret < 0)
  4630. return ret;
  4631. ins_nr = 0;
  4632. goto next_slot;
  4633. }
  4634. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  4635. ins_nr++;
  4636. goto next_slot;
  4637. } else if (!ins_nr) {
  4638. ins_start_slot = path->slots[0];
  4639. ins_nr = 1;
  4640. goto next_slot;
  4641. }
  4642. ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
  4643. ins_nr, inode_only, logged_isize);
  4644. if (ret < 0)
  4645. return ret;
  4646. ins_nr = 1;
  4647. ins_start_slot = path->slots[0];
  4648. next_slot:
  4649. path->slots[0]++;
  4650. if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
  4651. btrfs_item_key_to_cpu(path->nodes[0], min_key,
  4652. path->slots[0]);
  4653. goto again;
  4654. }
  4655. if (ins_nr) {
  4656. ret = copy_items(trans, inode, dst_path, path,
  4657. ins_start_slot, ins_nr, inode_only,
  4658. logged_isize);
  4659. if (ret < 0)
  4660. return ret;
  4661. ins_nr = 0;
  4662. }
  4663. btrfs_release_path(path);
  4664. next_key:
  4665. if (min_key->offset < (u64)-1) {
  4666. min_key->offset++;
  4667. } else if (min_key->type < max_key->type) {
  4668. min_key->type++;
  4669. min_key->offset = 0;
  4670. } else {
  4671. break;
  4672. }
  4673. }
  4674. if (ins_nr) {
  4675. ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
  4676. ins_nr, inode_only, logged_isize);
  4677. if (ret)
  4678. return ret;
  4679. }
  4680. if (inode_only == LOG_INODE_ALL && S_ISREG(inode->vfs_inode.i_mode)) {
  4681. /*
  4682. * Release the path because otherwise we might attempt to double
  4683. * lock the same leaf with btrfs_log_prealloc_extents() below.
  4684. */
  4685. btrfs_release_path(path);
  4686. ret = btrfs_log_prealloc_extents(trans, inode, dst_path);
  4687. }
  4688. return ret;
  4689. }
  4690. /* log a single inode in the tree log.
  4691. * At least one parent directory for this inode must exist in the tree
  4692. * or be logged already.
  4693. *
  4694. * Any items from this inode changed by the current transaction are copied
  4695. * to the log tree. An extra reference is taken on any extents in this
  4696. * file, allowing us to avoid a whole pile of corner cases around logging
  4697. * blocks that have been removed from the tree.
  4698. *
  4699. * See LOG_INODE_ALL and related defines for a description of what inode_only
  4700. * does.
  4701. *
  4702. * This handles both files and directories.
  4703. */
  4704. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  4705. struct btrfs_root *root, struct btrfs_inode *inode,
  4706. int inode_only,
  4707. struct btrfs_log_ctx *ctx)
  4708. {
  4709. struct btrfs_path *path;
  4710. struct btrfs_path *dst_path;
  4711. struct btrfs_key min_key;
  4712. struct btrfs_key max_key;
  4713. struct btrfs_root *log = root->log_root;
  4714. int err = 0;
  4715. int ret = 0;
  4716. bool fast_search = false;
  4717. u64 ino = btrfs_ino(inode);
  4718. struct extent_map_tree *em_tree = &inode->extent_tree;
  4719. u64 logged_isize = 0;
  4720. bool need_log_inode_item = true;
  4721. bool xattrs_logged = false;
  4722. bool recursive_logging = false;
  4723. path = btrfs_alloc_path();
  4724. if (!path)
  4725. return -ENOMEM;
  4726. dst_path = btrfs_alloc_path();
  4727. if (!dst_path) {
  4728. btrfs_free_path(path);
  4729. return -ENOMEM;
  4730. }
  4731. min_key.objectid = ino;
  4732. min_key.type = BTRFS_INODE_ITEM_KEY;
  4733. min_key.offset = 0;
  4734. max_key.objectid = ino;
  4735. /* today the code can only do partial logging of directories */
  4736. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  4737. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4738. &inode->runtime_flags) &&
  4739. inode_only >= LOG_INODE_EXISTS))
  4740. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4741. else
  4742. max_key.type = (u8)-1;
  4743. max_key.offset = (u64)-1;
  4744. /*
  4745. * Only run delayed items if we are a directory. We want to make sure
  4746. * all directory indexes hit the fs/subvolume tree so we can find them
  4747. * and figure out which index ranges have to be logged.
  4748. *
  4749. * Otherwise commit the delayed inode only if the full sync flag is set,
  4750. * as we want to make sure an up to date version is in the subvolume
  4751. * tree so copy_inode_items_to_log() / copy_items() can find it and copy
  4752. * it to the log tree. For a non full sync, we always log the inode item
  4753. * based on the in-memory struct btrfs_inode which is always up to date.
  4754. */
  4755. if (S_ISDIR(inode->vfs_inode.i_mode))
  4756. ret = btrfs_commit_inode_delayed_items(trans, inode);
  4757. else if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
  4758. ret = btrfs_commit_inode_delayed_inode(inode);
  4759. if (ret) {
  4760. btrfs_free_path(path);
  4761. btrfs_free_path(dst_path);
  4762. return ret;
  4763. }
  4764. if (inode_only == LOG_OTHER_INODE || inode_only == LOG_OTHER_INODE_ALL) {
  4765. recursive_logging = true;
  4766. if (inode_only == LOG_OTHER_INODE)
  4767. inode_only = LOG_INODE_EXISTS;
  4768. else
  4769. inode_only = LOG_INODE_ALL;
  4770. mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
  4771. } else {
  4772. mutex_lock(&inode->log_mutex);
  4773. }
  4774. /*
  4775. * a brute force approach to making sure we get the most uptodate
  4776. * copies of everything.
  4777. */
  4778. if (S_ISDIR(inode->vfs_inode.i_mode)) {
  4779. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  4780. if (inode_only == LOG_INODE_EXISTS)
  4781. max_key_type = BTRFS_XATTR_ITEM_KEY;
  4782. ret = drop_objectid_items(trans, log, path, ino, max_key_type);
  4783. } else {
  4784. if (inode_only == LOG_INODE_EXISTS) {
  4785. /*
  4786. * Make sure the new inode item we write to the log has
  4787. * the same isize as the current one (if it exists).
  4788. * This is necessary to prevent data loss after log
  4789. * replay, and also to prevent doing a wrong expanding
  4790. * truncate - for e.g. create file, write 4K into offset
  4791. * 0, fsync, write 4K into offset 4096, add hard link,
  4792. * fsync some other file (to sync log), power fail - if
  4793. * we use the inode's current i_size, after log replay
  4794. * we get a 8Kb file, with the last 4Kb extent as a hole
  4795. * (zeroes), as if an expanding truncate happened,
  4796. * instead of getting a file of 4Kb only.
  4797. */
  4798. err = logged_inode_size(log, inode, path, &logged_isize);
  4799. if (err)
  4800. goto out_unlock;
  4801. }
  4802. if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4803. &inode->runtime_flags)) {
  4804. if (inode_only == LOG_INODE_EXISTS) {
  4805. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4806. ret = drop_objectid_items(trans, log, path, ino,
  4807. max_key.type);
  4808. } else {
  4809. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4810. &inode->runtime_flags);
  4811. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4812. &inode->runtime_flags);
  4813. while(1) {
  4814. ret = btrfs_truncate_inode_items(trans,
  4815. log, &inode->vfs_inode, 0, 0);
  4816. if (ret != -EAGAIN)
  4817. break;
  4818. }
  4819. }
  4820. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4821. &inode->runtime_flags) ||
  4822. inode_only == LOG_INODE_EXISTS) {
  4823. if (inode_only == LOG_INODE_ALL)
  4824. fast_search = true;
  4825. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4826. ret = drop_objectid_items(trans, log, path, ino,
  4827. max_key.type);
  4828. } else {
  4829. if (inode_only == LOG_INODE_ALL)
  4830. fast_search = true;
  4831. goto log_extents;
  4832. }
  4833. }
  4834. if (ret) {
  4835. err = ret;
  4836. goto out_unlock;
  4837. }
  4838. err = copy_inode_items_to_log(trans, inode, &min_key, &max_key,
  4839. path, dst_path, logged_isize,
  4840. recursive_logging, inode_only, ctx,
  4841. &need_log_inode_item);
  4842. if (err)
  4843. goto out_unlock;
  4844. btrfs_release_path(path);
  4845. btrfs_release_path(dst_path);
  4846. err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
  4847. if (err)
  4848. goto out_unlock;
  4849. xattrs_logged = true;
  4850. if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
  4851. btrfs_release_path(path);
  4852. btrfs_release_path(dst_path);
  4853. err = btrfs_log_holes(trans, root, inode, path);
  4854. if (err)
  4855. goto out_unlock;
  4856. }
  4857. log_extents:
  4858. btrfs_release_path(path);
  4859. btrfs_release_path(dst_path);
  4860. if (need_log_inode_item) {
  4861. err = log_inode_item(trans, log, dst_path, inode);
  4862. if (!err && !xattrs_logged) {
  4863. err = btrfs_log_all_xattrs(trans, root, inode, path,
  4864. dst_path);
  4865. btrfs_release_path(path);
  4866. }
  4867. if (err)
  4868. goto out_unlock;
  4869. }
  4870. if (fast_search) {
  4871. ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
  4872. ctx);
  4873. if (ret) {
  4874. err = ret;
  4875. goto out_unlock;
  4876. }
  4877. } else if (inode_only == LOG_INODE_ALL) {
  4878. struct extent_map *em, *n;
  4879. write_lock(&em_tree->lock);
  4880. list_for_each_entry_safe(em, n, &em_tree->modified_extents, list)
  4881. list_del_init(&em->list);
  4882. write_unlock(&em_tree->lock);
  4883. }
  4884. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
  4885. ret = log_directory_changes(trans, root, inode, path, dst_path,
  4886. ctx);
  4887. if (ret) {
  4888. err = ret;
  4889. goto out_unlock;
  4890. }
  4891. }
  4892. /*
  4893. * If we are logging that an ancestor inode exists as part of logging a
  4894. * new name from a link or rename operation, don't mark the inode as
  4895. * logged - otherwise if an explicit fsync is made against an ancestor,
  4896. * the fsync considers the inode in the log and doesn't sync the log,
  4897. * resulting in the ancestor missing after a power failure unless the
  4898. * log was synced as part of an fsync against any other unrelated inode.
  4899. * So keep it simple for this case and just don't flag the ancestors as
  4900. * logged.
  4901. */
  4902. if (!ctx ||
  4903. !(S_ISDIR(inode->vfs_inode.i_mode) && ctx->logging_new_name &&
  4904. &inode->vfs_inode != ctx->inode)) {
  4905. spin_lock(&inode->lock);
  4906. inode->logged_trans = trans->transid;
  4907. /*
  4908. * Don't update last_log_commit if we logged that an inode exists
  4909. * after it was loaded to memory (full_sync bit set).
  4910. * This is to prevent data loss when we do a write to the inode,
  4911. * then the inode gets evicted after all delalloc was flushed,
  4912. * then we log it exists (due to a rename for example) and then
  4913. * fsync it. This last fsync would do nothing (not logging the
  4914. * extents previously written).
  4915. */
  4916. if (inode_only != LOG_INODE_EXISTS ||
  4917. !test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
  4918. inode->last_log_commit = inode->last_sub_trans;
  4919. spin_unlock(&inode->lock);
  4920. }
  4921. out_unlock:
  4922. mutex_unlock(&inode->log_mutex);
  4923. btrfs_free_path(path);
  4924. btrfs_free_path(dst_path);
  4925. return err;
  4926. }
  4927. /*
  4928. * Check if we must fallback to a transaction commit when logging an inode.
  4929. * This must be called after logging the inode and is used only in the context
  4930. * when fsyncing an inode requires the need to log some other inode - in which
  4931. * case we can't lock the i_mutex of each other inode we need to log as that
  4932. * can lead to deadlocks with concurrent fsync against other inodes (as we can
  4933. * log inodes up or down in the hierarchy) or rename operations for example. So
  4934. * we take the log_mutex of the inode after we have logged it and then check for
  4935. * its last_unlink_trans value - this is safe because any task setting
  4936. * last_unlink_trans must take the log_mutex and it must do this before it does
  4937. * the actual unlink operation, so if we do this check before a concurrent task
  4938. * sets last_unlink_trans it means we've logged a consistent version/state of
  4939. * all the inode items, otherwise we are not sure and must do a transaction
  4940. * commit (the concurrent task might have only updated last_unlink_trans before
  4941. * we logged the inode or it might have also done the unlink).
  4942. */
  4943. static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
  4944. struct btrfs_inode *inode)
  4945. {
  4946. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  4947. bool ret = false;
  4948. mutex_lock(&inode->log_mutex);
  4949. if (inode->last_unlink_trans > fs_info->last_trans_committed) {
  4950. /*
  4951. * Make sure any commits to the log are forced to be full
  4952. * commits.
  4953. */
  4954. btrfs_set_log_full_commit(trans);
  4955. ret = true;
  4956. }
  4957. mutex_unlock(&inode->log_mutex);
  4958. return ret;
  4959. }
  4960. /*
  4961. * follow the dentry parent pointers up the chain and see if any
  4962. * of the directories in it require a full commit before they can
  4963. * be logged. Returns zero if nothing special needs to be done or 1 if
  4964. * a full commit is required.
  4965. */
  4966. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  4967. struct btrfs_inode *inode,
  4968. struct dentry *parent,
  4969. struct super_block *sb,
  4970. u64 last_committed)
  4971. {
  4972. int ret = 0;
  4973. struct dentry *old_parent = NULL;
  4974. /*
  4975. * for regular files, if its inode is already on disk, we don't
  4976. * have to worry about the parents at all. This is because
  4977. * we can use the last_unlink_trans field to record renames
  4978. * and other fun in this file.
  4979. */
  4980. if (S_ISREG(inode->vfs_inode.i_mode) &&
  4981. inode->generation <= last_committed &&
  4982. inode->last_unlink_trans <= last_committed)
  4983. goto out;
  4984. if (!S_ISDIR(inode->vfs_inode.i_mode)) {
  4985. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4986. goto out;
  4987. inode = BTRFS_I(d_inode(parent));
  4988. }
  4989. while (1) {
  4990. if (btrfs_must_commit_transaction(trans, inode)) {
  4991. ret = 1;
  4992. break;
  4993. }
  4994. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4995. break;
  4996. if (IS_ROOT(parent)) {
  4997. inode = BTRFS_I(d_inode(parent));
  4998. if (btrfs_must_commit_transaction(trans, inode))
  4999. ret = 1;
  5000. break;
  5001. }
  5002. parent = dget_parent(parent);
  5003. dput(old_parent);
  5004. old_parent = parent;
  5005. inode = BTRFS_I(d_inode(parent));
  5006. }
  5007. dput(old_parent);
  5008. out:
  5009. return ret;
  5010. }
  5011. struct btrfs_dir_list {
  5012. u64 ino;
  5013. struct list_head list;
  5014. };
  5015. /*
  5016. * Log the inodes of the new dentries of a directory. See log_dir_items() for
  5017. * details about the why it is needed.
  5018. * This is a recursive operation - if an existing dentry corresponds to a
  5019. * directory, that directory's new entries are logged too (same behaviour as
  5020. * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
  5021. * the dentries point to we do not lock their i_mutex, otherwise lockdep
  5022. * complains about the following circular lock dependency / possible deadlock:
  5023. *
  5024. * CPU0 CPU1
  5025. * ---- ----
  5026. * lock(&type->i_mutex_dir_key#3/2);
  5027. * lock(sb_internal#2);
  5028. * lock(&type->i_mutex_dir_key#3/2);
  5029. * lock(&sb->s_type->i_mutex_key#14);
  5030. *
  5031. * Where sb_internal is the lock (a counter that works as a lock) acquired by
  5032. * sb_start_intwrite() in btrfs_start_transaction().
  5033. * Not locking i_mutex of the inodes is still safe because:
  5034. *
  5035. * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
  5036. * that while logging the inode new references (names) are added or removed
  5037. * from the inode, leaving the logged inode item with a link count that does
  5038. * not match the number of logged inode reference items. This is fine because
  5039. * at log replay time we compute the real number of links and correct the
  5040. * link count in the inode item (see replay_one_buffer() and
  5041. * link_to_fixup_dir());
  5042. *
  5043. * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
  5044. * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
  5045. * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
  5046. * has a size that doesn't match the sum of the lengths of all the logged
  5047. * names. This does not result in a problem because if a dir_item key is
  5048. * logged but its matching dir_index key is not logged, at log replay time we
  5049. * don't use it to replay the respective name (see replay_one_name()). On the
  5050. * other hand if only the dir_index key ends up being logged, the respective
  5051. * name is added to the fs/subvol tree with both the dir_item and dir_index
  5052. * keys created (see replay_one_name()).
  5053. * The directory's inode item with a wrong i_size is not a problem as well,
  5054. * since we don't use it at log replay time to set the i_size in the inode
  5055. * item of the fs/subvol tree (see overwrite_item()).
  5056. */
  5057. static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
  5058. struct btrfs_root *root,
  5059. struct btrfs_inode *start_inode,
  5060. struct btrfs_log_ctx *ctx)
  5061. {
  5062. struct btrfs_fs_info *fs_info = root->fs_info;
  5063. struct btrfs_root *log = root->log_root;
  5064. struct btrfs_path *path;
  5065. LIST_HEAD(dir_list);
  5066. struct btrfs_dir_list *dir_elem;
  5067. int ret = 0;
  5068. path = btrfs_alloc_path();
  5069. if (!path)
  5070. return -ENOMEM;
  5071. dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
  5072. if (!dir_elem) {
  5073. btrfs_free_path(path);
  5074. return -ENOMEM;
  5075. }
  5076. dir_elem->ino = btrfs_ino(start_inode);
  5077. list_add_tail(&dir_elem->list, &dir_list);
  5078. while (!list_empty(&dir_list)) {
  5079. struct extent_buffer *leaf;
  5080. struct btrfs_key min_key;
  5081. int nritems;
  5082. int i;
  5083. dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
  5084. list);
  5085. if (ret)
  5086. goto next_dir_inode;
  5087. min_key.objectid = dir_elem->ino;
  5088. min_key.type = BTRFS_DIR_ITEM_KEY;
  5089. min_key.offset = 0;
  5090. again:
  5091. btrfs_release_path(path);
  5092. ret = btrfs_search_forward(log, &min_key, path, trans->transid);
  5093. if (ret < 0) {
  5094. goto next_dir_inode;
  5095. } else if (ret > 0) {
  5096. ret = 0;
  5097. goto next_dir_inode;
  5098. }
  5099. process_leaf:
  5100. leaf = path->nodes[0];
  5101. nritems = btrfs_header_nritems(leaf);
  5102. for (i = path->slots[0]; i < nritems; i++) {
  5103. struct btrfs_dir_item *di;
  5104. struct btrfs_key di_key;
  5105. struct inode *di_inode;
  5106. struct btrfs_dir_list *new_dir_elem;
  5107. int log_mode = LOG_INODE_EXISTS;
  5108. int type;
  5109. btrfs_item_key_to_cpu(leaf, &min_key, i);
  5110. if (min_key.objectid != dir_elem->ino ||
  5111. min_key.type != BTRFS_DIR_ITEM_KEY)
  5112. goto next_dir_inode;
  5113. di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
  5114. type = btrfs_dir_type(leaf, di);
  5115. if (btrfs_dir_transid(leaf, di) < trans->transid &&
  5116. type != BTRFS_FT_DIR)
  5117. continue;
  5118. btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
  5119. if (di_key.type == BTRFS_ROOT_ITEM_KEY)
  5120. continue;
  5121. btrfs_release_path(path);
  5122. di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root);
  5123. if (IS_ERR(di_inode)) {
  5124. ret = PTR_ERR(di_inode);
  5125. goto next_dir_inode;
  5126. }
  5127. if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
  5128. btrfs_add_delayed_iput(di_inode);
  5129. break;
  5130. }
  5131. ctx->log_new_dentries = false;
  5132. if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
  5133. log_mode = LOG_INODE_ALL;
  5134. ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
  5135. log_mode, ctx);
  5136. if (!ret &&
  5137. btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
  5138. ret = 1;
  5139. btrfs_add_delayed_iput(di_inode);
  5140. if (ret)
  5141. goto next_dir_inode;
  5142. if (ctx->log_new_dentries) {
  5143. new_dir_elem = kmalloc(sizeof(*new_dir_elem),
  5144. GFP_NOFS);
  5145. if (!new_dir_elem) {
  5146. ret = -ENOMEM;
  5147. goto next_dir_inode;
  5148. }
  5149. new_dir_elem->ino = di_key.objectid;
  5150. list_add_tail(&new_dir_elem->list, &dir_list);
  5151. }
  5152. break;
  5153. }
  5154. if (i == nritems) {
  5155. ret = btrfs_next_leaf(log, path);
  5156. if (ret < 0) {
  5157. goto next_dir_inode;
  5158. } else if (ret > 0) {
  5159. ret = 0;
  5160. goto next_dir_inode;
  5161. }
  5162. goto process_leaf;
  5163. }
  5164. if (min_key.offset < (u64)-1) {
  5165. min_key.offset++;
  5166. goto again;
  5167. }
  5168. next_dir_inode:
  5169. list_del(&dir_elem->list);
  5170. kfree(dir_elem);
  5171. }
  5172. btrfs_free_path(path);
  5173. return ret;
  5174. }
  5175. static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
  5176. struct btrfs_inode *inode,
  5177. struct btrfs_log_ctx *ctx)
  5178. {
  5179. struct btrfs_fs_info *fs_info = trans->fs_info;
  5180. int ret;
  5181. struct btrfs_path *path;
  5182. struct btrfs_key key;
  5183. struct btrfs_root *root = inode->root;
  5184. const u64 ino = btrfs_ino(inode);
  5185. path = btrfs_alloc_path();
  5186. if (!path)
  5187. return -ENOMEM;
  5188. path->skip_locking = 1;
  5189. path->search_commit_root = 1;
  5190. key.objectid = ino;
  5191. key.type = BTRFS_INODE_REF_KEY;
  5192. key.offset = 0;
  5193. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5194. if (ret < 0)
  5195. goto out;
  5196. while (true) {
  5197. struct extent_buffer *leaf = path->nodes[0];
  5198. int slot = path->slots[0];
  5199. u32 cur_offset = 0;
  5200. u32 item_size;
  5201. unsigned long ptr;
  5202. if (slot >= btrfs_header_nritems(leaf)) {
  5203. ret = btrfs_next_leaf(root, path);
  5204. if (ret < 0)
  5205. goto out;
  5206. else if (ret > 0)
  5207. break;
  5208. continue;
  5209. }
  5210. btrfs_item_key_to_cpu(leaf, &key, slot);
  5211. /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
  5212. if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
  5213. break;
  5214. item_size = btrfs_item_size_nr(leaf, slot);
  5215. ptr = btrfs_item_ptr_offset(leaf, slot);
  5216. while (cur_offset < item_size) {
  5217. struct btrfs_key inode_key;
  5218. struct inode *dir_inode;
  5219. inode_key.type = BTRFS_INODE_ITEM_KEY;
  5220. inode_key.offset = 0;
  5221. if (key.type == BTRFS_INODE_EXTREF_KEY) {
  5222. struct btrfs_inode_extref *extref;
  5223. extref = (struct btrfs_inode_extref *)
  5224. (ptr + cur_offset);
  5225. inode_key.objectid = btrfs_inode_extref_parent(
  5226. leaf, extref);
  5227. cur_offset += sizeof(*extref);
  5228. cur_offset += btrfs_inode_extref_name_len(leaf,
  5229. extref);
  5230. } else {
  5231. inode_key.objectid = key.offset;
  5232. cur_offset = item_size;
  5233. }
  5234. dir_inode = btrfs_iget(fs_info->sb, inode_key.objectid,
  5235. root);
  5236. /*
  5237. * If the parent inode was deleted, return an error to
  5238. * fallback to a transaction commit. This is to prevent
  5239. * getting an inode that was moved from one parent A to
  5240. * a parent B, got its former parent A deleted and then
  5241. * it got fsync'ed, from existing at both parents after
  5242. * a log replay (and the old parent still existing).
  5243. * Example:
  5244. *
  5245. * mkdir /mnt/A
  5246. * mkdir /mnt/B
  5247. * touch /mnt/B/bar
  5248. * sync
  5249. * mv /mnt/B/bar /mnt/A/bar
  5250. * mv -T /mnt/A /mnt/B
  5251. * fsync /mnt/B/bar
  5252. * <power fail>
  5253. *
  5254. * If we ignore the old parent B which got deleted,
  5255. * after a log replay we would have file bar linked
  5256. * at both parents and the old parent B would still
  5257. * exist.
  5258. */
  5259. if (IS_ERR(dir_inode)) {
  5260. ret = PTR_ERR(dir_inode);
  5261. goto out;
  5262. }
  5263. if (ctx)
  5264. ctx->log_new_dentries = false;
  5265. ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
  5266. LOG_INODE_ALL, ctx);
  5267. if (!ret &&
  5268. btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
  5269. ret = 1;
  5270. if (!ret && ctx && ctx->log_new_dentries)
  5271. ret = log_new_dir_dentries(trans, root,
  5272. BTRFS_I(dir_inode), ctx);
  5273. btrfs_add_delayed_iput(dir_inode);
  5274. if (ret)
  5275. goto out;
  5276. }
  5277. path->slots[0]++;
  5278. }
  5279. ret = 0;
  5280. out:
  5281. btrfs_free_path(path);
  5282. return ret;
  5283. }
  5284. static int log_new_ancestors(struct btrfs_trans_handle *trans,
  5285. struct btrfs_root *root,
  5286. struct btrfs_path *path,
  5287. struct btrfs_log_ctx *ctx)
  5288. {
  5289. struct btrfs_key found_key;
  5290. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  5291. while (true) {
  5292. struct btrfs_fs_info *fs_info = root->fs_info;
  5293. const u64 last_committed = fs_info->last_trans_committed;
  5294. struct extent_buffer *leaf = path->nodes[0];
  5295. int slot = path->slots[0];
  5296. struct btrfs_key search_key;
  5297. struct inode *inode;
  5298. u64 ino;
  5299. int ret = 0;
  5300. btrfs_release_path(path);
  5301. ino = found_key.offset;
  5302. search_key.objectid = found_key.offset;
  5303. search_key.type = BTRFS_INODE_ITEM_KEY;
  5304. search_key.offset = 0;
  5305. inode = btrfs_iget(fs_info->sb, ino, root);
  5306. if (IS_ERR(inode))
  5307. return PTR_ERR(inode);
  5308. if (BTRFS_I(inode)->generation > last_committed)
  5309. ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
  5310. LOG_INODE_EXISTS, ctx);
  5311. btrfs_add_delayed_iput(inode);
  5312. if (ret)
  5313. return ret;
  5314. if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
  5315. break;
  5316. search_key.type = BTRFS_INODE_REF_KEY;
  5317. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  5318. if (ret < 0)
  5319. return ret;
  5320. leaf = path->nodes[0];
  5321. slot = path->slots[0];
  5322. if (slot >= btrfs_header_nritems(leaf)) {
  5323. ret = btrfs_next_leaf(root, path);
  5324. if (ret < 0)
  5325. return ret;
  5326. else if (ret > 0)
  5327. return -ENOENT;
  5328. leaf = path->nodes[0];
  5329. slot = path->slots[0];
  5330. }
  5331. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  5332. if (found_key.objectid != search_key.objectid ||
  5333. found_key.type != BTRFS_INODE_REF_KEY)
  5334. return -ENOENT;
  5335. }
  5336. return 0;
  5337. }
  5338. static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
  5339. struct btrfs_inode *inode,
  5340. struct dentry *parent,
  5341. struct btrfs_log_ctx *ctx)
  5342. {
  5343. struct btrfs_root *root = inode->root;
  5344. struct btrfs_fs_info *fs_info = root->fs_info;
  5345. struct dentry *old_parent = NULL;
  5346. struct super_block *sb = inode->vfs_inode.i_sb;
  5347. int ret = 0;
  5348. while (true) {
  5349. if (!parent || d_really_is_negative(parent) ||
  5350. sb != parent->d_sb)
  5351. break;
  5352. inode = BTRFS_I(d_inode(parent));
  5353. if (root != inode->root)
  5354. break;
  5355. if (inode->generation > fs_info->last_trans_committed) {
  5356. ret = btrfs_log_inode(trans, root, inode,
  5357. LOG_INODE_EXISTS, ctx);
  5358. if (ret)
  5359. break;
  5360. }
  5361. if (IS_ROOT(parent))
  5362. break;
  5363. parent = dget_parent(parent);
  5364. dput(old_parent);
  5365. old_parent = parent;
  5366. }
  5367. dput(old_parent);
  5368. return ret;
  5369. }
  5370. static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
  5371. struct btrfs_inode *inode,
  5372. struct dentry *parent,
  5373. struct btrfs_log_ctx *ctx)
  5374. {
  5375. struct btrfs_root *root = inode->root;
  5376. const u64 ino = btrfs_ino(inode);
  5377. struct btrfs_path *path;
  5378. struct btrfs_key search_key;
  5379. int ret;
  5380. /*
  5381. * For a single hard link case, go through a fast path that does not
  5382. * need to iterate the fs/subvolume tree.
  5383. */
  5384. if (inode->vfs_inode.i_nlink < 2)
  5385. return log_new_ancestors_fast(trans, inode, parent, ctx);
  5386. path = btrfs_alloc_path();
  5387. if (!path)
  5388. return -ENOMEM;
  5389. search_key.objectid = ino;
  5390. search_key.type = BTRFS_INODE_REF_KEY;
  5391. search_key.offset = 0;
  5392. again:
  5393. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  5394. if (ret < 0)
  5395. goto out;
  5396. if (ret == 0)
  5397. path->slots[0]++;
  5398. while (true) {
  5399. struct extent_buffer *leaf = path->nodes[0];
  5400. int slot = path->slots[0];
  5401. struct btrfs_key found_key;
  5402. if (slot >= btrfs_header_nritems(leaf)) {
  5403. ret = btrfs_next_leaf(root, path);
  5404. if (ret < 0)
  5405. goto out;
  5406. else if (ret > 0)
  5407. break;
  5408. continue;
  5409. }
  5410. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  5411. if (found_key.objectid != ino ||
  5412. found_key.type > BTRFS_INODE_EXTREF_KEY)
  5413. break;
  5414. /*
  5415. * Don't deal with extended references because they are rare
  5416. * cases and too complex to deal with (we would need to keep
  5417. * track of which subitem we are processing for each item in
  5418. * this loop, etc). So just return some error to fallback to
  5419. * a transaction commit.
  5420. */
  5421. if (found_key.type == BTRFS_INODE_EXTREF_KEY) {
  5422. ret = -EMLINK;
  5423. goto out;
  5424. }
  5425. /*
  5426. * Logging ancestors needs to do more searches on the fs/subvol
  5427. * tree, so it releases the path as needed to avoid deadlocks.
  5428. * Keep track of the last inode ref key and resume from that key
  5429. * after logging all new ancestors for the current hard link.
  5430. */
  5431. memcpy(&search_key, &found_key, sizeof(search_key));
  5432. ret = log_new_ancestors(trans, root, path, ctx);
  5433. if (ret)
  5434. goto out;
  5435. btrfs_release_path(path);
  5436. goto again;
  5437. }
  5438. ret = 0;
  5439. out:
  5440. btrfs_free_path(path);
  5441. return ret;
  5442. }
  5443. /*
  5444. * helper function around btrfs_log_inode to make sure newly created
  5445. * parent directories also end up in the log. A minimal inode and backref
  5446. * only logging is done of any parent directories that are older than
  5447. * the last committed transaction
  5448. */
  5449. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  5450. struct btrfs_inode *inode,
  5451. struct dentry *parent,
  5452. int inode_only,
  5453. struct btrfs_log_ctx *ctx)
  5454. {
  5455. struct btrfs_root *root = inode->root;
  5456. struct btrfs_fs_info *fs_info = root->fs_info;
  5457. struct super_block *sb;
  5458. int ret = 0;
  5459. u64 last_committed = fs_info->last_trans_committed;
  5460. bool log_dentries = false;
  5461. sb = inode->vfs_inode.i_sb;
  5462. if (btrfs_test_opt(fs_info, NOTREELOG)) {
  5463. ret = 1;
  5464. goto end_no_trans;
  5465. }
  5466. /*
  5467. * The prev transaction commit doesn't complete, we need do
  5468. * full commit by ourselves.
  5469. */
  5470. if (fs_info->last_trans_log_full_commit >
  5471. fs_info->last_trans_committed) {
  5472. ret = 1;
  5473. goto end_no_trans;
  5474. }
  5475. if (btrfs_root_refs(&root->root_item) == 0) {
  5476. ret = 1;
  5477. goto end_no_trans;
  5478. }
  5479. ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
  5480. last_committed);
  5481. if (ret)
  5482. goto end_no_trans;
  5483. /*
  5484. * Skip already logged inodes or inodes corresponding to tmpfiles
  5485. * (since logging them is pointless, a link count of 0 means they
  5486. * will never be accessible).
  5487. */
  5488. if ((btrfs_inode_in_log(inode, trans->transid) &&
  5489. list_empty(&ctx->ordered_extents)) ||
  5490. inode->vfs_inode.i_nlink == 0) {
  5491. ret = BTRFS_NO_LOG_SYNC;
  5492. goto end_no_trans;
  5493. }
  5494. ret = start_log_trans(trans, root, ctx);
  5495. if (ret)
  5496. goto end_no_trans;
  5497. ret = btrfs_log_inode(trans, root, inode, inode_only, ctx);
  5498. if (ret)
  5499. goto end_trans;
  5500. /*
  5501. * for regular files, if its inode is already on disk, we don't
  5502. * have to worry about the parents at all. This is because
  5503. * we can use the last_unlink_trans field to record renames
  5504. * and other fun in this file.
  5505. */
  5506. if (S_ISREG(inode->vfs_inode.i_mode) &&
  5507. inode->generation <= last_committed &&
  5508. inode->last_unlink_trans <= last_committed) {
  5509. ret = 0;
  5510. goto end_trans;
  5511. }
  5512. if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
  5513. log_dentries = true;
  5514. /*
  5515. * On unlink we must make sure all our current and old parent directory
  5516. * inodes are fully logged. This is to prevent leaving dangling
  5517. * directory index entries in directories that were our parents but are
  5518. * not anymore. Not doing this results in old parent directory being
  5519. * impossible to delete after log replay (rmdir will always fail with
  5520. * error -ENOTEMPTY).
  5521. *
  5522. * Example 1:
  5523. *
  5524. * mkdir testdir
  5525. * touch testdir/foo
  5526. * ln testdir/foo testdir/bar
  5527. * sync
  5528. * unlink testdir/bar
  5529. * xfs_io -c fsync testdir/foo
  5530. * <power failure>
  5531. * mount fs, triggers log replay
  5532. *
  5533. * If we don't log the parent directory (testdir), after log replay the
  5534. * directory still has an entry pointing to the file inode using the bar
  5535. * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
  5536. * the file inode has a link count of 1.
  5537. *
  5538. * Example 2:
  5539. *
  5540. * mkdir testdir
  5541. * touch foo
  5542. * ln foo testdir/foo2
  5543. * ln foo testdir/foo3
  5544. * sync
  5545. * unlink testdir/foo3
  5546. * xfs_io -c fsync foo
  5547. * <power failure>
  5548. * mount fs, triggers log replay
  5549. *
  5550. * Similar as the first example, after log replay the parent directory
  5551. * testdir still has an entry pointing to the inode file with name foo3
  5552. * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
  5553. * and has a link count of 2.
  5554. */
  5555. if (inode->last_unlink_trans > last_committed) {
  5556. ret = btrfs_log_all_parents(trans, inode, ctx);
  5557. if (ret)
  5558. goto end_trans;
  5559. }
  5560. ret = log_all_new_ancestors(trans, inode, parent, ctx);
  5561. if (ret)
  5562. goto end_trans;
  5563. if (log_dentries)
  5564. ret = log_new_dir_dentries(trans, root, inode, ctx);
  5565. else
  5566. ret = 0;
  5567. end_trans:
  5568. if (ret < 0) {
  5569. btrfs_set_log_full_commit(trans);
  5570. ret = 1;
  5571. }
  5572. if (ret)
  5573. btrfs_remove_log_ctx(root, ctx);
  5574. btrfs_end_log_trans(root);
  5575. end_no_trans:
  5576. return ret;
  5577. }
  5578. /*
  5579. * it is not safe to log dentry if the chunk root has added new
  5580. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  5581. * If this returns 1, you must commit the transaction to safely get your
  5582. * data on disk.
  5583. */
  5584. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  5585. struct dentry *dentry,
  5586. struct btrfs_log_ctx *ctx)
  5587. {
  5588. struct dentry *parent = dget_parent(dentry);
  5589. int ret;
  5590. ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
  5591. LOG_INODE_ALL, ctx);
  5592. dput(parent);
  5593. return ret;
  5594. }
  5595. /*
  5596. * should be called during mount to recover any replay any log trees
  5597. * from the FS
  5598. */
  5599. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  5600. {
  5601. int ret;
  5602. struct btrfs_path *path;
  5603. struct btrfs_trans_handle *trans;
  5604. struct btrfs_key key;
  5605. struct btrfs_key found_key;
  5606. struct btrfs_root *log;
  5607. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  5608. struct walk_control wc = {
  5609. .process_func = process_one_buffer,
  5610. .stage = LOG_WALK_PIN_ONLY,
  5611. };
  5612. path = btrfs_alloc_path();
  5613. if (!path)
  5614. return -ENOMEM;
  5615. set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5616. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  5617. if (IS_ERR(trans)) {
  5618. ret = PTR_ERR(trans);
  5619. goto error;
  5620. }
  5621. wc.trans = trans;
  5622. wc.pin = 1;
  5623. ret = walk_log_tree(trans, log_root_tree, &wc);
  5624. if (ret) {
  5625. btrfs_handle_fs_error(fs_info, ret,
  5626. "Failed to pin buffers while recovering log root tree.");
  5627. goto error;
  5628. }
  5629. again:
  5630. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  5631. key.offset = (u64)-1;
  5632. key.type = BTRFS_ROOT_ITEM_KEY;
  5633. while (1) {
  5634. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  5635. if (ret < 0) {
  5636. btrfs_handle_fs_error(fs_info, ret,
  5637. "Couldn't find tree log root.");
  5638. goto error;
  5639. }
  5640. if (ret > 0) {
  5641. if (path->slots[0] == 0)
  5642. break;
  5643. path->slots[0]--;
  5644. }
  5645. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  5646. path->slots[0]);
  5647. btrfs_release_path(path);
  5648. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  5649. break;
  5650. log = btrfs_read_tree_root(log_root_tree, &found_key);
  5651. if (IS_ERR(log)) {
  5652. ret = PTR_ERR(log);
  5653. btrfs_handle_fs_error(fs_info, ret,
  5654. "Couldn't read tree log root.");
  5655. goto error;
  5656. }
  5657. wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset,
  5658. true);
  5659. if (IS_ERR(wc.replay_dest)) {
  5660. ret = PTR_ERR(wc.replay_dest);
  5661. /*
  5662. * We didn't find the subvol, likely because it was
  5663. * deleted. This is ok, simply skip this log and go to
  5664. * the next one.
  5665. *
  5666. * We need to exclude the root because we can't have
  5667. * other log replays overwriting this log as we'll read
  5668. * it back in a few more times. This will keep our
  5669. * block from being modified, and we'll just bail for
  5670. * each subsequent pass.
  5671. */
  5672. if (ret == -ENOENT)
  5673. ret = btrfs_pin_extent_for_log_replay(trans,
  5674. log->node->start,
  5675. log->node->len);
  5676. btrfs_put_root(log);
  5677. if (!ret)
  5678. goto next;
  5679. btrfs_handle_fs_error(fs_info, ret,
  5680. "Couldn't read target root for tree log recovery.");
  5681. goto error;
  5682. }
  5683. wc.replay_dest->log_root = log;
  5684. btrfs_record_root_in_trans(trans, wc.replay_dest);
  5685. ret = walk_log_tree(trans, log, &wc);
  5686. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  5687. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  5688. path);
  5689. }
  5690. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  5691. struct btrfs_root *root = wc.replay_dest;
  5692. btrfs_release_path(path);
  5693. /*
  5694. * We have just replayed everything, and the highest
  5695. * objectid of fs roots probably has changed in case
  5696. * some inode_item's got replayed.
  5697. *
  5698. * root->objectid_mutex is not acquired as log replay
  5699. * could only happen during mount.
  5700. */
  5701. ret = btrfs_find_highest_objectid(root,
  5702. &root->highest_objectid);
  5703. }
  5704. wc.replay_dest->log_root = NULL;
  5705. btrfs_put_root(wc.replay_dest);
  5706. btrfs_put_root(log);
  5707. if (ret)
  5708. goto error;
  5709. next:
  5710. if (found_key.offset == 0)
  5711. break;
  5712. key.offset = found_key.offset - 1;
  5713. }
  5714. btrfs_release_path(path);
  5715. /* step one is to pin it all, step two is to replay just inodes */
  5716. if (wc.pin) {
  5717. wc.pin = 0;
  5718. wc.process_func = replay_one_buffer;
  5719. wc.stage = LOG_WALK_REPLAY_INODES;
  5720. goto again;
  5721. }
  5722. /* step three is to replay everything */
  5723. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  5724. wc.stage++;
  5725. goto again;
  5726. }
  5727. btrfs_free_path(path);
  5728. /* step 4: commit the transaction, which also unpins the blocks */
  5729. ret = btrfs_commit_transaction(trans);
  5730. if (ret)
  5731. return ret;
  5732. log_root_tree->log_root = NULL;
  5733. clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5734. btrfs_put_root(log_root_tree);
  5735. return 0;
  5736. error:
  5737. if (wc.trans)
  5738. btrfs_end_transaction(wc.trans);
  5739. clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5740. btrfs_free_path(path);
  5741. return ret;
  5742. }
  5743. /*
  5744. * there are some corner cases where we want to force a full
  5745. * commit instead of allowing a directory to be logged.
  5746. *
  5747. * They revolve around files there were unlinked from the directory, and
  5748. * this function updates the parent directory so that a full commit is
  5749. * properly done if it is fsync'd later after the unlinks are done.
  5750. *
  5751. * Must be called before the unlink operations (updates to the subvolume tree,
  5752. * inodes, etc) are done.
  5753. */
  5754. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  5755. struct btrfs_inode *dir, struct btrfs_inode *inode,
  5756. int for_rename)
  5757. {
  5758. /*
  5759. * when we're logging a file, if it hasn't been renamed
  5760. * or unlinked, and its inode is fully committed on disk,
  5761. * we don't have to worry about walking up the directory chain
  5762. * to log its parents.
  5763. *
  5764. * So, we use the last_unlink_trans field to put this transid
  5765. * into the file. When the file is logged we check it and
  5766. * don't log the parents if the file is fully on disk.
  5767. */
  5768. mutex_lock(&inode->log_mutex);
  5769. inode->last_unlink_trans = trans->transid;
  5770. mutex_unlock(&inode->log_mutex);
  5771. /*
  5772. * if this directory was already logged any new
  5773. * names for this file/dir will get recorded
  5774. */
  5775. if (dir->logged_trans == trans->transid)
  5776. return;
  5777. /*
  5778. * if the inode we're about to unlink was logged,
  5779. * the log will be properly updated for any new names
  5780. */
  5781. if (inode->logged_trans == trans->transid)
  5782. return;
  5783. /*
  5784. * when renaming files across directories, if the directory
  5785. * there we're unlinking from gets fsync'd later on, there's
  5786. * no way to find the destination directory later and fsync it
  5787. * properly. So, we have to be conservative and force commits
  5788. * so the new name gets discovered.
  5789. */
  5790. if (for_rename)
  5791. goto record;
  5792. /* we can safely do the unlink without any special recording */
  5793. return;
  5794. record:
  5795. mutex_lock(&dir->log_mutex);
  5796. dir->last_unlink_trans = trans->transid;
  5797. mutex_unlock(&dir->log_mutex);
  5798. }
  5799. /*
  5800. * Make sure that if someone attempts to fsync the parent directory of a deleted
  5801. * snapshot, it ends up triggering a transaction commit. This is to guarantee
  5802. * that after replaying the log tree of the parent directory's root we will not
  5803. * see the snapshot anymore and at log replay time we will not see any log tree
  5804. * corresponding to the deleted snapshot's root, which could lead to replaying
  5805. * it after replaying the log tree of the parent directory (which would replay
  5806. * the snapshot delete operation).
  5807. *
  5808. * Must be called before the actual snapshot destroy operation (updates to the
  5809. * parent root and tree of tree roots trees, etc) are done.
  5810. */
  5811. void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
  5812. struct btrfs_inode *dir)
  5813. {
  5814. mutex_lock(&dir->log_mutex);
  5815. dir->last_unlink_trans = trans->transid;
  5816. mutex_unlock(&dir->log_mutex);
  5817. }
  5818. /*
  5819. * Call this after adding a new name for a file and it will properly
  5820. * update the log to reflect the new name.
  5821. */
  5822. void btrfs_log_new_name(struct btrfs_trans_handle *trans,
  5823. struct btrfs_inode *inode, struct btrfs_inode *old_dir,
  5824. struct dentry *parent)
  5825. {
  5826. struct btrfs_log_ctx ctx;
  5827. /*
  5828. * this will force the logging code to walk the dentry chain
  5829. * up for the file
  5830. */
  5831. if (!S_ISDIR(inode->vfs_inode.i_mode))
  5832. inode->last_unlink_trans = trans->transid;
  5833. /*
  5834. * if this inode hasn't been logged and directory we're renaming it
  5835. * from hasn't been logged, we don't need to log it
  5836. */
  5837. if (!inode_logged(trans, inode) &&
  5838. (!old_dir || !inode_logged(trans, old_dir)))
  5839. return;
  5840. btrfs_init_log_ctx(&ctx, &inode->vfs_inode);
  5841. ctx.logging_new_name = true;
  5842. /*
  5843. * We don't care about the return value. If we fail to log the new name
  5844. * then we know the next attempt to sync the log will fallback to a full
  5845. * transaction commit (due to a call to btrfs_set_log_full_commit()), so
  5846. * we don't need to worry about getting a log committed that has an
  5847. * inconsistent state after a rename operation.
  5848. */
  5849. btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx);
  5850. }