send.c 107 KB

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