send.c 133 KB

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