send.c 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501
  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/vmalloc.h>
  27. #include <linux/string.h>
  28. #include "send.h"
  29. #include "backref.h"
  30. #include "hash.h"
  31. #include "locking.h"
  32. #include "disk-io.h"
  33. #include "btrfs_inode.h"
  34. #include "transaction.h"
  35. static int g_verbose = 0;
  36. #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
  37. /*
  38. * A fs_path is a helper to dynamically build path names with unknown size.
  39. * It reallocates the internal buffer on demand.
  40. * It allows fast adding of path elements on the right side (normal path) and
  41. * fast adding to the left side (reversed path). A reversed path can also be
  42. * unreversed if needed.
  43. */
  44. struct fs_path {
  45. union {
  46. struct {
  47. char *start;
  48. char *end;
  49. char *prepared;
  50. char *buf;
  51. int buf_len;
  52. unsigned int reversed:1;
  53. unsigned int virtual_mem:1;
  54. char inline_buf[];
  55. };
  56. char pad[PAGE_SIZE];
  57. };
  58. };
  59. #define FS_PATH_INLINE_SIZE \
  60. (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
  61. /* reused for each extent */
  62. struct clone_root {
  63. struct btrfs_root *root;
  64. u64 ino;
  65. u64 offset;
  66. u64 found_refs;
  67. };
  68. #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
  69. #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
  70. struct send_ctx {
  71. struct file *send_filp;
  72. loff_t send_off;
  73. char *send_buf;
  74. u32 send_size;
  75. u32 send_max_size;
  76. u64 total_send_size;
  77. u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
  78. u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
  79. struct btrfs_root *send_root;
  80. struct btrfs_root *parent_root;
  81. struct clone_root *clone_roots;
  82. int clone_roots_cnt;
  83. /* current state of the compare_tree call */
  84. struct btrfs_path *left_path;
  85. struct btrfs_path *right_path;
  86. struct btrfs_key *cmp_key;
  87. /*
  88. * infos of the currently processed inode. In case of deleted inodes,
  89. * these are the values from the deleted inode.
  90. */
  91. u64 cur_ino;
  92. u64 cur_inode_gen;
  93. int cur_inode_new;
  94. int cur_inode_new_gen;
  95. int cur_inode_deleted;
  96. u64 cur_inode_size;
  97. u64 cur_inode_mode;
  98. u64 cur_inode_last_extent;
  99. u64 send_progress;
  100. struct list_head new_refs;
  101. struct list_head deleted_refs;
  102. struct radix_tree_root name_cache;
  103. struct list_head name_cache_list;
  104. int name_cache_size;
  105. char *read_buf;
  106. /*
  107. * We process inodes by their increasing order, so if before an
  108. * incremental send we reverse the parent/child relationship of
  109. * directories such that a directory with a lower inode number was
  110. * the parent of a directory with a higher inode number, and the one
  111. * becoming the new parent got renamed too, we can't rename/move the
  112. * directory with lower inode number when we finish processing it - we
  113. * must process the directory with higher inode number first, then
  114. * rename/move it and then rename/move the directory with lower inode
  115. * number. Example follows.
  116. *
  117. * Tree state when the first send was performed:
  118. *
  119. * .
  120. * |-- a (ino 257)
  121. * |-- b (ino 258)
  122. * |
  123. * |
  124. * |-- c (ino 259)
  125. * | |-- d (ino 260)
  126. * |
  127. * |-- c2 (ino 261)
  128. *
  129. * Tree state when the second (incremental) send is performed:
  130. *
  131. * .
  132. * |-- a (ino 257)
  133. * |-- b (ino 258)
  134. * |-- c2 (ino 261)
  135. * |-- d2 (ino 260)
  136. * |-- cc (ino 259)
  137. *
  138. * The sequence of steps that lead to the second state was:
  139. *
  140. * mv /a/b/c/d /a/b/c2/d2
  141. * mv /a/b/c /a/b/c2/d2/cc
  142. *
  143. * "c" has lower inode number, but we can't move it (2nd mv operation)
  144. * before we move "d", which has higher inode number.
  145. *
  146. * So we just memorize which move/rename operations must be performed
  147. * later when their respective parent is processed and moved/renamed.
  148. */
  149. /* Indexed by parent directory inode number. */
  150. struct rb_root pending_dir_moves;
  151. /*
  152. * Reverse index, indexed by the inode number of a directory that
  153. * is waiting for the move/rename of its immediate parent before its
  154. * own move/rename can be performed.
  155. */
  156. struct rb_root waiting_dir_moves;
  157. };
  158. struct pending_dir_move {
  159. struct rb_node node;
  160. struct list_head list;
  161. u64 parent_ino;
  162. u64 ino;
  163. u64 gen;
  164. struct list_head update_refs;
  165. };
  166. struct waiting_dir_move {
  167. struct rb_node node;
  168. u64 ino;
  169. };
  170. struct name_cache_entry {
  171. struct list_head list;
  172. /*
  173. * radix_tree has only 32bit entries but we need to handle 64bit inums.
  174. * We use the lower 32bit of the 64bit inum to store it in the tree. If
  175. * more then one inum would fall into the same entry, we use radix_list
  176. * to store the additional entries. radix_list is also used to store
  177. * entries where two entries have the same inum but different
  178. * generations.
  179. */
  180. struct list_head radix_list;
  181. u64 ino;
  182. u64 gen;
  183. u64 parent_ino;
  184. u64 parent_gen;
  185. int ret;
  186. int need_later_update;
  187. int name_len;
  188. char name[];
  189. };
  190. static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
  191. static int need_send_hole(struct send_ctx *sctx)
  192. {
  193. return (sctx->parent_root && !sctx->cur_inode_new &&
  194. !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
  195. S_ISREG(sctx->cur_inode_mode));
  196. }
  197. static void fs_path_reset(struct fs_path *p)
  198. {
  199. if (p->reversed) {
  200. p->start = p->buf + p->buf_len - 1;
  201. p->end = p->start;
  202. *p->start = 0;
  203. } else {
  204. p->start = p->buf;
  205. p->end = p->start;
  206. *p->start = 0;
  207. }
  208. }
  209. static struct fs_path *fs_path_alloc(void)
  210. {
  211. struct fs_path *p;
  212. p = kmalloc(sizeof(*p), GFP_NOFS);
  213. if (!p)
  214. return NULL;
  215. p->reversed = 0;
  216. p->virtual_mem = 0;
  217. p->buf = p->inline_buf;
  218. p->buf_len = FS_PATH_INLINE_SIZE;
  219. fs_path_reset(p);
  220. return p;
  221. }
  222. static struct fs_path *fs_path_alloc_reversed(void)
  223. {
  224. struct fs_path *p;
  225. p = fs_path_alloc();
  226. if (!p)
  227. return NULL;
  228. p->reversed = 1;
  229. fs_path_reset(p);
  230. return p;
  231. }
  232. static void fs_path_free(struct fs_path *p)
  233. {
  234. if (!p)
  235. return;
  236. if (p->buf != p->inline_buf) {
  237. if (p->virtual_mem)
  238. vfree(p->buf);
  239. else
  240. kfree(p->buf);
  241. }
  242. kfree(p);
  243. }
  244. static int fs_path_len(struct fs_path *p)
  245. {
  246. return p->end - p->start;
  247. }
  248. static int fs_path_ensure_buf(struct fs_path *p, int len)
  249. {
  250. char *tmp_buf;
  251. int path_len;
  252. int old_buf_len;
  253. len++;
  254. if (p->buf_len >= len)
  255. return 0;
  256. path_len = p->end - p->start;
  257. old_buf_len = p->buf_len;
  258. len = PAGE_ALIGN(len);
  259. if (p->buf == p->inline_buf) {
  260. tmp_buf = kmalloc(len, GFP_NOFS | __GFP_NOWARN);
  261. if (!tmp_buf) {
  262. tmp_buf = vmalloc(len);
  263. if (!tmp_buf)
  264. return -ENOMEM;
  265. p->virtual_mem = 1;
  266. }
  267. memcpy(tmp_buf, p->buf, p->buf_len);
  268. p->buf = tmp_buf;
  269. p->buf_len = len;
  270. } else {
  271. if (p->virtual_mem) {
  272. tmp_buf = vmalloc(len);
  273. if (!tmp_buf)
  274. return -ENOMEM;
  275. memcpy(tmp_buf, p->buf, p->buf_len);
  276. vfree(p->buf);
  277. } else {
  278. tmp_buf = krealloc(p->buf, len, GFP_NOFS);
  279. if (!tmp_buf) {
  280. tmp_buf = vmalloc(len);
  281. if (!tmp_buf)
  282. return -ENOMEM;
  283. memcpy(tmp_buf, p->buf, p->buf_len);
  284. kfree(p->buf);
  285. p->virtual_mem = 1;
  286. }
  287. }
  288. p->buf = tmp_buf;
  289. p->buf_len = len;
  290. }
  291. if (p->reversed) {
  292. tmp_buf = p->buf + old_buf_len - path_len - 1;
  293. p->end = p->buf + p->buf_len - 1;
  294. p->start = p->end - path_len;
  295. memmove(p->start, tmp_buf, path_len + 1);
  296. } else {
  297. p->start = p->buf;
  298. p->end = p->start + path_len;
  299. }
  300. return 0;
  301. }
  302. static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
  303. {
  304. int ret;
  305. int new_len;
  306. new_len = p->end - p->start + name_len;
  307. if (p->start != p->end)
  308. new_len++;
  309. ret = fs_path_ensure_buf(p, new_len);
  310. if (ret < 0)
  311. goto out;
  312. if (p->reversed) {
  313. if (p->start != p->end)
  314. *--p->start = '/';
  315. p->start -= name_len;
  316. p->prepared = p->start;
  317. } else {
  318. if (p->start != p->end)
  319. *p->end++ = '/';
  320. p->prepared = p->end;
  321. p->end += name_len;
  322. *p->end = 0;
  323. }
  324. out:
  325. return ret;
  326. }
  327. static int fs_path_add(struct fs_path *p, const char *name, int name_len)
  328. {
  329. int ret;
  330. ret = fs_path_prepare_for_add(p, name_len);
  331. if (ret < 0)
  332. goto out;
  333. memcpy(p->prepared, name, name_len);
  334. p->prepared = NULL;
  335. out:
  336. return ret;
  337. }
  338. static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
  339. {
  340. int ret;
  341. ret = fs_path_prepare_for_add(p, p2->end - p2->start);
  342. if (ret < 0)
  343. goto out;
  344. memcpy(p->prepared, p2->start, p2->end - p2->start);
  345. p->prepared = NULL;
  346. out:
  347. return ret;
  348. }
  349. static int fs_path_add_from_extent_buffer(struct fs_path *p,
  350. struct extent_buffer *eb,
  351. unsigned long off, int len)
  352. {
  353. int ret;
  354. ret = fs_path_prepare_for_add(p, len);
  355. if (ret < 0)
  356. goto out;
  357. read_extent_buffer(eb, p->prepared, off, len);
  358. p->prepared = NULL;
  359. out:
  360. return ret;
  361. }
  362. static int fs_path_copy(struct fs_path *p, struct fs_path *from)
  363. {
  364. int ret;
  365. p->reversed = from->reversed;
  366. fs_path_reset(p);
  367. ret = fs_path_add_path(p, from);
  368. return ret;
  369. }
  370. static void fs_path_unreverse(struct fs_path *p)
  371. {
  372. char *tmp;
  373. int len;
  374. if (!p->reversed)
  375. return;
  376. tmp = p->start;
  377. len = p->end - p->start;
  378. p->start = p->buf;
  379. p->end = p->start + len;
  380. memmove(p->start, tmp, len + 1);
  381. p->reversed = 0;
  382. }
  383. static struct btrfs_path *alloc_path_for_send(void)
  384. {
  385. struct btrfs_path *path;
  386. path = btrfs_alloc_path();
  387. if (!path)
  388. return NULL;
  389. path->search_commit_root = 1;
  390. path->skip_locking = 1;
  391. return path;
  392. }
  393. static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
  394. {
  395. int ret;
  396. mm_segment_t old_fs;
  397. u32 pos = 0;
  398. old_fs = get_fs();
  399. set_fs(KERNEL_DS);
  400. while (pos < len) {
  401. ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
  402. /* TODO handle that correctly */
  403. /*if (ret == -ERESTARTSYS) {
  404. continue;
  405. }*/
  406. if (ret < 0)
  407. goto out;
  408. if (ret == 0) {
  409. ret = -EIO;
  410. goto out;
  411. }
  412. pos += ret;
  413. }
  414. ret = 0;
  415. out:
  416. set_fs(old_fs);
  417. return ret;
  418. }
  419. static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
  420. {
  421. struct btrfs_tlv_header *hdr;
  422. int total_len = sizeof(*hdr) + len;
  423. int left = sctx->send_max_size - sctx->send_size;
  424. if (unlikely(left < total_len))
  425. return -EOVERFLOW;
  426. hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
  427. hdr->tlv_type = cpu_to_le16(attr);
  428. hdr->tlv_len = cpu_to_le16(len);
  429. memcpy(hdr + 1, data, len);
  430. sctx->send_size += total_len;
  431. return 0;
  432. }
  433. #define TLV_PUT_DEFINE_INT(bits) \
  434. static int tlv_put_u##bits(struct send_ctx *sctx, \
  435. u##bits attr, u##bits value) \
  436. { \
  437. __le##bits __tmp = cpu_to_le##bits(value); \
  438. return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
  439. }
  440. TLV_PUT_DEFINE_INT(64)
  441. static int tlv_put_string(struct send_ctx *sctx, u16 attr,
  442. const char *str, int len)
  443. {
  444. if (len == -1)
  445. len = strlen(str);
  446. return tlv_put(sctx, attr, str, len);
  447. }
  448. static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
  449. const u8 *uuid)
  450. {
  451. return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
  452. }
  453. static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
  454. struct extent_buffer *eb,
  455. struct btrfs_timespec *ts)
  456. {
  457. struct btrfs_timespec bts;
  458. read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
  459. return tlv_put(sctx, attr, &bts, sizeof(bts));
  460. }
  461. #define TLV_PUT(sctx, attrtype, attrlen, data) \
  462. do { \
  463. ret = tlv_put(sctx, attrtype, attrlen, data); \
  464. if (ret < 0) \
  465. goto tlv_put_failure; \
  466. } while (0)
  467. #define TLV_PUT_INT(sctx, attrtype, bits, value) \
  468. do { \
  469. ret = tlv_put_u##bits(sctx, attrtype, value); \
  470. if (ret < 0) \
  471. goto tlv_put_failure; \
  472. } while (0)
  473. #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
  474. #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
  475. #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
  476. #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
  477. #define TLV_PUT_STRING(sctx, attrtype, str, len) \
  478. do { \
  479. ret = tlv_put_string(sctx, attrtype, str, len); \
  480. if (ret < 0) \
  481. goto tlv_put_failure; \
  482. } while (0)
  483. #define TLV_PUT_PATH(sctx, attrtype, p) \
  484. do { \
  485. ret = tlv_put_string(sctx, attrtype, p->start, \
  486. p->end - p->start); \
  487. if (ret < 0) \
  488. goto tlv_put_failure; \
  489. } while(0)
  490. #define TLV_PUT_UUID(sctx, attrtype, uuid) \
  491. do { \
  492. ret = tlv_put_uuid(sctx, attrtype, uuid); \
  493. if (ret < 0) \
  494. goto tlv_put_failure; \
  495. } while (0)
  496. #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
  497. do { \
  498. ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
  499. if (ret < 0) \
  500. goto tlv_put_failure; \
  501. } while (0)
  502. static int send_header(struct send_ctx *sctx)
  503. {
  504. struct btrfs_stream_header hdr;
  505. strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
  506. hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
  507. return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
  508. &sctx->send_off);
  509. }
  510. /*
  511. * For each command/item we want to send to userspace, we call this function.
  512. */
  513. static int begin_cmd(struct send_ctx *sctx, int cmd)
  514. {
  515. struct btrfs_cmd_header *hdr;
  516. if (WARN_ON(!sctx->send_buf))
  517. return -EINVAL;
  518. BUG_ON(sctx->send_size);
  519. sctx->send_size += sizeof(*hdr);
  520. hdr = (struct btrfs_cmd_header *)sctx->send_buf;
  521. hdr->cmd = cpu_to_le16(cmd);
  522. return 0;
  523. }
  524. static int send_cmd(struct send_ctx *sctx)
  525. {
  526. int ret;
  527. struct btrfs_cmd_header *hdr;
  528. u32 crc;
  529. hdr = (struct btrfs_cmd_header *)sctx->send_buf;
  530. hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
  531. hdr->crc = 0;
  532. crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
  533. hdr->crc = cpu_to_le32(crc);
  534. ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
  535. &sctx->send_off);
  536. sctx->total_send_size += sctx->send_size;
  537. sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
  538. sctx->send_size = 0;
  539. return ret;
  540. }
  541. /*
  542. * Sends a move instruction to user space
  543. */
  544. static int send_rename(struct send_ctx *sctx,
  545. struct fs_path *from, struct fs_path *to)
  546. {
  547. int ret;
  548. verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
  549. ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
  550. if (ret < 0)
  551. goto out;
  552. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
  553. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
  554. ret = send_cmd(sctx);
  555. tlv_put_failure:
  556. out:
  557. return ret;
  558. }
  559. /*
  560. * Sends a link instruction to user space
  561. */
  562. static int send_link(struct send_ctx *sctx,
  563. struct fs_path *path, struct fs_path *lnk)
  564. {
  565. int ret;
  566. verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
  567. ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
  568. if (ret < 0)
  569. goto out;
  570. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  571. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
  572. ret = send_cmd(sctx);
  573. tlv_put_failure:
  574. out:
  575. return ret;
  576. }
  577. /*
  578. * Sends an unlink instruction to user space
  579. */
  580. static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
  581. {
  582. int ret;
  583. verbose_printk("btrfs: send_unlink %s\n", path->start);
  584. ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
  585. if (ret < 0)
  586. goto out;
  587. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  588. ret = send_cmd(sctx);
  589. tlv_put_failure:
  590. out:
  591. return ret;
  592. }
  593. /*
  594. * Sends a rmdir instruction to user space
  595. */
  596. static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
  597. {
  598. int ret;
  599. verbose_printk("btrfs: send_rmdir %s\n", path->start);
  600. ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
  601. if (ret < 0)
  602. goto out;
  603. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  604. ret = send_cmd(sctx);
  605. tlv_put_failure:
  606. out:
  607. return ret;
  608. }
  609. /*
  610. * Helper function to retrieve some fields from an inode item.
  611. */
  612. static int get_inode_info(struct btrfs_root *root,
  613. u64 ino, u64 *size, u64 *gen,
  614. u64 *mode, u64 *uid, u64 *gid,
  615. u64 *rdev)
  616. {
  617. int ret;
  618. struct btrfs_inode_item *ii;
  619. struct btrfs_key key;
  620. struct btrfs_path *path;
  621. path = alloc_path_for_send();
  622. if (!path)
  623. return -ENOMEM;
  624. key.objectid = ino;
  625. key.type = BTRFS_INODE_ITEM_KEY;
  626. key.offset = 0;
  627. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  628. if (ret < 0)
  629. goto out;
  630. if (ret) {
  631. ret = -ENOENT;
  632. goto out;
  633. }
  634. ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
  635. struct btrfs_inode_item);
  636. if (size)
  637. *size = btrfs_inode_size(path->nodes[0], ii);
  638. if (gen)
  639. *gen = btrfs_inode_generation(path->nodes[0], ii);
  640. if (mode)
  641. *mode = btrfs_inode_mode(path->nodes[0], ii);
  642. if (uid)
  643. *uid = btrfs_inode_uid(path->nodes[0], ii);
  644. if (gid)
  645. *gid = btrfs_inode_gid(path->nodes[0], ii);
  646. if (rdev)
  647. *rdev = btrfs_inode_rdev(path->nodes[0], ii);
  648. out:
  649. btrfs_free_path(path);
  650. return ret;
  651. }
  652. typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
  653. struct fs_path *p,
  654. void *ctx);
  655. /*
  656. * Helper function to iterate the entries in ONE btrfs_inode_ref or
  657. * btrfs_inode_extref.
  658. * The iterate callback may return a non zero value to stop iteration. This can
  659. * be a negative value for error codes or 1 to simply stop it.
  660. *
  661. * path must point to the INODE_REF or INODE_EXTREF when called.
  662. */
  663. static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
  664. struct btrfs_key *found_key, int resolve,
  665. iterate_inode_ref_t iterate, void *ctx)
  666. {
  667. struct extent_buffer *eb = path->nodes[0];
  668. struct btrfs_item *item;
  669. struct btrfs_inode_ref *iref;
  670. struct btrfs_inode_extref *extref;
  671. struct btrfs_path *tmp_path;
  672. struct fs_path *p;
  673. u32 cur = 0;
  674. u32 total;
  675. int slot = path->slots[0];
  676. u32 name_len;
  677. char *start;
  678. int ret = 0;
  679. int num = 0;
  680. int index;
  681. u64 dir;
  682. unsigned long name_off;
  683. unsigned long elem_size;
  684. unsigned long ptr;
  685. p = fs_path_alloc_reversed();
  686. if (!p)
  687. return -ENOMEM;
  688. tmp_path = alloc_path_for_send();
  689. if (!tmp_path) {
  690. fs_path_free(p);
  691. return -ENOMEM;
  692. }
  693. if (found_key->type == BTRFS_INODE_REF_KEY) {
  694. ptr = (unsigned long)btrfs_item_ptr(eb, slot,
  695. struct btrfs_inode_ref);
  696. item = btrfs_item_nr(slot);
  697. total = btrfs_item_size(eb, item);
  698. elem_size = sizeof(*iref);
  699. } else {
  700. ptr = btrfs_item_ptr_offset(eb, slot);
  701. total = btrfs_item_size_nr(eb, slot);
  702. elem_size = sizeof(*extref);
  703. }
  704. while (cur < total) {
  705. fs_path_reset(p);
  706. if (found_key->type == BTRFS_INODE_REF_KEY) {
  707. iref = (struct btrfs_inode_ref *)(ptr + cur);
  708. name_len = btrfs_inode_ref_name_len(eb, iref);
  709. name_off = (unsigned long)(iref + 1);
  710. index = btrfs_inode_ref_index(eb, iref);
  711. dir = found_key->offset;
  712. } else {
  713. extref = (struct btrfs_inode_extref *)(ptr + cur);
  714. name_len = btrfs_inode_extref_name_len(eb, extref);
  715. name_off = (unsigned long)&extref->name;
  716. index = btrfs_inode_extref_index(eb, extref);
  717. dir = btrfs_inode_extref_parent(eb, extref);
  718. }
  719. if (resolve) {
  720. start = btrfs_ref_to_path(root, tmp_path, name_len,
  721. name_off, eb, dir,
  722. p->buf, p->buf_len);
  723. if (IS_ERR(start)) {
  724. ret = PTR_ERR(start);
  725. goto out;
  726. }
  727. if (start < p->buf) {
  728. /* overflow , try again with larger buffer */
  729. ret = fs_path_ensure_buf(p,
  730. p->buf_len + p->buf - start);
  731. if (ret < 0)
  732. goto out;
  733. start = btrfs_ref_to_path(root, tmp_path,
  734. name_len, name_off,
  735. eb, dir,
  736. p->buf, p->buf_len);
  737. if (IS_ERR(start)) {
  738. ret = PTR_ERR(start);
  739. goto out;
  740. }
  741. BUG_ON(start < p->buf);
  742. }
  743. p->start = start;
  744. } else {
  745. ret = fs_path_add_from_extent_buffer(p, eb, name_off,
  746. name_len);
  747. if (ret < 0)
  748. goto out;
  749. }
  750. cur += elem_size + name_len;
  751. ret = iterate(num, dir, index, p, ctx);
  752. if (ret)
  753. goto out;
  754. num++;
  755. }
  756. out:
  757. btrfs_free_path(tmp_path);
  758. fs_path_free(p);
  759. return ret;
  760. }
  761. typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
  762. const char *name, int name_len,
  763. const char *data, int data_len,
  764. u8 type, void *ctx);
  765. /*
  766. * Helper function to iterate the entries in ONE btrfs_dir_item.
  767. * The iterate callback may return a non zero value to stop iteration. This can
  768. * be a negative value for error codes or 1 to simply stop it.
  769. *
  770. * path must point to the dir item when called.
  771. */
  772. static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
  773. struct btrfs_key *found_key,
  774. iterate_dir_item_t iterate, void *ctx)
  775. {
  776. int ret = 0;
  777. struct extent_buffer *eb;
  778. struct btrfs_item *item;
  779. struct btrfs_dir_item *di;
  780. struct btrfs_key di_key;
  781. char *buf = NULL;
  782. char *buf2 = NULL;
  783. int buf_len;
  784. int buf_virtual = 0;
  785. u32 name_len;
  786. u32 data_len;
  787. u32 cur;
  788. u32 len;
  789. u32 total;
  790. int slot;
  791. int num;
  792. u8 type;
  793. buf_len = PAGE_SIZE;
  794. buf = kmalloc(buf_len, GFP_NOFS);
  795. if (!buf) {
  796. ret = -ENOMEM;
  797. goto out;
  798. }
  799. eb = path->nodes[0];
  800. slot = path->slots[0];
  801. item = btrfs_item_nr(slot);
  802. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  803. cur = 0;
  804. len = 0;
  805. total = btrfs_item_size(eb, item);
  806. num = 0;
  807. while (cur < total) {
  808. name_len = btrfs_dir_name_len(eb, di);
  809. data_len = btrfs_dir_data_len(eb, di);
  810. type = btrfs_dir_type(eb, di);
  811. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  812. if (name_len + data_len > buf_len) {
  813. buf_len = PAGE_ALIGN(name_len + data_len);
  814. if (buf_virtual) {
  815. buf2 = vmalloc(buf_len);
  816. if (!buf2) {
  817. ret = -ENOMEM;
  818. goto out;
  819. }
  820. vfree(buf);
  821. } else {
  822. buf2 = krealloc(buf, buf_len, GFP_NOFS);
  823. if (!buf2) {
  824. buf2 = vmalloc(buf_len);
  825. if (!buf2) {
  826. ret = -ENOMEM;
  827. goto out;
  828. }
  829. kfree(buf);
  830. buf_virtual = 1;
  831. }
  832. }
  833. buf = buf2;
  834. buf2 = NULL;
  835. }
  836. read_extent_buffer(eb, buf, (unsigned long)(di + 1),
  837. name_len + data_len);
  838. len = sizeof(*di) + name_len + data_len;
  839. di = (struct btrfs_dir_item *)((char *)di + len);
  840. cur += len;
  841. ret = iterate(num, &di_key, buf, name_len, buf + name_len,
  842. data_len, type, ctx);
  843. if (ret < 0)
  844. goto out;
  845. if (ret) {
  846. ret = 0;
  847. goto out;
  848. }
  849. num++;
  850. }
  851. out:
  852. if (buf_virtual)
  853. vfree(buf);
  854. else
  855. kfree(buf);
  856. return ret;
  857. }
  858. static int __copy_first_ref(int num, u64 dir, int index,
  859. struct fs_path *p, void *ctx)
  860. {
  861. int ret;
  862. struct fs_path *pt = ctx;
  863. ret = fs_path_copy(pt, p);
  864. if (ret < 0)
  865. return ret;
  866. /* we want the first only */
  867. return 1;
  868. }
  869. /*
  870. * Retrieve the first path of an inode. If an inode has more then one
  871. * ref/hardlink, this is ignored.
  872. */
  873. static int get_inode_path(struct btrfs_root *root,
  874. u64 ino, struct fs_path *path)
  875. {
  876. int ret;
  877. struct btrfs_key key, found_key;
  878. struct btrfs_path *p;
  879. p = alloc_path_for_send();
  880. if (!p)
  881. return -ENOMEM;
  882. fs_path_reset(path);
  883. key.objectid = ino;
  884. key.type = BTRFS_INODE_REF_KEY;
  885. key.offset = 0;
  886. ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
  887. if (ret < 0)
  888. goto out;
  889. if (ret) {
  890. ret = 1;
  891. goto out;
  892. }
  893. btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
  894. if (found_key.objectid != ino ||
  895. (found_key.type != BTRFS_INODE_REF_KEY &&
  896. found_key.type != BTRFS_INODE_EXTREF_KEY)) {
  897. ret = -ENOENT;
  898. goto out;
  899. }
  900. ret = iterate_inode_ref(root, p, &found_key, 1,
  901. __copy_first_ref, path);
  902. if (ret < 0)
  903. goto out;
  904. ret = 0;
  905. out:
  906. btrfs_free_path(p);
  907. return ret;
  908. }
  909. struct backref_ctx {
  910. struct send_ctx *sctx;
  911. /* number of total found references */
  912. u64 found;
  913. /*
  914. * used for clones found in send_root. clones found behind cur_objectid
  915. * and cur_offset are not considered as allowed clones.
  916. */
  917. u64 cur_objectid;
  918. u64 cur_offset;
  919. /* may be truncated in case it's the last extent in a file */
  920. u64 extent_len;
  921. /* Just to check for bugs in backref resolving */
  922. int found_itself;
  923. };
  924. static int __clone_root_cmp_bsearch(const void *key, const void *elt)
  925. {
  926. u64 root = (u64)(uintptr_t)key;
  927. struct clone_root *cr = (struct clone_root *)elt;
  928. if (root < cr->root->objectid)
  929. return -1;
  930. if (root > cr->root->objectid)
  931. return 1;
  932. return 0;
  933. }
  934. static int __clone_root_cmp_sort(const void *e1, const void *e2)
  935. {
  936. struct clone_root *cr1 = (struct clone_root *)e1;
  937. struct clone_root *cr2 = (struct clone_root *)e2;
  938. if (cr1->root->objectid < cr2->root->objectid)
  939. return -1;
  940. if (cr1->root->objectid > cr2->root->objectid)
  941. return 1;
  942. return 0;
  943. }
  944. /*
  945. * Called for every backref that is found for the current extent.
  946. * Results are collected in sctx->clone_roots->ino/offset/found_refs
  947. */
  948. static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
  949. {
  950. struct backref_ctx *bctx = ctx_;
  951. struct clone_root *found;
  952. int ret;
  953. u64 i_size;
  954. /* First check if the root is in the list of accepted clone sources */
  955. found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
  956. bctx->sctx->clone_roots_cnt,
  957. sizeof(struct clone_root),
  958. __clone_root_cmp_bsearch);
  959. if (!found)
  960. return 0;
  961. if (found->root == bctx->sctx->send_root &&
  962. ino == bctx->cur_objectid &&
  963. offset == bctx->cur_offset) {
  964. bctx->found_itself = 1;
  965. }
  966. /*
  967. * There are inodes that have extents that lie behind its i_size. Don't
  968. * accept clones from these extents.
  969. */
  970. ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
  971. NULL);
  972. if (ret < 0)
  973. return ret;
  974. if (offset + bctx->extent_len > i_size)
  975. return 0;
  976. /*
  977. * Make sure we don't consider clones from send_root that are
  978. * behind the current inode/offset.
  979. */
  980. if (found->root == bctx->sctx->send_root) {
  981. /*
  982. * TODO for the moment we don't accept clones from the inode
  983. * that is currently send. We may change this when
  984. * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
  985. * file.
  986. */
  987. if (ino >= bctx->cur_objectid)
  988. return 0;
  989. #if 0
  990. if (ino > bctx->cur_objectid)
  991. return 0;
  992. if (offset + bctx->extent_len > bctx->cur_offset)
  993. return 0;
  994. #endif
  995. }
  996. bctx->found++;
  997. found->found_refs++;
  998. if (ino < found->ino) {
  999. found->ino = ino;
  1000. found->offset = offset;
  1001. } else if (found->ino == ino) {
  1002. /*
  1003. * same extent found more then once in the same file.
  1004. */
  1005. if (found->offset > offset + bctx->extent_len)
  1006. found->offset = offset;
  1007. }
  1008. return 0;
  1009. }
  1010. /*
  1011. * Given an inode, offset and extent item, it finds a good clone for a clone
  1012. * instruction. Returns -ENOENT when none could be found. The function makes
  1013. * sure that the returned clone is usable at the point where sending is at the
  1014. * moment. This means, that no clones are accepted which lie behind the current
  1015. * inode+offset.
  1016. *
  1017. * path must point to the extent item when called.
  1018. */
  1019. static int find_extent_clone(struct send_ctx *sctx,
  1020. struct btrfs_path *path,
  1021. u64 ino, u64 data_offset,
  1022. u64 ino_size,
  1023. struct clone_root **found)
  1024. {
  1025. int ret;
  1026. int extent_type;
  1027. u64 logical;
  1028. u64 disk_byte;
  1029. u64 num_bytes;
  1030. u64 extent_item_pos;
  1031. u64 flags = 0;
  1032. struct btrfs_file_extent_item *fi;
  1033. struct extent_buffer *eb = path->nodes[0];
  1034. struct backref_ctx *backref_ctx = NULL;
  1035. struct clone_root *cur_clone_root;
  1036. struct btrfs_key found_key;
  1037. struct btrfs_path *tmp_path;
  1038. int compressed;
  1039. u32 i;
  1040. tmp_path = alloc_path_for_send();
  1041. if (!tmp_path)
  1042. return -ENOMEM;
  1043. backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
  1044. if (!backref_ctx) {
  1045. ret = -ENOMEM;
  1046. goto out;
  1047. }
  1048. if (data_offset >= ino_size) {
  1049. /*
  1050. * There may be extents that lie behind the file's size.
  1051. * I at least had this in combination with snapshotting while
  1052. * writing large files.
  1053. */
  1054. ret = 0;
  1055. goto out;
  1056. }
  1057. fi = btrfs_item_ptr(eb, path->slots[0],
  1058. struct btrfs_file_extent_item);
  1059. extent_type = btrfs_file_extent_type(eb, fi);
  1060. if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  1061. ret = -ENOENT;
  1062. goto out;
  1063. }
  1064. compressed = btrfs_file_extent_compression(eb, fi);
  1065. num_bytes = btrfs_file_extent_num_bytes(eb, fi);
  1066. disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
  1067. if (disk_byte == 0) {
  1068. ret = -ENOENT;
  1069. goto out;
  1070. }
  1071. logical = disk_byte + btrfs_file_extent_offset(eb, fi);
  1072. ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
  1073. &found_key, &flags);
  1074. btrfs_release_path(tmp_path);
  1075. if (ret < 0)
  1076. goto out;
  1077. if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  1078. ret = -EIO;
  1079. goto out;
  1080. }
  1081. /*
  1082. * Setup the clone roots.
  1083. */
  1084. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  1085. cur_clone_root = sctx->clone_roots + i;
  1086. cur_clone_root->ino = (u64)-1;
  1087. cur_clone_root->offset = 0;
  1088. cur_clone_root->found_refs = 0;
  1089. }
  1090. backref_ctx->sctx = sctx;
  1091. backref_ctx->found = 0;
  1092. backref_ctx->cur_objectid = ino;
  1093. backref_ctx->cur_offset = data_offset;
  1094. backref_ctx->found_itself = 0;
  1095. backref_ctx->extent_len = num_bytes;
  1096. /*
  1097. * The last extent of a file may be too large due to page alignment.
  1098. * We need to adjust extent_len in this case so that the checks in
  1099. * __iterate_backrefs work.
  1100. */
  1101. if (data_offset + num_bytes >= ino_size)
  1102. backref_ctx->extent_len = ino_size - data_offset;
  1103. /*
  1104. * Now collect all backrefs.
  1105. */
  1106. if (compressed == BTRFS_COMPRESS_NONE)
  1107. extent_item_pos = logical - found_key.objectid;
  1108. else
  1109. extent_item_pos = 0;
  1110. extent_item_pos = logical - found_key.objectid;
  1111. ret = iterate_extent_inodes(sctx->send_root->fs_info,
  1112. found_key.objectid, extent_item_pos, 1,
  1113. __iterate_backrefs, backref_ctx);
  1114. if (ret < 0)
  1115. goto out;
  1116. if (!backref_ctx->found_itself) {
  1117. /* found a bug in backref code? */
  1118. ret = -EIO;
  1119. btrfs_err(sctx->send_root->fs_info, "did not find backref in "
  1120. "send_root. inode=%llu, offset=%llu, "
  1121. "disk_byte=%llu found extent=%llu\n",
  1122. ino, data_offset, disk_byte, found_key.objectid);
  1123. goto out;
  1124. }
  1125. verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
  1126. "ino=%llu, "
  1127. "num_bytes=%llu, logical=%llu\n",
  1128. data_offset, ino, num_bytes, logical);
  1129. if (!backref_ctx->found)
  1130. verbose_printk("btrfs: no clones found\n");
  1131. cur_clone_root = NULL;
  1132. for (i = 0; i < sctx->clone_roots_cnt; i++) {
  1133. if (sctx->clone_roots[i].found_refs) {
  1134. if (!cur_clone_root)
  1135. cur_clone_root = sctx->clone_roots + i;
  1136. else if (sctx->clone_roots[i].root == sctx->send_root)
  1137. /* prefer clones from send_root over others */
  1138. cur_clone_root = sctx->clone_roots + i;
  1139. }
  1140. }
  1141. if (cur_clone_root) {
  1142. *found = cur_clone_root;
  1143. ret = 0;
  1144. } else {
  1145. ret = -ENOENT;
  1146. }
  1147. out:
  1148. btrfs_free_path(tmp_path);
  1149. kfree(backref_ctx);
  1150. return ret;
  1151. }
  1152. static int read_symlink(struct btrfs_root *root,
  1153. u64 ino,
  1154. struct fs_path *dest)
  1155. {
  1156. int ret;
  1157. struct btrfs_path *path;
  1158. struct btrfs_key key;
  1159. struct btrfs_file_extent_item *ei;
  1160. u8 type;
  1161. u8 compression;
  1162. unsigned long off;
  1163. int len;
  1164. path = alloc_path_for_send();
  1165. if (!path)
  1166. return -ENOMEM;
  1167. key.objectid = ino;
  1168. key.type = BTRFS_EXTENT_DATA_KEY;
  1169. key.offset = 0;
  1170. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1171. if (ret < 0)
  1172. goto out;
  1173. BUG_ON(ret);
  1174. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1175. struct btrfs_file_extent_item);
  1176. type = btrfs_file_extent_type(path->nodes[0], ei);
  1177. compression = btrfs_file_extent_compression(path->nodes[0], ei);
  1178. BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
  1179. BUG_ON(compression);
  1180. off = btrfs_file_extent_inline_start(ei);
  1181. len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
  1182. ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
  1183. out:
  1184. btrfs_free_path(path);
  1185. return ret;
  1186. }
  1187. /*
  1188. * Helper function to generate a file name that is unique in the root of
  1189. * send_root and parent_root. This is used to generate names for orphan inodes.
  1190. */
  1191. static int gen_unique_name(struct send_ctx *sctx,
  1192. u64 ino, u64 gen,
  1193. struct fs_path *dest)
  1194. {
  1195. int ret = 0;
  1196. struct btrfs_path *path;
  1197. struct btrfs_dir_item *di;
  1198. char tmp[64];
  1199. int len;
  1200. u64 idx = 0;
  1201. path = alloc_path_for_send();
  1202. if (!path)
  1203. return -ENOMEM;
  1204. while (1) {
  1205. len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
  1206. ino, gen, idx);
  1207. if (len >= sizeof(tmp)) {
  1208. /* should really not happen */
  1209. ret = -EOVERFLOW;
  1210. goto out;
  1211. }
  1212. di = btrfs_lookup_dir_item(NULL, sctx->send_root,
  1213. path, BTRFS_FIRST_FREE_OBJECTID,
  1214. tmp, strlen(tmp), 0);
  1215. btrfs_release_path(path);
  1216. if (IS_ERR(di)) {
  1217. ret = PTR_ERR(di);
  1218. goto out;
  1219. }
  1220. if (di) {
  1221. /* not unique, try again */
  1222. idx++;
  1223. continue;
  1224. }
  1225. if (!sctx->parent_root) {
  1226. /* unique */
  1227. ret = 0;
  1228. break;
  1229. }
  1230. di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
  1231. path, BTRFS_FIRST_FREE_OBJECTID,
  1232. tmp, strlen(tmp), 0);
  1233. btrfs_release_path(path);
  1234. if (IS_ERR(di)) {
  1235. ret = PTR_ERR(di);
  1236. goto out;
  1237. }
  1238. if (di) {
  1239. /* not unique, try again */
  1240. idx++;
  1241. continue;
  1242. }
  1243. /* unique */
  1244. break;
  1245. }
  1246. ret = fs_path_add(dest, tmp, strlen(tmp));
  1247. out:
  1248. btrfs_free_path(path);
  1249. return ret;
  1250. }
  1251. enum inode_state {
  1252. inode_state_no_change,
  1253. inode_state_will_create,
  1254. inode_state_did_create,
  1255. inode_state_will_delete,
  1256. inode_state_did_delete,
  1257. };
  1258. static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
  1259. {
  1260. int ret;
  1261. int left_ret;
  1262. int right_ret;
  1263. u64 left_gen;
  1264. u64 right_gen;
  1265. ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
  1266. NULL, NULL);
  1267. if (ret < 0 && ret != -ENOENT)
  1268. goto out;
  1269. left_ret = ret;
  1270. if (!sctx->parent_root) {
  1271. right_ret = -ENOENT;
  1272. } else {
  1273. ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
  1274. NULL, NULL, NULL, NULL);
  1275. if (ret < 0 && ret != -ENOENT)
  1276. goto out;
  1277. right_ret = ret;
  1278. }
  1279. if (!left_ret && !right_ret) {
  1280. if (left_gen == gen && right_gen == gen) {
  1281. ret = inode_state_no_change;
  1282. } else if (left_gen == gen) {
  1283. if (ino < sctx->send_progress)
  1284. ret = inode_state_did_create;
  1285. else
  1286. ret = inode_state_will_create;
  1287. } else if (right_gen == gen) {
  1288. if (ino < sctx->send_progress)
  1289. ret = inode_state_did_delete;
  1290. else
  1291. ret = inode_state_will_delete;
  1292. } else {
  1293. ret = -ENOENT;
  1294. }
  1295. } else if (!left_ret) {
  1296. if (left_gen == gen) {
  1297. if (ino < sctx->send_progress)
  1298. ret = inode_state_did_create;
  1299. else
  1300. ret = inode_state_will_create;
  1301. } else {
  1302. ret = -ENOENT;
  1303. }
  1304. } else if (!right_ret) {
  1305. if (right_gen == gen) {
  1306. if (ino < sctx->send_progress)
  1307. ret = inode_state_did_delete;
  1308. else
  1309. ret = inode_state_will_delete;
  1310. } else {
  1311. ret = -ENOENT;
  1312. }
  1313. } else {
  1314. ret = -ENOENT;
  1315. }
  1316. out:
  1317. return ret;
  1318. }
  1319. static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
  1320. {
  1321. int ret;
  1322. ret = get_cur_inode_state(sctx, ino, gen);
  1323. if (ret < 0)
  1324. goto out;
  1325. if (ret == inode_state_no_change ||
  1326. ret == inode_state_did_create ||
  1327. ret == inode_state_will_delete)
  1328. ret = 1;
  1329. else
  1330. ret = 0;
  1331. out:
  1332. return ret;
  1333. }
  1334. /*
  1335. * Helper function to lookup a dir item in a dir.
  1336. */
  1337. static int lookup_dir_item_inode(struct btrfs_root *root,
  1338. u64 dir, const char *name, int name_len,
  1339. u64 *found_inode,
  1340. u8 *found_type)
  1341. {
  1342. int ret = 0;
  1343. struct btrfs_dir_item *di;
  1344. struct btrfs_key key;
  1345. struct btrfs_path *path;
  1346. path = alloc_path_for_send();
  1347. if (!path)
  1348. return -ENOMEM;
  1349. di = btrfs_lookup_dir_item(NULL, root, path,
  1350. dir, name, name_len, 0);
  1351. if (!di) {
  1352. ret = -ENOENT;
  1353. goto out;
  1354. }
  1355. if (IS_ERR(di)) {
  1356. ret = PTR_ERR(di);
  1357. goto out;
  1358. }
  1359. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
  1360. *found_inode = key.objectid;
  1361. *found_type = btrfs_dir_type(path->nodes[0], di);
  1362. out:
  1363. btrfs_free_path(path);
  1364. return ret;
  1365. }
  1366. /*
  1367. * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
  1368. * generation of the parent dir and the name of the dir entry.
  1369. */
  1370. static int get_first_ref(struct btrfs_root *root, u64 ino,
  1371. u64 *dir, u64 *dir_gen, struct fs_path *name)
  1372. {
  1373. int ret;
  1374. struct btrfs_key key;
  1375. struct btrfs_key found_key;
  1376. struct btrfs_path *path;
  1377. int len;
  1378. u64 parent_dir;
  1379. path = alloc_path_for_send();
  1380. if (!path)
  1381. return -ENOMEM;
  1382. key.objectid = ino;
  1383. key.type = BTRFS_INODE_REF_KEY;
  1384. key.offset = 0;
  1385. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  1386. if (ret < 0)
  1387. goto out;
  1388. if (!ret)
  1389. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1390. path->slots[0]);
  1391. if (ret || found_key.objectid != ino ||
  1392. (found_key.type != BTRFS_INODE_REF_KEY &&
  1393. found_key.type != BTRFS_INODE_EXTREF_KEY)) {
  1394. ret = -ENOENT;
  1395. goto out;
  1396. }
  1397. if (key.type == BTRFS_INODE_REF_KEY) {
  1398. struct btrfs_inode_ref *iref;
  1399. iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1400. struct btrfs_inode_ref);
  1401. len = btrfs_inode_ref_name_len(path->nodes[0], iref);
  1402. ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
  1403. (unsigned long)(iref + 1),
  1404. len);
  1405. parent_dir = found_key.offset;
  1406. } else {
  1407. struct btrfs_inode_extref *extref;
  1408. extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1409. struct btrfs_inode_extref);
  1410. len = btrfs_inode_extref_name_len(path->nodes[0], extref);
  1411. ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
  1412. (unsigned long)&extref->name, len);
  1413. parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
  1414. }
  1415. if (ret < 0)
  1416. goto out;
  1417. btrfs_release_path(path);
  1418. ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
  1419. NULL, NULL);
  1420. if (ret < 0)
  1421. goto out;
  1422. *dir = parent_dir;
  1423. out:
  1424. btrfs_free_path(path);
  1425. return ret;
  1426. }
  1427. static int is_first_ref(struct btrfs_root *root,
  1428. u64 ino, u64 dir,
  1429. const char *name, int name_len)
  1430. {
  1431. int ret;
  1432. struct fs_path *tmp_name;
  1433. u64 tmp_dir;
  1434. u64 tmp_dir_gen;
  1435. tmp_name = fs_path_alloc();
  1436. if (!tmp_name)
  1437. return -ENOMEM;
  1438. ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
  1439. if (ret < 0)
  1440. goto out;
  1441. if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
  1442. ret = 0;
  1443. goto out;
  1444. }
  1445. ret = !memcmp(tmp_name->start, name, name_len);
  1446. out:
  1447. fs_path_free(tmp_name);
  1448. return ret;
  1449. }
  1450. /*
  1451. * Used by process_recorded_refs to determine if a new ref would overwrite an
  1452. * already existing ref. In case it detects an overwrite, it returns the
  1453. * inode/gen in who_ino/who_gen.
  1454. * When an overwrite is detected, process_recorded_refs does proper orphanizing
  1455. * to make sure later references to the overwritten inode are possible.
  1456. * Orphanizing is however only required for the first ref of an inode.
  1457. * process_recorded_refs does an additional is_first_ref check to see if
  1458. * orphanizing is really required.
  1459. */
  1460. static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
  1461. const char *name, int name_len,
  1462. u64 *who_ino, u64 *who_gen)
  1463. {
  1464. int ret = 0;
  1465. u64 gen;
  1466. u64 other_inode = 0;
  1467. u8 other_type = 0;
  1468. if (!sctx->parent_root)
  1469. goto out;
  1470. ret = is_inode_existent(sctx, dir, dir_gen);
  1471. if (ret <= 0)
  1472. goto out;
  1473. /*
  1474. * If we have a parent root we need to verify that the parent dir was
  1475. * not delted and then re-created, if it was then we have no overwrite
  1476. * and we can just unlink this entry.
  1477. */
  1478. if (sctx->parent_root) {
  1479. ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
  1480. NULL, NULL, NULL);
  1481. if (ret < 0 && ret != -ENOENT)
  1482. goto out;
  1483. if (ret) {
  1484. ret = 0;
  1485. goto out;
  1486. }
  1487. if (gen != dir_gen)
  1488. goto out;
  1489. }
  1490. ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
  1491. &other_inode, &other_type);
  1492. if (ret < 0 && ret != -ENOENT)
  1493. goto out;
  1494. if (ret) {
  1495. ret = 0;
  1496. goto out;
  1497. }
  1498. /*
  1499. * Check if the overwritten ref was already processed. If yes, the ref
  1500. * was already unlinked/moved, so we can safely assume that we will not
  1501. * overwrite anything at this point in time.
  1502. */
  1503. if (other_inode > sctx->send_progress) {
  1504. ret = get_inode_info(sctx->parent_root, other_inode, NULL,
  1505. who_gen, NULL, NULL, NULL, NULL);
  1506. if (ret < 0)
  1507. goto out;
  1508. ret = 1;
  1509. *who_ino = other_inode;
  1510. } else {
  1511. ret = 0;
  1512. }
  1513. out:
  1514. return ret;
  1515. }
  1516. /*
  1517. * Checks if the ref was overwritten by an already processed inode. This is
  1518. * used by __get_cur_name_and_parent to find out if the ref was orphanized and
  1519. * thus the orphan name needs be used.
  1520. * process_recorded_refs also uses it to avoid unlinking of refs that were
  1521. * overwritten.
  1522. */
  1523. static int did_overwrite_ref(struct send_ctx *sctx,
  1524. u64 dir, u64 dir_gen,
  1525. u64 ino, u64 ino_gen,
  1526. const char *name, int name_len)
  1527. {
  1528. int ret = 0;
  1529. u64 gen;
  1530. u64 ow_inode;
  1531. u8 other_type;
  1532. if (!sctx->parent_root)
  1533. goto out;
  1534. ret = is_inode_existent(sctx, dir, dir_gen);
  1535. if (ret <= 0)
  1536. goto out;
  1537. /* check if the ref was overwritten by another ref */
  1538. ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
  1539. &ow_inode, &other_type);
  1540. if (ret < 0 && ret != -ENOENT)
  1541. goto out;
  1542. if (ret) {
  1543. /* was never and will never be overwritten */
  1544. ret = 0;
  1545. goto out;
  1546. }
  1547. ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
  1548. NULL, NULL);
  1549. if (ret < 0)
  1550. goto out;
  1551. if (ow_inode == ino && gen == ino_gen) {
  1552. ret = 0;
  1553. goto out;
  1554. }
  1555. /* we know that it is or will be overwritten. check this now */
  1556. if (ow_inode < sctx->send_progress)
  1557. ret = 1;
  1558. else
  1559. ret = 0;
  1560. out:
  1561. return ret;
  1562. }
  1563. /*
  1564. * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
  1565. * that got overwritten. This is used by process_recorded_refs to determine
  1566. * if it has to use the path as returned by get_cur_path or the orphan name.
  1567. */
  1568. static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
  1569. {
  1570. int ret = 0;
  1571. struct fs_path *name = NULL;
  1572. u64 dir;
  1573. u64 dir_gen;
  1574. if (!sctx->parent_root)
  1575. goto out;
  1576. name = fs_path_alloc();
  1577. if (!name)
  1578. return -ENOMEM;
  1579. ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
  1580. if (ret < 0)
  1581. goto out;
  1582. ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
  1583. name->start, fs_path_len(name));
  1584. out:
  1585. fs_path_free(name);
  1586. return ret;
  1587. }
  1588. /*
  1589. * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
  1590. * so we need to do some special handling in case we have clashes. This function
  1591. * takes care of this with the help of name_cache_entry::radix_list.
  1592. * In case of error, nce is kfreed.
  1593. */
  1594. static int name_cache_insert(struct send_ctx *sctx,
  1595. struct name_cache_entry *nce)
  1596. {
  1597. int ret = 0;
  1598. struct list_head *nce_head;
  1599. nce_head = radix_tree_lookup(&sctx->name_cache,
  1600. (unsigned long)nce->ino);
  1601. if (!nce_head) {
  1602. nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
  1603. if (!nce_head) {
  1604. kfree(nce);
  1605. return -ENOMEM;
  1606. }
  1607. INIT_LIST_HEAD(nce_head);
  1608. ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
  1609. if (ret < 0) {
  1610. kfree(nce_head);
  1611. kfree(nce);
  1612. return ret;
  1613. }
  1614. }
  1615. list_add_tail(&nce->radix_list, nce_head);
  1616. list_add_tail(&nce->list, &sctx->name_cache_list);
  1617. sctx->name_cache_size++;
  1618. return ret;
  1619. }
  1620. static void name_cache_delete(struct send_ctx *sctx,
  1621. struct name_cache_entry *nce)
  1622. {
  1623. struct list_head *nce_head;
  1624. nce_head = radix_tree_lookup(&sctx->name_cache,
  1625. (unsigned long)nce->ino);
  1626. BUG_ON(!nce_head);
  1627. list_del(&nce->radix_list);
  1628. list_del(&nce->list);
  1629. sctx->name_cache_size--;
  1630. if (list_empty(nce_head)) {
  1631. radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
  1632. kfree(nce_head);
  1633. }
  1634. }
  1635. static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
  1636. u64 ino, u64 gen)
  1637. {
  1638. struct list_head *nce_head;
  1639. struct name_cache_entry *cur;
  1640. nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
  1641. if (!nce_head)
  1642. return NULL;
  1643. list_for_each_entry(cur, nce_head, radix_list) {
  1644. if (cur->ino == ino && cur->gen == gen)
  1645. return cur;
  1646. }
  1647. return NULL;
  1648. }
  1649. /*
  1650. * Removes the entry from the list and adds it back to the end. This marks the
  1651. * entry as recently used so that name_cache_clean_unused does not remove it.
  1652. */
  1653. static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
  1654. {
  1655. list_del(&nce->list);
  1656. list_add_tail(&nce->list, &sctx->name_cache_list);
  1657. }
  1658. /*
  1659. * Remove some entries from the beginning of name_cache_list.
  1660. */
  1661. static void name_cache_clean_unused(struct send_ctx *sctx)
  1662. {
  1663. struct name_cache_entry *nce;
  1664. if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
  1665. return;
  1666. while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
  1667. nce = list_entry(sctx->name_cache_list.next,
  1668. struct name_cache_entry, list);
  1669. name_cache_delete(sctx, nce);
  1670. kfree(nce);
  1671. }
  1672. }
  1673. static void name_cache_free(struct send_ctx *sctx)
  1674. {
  1675. struct name_cache_entry *nce;
  1676. while (!list_empty(&sctx->name_cache_list)) {
  1677. nce = list_entry(sctx->name_cache_list.next,
  1678. struct name_cache_entry, list);
  1679. name_cache_delete(sctx, nce);
  1680. kfree(nce);
  1681. }
  1682. }
  1683. /*
  1684. * Used by get_cur_path for each ref up to the root.
  1685. * Returns 0 if it succeeded.
  1686. * Returns 1 if the inode is not existent or got overwritten. In that case, the
  1687. * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
  1688. * is returned, parent_ino/parent_gen are not guaranteed to be valid.
  1689. * Returns <0 in case of error.
  1690. */
  1691. static int __get_cur_name_and_parent(struct send_ctx *sctx,
  1692. u64 ino, u64 gen,
  1693. int skip_name_cache,
  1694. u64 *parent_ino,
  1695. u64 *parent_gen,
  1696. struct fs_path *dest)
  1697. {
  1698. int ret;
  1699. int nce_ret;
  1700. struct btrfs_path *path = NULL;
  1701. struct name_cache_entry *nce = NULL;
  1702. if (skip_name_cache)
  1703. goto get_ref;
  1704. /*
  1705. * First check if we already did a call to this function with the same
  1706. * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
  1707. * return the cached result.
  1708. */
  1709. nce = name_cache_search(sctx, ino, gen);
  1710. if (nce) {
  1711. if (ino < sctx->send_progress && nce->need_later_update) {
  1712. name_cache_delete(sctx, nce);
  1713. kfree(nce);
  1714. nce = NULL;
  1715. } else {
  1716. name_cache_used(sctx, nce);
  1717. *parent_ino = nce->parent_ino;
  1718. *parent_gen = nce->parent_gen;
  1719. ret = fs_path_add(dest, nce->name, nce->name_len);
  1720. if (ret < 0)
  1721. goto out;
  1722. ret = nce->ret;
  1723. goto out;
  1724. }
  1725. }
  1726. path = alloc_path_for_send();
  1727. if (!path)
  1728. return -ENOMEM;
  1729. /*
  1730. * If the inode is not existent yet, add the orphan name and return 1.
  1731. * This should only happen for the parent dir that we determine in
  1732. * __record_new_ref
  1733. */
  1734. ret = is_inode_existent(sctx, ino, gen);
  1735. if (ret < 0)
  1736. goto out;
  1737. if (!ret) {
  1738. ret = gen_unique_name(sctx, ino, gen, dest);
  1739. if (ret < 0)
  1740. goto out;
  1741. ret = 1;
  1742. goto out_cache;
  1743. }
  1744. get_ref:
  1745. /*
  1746. * Depending on whether the inode was already processed or not, use
  1747. * send_root or parent_root for ref lookup.
  1748. */
  1749. if (ino < sctx->send_progress && !skip_name_cache)
  1750. ret = get_first_ref(sctx->send_root, ino,
  1751. parent_ino, parent_gen, dest);
  1752. else
  1753. ret = get_first_ref(sctx->parent_root, ino,
  1754. parent_ino, parent_gen, dest);
  1755. if (ret < 0)
  1756. goto out;
  1757. /*
  1758. * Check if the ref was overwritten by an inode's ref that was processed
  1759. * earlier. If yes, treat as orphan and return 1.
  1760. */
  1761. ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
  1762. dest->start, dest->end - dest->start);
  1763. if (ret < 0)
  1764. goto out;
  1765. if (ret) {
  1766. fs_path_reset(dest);
  1767. ret = gen_unique_name(sctx, ino, gen, dest);
  1768. if (ret < 0)
  1769. goto out;
  1770. ret = 1;
  1771. }
  1772. if (skip_name_cache)
  1773. goto out;
  1774. out_cache:
  1775. /*
  1776. * Store the result of the lookup in the name cache.
  1777. */
  1778. nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
  1779. if (!nce) {
  1780. ret = -ENOMEM;
  1781. goto out;
  1782. }
  1783. nce->ino = ino;
  1784. nce->gen = gen;
  1785. nce->parent_ino = *parent_ino;
  1786. nce->parent_gen = *parent_gen;
  1787. nce->name_len = fs_path_len(dest);
  1788. nce->ret = ret;
  1789. strcpy(nce->name, dest->start);
  1790. if (ino < sctx->send_progress)
  1791. nce->need_later_update = 0;
  1792. else
  1793. nce->need_later_update = 1;
  1794. nce_ret = name_cache_insert(sctx, nce);
  1795. if (nce_ret < 0)
  1796. ret = nce_ret;
  1797. name_cache_clean_unused(sctx);
  1798. out:
  1799. btrfs_free_path(path);
  1800. return ret;
  1801. }
  1802. /*
  1803. * Magic happens here. This function returns the first ref to an inode as it
  1804. * would look like while receiving the stream at this point in time.
  1805. * We walk the path up to the root. For every inode in between, we check if it
  1806. * was already processed/sent. If yes, we continue with the parent as found
  1807. * in send_root. If not, we continue with the parent as found in parent_root.
  1808. * If we encounter an inode that was deleted at this point in time, we use the
  1809. * inodes "orphan" name instead of the real name and stop. Same with new inodes
  1810. * that were not created yet and overwritten inodes/refs.
  1811. *
  1812. * When do we have have orphan inodes:
  1813. * 1. When an inode is freshly created and thus no valid refs are available yet
  1814. * 2. When a directory lost all it's refs (deleted) but still has dir items
  1815. * inside which were not processed yet (pending for move/delete). If anyone
  1816. * tried to get the path to the dir items, it would get a path inside that
  1817. * orphan directory.
  1818. * 3. When an inode is moved around or gets new links, it may overwrite the ref
  1819. * of an unprocessed inode. If in that case the first ref would be
  1820. * overwritten, the overwritten inode gets "orphanized". Later when we
  1821. * process this overwritten inode, it is restored at a new place by moving
  1822. * the orphan inode.
  1823. *
  1824. * sctx->send_progress tells this function at which point in time receiving
  1825. * would be.
  1826. */
  1827. static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
  1828. struct fs_path *dest)
  1829. {
  1830. int ret = 0;
  1831. struct fs_path *name = NULL;
  1832. u64 parent_inode = 0;
  1833. u64 parent_gen = 0;
  1834. int stop = 0;
  1835. u64 start_ino = ino;
  1836. u64 start_gen = gen;
  1837. int skip_name_cache = 0;
  1838. name = fs_path_alloc();
  1839. if (!name) {
  1840. ret = -ENOMEM;
  1841. goto out;
  1842. }
  1843. if (is_waiting_for_move(sctx, ino))
  1844. skip_name_cache = 1;
  1845. again:
  1846. dest->reversed = 1;
  1847. fs_path_reset(dest);
  1848. while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
  1849. fs_path_reset(name);
  1850. ret = __get_cur_name_and_parent(sctx, ino, gen, skip_name_cache,
  1851. &parent_inode, &parent_gen, name);
  1852. if (ret < 0)
  1853. goto out;
  1854. if (ret)
  1855. stop = 1;
  1856. if (!skip_name_cache &&
  1857. is_waiting_for_move(sctx, parent_inode)) {
  1858. ino = start_ino;
  1859. gen = start_gen;
  1860. stop = 0;
  1861. skip_name_cache = 1;
  1862. goto again;
  1863. }
  1864. ret = fs_path_add_path(dest, name);
  1865. if (ret < 0)
  1866. goto out;
  1867. ino = parent_inode;
  1868. gen = parent_gen;
  1869. }
  1870. out:
  1871. fs_path_free(name);
  1872. if (!ret)
  1873. fs_path_unreverse(dest);
  1874. return ret;
  1875. }
  1876. /*
  1877. * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
  1878. */
  1879. static int send_subvol_begin(struct send_ctx *sctx)
  1880. {
  1881. int ret;
  1882. struct btrfs_root *send_root = sctx->send_root;
  1883. struct btrfs_root *parent_root = sctx->parent_root;
  1884. struct btrfs_path *path;
  1885. struct btrfs_key key;
  1886. struct btrfs_root_ref *ref;
  1887. struct extent_buffer *leaf;
  1888. char *name = NULL;
  1889. int namelen;
  1890. path = btrfs_alloc_path();
  1891. if (!path)
  1892. return -ENOMEM;
  1893. name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
  1894. if (!name) {
  1895. btrfs_free_path(path);
  1896. return -ENOMEM;
  1897. }
  1898. key.objectid = send_root->objectid;
  1899. key.type = BTRFS_ROOT_BACKREF_KEY;
  1900. key.offset = 0;
  1901. ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
  1902. &key, path, 1, 0);
  1903. if (ret < 0)
  1904. goto out;
  1905. if (ret) {
  1906. ret = -ENOENT;
  1907. goto out;
  1908. }
  1909. leaf = path->nodes[0];
  1910. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1911. if (key.type != BTRFS_ROOT_BACKREF_KEY ||
  1912. key.objectid != send_root->objectid) {
  1913. ret = -ENOENT;
  1914. goto out;
  1915. }
  1916. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  1917. namelen = btrfs_root_ref_name_len(leaf, ref);
  1918. read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
  1919. btrfs_release_path(path);
  1920. if (parent_root) {
  1921. ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
  1922. if (ret < 0)
  1923. goto out;
  1924. } else {
  1925. ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
  1926. if (ret < 0)
  1927. goto out;
  1928. }
  1929. TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
  1930. TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
  1931. sctx->send_root->root_item.uuid);
  1932. TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
  1933. le64_to_cpu(sctx->send_root->root_item.ctransid));
  1934. if (parent_root) {
  1935. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  1936. sctx->parent_root->root_item.uuid);
  1937. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  1938. le64_to_cpu(sctx->parent_root->root_item.ctransid));
  1939. }
  1940. ret = send_cmd(sctx);
  1941. tlv_put_failure:
  1942. out:
  1943. btrfs_free_path(path);
  1944. kfree(name);
  1945. return ret;
  1946. }
  1947. static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
  1948. {
  1949. int ret = 0;
  1950. struct fs_path *p;
  1951. verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
  1952. p = fs_path_alloc();
  1953. if (!p)
  1954. return -ENOMEM;
  1955. ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
  1956. if (ret < 0)
  1957. goto out;
  1958. ret = get_cur_path(sctx, ino, gen, p);
  1959. if (ret < 0)
  1960. goto out;
  1961. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  1962. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
  1963. ret = send_cmd(sctx);
  1964. tlv_put_failure:
  1965. out:
  1966. fs_path_free(p);
  1967. return ret;
  1968. }
  1969. static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
  1970. {
  1971. int ret = 0;
  1972. struct fs_path *p;
  1973. verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
  1974. p = fs_path_alloc();
  1975. if (!p)
  1976. return -ENOMEM;
  1977. ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
  1978. if (ret < 0)
  1979. goto out;
  1980. ret = get_cur_path(sctx, ino, gen, p);
  1981. if (ret < 0)
  1982. goto out;
  1983. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  1984. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
  1985. ret = send_cmd(sctx);
  1986. tlv_put_failure:
  1987. out:
  1988. fs_path_free(p);
  1989. return ret;
  1990. }
  1991. static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
  1992. {
  1993. int ret = 0;
  1994. struct fs_path *p;
  1995. verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
  1996. p = fs_path_alloc();
  1997. if (!p)
  1998. return -ENOMEM;
  1999. ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
  2000. if (ret < 0)
  2001. goto out;
  2002. ret = get_cur_path(sctx, ino, gen, p);
  2003. if (ret < 0)
  2004. goto out;
  2005. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2006. TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
  2007. TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
  2008. ret = send_cmd(sctx);
  2009. tlv_put_failure:
  2010. out:
  2011. fs_path_free(p);
  2012. return ret;
  2013. }
  2014. static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
  2015. {
  2016. int ret = 0;
  2017. struct fs_path *p = NULL;
  2018. struct btrfs_inode_item *ii;
  2019. struct btrfs_path *path = NULL;
  2020. struct extent_buffer *eb;
  2021. struct btrfs_key key;
  2022. int slot;
  2023. verbose_printk("btrfs: send_utimes %llu\n", ino);
  2024. p = fs_path_alloc();
  2025. if (!p)
  2026. return -ENOMEM;
  2027. path = alloc_path_for_send();
  2028. if (!path) {
  2029. ret = -ENOMEM;
  2030. goto out;
  2031. }
  2032. key.objectid = ino;
  2033. key.type = BTRFS_INODE_ITEM_KEY;
  2034. key.offset = 0;
  2035. ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
  2036. if (ret < 0)
  2037. goto out;
  2038. eb = path->nodes[0];
  2039. slot = path->slots[0];
  2040. ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  2041. ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
  2042. if (ret < 0)
  2043. goto out;
  2044. ret = get_cur_path(sctx, ino, gen, p);
  2045. if (ret < 0)
  2046. goto out;
  2047. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2048. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
  2049. btrfs_inode_atime(ii));
  2050. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
  2051. btrfs_inode_mtime(ii));
  2052. TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
  2053. btrfs_inode_ctime(ii));
  2054. /* TODO Add otime support when the otime patches get into upstream */
  2055. ret = send_cmd(sctx);
  2056. tlv_put_failure:
  2057. out:
  2058. fs_path_free(p);
  2059. btrfs_free_path(path);
  2060. return ret;
  2061. }
  2062. /*
  2063. * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
  2064. * a valid path yet because we did not process the refs yet. So, the inode
  2065. * is created as orphan.
  2066. */
  2067. static int send_create_inode(struct send_ctx *sctx, u64 ino)
  2068. {
  2069. int ret = 0;
  2070. struct fs_path *p;
  2071. int cmd;
  2072. u64 gen;
  2073. u64 mode;
  2074. u64 rdev;
  2075. verbose_printk("btrfs: send_create_inode %llu\n", ino);
  2076. p = fs_path_alloc();
  2077. if (!p)
  2078. return -ENOMEM;
  2079. ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
  2080. NULL, &rdev);
  2081. if (ret < 0)
  2082. goto out;
  2083. if (S_ISREG(mode)) {
  2084. cmd = BTRFS_SEND_C_MKFILE;
  2085. } else if (S_ISDIR(mode)) {
  2086. cmd = BTRFS_SEND_C_MKDIR;
  2087. } else if (S_ISLNK(mode)) {
  2088. cmd = BTRFS_SEND_C_SYMLINK;
  2089. } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
  2090. cmd = BTRFS_SEND_C_MKNOD;
  2091. } else if (S_ISFIFO(mode)) {
  2092. cmd = BTRFS_SEND_C_MKFIFO;
  2093. } else if (S_ISSOCK(mode)) {
  2094. cmd = BTRFS_SEND_C_MKSOCK;
  2095. } else {
  2096. printk(KERN_WARNING "btrfs: unexpected inode type %o",
  2097. (int)(mode & S_IFMT));
  2098. ret = -ENOTSUPP;
  2099. goto out;
  2100. }
  2101. ret = begin_cmd(sctx, cmd);
  2102. if (ret < 0)
  2103. goto out;
  2104. ret = gen_unique_name(sctx, ino, gen, p);
  2105. if (ret < 0)
  2106. goto out;
  2107. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  2108. TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
  2109. if (S_ISLNK(mode)) {
  2110. fs_path_reset(p);
  2111. ret = read_symlink(sctx->send_root, ino, p);
  2112. if (ret < 0)
  2113. goto out;
  2114. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
  2115. } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
  2116. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  2117. TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
  2118. TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
  2119. }
  2120. ret = send_cmd(sctx);
  2121. if (ret < 0)
  2122. goto out;
  2123. tlv_put_failure:
  2124. out:
  2125. fs_path_free(p);
  2126. return ret;
  2127. }
  2128. /*
  2129. * We need some special handling for inodes that get processed before the parent
  2130. * directory got created. See process_recorded_refs for details.
  2131. * This function does the check if we already created the dir out of order.
  2132. */
  2133. static int did_create_dir(struct send_ctx *sctx, u64 dir)
  2134. {
  2135. int ret = 0;
  2136. struct btrfs_path *path = NULL;
  2137. struct btrfs_key key;
  2138. struct btrfs_key found_key;
  2139. struct btrfs_key di_key;
  2140. struct extent_buffer *eb;
  2141. struct btrfs_dir_item *di;
  2142. int slot;
  2143. path = alloc_path_for_send();
  2144. if (!path) {
  2145. ret = -ENOMEM;
  2146. goto out;
  2147. }
  2148. key.objectid = dir;
  2149. key.type = BTRFS_DIR_INDEX_KEY;
  2150. key.offset = 0;
  2151. while (1) {
  2152. ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
  2153. 1, 0);
  2154. if (ret < 0)
  2155. goto out;
  2156. if (!ret) {
  2157. eb = path->nodes[0];
  2158. slot = path->slots[0];
  2159. btrfs_item_key_to_cpu(eb, &found_key, slot);
  2160. }
  2161. if (ret || found_key.objectid != key.objectid ||
  2162. found_key.type != key.type) {
  2163. ret = 0;
  2164. goto out;
  2165. }
  2166. di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
  2167. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  2168. if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
  2169. di_key.objectid < sctx->send_progress) {
  2170. ret = 1;
  2171. goto out;
  2172. }
  2173. key.offset = found_key.offset + 1;
  2174. btrfs_release_path(path);
  2175. }
  2176. out:
  2177. btrfs_free_path(path);
  2178. return ret;
  2179. }
  2180. /*
  2181. * Only creates the inode if it is:
  2182. * 1. Not a directory
  2183. * 2. Or a directory which was not created already due to out of order
  2184. * directories. See did_create_dir and process_recorded_refs for details.
  2185. */
  2186. static int send_create_inode_if_needed(struct send_ctx *sctx)
  2187. {
  2188. int ret;
  2189. if (S_ISDIR(sctx->cur_inode_mode)) {
  2190. ret = did_create_dir(sctx, sctx->cur_ino);
  2191. if (ret < 0)
  2192. goto out;
  2193. if (ret) {
  2194. ret = 0;
  2195. goto out;
  2196. }
  2197. }
  2198. ret = send_create_inode(sctx, sctx->cur_ino);
  2199. if (ret < 0)
  2200. goto out;
  2201. out:
  2202. return ret;
  2203. }
  2204. struct recorded_ref {
  2205. struct list_head list;
  2206. char *dir_path;
  2207. char *name;
  2208. struct fs_path *full_path;
  2209. u64 dir;
  2210. u64 dir_gen;
  2211. int dir_path_len;
  2212. int name_len;
  2213. };
  2214. /*
  2215. * We need to process new refs before deleted refs, but compare_tree gives us
  2216. * everything mixed. So we first record all refs and later process them.
  2217. * This function is a helper to record one ref.
  2218. */
  2219. static int record_ref(struct list_head *head, u64 dir,
  2220. u64 dir_gen, struct fs_path *path)
  2221. {
  2222. struct recorded_ref *ref;
  2223. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  2224. if (!ref)
  2225. return -ENOMEM;
  2226. ref->dir = dir;
  2227. ref->dir_gen = dir_gen;
  2228. ref->full_path = path;
  2229. ref->name = (char *)kbasename(ref->full_path->start);
  2230. ref->name_len = ref->full_path->end - ref->name;
  2231. ref->dir_path = ref->full_path->start;
  2232. if (ref->name == ref->full_path->start)
  2233. ref->dir_path_len = 0;
  2234. else
  2235. ref->dir_path_len = ref->full_path->end -
  2236. ref->full_path->start - 1 - ref->name_len;
  2237. list_add_tail(&ref->list, head);
  2238. return 0;
  2239. }
  2240. static int dup_ref(struct recorded_ref *ref, struct list_head *list)
  2241. {
  2242. struct recorded_ref *new;
  2243. new = kmalloc(sizeof(*ref), GFP_NOFS);
  2244. if (!new)
  2245. return -ENOMEM;
  2246. new->dir = ref->dir;
  2247. new->dir_gen = ref->dir_gen;
  2248. new->full_path = NULL;
  2249. INIT_LIST_HEAD(&new->list);
  2250. list_add_tail(&new->list, list);
  2251. return 0;
  2252. }
  2253. static void __free_recorded_refs(struct list_head *head)
  2254. {
  2255. struct recorded_ref *cur;
  2256. while (!list_empty(head)) {
  2257. cur = list_entry(head->next, struct recorded_ref, list);
  2258. fs_path_free(cur->full_path);
  2259. list_del(&cur->list);
  2260. kfree(cur);
  2261. }
  2262. }
  2263. static void free_recorded_refs(struct send_ctx *sctx)
  2264. {
  2265. __free_recorded_refs(&sctx->new_refs);
  2266. __free_recorded_refs(&sctx->deleted_refs);
  2267. }
  2268. /*
  2269. * Renames/moves a file/dir to its orphan name. Used when the first
  2270. * ref of an unprocessed inode gets overwritten and for all non empty
  2271. * directories.
  2272. */
  2273. static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
  2274. struct fs_path *path)
  2275. {
  2276. int ret;
  2277. struct fs_path *orphan;
  2278. orphan = fs_path_alloc();
  2279. if (!orphan)
  2280. return -ENOMEM;
  2281. ret = gen_unique_name(sctx, ino, gen, orphan);
  2282. if (ret < 0)
  2283. goto out;
  2284. ret = send_rename(sctx, path, orphan);
  2285. out:
  2286. fs_path_free(orphan);
  2287. return ret;
  2288. }
  2289. /*
  2290. * Returns 1 if a directory can be removed at this point in time.
  2291. * We check this by iterating all dir items and checking if the inode behind
  2292. * the dir item was already processed.
  2293. */
  2294. static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
  2295. {
  2296. int ret = 0;
  2297. struct btrfs_root *root = sctx->parent_root;
  2298. struct btrfs_path *path;
  2299. struct btrfs_key key;
  2300. struct btrfs_key found_key;
  2301. struct btrfs_key loc;
  2302. struct btrfs_dir_item *di;
  2303. /*
  2304. * Don't try to rmdir the top/root subvolume dir.
  2305. */
  2306. if (dir == BTRFS_FIRST_FREE_OBJECTID)
  2307. return 0;
  2308. path = alloc_path_for_send();
  2309. if (!path)
  2310. return -ENOMEM;
  2311. key.objectid = dir;
  2312. key.type = BTRFS_DIR_INDEX_KEY;
  2313. key.offset = 0;
  2314. while (1) {
  2315. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  2316. if (ret < 0)
  2317. goto out;
  2318. if (!ret) {
  2319. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2320. path->slots[0]);
  2321. }
  2322. if (ret || found_key.objectid != key.objectid ||
  2323. found_key.type != key.type) {
  2324. break;
  2325. }
  2326. di = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2327. struct btrfs_dir_item);
  2328. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
  2329. if (loc.objectid > send_progress) {
  2330. ret = 0;
  2331. goto out;
  2332. }
  2333. btrfs_release_path(path);
  2334. key.offset = found_key.offset + 1;
  2335. }
  2336. ret = 1;
  2337. out:
  2338. btrfs_free_path(path);
  2339. return ret;
  2340. }
  2341. static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
  2342. {
  2343. struct rb_node *n = sctx->waiting_dir_moves.rb_node;
  2344. struct waiting_dir_move *entry;
  2345. while (n) {
  2346. entry = rb_entry(n, struct waiting_dir_move, node);
  2347. if (ino < entry->ino)
  2348. n = n->rb_left;
  2349. else if (ino > entry->ino)
  2350. n = n->rb_right;
  2351. else
  2352. return 1;
  2353. }
  2354. return 0;
  2355. }
  2356. static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
  2357. {
  2358. struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
  2359. struct rb_node *parent = NULL;
  2360. struct waiting_dir_move *entry, *dm;
  2361. dm = kmalloc(sizeof(*dm), GFP_NOFS);
  2362. if (!dm)
  2363. return -ENOMEM;
  2364. dm->ino = ino;
  2365. while (*p) {
  2366. parent = *p;
  2367. entry = rb_entry(parent, struct waiting_dir_move, node);
  2368. if (ino < entry->ino) {
  2369. p = &(*p)->rb_left;
  2370. } else if (ino > entry->ino) {
  2371. p = &(*p)->rb_right;
  2372. } else {
  2373. kfree(dm);
  2374. return -EEXIST;
  2375. }
  2376. }
  2377. rb_link_node(&dm->node, parent, p);
  2378. rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
  2379. return 0;
  2380. }
  2381. static int del_waiting_dir_move(struct send_ctx *sctx, u64 ino)
  2382. {
  2383. struct rb_node *n = sctx->waiting_dir_moves.rb_node;
  2384. struct waiting_dir_move *entry;
  2385. while (n) {
  2386. entry = rb_entry(n, struct waiting_dir_move, node);
  2387. if (ino < entry->ino) {
  2388. n = n->rb_left;
  2389. } else if (ino > entry->ino) {
  2390. n = n->rb_right;
  2391. } else {
  2392. rb_erase(&entry->node, &sctx->waiting_dir_moves);
  2393. kfree(entry);
  2394. return 0;
  2395. }
  2396. }
  2397. return -ENOENT;
  2398. }
  2399. static int add_pending_dir_move(struct send_ctx *sctx, u64 parent_ino)
  2400. {
  2401. struct rb_node **p = &sctx->pending_dir_moves.rb_node;
  2402. struct rb_node *parent = NULL;
  2403. struct pending_dir_move *entry, *pm;
  2404. struct recorded_ref *cur;
  2405. int exists = 0;
  2406. int ret;
  2407. pm = kmalloc(sizeof(*pm), GFP_NOFS);
  2408. if (!pm)
  2409. return -ENOMEM;
  2410. pm->parent_ino = parent_ino;
  2411. pm->ino = sctx->cur_ino;
  2412. pm->gen = sctx->cur_inode_gen;
  2413. INIT_LIST_HEAD(&pm->list);
  2414. INIT_LIST_HEAD(&pm->update_refs);
  2415. RB_CLEAR_NODE(&pm->node);
  2416. while (*p) {
  2417. parent = *p;
  2418. entry = rb_entry(parent, struct pending_dir_move, node);
  2419. if (parent_ino < entry->parent_ino) {
  2420. p = &(*p)->rb_left;
  2421. } else if (parent_ino > entry->parent_ino) {
  2422. p = &(*p)->rb_right;
  2423. } else {
  2424. exists = 1;
  2425. break;
  2426. }
  2427. }
  2428. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  2429. ret = dup_ref(cur, &pm->update_refs);
  2430. if (ret < 0)
  2431. goto out;
  2432. }
  2433. list_for_each_entry(cur, &sctx->new_refs, list) {
  2434. ret = dup_ref(cur, &pm->update_refs);
  2435. if (ret < 0)
  2436. goto out;
  2437. }
  2438. ret = add_waiting_dir_move(sctx, pm->ino);
  2439. if (ret)
  2440. goto out;
  2441. if (exists) {
  2442. list_add_tail(&pm->list, &entry->list);
  2443. } else {
  2444. rb_link_node(&pm->node, parent, p);
  2445. rb_insert_color(&pm->node, &sctx->pending_dir_moves);
  2446. }
  2447. ret = 0;
  2448. out:
  2449. if (ret) {
  2450. __free_recorded_refs(&pm->update_refs);
  2451. kfree(pm);
  2452. }
  2453. return ret;
  2454. }
  2455. static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
  2456. u64 parent_ino)
  2457. {
  2458. struct rb_node *n = sctx->pending_dir_moves.rb_node;
  2459. struct pending_dir_move *entry;
  2460. while (n) {
  2461. entry = rb_entry(n, struct pending_dir_move, node);
  2462. if (parent_ino < entry->parent_ino)
  2463. n = n->rb_left;
  2464. else if (parent_ino > entry->parent_ino)
  2465. n = n->rb_right;
  2466. else
  2467. return entry;
  2468. }
  2469. return NULL;
  2470. }
  2471. static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
  2472. {
  2473. struct fs_path *from_path = NULL;
  2474. struct fs_path *to_path = NULL;
  2475. u64 orig_progress = sctx->send_progress;
  2476. struct recorded_ref *cur;
  2477. int ret;
  2478. from_path = fs_path_alloc();
  2479. if (!from_path)
  2480. return -ENOMEM;
  2481. sctx->send_progress = pm->ino;
  2482. ret = get_cur_path(sctx, pm->ino, pm->gen, from_path);
  2483. if (ret < 0)
  2484. goto out;
  2485. to_path = fs_path_alloc();
  2486. if (!to_path) {
  2487. ret = -ENOMEM;
  2488. goto out;
  2489. }
  2490. sctx->send_progress = sctx->cur_ino + 1;
  2491. ret = del_waiting_dir_move(sctx, pm->ino);
  2492. ASSERT(ret == 0);
  2493. ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
  2494. if (ret < 0)
  2495. goto out;
  2496. ret = send_rename(sctx, from_path, to_path);
  2497. if (ret < 0)
  2498. goto out;
  2499. ret = send_utimes(sctx, pm->ino, pm->gen);
  2500. if (ret < 0)
  2501. goto out;
  2502. /*
  2503. * After rename/move, need to update the utimes of both new parent(s)
  2504. * and old parent(s).
  2505. */
  2506. list_for_each_entry(cur, &pm->update_refs, list) {
  2507. ret = send_utimes(sctx, cur->dir, cur->dir_gen);
  2508. if (ret < 0)
  2509. goto out;
  2510. }
  2511. out:
  2512. fs_path_free(from_path);
  2513. fs_path_free(to_path);
  2514. sctx->send_progress = orig_progress;
  2515. return ret;
  2516. }
  2517. static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
  2518. {
  2519. if (!list_empty(&m->list))
  2520. list_del(&m->list);
  2521. if (!RB_EMPTY_NODE(&m->node))
  2522. rb_erase(&m->node, &sctx->pending_dir_moves);
  2523. __free_recorded_refs(&m->update_refs);
  2524. kfree(m);
  2525. }
  2526. static void tail_append_pending_moves(struct pending_dir_move *moves,
  2527. struct list_head *stack)
  2528. {
  2529. if (list_empty(&moves->list)) {
  2530. list_add_tail(&moves->list, stack);
  2531. } else {
  2532. LIST_HEAD(list);
  2533. list_splice_init(&moves->list, &list);
  2534. list_add_tail(&moves->list, stack);
  2535. list_splice_tail(&list, stack);
  2536. }
  2537. }
  2538. static int apply_children_dir_moves(struct send_ctx *sctx)
  2539. {
  2540. struct pending_dir_move *pm;
  2541. struct list_head stack;
  2542. u64 parent_ino = sctx->cur_ino;
  2543. int ret = 0;
  2544. pm = get_pending_dir_moves(sctx, parent_ino);
  2545. if (!pm)
  2546. return 0;
  2547. INIT_LIST_HEAD(&stack);
  2548. tail_append_pending_moves(pm, &stack);
  2549. while (!list_empty(&stack)) {
  2550. pm = list_first_entry(&stack, struct pending_dir_move, list);
  2551. parent_ino = pm->ino;
  2552. ret = apply_dir_move(sctx, pm);
  2553. free_pending_move(sctx, pm);
  2554. if (ret)
  2555. goto out;
  2556. pm = get_pending_dir_moves(sctx, parent_ino);
  2557. if (pm)
  2558. tail_append_pending_moves(pm, &stack);
  2559. }
  2560. return 0;
  2561. out:
  2562. while (!list_empty(&stack)) {
  2563. pm = list_first_entry(&stack, struct pending_dir_move, list);
  2564. free_pending_move(sctx, pm);
  2565. }
  2566. return ret;
  2567. }
  2568. static int wait_for_parent_move(struct send_ctx *sctx,
  2569. struct recorded_ref *parent_ref)
  2570. {
  2571. int ret;
  2572. u64 ino = parent_ref->dir;
  2573. u64 parent_ino_before, parent_ino_after;
  2574. u64 new_gen, old_gen;
  2575. struct fs_path *path_before = NULL;
  2576. struct fs_path *path_after = NULL;
  2577. int len1, len2;
  2578. if (parent_ref->dir <= sctx->cur_ino)
  2579. return 0;
  2580. if (is_waiting_for_move(sctx, ino))
  2581. return 1;
  2582. ret = get_inode_info(sctx->parent_root, ino, NULL, &old_gen,
  2583. NULL, NULL, NULL, NULL);
  2584. if (ret == -ENOENT)
  2585. return 0;
  2586. else if (ret < 0)
  2587. return ret;
  2588. ret = get_inode_info(sctx->send_root, ino, NULL, &new_gen,
  2589. NULL, NULL, NULL, NULL);
  2590. if (ret < 0)
  2591. return ret;
  2592. if (new_gen != old_gen)
  2593. return 0;
  2594. path_before = fs_path_alloc();
  2595. if (!path_before)
  2596. return -ENOMEM;
  2597. ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
  2598. NULL, path_before);
  2599. if (ret == -ENOENT) {
  2600. ret = 0;
  2601. goto out;
  2602. } else if (ret < 0) {
  2603. goto out;
  2604. }
  2605. path_after = fs_path_alloc();
  2606. if (!path_after) {
  2607. ret = -ENOMEM;
  2608. goto out;
  2609. }
  2610. ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
  2611. NULL, path_after);
  2612. if (ret == -ENOENT) {
  2613. ret = 0;
  2614. goto out;
  2615. } else if (ret < 0) {
  2616. goto out;
  2617. }
  2618. len1 = fs_path_len(path_before);
  2619. len2 = fs_path_len(path_after);
  2620. if ((parent_ino_before != parent_ino_after) && (len1 != len2 ||
  2621. memcmp(path_before->start, path_after->start, len1))) {
  2622. ret = 1;
  2623. goto out;
  2624. }
  2625. ret = 0;
  2626. out:
  2627. fs_path_free(path_before);
  2628. fs_path_free(path_after);
  2629. return ret;
  2630. }
  2631. /*
  2632. * This does all the move/link/unlink/rmdir magic.
  2633. */
  2634. static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
  2635. {
  2636. int ret = 0;
  2637. struct recorded_ref *cur;
  2638. struct recorded_ref *cur2;
  2639. struct list_head check_dirs;
  2640. struct fs_path *valid_path = NULL;
  2641. u64 ow_inode = 0;
  2642. u64 ow_gen;
  2643. int did_overwrite = 0;
  2644. int is_orphan = 0;
  2645. verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
  2646. /*
  2647. * This should never happen as the root dir always has the same ref
  2648. * which is always '..'
  2649. */
  2650. BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
  2651. INIT_LIST_HEAD(&check_dirs);
  2652. valid_path = fs_path_alloc();
  2653. if (!valid_path) {
  2654. ret = -ENOMEM;
  2655. goto out;
  2656. }
  2657. /*
  2658. * First, check if the first ref of the current inode was overwritten
  2659. * before. If yes, we know that the current inode was already orphanized
  2660. * and thus use the orphan name. If not, we can use get_cur_path to
  2661. * get the path of the first ref as it would like while receiving at
  2662. * this point in time.
  2663. * New inodes are always orphan at the beginning, so force to use the
  2664. * orphan name in this case.
  2665. * The first ref is stored in valid_path and will be updated if it
  2666. * gets moved around.
  2667. */
  2668. if (!sctx->cur_inode_new) {
  2669. ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
  2670. sctx->cur_inode_gen);
  2671. if (ret < 0)
  2672. goto out;
  2673. if (ret)
  2674. did_overwrite = 1;
  2675. }
  2676. if (sctx->cur_inode_new || did_overwrite) {
  2677. ret = gen_unique_name(sctx, sctx->cur_ino,
  2678. sctx->cur_inode_gen, valid_path);
  2679. if (ret < 0)
  2680. goto out;
  2681. is_orphan = 1;
  2682. } else {
  2683. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  2684. valid_path);
  2685. if (ret < 0)
  2686. goto out;
  2687. }
  2688. list_for_each_entry(cur, &sctx->new_refs, list) {
  2689. /*
  2690. * We may have refs where the parent directory does not exist
  2691. * yet. This happens if the parent directories inum is higher
  2692. * the the current inum. To handle this case, we create the
  2693. * parent directory out of order. But we need to check if this
  2694. * did already happen before due to other refs in the same dir.
  2695. */
  2696. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
  2697. if (ret < 0)
  2698. goto out;
  2699. if (ret == inode_state_will_create) {
  2700. ret = 0;
  2701. /*
  2702. * First check if any of the current inodes refs did
  2703. * already create the dir.
  2704. */
  2705. list_for_each_entry(cur2, &sctx->new_refs, list) {
  2706. if (cur == cur2)
  2707. break;
  2708. if (cur2->dir == cur->dir) {
  2709. ret = 1;
  2710. break;
  2711. }
  2712. }
  2713. /*
  2714. * If that did not happen, check if a previous inode
  2715. * did already create the dir.
  2716. */
  2717. if (!ret)
  2718. ret = did_create_dir(sctx, cur->dir);
  2719. if (ret < 0)
  2720. goto out;
  2721. if (!ret) {
  2722. ret = send_create_inode(sctx, cur->dir);
  2723. if (ret < 0)
  2724. goto out;
  2725. }
  2726. }
  2727. /*
  2728. * Check if this new ref would overwrite the first ref of
  2729. * another unprocessed inode. If yes, orphanize the
  2730. * overwritten inode. If we find an overwritten ref that is
  2731. * not the first ref, simply unlink it.
  2732. */
  2733. ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  2734. cur->name, cur->name_len,
  2735. &ow_inode, &ow_gen);
  2736. if (ret < 0)
  2737. goto out;
  2738. if (ret) {
  2739. ret = is_first_ref(sctx->parent_root,
  2740. ow_inode, cur->dir, cur->name,
  2741. cur->name_len);
  2742. if (ret < 0)
  2743. goto out;
  2744. if (ret) {
  2745. ret = orphanize_inode(sctx, ow_inode, ow_gen,
  2746. cur->full_path);
  2747. if (ret < 0)
  2748. goto out;
  2749. } else {
  2750. ret = send_unlink(sctx, cur->full_path);
  2751. if (ret < 0)
  2752. goto out;
  2753. }
  2754. }
  2755. /*
  2756. * link/move the ref to the new place. If we have an orphan
  2757. * inode, move it and update valid_path. If not, link or move
  2758. * it depending on the inode mode.
  2759. */
  2760. if (is_orphan) {
  2761. ret = send_rename(sctx, valid_path, cur->full_path);
  2762. if (ret < 0)
  2763. goto out;
  2764. is_orphan = 0;
  2765. ret = fs_path_copy(valid_path, cur->full_path);
  2766. if (ret < 0)
  2767. goto out;
  2768. } else {
  2769. if (S_ISDIR(sctx->cur_inode_mode)) {
  2770. /*
  2771. * Dirs can't be linked, so move it. For moved
  2772. * dirs, we always have one new and one deleted
  2773. * ref. The deleted ref is ignored later.
  2774. */
  2775. if (wait_for_parent_move(sctx, cur)) {
  2776. ret = add_pending_dir_move(sctx,
  2777. cur->dir);
  2778. *pending_move = 1;
  2779. } else {
  2780. ret = send_rename(sctx, valid_path,
  2781. cur->full_path);
  2782. if (!ret)
  2783. ret = fs_path_copy(valid_path,
  2784. cur->full_path);
  2785. }
  2786. if (ret < 0)
  2787. goto out;
  2788. } else {
  2789. ret = send_link(sctx, cur->full_path,
  2790. valid_path);
  2791. if (ret < 0)
  2792. goto out;
  2793. }
  2794. }
  2795. ret = dup_ref(cur, &check_dirs);
  2796. if (ret < 0)
  2797. goto out;
  2798. }
  2799. if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
  2800. /*
  2801. * Check if we can already rmdir the directory. If not,
  2802. * orphanize it. For every dir item inside that gets deleted
  2803. * later, we do this check again and rmdir it then if possible.
  2804. * See the use of check_dirs for more details.
  2805. */
  2806. ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
  2807. if (ret < 0)
  2808. goto out;
  2809. if (ret) {
  2810. ret = send_rmdir(sctx, valid_path);
  2811. if (ret < 0)
  2812. goto out;
  2813. } else if (!is_orphan) {
  2814. ret = orphanize_inode(sctx, sctx->cur_ino,
  2815. sctx->cur_inode_gen, valid_path);
  2816. if (ret < 0)
  2817. goto out;
  2818. is_orphan = 1;
  2819. }
  2820. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  2821. ret = dup_ref(cur, &check_dirs);
  2822. if (ret < 0)
  2823. goto out;
  2824. }
  2825. } else if (S_ISDIR(sctx->cur_inode_mode) &&
  2826. !list_empty(&sctx->deleted_refs)) {
  2827. /*
  2828. * We have a moved dir. Add the old parent to check_dirs
  2829. */
  2830. cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
  2831. list);
  2832. ret = dup_ref(cur, &check_dirs);
  2833. if (ret < 0)
  2834. goto out;
  2835. } else if (!S_ISDIR(sctx->cur_inode_mode)) {
  2836. /*
  2837. * We have a non dir inode. Go through all deleted refs and
  2838. * unlink them if they were not already overwritten by other
  2839. * inodes.
  2840. */
  2841. list_for_each_entry(cur, &sctx->deleted_refs, list) {
  2842. ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
  2843. sctx->cur_ino, sctx->cur_inode_gen,
  2844. cur->name, cur->name_len);
  2845. if (ret < 0)
  2846. goto out;
  2847. if (!ret) {
  2848. ret = send_unlink(sctx, cur->full_path);
  2849. if (ret < 0)
  2850. goto out;
  2851. }
  2852. ret = dup_ref(cur, &check_dirs);
  2853. if (ret < 0)
  2854. goto out;
  2855. }
  2856. /*
  2857. * If the inode is still orphan, unlink the orphan. This may
  2858. * happen when a previous inode did overwrite the first ref
  2859. * of this inode and no new refs were added for the current
  2860. * inode. Unlinking does not mean that the inode is deleted in
  2861. * all cases. There may still be links to this inode in other
  2862. * places.
  2863. */
  2864. if (is_orphan) {
  2865. ret = send_unlink(sctx, valid_path);
  2866. if (ret < 0)
  2867. goto out;
  2868. }
  2869. }
  2870. /*
  2871. * We did collect all parent dirs where cur_inode was once located. We
  2872. * now go through all these dirs and check if they are pending for
  2873. * deletion and if it's finally possible to perform the rmdir now.
  2874. * We also update the inode stats of the parent dirs here.
  2875. */
  2876. list_for_each_entry(cur, &check_dirs, list) {
  2877. /*
  2878. * In case we had refs into dirs that were not processed yet,
  2879. * we don't need to do the utime and rmdir logic for these dirs.
  2880. * The dir will be processed later.
  2881. */
  2882. if (cur->dir > sctx->cur_ino)
  2883. continue;
  2884. ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
  2885. if (ret < 0)
  2886. goto out;
  2887. if (ret == inode_state_did_create ||
  2888. ret == inode_state_no_change) {
  2889. /* TODO delayed utimes */
  2890. ret = send_utimes(sctx, cur->dir, cur->dir_gen);
  2891. if (ret < 0)
  2892. goto out;
  2893. } else if (ret == inode_state_did_delete) {
  2894. ret = can_rmdir(sctx, cur->dir, sctx->cur_ino);
  2895. if (ret < 0)
  2896. goto out;
  2897. if (ret) {
  2898. ret = get_cur_path(sctx, cur->dir,
  2899. cur->dir_gen, valid_path);
  2900. if (ret < 0)
  2901. goto out;
  2902. ret = send_rmdir(sctx, valid_path);
  2903. if (ret < 0)
  2904. goto out;
  2905. }
  2906. }
  2907. }
  2908. ret = 0;
  2909. out:
  2910. __free_recorded_refs(&check_dirs);
  2911. free_recorded_refs(sctx);
  2912. fs_path_free(valid_path);
  2913. return ret;
  2914. }
  2915. static int __record_new_ref(int num, u64 dir, int index,
  2916. struct fs_path *name,
  2917. void *ctx)
  2918. {
  2919. int ret = 0;
  2920. struct send_ctx *sctx = ctx;
  2921. struct fs_path *p;
  2922. u64 gen;
  2923. p = fs_path_alloc();
  2924. if (!p)
  2925. return -ENOMEM;
  2926. ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
  2927. NULL, NULL);
  2928. if (ret < 0)
  2929. goto out;
  2930. ret = get_cur_path(sctx, dir, gen, p);
  2931. if (ret < 0)
  2932. goto out;
  2933. ret = fs_path_add_path(p, name);
  2934. if (ret < 0)
  2935. goto out;
  2936. ret = record_ref(&sctx->new_refs, dir, gen, p);
  2937. out:
  2938. if (ret)
  2939. fs_path_free(p);
  2940. return ret;
  2941. }
  2942. static int __record_deleted_ref(int num, u64 dir, int index,
  2943. struct fs_path *name,
  2944. void *ctx)
  2945. {
  2946. int ret = 0;
  2947. struct send_ctx *sctx = ctx;
  2948. struct fs_path *p;
  2949. u64 gen;
  2950. p = fs_path_alloc();
  2951. if (!p)
  2952. return -ENOMEM;
  2953. ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
  2954. NULL, NULL);
  2955. if (ret < 0)
  2956. goto out;
  2957. ret = get_cur_path(sctx, dir, gen, p);
  2958. if (ret < 0)
  2959. goto out;
  2960. ret = fs_path_add_path(p, name);
  2961. if (ret < 0)
  2962. goto out;
  2963. ret = record_ref(&sctx->deleted_refs, dir, gen, p);
  2964. out:
  2965. if (ret)
  2966. fs_path_free(p);
  2967. return ret;
  2968. }
  2969. static int record_new_ref(struct send_ctx *sctx)
  2970. {
  2971. int ret;
  2972. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  2973. sctx->cmp_key, 0, __record_new_ref, sctx);
  2974. if (ret < 0)
  2975. goto out;
  2976. ret = 0;
  2977. out:
  2978. return ret;
  2979. }
  2980. static int record_deleted_ref(struct send_ctx *sctx)
  2981. {
  2982. int ret;
  2983. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  2984. sctx->cmp_key, 0, __record_deleted_ref, sctx);
  2985. if (ret < 0)
  2986. goto out;
  2987. ret = 0;
  2988. out:
  2989. return ret;
  2990. }
  2991. struct find_ref_ctx {
  2992. u64 dir;
  2993. u64 dir_gen;
  2994. struct btrfs_root *root;
  2995. struct fs_path *name;
  2996. int found_idx;
  2997. };
  2998. static int __find_iref(int num, u64 dir, int index,
  2999. struct fs_path *name,
  3000. void *ctx_)
  3001. {
  3002. struct find_ref_ctx *ctx = ctx_;
  3003. u64 dir_gen;
  3004. int ret;
  3005. if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
  3006. strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
  3007. /*
  3008. * To avoid doing extra lookups we'll only do this if everything
  3009. * else matches.
  3010. */
  3011. ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
  3012. NULL, NULL, NULL);
  3013. if (ret)
  3014. return ret;
  3015. if (dir_gen != ctx->dir_gen)
  3016. return 0;
  3017. ctx->found_idx = num;
  3018. return 1;
  3019. }
  3020. return 0;
  3021. }
  3022. static int find_iref(struct btrfs_root *root,
  3023. struct btrfs_path *path,
  3024. struct btrfs_key *key,
  3025. u64 dir, u64 dir_gen, struct fs_path *name)
  3026. {
  3027. int ret;
  3028. struct find_ref_ctx ctx;
  3029. ctx.dir = dir;
  3030. ctx.name = name;
  3031. ctx.dir_gen = dir_gen;
  3032. ctx.found_idx = -1;
  3033. ctx.root = root;
  3034. ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
  3035. if (ret < 0)
  3036. return ret;
  3037. if (ctx.found_idx == -1)
  3038. return -ENOENT;
  3039. return ctx.found_idx;
  3040. }
  3041. static int __record_changed_new_ref(int num, u64 dir, int index,
  3042. struct fs_path *name,
  3043. void *ctx)
  3044. {
  3045. u64 dir_gen;
  3046. int ret;
  3047. struct send_ctx *sctx = ctx;
  3048. ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
  3049. NULL, NULL, NULL);
  3050. if (ret)
  3051. return ret;
  3052. ret = find_iref(sctx->parent_root, sctx->right_path,
  3053. sctx->cmp_key, dir, dir_gen, name);
  3054. if (ret == -ENOENT)
  3055. ret = __record_new_ref(num, dir, index, name, sctx);
  3056. else if (ret > 0)
  3057. ret = 0;
  3058. return ret;
  3059. }
  3060. static int __record_changed_deleted_ref(int num, u64 dir, int index,
  3061. struct fs_path *name,
  3062. void *ctx)
  3063. {
  3064. u64 dir_gen;
  3065. int ret;
  3066. struct send_ctx *sctx = ctx;
  3067. ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
  3068. NULL, NULL, NULL);
  3069. if (ret)
  3070. return ret;
  3071. ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
  3072. dir, dir_gen, name);
  3073. if (ret == -ENOENT)
  3074. ret = __record_deleted_ref(num, dir, index, name, sctx);
  3075. else if (ret > 0)
  3076. ret = 0;
  3077. return ret;
  3078. }
  3079. static int record_changed_ref(struct send_ctx *sctx)
  3080. {
  3081. int ret = 0;
  3082. ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
  3083. sctx->cmp_key, 0, __record_changed_new_ref, sctx);
  3084. if (ret < 0)
  3085. goto out;
  3086. ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
  3087. sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
  3088. if (ret < 0)
  3089. goto out;
  3090. ret = 0;
  3091. out:
  3092. return ret;
  3093. }
  3094. /*
  3095. * Record and process all refs at once. Needed when an inode changes the
  3096. * generation number, which means that it was deleted and recreated.
  3097. */
  3098. static int process_all_refs(struct send_ctx *sctx,
  3099. enum btrfs_compare_tree_result cmd)
  3100. {
  3101. int ret;
  3102. struct btrfs_root *root;
  3103. struct btrfs_path *path;
  3104. struct btrfs_key key;
  3105. struct btrfs_key found_key;
  3106. struct extent_buffer *eb;
  3107. int slot;
  3108. iterate_inode_ref_t cb;
  3109. int pending_move = 0;
  3110. path = alloc_path_for_send();
  3111. if (!path)
  3112. return -ENOMEM;
  3113. if (cmd == BTRFS_COMPARE_TREE_NEW) {
  3114. root = sctx->send_root;
  3115. cb = __record_new_ref;
  3116. } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
  3117. root = sctx->parent_root;
  3118. cb = __record_deleted_ref;
  3119. } else {
  3120. BUG();
  3121. }
  3122. key.objectid = sctx->cmp_key->objectid;
  3123. key.type = BTRFS_INODE_REF_KEY;
  3124. key.offset = 0;
  3125. while (1) {
  3126. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  3127. if (ret < 0)
  3128. goto out;
  3129. if (ret)
  3130. break;
  3131. eb = path->nodes[0];
  3132. slot = path->slots[0];
  3133. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3134. if (found_key.objectid != key.objectid ||
  3135. (found_key.type != BTRFS_INODE_REF_KEY &&
  3136. found_key.type != BTRFS_INODE_EXTREF_KEY))
  3137. break;
  3138. ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
  3139. btrfs_release_path(path);
  3140. if (ret < 0)
  3141. goto out;
  3142. key.offset = found_key.offset + 1;
  3143. }
  3144. btrfs_release_path(path);
  3145. ret = process_recorded_refs(sctx, &pending_move);
  3146. /* Only applicable to an incremental send. */
  3147. ASSERT(pending_move == 0);
  3148. out:
  3149. btrfs_free_path(path);
  3150. return ret;
  3151. }
  3152. static int send_set_xattr(struct send_ctx *sctx,
  3153. struct fs_path *path,
  3154. const char *name, int name_len,
  3155. const char *data, int data_len)
  3156. {
  3157. int ret = 0;
  3158. ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
  3159. if (ret < 0)
  3160. goto out;
  3161. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  3162. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  3163. TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
  3164. ret = send_cmd(sctx);
  3165. tlv_put_failure:
  3166. out:
  3167. return ret;
  3168. }
  3169. static int send_remove_xattr(struct send_ctx *sctx,
  3170. struct fs_path *path,
  3171. const char *name, int name_len)
  3172. {
  3173. int ret = 0;
  3174. ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
  3175. if (ret < 0)
  3176. goto out;
  3177. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
  3178. TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
  3179. ret = send_cmd(sctx);
  3180. tlv_put_failure:
  3181. out:
  3182. return ret;
  3183. }
  3184. static int __process_new_xattr(int num, struct btrfs_key *di_key,
  3185. const char *name, int name_len,
  3186. const char *data, int data_len,
  3187. u8 type, void *ctx)
  3188. {
  3189. int ret;
  3190. struct send_ctx *sctx = ctx;
  3191. struct fs_path *p;
  3192. posix_acl_xattr_header dummy_acl;
  3193. p = fs_path_alloc();
  3194. if (!p)
  3195. return -ENOMEM;
  3196. /*
  3197. * This hack is needed because empty acl's are stored as zero byte
  3198. * data in xattrs. Problem with that is, that receiving these zero byte
  3199. * acl's will fail later. To fix this, we send a dummy acl list that
  3200. * only contains the version number and no entries.
  3201. */
  3202. if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
  3203. !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
  3204. if (data_len == 0) {
  3205. dummy_acl.a_version =
  3206. cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  3207. data = (char *)&dummy_acl;
  3208. data_len = sizeof(dummy_acl);
  3209. }
  3210. }
  3211. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3212. if (ret < 0)
  3213. goto out;
  3214. ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
  3215. out:
  3216. fs_path_free(p);
  3217. return ret;
  3218. }
  3219. static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
  3220. const char *name, int name_len,
  3221. const char *data, int data_len,
  3222. u8 type, void *ctx)
  3223. {
  3224. int ret;
  3225. struct send_ctx *sctx = ctx;
  3226. struct fs_path *p;
  3227. p = fs_path_alloc();
  3228. if (!p)
  3229. return -ENOMEM;
  3230. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3231. if (ret < 0)
  3232. goto out;
  3233. ret = send_remove_xattr(sctx, p, name, name_len);
  3234. out:
  3235. fs_path_free(p);
  3236. return ret;
  3237. }
  3238. static int process_new_xattr(struct send_ctx *sctx)
  3239. {
  3240. int ret = 0;
  3241. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  3242. sctx->cmp_key, __process_new_xattr, sctx);
  3243. return ret;
  3244. }
  3245. static int process_deleted_xattr(struct send_ctx *sctx)
  3246. {
  3247. int ret;
  3248. ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
  3249. sctx->cmp_key, __process_deleted_xattr, sctx);
  3250. return ret;
  3251. }
  3252. struct find_xattr_ctx {
  3253. const char *name;
  3254. int name_len;
  3255. int found_idx;
  3256. char *found_data;
  3257. int found_data_len;
  3258. };
  3259. static int __find_xattr(int num, struct btrfs_key *di_key,
  3260. const char *name, int name_len,
  3261. const char *data, int data_len,
  3262. u8 type, void *vctx)
  3263. {
  3264. struct find_xattr_ctx *ctx = vctx;
  3265. if (name_len == ctx->name_len &&
  3266. strncmp(name, ctx->name, name_len) == 0) {
  3267. ctx->found_idx = num;
  3268. ctx->found_data_len = data_len;
  3269. ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
  3270. if (!ctx->found_data)
  3271. return -ENOMEM;
  3272. return 1;
  3273. }
  3274. return 0;
  3275. }
  3276. static int find_xattr(struct btrfs_root *root,
  3277. struct btrfs_path *path,
  3278. struct btrfs_key *key,
  3279. const char *name, int name_len,
  3280. char **data, int *data_len)
  3281. {
  3282. int ret;
  3283. struct find_xattr_ctx ctx;
  3284. ctx.name = name;
  3285. ctx.name_len = name_len;
  3286. ctx.found_idx = -1;
  3287. ctx.found_data = NULL;
  3288. ctx.found_data_len = 0;
  3289. ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
  3290. if (ret < 0)
  3291. return ret;
  3292. if (ctx.found_idx == -1)
  3293. return -ENOENT;
  3294. if (data) {
  3295. *data = ctx.found_data;
  3296. *data_len = ctx.found_data_len;
  3297. } else {
  3298. kfree(ctx.found_data);
  3299. }
  3300. return ctx.found_idx;
  3301. }
  3302. static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
  3303. const char *name, int name_len,
  3304. const char *data, int data_len,
  3305. u8 type, void *ctx)
  3306. {
  3307. int ret;
  3308. struct send_ctx *sctx = ctx;
  3309. char *found_data = NULL;
  3310. int found_data_len = 0;
  3311. ret = find_xattr(sctx->parent_root, sctx->right_path,
  3312. sctx->cmp_key, name, name_len, &found_data,
  3313. &found_data_len);
  3314. if (ret == -ENOENT) {
  3315. ret = __process_new_xattr(num, di_key, name, name_len, data,
  3316. data_len, type, ctx);
  3317. } else if (ret >= 0) {
  3318. if (data_len != found_data_len ||
  3319. memcmp(data, found_data, data_len)) {
  3320. ret = __process_new_xattr(num, di_key, name, name_len,
  3321. data, data_len, type, ctx);
  3322. } else {
  3323. ret = 0;
  3324. }
  3325. }
  3326. kfree(found_data);
  3327. return ret;
  3328. }
  3329. static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
  3330. const char *name, int name_len,
  3331. const char *data, int data_len,
  3332. u8 type, void *ctx)
  3333. {
  3334. int ret;
  3335. struct send_ctx *sctx = ctx;
  3336. ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
  3337. name, name_len, NULL, NULL);
  3338. if (ret == -ENOENT)
  3339. ret = __process_deleted_xattr(num, di_key, name, name_len, data,
  3340. data_len, type, ctx);
  3341. else if (ret >= 0)
  3342. ret = 0;
  3343. return ret;
  3344. }
  3345. static int process_changed_xattr(struct send_ctx *sctx)
  3346. {
  3347. int ret = 0;
  3348. ret = iterate_dir_item(sctx->send_root, sctx->left_path,
  3349. sctx->cmp_key, __process_changed_new_xattr, sctx);
  3350. if (ret < 0)
  3351. goto out;
  3352. ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
  3353. sctx->cmp_key, __process_changed_deleted_xattr, sctx);
  3354. out:
  3355. return ret;
  3356. }
  3357. static int process_all_new_xattrs(struct send_ctx *sctx)
  3358. {
  3359. int ret;
  3360. struct btrfs_root *root;
  3361. struct btrfs_path *path;
  3362. struct btrfs_key key;
  3363. struct btrfs_key found_key;
  3364. struct extent_buffer *eb;
  3365. int slot;
  3366. path = alloc_path_for_send();
  3367. if (!path)
  3368. return -ENOMEM;
  3369. root = sctx->send_root;
  3370. key.objectid = sctx->cmp_key->objectid;
  3371. key.type = BTRFS_XATTR_ITEM_KEY;
  3372. key.offset = 0;
  3373. while (1) {
  3374. ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
  3375. if (ret < 0)
  3376. goto out;
  3377. if (ret) {
  3378. ret = 0;
  3379. goto out;
  3380. }
  3381. eb = path->nodes[0];
  3382. slot = path->slots[0];
  3383. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3384. if (found_key.objectid != key.objectid ||
  3385. found_key.type != key.type) {
  3386. ret = 0;
  3387. goto out;
  3388. }
  3389. ret = iterate_dir_item(root, path, &found_key,
  3390. __process_new_xattr, sctx);
  3391. if (ret < 0)
  3392. goto out;
  3393. btrfs_release_path(path);
  3394. key.offset = found_key.offset + 1;
  3395. }
  3396. out:
  3397. btrfs_free_path(path);
  3398. return ret;
  3399. }
  3400. static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
  3401. {
  3402. struct btrfs_root *root = sctx->send_root;
  3403. struct btrfs_fs_info *fs_info = root->fs_info;
  3404. struct inode *inode;
  3405. struct page *page;
  3406. char *addr;
  3407. struct btrfs_key key;
  3408. pgoff_t index = offset >> PAGE_CACHE_SHIFT;
  3409. pgoff_t last_index;
  3410. unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
  3411. ssize_t ret = 0;
  3412. key.objectid = sctx->cur_ino;
  3413. key.type = BTRFS_INODE_ITEM_KEY;
  3414. key.offset = 0;
  3415. inode = btrfs_iget(fs_info->sb, &key, root, NULL);
  3416. if (IS_ERR(inode))
  3417. return PTR_ERR(inode);
  3418. if (offset + len > i_size_read(inode)) {
  3419. if (offset > i_size_read(inode))
  3420. len = 0;
  3421. else
  3422. len = offset - i_size_read(inode);
  3423. }
  3424. if (len == 0)
  3425. goto out;
  3426. last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
  3427. while (index <= last_index) {
  3428. unsigned cur_len = min_t(unsigned, len,
  3429. PAGE_CACHE_SIZE - pg_offset);
  3430. page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
  3431. if (!page) {
  3432. ret = -ENOMEM;
  3433. break;
  3434. }
  3435. if (!PageUptodate(page)) {
  3436. btrfs_readpage(NULL, page);
  3437. lock_page(page);
  3438. if (!PageUptodate(page)) {
  3439. unlock_page(page);
  3440. page_cache_release(page);
  3441. ret = -EIO;
  3442. break;
  3443. }
  3444. }
  3445. addr = kmap(page);
  3446. memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
  3447. kunmap(page);
  3448. unlock_page(page);
  3449. page_cache_release(page);
  3450. index++;
  3451. pg_offset = 0;
  3452. len -= cur_len;
  3453. ret += cur_len;
  3454. }
  3455. out:
  3456. iput(inode);
  3457. return ret;
  3458. }
  3459. /*
  3460. * Read some bytes from the current inode/file and send a write command to
  3461. * user space.
  3462. */
  3463. static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
  3464. {
  3465. int ret = 0;
  3466. struct fs_path *p;
  3467. ssize_t num_read = 0;
  3468. p = fs_path_alloc();
  3469. if (!p)
  3470. return -ENOMEM;
  3471. verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
  3472. num_read = fill_read_buf(sctx, offset, len);
  3473. if (num_read <= 0) {
  3474. if (num_read < 0)
  3475. ret = num_read;
  3476. goto out;
  3477. }
  3478. ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
  3479. if (ret < 0)
  3480. goto out;
  3481. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3482. if (ret < 0)
  3483. goto out;
  3484. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3485. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3486. TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
  3487. ret = send_cmd(sctx);
  3488. tlv_put_failure:
  3489. out:
  3490. fs_path_free(p);
  3491. if (ret < 0)
  3492. return ret;
  3493. return num_read;
  3494. }
  3495. /*
  3496. * Send a clone command to user space.
  3497. */
  3498. static int send_clone(struct send_ctx *sctx,
  3499. u64 offset, u32 len,
  3500. struct clone_root *clone_root)
  3501. {
  3502. int ret = 0;
  3503. struct fs_path *p;
  3504. u64 gen;
  3505. verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
  3506. "clone_inode=%llu, clone_offset=%llu\n", offset, len,
  3507. clone_root->root->objectid, clone_root->ino,
  3508. clone_root->offset);
  3509. p = fs_path_alloc();
  3510. if (!p)
  3511. return -ENOMEM;
  3512. ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
  3513. if (ret < 0)
  3514. goto out;
  3515. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3516. if (ret < 0)
  3517. goto out;
  3518. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3519. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
  3520. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3521. if (clone_root->root == sctx->send_root) {
  3522. ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
  3523. &gen, NULL, NULL, NULL, NULL);
  3524. if (ret < 0)
  3525. goto out;
  3526. ret = get_cur_path(sctx, clone_root->ino, gen, p);
  3527. } else {
  3528. ret = get_inode_path(clone_root->root, clone_root->ino, p);
  3529. }
  3530. if (ret < 0)
  3531. goto out;
  3532. TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
  3533. clone_root->root->root_item.uuid);
  3534. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
  3535. le64_to_cpu(clone_root->root->root_item.ctransid));
  3536. TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
  3537. TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
  3538. clone_root->offset);
  3539. ret = send_cmd(sctx);
  3540. tlv_put_failure:
  3541. out:
  3542. fs_path_free(p);
  3543. return ret;
  3544. }
  3545. /*
  3546. * Send an update extent command to user space.
  3547. */
  3548. static int send_update_extent(struct send_ctx *sctx,
  3549. u64 offset, u32 len)
  3550. {
  3551. int ret = 0;
  3552. struct fs_path *p;
  3553. p = fs_path_alloc();
  3554. if (!p)
  3555. return -ENOMEM;
  3556. ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
  3557. if (ret < 0)
  3558. goto out;
  3559. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3560. if (ret < 0)
  3561. goto out;
  3562. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3563. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3564. TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
  3565. ret = send_cmd(sctx);
  3566. tlv_put_failure:
  3567. out:
  3568. fs_path_free(p);
  3569. return ret;
  3570. }
  3571. static int send_hole(struct send_ctx *sctx, u64 end)
  3572. {
  3573. struct fs_path *p = NULL;
  3574. u64 offset = sctx->cur_inode_last_extent;
  3575. u64 len;
  3576. int ret = 0;
  3577. p = fs_path_alloc();
  3578. if (!p)
  3579. return -ENOMEM;
  3580. memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
  3581. while (offset < end) {
  3582. len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
  3583. ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
  3584. if (ret < 0)
  3585. break;
  3586. ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
  3587. if (ret < 0)
  3588. break;
  3589. TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
  3590. TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
  3591. TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
  3592. ret = send_cmd(sctx);
  3593. if (ret < 0)
  3594. break;
  3595. offset += len;
  3596. }
  3597. tlv_put_failure:
  3598. fs_path_free(p);
  3599. return ret;
  3600. }
  3601. static int send_write_or_clone(struct send_ctx *sctx,
  3602. struct btrfs_path *path,
  3603. struct btrfs_key *key,
  3604. struct clone_root *clone_root)
  3605. {
  3606. int ret = 0;
  3607. struct btrfs_file_extent_item *ei;
  3608. u64 offset = key->offset;
  3609. u64 pos = 0;
  3610. u64 len;
  3611. u32 l;
  3612. u8 type;
  3613. u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
  3614. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3615. struct btrfs_file_extent_item);
  3616. type = btrfs_file_extent_type(path->nodes[0], ei);
  3617. if (type == BTRFS_FILE_EXTENT_INLINE) {
  3618. len = btrfs_file_extent_inline_len(path->nodes[0],
  3619. path->slots[0], ei);
  3620. /*
  3621. * it is possible the inline item won't cover the whole page,
  3622. * but there may be items after this page. Make
  3623. * sure to send the whole thing
  3624. */
  3625. len = PAGE_CACHE_ALIGN(len);
  3626. } else {
  3627. len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
  3628. }
  3629. if (offset + len > sctx->cur_inode_size)
  3630. len = sctx->cur_inode_size - offset;
  3631. if (len == 0) {
  3632. ret = 0;
  3633. goto out;
  3634. }
  3635. if (clone_root && IS_ALIGNED(offset + len, bs)) {
  3636. ret = send_clone(sctx, offset, len, clone_root);
  3637. } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
  3638. ret = send_update_extent(sctx, offset, len);
  3639. } else {
  3640. while (pos < len) {
  3641. l = len - pos;
  3642. if (l > BTRFS_SEND_READ_SIZE)
  3643. l = BTRFS_SEND_READ_SIZE;
  3644. ret = send_write(sctx, pos + offset, l);
  3645. if (ret < 0)
  3646. goto out;
  3647. if (!ret)
  3648. break;
  3649. pos += ret;
  3650. }
  3651. ret = 0;
  3652. }
  3653. out:
  3654. return ret;
  3655. }
  3656. static int is_extent_unchanged(struct send_ctx *sctx,
  3657. struct btrfs_path *left_path,
  3658. struct btrfs_key *ekey)
  3659. {
  3660. int ret = 0;
  3661. struct btrfs_key key;
  3662. struct btrfs_path *path = NULL;
  3663. struct extent_buffer *eb;
  3664. int slot;
  3665. struct btrfs_key found_key;
  3666. struct btrfs_file_extent_item *ei;
  3667. u64 left_disknr;
  3668. u64 right_disknr;
  3669. u64 left_offset;
  3670. u64 right_offset;
  3671. u64 left_offset_fixed;
  3672. u64 left_len;
  3673. u64 right_len;
  3674. u64 left_gen;
  3675. u64 right_gen;
  3676. u8 left_type;
  3677. u8 right_type;
  3678. path = alloc_path_for_send();
  3679. if (!path)
  3680. return -ENOMEM;
  3681. eb = left_path->nodes[0];
  3682. slot = left_path->slots[0];
  3683. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  3684. left_type = btrfs_file_extent_type(eb, ei);
  3685. if (left_type != BTRFS_FILE_EXTENT_REG) {
  3686. ret = 0;
  3687. goto out;
  3688. }
  3689. left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  3690. left_len = btrfs_file_extent_num_bytes(eb, ei);
  3691. left_offset = btrfs_file_extent_offset(eb, ei);
  3692. left_gen = btrfs_file_extent_generation(eb, ei);
  3693. /*
  3694. * Following comments will refer to these graphics. L is the left
  3695. * extents which we are checking at the moment. 1-8 are the right
  3696. * extents that we iterate.
  3697. *
  3698. * |-----L-----|
  3699. * |-1-|-2a-|-3-|-4-|-5-|-6-|
  3700. *
  3701. * |-----L-----|
  3702. * |--1--|-2b-|...(same as above)
  3703. *
  3704. * Alternative situation. Happens on files where extents got split.
  3705. * |-----L-----|
  3706. * |-----------7-----------|-6-|
  3707. *
  3708. * Alternative situation. Happens on files which got larger.
  3709. * |-----L-----|
  3710. * |-8-|
  3711. * Nothing follows after 8.
  3712. */
  3713. key.objectid = ekey->objectid;
  3714. key.type = BTRFS_EXTENT_DATA_KEY;
  3715. key.offset = ekey->offset;
  3716. ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
  3717. if (ret < 0)
  3718. goto out;
  3719. if (ret) {
  3720. ret = 0;
  3721. goto out;
  3722. }
  3723. /*
  3724. * Handle special case where the right side has no extents at all.
  3725. */
  3726. eb = path->nodes[0];
  3727. slot = path->slots[0];
  3728. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3729. if (found_key.objectid != key.objectid ||
  3730. found_key.type != key.type) {
  3731. /* If we're a hole then just pretend nothing changed */
  3732. ret = (left_disknr) ? 0 : 1;
  3733. goto out;
  3734. }
  3735. /*
  3736. * We're now on 2a, 2b or 7.
  3737. */
  3738. key = found_key;
  3739. while (key.offset < ekey->offset + left_len) {
  3740. ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  3741. right_type = btrfs_file_extent_type(eb, ei);
  3742. if (right_type != BTRFS_FILE_EXTENT_REG) {
  3743. ret = 0;
  3744. goto out;
  3745. }
  3746. right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
  3747. right_len = btrfs_file_extent_num_bytes(eb, ei);
  3748. right_offset = btrfs_file_extent_offset(eb, ei);
  3749. right_gen = btrfs_file_extent_generation(eb, ei);
  3750. /*
  3751. * Are we at extent 8? If yes, we know the extent is changed.
  3752. * This may only happen on the first iteration.
  3753. */
  3754. if (found_key.offset + right_len <= ekey->offset) {
  3755. /* If we're a hole just pretend nothing changed */
  3756. ret = (left_disknr) ? 0 : 1;
  3757. goto out;
  3758. }
  3759. left_offset_fixed = left_offset;
  3760. if (key.offset < ekey->offset) {
  3761. /* Fix the right offset for 2a and 7. */
  3762. right_offset += ekey->offset - key.offset;
  3763. } else {
  3764. /* Fix the left offset for all behind 2a and 2b */
  3765. left_offset_fixed += key.offset - ekey->offset;
  3766. }
  3767. /*
  3768. * Check if we have the same extent.
  3769. */
  3770. if (left_disknr != right_disknr ||
  3771. left_offset_fixed != right_offset ||
  3772. left_gen != right_gen) {
  3773. ret = 0;
  3774. goto out;
  3775. }
  3776. /*
  3777. * Go to the next extent.
  3778. */
  3779. ret = btrfs_next_item(sctx->parent_root, path);
  3780. if (ret < 0)
  3781. goto out;
  3782. if (!ret) {
  3783. eb = path->nodes[0];
  3784. slot = path->slots[0];
  3785. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3786. }
  3787. if (ret || found_key.objectid != key.objectid ||
  3788. found_key.type != key.type) {
  3789. key.offset += right_len;
  3790. break;
  3791. }
  3792. if (found_key.offset != key.offset + right_len) {
  3793. ret = 0;
  3794. goto out;
  3795. }
  3796. key = found_key;
  3797. }
  3798. /*
  3799. * We're now behind the left extent (treat as unchanged) or at the end
  3800. * of the right side (treat as changed).
  3801. */
  3802. if (key.offset >= ekey->offset + left_len)
  3803. ret = 1;
  3804. else
  3805. ret = 0;
  3806. out:
  3807. btrfs_free_path(path);
  3808. return ret;
  3809. }
  3810. static int get_last_extent(struct send_ctx *sctx, u64 offset)
  3811. {
  3812. struct btrfs_path *path;
  3813. struct btrfs_root *root = sctx->send_root;
  3814. struct btrfs_file_extent_item *fi;
  3815. struct btrfs_key key;
  3816. u64 extent_end;
  3817. u8 type;
  3818. int ret;
  3819. path = alloc_path_for_send();
  3820. if (!path)
  3821. return -ENOMEM;
  3822. sctx->cur_inode_last_extent = 0;
  3823. key.objectid = sctx->cur_ino;
  3824. key.type = BTRFS_EXTENT_DATA_KEY;
  3825. key.offset = offset;
  3826. ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
  3827. if (ret < 0)
  3828. goto out;
  3829. ret = 0;
  3830. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  3831. if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
  3832. goto out;
  3833. fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3834. struct btrfs_file_extent_item);
  3835. type = btrfs_file_extent_type(path->nodes[0], fi);
  3836. if (type == BTRFS_FILE_EXTENT_INLINE) {
  3837. u64 size = btrfs_file_extent_inline_len(path->nodes[0],
  3838. path->slots[0], fi);
  3839. extent_end = ALIGN(key.offset + size,
  3840. sctx->send_root->sectorsize);
  3841. } else {
  3842. extent_end = key.offset +
  3843. btrfs_file_extent_num_bytes(path->nodes[0], fi);
  3844. }
  3845. sctx->cur_inode_last_extent = extent_end;
  3846. out:
  3847. btrfs_free_path(path);
  3848. return ret;
  3849. }
  3850. static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
  3851. struct btrfs_key *key)
  3852. {
  3853. struct btrfs_file_extent_item *fi;
  3854. u64 extent_end;
  3855. u8 type;
  3856. int ret = 0;
  3857. if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
  3858. return 0;
  3859. if (sctx->cur_inode_last_extent == (u64)-1) {
  3860. ret = get_last_extent(sctx, key->offset - 1);
  3861. if (ret)
  3862. return ret;
  3863. }
  3864. fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3865. struct btrfs_file_extent_item);
  3866. type = btrfs_file_extent_type(path->nodes[0], fi);
  3867. if (type == BTRFS_FILE_EXTENT_INLINE) {
  3868. u64 size = btrfs_file_extent_inline_len(path->nodes[0],
  3869. path->slots[0], fi);
  3870. extent_end = ALIGN(key->offset + size,
  3871. sctx->send_root->sectorsize);
  3872. } else {
  3873. extent_end = key->offset +
  3874. btrfs_file_extent_num_bytes(path->nodes[0], fi);
  3875. }
  3876. if (path->slots[0] == 0 &&
  3877. sctx->cur_inode_last_extent < key->offset) {
  3878. /*
  3879. * We might have skipped entire leafs that contained only
  3880. * file extent items for our current inode. These leafs have
  3881. * a generation number smaller (older) than the one in the
  3882. * current leaf and the leaf our last extent came from, and
  3883. * are located between these 2 leafs.
  3884. */
  3885. ret = get_last_extent(sctx, key->offset - 1);
  3886. if (ret)
  3887. return ret;
  3888. }
  3889. if (sctx->cur_inode_last_extent < key->offset)
  3890. ret = send_hole(sctx, key->offset);
  3891. sctx->cur_inode_last_extent = extent_end;
  3892. return ret;
  3893. }
  3894. static int process_extent(struct send_ctx *sctx,
  3895. struct btrfs_path *path,
  3896. struct btrfs_key *key)
  3897. {
  3898. struct clone_root *found_clone = NULL;
  3899. int ret = 0;
  3900. if (S_ISLNK(sctx->cur_inode_mode))
  3901. return 0;
  3902. if (sctx->parent_root && !sctx->cur_inode_new) {
  3903. ret = is_extent_unchanged(sctx, path, key);
  3904. if (ret < 0)
  3905. goto out;
  3906. if (ret) {
  3907. ret = 0;
  3908. goto out_hole;
  3909. }
  3910. } else {
  3911. struct btrfs_file_extent_item *ei;
  3912. u8 type;
  3913. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3914. struct btrfs_file_extent_item);
  3915. type = btrfs_file_extent_type(path->nodes[0], ei);
  3916. if (type == BTRFS_FILE_EXTENT_PREALLOC ||
  3917. type == BTRFS_FILE_EXTENT_REG) {
  3918. /*
  3919. * The send spec does not have a prealloc command yet,
  3920. * so just leave a hole for prealloc'ed extents until
  3921. * we have enough commands queued up to justify rev'ing
  3922. * the send spec.
  3923. */
  3924. if (type == BTRFS_FILE_EXTENT_PREALLOC) {
  3925. ret = 0;
  3926. goto out;
  3927. }
  3928. /* Have a hole, just skip it. */
  3929. if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
  3930. ret = 0;
  3931. goto out;
  3932. }
  3933. }
  3934. }
  3935. ret = find_extent_clone(sctx, path, key->objectid, key->offset,
  3936. sctx->cur_inode_size, &found_clone);
  3937. if (ret != -ENOENT && ret < 0)
  3938. goto out;
  3939. ret = send_write_or_clone(sctx, path, key, found_clone);
  3940. if (ret)
  3941. goto out;
  3942. out_hole:
  3943. ret = maybe_send_hole(sctx, path, key);
  3944. out:
  3945. return ret;
  3946. }
  3947. static int process_all_extents(struct send_ctx *sctx)
  3948. {
  3949. int ret;
  3950. struct btrfs_root *root;
  3951. struct btrfs_path *path;
  3952. struct btrfs_key key;
  3953. struct btrfs_key found_key;
  3954. struct extent_buffer *eb;
  3955. int slot;
  3956. root = sctx->send_root;
  3957. path = alloc_path_for_send();
  3958. if (!path)
  3959. return -ENOMEM;
  3960. key.objectid = sctx->cmp_key->objectid;
  3961. key.type = BTRFS_EXTENT_DATA_KEY;
  3962. key.offset = 0;
  3963. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3964. if (ret < 0)
  3965. goto out;
  3966. while (1) {
  3967. eb = path->nodes[0];
  3968. slot = path->slots[0];
  3969. if (slot >= btrfs_header_nritems(eb)) {
  3970. ret = btrfs_next_leaf(root, path);
  3971. if (ret < 0) {
  3972. goto out;
  3973. } else if (ret > 0) {
  3974. ret = 0;
  3975. break;
  3976. }
  3977. continue;
  3978. }
  3979. btrfs_item_key_to_cpu(eb, &found_key, slot);
  3980. if (found_key.objectid != key.objectid ||
  3981. found_key.type != key.type) {
  3982. ret = 0;
  3983. goto out;
  3984. }
  3985. ret = process_extent(sctx, path, &found_key);
  3986. if (ret < 0)
  3987. goto out;
  3988. path->slots[0]++;
  3989. }
  3990. out:
  3991. btrfs_free_path(path);
  3992. return ret;
  3993. }
  3994. static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
  3995. int *pending_move,
  3996. int *refs_processed)
  3997. {
  3998. int ret = 0;
  3999. if (sctx->cur_ino == 0)
  4000. goto out;
  4001. if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
  4002. sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
  4003. goto out;
  4004. if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
  4005. goto out;
  4006. ret = process_recorded_refs(sctx, pending_move);
  4007. if (ret < 0)
  4008. goto out;
  4009. *refs_processed = 1;
  4010. out:
  4011. return ret;
  4012. }
  4013. static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
  4014. {
  4015. int ret = 0;
  4016. u64 left_mode;
  4017. u64 left_uid;
  4018. u64 left_gid;
  4019. u64 right_mode;
  4020. u64 right_uid;
  4021. u64 right_gid;
  4022. int need_chmod = 0;
  4023. int need_chown = 0;
  4024. int pending_move = 0;
  4025. int refs_processed = 0;
  4026. ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
  4027. &refs_processed);
  4028. if (ret < 0)
  4029. goto out;
  4030. /*
  4031. * We have processed the refs and thus need to advance send_progress.
  4032. * Now, calls to get_cur_xxx will take the updated refs of the current
  4033. * inode into account.
  4034. *
  4035. * On the other hand, if our current inode is a directory and couldn't
  4036. * be moved/renamed because its parent was renamed/moved too and it has
  4037. * a higher inode number, we can only move/rename our current inode
  4038. * after we moved/renamed its parent. Therefore in this case operate on
  4039. * the old path (pre move/rename) of our current inode, and the
  4040. * move/rename will be performed later.
  4041. */
  4042. if (refs_processed && !pending_move)
  4043. sctx->send_progress = sctx->cur_ino + 1;
  4044. if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
  4045. goto out;
  4046. if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
  4047. goto out;
  4048. ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
  4049. &left_mode, &left_uid, &left_gid, NULL);
  4050. if (ret < 0)
  4051. goto out;
  4052. if (!sctx->parent_root || sctx->cur_inode_new) {
  4053. need_chown = 1;
  4054. if (!S_ISLNK(sctx->cur_inode_mode))
  4055. need_chmod = 1;
  4056. } else {
  4057. ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
  4058. NULL, NULL, &right_mode, &right_uid,
  4059. &right_gid, NULL);
  4060. if (ret < 0)
  4061. goto out;
  4062. if (left_uid != right_uid || left_gid != right_gid)
  4063. need_chown = 1;
  4064. if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
  4065. need_chmod = 1;
  4066. }
  4067. if (S_ISREG(sctx->cur_inode_mode)) {
  4068. if (need_send_hole(sctx)) {
  4069. if (sctx->cur_inode_last_extent == (u64)-1) {
  4070. ret = get_last_extent(sctx, (u64)-1);
  4071. if (ret)
  4072. goto out;
  4073. }
  4074. if (sctx->cur_inode_last_extent <
  4075. sctx->cur_inode_size) {
  4076. ret = send_hole(sctx, sctx->cur_inode_size);
  4077. if (ret)
  4078. goto out;
  4079. }
  4080. }
  4081. ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  4082. sctx->cur_inode_size);
  4083. if (ret < 0)
  4084. goto out;
  4085. }
  4086. if (need_chown) {
  4087. ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  4088. left_uid, left_gid);
  4089. if (ret < 0)
  4090. goto out;
  4091. }
  4092. if (need_chmod) {
  4093. ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
  4094. left_mode);
  4095. if (ret < 0)
  4096. goto out;
  4097. }
  4098. /*
  4099. * If other directory inodes depended on our current directory
  4100. * inode's move/rename, now do their move/rename operations.
  4101. */
  4102. if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
  4103. ret = apply_children_dir_moves(sctx);
  4104. if (ret)
  4105. goto out;
  4106. }
  4107. /*
  4108. * Need to send that every time, no matter if it actually
  4109. * changed between the two trees as we have done changes to
  4110. * the inode before.
  4111. */
  4112. sctx->send_progress = sctx->cur_ino + 1;
  4113. ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
  4114. if (ret < 0)
  4115. goto out;
  4116. out:
  4117. return ret;
  4118. }
  4119. static int changed_inode(struct send_ctx *sctx,
  4120. enum btrfs_compare_tree_result result)
  4121. {
  4122. int ret = 0;
  4123. struct btrfs_key *key = sctx->cmp_key;
  4124. struct btrfs_inode_item *left_ii = NULL;
  4125. struct btrfs_inode_item *right_ii = NULL;
  4126. u64 left_gen = 0;
  4127. u64 right_gen = 0;
  4128. sctx->cur_ino = key->objectid;
  4129. sctx->cur_inode_new_gen = 0;
  4130. sctx->cur_inode_last_extent = (u64)-1;
  4131. /*
  4132. * Set send_progress to current inode. This will tell all get_cur_xxx
  4133. * functions that the current inode's refs are not updated yet. Later,
  4134. * when process_recorded_refs is finished, it is set to cur_ino + 1.
  4135. */
  4136. sctx->send_progress = sctx->cur_ino;
  4137. if (result == BTRFS_COMPARE_TREE_NEW ||
  4138. result == BTRFS_COMPARE_TREE_CHANGED) {
  4139. left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
  4140. sctx->left_path->slots[0],
  4141. struct btrfs_inode_item);
  4142. left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
  4143. left_ii);
  4144. } else {
  4145. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  4146. sctx->right_path->slots[0],
  4147. struct btrfs_inode_item);
  4148. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  4149. right_ii);
  4150. }
  4151. if (result == BTRFS_COMPARE_TREE_CHANGED) {
  4152. right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
  4153. sctx->right_path->slots[0],
  4154. struct btrfs_inode_item);
  4155. right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
  4156. right_ii);
  4157. /*
  4158. * The cur_ino = root dir case is special here. We can't treat
  4159. * the inode as deleted+reused because it would generate a
  4160. * stream that tries to delete/mkdir the root dir.
  4161. */
  4162. if (left_gen != right_gen &&
  4163. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  4164. sctx->cur_inode_new_gen = 1;
  4165. }
  4166. if (result == BTRFS_COMPARE_TREE_NEW) {
  4167. sctx->cur_inode_gen = left_gen;
  4168. sctx->cur_inode_new = 1;
  4169. sctx->cur_inode_deleted = 0;
  4170. sctx->cur_inode_size = btrfs_inode_size(
  4171. sctx->left_path->nodes[0], left_ii);
  4172. sctx->cur_inode_mode = btrfs_inode_mode(
  4173. sctx->left_path->nodes[0], left_ii);
  4174. if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
  4175. ret = send_create_inode_if_needed(sctx);
  4176. } else if (result == BTRFS_COMPARE_TREE_DELETED) {
  4177. sctx->cur_inode_gen = right_gen;
  4178. sctx->cur_inode_new = 0;
  4179. sctx->cur_inode_deleted = 1;
  4180. sctx->cur_inode_size = btrfs_inode_size(
  4181. sctx->right_path->nodes[0], right_ii);
  4182. sctx->cur_inode_mode = btrfs_inode_mode(
  4183. sctx->right_path->nodes[0], right_ii);
  4184. } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
  4185. /*
  4186. * We need to do some special handling in case the inode was
  4187. * reported as changed with a changed generation number. This
  4188. * means that the original inode was deleted and new inode
  4189. * reused the same inum. So we have to treat the old inode as
  4190. * deleted and the new one as new.
  4191. */
  4192. if (sctx->cur_inode_new_gen) {
  4193. /*
  4194. * First, process the inode as if it was deleted.
  4195. */
  4196. sctx->cur_inode_gen = right_gen;
  4197. sctx->cur_inode_new = 0;
  4198. sctx->cur_inode_deleted = 1;
  4199. sctx->cur_inode_size = btrfs_inode_size(
  4200. sctx->right_path->nodes[0], right_ii);
  4201. sctx->cur_inode_mode = btrfs_inode_mode(
  4202. sctx->right_path->nodes[0], right_ii);
  4203. ret = process_all_refs(sctx,
  4204. BTRFS_COMPARE_TREE_DELETED);
  4205. if (ret < 0)
  4206. goto out;
  4207. /*
  4208. * Now process the inode as if it was new.
  4209. */
  4210. sctx->cur_inode_gen = left_gen;
  4211. sctx->cur_inode_new = 1;
  4212. sctx->cur_inode_deleted = 0;
  4213. sctx->cur_inode_size = btrfs_inode_size(
  4214. sctx->left_path->nodes[0], left_ii);
  4215. sctx->cur_inode_mode = btrfs_inode_mode(
  4216. sctx->left_path->nodes[0], left_ii);
  4217. ret = send_create_inode_if_needed(sctx);
  4218. if (ret < 0)
  4219. goto out;
  4220. ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
  4221. if (ret < 0)
  4222. goto out;
  4223. /*
  4224. * Advance send_progress now as we did not get into
  4225. * process_recorded_refs_if_needed in the new_gen case.
  4226. */
  4227. sctx->send_progress = sctx->cur_ino + 1;
  4228. /*
  4229. * Now process all extents and xattrs of the inode as if
  4230. * they were all new.
  4231. */
  4232. ret = process_all_extents(sctx);
  4233. if (ret < 0)
  4234. goto out;
  4235. ret = process_all_new_xattrs(sctx);
  4236. if (ret < 0)
  4237. goto out;
  4238. } else {
  4239. sctx->cur_inode_gen = left_gen;
  4240. sctx->cur_inode_new = 0;
  4241. sctx->cur_inode_new_gen = 0;
  4242. sctx->cur_inode_deleted = 0;
  4243. sctx->cur_inode_size = btrfs_inode_size(
  4244. sctx->left_path->nodes[0], left_ii);
  4245. sctx->cur_inode_mode = btrfs_inode_mode(
  4246. sctx->left_path->nodes[0], left_ii);
  4247. }
  4248. }
  4249. out:
  4250. return ret;
  4251. }
  4252. /*
  4253. * We have to process new refs before deleted refs, but compare_trees gives us
  4254. * the new and deleted refs mixed. To fix this, we record the new/deleted refs
  4255. * first and later process them in process_recorded_refs.
  4256. * For the cur_inode_new_gen case, we skip recording completely because
  4257. * changed_inode did already initiate processing of refs. The reason for this is
  4258. * that in this case, compare_tree actually compares the refs of 2 different
  4259. * inodes. To fix this, process_all_refs is used in changed_inode to handle all
  4260. * refs of the right tree as deleted and all refs of the left tree as new.
  4261. */
  4262. static int changed_ref(struct send_ctx *sctx,
  4263. enum btrfs_compare_tree_result result)
  4264. {
  4265. int ret = 0;
  4266. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  4267. if (!sctx->cur_inode_new_gen &&
  4268. sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
  4269. if (result == BTRFS_COMPARE_TREE_NEW)
  4270. ret = record_new_ref(sctx);
  4271. else if (result == BTRFS_COMPARE_TREE_DELETED)
  4272. ret = record_deleted_ref(sctx);
  4273. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  4274. ret = record_changed_ref(sctx);
  4275. }
  4276. return ret;
  4277. }
  4278. /*
  4279. * Process new/deleted/changed xattrs. We skip processing in the
  4280. * cur_inode_new_gen case because changed_inode did already initiate processing
  4281. * of xattrs. The reason is the same as in changed_ref
  4282. */
  4283. static int changed_xattr(struct send_ctx *sctx,
  4284. enum btrfs_compare_tree_result result)
  4285. {
  4286. int ret = 0;
  4287. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  4288. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  4289. if (result == BTRFS_COMPARE_TREE_NEW)
  4290. ret = process_new_xattr(sctx);
  4291. else if (result == BTRFS_COMPARE_TREE_DELETED)
  4292. ret = process_deleted_xattr(sctx);
  4293. else if (result == BTRFS_COMPARE_TREE_CHANGED)
  4294. ret = process_changed_xattr(sctx);
  4295. }
  4296. return ret;
  4297. }
  4298. /*
  4299. * Process new/deleted/changed extents. We skip processing in the
  4300. * cur_inode_new_gen case because changed_inode did already initiate processing
  4301. * of extents. The reason is the same as in changed_ref
  4302. */
  4303. static int changed_extent(struct send_ctx *sctx,
  4304. enum btrfs_compare_tree_result result)
  4305. {
  4306. int ret = 0;
  4307. BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
  4308. if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
  4309. if (result != BTRFS_COMPARE_TREE_DELETED)
  4310. ret = process_extent(sctx, sctx->left_path,
  4311. sctx->cmp_key);
  4312. }
  4313. return ret;
  4314. }
  4315. static int dir_changed(struct send_ctx *sctx, u64 dir)
  4316. {
  4317. u64 orig_gen, new_gen;
  4318. int ret;
  4319. ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
  4320. NULL, NULL);
  4321. if (ret)
  4322. return ret;
  4323. ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
  4324. NULL, NULL, NULL);
  4325. if (ret)
  4326. return ret;
  4327. return (orig_gen != new_gen) ? 1 : 0;
  4328. }
  4329. static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
  4330. struct btrfs_key *key)
  4331. {
  4332. struct btrfs_inode_extref *extref;
  4333. struct extent_buffer *leaf;
  4334. u64 dirid = 0, last_dirid = 0;
  4335. unsigned long ptr;
  4336. u32 item_size;
  4337. u32 cur_offset = 0;
  4338. int ref_name_len;
  4339. int ret = 0;
  4340. /* Easy case, just check this one dirid */
  4341. if (key->type == BTRFS_INODE_REF_KEY) {
  4342. dirid = key->offset;
  4343. ret = dir_changed(sctx, dirid);
  4344. goto out;
  4345. }
  4346. leaf = path->nodes[0];
  4347. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  4348. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  4349. while (cur_offset < item_size) {
  4350. extref = (struct btrfs_inode_extref *)(ptr +
  4351. cur_offset);
  4352. dirid = btrfs_inode_extref_parent(leaf, extref);
  4353. ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
  4354. cur_offset += ref_name_len + sizeof(*extref);
  4355. if (dirid == last_dirid)
  4356. continue;
  4357. ret = dir_changed(sctx, dirid);
  4358. if (ret)
  4359. break;
  4360. last_dirid = dirid;
  4361. }
  4362. out:
  4363. return ret;
  4364. }
  4365. /*
  4366. * Updates compare related fields in sctx and simply forwards to the actual
  4367. * changed_xxx functions.
  4368. */
  4369. static int changed_cb(struct btrfs_root *left_root,
  4370. struct btrfs_root *right_root,
  4371. struct btrfs_path *left_path,
  4372. struct btrfs_path *right_path,
  4373. struct btrfs_key *key,
  4374. enum btrfs_compare_tree_result result,
  4375. void *ctx)
  4376. {
  4377. int ret = 0;
  4378. struct send_ctx *sctx = ctx;
  4379. if (result == BTRFS_COMPARE_TREE_SAME) {
  4380. if (key->type == BTRFS_INODE_REF_KEY ||
  4381. key->type == BTRFS_INODE_EXTREF_KEY) {
  4382. ret = compare_refs(sctx, left_path, key);
  4383. if (!ret)
  4384. return 0;
  4385. if (ret < 0)
  4386. return ret;
  4387. } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
  4388. return maybe_send_hole(sctx, left_path, key);
  4389. } else {
  4390. return 0;
  4391. }
  4392. result = BTRFS_COMPARE_TREE_CHANGED;
  4393. ret = 0;
  4394. }
  4395. sctx->left_path = left_path;
  4396. sctx->right_path = right_path;
  4397. sctx->cmp_key = key;
  4398. ret = finish_inode_if_needed(sctx, 0);
  4399. if (ret < 0)
  4400. goto out;
  4401. /* Ignore non-FS objects */
  4402. if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
  4403. key->objectid == BTRFS_FREE_SPACE_OBJECTID)
  4404. goto out;
  4405. if (key->type == BTRFS_INODE_ITEM_KEY)
  4406. ret = changed_inode(sctx, result);
  4407. else if (key->type == BTRFS_INODE_REF_KEY ||
  4408. key->type == BTRFS_INODE_EXTREF_KEY)
  4409. ret = changed_ref(sctx, result);
  4410. else if (key->type == BTRFS_XATTR_ITEM_KEY)
  4411. ret = changed_xattr(sctx, result);
  4412. else if (key->type == BTRFS_EXTENT_DATA_KEY)
  4413. ret = changed_extent(sctx, result);
  4414. out:
  4415. return ret;
  4416. }
  4417. static int full_send_tree(struct send_ctx *sctx)
  4418. {
  4419. int ret;
  4420. struct btrfs_root *send_root = sctx->send_root;
  4421. struct btrfs_key key;
  4422. struct btrfs_key found_key;
  4423. struct btrfs_path *path;
  4424. struct extent_buffer *eb;
  4425. int slot;
  4426. u64 start_ctransid;
  4427. u64 ctransid;
  4428. path = alloc_path_for_send();
  4429. if (!path)
  4430. return -ENOMEM;
  4431. spin_lock(&send_root->root_item_lock);
  4432. start_ctransid = btrfs_root_ctransid(&send_root->root_item);
  4433. spin_unlock(&send_root->root_item_lock);
  4434. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  4435. key.type = BTRFS_INODE_ITEM_KEY;
  4436. key.offset = 0;
  4437. /*
  4438. * Make sure the tree has not changed after re-joining. We detect this
  4439. * by comparing start_ctransid and ctransid. They should always match.
  4440. */
  4441. spin_lock(&send_root->root_item_lock);
  4442. ctransid = btrfs_root_ctransid(&send_root->root_item);
  4443. spin_unlock(&send_root->root_item_lock);
  4444. if (ctransid != start_ctransid) {
  4445. WARN(1, KERN_WARNING "BTRFS: the root that you're trying to "
  4446. "send was modified in between. This is "
  4447. "probably a bug.\n");
  4448. ret = -EIO;
  4449. goto out;
  4450. }
  4451. ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
  4452. if (ret < 0)
  4453. goto out;
  4454. if (ret)
  4455. goto out_finish;
  4456. while (1) {
  4457. eb = path->nodes[0];
  4458. slot = path->slots[0];
  4459. btrfs_item_key_to_cpu(eb, &found_key, slot);
  4460. ret = changed_cb(send_root, NULL, path, NULL,
  4461. &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
  4462. if (ret < 0)
  4463. goto out;
  4464. key.objectid = found_key.objectid;
  4465. key.type = found_key.type;
  4466. key.offset = found_key.offset + 1;
  4467. ret = btrfs_next_item(send_root, path);
  4468. if (ret < 0)
  4469. goto out;
  4470. if (ret) {
  4471. ret = 0;
  4472. break;
  4473. }
  4474. }
  4475. out_finish:
  4476. ret = finish_inode_if_needed(sctx, 1);
  4477. out:
  4478. btrfs_free_path(path);
  4479. return ret;
  4480. }
  4481. static int send_subvol(struct send_ctx *sctx)
  4482. {
  4483. int ret;
  4484. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
  4485. ret = send_header(sctx);
  4486. if (ret < 0)
  4487. goto out;
  4488. }
  4489. ret = send_subvol_begin(sctx);
  4490. if (ret < 0)
  4491. goto out;
  4492. if (sctx->parent_root) {
  4493. ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
  4494. changed_cb, sctx);
  4495. if (ret < 0)
  4496. goto out;
  4497. ret = finish_inode_if_needed(sctx, 1);
  4498. if (ret < 0)
  4499. goto out;
  4500. } else {
  4501. ret = full_send_tree(sctx);
  4502. if (ret < 0)
  4503. goto out;
  4504. }
  4505. out:
  4506. free_recorded_refs(sctx);
  4507. return ret;
  4508. }
  4509. static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
  4510. {
  4511. spin_lock(&root->root_item_lock);
  4512. root->send_in_progress--;
  4513. /*
  4514. * Not much left to do, we don't know why it's unbalanced and
  4515. * can't blindly reset it to 0.
  4516. */
  4517. if (root->send_in_progress < 0)
  4518. btrfs_err(root->fs_info,
  4519. "send_in_progres unbalanced %d root %llu\n",
  4520. root->send_in_progress, root->root_key.objectid);
  4521. spin_unlock(&root->root_item_lock);
  4522. }
  4523. long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
  4524. {
  4525. int ret = 0;
  4526. struct btrfs_root *send_root;
  4527. struct btrfs_root *clone_root;
  4528. struct btrfs_fs_info *fs_info;
  4529. struct btrfs_ioctl_send_args *arg = NULL;
  4530. struct btrfs_key key;
  4531. struct send_ctx *sctx = NULL;
  4532. u32 i;
  4533. u64 *clone_sources_tmp = NULL;
  4534. int clone_sources_to_rollback = 0;
  4535. int sort_clone_roots = 0;
  4536. int index;
  4537. if (!capable(CAP_SYS_ADMIN))
  4538. return -EPERM;
  4539. send_root = BTRFS_I(file_inode(mnt_file))->root;
  4540. fs_info = send_root->fs_info;
  4541. /*
  4542. * The subvolume must remain read-only during send, protect against
  4543. * making it RW.
  4544. */
  4545. spin_lock(&send_root->root_item_lock);
  4546. send_root->send_in_progress++;
  4547. spin_unlock(&send_root->root_item_lock);
  4548. /*
  4549. * This is done when we lookup the root, it should already be complete
  4550. * by the time we get here.
  4551. */
  4552. WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
  4553. /*
  4554. * Userspace tools do the checks and warn the user if it's
  4555. * not RO.
  4556. */
  4557. if (!btrfs_root_readonly(send_root)) {
  4558. ret = -EPERM;
  4559. goto out;
  4560. }
  4561. arg = memdup_user(arg_, sizeof(*arg));
  4562. if (IS_ERR(arg)) {
  4563. ret = PTR_ERR(arg);
  4564. arg = NULL;
  4565. goto out;
  4566. }
  4567. if (!access_ok(VERIFY_READ, arg->clone_sources,
  4568. sizeof(*arg->clone_sources) *
  4569. arg->clone_sources_count)) {
  4570. ret = -EFAULT;
  4571. goto out;
  4572. }
  4573. if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
  4574. ret = -EINVAL;
  4575. goto out;
  4576. }
  4577. sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
  4578. if (!sctx) {
  4579. ret = -ENOMEM;
  4580. goto out;
  4581. }
  4582. INIT_LIST_HEAD(&sctx->new_refs);
  4583. INIT_LIST_HEAD(&sctx->deleted_refs);
  4584. INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
  4585. INIT_LIST_HEAD(&sctx->name_cache_list);
  4586. sctx->flags = arg->flags;
  4587. sctx->send_filp = fget(arg->send_fd);
  4588. if (!sctx->send_filp) {
  4589. ret = -EBADF;
  4590. goto out;
  4591. }
  4592. sctx->send_root = send_root;
  4593. sctx->clone_roots_cnt = arg->clone_sources_count;
  4594. sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
  4595. sctx->send_buf = vmalloc(sctx->send_max_size);
  4596. if (!sctx->send_buf) {
  4597. ret = -ENOMEM;
  4598. goto out;
  4599. }
  4600. sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
  4601. if (!sctx->read_buf) {
  4602. ret = -ENOMEM;
  4603. goto out;
  4604. }
  4605. sctx->pending_dir_moves = RB_ROOT;
  4606. sctx->waiting_dir_moves = RB_ROOT;
  4607. sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
  4608. (arg->clone_sources_count + 1));
  4609. if (!sctx->clone_roots) {
  4610. ret = -ENOMEM;
  4611. goto out;
  4612. }
  4613. if (arg->clone_sources_count) {
  4614. clone_sources_tmp = vmalloc(arg->clone_sources_count *
  4615. sizeof(*arg->clone_sources));
  4616. if (!clone_sources_tmp) {
  4617. ret = -ENOMEM;
  4618. goto out;
  4619. }
  4620. ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
  4621. arg->clone_sources_count *
  4622. sizeof(*arg->clone_sources));
  4623. if (ret) {
  4624. ret = -EFAULT;
  4625. goto out;
  4626. }
  4627. for (i = 0; i < arg->clone_sources_count; i++) {
  4628. key.objectid = clone_sources_tmp[i];
  4629. key.type = BTRFS_ROOT_ITEM_KEY;
  4630. key.offset = (u64)-1;
  4631. index = srcu_read_lock(&fs_info->subvol_srcu);
  4632. clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
  4633. if (IS_ERR(clone_root)) {
  4634. srcu_read_unlock(&fs_info->subvol_srcu, index);
  4635. ret = PTR_ERR(clone_root);
  4636. goto out;
  4637. }
  4638. clone_sources_to_rollback = i + 1;
  4639. spin_lock(&clone_root->root_item_lock);
  4640. clone_root->send_in_progress++;
  4641. if (!btrfs_root_readonly(clone_root)) {
  4642. spin_unlock(&clone_root->root_item_lock);
  4643. srcu_read_unlock(&fs_info->subvol_srcu, index);
  4644. ret = -EPERM;
  4645. goto out;
  4646. }
  4647. spin_unlock(&clone_root->root_item_lock);
  4648. srcu_read_unlock(&fs_info->subvol_srcu, index);
  4649. sctx->clone_roots[i].root = clone_root;
  4650. }
  4651. vfree(clone_sources_tmp);
  4652. clone_sources_tmp = NULL;
  4653. }
  4654. if (arg->parent_root) {
  4655. key.objectid = arg->parent_root;
  4656. key.type = BTRFS_ROOT_ITEM_KEY;
  4657. key.offset = (u64)-1;
  4658. index = srcu_read_lock(&fs_info->subvol_srcu);
  4659. sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
  4660. if (IS_ERR(sctx->parent_root)) {
  4661. srcu_read_unlock(&fs_info->subvol_srcu, index);
  4662. ret = PTR_ERR(sctx->parent_root);
  4663. goto out;
  4664. }
  4665. spin_lock(&sctx->parent_root->root_item_lock);
  4666. sctx->parent_root->send_in_progress++;
  4667. if (!btrfs_root_readonly(sctx->parent_root)) {
  4668. spin_unlock(&sctx->parent_root->root_item_lock);
  4669. srcu_read_unlock(&fs_info->subvol_srcu, index);
  4670. ret = -EPERM;
  4671. goto out;
  4672. }
  4673. spin_unlock(&sctx->parent_root->root_item_lock);
  4674. srcu_read_unlock(&fs_info->subvol_srcu, index);
  4675. }
  4676. /*
  4677. * Clones from send_root are allowed, but only if the clone source
  4678. * is behind the current send position. This is checked while searching
  4679. * for possible clone sources.
  4680. */
  4681. sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
  4682. /* We do a bsearch later */
  4683. sort(sctx->clone_roots, sctx->clone_roots_cnt,
  4684. sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
  4685. NULL);
  4686. sort_clone_roots = 1;
  4687. ret = send_subvol(sctx);
  4688. if (ret < 0)
  4689. goto out;
  4690. if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
  4691. ret = begin_cmd(sctx, BTRFS_SEND_C_END);
  4692. if (ret < 0)
  4693. goto out;
  4694. ret = send_cmd(sctx);
  4695. if (ret < 0)
  4696. goto out;
  4697. }
  4698. out:
  4699. WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
  4700. while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
  4701. struct rb_node *n;
  4702. struct pending_dir_move *pm;
  4703. n = rb_first(&sctx->pending_dir_moves);
  4704. pm = rb_entry(n, struct pending_dir_move, node);
  4705. while (!list_empty(&pm->list)) {
  4706. struct pending_dir_move *pm2;
  4707. pm2 = list_first_entry(&pm->list,
  4708. struct pending_dir_move, list);
  4709. free_pending_move(sctx, pm2);
  4710. }
  4711. free_pending_move(sctx, pm);
  4712. }
  4713. WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
  4714. while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
  4715. struct rb_node *n;
  4716. struct waiting_dir_move *dm;
  4717. n = rb_first(&sctx->waiting_dir_moves);
  4718. dm = rb_entry(n, struct waiting_dir_move, node);
  4719. rb_erase(&dm->node, &sctx->waiting_dir_moves);
  4720. kfree(dm);
  4721. }
  4722. if (sort_clone_roots) {
  4723. for (i = 0; i < sctx->clone_roots_cnt; i++)
  4724. btrfs_root_dec_send_in_progress(
  4725. sctx->clone_roots[i].root);
  4726. } else {
  4727. for (i = 0; sctx && i < clone_sources_to_rollback; i++)
  4728. btrfs_root_dec_send_in_progress(
  4729. sctx->clone_roots[i].root);
  4730. btrfs_root_dec_send_in_progress(send_root);
  4731. }
  4732. if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
  4733. btrfs_root_dec_send_in_progress(sctx->parent_root);
  4734. kfree(arg);
  4735. vfree(clone_sources_tmp);
  4736. if (sctx) {
  4737. if (sctx->send_filp)
  4738. fput(sctx->send_filp);
  4739. vfree(sctx->clone_roots);
  4740. vfree(sctx->send_buf);
  4741. vfree(sctx->read_buf);
  4742. name_cache_free(sctx);
  4743. kfree(sctx);
  4744. }
  4745. return ret;
  4746. }