send.c 96 KB

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