send.c 110 KB

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