send.c 102 KB

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