send.c 105 KB

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