send.c 125 KB

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