send.c 96 KB

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