send.c 103 KB

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