send.c 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905
  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/crc32c.h>
  27. #include <linux/vmalloc.h>
  28. #include "send.h"
  29. #include "backref.h"
  30. #include "locking.h"
  31. #include "disk-io.h"
  32. #include "btrfs_inode.h"
  33. #include "transaction.h"
  34. static int g_verbose = 0;
  35. #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
  36. /*
  37. * A fs_path is a helper to dynamically build path names with unknown size.
  38. * It reallocates the internal buffer on demand.
  39. * It allows fast adding of path elements on the right side (normal path) and
  40. * fast adding to the left side (reversed path). A reversed path can also be
  41. * unreversed if needed.
  42. */
  43. struct fs_path {
  44. union {
  45. struct {
  46. char *start;
  47. char *end;
  48. char *prepared;
  49. char *buf;
  50. int buf_len;
  51. unsigned int reversed:1;
  52. unsigned int virtual_mem:1;
  53. char inline_buf[];
  54. };
  55. char pad[PAGE_SIZE];
  56. };
  57. };
  58. #define FS_PATH_INLINE_SIZE \
  59. (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
  60. /* reused for each extent */
  61. struct clone_root {
  62. struct btrfs_root *root;
  63. u64 ino;
  64. u64 offset;
  65. u64 found_refs;
  66. };
  67. #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
  68. #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
  69. struct send_ctx {
  70. struct file *send_filp;
  71. loff_t send_off;
  72. char *send_buf;
  73. u32 send_size;
  74. u32 send_max_size;
  75. u64 total_send_size;
  76. u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
  77. u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
  78. struct vfsmount *mnt;
  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 send_progress;
  99. struct list_head new_refs;
  100. struct list_head deleted_refs;
  101. struct radix_tree_root name_cache;
  102. struct list_head name_cache_list;
  103. int name_cache_size;
  104. struct file *cur_inode_filp;
  105. char *read_buf;
  106. };
  107. struct name_cache_entry {
  108. struct list_head list;
  109. /*
  110. * radix_tree has only 32bit entries but we need to handle 64bit inums.
  111. * We use the lower 32bit of the 64bit inum to store it in the tree. If
  112. * more then one inum would fall into the same entry, we use radix_list
  113. * to store the additional entries. radix_list is also used to store
  114. * entries where two entries have the same inum but different
  115. * generations.
  116. */
  117. struct list_head radix_list;
  118. u64 ino;
  119. u64 gen;
  120. u64 parent_ino;
  121. u64 parent_gen;
  122. int ret;
  123. int need_later_update;
  124. int name_len;
  125. char name[];
  126. };
  127. static void fs_path_reset(struct fs_path *p)
  128. {
  129. if (p->reversed) {
  130. p->start = p->buf + p->buf_len - 1;
  131. p->end = p->start;
  132. *p->start = 0;
  133. } else {
  134. p->start = p->buf;
  135. p->end = p->start;
  136. *p->start = 0;
  137. }
  138. }
  139. static struct fs_path *fs_path_alloc(void)
  140. {
  141. struct fs_path *p;
  142. p = kmalloc(sizeof(*p), GFP_NOFS);
  143. if (!p)
  144. return NULL;
  145. p->reversed = 0;
  146. p->virtual_mem = 0;
  147. p->buf = p->inline_buf;
  148. p->buf_len = FS_PATH_INLINE_SIZE;
  149. fs_path_reset(p);
  150. return p;
  151. }
  152. static struct fs_path *fs_path_alloc_reversed(void)
  153. {
  154. struct fs_path *p;
  155. p = fs_path_alloc();
  156. if (!p)
  157. return NULL;
  158. p->reversed = 1;
  159. fs_path_reset(p);
  160. return p;
  161. }
  162. static void fs_path_free(struct fs_path *p)
  163. {
  164. if (!p)
  165. return;
  166. if (p->buf != p->inline_buf) {
  167. if (p->virtual_mem)
  168. vfree(p->buf);
  169. else
  170. kfree(p->buf);
  171. }
  172. kfree(p);
  173. }
  174. static int fs_path_len(struct fs_path *p)
  175. {
  176. return p->end - p->start;
  177. }
  178. static int fs_path_ensure_buf(struct fs_path *p, int len)
  179. {
  180. char *tmp_buf;
  181. int path_len;
  182. int old_buf_len;
  183. len++;
  184. if (p->buf_len >= len)
  185. return 0;
  186. path_len = p->end - p->start;
  187. old_buf_len = p->buf_len;
  188. len = PAGE_ALIGN(len);
  189. if (p->buf == p->inline_buf) {
  190. tmp_buf = kmalloc(len, GFP_NOFS);
  191. if (!tmp_buf) {
  192. tmp_buf = vmalloc(len);
  193. if (!tmp_buf)
  194. return -ENOMEM;
  195. p->virtual_mem = 1;
  196. }
  197. memcpy(tmp_buf, p->buf, p->buf_len);
  198. p->buf = tmp_buf;
  199. p->buf_len = len;
  200. } else {
  201. if (p->virtual_mem) {
  202. tmp_buf = vmalloc(len);
  203. if (!tmp_buf)
  204. return -ENOMEM;
  205. memcpy(tmp_buf, p->buf, p->buf_len);
  206. vfree(p->buf);
  207. } else {
  208. tmp_buf = krealloc(p->buf, len, GFP_NOFS);
  209. if (!tmp_buf) {
  210. tmp_buf = vmalloc(len);
  211. if (!tmp_buf)
  212. return -ENOMEM;
  213. memcpy(tmp_buf, p->buf, p->buf_len);
  214. kfree(p->buf);
  215. p->virtual_mem = 1;
  216. }
  217. }
  218. p->buf = tmp_buf;
  219. p->buf_len = len;
  220. }
  221. if (p->reversed) {
  222. tmp_buf = p->buf + old_buf_len - path_len - 1;
  223. p->end = p->buf + p->buf_len - 1;
  224. p->start = p->end - path_len;
  225. memmove(p->start, tmp_buf, path_len + 1);
  226. } else {
  227. p->start = p->buf;
  228. p->end = p->start + path_len;
  229. }
  230. return 0;
  231. }
  232. static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
  233. {
  234. int ret;
  235. int new_len;
  236. new_len = p->end - p->start + name_len;
  237. if (p->start != p->end)
  238. new_len++;
  239. ret = fs_path_ensure_buf(p, new_len);
  240. if (ret < 0)
  241. goto out;
  242. if (p->reversed) {
  243. if (p->start != p->end)
  244. *--p->start = '/';
  245. p->start -= name_len;
  246. p->prepared = p->start;
  247. } else {
  248. if (p->start != p->end)
  249. *p->end++ = '/';
  250. p->prepared = p->end;
  251. p->end += name_len;
  252. *p->end = 0;
  253. }
  254. out:
  255. return ret;
  256. }
  257. static int fs_path_add(struct fs_path *p, const char *name, int name_len)
  258. {
  259. int ret;
  260. ret = fs_path_prepare_for_add(p, name_len);
  261. if (ret < 0)
  262. goto out;
  263. memcpy(p->prepared, name, name_len);
  264. p->prepared = NULL;
  265. out:
  266. return ret;
  267. }
  268. static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
  269. {
  270. int ret;
  271. ret = fs_path_prepare_for_add(p, p2->end - p2->start);
  272. if (ret < 0)
  273. goto out;
  274. memcpy(p->prepared, p2->start, p2->end - p2->start);
  275. p->prepared = NULL;
  276. out:
  277. return ret;
  278. }
  279. static int fs_path_add_from_extent_buffer(struct fs_path *p,
  280. struct extent_buffer *eb,
  281. unsigned long off, int len)
  282. {
  283. int ret;
  284. ret = fs_path_prepare_for_add(p, len);
  285. if (ret < 0)
  286. goto out;
  287. read_extent_buffer(eb, p->prepared, off, len);
  288. p->prepared = NULL;
  289. out:
  290. return ret;
  291. }
  292. #if 0
  293. static void fs_path_remove(struct fs_path *p)
  294. {
  295. BUG_ON(p->reversed);
  296. while (p->start != p->end && *p->end != '/')
  297. p->end--;
  298. *p->end = 0;
  299. }
  300. #endif
  301. static int fs_path_copy(struct fs_path *p, struct fs_path *from)
  302. {
  303. int ret;
  304. p->reversed = from->reversed;
  305. fs_path_reset(p);
  306. ret = fs_path_add_path(p, from);
  307. return ret;
  308. }
  309. static void fs_path_unreverse(struct fs_path *p)
  310. {
  311. char *tmp;
  312. int len;
  313. if (!p->reversed)
  314. return;
  315. tmp = p->start;
  316. len = p->end - p->start;
  317. p->start = p->buf;
  318. p->end = p->start + len;
  319. memmove(p->start, tmp, len + 1);
  320. p->reversed = 0;
  321. }
  322. static struct btrfs_path *alloc_path_for_send(void)
  323. {
  324. struct btrfs_path *path;
  325. path = btrfs_alloc_path();
  326. if (!path)
  327. return NULL;
  328. path->search_commit_root = 1;
  329. path->skip_locking = 1;
  330. return path;
  331. }
  332. static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
  333. {
  334. int ret;
  335. mm_segment_t old_fs;
  336. u32 pos = 0;
  337. old_fs = get_fs();
  338. set_fs(KERNEL_DS);
  339. while (pos < len) {
  340. ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
  341. /* TODO handle that correctly */
  342. /*if (ret == -ERESTARTSYS) {
  343. continue;
  344. }*/
  345. if (ret < 0)
  346. goto out;
  347. if (ret == 0) {
  348. ret = -EIO;
  349. goto out;
  350. }
  351. pos += ret;
  352. }
  353. ret = 0;
  354. out:
  355. set_fs(old_fs);
  356. return ret;
  357. }
  358. static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
  359. {
  360. struct btrfs_tlv_header *hdr;
  361. int total_len = sizeof(*hdr) + len;
  362. int left = sctx->send_max_size - sctx->send_size;
  363. if (unlikely(left < total_len))
  364. return -EOVERFLOW;
  365. hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
  366. hdr->tlv_type = cpu_to_le16(attr);
  367. hdr->tlv_len = cpu_to_le16(len);
  368. memcpy(hdr + 1, data, len);
  369. sctx->send_size += total_len;
  370. return 0;
  371. }
  372. #if 0
  373. static int tlv_put_u8(struct send_ctx *sctx, u16 attr, u8 value)
  374. {
  375. return tlv_put(sctx, attr, &value, sizeof(value));
  376. }
  377. static int tlv_put_u16(struct send_ctx *sctx, u16 attr, u16 value)
  378. {
  379. __le16 tmp = cpu_to_le16(value);
  380. return tlv_put(sctx, attr, &tmp, sizeof(tmp));
  381. }
  382. static int tlv_put_u32(struct send_ctx *sctx, u16 attr, u32 value)
  383. {
  384. __le32 tmp = cpu_to_le32(value);
  385. return tlv_put(sctx, attr, &tmp, sizeof(tmp));
  386. }
  387. #endif
  388. static int tlv_put_u64(struct send_ctx *sctx, u16 attr, u64 value)
  389. {
  390. __le64 tmp = cpu_to_le64(value);
  391. return tlv_put(sctx, attr, &tmp, sizeof(tmp));
  392. }
  393. static int tlv_put_string(struct send_ctx *sctx, u16 attr,
  394. const char *str, int len)
  395. {
  396. if (len == -1)
  397. len = strlen(str);
  398. return tlv_put(sctx, attr, str, len);
  399. }
  400. static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
  401. const u8 *uuid)
  402. {
  403. return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
  404. }
  405. #if 0
  406. static int tlv_put_timespec(struct send_ctx *sctx, u16 attr,
  407. struct timespec *ts)
  408. {
  409. struct btrfs_timespec bts;
  410. bts.sec = cpu_to_le64(ts->tv_sec);
  411. bts.nsec = cpu_to_le32(ts->tv_nsec);
  412. return tlv_put(sctx, attr, &bts, sizeof(bts));
  413. }
  414. #endif
  415. static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
  416. struct extent_buffer *eb,
  417. struct btrfs_timespec *ts)
  418. {
  419. struct btrfs_timespec bts;
  420. read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
  421. return tlv_put(sctx, attr, &bts, sizeof(bts));
  422. }
  423. #define TLV_PUT(sctx, attrtype, attrlen, data) \
  424. do { \
  425. ret = tlv_put(sctx, attrtype, attrlen, data); \
  426. if (ret < 0) \
  427. goto tlv_put_failure; \
  428. } while (0)
  429. #define TLV_PUT_INT(sctx, attrtype, bits, value) \
  430. do { \
  431. ret = tlv_put_u##bits(sctx, attrtype, value); \
  432. if (ret < 0) \
  433. goto tlv_put_failure; \
  434. } while (0)
  435. #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
  436. #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
  437. #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
  438. #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
  439. #define TLV_PUT_STRING(sctx, attrtype, str, len) \
  440. do { \
  441. ret = tlv_put_string(sctx, attrtype, str, len); \
  442. if (ret < 0) \
  443. goto tlv_put_failure; \
  444. } while (0)
  445. #define TLV_PUT_PATH(sctx, attrtype, p) \
  446. do { \
  447. ret = tlv_put_string(sctx, attrtype, p->start, \
  448. p->end - p->start); \
  449. if (ret < 0) \
  450. goto tlv_put_failure; \
  451. } while(0)
  452. #define TLV_PUT_UUID(sctx, attrtype, uuid) \
  453. do { \
  454. ret = tlv_put_uuid(sctx, attrtype, uuid); \
  455. if (ret < 0) \
  456. goto tlv_put_failure; \
  457. } while (0)
  458. #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
  459. do { \
  460. ret = tlv_put_timespec(sctx, attrtype, ts); \
  461. if (ret < 0) \
  462. goto tlv_put_failure; \
  463. } while (0)
  464. #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
  465. do { \
  466. ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
  467. if (ret < 0) \
  468. goto tlv_put_failure; \
  469. } while (0)
  470. static int send_header(struct send_ctx *sctx)
  471. {
  472. struct btrfs_stream_header hdr;
  473. strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
  474. hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
  475. return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
  476. &sctx->send_off);
  477. }
  478. /*
  479. * For each command/item we want to send to userspace, we call this function.
  480. */
  481. static int begin_cmd(struct send_ctx *sctx, int cmd)
  482. {
  483. struct btrfs_cmd_header *hdr;
  484. if (!sctx->send_buf) {
  485. WARN_ON(1);
  486. return -EINVAL;
  487. }
  488. BUG_ON(sctx->send_size);
  489. sctx->send_size += sizeof(*hdr);
  490. hdr = (struct btrfs_cmd_header *)sctx->send_buf;
  491. hdr->cmd = cpu_to_le16(cmd);
  492. return 0;
  493. }
  494. static int send_cmd(struct send_ctx *sctx)
  495. {
  496. int ret;
  497. struct btrfs_cmd_header *hdr;
  498. u32 crc;
  499. hdr = (struct btrfs_cmd_header *)sctx->send_buf;
  500. hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
  501. hdr->crc = 0;
  502. crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
  503. hdr->crc = cpu_to_le32(crc);
  504. ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
  505. &sctx->send_off);
  506. sctx->total_send_size += sctx->send_size;
  507. sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
  508. sctx->send_size = 0;
  509. return ret;
  510. }
  511. /*
  512. * Sends a move instruction to user space
  513. */
  514. static int send_rename(struct send_ctx *sctx,
  515. struct fs_path *from, struct fs_path *to)
  516. {
  517. int ret;
  518. verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
  519. ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
  520. if (ret < 0)
  521. goto out;
  522. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
  523. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
  524. ret = send_cmd(sctx);
  525. tlv_put_failure:
  526. out:
  527. return ret;
  528. }
  529. /*
  530. * Sends a link instruction to user space
  531. */
  532. static int send_link(struct send_ctx *sctx,
  533. struct fs_path *path, struct fs_path *lnk)
  534. {
  535. int ret;
  536. verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
  537. ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
  538. if (ret < 0)
  539. goto out;
  540. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  541. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
  542. ret = send_cmd(sctx);
  543. tlv_put_failure:
  544. out:
  545. return ret;
  546. }
  547. /*
  548. * Sends an unlink instruction to user space
  549. */
  550. static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
  551. {
  552. int ret;
  553. verbose_printk("btrfs: send_unlink %s\n", path->start);
  554. ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
  555. if (ret < 0)
  556. goto out;
  557. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  558. ret = send_cmd(sctx);
  559. tlv_put_failure:
  560. out:
  561. return ret;
  562. }
  563. /*
  564. * Sends a rmdir instruction to user space
  565. */
  566. static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
  567. {
  568. int ret;
  569. verbose_printk("btrfs: send_rmdir %s\n", path->start);
  570. ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
  571. if (ret < 0)
  572. goto out;
  573. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  574. ret = send_cmd(sctx);
  575. tlv_put_failure:
  576. out:
  577. return ret;
  578. }
  579. /*
  580. * Helper function to retrieve some fields from an inode item.
  581. */
  582. static int get_inode_info(struct btrfs_root *root,
  583. u64 ino, u64 *size, u64 *gen,
  584. u64 *mode, u64 *uid, u64 *gid,
  585. u64 *rdev)
  586. {
  587. int ret;
  588. struct btrfs_inode_item *ii;
  589. struct btrfs_key key;
  590. struct btrfs_path *path;
  591. path = alloc_path_for_send();
  592. if (!path)
  593. return -ENOMEM;
  594. key.objectid = ino;
  595. key.type = BTRFS_INODE_ITEM_KEY;
  596. key.offset = 0;
  597. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  598. if (ret < 0)
  599. goto out;
  600. if (ret) {
  601. ret = -ENOENT;
  602. goto out;
  603. }
  604. ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
  605. struct btrfs_inode_item);
  606. if (size)
  607. *size = btrfs_inode_size(path->nodes[0], ii);
  608. if (gen)
  609. *gen = btrfs_inode_generation(path->nodes[0], ii);
  610. if (mode)
  611. *mode = btrfs_inode_mode(path->nodes[0], ii);
  612. if (uid)
  613. *uid = btrfs_inode_uid(path->nodes[0], ii);
  614. if (gid)
  615. *gid = btrfs_inode_gid(path->nodes[0], ii);
  616. if (rdev)
  617. *rdev = btrfs_inode_rdev(path->nodes[0], ii);
  618. out:
  619. btrfs_free_path(path);
  620. return ret;
  621. }
  622. typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
  623. struct fs_path *p,
  624. void *ctx);
  625. /*
  626. * Helper function to iterate the entries in ONE btrfs_inode_ref or
  627. * btrfs_inode_extref.
  628. * The iterate callback may return a non zero value to stop iteration. This can
  629. * be a negative value for error codes or 1 to simply stop it.
  630. *
  631. * path must point to the INODE_REF or INODE_EXTREF when called.
  632. */
  633. static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
  634. struct btrfs_key *found_key, int resolve,
  635. iterate_inode_ref_t iterate, void *ctx)
  636. {
  637. struct extent_buffer *eb = path->nodes[0];
  638. struct btrfs_item *item;
  639. struct btrfs_inode_ref *iref;
  640. struct btrfs_inode_extref *extref;
  641. struct btrfs_path *tmp_path;
  642. struct fs_path *p;
  643. u32 cur = 0;
  644. u32 total;
  645. int slot = path->slots[0];
  646. u32 name_len;
  647. char *start;
  648. int ret = 0;
  649. int num = 0;
  650. int index;
  651. u64 dir;
  652. unsigned long name_off;
  653. unsigned long elem_size;
  654. unsigned long ptr;
  655. p = fs_path_alloc_reversed();
  656. if (!p)
  657. return -ENOMEM;
  658. tmp_path = alloc_path_for_send();
  659. if (!tmp_path) {
  660. fs_path_free(p);
  661. return -ENOMEM;
  662. }
  663. if (found_key->type == BTRFS_INODE_REF_KEY) {
  664. ptr = (unsigned long)btrfs_item_ptr(eb, slot,
  665. struct btrfs_inode_ref);
  666. item = btrfs_item_nr(eb, slot);
  667. total = btrfs_item_size(eb, item);
  668. elem_size = sizeof(*iref);
  669. } else {
  670. ptr = btrfs_item_ptr_offset(eb, slot);
  671. total = btrfs_item_size_nr(eb, slot);
  672. elem_size = sizeof(*extref);
  673. }
  674. while (cur < total) {
  675. fs_path_reset(p);
  676. if (found_key->type == BTRFS_INODE_REF_KEY) {
  677. iref = (struct btrfs_inode_ref *)(ptr + cur);
  678. name_len = btrfs_inode_ref_name_len(eb, iref);
  679. name_off = (unsigned long)(iref + 1);
  680. index = btrfs_inode_ref_index(eb, iref);
  681. dir = found_key->offset;
  682. } else {
  683. extref = (struct btrfs_inode_extref *)(ptr + cur);
  684. name_len = btrfs_inode_extref_name_len(eb, extref);
  685. name_off = (unsigned long)&extref->name;
  686. index = btrfs_inode_extref_index(eb, extref);
  687. dir = btrfs_inode_extref_parent(eb, extref);
  688. }
  689. if (resolve) {
  690. start = btrfs_ref_to_path(root, tmp_path, name_len,
  691. name_off, eb, dir,
  692. p->buf, p->buf_len);
  693. if (IS_ERR(start)) {
  694. ret = PTR_ERR(start);
  695. goto out;
  696. }
  697. if (start < p->buf) {
  698. /* overflow , try again with larger buffer */
  699. ret = fs_path_ensure_buf(p,
  700. p->buf_len + p->buf - start);
  701. if (ret < 0)
  702. goto out;
  703. start = btrfs_ref_to_path(root, tmp_path,
  704. name_len, name_off,
  705. eb, dir,
  706. p->buf, p->buf_len);
  707. if (IS_ERR(start)) {
  708. ret = PTR_ERR(start);
  709. goto out;
  710. }
  711. BUG_ON(start < p->buf);
  712. }
  713. p->start = start;
  714. } else {
  715. ret = fs_path_add_from_extent_buffer(p, eb, name_off,
  716. name_len);
  717. if (ret < 0)
  718. goto out;
  719. }
  720. cur += elem_size + name_len;
  721. ret = iterate(num, dir, index, p, ctx);
  722. if (ret)
  723. goto out;
  724. num++;
  725. }
  726. out:
  727. btrfs_free_path(tmp_path);
  728. fs_path_free(p);
  729. return ret;
  730. }
  731. typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
  732. const char *name, int name_len,
  733. const char *data, int data_len,
  734. u8 type, void *ctx);
  735. /*
  736. * Helper function to iterate the entries in ONE btrfs_dir_item.
  737. * The iterate callback may return a non zero value to stop iteration. This can
  738. * be a negative value for error codes or 1 to simply stop it.
  739. *
  740. * path must point to the dir item when called.
  741. */
  742. static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
  743. struct btrfs_key *found_key,
  744. iterate_dir_item_t iterate, void *ctx)
  745. {
  746. int ret = 0;
  747. struct extent_buffer *eb;
  748. struct btrfs_item *item;
  749. struct btrfs_dir_item *di;
  750. struct btrfs_key di_key;
  751. char *buf = NULL;
  752. char *buf2 = NULL;
  753. int buf_len;
  754. int buf_virtual = 0;
  755. u32 name_len;
  756. u32 data_len;
  757. u32 cur;
  758. u32 len;
  759. u32 total;
  760. int slot;
  761. int num;
  762. u8 type;
  763. buf_len = PAGE_SIZE;
  764. buf = kmalloc(buf_len, GFP_NOFS);
  765. if (!buf) {
  766. ret = -ENOMEM;
  767. goto out;
  768. }
  769. eb = path->nodes[0];
  770. slot = path->slots[0];
  771. item = btrfs_item_nr(eb, slot);
  772. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  773. cur = 0;
  774. len = 0;
  775. total = btrfs_item_size(eb, item);
  776. num = 0;
  777. while (cur < total) {
  778. name_len = btrfs_dir_name_len(eb, di);
  779. data_len = btrfs_dir_data_len(eb, di);
  780. type = btrfs_dir_type(eb, di);
  781. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  782. if (name_len + data_len > buf_len) {
  783. buf_len = PAGE_ALIGN(name_len + data_len);
  784. if (buf_virtual) {
  785. buf2 = vmalloc(buf_len);
  786. if (!buf2) {
  787. ret = -ENOMEM;
  788. goto out;
  789. }
  790. vfree(buf);
  791. } else {
  792. buf2 = krealloc(buf, buf_len, GFP_NOFS);
  793. if (!buf2) {
  794. buf2 = vmalloc(buf_len);
  795. if (!buf2) {
  796. ret = -ENOMEM;
  797. goto out;
  798. }
  799. kfree(buf);
  800. buf_virtual = 1;
  801. }
  802. }
  803. buf = buf2;
  804. buf2 = NULL;
  805. }
  806. read_extent_buffer(eb, buf, (unsigned long)(di + 1),
  807. name_len + data_len);
  808. len = sizeof(*di) + name_len + data_len;
  809. di = (struct btrfs_dir_item *)((char *)di + len);
  810. cur += len;
  811. ret = iterate(num, &di_key, buf, name_len, buf + name_len,
  812. data_len, type, ctx);
  813. if (ret < 0)
  814. goto out;
  815. if (ret) {
  816. ret = 0;
  817. goto out;
  818. }
  819. num++;
  820. }
  821. out:
  822. if (buf_virtual)
  823. vfree(buf);
  824. else
  825. kfree(buf);
  826. return ret;
  827. }
  828. static int __copy_first_ref(int num, u64 dir, int index,
  829. struct fs_path *p, void *ctx)
  830. {
  831. int ret;
  832. struct fs_path *pt = ctx;
  833. ret = fs_path_copy(pt, p);
  834. if (ret < 0)
  835. return ret;
  836. /* we want the first only */
  837. return 1;
  838. }
  839. /*
  840. * Retrieve the first path of an inode. If an inode has more then one
  841. * ref/hardlink, this is ignored.
  842. */
  843. static int get_inode_path(struct btrfs_root *root,
  844. u64 ino, struct fs_path *path)
  845. {
  846. int ret;
  847. struct btrfs_key key, found_key;
  848. struct btrfs_path *p;
  849. p = alloc_path_for_send();
  850. if (!p)
  851. return -ENOMEM;
  852. fs_path_reset(path);
  853. key.objectid = ino;
  854. key.type = BTRFS_INODE_REF_KEY;
  855. key.offset = 0;
  856. ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
  857. if (ret < 0)
  858. goto out;
  859. if (ret) {
  860. ret = 1;
  861. goto out;
  862. }
  863. btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
  864. if (found_key.objectid != ino ||
  865. (found_key.type != BTRFS_INODE_REF_KEY &&
  866. found_key.type != BTRFS_INODE_EXTREF_KEY)) {
  867. ret = -ENOENT;
  868. goto out;
  869. }
  870. ret = iterate_inode_ref(root, p, &found_key, 1,
  871. __copy_first_ref, path);
  872. if (ret < 0)
  873. goto out;
  874. ret = 0;
  875. out:
  876. btrfs_free_path(p);
  877. return ret;
  878. }
  879. struct backref_ctx {
  880. struct send_ctx *sctx;
  881. /* number of total found references */
  882. u64 found;
  883. /*
  884. * used for clones found in send_root. clones found behind cur_objectid
  885. * and cur_offset are not considered as allowed clones.
  886. */
  887. u64 cur_objectid;
  888. u64 cur_offset;
  889. /* may be truncated in case it's the last extent in a file */
  890. u64 extent_len;
  891. /* Just to check for bugs in backref resolving */
  892. int found_itself;
  893. };
  894. static int __clone_root_cmp_bsearch(const void *key, const void *elt)
  895. {
  896. u64 root = (u64)(uintptr_t)key;
  897. struct clone_root *cr = (struct clone_root *)elt;
  898. if (root < cr->root->objectid)
  899. return -1;
  900. if (root > cr->root->objectid)
  901. return 1;
  902. return 0;
  903. }
  904. static int __clone_root_cmp_sort(const void *e1, const void *e2)
  905. {
  906. struct clone_root *cr1 = (struct clone_root *)e1;
  907. struct clone_root *cr2 = (struct clone_root *)e2;
  908. if (cr1->root->objectid < cr2->root->objectid)
  909. return -1;
  910. if (cr1->root->objectid > cr2->root->objectid)
  911. return 1;
  912. return 0;
  913. }
  914. /*
  915. * Called for every backref that is found for the current extent.
  916. * Results are collected in sctx->clone_roots->ino/offset/found_refs
  917. */
  918. static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
  919. {
  920. struct backref_ctx *bctx = ctx_;
  921. struct clone_root *found;
  922. int ret;
  923. u64 i_size;
  924. /* First check if the root is in the list of accepted clone sources */
  925. found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
  926. bctx->sctx->clone_roots_cnt,
  927. sizeof(struct clone_root),
  928. __clone_root_cmp_bsearch);
  929. if (!found)
  930. return 0;
  931. if (found->root == bctx->sctx->send_root &&
  932. ino == bctx->cur_objectid &&
  933. offset == bctx->cur_offset) {
  934. bctx->found_itself = 1;
  935. }
  936. /*
  937. * There are inodes that have extents that lie behind its i_size. Don't
  938. * accept clones from these extents.
  939. */
  940. ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
  941. NULL);
  942. if (ret < 0)
  943. return ret;
  944. if (offset + bctx->extent_len > i_size)
  945. return 0;
  946. /*
  947. * Make sure we don't consider clones from send_root that are
  948. * behind the current inode/offset.
  949. */
  950. if (found->root == bctx->sctx->send_root) {
  951. /*
  952. * TODO for the moment we don't accept clones from the inode
  953. * that is currently send. We may change this when
  954. * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
  955. * file.
  956. */
  957. if (ino >= bctx->cur_objectid)
  958. return 0;
  959. #if 0
  960. if (ino > bctx->cur_objectid)
  961. return 0;
  962. if (offset + bctx->extent_len > bctx->cur_offset)
  963. return 0;
  964. #endif
  965. }
  966. bctx->found++;
  967. found->found_refs++;
  968. if (ino < found->ino) {
  969. found->ino = ino;
  970. found->offset = offset;
  971. } else if (found->ino == ino) {
  972. /*
  973. * same extent found more then once in the same file.
  974. */
  975. if (found->offset > offset + bctx->extent_len)
  976. found->offset = offset;
  977. }
  978. return 0;
  979. }
  980. /*
  981. * Given an inode, offset and extent item, it finds a good clone for a clone
  982. * instruction. Returns -ENOENT when none could be found. The function makes
  983. * sure that the returned clone is usable at the point where sending is at the
  984. * moment. This means, that no clones are accepted which lie behind the current
  985. * inode+offset.
  986. *
  987. * path must point to the extent item when called.
  988. */
  989. static int find_extent_clone(struct send_ctx *sctx,
  990. struct btrfs_path *path,
  991. u64 ino, u64 data_offset,
  992. u64 ino_size,
  993. struct clone_root **found)
  994. {
  995. int ret;
  996. int extent_type;
  997. u64 logical;
  998. u64 disk_byte;
  999. u64 num_bytes;
  1000. u64 extent_item_pos;
  1001. u64 flags = 0;
  1002. struct btrfs_file_extent_item *fi;
  1003. struct extent_buffer *eb = path->nodes[0];
  1004. struct backref_ctx *backref_ctx = NULL;
  1005. struct clone_root *cur_clone_root;
  1006. struct btrfs_key found_key;
  1007. struct btrfs_path *tmp_path;
  1008. int compressed;
  1009. u32 i;
  1010. tmp_path = alloc_path_for_send();
  1011. if (!tmp_path)
  1012. return -ENOMEM;
  1013. backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
  1014. if (!backref_ctx) {
  1015. ret = -ENOMEM;
  1016. goto out;
  1017. }
  1018. if (data_offset >= ino_size) {
  1019. /*
  1020. * There may be extents that lie behind the file's size.
  1021. * I at least had this in combination with snapshotting while
  1022. * writing large files.
  1023. */
  1024. ret = 0;
  1025. goto out;
  1026. }
  1027. fi = btrfs_item_ptr(eb, path->slots[0],
  1028. struct btrfs_file_extent_item);
  1029. extent_type = btrfs_file_extent_type(eb, fi);
  1030. if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  1031. ret = -ENOENT;
  1032. goto out;
  1033. }
  1034. compressed = btrfs_file_extent_compression(eb, fi);
  1035. num_bytes = btrfs_file_extent_num_bytes(eb, fi);
  1036. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  1037. if (disk_byte == 0) {
  1038. ret = -ENOENT;
  1039. goto out;
  1040. }
  1041. logical = disk_byte + btrfs_file_extent_offset(eb, fi);
  1042. ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
  1043. &found_key, &flags);
  1044. btrfs_release_path(tmp_path);
  1045. if (ret < 0)
  1046. goto out;
  1047. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1048. ret = -EIO;
  1049. goto out;
  1050. }
  1051. /*
  1052. * Setup the clone roots.
  1053. */
  1054. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  1055. cur_clone_root = sctx->clone_roots + i;
  1056. cur_clone_root->ino = (u64)-1;
  1057. cur_clone_root->offset = 0;
  1058. cur_clone_root->found_refs = 0;
  1059. }
  1060. backref_ctx->sctx = sctx;
  1061. backref_ctx->found = 0;
  1062. backref_ctx->cur_objectid = ino;
  1063. backref_ctx->cur_offset = data_offset;
  1064. backref_ctx->found_itself = 0;
  1065. backref_ctx->extent_len = num_bytes;
  1066. /*
  1067. * The last extent of a file may be too large due to page alignment.
  1068. * We need to adjust extent_len in this case so that the checks in
  1069. * __iterate_backrefs work.
  1070. */
  1071. if (data_offset + num_bytes >= ino_size)
  1072. backref_ctx->extent_len = ino_size - data_offset;
  1073. /*
  1074. * Now collect all backrefs.
  1075. */
  1076. if (compressed == BTRFS_COMPRESS_NONE)
  1077. extent_item_pos = logical - found_key.objectid;
  1078. else
  1079. extent_item_pos = 0;
  1080. extent_item_pos = logical - found_key.objectid;
  1081. ret = iterate_extent_inodes(sctx->send_root->fs_info,
  1082. found_key.objectid, extent_item_pos, 1,
  1083. __iterate_backrefs, backref_ctx);
  1084. if (ret < 0)
  1085. goto out;
  1086. if (!backref_ctx->found_itself) {
  1087. /* found a bug in backref code? */
  1088. ret = -EIO;
  1089. printk(KERN_ERR "btrfs: ERROR did not find backref in "
  1090. "send_root. inode=%llu, offset=%llu, "
  1091. "disk_byte=%llu found extent=%llu\n",
  1092. ino, data_offset, disk_byte, found_key.objectid);
  1093. goto out;
  1094. }
  1095. verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
  1096. "ino=%llu, "
  1097. "num_bytes=%llu, logical=%llu\n",
  1098. data_offset, ino, num_bytes, logical);
  1099. if (!backref_ctx->found)
  1100. verbose_printk("btrfs: no clones found\n");
  1101. cur_clone_root = NULL;
  1102. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  1103. if (sctx->clone_roots[i].found_refs) {
  1104. if (!cur_clone_root)
  1105. cur_clone_root = sctx->clone_roots + i;
  1106. else if (sctx->clone_roots[i].root == sctx->send_root)
  1107. /* prefer clones from send_root over others */
  1108. cur_clone_root = sctx->clone_roots + i;
  1109. }
  1110. }
  1111. if (cur_clone_root) {
  1112. *found = cur_clone_root;
  1113. ret = 0;
  1114. } else {
  1115. ret = -ENOENT;
  1116. }
  1117. out:
  1118. btrfs_free_path(tmp_path);
  1119. kfree(backref_ctx);
  1120. return ret;
  1121. }
  1122. static int read_symlink(struct btrfs_root *root,
  1123. u64 ino,
  1124. struct fs_path *dest)
  1125. {
  1126. int ret;
  1127. struct btrfs_path *path;
  1128. struct btrfs_key key;
  1129. struct btrfs_file_extent_item *ei;
  1130. u8 type;
  1131. u8 compression;
  1132. unsigned long off;
  1133. int len;
  1134. path = alloc_path_for_send();
  1135. if (!path)
  1136. return -ENOMEM;
  1137. key.objectid = ino;
  1138. key.type = BTRFS_EXTENT_DATA_KEY;
  1139. key.offset = 0;
  1140. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1141. if (ret < 0)
  1142. goto out;
  1143. BUG_ON(ret);
  1144. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1145. struct btrfs_file_extent_item);
  1146. type = btrfs_file_extent_type(path->nodes[0], ei);
  1147. compression = btrfs_file_extent_compression(path->nodes[0], ei);
  1148. BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
  1149. BUG_ON(compression);
  1150. off = btrfs_file_extent_inline_start(ei);
  1151. len = btrfs_file_extent_inline_len(path->nodes[0], ei);
  1152. ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
  1153. out:
  1154. btrfs_free_path(path);
  1155. return ret;
  1156. }
  1157. /*
  1158. * Helper function to generate a file name that is unique in the root of
  1159. * send_root and parent_root. This is used to generate names for orphan inodes.
  1160. */
  1161. static int gen_unique_name(struct send_ctx *sctx,
  1162. u64 ino, u64 gen,
  1163. struct fs_path *dest)
  1164. {
  1165. int ret = 0;
  1166. struct btrfs_path *path;
  1167. struct btrfs_dir_item *di;
  1168. char tmp[64];
  1169. int len;
  1170. u64 idx = 0;
  1171. path = alloc_path_for_send();
  1172. if (!path)
  1173. return -ENOMEM;
  1174. while (1) {
  1175. len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
  1176. ino, gen, idx);
  1177. if (len >= sizeof(tmp)) {
  1178. /* should really not happen */
  1179. ret = -EOVERFLOW;
  1180. goto out;
  1181. }
  1182. di = btrfs_lookup_dir_item(NULL, sctx->send_root,
  1183. path, BTRFS_FIRST_FREE_OBJECTID,
  1184. tmp, strlen(tmp), 0);
  1185. btrfs_release_path(path);
  1186. if (IS_ERR(di)) {
  1187. ret = PTR_ERR(di);
  1188. goto out;
  1189. }
  1190. if (di) {
  1191. /* not unique, try again */
  1192. idx++;
  1193. continue;
  1194. }
  1195. if (!sctx->parent_root) {
  1196. /* unique */
  1197. ret = 0;
  1198. break;
  1199. }
  1200. di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
  1201. path, BTRFS_FIRST_FREE_OBJECTID,
  1202. tmp, strlen(tmp), 0);
  1203. btrfs_release_path(path);
  1204. if (IS_ERR(di)) {
  1205. ret = PTR_ERR(di);
  1206. goto out;
  1207. }
  1208. if (di) {
  1209. /* not unique, try again */
  1210. idx++;
  1211. continue;
  1212. }
  1213. /* unique */
  1214. break;
  1215. }
  1216. ret = fs_path_add(dest, tmp, strlen(tmp));
  1217. out:
  1218. btrfs_free_path(path);
  1219. return ret;
  1220. }
  1221. enum inode_state {
  1222. inode_state_no_change,
  1223. inode_state_will_create,
  1224. inode_state_did_create,
  1225. inode_state_will_delete,
  1226. inode_state_did_delete,
  1227. };
  1228. static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
  1229. {
  1230. int ret;
  1231. int left_ret;
  1232. int right_ret;
  1233. u64 left_gen;
  1234. u64 right_gen;
  1235. ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
  1236. NULL, NULL);
  1237. if (ret < 0 && ret != -ENOENT)
  1238. goto out;
  1239. left_ret = ret;
  1240. if (!sctx->parent_root) {
  1241. right_ret = -ENOENT;
  1242. } else {
  1243. ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
  1244. NULL, NULL, NULL, NULL);
  1245. if (ret < 0 && ret != -ENOENT)
  1246. goto out;
  1247. right_ret = ret;
  1248. }
  1249. if (!left_ret && !right_ret) {
  1250. if (left_gen == gen && right_gen == gen) {
  1251. ret = inode_state_no_change;
  1252. } else if (left_gen == gen) {
  1253. if (ino < sctx->send_progress)
  1254. ret = inode_state_did_create;
  1255. else
  1256. ret = inode_state_will_create;
  1257. } else if (right_gen == gen) {
  1258. if (ino < sctx->send_progress)
  1259. ret = inode_state_did_delete;
  1260. else
  1261. ret = inode_state_will_delete;
  1262. } else {
  1263. ret = -ENOENT;
  1264. }
  1265. } else if (!left_ret) {
  1266. if (left_gen == gen) {
  1267. if (ino < sctx->send_progress)
  1268. ret = inode_state_did_create;
  1269. else
  1270. ret = inode_state_will_create;
  1271. } else {
  1272. ret = -ENOENT;
  1273. }
  1274. } else if (!right_ret) {
  1275. if (right_gen == gen) {
  1276. if (ino < sctx->send_progress)
  1277. ret = inode_state_did_delete;
  1278. else
  1279. ret = inode_state_will_delete;
  1280. } else {
  1281. ret = -ENOENT;
  1282. }
  1283. } else {
  1284. ret = -ENOENT;
  1285. }
  1286. out:
  1287. return ret;
  1288. }
  1289. static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
  1290. {
  1291. int ret;
  1292. ret = get_cur_inode_state(sctx, ino, gen);
  1293. if (ret < 0)
  1294. goto out;
  1295. if (ret == inode_state_no_change ||
  1296. ret == inode_state_did_create ||
  1297. ret == inode_state_will_delete)
  1298. ret = 1;
  1299. else
  1300. ret = 0;
  1301. out:
  1302. return ret;
  1303. }
  1304. /*
  1305. * Helper function to lookup a dir item in a dir.
  1306. */
  1307. static int lookup_dir_item_inode(struct btrfs_root *root,
  1308. u64 dir, const char *name, int name_len,
  1309. u64 *found_inode,
  1310. u8 *found_type)
  1311. {
  1312. int ret = 0;
  1313. struct btrfs_dir_item *di;
  1314. struct btrfs_key key;
  1315. struct btrfs_path *path;
  1316. path = alloc_path_for_send();
  1317. if (!path)
  1318. return -ENOMEM;
  1319. di = btrfs_lookup_dir_item(NULL, root, path,
  1320. dir, name, name_len, 0);
  1321. if (!di) {
  1322. ret = -ENOENT;
  1323. goto out;
  1324. }
  1325. if (IS_ERR(di)) {
  1326. ret = PTR_ERR(di);
  1327. goto out;
  1328. }
  1329. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
  1330. *found_inode = key.objectid;
  1331. *found_type = btrfs_dir_type(path->nodes[0], di);
  1332. out:
  1333. btrfs_free_path(path);
  1334. return ret;
  1335. }
  1336. /*
  1337. * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
  1338. * generation of the parent dir and the name of the dir entry.
  1339. */
  1340. static int get_first_ref(struct btrfs_root *root, u64 ino,
  1341. u64 *dir, u64 *dir_gen, struct fs_path *name)
  1342. {
  1343. int ret;
  1344. struct btrfs_key key;
  1345. struct btrfs_key found_key;
  1346. struct btrfs_path *path;
  1347. int len;
  1348. u64 parent_dir;
  1349. path = alloc_path_for_send();
  1350. if (!path)
  1351. return -ENOMEM;
  1352. key.objectid = ino;
  1353. key.type = BTRFS_INODE_REF_KEY;
  1354. key.offset = 0;
  1355. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  1356. if (ret < 0)
  1357. goto out;
  1358. if (!ret)
  1359. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1360. path->slots[0]);
  1361. if (ret || found_key.objectid != ino ||
  1362. (found_key.type != BTRFS_INODE_REF_KEY &&
  1363. found_key.type != BTRFS_INODE_EXTREF_KEY)) {
  1364. ret = -ENOENT;
  1365. goto out;
  1366. }
  1367. if (key.type == BTRFS_INODE_REF_KEY) {
  1368. struct btrfs_inode_ref *iref;
  1369. iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1370. struct btrfs_inode_ref);
  1371. len = btrfs_inode_ref_name_len(path->nodes[0], iref);
  1372. ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
  1373. (unsigned long)(iref + 1),
  1374. len);
  1375. parent_dir = found_key.offset;
  1376. } else {
  1377. struct btrfs_inode_extref *extref;
  1378. extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1379. struct btrfs_inode_extref);
  1380. len = btrfs_inode_extref_name_len(path->nodes[0], extref);
  1381. ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
  1382. (unsigned long)&extref->name, len);
  1383. parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
  1384. }
  1385. if (ret < 0)
  1386. goto out;
  1387. btrfs_release_path(path);
  1388. ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
  1389. NULL, NULL);
  1390. if (ret < 0)
  1391. goto out;
  1392. *dir = parent_dir;
  1393. out:
  1394. btrfs_free_path(path);
  1395. return ret;
  1396. }
  1397. static int is_first_ref(struct btrfs_root *root,
  1398. u64 ino, u64 dir,
  1399. const char *name, int name_len)
  1400. {
  1401. int ret;
  1402. struct fs_path *tmp_name;
  1403. u64 tmp_dir;
  1404. u64 tmp_dir_gen;
  1405. tmp_name = fs_path_alloc();
  1406. if (!tmp_name)
  1407. return -ENOMEM;
  1408. ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
  1409. if (ret < 0)
  1410. goto out;
  1411. if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
  1412. ret = 0;
  1413. goto out;
  1414. }
  1415. ret = !memcmp(tmp_name->start, name, name_len);
  1416. out:
  1417. fs_path_free(tmp_name);
  1418. return ret;
  1419. }
  1420. /*
  1421. * Used by process_recorded_refs to determine if a new ref would overwrite an
  1422. * already existing ref. In case it detects an overwrite, it returns the
  1423. * inode/gen in who_ino/who_gen.
  1424. * When an overwrite is detected, process_recorded_refs does proper orphanizing
  1425. * to make sure later references to the overwritten inode are possible.
  1426. * Orphanizing is however only required for the first ref of an inode.
  1427. * process_recorded_refs does an additional is_first_ref check to see if
  1428. * orphanizing is really required.
  1429. */
  1430. static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
  1431. const char *name, int name_len,
  1432. u64 *who_ino, u64 *who_gen)
  1433. {
  1434. int ret = 0;
  1435. u64 gen;
  1436. u64 other_inode = 0;
  1437. u8 other_type = 0;
  1438. if (!sctx->parent_root)
  1439. goto out;
  1440. ret = is_inode_existent(sctx, dir, dir_gen);
  1441. if (ret <= 0)
  1442. goto out;
  1443. /*
  1444. * If we have a parent root we need to verify that the parent dir was
  1445. * not delted and then re-created, if it was then we have no overwrite
  1446. * and we can just unlink this entry.
  1447. */
  1448. if (sctx->parent_root) {
  1449. ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
  1450. NULL, NULL, NULL);
  1451. if (ret < 0 && ret != -ENOENT)
  1452. goto out;
  1453. if (ret) {
  1454. ret = 0;
  1455. goto out;
  1456. }
  1457. if (gen != dir_gen)
  1458. goto out;
  1459. }
  1460. ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
  1461. &other_inode, &other_type);
  1462. if (ret < 0 && ret != -ENOENT)
  1463. goto out;
  1464. if (ret) {
  1465. ret = 0;
  1466. goto out;
  1467. }
  1468. /*
  1469. * Check if the overwritten ref was already processed. If yes, the ref
  1470. * was already unlinked/moved, so we can safely assume that we will not
  1471. * overwrite anything at this point in time.
  1472. */
  1473. if (other_inode > sctx->send_progress) {
  1474. ret = get_inode_info(sctx->parent_root, other_inode, NULL,
  1475. who_gen, NULL, NULL, NULL, NULL);
  1476. if (ret < 0)
  1477. goto out;
  1478. ret = 1;
  1479. *who_ino = other_inode;
  1480. } else {
  1481. ret = 0;
  1482. }
  1483. out:
  1484. return ret;
  1485. }
  1486. /*
  1487. * Checks if the ref was overwritten by an already processed inode. This is
  1488. * used by __get_cur_name_and_parent to find out if the ref was orphanized and
  1489. * thus the orphan name needs be used.
  1490. * process_recorded_refs also uses it to avoid unlinking of refs that were
  1491. * overwritten.
  1492. */
  1493. static int did_overwrite_ref(struct send_ctx *sctx,
  1494. u64 dir, u64 dir_gen,
  1495. u64 ino, u64 ino_gen,
  1496. const char *name, int name_len)
  1497. {
  1498. int ret = 0;
  1499. u64 gen;
  1500. u64 ow_inode;
  1501. u8 other_type;
  1502. if (!sctx->parent_root)
  1503. goto out;
  1504. ret = is_inode_existent(sctx, dir, dir_gen);
  1505. if (ret <= 0)
  1506. goto out;
  1507. /* check if the ref was overwritten by another ref */
  1508. ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
  1509. &ow_inode, &other_type);
  1510. if (ret < 0 && ret != -ENOENT)
  1511. goto out;
  1512. if (ret) {
  1513. /* was never and will never be overwritten */
  1514. ret = 0;
  1515. goto out;
  1516. }
  1517. ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
  1518. NULL, NULL);
  1519. if (ret < 0)
  1520. goto out;
  1521. if (ow_inode == ino && gen == ino_gen) {
  1522. ret = 0;
  1523. goto out;
  1524. }
  1525. /* we know that it is or will be overwritten. check this now */
  1526. if (ow_inode < sctx->send_progress)
  1527. ret = 1;
  1528. else
  1529. ret = 0;
  1530. out:
  1531. return ret;
  1532. }
  1533. /*
  1534. * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
  1535. * that got overwritten. This is used by process_recorded_refs to determine
  1536. * if it has to use the path as returned by get_cur_path or the orphan name.
  1537. */
  1538. static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
  1539. {
  1540. int ret = 0;
  1541. struct fs_path *name = NULL;
  1542. u64 dir;
  1543. u64 dir_gen;
  1544. if (!sctx->parent_root)
  1545. goto out;
  1546. name = fs_path_alloc();
  1547. if (!name)
  1548. return -ENOMEM;
  1549. ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
  1550. if (ret < 0)
  1551. goto out;
  1552. ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
  1553. name->start, fs_path_len(name));
  1554. out:
  1555. fs_path_free(name);
  1556. return ret;
  1557. }
  1558. /*
  1559. * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
  1560. * so we need to do some special handling in case we have clashes. This function
  1561. * takes care of this with the help of name_cache_entry::radix_list.
  1562. * In case of error, nce is kfreed.
  1563. */
  1564. static int name_cache_insert(struct send_ctx *sctx,
  1565. struct name_cache_entry *nce)
  1566. {
  1567. int ret = 0;
  1568. struct list_head *nce_head;
  1569. nce_head = radix_tree_lookup(&sctx->name_cache,
  1570. (unsigned long)nce->ino);
  1571. if (!nce_head) {
  1572. nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
  1573. if (!nce_head) {
  1574. kfree(nce);
  1575. return -ENOMEM;
  1576. }
  1577. INIT_LIST_HEAD(nce_head);
  1578. ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
  1579. if (ret < 0) {
  1580. kfree(nce_head);
  1581. kfree(nce);
  1582. return ret;
  1583. }
  1584. }
  1585. list_add_tail(&nce->radix_list, nce_head);
  1586. list_add_tail(&nce->list, &sctx->name_cache_list);
  1587. sctx->name_cache_size++;
  1588. return ret;
  1589. }
  1590. static void name_cache_delete(struct send_ctx *sctx,
  1591. struct name_cache_entry *nce)
  1592. {
  1593. struct list_head *nce_head;
  1594. nce_head = radix_tree_lookup(&sctx->name_cache,
  1595. (unsigned long)nce->ino);
  1596. BUG_ON(!nce_head);
  1597. list_del(&nce->radix_list);
  1598. list_del(&nce->list);
  1599. sctx->name_cache_size--;
  1600. if (list_empty(nce_head)) {
  1601. radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
  1602. kfree(nce_head);
  1603. }
  1604. }
  1605. static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
  1606. u64 ino, u64 gen)
  1607. {
  1608. struct list_head *nce_head;
  1609. struct name_cache_entry *cur;
  1610. nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
  1611. if (!nce_head)
  1612. return NULL;
  1613. list_for_each_entry(cur, nce_head, radix_list) {
  1614. if (cur->ino == ino && cur->gen == gen)
  1615. return cur;
  1616. }
  1617. return NULL;
  1618. }
  1619. /*
  1620. * Removes the entry from the list and adds it back to the end. This marks the
  1621. * entry as recently used so that name_cache_clean_unused does not remove it.
  1622. */
  1623. static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
  1624. {
  1625. list_del(&nce->list);
  1626. list_add_tail(&nce->list, &sctx->name_cache_list);
  1627. }
  1628. /*
  1629. * Remove some entries from the beginning of name_cache_list.
  1630. */
  1631. static void name_cache_clean_unused(struct send_ctx *sctx)
  1632. {
  1633. struct name_cache_entry *nce;
  1634. if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
  1635. return;
  1636. while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
  1637. nce = list_entry(sctx->name_cache_list.next,
  1638. struct name_cache_entry, list);
  1639. name_cache_delete(sctx, nce);
  1640. kfree(nce);
  1641. }
  1642. }
  1643. static void name_cache_free(struct send_ctx *sctx)
  1644. {
  1645. struct name_cache_entry *nce;
  1646. while (!list_empty(&sctx->name_cache_list)) {
  1647. nce = list_entry(sctx->name_cache_list.next,
  1648. struct name_cache_entry, list);
  1649. name_cache_delete(sctx, nce);
  1650. kfree(nce);
  1651. }
  1652. }
  1653. /*
  1654. * Used by get_cur_path for each ref up to the root.
  1655. * Returns 0 if it succeeded.
  1656. * Returns 1 if the inode is not existent or got overwritten. In that case, the
  1657. * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
  1658. * is returned, parent_ino/parent_gen are not guaranteed to be valid.
  1659. * Returns <0 in case of error.
  1660. */
  1661. static int __get_cur_name_and_parent(struct send_ctx *sctx,
  1662. u64 ino, u64 gen,
  1663. u64 *parent_ino,
  1664. u64 *parent_gen,
  1665. struct fs_path *dest)
  1666. {
  1667. int ret;
  1668. int nce_ret;
  1669. struct btrfs_path *path = NULL;
  1670. struct name_cache_entry *nce = NULL;
  1671. /*
  1672. * First check if we already did a call to this function with the same
  1673. * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
  1674. * return the cached result.
  1675. */
  1676. nce = name_cache_search(sctx, ino, gen);
  1677. if (nce) {
  1678. if (ino < sctx->send_progress && nce->need_later_update) {
  1679. name_cache_delete(sctx, nce);
  1680. kfree(nce);
  1681. nce = NULL;
  1682. } else {
  1683. name_cache_used(sctx, nce);
  1684. *parent_ino = nce->parent_ino;
  1685. *parent_gen = nce->parent_gen;
  1686. ret = fs_path_add(dest, nce->name, nce->name_len);
  1687. if (ret < 0)
  1688. goto out;
  1689. ret = nce->ret;
  1690. goto out;
  1691. }
  1692. }
  1693. path = alloc_path_for_send();
  1694. if (!path)
  1695. return -ENOMEM;
  1696. /*
  1697. * If the inode is not existent yet, add the orphan name and return 1.
  1698. * This should only happen for the parent dir that we determine in
  1699. * __record_new_ref
  1700. */
  1701. ret = is_inode_existent(sctx, ino, gen);
  1702. if (ret < 0)
  1703. goto out;
  1704. if (!ret) {
  1705. ret = gen_unique_name(sctx, ino, gen, dest);
  1706. if (ret < 0)
  1707. goto out;
  1708. ret = 1;
  1709. goto out_cache;
  1710. }
  1711. /*
  1712. * Depending on whether the inode was already processed or not, use
  1713. * send_root or parent_root for ref lookup.
  1714. */
  1715. if (ino < sctx->send_progress)
  1716. ret = get_first_ref(sctx->send_root, ino,
  1717. parent_ino, parent_gen, dest);
  1718. else
  1719. ret = get_first_ref(sctx->parent_root, ino,
  1720. parent_ino, parent_gen, dest);
  1721. if (ret < 0)
  1722. goto out;
  1723. /*
  1724. * Check if the ref was overwritten by an inode's ref that was processed
  1725. * earlier. If yes, treat as orphan and return 1.
  1726. */
  1727. ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
  1728. dest->start, dest->end - dest->start);
  1729. if (ret < 0)
  1730. goto out;
  1731. if (ret) {
  1732. fs_path_reset(dest);
  1733. ret = gen_unique_name(sctx, ino, gen, dest);
  1734. if (ret < 0)
  1735. goto out;
  1736. ret = 1;
  1737. }
  1738. out_cache:
  1739. /*
  1740. * Store the result of the lookup in the name cache.
  1741. */
  1742. nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
  1743. if (!nce) {
  1744. ret = -ENOMEM;
  1745. goto out;
  1746. }
  1747. nce->ino = ino;
  1748. nce->gen = gen;
  1749. nce->parent_ino = *parent_ino;
  1750. nce->parent_gen = *parent_gen;
  1751. nce->name_len = fs_path_len(dest);
  1752. nce->ret = ret;
  1753. strcpy(nce->name, dest->start);
  1754. if (ino < sctx->send_progress)
  1755. nce->need_later_update = 0;
  1756. else
  1757. nce->need_later_update = 1;
  1758. nce_ret = name_cache_insert(sctx, nce);
  1759. if (nce_ret < 0)
  1760. ret = nce_ret;
  1761. name_cache_clean_unused(sctx);
  1762. out:
  1763. btrfs_free_path(path);
  1764. return ret;
  1765. }
  1766. /*
  1767. * Magic happens here. This function returns the first ref to an inode as it
  1768. * would look like while receiving the stream at this point in time.
  1769. * We walk the path up to the root. For every inode in between, we check if it
  1770. * was already processed/sent. If yes, we continue with the parent as found
  1771. * in send_root. If not, we continue with the parent as found in parent_root.
  1772. * If we encounter an inode that was deleted at this point in time, we use the
  1773. * inodes "orphan" name instead of the real name and stop. Same with new inodes
  1774. * that were not created yet and overwritten inodes/refs.
  1775. *
  1776. * When do we have have orphan inodes:
  1777. * 1. When an inode is freshly created and thus no valid refs are available yet
  1778. * 2. When a directory lost all it's refs (deleted) but still has dir items
  1779. * inside which were not processed yet (pending for move/delete). If anyone
  1780. * tried to get the path to the dir items, it would get a path inside that
  1781. * orphan directory.
  1782. * 3. When an inode is moved around or gets new links, it may overwrite the ref
  1783. * of an unprocessed inode. If in that case the first ref would be
  1784. * overwritten, the overwritten inode gets "orphanized". Later when we
  1785. * process this overwritten inode, it is restored at a new place by moving
  1786. * the orphan inode.
  1787. *
  1788. * sctx->send_progress tells this function at which point in time receiving
  1789. * would be.
  1790. */
  1791. static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
  1792. struct fs_path *dest)
  1793. {
  1794. int ret = 0;
  1795. struct fs_path *name = NULL;
  1796. u64 parent_inode = 0;
  1797. u64 parent_gen = 0;
  1798. int stop = 0;
  1799. name = fs_path_alloc();
  1800. if (!name) {
  1801. ret = -ENOMEM;
  1802. goto out;
  1803. }
  1804. dest->reversed = 1;
  1805. fs_path_reset(dest);
  1806. while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
  1807. fs_path_reset(name);
  1808. ret = __get_cur_name_and_parent(sctx, ino, gen,
  1809. &parent_inode, &parent_gen, name);
  1810. if (ret < 0)
  1811. goto out;
  1812. if (ret)
  1813. stop = 1;
  1814. ret = fs_path_add_path(dest, name);
  1815. if (ret < 0)
  1816. goto out;
  1817. ino = parent_inode;
  1818. gen = parent_gen;
  1819. }
  1820. out:
  1821. fs_path_free(name);
  1822. if (!ret)
  1823. fs_path_unreverse(dest);
  1824. return ret;
  1825. }
  1826. /*
  1827. * Called for regular files when sending extents data. Opens a struct file
  1828. * to read from the file.
  1829. */
  1830. static int open_cur_inode_file(struct send_ctx *sctx)
  1831. {
  1832. int ret = 0;
  1833. struct btrfs_key key;
  1834. struct path path;
  1835. struct inode *inode;
  1836. struct dentry *dentry;
  1837. struct file *filp;
  1838. int new = 0;
  1839. if (sctx->cur_inode_filp)
  1840. goto out;
  1841. key.objectid = sctx->cur_ino;
  1842. key.type = BTRFS_INODE_ITEM_KEY;
  1843. key.offset = 0;
  1844. inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
  1845. &new);
  1846. if (IS_ERR(inode)) {
  1847. ret = PTR_ERR(inode);
  1848. goto out;
  1849. }
  1850. dentry = d_obtain_alias(inode);
  1851. inode = NULL;
  1852. if (IS_ERR(dentry)) {
  1853. ret = PTR_ERR(dentry);
  1854. goto out;
  1855. }
  1856. path.mnt = sctx->mnt;
  1857. path.dentry = dentry;
  1858. filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
  1859. dput(dentry);
  1860. dentry = NULL;
  1861. if (IS_ERR(filp)) {
  1862. ret = PTR_ERR(filp);
  1863. goto out;
  1864. }
  1865. sctx->cur_inode_filp = filp;
  1866. out:
  1867. /*
  1868. * no xxxput required here as every vfs op
  1869. * does it by itself on failure
  1870. */
  1871. return ret;
  1872. }
  1873. /*
  1874. * Closes the struct file that was created in open_cur_inode_file
  1875. */
  1876. static int close_cur_inode_file(struct send_ctx *sctx)
  1877. {
  1878. int ret = 0;
  1879. if (!sctx->cur_inode_filp)
  1880. goto out;
  1881. ret = filp_close(sctx->cur_inode_filp, NULL);
  1882. sctx->cur_inode_filp = NULL;
  1883. out:
  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 = alloc_path_for_send();
  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. 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. 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. char *tmp;
  2234. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  2235. if (!ref)
  2236. return -ENOMEM;
  2237. ref->dir = dir;
  2238. ref->dir_gen = dir_gen;
  2239. ref->full_path = path;
  2240. tmp = strrchr(ref->full_path->start, '/');
  2241. if (!tmp) {
  2242. ref->name_len = ref->full_path->end - ref->full_path->start;
  2243. ref->name = ref->full_path->start;
  2244. ref->dir_path_len = 0;
  2245. ref->dir_path = ref->full_path->start;
  2246. } else {
  2247. tmp++;
  2248. ref->name_len = ref->full_path->end - tmp;
  2249. ref->name = tmp;
  2250. ref->dir_path = ref->full_path->start;
  2251. ref->dir_path_len = ref->full_path->end -
  2252. ref->full_path->start - 1 - ref->name_len;
  2253. }
  2254. list_add_tail(&ref->list, head);
  2255. return 0;
  2256. }
  2257. static int dup_ref(struct recorded_ref *ref, struct list_head *list)
  2258. {
  2259. struct recorded_ref *new;
  2260. new = kmalloc(sizeof(*ref), GFP_NOFS);
  2261. if (!new)
  2262. return -ENOMEM;
  2263. new->dir = ref->dir;
  2264. new->dir_gen = ref->dir_gen;
  2265. new->full_path = NULL;
  2266. INIT_LIST_HEAD(&new->list);
  2267. list_add_tail(&new->list, list);
  2268. return 0;
  2269. }
  2270. static void __free_recorded_refs(struct list_head *head)
  2271. {
  2272. struct recorded_ref *cur;
  2273. while (!list_empty(head)) {
  2274. cur = list_entry(head->next, struct recorded_ref, list);
  2275. fs_path_free(cur->full_path);
  2276. list_del(&cur->list);
  2277. kfree(cur);
  2278. }
  2279. }
  2280. static void free_recorded_refs(struct send_ctx *sctx)
  2281. {
  2282. __free_recorded_refs(&sctx->new_refs);
  2283. __free_recorded_refs(&sctx->deleted_refs);
  2284. }
  2285. /*
  2286. * Renames/moves a file/dir to its orphan name. Used when the first
  2287. * ref of an unprocessed inode gets overwritten and for all non empty
  2288. * directories.
  2289. */
  2290. static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
  2291. struct fs_path *path)
  2292. {
  2293. int ret;
  2294. struct fs_path *orphan;
  2295. orphan = fs_path_alloc();
  2296. if (!orphan)
  2297. return -ENOMEM;
  2298. ret = gen_unique_name(sctx, ino, gen, orphan);
  2299. if (ret < 0)
  2300. goto out;
  2301. ret = send_rename(sctx, path, orphan);
  2302. out:
  2303. fs_path_free(orphan);
  2304. return ret;
  2305. }
  2306. /*
  2307. * Returns 1 if a directory can be removed at this point in time.
  2308. * We check this by iterating all dir items and checking if the inode behind
  2309. * the dir item was already processed.
  2310. */
  2311. static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
  2312. {
  2313. int ret = 0;
  2314. struct btrfs_root *root = sctx->parent_root;
  2315. struct btrfs_path *path;
  2316. struct btrfs_key key;
  2317. struct btrfs_key found_key;
  2318. struct btrfs_key loc;
  2319. struct btrfs_dir_item *di;
  2320. /*
  2321. * Don't try to rmdir the top/root subvolume dir.
  2322. */
  2323. if (dir == BTRFS_FIRST_FREE_OBJECTID)
  2324. return 0;
  2325. path = alloc_path_for_send();
  2326. if (!path)
  2327. return -ENOMEM;
  2328. key.objectid = dir;
  2329. key.type = BTRFS_DIR_INDEX_KEY;
  2330. key.offset = 0;
  2331. while (1) {
  2332. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  2333. if (ret < 0)
  2334. goto out;
  2335. if (!ret) {
  2336. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2337. path->slots[0]);
  2338. }
  2339. if (ret || found_key.objectid != key.objectid ||
  2340. found_key.type != key.type) {
  2341. break;
  2342. }
  2343. di = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2344. struct btrfs_dir_item);
  2345. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
  2346. if (loc.objectid > send_progress) {
  2347. ret = 0;
  2348. goto out;
  2349. }
  2350. btrfs_release_path(path);
  2351. key.offset = found_key.offset + 1;
  2352. }
  2353. ret = 1;
  2354. out:
  2355. btrfs_free_path(path);
  2356. return ret;
  2357. }
  2358. /*
  2359. * This does all the move/link/unlink/rmdir magic.
  2360. */
  2361. static int process_recorded_refs(struct send_ctx *sctx)
  2362. {
  2363. int ret = 0;
  2364. struct recorded_ref *cur;
  2365. struct recorded_ref *cur2;
  2366. struct list_head check_dirs;
  2367. struct fs_path *valid_path = NULL;
  2368. u64 ow_inode = 0;
  2369. u64 ow_gen;
  2370. int did_overwrite = 0;
  2371. int is_orphan = 0;
  2372. verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
  2373. /*
  2374. * This should never happen as the root dir always has the same ref
  2375. * which is always '..'
  2376. */
  2377. BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
  2378. INIT_LIST_HEAD(&check_dirs);
  2379. valid_path = fs_path_alloc();
  2380. if (!valid_path) {
  2381. ret = -ENOMEM;
  2382. goto out;
  2383. }
  2384. /*
  2385. * First, check if the first ref of the current inode was overwritten
  2386. * before. If yes, we know that the current inode was already orphanized
  2387. * and thus use the orphan name. If not, we can use get_cur_path to
  2388. * get the path of the first ref as it would like while receiving at
  2389. * this point in time.
  2390. * New inodes are always orphan at the beginning, so force to use the
  2391. * orphan name in this case.
  2392. * The first ref is stored in valid_path and will be updated if it
  2393. * gets moved around.
  2394. */
  2395. if (!sctx->cur_inode_new) {
  2396. ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
  2397. sctx->cur_inode_gen);
  2398. if (ret < 0)
  2399. goto out;
  2400. if (ret)
  2401. did_overwrite = 1;
  2402. }
  2403. if (sctx->cur_inode_new || did_overwrite) {
  2404. ret = gen_unique_name(sctx, sctx->cur_ino,
  2405. sctx->cur_inode_gen, valid_path);
  2406. if (ret < 0)
  2407. goto out;
  2408. is_orphan = 1;
  2409. } else {
  2410. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  2411. valid_path);
  2412. if (ret < 0)
  2413. goto out;
  2414. }
  2415. list_for_each_entry(cur, &sctx->new_refs, list) {
  2416. /*
  2417. * We may have refs where the parent directory does not exist
  2418. * yet. This happens if the parent directories inum is higher
  2419. * the the current inum. To handle this case, we create the
  2420. * parent directory out of order. But we need to check if this
  2421. * did already happen before due to other refs in the same dir.
  2422. */
  2423. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
  2424. if (ret < 0)
  2425. goto out;
  2426. if (ret == inode_state_will_create) {
  2427. ret = 0;
  2428. /*
  2429. * First check if any of the current inodes refs did
  2430. * already create the dir.
  2431. */
  2432. list_for_each_entry(cur2, &sctx->new_refs, list) {
  2433. if (cur == cur2)
  2434. break;
  2435. if (cur2->dir == cur->dir) {
  2436. ret = 1;
  2437. break;
  2438. }
  2439. }
  2440. /*
  2441. * If that did not happen, check if a previous inode
  2442. * did already create the dir.
  2443. */
  2444. if (!ret)
  2445. ret = did_create_dir(sctx, cur->dir);
  2446. if (ret < 0)
  2447. goto out;
  2448. if (!ret) {
  2449. ret = send_create_inode(sctx, cur->dir);
  2450. if (ret < 0)
  2451. goto out;
  2452. }
  2453. }
  2454. /*
  2455. * Check if this new ref would overwrite the first ref of
  2456. * another unprocessed inode. If yes, orphanize the
  2457. * overwritten inode. If we find an overwritten ref that is
  2458. * not the first ref, simply unlink it.
  2459. */
  2460. ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  2461. cur->name, cur->name_len,
  2462. &ow_inode, &ow_gen);
  2463. if (ret < 0)
  2464. goto out;
  2465. if (ret) {
  2466. ret = is_first_ref(sctx->parent_root,
  2467. ow_inode, cur->dir, cur->name,
  2468. cur->name_len);
  2469. if (ret < 0)
  2470. goto out;
  2471. if (ret) {
  2472. ret = orphanize_inode(sctx, ow_inode, ow_gen,
  2473. cur->full_path);
  2474. if (ret < 0)
  2475. goto out;
  2476. } else {
  2477. ret = send_unlink(sctx, cur->full_path);
  2478. if (ret < 0)
  2479. goto out;
  2480. }
  2481. }
  2482. /*
  2483. * link/move the ref to the new place. If we have an orphan
  2484. * inode, move it and update valid_path. If not, link or move
  2485. * it depending on the inode mode.
  2486. */
  2487. if (is_orphan) {
  2488. ret = send_rename(sctx, valid_path, cur->full_path);
  2489. if (ret < 0)
  2490. goto out;
  2491. is_orphan = 0;
  2492. ret = fs_path_copy(valid_path, cur->full_path);
  2493. if (ret < 0)
  2494. goto out;
  2495. } else {
  2496. if (S_ISDIR(sctx->cur_inode_mode)) {
  2497. /*
  2498. * Dirs can't be linked, so move it. For moved
  2499. * dirs, we always have one new and one deleted
  2500. * ref. The deleted ref is ignored later.
  2501. */
  2502. ret = send_rename(sctx, valid_path,
  2503. cur->full_path);
  2504. if (ret < 0)
  2505. goto out;
  2506. ret = fs_path_copy(valid_path, cur->full_path);
  2507. if (ret < 0)
  2508. goto out;
  2509. } else {
  2510. ret = send_link(sctx, cur->full_path,
  2511. valid_path);
  2512. if (ret < 0)
  2513. goto out;
  2514. }
  2515. }
  2516. ret = dup_ref(cur, &check_dirs);
  2517. if (ret < 0)
  2518. goto out;
  2519. }
  2520. if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
  2521. /*
  2522. * Check if we can already rmdir the directory. If not,
  2523. * orphanize it. For every dir item inside that gets deleted
  2524. * later, we do this check again and rmdir it then if possible.
  2525. * See the use of check_dirs for more details.
  2526. */
  2527. ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
  2528. if (ret < 0)
  2529. goto out;
  2530. if (ret) {
  2531. ret = send_rmdir(sctx, valid_path);
  2532. if (ret < 0)
  2533. goto out;
  2534. } else if (!is_orphan) {
  2535. ret = orphanize_inode(sctx, sctx->cur_ino,
  2536. sctx->cur_inode_gen, valid_path);
  2537. if (ret < 0)
  2538. goto out;
  2539. is_orphan = 1;
  2540. }
  2541. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  2542. ret = dup_ref(cur, &check_dirs);
  2543. if (ret < 0)
  2544. goto out;
  2545. }
  2546. } else if (S_ISDIR(sctx->cur_inode_mode) &&
  2547. !list_empty(&sctx->deleted_refs)) {
  2548. /*
  2549. * We have a moved dir. Add the old parent to check_dirs
  2550. */
  2551. cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
  2552. list);
  2553. ret = dup_ref(cur, &check_dirs);
  2554. if (ret < 0)
  2555. goto out;
  2556. } else if (!S_ISDIR(sctx->cur_inode_mode)) {
  2557. /*
  2558. * We have a non dir inode. Go through all deleted refs and
  2559. * unlink them if they were not already overwritten by other
  2560. * inodes.
  2561. */
  2562. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  2563. ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  2564. sctx->cur_ino, sctx->cur_inode_gen,
  2565. cur->name, cur->name_len);
  2566. if (ret < 0)
  2567. goto out;
  2568. if (!ret) {
  2569. ret = send_unlink(sctx, cur->full_path);
  2570. if (ret < 0)
  2571. goto out;
  2572. }
  2573. ret = dup_ref(cur, &check_dirs);
  2574. if (ret < 0)
  2575. goto out;
  2576. }
  2577. /*
  2578. * If the inode is still orphan, unlink the orphan. This may
  2579. * happen when a previous inode did overwrite the first ref
  2580. * of this inode and no new refs were added for the current
  2581. * inode. Unlinking does not mean that the inode is deleted in
  2582. * all cases. There may still be links to this inode in other
  2583. * places.
  2584. */
  2585. if (is_orphan) {
  2586. ret = send_unlink(sctx, valid_path);
  2587. if (ret < 0)
  2588. goto out;
  2589. }
  2590. }
  2591. /*
  2592. * We did collect all parent dirs where cur_inode was once located. We
  2593. * now go through all these dirs and check if they are pending for
  2594. * deletion and if it's finally possible to perform the rmdir now.
  2595. * We also update the inode stats of the parent dirs here.
  2596. */
  2597. list_for_each_entry(cur, &check_dirs, list) {
  2598. /*
  2599. * In case we had refs into dirs that were not processed yet,
  2600. * we don't need to do the utime and rmdir logic for these dirs.
  2601. * The dir will be processed later.
  2602. */
  2603. if (cur->dir > sctx->cur_ino)
  2604. continue;
  2605. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
  2606. if (ret < 0)
  2607. goto out;
  2608. if (ret == inode_state_did_create ||
  2609. ret == inode_state_no_change) {
  2610. /* TODO delayed utimes */
  2611. ret = send_utimes(sctx, cur->dir, cur->dir_gen);
  2612. if (ret < 0)
  2613. goto out;
  2614. } else if (ret == inode_state_did_delete) {
  2615. ret = can_rmdir(sctx, cur->dir, sctx->cur_ino);
  2616. if (ret < 0)
  2617. goto out;
  2618. if (ret) {
  2619. ret = get_cur_path(sctx, cur->dir,
  2620. cur->dir_gen, valid_path);
  2621. if (ret < 0)
  2622. goto out;
  2623. ret = send_rmdir(sctx, valid_path);
  2624. if (ret < 0)
  2625. goto out;
  2626. }
  2627. }
  2628. }
  2629. ret = 0;
  2630. out:
  2631. __free_recorded_refs(&check_dirs);
  2632. free_recorded_refs(sctx);
  2633. fs_path_free(valid_path);
  2634. return ret;
  2635. }
  2636. static int __record_new_ref(int num, u64 dir, int index,
  2637. struct fs_path *name,
  2638. void *ctx)
  2639. {
  2640. int ret = 0;
  2641. struct send_ctx *sctx = ctx;
  2642. struct fs_path *p;
  2643. u64 gen;
  2644. p = fs_path_alloc();
  2645. if (!p)
  2646. return -ENOMEM;
  2647. ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
  2648. NULL, NULL);
  2649. if (ret < 0)
  2650. goto out;
  2651. ret = get_cur_path(sctx, dir, gen, p);
  2652. if (ret < 0)
  2653. goto out;
  2654. ret = fs_path_add_path(p, name);
  2655. if (ret < 0)
  2656. goto out;
  2657. ret = record_ref(&sctx->new_refs, dir, gen, p);
  2658. out:
  2659. if (ret)
  2660. fs_path_free(p);
  2661. return ret;
  2662. }
  2663. static int __record_deleted_ref(int num, u64 dir, int index,
  2664. struct fs_path *name,
  2665. void *ctx)
  2666. {
  2667. int ret = 0;
  2668. struct send_ctx *sctx = ctx;
  2669. struct fs_path *p;
  2670. u64 gen;
  2671. p = fs_path_alloc();
  2672. if (!p)
  2673. return -ENOMEM;
  2674. ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
  2675. NULL, NULL);
  2676. if (ret < 0)
  2677. goto out;
  2678. ret = get_cur_path(sctx, dir, gen, p);
  2679. if (ret < 0)
  2680. goto out;
  2681. ret = fs_path_add_path(p, name);
  2682. if (ret < 0)
  2683. goto out;
  2684. ret = record_ref(&sctx->deleted_refs, dir, gen, p);
  2685. out:
  2686. if (ret)
  2687. fs_path_free(p);
  2688. return ret;
  2689. }
  2690. static int record_new_ref(struct send_ctx *sctx)
  2691. {
  2692. int ret;
  2693. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  2694. sctx->cmp_key, 0, __record_new_ref, sctx);
  2695. if (ret < 0)
  2696. goto out;
  2697. ret = 0;
  2698. out:
  2699. return ret;
  2700. }
  2701. static int record_deleted_ref(struct send_ctx *sctx)
  2702. {
  2703. int ret;
  2704. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  2705. sctx->cmp_key, 0, __record_deleted_ref, sctx);
  2706. if (ret < 0)
  2707. goto out;
  2708. ret = 0;
  2709. out:
  2710. return ret;
  2711. }
  2712. struct find_ref_ctx {
  2713. u64 dir;
  2714. u64 dir_gen;
  2715. struct btrfs_root *root;
  2716. struct fs_path *name;
  2717. int found_idx;
  2718. };
  2719. static int __find_iref(int num, u64 dir, int index,
  2720. struct fs_path *name,
  2721. void *ctx_)
  2722. {
  2723. struct find_ref_ctx *ctx = ctx_;
  2724. u64 dir_gen;
  2725. int ret;
  2726. if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
  2727. strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
  2728. /*
  2729. * To avoid doing extra lookups we'll only do this if everything
  2730. * else matches.
  2731. */
  2732. ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
  2733. NULL, NULL, NULL);
  2734. if (ret)
  2735. return ret;
  2736. if (dir_gen != ctx->dir_gen)
  2737. return 0;
  2738. ctx->found_idx = num;
  2739. return 1;
  2740. }
  2741. return 0;
  2742. }
  2743. static int find_iref(struct btrfs_root *root,
  2744. struct btrfs_path *path,
  2745. struct btrfs_key *key,
  2746. u64 dir, u64 dir_gen, struct fs_path *name)
  2747. {
  2748. int ret;
  2749. struct find_ref_ctx ctx;
  2750. ctx.dir = dir;
  2751. ctx.name = name;
  2752. ctx.dir_gen = dir_gen;
  2753. ctx.found_idx = -1;
  2754. ctx.root = root;
  2755. ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
  2756. if (ret < 0)
  2757. return ret;
  2758. if (ctx.found_idx == -1)
  2759. return -ENOENT;
  2760. return ctx.found_idx;
  2761. }
  2762. static int __record_changed_new_ref(int num, u64 dir, int index,
  2763. struct fs_path *name,
  2764. void *ctx)
  2765. {
  2766. u64 dir_gen;
  2767. int ret;
  2768. struct send_ctx *sctx = ctx;
  2769. ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
  2770. NULL, NULL, NULL);
  2771. if (ret)
  2772. return ret;
  2773. ret = find_iref(sctx->parent_root, sctx->right_path,
  2774. sctx->cmp_key, dir, dir_gen, name);
  2775. if (ret == -ENOENT)
  2776. ret = __record_new_ref(num, dir, index, name, sctx);
  2777. else if (ret > 0)
  2778. ret = 0;
  2779. return ret;
  2780. }
  2781. static int __record_changed_deleted_ref(int num, u64 dir, int index,
  2782. struct fs_path *name,
  2783. void *ctx)
  2784. {
  2785. u64 dir_gen;
  2786. int ret;
  2787. struct send_ctx *sctx = ctx;
  2788. ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
  2789. NULL, NULL, NULL);
  2790. if (ret)
  2791. return ret;
  2792. ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
  2793. dir, dir_gen, name);
  2794. if (ret == -ENOENT)
  2795. ret = __record_deleted_ref(num, dir, index, name, sctx);
  2796. else if (ret > 0)
  2797. ret = 0;
  2798. return ret;
  2799. }
  2800. static int record_changed_ref(struct send_ctx *sctx)
  2801. {
  2802. int ret = 0;
  2803. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  2804. sctx->cmp_key, 0, __record_changed_new_ref, sctx);
  2805. if (ret < 0)
  2806. goto out;
  2807. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  2808. sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
  2809. if (ret < 0)
  2810. goto out;
  2811. ret = 0;
  2812. out:
  2813. return ret;
  2814. }
  2815. /*
  2816. * Record and process all refs at once. Needed when an inode changes the
  2817. * generation number, which means that it was deleted and recreated.
  2818. */
  2819. static int process_all_refs(struct send_ctx *sctx,
  2820. enum btrfs_compare_tree_result cmd)
  2821. {
  2822. int ret;
  2823. struct btrfs_root *root;
  2824. struct btrfs_path *path;
  2825. struct btrfs_key key;
  2826. struct btrfs_key found_key;
  2827. struct extent_buffer *eb;
  2828. int slot;
  2829. iterate_inode_ref_t cb;
  2830. path = alloc_path_for_send();
  2831. if (!path)
  2832. return -ENOMEM;
  2833. if (cmd == BTRFS_COMPARE_TREE_NEW) {
  2834. root = sctx->send_root;
  2835. cb = __record_new_ref;
  2836. } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
  2837. root = sctx->parent_root;
  2838. cb = __record_deleted_ref;
  2839. } else {
  2840. BUG();
  2841. }
  2842. key.objectid = sctx->cmp_key->objectid;
  2843. key.type = BTRFS_INODE_REF_KEY;
  2844. key.offset = 0;
  2845. while (1) {
  2846. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  2847. if (ret < 0)
  2848. goto out;
  2849. if (ret)
  2850. break;
  2851. eb = path->nodes[0];
  2852. slot = path->slots[0];
  2853. btrfs_item_key_to_cpu(eb, &found_key, slot);
  2854. if (found_key.objectid != key.objectid ||
  2855. (found_key.type != BTRFS_INODE_REF_KEY &&
  2856. found_key.type != BTRFS_INODE_EXTREF_KEY))
  2857. break;
  2858. ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
  2859. btrfs_release_path(path);
  2860. if (ret < 0)
  2861. goto out;
  2862. key.offset = found_key.offset + 1;
  2863. }
  2864. btrfs_release_path(path);
  2865. ret = process_recorded_refs(sctx);
  2866. out:
  2867. btrfs_free_path(path);
  2868. return ret;
  2869. }
  2870. static int send_set_xattr(struct send_ctx *sctx,
  2871. struct fs_path *path,
  2872. const char *name, int name_len,
  2873. const char *data, int data_len)
  2874. {
  2875. int ret = 0;
  2876. ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
  2877. if (ret < 0)
  2878. goto out;
  2879. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  2880. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  2881. TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
  2882. ret = send_cmd(sctx);
  2883. tlv_put_failure:
  2884. out:
  2885. return ret;
  2886. }
  2887. static int send_remove_xattr(struct send_ctx *sctx,
  2888. struct fs_path *path,
  2889. const char *name, int name_len)
  2890. {
  2891. int ret = 0;
  2892. ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
  2893. if (ret < 0)
  2894. goto out;
  2895. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  2896. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  2897. ret = send_cmd(sctx);
  2898. tlv_put_failure:
  2899. out:
  2900. return ret;
  2901. }
  2902. static int __process_new_xattr(int num, struct btrfs_key *di_key,
  2903. const char *name, int name_len,
  2904. const char *data, int data_len,
  2905. u8 type, void *ctx)
  2906. {
  2907. int ret;
  2908. struct send_ctx *sctx = ctx;
  2909. struct fs_path *p;
  2910. posix_acl_xattr_header dummy_acl;
  2911. p = fs_path_alloc();
  2912. if (!p)
  2913. return -ENOMEM;
  2914. /*
  2915. * This hack is needed because empty acl's are stored as zero byte
  2916. * data in xattrs. Problem with that is, that receiving these zero byte
  2917. * acl's will fail later. To fix this, we send a dummy acl list that
  2918. * only contains the version number and no entries.
  2919. */
  2920. if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
  2921. !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
  2922. if (data_len == 0) {
  2923. dummy_acl.a_version =
  2924. cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  2925. data = (char *)&dummy_acl;
  2926. data_len = sizeof(dummy_acl);
  2927. }
  2928. }
  2929. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  2930. if (ret < 0)
  2931. goto out;
  2932. ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
  2933. out:
  2934. fs_path_free(p);
  2935. return ret;
  2936. }
  2937. static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
  2938. const char *name, int name_len,
  2939. const char *data, int data_len,
  2940. u8 type, void *ctx)
  2941. {
  2942. int ret;
  2943. struct send_ctx *sctx = ctx;
  2944. struct fs_path *p;
  2945. p = fs_path_alloc();
  2946. if (!p)
  2947. return -ENOMEM;
  2948. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  2949. if (ret < 0)
  2950. goto out;
  2951. ret = send_remove_xattr(sctx, p, name, name_len);
  2952. out:
  2953. fs_path_free(p);
  2954. return ret;
  2955. }
  2956. static int process_new_xattr(struct send_ctx *sctx)
  2957. {
  2958. int ret = 0;
  2959. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  2960. sctx->cmp_key, __process_new_xattr, sctx);
  2961. return ret;
  2962. }
  2963. static int process_deleted_xattr(struct send_ctx *sctx)
  2964. {
  2965. int ret;
  2966. ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
  2967. sctx->cmp_key, __process_deleted_xattr, sctx);
  2968. return ret;
  2969. }
  2970. struct find_xattr_ctx {
  2971. const char *name;
  2972. int name_len;
  2973. int found_idx;
  2974. char *found_data;
  2975. int found_data_len;
  2976. };
  2977. static int __find_xattr(int num, struct btrfs_key *di_key,
  2978. const char *name, int name_len,
  2979. const char *data, int data_len,
  2980. u8 type, void *vctx)
  2981. {
  2982. struct find_xattr_ctx *ctx = vctx;
  2983. if (name_len == ctx->name_len &&
  2984. strncmp(name, ctx->name, name_len) == 0) {
  2985. ctx->found_idx = num;
  2986. ctx->found_data_len = data_len;
  2987. ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
  2988. if (!ctx->found_data)
  2989. return -ENOMEM;
  2990. return 1;
  2991. }
  2992. return 0;
  2993. }
  2994. static int find_xattr(struct btrfs_root *root,
  2995. struct btrfs_path *path,
  2996. struct btrfs_key *key,
  2997. const char *name, int name_len,
  2998. char **data, int *data_len)
  2999. {
  3000. int ret;
  3001. struct find_xattr_ctx ctx;
  3002. ctx.name = name;
  3003. ctx.name_len = name_len;
  3004. ctx.found_idx = -1;
  3005. ctx.found_data = NULL;
  3006. ctx.found_data_len = 0;
  3007. ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
  3008. if (ret < 0)
  3009. return ret;
  3010. if (ctx.found_idx == -1)
  3011. return -ENOENT;
  3012. if (data) {
  3013. *data = ctx.found_data;
  3014. *data_len = ctx.found_data_len;
  3015. } else {
  3016. kfree(ctx.found_data);
  3017. }
  3018. return ctx.found_idx;
  3019. }
  3020. static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
  3021. const char *name, int name_len,
  3022. const char *data, int data_len,
  3023. u8 type, void *ctx)
  3024. {
  3025. int ret;
  3026. struct send_ctx *sctx = ctx;
  3027. char *found_data = NULL;
  3028. int found_data_len = 0;
  3029. ret = find_xattr(sctx->parent_root, sctx->right_path,
  3030. sctx->cmp_key, name, name_len, &found_data,
  3031. &found_data_len);
  3032. if (ret == -ENOENT) {
  3033. ret = __process_new_xattr(num, di_key, name, name_len, data,
  3034. data_len, type, ctx);
  3035. } else if (ret >= 0) {
  3036. if (data_len != found_data_len ||
  3037. memcmp(data, found_data, data_len)) {
  3038. ret = __process_new_xattr(num, di_key, name, name_len,
  3039. data, data_len, type, ctx);
  3040. } else {
  3041. ret = 0;
  3042. }
  3043. }
  3044. kfree(found_data);
  3045. return ret;
  3046. }
  3047. static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
  3048. const char *name, int name_len,
  3049. const char *data, int data_len,
  3050. u8 type, void *ctx)
  3051. {
  3052. int ret;
  3053. struct send_ctx *sctx = ctx;
  3054. ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
  3055. name, name_len, NULL, NULL);
  3056. if (ret == -ENOENT)
  3057. ret = __process_deleted_xattr(num, di_key, name, name_len, data,
  3058. data_len, type, ctx);
  3059. else if (ret >= 0)
  3060. ret = 0;
  3061. return ret;
  3062. }
  3063. static int process_changed_xattr(struct send_ctx *sctx)
  3064. {
  3065. int ret = 0;
  3066. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  3067. sctx->cmp_key, __process_changed_new_xattr, sctx);
  3068. if (ret < 0)
  3069. goto out;
  3070. ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
  3071. sctx->cmp_key, __process_changed_deleted_xattr, sctx);
  3072. out:
  3073. return ret;
  3074. }
  3075. static int process_all_new_xattrs(struct send_ctx *sctx)
  3076. {
  3077. int ret;
  3078. struct btrfs_root *root;
  3079. struct btrfs_path *path;
  3080. struct btrfs_key key;
  3081. struct btrfs_key found_key;
  3082. struct extent_buffer *eb;
  3083. int slot;
  3084. path = alloc_path_for_send();
  3085. if (!path)
  3086. return -ENOMEM;
  3087. root = sctx->send_root;
  3088. key.objectid = sctx->cmp_key->objectid;
  3089. key.type = BTRFS_XATTR_ITEM_KEY;
  3090. key.offset = 0;
  3091. while (1) {
  3092. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  3093. if (ret < 0)
  3094. goto out;
  3095. if (ret) {
  3096. ret = 0;
  3097. goto out;
  3098. }
  3099. eb = path->nodes[0];
  3100. slot = path->slots[0];
  3101. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3102. if (found_key.objectid != key.objectid ||
  3103. found_key.type != key.type) {
  3104. ret = 0;
  3105. goto out;
  3106. }
  3107. ret = iterate_dir_item(root, path, &found_key,
  3108. __process_new_xattr, sctx);
  3109. if (ret < 0)
  3110. goto out;
  3111. btrfs_release_path(path);
  3112. key.offset = found_key.offset + 1;
  3113. }
  3114. out:
  3115. btrfs_free_path(path);
  3116. return ret;
  3117. }
  3118. /*
  3119. * Read some bytes from the current inode/file and send a write command to
  3120. * user space.
  3121. */
  3122. static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
  3123. {
  3124. int ret = 0;
  3125. struct fs_path *p;
  3126. loff_t pos = offset;
  3127. int num_read = 0;
  3128. mm_segment_t old_fs;
  3129. p = fs_path_alloc();
  3130. if (!p)
  3131. return -ENOMEM;
  3132. /*
  3133. * vfs normally only accepts user space buffers for security reasons.
  3134. * we only read from the file and also only provide the read_buf buffer
  3135. * to vfs. As this buffer does not come from a user space call, it's
  3136. * ok to temporary allow kernel space buffers.
  3137. */
  3138. old_fs = get_fs();
  3139. set_fs(KERNEL_DS);
  3140. verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
  3141. ret = open_cur_inode_file(sctx);
  3142. if (ret < 0)
  3143. goto out;
  3144. ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
  3145. if (ret < 0)
  3146. goto out;
  3147. num_read = ret;
  3148. if (!num_read)
  3149. goto out;
  3150. ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
  3151. if (ret < 0)
  3152. goto out;
  3153. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3154. if (ret < 0)
  3155. goto out;
  3156. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3157. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3158. TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
  3159. ret = send_cmd(sctx);
  3160. tlv_put_failure:
  3161. out:
  3162. fs_path_free(p);
  3163. set_fs(old_fs);
  3164. if (ret < 0)
  3165. return ret;
  3166. return num_read;
  3167. }
  3168. /*
  3169. * Send a clone command to user space.
  3170. */
  3171. static int send_clone(struct send_ctx *sctx,
  3172. u64 offset, u32 len,
  3173. struct clone_root *clone_root)
  3174. {
  3175. int ret = 0;
  3176. struct fs_path *p;
  3177. u64 gen;
  3178. verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
  3179. "clone_inode=%llu, clone_offset=%llu\n", offset, len,
  3180. clone_root->root->objectid, clone_root->ino,
  3181. clone_root->offset);
  3182. p = fs_path_alloc();
  3183. if (!p)
  3184. return -ENOMEM;
  3185. ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
  3186. if (ret < 0)
  3187. goto out;
  3188. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3189. if (ret < 0)
  3190. goto out;
  3191. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3192. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
  3193. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3194. if (clone_root->root == sctx->send_root) {
  3195. ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
  3196. &gen, NULL, NULL, NULL, NULL);
  3197. if (ret < 0)
  3198. goto out;
  3199. ret = get_cur_path(sctx, clone_root->ino, gen, p);
  3200. } else {
  3201. ret = get_inode_path(clone_root->root, clone_root->ino, p);
  3202. }
  3203. if (ret < 0)
  3204. goto out;
  3205. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  3206. clone_root->root->root_item.uuid);
  3207. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  3208. clone_root->root->root_item.ctransid);
  3209. TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
  3210. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
  3211. clone_root->offset);
  3212. ret = send_cmd(sctx);
  3213. tlv_put_failure:
  3214. out:
  3215. fs_path_free(p);
  3216. return ret;
  3217. }
  3218. /*
  3219. * Send an update extent command to user space.
  3220. */
  3221. static int send_update_extent(struct send_ctx *sctx,
  3222. u64 offset, u32 len)
  3223. {
  3224. int ret = 0;
  3225. struct fs_path *p;
  3226. p = fs_path_alloc();
  3227. if (!p)
  3228. return -ENOMEM;
  3229. ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
  3230. if (ret < 0)
  3231. goto out;
  3232. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3233. if (ret < 0)
  3234. goto out;
  3235. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3236. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3237. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
  3238. ret = send_cmd(sctx);
  3239. tlv_put_failure:
  3240. out:
  3241. fs_path_free(p);
  3242. return ret;
  3243. }
  3244. static int send_write_or_clone(struct send_ctx *sctx,
  3245. struct btrfs_path *path,
  3246. struct btrfs_key *key,
  3247. struct clone_root *clone_root)
  3248. {
  3249. int ret = 0;
  3250. struct btrfs_file_extent_item *ei;
  3251. u64 offset = key->offset;
  3252. u64 pos = 0;
  3253. u64 len;
  3254. u32 l;
  3255. u8 type;
  3256. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3257. struct btrfs_file_extent_item);
  3258. type = btrfs_file_extent_type(path->nodes[0], ei);
  3259. if (type == BTRFS_FILE_EXTENT_INLINE) {
  3260. len = btrfs_file_extent_inline_len(path->nodes[0], ei);
  3261. /*
  3262. * it is possible the inline item won't cover the whole page,
  3263. * but there may be items after this page. Make
  3264. * sure to send the whole thing
  3265. */
  3266. len = PAGE_CACHE_ALIGN(len);
  3267. } else {
  3268. len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
  3269. }
  3270. if (offset + len > sctx->cur_inode_size)
  3271. len = sctx->cur_inode_size - offset;
  3272. if (len == 0) {
  3273. ret = 0;
  3274. goto out;
  3275. }
  3276. if (clone_root) {
  3277. ret = send_clone(sctx, offset, len, clone_root);
  3278. } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
  3279. ret = send_update_extent(sctx, offset, len);
  3280. } else {
  3281. while (pos < len) {
  3282. l = len - pos;
  3283. if (l > BTRFS_SEND_READ_SIZE)
  3284. l = BTRFS_SEND_READ_SIZE;
  3285. ret = send_write(sctx, pos + offset, l);
  3286. if (ret < 0)
  3287. goto out;
  3288. if (!ret)
  3289. break;
  3290. pos += ret;
  3291. }
  3292. ret = 0;
  3293. }
  3294. out:
  3295. return ret;
  3296. }
  3297. static int is_extent_unchanged(struct send_ctx *sctx,
  3298. struct btrfs_path *left_path,
  3299. struct btrfs_key *ekey)
  3300. {
  3301. int ret = 0;
  3302. struct btrfs_key key;
  3303. struct btrfs_path *path = NULL;
  3304. struct extent_buffer *eb;
  3305. int slot;
  3306. struct btrfs_key found_key;
  3307. struct btrfs_file_extent_item *ei;
  3308. u64 left_disknr;
  3309. u64 right_disknr;
  3310. u64 left_offset;
  3311. u64 right_offset;
  3312. u64 left_offset_fixed;
  3313. u64 left_len;
  3314. u64 right_len;
  3315. u64 left_gen;
  3316. u64 right_gen;
  3317. u8 left_type;
  3318. u8 right_type;
  3319. path = alloc_path_for_send();
  3320. if (!path)
  3321. return -ENOMEM;
  3322. eb = left_path->nodes[0];
  3323. slot = left_path->slots[0];
  3324. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  3325. left_type = btrfs_file_extent_type(eb, ei);
  3326. if (left_type != BTRFS_FILE_EXTENT_REG) {
  3327. ret = 0;
  3328. goto out;
  3329. }
  3330. left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  3331. left_len = btrfs_file_extent_num_bytes(eb, ei);
  3332. left_offset = btrfs_file_extent_offset(eb, ei);
  3333. left_gen = btrfs_file_extent_generation(eb, ei);
  3334. /*
  3335. * Following comments will refer to these graphics. L is the left
  3336. * extents which we are checking at the moment. 1-8 are the right
  3337. * extents that we iterate.
  3338. *
  3339. * |-----L-----|
  3340. * |-1-|-2a-|-3-|-4-|-5-|-6-|
  3341. *
  3342. * |-----L-----|
  3343. * |--1--|-2b-|...(same as above)
  3344. *
  3345. * Alternative situation. Happens on files where extents got split.
  3346. * |-----L-----|
  3347. * |-----------7-----------|-6-|
  3348. *
  3349. * Alternative situation. Happens on files which got larger.
  3350. * |-----L-----|
  3351. * |-8-|
  3352. * Nothing follows after 8.
  3353. */
  3354. key.objectid = ekey->objectid;
  3355. key.type = BTRFS_EXTENT_DATA_KEY;
  3356. key.offset = ekey->offset;
  3357. ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
  3358. if (ret < 0)
  3359. goto out;
  3360. if (ret) {
  3361. ret = 0;
  3362. goto out;
  3363. }
  3364. /*
  3365. * Handle special case where the right side has no extents at all.
  3366. */
  3367. eb = path->nodes[0];
  3368. slot = path->slots[0];
  3369. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3370. if (found_key.objectid != key.objectid ||
  3371. found_key.type != key.type) {
  3372. /* If we're a hole then just pretend nothing changed */
  3373. ret = (left_disknr) ? 0 : 1;
  3374. goto out;
  3375. }
  3376. /*
  3377. * We're now on 2a, 2b or 7.
  3378. */
  3379. key = found_key;
  3380. while (key.offset < ekey->offset + left_len) {
  3381. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  3382. right_type = btrfs_file_extent_type(eb, ei);
  3383. right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  3384. right_len = btrfs_file_extent_num_bytes(eb, ei);
  3385. right_offset = btrfs_file_extent_offset(eb, ei);
  3386. right_gen = btrfs_file_extent_generation(eb, ei);
  3387. if (right_type != BTRFS_FILE_EXTENT_REG) {
  3388. ret = 0;
  3389. goto out;
  3390. }
  3391. /*
  3392. * Are we at extent 8? If yes, we know the extent is changed.
  3393. * This may only happen on the first iteration.
  3394. */
  3395. if (found_key.offset + right_len <= ekey->offset) {
  3396. /* If we're a hole just pretend nothing changed */
  3397. ret = (left_disknr) ? 0 : 1;
  3398. goto out;
  3399. }
  3400. left_offset_fixed = left_offset;
  3401. if (key.offset < ekey->offset) {
  3402. /* Fix the right offset for 2a and 7. */
  3403. right_offset += ekey->offset - key.offset;
  3404. } else {
  3405. /* Fix the left offset for all behind 2a and 2b */
  3406. left_offset_fixed += key.offset - ekey->offset;
  3407. }
  3408. /*
  3409. * Check if we have the same extent.
  3410. */
  3411. if (left_disknr != right_disknr ||
  3412. left_offset_fixed != right_offset ||
  3413. left_gen != right_gen) {
  3414. ret = 0;
  3415. goto out;
  3416. }
  3417. /*
  3418. * Go to the next extent.
  3419. */
  3420. ret = btrfs_next_item(sctx->parent_root, path);
  3421. if (ret < 0)
  3422. goto out;
  3423. if (!ret) {
  3424. eb = path->nodes[0];
  3425. slot = path->slots[0];
  3426. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3427. }
  3428. if (ret || found_key.objectid != key.objectid ||
  3429. found_key.type != key.type) {
  3430. key.offset += right_len;
  3431. break;
  3432. }
  3433. if (found_key.offset != key.offset + right_len) {
  3434. ret = 0;
  3435. goto out;
  3436. }
  3437. key = found_key;
  3438. }
  3439. /*
  3440. * We're now behind the left extent (treat as unchanged) or at the end
  3441. * of the right side (treat as changed).
  3442. */
  3443. if (key.offset >= ekey->offset + left_len)
  3444. ret = 1;
  3445. else
  3446. ret = 0;
  3447. out:
  3448. btrfs_free_path(path);
  3449. return ret;
  3450. }
  3451. static int process_extent(struct send_ctx *sctx,
  3452. struct btrfs_path *path,
  3453. struct btrfs_key *key)
  3454. {
  3455. struct clone_root *found_clone = NULL;
  3456. int ret = 0;
  3457. if (S_ISLNK(sctx->cur_inode_mode))
  3458. return 0;
  3459. if (sctx->parent_root && !sctx->cur_inode_new) {
  3460. ret = is_extent_unchanged(sctx, path, key);
  3461. if (ret < 0)
  3462. goto out;
  3463. if (ret) {
  3464. ret = 0;
  3465. goto out;
  3466. }
  3467. } else {
  3468. struct btrfs_file_extent_item *ei;
  3469. u8 type;
  3470. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3471. struct btrfs_file_extent_item);
  3472. type = btrfs_file_extent_type(path->nodes[0], ei);
  3473. if (type == BTRFS_FILE_EXTENT_PREALLOC ||
  3474. type == BTRFS_FILE_EXTENT_REG) {
  3475. /*
  3476. * The send spec does not have a prealloc command yet,
  3477. * so just leave a hole for prealloc'ed extents until
  3478. * we have enough commands queued up to justify rev'ing
  3479. * the send spec.
  3480. */
  3481. if (type == BTRFS_FILE_EXTENT_PREALLOC) {
  3482. ret = 0;
  3483. goto out;
  3484. }
  3485. /* Have a hole, just skip it. */
  3486. if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
  3487. ret = 0;
  3488. goto out;
  3489. }
  3490. }
  3491. }
  3492. ret = find_extent_clone(sctx, path, key->objectid, key->offset,
  3493. sctx->cur_inode_size, &found_clone);
  3494. if (ret != -ENOENT && ret < 0)
  3495. goto out;
  3496. ret = send_write_or_clone(sctx, path, key, found_clone);
  3497. out:
  3498. return ret;
  3499. }
  3500. static int process_all_extents(struct send_ctx *sctx)
  3501. {
  3502. int ret;
  3503. struct btrfs_root *root;
  3504. struct btrfs_path *path;
  3505. struct btrfs_key key;
  3506. struct btrfs_key found_key;
  3507. struct extent_buffer *eb;
  3508. int slot;
  3509. root = sctx->send_root;
  3510. path = alloc_path_for_send();
  3511. if (!path)
  3512. return -ENOMEM;
  3513. key.objectid = sctx->cmp_key->objectid;
  3514. key.type = BTRFS_EXTENT_DATA_KEY;
  3515. key.offset = 0;
  3516. while (1) {
  3517. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  3518. if (ret < 0)
  3519. goto out;
  3520. if (ret) {
  3521. ret = 0;
  3522. goto out;
  3523. }
  3524. eb = path->nodes[0];
  3525. slot = path->slots[0];
  3526. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3527. if (found_key.objectid != key.objectid ||
  3528. found_key.type != key.type) {
  3529. ret = 0;
  3530. goto out;
  3531. }
  3532. ret = process_extent(sctx, path, &found_key);
  3533. if (ret < 0)
  3534. goto out;
  3535. btrfs_release_path(path);
  3536. key.offset = found_key.offset + 1;
  3537. }
  3538. out:
  3539. btrfs_free_path(path);
  3540. return ret;
  3541. }
  3542. static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
  3543. {
  3544. int ret = 0;
  3545. if (sctx->cur_ino == 0)
  3546. goto out;
  3547. if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
  3548. sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
  3549. goto out;
  3550. if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
  3551. goto out;
  3552. ret = process_recorded_refs(sctx);
  3553. if (ret < 0)
  3554. goto out;
  3555. /*
  3556. * We have processed the refs and thus need to advance send_progress.
  3557. * Now, calls to get_cur_xxx will take the updated refs of the current
  3558. * inode into account.
  3559. */
  3560. sctx->send_progress = sctx->cur_ino + 1;
  3561. out:
  3562. return ret;
  3563. }
  3564. static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
  3565. {
  3566. int ret = 0;
  3567. u64 left_mode;
  3568. u64 left_uid;
  3569. u64 left_gid;
  3570. u64 right_mode;
  3571. u64 right_uid;
  3572. u64 right_gid;
  3573. int need_chmod = 0;
  3574. int need_chown = 0;
  3575. ret = process_recorded_refs_if_needed(sctx, at_end);
  3576. if (ret < 0)
  3577. goto out;
  3578. if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
  3579. goto out;
  3580. if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
  3581. goto out;
  3582. ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
  3583. &left_mode, &left_uid, &left_gid, NULL);
  3584. if (ret < 0)
  3585. goto out;
  3586. if (!sctx->parent_root || sctx->cur_inode_new) {
  3587. need_chown = 1;
  3588. if (!S_ISLNK(sctx->cur_inode_mode))
  3589. need_chmod = 1;
  3590. } else {
  3591. ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
  3592. NULL, NULL, &right_mode, &right_uid,
  3593. &right_gid, NULL);
  3594. if (ret < 0)
  3595. goto out;
  3596. if (left_uid != right_uid || left_gid != right_gid)
  3597. need_chown = 1;
  3598. if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
  3599. need_chmod = 1;
  3600. }
  3601. if (S_ISREG(sctx->cur_inode_mode)) {
  3602. ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3603. sctx->cur_inode_size);
  3604. if (ret < 0)
  3605. goto out;
  3606. }
  3607. if (need_chown) {
  3608. ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3609. left_uid, left_gid);
  3610. if (ret < 0)
  3611. goto out;
  3612. }
  3613. if (need_chmod) {
  3614. ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3615. left_mode);
  3616. if (ret < 0)
  3617. goto out;
  3618. }
  3619. /*
  3620. * Need to send that every time, no matter if it actually changed
  3621. * between the two trees as we have done changes to the inode before.
  3622. */
  3623. ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
  3624. if (ret < 0)
  3625. goto out;
  3626. out:
  3627. return ret;
  3628. }
  3629. static int changed_inode(struct send_ctx *sctx,
  3630. enum btrfs_compare_tree_result result)
  3631. {
  3632. int ret = 0;
  3633. struct btrfs_key *key = sctx->cmp_key;
  3634. struct btrfs_inode_item *left_ii = NULL;
  3635. struct btrfs_inode_item *right_ii = NULL;
  3636. u64 left_gen = 0;
  3637. u64 right_gen = 0;
  3638. ret = close_cur_inode_file(sctx);
  3639. if (ret < 0)
  3640. goto out;
  3641. sctx->cur_ino = key->objectid;
  3642. sctx->cur_inode_new_gen = 0;
  3643. /*
  3644. * Set send_progress to current inode. This will tell all get_cur_xxx
  3645. * functions that the current inode's refs are not updated yet. Later,
  3646. * when process_recorded_refs is finished, it is set to cur_ino + 1.
  3647. */
  3648. sctx->send_progress = sctx->cur_ino;
  3649. if (result == BTRFS_COMPARE_TREE_NEW ||
  3650. result == BTRFS_COMPARE_TREE_CHANGED) {
  3651. left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
  3652. sctx->left_path->slots[0],
  3653. struct btrfs_inode_item);
  3654. left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
  3655. left_ii);
  3656. } else {
  3657. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  3658. sctx->right_path->slots[0],
  3659. struct btrfs_inode_item);
  3660. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  3661. right_ii);
  3662. }
  3663. if (result == BTRFS_COMPARE_TREE_CHANGED) {
  3664. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  3665. sctx->right_path->slots[0],
  3666. struct btrfs_inode_item);
  3667. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  3668. right_ii);
  3669. /*
  3670. * The cur_ino = root dir case is special here. We can't treat
  3671. * the inode as deleted+reused because it would generate a
  3672. * stream that tries to delete/mkdir the root dir.
  3673. */
  3674. if (left_gen != right_gen &&
  3675. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  3676. sctx->cur_inode_new_gen = 1;
  3677. }
  3678. if (result == BTRFS_COMPARE_TREE_NEW) {
  3679. sctx->cur_inode_gen = left_gen;
  3680. sctx->cur_inode_new = 1;
  3681. sctx->cur_inode_deleted = 0;
  3682. sctx->cur_inode_size = btrfs_inode_size(
  3683. sctx->left_path->nodes[0], left_ii);
  3684. sctx->cur_inode_mode = btrfs_inode_mode(
  3685. sctx->left_path->nodes[0], left_ii);
  3686. if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  3687. ret = send_create_inode_if_needed(sctx);
  3688. } else if (result == BTRFS_COMPARE_TREE_DELETED) {
  3689. sctx->cur_inode_gen = right_gen;
  3690. sctx->cur_inode_new = 0;
  3691. sctx->cur_inode_deleted = 1;
  3692. sctx->cur_inode_size = btrfs_inode_size(
  3693. sctx->right_path->nodes[0], right_ii);
  3694. sctx->cur_inode_mode = btrfs_inode_mode(
  3695. sctx->right_path->nodes[0], right_ii);
  3696. } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
  3697. /*
  3698. * We need to do some special handling in case the inode was
  3699. * reported as changed with a changed generation number. This
  3700. * means that the original inode was deleted and new inode
  3701. * reused the same inum. So we have to treat the old inode as
  3702. * deleted and the new one as new.
  3703. */
  3704. if (sctx->cur_inode_new_gen) {
  3705. /*
  3706. * First, process the inode as if it was deleted.
  3707. */
  3708. sctx->cur_inode_gen = right_gen;
  3709. sctx->cur_inode_new = 0;
  3710. sctx->cur_inode_deleted = 1;
  3711. sctx->cur_inode_size = btrfs_inode_size(
  3712. sctx->right_path->nodes[0], right_ii);
  3713. sctx->cur_inode_mode = btrfs_inode_mode(
  3714. sctx->right_path->nodes[0], right_ii);
  3715. ret = process_all_refs(sctx,
  3716. BTRFS_COMPARE_TREE_DELETED);
  3717. if (ret < 0)
  3718. goto out;
  3719. /*
  3720. * Now process the inode as if it was new.
  3721. */
  3722. sctx->cur_inode_gen = left_gen;
  3723. sctx->cur_inode_new = 1;
  3724. sctx->cur_inode_deleted = 0;
  3725. sctx->cur_inode_size = btrfs_inode_size(
  3726. sctx->left_path->nodes[0], left_ii);
  3727. sctx->cur_inode_mode = btrfs_inode_mode(
  3728. sctx->left_path->nodes[0], left_ii);
  3729. ret = send_create_inode_if_needed(sctx);
  3730. if (ret < 0)
  3731. goto out;
  3732. ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
  3733. if (ret < 0)
  3734. goto out;
  3735. /*
  3736. * Advance send_progress now as we did not get into
  3737. * process_recorded_refs_if_needed in the new_gen case.
  3738. */
  3739. sctx->send_progress = sctx->cur_ino + 1;
  3740. /*
  3741. * Now process all extents and xattrs of the inode as if
  3742. * they were all new.
  3743. */
  3744. ret = process_all_extents(sctx);
  3745. if (ret < 0)
  3746. goto out;
  3747. ret = process_all_new_xattrs(sctx);
  3748. if (ret < 0)
  3749. goto out;
  3750. } else {
  3751. sctx->cur_inode_gen = left_gen;
  3752. sctx->cur_inode_new = 0;
  3753. sctx->cur_inode_new_gen = 0;
  3754. sctx->cur_inode_deleted = 0;
  3755. sctx->cur_inode_size = btrfs_inode_size(
  3756. sctx->left_path->nodes[0], left_ii);
  3757. sctx->cur_inode_mode = btrfs_inode_mode(
  3758. sctx->left_path->nodes[0], left_ii);
  3759. }
  3760. }
  3761. out:
  3762. return ret;
  3763. }
  3764. /*
  3765. * We have to process new refs before deleted refs, but compare_trees gives us
  3766. * the new and deleted refs mixed. To fix this, we record the new/deleted refs
  3767. * first and later process them in process_recorded_refs.
  3768. * For the cur_inode_new_gen case, we skip recording completely because
  3769. * changed_inode did already initiate processing of refs. The reason for this is
  3770. * that in this case, compare_tree actually compares the refs of 2 different
  3771. * inodes. To fix this, process_all_refs is used in changed_inode to handle all
  3772. * refs of the right tree as deleted and all refs of the left tree as new.
  3773. */
  3774. static int changed_ref(struct send_ctx *sctx,
  3775. enum btrfs_compare_tree_result result)
  3776. {
  3777. int ret = 0;
  3778. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  3779. if (!sctx->cur_inode_new_gen &&
  3780. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
  3781. if (result == BTRFS_COMPARE_TREE_NEW)
  3782. ret = record_new_ref(sctx);
  3783. else if (result == BTRFS_COMPARE_TREE_DELETED)
  3784. ret = record_deleted_ref(sctx);
  3785. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  3786. ret = record_changed_ref(sctx);
  3787. }
  3788. return ret;
  3789. }
  3790. /*
  3791. * Process new/deleted/changed xattrs. We skip processing in the
  3792. * cur_inode_new_gen case because changed_inode did already initiate processing
  3793. * of xattrs. The reason is the same as in changed_ref
  3794. */
  3795. static int changed_xattr(struct send_ctx *sctx,
  3796. enum btrfs_compare_tree_result result)
  3797. {
  3798. int ret = 0;
  3799. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  3800. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  3801. if (result == BTRFS_COMPARE_TREE_NEW)
  3802. ret = process_new_xattr(sctx);
  3803. else if (result == BTRFS_COMPARE_TREE_DELETED)
  3804. ret = process_deleted_xattr(sctx);
  3805. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  3806. ret = process_changed_xattr(sctx);
  3807. }
  3808. return ret;
  3809. }
  3810. /*
  3811. * Process new/deleted/changed extents. We skip processing in the
  3812. * cur_inode_new_gen case because changed_inode did already initiate processing
  3813. * of extents. The reason is the same as in changed_ref
  3814. */
  3815. static int changed_extent(struct send_ctx *sctx,
  3816. enum btrfs_compare_tree_result result)
  3817. {
  3818. int ret = 0;
  3819. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  3820. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  3821. if (result != BTRFS_COMPARE_TREE_DELETED)
  3822. ret = process_extent(sctx, sctx->left_path,
  3823. sctx->cmp_key);
  3824. }
  3825. return ret;
  3826. }
  3827. static int dir_changed(struct send_ctx *sctx, u64 dir)
  3828. {
  3829. u64 orig_gen, new_gen;
  3830. int ret;
  3831. ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
  3832. NULL, NULL);
  3833. if (ret)
  3834. return ret;
  3835. ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
  3836. NULL, NULL, NULL);
  3837. if (ret)
  3838. return ret;
  3839. return (orig_gen != new_gen) ? 1 : 0;
  3840. }
  3841. static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
  3842. struct btrfs_key *key)
  3843. {
  3844. struct btrfs_inode_extref *extref;
  3845. struct extent_buffer *leaf;
  3846. u64 dirid = 0, last_dirid = 0;
  3847. unsigned long ptr;
  3848. u32 item_size;
  3849. u32 cur_offset = 0;
  3850. int ref_name_len;
  3851. int ret = 0;
  3852. /* Easy case, just check this one dirid */
  3853. if (key->type == BTRFS_INODE_REF_KEY) {
  3854. dirid = key->offset;
  3855. ret = dir_changed(sctx, dirid);
  3856. goto out;
  3857. }
  3858. leaf = path->nodes[0];
  3859. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3860. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  3861. while (cur_offset < item_size) {
  3862. extref = (struct btrfs_inode_extref *)(ptr +
  3863. cur_offset);
  3864. dirid = btrfs_inode_extref_parent(leaf, extref);
  3865. ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
  3866. cur_offset += ref_name_len + sizeof(*extref);
  3867. if (dirid == last_dirid)
  3868. continue;
  3869. ret = dir_changed(sctx, dirid);
  3870. if (ret)
  3871. break;
  3872. last_dirid = dirid;
  3873. }
  3874. out:
  3875. return ret;
  3876. }
  3877. /*
  3878. * Updates compare related fields in sctx and simply forwards to the actual
  3879. * changed_xxx functions.
  3880. */
  3881. static int changed_cb(struct btrfs_root *left_root,
  3882. struct btrfs_root *right_root,
  3883. struct btrfs_path *left_path,
  3884. struct btrfs_path *right_path,
  3885. struct btrfs_key *key,
  3886. enum btrfs_compare_tree_result result,
  3887. void *ctx)
  3888. {
  3889. int ret = 0;
  3890. struct send_ctx *sctx = ctx;
  3891. if (result == BTRFS_COMPARE_TREE_SAME) {
  3892. if (key->type != BTRFS_INODE_REF_KEY &&
  3893. key->type != BTRFS_INODE_EXTREF_KEY)
  3894. return 0;
  3895. ret = compare_refs(sctx, left_path, key);
  3896. if (!ret)
  3897. return 0;
  3898. if (ret < 0)
  3899. return ret;
  3900. result = BTRFS_COMPARE_TREE_CHANGED;
  3901. ret = 0;
  3902. }
  3903. sctx->left_path = left_path;
  3904. sctx->right_path = right_path;
  3905. sctx->cmp_key = key;
  3906. ret = finish_inode_if_needed(sctx, 0);
  3907. if (ret < 0)
  3908. goto out;
  3909. /* Ignore non-FS objects */
  3910. if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
  3911. key->objectid == BTRFS_FREE_SPACE_OBJECTID)
  3912. goto out;
  3913. if (key->type == BTRFS_INODE_ITEM_KEY)
  3914. ret = changed_inode(sctx, result);
  3915. else if (key->type == BTRFS_INODE_REF_KEY ||
  3916. key->type == BTRFS_INODE_EXTREF_KEY)
  3917. ret = changed_ref(sctx, result);
  3918. else if (key->type == BTRFS_XATTR_ITEM_KEY)
  3919. ret = changed_xattr(sctx, result);
  3920. else if (key->type == BTRFS_EXTENT_DATA_KEY)
  3921. ret = changed_extent(sctx, result);
  3922. out:
  3923. return ret;
  3924. }
  3925. static int full_send_tree(struct send_ctx *sctx)
  3926. {
  3927. int ret;
  3928. struct btrfs_trans_handle *trans = NULL;
  3929. struct btrfs_root *send_root = sctx->send_root;
  3930. struct btrfs_key key;
  3931. struct btrfs_key found_key;
  3932. struct btrfs_path *path;
  3933. struct extent_buffer *eb;
  3934. int slot;
  3935. u64 start_ctransid;
  3936. u64 ctransid;
  3937. path = alloc_path_for_send();
  3938. if (!path)
  3939. return -ENOMEM;
  3940. spin_lock(&send_root->root_item_lock);
  3941. start_ctransid = btrfs_root_ctransid(&send_root->root_item);
  3942. spin_unlock(&send_root->root_item_lock);
  3943. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  3944. key.type = BTRFS_INODE_ITEM_KEY;
  3945. key.offset = 0;
  3946. join_trans:
  3947. /*
  3948. * We need to make sure the transaction does not get committed
  3949. * while we do anything on commit roots. Join a transaction to prevent
  3950. * this.
  3951. */
  3952. trans = btrfs_join_transaction(send_root);
  3953. if (IS_ERR(trans)) {
  3954. ret = PTR_ERR(trans);
  3955. trans = NULL;
  3956. goto out;
  3957. }
  3958. /*
  3959. * Make sure the tree has not changed after re-joining. We detect this
  3960. * by comparing start_ctransid and ctransid. They should always match.
  3961. */
  3962. spin_lock(&send_root->root_item_lock);
  3963. ctransid = btrfs_root_ctransid(&send_root->root_item);
  3964. spin_unlock(&send_root->root_item_lock);
  3965. if (ctransid != start_ctransid) {
  3966. WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
  3967. "send was modified in between. This is "
  3968. "probably a bug.\n");
  3969. ret = -EIO;
  3970. goto out;
  3971. }
  3972. ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
  3973. if (ret < 0)
  3974. goto out;
  3975. if (ret)
  3976. goto out_finish;
  3977. while (1) {
  3978. /*
  3979. * When someone want to commit while we iterate, end the
  3980. * joined transaction and rejoin.
  3981. */
  3982. if (btrfs_should_end_transaction(trans, send_root)) {
  3983. ret = btrfs_end_transaction(trans, send_root);
  3984. trans = NULL;
  3985. if (ret < 0)
  3986. goto out;
  3987. btrfs_release_path(path);
  3988. goto join_trans;
  3989. }
  3990. eb = path->nodes[0];
  3991. slot = path->slots[0];
  3992. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3993. ret = changed_cb(send_root, NULL, path, NULL,
  3994. &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
  3995. if (ret < 0)
  3996. goto out;
  3997. key.objectid = found_key.objectid;
  3998. key.type = found_key.type;
  3999. key.offset = found_key.offset + 1;
  4000. ret = btrfs_next_item(send_root, path);
  4001. if (ret < 0)
  4002. goto out;
  4003. if (ret) {
  4004. ret = 0;
  4005. break;
  4006. }
  4007. }
  4008. out_finish:
  4009. ret = finish_inode_if_needed(sctx, 1);
  4010. out:
  4011. btrfs_free_path(path);
  4012. if (trans) {
  4013. if (!ret)
  4014. ret = btrfs_end_transaction(trans, send_root);
  4015. else
  4016. btrfs_end_transaction(trans, send_root);
  4017. }
  4018. return ret;
  4019. }
  4020. static int send_subvol(struct send_ctx *sctx)
  4021. {
  4022. int ret;
  4023. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
  4024. ret = send_header(sctx);
  4025. if (ret < 0)
  4026. goto out;
  4027. }
  4028. ret = send_subvol_begin(sctx);
  4029. if (ret < 0)
  4030. goto out;
  4031. if (sctx->parent_root) {
  4032. ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
  4033. changed_cb, sctx);
  4034. if (ret < 0)
  4035. goto out;
  4036. ret = finish_inode_if_needed(sctx, 1);
  4037. if (ret < 0)
  4038. goto out;
  4039. } else {
  4040. ret = full_send_tree(sctx);
  4041. if (ret < 0)
  4042. goto out;
  4043. }
  4044. out:
  4045. if (!ret)
  4046. ret = close_cur_inode_file(sctx);
  4047. else
  4048. close_cur_inode_file(sctx);
  4049. free_recorded_refs(sctx);
  4050. return ret;
  4051. }
  4052. long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
  4053. {
  4054. int ret = 0;
  4055. struct btrfs_root *send_root;
  4056. struct btrfs_root *clone_root;
  4057. struct btrfs_fs_info *fs_info;
  4058. struct btrfs_ioctl_send_args *arg = NULL;
  4059. struct btrfs_key key;
  4060. struct send_ctx *sctx = NULL;
  4061. u32 i;
  4062. u64 *clone_sources_tmp = NULL;
  4063. if (!capable(CAP_SYS_ADMIN))
  4064. return -EPERM;
  4065. send_root = BTRFS_I(file_inode(mnt_file))->root;
  4066. fs_info = send_root->fs_info;
  4067. /*
  4068. * This is done when we lookup the root, it should already be complete
  4069. * by the time we get here.
  4070. */
  4071. WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
  4072. /*
  4073. * If we just created this root we need to make sure that the orphan
  4074. * cleanup has been done and committed since we search the commit root,
  4075. * so check its commit root transid with our otransid and if they match
  4076. * commit the transaction to make sure everything is updated.
  4077. */
  4078. down_read(&send_root->fs_info->extent_commit_sem);
  4079. if (btrfs_header_generation(send_root->commit_root) ==
  4080. btrfs_root_otransid(&send_root->root_item)) {
  4081. struct btrfs_trans_handle *trans;
  4082. up_read(&send_root->fs_info->extent_commit_sem);
  4083. trans = btrfs_attach_transaction_barrier(send_root);
  4084. if (IS_ERR(trans)) {
  4085. if (PTR_ERR(trans) != -ENOENT) {
  4086. ret = PTR_ERR(trans);
  4087. goto out;
  4088. }
  4089. /* ENOENT means theres no transaction */
  4090. } else {
  4091. ret = btrfs_commit_transaction(trans, send_root);
  4092. if (ret)
  4093. goto out;
  4094. }
  4095. } else {
  4096. up_read(&send_root->fs_info->extent_commit_sem);
  4097. }
  4098. arg = memdup_user(arg_, sizeof(*arg));
  4099. if (IS_ERR(arg)) {
  4100. ret = PTR_ERR(arg);
  4101. arg = NULL;
  4102. goto out;
  4103. }
  4104. if (!access_ok(VERIFY_READ, arg->clone_sources,
  4105. sizeof(*arg->clone_sources *
  4106. arg->clone_sources_count))) {
  4107. ret = -EFAULT;
  4108. goto out;
  4109. }
  4110. if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
  4111. ret = -EINVAL;
  4112. goto out;
  4113. }
  4114. sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
  4115. if (!sctx) {
  4116. ret = -ENOMEM;
  4117. goto out;
  4118. }
  4119. INIT_LIST_HEAD(&sctx->new_refs);
  4120. INIT_LIST_HEAD(&sctx->deleted_refs);
  4121. INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
  4122. INIT_LIST_HEAD(&sctx->name_cache_list);
  4123. sctx->flags = arg->flags;
  4124. sctx->send_filp = fget(arg->send_fd);
  4125. if (!sctx->send_filp) {
  4126. ret = -EBADF;
  4127. goto out;
  4128. }
  4129. sctx->mnt = mnt_file->f_path.mnt;
  4130. sctx->send_root = send_root;
  4131. sctx->clone_roots_cnt = arg->clone_sources_count;
  4132. sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
  4133. sctx->send_buf = vmalloc(sctx->send_max_size);
  4134. if (!sctx->send_buf) {
  4135. ret = -ENOMEM;
  4136. goto out;
  4137. }
  4138. sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
  4139. if (!sctx->read_buf) {
  4140. ret = -ENOMEM;
  4141. goto out;
  4142. }
  4143. sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
  4144. (arg->clone_sources_count + 1));
  4145. if (!sctx->clone_roots) {
  4146. ret = -ENOMEM;
  4147. goto out;
  4148. }
  4149. if (arg->clone_sources_count) {
  4150. clone_sources_tmp = vmalloc(arg->clone_sources_count *
  4151. sizeof(*arg->clone_sources));
  4152. if (!clone_sources_tmp) {
  4153. ret = -ENOMEM;
  4154. goto out;
  4155. }
  4156. ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
  4157. arg->clone_sources_count *
  4158. sizeof(*arg->clone_sources));
  4159. if (ret) {
  4160. ret = -EFAULT;
  4161. goto out;
  4162. }
  4163. for (i = 0; i < arg->clone_sources_count; i++) {
  4164. key.objectid = clone_sources_tmp[i];
  4165. key.type = BTRFS_ROOT_ITEM_KEY;
  4166. key.offset = (u64)-1;
  4167. clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
  4168. if (IS_ERR(clone_root)) {
  4169. ret = PTR_ERR(clone_root);
  4170. goto out;
  4171. }
  4172. sctx->clone_roots[i].root = clone_root;
  4173. }
  4174. vfree(clone_sources_tmp);
  4175. clone_sources_tmp = NULL;
  4176. }
  4177. if (arg->parent_root) {
  4178. key.objectid = arg->parent_root;
  4179. key.type = BTRFS_ROOT_ITEM_KEY;
  4180. key.offset = (u64)-1;
  4181. sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
  4182. if (IS_ERR(sctx->parent_root)) {
  4183. ret = PTR_ERR(sctx->parent_root);
  4184. goto out;
  4185. }
  4186. }
  4187. /*
  4188. * Clones from send_root are allowed, but only if the clone source
  4189. * is behind the current send position. This is checked while searching
  4190. * for possible clone sources.
  4191. */
  4192. sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
  4193. /* We do a bsearch later */
  4194. sort(sctx->clone_roots, sctx->clone_roots_cnt,
  4195. sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
  4196. NULL);
  4197. ret = send_subvol(sctx);
  4198. if (ret < 0)
  4199. goto out;
  4200. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
  4201. ret = begin_cmd(sctx, BTRFS_SEND_C_END);
  4202. if (ret < 0)
  4203. goto out;
  4204. ret = send_cmd(sctx);
  4205. if (ret < 0)
  4206. goto out;
  4207. }
  4208. out:
  4209. kfree(arg);
  4210. vfree(clone_sources_tmp);
  4211. if (sctx) {
  4212. if (sctx->send_filp)
  4213. fput(sctx->send_filp);
  4214. vfree(sctx->clone_roots);
  4215. vfree(sctx->send_buf);
  4216. vfree(sctx->read_buf);
  4217. name_cache_free(sctx);
  4218. kfree(sctx);
  4219. }
  4220. return ret;
  4221. }