send.c 99 KB

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