send.c 105 KB

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