send.c 152 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449
  1. /*
  2. * Copyright (C) 2012 Alexander Block. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/bsearch.h>
  19. #include <linux/fs.h>
  20. #include <linux/file.h>
  21. #include <linux/sort.h>
  22. #include <linux/mount.h>
  23. #include <linux/xattr.h>
  24. #include <linux/posix_acl_xattr.h>
  25. #include <linux/radix-tree.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/string.h>
  28. #include "send.h"
  29. #include "backref.h"
  30. #include "hash.h"
  31. #include "locking.h"
  32. #include "disk-io.h"
  33. #include "btrfs_inode.h"
  34. #include "transaction.h"
  35. #include "compression.h"
  36. /*
  37. * A fs_path is a helper to dynamically build path names with unknown size.
  38. * It reallocates the internal buffer on demand.
  39. * It allows fast adding of path elements on the right side (normal path) and
  40. * fast adding to the left side (reversed path). A reversed path can also be
  41. * unreversed if needed.
  42. */
  43. struct fs_path {
  44. union {
  45. struct {
  46. char *start;
  47. char *end;
  48. char *buf;
  49. unsigned short buf_len:15;
  50. unsigned short reversed:1;
  51. char inline_buf[];
  52. };
  53. /*
  54. * Average path length does not exceed 200 bytes, we'll have
  55. * better packing in the slab and higher chance to satisfy
  56. * a allocation later during send.
  57. */
  58. char pad[256];
  59. };
  60. };
  61. #define FS_PATH_INLINE_SIZE \
  62. (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
  63. /* reused for each extent */
  64. struct clone_root {
  65. struct btrfs_root *root;
  66. u64 ino;
  67. u64 offset;
  68. u64 found_refs;
  69. };
  70. #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
  71. #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
  72. struct send_ctx {
  73. struct file *send_filp;
  74. loff_t send_off;
  75. char *send_buf;
  76. u32 send_size;
  77. u32 send_max_size;
  78. u64 total_send_size;
  79. u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
  80. u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
  81. struct btrfs_root *send_root;
  82. struct btrfs_root *parent_root;
  83. struct clone_root *clone_roots;
  84. int clone_roots_cnt;
  85. /* current state of the compare_tree call */
  86. struct btrfs_path *left_path;
  87. struct btrfs_path *right_path;
  88. struct btrfs_key *cmp_key;
  89. /*
  90. * infos of the currently processed inode. In case of deleted inodes,
  91. * these are the values from the deleted inode.
  92. */
  93. u64 cur_ino;
  94. u64 cur_inode_gen;
  95. int cur_inode_new;
  96. int cur_inode_new_gen;
  97. int cur_inode_deleted;
  98. u64 cur_inode_size;
  99. u64 cur_inode_mode;
  100. u64 cur_inode_rdev;
  101. u64 cur_inode_last_extent;
  102. u64 send_progress;
  103. struct list_head new_refs;
  104. struct list_head deleted_refs;
  105. struct radix_tree_root name_cache;
  106. struct list_head name_cache_list;
  107. int name_cache_size;
  108. struct file_ra_state ra;
  109. char *read_buf;
  110. /*
  111. * We process inodes by their increasing order, so if before an
  112. * incremental send we reverse the parent/child relationship of
  113. * directories such that a directory with a lower inode number was
  114. * the parent of a directory with a higher inode number, and the one
  115. * becoming the new parent got renamed too, we can't rename/move the
  116. * directory with lower inode number when we finish processing it - we
  117. * must process the directory with higher inode number first, then
  118. * rename/move it and then rename/move the directory with lower inode
  119. * number. Example follows.
  120. *
  121. * Tree state when the first send was performed:
  122. *
  123. * .
  124. * |-- a (ino 257)
  125. * |-- b (ino 258)
  126. * |
  127. * |
  128. * |-- c (ino 259)
  129. * | |-- d (ino 260)
  130. * |
  131. * |-- c2 (ino 261)
  132. *
  133. * Tree state when the second (incremental) send is performed:
  134. *
  135. * .
  136. * |-- a (ino 257)
  137. * |-- b (ino 258)
  138. * |-- c2 (ino 261)
  139. * |-- d2 (ino 260)
  140. * |-- cc (ino 259)
  141. *
  142. * The sequence of steps that lead to the second state was:
  143. *
  144. * mv /a/b/c/d /a/b/c2/d2
  145. * mv /a/b/c /a/b/c2/d2/cc
  146. *
  147. * "c" has lower inode number, but we can't move it (2nd mv operation)
  148. * before we move "d", which has higher inode number.
  149. *
  150. * So we just memorize which move/rename operations must be performed
  151. * later when their respective parent is processed and moved/renamed.
  152. */
  153. /* Indexed by parent directory inode number. */
  154. struct rb_root pending_dir_moves;
  155. /*
  156. * Reverse index, indexed by the inode number of a directory that
  157. * is waiting for the move/rename of its immediate parent before its
  158. * own move/rename can be performed.
  159. */
  160. struct rb_root waiting_dir_moves;
  161. /*
  162. * A directory that is going to be rm'ed might have a child directory
  163. * which is in the pending directory moves index above. In this case,
  164. * the directory can only be removed after the move/rename of its child
  165. * is performed. Example:
  166. *
  167. * Parent snapshot:
  168. *
  169. * . (ino 256)
  170. * |-- a/ (ino 257)
  171. * |-- b/ (ino 258)
  172. * |-- c/ (ino 259)
  173. * | |-- x/ (ino 260)
  174. * |
  175. * |-- y/ (ino 261)
  176. *
  177. * Send snapshot:
  178. *
  179. * . (ino 256)
  180. * |-- a/ (ino 257)
  181. * |-- b/ (ino 258)
  182. * |-- YY/ (ino 261)
  183. * |-- x/ (ino 260)
  184. *
  185. * Sequence of steps that lead to the send snapshot:
  186. * rm -f /a/b/c/foo.txt
  187. * mv /a/b/y /a/b/YY
  188. * mv /a/b/c/x /a/b/YY
  189. * rmdir /a/b/c
  190. *
  191. * When the child is processed, its move/rename is delayed until its
  192. * parent is processed (as explained above), but all other operations
  193. * like update utimes, chown, chgrp, etc, are performed and the paths
  194. * that it uses for those operations must use the orphanized name of
  195. * its parent (the directory we're going to rm later), so we need to
  196. * memorize that name.
  197. *
  198. * Indexed by the inode number of the directory to be deleted.
  199. */
  200. struct rb_root orphan_dirs;
  201. };
  202. struct pending_dir_move {
  203. struct rb_node node;
  204. struct list_head list;
  205. u64 parent_ino;
  206. u64 ino;
  207. u64 gen;
  208. struct list_head update_refs;
  209. };
  210. struct waiting_dir_move {
  211. struct rb_node node;
  212. u64 ino;
  213. /*
  214. * There might be some directory that could not be removed because it
  215. * was waiting for this directory inode to be moved first. Therefore
  216. * after this directory is moved, we can try to rmdir the ino rmdir_ino.
  217. */
  218. u64 rmdir_ino;
  219. bool orphanized;
  220. };
  221. struct orphan_dir_info {
  222. struct rb_node node;
  223. u64 ino;
  224. u64 gen;
  225. };
  226. struct name_cache_entry {
  227. struct list_head list;
  228. /*
  229. * radix_tree has only 32bit entries but we need to handle 64bit inums.
  230. * We use the lower 32bit of the 64bit inum to store it in the tree. If
  231. * more then one inum would fall into the same entry, we use radix_list
  232. * to store the additional entries. radix_list is also used to store
  233. * entries where two entries have the same inum but different
  234. * generations.
  235. */
  236. struct list_head radix_list;
  237. u64 ino;
  238. u64 gen;
  239. u64 parent_ino;
  240. u64 parent_gen;
  241. int ret;
  242. int need_later_update;
  243. int name_len;
  244. char name[];
  245. };
  246. static void inconsistent_snapshot_error(struct send_ctx *sctx,
  247. enum btrfs_compare_tree_result result,
  248. const char *what)
  249. {
  250. const char *result_string;
  251. switch (result) {
  252. case BTRFS_COMPARE_TREE_NEW:
  253. result_string = "new";
  254. break;
  255. case BTRFS_COMPARE_TREE_DELETED:
  256. result_string = "deleted";
  257. break;
  258. case BTRFS_COMPARE_TREE_CHANGED:
  259. result_string = "updated";
  260. break;
  261. case BTRFS_COMPARE_TREE_SAME:
  262. ASSERT(0);
  263. result_string = "unchanged";
  264. break;
  265. default:
  266. ASSERT(0);
  267. result_string = "unexpected";
  268. }
  269. btrfs_err(sctx->send_root->fs_info,
  270. "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
  271. result_string, what, sctx->cmp_key->objectid,
  272. sctx->send_root->root_key.objectid,
  273. (sctx->parent_root ?
  274. sctx->parent_root->root_key.objectid : 0));
  275. }
  276. static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
  277. static struct waiting_dir_move *
  278. get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
  279. static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
  280. static int need_send_hole(struct send_ctx *sctx)
  281. {
  282. return (sctx->parent_root && !sctx->cur_inode_new &&
  283. !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
  284. S_ISREG(sctx->cur_inode_mode));
  285. }
  286. static void fs_path_reset(struct fs_path *p)
  287. {
  288. if (p->reversed) {
  289. p->start = p->buf + p->buf_len - 1;
  290. p->end = p->start;
  291. *p->start = 0;
  292. } else {
  293. p->start = p->buf;
  294. p->end = p->start;
  295. *p->start = 0;
  296. }
  297. }
  298. static struct fs_path *fs_path_alloc(void)
  299. {
  300. struct fs_path *p;
  301. p = kmalloc(sizeof(*p), GFP_KERNEL);
  302. if (!p)
  303. return NULL;
  304. p->reversed = 0;
  305. p->buf = p->inline_buf;
  306. p->buf_len = FS_PATH_INLINE_SIZE;
  307. fs_path_reset(p);
  308. return p;
  309. }
  310. static struct fs_path *fs_path_alloc_reversed(void)
  311. {
  312. struct fs_path *p;
  313. p = fs_path_alloc();
  314. if (!p)
  315. return NULL;
  316. p->reversed = 1;
  317. fs_path_reset(p);
  318. return p;
  319. }
  320. static void fs_path_free(struct fs_path *p)
  321. {
  322. if (!p)
  323. return;
  324. if (p->buf != p->inline_buf)
  325. kfree(p->buf);
  326. kfree(p);
  327. }
  328. static int fs_path_len(struct fs_path *p)
  329. {
  330. return p->end - p->start;
  331. }
  332. static int fs_path_ensure_buf(struct fs_path *p, int len)
  333. {
  334. char *tmp_buf;
  335. int path_len;
  336. int old_buf_len;
  337. len++;
  338. if (p->buf_len >= len)
  339. return 0;
  340. if (len > PATH_MAX) {
  341. WARN_ON(1);
  342. return -ENOMEM;
  343. }
  344. path_len = p->end - p->start;
  345. old_buf_len = p->buf_len;
  346. /*
  347. * First time the inline_buf does not suffice
  348. */
  349. if (p->buf == p->inline_buf) {
  350. tmp_buf = kmalloc(len, GFP_KERNEL);
  351. if (tmp_buf)
  352. memcpy(tmp_buf, p->buf, old_buf_len);
  353. } else {
  354. tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
  355. }
  356. if (!tmp_buf)
  357. return -ENOMEM;
  358. p->buf = tmp_buf;
  359. /*
  360. * The real size of the buffer is bigger, this will let the fast path
  361. * happen most of the time
  362. */
  363. p->buf_len = ksize(p->buf);
  364. if (p->reversed) {
  365. tmp_buf = p->buf + old_buf_len - path_len - 1;
  366. p->end = p->buf + p->buf_len - 1;
  367. p->start = p->end - path_len;
  368. memmove(p->start, tmp_buf, path_len + 1);
  369. } else {
  370. p->start = p->buf;
  371. p->end = p->start + path_len;
  372. }
  373. return 0;
  374. }
  375. static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
  376. char **prepared)
  377. {
  378. int ret;
  379. int new_len;
  380. new_len = p->end - p->start + name_len;
  381. if (p->start != p->end)
  382. new_len++;
  383. ret = fs_path_ensure_buf(p, new_len);
  384. if (ret < 0)
  385. goto out;
  386. if (p->reversed) {
  387. if (p->start != p->end)
  388. *--p->start = '/';
  389. p->start -= name_len;
  390. *prepared = p->start;
  391. } else {
  392. if (p->start != p->end)
  393. *p->end++ = '/';
  394. *prepared = p->end;
  395. p->end += name_len;
  396. *p->end = 0;
  397. }
  398. out:
  399. return ret;
  400. }
  401. static int fs_path_add(struct fs_path *p, const char *name, int name_len)
  402. {
  403. int ret;
  404. char *prepared;
  405. ret = fs_path_prepare_for_add(p, name_len, &prepared);
  406. if (ret < 0)
  407. goto out;
  408. memcpy(prepared, name, name_len);
  409. out:
  410. return ret;
  411. }
  412. static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
  413. {
  414. int ret;
  415. char *prepared;
  416. ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
  417. if (ret < 0)
  418. goto out;
  419. memcpy(prepared, p2->start, p2->end - p2->start);
  420. out:
  421. return ret;
  422. }
  423. static int fs_path_add_from_extent_buffer(struct fs_path *p,
  424. struct extent_buffer *eb,
  425. unsigned long off, int len)
  426. {
  427. int ret;
  428. char *prepared;
  429. ret = fs_path_prepare_for_add(p, len, &prepared);
  430. if (ret < 0)
  431. goto out;
  432. read_extent_buffer(eb, prepared, off, len);
  433. out:
  434. return ret;
  435. }
  436. static int fs_path_copy(struct fs_path *p, struct fs_path *from)
  437. {
  438. int ret;
  439. p->reversed = from->reversed;
  440. fs_path_reset(p);
  441. ret = fs_path_add_path(p, from);
  442. return ret;
  443. }
  444. static void fs_path_unreverse(struct fs_path *p)
  445. {
  446. char *tmp;
  447. int len;
  448. if (!p->reversed)
  449. return;
  450. tmp = p->start;
  451. len = p->end - p->start;
  452. p->start = p->buf;
  453. p->end = p->start + len;
  454. memmove(p->start, tmp, len + 1);
  455. p->reversed = 0;
  456. }
  457. static struct btrfs_path *alloc_path_for_send(void)
  458. {
  459. struct btrfs_path *path;
  460. path = btrfs_alloc_path();
  461. if (!path)
  462. return NULL;
  463. path->search_commit_root = 1;
  464. path->skip_locking = 1;
  465. path->need_commit_sem = 1;
  466. return path;
  467. }
  468. static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
  469. {
  470. int ret;
  471. mm_segment_t old_fs;
  472. u32 pos = 0;
  473. old_fs = get_fs();
  474. set_fs(KERNEL_DS);
  475. while (pos < len) {
  476. ret = vfs_write(filp, (__force const char __user *)buf + pos,
  477. len - pos, off);
  478. /* TODO handle that correctly */
  479. /*if (ret == -ERESTARTSYS) {
  480. continue;
  481. }*/
  482. if (ret < 0)
  483. goto out;
  484. if (ret == 0) {
  485. ret = -EIO;
  486. goto out;
  487. }
  488. pos += ret;
  489. }
  490. ret = 0;
  491. out:
  492. set_fs(old_fs);
  493. return ret;
  494. }
  495. static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
  496. {
  497. struct btrfs_tlv_header *hdr;
  498. int total_len = sizeof(*hdr) + len;
  499. int left = sctx->send_max_size - sctx->send_size;
  500. if (unlikely(left < total_len))
  501. return -EOVERFLOW;
  502. hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
  503. hdr->tlv_type = cpu_to_le16(attr);
  504. hdr->tlv_len = cpu_to_le16(len);
  505. memcpy(hdr + 1, data, len);
  506. sctx->send_size += total_len;
  507. return 0;
  508. }
  509. #define TLV_PUT_DEFINE_INT(bits) \
  510. static int tlv_put_u##bits(struct send_ctx *sctx, \
  511. u##bits attr, u##bits value) \
  512. { \
  513. __le##bits __tmp = cpu_to_le##bits(value); \
  514. return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
  515. }
  516. TLV_PUT_DEFINE_INT(64)
  517. static int tlv_put_string(struct send_ctx *sctx, u16 attr,
  518. const char *str, int len)
  519. {
  520. if (len == -1)
  521. len = strlen(str);
  522. return tlv_put(sctx, attr, str, len);
  523. }
  524. static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
  525. const u8 *uuid)
  526. {
  527. return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
  528. }
  529. static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
  530. struct extent_buffer *eb,
  531. struct btrfs_timespec *ts)
  532. {
  533. struct btrfs_timespec bts;
  534. read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
  535. return tlv_put(sctx, attr, &bts, sizeof(bts));
  536. }
  537. #define TLV_PUT(sctx, attrtype, attrlen, data) \
  538. do { \
  539. ret = tlv_put(sctx, attrtype, attrlen, data); \
  540. if (ret < 0) \
  541. goto tlv_put_failure; \
  542. } while (0)
  543. #define TLV_PUT_INT(sctx, attrtype, bits, value) \
  544. do { \
  545. ret = tlv_put_u##bits(sctx, attrtype, value); \
  546. if (ret < 0) \
  547. goto tlv_put_failure; \
  548. } while (0)
  549. #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
  550. #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
  551. #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
  552. #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
  553. #define TLV_PUT_STRING(sctx, attrtype, str, len) \
  554. do { \
  555. ret = tlv_put_string(sctx, attrtype, str, len); \
  556. if (ret < 0) \
  557. goto tlv_put_failure; \
  558. } while (0)
  559. #define TLV_PUT_PATH(sctx, attrtype, p) \
  560. do { \
  561. ret = tlv_put_string(sctx, attrtype, p->start, \
  562. p->end - p->start); \
  563. if (ret < 0) \
  564. goto tlv_put_failure; \
  565. } while(0)
  566. #define TLV_PUT_UUID(sctx, attrtype, uuid) \
  567. do { \
  568. ret = tlv_put_uuid(sctx, attrtype, uuid); \
  569. if (ret < 0) \
  570. goto tlv_put_failure; \
  571. } while (0)
  572. #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
  573. do { \
  574. ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
  575. if (ret < 0) \
  576. goto tlv_put_failure; \
  577. } while (0)
  578. static int send_header(struct send_ctx *sctx)
  579. {
  580. struct btrfs_stream_header hdr;
  581. strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
  582. hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
  583. return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
  584. &sctx->send_off);
  585. }
  586. /*
  587. * For each command/item we want to send to userspace, we call this function.
  588. */
  589. static int begin_cmd(struct send_ctx *sctx, int cmd)
  590. {
  591. struct btrfs_cmd_header *hdr;
  592. if (WARN_ON(!sctx->send_buf))
  593. return -EINVAL;
  594. BUG_ON(sctx->send_size);
  595. sctx->send_size += sizeof(*hdr);
  596. hdr = (struct btrfs_cmd_header *)sctx->send_buf;
  597. hdr->cmd = cpu_to_le16(cmd);
  598. return 0;
  599. }
  600. static int send_cmd(struct send_ctx *sctx)
  601. {
  602. int ret;
  603. struct btrfs_cmd_header *hdr;
  604. u32 crc;
  605. hdr = (struct btrfs_cmd_header *)sctx->send_buf;
  606. hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
  607. hdr->crc = 0;
  608. crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
  609. hdr->crc = cpu_to_le32(crc);
  610. ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
  611. &sctx->send_off);
  612. sctx->total_send_size += sctx->send_size;
  613. sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
  614. sctx->send_size = 0;
  615. return ret;
  616. }
  617. /*
  618. * Sends a move instruction to user space
  619. */
  620. static int send_rename(struct send_ctx *sctx,
  621. struct fs_path *from, struct fs_path *to)
  622. {
  623. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  624. int ret;
  625. btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
  626. ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
  627. if (ret < 0)
  628. goto out;
  629. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
  630. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
  631. ret = send_cmd(sctx);
  632. tlv_put_failure:
  633. out:
  634. return ret;
  635. }
  636. /*
  637. * Sends a link instruction to user space
  638. */
  639. static int send_link(struct send_ctx *sctx,
  640. struct fs_path *path, struct fs_path *lnk)
  641. {
  642. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  643. int ret;
  644. btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
  645. ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
  646. if (ret < 0)
  647. goto out;
  648. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  649. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
  650. ret = send_cmd(sctx);
  651. tlv_put_failure:
  652. out:
  653. return ret;
  654. }
  655. /*
  656. * Sends an unlink instruction to user space
  657. */
  658. static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
  659. {
  660. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  661. int ret;
  662. btrfs_debug(fs_info, "send_unlink %s", path->start);
  663. ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
  664. if (ret < 0)
  665. goto out;
  666. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  667. ret = send_cmd(sctx);
  668. tlv_put_failure:
  669. out:
  670. return ret;
  671. }
  672. /*
  673. * Sends a rmdir instruction to user space
  674. */
  675. static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
  676. {
  677. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  678. int ret;
  679. btrfs_debug(fs_info, "send_rmdir %s", path->start);
  680. ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
  681. if (ret < 0)
  682. goto out;
  683. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  684. ret = send_cmd(sctx);
  685. tlv_put_failure:
  686. out:
  687. return ret;
  688. }
  689. /*
  690. * Helper function to retrieve some fields from an inode item.
  691. */
  692. static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
  693. u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
  694. u64 *gid, u64 *rdev)
  695. {
  696. int ret;
  697. struct btrfs_inode_item *ii;
  698. struct btrfs_key key;
  699. key.objectid = ino;
  700. key.type = BTRFS_INODE_ITEM_KEY;
  701. key.offset = 0;
  702. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  703. if (ret) {
  704. if (ret > 0)
  705. ret = -ENOENT;
  706. return ret;
  707. }
  708. ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
  709. struct btrfs_inode_item);
  710. if (size)
  711. *size = btrfs_inode_size(path->nodes[0], ii);
  712. if (gen)
  713. *gen = btrfs_inode_generation(path->nodes[0], ii);
  714. if (mode)
  715. *mode = btrfs_inode_mode(path->nodes[0], ii);
  716. if (uid)
  717. *uid = btrfs_inode_uid(path->nodes[0], ii);
  718. if (gid)
  719. *gid = btrfs_inode_gid(path->nodes[0], ii);
  720. if (rdev)
  721. *rdev = btrfs_inode_rdev(path->nodes[0], ii);
  722. return ret;
  723. }
  724. static int get_inode_info(struct btrfs_root *root,
  725. u64 ino, u64 *size, u64 *gen,
  726. u64 *mode, u64 *uid, u64 *gid,
  727. u64 *rdev)
  728. {
  729. struct btrfs_path *path;
  730. int ret;
  731. path = alloc_path_for_send();
  732. if (!path)
  733. return -ENOMEM;
  734. ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
  735. rdev);
  736. btrfs_free_path(path);
  737. return ret;
  738. }
  739. typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
  740. struct fs_path *p,
  741. void *ctx);
  742. /*
  743. * Helper function to iterate the entries in ONE btrfs_inode_ref or
  744. * btrfs_inode_extref.
  745. * The iterate callback may return a non zero value to stop iteration. This can
  746. * be a negative value for error codes or 1 to simply stop it.
  747. *
  748. * path must point to the INODE_REF or INODE_EXTREF when called.
  749. */
  750. static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
  751. struct btrfs_key *found_key, int resolve,
  752. iterate_inode_ref_t iterate, void *ctx)
  753. {
  754. struct extent_buffer *eb = path->nodes[0];
  755. struct btrfs_item *item;
  756. struct btrfs_inode_ref *iref;
  757. struct btrfs_inode_extref *extref;
  758. struct btrfs_path *tmp_path;
  759. struct fs_path *p;
  760. u32 cur = 0;
  761. u32 total;
  762. int slot = path->slots[0];
  763. u32 name_len;
  764. char *start;
  765. int ret = 0;
  766. int num = 0;
  767. int index;
  768. u64 dir;
  769. unsigned long name_off;
  770. unsigned long elem_size;
  771. unsigned long ptr;
  772. p = fs_path_alloc_reversed();
  773. if (!p)
  774. return -ENOMEM;
  775. tmp_path = alloc_path_for_send();
  776. if (!tmp_path) {
  777. fs_path_free(p);
  778. return -ENOMEM;
  779. }
  780. if (found_key->type == BTRFS_INODE_REF_KEY) {
  781. ptr = (unsigned long)btrfs_item_ptr(eb, slot,
  782. struct btrfs_inode_ref);
  783. item = btrfs_item_nr(slot);
  784. total = btrfs_item_size(eb, item);
  785. elem_size = sizeof(*iref);
  786. } else {
  787. ptr = btrfs_item_ptr_offset(eb, slot);
  788. total = btrfs_item_size_nr(eb, slot);
  789. elem_size = sizeof(*extref);
  790. }
  791. while (cur < total) {
  792. fs_path_reset(p);
  793. if (found_key->type == BTRFS_INODE_REF_KEY) {
  794. iref = (struct btrfs_inode_ref *)(ptr + cur);
  795. name_len = btrfs_inode_ref_name_len(eb, iref);
  796. name_off = (unsigned long)(iref + 1);
  797. index = btrfs_inode_ref_index(eb, iref);
  798. dir = found_key->offset;
  799. } else {
  800. extref = (struct btrfs_inode_extref *)(ptr + cur);
  801. name_len = btrfs_inode_extref_name_len(eb, extref);
  802. name_off = (unsigned long)&extref->name;
  803. index = btrfs_inode_extref_index(eb, extref);
  804. dir = btrfs_inode_extref_parent(eb, extref);
  805. }
  806. if (resolve) {
  807. start = btrfs_ref_to_path(root, tmp_path, name_len,
  808. name_off, eb, dir,
  809. p->buf, p->buf_len);
  810. if (IS_ERR(start)) {
  811. ret = PTR_ERR(start);
  812. goto out;
  813. }
  814. if (start < p->buf) {
  815. /* overflow , try again with larger buffer */
  816. ret = fs_path_ensure_buf(p,
  817. p->buf_len + p->buf - start);
  818. if (ret < 0)
  819. goto out;
  820. start = btrfs_ref_to_path(root, tmp_path,
  821. name_len, name_off,
  822. eb, dir,
  823. p->buf, p->buf_len);
  824. if (IS_ERR(start)) {
  825. ret = PTR_ERR(start);
  826. goto out;
  827. }
  828. BUG_ON(start < p->buf);
  829. }
  830. p->start = start;
  831. } else {
  832. ret = fs_path_add_from_extent_buffer(p, eb, name_off,
  833. name_len);
  834. if (ret < 0)
  835. goto out;
  836. }
  837. cur += elem_size + name_len;
  838. ret = iterate(num, dir, index, p, ctx);
  839. if (ret)
  840. goto out;
  841. num++;
  842. }
  843. out:
  844. btrfs_free_path(tmp_path);
  845. fs_path_free(p);
  846. return ret;
  847. }
  848. typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
  849. const char *name, int name_len,
  850. const char *data, int data_len,
  851. u8 type, void *ctx);
  852. /*
  853. * Helper function to iterate the entries in ONE btrfs_dir_item.
  854. * The iterate callback may return a non zero value to stop iteration. This can
  855. * be a negative value for error codes or 1 to simply stop it.
  856. *
  857. * path must point to the dir item when called.
  858. */
  859. static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
  860. struct btrfs_key *found_key,
  861. iterate_dir_item_t iterate, void *ctx)
  862. {
  863. int ret = 0;
  864. struct extent_buffer *eb;
  865. struct btrfs_item *item;
  866. struct btrfs_dir_item *di;
  867. struct btrfs_key di_key;
  868. char *buf = NULL;
  869. int buf_len;
  870. u32 name_len;
  871. u32 data_len;
  872. u32 cur;
  873. u32 len;
  874. u32 total;
  875. int slot;
  876. int num;
  877. u8 type;
  878. /*
  879. * Start with a small buffer (1 page). If later we end up needing more
  880. * space, which can happen for xattrs on a fs with a leaf size greater
  881. * then the page size, attempt to increase the buffer. Typically xattr
  882. * values are small.
  883. */
  884. buf_len = PATH_MAX;
  885. buf = kmalloc(buf_len, GFP_KERNEL);
  886. if (!buf) {
  887. ret = -ENOMEM;
  888. goto out;
  889. }
  890. eb = path->nodes[0];
  891. slot = path->slots[0];
  892. item = btrfs_item_nr(slot);
  893. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  894. cur = 0;
  895. len = 0;
  896. total = btrfs_item_size(eb, item);
  897. num = 0;
  898. while (cur < total) {
  899. name_len = btrfs_dir_name_len(eb, di);
  900. data_len = btrfs_dir_data_len(eb, di);
  901. type = btrfs_dir_type(eb, di);
  902. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  903. if (type == BTRFS_FT_XATTR) {
  904. if (name_len > XATTR_NAME_MAX) {
  905. ret = -ENAMETOOLONG;
  906. goto out;
  907. }
  908. if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root)) {
  909. ret = -E2BIG;
  910. goto out;
  911. }
  912. } else {
  913. /*
  914. * Path too long
  915. */
  916. if (name_len + data_len > PATH_MAX) {
  917. ret = -ENAMETOOLONG;
  918. goto out;
  919. }
  920. }
  921. if (name_len + data_len > buf_len) {
  922. buf_len = name_len + data_len;
  923. if (is_vmalloc_addr(buf)) {
  924. vfree(buf);
  925. buf = NULL;
  926. } else {
  927. char *tmp = krealloc(buf, buf_len,
  928. GFP_KERNEL | __GFP_NOWARN);
  929. if (!tmp)
  930. kfree(buf);
  931. buf = tmp;
  932. }
  933. if (!buf) {
  934. buf = vmalloc(buf_len);
  935. if (!buf) {
  936. ret = -ENOMEM;
  937. goto out;
  938. }
  939. }
  940. }
  941. read_extent_buffer(eb, buf, (unsigned long)(di + 1),
  942. name_len + data_len);
  943. len = sizeof(*di) + name_len + data_len;
  944. di = (struct btrfs_dir_item *)((char *)di + len);
  945. cur += len;
  946. ret = iterate(num, &di_key, buf, name_len, buf + name_len,
  947. data_len, type, ctx);
  948. if (ret < 0)
  949. goto out;
  950. if (ret) {
  951. ret = 0;
  952. goto out;
  953. }
  954. num++;
  955. }
  956. out:
  957. kvfree(buf);
  958. return ret;
  959. }
  960. static int __copy_first_ref(int num, u64 dir, int index,
  961. struct fs_path *p, void *ctx)
  962. {
  963. int ret;
  964. struct fs_path *pt = ctx;
  965. ret = fs_path_copy(pt, p);
  966. if (ret < 0)
  967. return ret;
  968. /* we want the first only */
  969. return 1;
  970. }
  971. /*
  972. * Retrieve the first path of an inode. If an inode has more then one
  973. * ref/hardlink, this is ignored.
  974. */
  975. static int get_inode_path(struct btrfs_root *root,
  976. u64 ino, struct fs_path *path)
  977. {
  978. int ret;
  979. struct btrfs_key key, found_key;
  980. struct btrfs_path *p;
  981. p = alloc_path_for_send();
  982. if (!p)
  983. return -ENOMEM;
  984. fs_path_reset(path);
  985. key.objectid = ino;
  986. key.type = BTRFS_INODE_REF_KEY;
  987. key.offset = 0;
  988. ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
  989. if (ret < 0)
  990. goto out;
  991. if (ret) {
  992. ret = 1;
  993. goto out;
  994. }
  995. btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
  996. if (found_key.objectid != ino ||
  997. (found_key.type != BTRFS_INODE_REF_KEY &&
  998. found_key.type != BTRFS_INODE_EXTREF_KEY)) {
  999. ret = -ENOENT;
  1000. goto out;
  1001. }
  1002. ret = iterate_inode_ref(root, p, &found_key, 1,
  1003. __copy_first_ref, path);
  1004. if (ret < 0)
  1005. goto out;
  1006. ret = 0;
  1007. out:
  1008. btrfs_free_path(p);
  1009. return ret;
  1010. }
  1011. struct backref_ctx {
  1012. struct send_ctx *sctx;
  1013. struct btrfs_path *path;
  1014. /* number of total found references */
  1015. u64 found;
  1016. /*
  1017. * used for clones found in send_root. clones found behind cur_objectid
  1018. * and cur_offset are not considered as allowed clones.
  1019. */
  1020. u64 cur_objectid;
  1021. u64 cur_offset;
  1022. /* may be truncated in case it's the last extent in a file */
  1023. u64 extent_len;
  1024. /* data offset in the file extent item */
  1025. u64 data_offset;
  1026. /* Just to check for bugs in backref resolving */
  1027. int found_itself;
  1028. };
  1029. static int __clone_root_cmp_bsearch(const void *key, const void *elt)
  1030. {
  1031. u64 root = (u64)(uintptr_t)key;
  1032. struct clone_root *cr = (struct clone_root *)elt;
  1033. if (root < cr->root->objectid)
  1034. return -1;
  1035. if (root > cr->root->objectid)
  1036. return 1;
  1037. return 0;
  1038. }
  1039. static int __clone_root_cmp_sort(const void *e1, const void *e2)
  1040. {
  1041. struct clone_root *cr1 = (struct clone_root *)e1;
  1042. struct clone_root *cr2 = (struct clone_root *)e2;
  1043. if (cr1->root->objectid < cr2->root->objectid)
  1044. return -1;
  1045. if (cr1->root->objectid > cr2->root->objectid)
  1046. return 1;
  1047. return 0;
  1048. }
  1049. /*
  1050. * Called for every backref that is found for the current extent.
  1051. * Results are collected in sctx->clone_roots->ino/offset/found_refs
  1052. */
  1053. static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
  1054. {
  1055. struct backref_ctx *bctx = ctx_;
  1056. struct clone_root *found;
  1057. int ret;
  1058. u64 i_size;
  1059. /* First check if the root is in the list of accepted clone sources */
  1060. found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
  1061. bctx->sctx->clone_roots_cnt,
  1062. sizeof(struct clone_root),
  1063. __clone_root_cmp_bsearch);
  1064. if (!found)
  1065. return 0;
  1066. if (found->root == bctx->sctx->send_root &&
  1067. ino == bctx->cur_objectid &&
  1068. offset == bctx->cur_offset) {
  1069. bctx->found_itself = 1;
  1070. }
  1071. /*
  1072. * There are inodes that have extents that lie behind its i_size. Don't
  1073. * accept clones from these extents.
  1074. */
  1075. ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
  1076. NULL, NULL, NULL);
  1077. btrfs_release_path(bctx->path);
  1078. if (ret < 0)
  1079. return ret;
  1080. if (offset + bctx->data_offset + bctx->extent_len > i_size)
  1081. return 0;
  1082. /*
  1083. * Make sure we don't consider clones from send_root that are
  1084. * behind the current inode/offset.
  1085. */
  1086. if (found->root == bctx->sctx->send_root) {
  1087. /*
  1088. * TODO for the moment we don't accept clones from the inode
  1089. * that is currently send. We may change this when
  1090. * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
  1091. * file.
  1092. */
  1093. if (ino >= bctx->cur_objectid)
  1094. return 0;
  1095. #if 0
  1096. if (ino > bctx->cur_objectid)
  1097. return 0;
  1098. if (offset + bctx->extent_len > bctx->cur_offset)
  1099. return 0;
  1100. #endif
  1101. }
  1102. bctx->found++;
  1103. found->found_refs++;
  1104. if (ino < found->ino) {
  1105. found->ino = ino;
  1106. found->offset = offset;
  1107. } else if (found->ino == ino) {
  1108. /*
  1109. * same extent found more then once in the same file.
  1110. */
  1111. if (found->offset > offset + bctx->extent_len)
  1112. found->offset = offset;
  1113. }
  1114. return 0;
  1115. }
  1116. /*
  1117. * Given an inode, offset and extent item, it finds a good clone for a clone
  1118. * instruction. Returns -ENOENT when none could be found. The function makes
  1119. * sure that the returned clone is usable at the point where sending is at the
  1120. * moment. This means, that no clones are accepted which lie behind the current
  1121. * inode+offset.
  1122. *
  1123. * path must point to the extent item when called.
  1124. */
  1125. static int find_extent_clone(struct send_ctx *sctx,
  1126. struct btrfs_path *path,
  1127. u64 ino, u64 data_offset,
  1128. u64 ino_size,
  1129. struct clone_root **found)
  1130. {
  1131. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  1132. int ret;
  1133. int extent_type;
  1134. u64 logical;
  1135. u64 disk_byte;
  1136. u64 num_bytes;
  1137. u64 extent_item_pos;
  1138. u64 flags = 0;
  1139. struct btrfs_file_extent_item *fi;
  1140. struct extent_buffer *eb = path->nodes[0];
  1141. struct backref_ctx *backref_ctx = NULL;
  1142. struct clone_root *cur_clone_root;
  1143. struct btrfs_key found_key;
  1144. struct btrfs_path *tmp_path;
  1145. int compressed;
  1146. u32 i;
  1147. tmp_path = alloc_path_for_send();
  1148. if (!tmp_path)
  1149. return -ENOMEM;
  1150. /* We only use this path under the commit sem */
  1151. tmp_path->need_commit_sem = 0;
  1152. backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
  1153. if (!backref_ctx) {
  1154. ret = -ENOMEM;
  1155. goto out;
  1156. }
  1157. backref_ctx->path = tmp_path;
  1158. if (data_offset >= ino_size) {
  1159. /*
  1160. * There may be extents that lie behind the file's size.
  1161. * I at least had this in combination with snapshotting while
  1162. * writing large files.
  1163. */
  1164. ret = 0;
  1165. goto out;
  1166. }
  1167. fi = btrfs_item_ptr(eb, path->slots[0],
  1168. struct btrfs_file_extent_item);
  1169. extent_type = btrfs_file_extent_type(eb, fi);
  1170. if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  1171. ret = -ENOENT;
  1172. goto out;
  1173. }
  1174. compressed = btrfs_file_extent_compression(eb, fi);
  1175. num_bytes = btrfs_file_extent_num_bytes(eb, fi);
  1176. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  1177. if (disk_byte == 0) {
  1178. ret = -ENOENT;
  1179. goto out;
  1180. }
  1181. logical = disk_byte + btrfs_file_extent_offset(eb, fi);
  1182. down_read(&fs_info->commit_root_sem);
  1183. ret = extent_from_logical(fs_info, disk_byte, tmp_path,
  1184. &found_key, &flags);
  1185. up_read(&fs_info->commit_root_sem);
  1186. btrfs_release_path(tmp_path);
  1187. if (ret < 0)
  1188. goto out;
  1189. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1190. ret = -EIO;
  1191. goto out;
  1192. }
  1193. /*
  1194. * Setup the clone roots.
  1195. */
  1196. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  1197. cur_clone_root = sctx->clone_roots + i;
  1198. cur_clone_root->ino = (u64)-1;
  1199. cur_clone_root->offset = 0;
  1200. cur_clone_root->found_refs = 0;
  1201. }
  1202. backref_ctx->sctx = sctx;
  1203. backref_ctx->found = 0;
  1204. backref_ctx->cur_objectid = ino;
  1205. backref_ctx->cur_offset = data_offset;
  1206. backref_ctx->found_itself = 0;
  1207. backref_ctx->extent_len = num_bytes;
  1208. /*
  1209. * For non-compressed extents iterate_extent_inodes() gives us extent
  1210. * offsets that already take into account the data offset, but not for
  1211. * compressed extents, since the offset is logical and not relative to
  1212. * the physical extent locations. We must take this into account to
  1213. * avoid sending clone offsets that go beyond the source file's size,
  1214. * which would result in the clone ioctl failing with -EINVAL on the
  1215. * receiving end.
  1216. */
  1217. if (compressed == BTRFS_COMPRESS_NONE)
  1218. backref_ctx->data_offset = 0;
  1219. else
  1220. backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
  1221. /*
  1222. * The last extent of a file may be too large due to page alignment.
  1223. * We need to adjust extent_len in this case so that the checks in
  1224. * __iterate_backrefs work.
  1225. */
  1226. if (data_offset + num_bytes >= ino_size)
  1227. backref_ctx->extent_len = ino_size - data_offset;
  1228. /*
  1229. * Now collect all backrefs.
  1230. */
  1231. if (compressed == BTRFS_COMPRESS_NONE)
  1232. extent_item_pos = logical - found_key.objectid;
  1233. else
  1234. extent_item_pos = 0;
  1235. ret = iterate_extent_inodes(fs_info,
  1236. found_key.objectid, extent_item_pos, 1,
  1237. __iterate_backrefs, backref_ctx);
  1238. if (ret < 0)
  1239. goto out;
  1240. if (!backref_ctx->found_itself) {
  1241. /* found a bug in backref code? */
  1242. ret = -EIO;
  1243. btrfs_err(fs_info,
  1244. "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
  1245. ino, data_offset, disk_byte, found_key.objectid);
  1246. goto out;
  1247. }
  1248. btrfs_debug(fs_info,
  1249. "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
  1250. data_offset, ino, num_bytes, logical);
  1251. if (!backref_ctx->found)
  1252. btrfs_debug(fs_info, "no clones found");
  1253. cur_clone_root = NULL;
  1254. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  1255. if (sctx->clone_roots[i].found_refs) {
  1256. if (!cur_clone_root)
  1257. cur_clone_root = sctx->clone_roots + i;
  1258. else if (sctx->clone_roots[i].root == sctx->send_root)
  1259. /* prefer clones from send_root over others */
  1260. cur_clone_root = sctx->clone_roots + i;
  1261. }
  1262. }
  1263. if (cur_clone_root) {
  1264. *found = cur_clone_root;
  1265. ret = 0;
  1266. } else {
  1267. ret = -ENOENT;
  1268. }
  1269. out:
  1270. btrfs_free_path(tmp_path);
  1271. kfree(backref_ctx);
  1272. return ret;
  1273. }
  1274. static int read_symlink(struct btrfs_root *root,
  1275. u64 ino,
  1276. struct fs_path *dest)
  1277. {
  1278. int ret;
  1279. struct btrfs_path *path;
  1280. struct btrfs_key key;
  1281. struct btrfs_file_extent_item *ei;
  1282. u8 type;
  1283. u8 compression;
  1284. unsigned long off;
  1285. int len;
  1286. path = alloc_path_for_send();
  1287. if (!path)
  1288. return -ENOMEM;
  1289. key.objectid = ino;
  1290. key.type = BTRFS_EXTENT_DATA_KEY;
  1291. key.offset = 0;
  1292. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1293. if (ret < 0)
  1294. goto out;
  1295. if (ret) {
  1296. /*
  1297. * An empty symlink inode. Can happen in rare error paths when
  1298. * creating a symlink (transaction committed before the inode
  1299. * eviction handler removed the symlink inode items and a crash
  1300. * happened in between or the subvol was snapshoted in between).
  1301. * Print an informative message to dmesg/syslog so that the user
  1302. * can delete the symlink.
  1303. */
  1304. btrfs_err(root->fs_info,
  1305. "Found empty symlink inode %llu at root %llu",
  1306. ino, root->root_key.objectid);
  1307. ret = -EIO;
  1308. goto out;
  1309. }
  1310. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1311. struct btrfs_file_extent_item);
  1312. type = btrfs_file_extent_type(path->nodes[0], ei);
  1313. compression = btrfs_file_extent_compression(path->nodes[0], ei);
  1314. BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
  1315. BUG_ON(compression);
  1316. off = btrfs_file_extent_inline_start(ei);
  1317. len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
  1318. ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
  1319. out:
  1320. btrfs_free_path(path);
  1321. return ret;
  1322. }
  1323. /*
  1324. * Helper function to generate a file name that is unique in the root of
  1325. * send_root and parent_root. This is used to generate names for orphan inodes.
  1326. */
  1327. static int gen_unique_name(struct send_ctx *sctx,
  1328. u64 ino, u64 gen,
  1329. struct fs_path *dest)
  1330. {
  1331. int ret = 0;
  1332. struct btrfs_path *path;
  1333. struct btrfs_dir_item *di;
  1334. char tmp[64];
  1335. int len;
  1336. u64 idx = 0;
  1337. path = alloc_path_for_send();
  1338. if (!path)
  1339. return -ENOMEM;
  1340. while (1) {
  1341. len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
  1342. ino, gen, idx);
  1343. ASSERT(len < sizeof(tmp));
  1344. di = btrfs_lookup_dir_item(NULL, sctx->send_root,
  1345. path, BTRFS_FIRST_FREE_OBJECTID,
  1346. tmp, strlen(tmp), 0);
  1347. btrfs_release_path(path);
  1348. if (IS_ERR(di)) {
  1349. ret = PTR_ERR(di);
  1350. goto out;
  1351. }
  1352. if (di) {
  1353. /* not unique, try again */
  1354. idx++;
  1355. continue;
  1356. }
  1357. if (!sctx->parent_root) {
  1358. /* unique */
  1359. ret = 0;
  1360. break;
  1361. }
  1362. di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
  1363. path, BTRFS_FIRST_FREE_OBJECTID,
  1364. tmp, strlen(tmp), 0);
  1365. btrfs_release_path(path);
  1366. if (IS_ERR(di)) {
  1367. ret = PTR_ERR(di);
  1368. goto out;
  1369. }
  1370. if (di) {
  1371. /* not unique, try again */
  1372. idx++;
  1373. continue;
  1374. }
  1375. /* unique */
  1376. break;
  1377. }
  1378. ret = fs_path_add(dest, tmp, strlen(tmp));
  1379. out:
  1380. btrfs_free_path(path);
  1381. return ret;
  1382. }
  1383. enum inode_state {
  1384. inode_state_no_change,
  1385. inode_state_will_create,
  1386. inode_state_did_create,
  1387. inode_state_will_delete,
  1388. inode_state_did_delete,
  1389. };
  1390. static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
  1391. {
  1392. int ret;
  1393. int left_ret;
  1394. int right_ret;
  1395. u64 left_gen;
  1396. u64 right_gen;
  1397. ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
  1398. NULL, NULL);
  1399. if (ret < 0 && ret != -ENOENT)
  1400. goto out;
  1401. left_ret = ret;
  1402. if (!sctx->parent_root) {
  1403. right_ret = -ENOENT;
  1404. } else {
  1405. ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
  1406. NULL, NULL, NULL, NULL);
  1407. if (ret < 0 && ret != -ENOENT)
  1408. goto out;
  1409. right_ret = ret;
  1410. }
  1411. if (!left_ret && !right_ret) {
  1412. if (left_gen == gen && right_gen == gen) {
  1413. ret = inode_state_no_change;
  1414. } else if (left_gen == gen) {
  1415. if (ino < sctx->send_progress)
  1416. ret = inode_state_did_create;
  1417. else
  1418. ret = inode_state_will_create;
  1419. } else if (right_gen == gen) {
  1420. if (ino < sctx->send_progress)
  1421. ret = inode_state_did_delete;
  1422. else
  1423. ret = inode_state_will_delete;
  1424. } else {
  1425. ret = -ENOENT;
  1426. }
  1427. } else if (!left_ret) {
  1428. if (left_gen == gen) {
  1429. if (ino < sctx->send_progress)
  1430. ret = inode_state_did_create;
  1431. else
  1432. ret = inode_state_will_create;
  1433. } else {
  1434. ret = -ENOENT;
  1435. }
  1436. } else if (!right_ret) {
  1437. if (right_gen == gen) {
  1438. if (ino < sctx->send_progress)
  1439. ret = inode_state_did_delete;
  1440. else
  1441. ret = inode_state_will_delete;
  1442. } else {
  1443. ret = -ENOENT;
  1444. }
  1445. } else {
  1446. ret = -ENOENT;
  1447. }
  1448. out:
  1449. return ret;
  1450. }
  1451. static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
  1452. {
  1453. int ret;
  1454. if (ino == BTRFS_FIRST_FREE_OBJECTID)
  1455. return 1;
  1456. ret = get_cur_inode_state(sctx, ino, gen);
  1457. if (ret < 0)
  1458. goto out;
  1459. if (ret == inode_state_no_change ||
  1460. ret == inode_state_did_create ||
  1461. ret == inode_state_will_delete)
  1462. ret = 1;
  1463. else
  1464. ret = 0;
  1465. out:
  1466. return ret;
  1467. }
  1468. /*
  1469. * Helper function to lookup a dir item in a dir.
  1470. */
  1471. static int lookup_dir_item_inode(struct btrfs_root *root,
  1472. u64 dir, const char *name, int name_len,
  1473. u64 *found_inode,
  1474. u8 *found_type)
  1475. {
  1476. int ret = 0;
  1477. struct btrfs_dir_item *di;
  1478. struct btrfs_key key;
  1479. struct btrfs_path *path;
  1480. path = alloc_path_for_send();
  1481. if (!path)
  1482. return -ENOMEM;
  1483. di = btrfs_lookup_dir_item(NULL, root, path,
  1484. dir, name, name_len, 0);
  1485. if (!di) {
  1486. ret = -ENOENT;
  1487. goto out;
  1488. }
  1489. if (IS_ERR(di)) {
  1490. ret = PTR_ERR(di);
  1491. goto out;
  1492. }
  1493. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
  1494. if (key.type == BTRFS_ROOT_ITEM_KEY) {
  1495. ret = -ENOENT;
  1496. goto out;
  1497. }
  1498. *found_inode = key.objectid;
  1499. *found_type = btrfs_dir_type(path->nodes[0], di);
  1500. out:
  1501. btrfs_free_path(path);
  1502. return ret;
  1503. }
  1504. /*
  1505. * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
  1506. * generation of the parent dir and the name of the dir entry.
  1507. */
  1508. static int get_first_ref(struct btrfs_root *root, u64 ino,
  1509. u64 *dir, u64 *dir_gen, struct fs_path *name)
  1510. {
  1511. int ret;
  1512. struct btrfs_key key;
  1513. struct btrfs_key found_key;
  1514. struct btrfs_path *path;
  1515. int len;
  1516. u64 parent_dir;
  1517. path = alloc_path_for_send();
  1518. if (!path)
  1519. return -ENOMEM;
  1520. key.objectid = ino;
  1521. key.type = BTRFS_INODE_REF_KEY;
  1522. key.offset = 0;
  1523. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  1524. if (ret < 0)
  1525. goto out;
  1526. if (!ret)
  1527. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1528. path->slots[0]);
  1529. if (ret || found_key.objectid != ino ||
  1530. (found_key.type != BTRFS_INODE_REF_KEY &&
  1531. found_key.type != BTRFS_INODE_EXTREF_KEY)) {
  1532. ret = -ENOENT;
  1533. goto out;
  1534. }
  1535. if (found_key.type == BTRFS_INODE_REF_KEY) {
  1536. struct btrfs_inode_ref *iref;
  1537. iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1538. struct btrfs_inode_ref);
  1539. len = btrfs_inode_ref_name_len(path->nodes[0], iref);
  1540. ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
  1541. (unsigned long)(iref + 1),
  1542. len);
  1543. parent_dir = found_key.offset;
  1544. } else {
  1545. struct btrfs_inode_extref *extref;
  1546. extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1547. struct btrfs_inode_extref);
  1548. len = btrfs_inode_extref_name_len(path->nodes[0], extref);
  1549. ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
  1550. (unsigned long)&extref->name, len);
  1551. parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
  1552. }
  1553. if (ret < 0)
  1554. goto out;
  1555. btrfs_release_path(path);
  1556. if (dir_gen) {
  1557. ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
  1558. NULL, NULL, NULL);
  1559. if (ret < 0)
  1560. goto out;
  1561. }
  1562. *dir = parent_dir;
  1563. out:
  1564. btrfs_free_path(path);
  1565. return ret;
  1566. }
  1567. static int is_first_ref(struct btrfs_root *root,
  1568. u64 ino, u64 dir,
  1569. const char *name, int name_len)
  1570. {
  1571. int ret;
  1572. struct fs_path *tmp_name;
  1573. u64 tmp_dir;
  1574. tmp_name = fs_path_alloc();
  1575. if (!tmp_name)
  1576. return -ENOMEM;
  1577. ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
  1578. if (ret < 0)
  1579. goto out;
  1580. if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
  1581. ret = 0;
  1582. goto out;
  1583. }
  1584. ret = !memcmp(tmp_name->start, name, name_len);
  1585. out:
  1586. fs_path_free(tmp_name);
  1587. return ret;
  1588. }
  1589. /*
  1590. * Used by process_recorded_refs to determine if a new ref would overwrite an
  1591. * already existing ref. In case it detects an overwrite, it returns the
  1592. * inode/gen in who_ino/who_gen.
  1593. * When an overwrite is detected, process_recorded_refs does proper orphanizing
  1594. * to make sure later references to the overwritten inode are possible.
  1595. * Orphanizing is however only required for the first ref of an inode.
  1596. * process_recorded_refs does an additional is_first_ref check to see if
  1597. * orphanizing is really required.
  1598. */
  1599. static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
  1600. const char *name, int name_len,
  1601. u64 *who_ino, u64 *who_gen)
  1602. {
  1603. int ret = 0;
  1604. u64 gen;
  1605. u64 other_inode = 0;
  1606. u8 other_type = 0;
  1607. if (!sctx->parent_root)
  1608. goto out;
  1609. ret = is_inode_existent(sctx, dir, dir_gen);
  1610. if (ret <= 0)
  1611. goto out;
  1612. /*
  1613. * If we have a parent root we need to verify that the parent dir was
  1614. * not deleted and then re-created, if it was then we have no overwrite
  1615. * and we can just unlink this entry.
  1616. */
  1617. if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
  1618. ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
  1619. NULL, NULL, NULL);
  1620. if (ret < 0 && ret != -ENOENT)
  1621. goto out;
  1622. if (ret) {
  1623. ret = 0;
  1624. goto out;
  1625. }
  1626. if (gen != dir_gen)
  1627. goto out;
  1628. }
  1629. ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
  1630. &other_inode, &other_type);
  1631. if (ret < 0 && ret != -ENOENT)
  1632. goto out;
  1633. if (ret) {
  1634. ret = 0;
  1635. goto out;
  1636. }
  1637. /*
  1638. * Check if the overwritten ref was already processed. If yes, the ref
  1639. * was already unlinked/moved, so we can safely assume that we will not
  1640. * overwrite anything at this point in time.
  1641. */
  1642. if (other_inode > sctx->send_progress ||
  1643. is_waiting_for_move(sctx, other_inode)) {
  1644. ret = get_inode_info(sctx->parent_root, other_inode, NULL,
  1645. who_gen, NULL, NULL, NULL, NULL);
  1646. if (ret < 0)
  1647. goto out;
  1648. ret = 1;
  1649. *who_ino = other_inode;
  1650. } else {
  1651. ret = 0;
  1652. }
  1653. out:
  1654. return ret;
  1655. }
  1656. /*
  1657. * Checks if the ref was overwritten by an already processed inode. This is
  1658. * used by __get_cur_name_and_parent to find out if the ref was orphanized and
  1659. * thus the orphan name needs be used.
  1660. * process_recorded_refs also uses it to avoid unlinking of refs that were
  1661. * overwritten.
  1662. */
  1663. static int did_overwrite_ref(struct send_ctx *sctx,
  1664. u64 dir, u64 dir_gen,
  1665. u64 ino, u64 ino_gen,
  1666. const char *name, int name_len)
  1667. {
  1668. int ret = 0;
  1669. u64 gen;
  1670. u64 ow_inode;
  1671. u8 other_type;
  1672. if (!sctx->parent_root)
  1673. goto out;
  1674. ret = is_inode_existent(sctx, dir, dir_gen);
  1675. if (ret <= 0)
  1676. goto out;
  1677. /* check if the ref was overwritten by another ref */
  1678. ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
  1679. &ow_inode, &other_type);
  1680. if (ret < 0 && ret != -ENOENT)
  1681. goto out;
  1682. if (ret) {
  1683. /* was never and will never be overwritten */
  1684. ret = 0;
  1685. goto out;
  1686. }
  1687. ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
  1688. NULL, NULL);
  1689. if (ret < 0)
  1690. goto out;
  1691. if (ow_inode == ino && gen == ino_gen) {
  1692. ret = 0;
  1693. goto out;
  1694. }
  1695. /*
  1696. * We know that it is or will be overwritten. Check this now.
  1697. * The current inode being processed might have been the one that caused
  1698. * inode 'ino' to be orphanized, therefore check if ow_inode matches
  1699. * the current inode being processed.
  1700. */
  1701. if ((ow_inode < sctx->send_progress) ||
  1702. (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
  1703. gen == sctx->cur_inode_gen))
  1704. ret = 1;
  1705. else
  1706. ret = 0;
  1707. out:
  1708. return ret;
  1709. }
  1710. /*
  1711. * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
  1712. * that got overwritten. This is used by process_recorded_refs to determine
  1713. * if it has to use the path as returned by get_cur_path or the orphan name.
  1714. */
  1715. static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
  1716. {
  1717. int ret = 0;
  1718. struct fs_path *name = NULL;
  1719. u64 dir;
  1720. u64 dir_gen;
  1721. if (!sctx->parent_root)
  1722. goto out;
  1723. name = fs_path_alloc();
  1724. if (!name)
  1725. return -ENOMEM;
  1726. ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
  1727. if (ret < 0)
  1728. goto out;
  1729. ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
  1730. name->start, fs_path_len(name));
  1731. out:
  1732. fs_path_free(name);
  1733. return ret;
  1734. }
  1735. /*
  1736. * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
  1737. * so we need to do some special handling in case we have clashes. This function
  1738. * takes care of this with the help of name_cache_entry::radix_list.
  1739. * In case of error, nce is kfreed.
  1740. */
  1741. static int name_cache_insert(struct send_ctx *sctx,
  1742. struct name_cache_entry *nce)
  1743. {
  1744. int ret = 0;
  1745. struct list_head *nce_head;
  1746. nce_head = radix_tree_lookup(&sctx->name_cache,
  1747. (unsigned long)nce->ino);
  1748. if (!nce_head) {
  1749. nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
  1750. if (!nce_head) {
  1751. kfree(nce);
  1752. return -ENOMEM;
  1753. }
  1754. INIT_LIST_HEAD(nce_head);
  1755. ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
  1756. if (ret < 0) {
  1757. kfree(nce_head);
  1758. kfree(nce);
  1759. return ret;
  1760. }
  1761. }
  1762. list_add_tail(&nce->radix_list, nce_head);
  1763. list_add_tail(&nce->list, &sctx->name_cache_list);
  1764. sctx->name_cache_size++;
  1765. return ret;
  1766. }
  1767. static void name_cache_delete(struct send_ctx *sctx,
  1768. struct name_cache_entry *nce)
  1769. {
  1770. struct list_head *nce_head;
  1771. nce_head = radix_tree_lookup(&sctx->name_cache,
  1772. (unsigned long)nce->ino);
  1773. if (!nce_head) {
  1774. btrfs_err(sctx->send_root->fs_info,
  1775. "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
  1776. nce->ino, sctx->name_cache_size);
  1777. }
  1778. list_del(&nce->radix_list);
  1779. list_del(&nce->list);
  1780. sctx->name_cache_size--;
  1781. /*
  1782. * We may not get to the final release of nce_head if the lookup fails
  1783. */
  1784. if (nce_head && list_empty(nce_head)) {
  1785. radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
  1786. kfree(nce_head);
  1787. }
  1788. }
  1789. static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
  1790. u64 ino, u64 gen)
  1791. {
  1792. struct list_head *nce_head;
  1793. struct name_cache_entry *cur;
  1794. nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
  1795. if (!nce_head)
  1796. return NULL;
  1797. list_for_each_entry(cur, nce_head, radix_list) {
  1798. if (cur->ino == ino && cur->gen == gen)
  1799. return cur;
  1800. }
  1801. return NULL;
  1802. }
  1803. /*
  1804. * Removes the entry from the list and adds it back to the end. This marks the
  1805. * entry as recently used so that name_cache_clean_unused does not remove it.
  1806. */
  1807. static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
  1808. {
  1809. list_del(&nce->list);
  1810. list_add_tail(&nce->list, &sctx->name_cache_list);
  1811. }
  1812. /*
  1813. * Remove some entries from the beginning of name_cache_list.
  1814. */
  1815. static void name_cache_clean_unused(struct send_ctx *sctx)
  1816. {
  1817. struct name_cache_entry *nce;
  1818. if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
  1819. return;
  1820. while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
  1821. nce = list_entry(sctx->name_cache_list.next,
  1822. struct name_cache_entry, list);
  1823. name_cache_delete(sctx, nce);
  1824. kfree(nce);
  1825. }
  1826. }
  1827. static void name_cache_free(struct send_ctx *sctx)
  1828. {
  1829. struct name_cache_entry *nce;
  1830. while (!list_empty(&sctx->name_cache_list)) {
  1831. nce = list_entry(sctx->name_cache_list.next,
  1832. struct name_cache_entry, list);
  1833. name_cache_delete(sctx, nce);
  1834. kfree(nce);
  1835. }
  1836. }
  1837. /*
  1838. * Used by get_cur_path for each ref up to the root.
  1839. * Returns 0 if it succeeded.
  1840. * Returns 1 if the inode is not existent or got overwritten. In that case, the
  1841. * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
  1842. * is returned, parent_ino/parent_gen are not guaranteed to be valid.
  1843. * Returns <0 in case of error.
  1844. */
  1845. static int __get_cur_name_and_parent(struct send_ctx *sctx,
  1846. u64 ino, u64 gen,
  1847. u64 *parent_ino,
  1848. u64 *parent_gen,
  1849. struct fs_path *dest)
  1850. {
  1851. int ret;
  1852. int nce_ret;
  1853. struct name_cache_entry *nce = NULL;
  1854. /*
  1855. * First check if we already did a call to this function with the same
  1856. * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
  1857. * return the cached result.
  1858. */
  1859. nce = name_cache_search(sctx, ino, gen);
  1860. if (nce) {
  1861. if (ino < sctx->send_progress && nce->need_later_update) {
  1862. name_cache_delete(sctx, nce);
  1863. kfree(nce);
  1864. nce = NULL;
  1865. } else {
  1866. name_cache_used(sctx, nce);
  1867. *parent_ino = nce->parent_ino;
  1868. *parent_gen = nce->parent_gen;
  1869. ret = fs_path_add(dest, nce->name, nce->name_len);
  1870. if (ret < 0)
  1871. goto out;
  1872. ret = nce->ret;
  1873. goto out;
  1874. }
  1875. }
  1876. /*
  1877. * If the inode is not existent yet, add the orphan name and return 1.
  1878. * This should only happen for the parent dir that we determine in
  1879. * __record_new_ref
  1880. */
  1881. ret = is_inode_existent(sctx, ino, gen);
  1882. if (ret < 0)
  1883. goto out;
  1884. if (!ret) {
  1885. ret = gen_unique_name(sctx, ino, gen, dest);
  1886. if (ret < 0)
  1887. goto out;
  1888. ret = 1;
  1889. goto out_cache;
  1890. }
  1891. /*
  1892. * Depending on whether the inode was already processed or not, use
  1893. * send_root or parent_root for ref lookup.
  1894. */
  1895. if (ino < sctx->send_progress)
  1896. ret = get_first_ref(sctx->send_root, ino,
  1897. parent_ino, parent_gen, dest);
  1898. else
  1899. ret = get_first_ref(sctx->parent_root, ino,
  1900. parent_ino, parent_gen, dest);
  1901. if (ret < 0)
  1902. goto out;
  1903. /*
  1904. * Check if the ref was overwritten by an inode's ref that was processed
  1905. * earlier. If yes, treat as orphan and return 1.
  1906. */
  1907. ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
  1908. dest->start, dest->end - dest->start);
  1909. if (ret < 0)
  1910. goto out;
  1911. if (ret) {
  1912. fs_path_reset(dest);
  1913. ret = gen_unique_name(sctx, ino, gen, dest);
  1914. if (ret < 0)
  1915. goto out;
  1916. ret = 1;
  1917. }
  1918. out_cache:
  1919. /*
  1920. * Store the result of the lookup in the name cache.
  1921. */
  1922. nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
  1923. if (!nce) {
  1924. ret = -ENOMEM;
  1925. goto out;
  1926. }
  1927. nce->ino = ino;
  1928. nce->gen = gen;
  1929. nce->parent_ino = *parent_ino;
  1930. nce->parent_gen = *parent_gen;
  1931. nce->name_len = fs_path_len(dest);
  1932. nce->ret = ret;
  1933. strcpy(nce->name, dest->start);
  1934. if (ino < sctx->send_progress)
  1935. nce->need_later_update = 0;
  1936. else
  1937. nce->need_later_update = 1;
  1938. nce_ret = name_cache_insert(sctx, nce);
  1939. if (nce_ret < 0)
  1940. ret = nce_ret;
  1941. name_cache_clean_unused(sctx);
  1942. out:
  1943. return ret;
  1944. }
  1945. /*
  1946. * Magic happens here. This function returns the first ref to an inode as it
  1947. * would look like while receiving the stream at this point in time.
  1948. * We walk the path up to the root. For every inode in between, we check if it
  1949. * was already processed/sent. If yes, we continue with the parent as found
  1950. * in send_root. If not, we continue with the parent as found in parent_root.
  1951. * If we encounter an inode that was deleted at this point in time, we use the
  1952. * inodes "orphan" name instead of the real name and stop. Same with new inodes
  1953. * that were not created yet and overwritten inodes/refs.
  1954. *
  1955. * When do we have have orphan inodes:
  1956. * 1. When an inode is freshly created and thus no valid refs are available yet
  1957. * 2. When a directory lost all it's refs (deleted) but still has dir items
  1958. * inside which were not processed yet (pending for move/delete). If anyone
  1959. * tried to get the path to the dir items, it would get a path inside that
  1960. * orphan directory.
  1961. * 3. When an inode is moved around or gets new links, it may overwrite the ref
  1962. * of an unprocessed inode. If in that case the first ref would be
  1963. * overwritten, the overwritten inode gets "orphanized". Later when we
  1964. * process this overwritten inode, it is restored at a new place by moving
  1965. * the orphan inode.
  1966. *
  1967. * sctx->send_progress tells this function at which point in time receiving
  1968. * would be.
  1969. */
  1970. static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
  1971. struct fs_path *dest)
  1972. {
  1973. int ret = 0;
  1974. struct fs_path *name = NULL;
  1975. u64 parent_inode = 0;
  1976. u64 parent_gen = 0;
  1977. int stop = 0;
  1978. name = fs_path_alloc();
  1979. if (!name) {
  1980. ret = -ENOMEM;
  1981. goto out;
  1982. }
  1983. dest->reversed = 1;
  1984. fs_path_reset(dest);
  1985. while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
  1986. struct waiting_dir_move *wdm;
  1987. fs_path_reset(name);
  1988. if (is_waiting_for_rm(sctx, ino)) {
  1989. ret = gen_unique_name(sctx, ino, gen, name);
  1990. if (ret < 0)
  1991. goto out;
  1992. ret = fs_path_add_path(dest, name);
  1993. break;
  1994. }
  1995. wdm = get_waiting_dir_move(sctx, ino);
  1996. if (wdm && wdm->orphanized) {
  1997. ret = gen_unique_name(sctx, ino, gen, name);
  1998. stop = 1;
  1999. } else if (wdm) {
  2000. ret = get_first_ref(sctx->parent_root, ino,
  2001. &parent_inode, &parent_gen, name);
  2002. } else {
  2003. ret = __get_cur_name_and_parent(sctx, ino, gen,
  2004. &parent_inode,
  2005. &parent_gen, name);
  2006. if (ret)
  2007. stop = 1;
  2008. }
  2009. if (ret < 0)
  2010. goto out;
  2011. ret = fs_path_add_path(dest, name);
  2012. if (ret < 0)
  2013. goto out;
  2014. ino = parent_inode;
  2015. gen = parent_gen;
  2016. }
  2017. out:
  2018. fs_path_free(name);
  2019. if (!ret)
  2020. fs_path_unreverse(dest);
  2021. return ret;
  2022. }
  2023. /*
  2024. * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
  2025. */
  2026. static int send_subvol_begin(struct send_ctx *sctx)
  2027. {
  2028. int ret;
  2029. struct btrfs_root *send_root = sctx->send_root;
  2030. struct btrfs_root *parent_root = sctx->parent_root;
  2031. struct btrfs_path *path;
  2032. struct btrfs_key key;
  2033. struct btrfs_root_ref *ref;
  2034. struct extent_buffer *leaf;
  2035. char *name = NULL;
  2036. int namelen;
  2037. path = btrfs_alloc_path();
  2038. if (!path)
  2039. return -ENOMEM;
  2040. name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
  2041. if (!name) {
  2042. btrfs_free_path(path);
  2043. return -ENOMEM;
  2044. }
  2045. key.objectid = send_root->objectid;
  2046. key.type = BTRFS_ROOT_BACKREF_KEY;
  2047. key.offset = 0;
  2048. ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
  2049. &key, path, 1, 0);
  2050. if (ret < 0)
  2051. goto out;
  2052. if (ret) {
  2053. ret = -ENOENT;
  2054. goto out;
  2055. }
  2056. leaf = path->nodes[0];
  2057. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2058. if (key.type != BTRFS_ROOT_BACKREF_KEY ||
  2059. key.objectid != send_root->objectid) {
  2060. ret = -ENOENT;
  2061. goto out;
  2062. }
  2063. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  2064. namelen = btrfs_root_ref_name_len(leaf, ref);
  2065. read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
  2066. btrfs_release_path(path);
  2067. if (parent_root) {
  2068. ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
  2069. if (ret < 0)
  2070. goto out;
  2071. } else {
  2072. ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
  2073. if (ret < 0)
  2074. goto out;
  2075. }
  2076. TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
  2077. if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
  2078. TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
  2079. sctx->send_root->root_item.received_uuid);
  2080. else
  2081. TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
  2082. sctx->send_root->root_item.uuid);
  2083. TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
  2084. le64_to_cpu(sctx->send_root->root_item.ctransid));
  2085. if (parent_root) {
  2086. if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
  2087. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  2088. parent_root->root_item.received_uuid);
  2089. else
  2090. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  2091. parent_root->root_item.uuid);
  2092. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  2093. le64_to_cpu(sctx->parent_root->root_item.ctransid));
  2094. }
  2095. ret = send_cmd(sctx);
  2096. tlv_put_failure:
  2097. out:
  2098. btrfs_free_path(path);
  2099. kfree(name);
  2100. return ret;
  2101. }
  2102. static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
  2103. {
  2104. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  2105. int ret = 0;
  2106. struct fs_path *p;
  2107. btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
  2108. p = fs_path_alloc();
  2109. if (!p)
  2110. return -ENOMEM;
  2111. ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
  2112. if (ret < 0)
  2113. goto out;
  2114. ret = get_cur_path(sctx, ino, gen, p);
  2115. if (ret < 0)
  2116. goto out;
  2117. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2118. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
  2119. ret = send_cmd(sctx);
  2120. tlv_put_failure:
  2121. out:
  2122. fs_path_free(p);
  2123. return ret;
  2124. }
  2125. static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
  2126. {
  2127. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  2128. int ret = 0;
  2129. struct fs_path *p;
  2130. btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
  2131. p = fs_path_alloc();
  2132. if (!p)
  2133. return -ENOMEM;
  2134. ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
  2135. if (ret < 0)
  2136. goto out;
  2137. ret = get_cur_path(sctx, ino, gen, p);
  2138. if (ret < 0)
  2139. goto out;
  2140. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2141. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
  2142. ret = send_cmd(sctx);
  2143. tlv_put_failure:
  2144. out:
  2145. fs_path_free(p);
  2146. return ret;
  2147. }
  2148. static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
  2149. {
  2150. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  2151. int ret = 0;
  2152. struct fs_path *p;
  2153. btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
  2154. ino, uid, gid);
  2155. p = fs_path_alloc();
  2156. if (!p)
  2157. return -ENOMEM;
  2158. ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
  2159. if (ret < 0)
  2160. goto out;
  2161. ret = get_cur_path(sctx, ino, gen, p);
  2162. if (ret < 0)
  2163. goto out;
  2164. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2165. TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
  2166. TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
  2167. ret = send_cmd(sctx);
  2168. tlv_put_failure:
  2169. out:
  2170. fs_path_free(p);
  2171. return ret;
  2172. }
  2173. static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
  2174. {
  2175. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  2176. int ret = 0;
  2177. struct fs_path *p = NULL;
  2178. struct btrfs_inode_item *ii;
  2179. struct btrfs_path *path = NULL;
  2180. struct extent_buffer *eb;
  2181. struct btrfs_key key;
  2182. int slot;
  2183. btrfs_debug(fs_info, "send_utimes %llu", ino);
  2184. p = fs_path_alloc();
  2185. if (!p)
  2186. return -ENOMEM;
  2187. path = alloc_path_for_send();
  2188. if (!path) {
  2189. ret = -ENOMEM;
  2190. goto out;
  2191. }
  2192. key.objectid = ino;
  2193. key.type = BTRFS_INODE_ITEM_KEY;
  2194. key.offset = 0;
  2195. ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
  2196. if (ret > 0)
  2197. ret = -ENOENT;
  2198. if (ret < 0)
  2199. goto out;
  2200. eb = path->nodes[0];
  2201. slot = path->slots[0];
  2202. ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  2203. ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
  2204. if (ret < 0)
  2205. goto out;
  2206. ret = get_cur_path(sctx, ino, gen, p);
  2207. if (ret < 0)
  2208. goto out;
  2209. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2210. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
  2211. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
  2212. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
  2213. /* TODO Add otime support when the otime patches get into upstream */
  2214. ret = send_cmd(sctx);
  2215. tlv_put_failure:
  2216. out:
  2217. fs_path_free(p);
  2218. btrfs_free_path(path);
  2219. return ret;
  2220. }
  2221. /*
  2222. * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
  2223. * a valid path yet because we did not process the refs yet. So, the inode
  2224. * is created as orphan.
  2225. */
  2226. static int send_create_inode(struct send_ctx *sctx, u64 ino)
  2227. {
  2228. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  2229. int ret = 0;
  2230. struct fs_path *p;
  2231. int cmd;
  2232. u64 gen;
  2233. u64 mode;
  2234. u64 rdev;
  2235. btrfs_debug(fs_info, "send_create_inode %llu", ino);
  2236. p = fs_path_alloc();
  2237. if (!p)
  2238. return -ENOMEM;
  2239. if (ino != sctx->cur_ino) {
  2240. ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
  2241. NULL, NULL, &rdev);
  2242. if (ret < 0)
  2243. goto out;
  2244. } else {
  2245. gen = sctx->cur_inode_gen;
  2246. mode = sctx->cur_inode_mode;
  2247. rdev = sctx->cur_inode_rdev;
  2248. }
  2249. if (S_ISREG(mode)) {
  2250. cmd = BTRFS_SEND_C_MKFILE;
  2251. } else if (S_ISDIR(mode)) {
  2252. cmd = BTRFS_SEND_C_MKDIR;
  2253. } else if (S_ISLNK(mode)) {
  2254. cmd = BTRFS_SEND_C_SYMLINK;
  2255. } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
  2256. cmd = BTRFS_SEND_C_MKNOD;
  2257. } else if (S_ISFIFO(mode)) {
  2258. cmd = BTRFS_SEND_C_MKFIFO;
  2259. } else if (S_ISSOCK(mode)) {
  2260. cmd = BTRFS_SEND_C_MKSOCK;
  2261. } else {
  2262. btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
  2263. (int)(mode & S_IFMT));
  2264. ret = -ENOTSUPP;
  2265. goto out;
  2266. }
  2267. ret = begin_cmd(sctx, cmd);
  2268. if (ret < 0)
  2269. goto out;
  2270. ret = gen_unique_name(sctx, ino, gen, p);
  2271. if (ret < 0)
  2272. goto out;
  2273. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2274. TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
  2275. if (S_ISLNK(mode)) {
  2276. fs_path_reset(p);
  2277. ret = read_symlink(sctx->send_root, ino, p);
  2278. if (ret < 0)
  2279. goto out;
  2280. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
  2281. } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
  2282. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  2283. TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
  2284. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
  2285. }
  2286. ret = send_cmd(sctx);
  2287. if (ret < 0)
  2288. goto out;
  2289. tlv_put_failure:
  2290. out:
  2291. fs_path_free(p);
  2292. return ret;
  2293. }
  2294. /*
  2295. * We need some special handling for inodes that get processed before the parent
  2296. * directory got created. See process_recorded_refs for details.
  2297. * This function does the check if we already created the dir out of order.
  2298. */
  2299. static int did_create_dir(struct send_ctx *sctx, u64 dir)
  2300. {
  2301. int ret = 0;
  2302. struct btrfs_path *path = NULL;
  2303. struct btrfs_key key;
  2304. struct btrfs_key found_key;
  2305. struct btrfs_key di_key;
  2306. struct extent_buffer *eb;
  2307. struct btrfs_dir_item *di;
  2308. int slot;
  2309. path = alloc_path_for_send();
  2310. if (!path) {
  2311. ret = -ENOMEM;
  2312. goto out;
  2313. }
  2314. key.objectid = dir;
  2315. key.type = BTRFS_DIR_INDEX_KEY;
  2316. key.offset = 0;
  2317. ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
  2318. if (ret < 0)
  2319. goto out;
  2320. while (1) {
  2321. eb = path->nodes[0];
  2322. slot = path->slots[0];
  2323. if (slot >= btrfs_header_nritems(eb)) {
  2324. ret = btrfs_next_leaf(sctx->send_root, path);
  2325. if (ret < 0) {
  2326. goto out;
  2327. } else if (ret > 0) {
  2328. ret = 0;
  2329. break;
  2330. }
  2331. continue;
  2332. }
  2333. btrfs_item_key_to_cpu(eb, &found_key, slot);
  2334. if (found_key.objectid != key.objectid ||
  2335. found_key.type != key.type) {
  2336. ret = 0;
  2337. goto out;
  2338. }
  2339. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  2340. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  2341. if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
  2342. di_key.objectid < sctx->send_progress) {
  2343. ret = 1;
  2344. goto out;
  2345. }
  2346. path->slots[0]++;
  2347. }
  2348. out:
  2349. btrfs_free_path(path);
  2350. return ret;
  2351. }
  2352. /*
  2353. * Only creates the inode if it is:
  2354. * 1. Not a directory
  2355. * 2. Or a directory which was not created already due to out of order
  2356. * directories. See did_create_dir and process_recorded_refs for details.
  2357. */
  2358. static int send_create_inode_if_needed(struct send_ctx *sctx)
  2359. {
  2360. int ret;
  2361. if (S_ISDIR(sctx->cur_inode_mode)) {
  2362. ret = did_create_dir(sctx, sctx->cur_ino);
  2363. if (ret < 0)
  2364. goto out;
  2365. if (ret) {
  2366. ret = 0;
  2367. goto out;
  2368. }
  2369. }
  2370. ret = send_create_inode(sctx, sctx->cur_ino);
  2371. if (ret < 0)
  2372. goto out;
  2373. out:
  2374. return ret;
  2375. }
  2376. struct recorded_ref {
  2377. struct list_head list;
  2378. char *dir_path;
  2379. char *name;
  2380. struct fs_path *full_path;
  2381. u64 dir;
  2382. u64 dir_gen;
  2383. int dir_path_len;
  2384. int name_len;
  2385. };
  2386. /*
  2387. * We need to process new refs before deleted refs, but compare_tree gives us
  2388. * everything mixed. So we first record all refs and later process them.
  2389. * This function is a helper to record one ref.
  2390. */
  2391. static int __record_ref(struct list_head *head, u64 dir,
  2392. u64 dir_gen, struct fs_path *path)
  2393. {
  2394. struct recorded_ref *ref;
  2395. ref = kmalloc(sizeof(*ref), GFP_KERNEL);
  2396. if (!ref)
  2397. return -ENOMEM;
  2398. ref->dir = dir;
  2399. ref->dir_gen = dir_gen;
  2400. ref->full_path = path;
  2401. ref->name = (char *)kbasename(ref->full_path->start);
  2402. ref->name_len = ref->full_path->end - ref->name;
  2403. ref->dir_path = ref->full_path->start;
  2404. if (ref->name == ref->full_path->start)
  2405. ref->dir_path_len = 0;
  2406. else
  2407. ref->dir_path_len = ref->full_path->end -
  2408. ref->full_path->start - 1 - ref->name_len;
  2409. list_add_tail(&ref->list, head);
  2410. return 0;
  2411. }
  2412. static int dup_ref(struct recorded_ref *ref, struct list_head *list)
  2413. {
  2414. struct recorded_ref *new;
  2415. new = kmalloc(sizeof(*ref), GFP_KERNEL);
  2416. if (!new)
  2417. return -ENOMEM;
  2418. new->dir = ref->dir;
  2419. new->dir_gen = ref->dir_gen;
  2420. new->full_path = NULL;
  2421. INIT_LIST_HEAD(&new->list);
  2422. list_add_tail(&new->list, list);
  2423. return 0;
  2424. }
  2425. static void __free_recorded_refs(struct list_head *head)
  2426. {
  2427. struct recorded_ref *cur;
  2428. while (!list_empty(head)) {
  2429. cur = list_entry(head->next, struct recorded_ref, list);
  2430. fs_path_free(cur->full_path);
  2431. list_del(&cur->list);
  2432. kfree(cur);
  2433. }
  2434. }
  2435. static void free_recorded_refs(struct send_ctx *sctx)
  2436. {
  2437. __free_recorded_refs(&sctx->new_refs);
  2438. __free_recorded_refs(&sctx->deleted_refs);
  2439. }
  2440. /*
  2441. * Renames/moves a file/dir to its orphan name. Used when the first
  2442. * ref of an unprocessed inode gets overwritten and for all non empty
  2443. * directories.
  2444. */
  2445. static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
  2446. struct fs_path *path)
  2447. {
  2448. int ret;
  2449. struct fs_path *orphan;
  2450. orphan = fs_path_alloc();
  2451. if (!orphan)
  2452. return -ENOMEM;
  2453. ret = gen_unique_name(sctx, ino, gen, orphan);
  2454. if (ret < 0)
  2455. goto out;
  2456. ret = send_rename(sctx, path, orphan);
  2457. out:
  2458. fs_path_free(orphan);
  2459. return ret;
  2460. }
  2461. static struct orphan_dir_info *
  2462. add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
  2463. {
  2464. struct rb_node **p = &sctx->orphan_dirs.rb_node;
  2465. struct rb_node *parent = NULL;
  2466. struct orphan_dir_info *entry, *odi;
  2467. odi = kmalloc(sizeof(*odi), GFP_KERNEL);
  2468. if (!odi)
  2469. return ERR_PTR(-ENOMEM);
  2470. odi->ino = dir_ino;
  2471. odi->gen = 0;
  2472. while (*p) {
  2473. parent = *p;
  2474. entry = rb_entry(parent, struct orphan_dir_info, node);
  2475. if (dir_ino < entry->ino) {
  2476. p = &(*p)->rb_left;
  2477. } else if (dir_ino > entry->ino) {
  2478. p = &(*p)->rb_right;
  2479. } else {
  2480. kfree(odi);
  2481. return entry;
  2482. }
  2483. }
  2484. rb_link_node(&odi->node, parent, p);
  2485. rb_insert_color(&odi->node, &sctx->orphan_dirs);
  2486. return odi;
  2487. }
  2488. static struct orphan_dir_info *
  2489. get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
  2490. {
  2491. struct rb_node *n = sctx->orphan_dirs.rb_node;
  2492. struct orphan_dir_info *entry;
  2493. while (n) {
  2494. entry = rb_entry(n, struct orphan_dir_info, node);
  2495. if (dir_ino < entry->ino)
  2496. n = n->rb_left;
  2497. else if (dir_ino > entry->ino)
  2498. n = n->rb_right;
  2499. else
  2500. return entry;
  2501. }
  2502. return NULL;
  2503. }
  2504. static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
  2505. {
  2506. struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
  2507. return odi != NULL;
  2508. }
  2509. static void free_orphan_dir_info(struct send_ctx *sctx,
  2510. struct orphan_dir_info *odi)
  2511. {
  2512. if (!odi)
  2513. return;
  2514. rb_erase(&odi->node, &sctx->orphan_dirs);
  2515. kfree(odi);
  2516. }
  2517. /*
  2518. * Returns 1 if a directory can be removed at this point in time.
  2519. * We check this by iterating all dir items and checking if the inode behind
  2520. * the dir item was already processed.
  2521. */
  2522. static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
  2523. u64 send_progress)
  2524. {
  2525. int ret = 0;
  2526. struct btrfs_root *root = sctx->parent_root;
  2527. struct btrfs_path *path;
  2528. struct btrfs_key key;
  2529. struct btrfs_key found_key;
  2530. struct btrfs_key loc;
  2531. struct btrfs_dir_item *di;
  2532. /*
  2533. * Don't try to rmdir the top/root subvolume dir.
  2534. */
  2535. if (dir == BTRFS_FIRST_FREE_OBJECTID)
  2536. return 0;
  2537. path = alloc_path_for_send();
  2538. if (!path)
  2539. return -ENOMEM;
  2540. key.objectid = dir;
  2541. key.type = BTRFS_DIR_INDEX_KEY;
  2542. key.offset = 0;
  2543. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2544. if (ret < 0)
  2545. goto out;
  2546. while (1) {
  2547. struct waiting_dir_move *dm;
  2548. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  2549. ret = btrfs_next_leaf(root, path);
  2550. if (ret < 0)
  2551. goto out;
  2552. else if (ret > 0)
  2553. break;
  2554. continue;
  2555. }
  2556. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2557. path->slots[0]);
  2558. if (found_key.objectid != key.objectid ||
  2559. found_key.type != key.type)
  2560. break;
  2561. di = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2562. struct btrfs_dir_item);
  2563. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
  2564. dm = get_waiting_dir_move(sctx, loc.objectid);
  2565. if (dm) {
  2566. struct orphan_dir_info *odi;
  2567. odi = add_orphan_dir_info(sctx, dir);
  2568. if (IS_ERR(odi)) {
  2569. ret = PTR_ERR(odi);
  2570. goto out;
  2571. }
  2572. odi->gen = dir_gen;
  2573. dm->rmdir_ino = dir;
  2574. ret = 0;
  2575. goto out;
  2576. }
  2577. if (loc.objectid > send_progress) {
  2578. struct orphan_dir_info *odi;
  2579. odi = get_orphan_dir_info(sctx, dir);
  2580. free_orphan_dir_info(sctx, odi);
  2581. ret = 0;
  2582. goto out;
  2583. }
  2584. path->slots[0]++;
  2585. }
  2586. ret = 1;
  2587. out:
  2588. btrfs_free_path(path);
  2589. return ret;
  2590. }
  2591. static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
  2592. {
  2593. struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
  2594. return entry != NULL;
  2595. }
  2596. static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
  2597. {
  2598. struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
  2599. struct rb_node *parent = NULL;
  2600. struct waiting_dir_move *entry, *dm;
  2601. dm = kmalloc(sizeof(*dm), GFP_KERNEL);
  2602. if (!dm)
  2603. return -ENOMEM;
  2604. dm->ino = ino;
  2605. dm->rmdir_ino = 0;
  2606. dm->orphanized = orphanized;
  2607. while (*p) {
  2608. parent = *p;
  2609. entry = rb_entry(parent, struct waiting_dir_move, node);
  2610. if (ino < entry->ino) {
  2611. p = &(*p)->rb_left;
  2612. } else if (ino > entry->ino) {
  2613. p = &(*p)->rb_right;
  2614. } else {
  2615. kfree(dm);
  2616. return -EEXIST;
  2617. }
  2618. }
  2619. rb_link_node(&dm->node, parent, p);
  2620. rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
  2621. return 0;
  2622. }
  2623. static struct waiting_dir_move *
  2624. get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
  2625. {
  2626. struct rb_node *n = sctx->waiting_dir_moves.rb_node;
  2627. struct waiting_dir_move *entry;
  2628. while (n) {
  2629. entry = rb_entry(n, struct waiting_dir_move, node);
  2630. if (ino < entry->ino)
  2631. n = n->rb_left;
  2632. else if (ino > entry->ino)
  2633. n = n->rb_right;
  2634. else
  2635. return entry;
  2636. }
  2637. return NULL;
  2638. }
  2639. static void free_waiting_dir_move(struct send_ctx *sctx,
  2640. struct waiting_dir_move *dm)
  2641. {
  2642. if (!dm)
  2643. return;
  2644. rb_erase(&dm->node, &sctx->waiting_dir_moves);
  2645. kfree(dm);
  2646. }
  2647. static int add_pending_dir_move(struct send_ctx *sctx,
  2648. u64 ino,
  2649. u64 ino_gen,
  2650. u64 parent_ino,
  2651. struct list_head *new_refs,
  2652. struct list_head *deleted_refs,
  2653. const bool is_orphan)
  2654. {
  2655. struct rb_node **p = &sctx->pending_dir_moves.rb_node;
  2656. struct rb_node *parent = NULL;
  2657. struct pending_dir_move *entry = NULL, *pm;
  2658. struct recorded_ref *cur;
  2659. int exists = 0;
  2660. int ret;
  2661. pm = kmalloc(sizeof(*pm), GFP_KERNEL);
  2662. if (!pm)
  2663. return -ENOMEM;
  2664. pm->parent_ino = parent_ino;
  2665. pm->ino = ino;
  2666. pm->gen = ino_gen;
  2667. INIT_LIST_HEAD(&pm->list);
  2668. INIT_LIST_HEAD(&pm->update_refs);
  2669. RB_CLEAR_NODE(&pm->node);
  2670. while (*p) {
  2671. parent = *p;
  2672. entry = rb_entry(parent, struct pending_dir_move, node);
  2673. if (parent_ino < entry->parent_ino) {
  2674. p = &(*p)->rb_left;
  2675. } else if (parent_ino > entry->parent_ino) {
  2676. p = &(*p)->rb_right;
  2677. } else {
  2678. exists = 1;
  2679. break;
  2680. }
  2681. }
  2682. list_for_each_entry(cur, deleted_refs, list) {
  2683. ret = dup_ref(cur, &pm->update_refs);
  2684. if (ret < 0)
  2685. goto out;
  2686. }
  2687. list_for_each_entry(cur, new_refs, list) {
  2688. ret = dup_ref(cur, &pm->update_refs);
  2689. if (ret < 0)
  2690. goto out;
  2691. }
  2692. ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
  2693. if (ret)
  2694. goto out;
  2695. if (exists) {
  2696. list_add_tail(&pm->list, &entry->list);
  2697. } else {
  2698. rb_link_node(&pm->node, parent, p);
  2699. rb_insert_color(&pm->node, &sctx->pending_dir_moves);
  2700. }
  2701. ret = 0;
  2702. out:
  2703. if (ret) {
  2704. __free_recorded_refs(&pm->update_refs);
  2705. kfree(pm);
  2706. }
  2707. return ret;
  2708. }
  2709. static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
  2710. u64 parent_ino)
  2711. {
  2712. struct rb_node *n = sctx->pending_dir_moves.rb_node;
  2713. struct pending_dir_move *entry;
  2714. while (n) {
  2715. entry = rb_entry(n, struct pending_dir_move, node);
  2716. if (parent_ino < entry->parent_ino)
  2717. n = n->rb_left;
  2718. else if (parent_ino > entry->parent_ino)
  2719. n = n->rb_right;
  2720. else
  2721. return entry;
  2722. }
  2723. return NULL;
  2724. }
  2725. static int path_loop(struct send_ctx *sctx, struct fs_path *name,
  2726. u64 ino, u64 gen, u64 *ancestor_ino)
  2727. {
  2728. int ret = 0;
  2729. u64 parent_inode = 0;
  2730. u64 parent_gen = 0;
  2731. u64 start_ino = ino;
  2732. *ancestor_ino = 0;
  2733. while (ino != BTRFS_FIRST_FREE_OBJECTID) {
  2734. fs_path_reset(name);
  2735. if (is_waiting_for_rm(sctx, ino))
  2736. break;
  2737. if (is_waiting_for_move(sctx, ino)) {
  2738. if (*ancestor_ino == 0)
  2739. *ancestor_ino = ino;
  2740. ret = get_first_ref(sctx->parent_root, ino,
  2741. &parent_inode, &parent_gen, name);
  2742. } else {
  2743. ret = __get_cur_name_and_parent(sctx, ino, gen,
  2744. &parent_inode,
  2745. &parent_gen, name);
  2746. if (ret > 0) {
  2747. ret = 0;
  2748. break;
  2749. }
  2750. }
  2751. if (ret < 0)
  2752. break;
  2753. if (parent_inode == start_ino) {
  2754. ret = 1;
  2755. if (*ancestor_ino == 0)
  2756. *ancestor_ino = ino;
  2757. break;
  2758. }
  2759. ino = parent_inode;
  2760. gen = parent_gen;
  2761. }
  2762. return ret;
  2763. }
  2764. static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
  2765. {
  2766. struct fs_path *from_path = NULL;
  2767. struct fs_path *to_path = NULL;
  2768. struct fs_path *name = NULL;
  2769. u64 orig_progress = sctx->send_progress;
  2770. struct recorded_ref *cur;
  2771. u64 parent_ino, parent_gen;
  2772. struct waiting_dir_move *dm = NULL;
  2773. u64 rmdir_ino = 0;
  2774. u64 ancestor;
  2775. bool is_orphan;
  2776. int ret;
  2777. name = fs_path_alloc();
  2778. from_path = fs_path_alloc();
  2779. if (!name || !from_path) {
  2780. ret = -ENOMEM;
  2781. goto out;
  2782. }
  2783. dm = get_waiting_dir_move(sctx, pm->ino);
  2784. ASSERT(dm);
  2785. rmdir_ino = dm->rmdir_ino;
  2786. is_orphan = dm->orphanized;
  2787. free_waiting_dir_move(sctx, dm);
  2788. if (is_orphan) {
  2789. ret = gen_unique_name(sctx, pm->ino,
  2790. pm->gen, from_path);
  2791. } else {
  2792. ret = get_first_ref(sctx->parent_root, pm->ino,
  2793. &parent_ino, &parent_gen, name);
  2794. if (ret < 0)
  2795. goto out;
  2796. ret = get_cur_path(sctx, parent_ino, parent_gen,
  2797. from_path);
  2798. if (ret < 0)
  2799. goto out;
  2800. ret = fs_path_add_path(from_path, name);
  2801. }
  2802. if (ret < 0)
  2803. goto out;
  2804. sctx->send_progress = sctx->cur_ino + 1;
  2805. ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
  2806. if (ret < 0)
  2807. goto out;
  2808. if (ret) {
  2809. LIST_HEAD(deleted_refs);
  2810. ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
  2811. ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
  2812. &pm->update_refs, &deleted_refs,
  2813. is_orphan);
  2814. if (ret < 0)
  2815. goto out;
  2816. if (rmdir_ino) {
  2817. dm = get_waiting_dir_move(sctx, pm->ino);
  2818. ASSERT(dm);
  2819. dm->rmdir_ino = rmdir_ino;
  2820. }
  2821. goto out;
  2822. }
  2823. fs_path_reset(name);
  2824. to_path = name;
  2825. name = NULL;
  2826. ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
  2827. if (ret < 0)
  2828. goto out;
  2829. ret = send_rename(sctx, from_path, to_path);
  2830. if (ret < 0)
  2831. goto out;
  2832. if (rmdir_ino) {
  2833. struct orphan_dir_info *odi;
  2834. odi = get_orphan_dir_info(sctx, rmdir_ino);
  2835. if (!odi) {
  2836. /* already deleted */
  2837. goto finish;
  2838. }
  2839. ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino);
  2840. if (ret < 0)
  2841. goto out;
  2842. if (!ret)
  2843. goto finish;
  2844. name = fs_path_alloc();
  2845. if (!name) {
  2846. ret = -ENOMEM;
  2847. goto out;
  2848. }
  2849. ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
  2850. if (ret < 0)
  2851. goto out;
  2852. ret = send_rmdir(sctx, name);
  2853. if (ret < 0)
  2854. goto out;
  2855. free_orphan_dir_info(sctx, odi);
  2856. }
  2857. finish:
  2858. ret = send_utimes(sctx, pm->ino, pm->gen);
  2859. if (ret < 0)
  2860. goto out;
  2861. /*
  2862. * After rename/move, need to update the utimes of both new parent(s)
  2863. * and old parent(s).
  2864. */
  2865. list_for_each_entry(cur, &pm->update_refs, list) {
  2866. /*
  2867. * The parent inode might have been deleted in the send snapshot
  2868. */
  2869. ret = get_inode_info(sctx->send_root, cur->dir, NULL,
  2870. NULL, NULL, NULL, NULL, NULL);
  2871. if (ret == -ENOENT) {
  2872. ret = 0;
  2873. continue;
  2874. }
  2875. if (ret < 0)
  2876. goto out;
  2877. ret = send_utimes(sctx, cur->dir, cur->dir_gen);
  2878. if (ret < 0)
  2879. goto out;
  2880. }
  2881. out:
  2882. fs_path_free(name);
  2883. fs_path_free(from_path);
  2884. fs_path_free(to_path);
  2885. sctx->send_progress = orig_progress;
  2886. return ret;
  2887. }
  2888. static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
  2889. {
  2890. if (!list_empty(&m->list))
  2891. list_del(&m->list);
  2892. if (!RB_EMPTY_NODE(&m->node))
  2893. rb_erase(&m->node, &sctx->pending_dir_moves);
  2894. __free_recorded_refs(&m->update_refs);
  2895. kfree(m);
  2896. }
  2897. static void tail_append_pending_moves(struct send_ctx *sctx,
  2898. struct pending_dir_move *moves,
  2899. struct list_head *stack)
  2900. {
  2901. if (list_empty(&moves->list)) {
  2902. list_add_tail(&moves->list, stack);
  2903. } else {
  2904. LIST_HEAD(list);
  2905. list_splice_init(&moves->list, &list);
  2906. list_add_tail(&moves->list, stack);
  2907. list_splice_tail(&list, stack);
  2908. }
  2909. if (!RB_EMPTY_NODE(&moves->node)) {
  2910. rb_erase(&moves->node, &sctx->pending_dir_moves);
  2911. RB_CLEAR_NODE(&moves->node);
  2912. }
  2913. }
  2914. static int apply_children_dir_moves(struct send_ctx *sctx)
  2915. {
  2916. struct pending_dir_move *pm;
  2917. struct list_head stack;
  2918. u64 parent_ino = sctx->cur_ino;
  2919. int ret = 0;
  2920. pm = get_pending_dir_moves(sctx, parent_ino);
  2921. if (!pm)
  2922. return 0;
  2923. INIT_LIST_HEAD(&stack);
  2924. tail_append_pending_moves(sctx, pm, &stack);
  2925. while (!list_empty(&stack)) {
  2926. pm = list_first_entry(&stack, struct pending_dir_move, list);
  2927. parent_ino = pm->ino;
  2928. ret = apply_dir_move(sctx, pm);
  2929. free_pending_move(sctx, pm);
  2930. if (ret)
  2931. goto out;
  2932. pm = get_pending_dir_moves(sctx, parent_ino);
  2933. if (pm)
  2934. tail_append_pending_moves(sctx, pm, &stack);
  2935. }
  2936. return 0;
  2937. out:
  2938. while (!list_empty(&stack)) {
  2939. pm = list_first_entry(&stack, struct pending_dir_move, list);
  2940. free_pending_move(sctx, pm);
  2941. }
  2942. return ret;
  2943. }
  2944. /*
  2945. * We might need to delay a directory rename even when no ancestor directory
  2946. * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
  2947. * renamed. This happens when we rename a directory to the old name (the name
  2948. * in the parent root) of some other unrelated directory that got its rename
  2949. * delayed due to some ancestor with higher number that got renamed.
  2950. *
  2951. * Example:
  2952. *
  2953. * Parent snapshot:
  2954. * . (ino 256)
  2955. * |---- a/ (ino 257)
  2956. * | |---- file (ino 260)
  2957. * |
  2958. * |---- b/ (ino 258)
  2959. * |---- c/ (ino 259)
  2960. *
  2961. * Send snapshot:
  2962. * . (ino 256)
  2963. * |---- a/ (ino 258)
  2964. * |---- x/ (ino 259)
  2965. * |---- y/ (ino 257)
  2966. * |----- file (ino 260)
  2967. *
  2968. * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
  2969. * from 'a' to 'x/y' happening first, which in turn depends on the rename of
  2970. * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
  2971. * must issue is:
  2972. *
  2973. * 1 - rename 259 from 'c' to 'x'
  2974. * 2 - rename 257 from 'a' to 'x/y'
  2975. * 3 - rename 258 from 'b' to 'a'
  2976. *
  2977. * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
  2978. * be done right away and < 0 on error.
  2979. */
  2980. static int wait_for_dest_dir_move(struct send_ctx *sctx,
  2981. struct recorded_ref *parent_ref,
  2982. const bool is_orphan)
  2983. {
  2984. struct btrfs_path *path;
  2985. struct btrfs_key key;
  2986. struct btrfs_key di_key;
  2987. struct btrfs_dir_item *di;
  2988. u64 left_gen;
  2989. u64 right_gen;
  2990. int ret = 0;
  2991. struct waiting_dir_move *wdm;
  2992. if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
  2993. return 0;
  2994. path = alloc_path_for_send();
  2995. if (!path)
  2996. return -ENOMEM;
  2997. key.objectid = parent_ref->dir;
  2998. key.type = BTRFS_DIR_ITEM_KEY;
  2999. key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
  3000. ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
  3001. if (ret < 0) {
  3002. goto out;
  3003. } else if (ret > 0) {
  3004. ret = 0;
  3005. goto out;
  3006. }
  3007. di = btrfs_match_dir_item_name(sctx->parent_root, path,
  3008. parent_ref->name, parent_ref->name_len);
  3009. if (!di) {
  3010. ret = 0;
  3011. goto out;
  3012. }
  3013. /*
  3014. * di_key.objectid has the number of the inode that has a dentry in the
  3015. * parent directory with the same name that sctx->cur_ino is being
  3016. * renamed to. We need to check if that inode is in the send root as
  3017. * well and if it is currently marked as an inode with a pending rename,
  3018. * if it is, we need to delay the rename of sctx->cur_ino as well, so
  3019. * that it happens after that other inode is renamed.
  3020. */
  3021. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
  3022. if (di_key.type != BTRFS_INODE_ITEM_KEY) {
  3023. ret = 0;
  3024. goto out;
  3025. }
  3026. ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
  3027. &left_gen, NULL, NULL, NULL, NULL);
  3028. if (ret < 0)
  3029. goto out;
  3030. ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
  3031. &right_gen, NULL, NULL, NULL, NULL);
  3032. if (ret < 0) {
  3033. if (ret == -ENOENT)
  3034. ret = 0;
  3035. goto out;
  3036. }
  3037. /* Different inode, no need to delay the rename of sctx->cur_ino */
  3038. if (right_gen != left_gen) {
  3039. ret = 0;
  3040. goto out;
  3041. }
  3042. wdm = get_waiting_dir_move(sctx, di_key.objectid);
  3043. if (wdm && !wdm->orphanized) {
  3044. ret = add_pending_dir_move(sctx,
  3045. sctx->cur_ino,
  3046. sctx->cur_inode_gen,
  3047. di_key.objectid,
  3048. &sctx->new_refs,
  3049. &sctx->deleted_refs,
  3050. is_orphan);
  3051. if (!ret)
  3052. ret = 1;
  3053. }
  3054. out:
  3055. btrfs_free_path(path);
  3056. return ret;
  3057. }
  3058. /*
  3059. * Check if ino ino1 is an ancestor of inode ino2 in the given root.
  3060. * Return 1 if true, 0 if false and < 0 on error.
  3061. */
  3062. static int is_ancestor(struct btrfs_root *root,
  3063. const u64 ino1,
  3064. const u64 ino1_gen,
  3065. const u64 ino2,
  3066. struct fs_path *fs_path)
  3067. {
  3068. u64 ino = ino2;
  3069. while (ino > BTRFS_FIRST_FREE_OBJECTID) {
  3070. int ret;
  3071. u64 parent;
  3072. u64 parent_gen;
  3073. fs_path_reset(fs_path);
  3074. ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
  3075. if (ret < 0) {
  3076. if (ret == -ENOENT && ino == ino2)
  3077. ret = 0;
  3078. return ret;
  3079. }
  3080. if (parent == ino1)
  3081. return parent_gen == ino1_gen ? 1 : 0;
  3082. ino = parent;
  3083. }
  3084. return 0;
  3085. }
  3086. static int wait_for_parent_move(struct send_ctx *sctx,
  3087. struct recorded_ref *parent_ref,
  3088. const bool is_orphan)
  3089. {
  3090. int ret = 0;
  3091. u64 ino = parent_ref->dir;
  3092. u64 parent_ino_before, parent_ino_after;
  3093. struct fs_path *path_before = NULL;
  3094. struct fs_path *path_after = NULL;
  3095. int len1, len2;
  3096. path_after = fs_path_alloc();
  3097. path_before = fs_path_alloc();
  3098. if (!path_after || !path_before) {
  3099. ret = -ENOMEM;
  3100. goto out;
  3101. }
  3102. /*
  3103. * Our current directory inode may not yet be renamed/moved because some
  3104. * ancestor (immediate or not) has to be renamed/moved first. So find if
  3105. * such ancestor exists and make sure our own rename/move happens after
  3106. * that ancestor is processed to avoid path build infinite loops (done
  3107. * at get_cur_path()).
  3108. */
  3109. while (ino > BTRFS_FIRST_FREE_OBJECTID) {
  3110. if (is_waiting_for_move(sctx, ino)) {
  3111. /*
  3112. * If the current inode is an ancestor of ino in the
  3113. * parent root, we need to delay the rename of the
  3114. * current inode, otherwise don't delayed the rename
  3115. * because we can end up with a circular dependency
  3116. * of renames, resulting in some directories never
  3117. * getting the respective rename operations issued in
  3118. * the send stream or getting into infinite path build
  3119. * loops.
  3120. */
  3121. ret = is_ancestor(sctx->parent_root,
  3122. sctx->cur_ino, sctx->cur_inode_gen,
  3123. ino, path_before);
  3124. if (ret)
  3125. break;
  3126. }
  3127. fs_path_reset(path_before);
  3128. fs_path_reset(path_after);
  3129. ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
  3130. NULL, path_after);
  3131. if (ret < 0)
  3132. goto out;
  3133. ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
  3134. NULL, path_before);
  3135. if (ret < 0 && ret != -ENOENT) {
  3136. goto out;
  3137. } else if (ret == -ENOENT) {
  3138. ret = 0;
  3139. break;
  3140. }
  3141. len1 = fs_path_len(path_before);
  3142. len2 = fs_path_len(path_after);
  3143. if (ino > sctx->cur_ino &&
  3144. (parent_ino_before != parent_ino_after || len1 != len2 ||
  3145. memcmp(path_before->start, path_after->start, len1))) {
  3146. ret = 1;
  3147. break;
  3148. }
  3149. ino = parent_ino_after;
  3150. }
  3151. out:
  3152. fs_path_free(path_before);
  3153. fs_path_free(path_after);
  3154. if (ret == 1) {
  3155. ret = add_pending_dir_move(sctx,
  3156. sctx->cur_ino,
  3157. sctx->cur_inode_gen,
  3158. ino,
  3159. &sctx->new_refs,
  3160. &sctx->deleted_refs,
  3161. is_orphan);
  3162. if (!ret)
  3163. ret = 1;
  3164. }
  3165. return ret;
  3166. }
  3167. /*
  3168. * This does all the move/link/unlink/rmdir magic.
  3169. */
  3170. static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
  3171. {
  3172. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  3173. int ret = 0;
  3174. struct recorded_ref *cur;
  3175. struct recorded_ref *cur2;
  3176. struct list_head check_dirs;
  3177. struct fs_path *valid_path = NULL;
  3178. u64 ow_inode = 0;
  3179. u64 ow_gen;
  3180. int did_overwrite = 0;
  3181. int is_orphan = 0;
  3182. u64 last_dir_ino_rm = 0;
  3183. bool can_rename = true;
  3184. btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
  3185. /*
  3186. * This should never happen as the root dir always has the same ref
  3187. * which is always '..'
  3188. */
  3189. BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
  3190. INIT_LIST_HEAD(&check_dirs);
  3191. valid_path = fs_path_alloc();
  3192. if (!valid_path) {
  3193. ret = -ENOMEM;
  3194. goto out;
  3195. }
  3196. /*
  3197. * First, check if the first ref of the current inode was overwritten
  3198. * before. If yes, we know that the current inode was already orphanized
  3199. * and thus use the orphan name. If not, we can use get_cur_path to
  3200. * get the path of the first ref as it would like while receiving at
  3201. * this point in time.
  3202. * New inodes are always orphan at the beginning, so force to use the
  3203. * orphan name in this case.
  3204. * The first ref is stored in valid_path and will be updated if it
  3205. * gets moved around.
  3206. */
  3207. if (!sctx->cur_inode_new) {
  3208. ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
  3209. sctx->cur_inode_gen);
  3210. if (ret < 0)
  3211. goto out;
  3212. if (ret)
  3213. did_overwrite = 1;
  3214. }
  3215. if (sctx->cur_inode_new || did_overwrite) {
  3216. ret = gen_unique_name(sctx, sctx->cur_ino,
  3217. sctx->cur_inode_gen, valid_path);
  3218. if (ret < 0)
  3219. goto out;
  3220. is_orphan = 1;
  3221. } else {
  3222. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3223. valid_path);
  3224. if (ret < 0)
  3225. goto out;
  3226. }
  3227. list_for_each_entry(cur, &sctx->new_refs, list) {
  3228. /*
  3229. * We may have refs where the parent directory does not exist
  3230. * yet. This happens if the parent directories inum is higher
  3231. * the the current inum. To handle this case, we create the
  3232. * parent directory out of order. But we need to check if this
  3233. * did already happen before due to other refs in the same dir.
  3234. */
  3235. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
  3236. if (ret < 0)
  3237. goto out;
  3238. if (ret == inode_state_will_create) {
  3239. ret = 0;
  3240. /*
  3241. * First check if any of the current inodes refs did
  3242. * already create the dir.
  3243. */
  3244. list_for_each_entry(cur2, &sctx->new_refs, list) {
  3245. if (cur == cur2)
  3246. break;
  3247. if (cur2->dir == cur->dir) {
  3248. ret = 1;
  3249. break;
  3250. }
  3251. }
  3252. /*
  3253. * If that did not happen, check if a previous inode
  3254. * did already create the dir.
  3255. */
  3256. if (!ret)
  3257. ret = did_create_dir(sctx, cur->dir);
  3258. if (ret < 0)
  3259. goto out;
  3260. if (!ret) {
  3261. ret = send_create_inode(sctx, cur->dir);
  3262. if (ret < 0)
  3263. goto out;
  3264. }
  3265. }
  3266. /*
  3267. * Check if this new ref would overwrite the first ref of
  3268. * another unprocessed inode. If yes, orphanize the
  3269. * overwritten inode. If we find an overwritten ref that is
  3270. * not the first ref, simply unlink it.
  3271. */
  3272. ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  3273. cur->name, cur->name_len,
  3274. &ow_inode, &ow_gen);
  3275. if (ret < 0)
  3276. goto out;
  3277. if (ret) {
  3278. ret = is_first_ref(sctx->parent_root,
  3279. ow_inode, cur->dir, cur->name,
  3280. cur->name_len);
  3281. if (ret < 0)
  3282. goto out;
  3283. if (ret) {
  3284. struct name_cache_entry *nce;
  3285. struct waiting_dir_move *wdm;
  3286. ret = orphanize_inode(sctx, ow_inode, ow_gen,
  3287. cur->full_path);
  3288. if (ret < 0)
  3289. goto out;
  3290. /*
  3291. * If ow_inode has its rename operation delayed
  3292. * make sure that its orphanized name is used in
  3293. * the source path when performing its rename
  3294. * operation.
  3295. */
  3296. if (is_waiting_for_move(sctx, ow_inode)) {
  3297. wdm = get_waiting_dir_move(sctx,
  3298. ow_inode);
  3299. ASSERT(wdm);
  3300. wdm->orphanized = true;
  3301. }
  3302. /*
  3303. * Make sure we clear our orphanized inode's
  3304. * name from the name cache. This is because the
  3305. * inode ow_inode might be an ancestor of some
  3306. * other inode that will be orphanized as well
  3307. * later and has an inode number greater than
  3308. * sctx->send_progress. We need to prevent
  3309. * future name lookups from using the old name
  3310. * and get instead the orphan name.
  3311. */
  3312. nce = name_cache_search(sctx, ow_inode, ow_gen);
  3313. if (nce) {
  3314. name_cache_delete(sctx, nce);
  3315. kfree(nce);
  3316. }
  3317. /*
  3318. * ow_inode might currently be an ancestor of
  3319. * cur_ino, therefore compute valid_path (the
  3320. * current path of cur_ino) again because it
  3321. * might contain the pre-orphanization name of
  3322. * ow_inode, which is no longer valid.
  3323. */
  3324. fs_path_reset(valid_path);
  3325. ret = get_cur_path(sctx, sctx->cur_ino,
  3326. sctx->cur_inode_gen, valid_path);
  3327. if (ret < 0)
  3328. goto out;
  3329. } else {
  3330. ret = send_unlink(sctx, cur->full_path);
  3331. if (ret < 0)
  3332. goto out;
  3333. }
  3334. }
  3335. if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
  3336. ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
  3337. if (ret < 0)
  3338. goto out;
  3339. if (ret == 1) {
  3340. can_rename = false;
  3341. *pending_move = 1;
  3342. }
  3343. }
  3344. if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
  3345. can_rename) {
  3346. ret = wait_for_parent_move(sctx, cur, is_orphan);
  3347. if (ret < 0)
  3348. goto out;
  3349. if (ret == 1) {
  3350. can_rename = false;
  3351. *pending_move = 1;
  3352. }
  3353. }
  3354. /*
  3355. * link/move the ref to the new place. If we have an orphan
  3356. * inode, move it and update valid_path. If not, link or move
  3357. * it depending on the inode mode.
  3358. */
  3359. if (is_orphan && can_rename) {
  3360. ret = send_rename(sctx, valid_path, cur->full_path);
  3361. if (ret < 0)
  3362. goto out;
  3363. is_orphan = 0;
  3364. ret = fs_path_copy(valid_path, cur->full_path);
  3365. if (ret < 0)
  3366. goto out;
  3367. } else if (can_rename) {
  3368. if (S_ISDIR(sctx->cur_inode_mode)) {
  3369. /*
  3370. * Dirs can't be linked, so move it. For moved
  3371. * dirs, we always have one new and one deleted
  3372. * ref. The deleted ref is ignored later.
  3373. */
  3374. ret = send_rename(sctx, valid_path,
  3375. cur->full_path);
  3376. if (!ret)
  3377. ret = fs_path_copy(valid_path,
  3378. cur->full_path);
  3379. if (ret < 0)
  3380. goto out;
  3381. } else {
  3382. ret = send_link(sctx, cur->full_path,
  3383. valid_path);
  3384. if (ret < 0)
  3385. goto out;
  3386. }
  3387. }
  3388. ret = dup_ref(cur, &check_dirs);
  3389. if (ret < 0)
  3390. goto out;
  3391. }
  3392. if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
  3393. /*
  3394. * Check if we can already rmdir the directory. If not,
  3395. * orphanize it. For every dir item inside that gets deleted
  3396. * later, we do this check again and rmdir it then if possible.
  3397. * See the use of check_dirs for more details.
  3398. */
  3399. ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3400. sctx->cur_ino);
  3401. if (ret < 0)
  3402. goto out;
  3403. if (ret) {
  3404. ret = send_rmdir(sctx, valid_path);
  3405. if (ret < 0)
  3406. goto out;
  3407. } else if (!is_orphan) {
  3408. ret = orphanize_inode(sctx, sctx->cur_ino,
  3409. sctx->cur_inode_gen, valid_path);
  3410. if (ret < 0)
  3411. goto out;
  3412. is_orphan = 1;
  3413. }
  3414. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  3415. ret = dup_ref(cur, &check_dirs);
  3416. if (ret < 0)
  3417. goto out;
  3418. }
  3419. } else if (S_ISDIR(sctx->cur_inode_mode) &&
  3420. !list_empty(&sctx->deleted_refs)) {
  3421. /*
  3422. * We have a moved dir. Add the old parent to check_dirs
  3423. */
  3424. cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
  3425. list);
  3426. ret = dup_ref(cur, &check_dirs);
  3427. if (ret < 0)
  3428. goto out;
  3429. } else if (!S_ISDIR(sctx->cur_inode_mode)) {
  3430. /*
  3431. * We have a non dir inode. Go through all deleted refs and
  3432. * unlink them if they were not already overwritten by other
  3433. * inodes.
  3434. */
  3435. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  3436. ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  3437. sctx->cur_ino, sctx->cur_inode_gen,
  3438. cur->name, cur->name_len);
  3439. if (ret < 0)
  3440. goto out;
  3441. if (!ret) {
  3442. ret = send_unlink(sctx, cur->full_path);
  3443. if (ret < 0)
  3444. goto out;
  3445. }
  3446. ret = dup_ref(cur, &check_dirs);
  3447. if (ret < 0)
  3448. goto out;
  3449. }
  3450. /*
  3451. * If the inode is still orphan, unlink the orphan. This may
  3452. * happen when a previous inode did overwrite the first ref
  3453. * of this inode and no new refs were added for the current
  3454. * inode. Unlinking does not mean that the inode is deleted in
  3455. * all cases. There may still be links to this inode in other
  3456. * places.
  3457. */
  3458. if (is_orphan) {
  3459. ret = send_unlink(sctx, valid_path);
  3460. if (ret < 0)
  3461. goto out;
  3462. }
  3463. }
  3464. /*
  3465. * We did collect all parent dirs where cur_inode was once located. We
  3466. * now go through all these dirs and check if they are pending for
  3467. * deletion and if it's finally possible to perform the rmdir now.
  3468. * We also update the inode stats of the parent dirs here.
  3469. */
  3470. list_for_each_entry(cur, &check_dirs, list) {
  3471. /*
  3472. * In case we had refs into dirs that were not processed yet,
  3473. * we don't need to do the utime and rmdir logic for these dirs.
  3474. * The dir will be processed later.
  3475. */
  3476. if (cur->dir > sctx->cur_ino)
  3477. continue;
  3478. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
  3479. if (ret < 0)
  3480. goto out;
  3481. if (ret == inode_state_did_create ||
  3482. ret == inode_state_no_change) {
  3483. /* TODO delayed utimes */
  3484. ret = send_utimes(sctx, cur->dir, cur->dir_gen);
  3485. if (ret < 0)
  3486. goto out;
  3487. } else if (ret == inode_state_did_delete &&
  3488. cur->dir != last_dir_ino_rm) {
  3489. ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
  3490. sctx->cur_ino);
  3491. if (ret < 0)
  3492. goto out;
  3493. if (ret) {
  3494. ret = get_cur_path(sctx, cur->dir,
  3495. cur->dir_gen, valid_path);
  3496. if (ret < 0)
  3497. goto out;
  3498. ret = send_rmdir(sctx, valid_path);
  3499. if (ret < 0)
  3500. goto out;
  3501. last_dir_ino_rm = cur->dir;
  3502. }
  3503. }
  3504. }
  3505. ret = 0;
  3506. out:
  3507. __free_recorded_refs(&check_dirs);
  3508. free_recorded_refs(sctx);
  3509. fs_path_free(valid_path);
  3510. return ret;
  3511. }
  3512. static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
  3513. struct fs_path *name, void *ctx, struct list_head *refs)
  3514. {
  3515. int ret = 0;
  3516. struct send_ctx *sctx = ctx;
  3517. struct fs_path *p;
  3518. u64 gen;
  3519. p = fs_path_alloc();
  3520. if (!p)
  3521. return -ENOMEM;
  3522. ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
  3523. NULL, NULL);
  3524. if (ret < 0)
  3525. goto out;
  3526. ret = get_cur_path(sctx, dir, gen, p);
  3527. if (ret < 0)
  3528. goto out;
  3529. ret = fs_path_add_path(p, name);
  3530. if (ret < 0)
  3531. goto out;
  3532. ret = __record_ref(refs, dir, gen, p);
  3533. out:
  3534. if (ret)
  3535. fs_path_free(p);
  3536. return ret;
  3537. }
  3538. static int __record_new_ref(int num, u64 dir, int index,
  3539. struct fs_path *name,
  3540. void *ctx)
  3541. {
  3542. struct send_ctx *sctx = ctx;
  3543. return record_ref(sctx->send_root, num, dir, index, name,
  3544. ctx, &sctx->new_refs);
  3545. }
  3546. static int __record_deleted_ref(int num, u64 dir, int index,
  3547. struct fs_path *name,
  3548. void *ctx)
  3549. {
  3550. struct send_ctx *sctx = ctx;
  3551. return record_ref(sctx->parent_root, num, dir, index, name,
  3552. ctx, &sctx->deleted_refs);
  3553. }
  3554. static int record_new_ref(struct send_ctx *sctx)
  3555. {
  3556. int ret;
  3557. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  3558. sctx->cmp_key, 0, __record_new_ref, sctx);
  3559. if (ret < 0)
  3560. goto out;
  3561. ret = 0;
  3562. out:
  3563. return ret;
  3564. }
  3565. static int record_deleted_ref(struct send_ctx *sctx)
  3566. {
  3567. int ret;
  3568. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  3569. sctx->cmp_key, 0, __record_deleted_ref, sctx);
  3570. if (ret < 0)
  3571. goto out;
  3572. ret = 0;
  3573. out:
  3574. return ret;
  3575. }
  3576. struct find_ref_ctx {
  3577. u64 dir;
  3578. u64 dir_gen;
  3579. struct btrfs_root *root;
  3580. struct fs_path *name;
  3581. int found_idx;
  3582. };
  3583. static int __find_iref(int num, u64 dir, int index,
  3584. struct fs_path *name,
  3585. void *ctx_)
  3586. {
  3587. struct find_ref_ctx *ctx = ctx_;
  3588. u64 dir_gen;
  3589. int ret;
  3590. if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
  3591. strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
  3592. /*
  3593. * To avoid doing extra lookups we'll only do this if everything
  3594. * else matches.
  3595. */
  3596. ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
  3597. NULL, NULL, NULL);
  3598. if (ret)
  3599. return ret;
  3600. if (dir_gen != ctx->dir_gen)
  3601. return 0;
  3602. ctx->found_idx = num;
  3603. return 1;
  3604. }
  3605. return 0;
  3606. }
  3607. static int find_iref(struct btrfs_root *root,
  3608. struct btrfs_path *path,
  3609. struct btrfs_key *key,
  3610. u64 dir, u64 dir_gen, struct fs_path *name)
  3611. {
  3612. int ret;
  3613. struct find_ref_ctx ctx;
  3614. ctx.dir = dir;
  3615. ctx.name = name;
  3616. ctx.dir_gen = dir_gen;
  3617. ctx.found_idx = -1;
  3618. ctx.root = root;
  3619. ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
  3620. if (ret < 0)
  3621. return ret;
  3622. if (ctx.found_idx == -1)
  3623. return -ENOENT;
  3624. return ctx.found_idx;
  3625. }
  3626. static int __record_changed_new_ref(int num, u64 dir, int index,
  3627. struct fs_path *name,
  3628. void *ctx)
  3629. {
  3630. u64 dir_gen;
  3631. int ret;
  3632. struct send_ctx *sctx = ctx;
  3633. ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
  3634. NULL, NULL, NULL);
  3635. if (ret)
  3636. return ret;
  3637. ret = find_iref(sctx->parent_root, sctx->right_path,
  3638. sctx->cmp_key, dir, dir_gen, name);
  3639. if (ret == -ENOENT)
  3640. ret = __record_new_ref(num, dir, index, name, sctx);
  3641. else if (ret > 0)
  3642. ret = 0;
  3643. return ret;
  3644. }
  3645. static int __record_changed_deleted_ref(int num, u64 dir, int index,
  3646. struct fs_path *name,
  3647. void *ctx)
  3648. {
  3649. u64 dir_gen;
  3650. int ret;
  3651. struct send_ctx *sctx = ctx;
  3652. ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
  3653. NULL, NULL, NULL);
  3654. if (ret)
  3655. return ret;
  3656. ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
  3657. dir, dir_gen, name);
  3658. if (ret == -ENOENT)
  3659. ret = __record_deleted_ref(num, dir, index, name, sctx);
  3660. else if (ret > 0)
  3661. ret = 0;
  3662. return ret;
  3663. }
  3664. static int record_changed_ref(struct send_ctx *sctx)
  3665. {
  3666. int ret = 0;
  3667. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  3668. sctx->cmp_key, 0, __record_changed_new_ref, sctx);
  3669. if (ret < 0)
  3670. goto out;
  3671. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  3672. sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
  3673. if (ret < 0)
  3674. goto out;
  3675. ret = 0;
  3676. out:
  3677. return ret;
  3678. }
  3679. /*
  3680. * Record and process all refs at once. Needed when an inode changes the
  3681. * generation number, which means that it was deleted and recreated.
  3682. */
  3683. static int process_all_refs(struct send_ctx *sctx,
  3684. enum btrfs_compare_tree_result cmd)
  3685. {
  3686. int ret;
  3687. struct btrfs_root *root;
  3688. struct btrfs_path *path;
  3689. struct btrfs_key key;
  3690. struct btrfs_key found_key;
  3691. struct extent_buffer *eb;
  3692. int slot;
  3693. iterate_inode_ref_t cb;
  3694. int pending_move = 0;
  3695. path = alloc_path_for_send();
  3696. if (!path)
  3697. return -ENOMEM;
  3698. if (cmd == BTRFS_COMPARE_TREE_NEW) {
  3699. root = sctx->send_root;
  3700. cb = __record_new_ref;
  3701. } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
  3702. root = sctx->parent_root;
  3703. cb = __record_deleted_ref;
  3704. } else {
  3705. btrfs_err(sctx->send_root->fs_info,
  3706. "Wrong command %d in process_all_refs", cmd);
  3707. ret = -EINVAL;
  3708. goto out;
  3709. }
  3710. key.objectid = sctx->cmp_key->objectid;
  3711. key.type = BTRFS_INODE_REF_KEY;
  3712. key.offset = 0;
  3713. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3714. if (ret < 0)
  3715. goto out;
  3716. while (1) {
  3717. eb = path->nodes[0];
  3718. slot = path->slots[0];
  3719. if (slot >= btrfs_header_nritems(eb)) {
  3720. ret = btrfs_next_leaf(root, path);
  3721. if (ret < 0)
  3722. goto out;
  3723. else if (ret > 0)
  3724. break;
  3725. continue;
  3726. }
  3727. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3728. if (found_key.objectid != key.objectid ||
  3729. (found_key.type != BTRFS_INODE_REF_KEY &&
  3730. found_key.type != BTRFS_INODE_EXTREF_KEY))
  3731. break;
  3732. ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
  3733. if (ret < 0)
  3734. goto out;
  3735. path->slots[0]++;
  3736. }
  3737. btrfs_release_path(path);
  3738. /*
  3739. * We don't actually care about pending_move as we are simply
  3740. * re-creating this inode and will be rename'ing it into place once we
  3741. * rename the parent directory.
  3742. */
  3743. ret = process_recorded_refs(sctx, &pending_move);
  3744. out:
  3745. btrfs_free_path(path);
  3746. return ret;
  3747. }
  3748. static int send_set_xattr(struct send_ctx *sctx,
  3749. struct fs_path *path,
  3750. const char *name, int name_len,
  3751. const char *data, int data_len)
  3752. {
  3753. int ret = 0;
  3754. ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
  3755. if (ret < 0)
  3756. goto out;
  3757. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  3758. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  3759. TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
  3760. ret = send_cmd(sctx);
  3761. tlv_put_failure:
  3762. out:
  3763. return ret;
  3764. }
  3765. static int send_remove_xattr(struct send_ctx *sctx,
  3766. struct fs_path *path,
  3767. const char *name, int name_len)
  3768. {
  3769. int ret = 0;
  3770. ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
  3771. if (ret < 0)
  3772. goto out;
  3773. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  3774. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  3775. ret = send_cmd(sctx);
  3776. tlv_put_failure:
  3777. out:
  3778. return ret;
  3779. }
  3780. static int __process_new_xattr(int num, struct btrfs_key *di_key,
  3781. const char *name, int name_len,
  3782. const char *data, int data_len,
  3783. u8 type, void *ctx)
  3784. {
  3785. int ret;
  3786. struct send_ctx *sctx = ctx;
  3787. struct fs_path *p;
  3788. struct posix_acl_xattr_header dummy_acl;
  3789. p = fs_path_alloc();
  3790. if (!p)
  3791. return -ENOMEM;
  3792. /*
  3793. * This hack is needed because empty acls are stored as zero byte
  3794. * data in xattrs. Problem with that is, that receiving these zero byte
  3795. * acls will fail later. To fix this, we send a dummy acl list that
  3796. * only contains the version number and no entries.
  3797. */
  3798. if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
  3799. !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
  3800. if (data_len == 0) {
  3801. dummy_acl.a_version =
  3802. cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  3803. data = (char *)&dummy_acl;
  3804. data_len = sizeof(dummy_acl);
  3805. }
  3806. }
  3807. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3808. if (ret < 0)
  3809. goto out;
  3810. ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
  3811. out:
  3812. fs_path_free(p);
  3813. return ret;
  3814. }
  3815. static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
  3816. const char *name, int name_len,
  3817. const char *data, int data_len,
  3818. u8 type, void *ctx)
  3819. {
  3820. int ret;
  3821. struct send_ctx *sctx = ctx;
  3822. struct fs_path *p;
  3823. p = fs_path_alloc();
  3824. if (!p)
  3825. return -ENOMEM;
  3826. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3827. if (ret < 0)
  3828. goto out;
  3829. ret = send_remove_xattr(sctx, p, name, name_len);
  3830. out:
  3831. fs_path_free(p);
  3832. return ret;
  3833. }
  3834. static int process_new_xattr(struct send_ctx *sctx)
  3835. {
  3836. int ret = 0;
  3837. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  3838. sctx->cmp_key, __process_new_xattr, sctx);
  3839. return ret;
  3840. }
  3841. static int process_deleted_xattr(struct send_ctx *sctx)
  3842. {
  3843. return iterate_dir_item(sctx->parent_root, sctx->right_path,
  3844. sctx->cmp_key, __process_deleted_xattr, sctx);
  3845. }
  3846. struct find_xattr_ctx {
  3847. const char *name;
  3848. int name_len;
  3849. int found_idx;
  3850. char *found_data;
  3851. int found_data_len;
  3852. };
  3853. static int __find_xattr(int num, struct btrfs_key *di_key,
  3854. const char *name, int name_len,
  3855. const char *data, int data_len,
  3856. u8 type, void *vctx)
  3857. {
  3858. struct find_xattr_ctx *ctx = vctx;
  3859. if (name_len == ctx->name_len &&
  3860. strncmp(name, ctx->name, name_len) == 0) {
  3861. ctx->found_idx = num;
  3862. ctx->found_data_len = data_len;
  3863. ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
  3864. if (!ctx->found_data)
  3865. return -ENOMEM;
  3866. return 1;
  3867. }
  3868. return 0;
  3869. }
  3870. static int find_xattr(struct btrfs_root *root,
  3871. struct btrfs_path *path,
  3872. struct btrfs_key *key,
  3873. const char *name, int name_len,
  3874. char **data, int *data_len)
  3875. {
  3876. int ret;
  3877. struct find_xattr_ctx ctx;
  3878. ctx.name = name;
  3879. ctx.name_len = name_len;
  3880. ctx.found_idx = -1;
  3881. ctx.found_data = NULL;
  3882. ctx.found_data_len = 0;
  3883. ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
  3884. if (ret < 0)
  3885. return ret;
  3886. if (ctx.found_idx == -1)
  3887. return -ENOENT;
  3888. if (data) {
  3889. *data = ctx.found_data;
  3890. *data_len = ctx.found_data_len;
  3891. } else {
  3892. kfree(ctx.found_data);
  3893. }
  3894. return ctx.found_idx;
  3895. }
  3896. static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
  3897. const char *name, int name_len,
  3898. const char *data, int data_len,
  3899. u8 type, void *ctx)
  3900. {
  3901. int ret;
  3902. struct send_ctx *sctx = ctx;
  3903. char *found_data = NULL;
  3904. int found_data_len = 0;
  3905. ret = find_xattr(sctx->parent_root, sctx->right_path,
  3906. sctx->cmp_key, name, name_len, &found_data,
  3907. &found_data_len);
  3908. if (ret == -ENOENT) {
  3909. ret = __process_new_xattr(num, di_key, name, name_len, data,
  3910. data_len, type, ctx);
  3911. } else if (ret >= 0) {
  3912. if (data_len != found_data_len ||
  3913. memcmp(data, found_data, data_len)) {
  3914. ret = __process_new_xattr(num, di_key, name, name_len,
  3915. data, data_len, type, ctx);
  3916. } else {
  3917. ret = 0;
  3918. }
  3919. }
  3920. kfree(found_data);
  3921. return ret;
  3922. }
  3923. static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
  3924. const char *name, int name_len,
  3925. const char *data, int data_len,
  3926. u8 type, void *ctx)
  3927. {
  3928. int ret;
  3929. struct send_ctx *sctx = ctx;
  3930. ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
  3931. name, name_len, NULL, NULL);
  3932. if (ret == -ENOENT)
  3933. ret = __process_deleted_xattr(num, di_key, name, name_len, data,
  3934. data_len, type, ctx);
  3935. else if (ret >= 0)
  3936. ret = 0;
  3937. return ret;
  3938. }
  3939. static int process_changed_xattr(struct send_ctx *sctx)
  3940. {
  3941. int ret = 0;
  3942. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  3943. sctx->cmp_key, __process_changed_new_xattr, sctx);
  3944. if (ret < 0)
  3945. goto out;
  3946. ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
  3947. sctx->cmp_key, __process_changed_deleted_xattr, sctx);
  3948. out:
  3949. return ret;
  3950. }
  3951. static int process_all_new_xattrs(struct send_ctx *sctx)
  3952. {
  3953. int ret;
  3954. struct btrfs_root *root;
  3955. struct btrfs_path *path;
  3956. struct btrfs_key key;
  3957. struct btrfs_key found_key;
  3958. struct extent_buffer *eb;
  3959. int slot;
  3960. path = alloc_path_for_send();
  3961. if (!path)
  3962. return -ENOMEM;
  3963. root = sctx->send_root;
  3964. key.objectid = sctx->cmp_key->objectid;
  3965. key.type = BTRFS_XATTR_ITEM_KEY;
  3966. key.offset = 0;
  3967. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3968. if (ret < 0)
  3969. goto out;
  3970. while (1) {
  3971. eb = path->nodes[0];
  3972. slot = path->slots[0];
  3973. if (slot >= btrfs_header_nritems(eb)) {
  3974. ret = btrfs_next_leaf(root, path);
  3975. if (ret < 0) {
  3976. goto out;
  3977. } else if (ret > 0) {
  3978. ret = 0;
  3979. break;
  3980. }
  3981. continue;
  3982. }
  3983. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3984. if (found_key.objectid != key.objectid ||
  3985. found_key.type != key.type) {
  3986. ret = 0;
  3987. goto out;
  3988. }
  3989. ret = iterate_dir_item(root, path, &found_key,
  3990. __process_new_xattr, sctx);
  3991. if (ret < 0)
  3992. goto out;
  3993. path->slots[0]++;
  3994. }
  3995. out:
  3996. btrfs_free_path(path);
  3997. return ret;
  3998. }
  3999. static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
  4000. {
  4001. struct btrfs_root *root = sctx->send_root;
  4002. struct btrfs_fs_info *fs_info = root->fs_info;
  4003. struct inode *inode;
  4004. struct page *page;
  4005. char *addr;
  4006. struct btrfs_key key;
  4007. pgoff_t index = offset >> PAGE_SHIFT;
  4008. pgoff_t last_index;
  4009. unsigned pg_offset = offset & ~PAGE_MASK;
  4010. ssize_t ret = 0;
  4011. key.objectid = sctx->cur_ino;
  4012. key.type = BTRFS_INODE_ITEM_KEY;
  4013. key.offset = 0;
  4014. inode = btrfs_iget(fs_info->sb, &key, root, NULL);
  4015. if (IS_ERR(inode))
  4016. return PTR_ERR(inode);
  4017. if (offset + len > i_size_read(inode)) {
  4018. if (offset > i_size_read(inode))
  4019. len = 0;
  4020. else
  4021. len = offset - i_size_read(inode);
  4022. }
  4023. if (len == 0)
  4024. goto out;
  4025. last_index = (offset + len - 1) >> PAGE_SHIFT;
  4026. /* initial readahead */
  4027. memset(&sctx->ra, 0, sizeof(struct file_ra_state));
  4028. file_ra_state_init(&sctx->ra, inode->i_mapping);
  4029. btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
  4030. last_index - index + 1);
  4031. while (index <= last_index) {
  4032. unsigned cur_len = min_t(unsigned, len,
  4033. PAGE_SIZE - pg_offset);
  4034. page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
  4035. if (!page) {
  4036. ret = -ENOMEM;
  4037. break;
  4038. }
  4039. if (!PageUptodate(page)) {
  4040. btrfs_readpage(NULL, page);
  4041. lock_page(page);
  4042. if (!PageUptodate(page)) {
  4043. unlock_page(page);
  4044. put_page(page);
  4045. ret = -EIO;
  4046. break;
  4047. }
  4048. }
  4049. addr = kmap(page);
  4050. memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
  4051. kunmap(page);
  4052. unlock_page(page);
  4053. put_page(page);
  4054. index++;
  4055. pg_offset = 0;
  4056. len -= cur_len;
  4057. ret += cur_len;
  4058. }
  4059. out:
  4060. iput(inode);
  4061. return ret;
  4062. }
  4063. /*
  4064. * Read some bytes from the current inode/file and send a write command to
  4065. * user space.
  4066. */
  4067. static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
  4068. {
  4069. struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
  4070. int ret = 0;
  4071. struct fs_path *p;
  4072. ssize_t num_read = 0;
  4073. p = fs_path_alloc();
  4074. if (!p)
  4075. return -ENOMEM;
  4076. btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
  4077. num_read = fill_read_buf(sctx, offset, len);
  4078. if (num_read <= 0) {
  4079. if (num_read < 0)
  4080. ret = num_read;
  4081. goto out;
  4082. }
  4083. ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
  4084. if (ret < 0)
  4085. goto out;
  4086. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  4087. if (ret < 0)
  4088. goto out;
  4089. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  4090. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4091. TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
  4092. ret = send_cmd(sctx);
  4093. tlv_put_failure:
  4094. out:
  4095. fs_path_free(p);
  4096. if (ret < 0)
  4097. return ret;
  4098. return num_read;
  4099. }
  4100. /*
  4101. * Send a clone command to user space.
  4102. */
  4103. static int send_clone(struct send_ctx *sctx,
  4104. u64 offset, u32 len,
  4105. struct clone_root *clone_root)
  4106. {
  4107. int ret = 0;
  4108. struct fs_path *p;
  4109. u64 gen;
  4110. btrfs_debug(sctx->send_root->fs_info,
  4111. "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
  4112. offset, len, clone_root->root->objectid, clone_root->ino,
  4113. clone_root->offset);
  4114. p = fs_path_alloc();
  4115. if (!p)
  4116. return -ENOMEM;
  4117. ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
  4118. if (ret < 0)
  4119. goto out;
  4120. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  4121. if (ret < 0)
  4122. goto out;
  4123. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4124. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
  4125. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  4126. if (clone_root->root == sctx->send_root) {
  4127. ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
  4128. &gen, NULL, NULL, NULL, NULL);
  4129. if (ret < 0)
  4130. goto out;
  4131. ret = get_cur_path(sctx, clone_root->ino, gen, p);
  4132. } else {
  4133. ret = get_inode_path(clone_root->root, clone_root->ino, p);
  4134. }
  4135. if (ret < 0)
  4136. goto out;
  4137. /*
  4138. * If the parent we're using has a received_uuid set then use that as
  4139. * our clone source as that is what we will look for when doing a
  4140. * receive.
  4141. *
  4142. * This covers the case that we create a snapshot off of a received
  4143. * subvolume and then use that as the parent and try to receive on a
  4144. * different host.
  4145. */
  4146. if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
  4147. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  4148. clone_root->root->root_item.received_uuid);
  4149. else
  4150. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  4151. clone_root->root->root_item.uuid);
  4152. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  4153. le64_to_cpu(clone_root->root->root_item.ctransid));
  4154. TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
  4155. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
  4156. clone_root->offset);
  4157. ret = send_cmd(sctx);
  4158. tlv_put_failure:
  4159. out:
  4160. fs_path_free(p);
  4161. return ret;
  4162. }
  4163. /*
  4164. * Send an update extent command to user space.
  4165. */
  4166. static int send_update_extent(struct send_ctx *sctx,
  4167. u64 offset, u32 len)
  4168. {
  4169. int ret = 0;
  4170. struct fs_path *p;
  4171. p = fs_path_alloc();
  4172. if (!p)
  4173. return -ENOMEM;
  4174. ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
  4175. if (ret < 0)
  4176. goto out;
  4177. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  4178. if (ret < 0)
  4179. goto out;
  4180. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  4181. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4182. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
  4183. ret = send_cmd(sctx);
  4184. tlv_put_failure:
  4185. out:
  4186. fs_path_free(p);
  4187. return ret;
  4188. }
  4189. static int send_hole(struct send_ctx *sctx, u64 end)
  4190. {
  4191. struct fs_path *p = NULL;
  4192. u64 offset = sctx->cur_inode_last_extent;
  4193. u64 len;
  4194. int ret = 0;
  4195. if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
  4196. return send_update_extent(sctx, offset, end - offset);
  4197. p = fs_path_alloc();
  4198. if (!p)
  4199. return -ENOMEM;
  4200. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  4201. if (ret < 0)
  4202. goto tlv_put_failure;
  4203. memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
  4204. while (offset < end) {
  4205. len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
  4206. ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
  4207. if (ret < 0)
  4208. break;
  4209. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  4210. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  4211. TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
  4212. ret = send_cmd(sctx);
  4213. if (ret < 0)
  4214. break;
  4215. offset += len;
  4216. }
  4217. tlv_put_failure:
  4218. fs_path_free(p);
  4219. return ret;
  4220. }
  4221. static int send_extent_data(struct send_ctx *sctx,
  4222. const u64 offset,
  4223. const u64 len)
  4224. {
  4225. u64 sent = 0;
  4226. if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
  4227. return send_update_extent(sctx, offset, len);
  4228. while (sent < len) {
  4229. u64 size = len - sent;
  4230. int ret;
  4231. if (size > BTRFS_SEND_READ_SIZE)
  4232. size = BTRFS_SEND_READ_SIZE;
  4233. ret = send_write(sctx, offset + sent, size);
  4234. if (ret < 0)
  4235. return ret;
  4236. if (!ret)
  4237. break;
  4238. sent += ret;
  4239. }
  4240. return 0;
  4241. }
  4242. static int clone_range(struct send_ctx *sctx,
  4243. struct clone_root *clone_root,
  4244. const u64 disk_byte,
  4245. u64 data_offset,
  4246. u64 offset,
  4247. u64 len)
  4248. {
  4249. struct btrfs_path *path;
  4250. struct btrfs_key key;
  4251. int ret;
  4252. path = alloc_path_for_send();
  4253. if (!path)
  4254. return -ENOMEM;
  4255. /*
  4256. * We can't send a clone operation for the entire range if we find
  4257. * extent items in the respective range in the source file that
  4258. * refer to different extents or if we find holes.
  4259. * So check for that and do a mix of clone and regular write/copy
  4260. * operations if needed.
  4261. *
  4262. * Example:
  4263. *
  4264. * mkfs.btrfs -f /dev/sda
  4265. * mount /dev/sda /mnt
  4266. * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
  4267. * cp --reflink=always /mnt/foo /mnt/bar
  4268. * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
  4269. * btrfs subvolume snapshot -r /mnt /mnt/snap
  4270. *
  4271. * If when we send the snapshot and we are processing file bar (which
  4272. * has a higher inode number than foo) we blindly send a clone operation
  4273. * for the [0, 100K[ range from foo to bar, the receiver ends up getting
  4274. * a file bar that matches the content of file foo - iow, doesn't match
  4275. * the content from bar in the original filesystem.
  4276. */
  4277. key.objectid = clone_root->ino;
  4278. key.type = BTRFS_EXTENT_DATA_KEY;
  4279. key.offset = clone_root->offset;
  4280. ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
  4281. if (ret < 0)
  4282. goto out;
  4283. if (ret > 0 && path->slots[0] > 0) {
  4284. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
  4285. if (key.objectid == clone_root->ino &&
  4286. key.type == BTRFS_EXTENT_DATA_KEY)
  4287. path->slots[0]--;
  4288. }
  4289. while (true) {
  4290. struct extent_buffer *leaf = path->nodes[0];
  4291. int slot = path->slots[0];
  4292. struct btrfs_file_extent_item *ei;
  4293. u8 type;
  4294. u64 ext_len;
  4295. u64 clone_len;
  4296. if (slot >= btrfs_header_nritems(leaf)) {
  4297. ret = btrfs_next_leaf(clone_root->root, path);
  4298. if (ret < 0)
  4299. goto out;
  4300. else if (ret > 0)
  4301. break;
  4302. continue;
  4303. }
  4304. btrfs_item_key_to_cpu(leaf, &key, slot);
  4305. /*
  4306. * We might have an implicit trailing hole (NO_HOLES feature
  4307. * enabled). We deal with it after leaving this loop.
  4308. */
  4309. if (key.objectid != clone_root->ino ||
  4310. key.type != BTRFS_EXTENT_DATA_KEY)
  4311. break;
  4312. ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  4313. type = btrfs_file_extent_type(leaf, ei);
  4314. if (type == BTRFS_FILE_EXTENT_INLINE) {
  4315. ext_len = btrfs_file_extent_inline_len(leaf, slot, ei);
  4316. ext_len = PAGE_ALIGN(ext_len);
  4317. } else {
  4318. ext_len = btrfs_file_extent_num_bytes(leaf, ei);
  4319. }
  4320. if (key.offset + ext_len <= clone_root->offset)
  4321. goto next;
  4322. if (key.offset > clone_root->offset) {
  4323. /* Implicit hole, NO_HOLES feature enabled. */
  4324. u64 hole_len = key.offset - clone_root->offset;
  4325. if (hole_len > len)
  4326. hole_len = len;
  4327. ret = send_extent_data(sctx, offset, hole_len);
  4328. if (ret < 0)
  4329. goto out;
  4330. len -= hole_len;
  4331. if (len == 0)
  4332. break;
  4333. offset += hole_len;
  4334. clone_root->offset += hole_len;
  4335. data_offset += hole_len;
  4336. }
  4337. if (key.offset >= clone_root->offset + len)
  4338. break;
  4339. clone_len = min_t(u64, ext_len, len);
  4340. if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
  4341. btrfs_file_extent_offset(leaf, ei) == data_offset)
  4342. ret = send_clone(sctx, offset, clone_len, clone_root);
  4343. else
  4344. ret = send_extent_data(sctx, offset, clone_len);
  4345. if (ret < 0)
  4346. goto out;
  4347. len -= clone_len;
  4348. if (len == 0)
  4349. break;
  4350. offset += clone_len;
  4351. clone_root->offset += clone_len;
  4352. data_offset += clone_len;
  4353. next:
  4354. path->slots[0]++;
  4355. }
  4356. if (len > 0)
  4357. ret = send_extent_data(sctx, offset, len);
  4358. else
  4359. ret = 0;
  4360. out:
  4361. btrfs_free_path(path);
  4362. return ret;
  4363. }
  4364. static int send_write_or_clone(struct send_ctx *sctx,
  4365. struct btrfs_path *path,
  4366. struct btrfs_key *key,
  4367. struct clone_root *clone_root)
  4368. {
  4369. int ret = 0;
  4370. struct btrfs_file_extent_item *ei;
  4371. u64 offset = key->offset;
  4372. u64 len;
  4373. u8 type;
  4374. u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
  4375. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4376. struct btrfs_file_extent_item);
  4377. type = btrfs_file_extent_type(path->nodes[0], ei);
  4378. if (type == BTRFS_FILE_EXTENT_INLINE) {
  4379. len = btrfs_file_extent_inline_len(path->nodes[0],
  4380. path->slots[0], ei);
  4381. /*
  4382. * it is possible the inline item won't cover the whole page,
  4383. * but there may be items after this page. Make
  4384. * sure to send the whole thing
  4385. */
  4386. len = PAGE_ALIGN(len);
  4387. } else {
  4388. len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
  4389. }
  4390. if (offset + len > sctx->cur_inode_size)
  4391. len = sctx->cur_inode_size - offset;
  4392. if (len == 0) {
  4393. ret = 0;
  4394. goto out;
  4395. }
  4396. if (clone_root && IS_ALIGNED(offset + len, bs)) {
  4397. u64 disk_byte;
  4398. u64 data_offset;
  4399. disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
  4400. data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
  4401. ret = clone_range(sctx, clone_root, disk_byte, data_offset,
  4402. offset, len);
  4403. } else {
  4404. ret = send_extent_data(sctx, offset, len);
  4405. }
  4406. out:
  4407. return ret;
  4408. }
  4409. static int is_extent_unchanged(struct send_ctx *sctx,
  4410. struct btrfs_path *left_path,
  4411. struct btrfs_key *ekey)
  4412. {
  4413. int ret = 0;
  4414. struct btrfs_key key;
  4415. struct btrfs_path *path = NULL;
  4416. struct extent_buffer *eb;
  4417. int slot;
  4418. struct btrfs_key found_key;
  4419. struct btrfs_file_extent_item *ei;
  4420. u64 left_disknr;
  4421. u64 right_disknr;
  4422. u64 left_offset;
  4423. u64 right_offset;
  4424. u64 left_offset_fixed;
  4425. u64 left_len;
  4426. u64 right_len;
  4427. u64 left_gen;
  4428. u64 right_gen;
  4429. u8 left_type;
  4430. u8 right_type;
  4431. path = alloc_path_for_send();
  4432. if (!path)
  4433. return -ENOMEM;
  4434. eb = left_path->nodes[0];
  4435. slot = left_path->slots[0];
  4436. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  4437. left_type = btrfs_file_extent_type(eb, ei);
  4438. if (left_type != BTRFS_FILE_EXTENT_REG) {
  4439. ret = 0;
  4440. goto out;
  4441. }
  4442. left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  4443. left_len = btrfs_file_extent_num_bytes(eb, ei);
  4444. left_offset = btrfs_file_extent_offset(eb, ei);
  4445. left_gen = btrfs_file_extent_generation(eb, ei);
  4446. /*
  4447. * Following comments will refer to these graphics. L is the left
  4448. * extents which we are checking at the moment. 1-8 are the right
  4449. * extents that we iterate.
  4450. *
  4451. * |-----L-----|
  4452. * |-1-|-2a-|-3-|-4-|-5-|-6-|
  4453. *
  4454. * |-----L-----|
  4455. * |--1--|-2b-|...(same as above)
  4456. *
  4457. * Alternative situation. Happens on files where extents got split.
  4458. * |-----L-----|
  4459. * |-----------7-----------|-6-|
  4460. *
  4461. * Alternative situation. Happens on files which got larger.
  4462. * |-----L-----|
  4463. * |-8-|
  4464. * Nothing follows after 8.
  4465. */
  4466. key.objectid = ekey->objectid;
  4467. key.type = BTRFS_EXTENT_DATA_KEY;
  4468. key.offset = ekey->offset;
  4469. ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
  4470. if (ret < 0)
  4471. goto out;
  4472. if (ret) {
  4473. ret = 0;
  4474. goto out;
  4475. }
  4476. /*
  4477. * Handle special case where the right side has no extents at all.
  4478. */
  4479. eb = path->nodes[0];
  4480. slot = path->slots[0];
  4481. btrfs_item_key_to_cpu(eb, &found_key, slot);
  4482. if (found_key.objectid != key.objectid ||
  4483. found_key.type != key.type) {
  4484. /* If we're a hole then just pretend nothing changed */
  4485. ret = (left_disknr) ? 0 : 1;
  4486. goto out;
  4487. }
  4488. /*
  4489. * We're now on 2a, 2b or 7.
  4490. */
  4491. key = found_key;
  4492. while (key.offset < ekey->offset + left_len) {
  4493. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  4494. right_type = btrfs_file_extent_type(eb, ei);
  4495. if (right_type != BTRFS_FILE_EXTENT_REG &&
  4496. right_type != BTRFS_FILE_EXTENT_INLINE) {
  4497. ret = 0;
  4498. goto out;
  4499. }
  4500. if (right_type == BTRFS_FILE_EXTENT_INLINE) {
  4501. right_len = btrfs_file_extent_inline_len(eb, slot, ei);
  4502. right_len = PAGE_ALIGN(right_len);
  4503. } else {
  4504. right_len = btrfs_file_extent_num_bytes(eb, ei);
  4505. }
  4506. /*
  4507. * Are we at extent 8? If yes, we know the extent is changed.
  4508. * This may only happen on the first iteration.
  4509. */
  4510. if (found_key.offset + right_len <= ekey->offset) {
  4511. /* If we're a hole just pretend nothing changed */
  4512. ret = (left_disknr) ? 0 : 1;
  4513. goto out;
  4514. }
  4515. /*
  4516. * We just wanted to see if when we have an inline extent, what
  4517. * follows it is a regular extent (wanted to check the above
  4518. * condition for inline extents too). This should normally not
  4519. * happen but it's possible for example when we have an inline
  4520. * compressed extent representing data with a size matching
  4521. * the page size (currently the same as sector size).
  4522. */
  4523. if (right_type == BTRFS_FILE_EXTENT_INLINE) {
  4524. ret = 0;
  4525. goto out;
  4526. }
  4527. right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  4528. right_offset = btrfs_file_extent_offset(eb, ei);
  4529. right_gen = btrfs_file_extent_generation(eb, ei);
  4530. left_offset_fixed = left_offset;
  4531. if (key.offset < ekey->offset) {
  4532. /* Fix the right offset for 2a and 7. */
  4533. right_offset += ekey->offset - key.offset;
  4534. } else {
  4535. /* Fix the left offset for all behind 2a and 2b */
  4536. left_offset_fixed += key.offset - ekey->offset;
  4537. }
  4538. /*
  4539. * Check if we have the same extent.
  4540. */
  4541. if (left_disknr != right_disknr ||
  4542. left_offset_fixed != right_offset ||
  4543. left_gen != right_gen) {
  4544. ret = 0;
  4545. goto out;
  4546. }
  4547. /*
  4548. * Go to the next extent.
  4549. */
  4550. ret = btrfs_next_item(sctx->parent_root, path);
  4551. if (ret < 0)
  4552. goto out;
  4553. if (!ret) {
  4554. eb = path->nodes[0];
  4555. slot = path->slots[0];
  4556. btrfs_item_key_to_cpu(eb, &found_key, slot);
  4557. }
  4558. if (ret || found_key.objectid != key.objectid ||
  4559. found_key.type != key.type) {
  4560. key.offset += right_len;
  4561. break;
  4562. }
  4563. if (found_key.offset != key.offset + right_len) {
  4564. ret = 0;
  4565. goto out;
  4566. }
  4567. key = found_key;
  4568. }
  4569. /*
  4570. * We're now behind the left extent (treat as unchanged) or at the end
  4571. * of the right side (treat as changed).
  4572. */
  4573. if (key.offset >= ekey->offset + left_len)
  4574. ret = 1;
  4575. else
  4576. ret = 0;
  4577. out:
  4578. btrfs_free_path(path);
  4579. return ret;
  4580. }
  4581. static int get_last_extent(struct send_ctx *sctx, u64 offset)
  4582. {
  4583. struct btrfs_path *path;
  4584. struct btrfs_root *root = sctx->send_root;
  4585. struct btrfs_file_extent_item *fi;
  4586. struct btrfs_key key;
  4587. u64 extent_end;
  4588. u8 type;
  4589. int ret;
  4590. path = alloc_path_for_send();
  4591. if (!path)
  4592. return -ENOMEM;
  4593. sctx->cur_inode_last_extent = 0;
  4594. key.objectid = sctx->cur_ino;
  4595. key.type = BTRFS_EXTENT_DATA_KEY;
  4596. key.offset = offset;
  4597. ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
  4598. if (ret < 0)
  4599. goto out;
  4600. ret = 0;
  4601. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  4602. if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
  4603. goto out;
  4604. fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4605. struct btrfs_file_extent_item);
  4606. type = btrfs_file_extent_type(path->nodes[0], fi);
  4607. if (type == BTRFS_FILE_EXTENT_INLINE) {
  4608. u64 size = btrfs_file_extent_inline_len(path->nodes[0],
  4609. path->slots[0], fi);
  4610. extent_end = ALIGN(key.offset + size,
  4611. sctx->send_root->sectorsize);
  4612. } else {
  4613. extent_end = key.offset +
  4614. btrfs_file_extent_num_bytes(path->nodes[0], fi);
  4615. }
  4616. sctx->cur_inode_last_extent = extent_end;
  4617. out:
  4618. btrfs_free_path(path);
  4619. return ret;
  4620. }
  4621. static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
  4622. struct btrfs_key *key)
  4623. {
  4624. struct btrfs_file_extent_item *fi;
  4625. u64 extent_end;
  4626. u8 type;
  4627. int ret = 0;
  4628. if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
  4629. return 0;
  4630. if (sctx->cur_inode_last_extent == (u64)-1) {
  4631. ret = get_last_extent(sctx, key->offset - 1);
  4632. if (ret)
  4633. return ret;
  4634. }
  4635. fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4636. struct btrfs_file_extent_item);
  4637. type = btrfs_file_extent_type(path->nodes[0], fi);
  4638. if (type == BTRFS_FILE_EXTENT_INLINE) {
  4639. u64 size = btrfs_file_extent_inline_len(path->nodes[0],
  4640. path->slots[0], fi);
  4641. extent_end = ALIGN(key->offset + size,
  4642. sctx->send_root->sectorsize);
  4643. } else {
  4644. extent_end = key->offset +
  4645. btrfs_file_extent_num_bytes(path->nodes[0], fi);
  4646. }
  4647. if (path->slots[0] == 0 &&
  4648. sctx->cur_inode_last_extent < key->offset) {
  4649. /*
  4650. * We might have skipped entire leafs that contained only
  4651. * file extent items for our current inode. These leafs have
  4652. * a generation number smaller (older) than the one in the
  4653. * current leaf and the leaf our last extent came from, and
  4654. * are located between these 2 leafs.
  4655. */
  4656. ret = get_last_extent(sctx, key->offset - 1);
  4657. if (ret)
  4658. return ret;
  4659. }
  4660. if (sctx->cur_inode_last_extent < key->offset)
  4661. ret = send_hole(sctx, key->offset);
  4662. sctx->cur_inode_last_extent = extent_end;
  4663. return ret;
  4664. }
  4665. static int process_extent(struct send_ctx *sctx,
  4666. struct btrfs_path *path,
  4667. struct btrfs_key *key)
  4668. {
  4669. struct clone_root *found_clone = NULL;
  4670. int ret = 0;
  4671. if (S_ISLNK(sctx->cur_inode_mode))
  4672. return 0;
  4673. if (sctx->parent_root && !sctx->cur_inode_new) {
  4674. ret = is_extent_unchanged(sctx, path, key);
  4675. if (ret < 0)
  4676. goto out;
  4677. if (ret) {
  4678. ret = 0;
  4679. goto out_hole;
  4680. }
  4681. } else {
  4682. struct btrfs_file_extent_item *ei;
  4683. u8 type;
  4684. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4685. struct btrfs_file_extent_item);
  4686. type = btrfs_file_extent_type(path->nodes[0], ei);
  4687. if (type == BTRFS_FILE_EXTENT_PREALLOC ||
  4688. type == BTRFS_FILE_EXTENT_REG) {
  4689. /*
  4690. * The send spec does not have a prealloc command yet,
  4691. * so just leave a hole for prealloc'ed extents until
  4692. * we have enough commands queued up to justify rev'ing
  4693. * the send spec.
  4694. */
  4695. if (type == BTRFS_FILE_EXTENT_PREALLOC) {
  4696. ret = 0;
  4697. goto out;
  4698. }
  4699. /* Have a hole, just skip it. */
  4700. if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
  4701. ret = 0;
  4702. goto out;
  4703. }
  4704. }
  4705. }
  4706. ret = find_extent_clone(sctx, path, key->objectid, key->offset,
  4707. sctx->cur_inode_size, &found_clone);
  4708. if (ret != -ENOENT && ret < 0)
  4709. goto out;
  4710. ret = send_write_or_clone(sctx, path, key, found_clone);
  4711. if (ret)
  4712. goto out;
  4713. out_hole:
  4714. ret = maybe_send_hole(sctx, path, key);
  4715. out:
  4716. return ret;
  4717. }
  4718. static int process_all_extents(struct send_ctx *sctx)
  4719. {
  4720. int ret;
  4721. struct btrfs_root *root;
  4722. struct btrfs_path *path;
  4723. struct btrfs_key key;
  4724. struct btrfs_key found_key;
  4725. struct extent_buffer *eb;
  4726. int slot;
  4727. root = sctx->send_root;
  4728. path = alloc_path_for_send();
  4729. if (!path)
  4730. return -ENOMEM;
  4731. key.objectid = sctx->cmp_key->objectid;
  4732. key.type = BTRFS_EXTENT_DATA_KEY;
  4733. key.offset = 0;
  4734. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4735. if (ret < 0)
  4736. goto out;
  4737. while (1) {
  4738. eb = path->nodes[0];
  4739. slot = path->slots[0];
  4740. if (slot >= btrfs_header_nritems(eb)) {
  4741. ret = btrfs_next_leaf(root, path);
  4742. if (ret < 0) {
  4743. goto out;
  4744. } else if (ret > 0) {
  4745. ret = 0;
  4746. break;
  4747. }
  4748. continue;
  4749. }
  4750. btrfs_item_key_to_cpu(eb, &found_key, slot);
  4751. if (found_key.objectid != key.objectid ||
  4752. found_key.type != key.type) {
  4753. ret = 0;
  4754. goto out;
  4755. }
  4756. ret = process_extent(sctx, path, &found_key);
  4757. if (ret < 0)
  4758. goto out;
  4759. path->slots[0]++;
  4760. }
  4761. out:
  4762. btrfs_free_path(path);
  4763. return ret;
  4764. }
  4765. static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
  4766. int *pending_move,
  4767. int *refs_processed)
  4768. {
  4769. int ret = 0;
  4770. if (sctx->cur_ino == 0)
  4771. goto out;
  4772. if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
  4773. sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
  4774. goto out;
  4775. if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
  4776. goto out;
  4777. ret = process_recorded_refs(sctx, pending_move);
  4778. if (ret < 0)
  4779. goto out;
  4780. *refs_processed = 1;
  4781. out:
  4782. return ret;
  4783. }
  4784. static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
  4785. {
  4786. int ret = 0;
  4787. u64 left_mode;
  4788. u64 left_uid;
  4789. u64 left_gid;
  4790. u64 right_mode;
  4791. u64 right_uid;
  4792. u64 right_gid;
  4793. int need_chmod = 0;
  4794. int need_chown = 0;
  4795. int pending_move = 0;
  4796. int refs_processed = 0;
  4797. ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
  4798. &refs_processed);
  4799. if (ret < 0)
  4800. goto out;
  4801. /*
  4802. * We have processed the refs and thus need to advance send_progress.
  4803. * Now, calls to get_cur_xxx will take the updated refs of the current
  4804. * inode into account.
  4805. *
  4806. * On the other hand, if our current inode is a directory and couldn't
  4807. * be moved/renamed because its parent was renamed/moved too and it has
  4808. * a higher inode number, we can only move/rename our current inode
  4809. * after we moved/renamed its parent. Therefore in this case operate on
  4810. * the old path (pre move/rename) of our current inode, and the
  4811. * move/rename will be performed later.
  4812. */
  4813. if (refs_processed && !pending_move)
  4814. sctx->send_progress = sctx->cur_ino + 1;
  4815. if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
  4816. goto out;
  4817. if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
  4818. goto out;
  4819. ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
  4820. &left_mode, &left_uid, &left_gid, NULL);
  4821. if (ret < 0)
  4822. goto out;
  4823. if (!sctx->parent_root || sctx->cur_inode_new) {
  4824. need_chown = 1;
  4825. if (!S_ISLNK(sctx->cur_inode_mode))
  4826. need_chmod = 1;
  4827. } else {
  4828. ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
  4829. NULL, NULL, &right_mode, &right_uid,
  4830. &right_gid, NULL);
  4831. if (ret < 0)
  4832. goto out;
  4833. if (left_uid != right_uid || left_gid != right_gid)
  4834. need_chown = 1;
  4835. if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
  4836. need_chmod = 1;
  4837. }
  4838. if (S_ISREG(sctx->cur_inode_mode)) {
  4839. if (need_send_hole(sctx)) {
  4840. if (sctx->cur_inode_last_extent == (u64)-1 ||
  4841. sctx->cur_inode_last_extent <
  4842. sctx->cur_inode_size) {
  4843. ret = get_last_extent(sctx, (u64)-1);
  4844. if (ret)
  4845. goto out;
  4846. }
  4847. if (sctx->cur_inode_last_extent <
  4848. sctx->cur_inode_size) {
  4849. ret = send_hole(sctx, sctx->cur_inode_size);
  4850. if (ret)
  4851. goto out;
  4852. }
  4853. }
  4854. ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  4855. sctx->cur_inode_size);
  4856. if (ret < 0)
  4857. goto out;
  4858. }
  4859. if (need_chown) {
  4860. ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  4861. left_uid, left_gid);
  4862. if (ret < 0)
  4863. goto out;
  4864. }
  4865. if (need_chmod) {
  4866. ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  4867. left_mode);
  4868. if (ret < 0)
  4869. goto out;
  4870. }
  4871. /*
  4872. * If other directory inodes depended on our current directory
  4873. * inode's move/rename, now do their move/rename operations.
  4874. */
  4875. if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
  4876. ret = apply_children_dir_moves(sctx);
  4877. if (ret)
  4878. goto out;
  4879. /*
  4880. * Need to send that every time, no matter if it actually
  4881. * changed between the two trees as we have done changes to
  4882. * the inode before. If our inode is a directory and it's
  4883. * waiting to be moved/renamed, we will send its utimes when
  4884. * it's moved/renamed, therefore we don't need to do it here.
  4885. */
  4886. sctx->send_progress = sctx->cur_ino + 1;
  4887. ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
  4888. if (ret < 0)
  4889. goto out;
  4890. }
  4891. out:
  4892. return ret;
  4893. }
  4894. static int changed_inode(struct send_ctx *sctx,
  4895. enum btrfs_compare_tree_result result)
  4896. {
  4897. int ret = 0;
  4898. struct btrfs_key *key = sctx->cmp_key;
  4899. struct btrfs_inode_item *left_ii = NULL;
  4900. struct btrfs_inode_item *right_ii = NULL;
  4901. u64 left_gen = 0;
  4902. u64 right_gen = 0;
  4903. sctx->cur_ino = key->objectid;
  4904. sctx->cur_inode_new_gen = 0;
  4905. sctx->cur_inode_last_extent = (u64)-1;
  4906. /*
  4907. * Set send_progress to current inode. This will tell all get_cur_xxx
  4908. * functions that the current inode's refs are not updated yet. Later,
  4909. * when process_recorded_refs is finished, it is set to cur_ino + 1.
  4910. */
  4911. sctx->send_progress = sctx->cur_ino;
  4912. if (result == BTRFS_COMPARE_TREE_NEW ||
  4913. result == BTRFS_COMPARE_TREE_CHANGED) {
  4914. left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
  4915. sctx->left_path->slots[0],
  4916. struct btrfs_inode_item);
  4917. left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
  4918. left_ii);
  4919. } else {
  4920. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  4921. sctx->right_path->slots[0],
  4922. struct btrfs_inode_item);
  4923. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  4924. right_ii);
  4925. }
  4926. if (result == BTRFS_COMPARE_TREE_CHANGED) {
  4927. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  4928. sctx->right_path->slots[0],
  4929. struct btrfs_inode_item);
  4930. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  4931. right_ii);
  4932. /*
  4933. * The cur_ino = root dir case is special here. We can't treat
  4934. * the inode as deleted+reused because it would generate a
  4935. * stream that tries to delete/mkdir the root dir.
  4936. */
  4937. if (left_gen != right_gen &&
  4938. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  4939. sctx->cur_inode_new_gen = 1;
  4940. }
  4941. if (result == BTRFS_COMPARE_TREE_NEW) {
  4942. sctx->cur_inode_gen = left_gen;
  4943. sctx->cur_inode_new = 1;
  4944. sctx->cur_inode_deleted = 0;
  4945. sctx->cur_inode_size = btrfs_inode_size(
  4946. sctx->left_path->nodes[0], left_ii);
  4947. sctx->cur_inode_mode = btrfs_inode_mode(
  4948. sctx->left_path->nodes[0], left_ii);
  4949. sctx->cur_inode_rdev = btrfs_inode_rdev(
  4950. sctx->left_path->nodes[0], left_ii);
  4951. if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  4952. ret = send_create_inode_if_needed(sctx);
  4953. } else if (result == BTRFS_COMPARE_TREE_DELETED) {
  4954. sctx->cur_inode_gen = right_gen;
  4955. sctx->cur_inode_new = 0;
  4956. sctx->cur_inode_deleted = 1;
  4957. sctx->cur_inode_size = btrfs_inode_size(
  4958. sctx->right_path->nodes[0], right_ii);
  4959. sctx->cur_inode_mode = btrfs_inode_mode(
  4960. sctx->right_path->nodes[0], right_ii);
  4961. } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
  4962. /*
  4963. * We need to do some special handling in case the inode was
  4964. * reported as changed with a changed generation number. This
  4965. * means that the original inode was deleted and new inode
  4966. * reused the same inum. So we have to treat the old inode as
  4967. * deleted and the new one as new.
  4968. */
  4969. if (sctx->cur_inode_new_gen) {
  4970. /*
  4971. * First, process the inode as if it was deleted.
  4972. */
  4973. sctx->cur_inode_gen = right_gen;
  4974. sctx->cur_inode_new = 0;
  4975. sctx->cur_inode_deleted = 1;
  4976. sctx->cur_inode_size = btrfs_inode_size(
  4977. sctx->right_path->nodes[0], right_ii);
  4978. sctx->cur_inode_mode = btrfs_inode_mode(
  4979. sctx->right_path->nodes[0], right_ii);
  4980. ret = process_all_refs(sctx,
  4981. BTRFS_COMPARE_TREE_DELETED);
  4982. if (ret < 0)
  4983. goto out;
  4984. /*
  4985. * Now process the inode as if it was new.
  4986. */
  4987. sctx->cur_inode_gen = left_gen;
  4988. sctx->cur_inode_new = 1;
  4989. sctx->cur_inode_deleted = 0;
  4990. sctx->cur_inode_size = btrfs_inode_size(
  4991. sctx->left_path->nodes[0], left_ii);
  4992. sctx->cur_inode_mode = btrfs_inode_mode(
  4993. sctx->left_path->nodes[0], left_ii);
  4994. sctx->cur_inode_rdev = btrfs_inode_rdev(
  4995. sctx->left_path->nodes[0], left_ii);
  4996. ret = send_create_inode_if_needed(sctx);
  4997. if (ret < 0)
  4998. goto out;
  4999. ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
  5000. if (ret < 0)
  5001. goto out;
  5002. /*
  5003. * Advance send_progress now as we did not get into
  5004. * process_recorded_refs_if_needed in the new_gen case.
  5005. */
  5006. sctx->send_progress = sctx->cur_ino + 1;
  5007. /*
  5008. * Now process all extents and xattrs of the inode as if
  5009. * they were all new.
  5010. */
  5011. ret = process_all_extents(sctx);
  5012. if (ret < 0)
  5013. goto out;
  5014. ret = process_all_new_xattrs(sctx);
  5015. if (ret < 0)
  5016. goto out;
  5017. } else {
  5018. sctx->cur_inode_gen = left_gen;
  5019. sctx->cur_inode_new = 0;
  5020. sctx->cur_inode_new_gen = 0;
  5021. sctx->cur_inode_deleted = 0;
  5022. sctx->cur_inode_size = btrfs_inode_size(
  5023. sctx->left_path->nodes[0], left_ii);
  5024. sctx->cur_inode_mode = btrfs_inode_mode(
  5025. sctx->left_path->nodes[0], left_ii);
  5026. }
  5027. }
  5028. out:
  5029. return ret;
  5030. }
  5031. /*
  5032. * We have to process new refs before deleted refs, but compare_trees gives us
  5033. * the new and deleted refs mixed. To fix this, we record the new/deleted refs
  5034. * first and later process them in process_recorded_refs.
  5035. * For the cur_inode_new_gen case, we skip recording completely because
  5036. * changed_inode did already initiate processing of refs. The reason for this is
  5037. * that in this case, compare_tree actually compares the refs of 2 different
  5038. * inodes. To fix this, process_all_refs is used in changed_inode to handle all
  5039. * refs of the right tree as deleted and all refs of the left tree as new.
  5040. */
  5041. static int changed_ref(struct send_ctx *sctx,
  5042. enum btrfs_compare_tree_result result)
  5043. {
  5044. int ret = 0;
  5045. if (sctx->cur_ino != sctx->cmp_key->objectid) {
  5046. inconsistent_snapshot_error(sctx, result, "reference");
  5047. return -EIO;
  5048. }
  5049. if (!sctx->cur_inode_new_gen &&
  5050. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
  5051. if (result == BTRFS_COMPARE_TREE_NEW)
  5052. ret = record_new_ref(sctx);
  5053. else if (result == BTRFS_COMPARE_TREE_DELETED)
  5054. ret = record_deleted_ref(sctx);
  5055. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  5056. ret = record_changed_ref(sctx);
  5057. }
  5058. return ret;
  5059. }
  5060. /*
  5061. * Process new/deleted/changed xattrs. We skip processing in the
  5062. * cur_inode_new_gen case because changed_inode did already initiate processing
  5063. * of xattrs. The reason is the same as in changed_ref
  5064. */
  5065. static int changed_xattr(struct send_ctx *sctx,
  5066. enum btrfs_compare_tree_result result)
  5067. {
  5068. int ret = 0;
  5069. if (sctx->cur_ino != sctx->cmp_key->objectid) {
  5070. inconsistent_snapshot_error(sctx, result, "xattr");
  5071. return -EIO;
  5072. }
  5073. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  5074. if (result == BTRFS_COMPARE_TREE_NEW)
  5075. ret = process_new_xattr(sctx);
  5076. else if (result == BTRFS_COMPARE_TREE_DELETED)
  5077. ret = process_deleted_xattr(sctx);
  5078. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  5079. ret = process_changed_xattr(sctx);
  5080. }
  5081. return ret;
  5082. }
  5083. /*
  5084. * Process new/deleted/changed extents. We skip processing in the
  5085. * cur_inode_new_gen case because changed_inode did already initiate processing
  5086. * of extents. The reason is the same as in changed_ref
  5087. */
  5088. static int changed_extent(struct send_ctx *sctx,
  5089. enum btrfs_compare_tree_result result)
  5090. {
  5091. int ret = 0;
  5092. /*
  5093. * We have found an extent item that changed without the inode item
  5094. * having changed. This can happen either after relocation (where the
  5095. * disk_bytenr of an extent item is replaced at
  5096. * relocation.c:replace_file_extents()) or after deduplication into a
  5097. * file in both the parent and send snapshots (where an extent item can
  5098. * get modified or replaced with a new one). Note that deduplication
  5099. * updates the inode item, but it only changes the iversion (sequence
  5100. * field in the inode item) of the inode, so if a file is deduplicated
  5101. * the same amount of times in both the parent and send snapshots, its
  5102. * iversion becames the same in both snapshots, whence the inode item is
  5103. * the same on both snapshots.
  5104. */
  5105. if (sctx->cur_ino != sctx->cmp_key->objectid)
  5106. return 0;
  5107. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  5108. if (result != BTRFS_COMPARE_TREE_DELETED)
  5109. ret = process_extent(sctx, sctx->left_path,
  5110. sctx->cmp_key);
  5111. }
  5112. return ret;
  5113. }
  5114. static int dir_changed(struct send_ctx *sctx, u64 dir)
  5115. {
  5116. u64 orig_gen, new_gen;
  5117. int ret;
  5118. ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
  5119. NULL, NULL);
  5120. if (ret)
  5121. return ret;
  5122. ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
  5123. NULL, NULL, NULL);
  5124. if (ret)
  5125. return ret;
  5126. return (orig_gen != new_gen) ? 1 : 0;
  5127. }
  5128. static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
  5129. struct btrfs_key *key)
  5130. {
  5131. struct btrfs_inode_extref *extref;
  5132. struct extent_buffer *leaf;
  5133. u64 dirid = 0, last_dirid = 0;
  5134. unsigned long ptr;
  5135. u32 item_size;
  5136. u32 cur_offset = 0;
  5137. int ref_name_len;
  5138. int ret = 0;
  5139. /* Easy case, just check this one dirid */
  5140. if (key->type == BTRFS_INODE_REF_KEY) {
  5141. dirid = key->offset;
  5142. ret = dir_changed(sctx, dirid);
  5143. goto out;
  5144. }
  5145. leaf = path->nodes[0];
  5146. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  5147. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  5148. while (cur_offset < item_size) {
  5149. extref = (struct btrfs_inode_extref *)(ptr +
  5150. cur_offset);
  5151. dirid = btrfs_inode_extref_parent(leaf, extref);
  5152. ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
  5153. cur_offset += ref_name_len + sizeof(*extref);
  5154. if (dirid == last_dirid)
  5155. continue;
  5156. ret = dir_changed(sctx, dirid);
  5157. if (ret)
  5158. break;
  5159. last_dirid = dirid;
  5160. }
  5161. out:
  5162. return ret;
  5163. }
  5164. /*
  5165. * Updates compare related fields in sctx and simply forwards to the actual
  5166. * changed_xxx functions.
  5167. */
  5168. static int changed_cb(struct btrfs_root *left_root,
  5169. struct btrfs_root *right_root,
  5170. struct btrfs_path *left_path,
  5171. struct btrfs_path *right_path,
  5172. struct btrfs_key *key,
  5173. enum btrfs_compare_tree_result result,
  5174. void *ctx)
  5175. {
  5176. int ret = 0;
  5177. struct send_ctx *sctx = ctx;
  5178. if (result == BTRFS_COMPARE_TREE_SAME) {
  5179. if (key->type == BTRFS_INODE_REF_KEY ||
  5180. key->type == BTRFS_INODE_EXTREF_KEY) {
  5181. ret = compare_refs(sctx, left_path, key);
  5182. if (!ret)
  5183. return 0;
  5184. if (ret < 0)
  5185. return ret;
  5186. } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
  5187. return maybe_send_hole(sctx, left_path, key);
  5188. } else {
  5189. return 0;
  5190. }
  5191. result = BTRFS_COMPARE_TREE_CHANGED;
  5192. ret = 0;
  5193. }
  5194. sctx->left_path = left_path;
  5195. sctx->right_path = right_path;
  5196. sctx->cmp_key = key;
  5197. ret = finish_inode_if_needed(sctx, 0);
  5198. if (ret < 0)
  5199. goto out;
  5200. /* Ignore non-FS objects */
  5201. if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
  5202. key->objectid == BTRFS_FREE_SPACE_OBJECTID)
  5203. goto out;
  5204. if (key->type == BTRFS_INODE_ITEM_KEY)
  5205. ret = changed_inode(sctx, result);
  5206. else if (key->type == BTRFS_INODE_REF_KEY ||
  5207. key->type == BTRFS_INODE_EXTREF_KEY)
  5208. ret = changed_ref(sctx, result);
  5209. else if (key->type == BTRFS_XATTR_ITEM_KEY)
  5210. ret = changed_xattr(sctx, result);
  5211. else if (key->type == BTRFS_EXTENT_DATA_KEY)
  5212. ret = changed_extent(sctx, result);
  5213. out:
  5214. return ret;
  5215. }
  5216. static int full_send_tree(struct send_ctx *sctx)
  5217. {
  5218. int ret;
  5219. struct btrfs_root *send_root = sctx->send_root;
  5220. struct btrfs_key key;
  5221. struct btrfs_key found_key;
  5222. struct btrfs_path *path;
  5223. struct extent_buffer *eb;
  5224. int slot;
  5225. path = alloc_path_for_send();
  5226. if (!path)
  5227. return -ENOMEM;
  5228. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  5229. key.type = BTRFS_INODE_ITEM_KEY;
  5230. key.offset = 0;
  5231. ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
  5232. if (ret < 0)
  5233. goto out;
  5234. if (ret)
  5235. goto out_finish;
  5236. while (1) {
  5237. eb = path->nodes[0];
  5238. slot = path->slots[0];
  5239. btrfs_item_key_to_cpu(eb, &found_key, slot);
  5240. ret = changed_cb(send_root, NULL, path, NULL,
  5241. &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
  5242. if (ret < 0)
  5243. goto out;
  5244. key.objectid = found_key.objectid;
  5245. key.type = found_key.type;
  5246. key.offset = found_key.offset + 1;
  5247. ret = btrfs_next_item(send_root, path);
  5248. if (ret < 0)
  5249. goto out;
  5250. if (ret) {
  5251. ret = 0;
  5252. break;
  5253. }
  5254. }
  5255. out_finish:
  5256. ret = finish_inode_if_needed(sctx, 1);
  5257. out:
  5258. btrfs_free_path(path);
  5259. return ret;
  5260. }
  5261. static int send_subvol(struct send_ctx *sctx)
  5262. {
  5263. int ret;
  5264. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
  5265. ret = send_header(sctx);
  5266. if (ret < 0)
  5267. goto out;
  5268. }
  5269. ret = send_subvol_begin(sctx);
  5270. if (ret < 0)
  5271. goto out;
  5272. if (sctx->parent_root) {
  5273. ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
  5274. changed_cb, sctx);
  5275. if (ret < 0)
  5276. goto out;
  5277. ret = finish_inode_if_needed(sctx, 1);
  5278. if (ret < 0)
  5279. goto out;
  5280. } else {
  5281. ret = full_send_tree(sctx);
  5282. if (ret < 0)
  5283. goto out;
  5284. }
  5285. out:
  5286. free_recorded_refs(sctx);
  5287. return ret;
  5288. }
  5289. /*
  5290. * If orphan cleanup did remove any orphans from a root, it means the tree
  5291. * was modified and therefore the commit root is not the same as the current
  5292. * root anymore. This is a problem, because send uses the commit root and
  5293. * therefore can see inode items that don't exist in the current root anymore,
  5294. * and for example make calls to btrfs_iget, which will do tree lookups based
  5295. * on the current root and not on the commit root. Those lookups will fail,
  5296. * returning a -ESTALE error, and making send fail with that error. So make
  5297. * sure a send does not see any orphans we have just removed, and that it will
  5298. * see the same inodes regardless of whether a transaction commit happened
  5299. * before it started (meaning that the commit root will be the same as the
  5300. * current root) or not.
  5301. */
  5302. static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
  5303. {
  5304. int i;
  5305. struct btrfs_trans_handle *trans = NULL;
  5306. again:
  5307. if (sctx->parent_root &&
  5308. sctx->parent_root->node != sctx->parent_root->commit_root)
  5309. goto commit_trans;
  5310. for (i = 0; i < sctx->clone_roots_cnt; i++)
  5311. if (sctx->clone_roots[i].root->node !=
  5312. sctx->clone_roots[i].root->commit_root)
  5313. goto commit_trans;
  5314. if (trans)
  5315. return btrfs_end_transaction(trans, sctx->send_root);
  5316. return 0;
  5317. commit_trans:
  5318. /* Use any root, all fs roots will get their commit roots updated. */
  5319. if (!trans) {
  5320. trans = btrfs_join_transaction(sctx->send_root);
  5321. if (IS_ERR(trans))
  5322. return PTR_ERR(trans);
  5323. goto again;
  5324. }
  5325. return btrfs_commit_transaction(trans, sctx->send_root);
  5326. }
  5327. static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
  5328. {
  5329. spin_lock(&root->root_item_lock);
  5330. root->send_in_progress--;
  5331. /*
  5332. * Not much left to do, we don't know why it's unbalanced and
  5333. * can't blindly reset it to 0.
  5334. */
  5335. if (root->send_in_progress < 0)
  5336. btrfs_err(root->fs_info,
  5337. "send_in_progres unbalanced %d root %llu",
  5338. root->send_in_progress, root->root_key.objectid);
  5339. spin_unlock(&root->root_item_lock);
  5340. }
  5341. long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
  5342. {
  5343. int ret = 0;
  5344. struct btrfs_root *send_root;
  5345. struct btrfs_root *clone_root;
  5346. struct btrfs_fs_info *fs_info;
  5347. struct btrfs_ioctl_send_args *arg = NULL;
  5348. struct btrfs_key key;
  5349. struct send_ctx *sctx = NULL;
  5350. u32 i;
  5351. u64 *clone_sources_tmp = NULL;
  5352. int clone_sources_to_rollback = 0;
  5353. unsigned alloc_size;
  5354. int sort_clone_roots = 0;
  5355. int index;
  5356. if (!capable(CAP_SYS_ADMIN))
  5357. return -EPERM;
  5358. send_root = BTRFS_I(file_inode(mnt_file))->root;
  5359. fs_info = send_root->fs_info;
  5360. /*
  5361. * The subvolume must remain read-only during send, protect against
  5362. * making it RW. This also protects against deletion.
  5363. */
  5364. spin_lock(&send_root->root_item_lock);
  5365. send_root->send_in_progress++;
  5366. spin_unlock(&send_root->root_item_lock);
  5367. /*
  5368. * This is done when we lookup the root, it should already be complete
  5369. * by the time we get here.
  5370. */
  5371. WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
  5372. /*
  5373. * Userspace tools do the checks and warn the user if it's
  5374. * not RO.
  5375. */
  5376. if (!btrfs_root_readonly(send_root)) {
  5377. ret = -EPERM;
  5378. goto out;
  5379. }
  5380. arg = memdup_user(arg_, sizeof(*arg));
  5381. if (IS_ERR(arg)) {
  5382. ret = PTR_ERR(arg);
  5383. arg = NULL;
  5384. goto out;
  5385. }
  5386. /*
  5387. * Check that we don't overflow at later allocations, we request
  5388. * clone_sources_count + 1 items, and compare to unsigned long inside
  5389. * access_ok.
  5390. */
  5391. if (arg->clone_sources_count >
  5392. ULONG_MAX / sizeof(struct clone_root) - 1) {
  5393. ret = -EINVAL;
  5394. goto out;
  5395. }
  5396. if (!access_ok(VERIFY_READ, arg->clone_sources,
  5397. sizeof(*arg->clone_sources) *
  5398. arg->clone_sources_count)) {
  5399. ret = -EFAULT;
  5400. goto out;
  5401. }
  5402. if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
  5403. ret = -EINVAL;
  5404. goto out;
  5405. }
  5406. sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
  5407. if (!sctx) {
  5408. ret = -ENOMEM;
  5409. goto out;
  5410. }
  5411. INIT_LIST_HEAD(&sctx->new_refs);
  5412. INIT_LIST_HEAD(&sctx->deleted_refs);
  5413. INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
  5414. INIT_LIST_HEAD(&sctx->name_cache_list);
  5415. sctx->flags = arg->flags;
  5416. sctx->send_filp = fget(arg->send_fd);
  5417. if (!sctx->send_filp) {
  5418. ret = -EBADF;
  5419. goto out;
  5420. }
  5421. sctx->send_root = send_root;
  5422. /*
  5423. * Unlikely but possible, if the subvolume is marked for deletion but
  5424. * is slow to remove the directory entry, send can still be started
  5425. */
  5426. if (btrfs_root_dead(sctx->send_root)) {
  5427. ret = -EPERM;
  5428. goto out;
  5429. }
  5430. sctx->clone_roots_cnt = arg->clone_sources_count;
  5431. sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
  5432. sctx->send_buf = kmalloc(sctx->send_max_size, GFP_KERNEL | __GFP_NOWARN);
  5433. if (!sctx->send_buf) {
  5434. sctx->send_buf = vmalloc(sctx->send_max_size);
  5435. if (!sctx->send_buf) {
  5436. ret = -ENOMEM;
  5437. goto out;
  5438. }
  5439. }
  5440. sctx->read_buf = kmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL | __GFP_NOWARN);
  5441. if (!sctx->read_buf) {
  5442. sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
  5443. if (!sctx->read_buf) {
  5444. ret = -ENOMEM;
  5445. goto out;
  5446. }
  5447. }
  5448. sctx->pending_dir_moves = RB_ROOT;
  5449. sctx->waiting_dir_moves = RB_ROOT;
  5450. sctx->orphan_dirs = RB_ROOT;
  5451. alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
  5452. sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL | __GFP_NOWARN);
  5453. if (!sctx->clone_roots) {
  5454. sctx->clone_roots = vzalloc(alloc_size);
  5455. if (!sctx->clone_roots) {
  5456. ret = -ENOMEM;
  5457. goto out;
  5458. }
  5459. }
  5460. alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
  5461. if (arg->clone_sources_count) {
  5462. clone_sources_tmp = kmalloc(alloc_size, GFP_KERNEL | __GFP_NOWARN);
  5463. if (!clone_sources_tmp) {
  5464. clone_sources_tmp = vmalloc(alloc_size);
  5465. if (!clone_sources_tmp) {
  5466. ret = -ENOMEM;
  5467. goto out;
  5468. }
  5469. }
  5470. ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
  5471. alloc_size);
  5472. if (ret) {
  5473. ret = -EFAULT;
  5474. goto out;
  5475. }
  5476. for (i = 0; i < arg->clone_sources_count; i++) {
  5477. key.objectid = clone_sources_tmp[i];
  5478. key.type = BTRFS_ROOT_ITEM_KEY;
  5479. key.offset = (u64)-1;
  5480. index = srcu_read_lock(&fs_info->subvol_srcu);
  5481. clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
  5482. if (IS_ERR(clone_root)) {
  5483. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5484. ret = PTR_ERR(clone_root);
  5485. goto out;
  5486. }
  5487. spin_lock(&clone_root->root_item_lock);
  5488. if (!btrfs_root_readonly(clone_root) ||
  5489. btrfs_root_dead(clone_root)) {
  5490. spin_unlock(&clone_root->root_item_lock);
  5491. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5492. ret = -EPERM;
  5493. goto out;
  5494. }
  5495. clone_root->send_in_progress++;
  5496. spin_unlock(&clone_root->root_item_lock);
  5497. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5498. sctx->clone_roots[i].root = clone_root;
  5499. clone_sources_to_rollback = i + 1;
  5500. }
  5501. kvfree(clone_sources_tmp);
  5502. clone_sources_tmp = NULL;
  5503. }
  5504. if (arg->parent_root) {
  5505. key.objectid = arg->parent_root;
  5506. key.type = BTRFS_ROOT_ITEM_KEY;
  5507. key.offset = (u64)-1;
  5508. index = srcu_read_lock(&fs_info->subvol_srcu);
  5509. sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
  5510. if (IS_ERR(sctx->parent_root)) {
  5511. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5512. ret = PTR_ERR(sctx->parent_root);
  5513. goto out;
  5514. }
  5515. spin_lock(&sctx->parent_root->root_item_lock);
  5516. sctx->parent_root->send_in_progress++;
  5517. if (!btrfs_root_readonly(sctx->parent_root) ||
  5518. btrfs_root_dead(sctx->parent_root)) {
  5519. spin_unlock(&sctx->parent_root->root_item_lock);
  5520. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5521. ret = -EPERM;
  5522. goto out;
  5523. }
  5524. spin_unlock(&sctx->parent_root->root_item_lock);
  5525. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5526. }
  5527. /*
  5528. * Clones from send_root are allowed, but only if the clone source
  5529. * is behind the current send position. This is checked while searching
  5530. * for possible clone sources.
  5531. */
  5532. sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
  5533. /* We do a bsearch later */
  5534. sort(sctx->clone_roots, sctx->clone_roots_cnt,
  5535. sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
  5536. NULL);
  5537. sort_clone_roots = 1;
  5538. ret = ensure_commit_roots_uptodate(sctx);
  5539. if (ret)
  5540. goto out;
  5541. current->journal_info = BTRFS_SEND_TRANS_STUB;
  5542. ret = send_subvol(sctx);
  5543. current->journal_info = NULL;
  5544. if (ret < 0)
  5545. goto out;
  5546. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
  5547. ret = begin_cmd(sctx, BTRFS_SEND_C_END);
  5548. if (ret < 0)
  5549. goto out;
  5550. ret = send_cmd(sctx);
  5551. if (ret < 0)
  5552. goto out;
  5553. }
  5554. out:
  5555. WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
  5556. while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
  5557. struct rb_node *n;
  5558. struct pending_dir_move *pm;
  5559. n = rb_first(&sctx->pending_dir_moves);
  5560. pm = rb_entry(n, struct pending_dir_move, node);
  5561. while (!list_empty(&pm->list)) {
  5562. struct pending_dir_move *pm2;
  5563. pm2 = list_first_entry(&pm->list,
  5564. struct pending_dir_move, list);
  5565. free_pending_move(sctx, pm2);
  5566. }
  5567. free_pending_move(sctx, pm);
  5568. }
  5569. WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
  5570. while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
  5571. struct rb_node *n;
  5572. struct waiting_dir_move *dm;
  5573. n = rb_first(&sctx->waiting_dir_moves);
  5574. dm = rb_entry(n, struct waiting_dir_move, node);
  5575. rb_erase(&dm->node, &sctx->waiting_dir_moves);
  5576. kfree(dm);
  5577. }
  5578. WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
  5579. while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
  5580. struct rb_node *n;
  5581. struct orphan_dir_info *odi;
  5582. n = rb_first(&sctx->orphan_dirs);
  5583. odi = rb_entry(n, struct orphan_dir_info, node);
  5584. free_orphan_dir_info(sctx, odi);
  5585. }
  5586. if (sort_clone_roots) {
  5587. for (i = 0; i < sctx->clone_roots_cnt; i++)
  5588. btrfs_root_dec_send_in_progress(
  5589. sctx->clone_roots[i].root);
  5590. } else {
  5591. for (i = 0; sctx && i < clone_sources_to_rollback; i++)
  5592. btrfs_root_dec_send_in_progress(
  5593. sctx->clone_roots[i].root);
  5594. btrfs_root_dec_send_in_progress(send_root);
  5595. }
  5596. if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
  5597. btrfs_root_dec_send_in_progress(sctx->parent_root);
  5598. kfree(arg);
  5599. kvfree(clone_sources_tmp);
  5600. if (sctx) {
  5601. if (sctx->send_filp)
  5602. fput(sctx->send_filp);
  5603. kvfree(sctx->clone_roots);
  5604. kvfree(sctx->send_buf);
  5605. kvfree(sctx->read_buf);
  5606. name_cache_free(sctx);
  5607. kfree(sctx);
  5608. }
  5609. return ret;
  5610. }