send.c 99 KB

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