send.c 131 KB

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