send.c 132 KB

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