send.c 109 KB

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