send.c 133 KB

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