send.c 141 KB

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