send.c 103 KB

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