send.c 184 KB

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