send.c 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755
  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. int reversed:1;
  52. 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 other_inode = 0;
  1436. u8 other_type = 0;
  1437. if (!sctx->parent_root)
  1438. goto out;
  1439. ret = is_inode_existent(sctx, dir, dir_gen);
  1440. if (ret <= 0)
  1441. goto out;
  1442. ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
  1443. &other_inode, &other_type);
  1444. if (ret < 0 && ret != -ENOENT)
  1445. goto out;
  1446. if (ret) {
  1447. ret = 0;
  1448. goto out;
  1449. }
  1450. /*
  1451. * Check if the overwritten ref was already processed. If yes, the ref
  1452. * was already unlinked/moved, so we can safely assume that we will not
  1453. * overwrite anything at this point in time.
  1454. */
  1455. if (other_inode > sctx->send_progress) {
  1456. ret = get_inode_info(sctx->parent_root, other_inode, NULL,
  1457. who_gen, NULL, NULL, NULL, NULL);
  1458. if (ret < 0)
  1459. goto out;
  1460. ret = 1;
  1461. *who_ino = other_inode;
  1462. } else {
  1463. ret = 0;
  1464. }
  1465. out:
  1466. return ret;
  1467. }
  1468. /*
  1469. * Checks if the ref was overwritten by an already processed inode. This is
  1470. * used by __get_cur_name_and_parent to find out if the ref was orphanized and
  1471. * thus the orphan name needs be used.
  1472. * process_recorded_refs also uses it to avoid unlinking of refs that were
  1473. * overwritten.
  1474. */
  1475. static int did_overwrite_ref(struct send_ctx *sctx,
  1476. u64 dir, u64 dir_gen,
  1477. u64 ino, u64 ino_gen,
  1478. const char *name, int name_len)
  1479. {
  1480. int ret = 0;
  1481. u64 gen;
  1482. u64 ow_inode;
  1483. u8 other_type;
  1484. if (!sctx->parent_root)
  1485. goto out;
  1486. ret = is_inode_existent(sctx, dir, dir_gen);
  1487. if (ret <= 0)
  1488. goto out;
  1489. /* check if the ref was overwritten by another ref */
  1490. ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
  1491. &ow_inode, &other_type);
  1492. if (ret < 0 && ret != -ENOENT)
  1493. goto out;
  1494. if (ret) {
  1495. /* was never and will never be overwritten */
  1496. ret = 0;
  1497. goto out;
  1498. }
  1499. ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
  1500. NULL, NULL);
  1501. if (ret < 0)
  1502. goto out;
  1503. if (ow_inode == ino && gen == ino_gen) {
  1504. ret = 0;
  1505. goto out;
  1506. }
  1507. /* we know that it is or will be overwritten. check this now */
  1508. if (ow_inode < sctx->send_progress)
  1509. ret = 1;
  1510. else
  1511. ret = 0;
  1512. out:
  1513. return ret;
  1514. }
  1515. /*
  1516. * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
  1517. * that got overwritten. This is used by process_recorded_refs to determine
  1518. * if it has to use the path as returned by get_cur_path or the orphan name.
  1519. */
  1520. static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
  1521. {
  1522. int ret = 0;
  1523. struct fs_path *name = NULL;
  1524. u64 dir;
  1525. u64 dir_gen;
  1526. if (!sctx->parent_root)
  1527. goto out;
  1528. name = fs_path_alloc();
  1529. if (!name)
  1530. return -ENOMEM;
  1531. ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
  1532. if (ret < 0)
  1533. goto out;
  1534. ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
  1535. name->start, fs_path_len(name));
  1536. out:
  1537. fs_path_free(name);
  1538. return ret;
  1539. }
  1540. /*
  1541. * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
  1542. * so we need to do some special handling in case we have clashes. This function
  1543. * takes care of this with the help of name_cache_entry::radix_list.
  1544. * In case of error, nce is kfreed.
  1545. */
  1546. static int name_cache_insert(struct send_ctx *sctx,
  1547. struct name_cache_entry *nce)
  1548. {
  1549. int ret = 0;
  1550. struct list_head *nce_head;
  1551. nce_head = radix_tree_lookup(&sctx->name_cache,
  1552. (unsigned long)nce->ino);
  1553. if (!nce_head) {
  1554. nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
  1555. if (!nce_head) {
  1556. kfree(nce);
  1557. return -ENOMEM;
  1558. }
  1559. INIT_LIST_HEAD(nce_head);
  1560. ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
  1561. if (ret < 0) {
  1562. kfree(nce_head);
  1563. kfree(nce);
  1564. return ret;
  1565. }
  1566. }
  1567. list_add_tail(&nce->radix_list, nce_head);
  1568. list_add_tail(&nce->list, &sctx->name_cache_list);
  1569. sctx->name_cache_size++;
  1570. return ret;
  1571. }
  1572. static void name_cache_delete(struct send_ctx *sctx,
  1573. struct name_cache_entry *nce)
  1574. {
  1575. struct list_head *nce_head;
  1576. nce_head = radix_tree_lookup(&sctx->name_cache,
  1577. (unsigned long)nce->ino);
  1578. BUG_ON(!nce_head);
  1579. list_del(&nce->radix_list);
  1580. list_del(&nce->list);
  1581. sctx->name_cache_size--;
  1582. if (list_empty(nce_head)) {
  1583. radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
  1584. kfree(nce_head);
  1585. }
  1586. }
  1587. static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
  1588. u64 ino, u64 gen)
  1589. {
  1590. struct list_head *nce_head;
  1591. struct name_cache_entry *cur;
  1592. nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
  1593. if (!nce_head)
  1594. return NULL;
  1595. list_for_each_entry(cur, nce_head, radix_list) {
  1596. if (cur->ino == ino && cur->gen == gen)
  1597. return cur;
  1598. }
  1599. return NULL;
  1600. }
  1601. /*
  1602. * Removes the entry from the list and adds it back to the end. This marks the
  1603. * entry as recently used so that name_cache_clean_unused does not remove it.
  1604. */
  1605. static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
  1606. {
  1607. list_del(&nce->list);
  1608. list_add_tail(&nce->list, &sctx->name_cache_list);
  1609. }
  1610. /*
  1611. * Remove some entries from the beginning of name_cache_list.
  1612. */
  1613. static void name_cache_clean_unused(struct send_ctx *sctx)
  1614. {
  1615. struct name_cache_entry *nce;
  1616. if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
  1617. return;
  1618. while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
  1619. nce = list_entry(sctx->name_cache_list.next,
  1620. struct name_cache_entry, list);
  1621. name_cache_delete(sctx, nce);
  1622. kfree(nce);
  1623. }
  1624. }
  1625. static void name_cache_free(struct send_ctx *sctx)
  1626. {
  1627. struct name_cache_entry *nce;
  1628. while (!list_empty(&sctx->name_cache_list)) {
  1629. nce = list_entry(sctx->name_cache_list.next,
  1630. struct name_cache_entry, list);
  1631. name_cache_delete(sctx, nce);
  1632. kfree(nce);
  1633. }
  1634. }
  1635. /*
  1636. * Used by get_cur_path for each ref up to the root.
  1637. * Returns 0 if it succeeded.
  1638. * Returns 1 if the inode is not existent or got overwritten. In that case, the
  1639. * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
  1640. * is returned, parent_ino/parent_gen are not guaranteed to be valid.
  1641. * Returns <0 in case of error.
  1642. */
  1643. static int __get_cur_name_and_parent(struct send_ctx *sctx,
  1644. u64 ino, u64 gen,
  1645. u64 *parent_ino,
  1646. u64 *parent_gen,
  1647. struct fs_path *dest)
  1648. {
  1649. int ret;
  1650. int nce_ret;
  1651. struct btrfs_path *path = NULL;
  1652. struct name_cache_entry *nce = NULL;
  1653. /*
  1654. * First check if we already did a call to this function with the same
  1655. * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
  1656. * return the cached result.
  1657. */
  1658. nce = name_cache_search(sctx, ino, gen);
  1659. if (nce) {
  1660. if (ino < sctx->send_progress && nce->need_later_update) {
  1661. name_cache_delete(sctx, nce);
  1662. kfree(nce);
  1663. nce = NULL;
  1664. } else {
  1665. name_cache_used(sctx, nce);
  1666. *parent_ino = nce->parent_ino;
  1667. *parent_gen = nce->parent_gen;
  1668. ret = fs_path_add(dest, nce->name, nce->name_len);
  1669. if (ret < 0)
  1670. goto out;
  1671. ret = nce->ret;
  1672. goto out;
  1673. }
  1674. }
  1675. path = alloc_path_for_send();
  1676. if (!path)
  1677. return -ENOMEM;
  1678. /*
  1679. * If the inode is not existent yet, add the orphan name and return 1.
  1680. * This should only happen for the parent dir that we determine in
  1681. * __record_new_ref
  1682. */
  1683. ret = is_inode_existent(sctx, ino, gen);
  1684. if (ret < 0)
  1685. goto out;
  1686. if (!ret) {
  1687. ret = gen_unique_name(sctx, ino, gen, dest);
  1688. if (ret < 0)
  1689. goto out;
  1690. ret = 1;
  1691. goto out_cache;
  1692. }
  1693. /*
  1694. * Depending on whether the inode was already processed or not, use
  1695. * send_root or parent_root for ref lookup.
  1696. */
  1697. if (ino < sctx->send_progress)
  1698. ret = get_first_ref(sctx->send_root, ino,
  1699. parent_ino, parent_gen, dest);
  1700. else
  1701. ret = get_first_ref(sctx->parent_root, ino,
  1702. parent_ino, parent_gen, dest);
  1703. if (ret < 0)
  1704. goto out;
  1705. /*
  1706. * Check if the ref was overwritten by an inode's ref that was processed
  1707. * earlier. If yes, treat as orphan and return 1.
  1708. */
  1709. ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
  1710. dest->start, dest->end - dest->start);
  1711. if (ret < 0)
  1712. goto out;
  1713. if (ret) {
  1714. fs_path_reset(dest);
  1715. ret = gen_unique_name(sctx, ino, gen, dest);
  1716. if (ret < 0)
  1717. goto out;
  1718. ret = 1;
  1719. }
  1720. out_cache:
  1721. /*
  1722. * Store the result of the lookup in the name cache.
  1723. */
  1724. nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
  1725. if (!nce) {
  1726. ret = -ENOMEM;
  1727. goto out;
  1728. }
  1729. nce->ino = ino;
  1730. nce->gen = gen;
  1731. nce->parent_ino = *parent_ino;
  1732. nce->parent_gen = *parent_gen;
  1733. nce->name_len = fs_path_len(dest);
  1734. nce->ret = ret;
  1735. strcpy(nce->name, dest->start);
  1736. if (ino < sctx->send_progress)
  1737. nce->need_later_update = 0;
  1738. else
  1739. nce->need_later_update = 1;
  1740. nce_ret = name_cache_insert(sctx, nce);
  1741. if (nce_ret < 0)
  1742. ret = nce_ret;
  1743. name_cache_clean_unused(sctx);
  1744. out:
  1745. btrfs_free_path(path);
  1746. return ret;
  1747. }
  1748. /*
  1749. * Magic happens here. This function returns the first ref to an inode as it
  1750. * would look like while receiving the stream at this point in time.
  1751. * We walk the path up to the root. For every inode in between, we check if it
  1752. * was already processed/sent. If yes, we continue with the parent as found
  1753. * in send_root. If not, we continue with the parent as found in parent_root.
  1754. * If we encounter an inode that was deleted at this point in time, we use the
  1755. * inodes "orphan" name instead of the real name and stop. Same with new inodes
  1756. * that were not created yet and overwritten inodes/refs.
  1757. *
  1758. * When do we have have orphan inodes:
  1759. * 1. When an inode is freshly created and thus no valid refs are available yet
  1760. * 2. When a directory lost all it's refs (deleted) but still has dir items
  1761. * inside which were not processed yet (pending for move/delete). If anyone
  1762. * tried to get the path to the dir items, it would get a path inside that
  1763. * orphan directory.
  1764. * 3. When an inode is moved around or gets new links, it may overwrite the ref
  1765. * of an unprocessed inode. If in that case the first ref would be
  1766. * overwritten, the overwritten inode gets "orphanized". Later when we
  1767. * process this overwritten inode, it is restored at a new place by moving
  1768. * the orphan inode.
  1769. *
  1770. * sctx->send_progress tells this function at which point in time receiving
  1771. * would be.
  1772. */
  1773. static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
  1774. struct fs_path *dest)
  1775. {
  1776. int ret = 0;
  1777. struct fs_path *name = NULL;
  1778. u64 parent_inode = 0;
  1779. u64 parent_gen = 0;
  1780. int stop = 0;
  1781. name = fs_path_alloc();
  1782. if (!name) {
  1783. ret = -ENOMEM;
  1784. goto out;
  1785. }
  1786. dest->reversed = 1;
  1787. fs_path_reset(dest);
  1788. while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
  1789. fs_path_reset(name);
  1790. ret = __get_cur_name_and_parent(sctx, ino, gen,
  1791. &parent_inode, &parent_gen, name);
  1792. if (ret < 0)
  1793. goto out;
  1794. if (ret)
  1795. stop = 1;
  1796. ret = fs_path_add_path(dest, name);
  1797. if (ret < 0)
  1798. goto out;
  1799. ino = parent_inode;
  1800. gen = parent_gen;
  1801. }
  1802. out:
  1803. fs_path_free(name);
  1804. if (!ret)
  1805. fs_path_unreverse(dest);
  1806. return ret;
  1807. }
  1808. /*
  1809. * Called for regular files when sending extents data. Opens a struct file
  1810. * to read from the file.
  1811. */
  1812. static int open_cur_inode_file(struct send_ctx *sctx)
  1813. {
  1814. int ret = 0;
  1815. struct btrfs_key key;
  1816. struct path path;
  1817. struct inode *inode;
  1818. struct dentry *dentry;
  1819. struct file *filp;
  1820. int new = 0;
  1821. if (sctx->cur_inode_filp)
  1822. goto out;
  1823. key.objectid = sctx->cur_ino;
  1824. key.type = BTRFS_INODE_ITEM_KEY;
  1825. key.offset = 0;
  1826. inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
  1827. &new);
  1828. if (IS_ERR(inode)) {
  1829. ret = PTR_ERR(inode);
  1830. goto out;
  1831. }
  1832. dentry = d_obtain_alias(inode);
  1833. inode = NULL;
  1834. if (IS_ERR(dentry)) {
  1835. ret = PTR_ERR(dentry);
  1836. goto out;
  1837. }
  1838. path.mnt = sctx->mnt;
  1839. path.dentry = dentry;
  1840. filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
  1841. dput(dentry);
  1842. dentry = NULL;
  1843. if (IS_ERR(filp)) {
  1844. ret = PTR_ERR(filp);
  1845. goto out;
  1846. }
  1847. sctx->cur_inode_filp = filp;
  1848. out:
  1849. /*
  1850. * no xxxput required here as every vfs op
  1851. * does it by itself on failure
  1852. */
  1853. return ret;
  1854. }
  1855. /*
  1856. * Closes the struct file that was created in open_cur_inode_file
  1857. */
  1858. static int close_cur_inode_file(struct send_ctx *sctx)
  1859. {
  1860. int ret = 0;
  1861. if (!sctx->cur_inode_filp)
  1862. goto out;
  1863. ret = filp_close(sctx->cur_inode_filp, NULL);
  1864. sctx->cur_inode_filp = NULL;
  1865. out:
  1866. return ret;
  1867. }
  1868. /*
  1869. * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
  1870. */
  1871. static int send_subvol_begin(struct send_ctx *sctx)
  1872. {
  1873. int ret;
  1874. struct btrfs_root *send_root = sctx->send_root;
  1875. struct btrfs_root *parent_root = sctx->parent_root;
  1876. struct btrfs_path *path;
  1877. struct btrfs_key key;
  1878. struct btrfs_root_ref *ref;
  1879. struct extent_buffer *leaf;
  1880. char *name = NULL;
  1881. int namelen;
  1882. path = alloc_path_for_send();
  1883. if (!path)
  1884. return -ENOMEM;
  1885. name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
  1886. if (!name) {
  1887. btrfs_free_path(path);
  1888. return -ENOMEM;
  1889. }
  1890. key.objectid = send_root->objectid;
  1891. key.type = BTRFS_ROOT_BACKREF_KEY;
  1892. key.offset = 0;
  1893. ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
  1894. &key, path, 1, 0);
  1895. if (ret < 0)
  1896. goto out;
  1897. if (ret) {
  1898. ret = -ENOENT;
  1899. goto out;
  1900. }
  1901. leaf = path->nodes[0];
  1902. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1903. if (key.type != BTRFS_ROOT_BACKREF_KEY ||
  1904. key.objectid != send_root->objectid) {
  1905. ret = -ENOENT;
  1906. goto out;
  1907. }
  1908. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  1909. namelen = btrfs_root_ref_name_len(leaf, ref);
  1910. read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
  1911. btrfs_release_path(path);
  1912. if (parent_root) {
  1913. ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
  1914. if (ret < 0)
  1915. goto out;
  1916. } else {
  1917. ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
  1918. if (ret < 0)
  1919. goto out;
  1920. }
  1921. TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
  1922. TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
  1923. sctx->send_root->root_item.uuid);
  1924. TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
  1925. sctx->send_root->root_item.ctransid);
  1926. if (parent_root) {
  1927. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  1928. sctx->parent_root->root_item.uuid);
  1929. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  1930. sctx->parent_root->root_item.ctransid);
  1931. }
  1932. ret = send_cmd(sctx);
  1933. tlv_put_failure:
  1934. out:
  1935. btrfs_free_path(path);
  1936. kfree(name);
  1937. return ret;
  1938. }
  1939. static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
  1940. {
  1941. int ret = 0;
  1942. struct fs_path *p;
  1943. verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
  1944. p = fs_path_alloc();
  1945. if (!p)
  1946. return -ENOMEM;
  1947. ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
  1948. if (ret < 0)
  1949. goto out;
  1950. ret = get_cur_path(sctx, ino, gen, p);
  1951. if (ret < 0)
  1952. goto out;
  1953. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  1954. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
  1955. ret = send_cmd(sctx);
  1956. tlv_put_failure:
  1957. out:
  1958. fs_path_free(p);
  1959. return ret;
  1960. }
  1961. static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
  1962. {
  1963. int ret = 0;
  1964. struct fs_path *p;
  1965. verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
  1966. p = fs_path_alloc();
  1967. if (!p)
  1968. return -ENOMEM;
  1969. ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
  1970. if (ret < 0)
  1971. goto out;
  1972. ret = get_cur_path(sctx, ino, gen, p);
  1973. if (ret < 0)
  1974. goto out;
  1975. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  1976. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
  1977. ret = send_cmd(sctx);
  1978. tlv_put_failure:
  1979. out:
  1980. fs_path_free(p);
  1981. return ret;
  1982. }
  1983. static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
  1984. {
  1985. int ret = 0;
  1986. struct fs_path *p;
  1987. verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
  1988. p = fs_path_alloc();
  1989. if (!p)
  1990. return -ENOMEM;
  1991. ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
  1992. if (ret < 0)
  1993. goto out;
  1994. ret = get_cur_path(sctx, ino, gen, p);
  1995. if (ret < 0)
  1996. goto out;
  1997. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  1998. TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
  1999. TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
  2000. ret = send_cmd(sctx);
  2001. tlv_put_failure:
  2002. out:
  2003. fs_path_free(p);
  2004. return ret;
  2005. }
  2006. static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
  2007. {
  2008. int ret = 0;
  2009. struct fs_path *p = NULL;
  2010. struct btrfs_inode_item *ii;
  2011. struct btrfs_path *path = NULL;
  2012. struct extent_buffer *eb;
  2013. struct btrfs_key key;
  2014. int slot;
  2015. verbose_printk("btrfs: send_utimes %llu\n", ino);
  2016. p = fs_path_alloc();
  2017. if (!p)
  2018. return -ENOMEM;
  2019. path = alloc_path_for_send();
  2020. if (!path) {
  2021. ret = -ENOMEM;
  2022. goto out;
  2023. }
  2024. key.objectid = ino;
  2025. key.type = BTRFS_INODE_ITEM_KEY;
  2026. key.offset = 0;
  2027. ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
  2028. if (ret < 0)
  2029. goto out;
  2030. eb = path->nodes[0];
  2031. slot = path->slots[0];
  2032. ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  2033. ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
  2034. if (ret < 0)
  2035. goto out;
  2036. ret = get_cur_path(sctx, ino, gen, p);
  2037. if (ret < 0)
  2038. goto out;
  2039. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2040. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
  2041. btrfs_inode_atime(ii));
  2042. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
  2043. btrfs_inode_mtime(ii));
  2044. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
  2045. btrfs_inode_ctime(ii));
  2046. /* TODO Add otime support when the otime patches get into upstream */
  2047. ret = send_cmd(sctx);
  2048. tlv_put_failure:
  2049. out:
  2050. fs_path_free(p);
  2051. btrfs_free_path(path);
  2052. return ret;
  2053. }
  2054. /*
  2055. * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
  2056. * a valid path yet because we did not process the refs yet. So, the inode
  2057. * is created as orphan.
  2058. */
  2059. static int send_create_inode(struct send_ctx *sctx, u64 ino)
  2060. {
  2061. int ret = 0;
  2062. struct fs_path *p;
  2063. int cmd;
  2064. u64 gen;
  2065. u64 mode;
  2066. u64 rdev;
  2067. verbose_printk("btrfs: send_create_inode %llu\n", ino);
  2068. p = fs_path_alloc();
  2069. if (!p)
  2070. return -ENOMEM;
  2071. ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
  2072. NULL, &rdev);
  2073. if (ret < 0)
  2074. goto out;
  2075. if (S_ISREG(mode)) {
  2076. cmd = BTRFS_SEND_C_MKFILE;
  2077. } else if (S_ISDIR(mode)) {
  2078. cmd = BTRFS_SEND_C_MKDIR;
  2079. } else if (S_ISLNK(mode)) {
  2080. cmd = BTRFS_SEND_C_SYMLINK;
  2081. } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
  2082. cmd = BTRFS_SEND_C_MKNOD;
  2083. } else if (S_ISFIFO(mode)) {
  2084. cmd = BTRFS_SEND_C_MKFIFO;
  2085. } else if (S_ISSOCK(mode)) {
  2086. cmd = BTRFS_SEND_C_MKSOCK;
  2087. } else {
  2088. printk(KERN_WARNING "btrfs: unexpected inode type %o",
  2089. (int)(mode & S_IFMT));
  2090. ret = -ENOTSUPP;
  2091. goto out;
  2092. }
  2093. ret = begin_cmd(sctx, cmd);
  2094. if (ret < 0)
  2095. goto out;
  2096. ret = gen_unique_name(sctx, ino, gen, p);
  2097. if (ret < 0)
  2098. goto out;
  2099. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2100. TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
  2101. if (S_ISLNK(mode)) {
  2102. fs_path_reset(p);
  2103. ret = read_symlink(sctx->send_root, ino, p);
  2104. if (ret < 0)
  2105. goto out;
  2106. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
  2107. } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
  2108. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  2109. TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
  2110. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
  2111. }
  2112. ret = send_cmd(sctx);
  2113. if (ret < 0)
  2114. goto out;
  2115. tlv_put_failure:
  2116. out:
  2117. fs_path_free(p);
  2118. return ret;
  2119. }
  2120. /*
  2121. * We need some special handling for inodes that get processed before the parent
  2122. * directory got created. See process_recorded_refs for details.
  2123. * This function does the check if we already created the dir out of order.
  2124. */
  2125. static int did_create_dir(struct send_ctx *sctx, u64 dir)
  2126. {
  2127. int ret = 0;
  2128. struct btrfs_path *path = NULL;
  2129. struct btrfs_key key;
  2130. struct btrfs_key found_key;
  2131. struct btrfs_key di_key;
  2132. struct extent_buffer *eb;
  2133. struct btrfs_dir_item *di;
  2134. int slot;
  2135. path = alloc_path_for_send();
  2136. if (!path) {
  2137. ret = -ENOMEM;
  2138. goto out;
  2139. }
  2140. key.objectid = dir;
  2141. key.type = BTRFS_DIR_INDEX_KEY;
  2142. key.offset = 0;
  2143. while (1) {
  2144. ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
  2145. 1, 0);
  2146. if (ret < 0)
  2147. goto out;
  2148. if (!ret) {
  2149. eb = path->nodes[0];
  2150. slot = path->slots[0];
  2151. btrfs_item_key_to_cpu(eb, &found_key, slot);
  2152. }
  2153. if (ret || found_key.objectid != key.objectid ||
  2154. found_key.type != key.type) {
  2155. ret = 0;
  2156. goto out;
  2157. }
  2158. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  2159. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  2160. if (di_key.objectid < sctx->send_progress) {
  2161. ret = 1;
  2162. goto out;
  2163. }
  2164. key.offset = found_key.offset + 1;
  2165. btrfs_release_path(path);
  2166. }
  2167. out:
  2168. btrfs_free_path(path);
  2169. return ret;
  2170. }
  2171. /*
  2172. * Only creates the inode if it is:
  2173. * 1. Not a directory
  2174. * 2. Or a directory which was not created already due to out of order
  2175. * directories. See did_create_dir and process_recorded_refs for details.
  2176. */
  2177. static int send_create_inode_if_needed(struct send_ctx *sctx)
  2178. {
  2179. int ret;
  2180. if (S_ISDIR(sctx->cur_inode_mode)) {
  2181. ret = did_create_dir(sctx, sctx->cur_ino);
  2182. if (ret < 0)
  2183. goto out;
  2184. if (ret) {
  2185. ret = 0;
  2186. goto out;
  2187. }
  2188. }
  2189. ret = send_create_inode(sctx, sctx->cur_ino);
  2190. if (ret < 0)
  2191. goto out;
  2192. out:
  2193. return ret;
  2194. }
  2195. struct recorded_ref {
  2196. struct list_head list;
  2197. char *dir_path;
  2198. char *name;
  2199. struct fs_path *full_path;
  2200. u64 dir;
  2201. u64 dir_gen;
  2202. int dir_path_len;
  2203. int name_len;
  2204. };
  2205. /*
  2206. * We need to process new refs before deleted refs, but compare_tree gives us
  2207. * everything mixed. So we first record all refs and later process them.
  2208. * This function is a helper to record one ref.
  2209. */
  2210. static int record_ref(struct list_head *head, u64 dir,
  2211. u64 dir_gen, struct fs_path *path)
  2212. {
  2213. struct recorded_ref *ref;
  2214. char *tmp;
  2215. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  2216. if (!ref)
  2217. return -ENOMEM;
  2218. ref->dir = dir;
  2219. ref->dir_gen = dir_gen;
  2220. ref->full_path = path;
  2221. tmp = strrchr(ref->full_path->start, '/');
  2222. if (!tmp) {
  2223. ref->name_len = ref->full_path->end - ref->full_path->start;
  2224. ref->name = ref->full_path->start;
  2225. ref->dir_path_len = 0;
  2226. ref->dir_path = ref->full_path->start;
  2227. } else {
  2228. tmp++;
  2229. ref->name_len = ref->full_path->end - tmp;
  2230. ref->name = tmp;
  2231. ref->dir_path = ref->full_path->start;
  2232. ref->dir_path_len = ref->full_path->end -
  2233. ref->full_path->start - 1 - ref->name_len;
  2234. }
  2235. list_add_tail(&ref->list, head);
  2236. return 0;
  2237. }
  2238. static void __free_recorded_refs(struct list_head *head)
  2239. {
  2240. struct recorded_ref *cur;
  2241. while (!list_empty(head)) {
  2242. cur = list_entry(head->next, struct recorded_ref, list);
  2243. fs_path_free(cur->full_path);
  2244. list_del(&cur->list);
  2245. kfree(cur);
  2246. }
  2247. }
  2248. static void free_recorded_refs(struct send_ctx *sctx)
  2249. {
  2250. __free_recorded_refs(&sctx->new_refs);
  2251. __free_recorded_refs(&sctx->deleted_refs);
  2252. }
  2253. /*
  2254. * Renames/moves a file/dir to its orphan name. Used when the first
  2255. * ref of an unprocessed inode gets overwritten and for all non empty
  2256. * directories.
  2257. */
  2258. static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
  2259. struct fs_path *path)
  2260. {
  2261. int ret;
  2262. struct fs_path *orphan;
  2263. orphan = fs_path_alloc();
  2264. if (!orphan)
  2265. return -ENOMEM;
  2266. ret = gen_unique_name(sctx, ino, gen, orphan);
  2267. if (ret < 0)
  2268. goto out;
  2269. ret = send_rename(sctx, path, orphan);
  2270. out:
  2271. fs_path_free(orphan);
  2272. return ret;
  2273. }
  2274. /*
  2275. * Returns 1 if a directory can be removed at this point in time.
  2276. * We check this by iterating all dir items and checking if the inode behind
  2277. * the dir item was already processed.
  2278. */
  2279. static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
  2280. {
  2281. int ret = 0;
  2282. struct btrfs_root *root = sctx->parent_root;
  2283. struct btrfs_path *path;
  2284. struct btrfs_key key;
  2285. struct btrfs_key found_key;
  2286. struct btrfs_key loc;
  2287. struct btrfs_dir_item *di;
  2288. /*
  2289. * Don't try to rmdir the top/root subvolume dir.
  2290. */
  2291. if (dir == BTRFS_FIRST_FREE_OBJECTID)
  2292. return 0;
  2293. path = alloc_path_for_send();
  2294. if (!path)
  2295. return -ENOMEM;
  2296. key.objectid = dir;
  2297. key.type = BTRFS_DIR_INDEX_KEY;
  2298. key.offset = 0;
  2299. while (1) {
  2300. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  2301. if (ret < 0)
  2302. goto out;
  2303. if (!ret) {
  2304. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2305. path->slots[0]);
  2306. }
  2307. if (ret || found_key.objectid != key.objectid ||
  2308. found_key.type != key.type) {
  2309. break;
  2310. }
  2311. di = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2312. struct btrfs_dir_item);
  2313. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
  2314. if (loc.objectid > send_progress) {
  2315. ret = 0;
  2316. goto out;
  2317. }
  2318. btrfs_release_path(path);
  2319. key.offset = found_key.offset + 1;
  2320. }
  2321. ret = 1;
  2322. out:
  2323. btrfs_free_path(path);
  2324. return ret;
  2325. }
  2326. /*
  2327. * This does all the move/link/unlink/rmdir magic.
  2328. */
  2329. static int process_recorded_refs(struct send_ctx *sctx)
  2330. {
  2331. int ret = 0;
  2332. struct recorded_ref *cur;
  2333. struct recorded_ref *cur2;
  2334. struct ulist *check_dirs = NULL;
  2335. struct ulist_iterator uit;
  2336. struct ulist_node *un;
  2337. struct fs_path *valid_path = NULL;
  2338. u64 ow_inode = 0;
  2339. u64 ow_gen;
  2340. int did_overwrite = 0;
  2341. int is_orphan = 0;
  2342. verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
  2343. /*
  2344. * This should never happen as the root dir always has the same ref
  2345. * which is always '..'
  2346. */
  2347. BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
  2348. valid_path = fs_path_alloc();
  2349. if (!valid_path) {
  2350. ret = -ENOMEM;
  2351. goto out;
  2352. }
  2353. check_dirs = ulist_alloc(GFP_NOFS);
  2354. if (!check_dirs) {
  2355. ret = -ENOMEM;
  2356. goto out;
  2357. }
  2358. /*
  2359. * First, check if the first ref of the current inode was overwritten
  2360. * before. If yes, we know that the current inode was already orphanized
  2361. * and thus use the orphan name. If not, we can use get_cur_path to
  2362. * get the path of the first ref as it would like while receiving at
  2363. * this point in time.
  2364. * New inodes are always orphan at the beginning, so force to use the
  2365. * orphan name in this case.
  2366. * The first ref is stored in valid_path and will be updated if it
  2367. * gets moved around.
  2368. */
  2369. if (!sctx->cur_inode_new) {
  2370. ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
  2371. sctx->cur_inode_gen);
  2372. if (ret < 0)
  2373. goto out;
  2374. if (ret)
  2375. did_overwrite = 1;
  2376. }
  2377. if (sctx->cur_inode_new || did_overwrite) {
  2378. ret = gen_unique_name(sctx, sctx->cur_ino,
  2379. sctx->cur_inode_gen, valid_path);
  2380. if (ret < 0)
  2381. goto out;
  2382. is_orphan = 1;
  2383. } else {
  2384. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  2385. valid_path);
  2386. if (ret < 0)
  2387. goto out;
  2388. }
  2389. list_for_each_entry(cur, &sctx->new_refs, list) {
  2390. /*
  2391. * We may have refs where the parent directory does not exist
  2392. * yet. This happens if the parent directories inum is higher
  2393. * the the current inum. To handle this case, we create the
  2394. * parent directory out of order. But we need to check if this
  2395. * did already happen before due to other refs in the same dir.
  2396. */
  2397. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
  2398. if (ret < 0)
  2399. goto out;
  2400. if (ret == inode_state_will_create) {
  2401. ret = 0;
  2402. /*
  2403. * First check if any of the current inodes refs did
  2404. * already create the dir.
  2405. */
  2406. list_for_each_entry(cur2, &sctx->new_refs, list) {
  2407. if (cur == cur2)
  2408. break;
  2409. if (cur2->dir == cur->dir) {
  2410. ret = 1;
  2411. break;
  2412. }
  2413. }
  2414. /*
  2415. * If that did not happen, check if a previous inode
  2416. * did already create the dir.
  2417. */
  2418. if (!ret)
  2419. ret = did_create_dir(sctx, cur->dir);
  2420. if (ret < 0)
  2421. goto out;
  2422. if (!ret) {
  2423. ret = send_create_inode(sctx, cur->dir);
  2424. if (ret < 0)
  2425. goto out;
  2426. }
  2427. }
  2428. /*
  2429. * Check if this new ref would overwrite the first ref of
  2430. * another unprocessed inode. If yes, orphanize the
  2431. * overwritten inode. If we find an overwritten ref that is
  2432. * not the first ref, simply unlink it.
  2433. */
  2434. ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  2435. cur->name, cur->name_len,
  2436. &ow_inode, &ow_gen);
  2437. if (ret < 0)
  2438. goto out;
  2439. if (ret) {
  2440. ret = is_first_ref(sctx->parent_root,
  2441. ow_inode, cur->dir, cur->name,
  2442. cur->name_len);
  2443. if (ret < 0)
  2444. goto out;
  2445. if (ret) {
  2446. ret = orphanize_inode(sctx, ow_inode, ow_gen,
  2447. cur->full_path);
  2448. if (ret < 0)
  2449. goto out;
  2450. } else {
  2451. ret = send_unlink(sctx, cur->full_path);
  2452. if (ret < 0)
  2453. goto out;
  2454. }
  2455. }
  2456. /*
  2457. * link/move the ref to the new place. If we have an orphan
  2458. * inode, move it and update valid_path. If not, link or move
  2459. * it depending on the inode mode.
  2460. */
  2461. if (is_orphan) {
  2462. ret = send_rename(sctx, valid_path, cur->full_path);
  2463. if (ret < 0)
  2464. goto out;
  2465. is_orphan = 0;
  2466. ret = fs_path_copy(valid_path, cur->full_path);
  2467. if (ret < 0)
  2468. goto out;
  2469. } else {
  2470. if (S_ISDIR(sctx->cur_inode_mode)) {
  2471. /*
  2472. * Dirs can't be linked, so move it. For moved
  2473. * dirs, we always have one new and one deleted
  2474. * ref. The deleted ref is ignored later.
  2475. */
  2476. ret = send_rename(sctx, valid_path,
  2477. cur->full_path);
  2478. if (ret < 0)
  2479. goto out;
  2480. ret = fs_path_copy(valid_path, cur->full_path);
  2481. if (ret < 0)
  2482. goto out;
  2483. } else {
  2484. ret = send_link(sctx, cur->full_path,
  2485. valid_path);
  2486. if (ret < 0)
  2487. goto out;
  2488. }
  2489. }
  2490. ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
  2491. GFP_NOFS);
  2492. if (ret < 0)
  2493. goto out;
  2494. }
  2495. if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
  2496. /*
  2497. * Check if we can already rmdir the directory. If not,
  2498. * orphanize it. For every dir item inside that gets deleted
  2499. * later, we do this check again and rmdir it then if possible.
  2500. * See the use of check_dirs for more details.
  2501. */
  2502. ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
  2503. if (ret < 0)
  2504. goto out;
  2505. if (ret) {
  2506. ret = send_rmdir(sctx, valid_path);
  2507. if (ret < 0)
  2508. goto out;
  2509. } else if (!is_orphan) {
  2510. ret = orphanize_inode(sctx, sctx->cur_ino,
  2511. sctx->cur_inode_gen, valid_path);
  2512. if (ret < 0)
  2513. goto out;
  2514. is_orphan = 1;
  2515. }
  2516. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  2517. ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
  2518. GFP_NOFS);
  2519. if (ret < 0)
  2520. goto out;
  2521. }
  2522. } else if (S_ISDIR(sctx->cur_inode_mode) &&
  2523. !list_empty(&sctx->deleted_refs)) {
  2524. /*
  2525. * We have a moved dir. Add the old parent to check_dirs
  2526. */
  2527. cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
  2528. list);
  2529. ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
  2530. GFP_NOFS);
  2531. if (ret < 0)
  2532. goto out;
  2533. } else if (!S_ISDIR(sctx->cur_inode_mode)) {
  2534. /*
  2535. * We have a non dir inode. Go through all deleted refs and
  2536. * unlink them if they were not already overwritten by other
  2537. * inodes.
  2538. */
  2539. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  2540. ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  2541. sctx->cur_ino, sctx->cur_inode_gen,
  2542. cur->name, cur->name_len);
  2543. if (ret < 0)
  2544. goto out;
  2545. if (!ret) {
  2546. ret = send_unlink(sctx, cur->full_path);
  2547. if (ret < 0)
  2548. goto out;
  2549. }
  2550. ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
  2551. GFP_NOFS);
  2552. if (ret < 0)
  2553. goto out;
  2554. }
  2555. /*
  2556. * If the inode is still orphan, unlink the orphan. This may
  2557. * happen when a previous inode did overwrite the first ref
  2558. * of this inode and no new refs were added for the current
  2559. * inode. Unlinking does not mean that the inode is deleted in
  2560. * all cases. There may still be links to this inode in other
  2561. * places.
  2562. */
  2563. if (is_orphan) {
  2564. ret = send_unlink(sctx, valid_path);
  2565. if (ret < 0)
  2566. goto out;
  2567. }
  2568. }
  2569. /*
  2570. * We did collect all parent dirs where cur_inode was once located. We
  2571. * now go through all these dirs and check if they are pending for
  2572. * deletion and if it's finally possible to perform the rmdir now.
  2573. * We also update the inode stats of the parent dirs here.
  2574. */
  2575. ULIST_ITER_INIT(&uit);
  2576. while ((un = ulist_next(check_dirs, &uit))) {
  2577. /*
  2578. * In case we had refs into dirs that were not processed yet,
  2579. * we don't need to do the utime and rmdir logic for these dirs.
  2580. * The dir will be processed later.
  2581. */
  2582. if (un->val > sctx->cur_ino)
  2583. continue;
  2584. ret = get_cur_inode_state(sctx, un->val, un->aux);
  2585. if (ret < 0)
  2586. goto out;
  2587. if (ret == inode_state_did_create ||
  2588. ret == inode_state_no_change) {
  2589. /* TODO delayed utimes */
  2590. ret = send_utimes(sctx, un->val, un->aux);
  2591. if (ret < 0)
  2592. goto out;
  2593. } else if (ret == inode_state_did_delete) {
  2594. ret = can_rmdir(sctx, un->val, sctx->cur_ino);
  2595. if (ret < 0)
  2596. goto out;
  2597. if (ret) {
  2598. ret = get_cur_path(sctx, un->val, un->aux,
  2599. valid_path);
  2600. if (ret < 0)
  2601. goto out;
  2602. ret = send_rmdir(sctx, valid_path);
  2603. if (ret < 0)
  2604. goto out;
  2605. }
  2606. }
  2607. }
  2608. ret = 0;
  2609. out:
  2610. free_recorded_refs(sctx);
  2611. ulist_free(check_dirs);
  2612. fs_path_free(valid_path);
  2613. return ret;
  2614. }
  2615. static int __record_new_ref(int num, u64 dir, int index,
  2616. struct fs_path *name,
  2617. void *ctx)
  2618. {
  2619. int ret = 0;
  2620. struct send_ctx *sctx = ctx;
  2621. struct fs_path *p;
  2622. u64 gen;
  2623. p = fs_path_alloc();
  2624. if (!p)
  2625. return -ENOMEM;
  2626. ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
  2627. NULL, NULL);
  2628. if (ret < 0)
  2629. goto out;
  2630. ret = get_cur_path(sctx, dir, gen, p);
  2631. if (ret < 0)
  2632. goto out;
  2633. ret = fs_path_add_path(p, name);
  2634. if (ret < 0)
  2635. goto out;
  2636. ret = record_ref(&sctx->new_refs, dir, gen, p);
  2637. out:
  2638. if (ret)
  2639. fs_path_free(p);
  2640. return ret;
  2641. }
  2642. static int __record_deleted_ref(int num, u64 dir, int index,
  2643. struct fs_path *name,
  2644. void *ctx)
  2645. {
  2646. int ret = 0;
  2647. struct send_ctx *sctx = ctx;
  2648. struct fs_path *p;
  2649. u64 gen;
  2650. p = fs_path_alloc();
  2651. if (!p)
  2652. return -ENOMEM;
  2653. ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
  2654. NULL, NULL);
  2655. if (ret < 0)
  2656. goto out;
  2657. ret = get_cur_path(sctx, dir, gen, p);
  2658. if (ret < 0)
  2659. goto out;
  2660. ret = fs_path_add_path(p, name);
  2661. if (ret < 0)
  2662. goto out;
  2663. ret = record_ref(&sctx->deleted_refs, dir, gen, p);
  2664. out:
  2665. if (ret)
  2666. fs_path_free(p);
  2667. return ret;
  2668. }
  2669. static int record_new_ref(struct send_ctx *sctx)
  2670. {
  2671. int ret;
  2672. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  2673. sctx->cmp_key, 0, __record_new_ref, sctx);
  2674. if (ret < 0)
  2675. goto out;
  2676. ret = 0;
  2677. out:
  2678. return ret;
  2679. }
  2680. static int record_deleted_ref(struct send_ctx *sctx)
  2681. {
  2682. int ret;
  2683. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  2684. sctx->cmp_key, 0, __record_deleted_ref, sctx);
  2685. if (ret < 0)
  2686. goto out;
  2687. ret = 0;
  2688. out:
  2689. return ret;
  2690. }
  2691. struct find_ref_ctx {
  2692. u64 dir;
  2693. struct fs_path *name;
  2694. int found_idx;
  2695. };
  2696. static int __find_iref(int num, u64 dir, int index,
  2697. struct fs_path *name,
  2698. void *ctx_)
  2699. {
  2700. struct find_ref_ctx *ctx = ctx_;
  2701. if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
  2702. strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
  2703. ctx->found_idx = num;
  2704. return 1;
  2705. }
  2706. return 0;
  2707. }
  2708. static int find_iref(struct btrfs_root *root,
  2709. struct btrfs_path *path,
  2710. struct btrfs_key *key,
  2711. u64 dir, struct fs_path *name)
  2712. {
  2713. int ret;
  2714. struct find_ref_ctx ctx;
  2715. ctx.dir = dir;
  2716. ctx.name = name;
  2717. ctx.found_idx = -1;
  2718. ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
  2719. if (ret < 0)
  2720. return ret;
  2721. if (ctx.found_idx == -1)
  2722. return -ENOENT;
  2723. return ctx.found_idx;
  2724. }
  2725. static int __record_changed_new_ref(int num, u64 dir, int index,
  2726. struct fs_path *name,
  2727. void *ctx)
  2728. {
  2729. int ret;
  2730. struct send_ctx *sctx = ctx;
  2731. ret = find_iref(sctx->parent_root, sctx->right_path,
  2732. sctx->cmp_key, dir, name);
  2733. if (ret == -ENOENT)
  2734. ret = __record_new_ref(num, dir, index, name, sctx);
  2735. else if (ret > 0)
  2736. ret = 0;
  2737. return ret;
  2738. }
  2739. static int __record_changed_deleted_ref(int num, u64 dir, int index,
  2740. struct fs_path *name,
  2741. void *ctx)
  2742. {
  2743. int ret;
  2744. struct send_ctx *sctx = ctx;
  2745. ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
  2746. dir, name);
  2747. if (ret == -ENOENT)
  2748. ret = __record_deleted_ref(num, dir, index, name, sctx);
  2749. else if (ret > 0)
  2750. ret = 0;
  2751. return ret;
  2752. }
  2753. static int record_changed_ref(struct send_ctx *sctx)
  2754. {
  2755. int ret = 0;
  2756. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  2757. sctx->cmp_key, 0, __record_changed_new_ref, sctx);
  2758. if (ret < 0)
  2759. goto out;
  2760. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  2761. sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
  2762. if (ret < 0)
  2763. goto out;
  2764. ret = 0;
  2765. out:
  2766. return ret;
  2767. }
  2768. /*
  2769. * Record and process all refs at once. Needed when an inode changes the
  2770. * generation number, which means that it was deleted and recreated.
  2771. */
  2772. static int process_all_refs(struct send_ctx *sctx,
  2773. enum btrfs_compare_tree_result cmd)
  2774. {
  2775. int ret;
  2776. struct btrfs_root *root;
  2777. struct btrfs_path *path;
  2778. struct btrfs_key key;
  2779. struct btrfs_key found_key;
  2780. struct extent_buffer *eb;
  2781. int slot;
  2782. iterate_inode_ref_t cb;
  2783. path = alloc_path_for_send();
  2784. if (!path)
  2785. return -ENOMEM;
  2786. if (cmd == BTRFS_COMPARE_TREE_NEW) {
  2787. root = sctx->send_root;
  2788. cb = __record_new_ref;
  2789. } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
  2790. root = sctx->parent_root;
  2791. cb = __record_deleted_ref;
  2792. } else {
  2793. BUG();
  2794. }
  2795. key.objectid = sctx->cmp_key->objectid;
  2796. key.type = BTRFS_INODE_REF_KEY;
  2797. key.offset = 0;
  2798. while (1) {
  2799. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  2800. if (ret < 0)
  2801. goto out;
  2802. if (ret)
  2803. break;
  2804. eb = path->nodes[0];
  2805. slot = path->slots[0];
  2806. btrfs_item_key_to_cpu(eb, &found_key, slot);
  2807. if (found_key.objectid != key.objectid ||
  2808. (found_key.type != BTRFS_INODE_REF_KEY &&
  2809. found_key.type != BTRFS_INODE_EXTREF_KEY))
  2810. break;
  2811. ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
  2812. btrfs_release_path(path);
  2813. if (ret < 0)
  2814. goto out;
  2815. key.offset = found_key.offset + 1;
  2816. }
  2817. btrfs_release_path(path);
  2818. ret = process_recorded_refs(sctx);
  2819. out:
  2820. btrfs_free_path(path);
  2821. return ret;
  2822. }
  2823. static int send_set_xattr(struct send_ctx *sctx,
  2824. struct fs_path *path,
  2825. const char *name, int name_len,
  2826. const char *data, int data_len)
  2827. {
  2828. int ret = 0;
  2829. ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
  2830. if (ret < 0)
  2831. goto out;
  2832. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  2833. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  2834. TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
  2835. ret = send_cmd(sctx);
  2836. tlv_put_failure:
  2837. out:
  2838. return ret;
  2839. }
  2840. static int send_remove_xattr(struct send_ctx *sctx,
  2841. struct fs_path *path,
  2842. const char *name, int name_len)
  2843. {
  2844. int ret = 0;
  2845. ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
  2846. if (ret < 0)
  2847. goto out;
  2848. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  2849. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  2850. ret = send_cmd(sctx);
  2851. tlv_put_failure:
  2852. out:
  2853. return ret;
  2854. }
  2855. static int __process_new_xattr(int num, struct btrfs_key *di_key,
  2856. const char *name, int name_len,
  2857. const char *data, int data_len,
  2858. u8 type, void *ctx)
  2859. {
  2860. int ret;
  2861. struct send_ctx *sctx = ctx;
  2862. struct fs_path *p;
  2863. posix_acl_xattr_header dummy_acl;
  2864. p = fs_path_alloc();
  2865. if (!p)
  2866. return -ENOMEM;
  2867. /*
  2868. * This hack is needed because empty acl's are stored as zero byte
  2869. * data in xattrs. Problem with that is, that receiving these zero byte
  2870. * acl's will fail later. To fix this, we send a dummy acl list that
  2871. * only contains the version number and no entries.
  2872. */
  2873. if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
  2874. !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
  2875. if (data_len == 0) {
  2876. dummy_acl.a_version =
  2877. cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  2878. data = (char *)&dummy_acl;
  2879. data_len = sizeof(dummy_acl);
  2880. }
  2881. }
  2882. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  2883. if (ret < 0)
  2884. goto out;
  2885. ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
  2886. out:
  2887. fs_path_free(p);
  2888. return ret;
  2889. }
  2890. static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
  2891. const char *name, int name_len,
  2892. const char *data, int data_len,
  2893. u8 type, void *ctx)
  2894. {
  2895. int ret;
  2896. struct send_ctx *sctx = ctx;
  2897. struct fs_path *p;
  2898. p = fs_path_alloc();
  2899. if (!p)
  2900. return -ENOMEM;
  2901. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  2902. if (ret < 0)
  2903. goto out;
  2904. ret = send_remove_xattr(sctx, p, name, name_len);
  2905. out:
  2906. fs_path_free(p);
  2907. return ret;
  2908. }
  2909. static int process_new_xattr(struct send_ctx *sctx)
  2910. {
  2911. int ret = 0;
  2912. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  2913. sctx->cmp_key, __process_new_xattr, sctx);
  2914. return ret;
  2915. }
  2916. static int process_deleted_xattr(struct send_ctx *sctx)
  2917. {
  2918. int ret;
  2919. ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
  2920. sctx->cmp_key, __process_deleted_xattr, sctx);
  2921. return ret;
  2922. }
  2923. struct find_xattr_ctx {
  2924. const char *name;
  2925. int name_len;
  2926. int found_idx;
  2927. char *found_data;
  2928. int found_data_len;
  2929. };
  2930. static int __find_xattr(int num, struct btrfs_key *di_key,
  2931. const char *name, int name_len,
  2932. const char *data, int data_len,
  2933. u8 type, void *vctx)
  2934. {
  2935. struct find_xattr_ctx *ctx = vctx;
  2936. if (name_len == ctx->name_len &&
  2937. strncmp(name, ctx->name, name_len) == 0) {
  2938. ctx->found_idx = num;
  2939. ctx->found_data_len = data_len;
  2940. ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
  2941. if (!ctx->found_data)
  2942. return -ENOMEM;
  2943. return 1;
  2944. }
  2945. return 0;
  2946. }
  2947. static int find_xattr(struct btrfs_root *root,
  2948. struct btrfs_path *path,
  2949. struct btrfs_key *key,
  2950. const char *name, int name_len,
  2951. char **data, int *data_len)
  2952. {
  2953. int ret;
  2954. struct find_xattr_ctx ctx;
  2955. ctx.name = name;
  2956. ctx.name_len = name_len;
  2957. ctx.found_idx = -1;
  2958. ctx.found_data = NULL;
  2959. ctx.found_data_len = 0;
  2960. ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
  2961. if (ret < 0)
  2962. return ret;
  2963. if (ctx.found_idx == -1)
  2964. return -ENOENT;
  2965. if (data) {
  2966. *data = ctx.found_data;
  2967. *data_len = ctx.found_data_len;
  2968. } else {
  2969. kfree(ctx.found_data);
  2970. }
  2971. return ctx.found_idx;
  2972. }
  2973. static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
  2974. const char *name, int name_len,
  2975. const char *data, int data_len,
  2976. u8 type, void *ctx)
  2977. {
  2978. int ret;
  2979. struct send_ctx *sctx = ctx;
  2980. char *found_data = NULL;
  2981. int found_data_len = 0;
  2982. ret = find_xattr(sctx->parent_root, sctx->right_path,
  2983. sctx->cmp_key, name, name_len, &found_data,
  2984. &found_data_len);
  2985. if (ret == -ENOENT) {
  2986. ret = __process_new_xattr(num, di_key, name, name_len, data,
  2987. data_len, type, ctx);
  2988. } else if (ret >= 0) {
  2989. if (data_len != found_data_len ||
  2990. memcmp(data, found_data, data_len)) {
  2991. ret = __process_new_xattr(num, di_key, name, name_len,
  2992. data, data_len, type, ctx);
  2993. } else {
  2994. ret = 0;
  2995. }
  2996. }
  2997. kfree(found_data);
  2998. return ret;
  2999. }
  3000. static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
  3001. const char *name, int name_len,
  3002. const char *data, int data_len,
  3003. u8 type, void *ctx)
  3004. {
  3005. int ret;
  3006. struct send_ctx *sctx = ctx;
  3007. ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
  3008. name, name_len, NULL, NULL);
  3009. if (ret == -ENOENT)
  3010. ret = __process_deleted_xattr(num, di_key, name, name_len, data,
  3011. data_len, type, ctx);
  3012. else if (ret >= 0)
  3013. ret = 0;
  3014. return ret;
  3015. }
  3016. static int process_changed_xattr(struct send_ctx *sctx)
  3017. {
  3018. int ret = 0;
  3019. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  3020. sctx->cmp_key, __process_changed_new_xattr, sctx);
  3021. if (ret < 0)
  3022. goto out;
  3023. ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
  3024. sctx->cmp_key, __process_changed_deleted_xattr, sctx);
  3025. out:
  3026. return ret;
  3027. }
  3028. static int process_all_new_xattrs(struct send_ctx *sctx)
  3029. {
  3030. int ret;
  3031. struct btrfs_root *root;
  3032. struct btrfs_path *path;
  3033. struct btrfs_key key;
  3034. struct btrfs_key found_key;
  3035. struct extent_buffer *eb;
  3036. int slot;
  3037. path = alloc_path_for_send();
  3038. if (!path)
  3039. return -ENOMEM;
  3040. root = sctx->send_root;
  3041. key.objectid = sctx->cmp_key->objectid;
  3042. key.type = BTRFS_XATTR_ITEM_KEY;
  3043. key.offset = 0;
  3044. while (1) {
  3045. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  3046. if (ret < 0)
  3047. goto out;
  3048. if (ret) {
  3049. ret = 0;
  3050. goto out;
  3051. }
  3052. eb = path->nodes[0];
  3053. slot = path->slots[0];
  3054. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3055. if (found_key.objectid != key.objectid ||
  3056. found_key.type != key.type) {
  3057. ret = 0;
  3058. goto out;
  3059. }
  3060. ret = iterate_dir_item(root, path, &found_key,
  3061. __process_new_xattr, sctx);
  3062. if (ret < 0)
  3063. goto out;
  3064. btrfs_release_path(path);
  3065. key.offset = found_key.offset + 1;
  3066. }
  3067. out:
  3068. btrfs_free_path(path);
  3069. return ret;
  3070. }
  3071. /*
  3072. * Read some bytes from the current inode/file and send a write command to
  3073. * user space.
  3074. */
  3075. static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
  3076. {
  3077. int ret = 0;
  3078. struct fs_path *p;
  3079. loff_t pos = offset;
  3080. int num_read = 0;
  3081. mm_segment_t old_fs;
  3082. p = fs_path_alloc();
  3083. if (!p)
  3084. return -ENOMEM;
  3085. /*
  3086. * vfs normally only accepts user space buffers for security reasons.
  3087. * we only read from the file and also only provide the read_buf buffer
  3088. * to vfs. As this buffer does not come from a user space call, it's
  3089. * ok to temporary allow kernel space buffers.
  3090. */
  3091. old_fs = get_fs();
  3092. set_fs(KERNEL_DS);
  3093. verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
  3094. ret = open_cur_inode_file(sctx);
  3095. if (ret < 0)
  3096. goto out;
  3097. ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
  3098. if (ret < 0)
  3099. goto out;
  3100. num_read = ret;
  3101. if (!num_read)
  3102. goto out;
  3103. ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
  3104. if (ret < 0)
  3105. goto out;
  3106. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3107. if (ret < 0)
  3108. goto out;
  3109. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3110. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3111. TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
  3112. ret = send_cmd(sctx);
  3113. tlv_put_failure:
  3114. out:
  3115. fs_path_free(p);
  3116. set_fs(old_fs);
  3117. if (ret < 0)
  3118. return ret;
  3119. return num_read;
  3120. }
  3121. /*
  3122. * Send a clone command to user space.
  3123. */
  3124. static int send_clone(struct send_ctx *sctx,
  3125. u64 offset, u32 len,
  3126. struct clone_root *clone_root)
  3127. {
  3128. int ret = 0;
  3129. struct fs_path *p;
  3130. u64 gen;
  3131. verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
  3132. "clone_inode=%llu, clone_offset=%llu\n", offset, len,
  3133. clone_root->root->objectid, clone_root->ino,
  3134. clone_root->offset);
  3135. p = fs_path_alloc();
  3136. if (!p)
  3137. return -ENOMEM;
  3138. ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
  3139. if (ret < 0)
  3140. goto out;
  3141. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3142. if (ret < 0)
  3143. goto out;
  3144. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3145. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
  3146. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3147. if (clone_root->root == sctx->send_root) {
  3148. ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
  3149. &gen, NULL, NULL, NULL, NULL);
  3150. if (ret < 0)
  3151. goto out;
  3152. ret = get_cur_path(sctx, clone_root->ino, gen, p);
  3153. } else {
  3154. ret = get_inode_path(clone_root->root, clone_root->ino, p);
  3155. }
  3156. if (ret < 0)
  3157. goto out;
  3158. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  3159. clone_root->root->root_item.uuid);
  3160. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  3161. clone_root->root->root_item.ctransid);
  3162. TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
  3163. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
  3164. clone_root->offset);
  3165. ret = send_cmd(sctx);
  3166. tlv_put_failure:
  3167. out:
  3168. fs_path_free(p);
  3169. return ret;
  3170. }
  3171. /*
  3172. * Send an update extent command to user space.
  3173. */
  3174. static int send_update_extent(struct send_ctx *sctx,
  3175. u64 offset, u32 len)
  3176. {
  3177. int ret = 0;
  3178. struct fs_path *p;
  3179. p = fs_path_alloc();
  3180. if (!p)
  3181. return -ENOMEM;
  3182. ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
  3183. if (ret < 0)
  3184. goto out;
  3185. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3186. if (ret < 0)
  3187. goto out;
  3188. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3189. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3190. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
  3191. ret = send_cmd(sctx);
  3192. tlv_put_failure:
  3193. out:
  3194. fs_path_free(p);
  3195. return ret;
  3196. }
  3197. static int send_write_or_clone(struct send_ctx *sctx,
  3198. struct btrfs_path *path,
  3199. struct btrfs_key *key,
  3200. struct clone_root *clone_root)
  3201. {
  3202. int ret = 0;
  3203. struct btrfs_file_extent_item *ei;
  3204. u64 offset = key->offset;
  3205. u64 pos = 0;
  3206. u64 len;
  3207. u32 l;
  3208. u8 type;
  3209. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3210. struct btrfs_file_extent_item);
  3211. type = btrfs_file_extent_type(path->nodes[0], ei);
  3212. if (type == BTRFS_FILE_EXTENT_INLINE) {
  3213. len = btrfs_file_extent_inline_len(path->nodes[0], ei);
  3214. /*
  3215. * it is possible the inline item won't cover the whole page,
  3216. * but there may be items after this page. Make
  3217. * sure to send the whole thing
  3218. */
  3219. len = PAGE_CACHE_ALIGN(len);
  3220. } else {
  3221. len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
  3222. }
  3223. if (offset + len > sctx->cur_inode_size)
  3224. len = sctx->cur_inode_size - offset;
  3225. if (len == 0) {
  3226. ret = 0;
  3227. goto out;
  3228. }
  3229. if (clone_root) {
  3230. ret = send_clone(sctx, offset, len, clone_root);
  3231. } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
  3232. ret = send_update_extent(sctx, offset, len);
  3233. } else {
  3234. while (pos < len) {
  3235. l = len - pos;
  3236. if (l > BTRFS_SEND_READ_SIZE)
  3237. l = BTRFS_SEND_READ_SIZE;
  3238. ret = send_write(sctx, pos + offset, l);
  3239. if (ret < 0)
  3240. goto out;
  3241. if (!ret)
  3242. break;
  3243. pos += ret;
  3244. }
  3245. ret = 0;
  3246. }
  3247. out:
  3248. return ret;
  3249. }
  3250. static int is_extent_unchanged(struct send_ctx *sctx,
  3251. struct btrfs_path *left_path,
  3252. struct btrfs_key *ekey)
  3253. {
  3254. int ret = 0;
  3255. struct btrfs_key key;
  3256. struct btrfs_path *path = NULL;
  3257. struct extent_buffer *eb;
  3258. int slot;
  3259. struct btrfs_key found_key;
  3260. struct btrfs_file_extent_item *ei;
  3261. u64 left_disknr;
  3262. u64 right_disknr;
  3263. u64 left_offset;
  3264. u64 right_offset;
  3265. u64 left_offset_fixed;
  3266. u64 left_len;
  3267. u64 right_len;
  3268. u64 left_gen;
  3269. u64 right_gen;
  3270. u8 left_type;
  3271. u8 right_type;
  3272. path = alloc_path_for_send();
  3273. if (!path)
  3274. return -ENOMEM;
  3275. eb = left_path->nodes[0];
  3276. slot = left_path->slots[0];
  3277. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  3278. left_type = btrfs_file_extent_type(eb, ei);
  3279. if (left_type != BTRFS_FILE_EXTENT_REG) {
  3280. ret = 0;
  3281. goto out;
  3282. }
  3283. left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  3284. left_len = btrfs_file_extent_num_bytes(eb, ei);
  3285. left_offset = btrfs_file_extent_offset(eb, ei);
  3286. left_gen = btrfs_file_extent_generation(eb, ei);
  3287. /*
  3288. * Following comments will refer to these graphics. L is the left
  3289. * extents which we are checking at the moment. 1-8 are the right
  3290. * extents that we iterate.
  3291. *
  3292. * |-----L-----|
  3293. * |-1-|-2a-|-3-|-4-|-5-|-6-|
  3294. *
  3295. * |-----L-----|
  3296. * |--1--|-2b-|...(same as above)
  3297. *
  3298. * Alternative situation. Happens on files where extents got split.
  3299. * |-----L-----|
  3300. * |-----------7-----------|-6-|
  3301. *
  3302. * Alternative situation. Happens on files which got larger.
  3303. * |-----L-----|
  3304. * |-8-|
  3305. * Nothing follows after 8.
  3306. */
  3307. key.objectid = ekey->objectid;
  3308. key.type = BTRFS_EXTENT_DATA_KEY;
  3309. key.offset = ekey->offset;
  3310. ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
  3311. if (ret < 0)
  3312. goto out;
  3313. if (ret) {
  3314. ret = 0;
  3315. goto out;
  3316. }
  3317. /*
  3318. * Handle special case where the right side has no extents at all.
  3319. */
  3320. eb = path->nodes[0];
  3321. slot = path->slots[0];
  3322. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3323. if (found_key.objectid != key.objectid ||
  3324. found_key.type != key.type) {
  3325. ret = 0;
  3326. goto out;
  3327. }
  3328. /*
  3329. * We're now on 2a, 2b or 7.
  3330. */
  3331. key = found_key;
  3332. while (key.offset < ekey->offset + left_len) {
  3333. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  3334. right_type = btrfs_file_extent_type(eb, ei);
  3335. right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  3336. right_len = btrfs_file_extent_num_bytes(eb, ei);
  3337. right_offset = btrfs_file_extent_offset(eb, ei);
  3338. right_gen = btrfs_file_extent_generation(eb, ei);
  3339. if (right_type != BTRFS_FILE_EXTENT_REG) {
  3340. ret = 0;
  3341. goto out;
  3342. }
  3343. /*
  3344. * Are we at extent 8? If yes, we know the extent is changed.
  3345. * This may only happen on the first iteration.
  3346. */
  3347. if (found_key.offset + right_len <= ekey->offset) {
  3348. ret = 0;
  3349. goto out;
  3350. }
  3351. left_offset_fixed = left_offset;
  3352. if (key.offset < ekey->offset) {
  3353. /* Fix the right offset for 2a and 7. */
  3354. right_offset += ekey->offset - key.offset;
  3355. } else {
  3356. /* Fix the left offset for all behind 2a and 2b */
  3357. left_offset_fixed += key.offset - ekey->offset;
  3358. }
  3359. /*
  3360. * Check if we have the same extent.
  3361. */
  3362. if (left_disknr != right_disknr ||
  3363. left_offset_fixed != right_offset ||
  3364. left_gen != right_gen) {
  3365. ret = 0;
  3366. goto out;
  3367. }
  3368. /*
  3369. * Go to the next extent.
  3370. */
  3371. ret = btrfs_next_item(sctx->parent_root, path);
  3372. if (ret < 0)
  3373. goto out;
  3374. if (!ret) {
  3375. eb = path->nodes[0];
  3376. slot = path->slots[0];
  3377. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3378. }
  3379. if (ret || found_key.objectid != key.objectid ||
  3380. found_key.type != key.type) {
  3381. key.offset += right_len;
  3382. break;
  3383. }
  3384. if (found_key.offset != key.offset + right_len) {
  3385. ret = 0;
  3386. goto out;
  3387. }
  3388. key = found_key;
  3389. }
  3390. /*
  3391. * We're now behind the left extent (treat as unchanged) or at the end
  3392. * of the right side (treat as changed).
  3393. */
  3394. if (key.offset >= ekey->offset + left_len)
  3395. ret = 1;
  3396. else
  3397. ret = 0;
  3398. out:
  3399. btrfs_free_path(path);
  3400. return ret;
  3401. }
  3402. static int process_extent(struct send_ctx *sctx,
  3403. struct btrfs_path *path,
  3404. struct btrfs_key *key)
  3405. {
  3406. int ret = 0;
  3407. struct clone_root *found_clone = NULL;
  3408. if (S_ISLNK(sctx->cur_inode_mode))
  3409. return 0;
  3410. if (sctx->parent_root && !sctx->cur_inode_new) {
  3411. ret = is_extent_unchanged(sctx, path, key);
  3412. if (ret < 0)
  3413. goto out;
  3414. if (ret) {
  3415. ret = 0;
  3416. goto out;
  3417. }
  3418. }
  3419. ret = find_extent_clone(sctx, path, key->objectid, key->offset,
  3420. sctx->cur_inode_size, &found_clone);
  3421. if (ret != -ENOENT && ret < 0)
  3422. goto out;
  3423. ret = send_write_or_clone(sctx, path, key, found_clone);
  3424. out:
  3425. return ret;
  3426. }
  3427. static int process_all_extents(struct send_ctx *sctx)
  3428. {
  3429. int ret;
  3430. struct btrfs_root *root;
  3431. struct btrfs_path *path;
  3432. struct btrfs_key key;
  3433. struct btrfs_key found_key;
  3434. struct extent_buffer *eb;
  3435. int slot;
  3436. root = sctx->send_root;
  3437. path = alloc_path_for_send();
  3438. if (!path)
  3439. return -ENOMEM;
  3440. key.objectid = sctx->cmp_key->objectid;
  3441. key.type = BTRFS_EXTENT_DATA_KEY;
  3442. key.offset = 0;
  3443. while (1) {
  3444. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  3445. if (ret < 0)
  3446. goto out;
  3447. if (ret) {
  3448. ret = 0;
  3449. goto out;
  3450. }
  3451. eb = path->nodes[0];
  3452. slot = path->slots[0];
  3453. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3454. if (found_key.objectid != key.objectid ||
  3455. found_key.type != key.type) {
  3456. ret = 0;
  3457. goto out;
  3458. }
  3459. ret = process_extent(sctx, path, &found_key);
  3460. if (ret < 0)
  3461. goto out;
  3462. btrfs_release_path(path);
  3463. key.offset = found_key.offset + 1;
  3464. }
  3465. out:
  3466. btrfs_free_path(path);
  3467. return ret;
  3468. }
  3469. static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
  3470. {
  3471. int ret = 0;
  3472. if (sctx->cur_ino == 0)
  3473. goto out;
  3474. if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
  3475. sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
  3476. goto out;
  3477. if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
  3478. goto out;
  3479. ret = process_recorded_refs(sctx);
  3480. if (ret < 0)
  3481. goto out;
  3482. /*
  3483. * We have processed the refs and thus need to advance send_progress.
  3484. * Now, calls to get_cur_xxx will take the updated refs of the current
  3485. * inode into account.
  3486. */
  3487. sctx->send_progress = sctx->cur_ino + 1;
  3488. out:
  3489. return ret;
  3490. }
  3491. static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
  3492. {
  3493. int ret = 0;
  3494. u64 left_mode;
  3495. u64 left_uid;
  3496. u64 left_gid;
  3497. u64 right_mode;
  3498. u64 right_uid;
  3499. u64 right_gid;
  3500. int need_chmod = 0;
  3501. int need_chown = 0;
  3502. ret = process_recorded_refs_if_needed(sctx, at_end);
  3503. if (ret < 0)
  3504. goto out;
  3505. if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
  3506. goto out;
  3507. if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
  3508. goto out;
  3509. ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
  3510. &left_mode, &left_uid, &left_gid, NULL);
  3511. if (ret < 0)
  3512. goto out;
  3513. if (!sctx->parent_root || sctx->cur_inode_new) {
  3514. need_chown = 1;
  3515. if (!S_ISLNK(sctx->cur_inode_mode))
  3516. need_chmod = 1;
  3517. } else {
  3518. ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
  3519. NULL, NULL, &right_mode, &right_uid,
  3520. &right_gid, NULL);
  3521. if (ret < 0)
  3522. goto out;
  3523. if (left_uid != right_uid || left_gid != right_gid)
  3524. need_chown = 1;
  3525. if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
  3526. need_chmod = 1;
  3527. }
  3528. if (S_ISREG(sctx->cur_inode_mode)) {
  3529. ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3530. sctx->cur_inode_size);
  3531. if (ret < 0)
  3532. goto out;
  3533. }
  3534. if (need_chown) {
  3535. ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3536. left_uid, left_gid);
  3537. if (ret < 0)
  3538. goto out;
  3539. }
  3540. if (need_chmod) {
  3541. ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3542. left_mode);
  3543. if (ret < 0)
  3544. goto out;
  3545. }
  3546. /*
  3547. * Need to send that every time, no matter if it actually changed
  3548. * between the two trees as we have done changes to the inode before.
  3549. */
  3550. ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
  3551. if (ret < 0)
  3552. goto out;
  3553. out:
  3554. return ret;
  3555. }
  3556. static int changed_inode(struct send_ctx *sctx,
  3557. enum btrfs_compare_tree_result result)
  3558. {
  3559. int ret = 0;
  3560. struct btrfs_key *key = sctx->cmp_key;
  3561. struct btrfs_inode_item *left_ii = NULL;
  3562. struct btrfs_inode_item *right_ii = NULL;
  3563. u64 left_gen = 0;
  3564. u64 right_gen = 0;
  3565. ret = close_cur_inode_file(sctx);
  3566. if (ret < 0)
  3567. goto out;
  3568. sctx->cur_ino = key->objectid;
  3569. sctx->cur_inode_new_gen = 0;
  3570. /*
  3571. * Set send_progress to current inode. This will tell all get_cur_xxx
  3572. * functions that the current inode's refs are not updated yet. Later,
  3573. * when process_recorded_refs is finished, it is set to cur_ino + 1.
  3574. */
  3575. sctx->send_progress = sctx->cur_ino;
  3576. if (result == BTRFS_COMPARE_TREE_NEW ||
  3577. result == BTRFS_COMPARE_TREE_CHANGED) {
  3578. left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
  3579. sctx->left_path->slots[0],
  3580. struct btrfs_inode_item);
  3581. left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
  3582. left_ii);
  3583. } else {
  3584. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  3585. sctx->right_path->slots[0],
  3586. struct btrfs_inode_item);
  3587. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  3588. right_ii);
  3589. }
  3590. if (result == BTRFS_COMPARE_TREE_CHANGED) {
  3591. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  3592. sctx->right_path->slots[0],
  3593. struct btrfs_inode_item);
  3594. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  3595. right_ii);
  3596. /*
  3597. * The cur_ino = root dir case is special here. We can't treat
  3598. * the inode as deleted+reused because it would generate a
  3599. * stream that tries to delete/mkdir the root dir.
  3600. */
  3601. if (left_gen != right_gen &&
  3602. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  3603. sctx->cur_inode_new_gen = 1;
  3604. }
  3605. if (result == BTRFS_COMPARE_TREE_NEW) {
  3606. sctx->cur_inode_gen = left_gen;
  3607. sctx->cur_inode_new = 1;
  3608. sctx->cur_inode_deleted = 0;
  3609. sctx->cur_inode_size = btrfs_inode_size(
  3610. sctx->left_path->nodes[0], left_ii);
  3611. sctx->cur_inode_mode = btrfs_inode_mode(
  3612. sctx->left_path->nodes[0], left_ii);
  3613. if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  3614. ret = send_create_inode_if_needed(sctx);
  3615. } else if (result == BTRFS_COMPARE_TREE_DELETED) {
  3616. sctx->cur_inode_gen = right_gen;
  3617. sctx->cur_inode_new = 0;
  3618. sctx->cur_inode_deleted = 1;
  3619. sctx->cur_inode_size = btrfs_inode_size(
  3620. sctx->right_path->nodes[0], right_ii);
  3621. sctx->cur_inode_mode = btrfs_inode_mode(
  3622. sctx->right_path->nodes[0], right_ii);
  3623. } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
  3624. /*
  3625. * We need to do some special handling in case the inode was
  3626. * reported as changed with a changed generation number. This
  3627. * means that the original inode was deleted and new inode
  3628. * reused the same inum. So we have to treat the old inode as
  3629. * deleted and the new one as new.
  3630. */
  3631. if (sctx->cur_inode_new_gen) {
  3632. /*
  3633. * First, process the inode as if it was deleted.
  3634. */
  3635. sctx->cur_inode_gen = right_gen;
  3636. sctx->cur_inode_new = 0;
  3637. sctx->cur_inode_deleted = 1;
  3638. sctx->cur_inode_size = btrfs_inode_size(
  3639. sctx->right_path->nodes[0], right_ii);
  3640. sctx->cur_inode_mode = btrfs_inode_mode(
  3641. sctx->right_path->nodes[0], right_ii);
  3642. ret = process_all_refs(sctx,
  3643. BTRFS_COMPARE_TREE_DELETED);
  3644. if (ret < 0)
  3645. goto out;
  3646. /*
  3647. * Now process the inode as if it was new.
  3648. */
  3649. sctx->cur_inode_gen = left_gen;
  3650. sctx->cur_inode_new = 1;
  3651. sctx->cur_inode_deleted = 0;
  3652. sctx->cur_inode_size = btrfs_inode_size(
  3653. sctx->left_path->nodes[0], left_ii);
  3654. sctx->cur_inode_mode = btrfs_inode_mode(
  3655. sctx->left_path->nodes[0], left_ii);
  3656. ret = send_create_inode_if_needed(sctx);
  3657. if (ret < 0)
  3658. goto out;
  3659. ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
  3660. if (ret < 0)
  3661. goto out;
  3662. /*
  3663. * Advance send_progress now as we did not get into
  3664. * process_recorded_refs_if_needed in the new_gen case.
  3665. */
  3666. sctx->send_progress = sctx->cur_ino + 1;
  3667. /*
  3668. * Now process all extents and xattrs of the inode as if
  3669. * they were all new.
  3670. */
  3671. ret = process_all_extents(sctx);
  3672. if (ret < 0)
  3673. goto out;
  3674. ret = process_all_new_xattrs(sctx);
  3675. if (ret < 0)
  3676. goto out;
  3677. } else {
  3678. sctx->cur_inode_gen = left_gen;
  3679. sctx->cur_inode_new = 0;
  3680. sctx->cur_inode_new_gen = 0;
  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. }
  3687. }
  3688. out:
  3689. return ret;
  3690. }
  3691. /*
  3692. * We have to process new refs before deleted refs, but compare_trees gives us
  3693. * the new and deleted refs mixed. To fix this, we record the new/deleted refs
  3694. * first and later process them in process_recorded_refs.
  3695. * For the cur_inode_new_gen case, we skip recording completely because
  3696. * changed_inode did already initiate processing of refs. The reason for this is
  3697. * that in this case, compare_tree actually compares the refs of 2 different
  3698. * inodes. To fix this, process_all_refs is used in changed_inode to handle all
  3699. * refs of the right tree as deleted and all refs of the left tree as new.
  3700. */
  3701. static int changed_ref(struct send_ctx *sctx,
  3702. enum btrfs_compare_tree_result result)
  3703. {
  3704. int ret = 0;
  3705. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  3706. if (!sctx->cur_inode_new_gen &&
  3707. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
  3708. if (result == BTRFS_COMPARE_TREE_NEW)
  3709. ret = record_new_ref(sctx);
  3710. else if (result == BTRFS_COMPARE_TREE_DELETED)
  3711. ret = record_deleted_ref(sctx);
  3712. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  3713. ret = record_changed_ref(sctx);
  3714. }
  3715. return ret;
  3716. }
  3717. /*
  3718. * Process new/deleted/changed xattrs. We skip processing in the
  3719. * cur_inode_new_gen case because changed_inode did already initiate processing
  3720. * of xattrs. The reason is the same as in changed_ref
  3721. */
  3722. static int changed_xattr(struct send_ctx *sctx,
  3723. enum btrfs_compare_tree_result result)
  3724. {
  3725. int ret = 0;
  3726. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  3727. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  3728. if (result == BTRFS_COMPARE_TREE_NEW)
  3729. ret = process_new_xattr(sctx);
  3730. else if (result == BTRFS_COMPARE_TREE_DELETED)
  3731. ret = process_deleted_xattr(sctx);
  3732. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  3733. ret = process_changed_xattr(sctx);
  3734. }
  3735. return ret;
  3736. }
  3737. /*
  3738. * Process new/deleted/changed extents. We skip processing in the
  3739. * cur_inode_new_gen case because changed_inode did already initiate processing
  3740. * of extents. The reason is the same as in changed_ref
  3741. */
  3742. static int changed_extent(struct send_ctx *sctx,
  3743. enum btrfs_compare_tree_result result)
  3744. {
  3745. int ret = 0;
  3746. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  3747. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  3748. if (result != BTRFS_COMPARE_TREE_DELETED)
  3749. ret = process_extent(sctx, sctx->left_path,
  3750. sctx->cmp_key);
  3751. }
  3752. return ret;
  3753. }
  3754. /*
  3755. * Updates compare related fields in sctx and simply forwards to the actual
  3756. * changed_xxx functions.
  3757. */
  3758. static int changed_cb(struct btrfs_root *left_root,
  3759. struct btrfs_root *right_root,
  3760. struct btrfs_path *left_path,
  3761. struct btrfs_path *right_path,
  3762. struct btrfs_key *key,
  3763. enum btrfs_compare_tree_result result,
  3764. void *ctx)
  3765. {
  3766. int ret = 0;
  3767. struct send_ctx *sctx = ctx;
  3768. sctx->left_path = left_path;
  3769. sctx->right_path = right_path;
  3770. sctx->cmp_key = key;
  3771. ret = finish_inode_if_needed(sctx, 0);
  3772. if (ret < 0)
  3773. goto out;
  3774. /* Ignore non-FS objects */
  3775. if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
  3776. key->objectid == BTRFS_FREE_SPACE_OBJECTID)
  3777. goto out;
  3778. if (key->type == BTRFS_INODE_ITEM_KEY)
  3779. ret = changed_inode(sctx, result);
  3780. else if (key->type == BTRFS_INODE_REF_KEY ||
  3781. key->type == BTRFS_INODE_EXTREF_KEY)
  3782. ret = changed_ref(sctx, result);
  3783. else if (key->type == BTRFS_XATTR_ITEM_KEY)
  3784. ret = changed_xattr(sctx, result);
  3785. else if (key->type == BTRFS_EXTENT_DATA_KEY)
  3786. ret = changed_extent(sctx, result);
  3787. out:
  3788. return ret;
  3789. }
  3790. static int full_send_tree(struct send_ctx *sctx)
  3791. {
  3792. int ret;
  3793. struct btrfs_trans_handle *trans = NULL;
  3794. struct btrfs_root *send_root = sctx->send_root;
  3795. struct btrfs_key key;
  3796. struct btrfs_key found_key;
  3797. struct btrfs_path *path;
  3798. struct extent_buffer *eb;
  3799. int slot;
  3800. u64 start_ctransid;
  3801. u64 ctransid;
  3802. path = alloc_path_for_send();
  3803. if (!path)
  3804. return -ENOMEM;
  3805. spin_lock(&send_root->root_item_lock);
  3806. start_ctransid = btrfs_root_ctransid(&send_root->root_item);
  3807. spin_unlock(&send_root->root_item_lock);
  3808. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  3809. key.type = BTRFS_INODE_ITEM_KEY;
  3810. key.offset = 0;
  3811. join_trans:
  3812. /*
  3813. * We need to make sure the transaction does not get committed
  3814. * while we do anything on commit roots. Join a transaction to prevent
  3815. * this.
  3816. */
  3817. trans = btrfs_join_transaction(send_root);
  3818. if (IS_ERR(trans)) {
  3819. ret = PTR_ERR(trans);
  3820. trans = NULL;
  3821. goto out;
  3822. }
  3823. /*
  3824. * Make sure the tree has not changed after re-joining. We detect this
  3825. * by comparing start_ctransid and ctransid. They should always match.
  3826. */
  3827. spin_lock(&send_root->root_item_lock);
  3828. ctransid = btrfs_root_ctransid(&send_root->root_item);
  3829. spin_unlock(&send_root->root_item_lock);
  3830. if (ctransid != start_ctransid) {
  3831. WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
  3832. "send was modified in between. This is "
  3833. "probably a bug.\n");
  3834. ret = -EIO;
  3835. goto out;
  3836. }
  3837. ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
  3838. if (ret < 0)
  3839. goto out;
  3840. if (ret)
  3841. goto out_finish;
  3842. while (1) {
  3843. /*
  3844. * When someone want to commit while we iterate, end the
  3845. * joined transaction and rejoin.
  3846. */
  3847. if (btrfs_should_end_transaction(trans, send_root)) {
  3848. ret = btrfs_end_transaction(trans, send_root);
  3849. trans = NULL;
  3850. if (ret < 0)
  3851. goto out;
  3852. btrfs_release_path(path);
  3853. goto join_trans;
  3854. }
  3855. eb = path->nodes[0];
  3856. slot = path->slots[0];
  3857. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3858. ret = changed_cb(send_root, NULL, path, NULL,
  3859. &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
  3860. if (ret < 0)
  3861. goto out;
  3862. key.objectid = found_key.objectid;
  3863. key.type = found_key.type;
  3864. key.offset = found_key.offset + 1;
  3865. ret = btrfs_next_item(send_root, path);
  3866. if (ret < 0)
  3867. goto out;
  3868. if (ret) {
  3869. ret = 0;
  3870. break;
  3871. }
  3872. }
  3873. out_finish:
  3874. ret = finish_inode_if_needed(sctx, 1);
  3875. out:
  3876. btrfs_free_path(path);
  3877. if (trans) {
  3878. if (!ret)
  3879. ret = btrfs_end_transaction(trans, send_root);
  3880. else
  3881. btrfs_end_transaction(trans, send_root);
  3882. }
  3883. return ret;
  3884. }
  3885. static int send_subvol(struct send_ctx *sctx)
  3886. {
  3887. int ret;
  3888. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
  3889. ret = send_header(sctx);
  3890. if (ret < 0)
  3891. goto out;
  3892. }
  3893. ret = send_subvol_begin(sctx);
  3894. if (ret < 0)
  3895. goto out;
  3896. if (sctx->parent_root) {
  3897. ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
  3898. changed_cb, sctx);
  3899. if (ret < 0)
  3900. goto out;
  3901. ret = finish_inode_if_needed(sctx, 1);
  3902. if (ret < 0)
  3903. goto out;
  3904. } else {
  3905. ret = full_send_tree(sctx);
  3906. if (ret < 0)
  3907. goto out;
  3908. }
  3909. out:
  3910. if (!ret)
  3911. ret = close_cur_inode_file(sctx);
  3912. else
  3913. close_cur_inode_file(sctx);
  3914. free_recorded_refs(sctx);
  3915. return ret;
  3916. }
  3917. long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
  3918. {
  3919. int ret = 0;
  3920. struct btrfs_root *send_root;
  3921. struct btrfs_root *clone_root;
  3922. struct btrfs_fs_info *fs_info;
  3923. struct btrfs_ioctl_send_args *arg = NULL;
  3924. struct btrfs_key key;
  3925. struct send_ctx *sctx = NULL;
  3926. u32 i;
  3927. u64 *clone_sources_tmp = NULL;
  3928. if (!capable(CAP_SYS_ADMIN))
  3929. return -EPERM;
  3930. send_root = BTRFS_I(file_inode(mnt_file))->root;
  3931. fs_info = send_root->fs_info;
  3932. /*
  3933. * This is done when we lookup the root, it should already be complete
  3934. * by the time we get here.
  3935. */
  3936. WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
  3937. /*
  3938. * If we just created this root we need to make sure that the orphan
  3939. * cleanup has been done and committed since we search the commit root,
  3940. * so check its commit root transid with our otransid and if they match
  3941. * commit the transaction to make sure everything is updated.
  3942. */
  3943. down_read(&send_root->fs_info->extent_commit_sem);
  3944. if (btrfs_header_generation(send_root->commit_root) ==
  3945. btrfs_root_otransid(&send_root->root_item)) {
  3946. struct btrfs_trans_handle *trans;
  3947. up_read(&send_root->fs_info->extent_commit_sem);
  3948. trans = btrfs_attach_transaction_barrier(send_root);
  3949. if (IS_ERR(trans)) {
  3950. if (PTR_ERR(trans) != -ENOENT) {
  3951. ret = PTR_ERR(trans);
  3952. goto out;
  3953. }
  3954. /* ENOENT means theres no transaction */
  3955. } else {
  3956. ret = btrfs_commit_transaction(trans, send_root);
  3957. if (ret)
  3958. goto out;
  3959. }
  3960. } else {
  3961. up_read(&send_root->fs_info->extent_commit_sem);
  3962. }
  3963. arg = memdup_user(arg_, sizeof(*arg));
  3964. if (IS_ERR(arg)) {
  3965. ret = PTR_ERR(arg);
  3966. arg = NULL;
  3967. goto out;
  3968. }
  3969. if (!access_ok(VERIFY_READ, arg->clone_sources,
  3970. sizeof(*arg->clone_sources *
  3971. arg->clone_sources_count))) {
  3972. ret = -EFAULT;
  3973. goto out;
  3974. }
  3975. if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
  3976. ret = -EINVAL;
  3977. goto out;
  3978. }
  3979. sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
  3980. if (!sctx) {
  3981. ret = -ENOMEM;
  3982. goto out;
  3983. }
  3984. INIT_LIST_HEAD(&sctx->new_refs);
  3985. INIT_LIST_HEAD(&sctx->deleted_refs);
  3986. INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
  3987. INIT_LIST_HEAD(&sctx->name_cache_list);
  3988. sctx->flags = arg->flags;
  3989. sctx->send_filp = fget(arg->send_fd);
  3990. if (!sctx->send_filp) {
  3991. ret = -EBADF;
  3992. goto out;
  3993. }
  3994. sctx->mnt = mnt_file->f_path.mnt;
  3995. sctx->send_root = send_root;
  3996. sctx->clone_roots_cnt = arg->clone_sources_count;
  3997. sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
  3998. sctx->send_buf = vmalloc(sctx->send_max_size);
  3999. if (!sctx->send_buf) {
  4000. ret = -ENOMEM;
  4001. goto out;
  4002. }
  4003. sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
  4004. if (!sctx->read_buf) {
  4005. ret = -ENOMEM;
  4006. goto out;
  4007. }
  4008. sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
  4009. (arg->clone_sources_count + 1));
  4010. if (!sctx->clone_roots) {
  4011. ret = -ENOMEM;
  4012. goto out;
  4013. }
  4014. if (arg->clone_sources_count) {
  4015. clone_sources_tmp = vmalloc(arg->clone_sources_count *
  4016. sizeof(*arg->clone_sources));
  4017. if (!clone_sources_tmp) {
  4018. ret = -ENOMEM;
  4019. goto out;
  4020. }
  4021. ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
  4022. arg->clone_sources_count *
  4023. sizeof(*arg->clone_sources));
  4024. if (ret) {
  4025. ret = -EFAULT;
  4026. goto out;
  4027. }
  4028. for (i = 0; i < arg->clone_sources_count; i++) {
  4029. key.objectid = clone_sources_tmp[i];
  4030. key.type = BTRFS_ROOT_ITEM_KEY;
  4031. key.offset = (u64)-1;
  4032. clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
  4033. if (IS_ERR(clone_root)) {
  4034. ret = PTR_ERR(clone_root);
  4035. goto out;
  4036. }
  4037. sctx->clone_roots[i].root = clone_root;
  4038. }
  4039. vfree(clone_sources_tmp);
  4040. clone_sources_tmp = NULL;
  4041. }
  4042. if (arg->parent_root) {
  4043. key.objectid = arg->parent_root;
  4044. key.type = BTRFS_ROOT_ITEM_KEY;
  4045. key.offset = (u64)-1;
  4046. sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
  4047. if (IS_ERR(sctx->parent_root)) {
  4048. ret = PTR_ERR(sctx->parent_root);
  4049. goto out;
  4050. }
  4051. }
  4052. /*
  4053. * Clones from send_root are allowed, but only if the clone source
  4054. * is behind the current send position. This is checked while searching
  4055. * for possible clone sources.
  4056. */
  4057. sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
  4058. /* We do a bsearch later */
  4059. sort(sctx->clone_roots, sctx->clone_roots_cnt,
  4060. sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
  4061. NULL);
  4062. ret = send_subvol(sctx);
  4063. if (ret < 0)
  4064. goto out;
  4065. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
  4066. ret = begin_cmd(sctx, BTRFS_SEND_C_END);
  4067. if (ret < 0)
  4068. goto out;
  4069. ret = send_cmd(sctx);
  4070. if (ret < 0)
  4071. goto out;
  4072. }
  4073. out:
  4074. kfree(arg);
  4075. vfree(clone_sources_tmp);
  4076. if (sctx) {
  4077. if (sctx->send_filp)
  4078. fput(sctx->send_filp);
  4079. vfree(sctx->clone_roots);
  4080. vfree(sctx->send_buf);
  4081. vfree(sctx->read_buf);
  4082. name_cache_free(sctx);
  4083. kfree(sctx);
  4084. }
  4085. return ret;
  4086. }