send.c 132 KB

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