send.c 132 KB

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