send.c 126 KB

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