send.c 106 KB

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