send.c 102 KB

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