send.c 96 KB

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