send.c 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734
  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(struct send_ctx *sctx)
  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(struct send_ctx *sctx)
  153. {
  154. struct fs_path *p;
  155. p = fs_path_alloc(sctx);
  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 send_ctx *sctx, 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. 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 send_ctx *sctx,
  634. struct btrfs_root *root, struct btrfs_path *path,
  635. struct btrfs_key *found_key, int resolve,
  636. iterate_inode_ref_t iterate, void *ctx)
  637. {
  638. struct extent_buffer *eb = path->nodes[0];
  639. struct btrfs_item *item;
  640. struct btrfs_inode_ref *iref;
  641. struct btrfs_inode_extref *extref;
  642. struct btrfs_path *tmp_path;
  643. struct fs_path *p;
  644. u32 cur = 0;
  645. u32 total;
  646. int slot = path->slots[0];
  647. u32 name_len;
  648. char *start;
  649. int ret = 0;
  650. int num = 0;
  651. int index;
  652. u64 dir;
  653. unsigned long name_off;
  654. unsigned long elem_size;
  655. unsigned long ptr;
  656. p = fs_path_alloc_reversed(sctx);
  657. if (!p)
  658. return -ENOMEM;
  659. tmp_path = alloc_path_for_send();
  660. if (!tmp_path) {
  661. fs_path_free(sctx, p);
  662. return -ENOMEM;
  663. }
  664. if (found_key->type == BTRFS_INODE_REF_KEY) {
  665. ptr = (unsigned long)btrfs_item_ptr(eb, slot,
  666. struct btrfs_inode_ref);
  667. item = btrfs_item_nr(eb, slot);
  668. total = btrfs_item_size(eb, item);
  669. elem_size = sizeof(*iref);
  670. } else {
  671. ptr = btrfs_item_ptr_offset(eb, slot);
  672. total = btrfs_item_size_nr(eb, slot);
  673. elem_size = sizeof(*extref);
  674. }
  675. while (cur < total) {
  676. fs_path_reset(p);
  677. if (found_key->type == BTRFS_INODE_REF_KEY) {
  678. iref = (struct btrfs_inode_ref *)(ptr + cur);
  679. name_len = btrfs_inode_ref_name_len(eb, iref);
  680. name_off = (unsigned long)(iref + 1);
  681. index = btrfs_inode_ref_index(eb, iref);
  682. dir = found_key->offset;
  683. } else {
  684. extref = (struct btrfs_inode_extref *)(ptr + cur);
  685. name_len = btrfs_inode_extref_name_len(eb, extref);
  686. name_off = (unsigned long)&extref->name;
  687. index = btrfs_inode_extref_index(eb, extref);
  688. dir = btrfs_inode_extref_parent(eb, extref);
  689. }
  690. if (resolve) {
  691. start = btrfs_ref_to_path(root, tmp_path, name_len,
  692. name_off, eb, dir,
  693. p->buf, p->buf_len);
  694. if (IS_ERR(start)) {
  695. ret = PTR_ERR(start);
  696. goto out;
  697. }
  698. if (start < p->buf) {
  699. /* overflow , try again with larger buffer */
  700. ret = fs_path_ensure_buf(p,
  701. p->buf_len + p->buf - start);
  702. if (ret < 0)
  703. goto out;
  704. start = btrfs_ref_to_path(root, tmp_path,
  705. name_len, name_off,
  706. eb, dir,
  707. p->buf, p->buf_len);
  708. if (IS_ERR(start)) {
  709. ret = PTR_ERR(start);
  710. goto out;
  711. }
  712. BUG_ON(start < p->buf);
  713. }
  714. p->start = start;
  715. } else {
  716. ret = fs_path_add_from_extent_buffer(p, eb, name_off,
  717. name_len);
  718. if (ret < 0)
  719. goto out;
  720. }
  721. cur += elem_size + name_len;
  722. ret = iterate(num, dir, index, p, ctx);
  723. if (ret)
  724. goto out;
  725. num++;
  726. }
  727. out:
  728. btrfs_free_path(tmp_path);
  729. fs_path_free(sctx, p);
  730. return ret;
  731. }
  732. typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
  733. const char *name, int name_len,
  734. const char *data, int data_len,
  735. u8 type, void *ctx);
  736. /*
  737. * Helper function to iterate the entries in ONE btrfs_dir_item.
  738. * The iterate callback may return a non zero value to stop iteration. This can
  739. * be a negative value for error codes or 1 to simply stop it.
  740. *
  741. * path must point to the dir item when called.
  742. */
  743. static int iterate_dir_item(struct send_ctx *sctx,
  744. struct btrfs_root *root, struct btrfs_path *path,
  745. struct btrfs_key *found_key,
  746. iterate_dir_item_t iterate, void *ctx)
  747. {
  748. int ret = 0;
  749. struct extent_buffer *eb;
  750. struct btrfs_item *item;
  751. struct btrfs_dir_item *di;
  752. struct btrfs_key di_key;
  753. char *buf = NULL;
  754. char *buf2 = NULL;
  755. int buf_len;
  756. int buf_virtual = 0;
  757. u32 name_len;
  758. u32 data_len;
  759. u32 cur;
  760. u32 len;
  761. u32 total;
  762. int slot;
  763. int num;
  764. u8 type;
  765. buf_len = PAGE_SIZE;
  766. buf = kmalloc(buf_len, GFP_NOFS);
  767. if (!buf) {
  768. ret = -ENOMEM;
  769. goto out;
  770. }
  771. eb = path->nodes[0];
  772. slot = path->slots[0];
  773. item = btrfs_item_nr(eb, slot);
  774. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  775. cur = 0;
  776. len = 0;
  777. total = btrfs_item_size(eb, item);
  778. num = 0;
  779. while (cur < total) {
  780. name_len = btrfs_dir_name_len(eb, di);
  781. data_len = btrfs_dir_data_len(eb, di);
  782. type = btrfs_dir_type(eb, di);
  783. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  784. if (name_len + data_len > buf_len) {
  785. buf_len = PAGE_ALIGN(name_len + data_len);
  786. if (buf_virtual) {
  787. buf2 = vmalloc(buf_len);
  788. if (!buf2) {
  789. ret = -ENOMEM;
  790. goto out;
  791. }
  792. vfree(buf);
  793. } else {
  794. buf2 = krealloc(buf, buf_len, GFP_NOFS);
  795. if (!buf2) {
  796. buf2 = vmalloc(buf_len);
  797. if (!buf2) {
  798. ret = -ENOMEM;
  799. goto out;
  800. }
  801. kfree(buf);
  802. buf_virtual = 1;
  803. }
  804. }
  805. buf = buf2;
  806. buf2 = NULL;
  807. }
  808. read_extent_buffer(eb, buf, (unsigned long)(di + 1),
  809. name_len + data_len);
  810. len = sizeof(*di) + name_len + data_len;
  811. di = (struct btrfs_dir_item *)((char *)di + len);
  812. cur += len;
  813. ret = iterate(num, &di_key, buf, name_len, buf + name_len,
  814. data_len, type, ctx);
  815. if (ret < 0)
  816. goto out;
  817. if (ret) {
  818. ret = 0;
  819. goto out;
  820. }
  821. num++;
  822. }
  823. out:
  824. if (buf_virtual)
  825. vfree(buf);
  826. else
  827. kfree(buf);
  828. return ret;
  829. }
  830. static int __copy_first_ref(int num, u64 dir, int index,
  831. struct fs_path *p, void *ctx)
  832. {
  833. int ret;
  834. struct fs_path *pt = ctx;
  835. ret = fs_path_copy(pt, p);
  836. if (ret < 0)
  837. return ret;
  838. /* we want the first only */
  839. return 1;
  840. }
  841. /*
  842. * Retrieve the first path of an inode. If an inode has more then one
  843. * ref/hardlink, this is ignored.
  844. */
  845. static int get_inode_path(struct send_ctx *sctx, struct btrfs_root *root,
  846. u64 ino, struct fs_path *path)
  847. {
  848. int ret;
  849. struct btrfs_key key, found_key;
  850. struct btrfs_path *p;
  851. p = alloc_path_for_send();
  852. if (!p)
  853. return -ENOMEM;
  854. fs_path_reset(path);
  855. key.objectid = ino;
  856. key.type = BTRFS_INODE_REF_KEY;
  857. key.offset = 0;
  858. ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
  859. if (ret < 0)
  860. goto out;
  861. if (ret) {
  862. ret = 1;
  863. goto out;
  864. }
  865. btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
  866. if (found_key.objectid != ino ||
  867. (found_key.type != BTRFS_INODE_REF_KEY &&
  868. found_key.type != BTRFS_INODE_EXTREF_KEY)) {
  869. ret = -ENOENT;
  870. goto out;
  871. }
  872. ret = iterate_inode_ref(sctx, root, p, &found_key, 1,
  873. __copy_first_ref, path);
  874. if (ret < 0)
  875. goto out;
  876. ret = 0;
  877. out:
  878. btrfs_free_path(p);
  879. return ret;
  880. }
  881. struct backref_ctx {
  882. struct send_ctx *sctx;
  883. /* number of total found references */
  884. u64 found;
  885. /*
  886. * used for clones found in send_root. clones found behind cur_objectid
  887. * and cur_offset are not considered as allowed clones.
  888. */
  889. u64 cur_objectid;
  890. u64 cur_offset;
  891. /* may be truncated in case it's the last extent in a file */
  892. u64 extent_len;
  893. /* Just to check for bugs in backref resolving */
  894. int found_itself;
  895. };
  896. static int __clone_root_cmp_bsearch(const void *key, const void *elt)
  897. {
  898. u64 root = (u64)(uintptr_t)key;
  899. struct clone_root *cr = (struct clone_root *)elt;
  900. if (root < cr->root->objectid)
  901. return -1;
  902. if (root > cr->root->objectid)
  903. return 1;
  904. return 0;
  905. }
  906. static int __clone_root_cmp_sort(const void *e1, const void *e2)
  907. {
  908. struct clone_root *cr1 = (struct clone_root *)e1;
  909. struct clone_root *cr2 = (struct clone_root *)e2;
  910. if (cr1->root->objectid < cr2->root->objectid)
  911. return -1;
  912. if (cr1->root->objectid > cr2->root->objectid)
  913. return 1;
  914. return 0;
  915. }
  916. /*
  917. * Called for every backref that is found for the current extent.
  918. * Results are collected in sctx->clone_roots->ino/offset/found_refs
  919. */
  920. static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
  921. {
  922. struct backref_ctx *bctx = ctx_;
  923. struct clone_root *found;
  924. int ret;
  925. u64 i_size;
  926. /* First check if the root is in the list of accepted clone sources */
  927. found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
  928. bctx->sctx->clone_roots_cnt,
  929. sizeof(struct clone_root),
  930. __clone_root_cmp_bsearch);
  931. if (!found)
  932. return 0;
  933. if (found->root == bctx->sctx->send_root &&
  934. ino == bctx->cur_objectid &&
  935. offset == bctx->cur_offset) {
  936. bctx->found_itself = 1;
  937. }
  938. /*
  939. * There are inodes that have extents that lie behind its i_size. Don't
  940. * accept clones from these extents.
  941. */
  942. ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
  943. NULL);
  944. if (ret < 0)
  945. return ret;
  946. if (offset + bctx->extent_len > i_size)
  947. return 0;
  948. /*
  949. * Make sure we don't consider clones from send_root that are
  950. * behind the current inode/offset.
  951. */
  952. if (found->root == bctx->sctx->send_root) {
  953. /*
  954. * TODO for the moment we don't accept clones from the inode
  955. * that is currently send. We may change this when
  956. * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
  957. * file.
  958. */
  959. if (ino >= bctx->cur_objectid)
  960. return 0;
  961. #if 0
  962. if (ino > bctx->cur_objectid)
  963. return 0;
  964. if (offset + bctx->extent_len > bctx->cur_offset)
  965. return 0;
  966. #endif
  967. }
  968. bctx->found++;
  969. found->found_refs++;
  970. if (ino < found->ino) {
  971. found->ino = ino;
  972. found->offset = offset;
  973. } else if (found->ino == ino) {
  974. /*
  975. * same extent found more then once in the same file.
  976. */
  977. if (found->offset > offset + bctx->extent_len)
  978. found->offset = offset;
  979. }
  980. return 0;
  981. }
  982. /*
  983. * Given an inode, offset and extent item, it finds a good clone for a clone
  984. * instruction. Returns -ENOENT when none could be found. The function makes
  985. * sure that the returned clone is usable at the point where sending is at the
  986. * moment. This means, that no clones are accepted which lie behind the current
  987. * inode+offset.
  988. *
  989. * path must point to the extent item when called.
  990. */
  991. static int find_extent_clone(struct send_ctx *sctx,
  992. struct btrfs_path *path,
  993. u64 ino, u64 data_offset,
  994. u64 ino_size,
  995. struct clone_root **found)
  996. {
  997. int ret;
  998. int extent_type;
  999. u64 logical;
  1000. u64 disk_byte;
  1001. u64 num_bytes;
  1002. u64 extent_item_pos;
  1003. u64 flags = 0;
  1004. struct btrfs_file_extent_item *fi;
  1005. struct extent_buffer *eb = path->nodes[0];
  1006. struct backref_ctx *backref_ctx = NULL;
  1007. struct clone_root *cur_clone_root;
  1008. struct btrfs_key found_key;
  1009. struct btrfs_path *tmp_path;
  1010. int compressed;
  1011. u32 i;
  1012. tmp_path = alloc_path_for_send();
  1013. if (!tmp_path)
  1014. return -ENOMEM;
  1015. backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
  1016. if (!backref_ctx) {
  1017. ret = -ENOMEM;
  1018. goto out;
  1019. }
  1020. if (data_offset >= ino_size) {
  1021. /*
  1022. * There may be extents that lie behind the file's size.
  1023. * I at least had this in combination with snapshotting while
  1024. * writing large files.
  1025. */
  1026. ret = 0;
  1027. goto out;
  1028. }
  1029. fi = btrfs_item_ptr(eb, path->slots[0],
  1030. struct btrfs_file_extent_item);
  1031. extent_type = btrfs_file_extent_type(eb, fi);
  1032. if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  1033. ret = -ENOENT;
  1034. goto out;
  1035. }
  1036. compressed = btrfs_file_extent_compression(eb, fi);
  1037. num_bytes = btrfs_file_extent_num_bytes(eb, fi);
  1038. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  1039. if (disk_byte == 0) {
  1040. ret = -ENOENT;
  1041. goto out;
  1042. }
  1043. logical = disk_byte + btrfs_file_extent_offset(eb, fi);
  1044. ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
  1045. &found_key, &flags);
  1046. btrfs_release_path(tmp_path);
  1047. if (ret < 0)
  1048. goto out;
  1049. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1050. ret = -EIO;
  1051. goto out;
  1052. }
  1053. /*
  1054. * Setup the clone roots.
  1055. */
  1056. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  1057. cur_clone_root = sctx->clone_roots + i;
  1058. cur_clone_root->ino = (u64)-1;
  1059. cur_clone_root->offset = 0;
  1060. cur_clone_root->found_refs = 0;
  1061. }
  1062. backref_ctx->sctx = sctx;
  1063. backref_ctx->found = 0;
  1064. backref_ctx->cur_objectid = ino;
  1065. backref_ctx->cur_offset = data_offset;
  1066. backref_ctx->found_itself = 0;
  1067. backref_ctx->extent_len = num_bytes;
  1068. /*
  1069. * The last extent of a file may be too large due to page alignment.
  1070. * We need to adjust extent_len in this case so that the checks in
  1071. * __iterate_backrefs work.
  1072. */
  1073. if (data_offset + num_bytes >= ino_size)
  1074. backref_ctx->extent_len = ino_size - data_offset;
  1075. /*
  1076. * Now collect all backrefs.
  1077. */
  1078. if (compressed == BTRFS_COMPRESS_NONE)
  1079. extent_item_pos = logical - found_key.objectid;
  1080. else
  1081. extent_item_pos = 0;
  1082. extent_item_pos = logical - found_key.objectid;
  1083. ret = iterate_extent_inodes(sctx->send_root->fs_info,
  1084. found_key.objectid, extent_item_pos, 1,
  1085. __iterate_backrefs, backref_ctx);
  1086. if (ret < 0)
  1087. goto out;
  1088. if (!backref_ctx->found_itself) {
  1089. /* found a bug in backref code? */
  1090. ret = -EIO;
  1091. printk(KERN_ERR "btrfs: ERROR did not find backref in "
  1092. "send_root. inode=%llu, offset=%llu, "
  1093. "disk_byte=%llu found extent=%llu\n",
  1094. ino, data_offset, disk_byte, found_key.objectid);
  1095. goto out;
  1096. }
  1097. verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
  1098. "ino=%llu, "
  1099. "num_bytes=%llu, logical=%llu\n",
  1100. data_offset, ino, num_bytes, logical);
  1101. if (!backref_ctx->found)
  1102. verbose_printk("btrfs: no clones found\n");
  1103. cur_clone_root = NULL;
  1104. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  1105. if (sctx->clone_roots[i].found_refs) {
  1106. if (!cur_clone_root)
  1107. cur_clone_root = sctx->clone_roots + i;
  1108. else if (sctx->clone_roots[i].root == sctx->send_root)
  1109. /* prefer clones from send_root over others */
  1110. cur_clone_root = sctx->clone_roots + i;
  1111. }
  1112. }
  1113. if (cur_clone_root) {
  1114. *found = cur_clone_root;
  1115. ret = 0;
  1116. } else {
  1117. ret = -ENOENT;
  1118. }
  1119. out:
  1120. btrfs_free_path(tmp_path);
  1121. kfree(backref_ctx);
  1122. return ret;
  1123. }
  1124. static int read_symlink(struct send_ctx *sctx,
  1125. struct btrfs_root *root,
  1126. u64 ino,
  1127. struct fs_path *dest)
  1128. {
  1129. int ret;
  1130. struct btrfs_path *path;
  1131. struct btrfs_key key;
  1132. struct btrfs_file_extent_item *ei;
  1133. u8 type;
  1134. u8 compression;
  1135. unsigned long off;
  1136. int len;
  1137. path = alloc_path_for_send();
  1138. if (!path)
  1139. return -ENOMEM;
  1140. key.objectid = ino;
  1141. key.type = BTRFS_EXTENT_DATA_KEY;
  1142. key.offset = 0;
  1143. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1144. if (ret < 0)
  1145. goto out;
  1146. BUG_ON(ret);
  1147. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1148. struct btrfs_file_extent_item);
  1149. type = btrfs_file_extent_type(path->nodes[0], ei);
  1150. compression = btrfs_file_extent_compression(path->nodes[0], ei);
  1151. BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
  1152. BUG_ON(compression);
  1153. off = btrfs_file_extent_inline_start(ei);
  1154. len = btrfs_file_extent_inline_len(path->nodes[0], ei);
  1155. ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
  1156. out:
  1157. btrfs_free_path(path);
  1158. return ret;
  1159. }
  1160. /*
  1161. * Helper function to generate a file name that is unique in the root of
  1162. * send_root and parent_root. This is used to generate names for orphan inodes.
  1163. */
  1164. static int gen_unique_name(struct send_ctx *sctx,
  1165. u64 ino, u64 gen,
  1166. struct fs_path *dest)
  1167. {
  1168. int ret = 0;
  1169. struct btrfs_path *path;
  1170. struct btrfs_dir_item *di;
  1171. char tmp[64];
  1172. int len;
  1173. u64 idx = 0;
  1174. path = alloc_path_for_send();
  1175. if (!path)
  1176. return -ENOMEM;
  1177. while (1) {
  1178. len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
  1179. ino, gen, idx);
  1180. if (len >= sizeof(tmp)) {
  1181. /* should really not happen */
  1182. ret = -EOVERFLOW;
  1183. goto out;
  1184. }
  1185. di = btrfs_lookup_dir_item(NULL, sctx->send_root,
  1186. path, BTRFS_FIRST_FREE_OBJECTID,
  1187. tmp, strlen(tmp), 0);
  1188. btrfs_release_path(path);
  1189. if (IS_ERR(di)) {
  1190. ret = PTR_ERR(di);
  1191. goto out;
  1192. }
  1193. if (di) {
  1194. /* not unique, try again */
  1195. idx++;
  1196. continue;
  1197. }
  1198. if (!sctx->parent_root) {
  1199. /* unique */
  1200. ret = 0;
  1201. break;
  1202. }
  1203. di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
  1204. path, BTRFS_FIRST_FREE_OBJECTID,
  1205. tmp, strlen(tmp), 0);
  1206. btrfs_release_path(path);
  1207. if (IS_ERR(di)) {
  1208. ret = PTR_ERR(di);
  1209. goto out;
  1210. }
  1211. if (di) {
  1212. /* not unique, try again */
  1213. idx++;
  1214. continue;
  1215. }
  1216. /* unique */
  1217. break;
  1218. }
  1219. ret = fs_path_add(dest, tmp, strlen(tmp));
  1220. out:
  1221. btrfs_free_path(path);
  1222. return ret;
  1223. }
  1224. enum inode_state {
  1225. inode_state_no_change,
  1226. inode_state_will_create,
  1227. inode_state_did_create,
  1228. inode_state_will_delete,
  1229. inode_state_did_delete,
  1230. };
  1231. static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
  1232. {
  1233. int ret;
  1234. int left_ret;
  1235. int right_ret;
  1236. u64 left_gen;
  1237. u64 right_gen;
  1238. ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
  1239. NULL, NULL);
  1240. if (ret < 0 && ret != -ENOENT)
  1241. goto out;
  1242. left_ret = ret;
  1243. if (!sctx->parent_root) {
  1244. right_ret = -ENOENT;
  1245. } else {
  1246. ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
  1247. NULL, NULL, NULL, NULL);
  1248. if (ret < 0 && ret != -ENOENT)
  1249. goto out;
  1250. right_ret = ret;
  1251. }
  1252. if (!left_ret && !right_ret) {
  1253. if (left_gen == gen && right_gen == gen) {
  1254. ret = inode_state_no_change;
  1255. } else if (left_gen == gen) {
  1256. if (ino < sctx->send_progress)
  1257. ret = inode_state_did_create;
  1258. else
  1259. ret = inode_state_will_create;
  1260. } else if (right_gen == gen) {
  1261. if (ino < sctx->send_progress)
  1262. ret = inode_state_did_delete;
  1263. else
  1264. ret = inode_state_will_delete;
  1265. } else {
  1266. ret = -ENOENT;
  1267. }
  1268. } else if (!left_ret) {
  1269. if (left_gen == gen) {
  1270. if (ino < sctx->send_progress)
  1271. ret = inode_state_did_create;
  1272. else
  1273. ret = inode_state_will_create;
  1274. } else {
  1275. ret = -ENOENT;
  1276. }
  1277. } else if (!right_ret) {
  1278. if (right_gen == gen) {
  1279. if (ino < sctx->send_progress)
  1280. ret = inode_state_did_delete;
  1281. else
  1282. ret = inode_state_will_delete;
  1283. } else {
  1284. ret = -ENOENT;
  1285. }
  1286. } else {
  1287. ret = -ENOENT;
  1288. }
  1289. out:
  1290. return ret;
  1291. }
  1292. static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
  1293. {
  1294. int ret;
  1295. ret = get_cur_inode_state(sctx, ino, gen);
  1296. if (ret < 0)
  1297. goto out;
  1298. if (ret == inode_state_no_change ||
  1299. ret == inode_state_did_create ||
  1300. ret == inode_state_will_delete)
  1301. ret = 1;
  1302. else
  1303. ret = 0;
  1304. out:
  1305. return ret;
  1306. }
  1307. /*
  1308. * Helper function to lookup a dir item in a dir.
  1309. */
  1310. static int lookup_dir_item_inode(struct btrfs_root *root,
  1311. u64 dir, const char *name, int name_len,
  1312. u64 *found_inode,
  1313. u8 *found_type)
  1314. {
  1315. int ret = 0;
  1316. struct btrfs_dir_item *di;
  1317. struct btrfs_key key;
  1318. struct btrfs_path *path;
  1319. path = alloc_path_for_send();
  1320. if (!path)
  1321. return -ENOMEM;
  1322. di = btrfs_lookup_dir_item(NULL, root, path,
  1323. dir, name, name_len, 0);
  1324. if (!di) {
  1325. ret = -ENOENT;
  1326. goto out;
  1327. }
  1328. if (IS_ERR(di)) {
  1329. ret = PTR_ERR(di);
  1330. goto out;
  1331. }
  1332. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
  1333. *found_inode = key.objectid;
  1334. *found_type = btrfs_dir_type(path->nodes[0], di);
  1335. out:
  1336. btrfs_free_path(path);
  1337. return ret;
  1338. }
  1339. /*
  1340. * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
  1341. * generation of the parent dir and the name of the dir entry.
  1342. */
  1343. static int get_first_ref(struct send_ctx *sctx,
  1344. struct btrfs_root *root, u64 ino,
  1345. u64 *dir, u64 *dir_gen, struct fs_path *name)
  1346. {
  1347. int ret;
  1348. struct btrfs_key key;
  1349. struct btrfs_key found_key;
  1350. struct btrfs_path *path;
  1351. int len;
  1352. u64 parent_dir;
  1353. path = alloc_path_for_send();
  1354. if (!path)
  1355. return -ENOMEM;
  1356. key.objectid = ino;
  1357. key.type = BTRFS_INODE_REF_KEY;
  1358. key.offset = 0;
  1359. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  1360. if (ret < 0)
  1361. goto out;
  1362. if (!ret)
  1363. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1364. path->slots[0]);
  1365. if (ret || found_key.objectid != ino ||
  1366. (found_key.type != BTRFS_INODE_REF_KEY &&
  1367. found_key.type != BTRFS_INODE_EXTREF_KEY)) {
  1368. ret = -ENOENT;
  1369. goto out;
  1370. }
  1371. if (key.type == BTRFS_INODE_REF_KEY) {
  1372. struct btrfs_inode_ref *iref;
  1373. iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1374. struct btrfs_inode_ref);
  1375. len = btrfs_inode_ref_name_len(path->nodes[0], iref);
  1376. ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
  1377. (unsigned long)(iref + 1),
  1378. len);
  1379. parent_dir = found_key.offset;
  1380. } else {
  1381. struct btrfs_inode_extref *extref;
  1382. extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1383. struct btrfs_inode_extref);
  1384. len = btrfs_inode_extref_name_len(path->nodes[0], extref);
  1385. ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
  1386. (unsigned long)&extref->name, len);
  1387. parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
  1388. }
  1389. if (ret < 0)
  1390. goto out;
  1391. btrfs_release_path(path);
  1392. ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
  1393. NULL, NULL);
  1394. if (ret < 0)
  1395. goto out;
  1396. *dir = parent_dir;
  1397. out:
  1398. btrfs_free_path(path);
  1399. return ret;
  1400. }
  1401. static int is_first_ref(struct send_ctx *sctx,
  1402. struct btrfs_root *root,
  1403. u64 ino, u64 dir,
  1404. const char *name, int name_len)
  1405. {
  1406. int ret;
  1407. struct fs_path *tmp_name;
  1408. u64 tmp_dir;
  1409. u64 tmp_dir_gen;
  1410. tmp_name = fs_path_alloc(sctx);
  1411. if (!tmp_name)
  1412. return -ENOMEM;
  1413. ret = get_first_ref(sctx, root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
  1414. if (ret < 0)
  1415. goto out;
  1416. if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
  1417. ret = 0;
  1418. goto out;
  1419. }
  1420. ret = !memcmp(tmp_name->start, name, name_len);
  1421. out:
  1422. fs_path_free(sctx, tmp_name);
  1423. return ret;
  1424. }
  1425. /*
  1426. * Used by process_recorded_refs to determine if a new ref would overwrite an
  1427. * already existing ref. In case it detects an overwrite, it returns the
  1428. * inode/gen in who_ino/who_gen.
  1429. * When an overwrite is detected, process_recorded_refs does proper orphanizing
  1430. * to make sure later references to the overwritten inode are possible.
  1431. * Orphanizing is however only required for the first ref of an inode.
  1432. * process_recorded_refs does an additional is_first_ref check to see if
  1433. * orphanizing is really required.
  1434. */
  1435. static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
  1436. const char *name, int name_len,
  1437. u64 *who_ino, u64 *who_gen)
  1438. {
  1439. int ret = 0;
  1440. u64 other_inode = 0;
  1441. u8 other_type = 0;
  1442. if (!sctx->parent_root)
  1443. goto out;
  1444. ret = is_inode_existent(sctx, dir, dir_gen);
  1445. if (ret <= 0)
  1446. goto out;
  1447. ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
  1448. &other_inode, &other_type);
  1449. if (ret < 0 && ret != -ENOENT)
  1450. goto out;
  1451. if (ret) {
  1452. ret = 0;
  1453. goto out;
  1454. }
  1455. /*
  1456. * Check if the overwritten ref was already processed. If yes, the ref
  1457. * was already unlinked/moved, so we can safely assume that we will not
  1458. * overwrite anything at this point in time.
  1459. */
  1460. if (other_inode > sctx->send_progress) {
  1461. ret = get_inode_info(sctx->parent_root, other_inode, NULL,
  1462. who_gen, NULL, NULL, NULL, NULL);
  1463. if (ret < 0)
  1464. goto out;
  1465. ret = 1;
  1466. *who_ino = other_inode;
  1467. } else {
  1468. ret = 0;
  1469. }
  1470. out:
  1471. return ret;
  1472. }
  1473. /*
  1474. * Checks if the ref was overwritten by an already processed inode. This is
  1475. * used by __get_cur_name_and_parent to find out if the ref was orphanized and
  1476. * thus the orphan name needs be used.
  1477. * process_recorded_refs also uses it to avoid unlinking of refs that were
  1478. * overwritten.
  1479. */
  1480. static int did_overwrite_ref(struct send_ctx *sctx,
  1481. u64 dir, u64 dir_gen,
  1482. u64 ino, u64 ino_gen,
  1483. const char *name, int name_len)
  1484. {
  1485. int ret = 0;
  1486. u64 gen;
  1487. u64 ow_inode;
  1488. u8 other_type;
  1489. if (!sctx->parent_root)
  1490. goto out;
  1491. ret = is_inode_existent(sctx, dir, dir_gen);
  1492. if (ret <= 0)
  1493. goto out;
  1494. /* check if the ref was overwritten by another ref */
  1495. ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
  1496. &ow_inode, &other_type);
  1497. if (ret < 0 && ret != -ENOENT)
  1498. goto out;
  1499. if (ret) {
  1500. /* was never and will never be overwritten */
  1501. ret = 0;
  1502. goto out;
  1503. }
  1504. ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
  1505. NULL, NULL);
  1506. if (ret < 0)
  1507. goto out;
  1508. if (ow_inode == ino && gen == ino_gen) {
  1509. ret = 0;
  1510. goto out;
  1511. }
  1512. /* we know that it is or will be overwritten. check this now */
  1513. if (ow_inode < sctx->send_progress)
  1514. ret = 1;
  1515. else
  1516. ret = 0;
  1517. out:
  1518. return ret;
  1519. }
  1520. /*
  1521. * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
  1522. * that got overwritten. This is used by process_recorded_refs to determine
  1523. * if it has to use the path as returned by get_cur_path or the orphan name.
  1524. */
  1525. static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
  1526. {
  1527. int ret = 0;
  1528. struct fs_path *name = NULL;
  1529. u64 dir;
  1530. u64 dir_gen;
  1531. if (!sctx->parent_root)
  1532. goto out;
  1533. name = fs_path_alloc(sctx);
  1534. if (!name)
  1535. return -ENOMEM;
  1536. ret = get_first_ref(sctx, sctx->parent_root, ino, &dir, &dir_gen, name);
  1537. if (ret < 0)
  1538. goto out;
  1539. ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
  1540. name->start, fs_path_len(name));
  1541. out:
  1542. fs_path_free(sctx, name);
  1543. return ret;
  1544. }
  1545. /*
  1546. * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
  1547. * so we need to do some special handling in case we have clashes. This function
  1548. * takes care of this with the help of name_cache_entry::radix_list.
  1549. * In case of error, nce is kfreed.
  1550. */
  1551. static int name_cache_insert(struct send_ctx *sctx,
  1552. struct name_cache_entry *nce)
  1553. {
  1554. int ret = 0;
  1555. struct list_head *nce_head;
  1556. nce_head = radix_tree_lookup(&sctx->name_cache,
  1557. (unsigned long)nce->ino);
  1558. if (!nce_head) {
  1559. nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
  1560. if (!nce_head) {
  1561. kfree(nce);
  1562. return -ENOMEM;
  1563. }
  1564. INIT_LIST_HEAD(nce_head);
  1565. ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
  1566. if (ret < 0) {
  1567. kfree(nce_head);
  1568. kfree(nce);
  1569. return ret;
  1570. }
  1571. }
  1572. list_add_tail(&nce->radix_list, nce_head);
  1573. list_add_tail(&nce->list, &sctx->name_cache_list);
  1574. sctx->name_cache_size++;
  1575. return ret;
  1576. }
  1577. static void name_cache_delete(struct send_ctx *sctx,
  1578. struct name_cache_entry *nce)
  1579. {
  1580. struct list_head *nce_head;
  1581. nce_head = radix_tree_lookup(&sctx->name_cache,
  1582. (unsigned long)nce->ino);
  1583. BUG_ON(!nce_head);
  1584. list_del(&nce->radix_list);
  1585. list_del(&nce->list);
  1586. sctx->name_cache_size--;
  1587. if (list_empty(nce_head)) {
  1588. radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
  1589. kfree(nce_head);
  1590. }
  1591. }
  1592. static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
  1593. u64 ino, u64 gen)
  1594. {
  1595. struct list_head *nce_head;
  1596. struct name_cache_entry *cur;
  1597. nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
  1598. if (!nce_head)
  1599. return NULL;
  1600. list_for_each_entry(cur, nce_head, radix_list) {
  1601. if (cur->ino == ino && cur->gen == gen)
  1602. return cur;
  1603. }
  1604. return NULL;
  1605. }
  1606. /*
  1607. * Removes the entry from the list and adds it back to the end. This marks the
  1608. * entry as recently used so that name_cache_clean_unused does not remove it.
  1609. */
  1610. static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
  1611. {
  1612. list_del(&nce->list);
  1613. list_add_tail(&nce->list, &sctx->name_cache_list);
  1614. }
  1615. /*
  1616. * Remove some entries from the beginning of name_cache_list.
  1617. */
  1618. static void name_cache_clean_unused(struct send_ctx *sctx)
  1619. {
  1620. struct name_cache_entry *nce;
  1621. if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
  1622. return;
  1623. while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
  1624. nce = list_entry(sctx->name_cache_list.next,
  1625. struct name_cache_entry, list);
  1626. name_cache_delete(sctx, nce);
  1627. kfree(nce);
  1628. }
  1629. }
  1630. static void name_cache_free(struct send_ctx *sctx)
  1631. {
  1632. struct name_cache_entry *nce;
  1633. while (!list_empty(&sctx->name_cache_list)) {
  1634. nce = list_entry(sctx->name_cache_list.next,
  1635. struct name_cache_entry, list);
  1636. name_cache_delete(sctx, nce);
  1637. kfree(nce);
  1638. }
  1639. }
  1640. /*
  1641. * Used by get_cur_path for each ref up to the root.
  1642. * Returns 0 if it succeeded.
  1643. * Returns 1 if the inode is not existent or got overwritten. In that case, the
  1644. * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
  1645. * is returned, parent_ino/parent_gen are not guaranteed to be valid.
  1646. * Returns <0 in case of error.
  1647. */
  1648. static int __get_cur_name_and_parent(struct send_ctx *sctx,
  1649. u64 ino, u64 gen,
  1650. u64 *parent_ino,
  1651. u64 *parent_gen,
  1652. struct fs_path *dest)
  1653. {
  1654. int ret;
  1655. int nce_ret;
  1656. struct btrfs_path *path = NULL;
  1657. struct name_cache_entry *nce = NULL;
  1658. /*
  1659. * First check if we already did a call to this function with the same
  1660. * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
  1661. * return the cached result.
  1662. */
  1663. nce = name_cache_search(sctx, ino, gen);
  1664. if (nce) {
  1665. if (ino < sctx->send_progress && nce->need_later_update) {
  1666. name_cache_delete(sctx, nce);
  1667. kfree(nce);
  1668. nce = NULL;
  1669. } else {
  1670. name_cache_used(sctx, nce);
  1671. *parent_ino = nce->parent_ino;
  1672. *parent_gen = nce->parent_gen;
  1673. ret = fs_path_add(dest, nce->name, nce->name_len);
  1674. if (ret < 0)
  1675. goto out;
  1676. ret = nce->ret;
  1677. goto out;
  1678. }
  1679. }
  1680. path = alloc_path_for_send();
  1681. if (!path)
  1682. return -ENOMEM;
  1683. /*
  1684. * If the inode is not existent yet, add the orphan name and return 1.
  1685. * This should only happen for the parent dir that we determine in
  1686. * __record_new_ref
  1687. */
  1688. ret = is_inode_existent(sctx, ino, gen);
  1689. if (ret < 0)
  1690. goto out;
  1691. if (!ret) {
  1692. ret = gen_unique_name(sctx, ino, gen, dest);
  1693. if (ret < 0)
  1694. goto out;
  1695. ret = 1;
  1696. goto out_cache;
  1697. }
  1698. /*
  1699. * Depending on whether the inode was already processed or not, use
  1700. * send_root or parent_root for ref lookup.
  1701. */
  1702. if (ino < sctx->send_progress)
  1703. ret = get_first_ref(sctx, sctx->send_root, ino,
  1704. parent_ino, parent_gen, dest);
  1705. else
  1706. ret = get_first_ref(sctx, sctx->parent_root, ino,
  1707. parent_ino, parent_gen, dest);
  1708. if (ret < 0)
  1709. goto out;
  1710. /*
  1711. * Check if the ref was overwritten by an inode's ref that was processed
  1712. * earlier. If yes, treat as orphan and return 1.
  1713. */
  1714. ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
  1715. dest->start, dest->end - dest->start);
  1716. if (ret < 0)
  1717. goto out;
  1718. if (ret) {
  1719. fs_path_reset(dest);
  1720. ret = gen_unique_name(sctx, ino, gen, dest);
  1721. if (ret < 0)
  1722. goto out;
  1723. ret = 1;
  1724. }
  1725. out_cache:
  1726. /*
  1727. * Store the result of the lookup in the name cache.
  1728. */
  1729. nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
  1730. if (!nce) {
  1731. ret = -ENOMEM;
  1732. goto out;
  1733. }
  1734. nce->ino = ino;
  1735. nce->gen = gen;
  1736. nce->parent_ino = *parent_ino;
  1737. nce->parent_gen = *parent_gen;
  1738. nce->name_len = fs_path_len(dest);
  1739. nce->ret = ret;
  1740. strcpy(nce->name, dest->start);
  1741. if (ino < sctx->send_progress)
  1742. nce->need_later_update = 0;
  1743. else
  1744. nce->need_later_update = 1;
  1745. nce_ret = name_cache_insert(sctx, nce);
  1746. if (nce_ret < 0)
  1747. ret = nce_ret;
  1748. name_cache_clean_unused(sctx);
  1749. out:
  1750. btrfs_free_path(path);
  1751. return ret;
  1752. }
  1753. /*
  1754. * Magic happens here. This function returns the first ref to an inode as it
  1755. * would look like while receiving the stream at this point in time.
  1756. * We walk the path up to the root. For every inode in between, we check if it
  1757. * was already processed/sent. If yes, we continue with the parent as found
  1758. * in send_root. If not, we continue with the parent as found in parent_root.
  1759. * If we encounter an inode that was deleted at this point in time, we use the
  1760. * inodes "orphan" name instead of the real name and stop. Same with new inodes
  1761. * that were not created yet and overwritten inodes/refs.
  1762. *
  1763. * When do we have have orphan inodes:
  1764. * 1. When an inode is freshly created and thus no valid refs are available yet
  1765. * 2. When a directory lost all it's refs (deleted) but still has dir items
  1766. * inside which were not processed yet (pending for move/delete). If anyone
  1767. * tried to get the path to the dir items, it would get a path inside that
  1768. * orphan directory.
  1769. * 3. When an inode is moved around or gets new links, it may overwrite the ref
  1770. * of an unprocessed inode. If in that case the first ref would be
  1771. * overwritten, the overwritten inode gets "orphanized". Later when we
  1772. * process this overwritten inode, it is restored at a new place by moving
  1773. * the orphan inode.
  1774. *
  1775. * sctx->send_progress tells this function at which point in time receiving
  1776. * would be.
  1777. */
  1778. static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
  1779. struct fs_path *dest)
  1780. {
  1781. int ret = 0;
  1782. struct fs_path *name = NULL;
  1783. u64 parent_inode = 0;
  1784. u64 parent_gen = 0;
  1785. int stop = 0;
  1786. name = fs_path_alloc(sctx);
  1787. if (!name) {
  1788. ret = -ENOMEM;
  1789. goto out;
  1790. }
  1791. dest->reversed = 1;
  1792. fs_path_reset(dest);
  1793. while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
  1794. fs_path_reset(name);
  1795. ret = __get_cur_name_and_parent(sctx, ino, gen,
  1796. &parent_inode, &parent_gen, name);
  1797. if (ret < 0)
  1798. goto out;
  1799. if (ret)
  1800. stop = 1;
  1801. ret = fs_path_add_path(dest, name);
  1802. if (ret < 0)
  1803. goto out;
  1804. ino = parent_inode;
  1805. gen = parent_gen;
  1806. }
  1807. out:
  1808. fs_path_free(sctx, name);
  1809. if (!ret)
  1810. fs_path_unreverse(dest);
  1811. return ret;
  1812. }
  1813. /*
  1814. * Called for regular files when sending extents data. Opens a struct file
  1815. * to read from the file.
  1816. */
  1817. static int open_cur_inode_file(struct send_ctx *sctx)
  1818. {
  1819. int ret = 0;
  1820. struct btrfs_key key;
  1821. struct path path;
  1822. struct inode *inode;
  1823. struct dentry *dentry;
  1824. struct file *filp;
  1825. int new = 0;
  1826. if (sctx->cur_inode_filp)
  1827. goto out;
  1828. key.objectid = sctx->cur_ino;
  1829. key.type = BTRFS_INODE_ITEM_KEY;
  1830. key.offset = 0;
  1831. inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
  1832. &new);
  1833. if (IS_ERR(inode)) {
  1834. ret = PTR_ERR(inode);
  1835. goto out;
  1836. }
  1837. dentry = d_obtain_alias(inode);
  1838. inode = NULL;
  1839. if (IS_ERR(dentry)) {
  1840. ret = PTR_ERR(dentry);
  1841. goto out;
  1842. }
  1843. path.mnt = sctx->mnt;
  1844. path.dentry = dentry;
  1845. filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
  1846. dput(dentry);
  1847. dentry = NULL;
  1848. if (IS_ERR(filp)) {
  1849. ret = PTR_ERR(filp);
  1850. goto out;
  1851. }
  1852. sctx->cur_inode_filp = filp;
  1853. out:
  1854. /*
  1855. * no xxxput required here as every vfs op
  1856. * does it by itself on failure
  1857. */
  1858. return ret;
  1859. }
  1860. /*
  1861. * Closes the struct file that was created in open_cur_inode_file
  1862. */
  1863. static int close_cur_inode_file(struct send_ctx *sctx)
  1864. {
  1865. int ret = 0;
  1866. if (!sctx->cur_inode_filp)
  1867. goto out;
  1868. ret = filp_close(sctx->cur_inode_filp, NULL);
  1869. sctx->cur_inode_filp = NULL;
  1870. out:
  1871. return ret;
  1872. }
  1873. /*
  1874. * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
  1875. */
  1876. static int send_subvol_begin(struct send_ctx *sctx)
  1877. {
  1878. int ret;
  1879. struct btrfs_root *send_root = sctx->send_root;
  1880. struct btrfs_root *parent_root = sctx->parent_root;
  1881. struct btrfs_path *path;
  1882. struct btrfs_key key;
  1883. struct btrfs_root_ref *ref;
  1884. struct extent_buffer *leaf;
  1885. char *name = NULL;
  1886. int namelen;
  1887. path = alloc_path_for_send();
  1888. if (!path)
  1889. return -ENOMEM;
  1890. name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
  1891. if (!name) {
  1892. btrfs_free_path(path);
  1893. return -ENOMEM;
  1894. }
  1895. key.objectid = send_root->objectid;
  1896. key.type = BTRFS_ROOT_BACKREF_KEY;
  1897. key.offset = 0;
  1898. ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
  1899. &key, path, 1, 0);
  1900. if (ret < 0)
  1901. goto out;
  1902. if (ret) {
  1903. ret = -ENOENT;
  1904. goto out;
  1905. }
  1906. leaf = path->nodes[0];
  1907. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1908. if (key.type != BTRFS_ROOT_BACKREF_KEY ||
  1909. key.objectid != send_root->objectid) {
  1910. ret = -ENOENT;
  1911. goto out;
  1912. }
  1913. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  1914. namelen = btrfs_root_ref_name_len(leaf, ref);
  1915. read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
  1916. btrfs_release_path(path);
  1917. if (parent_root) {
  1918. ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
  1919. if (ret < 0)
  1920. goto out;
  1921. } else {
  1922. ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
  1923. if (ret < 0)
  1924. goto out;
  1925. }
  1926. TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
  1927. TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
  1928. sctx->send_root->root_item.uuid);
  1929. TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
  1930. sctx->send_root->root_item.ctransid);
  1931. if (parent_root) {
  1932. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  1933. sctx->parent_root->root_item.uuid);
  1934. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  1935. sctx->parent_root->root_item.ctransid);
  1936. }
  1937. ret = send_cmd(sctx);
  1938. tlv_put_failure:
  1939. out:
  1940. btrfs_free_path(path);
  1941. kfree(name);
  1942. return ret;
  1943. }
  1944. static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
  1945. {
  1946. int ret = 0;
  1947. struct fs_path *p;
  1948. verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
  1949. p = fs_path_alloc(sctx);
  1950. if (!p)
  1951. return -ENOMEM;
  1952. ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
  1953. if (ret < 0)
  1954. goto out;
  1955. ret = get_cur_path(sctx, ino, gen, p);
  1956. if (ret < 0)
  1957. goto out;
  1958. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  1959. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
  1960. ret = send_cmd(sctx);
  1961. tlv_put_failure:
  1962. out:
  1963. fs_path_free(sctx, p);
  1964. return ret;
  1965. }
  1966. static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
  1967. {
  1968. int ret = 0;
  1969. struct fs_path *p;
  1970. verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
  1971. p = fs_path_alloc(sctx);
  1972. if (!p)
  1973. return -ENOMEM;
  1974. ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
  1975. if (ret < 0)
  1976. goto out;
  1977. ret = get_cur_path(sctx, ino, gen, p);
  1978. if (ret < 0)
  1979. goto out;
  1980. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  1981. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
  1982. ret = send_cmd(sctx);
  1983. tlv_put_failure:
  1984. out:
  1985. fs_path_free(sctx, p);
  1986. return ret;
  1987. }
  1988. static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
  1989. {
  1990. int ret = 0;
  1991. struct fs_path *p;
  1992. verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
  1993. p = fs_path_alloc(sctx);
  1994. if (!p)
  1995. return -ENOMEM;
  1996. ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
  1997. if (ret < 0)
  1998. goto out;
  1999. ret = get_cur_path(sctx, ino, gen, p);
  2000. if (ret < 0)
  2001. goto out;
  2002. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2003. TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
  2004. TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
  2005. ret = send_cmd(sctx);
  2006. tlv_put_failure:
  2007. out:
  2008. fs_path_free(sctx, p);
  2009. return ret;
  2010. }
  2011. static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
  2012. {
  2013. int ret = 0;
  2014. struct fs_path *p = NULL;
  2015. struct btrfs_inode_item *ii;
  2016. struct btrfs_path *path = NULL;
  2017. struct extent_buffer *eb;
  2018. struct btrfs_key key;
  2019. int slot;
  2020. verbose_printk("btrfs: send_utimes %llu\n", ino);
  2021. p = fs_path_alloc(sctx);
  2022. if (!p)
  2023. return -ENOMEM;
  2024. path = alloc_path_for_send();
  2025. if (!path) {
  2026. ret = -ENOMEM;
  2027. goto out;
  2028. }
  2029. key.objectid = ino;
  2030. key.type = BTRFS_INODE_ITEM_KEY;
  2031. key.offset = 0;
  2032. ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
  2033. if (ret < 0)
  2034. goto out;
  2035. eb = path->nodes[0];
  2036. slot = path->slots[0];
  2037. ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  2038. ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
  2039. if (ret < 0)
  2040. goto out;
  2041. ret = get_cur_path(sctx, ino, gen, p);
  2042. if (ret < 0)
  2043. goto out;
  2044. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2045. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
  2046. btrfs_inode_atime(ii));
  2047. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
  2048. btrfs_inode_mtime(ii));
  2049. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
  2050. btrfs_inode_ctime(ii));
  2051. /* TODO Add otime support when the otime patches get into upstream */
  2052. ret = send_cmd(sctx);
  2053. tlv_put_failure:
  2054. out:
  2055. fs_path_free(sctx, p);
  2056. btrfs_free_path(path);
  2057. return ret;
  2058. }
  2059. /*
  2060. * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
  2061. * a valid path yet because we did not process the refs yet. So, the inode
  2062. * is created as orphan.
  2063. */
  2064. static int send_create_inode(struct send_ctx *sctx, u64 ino)
  2065. {
  2066. int ret = 0;
  2067. struct fs_path *p;
  2068. int cmd;
  2069. u64 gen;
  2070. u64 mode;
  2071. u64 rdev;
  2072. verbose_printk("btrfs: send_create_inode %llu\n", ino);
  2073. p = fs_path_alloc(sctx);
  2074. if (!p)
  2075. return -ENOMEM;
  2076. ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
  2077. NULL, &rdev);
  2078. if (ret < 0)
  2079. goto out;
  2080. if (S_ISREG(mode)) {
  2081. cmd = BTRFS_SEND_C_MKFILE;
  2082. } else if (S_ISDIR(mode)) {
  2083. cmd = BTRFS_SEND_C_MKDIR;
  2084. } else if (S_ISLNK(mode)) {
  2085. cmd = BTRFS_SEND_C_SYMLINK;
  2086. } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
  2087. cmd = BTRFS_SEND_C_MKNOD;
  2088. } else if (S_ISFIFO(mode)) {
  2089. cmd = BTRFS_SEND_C_MKFIFO;
  2090. } else if (S_ISSOCK(mode)) {
  2091. cmd = BTRFS_SEND_C_MKSOCK;
  2092. } else {
  2093. printk(KERN_WARNING "btrfs: unexpected inode type %o",
  2094. (int)(mode & S_IFMT));
  2095. ret = -ENOTSUPP;
  2096. goto out;
  2097. }
  2098. ret = begin_cmd(sctx, cmd);
  2099. if (ret < 0)
  2100. goto out;
  2101. ret = gen_unique_name(sctx, ino, gen, p);
  2102. if (ret < 0)
  2103. goto out;
  2104. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2105. TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
  2106. if (S_ISLNK(mode)) {
  2107. fs_path_reset(p);
  2108. ret = read_symlink(sctx, sctx->send_root, ino, p);
  2109. if (ret < 0)
  2110. goto out;
  2111. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
  2112. } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
  2113. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  2114. TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
  2115. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
  2116. }
  2117. ret = send_cmd(sctx);
  2118. if (ret < 0)
  2119. goto out;
  2120. tlv_put_failure:
  2121. out:
  2122. fs_path_free(sctx, p);
  2123. return ret;
  2124. }
  2125. /*
  2126. * We need some special handling for inodes that get processed before the parent
  2127. * directory got created. See process_recorded_refs for details.
  2128. * This function does the check if we already created the dir out of order.
  2129. */
  2130. static int did_create_dir(struct send_ctx *sctx, u64 dir)
  2131. {
  2132. int ret = 0;
  2133. struct btrfs_path *path = NULL;
  2134. struct btrfs_key key;
  2135. struct btrfs_key found_key;
  2136. struct btrfs_key di_key;
  2137. struct extent_buffer *eb;
  2138. struct btrfs_dir_item *di;
  2139. int slot;
  2140. path = alloc_path_for_send();
  2141. if (!path) {
  2142. ret = -ENOMEM;
  2143. goto out;
  2144. }
  2145. key.objectid = dir;
  2146. key.type = BTRFS_DIR_INDEX_KEY;
  2147. key.offset = 0;
  2148. while (1) {
  2149. ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
  2150. 1, 0);
  2151. if (ret < 0)
  2152. goto out;
  2153. if (!ret) {
  2154. eb = path->nodes[0];
  2155. slot = path->slots[0];
  2156. btrfs_item_key_to_cpu(eb, &found_key, slot);
  2157. }
  2158. if (ret || found_key.objectid != key.objectid ||
  2159. found_key.type != key.type) {
  2160. ret = 0;
  2161. goto out;
  2162. }
  2163. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  2164. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  2165. if (di_key.objectid < sctx->send_progress) {
  2166. ret = 1;
  2167. goto out;
  2168. }
  2169. key.offset = found_key.offset + 1;
  2170. btrfs_release_path(path);
  2171. }
  2172. out:
  2173. btrfs_free_path(path);
  2174. return ret;
  2175. }
  2176. /*
  2177. * Only creates the inode if it is:
  2178. * 1. Not a directory
  2179. * 2. Or a directory which was not created already due to out of order
  2180. * directories. See did_create_dir and process_recorded_refs for details.
  2181. */
  2182. static int send_create_inode_if_needed(struct send_ctx *sctx)
  2183. {
  2184. int ret;
  2185. if (S_ISDIR(sctx->cur_inode_mode)) {
  2186. ret = did_create_dir(sctx, sctx->cur_ino);
  2187. if (ret < 0)
  2188. goto out;
  2189. if (ret) {
  2190. ret = 0;
  2191. goto out;
  2192. }
  2193. }
  2194. ret = send_create_inode(sctx, sctx->cur_ino);
  2195. if (ret < 0)
  2196. goto out;
  2197. out:
  2198. return ret;
  2199. }
  2200. struct recorded_ref {
  2201. struct list_head list;
  2202. char *dir_path;
  2203. char *name;
  2204. struct fs_path *full_path;
  2205. u64 dir;
  2206. u64 dir_gen;
  2207. int dir_path_len;
  2208. int name_len;
  2209. };
  2210. /*
  2211. * We need to process new refs before deleted refs, but compare_tree gives us
  2212. * everything mixed. So we first record all refs and later process them.
  2213. * This function is a helper to record one ref.
  2214. */
  2215. static int record_ref(struct list_head *head, u64 dir,
  2216. u64 dir_gen, struct fs_path *path)
  2217. {
  2218. struct recorded_ref *ref;
  2219. char *tmp;
  2220. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  2221. if (!ref)
  2222. return -ENOMEM;
  2223. ref->dir = dir;
  2224. ref->dir_gen = dir_gen;
  2225. ref->full_path = path;
  2226. tmp = strrchr(ref->full_path->start, '/');
  2227. if (!tmp) {
  2228. ref->name_len = ref->full_path->end - ref->full_path->start;
  2229. ref->name = ref->full_path->start;
  2230. ref->dir_path_len = 0;
  2231. ref->dir_path = ref->full_path->start;
  2232. } else {
  2233. tmp++;
  2234. ref->name_len = ref->full_path->end - tmp;
  2235. ref->name = tmp;
  2236. ref->dir_path = ref->full_path->start;
  2237. ref->dir_path_len = ref->full_path->end -
  2238. ref->full_path->start - 1 - ref->name_len;
  2239. }
  2240. list_add_tail(&ref->list, head);
  2241. return 0;
  2242. }
  2243. static void __free_recorded_refs(struct send_ctx *sctx, struct list_head *head)
  2244. {
  2245. struct recorded_ref *cur;
  2246. while (!list_empty(head)) {
  2247. cur = list_entry(head->next, struct recorded_ref, list);
  2248. fs_path_free(sctx, cur->full_path);
  2249. list_del(&cur->list);
  2250. kfree(cur);
  2251. }
  2252. }
  2253. static void free_recorded_refs(struct send_ctx *sctx)
  2254. {
  2255. __free_recorded_refs(sctx, &sctx->new_refs);
  2256. __free_recorded_refs(sctx, &sctx->deleted_refs);
  2257. }
  2258. /*
  2259. * Renames/moves a file/dir to its orphan name. Used when the first
  2260. * ref of an unprocessed inode gets overwritten and for all non empty
  2261. * directories.
  2262. */
  2263. static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
  2264. struct fs_path *path)
  2265. {
  2266. int ret;
  2267. struct fs_path *orphan;
  2268. orphan = fs_path_alloc(sctx);
  2269. if (!orphan)
  2270. return -ENOMEM;
  2271. ret = gen_unique_name(sctx, ino, gen, orphan);
  2272. if (ret < 0)
  2273. goto out;
  2274. ret = send_rename(sctx, path, orphan);
  2275. out:
  2276. fs_path_free(sctx, orphan);
  2277. return ret;
  2278. }
  2279. /*
  2280. * Returns 1 if a directory can be removed at this point in time.
  2281. * We check this by iterating all dir items and checking if the inode behind
  2282. * the dir item was already processed.
  2283. */
  2284. static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
  2285. {
  2286. int ret = 0;
  2287. struct btrfs_root *root = sctx->parent_root;
  2288. struct btrfs_path *path;
  2289. struct btrfs_key key;
  2290. struct btrfs_key found_key;
  2291. struct btrfs_key loc;
  2292. struct btrfs_dir_item *di;
  2293. /*
  2294. * Don't try to rmdir the top/root subvolume dir.
  2295. */
  2296. if (dir == BTRFS_FIRST_FREE_OBJECTID)
  2297. return 0;
  2298. path = alloc_path_for_send();
  2299. if (!path)
  2300. return -ENOMEM;
  2301. key.objectid = dir;
  2302. key.type = BTRFS_DIR_INDEX_KEY;
  2303. key.offset = 0;
  2304. while (1) {
  2305. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  2306. if (ret < 0)
  2307. goto out;
  2308. if (!ret) {
  2309. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2310. path->slots[0]);
  2311. }
  2312. if (ret || found_key.objectid != key.objectid ||
  2313. found_key.type != key.type) {
  2314. break;
  2315. }
  2316. di = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2317. struct btrfs_dir_item);
  2318. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
  2319. if (loc.objectid > send_progress) {
  2320. ret = 0;
  2321. goto out;
  2322. }
  2323. btrfs_release_path(path);
  2324. key.offset = found_key.offset + 1;
  2325. }
  2326. ret = 1;
  2327. out:
  2328. btrfs_free_path(path);
  2329. return ret;
  2330. }
  2331. /*
  2332. * This does all the move/link/unlink/rmdir magic.
  2333. */
  2334. static int process_recorded_refs(struct send_ctx *sctx)
  2335. {
  2336. int ret = 0;
  2337. struct recorded_ref *cur;
  2338. struct recorded_ref *cur2;
  2339. struct ulist *check_dirs = NULL;
  2340. struct ulist_iterator uit;
  2341. struct ulist_node *un;
  2342. struct fs_path *valid_path = NULL;
  2343. u64 ow_inode = 0;
  2344. u64 ow_gen;
  2345. int did_overwrite = 0;
  2346. int is_orphan = 0;
  2347. verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
  2348. /*
  2349. * This should never happen as the root dir always has the same ref
  2350. * which is always '..'
  2351. */
  2352. BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
  2353. valid_path = fs_path_alloc(sctx);
  2354. if (!valid_path) {
  2355. ret = -ENOMEM;
  2356. goto out;
  2357. }
  2358. check_dirs = ulist_alloc(GFP_NOFS);
  2359. if (!check_dirs) {
  2360. ret = -ENOMEM;
  2361. goto out;
  2362. }
  2363. /*
  2364. * First, check if the first ref of the current inode was overwritten
  2365. * before. If yes, we know that the current inode was already orphanized
  2366. * and thus use the orphan name. If not, we can use get_cur_path to
  2367. * get the path of the first ref as it would like while receiving at
  2368. * this point in time.
  2369. * New inodes are always orphan at the beginning, so force to use the
  2370. * orphan name in this case.
  2371. * The first ref is stored in valid_path and will be updated if it
  2372. * gets moved around.
  2373. */
  2374. if (!sctx->cur_inode_new) {
  2375. ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
  2376. sctx->cur_inode_gen);
  2377. if (ret < 0)
  2378. goto out;
  2379. if (ret)
  2380. did_overwrite = 1;
  2381. }
  2382. if (sctx->cur_inode_new || did_overwrite) {
  2383. ret = gen_unique_name(sctx, sctx->cur_ino,
  2384. sctx->cur_inode_gen, valid_path);
  2385. if (ret < 0)
  2386. goto out;
  2387. is_orphan = 1;
  2388. } else {
  2389. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  2390. valid_path);
  2391. if (ret < 0)
  2392. goto out;
  2393. }
  2394. list_for_each_entry(cur, &sctx->new_refs, list) {
  2395. /*
  2396. * We may have refs where the parent directory does not exist
  2397. * yet. This happens if the parent directories inum is higher
  2398. * the the current inum. To handle this case, we create the
  2399. * parent directory out of order. But we need to check if this
  2400. * did already happen before due to other refs in the same dir.
  2401. */
  2402. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
  2403. if (ret < 0)
  2404. goto out;
  2405. if (ret == inode_state_will_create) {
  2406. ret = 0;
  2407. /*
  2408. * First check if any of the current inodes refs did
  2409. * already create the dir.
  2410. */
  2411. list_for_each_entry(cur2, &sctx->new_refs, list) {
  2412. if (cur == cur2)
  2413. break;
  2414. if (cur2->dir == cur->dir) {
  2415. ret = 1;
  2416. break;
  2417. }
  2418. }
  2419. /*
  2420. * If that did not happen, check if a previous inode
  2421. * did already create the dir.
  2422. */
  2423. if (!ret)
  2424. ret = did_create_dir(sctx, cur->dir);
  2425. if (ret < 0)
  2426. goto out;
  2427. if (!ret) {
  2428. ret = send_create_inode(sctx, cur->dir);
  2429. if (ret < 0)
  2430. goto out;
  2431. }
  2432. }
  2433. /*
  2434. * Check if this new ref would overwrite the first ref of
  2435. * another unprocessed inode. If yes, orphanize the
  2436. * overwritten inode. If we find an overwritten ref that is
  2437. * not the first ref, simply unlink it.
  2438. */
  2439. ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  2440. cur->name, cur->name_len,
  2441. &ow_inode, &ow_gen);
  2442. if (ret < 0)
  2443. goto out;
  2444. if (ret) {
  2445. ret = is_first_ref(sctx, sctx->parent_root,
  2446. ow_inode, cur->dir, cur->name,
  2447. cur->name_len);
  2448. if (ret < 0)
  2449. goto out;
  2450. if (ret) {
  2451. ret = orphanize_inode(sctx, ow_inode, ow_gen,
  2452. cur->full_path);
  2453. if (ret < 0)
  2454. goto out;
  2455. } else {
  2456. ret = send_unlink(sctx, cur->full_path);
  2457. if (ret < 0)
  2458. goto out;
  2459. }
  2460. }
  2461. /*
  2462. * link/move the ref to the new place. If we have an orphan
  2463. * inode, move it and update valid_path. If not, link or move
  2464. * it depending on the inode mode.
  2465. */
  2466. if (is_orphan) {
  2467. ret = send_rename(sctx, valid_path, cur->full_path);
  2468. if (ret < 0)
  2469. goto out;
  2470. is_orphan = 0;
  2471. ret = fs_path_copy(valid_path, cur->full_path);
  2472. if (ret < 0)
  2473. goto out;
  2474. } else {
  2475. if (S_ISDIR(sctx->cur_inode_mode)) {
  2476. /*
  2477. * Dirs can't be linked, so move it. For moved
  2478. * dirs, we always have one new and one deleted
  2479. * ref. The deleted ref is ignored later.
  2480. */
  2481. ret = send_rename(sctx, valid_path,
  2482. cur->full_path);
  2483. if (ret < 0)
  2484. goto out;
  2485. ret = fs_path_copy(valid_path, cur->full_path);
  2486. if (ret < 0)
  2487. goto out;
  2488. } else {
  2489. ret = send_link(sctx, cur->full_path,
  2490. valid_path);
  2491. if (ret < 0)
  2492. goto out;
  2493. }
  2494. }
  2495. ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
  2496. GFP_NOFS);
  2497. if (ret < 0)
  2498. goto out;
  2499. }
  2500. if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
  2501. /*
  2502. * Check if we can already rmdir the directory. If not,
  2503. * orphanize it. For every dir item inside that gets deleted
  2504. * later, we do this check again and rmdir it then if possible.
  2505. * See the use of check_dirs for more details.
  2506. */
  2507. ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
  2508. if (ret < 0)
  2509. goto out;
  2510. if (ret) {
  2511. ret = send_rmdir(sctx, valid_path);
  2512. if (ret < 0)
  2513. goto out;
  2514. } else if (!is_orphan) {
  2515. ret = orphanize_inode(sctx, sctx->cur_ino,
  2516. sctx->cur_inode_gen, valid_path);
  2517. if (ret < 0)
  2518. goto out;
  2519. is_orphan = 1;
  2520. }
  2521. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  2522. ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
  2523. GFP_NOFS);
  2524. if (ret < 0)
  2525. goto out;
  2526. }
  2527. } else if (S_ISDIR(sctx->cur_inode_mode) &&
  2528. !list_empty(&sctx->deleted_refs)) {
  2529. /*
  2530. * We have a moved dir. Add the old parent to check_dirs
  2531. */
  2532. cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
  2533. list);
  2534. ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
  2535. GFP_NOFS);
  2536. if (ret < 0)
  2537. goto out;
  2538. } else if (!S_ISDIR(sctx->cur_inode_mode)) {
  2539. /*
  2540. * We have a non dir inode. Go through all deleted refs and
  2541. * unlink them if they were not already overwritten by other
  2542. * inodes.
  2543. */
  2544. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  2545. ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  2546. sctx->cur_ino, sctx->cur_inode_gen,
  2547. cur->name, cur->name_len);
  2548. if (ret < 0)
  2549. goto out;
  2550. if (!ret) {
  2551. ret = send_unlink(sctx, cur->full_path);
  2552. if (ret < 0)
  2553. goto out;
  2554. }
  2555. ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
  2556. GFP_NOFS);
  2557. if (ret < 0)
  2558. goto out;
  2559. }
  2560. /*
  2561. * If the inode is still orphan, unlink the orphan. This may
  2562. * happen when a previous inode did overwrite the first ref
  2563. * of this inode and no new refs were added for the current
  2564. * inode. Unlinking does not mean that the inode is deleted in
  2565. * all cases. There may still be links to this inode in other
  2566. * places.
  2567. */
  2568. if (is_orphan) {
  2569. ret = send_unlink(sctx, valid_path);
  2570. if (ret < 0)
  2571. goto out;
  2572. }
  2573. }
  2574. /*
  2575. * We did collect all parent dirs where cur_inode was once located. We
  2576. * now go through all these dirs and check if they are pending for
  2577. * deletion and if it's finally possible to perform the rmdir now.
  2578. * We also update the inode stats of the parent dirs here.
  2579. */
  2580. ULIST_ITER_INIT(&uit);
  2581. while ((un = ulist_next(check_dirs, &uit))) {
  2582. /*
  2583. * In case we had refs into dirs that were not processed yet,
  2584. * we don't need to do the utime and rmdir logic for these dirs.
  2585. * The dir will be processed later.
  2586. */
  2587. if (un->val > sctx->cur_ino)
  2588. continue;
  2589. ret = get_cur_inode_state(sctx, un->val, un->aux);
  2590. if (ret < 0)
  2591. goto out;
  2592. if (ret == inode_state_did_create ||
  2593. ret == inode_state_no_change) {
  2594. /* TODO delayed utimes */
  2595. ret = send_utimes(sctx, un->val, un->aux);
  2596. if (ret < 0)
  2597. goto out;
  2598. } else if (ret == inode_state_did_delete) {
  2599. ret = can_rmdir(sctx, un->val, sctx->cur_ino);
  2600. if (ret < 0)
  2601. goto out;
  2602. if (ret) {
  2603. ret = get_cur_path(sctx, un->val, un->aux,
  2604. valid_path);
  2605. if (ret < 0)
  2606. goto out;
  2607. ret = send_rmdir(sctx, valid_path);
  2608. if (ret < 0)
  2609. goto out;
  2610. }
  2611. }
  2612. }
  2613. ret = 0;
  2614. out:
  2615. free_recorded_refs(sctx);
  2616. ulist_free(check_dirs);
  2617. fs_path_free(sctx, valid_path);
  2618. return ret;
  2619. }
  2620. static int __record_new_ref(int num, u64 dir, int index,
  2621. struct fs_path *name,
  2622. void *ctx)
  2623. {
  2624. int ret = 0;
  2625. struct send_ctx *sctx = ctx;
  2626. struct fs_path *p;
  2627. u64 gen;
  2628. p = fs_path_alloc(sctx);
  2629. if (!p)
  2630. return -ENOMEM;
  2631. ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
  2632. NULL, NULL);
  2633. if (ret < 0)
  2634. goto out;
  2635. ret = get_cur_path(sctx, dir, gen, p);
  2636. if (ret < 0)
  2637. goto out;
  2638. ret = fs_path_add_path(p, name);
  2639. if (ret < 0)
  2640. goto out;
  2641. ret = record_ref(&sctx->new_refs, dir, gen, p);
  2642. out:
  2643. if (ret)
  2644. fs_path_free(sctx, p);
  2645. return ret;
  2646. }
  2647. static int __record_deleted_ref(int num, u64 dir, int index,
  2648. struct fs_path *name,
  2649. void *ctx)
  2650. {
  2651. int ret = 0;
  2652. struct send_ctx *sctx = ctx;
  2653. struct fs_path *p;
  2654. u64 gen;
  2655. p = fs_path_alloc(sctx);
  2656. if (!p)
  2657. return -ENOMEM;
  2658. ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
  2659. NULL, NULL);
  2660. if (ret < 0)
  2661. goto out;
  2662. ret = get_cur_path(sctx, dir, gen, p);
  2663. if (ret < 0)
  2664. goto out;
  2665. ret = fs_path_add_path(p, name);
  2666. if (ret < 0)
  2667. goto out;
  2668. ret = record_ref(&sctx->deleted_refs, dir, gen, p);
  2669. out:
  2670. if (ret)
  2671. fs_path_free(sctx, p);
  2672. return ret;
  2673. }
  2674. static int record_new_ref(struct send_ctx *sctx)
  2675. {
  2676. int ret;
  2677. ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
  2678. sctx->cmp_key, 0, __record_new_ref, sctx);
  2679. if (ret < 0)
  2680. goto out;
  2681. ret = 0;
  2682. out:
  2683. return ret;
  2684. }
  2685. static int record_deleted_ref(struct send_ctx *sctx)
  2686. {
  2687. int ret;
  2688. ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
  2689. sctx->cmp_key, 0, __record_deleted_ref, sctx);
  2690. if (ret < 0)
  2691. goto out;
  2692. ret = 0;
  2693. out:
  2694. return ret;
  2695. }
  2696. struct find_ref_ctx {
  2697. u64 dir;
  2698. struct fs_path *name;
  2699. int found_idx;
  2700. };
  2701. static int __find_iref(int num, u64 dir, int index,
  2702. struct fs_path *name,
  2703. void *ctx_)
  2704. {
  2705. struct find_ref_ctx *ctx = ctx_;
  2706. if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
  2707. strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
  2708. ctx->found_idx = num;
  2709. return 1;
  2710. }
  2711. return 0;
  2712. }
  2713. static int find_iref(struct send_ctx *sctx,
  2714. struct btrfs_root *root,
  2715. struct btrfs_path *path,
  2716. struct btrfs_key *key,
  2717. u64 dir, struct fs_path *name)
  2718. {
  2719. int ret;
  2720. struct find_ref_ctx ctx;
  2721. ctx.dir = dir;
  2722. ctx.name = name;
  2723. ctx.found_idx = -1;
  2724. ret = iterate_inode_ref(sctx, root, path, key, 0, __find_iref, &ctx);
  2725. if (ret < 0)
  2726. return ret;
  2727. if (ctx.found_idx == -1)
  2728. return -ENOENT;
  2729. return ctx.found_idx;
  2730. }
  2731. static int __record_changed_new_ref(int num, u64 dir, int index,
  2732. struct fs_path *name,
  2733. void *ctx)
  2734. {
  2735. int ret;
  2736. struct send_ctx *sctx = ctx;
  2737. ret = find_iref(sctx, sctx->parent_root, sctx->right_path,
  2738. sctx->cmp_key, dir, name);
  2739. if (ret == -ENOENT)
  2740. ret = __record_new_ref(num, dir, index, name, sctx);
  2741. else if (ret > 0)
  2742. ret = 0;
  2743. return ret;
  2744. }
  2745. static int __record_changed_deleted_ref(int num, u64 dir, int index,
  2746. struct fs_path *name,
  2747. void *ctx)
  2748. {
  2749. int ret;
  2750. struct send_ctx *sctx = ctx;
  2751. ret = find_iref(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
  2752. dir, name);
  2753. if (ret == -ENOENT)
  2754. ret = __record_deleted_ref(num, dir, index, name, sctx);
  2755. else if (ret > 0)
  2756. ret = 0;
  2757. return ret;
  2758. }
  2759. static int record_changed_ref(struct send_ctx *sctx)
  2760. {
  2761. int ret = 0;
  2762. ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
  2763. sctx->cmp_key, 0, __record_changed_new_ref, sctx);
  2764. if (ret < 0)
  2765. goto out;
  2766. ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
  2767. sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
  2768. if (ret < 0)
  2769. goto out;
  2770. ret = 0;
  2771. out:
  2772. return ret;
  2773. }
  2774. /*
  2775. * Record and process all refs at once. Needed when an inode changes the
  2776. * generation number, which means that it was deleted and recreated.
  2777. */
  2778. static int process_all_refs(struct send_ctx *sctx,
  2779. enum btrfs_compare_tree_result cmd)
  2780. {
  2781. int ret;
  2782. struct btrfs_root *root;
  2783. struct btrfs_path *path;
  2784. struct btrfs_key key;
  2785. struct btrfs_key found_key;
  2786. struct extent_buffer *eb;
  2787. int slot;
  2788. iterate_inode_ref_t cb;
  2789. path = alloc_path_for_send();
  2790. if (!path)
  2791. return -ENOMEM;
  2792. if (cmd == BTRFS_COMPARE_TREE_NEW) {
  2793. root = sctx->send_root;
  2794. cb = __record_new_ref;
  2795. } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
  2796. root = sctx->parent_root;
  2797. cb = __record_deleted_ref;
  2798. } else {
  2799. BUG();
  2800. }
  2801. key.objectid = sctx->cmp_key->objectid;
  2802. key.type = BTRFS_INODE_REF_KEY;
  2803. key.offset = 0;
  2804. while (1) {
  2805. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  2806. if (ret < 0)
  2807. goto out;
  2808. if (ret)
  2809. break;
  2810. eb = path->nodes[0];
  2811. slot = path->slots[0];
  2812. btrfs_item_key_to_cpu(eb, &found_key, slot);
  2813. if (found_key.objectid != key.objectid ||
  2814. (found_key.type != BTRFS_INODE_REF_KEY &&
  2815. found_key.type != BTRFS_INODE_EXTREF_KEY))
  2816. break;
  2817. ret = iterate_inode_ref(sctx, root, path, &found_key, 0, cb,
  2818. sctx);
  2819. btrfs_release_path(path);
  2820. if (ret < 0)
  2821. goto out;
  2822. key.offset = found_key.offset + 1;
  2823. }
  2824. btrfs_release_path(path);
  2825. ret = process_recorded_refs(sctx);
  2826. out:
  2827. btrfs_free_path(path);
  2828. return ret;
  2829. }
  2830. static int send_set_xattr(struct send_ctx *sctx,
  2831. struct fs_path *path,
  2832. const char *name, int name_len,
  2833. const char *data, int data_len)
  2834. {
  2835. int ret = 0;
  2836. ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
  2837. if (ret < 0)
  2838. goto out;
  2839. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  2840. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  2841. TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
  2842. ret = send_cmd(sctx);
  2843. tlv_put_failure:
  2844. out:
  2845. return ret;
  2846. }
  2847. static int send_remove_xattr(struct send_ctx *sctx,
  2848. struct fs_path *path,
  2849. const char *name, int name_len)
  2850. {
  2851. int ret = 0;
  2852. ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
  2853. if (ret < 0)
  2854. goto out;
  2855. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  2856. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  2857. ret = send_cmd(sctx);
  2858. tlv_put_failure:
  2859. out:
  2860. return ret;
  2861. }
  2862. static int __process_new_xattr(int num, struct btrfs_key *di_key,
  2863. const char *name, int name_len,
  2864. const char *data, int data_len,
  2865. u8 type, void *ctx)
  2866. {
  2867. int ret;
  2868. struct send_ctx *sctx = ctx;
  2869. struct fs_path *p;
  2870. posix_acl_xattr_header dummy_acl;
  2871. p = fs_path_alloc(sctx);
  2872. if (!p)
  2873. return -ENOMEM;
  2874. /*
  2875. * This hack is needed because empty acl's are stored as zero byte
  2876. * data in xattrs. Problem with that is, that receiving these zero byte
  2877. * acl's will fail later. To fix this, we send a dummy acl list that
  2878. * only contains the version number and no entries.
  2879. */
  2880. if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
  2881. !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
  2882. if (data_len == 0) {
  2883. dummy_acl.a_version =
  2884. cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  2885. data = (char *)&dummy_acl;
  2886. data_len = sizeof(dummy_acl);
  2887. }
  2888. }
  2889. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  2890. if (ret < 0)
  2891. goto out;
  2892. ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
  2893. out:
  2894. fs_path_free(sctx, p);
  2895. return ret;
  2896. }
  2897. static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
  2898. const char *name, int name_len,
  2899. const char *data, int data_len,
  2900. u8 type, void *ctx)
  2901. {
  2902. int ret;
  2903. struct send_ctx *sctx = ctx;
  2904. struct fs_path *p;
  2905. p = fs_path_alloc(sctx);
  2906. if (!p)
  2907. return -ENOMEM;
  2908. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  2909. if (ret < 0)
  2910. goto out;
  2911. ret = send_remove_xattr(sctx, p, name, name_len);
  2912. out:
  2913. fs_path_free(sctx, p);
  2914. return ret;
  2915. }
  2916. static int process_new_xattr(struct send_ctx *sctx)
  2917. {
  2918. int ret = 0;
  2919. ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
  2920. sctx->cmp_key, __process_new_xattr, sctx);
  2921. return ret;
  2922. }
  2923. static int process_deleted_xattr(struct send_ctx *sctx)
  2924. {
  2925. int ret;
  2926. ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
  2927. sctx->cmp_key, __process_deleted_xattr, sctx);
  2928. return ret;
  2929. }
  2930. struct find_xattr_ctx {
  2931. const char *name;
  2932. int name_len;
  2933. int found_idx;
  2934. char *found_data;
  2935. int found_data_len;
  2936. };
  2937. static int __find_xattr(int num, struct btrfs_key *di_key,
  2938. const char *name, int name_len,
  2939. const char *data, int data_len,
  2940. u8 type, void *vctx)
  2941. {
  2942. struct find_xattr_ctx *ctx = vctx;
  2943. if (name_len == ctx->name_len &&
  2944. strncmp(name, ctx->name, name_len) == 0) {
  2945. ctx->found_idx = num;
  2946. ctx->found_data_len = data_len;
  2947. ctx->found_data = kmalloc(data_len, GFP_NOFS);
  2948. if (!ctx->found_data)
  2949. return -ENOMEM;
  2950. memcpy(ctx->found_data, data, data_len);
  2951. return 1;
  2952. }
  2953. return 0;
  2954. }
  2955. static int find_xattr(struct send_ctx *sctx,
  2956. struct btrfs_root *root,
  2957. struct btrfs_path *path,
  2958. struct btrfs_key *key,
  2959. const char *name, int name_len,
  2960. char **data, int *data_len)
  2961. {
  2962. int ret;
  2963. struct find_xattr_ctx ctx;
  2964. ctx.name = name;
  2965. ctx.name_len = name_len;
  2966. ctx.found_idx = -1;
  2967. ctx.found_data = NULL;
  2968. ctx.found_data_len = 0;
  2969. ret = iterate_dir_item(sctx, root, path, key, __find_xattr, &ctx);
  2970. if (ret < 0)
  2971. return ret;
  2972. if (ctx.found_idx == -1)
  2973. return -ENOENT;
  2974. if (data) {
  2975. *data = ctx.found_data;
  2976. *data_len = ctx.found_data_len;
  2977. } else {
  2978. kfree(ctx.found_data);
  2979. }
  2980. return ctx.found_idx;
  2981. }
  2982. static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
  2983. const char *name, int name_len,
  2984. const char *data, int data_len,
  2985. u8 type, void *ctx)
  2986. {
  2987. int ret;
  2988. struct send_ctx *sctx = ctx;
  2989. char *found_data = NULL;
  2990. int found_data_len = 0;
  2991. struct fs_path *p = NULL;
  2992. ret = find_xattr(sctx, sctx->parent_root, sctx->right_path,
  2993. sctx->cmp_key, name, name_len, &found_data,
  2994. &found_data_len);
  2995. if (ret == -ENOENT) {
  2996. ret = __process_new_xattr(num, di_key, name, name_len, data,
  2997. data_len, type, ctx);
  2998. } else if (ret >= 0) {
  2999. if (data_len != found_data_len ||
  3000. memcmp(data, found_data, data_len)) {
  3001. ret = __process_new_xattr(num, di_key, name, name_len,
  3002. data, data_len, type, ctx);
  3003. } else {
  3004. ret = 0;
  3005. }
  3006. }
  3007. kfree(found_data);
  3008. fs_path_free(sctx, p);
  3009. return ret;
  3010. }
  3011. static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
  3012. const char *name, int name_len,
  3013. const char *data, int data_len,
  3014. u8 type, void *ctx)
  3015. {
  3016. int ret;
  3017. struct send_ctx *sctx = ctx;
  3018. ret = find_xattr(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
  3019. name, name_len, NULL, NULL);
  3020. if (ret == -ENOENT)
  3021. ret = __process_deleted_xattr(num, di_key, name, name_len, data,
  3022. data_len, type, ctx);
  3023. else if (ret >= 0)
  3024. ret = 0;
  3025. return ret;
  3026. }
  3027. static int process_changed_xattr(struct send_ctx *sctx)
  3028. {
  3029. int ret = 0;
  3030. ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
  3031. sctx->cmp_key, __process_changed_new_xattr, sctx);
  3032. if (ret < 0)
  3033. goto out;
  3034. ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
  3035. sctx->cmp_key, __process_changed_deleted_xattr, sctx);
  3036. out:
  3037. return ret;
  3038. }
  3039. static int process_all_new_xattrs(struct send_ctx *sctx)
  3040. {
  3041. int ret;
  3042. struct btrfs_root *root;
  3043. struct btrfs_path *path;
  3044. struct btrfs_key key;
  3045. struct btrfs_key found_key;
  3046. struct extent_buffer *eb;
  3047. int slot;
  3048. path = alloc_path_for_send();
  3049. if (!path)
  3050. return -ENOMEM;
  3051. root = sctx->send_root;
  3052. key.objectid = sctx->cmp_key->objectid;
  3053. key.type = BTRFS_XATTR_ITEM_KEY;
  3054. key.offset = 0;
  3055. while (1) {
  3056. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  3057. if (ret < 0)
  3058. goto out;
  3059. if (ret) {
  3060. ret = 0;
  3061. goto out;
  3062. }
  3063. eb = path->nodes[0];
  3064. slot = path->slots[0];
  3065. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3066. if (found_key.objectid != key.objectid ||
  3067. found_key.type != key.type) {
  3068. ret = 0;
  3069. goto out;
  3070. }
  3071. ret = iterate_dir_item(sctx, root, path, &found_key,
  3072. __process_new_xattr, sctx);
  3073. if (ret < 0)
  3074. goto out;
  3075. btrfs_release_path(path);
  3076. key.offset = found_key.offset + 1;
  3077. }
  3078. out:
  3079. btrfs_free_path(path);
  3080. return ret;
  3081. }
  3082. /*
  3083. * Read some bytes from the current inode/file and send a write command to
  3084. * user space.
  3085. */
  3086. static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
  3087. {
  3088. int ret = 0;
  3089. struct fs_path *p;
  3090. loff_t pos = offset;
  3091. int num_read = 0;
  3092. mm_segment_t old_fs;
  3093. p = fs_path_alloc(sctx);
  3094. if (!p)
  3095. return -ENOMEM;
  3096. /*
  3097. * vfs normally only accepts user space buffers for security reasons.
  3098. * we only read from the file and also only provide the read_buf buffer
  3099. * to vfs. As this buffer does not come from a user space call, it's
  3100. * ok to temporary allow kernel space buffers.
  3101. */
  3102. old_fs = get_fs();
  3103. set_fs(KERNEL_DS);
  3104. verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
  3105. ret = open_cur_inode_file(sctx);
  3106. if (ret < 0)
  3107. goto out;
  3108. ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
  3109. if (ret < 0)
  3110. goto out;
  3111. num_read = ret;
  3112. if (!num_read)
  3113. goto out;
  3114. ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
  3115. if (ret < 0)
  3116. goto out;
  3117. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3118. if (ret < 0)
  3119. goto out;
  3120. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3121. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3122. TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
  3123. ret = send_cmd(sctx);
  3124. tlv_put_failure:
  3125. out:
  3126. fs_path_free(sctx, p);
  3127. set_fs(old_fs);
  3128. if (ret < 0)
  3129. return ret;
  3130. return num_read;
  3131. }
  3132. /*
  3133. * Send a clone command to user space.
  3134. */
  3135. static int send_clone(struct send_ctx *sctx,
  3136. u64 offset, u32 len,
  3137. struct clone_root *clone_root)
  3138. {
  3139. int ret = 0;
  3140. struct fs_path *p;
  3141. u64 gen;
  3142. verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
  3143. "clone_inode=%llu, clone_offset=%llu\n", offset, len,
  3144. clone_root->root->objectid, clone_root->ino,
  3145. clone_root->offset);
  3146. p = fs_path_alloc(sctx);
  3147. if (!p)
  3148. return -ENOMEM;
  3149. ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
  3150. if (ret < 0)
  3151. goto out;
  3152. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3153. if (ret < 0)
  3154. goto out;
  3155. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3156. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
  3157. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3158. if (clone_root->root == sctx->send_root) {
  3159. ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
  3160. &gen, NULL, NULL, NULL, NULL);
  3161. if (ret < 0)
  3162. goto out;
  3163. ret = get_cur_path(sctx, clone_root->ino, gen, p);
  3164. } else {
  3165. ret = get_inode_path(sctx, clone_root->root,
  3166. clone_root->ino, p);
  3167. }
  3168. if (ret < 0)
  3169. goto out;
  3170. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  3171. clone_root->root->root_item.uuid);
  3172. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  3173. clone_root->root->root_item.ctransid);
  3174. TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
  3175. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
  3176. clone_root->offset);
  3177. ret = send_cmd(sctx);
  3178. tlv_put_failure:
  3179. out:
  3180. fs_path_free(sctx, p);
  3181. return ret;
  3182. }
  3183. /*
  3184. * Send an update extent command to user space.
  3185. */
  3186. static int send_update_extent(struct send_ctx *sctx,
  3187. u64 offset, u32 len)
  3188. {
  3189. int ret = 0;
  3190. struct fs_path *p;
  3191. p = fs_path_alloc(sctx);
  3192. if (!p)
  3193. return -ENOMEM;
  3194. ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
  3195. if (ret < 0)
  3196. goto out;
  3197. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3198. if (ret < 0)
  3199. goto out;
  3200. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3201. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3202. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
  3203. ret = send_cmd(sctx);
  3204. tlv_put_failure:
  3205. out:
  3206. fs_path_free(sctx, p);
  3207. return ret;
  3208. }
  3209. static int send_write_or_clone(struct send_ctx *sctx,
  3210. struct btrfs_path *path,
  3211. struct btrfs_key *key,
  3212. struct clone_root *clone_root)
  3213. {
  3214. int ret = 0;
  3215. struct btrfs_file_extent_item *ei;
  3216. u64 offset = key->offset;
  3217. u64 pos = 0;
  3218. u64 len;
  3219. u32 l;
  3220. u8 type;
  3221. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3222. struct btrfs_file_extent_item);
  3223. type = btrfs_file_extent_type(path->nodes[0], ei);
  3224. if (type == BTRFS_FILE_EXTENT_INLINE) {
  3225. len = btrfs_file_extent_inline_len(path->nodes[0], ei);
  3226. /*
  3227. * it is possible the inline item won't cover the whole page,
  3228. * but there may be items after this page. Make
  3229. * sure to send the whole thing
  3230. */
  3231. len = PAGE_CACHE_ALIGN(len);
  3232. } else {
  3233. len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
  3234. }
  3235. if (offset + len > sctx->cur_inode_size)
  3236. len = sctx->cur_inode_size - offset;
  3237. if (len == 0) {
  3238. ret = 0;
  3239. goto out;
  3240. }
  3241. if (clone_root) {
  3242. ret = send_clone(sctx, offset, len, clone_root);
  3243. } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
  3244. ret = send_update_extent(sctx, offset, len);
  3245. } else {
  3246. while (pos < len) {
  3247. l = len - pos;
  3248. if (l > BTRFS_SEND_READ_SIZE)
  3249. l = BTRFS_SEND_READ_SIZE;
  3250. ret = send_write(sctx, pos + offset, l);
  3251. if (ret < 0)
  3252. goto out;
  3253. if (!ret)
  3254. break;
  3255. pos += ret;
  3256. }
  3257. ret = 0;
  3258. }
  3259. out:
  3260. return ret;
  3261. }
  3262. static int is_extent_unchanged(struct send_ctx *sctx,
  3263. struct btrfs_path *left_path,
  3264. struct btrfs_key *ekey)
  3265. {
  3266. int ret = 0;
  3267. struct btrfs_key key;
  3268. struct btrfs_path *path = NULL;
  3269. struct extent_buffer *eb;
  3270. int slot;
  3271. struct btrfs_key found_key;
  3272. struct btrfs_file_extent_item *ei;
  3273. u64 left_disknr;
  3274. u64 right_disknr;
  3275. u64 left_offset;
  3276. u64 right_offset;
  3277. u64 left_offset_fixed;
  3278. u64 left_len;
  3279. u64 right_len;
  3280. u64 left_gen;
  3281. u64 right_gen;
  3282. u8 left_type;
  3283. u8 right_type;
  3284. path = alloc_path_for_send();
  3285. if (!path)
  3286. return -ENOMEM;
  3287. eb = left_path->nodes[0];
  3288. slot = left_path->slots[0];
  3289. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  3290. left_type = btrfs_file_extent_type(eb, ei);
  3291. if (left_type != BTRFS_FILE_EXTENT_REG) {
  3292. ret = 0;
  3293. goto out;
  3294. }
  3295. left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  3296. left_len = btrfs_file_extent_num_bytes(eb, ei);
  3297. left_offset = btrfs_file_extent_offset(eb, ei);
  3298. left_gen = btrfs_file_extent_generation(eb, ei);
  3299. /*
  3300. * Following comments will refer to these graphics. L is the left
  3301. * extents which we are checking at the moment. 1-8 are the right
  3302. * extents that we iterate.
  3303. *
  3304. * |-----L-----|
  3305. * |-1-|-2a-|-3-|-4-|-5-|-6-|
  3306. *
  3307. * |-----L-----|
  3308. * |--1--|-2b-|...(same as above)
  3309. *
  3310. * Alternative situation. Happens on files where extents got split.
  3311. * |-----L-----|
  3312. * |-----------7-----------|-6-|
  3313. *
  3314. * Alternative situation. Happens on files which got larger.
  3315. * |-----L-----|
  3316. * |-8-|
  3317. * Nothing follows after 8.
  3318. */
  3319. key.objectid = ekey->objectid;
  3320. key.type = BTRFS_EXTENT_DATA_KEY;
  3321. key.offset = ekey->offset;
  3322. ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
  3323. if (ret < 0)
  3324. goto out;
  3325. if (ret) {
  3326. ret = 0;
  3327. goto out;
  3328. }
  3329. /*
  3330. * Handle special case where the right side has no extents at all.
  3331. */
  3332. eb = path->nodes[0];
  3333. slot = path->slots[0];
  3334. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3335. if (found_key.objectid != key.objectid ||
  3336. found_key.type != key.type) {
  3337. ret = 0;
  3338. goto out;
  3339. }
  3340. /*
  3341. * We're now on 2a, 2b or 7.
  3342. */
  3343. key = found_key;
  3344. while (key.offset < ekey->offset + left_len) {
  3345. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  3346. right_type = btrfs_file_extent_type(eb, ei);
  3347. right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  3348. right_len = btrfs_file_extent_num_bytes(eb, ei);
  3349. right_offset = btrfs_file_extent_offset(eb, ei);
  3350. right_gen = btrfs_file_extent_generation(eb, ei);
  3351. if (right_type != BTRFS_FILE_EXTENT_REG) {
  3352. ret = 0;
  3353. goto out;
  3354. }
  3355. /*
  3356. * Are we at extent 8? If yes, we know the extent is changed.
  3357. * This may only happen on the first iteration.
  3358. */
  3359. if (found_key.offset + right_len <= ekey->offset) {
  3360. ret = 0;
  3361. goto out;
  3362. }
  3363. left_offset_fixed = left_offset;
  3364. if (key.offset < ekey->offset) {
  3365. /* Fix the right offset for 2a and 7. */
  3366. right_offset += ekey->offset - key.offset;
  3367. } else {
  3368. /* Fix the left offset for all behind 2a and 2b */
  3369. left_offset_fixed += key.offset - ekey->offset;
  3370. }
  3371. /*
  3372. * Check if we have the same extent.
  3373. */
  3374. if (left_disknr != right_disknr ||
  3375. left_offset_fixed != right_offset ||
  3376. left_gen != right_gen) {
  3377. ret = 0;
  3378. goto out;
  3379. }
  3380. /*
  3381. * Go to the next extent.
  3382. */
  3383. ret = btrfs_next_item(sctx->parent_root, path);
  3384. if (ret < 0)
  3385. goto out;
  3386. if (!ret) {
  3387. eb = path->nodes[0];
  3388. slot = path->slots[0];
  3389. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3390. }
  3391. if (ret || found_key.objectid != key.objectid ||
  3392. found_key.type != key.type) {
  3393. key.offset += right_len;
  3394. break;
  3395. } else {
  3396. if (found_key.offset != key.offset + right_len) {
  3397. /* Should really not happen */
  3398. ret = -EIO;
  3399. goto out;
  3400. }
  3401. }
  3402. key = found_key;
  3403. }
  3404. /*
  3405. * We're now behind the left extent (treat as unchanged) or at the end
  3406. * of the right side (treat as changed).
  3407. */
  3408. if (key.offset >= ekey->offset + left_len)
  3409. ret = 1;
  3410. else
  3411. ret = 0;
  3412. out:
  3413. btrfs_free_path(path);
  3414. return ret;
  3415. }
  3416. static int process_extent(struct send_ctx *sctx,
  3417. struct btrfs_path *path,
  3418. struct btrfs_key *key)
  3419. {
  3420. int ret = 0;
  3421. struct clone_root *found_clone = NULL;
  3422. if (S_ISLNK(sctx->cur_inode_mode))
  3423. return 0;
  3424. if (sctx->parent_root && !sctx->cur_inode_new) {
  3425. ret = is_extent_unchanged(sctx, path, key);
  3426. if (ret < 0)
  3427. goto out;
  3428. if (ret) {
  3429. ret = 0;
  3430. goto out;
  3431. }
  3432. }
  3433. ret = find_extent_clone(sctx, path, key->objectid, key->offset,
  3434. sctx->cur_inode_size, &found_clone);
  3435. if (ret != -ENOENT && ret < 0)
  3436. goto out;
  3437. ret = send_write_or_clone(sctx, path, key, found_clone);
  3438. out:
  3439. return ret;
  3440. }
  3441. static int process_all_extents(struct send_ctx *sctx)
  3442. {
  3443. int ret;
  3444. struct btrfs_root *root;
  3445. struct btrfs_path *path;
  3446. struct btrfs_key key;
  3447. struct btrfs_key found_key;
  3448. struct extent_buffer *eb;
  3449. int slot;
  3450. root = sctx->send_root;
  3451. path = alloc_path_for_send();
  3452. if (!path)
  3453. return -ENOMEM;
  3454. key.objectid = sctx->cmp_key->objectid;
  3455. key.type = BTRFS_EXTENT_DATA_KEY;
  3456. key.offset = 0;
  3457. while (1) {
  3458. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  3459. if (ret < 0)
  3460. goto out;
  3461. if (ret) {
  3462. ret = 0;
  3463. goto out;
  3464. }
  3465. eb = path->nodes[0];
  3466. slot = path->slots[0];
  3467. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3468. if (found_key.objectid != key.objectid ||
  3469. found_key.type != key.type) {
  3470. ret = 0;
  3471. goto out;
  3472. }
  3473. ret = process_extent(sctx, path, &found_key);
  3474. if (ret < 0)
  3475. goto out;
  3476. btrfs_release_path(path);
  3477. key.offset = found_key.offset + 1;
  3478. }
  3479. out:
  3480. btrfs_free_path(path);
  3481. return ret;
  3482. }
  3483. static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
  3484. {
  3485. int ret = 0;
  3486. if (sctx->cur_ino == 0)
  3487. goto out;
  3488. if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
  3489. sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
  3490. goto out;
  3491. if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
  3492. goto out;
  3493. ret = process_recorded_refs(sctx);
  3494. if (ret < 0)
  3495. goto out;
  3496. /*
  3497. * We have processed the refs and thus need to advance send_progress.
  3498. * Now, calls to get_cur_xxx will take the updated refs of the current
  3499. * inode into account.
  3500. */
  3501. sctx->send_progress = sctx->cur_ino + 1;
  3502. out:
  3503. return ret;
  3504. }
  3505. static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
  3506. {
  3507. int ret = 0;
  3508. u64 left_mode;
  3509. u64 left_uid;
  3510. u64 left_gid;
  3511. u64 right_mode;
  3512. u64 right_uid;
  3513. u64 right_gid;
  3514. int need_chmod = 0;
  3515. int need_chown = 0;
  3516. ret = process_recorded_refs_if_needed(sctx, at_end);
  3517. if (ret < 0)
  3518. goto out;
  3519. if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
  3520. goto out;
  3521. if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
  3522. goto out;
  3523. ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
  3524. &left_mode, &left_uid, &left_gid, NULL);
  3525. if (ret < 0)
  3526. goto out;
  3527. if (!sctx->parent_root || sctx->cur_inode_new) {
  3528. need_chown = 1;
  3529. if (!S_ISLNK(sctx->cur_inode_mode))
  3530. need_chmod = 1;
  3531. } else {
  3532. ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
  3533. NULL, NULL, &right_mode, &right_uid,
  3534. &right_gid, NULL);
  3535. if (ret < 0)
  3536. goto out;
  3537. if (left_uid != right_uid || left_gid != right_gid)
  3538. need_chown = 1;
  3539. if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
  3540. need_chmod = 1;
  3541. }
  3542. if (S_ISREG(sctx->cur_inode_mode)) {
  3543. ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3544. sctx->cur_inode_size);
  3545. if (ret < 0)
  3546. goto out;
  3547. }
  3548. if (need_chown) {
  3549. ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3550. left_uid, left_gid);
  3551. if (ret < 0)
  3552. goto out;
  3553. }
  3554. if (need_chmod) {
  3555. ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3556. left_mode);
  3557. if (ret < 0)
  3558. goto out;
  3559. }
  3560. /*
  3561. * Need to send that every time, no matter if it actually changed
  3562. * between the two trees as we have done changes to the inode before.
  3563. */
  3564. ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
  3565. if (ret < 0)
  3566. goto out;
  3567. out:
  3568. return ret;
  3569. }
  3570. static int changed_inode(struct send_ctx *sctx,
  3571. enum btrfs_compare_tree_result result)
  3572. {
  3573. int ret = 0;
  3574. struct btrfs_key *key = sctx->cmp_key;
  3575. struct btrfs_inode_item *left_ii = NULL;
  3576. struct btrfs_inode_item *right_ii = NULL;
  3577. u64 left_gen = 0;
  3578. u64 right_gen = 0;
  3579. ret = close_cur_inode_file(sctx);
  3580. if (ret < 0)
  3581. goto out;
  3582. sctx->cur_ino = key->objectid;
  3583. sctx->cur_inode_new_gen = 0;
  3584. /*
  3585. * Set send_progress to current inode. This will tell all get_cur_xxx
  3586. * functions that the current inode's refs are not updated yet. Later,
  3587. * when process_recorded_refs is finished, it is set to cur_ino + 1.
  3588. */
  3589. sctx->send_progress = sctx->cur_ino;
  3590. if (result == BTRFS_COMPARE_TREE_NEW ||
  3591. result == BTRFS_COMPARE_TREE_CHANGED) {
  3592. left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
  3593. sctx->left_path->slots[0],
  3594. struct btrfs_inode_item);
  3595. left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
  3596. left_ii);
  3597. } else {
  3598. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  3599. sctx->right_path->slots[0],
  3600. struct btrfs_inode_item);
  3601. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  3602. right_ii);
  3603. }
  3604. if (result == BTRFS_COMPARE_TREE_CHANGED) {
  3605. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  3606. sctx->right_path->slots[0],
  3607. struct btrfs_inode_item);
  3608. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  3609. right_ii);
  3610. /*
  3611. * The cur_ino = root dir case is special here. We can't treat
  3612. * the inode as deleted+reused because it would generate a
  3613. * stream that tries to delete/mkdir the root dir.
  3614. */
  3615. if (left_gen != right_gen &&
  3616. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  3617. sctx->cur_inode_new_gen = 1;
  3618. }
  3619. if (result == BTRFS_COMPARE_TREE_NEW) {
  3620. sctx->cur_inode_gen = left_gen;
  3621. sctx->cur_inode_new = 1;
  3622. sctx->cur_inode_deleted = 0;
  3623. sctx->cur_inode_size = btrfs_inode_size(
  3624. sctx->left_path->nodes[0], left_ii);
  3625. sctx->cur_inode_mode = btrfs_inode_mode(
  3626. sctx->left_path->nodes[0], left_ii);
  3627. if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  3628. ret = send_create_inode_if_needed(sctx);
  3629. } else if (result == BTRFS_COMPARE_TREE_DELETED) {
  3630. sctx->cur_inode_gen = right_gen;
  3631. sctx->cur_inode_new = 0;
  3632. sctx->cur_inode_deleted = 1;
  3633. sctx->cur_inode_size = btrfs_inode_size(
  3634. sctx->right_path->nodes[0], right_ii);
  3635. sctx->cur_inode_mode = btrfs_inode_mode(
  3636. sctx->right_path->nodes[0], right_ii);
  3637. } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
  3638. /*
  3639. * We need to do some special handling in case the inode was
  3640. * reported as changed with a changed generation number. This
  3641. * means that the original inode was deleted and new inode
  3642. * reused the same inum. So we have to treat the old inode as
  3643. * deleted and the new one as new.
  3644. */
  3645. if (sctx->cur_inode_new_gen) {
  3646. /*
  3647. * First, process the inode as if it was deleted.
  3648. */
  3649. sctx->cur_inode_gen = right_gen;
  3650. sctx->cur_inode_new = 0;
  3651. sctx->cur_inode_deleted = 1;
  3652. sctx->cur_inode_size = btrfs_inode_size(
  3653. sctx->right_path->nodes[0], right_ii);
  3654. sctx->cur_inode_mode = btrfs_inode_mode(
  3655. sctx->right_path->nodes[0], right_ii);
  3656. ret = process_all_refs(sctx,
  3657. BTRFS_COMPARE_TREE_DELETED);
  3658. if (ret < 0)
  3659. goto out;
  3660. /*
  3661. * Now process the inode as if it was new.
  3662. */
  3663. sctx->cur_inode_gen = left_gen;
  3664. sctx->cur_inode_new = 1;
  3665. sctx->cur_inode_deleted = 0;
  3666. sctx->cur_inode_size = btrfs_inode_size(
  3667. sctx->left_path->nodes[0], left_ii);
  3668. sctx->cur_inode_mode = btrfs_inode_mode(
  3669. sctx->left_path->nodes[0], left_ii);
  3670. ret = send_create_inode_if_needed(sctx);
  3671. if (ret < 0)
  3672. goto out;
  3673. ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
  3674. if (ret < 0)
  3675. goto out;
  3676. /*
  3677. * Advance send_progress now as we did not get into
  3678. * process_recorded_refs_if_needed in the new_gen case.
  3679. */
  3680. sctx->send_progress = sctx->cur_ino + 1;
  3681. /*
  3682. * Now process all extents and xattrs of the inode as if
  3683. * they were all new.
  3684. */
  3685. ret = process_all_extents(sctx);
  3686. if (ret < 0)
  3687. goto out;
  3688. ret = process_all_new_xattrs(sctx);
  3689. if (ret < 0)
  3690. goto out;
  3691. } else {
  3692. sctx->cur_inode_gen = left_gen;
  3693. sctx->cur_inode_new = 0;
  3694. sctx->cur_inode_new_gen = 0;
  3695. sctx->cur_inode_deleted = 0;
  3696. sctx->cur_inode_size = btrfs_inode_size(
  3697. sctx->left_path->nodes[0], left_ii);
  3698. sctx->cur_inode_mode = btrfs_inode_mode(
  3699. sctx->left_path->nodes[0], left_ii);
  3700. }
  3701. }
  3702. out:
  3703. return ret;
  3704. }
  3705. /*
  3706. * We have to process new refs before deleted refs, but compare_trees gives us
  3707. * the new and deleted refs mixed. To fix this, we record the new/deleted refs
  3708. * first and later process them in process_recorded_refs.
  3709. * For the cur_inode_new_gen case, we skip recording completely because
  3710. * changed_inode did already initiate processing of refs. The reason for this is
  3711. * that in this case, compare_tree actually compares the refs of 2 different
  3712. * inodes. To fix this, process_all_refs is used in changed_inode to handle all
  3713. * refs of the right tree as deleted and all refs of the left tree as new.
  3714. */
  3715. static int changed_ref(struct send_ctx *sctx,
  3716. enum btrfs_compare_tree_result result)
  3717. {
  3718. int ret = 0;
  3719. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  3720. if (!sctx->cur_inode_new_gen &&
  3721. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
  3722. if (result == BTRFS_COMPARE_TREE_NEW)
  3723. ret = record_new_ref(sctx);
  3724. else if (result == BTRFS_COMPARE_TREE_DELETED)
  3725. ret = record_deleted_ref(sctx);
  3726. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  3727. ret = record_changed_ref(sctx);
  3728. }
  3729. return ret;
  3730. }
  3731. /*
  3732. * Process new/deleted/changed xattrs. We skip processing in the
  3733. * cur_inode_new_gen case because changed_inode did already initiate processing
  3734. * of xattrs. The reason is the same as in changed_ref
  3735. */
  3736. static int changed_xattr(struct send_ctx *sctx,
  3737. enum btrfs_compare_tree_result result)
  3738. {
  3739. int ret = 0;
  3740. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  3741. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  3742. if (result == BTRFS_COMPARE_TREE_NEW)
  3743. ret = process_new_xattr(sctx);
  3744. else if (result == BTRFS_COMPARE_TREE_DELETED)
  3745. ret = process_deleted_xattr(sctx);
  3746. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  3747. ret = process_changed_xattr(sctx);
  3748. }
  3749. return ret;
  3750. }
  3751. /*
  3752. * Process new/deleted/changed extents. We skip processing in the
  3753. * cur_inode_new_gen case because changed_inode did already initiate processing
  3754. * of extents. The reason is the same as in changed_ref
  3755. */
  3756. static int changed_extent(struct send_ctx *sctx,
  3757. enum btrfs_compare_tree_result result)
  3758. {
  3759. int ret = 0;
  3760. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  3761. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  3762. if (result != BTRFS_COMPARE_TREE_DELETED)
  3763. ret = process_extent(sctx, sctx->left_path,
  3764. sctx->cmp_key);
  3765. }
  3766. return ret;
  3767. }
  3768. /*
  3769. * Updates compare related fields in sctx and simply forwards to the actual
  3770. * changed_xxx functions.
  3771. */
  3772. static int changed_cb(struct btrfs_root *left_root,
  3773. struct btrfs_root *right_root,
  3774. struct btrfs_path *left_path,
  3775. struct btrfs_path *right_path,
  3776. struct btrfs_key *key,
  3777. enum btrfs_compare_tree_result result,
  3778. void *ctx)
  3779. {
  3780. int ret = 0;
  3781. struct send_ctx *sctx = ctx;
  3782. sctx->left_path = left_path;
  3783. sctx->right_path = right_path;
  3784. sctx->cmp_key = key;
  3785. ret = finish_inode_if_needed(sctx, 0);
  3786. if (ret < 0)
  3787. goto out;
  3788. /* Ignore non-FS objects */
  3789. if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
  3790. key->objectid == BTRFS_FREE_SPACE_OBJECTID)
  3791. goto out;
  3792. if (key->type == BTRFS_INODE_ITEM_KEY)
  3793. ret = changed_inode(sctx, result);
  3794. else if (key->type == BTRFS_INODE_REF_KEY ||
  3795. key->type == BTRFS_INODE_EXTREF_KEY)
  3796. ret = changed_ref(sctx, result);
  3797. else if (key->type == BTRFS_XATTR_ITEM_KEY)
  3798. ret = changed_xattr(sctx, result);
  3799. else if (key->type == BTRFS_EXTENT_DATA_KEY)
  3800. ret = changed_extent(sctx, result);
  3801. out:
  3802. return ret;
  3803. }
  3804. static int full_send_tree(struct send_ctx *sctx)
  3805. {
  3806. int ret;
  3807. struct btrfs_trans_handle *trans = NULL;
  3808. struct btrfs_root *send_root = sctx->send_root;
  3809. struct btrfs_key key;
  3810. struct btrfs_key found_key;
  3811. struct btrfs_path *path;
  3812. struct extent_buffer *eb;
  3813. int slot;
  3814. u64 start_ctransid;
  3815. u64 ctransid;
  3816. path = alloc_path_for_send();
  3817. if (!path)
  3818. return -ENOMEM;
  3819. spin_lock(&send_root->root_item_lock);
  3820. start_ctransid = btrfs_root_ctransid(&send_root->root_item);
  3821. spin_unlock(&send_root->root_item_lock);
  3822. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  3823. key.type = BTRFS_INODE_ITEM_KEY;
  3824. key.offset = 0;
  3825. join_trans:
  3826. /*
  3827. * We need to make sure the transaction does not get committed
  3828. * while we do anything on commit roots. Join a transaction to prevent
  3829. * this.
  3830. */
  3831. trans = btrfs_join_transaction(send_root);
  3832. if (IS_ERR(trans)) {
  3833. ret = PTR_ERR(trans);
  3834. trans = NULL;
  3835. goto out;
  3836. }
  3837. /*
  3838. * Make sure the tree has not changed after re-joining. We detect this
  3839. * by comparing start_ctransid and ctransid. They should always match.
  3840. */
  3841. spin_lock(&send_root->root_item_lock);
  3842. ctransid = btrfs_root_ctransid(&send_root->root_item);
  3843. spin_unlock(&send_root->root_item_lock);
  3844. if (ctransid != start_ctransid) {
  3845. WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
  3846. "send was modified in between. This is "
  3847. "probably a bug.\n");
  3848. ret = -EIO;
  3849. goto out;
  3850. }
  3851. ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
  3852. if (ret < 0)
  3853. goto out;
  3854. if (ret)
  3855. goto out_finish;
  3856. while (1) {
  3857. /*
  3858. * When someone want to commit while we iterate, end the
  3859. * joined transaction and rejoin.
  3860. */
  3861. if (btrfs_should_end_transaction(trans, send_root)) {
  3862. ret = btrfs_end_transaction(trans, send_root);
  3863. trans = NULL;
  3864. if (ret < 0)
  3865. goto out;
  3866. btrfs_release_path(path);
  3867. goto join_trans;
  3868. }
  3869. eb = path->nodes[0];
  3870. slot = path->slots[0];
  3871. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3872. ret = changed_cb(send_root, NULL, path, NULL,
  3873. &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
  3874. if (ret < 0)
  3875. goto out;
  3876. key.objectid = found_key.objectid;
  3877. key.type = found_key.type;
  3878. key.offset = found_key.offset + 1;
  3879. ret = btrfs_next_item(send_root, path);
  3880. if (ret < 0)
  3881. goto out;
  3882. if (ret) {
  3883. ret = 0;
  3884. break;
  3885. }
  3886. }
  3887. out_finish:
  3888. ret = finish_inode_if_needed(sctx, 1);
  3889. out:
  3890. btrfs_free_path(path);
  3891. if (trans) {
  3892. if (!ret)
  3893. ret = btrfs_end_transaction(trans, send_root);
  3894. else
  3895. btrfs_end_transaction(trans, send_root);
  3896. }
  3897. return ret;
  3898. }
  3899. static int send_subvol(struct send_ctx *sctx)
  3900. {
  3901. int ret;
  3902. ret = send_header(sctx);
  3903. if (ret < 0)
  3904. goto out;
  3905. ret = send_subvol_begin(sctx);
  3906. if (ret < 0)
  3907. goto out;
  3908. if (sctx->parent_root) {
  3909. ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
  3910. changed_cb, sctx);
  3911. if (ret < 0)
  3912. goto out;
  3913. ret = finish_inode_if_needed(sctx, 1);
  3914. if (ret < 0)
  3915. goto out;
  3916. } else {
  3917. ret = full_send_tree(sctx);
  3918. if (ret < 0)
  3919. goto out;
  3920. }
  3921. out:
  3922. if (!ret)
  3923. ret = close_cur_inode_file(sctx);
  3924. else
  3925. close_cur_inode_file(sctx);
  3926. free_recorded_refs(sctx);
  3927. return ret;
  3928. }
  3929. long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
  3930. {
  3931. int ret = 0;
  3932. struct btrfs_root *send_root;
  3933. struct btrfs_root *clone_root;
  3934. struct btrfs_fs_info *fs_info;
  3935. struct btrfs_ioctl_send_args *arg = NULL;
  3936. struct btrfs_key key;
  3937. struct send_ctx *sctx = NULL;
  3938. u32 i;
  3939. u64 *clone_sources_tmp = NULL;
  3940. if (!capable(CAP_SYS_ADMIN))
  3941. return -EPERM;
  3942. send_root = BTRFS_I(file_inode(mnt_file))->root;
  3943. fs_info = send_root->fs_info;
  3944. arg = memdup_user(arg_, sizeof(*arg));
  3945. if (IS_ERR(arg)) {
  3946. ret = PTR_ERR(arg);
  3947. arg = NULL;
  3948. goto out;
  3949. }
  3950. if (!access_ok(VERIFY_READ, arg->clone_sources,
  3951. sizeof(*arg->clone_sources *
  3952. arg->clone_sources_count))) {
  3953. ret = -EFAULT;
  3954. goto out;
  3955. }
  3956. if (arg->flags & ~BTRFS_SEND_FLAG_NO_FILE_DATA) {
  3957. ret = -EINVAL;
  3958. goto out;
  3959. }
  3960. sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
  3961. if (!sctx) {
  3962. ret = -ENOMEM;
  3963. goto out;
  3964. }
  3965. INIT_LIST_HEAD(&sctx->new_refs);
  3966. INIT_LIST_HEAD(&sctx->deleted_refs);
  3967. INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
  3968. INIT_LIST_HEAD(&sctx->name_cache_list);
  3969. sctx->flags = arg->flags;
  3970. sctx->send_filp = fget(arg->send_fd);
  3971. if (IS_ERR(sctx->send_filp)) {
  3972. ret = PTR_ERR(sctx->send_filp);
  3973. goto out;
  3974. }
  3975. sctx->mnt = mnt_file->f_path.mnt;
  3976. sctx->send_root = send_root;
  3977. sctx->clone_roots_cnt = arg->clone_sources_count;
  3978. sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
  3979. sctx->send_buf = vmalloc(sctx->send_max_size);
  3980. if (!sctx->send_buf) {
  3981. ret = -ENOMEM;
  3982. goto out;
  3983. }
  3984. sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
  3985. if (!sctx->read_buf) {
  3986. ret = -ENOMEM;
  3987. goto out;
  3988. }
  3989. sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
  3990. (arg->clone_sources_count + 1));
  3991. if (!sctx->clone_roots) {
  3992. ret = -ENOMEM;
  3993. goto out;
  3994. }
  3995. if (arg->clone_sources_count) {
  3996. clone_sources_tmp = vmalloc(arg->clone_sources_count *
  3997. sizeof(*arg->clone_sources));
  3998. if (!clone_sources_tmp) {
  3999. ret = -ENOMEM;
  4000. goto out;
  4001. }
  4002. ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
  4003. arg->clone_sources_count *
  4004. sizeof(*arg->clone_sources));
  4005. if (ret) {
  4006. ret = -EFAULT;
  4007. goto out;
  4008. }
  4009. for (i = 0; i < arg->clone_sources_count; i++) {
  4010. key.objectid = clone_sources_tmp[i];
  4011. key.type = BTRFS_ROOT_ITEM_KEY;
  4012. key.offset = (u64)-1;
  4013. clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
  4014. if (!clone_root) {
  4015. ret = -EINVAL;
  4016. goto out;
  4017. }
  4018. if (IS_ERR(clone_root)) {
  4019. ret = PTR_ERR(clone_root);
  4020. goto out;
  4021. }
  4022. sctx->clone_roots[i].root = clone_root;
  4023. }
  4024. vfree(clone_sources_tmp);
  4025. clone_sources_tmp = NULL;
  4026. }
  4027. if (arg->parent_root) {
  4028. key.objectid = arg->parent_root;
  4029. key.type = BTRFS_ROOT_ITEM_KEY;
  4030. key.offset = (u64)-1;
  4031. sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
  4032. if (!sctx->parent_root) {
  4033. ret = -EINVAL;
  4034. goto out;
  4035. }
  4036. }
  4037. /*
  4038. * Clones from send_root are allowed, but only if the clone source
  4039. * is behind the current send position. This is checked while searching
  4040. * for possible clone sources.
  4041. */
  4042. sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
  4043. /* We do a bsearch later */
  4044. sort(sctx->clone_roots, sctx->clone_roots_cnt,
  4045. sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
  4046. NULL);
  4047. ret = send_subvol(sctx);
  4048. if (ret < 0)
  4049. goto out;
  4050. ret = begin_cmd(sctx, BTRFS_SEND_C_END);
  4051. if (ret < 0)
  4052. goto out;
  4053. ret = send_cmd(sctx);
  4054. if (ret < 0)
  4055. goto out;
  4056. out:
  4057. kfree(arg);
  4058. vfree(clone_sources_tmp);
  4059. if (sctx) {
  4060. if (sctx->send_filp)
  4061. fput(sctx->send_filp);
  4062. vfree(sctx->clone_roots);
  4063. vfree(sctx->send_buf);
  4064. vfree(sctx->read_buf);
  4065. name_cache_free(sctx);
  4066. kfree(sctx);
  4067. }
  4068. return ret;
  4069. }