send.c 97 KB

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