send.c 125 KB

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