send.c 138 KB

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