send.c 133 KB

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