send.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868
  1. /*
  2. * Copyright (C) 2012 Alexander Block. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/bsearch.h>
  19. #include <linux/fs.h>
  20. #include <linux/file.h>
  21. #include <linux/sort.h>
  22. #include <linux/mount.h>
  23. #include <linux/xattr.h>
  24. #include <linux/posix_acl_xattr.h>
  25. #include <linux/radix-tree.h>
  26. #include <linux/crc32c.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/string.h>
  29. #include "send.h"
  30. #include "backref.h"
  31. #include "locking.h"
  32. #include "disk-io.h"
  33. #include "btrfs_inode.h"
  34. #include "transaction.h"
  35. static int g_verbose = 0;
  36. #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
  37. /*
  38. * A fs_path is a helper to dynamically build path names with unknown size.
  39. * It reallocates the internal buffer on demand.
  40. * It allows fast adding of path elements on the right side (normal path) and
  41. * fast adding to the left side (reversed path). A reversed path can also be
  42. * unreversed if needed.
  43. */
  44. struct fs_path {
  45. union {
  46. struct {
  47. char *start;
  48. char *end;
  49. char *prepared;
  50. char *buf;
  51. int buf_len;
  52. unsigned int reversed:1;
  53. unsigned int virtual_mem:1;
  54. char inline_buf[];
  55. };
  56. char pad[PAGE_SIZE];
  57. };
  58. };
  59. #define FS_PATH_INLINE_SIZE \
  60. (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
  61. /* reused for each extent */
  62. struct clone_root {
  63. struct btrfs_root *root;
  64. u64 ino;
  65. u64 offset;
  66. u64 found_refs;
  67. };
  68. #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
  69. #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
  70. struct send_ctx {
  71. struct file *send_filp;
  72. loff_t send_off;
  73. char *send_buf;
  74. u32 send_size;
  75. u32 send_max_size;
  76. u64 total_send_size;
  77. u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
  78. u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
  79. struct vfsmount *mnt;
  80. struct btrfs_root *send_root;
  81. struct btrfs_root *parent_root;
  82. struct clone_root *clone_roots;
  83. int clone_roots_cnt;
  84. /* current state of the compare_tree call */
  85. struct btrfs_path *left_path;
  86. struct btrfs_path *right_path;
  87. struct btrfs_key *cmp_key;
  88. /*
  89. * infos of the currently processed inode. In case of deleted inodes,
  90. * these are the values from the deleted inode.
  91. */
  92. u64 cur_ino;
  93. u64 cur_inode_gen;
  94. int cur_inode_new;
  95. int cur_inode_new_gen;
  96. int cur_inode_deleted;
  97. u64 cur_inode_size;
  98. u64 cur_inode_mode;
  99. u64 send_progress;
  100. struct list_head new_refs;
  101. struct list_head deleted_refs;
  102. struct radix_tree_root name_cache;
  103. struct list_head name_cache_list;
  104. int name_cache_size;
  105. char *read_buf;
  106. };
  107. struct name_cache_entry {
  108. struct list_head list;
  109. /*
  110. * radix_tree has only 32bit entries but we need to handle 64bit inums.
  111. * We use the lower 32bit of the 64bit inum to store it in the tree. If
  112. * more then one inum would fall into the same entry, we use radix_list
  113. * to store the additional entries. radix_list is also used to store
  114. * entries where two entries have the same inum but different
  115. * generations.
  116. */
  117. struct list_head radix_list;
  118. u64 ino;
  119. u64 gen;
  120. u64 parent_ino;
  121. u64 parent_gen;
  122. int ret;
  123. int need_later_update;
  124. int name_len;
  125. char name[];
  126. };
  127. static void fs_path_reset(struct fs_path *p)
  128. {
  129. if (p->reversed) {
  130. p->start = p->buf + p->buf_len - 1;
  131. p->end = p->start;
  132. *p->start = 0;
  133. } else {
  134. p->start = p->buf;
  135. p->end = p->start;
  136. *p->start = 0;
  137. }
  138. }
  139. static struct fs_path *fs_path_alloc(void)
  140. {
  141. struct fs_path *p;
  142. p = kmalloc(sizeof(*p), GFP_NOFS);
  143. if (!p)
  144. return NULL;
  145. p->reversed = 0;
  146. p->virtual_mem = 0;
  147. p->buf = p->inline_buf;
  148. p->buf_len = FS_PATH_INLINE_SIZE;
  149. fs_path_reset(p);
  150. return p;
  151. }
  152. static struct fs_path *fs_path_alloc_reversed(void)
  153. {
  154. struct fs_path *p;
  155. p = fs_path_alloc();
  156. if (!p)
  157. return NULL;
  158. p->reversed = 1;
  159. fs_path_reset(p);
  160. return p;
  161. }
  162. static void fs_path_free(struct fs_path *p)
  163. {
  164. if (!p)
  165. return;
  166. if (p->buf != p->inline_buf) {
  167. if (p->virtual_mem)
  168. vfree(p->buf);
  169. else
  170. kfree(p->buf);
  171. }
  172. kfree(p);
  173. }
  174. static int fs_path_len(struct fs_path *p)
  175. {
  176. return p->end - p->start;
  177. }
  178. static int fs_path_ensure_buf(struct fs_path *p, int len)
  179. {
  180. char *tmp_buf;
  181. int path_len;
  182. int old_buf_len;
  183. len++;
  184. if (p->buf_len >= len)
  185. return 0;
  186. path_len = p->end - p->start;
  187. old_buf_len = p->buf_len;
  188. len = PAGE_ALIGN(len);
  189. if (p->buf == p->inline_buf) {
  190. tmp_buf = kmalloc(len, GFP_NOFS | __GFP_NOWARN);
  191. if (!tmp_buf) {
  192. tmp_buf = vmalloc(len);
  193. if (!tmp_buf)
  194. return -ENOMEM;
  195. p->virtual_mem = 1;
  196. }
  197. memcpy(tmp_buf, p->buf, p->buf_len);
  198. p->buf = tmp_buf;
  199. p->buf_len = len;
  200. } else {
  201. if (p->virtual_mem) {
  202. tmp_buf = vmalloc(len);
  203. if (!tmp_buf)
  204. return -ENOMEM;
  205. memcpy(tmp_buf, p->buf, p->buf_len);
  206. vfree(p->buf);
  207. } else {
  208. tmp_buf = krealloc(p->buf, len, GFP_NOFS);
  209. if (!tmp_buf) {
  210. tmp_buf = vmalloc(len);
  211. if (!tmp_buf)
  212. return -ENOMEM;
  213. memcpy(tmp_buf, p->buf, p->buf_len);
  214. kfree(p->buf);
  215. p->virtual_mem = 1;
  216. }
  217. }
  218. p->buf = tmp_buf;
  219. p->buf_len = len;
  220. }
  221. if (p->reversed) {
  222. tmp_buf = p->buf + old_buf_len - path_len - 1;
  223. p->end = p->buf + p->buf_len - 1;
  224. p->start = p->end - path_len;
  225. memmove(p->start, tmp_buf, path_len + 1);
  226. } else {
  227. p->start = p->buf;
  228. p->end = p->start + path_len;
  229. }
  230. return 0;
  231. }
  232. static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
  233. {
  234. int ret;
  235. int new_len;
  236. new_len = p->end - p->start + name_len;
  237. if (p->start != p->end)
  238. new_len++;
  239. ret = fs_path_ensure_buf(p, new_len);
  240. if (ret < 0)
  241. goto out;
  242. if (p->reversed) {
  243. if (p->start != p->end)
  244. *--p->start = '/';
  245. p->start -= name_len;
  246. p->prepared = p->start;
  247. } else {
  248. if (p->start != p->end)
  249. *p->end++ = '/';
  250. p->prepared = p->end;
  251. p->end += name_len;
  252. *p->end = 0;
  253. }
  254. out:
  255. return ret;
  256. }
  257. static int fs_path_add(struct fs_path *p, const char *name, int name_len)
  258. {
  259. int ret;
  260. ret = fs_path_prepare_for_add(p, name_len);
  261. if (ret < 0)
  262. goto out;
  263. memcpy(p->prepared, name, name_len);
  264. p->prepared = NULL;
  265. out:
  266. return ret;
  267. }
  268. static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
  269. {
  270. int ret;
  271. ret = fs_path_prepare_for_add(p, p2->end - p2->start);
  272. if (ret < 0)
  273. goto out;
  274. memcpy(p->prepared, p2->start, p2->end - p2->start);
  275. p->prepared = NULL;
  276. out:
  277. return ret;
  278. }
  279. static int fs_path_add_from_extent_buffer(struct fs_path *p,
  280. struct extent_buffer *eb,
  281. unsigned long off, int len)
  282. {
  283. int ret;
  284. ret = fs_path_prepare_for_add(p, len);
  285. if (ret < 0)
  286. goto out;
  287. read_extent_buffer(eb, p->prepared, off, len);
  288. p->prepared = NULL;
  289. out:
  290. return ret;
  291. }
  292. #if 0
  293. static void fs_path_remove(struct fs_path *p)
  294. {
  295. BUG_ON(p->reversed);
  296. while (p->start != p->end && *p->end != '/')
  297. p->end--;
  298. *p->end = 0;
  299. }
  300. #endif
  301. static int fs_path_copy(struct fs_path *p, struct fs_path *from)
  302. {
  303. int ret;
  304. p->reversed = from->reversed;
  305. fs_path_reset(p);
  306. ret = fs_path_add_path(p, from);
  307. return ret;
  308. }
  309. static void fs_path_unreverse(struct fs_path *p)
  310. {
  311. char *tmp;
  312. int len;
  313. if (!p->reversed)
  314. return;
  315. tmp = p->start;
  316. len = p->end - p->start;
  317. p->start = p->buf;
  318. p->end = p->start + len;
  319. memmove(p->start, tmp, len + 1);
  320. p->reversed = 0;
  321. }
  322. static struct btrfs_path *alloc_path_for_send(void)
  323. {
  324. struct btrfs_path *path;
  325. path = btrfs_alloc_path();
  326. if (!path)
  327. return NULL;
  328. path->search_commit_root = 1;
  329. path->skip_locking = 1;
  330. return path;
  331. }
  332. static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
  333. {
  334. int ret;
  335. mm_segment_t old_fs;
  336. u32 pos = 0;
  337. old_fs = get_fs();
  338. set_fs(KERNEL_DS);
  339. while (pos < len) {
  340. ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
  341. /* TODO handle that correctly */
  342. /*if (ret == -ERESTARTSYS) {
  343. continue;
  344. }*/
  345. if (ret < 0)
  346. goto out;
  347. if (ret == 0) {
  348. ret = -EIO;
  349. goto out;
  350. }
  351. pos += ret;
  352. }
  353. ret = 0;
  354. out:
  355. set_fs(old_fs);
  356. return ret;
  357. }
  358. static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
  359. {
  360. struct btrfs_tlv_header *hdr;
  361. int total_len = sizeof(*hdr) + len;
  362. int left = sctx->send_max_size - sctx->send_size;
  363. if (unlikely(left < total_len))
  364. return -EOVERFLOW;
  365. hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
  366. hdr->tlv_type = cpu_to_le16(attr);
  367. hdr->tlv_len = cpu_to_le16(len);
  368. memcpy(hdr + 1, data, len);
  369. sctx->send_size += total_len;
  370. return 0;
  371. }
  372. #if 0
  373. static int tlv_put_u8(struct send_ctx *sctx, u16 attr, u8 value)
  374. {
  375. return tlv_put(sctx, attr, &value, sizeof(value));
  376. }
  377. static int tlv_put_u16(struct send_ctx *sctx, u16 attr, u16 value)
  378. {
  379. __le16 tmp = cpu_to_le16(value);
  380. return tlv_put(sctx, attr, &tmp, sizeof(tmp));
  381. }
  382. static int tlv_put_u32(struct send_ctx *sctx, u16 attr, u32 value)
  383. {
  384. __le32 tmp = cpu_to_le32(value);
  385. return tlv_put(sctx, attr, &tmp, sizeof(tmp));
  386. }
  387. #endif
  388. static int tlv_put_u64(struct send_ctx *sctx, u16 attr, u64 value)
  389. {
  390. __le64 tmp = cpu_to_le64(value);
  391. return tlv_put(sctx, attr, &tmp, sizeof(tmp));
  392. }
  393. static int tlv_put_string(struct send_ctx *sctx, u16 attr,
  394. const char *str, int len)
  395. {
  396. if (len == -1)
  397. len = strlen(str);
  398. return tlv_put(sctx, attr, str, len);
  399. }
  400. static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
  401. const u8 *uuid)
  402. {
  403. return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
  404. }
  405. #if 0
  406. static int tlv_put_timespec(struct send_ctx *sctx, u16 attr,
  407. struct timespec *ts)
  408. {
  409. struct btrfs_timespec bts;
  410. bts.sec = cpu_to_le64(ts->tv_sec);
  411. bts.nsec = cpu_to_le32(ts->tv_nsec);
  412. return tlv_put(sctx, attr, &bts, sizeof(bts));
  413. }
  414. #endif
  415. static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
  416. struct extent_buffer *eb,
  417. struct btrfs_timespec *ts)
  418. {
  419. struct btrfs_timespec bts;
  420. read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
  421. return tlv_put(sctx, attr, &bts, sizeof(bts));
  422. }
  423. #define TLV_PUT(sctx, attrtype, attrlen, data) \
  424. do { \
  425. ret = tlv_put(sctx, attrtype, attrlen, data); \
  426. if (ret < 0) \
  427. goto tlv_put_failure; \
  428. } while (0)
  429. #define TLV_PUT_INT(sctx, attrtype, bits, value) \
  430. do { \
  431. ret = tlv_put_u##bits(sctx, attrtype, value); \
  432. if (ret < 0) \
  433. goto tlv_put_failure; \
  434. } while (0)
  435. #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
  436. #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
  437. #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
  438. #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
  439. #define TLV_PUT_STRING(sctx, attrtype, str, len) \
  440. do { \
  441. ret = tlv_put_string(sctx, attrtype, str, len); \
  442. if (ret < 0) \
  443. goto tlv_put_failure; \
  444. } while (0)
  445. #define TLV_PUT_PATH(sctx, attrtype, p) \
  446. do { \
  447. ret = tlv_put_string(sctx, attrtype, p->start, \
  448. p->end - p->start); \
  449. if (ret < 0) \
  450. goto tlv_put_failure; \
  451. } while(0)
  452. #define TLV_PUT_UUID(sctx, attrtype, uuid) \
  453. do { \
  454. ret = tlv_put_uuid(sctx, attrtype, uuid); \
  455. if (ret < 0) \
  456. goto tlv_put_failure; \
  457. } while (0)
  458. #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
  459. do { \
  460. ret = tlv_put_timespec(sctx, attrtype, ts); \
  461. if (ret < 0) \
  462. goto tlv_put_failure; \
  463. } while (0)
  464. #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
  465. do { \
  466. ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
  467. if (ret < 0) \
  468. goto tlv_put_failure; \
  469. } while (0)
  470. static int send_header(struct send_ctx *sctx)
  471. {
  472. struct btrfs_stream_header hdr;
  473. strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
  474. hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
  475. return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
  476. &sctx->send_off);
  477. }
  478. /*
  479. * For each command/item we want to send to userspace, we call this function.
  480. */
  481. static int begin_cmd(struct send_ctx *sctx, int cmd)
  482. {
  483. struct btrfs_cmd_header *hdr;
  484. if (!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(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(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. * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
  1828. */
  1829. static int send_subvol_begin(struct send_ctx *sctx)
  1830. {
  1831. int ret;
  1832. struct btrfs_root *send_root = sctx->send_root;
  1833. struct btrfs_root *parent_root = sctx->parent_root;
  1834. struct btrfs_path *path;
  1835. struct btrfs_key key;
  1836. struct btrfs_root_ref *ref;
  1837. struct extent_buffer *leaf;
  1838. char *name = NULL;
  1839. int namelen;
  1840. path = alloc_path_for_send();
  1841. if (!path)
  1842. return -ENOMEM;
  1843. name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
  1844. if (!name) {
  1845. btrfs_free_path(path);
  1846. return -ENOMEM;
  1847. }
  1848. key.objectid = send_root->objectid;
  1849. key.type = BTRFS_ROOT_BACKREF_KEY;
  1850. key.offset = 0;
  1851. ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
  1852. &key, path, 1, 0);
  1853. if (ret < 0)
  1854. goto out;
  1855. if (ret) {
  1856. ret = -ENOENT;
  1857. goto out;
  1858. }
  1859. leaf = path->nodes[0];
  1860. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1861. if (key.type != BTRFS_ROOT_BACKREF_KEY ||
  1862. key.objectid != send_root->objectid) {
  1863. ret = -ENOENT;
  1864. goto out;
  1865. }
  1866. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  1867. namelen = btrfs_root_ref_name_len(leaf, ref);
  1868. read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
  1869. btrfs_release_path(path);
  1870. if (parent_root) {
  1871. ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
  1872. if (ret < 0)
  1873. goto out;
  1874. } else {
  1875. ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
  1876. if (ret < 0)
  1877. goto out;
  1878. }
  1879. TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
  1880. TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
  1881. sctx->send_root->root_item.uuid);
  1882. TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
  1883. sctx->send_root->root_item.ctransid);
  1884. if (parent_root) {
  1885. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  1886. sctx->parent_root->root_item.uuid);
  1887. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  1888. sctx->parent_root->root_item.ctransid);
  1889. }
  1890. ret = send_cmd(sctx);
  1891. tlv_put_failure:
  1892. out:
  1893. btrfs_free_path(path);
  1894. kfree(name);
  1895. return ret;
  1896. }
  1897. static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
  1898. {
  1899. int ret = 0;
  1900. struct fs_path *p;
  1901. verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
  1902. p = fs_path_alloc();
  1903. if (!p)
  1904. return -ENOMEM;
  1905. ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
  1906. if (ret < 0)
  1907. goto out;
  1908. ret = get_cur_path(sctx, ino, gen, p);
  1909. if (ret < 0)
  1910. goto out;
  1911. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  1912. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
  1913. ret = send_cmd(sctx);
  1914. tlv_put_failure:
  1915. out:
  1916. fs_path_free(p);
  1917. return ret;
  1918. }
  1919. static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
  1920. {
  1921. int ret = 0;
  1922. struct fs_path *p;
  1923. verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
  1924. p = fs_path_alloc();
  1925. if (!p)
  1926. return -ENOMEM;
  1927. ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
  1928. if (ret < 0)
  1929. goto out;
  1930. ret = get_cur_path(sctx, ino, gen, p);
  1931. if (ret < 0)
  1932. goto out;
  1933. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  1934. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
  1935. ret = send_cmd(sctx);
  1936. tlv_put_failure:
  1937. out:
  1938. fs_path_free(p);
  1939. return ret;
  1940. }
  1941. static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
  1942. {
  1943. int ret = 0;
  1944. struct fs_path *p;
  1945. verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
  1946. p = fs_path_alloc();
  1947. if (!p)
  1948. return -ENOMEM;
  1949. ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
  1950. if (ret < 0)
  1951. goto out;
  1952. ret = get_cur_path(sctx, ino, gen, p);
  1953. if (ret < 0)
  1954. goto out;
  1955. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  1956. TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
  1957. TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
  1958. ret = send_cmd(sctx);
  1959. tlv_put_failure:
  1960. out:
  1961. fs_path_free(p);
  1962. return ret;
  1963. }
  1964. static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
  1965. {
  1966. int ret = 0;
  1967. struct fs_path *p = NULL;
  1968. struct btrfs_inode_item *ii;
  1969. struct btrfs_path *path = NULL;
  1970. struct extent_buffer *eb;
  1971. struct btrfs_key key;
  1972. int slot;
  1973. verbose_printk("btrfs: send_utimes %llu\n", ino);
  1974. p = fs_path_alloc();
  1975. if (!p)
  1976. return -ENOMEM;
  1977. path = alloc_path_for_send();
  1978. if (!path) {
  1979. ret = -ENOMEM;
  1980. goto out;
  1981. }
  1982. key.objectid = ino;
  1983. key.type = BTRFS_INODE_ITEM_KEY;
  1984. key.offset = 0;
  1985. ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
  1986. if (ret < 0)
  1987. goto out;
  1988. eb = path->nodes[0];
  1989. slot = path->slots[0];
  1990. ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  1991. ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
  1992. if (ret < 0)
  1993. goto out;
  1994. ret = get_cur_path(sctx, ino, gen, p);
  1995. if (ret < 0)
  1996. goto out;
  1997. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  1998. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
  1999. btrfs_inode_atime(ii));
  2000. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
  2001. btrfs_inode_mtime(ii));
  2002. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
  2003. btrfs_inode_ctime(ii));
  2004. /* TODO Add otime support when the otime patches get into upstream */
  2005. ret = send_cmd(sctx);
  2006. tlv_put_failure:
  2007. out:
  2008. fs_path_free(p);
  2009. btrfs_free_path(path);
  2010. return ret;
  2011. }
  2012. /*
  2013. * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
  2014. * a valid path yet because we did not process the refs yet. So, the inode
  2015. * is created as orphan.
  2016. */
  2017. static int send_create_inode(struct send_ctx *sctx, u64 ino)
  2018. {
  2019. int ret = 0;
  2020. struct fs_path *p;
  2021. int cmd;
  2022. u64 gen;
  2023. u64 mode;
  2024. u64 rdev;
  2025. verbose_printk("btrfs: send_create_inode %llu\n", ino);
  2026. p = fs_path_alloc();
  2027. if (!p)
  2028. return -ENOMEM;
  2029. ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
  2030. NULL, &rdev);
  2031. if (ret < 0)
  2032. goto out;
  2033. if (S_ISREG(mode)) {
  2034. cmd = BTRFS_SEND_C_MKFILE;
  2035. } else if (S_ISDIR(mode)) {
  2036. cmd = BTRFS_SEND_C_MKDIR;
  2037. } else if (S_ISLNK(mode)) {
  2038. cmd = BTRFS_SEND_C_SYMLINK;
  2039. } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
  2040. cmd = BTRFS_SEND_C_MKNOD;
  2041. } else if (S_ISFIFO(mode)) {
  2042. cmd = BTRFS_SEND_C_MKFIFO;
  2043. } else if (S_ISSOCK(mode)) {
  2044. cmd = BTRFS_SEND_C_MKSOCK;
  2045. } else {
  2046. printk(KERN_WARNING "btrfs: unexpected inode type %o",
  2047. (int)(mode & S_IFMT));
  2048. ret = -ENOTSUPP;
  2049. goto out;
  2050. }
  2051. ret = begin_cmd(sctx, cmd);
  2052. if (ret < 0)
  2053. goto out;
  2054. ret = gen_unique_name(sctx, ino, gen, p);
  2055. if (ret < 0)
  2056. goto out;
  2057. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2058. TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
  2059. if (S_ISLNK(mode)) {
  2060. fs_path_reset(p);
  2061. ret = read_symlink(sctx->send_root, ino, p);
  2062. if (ret < 0)
  2063. goto out;
  2064. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
  2065. } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
  2066. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  2067. TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
  2068. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
  2069. }
  2070. ret = send_cmd(sctx);
  2071. if (ret < 0)
  2072. goto out;
  2073. tlv_put_failure:
  2074. out:
  2075. fs_path_free(p);
  2076. return ret;
  2077. }
  2078. /*
  2079. * We need some special handling for inodes that get processed before the parent
  2080. * directory got created. See process_recorded_refs for details.
  2081. * This function does the check if we already created the dir out of order.
  2082. */
  2083. static int did_create_dir(struct send_ctx *sctx, u64 dir)
  2084. {
  2085. int ret = 0;
  2086. struct btrfs_path *path = NULL;
  2087. struct btrfs_key key;
  2088. struct btrfs_key found_key;
  2089. struct btrfs_key di_key;
  2090. struct extent_buffer *eb;
  2091. struct btrfs_dir_item *di;
  2092. int slot;
  2093. path = alloc_path_for_send();
  2094. if (!path) {
  2095. ret = -ENOMEM;
  2096. goto out;
  2097. }
  2098. key.objectid = dir;
  2099. key.type = BTRFS_DIR_INDEX_KEY;
  2100. key.offset = 0;
  2101. while (1) {
  2102. ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
  2103. 1, 0);
  2104. if (ret < 0)
  2105. goto out;
  2106. if (!ret) {
  2107. eb = path->nodes[0];
  2108. slot = path->slots[0];
  2109. btrfs_item_key_to_cpu(eb, &found_key, slot);
  2110. }
  2111. if (ret || found_key.objectid != key.objectid ||
  2112. found_key.type != key.type) {
  2113. ret = 0;
  2114. goto out;
  2115. }
  2116. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  2117. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  2118. if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
  2119. di_key.objectid < sctx->send_progress) {
  2120. ret = 1;
  2121. goto out;
  2122. }
  2123. key.offset = found_key.offset + 1;
  2124. btrfs_release_path(path);
  2125. }
  2126. out:
  2127. btrfs_free_path(path);
  2128. return ret;
  2129. }
  2130. /*
  2131. * Only creates the inode if it is:
  2132. * 1. Not a directory
  2133. * 2. Or a directory which was not created already due to out of order
  2134. * directories. See did_create_dir and process_recorded_refs for details.
  2135. */
  2136. static int send_create_inode_if_needed(struct send_ctx *sctx)
  2137. {
  2138. int ret;
  2139. if (S_ISDIR(sctx->cur_inode_mode)) {
  2140. ret = did_create_dir(sctx, sctx->cur_ino);
  2141. if (ret < 0)
  2142. goto out;
  2143. if (ret) {
  2144. ret = 0;
  2145. goto out;
  2146. }
  2147. }
  2148. ret = send_create_inode(sctx, sctx->cur_ino);
  2149. if (ret < 0)
  2150. goto out;
  2151. out:
  2152. return ret;
  2153. }
  2154. struct recorded_ref {
  2155. struct list_head list;
  2156. char *dir_path;
  2157. char *name;
  2158. struct fs_path *full_path;
  2159. u64 dir;
  2160. u64 dir_gen;
  2161. int dir_path_len;
  2162. int name_len;
  2163. };
  2164. /*
  2165. * We need to process new refs before deleted refs, but compare_tree gives us
  2166. * everything mixed. So we first record all refs and later process them.
  2167. * This function is a helper to record one ref.
  2168. */
  2169. static int record_ref(struct list_head *head, u64 dir,
  2170. u64 dir_gen, struct fs_path *path)
  2171. {
  2172. struct recorded_ref *ref;
  2173. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  2174. if (!ref)
  2175. return -ENOMEM;
  2176. ref->dir = dir;
  2177. ref->dir_gen = dir_gen;
  2178. ref->full_path = path;
  2179. ref->name = (char *)kbasename(ref->full_path->start);
  2180. ref->name_len = ref->full_path->end - ref->name;
  2181. ref->dir_path = ref->full_path->start;
  2182. if (ref->name == ref->full_path->start)
  2183. ref->dir_path_len = 0;
  2184. else
  2185. ref->dir_path_len = ref->full_path->end -
  2186. ref->full_path->start - 1 - ref->name_len;
  2187. list_add_tail(&ref->list, head);
  2188. return 0;
  2189. }
  2190. static int dup_ref(struct recorded_ref *ref, struct list_head *list)
  2191. {
  2192. struct recorded_ref *new;
  2193. new = kmalloc(sizeof(*ref), GFP_NOFS);
  2194. if (!new)
  2195. return -ENOMEM;
  2196. new->dir = ref->dir;
  2197. new->dir_gen = ref->dir_gen;
  2198. new->full_path = NULL;
  2199. INIT_LIST_HEAD(&new->list);
  2200. list_add_tail(&new->list, list);
  2201. return 0;
  2202. }
  2203. static void __free_recorded_refs(struct list_head *head)
  2204. {
  2205. struct recorded_ref *cur;
  2206. while (!list_empty(head)) {
  2207. cur = list_entry(head->next, struct recorded_ref, list);
  2208. fs_path_free(cur->full_path);
  2209. list_del(&cur->list);
  2210. kfree(cur);
  2211. }
  2212. }
  2213. static void free_recorded_refs(struct send_ctx *sctx)
  2214. {
  2215. __free_recorded_refs(&sctx->new_refs);
  2216. __free_recorded_refs(&sctx->deleted_refs);
  2217. }
  2218. /*
  2219. * Renames/moves a file/dir to its orphan name. Used when the first
  2220. * ref of an unprocessed inode gets overwritten and for all non empty
  2221. * directories.
  2222. */
  2223. static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
  2224. struct fs_path *path)
  2225. {
  2226. int ret;
  2227. struct fs_path *orphan;
  2228. orphan = fs_path_alloc();
  2229. if (!orphan)
  2230. return -ENOMEM;
  2231. ret = gen_unique_name(sctx, ino, gen, orphan);
  2232. if (ret < 0)
  2233. goto out;
  2234. ret = send_rename(sctx, path, orphan);
  2235. out:
  2236. fs_path_free(orphan);
  2237. return ret;
  2238. }
  2239. /*
  2240. * Returns 1 if a directory can be removed at this point in time.
  2241. * We check this by iterating all dir items and checking if the inode behind
  2242. * the dir item was already processed.
  2243. */
  2244. static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
  2245. {
  2246. int ret = 0;
  2247. struct btrfs_root *root = sctx->parent_root;
  2248. struct btrfs_path *path;
  2249. struct btrfs_key key;
  2250. struct btrfs_key found_key;
  2251. struct btrfs_key loc;
  2252. struct btrfs_dir_item *di;
  2253. /*
  2254. * Don't try to rmdir the top/root subvolume dir.
  2255. */
  2256. if (dir == BTRFS_FIRST_FREE_OBJECTID)
  2257. return 0;
  2258. path = alloc_path_for_send();
  2259. if (!path)
  2260. return -ENOMEM;
  2261. key.objectid = dir;
  2262. key.type = BTRFS_DIR_INDEX_KEY;
  2263. key.offset = 0;
  2264. while (1) {
  2265. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  2266. if (ret < 0)
  2267. goto out;
  2268. if (!ret) {
  2269. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2270. path->slots[0]);
  2271. }
  2272. if (ret || found_key.objectid != key.objectid ||
  2273. found_key.type != key.type) {
  2274. break;
  2275. }
  2276. di = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2277. struct btrfs_dir_item);
  2278. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
  2279. if (loc.objectid > send_progress) {
  2280. ret = 0;
  2281. goto out;
  2282. }
  2283. btrfs_release_path(path);
  2284. key.offset = found_key.offset + 1;
  2285. }
  2286. ret = 1;
  2287. out:
  2288. btrfs_free_path(path);
  2289. return ret;
  2290. }
  2291. /*
  2292. * This does all the move/link/unlink/rmdir magic.
  2293. */
  2294. static int process_recorded_refs(struct send_ctx *sctx)
  2295. {
  2296. int ret = 0;
  2297. struct recorded_ref *cur;
  2298. struct recorded_ref *cur2;
  2299. struct list_head check_dirs;
  2300. struct fs_path *valid_path = NULL;
  2301. u64 ow_inode = 0;
  2302. u64 ow_gen;
  2303. int did_overwrite = 0;
  2304. int is_orphan = 0;
  2305. verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
  2306. /*
  2307. * This should never happen as the root dir always has the same ref
  2308. * which is always '..'
  2309. */
  2310. BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
  2311. INIT_LIST_HEAD(&check_dirs);
  2312. valid_path = fs_path_alloc();
  2313. if (!valid_path) {
  2314. ret = -ENOMEM;
  2315. goto out;
  2316. }
  2317. /*
  2318. * First, check if the first ref of the current inode was overwritten
  2319. * before. If yes, we know that the current inode was already orphanized
  2320. * and thus use the orphan name. If not, we can use get_cur_path to
  2321. * get the path of the first ref as it would like while receiving at
  2322. * this point in time.
  2323. * New inodes are always orphan at the beginning, so force to use the
  2324. * orphan name in this case.
  2325. * The first ref is stored in valid_path and will be updated if it
  2326. * gets moved around.
  2327. */
  2328. if (!sctx->cur_inode_new) {
  2329. ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
  2330. sctx->cur_inode_gen);
  2331. if (ret < 0)
  2332. goto out;
  2333. if (ret)
  2334. did_overwrite = 1;
  2335. }
  2336. if (sctx->cur_inode_new || did_overwrite) {
  2337. ret = gen_unique_name(sctx, sctx->cur_ino,
  2338. sctx->cur_inode_gen, valid_path);
  2339. if (ret < 0)
  2340. goto out;
  2341. is_orphan = 1;
  2342. } else {
  2343. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  2344. valid_path);
  2345. if (ret < 0)
  2346. goto out;
  2347. }
  2348. list_for_each_entry(cur, &sctx->new_refs, list) {
  2349. /*
  2350. * We may have refs where the parent directory does not exist
  2351. * yet. This happens if the parent directories inum is higher
  2352. * the the current inum. To handle this case, we create the
  2353. * parent directory out of order. But we need to check if this
  2354. * did already happen before due to other refs in the same dir.
  2355. */
  2356. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
  2357. if (ret < 0)
  2358. goto out;
  2359. if (ret == inode_state_will_create) {
  2360. ret = 0;
  2361. /*
  2362. * First check if any of the current inodes refs did
  2363. * already create the dir.
  2364. */
  2365. list_for_each_entry(cur2, &sctx->new_refs, list) {
  2366. if (cur == cur2)
  2367. break;
  2368. if (cur2->dir == cur->dir) {
  2369. ret = 1;
  2370. break;
  2371. }
  2372. }
  2373. /*
  2374. * If that did not happen, check if a previous inode
  2375. * did already create the dir.
  2376. */
  2377. if (!ret)
  2378. ret = did_create_dir(sctx, cur->dir);
  2379. if (ret < 0)
  2380. goto out;
  2381. if (!ret) {
  2382. ret = send_create_inode(sctx, cur->dir);
  2383. if (ret < 0)
  2384. goto out;
  2385. }
  2386. }
  2387. /*
  2388. * Check if this new ref would overwrite the first ref of
  2389. * another unprocessed inode. If yes, orphanize the
  2390. * overwritten inode. If we find an overwritten ref that is
  2391. * not the first ref, simply unlink it.
  2392. */
  2393. ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  2394. cur->name, cur->name_len,
  2395. &ow_inode, &ow_gen);
  2396. if (ret < 0)
  2397. goto out;
  2398. if (ret) {
  2399. ret = is_first_ref(sctx->parent_root,
  2400. ow_inode, cur->dir, cur->name,
  2401. cur->name_len);
  2402. if (ret < 0)
  2403. goto out;
  2404. if (ret) {
  2405. ret = orphanize_inode(sctx, ow_inode, ow_gen,
  2406. cur->full_path);
  2407. if (ret < 0)
  2408. goto out;
  2409. } else {
  2410. ret = send_unlink(sctx, cur->full_path);
  2411. if (ret < 0)
  2412. goto out;
  2413. }
  2414. }
  2415. /*
  2416. * link/move the ref to the new place. If we have an orphan
  2417. * inode, move it and update valid_path. If not, link or move
  2418. * it depending on the inode mode.
  2419. */
  2420. if (is_orphan) {
  2421. ret = send_rename(sctx, valid_path, cur->full_path);
  2422. if (ret < 0)
  2423. goto out;
  2424. is_orphan = 0;
  2425. ret = fs_path_copy(valid_path, cur->full_path);
  2426. if (ret < 0)
  2427. goto out;
  2428. } else {
  2429. if (S_ISDIR(sctx->cur_inode_mode)) {
  2430. /*
  2431. * Dirs can't be linked, so move it. For moved
  2432. * dirs, we always have one new and one deleted
  2433. * ref. The deleted ref is ignored later.
  2434. */
  2435. ret = send_rename(sctx, valid_path,
  2436. cur->full_path);
  2437. if (ret < 0)
  2438. goto out;
  2439. ret = fs_path_copy(valid_path, cur->full_path);
  2440. if (ret < 0)
  2441. goto out;
  2442. } else {
  2443. ret = send_link(sctx, cur->full_path,
  2444. valid_path);
  2445. if (ret < 0)
  2446. goto out;
  2447. }
  2448. }
  2449. ret = dup_ref(cur, &check_dirs);
  2450. if (ret < 0)
  2451. goto out;
  2452. }
  2453. if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
  2454. /*
  2455. * Check if we can already rmdir the directory. If not,
  2456. * orphanize it. For every dir item inside that gets deleted
  2457. * later, we do this check again and rmdir it then if possible.
  2458. * See the use of check_dirs for more details.
  2459. */
  2460. ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
  2461. if (ret < 0)
  2462. goto out;
  2463. if (ret) {
  2464. ret = send_rmdir(sctx, valid_path);
  2465. if (ret < 0)
  2466. goto out;
  2467. } else if (!is_orphan) {
  2468. ret = orphanize_inode(sctx, sctx->cur_ino,
  2469. sctx->cur_inode_gen, valid_path);
  2470. if (ret < 0)
  2471. goto out;
  2472. is_orphan = 1;
  2473. }
  2474. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  2475. ret = dup_ref(cur, &check_dirs);
  2476. if (ret < 0)
  2477. goto out;
  2478. }
  2479. } else if (S_ISDIR(sctx->cur_inode_mode) &&
  2480. !list_empty(&sctx->deleted_refs)) {
  2481. /*
  2482. * We have a moved dir. Add the old parent to check_dirs
  2483. */
  2484. cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
  2485. list);
  2486. ret = dup_ref(cur, &check_dirs);
  2487. if (ret < 0)
  2488. goto out;
  2489. } else if (!S_ISDIR(sctx->cur_inode_mode)) {
  2490. /*
  2491. * We have a non dir inode. Go through all deleted refs and
  2492. * unlink them if they were not already overwritten by other
  2493. * inodes.
  2494. */
  2495. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  2496. ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  2497. sctx->cur_ino, sctx->cur_inode_gen,
  2498. cur->name, cur->name_len);
  2499. if (ret < 0)
  2500. goto out;
  2501. if (!ret) {
  2502. ret = send_unlink(sctx, cur->full_path);
  2503. if (ret < 0)
  2504. goto out;
  2505. }
  2506. ret = dup_ref(cur, &check_dirs);
  2507. if (ret < 0)
  2508. goto out;
  2509. }
  2510. /*
  2511. * If the inode is still orphan, unlink the orphan. This may
  2512. * happen when a previous inode did overwrite the first ref
  2513. * of this inode and no new refs were added for the current
  2514. * inode. Unlinking does not mean that the inode is deleted in
  2515. * all cases. There may still be links to this inode in other
  2516. * places.
  2517. */
  2518. if (is_orphan) {
  2519. ret = send_unlink(sctx, valid_path);
  2520. if (ret < 0)
  2521. goto out;
  2522. }
  2523. }
  2524. /*
  2525. * We did collect all parent dirs where cur_inode was once located. We
  2526. * now go through all these dirs and check if they are pending for
  2527. * deletion and if it's finally possible to perform the rmdir now.
  2528. * We also update the inode stats of the parent dirs here.
  2529. */
  2530. list_for_each_entry(cur, &check_dirs, list) {
  2531. /*
  2532. * In case we had refs into dirs that were not processed yet,
  2533. * we don't need to do the utime and rmdir logic for these dirs.
  2534. * The dir will be processed later.
  2535. */
  2536. if (cur->dir > sctx->cur_ino)
  2537. continue;
  2538. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
  2539. if (ret < 0)
  2540. goto out;
  2541. if (ret == inode_state_did_create ||
  2542. ret == inode_state_no_change) {
  2543. /* TODO delayed utimes */
  2544. ret = send_utimes(sctx, cur->dir, cur->dir_gen);
  2545. if (ret < 0)
  2546. goto out;
  2547. } else if (ret == inode_state_did_delete) {
  2548. ret = can_rmdir(sctx, cur->dir, sctx->cur_ino);
  2549. if (ret < 0)
  2550. goto out;
  2551. if (ret) {
  2552. ret = get_cur_path(sctx, cur->dir,
  2553. cur->dir_gen, valid_path);
  2554. if (ret < 0)
  2555. goto out;
  2556. ret = send_rmdir(sctx, valid_path);
  2557. if (ret < 0)
  2558. goto out;
  2559. }
  2560. }
  2561. }
  2562. ret = 0;
  2563. out:
  2564. __free_recorded_refs(&check_dirs);
  2565. free_recorded_refs(sctx);
  2566. fs_path_free(valid_path);
  2567. return ret;
  2568. }
  2569. static int __record_new_ref(int num, u64 dir, int index,
  2570. struct fs_path *name,
  2571. void *ctx)
  2572. {
  2573. int ret = 0;
  2574. struct send_ctx *sctx = ctx;
  2575. struct fs_path *p;
  2576. u64 gen;
  2577. p = fs_path_alloc();
  2578. if (!p)
  2579. return -ENOMEM;
  2580. ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
  2581. NULL, NULL);
  2582. if (ret < 0)
  2583. goto out;
  2584. ret = get_cur_path(sctx, dir, gen, p);
  2585. if (ret < 0)
  2586. goto out;
  2587. ret = fs_path_add_path(p, name);
  2588. if (ret < 0)
  2589. goto out;
  2590. ret = record_ref(&sctx->new_refs, dir, gen, p);
  2591. out:
  2592. if (ret)
  2593. fs_path_free(p);
  2594. return ret;
  2595. }
  2596. static int __record_deleted_ref(int num, u64 dir, int index,
  2597. struct fs_path *name,
  2598. void *ctx)
  2599. {
  2600. int ret = 0;
  2601. struct send_ctx *sctx = ctx;
  2602. struct fs_path *p;
  2603. u64 gen;
  2604. p = fs_path_alloc();
  2605. if (!p)
  2606. return -ENOMEM;
  2607. ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
  2608. NULL, NULL);
  2609. if (ret < 0)
  2610. goto out;
  2611. ret = get_cur_path(sctx, dir, gen, p);
  2612. if (ret < 0)
  2613. goto out;
  2614. ret = fs_path_add_path(p, name);
  2615. if (ret < 0)
  2616. goto out;
  2617. ret = record_ref(&sctx->deleted_refs, dir, gen, p);
  2618. out:
  2619. if (ret)
  2620. fs_path_free(p);
  2621. return ret;
  2622. }
  2623. static int record_new_ref(struct send_ctx *sctx)
  2624. {
  2625. int ret;
  2626. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  2627. sctx->cmp_key, 0, __record_new_ref, sctx);
  2628. if (ret < 0)
  2629. goto out;
  2630. ret = 0;
  2631. out:
  2632. return ret;
  2633. }
  2634. static int record_deleted_ref(struct send_ctx *sctx)
  2635. {
  2636. int ret;
  2637. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  2638. sctx->cmp_key, 0, __record_deleted_ref, sctx);
  2639. if (ret < 0)
  2640. goto out;
  2641. ret = 0;
  2642. out:
  2643. return ret;
  2644. }
  2645. struct find_ref_ctx {
  2646. u64 dir;
  2647. u64 dir_gen;
  2648. struct btrfs_root *root;
  2649. struct fs_path *name;
  2650. int found_idx;
  2651. };
  2652. static int __find_iref(int num, u64 dir, int index,
  2653. struct fs_path *name,
  2654. void *ctx_)
  2655. {
  2656. struct find_ref_ctx *ctx = ctx_;
  2657. u64 dir_gen;
  2658. int ret;
  2659. if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
  2660. strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
  2661. /*
  2662. * To avoid doing extra lookups we'll only do this if everything
  2663. * else matches.
  2664. */
  2665. ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
  2666. NULL, NULL, NULL);
  2667. if (ret)
  2668. return ret;
  2669. if (dir_gen != ctx->dir_gen)
  2670. return 0;
  2671. ctx->found_idx = num;
  2672. return 1;
  2673. }
  2674. return 0;
  2675. }
  2676. static int find_iref(struct btrfs_root *root,
  2677. struct btrfs_path *path,
  2678. struct btrfs_key *key,
  2679. u64 dir, u64 dir_gen, struct fs_path *name)
  2680. {
  2681. int ret;
  2682. struct find_ref_ctx ctx;
  2683. ctx.dir = dir;
  2684. ctx.name = name;
  2685. ctx.dir_gen = dir_gen;
  2686. ctx.found_idx = -1;
  2687. ctx.root = root;
  2688. ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
  2689. if (ret < 0)
  2690. return ret;
  2691. if (ctx.found_idx == -1)
  2692. return -ENOENT;
  2693. return ctx.found_idx;
  2694. }
  2695. static int __record_changed_new_ref(int num, u64 dir, int index,
  2696. struct fs_path *name,
  2697. void *ctx)
  2698. {
  2699. u64 dir_gen;
  2700. int ret;
  2701. struct send_ctx *sctx = ctx;
  2702. ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
  2703. NULL, NULL, NULL);
  2704. if (ret)
  2705. return ret;
  2706. ret = find_iref(sctx->parent_root, sctx->right_path,
  2707. sctx->cmp_key, dir, dir_gen, name);
  2708. if (ret == -ENOENT)
  2709. ret = __record_new_ref(num, dir, index, name, sctx);
  2710. else if (ret > 0)
  2711. ret = 0;
  2712. return ret;
  2713. }
  2714. static int __record_changed_deleted_ref(int num, u64 dir, int index,
  2715. struct fs_path *name,
  2716. void *ctx)
  2717. {
  2718. u64 dir_gen;
  2719. int ret;
  2720. struct send_ctx *sctx = ctx;
  2721. ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
  2722. NULL, NULL, NULL);
  2723. if (ret)
  2724. return ret;
  2725. ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
  2726. dir, dir_gen, name);
  2727. if (ret == -ENOENT)
  2728. ret = __record_deleted_ref(num, dir, index, name, sctx);
  2729. else if (ret > 0)
  2730. ret = 0;
  2731. return ret;
  2732. }
  2733. static int record_changed_ref(struct send_ctx *sctx)
  2734. {
  2735. int ret = 0;
  2736. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  2737. sctx->cmp_key, 0, __record_changed_new_ref, sctx);
  2738. if (ret < 0)
  2739. goto out;
  2740. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  2741. sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
  2742. if (ret < 0)
  2743. goto out;
  2744. ret = 0;
  2745. out:
  2746. return ret;
  2747. }
  2748. /*
  2749. * Record and process all refs at once. Needed when an inode changes the
  2750. * generation number, which means that it was deleted and recreated.
  2751. */
  2752. static int process_all_refs(struct send_ctx *sctx,
  2753. enum btrfs_compare_tree_result cmd)
  2754. {
  2755. int ret;
  2756. struct btrfs_root *root;
  2757. struct btrfs_path *path;
  2758. struct btrfs_key key;
  2759. struct btrfs_key found_key;
  2760. struct extent_buffer *eb;
  2761. int slot;
  2762. iterate_inode_ref_t cb;
  2763. path = alloc_path_for_send();
  2764. if (!path)
  2765. return -ENOMEM;
  2766. if (cmd == BTRFS_COMPARE_TREE_NEW) {
  2767. root = sctx->send_root;
  2768. cb = __record_new_ref;
  2769. } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
  2770. root = sctx->parent_root;
  2771. cb = __record_deleted_ref;
  2772. } else {
  2773. BUG();
  2774. }
  2775. key.objectid = sctx->cmp_key->objectid;
  2776. key.type = BTRFS_INODE_REF_KEY;
  2777. key.offset = 0;
  2778. while (1) {
  2779. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  2780. if (ret < 0)
  2781. goto out;
  2782. if (ret)
  2783. break;
  2784. eb = path->nodes[0];
  2785. slot = path->slots[0];
  2786. btrfs_item_key_to_cpu(eb, &found_key, slot);
  2787. if (found_key.objectid != key.objectid ||
  2788. (found_key.type != BTRFS_INODE_REF_KEY &&
  2789. found_key.type != BTRFS_INODE_EXTREF_KEY))
  2790. break;
  2791. ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
  2792. btrfs_release_path(path);
  2793. if (ret < 0)
  2794. goto out;
  2795. key.offset = found_key.offset + 1;
  2796. }
  2797. btrfs_release_path(path);
  2798. ret = process_recorded_refs(sctx);
  2799. out:
  2800. btrfs_free_path(path);
  2801. return ret;
  2802. }
  2803. static int send_set_xattr(struct send_ctx *sctx,
  2804. struct fs_path *path,
  2805. const char *name, int name_len,
  2806. const char *data, int data_len)
  2807. {
  2808. int ret = 0;
  2809. ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
  2810. if (ret < 0)
  2811. goto out;
  2812. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  2813. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  2814. TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
  2815. ret = send_cmd(sctx);
  2816. tlv_put_failure:
  2817. out:
  2818. return ret;
  2819. }
  2820. static int send_remove_xattr(struct send_ctx *sctx,
  2821. struct fs_path *path,
  2822. const char *name, int name_len)
  2823. {
  2824. int ret = 0;
  2825. ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
  2826. if (ret < 0)
  2827. goto out;
  2828. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  2829. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  2830. ret = send_cmd(sctx);
  2831. tlv_put_failure:
  2832. out:
  2833. return ret;
  2834. }
  2835. static int __process_new_xattr(int num, struct btrfs_key *di_key,
  2836. const char *name, int name_len,
  2837. const char *data, int data_len,
  2838. u8 type, void *ctx)
  2839. {
  2840. int ret;
  2841. struct send_ctx *sctx = ctx;
  2842. struct fs_path *p;
  2843. posix_acl_xattr_header dummy_acl;
  2844. p = fs_path_alloc();
  2845. if (!p)
  2846. return -ENOMEM;
  2847. /*
  2848. * This hack is needed because empty acl's are stored as zero byte
  2849. * data in xattrs. Problem with that is, that receiving these zero byte
  2850. * acl's will fail later. To fix this, we send a dummy acl list that
  2851. * only contains the version number and no entries.
  2852. */
  2853. if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
  2854. !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
  2855. if (data_len == 0) {
  2856. dummy_acl.a_version =
  2857. cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  2858. data = (char *)&dummy_acl;
  2859. data_len = sizeof(dummy_acl);
  2860. }
  2861. }
  2862. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  2863. if (ret < 0)
  2864. goto out;
  2865. ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
  2866. out:
  2867. fs_path_free(p);
  2868. return ret;
  2869. }
  2870. static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
  2871. const char *name, int name_len,
  2872. const char *data, int data_len,
  2873. u8 type, void *ctx)
  2874. {
  2875. int ret;
  2876. struct send_ctx *sctx = ctx;
  2877. struct fs_path *p;
  2878. p = fs_path_alloc();
  2879. if (!p)
  2880. return -ENOMEM;
  2881. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  2882. if (ret < 0)
  2883. goto out;
  2884. ret = send_remove_xattr(sctx, p, name, name_len);
  2885. out:
  2886. fs_path_free(p);
  2887. return ret;
  2888. }
  2889. static int process_new_xattr(struct send_ctx *sctx)
  2890. {
  2891. int ret = 0;
  2892. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  2893. sctx->cmp_key, __process_new_xattr, sctx);
  2894. return ret;
  2895. }
  2896. static int process_deleted_xattr(struct send_ctx *sctx)
  2897. {
  2898. int ret;
  2899. ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
  2900. sctx->cmp_key, __process_deleted_xattr, sctx);
  2901. return ret;
  2902. }
  2903. struct find_xattr_ctx {
  2904. const char *name;
  2905. int name_len;
  2906. int found_idx;
  2907. char *found_data;
  2908. int found_data_len;
  2909. };
  2910. static int __find_xattr(int num, struct btrfs_key *di_key,
  2911. const char *name, int name_len,
  2912. const char *data, int data_len,
  2913. u8 type, void *vctx)
  2914. {
  2915. struct find_xattr_ctx *ctx = vctx;
  2916. if (name_len == ctx->name_len &&
  2917. strncmp(name, ctx->name, name_len) == 0) {
  2918. ctx->found_idx = num;
  2919. ctx->found_data_len = data_len;
  2920. ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
  2921. if (!ctx->found_data)
  2922. return -ENOMEM;
  2923. return 1;
  2924. }
  2925. return 0;
  2926. }
  2927. static int find_xattr(struct btrfs_root *root,
  2928. struct btrfs_path *path,
  2929. struct btrfs_key *key,
  2930. const char *name, int name_len,
  2931. char **data, int *data_len)
  2932. {
  2933. int ret;
  2934. struct find_xattr_ctx ctx;
  2935. ctx.name = name;
  2936. ctx.name_len = name_len;
  2937. ctx.found_idx = -1;
  2938. ctx.found_data = NULL;
  2939. ctx.found_data_len = 0;
  2940. ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
  2941. if (ret < 0)
  2942. return ret;
  2943. if (ctx.found_idx == -1)
  2944. return -ENOENT;
  2945. if (data) {
  2946. *data = ctx.found_data;
  2947. *data_len = ctx.found_data_len;
  2948. } else {
  2949. kfree(ctx.found_data);
  2950. }
  2951. return ctx.found_idx;
  2952. }
  2953. static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
  2954. const char *name, int name_len,
  2955. const char *data, int data_len,
  2956. u8 type, void *ctx)
  2957. {
  2958. int ret;
  2959. struct send_ctx *sctx = ctx;
  2960. char *found_data = NULL;
  2961. int found_data_len = 0;
  2962. ret = find_xattr(sctx->parent_root, sctx->right_path,
  2963. sctx->cmp_key, name, name_len, &found_data,
  2964. &found_data_len);
  2965. if (ret == -ENOENT) {
  2966. ret = __process_new_xattr(num, di_key, name, name_len, data,
  2967. data_len, type, ctx);
  2968. } else if (ret >= 0) {
  2969. if (data_len != found_data_len ||
  2970. memcmp(data, found_data, data_len)) {
  2971. ret = __process_new_xattr(num, di_key, name, name_len,
  2972. data, data_len, type, ctx);
  2973. } else {
  2974. ret = 0;
  2975. }
  2976. }
  2977. kfree(found_data);
  2978. return ret;
  2979. }
  2980. static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
  2981. const char *name, int name_len,
  2982. const char *data, int data_len,
  2983. u8 type, void *ctx)
  2984. {
  2985. int ret;
  2986. struct send_ctx *sctx = ctx;
  2987. ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
  2988. name, name_len, NULL, NULL);
  2989. if (ret == -ENOENT)
  2990. ret = __process_deleted_xattr(num, di_key, name, name_len, data,
  2991. data_len, type, ctx);
  2992. else if (ret >= 0)
  2993. ret = 0;
  2994. return ret;
  2995. }
  2996. static int process_changed_xattr(struct send_ctx *sctx)
  2997. {
  2998. int ret = 0;
  2999. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  3000. sctx->cmp_key, __process_changed_new_xattr, sctx);
  3001. if (ret < 0)
  3002. goto out;
  3003. ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
  3004. sctx->cmp_key, __process_changed_deleted_xattr, sctx);
  3005. out:
  3006. return ret;
  3007. }
  3008. static int process_all_new_xattrs(struct send_ctx *sctx)
  3009. {
  3010. int ret;
  3011. struct btrfs_root *root;
  3012. struct btrfs_path *path;
  3013. struct btrfs_key key;
  3014. struct btrfs_key found_key;
  3015. struct extent_buffer *eb;
  3016. int slot;
  3017. path = alloc_path_for_send();
  3018. if (!path)
  3019. return -ENOMEM;
  3020. root = sctx->send_root;
  3021. key.objectid = sctx->cmp_key->objectid;
  3022. key.type = BTRFS_XATTR_ITEM_KEY;
  3023. key.offset = 0;
  3024. while (1) {
  3025. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  3026. if (ret < 0)
  3027. goto out;
  3028. if (ret) {
  3029. ret = 0;
  3030. goto out;
  3031. }
  3032. eb = path->nodes[0];
  3033. slot = path->slots[0];
  3034. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3035. if (found_key.objectid != key.objectid ||
  3036. found_key.type != key.type) {
  3037. ret = 0;
  3038. goto out;
  3039. }
  3040. ret = iterate_dir_item(root, path, &found_key,
  3041. __process_new_xattr, sctx);
  3042. if (ret < 0)
  3043. goto out;
  3044. btrfs_release_path(path);
  3045. key.offset = found_key.offset + 1;
  3046. }
  3047. out:
  3048. btrfs_free_path(path);
  3049. return ret;
  3050. }
  3051. static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
  3052. {
  3053. struct btrfs_root *root = sctx->send_root;
  3054. struct btrfs_fs_info *fs_info = root->fs_info;
  3055. struct inode *inode;
  3056. struct page *page;
  3057. char *addr;
  3058. struct btrfs_key key;
  3059. pgoff_t index = offset >> PAGE_CACHE_SHIFT;
  3060. pgoff_t last_index;
  3061. unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
  3062. ssize_t ret = 0;
  3063. key.objectid = sctx->cur_ino;
  3064. key.type = BTRFS_INODE_ITEM_KEY;
  3065. key.offset = 0;
  3066. inode = btrfs_iget(fs_info->sb, &key, root, NULL);
  3067. if (IS_ERR(inode))
  3068. return PTR_ERR(inode);
  3069. if (offset + len > i_size_read(inode)) {
  3070. if (offset > i_size_read(inode))
  3071. len = 0;
  3072. else
  3073. len = offset - i_size_read(inode);
  3074. }
  3075. if (len == 0)
  3076. goto out;
  3077. last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
  3078. while (index <= last_index) {
  3079. unsigned cur_len = min_t(unsigned, len,
  3080. PAGE_CACHE_SIZE - pg_offset);
  3081. page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
  3082. if (!page) {
  3083. ret = -ENOMEM;
  3084. break;
  3085. }
  3086. if (!PageUptodate(page)) {
  3087. btrfs_readpage(NULL, page);
  3088. lock_page(page);
  3089. if (!PageUptodate(page)) {
  3090. unlock_page(page);
  3091. page_cache_release(page);
  3092. ret = -EIO;
  3093. break;
  3094. }
  3095. }
  3096. addr = kmap(page);
  3097. memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
  3098. kunmap(page);
  3099. unlock_page(page);
  3100. page_cache_release(page);
  3101. index++;
  3102. pg_offset = 0;
  3103. len -= cur_len;
  3104. ret += cur_len;
  3105. }
  3106. out:
  3107. iput(inode);
  3108. return ret;
  3109. }
  3110. /*
  3111. * Read some bytes from the current inode/file and send a write command to
  3112. * user space.
  3113. */
  3114. static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
  3115. {
  3116. int ret = 0;
  3117. struct fs_path *p;
  3118. ssize_t num_read = 0;
  3119. p = fs_path_alloc();
  3120. if (!p)
  3121. return -ENOMEM;
  3122. verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
  3123. num_read = fill_read_buf(sctx, offset, len);
  3124. if (num_read <= 0) {
  3125. if (num_read < 0)
  3126. ret = num_read;
  3127. goto out;
  3128. }
  3129. ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
  3130. if (ret < 0)
  3131. goto out;
  3132. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3133. if (ret < 0)
  3134. goto out;
  3135. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3136. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3137. TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
  3138. ret = send_cmd(sctx);
  3139. tlv_put_failure:
  3140. out:
  3141. fs_path_free(p);
  3142. if (ret < 0)
  3143. return ret;
  3144. return num_read;
  3145. }
  3146. /*
  3147. * Send a clone command to user space.
  3148. */
  3149. static int send_clone(struct send_ctx *sctx,
  3150. u64 offset, u32 len,
  3151. struct clone_root *clone_root)
  3152. {
  3153. int ret = 0;
  3154. struct fs_path *p;
  3155. u64 gen;
  3156. verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
  3157. "clone_inode=%llu, clone_offset=%llu\n", offset, len,
  3158. clone_root->root->objectid, clone_root->ino,
  3159. clone_root->offset);
  3160. p = fs_path_alloc();
  3161. if (!p)
  3162. return -ENOMEM;
  3163. ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
  3164. if (ret < 0)
  3165. goto out;
  3166. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3167. if (ret < 0)
  3168. goto out;
  3169. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3170. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
  3171. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3172. if (clone_root->root == sctx->send_root) {
  3173. ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
  3174. &gen, NULL, NULL, NULL, NULL);
  3175. if (ret < 0)
  3176. goto out;
  3177. ret = get_cur_path(sctx, clone_root->ino, gen, p);
  3178. } else {
  3179. ret = get_inode_path(clone_root->root, clone_root->ino, p);
  3180. }
  3181. if (ret < 0)
  3182. goto out;
  3183. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  3184. clone_root->root->root_item.uuid);
  3185. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  3186. clone_root->root->root_item.ctransid);
  3187. TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
  3188. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
  3189. clone_root->offset);
  3190. ret = send_cmd(sctx);
  3191. tlv_put_failure:
  3192. out:
  3193. fs_path_free(p);
  3194. return ret;
  3195. }
  3196. /*
  3197. * Send an update extent command to user space.
  3198. */
  3199. static int send_update_extent(struct send_ctx *sctx,
  3200. u64 offset, u32 len)
  3201. {
  3202. int ret = 0;
  3203. struct fs_path *p;
  3204. p = fs_path_alloc();
  3205. if (!p)
  3206. return -ENOMEM;
  3207. ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
  3208. if (ret < 0)
  3209. goto out;
  3210. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3211. if (ret < 0)
  3212. goto out;
  3213. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3214. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3215. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
  3216. ret = send_cmd(sctx);
  3217. tlv_put_failure:
  3218. out:
  3219. fs_path_free(p);
  3220. return ret;
  3221. }
  3222. static int send_write_or_clone(struct send_ctx *sctx,
  3223. struct btrfs_path *path,
  3224. struct btrfs_key *key,
  3225. struct clone_root *clone_root)
  3226. {
  3227. int ret = 0;
  3228. struct btrfs_file_extent_item *ei;
  3229. u64 offset = key->offset;
  3230. u64 pos = 0;
  3231. u64 len;
  3232. u32 l;
  3233. u8 type;
  3234. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3235. struct btrfs_file_extent_item);
  3236. type = btrfs_file_extent_type(path->nodes[0], ei);
  3237. if (type == BTRFS_FILE_EXTENT_INLINE) {
  3238. len = btrfs_file_extent_inline_len(path->nodes[0], ei);
  3239. /*
  3240. * it is possible the inline item won't cover the whole page,
  3241. * but there may be items after this page. Make
  3242. * sure to send the whole thing
  3243. */
  3244. len = PAGE_CACHE_ALIGN(len);
  3245. } else {
  3246. len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
  3247. }
  3248. if (offset + len > sctx->cur_inode_size)
  3249. len = sctx->cur_inode_size - offset;
  3250. if (len == 0) {
  3251. ret = 0;
  3252. goto out;
  3253. }
  3254. if (clone_root) {
  3255. ret = send_clone(sctx, offset, len, clone_root);
  3256. } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
  3257. ret = send_update_extent(sctx, offset, len);
  3258. } else {
  3259. while (pos < len) {
  3260. l = len - pos;
  3261. if (l > BTRFS_SEND_READ_SIZE)
  3262. l = BTRFS_SEND_READ_SIZE;
  3263. ret = send_write(sctx, pos + offset, l);
  3264. if (ret < 0)
  3265. goto out;
  3266. if (!ret)
  3267. break;
  3268. pos += ret;
  3269. }
  3270. ret = 0;
  3271. }
  3272. out:
  3273. return ret;
  3274. }
  3275. static int is_extent_unchanged(struct send_ctx *sctx,
  3276. struct btrfs_path *left_path,
  3277. struct btrfs_key *ekey)
  3278. {
  3279. int ret = 0;
  3280. struct btrfs_key key;
  3281. struct btrfs_path *path = NULL;
  3282. struct extent_buffer *eb;
  3283. int slot;
  3284. struct btrfs_key found_key;
  3285. struct btrfs_file_extent_item *ei;
  3286. u64 left_disknr;
  3287. u64 right_disknr;
  3288. u64 left_offset;
  3289. u64 right_offset;
  3290. u64 left_offset_fixed;
  3291. u64 left_len;
  3292. u64 right_len;
  3293. u64 left_gen;
  3294. u64 right_gen;
  3295. u8 left_type;
  3296. u8 right_type;
  3297. path = alloc_path_for_send();
  3298. if (!path)
  3299. return -ENOMEM;
  3300. eb = left_path->nodes[0];
  3301. slot = left_path->slots[0];
  3302. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  3303. left_type = btrfs_file_extent_type(eb, ei);
  3304. if (left_type != BTRFS_FILE_EXTENT_REG) {
  3305. ret = 0;
  3306. goto out;
  3307. }
  3308. left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  3309. left_len = btrfs_file_extent_num_bytes(eb, ei);
  3310. left_offset = btrfs_file_extent_offset(eb, ei);
  3311. left_gen = btrfs_file_extent_generation(eb, ei);
  3312. /*
  3313. * Following comments will refer to these graphics. L is the left
  3314. * extents which we are checking at the moment. 1-8 are the right
  3315. * extents that we iterate.
  3316. *
  3317. * |-----L-----|
  3318. * |-1-|-2a-|-3-|-4-|-5-|-6-|
  3319. *
  3320. * |-----L-----|
  3321. * |--1--|-2b-|...(same as above)
  3322. *
  3323. * Alternative situation. Happens on files where extents got split.
  3324. * |-----L-----|
  3325. * |-----------7-----------|-6-|
  3326. *
  3327. * Alternative situation. Happens on files which got larger.
  3328. * |-----L-----|
  3329. * |-8-|
  3330. * Nothing follows after 8.
  3331. */
  3332. key.objectid = ekey->objectid;
  3333. key.type = BTRFS_EXTENT_DATA_KEY;
  3334. key.offset = ekey->offset;
  3335. ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
  3336. if (ret < 0)
  3337. goto out;
  3338. if (ret) {
  3339. ret = 0;
  3340. goto out;
  3341. }
  3342. /*
  3343. * Handle special case where the right side has no extents at all.
  3344. */
  3345. eb = path->nodes[0];
  3346. slot = path->slots[0];
  3347. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3348. if (found_key.objectid != key.objectid ||
  3349. found_key.type != key.type) {
  3350. /* If we're a hole then just pretend nothing changed */
  3351. ret = (left_disknr) ? 0 : 1;
  3352. goto out;
  3353. }
  3354. /*
  3355. * We're now on 2a, 2b or 7.
  3356. */
  3357. key = found_key;
  3358. while (key.offset < ekey->offset + left_len) {
  3359. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  3360. right_type = btrfs_file_extent_type(eb, ei);
  3361. right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  3362. right_len = btrfs_file_extent_num_bytes(eb, ei);
  3363. right_offset = btrfs_file_extent_offset(eb, ei);
  3364. right_gen = btrfs_file_extent_generation(eb, ei);
  3365. if (right_type != BTRFS_FILE_EXTENT_REG) {
  3366. ret = 0;
  3367. goto out;
  3368. }
  3369. /*
  3370. * Are we at extent 8? If yes, we know the extent is changed.
  3371. * This may only happen on the first iteration.
  3372. */
  3373. if (found_key.offset + right_len <= ekey->offset) {
  3374. /* If we're a hole just pretend nothing changed */
  3375. ret = (left_disknr) ? 0 : 1;
  3376. goto out;
  3377. }
  3378. left_offset_fixed = left_offset;
  3379. if (key.offset < ekey->offset) {
  3380. /* Fix the right offset for 2a and 7. */
  3381. right_offset += ekey->offset - key.offset;
  3382. } else {
  3383. /* Fix the left offset for all behind 2a and 2b */
  3384. left_offset_fixed += key.offset - ekey->offset;
  3385. }
  3386. /*
  3387. * Check if we have the same extent.
  3388. */
  3389. if (left_disknr != right_disknr ||
  3390. left_offset_fixed != right_offset ||
  3391. left_gen != right_gen) {
  3392. ret = 0;
  3393. goto out;
  3394. }
  3395. /*
  3396. * Go to the next extent.
  3397. */
  3398. ret = btrfs_next_item(sctx->parent_root, path);
  3399. if (ret < 0)
  3400. goto out;
  3401. if (!ret) {
  3402. eb = path->nodes[0];
  3403. slot = path->slots[0];
  3404. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3405. }
  3406. if (ret || found_key.objectid != key.objectid ||
  3407. found_key.type != key.type) {
  3408. key.offset += right_len;
  3409. break;
  3410. }
  3411. if (found_key.offset != key.offset + right_len) {
  3412. ret = 0;
  3413. goto out;
  3414. }
  3415. key = found_key;
  3416. }
  3417. /*
  3418. * We're now behind the left extent (treat as unchanged) or at the end
  3419. * of the right side (treat as changed).
  3420. */
  3421. if (key.offset >= ekey->offset + left_len)
  3422. ret = 1;
  3423. else
  3424. ret = 0;
  3425. out:
  3426. btrfs_free_path(path);
  3427. return ret;
  3428. }
  3429. static int process_extent(struct send_ctx *sctx,
  3430. struct btrfs_path *path,
  3431. struct btrfs_key *key)
  3432. {
  3433. struct clone_root *found_clone = NULL;
  3434. int ret = 0;
  3435. if (S_ISLNK(sctx->cur_inode_mode))
  3436. return 0;
  3437. if (sctx->parent_root && !sctx->cur_inode_new) {
  3438. ret = is_extent_unchanged(sctx, path, key);
  3439. if (ret < 0)
  3440. goto out;
  3441. if (ret) {
  3442. ret = 0;
  3443. goto out;
  3444. }
  3445. } else {
  3446. struct btrfs_file_extent_item *ei;
  3447. u8 type;
  3448. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3449. struct btrfs_file_extent_item);
  3450. type = btrfs_file_extent_type(path->nodes[0], ei);
  3451. if (type == BTRFS_FILE_EXTENT_PREALLOC ||
  3452. type == BTRFS_FILE_EXTENT_REG) {
  3453. /*
  3454. * The send spec does not have a prealloc command yet,
  3455. * so just leave a hole for prealloc'ed extents until
  3456. * we have enough commands queued up to justify rev'ing
  3457. * the send spec.
  3458. */
  3459. if (type == BTRFS_FILE_EXTENT_PREALLOC) {
  3460. ret = 0;
  3461. goto out;
  3462. }
  3463. /* Have a hole, just skip it. */
  3464. if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
  3465. ret = 0;
  3466. goto out;
  3467. }
  3468. }
  3469. }
  3470. ret = find_extent_clone(sctx, path, key->objectid, key->offset,
  3471. sctx->cur_inode_size, &found_clone);
  3472. if (ret != -ENOENT && ret < 0)
  3473. goto out;
  3474. ret = send_write_or_clone(sctx, path, key, found_clone);
  3475. out:
  3476. return ret;
  3477. }
  3478. static int process_all_extents(struct send_ctx *sctx)
  3479. {
  3480. int ret;
  3481. struct btrfs_root *root;
  3482. struct btrfs_path *path;
  3483. struct btrfs_key key;
  3484. struct btrfs_key found_key;
  3485. struct extent_buffer *eb;
  3486. int slot;
  3487. root = sctx->send_root;
  3488. path = alloc_path_for_send();
  3489. if (!path)
  3490. return -ENOMEM;
  3491. key.objectid = sctx->cmp_key->objectid;
  3492. key.type = BTRFS_EXTENT_DATA_KEY;
  3493. key.offset = 0;
  3494. while (1) {
  3495. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  3496. if (ret < 0)
  3497. goto out;
  3498. if (ret) {
  3499. ret = 0;
  3500. goto out;
  3501. }
  3502. eb = path->nodes[0];
  3503. slot = path->slots[0];
  3504. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3505. if (found_key.objectid != key.objectid ||
  3506. found_key.type != key.type) {
  3507. ret = 0;
  3508. goto out;
  3509. }
  3510. ret = process_extent(sctx, path, &found_key);
  3511. if (ret < 0)
  3512. goto out;
  3513. btrfs_release_path(path);
  3514. key.offset = found_key.offset + 1;
  3515. }
  3516. out:
  3517. btrfs_free_path(path);
  3518. return ret;
  3519. }
  3520. static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
  3521. {
  3522. int ret = 0;
  3523. if (sctx->cur_ino == 0)
  3524. goto out;
  3525. if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
  3526. sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
  3527. goto out;
  3528. if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
  3529. goto out;
  3530. ret = process_recorded_refs(sctx);
  3531. if (ret < 0)
  3532. goto out;
  3533. /*
  3534. * We have processed the refs and thus need to advance send_progress.
  3535. * Now, calls to get_cur_xxx will take the updated refs of the current
  3536. * inode into account.
  3537. */
  3538. sctx->send_progress = sctx->cur_ino + 1;
  3539. out:
  3540. return ret;
  3541. }
  3542. static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
  3543. {
  3544. int ret = 0;
  3545. u64 left_mode;
  3546. u64 left_uid;
  3547. u64 left_gid;
  3548. u64 right_mode;
  3549. u64 right_uid;
  3550. u64 right_gid;
  3551. int need_chmod = 0;
  3552. int need_chown = 0;
  3553. ret = process_recorded_refs_if_needed(sctx, at_end);
  3554. if (ret < 0)
  3555. goto out;
  3556. if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
  3557. goto out;
  3558. if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
  3559. goto out;
  3560. ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
  3561. &left_mode, &left_uid, &left_gid, NULL);
  3562. if (ret < 0)
  3563. goto out;
  3564. if (!sctx->parent_root || sctx->cur_inode_new) {
  3565. need_chown = 1;
  3566. if (!S_ISLNK(sctx->cur_inode_mode))
  3567. need_chmod = 1;
  3568. } else {
  3569. ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
  3570. NULL, NULL, &right_mode, &right_uid,
  3571. &right_gid, NULL);
  3572. if (ret < 0)
  3573. goto out;
  3574. if (left_uid != right_uid || left_gid != right_gid)
  3575. need_chown = 1;
  3576. if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
  3577. need_chmod = 1;
  3578. }
  3579. if (S_ISREG(sctx->cur_inode_mode)) {
  3580. ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3581. sctx->cur_inode_size);
  3582. if (ret < 0)
  3583. goto out;
  3584. }
  3585. if (need_chown) {
  3586. ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3587. left_uid, left_gid);
  3588. if (ret < 0)
  3589. goto out;
  3590. }
  3591. if (need_chmod) {
  3592. ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  3593. left_mode);
  3594. if (ret < 0)
  3595. goto out;
  3596. }
  3597. /*
  3598. * Need to send that every time, no matter if it actually changed
  3599. * between the two trees as we have done changes to the inode before.
  3600. */
  3601. ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
  3602. if (ret < 0)
  3603. goto out;
  3604. out:
  3605. return ret;
  3606. }
  3607. static int changed_inode(struct send_ctx *sctx,
  3608. enum btrfs_compare_tree_result result)
  3609. {
  3610. int ret = 0;
  3611. struct btrfs_key *key = sctx->cmp_key;
  3612. struct btrfs_inode_item *left_ii = NULL;
  3613. struct btrfs_inode_item *right_ii = NULL;
  3614. u64 left_gen = 0;
  3615. u64 right_gen = 0;
  3616. sctx->cur_ino = key->objectid;
  3617. sctx->cur_inode_new_gen = 0;
  3618. /*
  3619. * Set send_progress to current inode. This will tell all get_cur_xxx
  3620. * functions that the current inode's refs are not updated yet. Later,
  3621. * when process_recorded_refs is finished, it is set to cur_ino + 1.
  3622. */
  3623. sctx->send_progress = sctx->cur_ino;
  3624. if (result == BTRFS_COMPARE_TREE_NEW ||
  3625. result == BTRFS_COMPARE_TREE_CHANGED) {
  3626. left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
  3627. sctx->left_path->slots[0],
  3628. struct btrfs_inode_item);
  3629. left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
  3630. left_ii);
  3631. } else {
  3632. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  3633. sctx->right_path->slots[0],
  3634. struct btrfs_inode_item);
  3635. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  3636. right_ii);
  3637. }
  3638. if (result == BTRFS_COMPARE_TREE_CHANGED) {
  3639. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  3640. sctx->right_path->slots[0],
  3641. struct btrfs_inode_item);
  3642. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  3643. right_ii);
  3644. /*
  3645. * The cur_ino = root dir case is special here. We can't treat
  3646. * the inode as deleted+reused because it would generate a
  3647. * stream that tries to delete/mkdir the root dir.
  3648. */
  3649. if (left_gen != right_gen &&
  3650. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  3651. sctx->cur_inode_new_gen = 1;
  3652. }
  3653. if (result == BTRFS_COMPARE_TREE_NEW) {
  3654. sctx->cur_inode_gen = left_gen;
  3655. sctx->cur_inode_new = 1;
  3656. sctx->cur_inode_deleted = 0;
  3657. sctx->cur_inode_size = btrfs_inode_size(
  3658. sctx->left_path->nodes[0], left_ii);
  3659. sctx->cur_inode_mode = btrfs_inode_mode(
  3660. sctx->left_path->nodes[0], left_ii);
  3661. if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  3662. ret = send_create_inode_if_needed(sctx);
  3663. } else if (result == BTRFS_COMPARE_TREE_DELETED) {
  3664. sctx->cur_inode_gen = right_gen;
  3665. sctx->cur_inode_new = 0;
  3666. sctx->cur_inode_deleted = 1;
  3667. sctx->cur_inode_size = btrfs_inode_size(
  3668. sctx->right_path->nodes[0], right_ii);
  3669. sctx->cur_inode_mode = btrfs_inode_mode(
  3670. sctx->right_path->nodes[0], right_ii);
  3671. } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
  3672. /*
  3673. * We need to do some special handling in case the inode was
  3674. * reported as changed with a changed generation number. This
  3675. * means that the original inode was deleted and new inode
  3676. * reused the same inum. So we have to treat the old inode as
  3677. * deleted and the new one as new.
  3678. */
  3679. if (sctx->cur_inode_new_gen) {
  3680. /*
  3681. * First, process the inode as if it was deleted.
  3682. */
  3683. sctx->cur_inode_gen = right_gen;
  3684. sctx->cur_inode_new = 0;
  3685. sctx->cur_inode_deleted = 1;
  3686. sctx->cur_inode_size = btrfs_inode_size(
  3687. sctx->right_path->nodes[0], right_ii);
  3688. sctx->cur_inode_mode = btrfs_inode_mode(
  3689. sctx->right_path->nodes[0], right_ii);
  3690. ret = process_all_refs(sctx,
  3691. BTRFS_COMPARE_TREE_DELETED);
  3692. if (ret < 0)
  3693. goto out;
  3694. /*
  3695. * Now process the inode as if it was new.
  3696. */
  3697. sctx->cur_inode_gen = left_gen;
  3698. sctx->cur_inode_new = 1;
  3699. sctx->cur_inode_deleted = 0;
  3700. sctx->cur_inode_size = btrfs_inode_size(
  3701. sctx->left_path->nodes[0], left_ii);
  3702. sctx->cur_inode_mode = btrfs_inode_mode(
  3703. sctx->left_path->nodes[0], left_ii);
  3704. ret = send_create_inode_if_needed(sctx);
  3705. if (ret < 0)
  3706. goto out;
  3707. ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
  3708. if (ret < 0)
  3709. goto out;
  3710. /*
  3711. * Advance send_progress now as we did not get into
  3712. * process_recorded_refs_if_needed in the new_gen case.
  3713. */
  3714. sctx->send_progress = sctx->cur_ino + 1;
  3715. /*
  3716. * Now process all extents and xattrs of the inode as if
  3717. * they were all new.
  3718. */
  3719. ret = process_all_extents(sctx);
  3720. if (ret < 0)
  3721. goto out;
  3722. ret = process_all_new_xattrs(sctx);
  3723. if (ret < 0)
  3724. goto out;
  3725. } else {
  3726. sctx->cur_inode_gen = left_gen;
  3727. sctx->cur_inode_new = 0;
  3728. sctx->cur_inode_new_gen = 0;
  3729. sctx->cur_inode_deleted = 0;
  3730. sctx->cur_inode_size = btrfs_inode_size(
  3731. sctx->left_path->nodes[0], left_ii);
  3732. sctx->cur_inode_mode = btrfs_inode_mode(
  3733. sctx->left_path->nodes[0], left_ii);
  3734. }
  3735. }
  3736. out:
  3737. return ret;
  3738. }
  3739. /*
  3740. * We have to process new refs before deleted refs, but compare_trees gives us
  3741. * the new and deleted refs mixed. To fix this, we record the new/deleted refs
  3742. * first and later process them in process_recorded_refs.
  3743. * For the cur_inode_new_gen case, we skip recording completely because
  3744. * changed_inode did already initiate processing of refs. The reason for this is
  3745. * that in this case, compare_tree actually compares the refs of 2 different
  3746. * inodes. To fix this, process_all_refs is used in changed_inode to handle all
  3747. * refs of the right tree as deleted and all refs of the left tree as new.
  3748. */
  3749. static int changed_ref(struct send_ctx *sctx,
  3750. enum btrfs_compare_tree_result result)
  3751. {
  3752. int ret = 0;
  3753. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  3754. if (!sctx->cur_inode_new_gen &&
  3755. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
  3756. if (result == BTRFS_COMPARE_TREE_NEW)
  3757. ret = record_new_ref(sctx);
  3758. else if (result == BTRFS_COMPARE_TREE_DELETED)
  3759. ret = record_deleted_ref(sctx);
  3760. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  3761. ret = record_changed_ref(sctx);
  3762. }
  3763. return ret;
  3764. }
  3765. /*
  3766. * Process new/deleted/changed xattrs. We skip processing in the
  3767. * cur_inode_new_gen case because changed_inode did already initiate processing
  3768. * of xattrs. The reason is the same as in changed_ref
  3769. */
  3770. static int changed_xattr(struct send_ctx *sctx,
  3771. enum btrfs_compare_tree_result result)
  3772. {
  3773. int ret = 0;
  3774. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  3775. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  3776. if (result == BTRFS_COMPARE_TREE_NEW)
  3777. ret = process_new_xattr(sctx);
  3778. else if (result == BTRFS_COMPARE_TREE_DELETED)
  3779. ret = process_deleted_xattr(sctx);
  3780. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  3781. ret = process_changed_xattr(sctx);
  3782. }
  3783. return ret;
  3784. }
  3785. /*
  3786. * Process new/deleted/changed extents. We skip processing in the
  3787. * cur_inode_new_gen case because changed_inode did already initiate processing
  3788. * of extents. The reason is the same as in changed_ref
  3789. */
  3790. static int changed_extent(struct send_ctx *sctx,
  3791. enum btrfs_compare_tree_result result)
  3792. {
  3793. int ret = 0;
  3794. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  3795. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  3796. if (result != BTRFS_COMPARE_TREE_DELETED)
  3797. ret = process_extent(sctx, sctx->left_path,
  3798. sctx->cmp_key);
  3799. }
  3800. return ret;
  3801. }
  3802. static int dir_changed(struct send_ctx *sctx, u64 dir)
  3803. {
  3804. u64 orig_gen, new_gen;
  3805. int ret;
  3806. ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
  3807. NULL, NULL);
  3808. if (ret)
  3809. return ret;
  3810. ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
  3811. NULL, NULL, NULL);
  3812. if (ret)
  3813. return ret;
  3814. return (orig_gen != new_gen) ? 1 : 0;
  3815. }
  3816. static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
  3817. struct btrfs_key *key)
  3818. {
  3819. struct btrfs_inode_extref *extref;
  3820. struct extent_buffer *leaf;
  3821. u64 dirid = 0, last_dirid = 0;
  3822. unsigned long ptr;
  3823. u32 item_size;
  3824. u32 cur_offset = 0;
  3825. int ref_name_len;
  3826. int ret = 0;
  3827. /* Easy case, just check this one dirid */
  3828. if (key->type == BTRFS_INODE_REF_KEY) {
  3829. dirid = key->offset;
  3830. ret = dir_changed(sctx, dirid);
  3831. goto out;
  3832. }
  3833. leaf = path->nodes[0];
  3834. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3835. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  3836. while (cur_offset < item_size) {
  3837. extref = (struct btrfs_inode_extref *)(ptr +
  3838. cur_offset);
  3839. dirid = btrfs_inode_extref_parent(leaf, extref);
  3840. ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
  3841. cur_offset += ref_name_len + sizeof(*extref);
  3842. if (dirid == last_dirid)
  3843. continue;
  3844. ret = dir_changed(sctx, dirid);
  3845. if (ret)
  3846. break;
  3847. last_dirid = dirid;
  3848. }
  3849. out:
  3850. return ret;
  3851. }
  3852. /*
  3853. * Updates compare related fields in sctx and simply forwards to the actual
  3854. * changed_xxx functions.
  3855. */
  3856. static int changed_cb(struct btrfs_root *left_root,
  3857. struct btrfs_root *right_root,
  3858. struct btrfs_path *left_path,
  3859. struct btrfs_path *right_path,
  3860. struct btrfs_key *key,
  3861. enum btrfs_compare_tree_result result,
  3862. void *ctx)
  3863. {
  3864. int ret = 0;
  3865. struct send_ctx *sctx = ctx;
  3866. if (result == BTRFS_COMPARE_TREE_SAME) {
  3867. if (key->type != BTRFS_INODE_REF_KEY &&
  3868. key->type != BTRFS_INODE_EXTREF_KEY)
  3869. return 0;
  3870. ret = compare_refs(sctx, left_path, key);
  3871. if (!ret)
  3872. return 0;
  3873. if (ret < 0)
  3874. return ret;
  3875. result = BTRFS_COMPARE_TREE_CHANGED;
  3876. ret = 0;
  3877. }
  3878. sctx->left_path = left_path;
  3879. sctx->right_path = right_path;
  3880. sctx->cmp_key = key;
  3881. ret = finish_inode_if_needed(sctx, 0);
  3882. if (ret < 0)
  3883. goto out;
  3884. /* Ignore non-FS objects */
  3885. if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
  3886. key->objectid == BTRFS_FREE_SPACE_OBJECTID)
  3887. goto out;
  3888. if (key->type == BTRFS_INODE_ITEM_KEY)
  3889. ret = changed_inode(sctx, result);
  3890. else if (key->type == BTRFS_INODE_REF_KEY ||
  3891. key->type == BTRFS_INODE_EXTREF_KEY)
  3892. ret = changed_ref(sctx, result);
  3893. else if (key->type == BTRFS_XATTR_ITEM_KEY)
  3894. ret = changed_xattr(sctx, result);
  3895. else if (key->type == BTRFS_EXTENT_DATA_KEY)
  3896. ret = changed_extent(sctx, result);
  3897. out:
  3898. return ret;
  3899. }
  3900. static int full_send_tree(struct send_ctx *sctx)
  3901. {
  3902. int ret;
  3903. struct btrfs_trans_handle *trans = NULL;
  3904. struct btrfs_root *send_root = sctx->send_root;
  3905. struct btrfs_key key;
  3906. struct btrfs_key found_key;
  3907. struct btrfs_path *path;
  3908. struct extent_buffer *eb;
  3909. int slot;
  3910. u64 start_ctransid;
  3911. u64 ctransid;
  3912. path = alloc_path_for_send();
  3913. if (!path)
  3914. return -ENOMEM;
  3915. spin_lock(&send_root->root_item_lock);
  3916. start_ctransid = btrfs_root_ctransid(&send_root->root_item);
  3917. spin_unlock(&send_root->root_item_lock);
  3918. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  3919. key.type = BTRFS_INODE_ITEM_KEY;
  3920. key.offset = 0;
  3921. join_trans:
  3922. /*
  3923. * We need to make sure the transaction does not get committed
  3924. * while we do anything on commit roots. Join a transaction to prevent
  3925. * this.
  3926. */
  3927. trans = btrfs_join_transaction(send_root);
  3928. if (IS_ERR(trans)) {
  3929. ret = PTR_ERR(trans);
  3930. trans = NULL;
  3931. goto out;
  3932. }
  3933. /*
  3934. * Make sure the tree has not changed after re-joining. We detect this
  3935. * by comparing start_ctransid and ctransid. They should always match.
  3936. */
  3937. spin_lock(&send_root->root_item_lock);
  3938. ctransid = btrfs_root_ctransid(&send_root->root_item);
  3939. spin_unlock(&send_root->root_item_lock);
  3940. if (ctransid != start_ctransid) {
  3941. WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
  3942. "send was modified in between. This is "
  3943. "probably a bug.\n");
  3944. ret = -EIO;
  3945. goto out;
  3946. }
  3947. ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
  3948. if (ret < 0)
  3949. goto out;
  3950. if (ret)
  3951. goto out_finish;
  3952. while (1) {
  3953. /*
  3954. * When someone want to commit while we iterate, end the
  3955. * joined transaction and rejoin.
  3956. */
  3957. if (btrfs_should_end_transaction(trans, send_root)) {
  3958. ret = btrfs_end_transaction(trans, send_root);
  3959. trans = NULL;
  3960. if (ret < 0)
  3961. goto out;
  3962. btrfs_release_path(path);
  3963. goto join_trans;
  3964. }
  3965. eb = path->nodes[0];
  3966. slot = path->slots[0];
  3967. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3968. ret = changed_cb(send_root, NULL, path, NULL,
  3969. &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
  3970. if (ret < 0)
  3971. goto out;
  3972. key.objectid = found_key.objectid;
  3973. key.type = found_key.type;
  3974. key.offset = found_key.offset + 1;
  3975. ret = btrfs_next_item(send_root, path);
  3976. if (ret < 0)
  3977. goto out;
  3978. if (ret) {
  3979. ret = 0;
  3980. break;
  3981. }
  3982. }
  3983. out_finish:
  3984. ret = finish_inode_if_needed(sctx, 1);
  3985. out:
  3986. btrfs_free_path(path);
  3987. if (trans) {
  3988. if (!ret)
  3989. ret = btrfs_end_transaction(trans, send_root);
  3990. else
  3991. btrfs_end_transaction(trans, send_root);
  3992. }
  3993. return ret;
  3994. }
  3995. static int send_subvol(struct send_ctx *sctx)
  3996. {
  3997. int ret;
  3998. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
  3999. ret = send_header(sctx);
  4000. if (ret < 0)
  4001. goto out;
  4002. }
  4003. ret = send_subvol_begin(sctx);
  4004. if (ret < 0)
  4005. goto out;
  4006. if (sctx->parent_root) {
  4007. ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
  4008. changed_cb, sctx);
  4009. if (ret < 0)
  4010. goto out;
  4011. ret = finish_inode_if_needed(sctx, 1);
  4012. if (ret < 0)
  4013. goto out;
  4014. } else {
  4015. ret = full_send_tree(sctx);
  4016. if (ret < 0)
  4017. goto out;
  4018. }
  4019. out:
  4020. free_recorded_refs(sctx);
  4021. return ret;
  4022. }
  4023. long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
  4024. {
  4025. int ret = 0;
  4026. struct btrfs_root *send_root;
  4027. struct btrfs_root *clone_root;
  4028. struct btrfs_fs_info *fs_info;
  4029. struct btrfs_ioctl_send_args *arg = NULL;
  4030. struct btrfs_key key;
  4031. struct send_ctx *sctx = NULL;
  4032. u32 i;
  4033. u64 *clone_sources_tmp = NULL;
  4034. if (!capable(CAP_SYS_ADMIN))
  4035. return -EPERM;
  4036. send_root = BTRFS_I(file_inode(mnt_file))->root;
  4037. fs_info = send_root->fs_info;
  4038. /*
  4039. * This is done when we lookup the root, it should already be complete
  4040. * by the time we get here.
  4041. */
  4042. WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
  4043. /*
  4044. * If we just created this root we need to make sure that the orphan
  4045. * cleanup has been done and committed since we search the commit root,
  4046. * so check its commit root transid with our otransid and if they match
  4047. * commit the transaction to make sure everything is updated.
  4048. */
  4049. down_read(&send_root->fs_info->extent_commit_sem);
  4050. if (btrfs_header_generation(send_root->commit_root) ==
  4051. btrfs_root_otransid(&send_root->root_item)) {
  4052. struct btrfs_trans_handle *trans;
  4053. up_read(&send_root->fs_info->extent_commit_sem);
  4054. trans = btrfs_attach_transaction_barrier(send_root);
  4055. if (IS_ERR(trans)) {
  4056. if (PTR_ERR(trans) != -ENOENT) {
  4057. ret = PTR_ERR(trans);
  4058. goto out;
  4059. }
  4060. /* ENOENT means theres no transaction */
  4061. } else {
  4062. ret = btrfs_commit_transaction(trans, send_root);
  4063. if (ret)
  4064. goto out;
  4065. }
  4066. } else {
  4067. up_read(&send_root->fs_info->extent_commit_sem);
  4068. }
  4069. arg = memdup_user(arg_, sizeof(*arg));
  4070. if (IS_ERR(arg)) {
  4071. ret = PTR_ERR(arg);
  4072. arg = NULL;
  4073. goto out;
  4074. }
  4075. if (!access_ok(VERIFY_READ, arg->clone_sources,
  4076. sizeof(*arg->clone_sources *
  4077. arg->clone_sources_count))) {
  4078. ret = -EFAULT;
  4079. goto out;
  4080. }
  4081. if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
  4082. ret = -EINVAL;
  4083. goto out;
  4084. }
  4085. sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
  4086. if (!sctx) {
  4087. ret = -ENOMEM;
  4088. goto out;
  4089. }
  4090. INIT_LIST_HEAD(&sctx->new_refs);
  4091. INIT_LIST_HEAD(&sctx->deleted_refs);
  4092. INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
  4093. INIT_LIST_HEAD(&sctx->name_cache_list);
  4094. sctx->flags = arg->flags;
  4095. sctx->send_filp = fget(arg->send_fd);
  4096. if (!sctx->send_filp) {
  4097. ret = -EBADF;
  4098. goto out;
  4099. }
  4100. sctx->mnt = mnt_file->f_path.mnt;
  4101. sctx->send_root = send_root;
  4102. sctx->clone_roots_cnt = arg->clone_sources_count;
  4103. sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
  4104. sctx->send_buf = vmalloc(sctx->send_max_size);
  4105. if (!sctx->send_buf) {
  4106. ret = -ENOMEM;
  4107. goto out;
  4108. }
  4109. sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
  4110. if (!sctx->read_buf) {
  4111. ret = -ENOMEM;
  4112. goto out;
  4113. }
  4114. sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
  4115. (arg->clone_sources_count + 1));
  4116. if (!sctx->clone_roots) {
  4117. ret = -ENOMEM;
  4118. goto out;
  4119. }
  4120. if (arg->clone_sources_count) {
  4121. clone_sources_tmp = vmalloc(arg->clone_sources_count *
  4122. sizeof(*arg->clone_sources));
  4123. if (!clone_sources_tmp) {
  4124. ret = -ENOMEM;
  4125. goto out;
  4126. }
  4127. ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
  4128. arg->clone_sources_count *
  4129. sizeof(*arg->clone_sources));
  4130. if (ret) {
  4131. ret = -EFAULT;
  4132. goto out;
  4133. }
  4134. for (i = 0; i < arg->clone_sources_count; i++) {
  4135. key.objectid = clone_sources_tmp[i];
  4136. key.type = BTRFS_ROOT_ITEM_KEY;
  4137. key.offset = (u64)-1;
  4138. clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
  4139. if (IS_ERR(clone_root)) {
  4140. ret = PTR_ERR(clone_root);
  4141. goto out;
  4142. }
  4143. sctx->clone_roots[i].root = clone_root;
  4144. }
  4145. vfree(clone_sources_tmp);
  4146. clone_sources_tmp = NULL;
  4147. }
  4148. if (arg->parent_root) {
  4149. key.objectid = arg->parent_root;
  4150. key.type = BTRFS_ROOT_ITEM_KEY;
  4151. key.offset = (u64)-1;
  4152. sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
  4153. if (IS_ERR(sctx->parent_root)) {
  4154. ret = PTR_ERR(sctx->parent_root);
  4155. goto out;
  4156. }
  4157. }
  4158. /*
  4159. * Clones from send_root are allowed, but only if the clone source
  4160. * is behind the current send position. This is checked while searching
  4161. * for possible clone sources.
  4162. */
  4163. sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
  4164. /* We do a bsearch later */
  4165. sort(sctx->clone_roots, sctx->clone_roots_cnt,
  4166. sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
  4167. NULL);
  4168. ret = send_subvol(sctx);
  4169. if (ret < 0)
  4170. goto out;
  4171. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
  4172. ret = begin_cmd(sctx, BTRFS_SEND_C_END);
  4173. if (ret < 0)
  4174. goto out;
  4175. ret = send_cmd(sctx);
  4176. if (ret < 0)
  4177. goto out;
  4178. }
  4179. out:
  4180. kfree(arg);
  4181. vfree(clone_sources_tmp);
  4182. if (sctx) {
  4183. if (sctx->send_filp)
  4184. fput(sctx->send_filp);
  4185. vfree(sctx->clone_roots);
  4186. vfree(sctx->send_buf);
  4187. vfree(sctx->read_buf);
  4188. name_cache_free(sctx);
  4189. kfree(sctx);
  4190. }
  4191. return ret;
  4192. }