send.c 97 KB

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