send.c 99 KB

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