send.c 109 KB

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