send.c 141 KB

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