send.c 96 KB

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