send.c 96 KB

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