ioctl.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846
  1. /*
  2. * Copyright (C) 2007 Oracle. 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/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/fsnotify.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/highmem.h>
  26. #include <linux/time.h>
  27. #include <linux/init.h>
  28. #include <linux/string.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mount.h>
  31. #include <linux/mpage.h>
  32. #include <linux/namei.h>
  33. #include <linux/swap.h>
  34. #include <linux/writeback.h>
  35. #include <linux/statfs.h>
  36. #include <linux/compat.h>
  37. #include <linux/bit_spinlock.h>
  38. #include <linux/security.h>
  39. #include <linux/xattr.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/slab.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/uuid.h>
  44. #include <linux/btrfs.h>
  45. #include <linux/uaccess.h>
  46. #include "ctree.h"
  47. #include "disk-io.h"
  48. #include "transaction.h"
  49. #include "btrfs_inode.h"
  50. #include "print-tree.h"
  51. #include "volumes.h"
  52. #include "locking.h"
  53. #include "inode-map.h"
  54. #include "backref.h"
  55. #include "rcu-string.h"
  56. #include "send.h"
  57. #include "dev-replace.h"
  58. #include "props.h"
  59. #include "sysfs.h"
  60. static int btrfs_clone(struct inode *src, struct inode *inode,
  61. u64 off, u64 olen, u64 olen_aligned, u64 destoff);
  62. /* Mask out flags that are inappropriate for the given type of inode. */
  63. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  64. {
  65. if (S_ISDIR(mode))
  66. return flags;
  67. else if (S_ISREG(mode))
  68. return flags & ~FS_DIRSYNC_FL;
  69. else
  70. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  71. }
  72. /*
  73. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  74. */
  75. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  76. {
  77. unsigned int iflags = 0;
  78. if (flags & BTRFS_INODE_SYNC)
  79. iflags |= FS_SYNC_FL;
  80. if (flags & BTRFS_INODE_IMMUTABLE)
  81. iflags |= FS_IMMUTABLE_FL;
  82. if (flags & BTRFS_INODE_APPEND)
  83. iflags |= FS_APPEND_FL;
  84. if (flags & BTRFS_INODE_NODUMP)
  85. iflags |= FS_NODUMP_FL;
  86. if (flags & BTRFS_INODE_NOATIME)
  87. iflags |= FS_NOATIME_FL;
  88. if (flags & BTRFS_INODE_DIRSYNC)
  89. iflags |= FS_DIRSYNC_FL;
  90. if (flags & BTRFS_INODE_NODATACOW)
  91. iflags |= FS_NOCOW_FL;
  92. if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
  93. iflags |= FS_COMPR_FL;
  94. else if (flags & BTRFS_INODE_NOCOMPRESS)
  95. iflags |= FS_NOCOMP_FL;
  96. return iflags;
  97. }
  98. /*
  99. * Update inode->i_flags based on the btrfs internal flags.
  100. */
  101. void btrfs_update_iflags(struct inode *inode)
  102. {
  103. struct btrfs_inode *ip = BTRFS_I(inode);
  104. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  105. if (ip->flags & BTRFS_INODE_SYNC)
  106. inode->i_flags |= S_SYNC;
  107. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  108. inode->i_flags |= S_IMMUTABLE;
  109. if (ip->flags & BTRFS_INODE_APPEND)
  110. inode->i_flags |= S_APPEND;
  111. if (ip->flags & BTRFS_INODE_NOATIME)
  112. inode->i_flags |= S_NOATIME;
  113. if (ip->flags & BTRFS_INODE_DIRSYNC)
  114. inode->i_flags |= S_DIRSYNC;
  115. }
  116. /*
  117. * Inherit flags from the parent inode.
  118. *
  119. * Currently only the compression flags and the cow flags are inherited.
  120. */
  121. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  122. {
  123. unsigned int flags;
  124. if (!dir)
  125. return;
  126. flags = BTRFS_I(dir)->flags;
  127. if (flags & BTRFS_INODE_NOCOMPRESS) {
  128. BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
  129. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  130. } else if (flags & BTRFS_INODE_COMPRESS) {
  131. BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
  132. BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
  133. }
  134. if (flags & BTRFS_INODE_NODATACOW) {
  135. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  136. if (S_ISREG(inode->i_mode))
  137. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  138. }
  139. btrfs_update_iflags(inode);
  140. }
  141. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  142. {
  143. struct btrfs_inode *ip = BTRFS_I(file_inode(file));
  144. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  145. if (copy_to_user(arg, &flags, sizeof(flags)))
  146. return -EFAULT;
  147. return 0;
  148. }
  149. static int check_flags(unsigned int flags)
  150. {
  151. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  152. FS_NOATIME_FL | FS_NODUMP_FL | \
  153. FS_SYNC_FL | FS_DIRSYNC_FL | \
  154. FS_NOCOMP_FL | FS_COMPR_FL |
  155. FS_NOCOW_FL))
  156. return -EOPNOTSUPP;
  157. if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
  158. return -EINVAL;
  159. return 0;
  160. }
  161. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  162. {
  163. struct inode *inode = file_inode(file);
  164. struct btrfs_inode *ip = BTRFS_I(inode);
  165. struct btrfs_root *root = ip->root;
  166. struct btrfs_trans_handle *trans;
  167. unsigned int flags, oldflags;
  168. int ret;
  169. u64 ip_oldflags;
  170. unsigned int i_oldflags;
  171. umode_t mode;
  172. if (!inode_owner_or_capable(inode))
  173. return -EPERM;
  174. if (btrfs_root_readonly(root))
  175. return -EROFS;
  176. if (copy_from_user(&flags, arg, sizeof(flags)))
  177. return -EFAULT;
  178. ret = check_flags(flags);
  179. if (ret)
  180. return ret;
  181. ret = mnt_want_write_file(file);
  182. if (ret)
  183. return ret;
  184. mutex_lock(&inode->i_mutex);
  185. ip_oldflags = ip->flags;
  186. i_oldflags = inode->i_flags;
  187. mode = inode->i_mode;
  188. flags = btrfs_mask_flags(inode->i_mode, flags);
  189. oldflags = btrfs_flags_to_ioctl(ip->flags);
  190. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  191. if (!capable(CAP_LINUX_IMMUTABLE)) {
  192. ret = -EPERM;
  193. goto out_unlock;
  194. }
  195. }
  196. if (flags & FS_SYNC_FL)
  197. ip->flags |= BTRFS_INODE_SYNC;
  198. else
  199. ip->flags &= ~BTRFS_INODE_SYNC;
  200. if (flags & FS_IMMUTABLE_FL)
  201. ip->flags |= BTRFS_INODE_IMMUTABLE;
  202. else
  203. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  204. if (flags & FS_APPEND_FL)
  205. ip->flags |= BTRFS_INODE_APPEND;
  206. else
  207. ip->flags &= ~BTRFS_INODE_APPEND;
  208. if (flags & FS_NODUMP_FL)
  209. ip->flags |= BTRFS_INODE_NODUMP;
  210. else
  211. ip->flags &= ~BTRFS_INODE_NODUMP;
  212. if (flags & FS_NOATIME_FL)
  213. ip->flags |= BTRFS_INODE_NOATIME;
  214. else
  215. ip->flags &= ~BTRFS_INODE_NOATIME;
  216. if (flags & FS_DIRSYNC_FL)
  217. ip->flags |= BTRFS_INODE_DIRSYNC;
  218. else
  219. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  220. if (flags & FS_NOCOW_FL) {
  221. if (S_ISREG(mode)) {
  222. /*
  223. * It's safe to turn csums off here, no extents exist.
  224. * Otherwise we want the flag to reflect the real COW
  225. * status of the file and will not set it.
  226. */
  227. if (inode->i_size == 0)
  228. ip->flags |= BTRFS_INODE_NODATACOW
  229. | BTRFS_INODE_NODATASUM;
  230. } else {
  231. ip->flags |= BTRFS_INODE_NODATACOW;
  232. }
  233. } else {
  234. /*
  235. * Revert back under same assuptions as above
  236. */
  237. if (S_ISREG(mode)) {
  238. if (inode->i_size == 0)
  239. ip->flags &= ~(BTRFS_INODE_NODATACOW
  240. | BTRFS_INODE_NODATASUM);
  241. } else {
  242. ip->flags &= ~BTRFS_INODE_NODATACOW;
  243. }
  244. }
  245. /*
  246. * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
  247. * flag may be changed automatically if compression code won't make
  248. * things smaller.
  249. */
  250. if (flags & FS_NOCOMP_FL) {
  251. ip->flags &= ~BTRFS_INODE_COMPRESS;
  252. ip->flags |= BTRFS_INODE_NOCOMPRESS;
  253. ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
  254. if (ret && ret != -ENODATA)
  255. goto out_drop;
  256. } else if (flags & FS_COMPR_FL) {
  257. const char *comp;
  258. ip->flags |= BTRFS_INODE_COMPRESS;
  259. ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
  260. if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
  261. comp = "lzo";
  262. else
  263. comp = "zlib";
  264. ret = btrfs_set_prop(inode, "btrfs.compression",
  265. comp, strlen(comp), 0);
  266. if (ret)
  267. goto out_drop;
  268. } else {
  269. ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
  270. }
  271. trans = btrfs_start_transaction(root, 1);
  272. if (IS_ERR(trans)) {
  273. ret = PTR_ERR(trans);
  274. goto out_drop;
  275. }
  276. btrfs_update_iflags(inode);
  277. inode_inc_iversion(inode);
  278. inode->i_ctime = CURRENT_TIME;
  279. ret = btrfs_update_inode(trans, root, inode);
  280. btrfs_end_transaction(trans, root);
  281. out_drop:
  282. if (ret) {
  283. ip->flags = ip_oldflags;
  284. inode->i_flags = i_oldflags;
  285. }
  286. out_unlock:
  287. mutex_unlock(&inode->i_mutex);
  288. mnt_drop_write_file(file);
  289. return ret;
  290. }
  291. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  292. {
  293. struct inode *inode = file_inode(file);
  294. return put_user(inode->i_generation, arg);
  295. }
  296. static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
  297. {
  298. struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
  299. struct btrfs_device *device;
  300. struct request_queue *q;
  301. struct fstrim_range range;
  302. u64 minlen = ULLONG_MAX;
  303. u64 num_devices = 0;
  304. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  305. int ret;
  306. if (!capable(CAP_SYS_ADMIN))
  307. return -EPERM;
  308. rcu_read_lock();
  309. list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
  310. dev_list) {
  311. if (!device->bdev)
  312. continue;
  313. q = bdev_get_queue(device->bdev);
  314. if (blk_queue_discard(q)) {
  315. num_devices++;
  316. minlen = min((u64)q->limits.discard_granularity,
  317. minlen);
  318. }
  319. }
  320. rcu_read_unlock();
  321. if (!num_devices)
  322. return -EOPNOTSUPP;
  323. if (copy_from_user(&range, arg, sizeof(range)))
  324. return -EFAULT;
  325. if (range.start > total_bytes ||
  326. range.len < fs_info->sb->s_blocksize)
  327. return -EINVAL;
  328. range.len = min(range.len, total_bytes - range.start);
  329. range.minlen = max(range.minlen, minlen);
  330. ret = btrfs_trim_fs(fs_info->tree_root, &range);
  331. if (ret < 0)
  332. return ret;
  333. if (copy_to_user(arg, &range, sizeof(range)))
  334. return -EFAULT;
  335. return 0;
  336. }
  337. int btrfs_is_empty_uuid(u8 *uuid)
  338. {
  339. int i;
  340. for (i = 0; i < BTRFS_UUID_SIZE; i++) {
  341. if (uuid[i])
  342. return 0;
  343. }
  344. return 1;
  345. }
  346. static noinline int create_subvol(struct inode *dir,
  347. struct dentry *dentry,
  348. char *name, int namelen,
  349. u64 *async_transid,
  350. struct btrfs_qgroup_inherit *inherit)
  351. {
  352. struct btrfs_trans_handle *trans;
  353. struct btrfs_key key;
  354. struct btrfs_root_item root_item;
  355. struct btrfs_inode_item *inode_item;
  356. struct extent_buffer *leaf;
  357. struct btrfs_root *root = BTRFS_I(dir)->root;
  358. struct btrfs_root *new_root;
  359. struct btrfs_block_rsv block_rsv;
  360. struct timespec cur_time = CURRENT_TIME;
  361. struct inode *inode;
  362. int ret;
  363. int err;
  364. u64 objectid;
  365. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  366. u64 index = 0;
  367. u64 qgroup_reserved;
  368. uuid_le new_uuid;
  369. ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
  370. if (ret)
  371. return ret;
  372. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  373. /*
  374. * The same as the snapshot creation, please see the comment
  375. * of create_snapshot().
  376. */
  377. ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
  378. 8, &qgroup_reserved, false);
  379. if (ret)
  380. return ret;
  381. trans = btrfs_start_transaction(root, 0);
  382. if (IS_ERR(trans)) {
  383. ret = PTR_ERR(trans);
  384. btrfs_subvolume_release_metadata(root, &block_rsv,
  385. qgroup_reserved);
  386. return ret;
  387. }
  388. trans->block_rsv = &block_rsv;
  389. trans->bytes_reserved = block_rsv.size;
  390. ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
  391. if (ret)
  392. goto fail;
  393. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  394. 0, objectid, NULL, 0, 0, 0);
  395. if (IS_ERR(leaf)) {
  396. ret = PTR_ERR(leaf);
  397. goto fail;
  398. }
  399. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  400. btrfs_set_header_bytenr(leaf, leaf->start);
  401. btrfs_set_header_generation(leaf, trans->transid);
  402. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  403. btrfs_set_header_owner(leaf, objectid);
  404. write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
  405. BTRFS_FSID_SIZE);
  406. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  407. btrfs_header_chunk_tree_uuid(leaf),
  408. BTRFS_UUID_SIZE);
  409. btrfs_mark_buffer_dirty(leaf);
  410. memset(&root_item, 0, sizeof(root_item));
  411. inode_item = &root_item.inode;
  412. btrfs_set_stack_inode_generation(inode_item, 1);
  413. btrfs_set_stack_inode_size(inode_item, 3);
  414. btrfs_set_stack_inode_nlink(inode_item, 1);
  415. btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
  416. btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
  417. btrfs_set_root_flags(&root_item, 0);
  418. btrfs_set_root_limit(&root_item, 0);
  419. btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
  420. btrfs_set_root_bytenr(&root_item, leaf->start);
  421. btrfs_set_root_generation(&root_item, trans->transid);
  422. btrfs_set_root_level(&root_item, 0);
  423. btrfs_set_root_refs(&root_item, 1);
  424. btrfs_set_root_used(&root_item, leaf->len);
  425. btrfs_set_root_last_snapshot(&root_item, 0);
  426. btrfs_set_root_generation_v2(&root_item,
  427. btrfs_root_generation(&root_item));
  428. uuid_le_gen(&new_uuid);
  429. memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
  430. btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
  431. btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
  432. root_item.ctime = root_item.otime;
  433. btrfs_set_root_ctransid(&root_item, trans->transid);
  434. btrfs_set_root_otransid(&root_item, trans->transid);
  435. btrfs_tree_unlock(leaf);
  436. free_extent_buffer(leaf);
  437. leaf = NULL;
  438. btrfs_set_root_dirid(&root_item, new_dirid);
  439. key.objectid = objectid;
  440. key.offset = 0;
  441. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  442. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  443. &root_item);
  444. if (ret)
  445. goto fail;
  446. key.offset = (u64)-1;
  447. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  448. if (IS_ERR(new_root)) {
  449. btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
  450. ret = PTR_ERR(new_root);
  451. goto fail;
  452. }
  453. btrfs_record_root_in_trans(trans, new_root);
  454. ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
  455. if (ret) {
  456. /* We potentially lose an unused inode item here */
  457. btrfs_abort_transaction(trans, root, ret);
  458. goto fail;
  459. }
  460. /*
  461. * insert the directory item
  462. */
  463. ret = btrfs_set_inode_index(dir, &index);
  464. if (ret) {
  465. btrfs_abort_transaction(trans, root, ret);
  466. goto fail;
  467. }
  468. ret = btrfs_insert_dir_item(trans, root,
  469. name, namelen, dir, &key,
  470. BTRFS_FT_DIR, index);
  471. if (ret) {
  472. btrfs_abort_transaction(trans, root, ret);
  473. goto fail;
  474. }
  475. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  476. ret = btrfs_update_inode(trans, root, dir);
  477. BUG_ON(ret);
  478. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  479. objectid, root->root_key.objectid,
  480. btrfs_ino(dir), index, name, namelen);
  481. BUG_ON(ret);
  482. ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
  483. root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
  484. objectid);
  485. if (ret)
  486. btrfs_abort_transaction(trans, root, ret);
  487. fail:
  488. trans->block_rsv = NULL;
  489. trans->bytes_reserved = 0;
  490. btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
  491. if (async_transid) {
  492. *async_transid = trans->transid;
  493. err = btrfs_commit_transaction_async(trans, root, 1);
  494. if (err)
  495. err = btrfs_commit_transaction(trans, root);
  496. } else {
  497. err = btrfs_commit_transaction(trans, root);
  498. }
  499. if (err && !ret)
  500. ret = err;
  501. if (!ret) {
  502. inode = btrfs_lookup_dentry(dir, dentry);
  503. if (IS_ERR(inode))
  504. return PTR_ERR(inode);
  505. d_instantiate(dentry, inode);
  506. }
  507. return ret;
  508. }
  509. static int create_snapshot(struct btrfs_root *root, struct inode *dir,
  510. struct dentry *dentry, char *name, int namelen,
  511. u64 *async_transid, bool readonly,
  512. struct btrfs_qgroup_inherit *inherit)
  513. {
  514. struct inode *inode;
  515. struct btrfs_pending_snapshot *pending_snapshot;
  516. struct btrfs_trans_handle *trans;
  517. int ret;
  518. if (!root->ref_cows)
  519. return -EINVAL;
  520. ret = btrfs_start_delalloc_inodes(root, 0);
  521. if (ret)
  522. return ret;
  523. btrfs_wait_ordered_extents(root, -1);
  524. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  525. if (!pending_snapshot)
  526. return -ENOMEM;
  527. btrfs_init_block_rsv(&pending_snapshot->block_rsv,
  528. BTRFS_BLOCK_RSV_TEMP);
  529. /*
  530. * 1 - parent dir inode
  531. * 2 - dir entries
  532. * 1 - root item
  533. * 2 - root ref/backref
  534. * 1 - root of snapshot
  535. * 1 - UUID item
  536. */
  537. ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
  538. &pending_snapshot->block_rsv, 8,
  539. &pending_snapshot->qgroup_reserved,
  540. false);
  541. if (ret)
  542. goto out;
  543. pending_snapshot->dentry = dentry;
  544. pending_snapshot->root = root;
  545. pending_snapshot->readonly = readonly;
  546. pending_snapshot->dir = dir;
  547. pending_snapshot->inherit = inherit;
  548. trans = btrfs_start_transaction(root, 0);
  549. if (IS_ERR(trans)) {
  550. ret = PTR_ERR(trans);
  551. goto fail;
  552. }
  553. spin_lock(&root->fs_info->trans_lock);
  554. list_add(&pending_snapshot->list,
  555. &trans->transaction->pending_snapshots);
  556. spin_unlock(&root->fs_info->trans_lock);
  557. if (async_transid) {
  558. *async_transid = trans->transid;
  559. ret = btrfs_commit_transaction_async(trans,
  560. root->fs_info->extent_root, 1);
  561. if (ret)
  562. ret = btrfs_commit_transaction(trans, root);
  563. } else {
  564. ret = btrfs_commit_transaction(trans,
  565. root->fs_info->extent_root);
  566. }
  567. if (ret)
  568. goto fail;
  569. ret = pending_snapshot->error;
  570. if (ret)
  571. goto fail;
  572. ret = btrfs_orphan_cleanup(pending_snapshot->snap);
  573. if (ret)
  574. goto fail;
  575. inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
  576. if (IS_ERR(inode)) {
  577. ret = PTR_ERR(inode);
  578. goto fail;
  579. }
  580. d_instantiate(dentry, inode);
  581. ret = 0;
  582. fail:
  583. btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
  584. &pending_snapshot->block_rsv,
  585. pending_snapshot->qgroup_reserved);
  586. out:
  587. kfree(pending_snapshot);
  588. return ret;
  589. }
  590. /* copy of check_sticky in fs/namei.c()
  591. * It's inline, so penalty for filesystems that don't use sticky bit is
  592. * minimal.
  593. */
  594. static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
  595. {
  596. kuid_t fsuid = current_fsuid();
  597. if (!(dir->i_mode & S_ISVTX))
  598. return 0;
  599. if (uid_eq(inode->i_uid, fsuid))
  600. return 0;
  601. if (uid_eq(dir->i_uid, fsuid))
  602. return 0;
  603. return !capable(CAP_FOWNER);
  604. }
  605. /* copy of may_delete in fs/namei.c()
  606. * Check whether we can remove a link victim from directory dir, check
  607. * whether the type of victim is right.
  608. * 1. We can't do it if dir is read-only (done in permission())
  609. * 2. We should have write and exec permissions on dir
  610. * 3. We can't remove anything from append-only dir
  611. * 4. We can't do anything with immutable dir (done in permission())
  612. * 5. If the sticky bit on dir is set we should either
  613. * a. be owner of dir, or
  614. * b. be owner of victim, or
  615. * c. have CAP_FOWNER capability
  616. * 6. If the victim is append-only or immutable we can't do antyhing with
  617. * links pointing to it.
  618. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  619. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  620. * 9. We can't remove a root or mountpoint.
  621. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  622. * nfs_async_unlink().
  623. */
  624. static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
  625. {
  626. int error;
  627. if (!victim->d_inode)
  628. return -ENOENT;
  629. BUG_ON(victim->d_parent->d_inode != dir);
  630. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  631. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  632. if (error)
  633. return error;
  634. if (IS_APPEND(dir))
  635. return -EPERM;
  636. if (btrfs_check_sticky(dir, victim->d_inode)||
  637. IS_APPEND(victim->d_inode)||
  638. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  639. return -EPERM;
  640. if (isdir) {
  641. if (!S_ISDIR(victim->d_inode->i_mode))
  642. return -ENOTDIR;
  643. if (IS_ROOT(victim))
  644. return -EBUSY;
  645. } else if (S_ISDIR(victim->d_inode->i_mode))
  646. return -EISDIR;
  647. if (IS_DEADDIR(dir))
  648. return -ENOENT;
  649. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  650. return -EBUSY;
  651. return 0;
  652. }
  653. /* copy of may_create in fs/namei.c() */
  654. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  655. {
  656. if (child->d_inode)
  657. return -EEXIST;
  658. if (IS_DEADDIR(dir))
  659. return -ENOENT;
  660. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  661. }
  662. /*
  663. * Create a new subvolume below @parent. This is largely modeled after
  664. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  665. * inside this filesystem so it's quite a bit simpler.
  666. */
  667. static noinline int btrfs_mksubvol(struct path *parent,
  668. char *name, int namelen,
  669. struct btrfs_root *snap_src,
  670. u64 *async_transid, bool readonly,
  671. struct btrfs_qgroup_inherit *inherit)
  672. {
  673. struct inode *dir = parent->dentry->d_inode;
  674. struct dentry *dentry;
  675. int error;
  676. error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
  677. if (error == -EINTR)
  678. return error;
  679. dentry = lookup_one_len(name, parent->dentry, namelen);
  680. error = PTR_ERR(dentry);
  681. if (IS_ERR(dentry))
  682. goto out_unlock;
  683. error = -EEXIST;
  684. if (dentry->d_inode)
  685. goto out_dput;
  686. error = btrfs_may_create(dir, dentry);
  687. if (error)
  688. goto out_dput;
  689. /*
  690. * even if this name doesn't exist, we may get hash collisions.
  691. * check for them now when we can safely fail
  692. */
  693. error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
  694. dir->i_ino, name,
  695. namelen);
  696. if (error)
  697. goto out_dput;
  698. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  699. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  700. goto out_up_read;
  701. if (snap_src) {
  702. error = create_snapshot(snap_src, dir, dentry, name, namelen,
  703. async_transid, readonly, inherit);
  704. } else {
  705. error = create_subvol(dir, dentry, name, namelen,
  706. async_transid, inherit);
  707. }
  708. if (!error)
  709. fsnotify_mkdir(dir, dentry);
  710. out_up_read:
  711. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  712. out_dput:
  713. dput(dentry);
  714. out_unlock:
  715. mutex_unlock(&dir->i_mutex);
  716. return error;
  717. }
  718. /*
  719. * When we're defragging a range, we don't want to kick it off again
  720. * if it is really just waiting for delalloc to send it down.
  721. * If we find a nice big extent or delalloc range for the bytes in the
  722. * file you want to defrag, we return 0 to let you know to skip this
  723. * part of the file
  724. */
  725. static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
  726. {
  727. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  728. struct extent_map *em = NULL;
  729. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  730. u64 end;
  731. read_lock(&em_tree->lock);
  732. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  733. read_unlock(&em_tree->lock);
  734. if (em) {
  735. end = extent_map_end(em);
  736. free_extent_map(em);
  737. if (end - offset > thresh)
  738. return 0;
  739. }
  740. /* if we already have a nice delalloc here, just stop */
  741. thresh /= 2;
  742. end = count_range_bits(io_tree, &offset, offset + thresh,
  743. thresh, EXTENT_DELALLOC, 1);
  744. if (end >= thresh)
  745. return 0;
  746. return 1;
  747. }
  748. /*
  749. * helper function to walk through a file and find extents
  750. * newer than a specific transid, and smaller than thresh.
  751. *
  752. * This is used by the defragging code to find new and small
  753. * extents
  754. */
  755. static int find_new_extents(struct btrfs_root *root,
  756. struct inode *inode, u64 newer_than,
  757. u64 *off, int thresh)
  758. {
  759. struct btrfs_path *path;
  760. struct btrfs_key min_key;
  761. struct extent_buffer *leaf;
  762. struct btrfs_file_extent_item *extent;
  763. int type;
  764. int ret;
  765. u64 ino = btrfs_ino(inode);
  766. path = btrfs_alloc_path();
  767. if (!path)
  768. return -ENOMEM;
  769. min_key.objectid = ino;
  770. min_key.type = BTRFS_EXTENT_DATA_KEY;
  771. min_key.offset = *off;
  772. path->keep_locks = 1;
  773. while (1) {
  774. ret = btrfs_search_forward(root, &min_key, path, newer_than);
  775. if (ret != 0)
  776. goto none;
  777. if (min_key.objectid != ino)
  778. goto none;
  779. if (min_key.type != BTRFS_EXTENT_DATA_KEY)
  780. goto none;
  781. leaf = path->nodes[0];
  782. extent = btrfs_item_ptr(leaf, path->slots[0],
  783. struct btrfs_file_extent_item);
  784. type = btrfs_file_extent_type(leaf, extent);
  785. if (type == BTRFS_FILE_EXTENT_REG &&
  786. btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
  787. check_defrag_in_cache(inode, min_key.offset, thresh)) {
  788. *off = min_key.offset;
  789. btrfs_free_path(path);
  790. return 0;
  791. }
  792. if (min_key.offset == (u64)-1)
  793. goto none;
  794. min_key.offset++;
  795. btrfs_release_path(path);
  796. }
  797. none:
  798. btrfs_free_path(path);
  799. return -ENOENT;
  800. }
  801. static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
  802. {
  803. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  804. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  805. struct extent_map *em;
  806. u64 len = PAGE_CACHE_SIZE;
  807. /*
  808. * hopefully we have this extent in the tree already, try without
  809. * the full extent lock
  810. */
  811. read_lock(&em_tree->lock);
  812. em = lookup_extent_mapping(em_tree, start, len);
  813. read_unlock(&em_tree->lock);
  814. if (!em) {
  815. /* get the big lock and read metadata off disk */
  816. lock_extent(io_tree, start, start + len - 1);
  817. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  818. unlock_extent(io_tree, start, start + len - 1);
  819. if (IS_ERR(em))
  820. return NULL;
  821. }
  822. return em;
  823. }
  824. static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
  825. {
  826. struct extent_map *next;
  827. bool ret = true;
  828. /* this is the last extent */
  829. if (em->start + em->len >= i_size_read(inode))
  830. return false;
  831. next = defrag_lookup_extent(inode, em->start + em->len);
  832. if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
  833. ret = false;
  834. free_extent_map(next);
  835. return ret;
  836. }
  837. static int should_defrag_range(struct inode *inode, u64 start, int thresh,
  838. u64 *last_len, u64 *skip, u64 *defrag_end,
  839. int compress)
  840. {
  841. struct extent_map *em;
  842. int ret = 1;
  843. bool next_mergeable = true;
  844. /*
  845. * make sure that once we start defragging an extent, we keep on
  846. * defragging it
  847. */
  848. if (start < *defrag_end)
  849. return 1;
  850. *skip = 0;
  851. em = defrag_lookup_extent(inode, start);
  852. if (!em)
  853. return 0;
  854. /* this will cover holes, and inline extents */
  855. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  856. ret = 0;
  857. goto out;
  858. }
  859. next_mergeable = defrag_check_next_extent(inode, em);
  860. /*
  861. * we hit a real extent, if it is big or the next extent is not a
  862. * real extent, don't bother defragging it
  863. */
  864. if (!compress && (*last_len == 0 || *last_len >= thresh) &&
  865. (em->len >= thresh || !next_mergeable))
  866. ret = 0;
  867. out:
  868. /*
  869. * last_len ends up being a counter of how many bytes we've defragged.
  870. * every time we choose not to defrag an extent, we reset *last_len
  871. * so that the next tiny extent will force a defrag.
  872. *
  873. * The end result of this is that tiny extents before a single big
  874. * extent will force at least part of that big extent to be defragged.
  875. */
  876. if (ret) {
  877. *defrag_end = extent_map_end(em);
  878. } else {
  879. *last_len = 0;
  880. *skip = extent_map_end(em);
  881. *defrag_end = 0;
  882. }
  883. free_extent_map(em);
  884. return ret;
  885. }
  886. /*
  887. * it doesn't do much good to defrag one or two pages
  888. * at a time. This pulls in a nice chunk of pages
  889. * to COW and defrag.
  890. *
  891. * It also makes sure the delalloc code has enough
  892. * dirty data to avoid making new small extents as part
  893. * of the defrag
  894. *
  895. * It's a good idea to start RA on this range
  896. * before calling this.
  897. */
  898. static int cluster_pages_for_defrag(struct inode *inode,
  899. struct page **pages,
  900. unsigned long start_index,
  901. unsigned long num_pages)
  902. {
  903. unsigned long file_end;
  904. u64 isize = i_size_read(inode);
  905. u64 page_start;
  906. u64 page_end;
  907. u64 page_cnt;
  908. int ret;
  909. int i;
  910. int i_done;
  911. struct btrfs_ordered_extent *ordered;
  912. struct extent_state *cached_state = NULL;
  913. struct extent_io_tree *tree;
  914. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  915. file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
  916. if (!isize || start_index > file_end)
  917. return 0;
  918. page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
  919. ret = btrfs_delalloc_reserve_space(inode,
  920. page_cnt << PAGE_CACHE_SHIFT);
  921. if (ret)
  922. return ret;
  923. i_done = 0;
  924. tree = &BTRFS_I(inode)->io_tree;
  925. /* step one, lock all the pages */
  926. for (i = 0; i < page_cnt; i++) {
  927. struct page *page;
  928. again:
  929. page = find_or_create_page(inode->i_mapping,
  930. start_index + i, mask);
  931. if (!page)
  932. break;
  933. page_start = page_offset(page);
  934. page_end = page_start + PAGE_CACHE_SIZE - 1;
  935. while (1) {
  936. lock_extent(tree, page_start, page_end);
  937. ordered = btrfs_lookup_ordered_extent(inode,
  938. page_start);
  939. unlock_extent(tree, page_start, page_end);
  940. if (!ordered)
  941. break;
  942. unlock_page(page);
  943. btrfs_start_ordered_extent(inode, ordered, 1);
  944. btrfs_put_ordered_extent(ordered);
  945. lock_page(page);
  946. /*
  947. * we unlocked the page above, so we need check if
  948. * it was released or not.
  949. */
  950. if (page->mapping != inode->i_mapping) {
  951. unlock_page(page);
  952. page_cache_release(page);
  953. goto again;
  954. }
  955. }
  956. if (!PageUptodate(page)) {
  957. btrfs_readpage(NULL, page);
  958. lock_page(page);
  959. if (!PageUptodate(page)) {
  960. unlock_page(page);
  961. page_cache_release(page);
  962. ret = -EIO;
  963. break;
  964. }
  965. }
  966. if (page->mapping != inode->i_mapping) {
  967. unlock_page(page);
  968. page_cache_release(page);
  969. goto again;
  970. }
  971. pages[i] = page;
  972. i_done++;
  973. }
  974. if (!i_done || ret)
  975. goto out;
  976. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  977. goto out;
  978. /*
  979. * so now we have a nice long stream of locked
  980. * and up to date pages, lets wait on them
  981. */
  982. for (i = 0; i < i_done; i++)
  983. wait_on_page_writeback(pages[i]);
  984. page_start = page_offset(pages[0]);
  985. page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
  986. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  987. page_start, page_end - 1, 0, &cached_state);
  988. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
  989. page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  990. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
  991. &cached_state, GFP_NOFS);
  992. if (i_done != page_cnt) {
  993. spin_lock(&BTRFS_I(inode)->lock);
  994. BTRFS_I(inode)->outstanding_extents++;
  995. spin_unlock(&BTRFS_I(inode)->lock);
  996. btrfs_delalloc_release_space(inode,
  997. (page_cnt - i_done) << PAGE_CACHE_SHIFT);
  998. }
  999. set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
  1000. &cached_state, GFP_NOFS);
  1001. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1002. page_start, page_end - 1, &cached_state,
  1003. GFP_NOFS);
  1004. for (i = 0; i < i_done; i++) {
  1005. clear_page_dirty_for_io(pages[i]);
  1006. ClearPageChecked(pages[i]);
  1007. set_page_extent_mapped(pages[i]);
  1008. set_page_dirty(pages[i]);
  1009. unlock_page(pages[i]);
  1010. page_cache_release(pages[i]);
  1011. }
  1012. return i_done;
  1013. out:
  1014. for (i = 0; i < i_done; i++) {
  1015. unlock_page(pages[i]);
  1016. page_cache_release(pages[i]);
  1017. }
  1018. btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
  1019. return ret;
  1020. }
  1021. int btrfs_defrag_file(struct inode *inode, struct file *file,
  1022. struct btrfs_ioctl_defrag_range_args *range,
  1023. u64 newer_than, unsigned long max_to_defrag)
  1024. {
  1025. struct btrfs_root *root = BTRFS_I(inode)->root;
  1026. struct file_ra_state *ra = NULL;
  1027. unsigned long last_index;
  1028. u64 isize = i_size_read(inode);
  1029. u64 last_len = 0;
  1030. u64 skip = 0;
  1031. u64 defrag_end = 0;
  1032. u64 newer_off = range->start;
  1033. unsigned long i;
  1034. unsigned long ra_index = 0;
  1035. int ret;
  1036. int defrag_count = 0;
  1037. int compress_type = BTRFS_COMPRESS_ZLIB;
  1038. int extent_thresh = range->extent_thresh;
  1039. unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
  1040. unsigned long cluster = max_cluster;
  1041. u64 new_align = ~((u64)128 * 1024 - 1);
  1042. struct page **pages = NULL;
  1043. if (isize == 0)
  1044. return 0;
  1045. if (range->start >= isize)
  1046. return -EINVAL;
  1047. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  1048. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  1049. return -EINVAL;
  1050. if (range->compress_type)
  1051. compress_type = range->compress_type;
  1052. }
  1053. if (extent_thresh == 0)
  1054. extent_thresh = 256 * 1024;
  1055. /*
  1056. * if we were not given a file, allocate a readahead
  1057. * context
  1058. */
  1059. if (!file) {
  1060. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  1061. if (!ra)
  1062. return -ENOMEM;
  1063. file_ra_state_init(ra, inode->i_mapping);
  1064. } else {
  1065. ra = &file->f_ra;
  1066. }
  1067. pages = kmalloc_array(max_cluster, sizeof(struct page *),
  1068. GFP_NOFS);
  1069. if (!pages) {
  1070. ret = -ENOMEM;
  1071. goto out_ra;
  1072. }
  1073. /* find the last page to defrag */
  1074. if (range->start + range->len > range->start) {
  1075. last_index = min_t(u64, isize - 1,
  1076. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  1077. } else {
  1078. last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1079. }
  1080. if (newer_than) {
  1081. ret = find_new_extents(root, inode, newer_than,
  1082. &newer_off, 64 * 1024);
  1083. if (!ret) {
  1084. range->start = newer_off;
  1085. /*
  1086. * we always align our defrag to help keep
  1087. * the extents in the file evenly spaced
  1088. */
  1089. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  1090. } else
  1091. goto out_ra;
  1092. } else {
  1093. i = range->start >> PAGE_CACHE_SHIFT;
  1094. }
  1095. if (!max_to_defrag)
  1096. max_to_defrag = last_index + 1;
  1097. /*
  1098. * make writeback starts from i, so the defrag range can be
  1099. * written sequentially.
  1100. */
  1101. if (i < inode->i_mapping->writeback_index)
  1102. inode->i_mapping->writeback_index = i;
  1103. while (i <= last_index && defrag_count < max_to_defrag &&
  1104. (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
  1105. PAGE_CACHE_SHIFT)) {
  1106. /*
  1107. * make sure we stop running if someone unmounts
  1108. * the FS
  1109. */
  1110. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  1111. break;
  1112. if (btrfs_defrag_cancelled(root->fs_info)) {
  1113. printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
  1114. ret = -EAGAIN;
  1115. break;
  1116. }
  1117. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  1118. extent_thresh, &last_len, &skip,
  1119. &defrag_end, range->flags &
  1120. BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1121. unsigned long next;
  1122. /*
  1123. * the should_defrag function tells us how much to skip
  1124. * bump our counter by the suggested amount
  1125. */
  1126. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1127. i = max(i + 1, next);
  1128. continue;
  1129. }
  1130. if (!newer_than) {
  1131. cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
  1132. PAGE_CACHE_SHIFT) - i;
  1133. cluster = min(cluster, max_cluster);
  1134. } else {
  1135. cluster = max_cluster;
  1136. }
  1137. if (i + cluster > ra_index) {
  1138. ra_index = max(i, ra_index);
  1139. btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
  1140. cluster);
  1141. ra_index += max_cluster;
  1142. }
  1143. mutex_lock(&inode->i_mutex);
  1144. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  1145. BTRFS_I(inode)->force_compress = compress_type;
  1146. ret = cluster_pages_for_defrag(inode, pages, i, cluster);
  1147. if (ret < 0) {
  1148. mutex_unlock(&inode->i_mutex);
  1149. goto out_ra;
  1150. }
  1151. defrag_count += ret;
  1152. balance_dirty_pages_ratelimited(inode->i_mapping);
  1153. mutex_unlock(&inode->i_mutex);
  1154. if (newer_than) {
  1155. if (newer_off == (u64)-1)
  1156. break;
  1157. if (ret > 0)
  1158. i += ret;
  1159. newer_off = max(newer_off + 1,
  1160. (u64)i << PAGE_CACHE_SHIFT);
  1161. ret = find_new_extents(root, inode,
  1162. newer_than, &newer_off,
  1163. 64 * 1024);
  1164. if (!ret) {
  1165. range->start = newer_off;
  1166. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  1167. } else {
  1168. break;
  1169. }
  1170. } else {
  1171. if (ret > 0) {
  1172. i += ret;
  1173. last_len += ret << PAGE_CACHE_SHIFT;
  1174. } else {
  1175. i++;
  1176. last_len = 0;
  1177. }
  1178. }
  1179. }
  1180. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  1181. filemap_flush(inode->i_mapping);
  1182. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1183. /* the filemap_flush will queue IO into the worker threads, but
  1184. * we have to make sure the IO is actually started and that
  1185. * ordered extents get created before we return
  1186. */
  1187. atomic_inc(&root->fs_info->async_submit_draining);
  1188. while (atomic_read(&root->fs_info->nr_async_submits) ||
  1189. atomic_read(&root->fs_info->async_delalloc_pages)) {
  1190. wait_event(root->fs_info->async_submit_wait,
  1191. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  1192. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  1193. }
  1194. atomic_dec(&root->fs_info->async_submit_draining);
  1195. }
  1196. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  1197. btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
  1198. }
  1199. ret = defrag_count;
  1200. out_ra:
  1201. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  1202. mutex_lock(&inode->i_mutex);
  1203. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  1204. mutex_unlock(&inode->i_mutex);
  1205. }
  1206. if (!file)
  1207. kfree(ra);
  1208. kfree(pages);
  1209. return ret;
  1210. }
  1211. static noinline int btrfs_ioctl_resize(struct file *file,
  1212. void __user *arg)
  1213. {
  1214. u64 new_size;
  1215. u64 old_size;
  1216. u64 devid = 1;
  1217. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  1218. struct btrfs_ioctl_vol_args *vol_args;
  1219. struct btrfs_trans_handle *trans;
  1220. struct btrfs_device *device = NULL;
  1221. char *sizestr;
  1222. char *devstr = NULL;
  1223. int ret = 0;
  1224. int mod = 0;
  1225. if (!capable(CAP_SYS_ADMIN))
  1226. return -EPERM;
  1227. ret = mnt_want_write_file(file);
  1228. if (ret)
  1229. return ret;
  1230. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  1231. 1)) {
  1232. mnt_drop_write_file(file);
  1233. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  1234. }
  1235. mutex_lock(&root->fs_info->volume_mutex);
  1236. vol_args = memdup_user(arg, sizeof(*vol_args));
  1237. if (IS_ERR(vol_args)) {
  1238. ret = PTR_ERR(vol_args);
  1239. goto out;
  1240. }
  1241. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1242. sizestr = vol_args->name;
  1243. devstr = strchr(sizestr, ':');
  1244. if (devstr) {
  1245. char *end;
  1246. sizestr = devstr + 1;
  1247. *devstr = '\0';
  1248. devstr = vol_args->name;
  1249. devid = simple_strtoull(devstr, &end, 10);
  1250. if (!devid) {
  1251. ret = -EINVAL;
  1252. goto out_free;
  1253. }
  1254. btrfs_info(root->fs_info, "resizing devid %llu", devid);
  1255. }
  1256. device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
  1257. if (!device) {
  1258. btrfs_info(root->fs_info, "resizer unable to find device %llu",
  1259. devid);
  1260. ret = -ENODEV;
  1261. goto out_free;
  1262. }
  1263. if (!device->writeable) {
  1264. btrfs_info(root->fs_info,
  1265. "resizer unable to apply on readonly device %llu",
  1266. devid);
  1267. ret = -EPERM;
  1268. goto out_free;
  1269. }
  1270. if (!strcmp(sizestr, "max"))
  1271. new_size = device->bdev->bd_inode->i_size;
  1272. else {
  1273. if (sizestr[0] == '-') {
  1274. mod = -1;
  1275. sizestr++;
  1276. } else if (sizestr[0] == '+') {
  1277. mod = 1;
  1278. sizestr++;
  1279. }
  1280. new_size = memparse(sizestr, NULL);
  1281. if (new_size == 0) {
  1282. ret = -EINVAL;
  1283. goto out_free;
  1284. }
  1285. }
  1286. if (device->is_tgtdev_for_dev_replace) {
  1287. ret = -EPERM;
  1288. goto out_free;
  1289. }
  1290. old_size = device->total_bytes;
  1291. if (mod < 0) {
  1292. if (new_size > old_size) {
  1293. ret = -EINVAL;
  1294. goto out_free;
  1295. }
  1296. new_size = old_size - new_size;
  1297. } else if (mod > 0) {
  1298. if (new_size > ULLONG_MAX - old_size) {
  1299. ret = -EINVAL;
  1300. goto out_free;
  1301. }
  1302. new_size = old_size + new_size;
  1303. }
  1304. if (new_size < 256 * 1024 * 1024) {
  1305. ret = -EINVAL;
  1306. goto out_free;
  1307. }
  1308. if (new_size > device->bdev->bd_inode->i_size) {
  1309. ret = -EFBIG;
  1310. goto out_free;
  1311. }
  1312. do_div(new_size, root->sectorsize);
  1313. new_size *= root->sectorsize;
  1314. printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
  1315. rcu_str_deref(device->name), new_size);
  1316. if (new_size > old_size) {
  1317. trans = btrfs_start_transaction(root, 0);
  1318. if (IS_ERR(trans)) {
  1319. ret = PTR_ERR(trans);
  1320. goto out_free;
  1321. }
  1322. ret = btrfs_grow_device(trans, device, new_size);
  1323. btrfs_commit_transaction(trans, root);
  1324. } else if (new_size < old_size) {
  1325. ret = btrfs_shrink_device(device, new_size);
  1326. } /* equal, nothing need to do */
  1327. out_free:
  1328. kfree(vol_args);
  1329. out:
  1330. mutex_unlock(&root->fs_info->volume_mutex);
  1331. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  1332. mnt_drop_write_file(file);
  1333. return ret;
  1334. }
  1335. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1336. char *name, unsigned long fd, int subvol,
  1337. u64 *transid, bool readonly,
  1338. struct btrfs_qgroup_inherit *inherit)
  1339. {
  1340. int namelen;
  1341. int ret = 0;
  1342. ret = mnt_want_write_file(file);
  1343. if (ret)
  1344. goto out;
  1345. namelen = strlen(name);
  1346. if (strchr(name, '/')) {
  1347. ret = -EINVAL;
  1348. goto out_drop_write;
  1349. }
  1350. if (name[0] == '.' &&
  1351. (namelen == 1 || (name[1] == '.' && namelen == 2))) {
  1352. ret = -EEXIST;
  1353. goto out_drop_write;
  1354. }
  1355. if (subvol) {
  1356. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1357. NULL, transid, readonly, inherit);
  1358. } else {
  1359. struct fd src = fdget(fd);
  1360. struct inode *src_inode;
  1361. if (!src.file) {
  1362. ret = -EINVAL;
  1363. goto out_drop_write;
  1364. }
  1365. src_inode = file_inode(src.file);
  1366. if (src_inode->i_sb != file_inode(file)->i_sb) {
  1367. btrfs_info(BTRFS_I(src_inode)->root->fs_info,
  1368. "Snapshot src from another FS");
  1369. ret = -EINVAL;
  1370. } else if (!inode_owner_or_capable(src_inode)) {
  1371. /*
  1372. * Subvolume creation is not restricted, but snapshots
  1373. * are limited to own subvolumes only
  1374. */
  1375. ret = -EPERM;
  1376. } else {
  1377. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1378. BTRFS_I(src_inode)->root,
  1379. transid, readonly, inherit);
  1380. }
  1381. fdput(src);
  1382. }
  1383. out_drop_write:
  1384. mnt_drop_write_file(file);
  1385. out:
  1386. return ret;
  1387. }
  1388. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1389. void __user *arg, int subvol)
  1390. {
  1391. struct btrfs_ioctl_vol_args *vol_args;
  1392. int ret;
  1393. vol_args = memdup_user(arg, sizeof(*vol_args));
  1394. if (IS_ERR(vol_args))
  1395. return PTR_ERR(vol_args);
  1396. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1397. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1398. vol_args->fd, subvol,
  1399. NULL, false, NULL);
  1400. kfree(vol_args);
  1401. return ret;
  1402. }
  1403. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1404. void __user *arg, int subvol)
  1405. {
  1406. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1407. int ret;
  1408. u64 transid = 0;
  1409. u64 *ptr = NULL;
  1410. bool readonly = false;
  1411. struct btrfs_qgroup_inherit *inherit = NULL;
  1412. vol_args = memdup_user(arg, sizeof(*vol_args));
  1413. if (IS_ERR(vol_args))
  1414. return PTR_ERR(vol_args);
  1415. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1416. if (vol_args->flags &
  1417. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
  1418. BTRFS_SUBVOL_QGROUP_INHERIT)) {
  1419. ret = -EOPNOTSUPP;
  1420. goto out;
  1421. }
  1422. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1423. ptr = &transid;
  1424. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1425. readonly = true;
  1426. if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
  1427. if (vol_args->size > PAGE_CACHE_SIZE) {
  1428. ret = -EINVAL;
  1429. goto out;
  1430. }
  1431. inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
  1432. if (IS_ERR(inherit)) {
  1433. ret = PTR_ERR(inherit);
  1434. goto out;
  1435. }
  1436. }
  1437. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1438. vol_args->fd, subvol, ptr,
  1439. readonly, inherit);
  1440. if (ret == 0 && ptr &&
  1441. copy_to_user(arg +
  1442. offsetof(struct btrfs_ioctl_vol_args_v2,
  1443. transid), ptr, sizeof(*ptr)))
  1444. ret = -EFAULT;
  1445. out:
  1446. kfree(vol_args);
  1447. kfree(inherit);
  1448. return ret;
  1449. }
  1450. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1451. void __user *arg)
  1452. {
  1453. struct inode *inode = file_inode(file);
  1454. struct btrfs_root *root = BTRFS_I(inode)->root;
  1455. int ret = 0;
  1456. u64 flags = 0;
  1457. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1458. return -EINVAL;
  1459. down_read(&root->fs_info->subvol_sem);
  1460. if (btrfs_root_readonly(root))
  1461. flags |= BTRFS_SUBVOL_RDONLY;
  1462. up_read(&root->fs_info->subvol_sem);
  1463. if (copy_to_user(arg, &flags, sizeof(flags)))
  1464. ret = -EFAULT;
  1465. return ret;
  1466. }
  1467. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1468. void __user *arg)
  1469. {
  1470. struct inode *inode = file_inode(file);
  1471. struct btrfs_root *root = BTRFS_I(inode)->root;
  1472. struct btrfs_trans_handle *trans;
  1473. u64 root_flags;
  1474. u64 flags;
  1475. int ret = 0;
  1476. if (!inode_owner_or_capable(inode))
  1477. return -EPERM;
  1478. ret = mnt_want_write_file(file);
  1479. if (ret)
  1480. goto out;
  1481. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1482. ret = -EINVAL;
  1483. goto out_drop_write;
  1484. }
  1485. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1486. ret = -EFAULT;
  1487. goto out_drop_write;
  1488. }
  1489. if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
  1490. ret = -EINVAL;
  1491. goto out_drop_write;
  1492. }
  1493. if (flags & ~BTRFS_SUBVOL_RDONLY) {
  1494. ret = -EOPNOTSUPP;
  1495. goto out_drop_write;
  1496. }
  1497. down_write(&root->fs_info->subvol_sem);
  1498. /* nothing to do */
  1499. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1500. goto out_drop_sem;
  1501. root_flags = btrfs_root_flags(&root->root_item);
  1502. if (flags & BTRFS_SUBVOL_RDONLY) {
  1503. btrfs_set_root_flags(&root->root_item,
  1504. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1505. } else {
  1506. /*
  1507. * Block RO -> RW transition if this subvolume is involved in
  1508. * send
  1509. */
  1510. spin_lock(&root->root_item_lock);
  1511. if (root->send_in_progress == 0) {
  1512. btrfs_set_root_flags(&root->root_item,
  1513. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1514. spin_unlock(&root->root_item_lock);
  1515. } else {
  1516. spin_unlock(&root->root_item_lock);
  1517. btrfs_warn(root->fs_info,
  1518. "Attempt to set subvolume %llu read-write during send",
  1519. root->root_key.objectid);
  1520. ret = -EPERM;
  1521. goto out_drop_sem;
  1522. }
  1523. }
  1524. trans = btrfs_start_transaction(root, 1);
  1525. if (IS_ERR(trans)) {
  1526. ret = PTR_ERR(trans);
  1527. goto out_reset;
  1528. }
  1529. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1530. &root->root_key, &root->root_item);
  1531. btrfs_commit_transaction(trans, root);
  1532. out_reset:
  1533. if (ret)
  1534. btrfs_set_root_flags(&root->root_item, root_flags);
  1535. out_drop_sem:
  1536. up_write(&root->fs_info->subvol_sem);
  1537. out_drop_write:
  1538. mnt_drop_write_file(file);
  1539. out:
  1540. return ret;
  1541. }
  1542. /*
  1543. * helper to check if the subvolume references other subvolumes
  1544. */
  1545. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1546. {
  1547. struct btrfs_path *path;
  1548. struct btrfs_dir_item *di;
  1549. struct btrfs_key key;
  1550. u64 dir_id;
  1551. int ret;
  1552. path = btrfs_alloc_path();
  1553. if (!path)
  1554. return -ENOMEM;
  1555. /* Make sure this root isn't set as the default subvol */
  1556. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  1557. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
  1558. dir_id, "default", 7, 0);
  1559. if (di && !IS_ERR(di)) {
  1560. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
  1561. if (key.objectid == root->root_key.objectid) {
  1562. ret = -ENOTEMPTY;
  1563. goto out;
  1564. }
  1565. btrfs_release_path(path);
  1566. }
  1567. key.objectid = root->root_key.objectid;
  1568. key.type = BTRFS_ROOT_REF_KEY;
  1569. key.offset = (u64)-1;
  1570. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1571. &key, path, 0, 0);
  1572. if (ret < 0)
  1573. goto out;
  1574. BUG_ON(ret == 0);
  1575. ret = 0;
  1576. if (path->slots[0] > 0) {
  1577. path->slots[0]--;
  1578. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1579. if (key.objectid == root->root_key.objectid &&
  1580. key.type == BTRFS_ROOT_REF_KEY)
  1581. ret = -ENOTEMPTY;
  1582. }
  1583. out:
  1584. btrfs_free_path(path);
  1585. return ret;
  1586. }
  1587. static noinline int key_in_sk(struct btrfs_key *key,
  1588. struct btrfs_ioctl_search_key *sk)
  1589. {
  1590. struct btrfs_key test;
  1591. int ret;
  1592. test.objectid = sk->min_objectid;
  1593. test.type = sk->min_type;
  1594. test.offset = sk->min_offset;
  1595. ret = btrfs_comp_cpu_keys(key, &test);
  1596. if (ret < 0)
  1597. return 0;
  1598. test.objectid = sk->max_objectid;
  1599. test.type = sk->max_type;
  1600. test.offset = sk->max_offset;
  1601. ret = btrfs_comp_cpu_keys(key, &test);
  1602. if (ret > 0)
  1603. return 0;
  1604. return 1;
  1605. }
  1606. static noinline int copy_to_sk(struct btrfs_root *root,
  1607. struct btrfs_path *path,
  1608. struct btrfs_key *key,
  1609. struct btrfs_ioctl_search_key *sk,
  1610. char *buf,
  1611. unsigned long *sk_offset,
  1612. int *num_found)
  1613. {
  1614. u64 found_transid;
  1615. struct extent_buffer *leaf;
  1616. struct btrfs_ioctl_search_header sh;
  1617. unsigned long item_off;
  1618. unsigned long item_len;
  1619. int nritems;
  1620. int i;
  1621. int slot;
  1622. int ret = 0;
  1623. leaf = path->nodes[0];
  1624. slot = path->slots[0];
  1625. nritems = btrfs_header_nritems(leaf);
  1626. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1627. i = nritems;
  1628. goto advance_key;
  1629. }
  1630. found_transid = btrfs_header_generation(leaf);
  1631. for (i = slot; i < nritems; i++) {
  1632. item_off = btrfs_item_ptr_offset(leaf, i);
  1633. item_len = btrfs_item_size_nr(leaf, i);
  1634. btrfs_item_key_to_cpu(leaf, key, i);
  1635. if (!key_in_sk(key, sk))
  1636. continue;
  1637. if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1638. item_len = 0;
  1639. if (sizeof(sh) + item_len + *sk_offset >
  1640. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1641. ret = 1;
  1642. goto overflow;
  1643. }
  1644. sh.objectid = key->objectid;
  1645. sh.offset = key->offset;
  1646. sh.type = key->type;
  1647. sh.len = item_len;
  1648. sh.transid = found_transid;
  1649. /* copy search result header */
  1650. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1651. *sk_offset += sizeof(sh);
  1652. if (item_len) {
  1653. char *p = buf + *sk_offset;
  1654. /* copy the item */
  1655. read_extent_buffer(leaf, p,
  1656. item_off, item_len);
  1657. *sk_offset += item_len;
  1658. }
  1659. (*num_found)++;
  1660. if (*num_found >= sk->nr_items)
  1661. break;
  1662. }
  1663. advance_key:
  1664. ret = 0;
  1665. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1666. key->offset++;
  1667. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1668. key->offset = 0;
  1669. key->type++;
  1670. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1671. key->offset = 0;
  1672. key->type = 0;
  1673. key->objectid++;
  1674. } else
  1675. ret = 1;
  1676. overflow:
  1677. return ret;
  1678. }
  1679. static noinline int search_ioctl(struct inode *inode,
  1680. struct btrfs_ioctl_search_args *args)
  1681. {
  1682. struct btrfs_root *root;
  1683. struct btrfs_key key;
  1684. struct btrfs_path *path;
  1685. struct btrfs_ioctl_search_key *sk = &args->key;
  1686. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1687. int ret;
  1688. int num_found = 0;
  1689. unsigned long sk_offset = 0;
  1690. path = btrfs_alloc_path();
  1691. if (!path)
  1692. return -ENOMEM;
  1693. if (sk->tree_id == 0) {
  1694. /* search the root of the inode that was passed */
  1695. root = BTRFS_I(inode)->root;
  1696. } else {
  1697. key.objectid = sk->tree_id;
  1698. key.type = BTRFS_ROOT_ITEM_KEY;
  1699. key.offset = (u64)-1;
  1700. root = btrfs_read_fs_root_no_name(info, &key);
  1701. if (IS_ERR(root)) {
  1702. printk(KERN_ERR "BTRFS: could not find root %llu\n",
  1703. sk->tree_id);
  1704. btrfs_free_path(path);
  1705. return -ENOENT;
  1706. }
  1707. }
  1708. key.objectid = sk->min_objectid;
  1709. key.type = sk->min_type;
  1710. key.offset = sk->min_offset;
  1711. path->keep_locks = 1;
  1712. while (1) {
  1713. ret = btrfs_search_forward(root, &key, path, sk->min_transid);
  1714. if (ret != 0) {
  1715. if (ret > 0)
  1716. ret = 0;
  1717. goto err;
  1718. }
  1719. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1720. &sk_offset, &num_found);
  1721. btrfs_release_path(path);
  1722. if (ret || num_found >= sk->nr_items)
  1723. break;
  1724. }
  1725. ret = 0;
  1726. err:
  1727. sk->nr_items = num_found;
  1728. btrfs_free_path(path);
  1729. return ret;
  1730. }
  1731. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1732. void __user *argp)
  1733. {
  1734. struct btrfs_ioctl_search_args *args;
  1735. struct inode *inode;
  1736. int ret;
  1737. if (!capable(CAP_SYS_ADMIN))
  1738. return -EPERM;
  1739. args = memdup_user(argp, sizeof(*args));
  1740. if (IS_ERR(args))
  1741. return PTR_ERR(args);
  1742. inode = file_inode(file);
  1743. ret = search_ioctl(inode, args);
  1744. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1745. ret = -EFAULT;
  1746. kfree(args);
  1747. return ret;
  1748. }
  1749. /*
  1750. * Search INODE_REFs to identify path name of 'dirid' directory
  1751. * in a 'tree_id' tree. and sets path name to 'name'.
  1752. */
  1753. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1754. u64 tree_id, u64 dirid, char *name)
  1755. {
  1756. struct btrfs_root *root;
  1757. struct btrfs_key key;
  1758. char *ptr;
  1759. int ret = -1;
  1760. int slot;
  1761. int len;
  1762. int total_len = 0;
  1763. struct btrfs_inode_ref *iref;
  1764. struct extent_buffer *l;
  1765. struct btrfs_path *path;
  1766. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1767. name[0]='\0';
  1768. return 0;
  1769. }
  1770. path = btrfs_alloc_path();
  1771. if (!path)
  1772. return -ENOMEM;
  1773. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1774. key.objectid = tree_id;
  1775. key.type = BTRFS_ROOT_ITEM_KEY;
  1776. key.offset = (u64)-1;
  1777. root = btrfs_read_fs_root_no_name(info, &key);
  1778. if (IS_ERR(root)) {
  1779. printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
  1780. ret = -ENOENT;
  1781. goto out;
  1782. }
  1783. key.objectid = dirid;
  1784. key.type = BTRFS_INODE_REF_KEY;
  1785. key.offset = (u64)-1;
  1786. while (1) {
  1787. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1788. if (ret < 0)
  1789. goto out;
  1790. else if (ret > 0) {
  1791. ret = btrfs_previous_item(root, path, dirid,
  1792. BTRFS_INODE_REF_KEY);
  1793. if (ret < 0)
  1794. goto out;
  1795. else if (ret > 0) {
  1796. ret = -ENOENT;
  1797. goto out;
  1798. }
  1799. }
  1800. l = path->nodes[0];
  1801. slot = path->slots[0];
  1802. btrfs_item_key_to_cpu(l, &key, slot);
  1803. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1804. len = btrfs_inode_ref_name_len(l, iref);
  1805. ptr -= len + 1;
  1806. total_len += len + 1;
  1807. if (ptr < name) {
  1808. ret = -ENAMETOOLONG;
  1809. goto out;
  1810. }
  1811. *(ptr + len) = '/';
  1812. read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
  1813. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1814. break;
  1815. btrfs_release_path(path);
  1816. key.objectid = key.offset;
  1817. key.offset = (u64)-1;
  1818. dirid = key.objectid;
  1819. }
  1820. memmove(name, ptr, total_len);
  1821. name[total_len] = '\0';
  1822. ret = 0;
  1823. out:
  1824. btrfs_free_path(path);
  1825. return ret;
  1826. }
  1827. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1828. void __user *argp)
  1829. {
  1830. struct btrfs_ioctl_ino_lookup_args *args;
  1831. struct inode *inode;
  1832. int ret;
  1833. if (!capable(CAP_SYS_ADMIN))
  1834. return -EPERM;
  1835. args = memdup_user(argp, sizeof(*args));
  1836. if (IS_ERR(args))
  1837. return PTR_ERR(args);
  1838. inode = file_inode(file);
  1839. if (args->treeid == 0)
  1840. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1841. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1842. args->treeid, args->objectid,
  1843. args->name);
  1844. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1845. ret = -EFAULT;
  1846. kfree(args);
  1847. return ret;
  1848. }
  1849. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1850. void __user *arg)
  1851. {
  1852. struct dentry *parent = file->f_path.dentry;
  1853. struct dentry *dentry;
  1854. struct inode *dir = parent->d_inode;
  1855. struct inode *inode;
  1856. struct btrfs_root *root = BTRFS_I(dir)->root;
  1857. struct btrfs_root *dest = NULL;
  1858. struct btrfs_ioctl_vol_args *vol_args;
  1859. struct btrfs_trans_handle *trans;
  1860. struct btrfs_block_rsv block_rsv;
  1861. u64 qgroup_reserved;
  1862. int namelen;
  1863. int ret;
  1864. int err = 0;
  1865. vol_args = memdup_user(arg, sizeof(*vol_args));
  1866. if (IS_ERR(vol_args))
  1867. return PTR_ERR(vol_args);
  1868. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1869. namelen = strlen(vol_args->name);
  1870. if (strchr(vol_args->name, '/') ||
  1871. strncmp(vol_args->name, "..", namelen) == 0) {
  1872. err = -EINVAL;
  1873. goto out;
  1874. }
  1875. err = mnt_want_write_file(file);
  1876. if (err)
  1877. goto out;
  1878. err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1879. if (err == -EINTR)
  1880. goto out_drop_write;
  1881. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1882. if (IS_ERR(dentry)) {
  1883. err = PTR_ERR(dentry);
  1884. goto out_unlock_dir;
  1885. }
  1886. if (!dentry->d_inode) {
  1887. err = -ENOENT;
  1888. goto out_dput;
  1889. }
  1890. inode = dentry->d_inode;
  1891. dest = BTRFS_I(inode)->root;
  1892. if (!capable(CAP_SYS_ADMIN)) {
  1893. /*
  1894. * Regular user. Only allow this with a special mount
  1895. * option, when the user has write+exec access to the
  1896. * subvol root, and when rmdir(2) would have been
  1897. * allowed.
  1898. *
  1899. * Note that this is _not_ check that the subvol is
  1900. * empty or doesn't contain data that we wouldn't
  1901. * otherwise be able to delete.
  1902. *
  1903. * Users who want to delete empty subvols should try
  1904. * rmdir(2).
  1905. */
  1906. err = -EPERM;
  1907. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1908. goto out_dput;
  1909. /*
  1910. * Do not allow deletion if the parent dir is the same
  1911. * as the dir to be deleted. That means the ioctl
  1912. * must be called on the dentry referencing the root
  1913. * of the subvol, not a random directory contained
  1914. * within it.
  1915. */
  1916. err = -EINVAL;
  1917. if (root == dest)
  1918. goto out_dput;
  1919. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1920. if (err)
  1921. goto out_dput;
  1922. }
  1923. /* check if subvolume may be deleted by a user */
  1924. err = btrfs_may_delete(dir, dentry, 1);
  1925. if (err)
  1926. goto out_dput;
  1927. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1928. err = -EINVAL;
  1929. goto out_dput;
  1930. }
  1931. mutex_lock(&inode->i_mutex);
  1932. err = d_invalidate(dentry);
  1933. if (err)
  1934. goto out_unlock;
  1935. down_write(&root->fs_info->subvol_sem);
  1936. err = may_destroy_subvol(dest);
  1937. if (err)
  1938. goto out_up_write;
  1939. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  1940. /*
  1941. * One for dir inode, two for dir entries, two for root
  1942. * ref/backref.
  1943. */
  1944. err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
  1945. 5, &qgroup_reserved, true);
  1946. if (err)
  1947. goto out_up_write;
  1948. trans = btrfs_start_transaction(root, 0);
  1949. if (IS_ERR(trans)) {
  1950. err = PTR_ERR(trans);
  1951. goto out_release;
  1952. }
  1953. trans->block_rsv = &block_rsv;
  1954. trans->bytes_reserved = block_rsv.size;
  1955. ret = btrfs_unlink_subvol(trans, root, dir,
  1956. dest->root_key.objectid,
  1957. dentry->d_name.name,
  1958. dentry->d_name.len);
  1959. if (ret) {
  1960. err = ret;
  1961. btrfs_abort_transaction(trans, root, ret);
  1962. goto out_end_trans;
  1963. }
  1964. btrfs_record_root_in_trans(trans, dest);
  1965. memset(&dest->root_item.drop_progress, 0,
  1966. sizeof(dest->root_item.drop_progress));
  1967. dest->root_item.drop_level = 0;
  1968. btrfs_set_root_refs(&dest->root_item, 0);
  1969. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1970. ret = btrfs_insert_orphan_item(trans,
  1971. root->fs_info->tree_root,
  1972. dest->root_key.objectid);
  1973. if (ret) {
  1974. btrfs_abort_transaction(trans, root, ret);
  1975. err = ret;
  1976. goto out_end_trans;
  1977. }
  1978. }
  1979. ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
  1980. dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
  1981. dest->root_key.objectid);
  1982. if (ret && ret != -ENOENT) {
  1983. btrfs_abort_transaction(trans, root, ret);
  1984. err = ret;
  1985. goto out_end_trans;
  1986. }
  1987. if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
  1988. ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
  1989. dest->root_item.received_uuid,
  1990. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  1991. dest->root_key.objectid);
  1992. if (ret && ret != -ENOENT) {
  1993. btrfs_abort_transaction(trans, root, ret);
  1994. err = ret;
  1995. goto out_end_trans;
  1996. }
  1997. }
  1998. out_end_trans:
  1999. trans->block_rsv = NULL;
  2000. trans->bytes_reserved = 0;
  2001. ret = btrfs_end_transaction(trans, root);
  2002. if (ret && !err)
  2003. err = ret;
  2004. inode->i_flags |= S_DEAD;
  2005. out_release:
  2006. btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
  2007. out_up_write:
  2008. up_write(&root->fs_info->subvol_sem);
  2009. out_unlock:
  2010. mutex_unlock(&inode->i_mutex);
  2011. if (!err) {
  2012. shrink_dcache_sb(root->fs_info->sb);
  2013. btrfs_invalidate_inodes(dest);
  2014. d_delete(dentry);
  2015. /* the last ref */
  2016. if (dest->cache_inode) {
  2017. iput(dest->cache_inode);
  2018. dest->cache_inode = NULL;
  2019. }
  2020. }
  2021. out_dput:
  2022. dput(dentry);
  2023. out_unlock_dir:
  2024. mutex_unlock(&dir->i_mutex);
  2025. out_drop_write:
  2026. mnt_drop_write_file(file);
  2027. out:
  2028. kfree(vol_args);
  2029. return err;
  2030. }
  2031. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  2032. {
  2033. struct inode *inode = file_inode(file);
  2034. struct btrfs_root *root = BTRFS_I(inode)->root;
  2035. struct btrfs_ioctl_defrag_range_args *range;
  2036. int ret;
  2037. ret = mnt_want_write_file(file);
  2038. if (ret)
  2039. return ret;
  2040. if (btrfs_root_readonly(root)) {
  2041. ret = -EROFS;
  2042. goto out;
  2043. }
  2044. switch (inode->i_mode & S_IFMT) {
  2045. case S_IFDIR:
  2046. if (!capable(CAP_SYS_ADMIN)) {
  2047. ret = -EPERM;
  2048. goto out;
  2049. }
  2050. ret = btrfs_defrag_root(root);
  2051. if (ret)
  2052. goto out;
  2053. ret = btrfs_defrag_root(root->fs_info->extent_root);
  2054. break;
  2055. case S_IFREG:
  2056. if (!(file->f_mode & FMODE_WRITE)) {
  2057. ret = -EINVAL;
  2058. goto out;
  2059. }
  2060. range = kzalloc(sizeof(*range), GFP_KERNEL);
  2061. if (!range) {
  2062. ret = -ENOMEM;
  2063. goto out;
  2064. }
  2065. if (argp) {
  2066. if (copy_from_user(range, argp,
  2067. sizeof(*range))) {
  2068. ret = -EFAULT;
  2069. kfree(range);
  2070. goto out;
  2071. }
  2072. /* compression requires us to start the IO */
  2073. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  2074. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  2075. range->extent_thresh = (u32)-1;
  2076. }
  2077. } else {
  2078. /* the rest are all set to zero by kzalloc */
  2079. range->len = (u64)-1;
  2080. }
  2081. ret = btrfs_defrag_file(file_inode(file), file,
  2082. range, 0, 0);
  2083. if (ret > 0)
  2084. ret = 0;
  2085. kfree(range);
  2086. break;
  2087. default:
  2088. ret = -EINVAL;
  2089. }
  2090. out:
  2091. mnt_drop_write_file(file);
  2092. return ret;
  2093. }
  2094. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  2095. {
  2096. struct btrfs_ioctl_vol_args *vol_args;
  2097. int ret;
  2098. if (!capable(CAP_SYS_ADMIN))
  2099. return -EPERM;
  2100. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2101. 1)) {
  2102. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2103. }
  2104. mutex_lock(&root->fs_info->volume_mutex);
  2105. vol_args = memdup_user(arg, sizeof(*vol_args));
  2106. if (IS_ERR(vol_args)) {
  2107. ret = PTR_ERR(vol_args);
  2108. goto out;
  2109. }
  2110. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2111. ret = btrfs_init_new_device(root, vol_args->name);
  2112. kfree(vol_args);
  2113. out:
  2114. mutex_unlock(&root->fs_info->volume_mutex);
  2115. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2116. return ret;
  2117. }
  2118. static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
  2119. {
  2120. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  2121. struct btrfs_ioctl_vol_args *vol_args;
  2122. int ret;
  2123. if (!capable(CAP_SYS_ADMIN))
  2124. return -EPERM;
  2125. ret = mnt_want_write_file(file);
  2126. if (ret)
  2127. return ret;
  2128. vol_args = memdup_user(arg, sizeof(*vol_args));
  2129. if (IS_ERR(vol_args)) {
  2130. ret = PTR_ERR(vol_args);
  2131. goto out;
  2132. }
  2133. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2134. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2135. 1)) {
  2136. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2137. goto out;
  2138. }
  2139. mutex_lock(&root->fs_info->volume_mutex);
  2140. ret = btrfs_rm_device(root, vol_args->name);
  2141. mutex_unlock(&root->fs_info->volume_mutex);
  2142. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2143. out:
  2144. kfree(vol_args);
  2145. mnt_drop_write_file(file);
  2146. return ret;
  2147. }
  2148. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  2149. {
  2150. struct btrfs_ioctl_fs_info_args *fi_args;
  2151. struct btrfs_device *device;
  2152. struct btrfs_device *next;
  2153. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2154. int ret = 0;
  2155. if (!capable(CAP_SYS_ADMIN))
  2156. return -EPERM;
  2157. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  2158. if (!fi_args)
  2159. return -ENOMEM;
  2160. mutex_lock(&fs_devices->device_list_mutex);
  2161. fi_args->num_devices = fs_devices->num_devices;
  2162. memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
  2163. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  2164. if (device->devid > fi_args->max_id)
  2165. fi_args->max_id = device->devid;
  2166. }
  2167. mutex_unlock(&fs_devices->device_list_mutex);
  2168. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  2169. ret = -EFAULT;
  2170. kfree(fi_args);
  2171. return ret;
  2172. }
  2173. static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
  2174. {
  2175. struct btrfs_ioctl_dev_info_args *di_args;
  2176. struct btrfs_device *dev;
  2177. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2178. int ret = 0;
  2179. char *s_uuid = NULL;
  2180. if (!capable(CAP_SYS_ADMIN))
  2181. return -EPERM;
  2182. di_args = memdup_user(arg, sizeof(*di_args));
  2183. if (IS_ERR(di_args))
  2184. return PTR_ERR(di_args);
  2185. if (!btrfs_is_empty_uuid(di_args->uuid))
  2186. s_uuid = di_args->uuid;
  2187. mutex_lock(&fs_devices->device_list_mutex);
  2188. dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
  2189. if (!dev) {
  2190. ret = -ENODEV;
  2191. goto out;
  2192. }
  2193. di_args->devid = dev->devid;
  2194. di_args->bytes_used = dev->bytes_used;
  2195. di_args->total_bytes = dev->total_bytes;
  2196. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  2197. if (dev->name) {
  2198. struct rcu_string *name;
  2199. rcu_read_lock();
  2200. name = rcu_dereference(dev->name);
  2201. strncpy(di_args->path, name->str, sizeof(di_args->path));
  2202. rcu_read_unlock();
  2203. di_args->path[sizeof(di_args->path) - 1] = 0;
  2204. } else {
  2205. di_args->path[0] = '\0';
  2206. }
  2207. out:
  2208. mutex_unlock(&fs_devices->device_list_mutex);
  2209. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  2210. ret = -EFAULT;
  2211. kfree(di_args);
  2212. return ret;
  2213. }
  2214. static struct page *extent_same_get_page(struct inode *inode, u64 off)
  2215. {
  2216. struct page *page;
  2217. pgoff_t index;
  2218. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  2219. index = off >> PAGE_CACHE_SHIFT;
  2220. page = grab_cache_page(inode->i_mapping, index);
  2221. if (!page)
  2222. return NULL;
  2223. if (!PageUptodate(page)) {
  2224. if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
  2225. 0))
  2226. return NULL;
  2227. lock_page(page);
  2228. if (!PageUptodate(page)) {
  2229. unlock_page(page);
  2230. page_cache_release(page);
  2231. return NULL;
  2232. }
  2233. }
  2234. unlock_page(page);
  2235. return page;
  2236. }
  2237. static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
  2238. {
  2239. /* do any pending delalloc/csum calc on src, one way or
  2240. another, and lock file content */
  2241. while (1) {
  2242. struct btrfs_ordered_extent *ordered;
  2243. lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2244. ordered = btrfs_lookup_first_ordered_extent(inode,
  2245. off + len - 1);
  2246. if (!ordered &&
  2247. !test_range_bit(&BTRFS_I(inode)->io_tree, off,
  2248. off + len - 1, EXTENT_DELALLOC, 0, NULL))
  2249. break;
  2250. unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2251. if (ordered)
  2252. btrfs_put_ordered_extent(ordered);
  2253. btrfs_wait_ordered_range(inode, off, len);
  2254. }
  2255. }
  2256. static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
  2257. struct inode *inode2, u64 loff2, u64 len)
  2258. {
  2259. unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
  2260. unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
  2261. mutex_unlock(&inode1->i_mutex);
  2262. mutex_unlock(&inode2->i_mutex);
  2263. }
  2264. static void btrfs_double_lock(struct inode *inode1, u64 loff1,
  2265. struct inode *inode2, u64 loff2, u64 len)
  2266. {
  2267. if (inode1 < inode2) {
  2268. swap(inode1, inode2);
  2269. swap(loff1, loff2);
  2270. }
  2271. mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
  2272. lock_extent_range(inode1, loff1, len);
  2273. if (inode1 != inode2) {
  2274. mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
  2275. lock_extent_range(inode2, loff2, len);
  2276. }
  2277. }
  2278. static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
  2279. u64 dst_loff, u64 len)
  2280. {
  2281. int ret = 0;
  2282. struct page *src_page, *dst_page;
  2283. unsigned int cmp_len = PAGE_CACHE_SIZE;
  2284. void *addr, *dst_addr;
  2285. while (len) {
  2286. if (len < PAGE_CACHE_SIZE)
  2287. cmp_len = len;
  2288. src_page = extent_same_get_page(src, loff);
  2289. if (!src_page)
  2290. return -EINVAL;
  2291. dst_page = extent_same_get_page(dst, dst_loff);
  2292. if (!dst_page) {
  2293. page_cache_release(src_page);
  2294. return -EINVAL;
  2295. }
  2296. addr = kmap_atomic(src_page);
  2297. dst_addr = kmap_atomic(dst_page);
  2298. flush_dcache_page(src_page);
  2299. flush_dcache_page(dst_page);
  2300. if (memcmp(addr, dst_addr, cmp_len))
  2301. ret = BTRFS_SAME_DATA_DIFFERS;
  2302. kunmap_atomic(addr);
  2303. kunmap_atomic(dst_addr);
  2304. page_cache_release(src_page);
  2305. page_cache_release(dst_page);
  2306. if (ret)
  2307. break;
  2308. loff += cmp_len;
  2309. dst_loff += cmp_len;
  2310. len -= cmp_len;
  2311. }
  2312. return ret;
  2313. }
  2314. static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
  2315. {
  2316. u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
  2317. if (off + len > inode->i_size || off + len < off)
  2318. return -EINVAL;
  2319. /* Check that we are block aligned - btrfs_clone() requires this */
  2320. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
  2321. return -EINVAL;
  2322. return 0;
  2323. }
  2324. static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
  2325. struct inode *dst, u64 dst_loff)
  2326. {
  2327. int ret;
  2328. /*
  2329. * btrfs_clone() can't handle extents in the same file
  2330. * yet. Once that works, we can drop this check and replace it
  2331. * with a check for the same inode, but overlapping extents.
  2332. */
  2333. if (src == dst)
  2334. return -EINVAL;
  2335. btrfs_double_lock(src, loff, dst, dst_loff, len);
  2336. ret = extent_same_check_offsets(src, loff, len);
  2337. if (ret)
  2338. goto out_unlock;
  2339. ret = extent_same_check_offsets(dst, dst_loff, len);
  2340. if (ret)
  2341. goto out_unlock;
  2342. /* don't make the dst file partly checksummed */
  2343. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  2344. (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
  2345. ret = -EINVAL;
  2346. goto out_unlock;
  2347. }
  2348. ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
  2349. if (ret == 0)
  2350. ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
  2351. out_unlock:
  2352. btrfs_double_unlock(src, loff, dst, dst_loff, len);
  2353. return ret;
  2354. }
  2355. #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
  2356. static long btrfs_ioctl_file_extent_same(struct file *file,
  2357. void __user *argp)
  2358. {
  2359. struct btrfs_ioctl_same_args tmp;
  2360. struct btrfs_ioctl_same_args *same;
  2361. struct btrfs_ioctl_same_extent_info *info;
  2362. struct inode *src = file->f_dentry->d_inode;
  2363. struct file *dst_file = NULL;
  2364. struct inode *dst;
  2365. u64 off;
  2366. u64 len;
  2367. int i;
  2368. int ret;
  2369. unsigned long size;
  2370. u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
  2371. bool is_admin = capable(CAP_SYS_ADMIN);
  2372. if (!(file->f_mode & FMODE_READ))
  2373. return -EINVAL;
  2374. ret = mnt_want_write_file(file);
  2375. if (ret)
  2376. return ret;
  2377. if (copy_from_user(&tmp,
  2378. (struct btrfs_ioctl_same_args __user *)argp,
  2379. sizeof(tmp))) {
  2380. ret = -EFAULT;
  2381. goto out;
  2382. }
  2383. size = sizeof(tmp) +
  2384. tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
  2385. same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
  2386. if (IS_ERR(same)) {
  2387. ret = PTR_ERR(same);
  2388. goto out;
  2389. }
  2390. off = same->logical_offset;
  2391. len = same->length;
  2392. /*
  2393. * Limit the total length we will dedupe for each operation.
  2394. * This is intended to bound the total time spent in this
  2395. * ioctl to something sane.
  2396. */
  2397. if (len > BTRFS_MAX_DEDUPE_LEN)
  2398. len = BTRFS_MAX_DEDUPE_LEN;
  2399. if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
  2400. /*
  2401. * Btrfs does not support blocksize < page_size. As a
  2402. * result, btrfs_cmp_data() won't correctly handle
  2403. * this situation without an update.
  2404. */
  2405. ret = -EINVAL;
  2406. goto out;
  2407. }
  2408. ret = -EISDIR;
  2409. if (S_ISDIR(src->i_mode))
  2410. goto out;
  2411. ret = -EACCES;
  2412. if (!S_ISREG(src->i_mode))
  2413. goto out;
  2414. /* pre-format output fields to sane values */
  2415. for (i = 0; i < same->dest_count; i++) {
  2416. same->info[i].bytes_deduped = 0ULL;
  2417. same->info[i].status = 0;
  2418. }
  2419. ret = 0;
  2420. for (i = 0; i < same->dest_count; i++) {
  2421. info = &same->info[i];
  2422. dst_file = fget(info->fd);
  2423. if (!dst_file) {
  2424. info->status = -EBADF;
  2425. goto next;
  2426. }
  2427. if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
  2428. info->status = -EINVAL;
  2429. goto next;
  2430. }
  2431. info->status = -EXDEV;
  2432. if (file->f_path.mnt != dst_file->f_path.mnt)
  2433. goto next;
  2434. dst = dst_file->f_dentry->d_inode;
  2435. if (src->i_sb != dst->i_sb)
  2436. goto next;
  2437. if (S_ISDIR(dst->i_mode)) {
  2438. info->status = -EISDIR;
  2439. goto next;
  2440. }
  2441. if (!S_ISREG(dst->i_mode)) {
  2442. info->status = -EACCES;
  2443. goto next;
  2444. }
  2445. info->status = btrfs_extent_same(src, off, len, dst,
  2446. info->logical_offset);
  2447. if (info->status == 0)
  2448. info->bytes_deduped += len;
  2449. next:
  2450. if (dst_file)
  2451. fput(dst_file);
  2452. }
  2453. ret = copy_to_user(argp, same, size);
  2454. if (ret)
  2455. ret = -EFAULT;
  2456. out:
  2457. mnt_drop_write_file(file);
  2458. return ret;
  2459. }
  2460. /**
  2461. * btrfs_clone() - clone a range from inode file to another
  2462. *
  2463. * @src: Inode to clone from
  2464. * @inode: Inode to clone to
  2465. * @off: Offset within source to start clone from
  2466. * @olen: Original length, passed by user, of range to clone
  2467. * @olen_aligned: Block-aligned value of olen, extent_same uses
  2468. * identical values here
  2469. * @destoff: Offset within @inode to start clone
  2470. */
  2471. static int btrfs_clone(struct inode *src, struct inode *inode,
  2472. u64 off, u64 olen, u64 olen_aligned, u64 destoff)
  2473. {
  2474. struct btrfs_root *root = BTRFS_I(inode)->root;
  2475. struct btrfs_path *path = NULL;
  2476. struct extent_buffer *leaf;
  2477. struct btrfs_trans_handle *trans;
  2478. char *buf = NULL;
  2479. struct btrfs_key key;
  2480. u32 nritems;
  2481. int slot;
  2482. int ret;
  2483. u64 len = olen_aligned;
  2484. ret = -ENOMEM;
  2485. buf = vmalloc(btrfs_level_size(root, 0));
  2486. if (!buf)
  2487. return ret;
  2488. path = btrfs_alloc_path();
  2489. if (!path) {
  2490. vfree(buf);
  2491. return ret;
  2492. }
  2493. path->reada = 2;
  2494. /* clone data */
  2495. key.objectid = btrfs_ino(src);
  2496. key.type = BTRFS_EXTENT_DATA_KEY;
  2497. key.offset = 0;
  2498. while (1) {
  2499. /*
  2500. * note the key will change type as we walk through the
  2501. * tree.
  2502. */
  2503. path->leave_spinning = 1;
  2504. ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
  2505. 0, 0);
  2506. if (ret < 0)
  2507. goto out;
  2508. nritems = btrfs_header_nritems(path->nodes[0]);
  2509. process_slot:
  2510. if (path->slots[0] >= nritems) {
  2511. ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
  2512. if (ret < 0)
  2513. goto out;
  2514. if (ret > 0)
  2515. break;
  2516. nritems = btrfs_header_nritems(path->nodes[0]);
  2517. }
  2518. leaf = path->nodes[0];
  2519. slot = path->slots[0];
  2520. btrfs_item_key_to_cpu(leaf, &key, slot);
  2521. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  2522. key.objectid != btrfs_ino(src))
  2523. break;
  2524. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  2525. struct btrfs_file_extent_item *extent;
  2526. int type;
  2527. u32 size;
  2528. struct btrfs_key new_key;
  2529. u64 disko = 0, diskl = 0;
  2530. u64 datao = 0, datal = 0;
  2531. u8 comp;
  2532. u64 endoff;
  2533. extent = btrfs_item_ptr(leaf, slot,
  2534. struct btrfs_file_extent_item);
  2535. comp = btrfs_file_extent_compression(leaf, extent);
  2536. type = btrfs_file_extent_type(leaf, extent);
  2537. if (type == BTRFS_FILE_EXTENT_REG ||
  2538. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2539. disko = btrfs_file_extent_disk_bytenr(leaf,
  2540. extent);
  2541. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  2542. extent);
  2543. datao = btrfs_file_extent_offset(leaf, extent);
  2544. datal = btrfs_file_extent_num_bytes(leaf,
  2545. extent);
  2546. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2547. /* take upper bound, may be compressed */
  2548. datal = btrfs_file_extent_ram_bytes(leaf,
  2549. extent);
  2550. }
  2551. if (key.offset + datal <= off ||
  2552. key.offset >= off + len - 1) {
  2553. path->slots[0]++;
  2554. goto process_slot;
  2555. }
  2556. size = btrfs_item_size_nr(leaf, slot);
  2557. read_extent_buffer(leaf, buf,
  2558. btrfs_item_ptr_offset(leaf, slot),
  2559. size);
  2560. btrfs_release_path(path);
  2561. path->leave_spinning = 0;
  2562. memcpy(&new_key, &key, sizeof(new_key));
  2563. new_key.objectid = btrfs_ino(inode);
  2564. if (off <= key.offset)
  2565. new_key.offset = key.offset + destoff - off;
  2566. else
  2567. new_key.offset = destoff;
  2568. /*
  2569. * 1 - adjusting old extent (we may have to split it)
  2570. * 1 - add new extent
  2571. * 1 - inode update
  2572. */
  2573. trans = btrfs_start_transaction(root, 3);
  2574. if (IS_ERR(trans)) {
  2575. ret = PTR_ERR(trans);
  2576. goto out;
  2577. }
  2578. if (type == BTRFS_FILE_EXTENT_REG ||
  2579. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2580. /*
  2581. * a | --- range to clone ---| b
  2582. * | ------------- extent ------------- |
  2583. */
  2584. /* substract range b */
  2585. if (key.offset + datal > off + len)
  2586. datal = off + len - key.offset;
  2587. /* substract range a */
  2588. if (off > key.offset) {
  2589. datao += off - key.offset;
  2590. datal -= off - key.offset;
  2591. }
  2592. ret = btrfs_drop_extents(trans, root, inode,
  2593. new_key.offset,
  2594. new_key.offset + datal,
  2595. 1);
  2596. if (ret) {
  2597. btrfs_abort_transaction(trans, root,
  2598. ret);
  2599. btrfs_end_transaction(trans, root);
  2600. goto out;
  2601. }
  2602. ret = btrfs_insert_empty_item(trans, root, path,
  2603. &new_key, size);
  2604. if (ret) {
  2605. btrfs_abort_transaction(trans, root,
  2606. ret);
  2607. btrfs_end_transaction(trans, root);
  2608. goto out;
  2609. }
  2610. leaf = path->nodes[0];
  2611. slot = path->slots[0];
  2612. write_extent_buffer(leaf, buf,
  2613. btrfs_item_ptr_offset(leaf, slot),
  2614. size);
  2615. extent = btrfs_item_ptr(leaf, slot,
  2616. struct btrfs_file_extent_item);
  2617. /* disko == 0 means it's a hole */
  2618. if (!disko)
  2619. datao = 0;
  2620. btrfs_set_file_extent_offset(leaf, extent,
  2621. datao);
  2622. btrfs_set_file_extent_num_bytes(leaf, extent,
  2623. datal);
  2624. if (disko) {
  2625. inode_add_bytes(inode, datal);
  2626. ret = btrfs_inc_extent_ref(trans, root,
  2627. disko, diskl, 0,
  2628. root->root_key.objectid,
  2629. btrfs_ino(inode),
  2630. new_key.offset - datao,
  2631. 0);
  2632. if (ret) {
  2633. btrfs_abort_transaction(trans,
  2634. root,
  2635. ret);
  2636. btrfs_end_transaction(trans,
  2637. root);
  2638. goto out;
  2639. }
  2640. }
  2641. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2642. u64 skip = 0;
  2643. u64 trim = 0;
  2644. if (off > key.offset) {
  2645. skip = off - key.offset;
  2646. new_key.offset += skip;
  2647. }
  2648. if (key.offset + datal > off + len)
  2649. trim = key.offset + datal - (off + len);
  2650. if (comp && (skip || trim)) {
  2651. ret = -EINVAL;
  2652. btrfs_end_transaction(trans, root);
  2653. goto out;
  2654. }
  2655. size -= skip + trim;
  2656. datal -= skip + trim;
  2657. ret = btrfs_drop_extents(trans, root, inode,
  2658. new_key.offset,
  2659. new_key.offset + datal,
  2660. 1);
  2661. if (ret) {
  2662. btrfs_abort_transaction(trans, root,
  2663. ret);
  2664. btrfs_end_transaction(trans, root);
  2665. goto out;
  2666. }
  2667. ret = btrfs_insert_empty_item(trans, root, path,
  2668. &new_key, size);
  2669. if (ret) {
  2670. btrfs_abort_transaction(trans, root,
  2671. ret);
  2672. btrfs_end_transaction(trans, root);
  2673. goto out;
  2674. }
  2675. if (skip) {
  2676. u32 start =
  2677. btrfs_file_extent_calc_inline_size(0);
  2678. memmove(buf+start, buf+start+skip,
  2679. datal);
  2680. }
  2681. leaf = path->nodes[0];
  2682. slot = path->slots[0];
  2683. write_extent_buffer(leaf, buf,
  2684. btrfs_item_ptr_offset(leaf, slot),
  2685. size);
  2686. inode_add_bytes(inode, datal);
  2687. }
  2688. btrfs_mark_buffer_dirty(leaf);
  2689. btrfs_release_path(path);
  2690. inode_inc_iversion(inode);
  2691. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2692. /*
  2693. * we round up to the block size at eof when
  2694. * determining which extents to clone above,
  2695. * but shouldn't round up the file size
  2696. */
  2697. endoff = new_key.offset + datal;
  2698. if (endoff > destoff+olen)
  2699. endoff = destoff+olen;
  2700. if (endoff > inode->i_size)
  2701. btrfs_i_size_write(inode, endoff);
  2702. ret = btrfs_update_inode(trans, root, inode);
  2703. if (ret) {
  2704. btrfs_abort_transaction(trans, root, ret);
  2705. btrfs_end_transaction(trans, root);
  2706. goto out;
  2707. }
  2708. ret = btrfs_end_transaction(trans, root);
  2709. }
  2710. btrfs_release_path(path);
  2711. key.offset++;
  2712. }
  2713. ret = 0;
  2714. out:
  2715. btrfs_release_path(path);
  2716. btrfs_free_path(path);
  2717. vfree(buf);
  2718. return ret;
  2719. }
  2720. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  2721. u64 off, u64 olen, u64 destoff)
  2722. {
  2723. struct inode *inode = file_inode(file);
  2724. struct btrfs_root *root = BTRFS_I(inode)->root;
  2725. struct fd src_file;
  2726. struct inode *src;
  2727. int ret;
  2728. u64 len = olen;
  2729. u64 bs = root->fs_info->sb->s_blocksize;
  2730. int same_inode = 0;
  2731. /*
  2732. * TODO:
  2733. * - split compressed inline extents. annoying: we need to
  2734. * decompress into destination's address_space (the file offset
  2735. * may change, so source mapping won't do), then recompress (or
  2736. * otherwise reinsert) a subrange.
  2737. * - allow ranges within the same file to be cloned (provided
  2738. * they don't overlap)?
  2739. */
  2740. /* the destination must be opened for writing */
  2741. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  2742. return -EINVAL;
  2743. if (btrfs_root_readonly(root))
  2744. return -EROFS;
  2745. ret = mnt_want_write_file(file);
  2746. if (ret)
  2747. return ret;
  2748. src_file = fdget(srcfd);
  2749. if (!src_file.file) {
  2750. ret = -EBADF;
  2751. goto out_drop_write;
  2752. }
  2753. ret = -EXDEV;
  2754. if (src_file.file->f_path.mnt != file->f_path.mnt)
  2755. goto out_fput;
  2756. src = file_inode(src_file.file);
  2757. ret = -EINVAL;
  2758. if (src == inode)
  2759. same_inode = 1;
  2760. /* the src must be open for reading */
  2761. if (!(src_file.file->f_mode & FMODE_READ))
  2762. goto out_fput;
  2763. /* don't make the dst file partly checksummed */
  2764. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  2765. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
  2766. goto out_fput;
  2767. ret = -EISDIR;
  2768. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  2769. goto out_fput;
  2770. ret = -EXDEV;
  2771. if (src->i_sb != inode->i_sb)
  2772. goto out_fput;
  2773. if (!same_inode) {
  2774. if (inode < src) {
  2775. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  2776. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  2777. } else {
  2778. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  2779. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  2780. }
  2781. } else {
  2782. mutex_lock(&src->i_mutex);
  2783. }
  2784. /* determine range to clone */
  2785. ret = -EINVAL;
  2786. if (off + len > src->i_size || off + len < off)
  2787. goto out_unlock;
  2788. if (len == 0)
  2789. olen = len = src->i_size - off;
  2790. /* if we extend to eof, continue to block boundary */
  2791. if (off + len == src->i_size)
  2792. len = ALIGN(src->i_size, bs) - off;
  2793. /* verify the end result is block aligned */
  2794. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  2795. !IS_ALIGNED(destoff, bs))
  2796. goto out_unlock;
  2797. /* verify if ranges are overlapped within the same file */
  2798. if (same_inode) {
  2799. if (destoff + len > off && destoff < off + len)
  2800. goto out_unlock;
  2801. }
  2802. if (destoff > inode->i_size) {
  2803. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  2804. if (ret)
  2805. goto out_unlock;
  2806. }
  2807. /* truncate page cache pages from target inode range */
  2808. truncate_inode_pages_range(&inode->i_data, destoff,
  2809. PAGE_CACHE_ALIGN(destoff + len) - 1);
  2810. lock_extent_range(src, off, len);
  2811. ret = btrfs_clone(src, inode, off, olen, len, destoff);
  2812. unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  2813. out_unlock:
  2814. if (!same_inode) {
  2815. if (inode < src) {
  2816. mutex_unlock(&src->i_mutex);
  2817. mutex_unlock(&inode->i_mutex);
  2818. } else {
  2819. mutex_unlock(&inode->i_mutex);
  2820. mutex_unlock(&src->i_mutex);
  2821. }
  2822. } else {
  2823. mutex_unlock(&src->i_mutex);
  2824. }
  2825. out_fput:
  2826. fdput(src_file);
  2827. out_drop_write:
  2828. mnt_drop_write_file(file);
  2829. return ret;
  2830. }
  2831. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  2832. {
  2833. struct btrfs_ioctl_clone_range_args args;
  2834. if (copy_from_user(&args, argp, sizeof(args)))
  2835. return -EFAULT;
  2836. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  2837. args.src_length, args.dest_offset);
  2838. }
  2839. /*
  2840. * there are many ways the trans_start and trans_end ioctls can lead
  2841. * to deadlocks. They should only be used by applications that
  2842. * basically own the machine, and have a very in depth understanding
  2843. * of all the possible deadlocks and enospc problems.
  2844. */
  2845. static long btrfs_ioctl_trans_start(struct file *file)
  2846. {
  2847. struct inode *inode = file_inode(file);
  2848. struct btrfs_root *root = BTRFS_I(inode)->root;
  2849. struct btrfs_trans_handle *trans;
  2850. int ret;
  2851. ret = -EPERM;
  2852. if (!capable(CAP_SYS_ADMIN))
  2853. goto out;
  2854. ret = -EINPROGRESS;
  2855. if (file->private_data)
  2856. goto out;
  2857. ret = -EROFS;
  2858. if (btrfs_root_readonly(root))
  2859. goto out;
  2860. ret = mnt_want_write_file(file);
  2861. if (ret)
  2862. goto out;
  2863. atomic_inc(&root->fs_info->open_ioctl_trans);
  2864. ret = -ENOMEM;
  2865. trans = btrfs_start_ioctl_transaction(root);
  2866. if (IS_ERR(trans))
  2867. goto out_drop;
  2868. file->private_data = trans;
  2869. return 0;
  2870. out_drop:
  2871. atomic_dec(&root->fs_info->open_ioctl_trans);
  2872. mnt_drop_write_file(file);
  2873. out:
  2874. return ret;
  2875. }
  2876. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  2877. {
  2878. struct inode *inode = file_inode(file);
  2879. struct btrfs_root *root = BTRFS_I(inode)->root;
  2880. struct btrfs_root *new_root;
  2881. struct btrfs_dir_item *di;
  2882. struct btrfs_trans_handle *trans;
  2883. struct btrfs_path *path;
  2884. struct btrfs_key location;
  2885. struct btrfs_disk_key disk_key;
  2886. u64 objectid = 0;
  2887. u64 dir_id;
  2888. int ret;
  2889. if (!capable(CAP_SYS_ADMIN))
  2890. return -EPERM;
  2891. ret = mnt_want_write_file(file);
  2892. if (ret)
  2893. return ret;
  2894. if (copy_from_user(&objectid, argp, sizeof(objectid))) {
  2895. ret = -EFAULT;
  2896. goto out;
  2897. }
  2898. if (!objectid)
  2899. objectid = BTRFS_FS_TREE_OBJECTID;
  2900. location.objectid = objectid;
  2901. location.type = BTRFS_ROOT_ITEM_KEY;
  2902. location.offset = (u64)-1;
  2903. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  2904. if (IS_ERR(new_root)) {
  2905. ret = PTR_ERR(new_root);
  2906. goto out;
  2907. }
  2908. path = btrfs_alloc_path();
  2909. if (!path) {
  2910. ret = -ENOMEM;
  2911. goto out;
  2912. }
  2913. path->leave_spinning = 1;
  2914. trans = btrfs_start_transaction(root, 1);
  2915. if (IS_ERR(trans)) {
  2916. btrfs_free_path(path);
  2917. ret = PTR_ERR(trans);
  2918. goto out;
  2919. }
  2920. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  2921. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  2922. dir_id, "default", 7, 1);
  2923. if (IS_ERR_OR_NULL(di)) {
  2924. btrfs_free_path(path);
  2925. btrfs_end_transaction(trans, root);
  2926. btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
  2927. "item, this isn't going to work");
  2928. ret = -ENOENT;
  2929. goto out;
  2930. }
  2931. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  2932. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  2933. btrfs_mark_buffer_dirty(path->nodes[0]);
  2934. btrfs_free_path(path);
  2935. btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
  2936. btrfs_end_transaction(trans, root);
  2937. out:
  2938. mnt_drop_write_file(file);
  2939. return ret;
  2940. }
  2941. void btrfs_get_block_group_info(struct list_head *groups_list,
  2942. struct btrfs_ioctl_space_info *space)
  2943. {
  2944. struct btrfs_block_group_cache *block_group;
  2945. space->total_bytes = 0;
  2946. space->used_bytes = 0;
  2947. space->flags = 0;
  2948. list_for_each_entry(block_group, groups_list, list) {
  2949. space->flags = block_group->flags;
  2950. space->total_bytes += block_group->key.offset;
  2951. space->used_bytes +=
  2952. btrfs_block_group_used(&block_group->item);
  2953. }
  2954. }
  2955. static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  2956. {
  2957. struct btrfs_ioctl_space_args space_args;
  2958. struct btrfs_ioctl_space_info space;
  2959. struct btrfs_ioctl_space_info *dest;
  2960. struct btrfs_ioctl_space_info *dest_orig;
  2961. struct btrfs_ioctl_space_info __user *user_dest;
  2962. struct btrfs_space_info *info;
  2963. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  2964. BTRFS_BLOCK_GROUP_SYSTEM,
  2965. BTRFS_BLOCK_GROUP_METADATA,
  2966. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  2967. int num_types = 4;
  2968. int alloc_size;
  2969. int ret = 0;
  2970. u64 slot_count = 0;
  2971. int i, c;
  2972. if (copy_from_user(&space_args,
  2973. (struct btrfs_ioctl_space_args __user *)arg,
  2974. sizeof(space_args)))
  2975. return -EFAULT;
  2976. for (i = 0; i < num_types; i++) {
  2977. struct btrfs_space_info *tmp;
  2978. info = NULL;
  2979. rcu_read_lock();
  2980. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2981. list) {
  2982. if (tmp->flags == types[i]) {
  2983. info = tmp;
  2984. break;
  2985. }
  2986. }
  2987. rcu_read_unlock();
  2988. if (!info)
  2989. continue;
  2990. down_read(&info->groups_sem);
  2991. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2992. if (!list_empty(&info->block_groups[c]))
  2993. slot_count++;
  2994. }
  2995. up_read(&info->groups_sem);
  2996. }
  2997. /* space_slots == 0 means they are asking for a count */
  2998. if (space_args.space_slots == 0) {
  2999. space_args.total_spaces = slot_count;
  3000. goto out;
  3001. }
  3002. slot_count = min_t(u64, space_args.space_slots, slot_count);
  3003. alloc_size = sizeof(*dest) * slot_count;
  3004. /* we generally have at most 6 or so space infos, one for each raid
  3005. * level. So, a whole page should be more than enough for everyone
  3006. */
  3007. if (alloc_size > PAGE_CACHE_SIZE)
  3008. return -ENOMEM;
  3009. space_args.total_spaces = 0;
  3010. dest = kmalloc(alloc_size, GFP_NOFS);
  3011. if (!dest)
  3012. return -ENOMEM;
  3013. dest_orig = dest;
  3014. /* now we have a buffer to copy into */
  3015. for (i = 0; i < num_types; i++) {
  3016. struct btrfs_space_info *tmp;
  3017. if (!slot_count)
  3018. break;
  3019. info = NULL;
  3020. rcu_read_lock();
  3021. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  3022. list) {
  3023. if (tmp->flags == types[i]) {
  3024. info = tmp;
  3025. break;
  3026. }
  3027. }
  3028. rcu_read_unlock();
  3029. if (!info)
  3030. continue;
  3031. down_read(&info->groups_sem);
  3032. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  3033. if (!list_empty(&info->block_groups[c])) {
  3034. btrfs_get_block_group_info(
  3035. &info->block_groups[c], &space);
  3036. memcpy(dest, &space, sizeof(space));
  3037. dest++;
  3038. space_args.total_spaces++;
  3039. slot_count--;
  3040. }
  3041. if (!slot_count)
  3042. break;
  3043. }
  3044. up_read(&info->groups_sem);
  3045. }
  3046. user_dest = (struct btrfs_ioctl_space_info __user *)
  3047. (arg + sizeof(struct btrfs_ioctl_space_args));
  3048. if (copy_to_user(user_dest, dest_orig, alloc_size))
  3049. ret = -EFAULT;
  3050. kfree(dest_orig);
  3051. out:
  3052. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  3053. ret = -EFAULT;
  3054. return ret;
  3055. }
  3056. static long btrfs_ioctl_global_rsv(struct btrfs_root *root, void __user *arg)
  3057. {
  3058. struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
  3059. u64 reserved;
  3060. spin_lock(&block_rsv->lock);
  3061. reserved = block_rsv->reserved;
  3062. spin_unlock(&block_rsv->lock);
  3063. if (arg && copy_to_user(arg, &reserved, sizeof(reserved)))
  3064. return -EFAULT;
  3065. return 0;
  3066. }
  3067. /*
  3068. * there are many ways the trans_start and trans_end ioctls can lead
  3069. * to deadlocks. They should only be used by applications that
  3070. * basically own the machine, and have a very in depth understanding
  3071. * of all the possible deadlocks and enospc problems.
  3072. */
  3073. long btrfs_ioctl_trans_end(struct file *file)
  3074. {
  3075. struct inode *inode = file_inode(file);
  3076. struct btrfs_root *root = BTRFS_I(inode)->root;
  3077. struct btrfs_trans_handle *trans;
  3078. trans = file->private_data;
  3079. if (!trans)
  3080. return -EINVAL;
  3081. file->private_data = NULL;
  3082. btrfs_end_transaction(trans, root);
  3083. atomic_dec(&root->fs_info->open_ioctl_trans);
  3084. mnt_drop_write_file(file);
  3085. return 0;
  3086. }
  3087. static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  3088. void __user *argp)
  3089. {
  3090. struct btrfs_trans_handle *trans;
  3091. u64 transid;
  3092. int ret;
  3093. trans = btrfs_attach_transaction_barrier(root);
  3094. if (IS_ERR(trans)) {
  3095. if (PTR_ERR(trans) != -ENOENT)
  3096. return PTR_ERR(trans);
  3097. /* No running transaction, don't bother */
  3098. transid = root->fs_info->last_trans_committed;
  3099. goto out;
  3100. }
  3101. transid = trans->transid;
  3102. ret = btrfs_commit_transaction_async(trans, root, 0);
  3103. if (ret) {
  3104. btrfs_end_transaction(trans, root);
  3105. return ret;
  3106. }
  3107. out:
  3108. if (argp)
  3109. if (copy_to_user(argp, &transid, sizeof(transid)))
  3110. return -EFAULT;
  3111. return 0;
  3112. }
  3113. static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
  3114. void __user *argp)
  3115. {
  3116. u64 transid;
  3117. if (argp) {
  3118. if (copy_from_user(&transid, argp, sizeof(transid)))
  3119. return -EFAULT;
  3120. } else {
  3121. transid = 0; /* current trans */
  3122. }
  3123. return btrfs_wait_for_commit(root, transid);
  3124. }
  3125. static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
  3126. {
  3127. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3128. struct btrfs_ioctl_scrub_args *sa;
  3129. int ret;
  3130. if (!capable(CAP_SYS_ADMIN))
  3131. return -EPERM;
  3132. sa = memdup_user(arg, sizeof(*sa));
  3133. if (IS_ERR(sa))
  3134. return PTR_ERR(sa);
  3135. if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
  3136. ret = mnt_want_write_file(file);
  3137. if (ret)
  3138. goto out;
  3139. }
  3140. ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
  3141. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
  3142. 0);
  3143. if (copy_to_user(arg, sa, sizeof(*sa)))
  3144. ret = -EFAULT;
  3145. if (!(sa->flags & BTRFS_SCRUB_READONLY))
  3146. mnt_drop_write_file(file);
  3147. out:
  3148. kfree(sa);
  3149. return ret;
  3150. }
  3151. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  3152. {
  3153. if (!capable(CAP_SYS_ADMIN))
  3154. return -EPERM;
  3155. return btrfs_scrub_cancel(root->fs_info);
  3156. }
  3157. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  3158. void __user *arg)
  3159. {
  3160. struct btrfs_ioctl_scrub_args *sa;
  3161. int ret;
  3162. if (!capable(CAP_SYS_ADMIN))
  3163. return -EPERM;
  3164. sa = memdup_user(arg, sizeof(*sa));
  3165. if (IS_ERR(sa))
  3166. return PTR_ERR(sa);
  3167. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  3168. if (copy_to_user(arg, sa, sizeof(*sa)))
  3169. ret = -EFAULT;
  3170. kfree(sa);
  3171. return ret;
  3172. }
  3173. static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
  3174. void __user *arg)
  3175. {
  3176. struct btrfs_ioctl_get_dev_stats *sa;
  3177. int ret;
  3178. sa = memdup_user(arg, sizeof(*sa));
  3179. if (IS_ERR(sa))
  3180. return PTR_ERR(sa);
  3181. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  3182. kfree(sa);
  3183. return -EPERM;
  3184. }
  3185. ret = btrfs_get_dev_stats(root, sa);
  3186. if (copy_to_user(arg, sa, sizeof(*sa)))
  3187. ret = -EFAULT;
  3188. kfree(sa);
  3189. return ret;
  3190. }
  3191. static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
  3192. {
  3193. struct btrfs_ioctl_dev_replace_args *p;
  3194. int ret;
  3195. if (!capable(CAP_SYS_ADMIN))
  3196. return -EPERM;
  3197. p = memdup_user(arg, sizeof(*p));
  3198. if (IS_ERR(p))
  3199. return PTR_ERR(p);
  3200. switch (p->cmd) {
  3201. case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
  3202. if (root->fs_info->sb->s_flags & MS_RDONLY) {
  3203. ret = -EROFS;
  3204. goto out;
  3205. }
  3206. if (atomic_xchg(
  3207. &root->fs_info->mutually_exclusive_operation_running,
  3208. 1)) {
  3209. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  3210. } else {
  3211. ret = btrfs_dev_replace_start(root, p);
  3212. atomic_set(
  3213. &root->fs_info->mutually_exclusive_operation_running,
  3214. 0);
  3215. }
  3216. break;
  3217. case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
  3218. btrfs_dev_replace_status(root->fs_info, p);
  3219. ret = 0;
  3220. break;
  3221. case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
  3222. ret = btrfs_dev_replace_cancel(root->fs_info, p);
  3223. break;
  3224. default:
  3225. ret = -EINVAL;
  3226. break;
  3227. }
  3228. if (copy_to_user(arg, p, sizeof(*p)))
  3229. ret = -EFAULT;
  3230. out:
  3231. kfree(p);
  3232. return ret;
  3233. }
  3234. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  3235. {
  3236. int ret = 0;
  3237. int i;
  3238. u64 rel_ptr;
  3239. int size;
  3240. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  3241. struct inode_fs_paths *ipath = NULL;
  3242. struct btrfs_path *path;
  3243. if (!capable(CAP_DAC_READ_SEARCH))
  3244. return -EPERM;
  3245. path = btrfs_alloc_path();
  3246. if (!path) {
  3247. ret = -ENOMEM;
  3248. goto out;
  3249. }
  3250. ipa = memdup_user(arg, sizeof(*ipa));
  3251. if (IS_ERR(ipa)) {
  3252. ret = PTR_ERR(ipa);
  3253. ipa = NULL;
  3254. goto out;
  3255. }
  3256. size = min_t(u32, ipa->size, 4096);
  3257. ipath = init_ipath(size, root, path);
  3258. if (IS_ERR(ipath)) {
  3259. ret = PTR_ERR(ipath);
  3260. ipath = NULL;
  3261. goto out;
  3262. }
  3263. ret = paths_from_inode(ipa->inum, ipath);
  3264. if (ret < 0)
  3265. goto out;
  3266. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  3267. rel_ptr = ipath->fspath->val[i] -
  3268. (u64)(unsigned long)ipath->fspath->val;
  3269. ipath->fspath->val[i] = rel_ptr;
  3270. }
  3271. ret = copy_to_user((void *)(unsigned long)ipa->fspath,
  3272. (void *)(unsigned long)ipath->fspath, size);
  3273. if (ret) {
  3274. ret = -EFAULT;
  3275. goto out;
  3276. }
  3277. out:
  3278. btrfs_free_path(path);
  3279. free_ipath(ipath);
  3280. kfree(ipa);
  3281. return ret;
  3282. }
  3283. static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
  3284. {
  3285. struct btrfs_data_container *inodes = ctx;
  3286. const size_t c = 3 * sizeof(u64);
  3287. if (inodes->bytes_left >= c) {
  3288. inodes->bytes_left -= c;
  3289. inodes->val[inodes->elem_cnt] = inum;
  3290. inodes->val[inodes->elem_cnt + 1] = offset;
  3291. inodes->val[inodes->elem_cnt + 2] = root;
  3292. inodes->elem_cnt += 3;
  3293. } else {
  3294. inodes->bytes_missing += c - inodes->bytes_left;
  3295. inodes->bytes_left = 0;
  3296. inodes->elem_missed += 3;
  3297. }
  3298. return 0;
  3299. }
  3300. static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
  3301. void __user *arg)
  3302. {
  3303. int ret = 0;
  3304. int size;
  3305. struct btrfs_ioctl_logical_ino_args *loi;
  3306. struct btrfs_data_container *inodes = NULL;
  3307. struct btrfs_path *path = NULL;
  3308. if (!capable(CAP_SYS_ADMIN))
  3309. return -EPERM;
  3310. loi = memdup_user(arg, sizeof(*loi));
  3311. if (IS_ERR(loi)) {
  3312. ret = PTR_ERR(loi);
  3313. loi = NULL;
  3314. goto out;
  3315. }
  3316. path = btrfs_alloc_path();
  3317. if (!path) {
  3318. ret = -ENOMEM;
  3319. goto out;
  3320. }
  3321. size = min_t(u32, loi->size, 64 * 1024);
  3322. inodes = init_data_container(size);
  3323. if (IS_ERR(inodes)) {
  3324. ret = PTR_ERR(inodes);
  3325. inodes = NULL;
  3326. goto out;
  3327. }
  3328. ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
  3329. build_ino_list, inodes);
  3330. if (ret == -EINVAL)
  3331. ret = -ENOENT;
  3332. if (ret < 0)
  3333. goto out;
  3334. ret = copy_to_user((void *)(unsigned long)loi->inodes,
  3335. (void *)(unsigned long)inodes, size);
  3336. if (ret)
  3337. ret = -EFAULT;
  3338. out:
  3339. btrfs_free_path(path);
  3340. vfree(inodes);
  3341. kfree(loi);
  3342. return ret;
  3343. }
  3344. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  3345. struct btrfs_ioctl_balance_args *bargs)
  3346. {
  3347. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3348. bargs->flags = bctl->flags;
  3349. if (atomic_read(&fs_info->balance_running))
  3350. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  3351. if (atomic_read(&fs_info->balance_pause_req))
  3352. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  3353. if (atomic_read(&fs_info->balance_cancel_req))
  3354. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  3355. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  3356. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  3357. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  3358. if (lock) {
  3359. spin_lock(&fs_info->balance_lock);
  3360. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  3361. spin_unlock(&fs_info->balance_lock);
  3362. } else {
  3363. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  3364. }
  3365. }
  3366. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  3367. {
  3368. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3369. struct btrfs_fs_info *fs_info = root->fs_info;
  3370. struct btrfs_ioctl_balance_args *bargs;
  3371. struct btrfs_balance_control *bctl;
  3372. bool need_unlock; /* for mut. excl. ops lock */
  3373. int ret;
  3374. if (!capable(CAP_SYS_ADMIN))
  3375. return -EPERM;
  3376. ret = mnt_want_write_file(file);
  3377. if (ret)
  3378. return ret;
  3379. again:
  3380. if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
  3381. mutex_lock(&fs_info->volume_mutex);
  3382. mutex_lock(&fs_info->balance_mutex);
  3383. need_unlock = true;
  3384. goto locked;
  3385. }
  3386. /*
  3387. * mut. excl. ops lock is locked. Three possibilites:
  3388. * (1) some other op is running
  3389. * (2) balance is running
  3390. * (3) balance is paused -- special case (think resume)
  3391. */
  3392. mutex_lock(&fs_info->balance_mutex);
  3393. if (fs_info->balance_ctl) {
  3394. /* this is either (2) or (3) */
  3395. if (!atomic_read(&fs_info->balance_running)) {
  3396. mutex_unlock(&fs_info->balance_mutex);
  3397. if (!mutex_trylock(&fs_info->volume_mutex))
  3398. goto again;
  3399. mutex_lock(&fs_info->balance_mutex);
  3400. if (fs_info->balance_ctl &&
  3401. !atomic_read(&fs_info->balance_running)) {
  3402. /* this is (3) */
  3403. need_unlock = false;
  3404. goto locked;
  3405. }
  3406. mutex_unlock(&fs_info->balance_mutex);
  3407. mutex_unlock(&fs_info->volume_mutex);
  3408. goto again;
  3409. } else {
  3410. /* this is (2) */
  3411. mutex_unlock(&fs_info->balance_mutex);
  3412. ret = -EINPROGRESS;
  3413. goto out;
  3414. }
  3415. } else {
  3416. /* this is (1) */
  3417. mutex_unlock(&fs_info->balance_mutex);
  3418. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  3419. goto out;
  3420. }
  3421. locked:
  3422. BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
  3423. if (arg) {
  3424. bargs = memdup_user(arg, sizeof(*bargs));
  3425. if (IS_ERR(bargs)) {
  3426. ret = PTR_ERR(bargs);
  3427. goto out_unlock;
  3428. }
  3429. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  3430. if (!fs_info->balance_ctl) {
  3431. ret = -ENOTCONN;
  3432. goto out_bargs;
  3433. }
  3434. bctl = fs_info->balance_ctl;
  3435. spin_lock(&fs_info->balance_lock);
  3436. bctl->flags |= BTRFS_BALANCE_RESUME;
  3437. spin_unlock(&fs_info->balance_lock);
  3438. goto do_balance;
  3439. }
  3440. } else {
  3441. bargs = NULL;
  3442. }
  3443. if (fs_info->balance_ctl) {
  3444. ret = -EINPROGRESS;
  3445. goto out_bargs;
  3446. }
  3447. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  3448. if (!bctl) {
  3449. ret = -ENOMEM;
  3450. goto out_bargs;
  3451. }
  3452. bctl->fs_info = fs_info;
  3453. if (arg) {
  3454. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  3455. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  3456. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  3457. bctl->flags = bargs->flags;
  3458. } else {
  3459. /* balance everything - no filters */
  3460. bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
  3461. }
  3462. do_balance:
  3463. /*
  3464. * Ownership of bctl and mutually_exclusive_operation_running
  3465. * goes to to btrfs_balance. bctl is freed in __cancel_balance,
  3466. * or, if restriper was paused all the way until unmount, in
  3467. * free_fs_info. mutually_exclusive_operation_running is
  3468. * cleared in __cancel_balance.
  3469. */
  3470. need_unlock = false;
  3471. ret = btrfs_balance(bctl, bargs);
  3472. if (arg) {
  3473. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3474. ret = -EFAULT;
  3475. }
  3476. out_bargs:
  3477. kfree(bargs);
  3478. out_unlock:
  3479. mutex_unlock(&fs_info->balance_mutex);
  3480. mutex_unlock(&fs_info->volume_mutex);
  3481. if (need_unlock)
  3482. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  3483. out:
  3484. mnt_drop_write_file(file);
  3485. return ret;
  3486. }
  3487. static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
  3488. {
  3489. if (!capable(CAP_SYS_ADMIN))
  3490. return -EPERM;
  3491. switch (cmd) {
  3492. case BTRFS_BALANCE_CTL_PAUSE:
  3493. return btrfs_pause_balance(root->fs_info);
  3494. case BTRFS_BALANCE_CTL_CANCEL:
  3495. return btrfs_cancel_balance(root->fs_info);
  3496. }
  3497. return -EINVAL;
  3498. }
  3499. static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
  3500. void __user *arg)
  3501. {
  3502. struct btrfs_fs_info *fs_info = root->fs_info;
  3503. struct btrfs_ioctl_balance_args *bargs;
  3504. int ret = 0;
  3505. if (!capable(CAP_SYS_ADMIN))
  3506. return -EPERM;
  3507. mutex_lock(&fs_info->balance_mutex);
  3508. if (!fs_info->balance_ctl) {
  3509. ret = -ENOTCONN;
  3510. goto out;
  3511. }
  3512. bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
  3513. if (!bargs) {
  3514. ret = -ENOMEM;
  3515. goto out;
  3516. }
  3517. update_ioctl_balance_args(fs_info, 1, bargs);
  3518. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3519. ret = -EFAULT;
  3520. kfree(bargs);
  3521. out:
  3522. mutex_unlock(&fs_info->balance_mutex);
  3523. return ret;
  3524. }
  3525. static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  3526. {
  3527. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3528. struct btrfs_ioctl_quota_ctl_args *sa;
  3529. struct btrfs_trans_handle *trans = NULL;
  3530. int ret;
  3531. int err;
  3532. if (!capable(CAP_SYS_ADMIN))
  3533. return -EPERM;
  3534. ret = mnt_want_write_file(file);
  3535. if (ret)
  3536. return ret;
  3537. sa = memdup_user(arg, sizeof(*sa));
  3538. if (IS_ERR(sa)) {
  3539. ret = PTR_ERR(sa);
  3540. goto drop_write;
  3541. }
  3542. down_write(&root->fs_info->subvol_sem);
  3543. trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
  3544. if (IS_ERR(trans)) {
  3545. ret = PTR_ERR(trans);
  3546. goto out;
  3547. }
  3548. switch (sa->cmd) {
  3549. case BTRFS_QUOTA_CTL_ENABLE:
  3550. ret = btrfs_quota_enable(trans, root->fs_info);
  3551. break;
  3552. case BTRFS_QUOTA_CTL_DISABLE:
  3553. ret = btrfs_quota_disable(trans, root->fs_info);
  3554. break;
  3555. default:
  3556. ret = -EINVAL;
  3557. break;
  3558. }
  3559. err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
  3560. if (err && !ret)
  3561. ret = err;
  3562. out:
  3563. kfree(sa);
  3564. up_write(&root->fs_info->subvol_sem);
  3565. drop_write:
  3566. mnt_drop_write_file(file);
  3567. return ret;
  3568. }
  3569. static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  3570. {
  3571. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3572. struct btrfs_ioctl_qgroup_assign_args *sa;
  3573. struct btrfs_trans_handle *trans;
  3574. int ret;
  3575. int err;
  3576. if (!capable(CAP_SYS_ADMIN))
  3577. return -EPERM;
  3578. ret = mnt_want_write_file(file);
  3579. if (ret)
  3580. return ret;
  3581. sa = memdup_user(arg, sizeof(*sa));
  3582. if (IS_ERR(sa)) {
  3583. ret = PTR_ERR(sa);
  3584. goto drop_write;
  3585. }
  3586. trans = btrfs_join_transaction(root);
  3587. if (IS_ERR(trans)) {
  3588. ret = PTR_ERR(trans);
  3589. goto out;
  3590. }
  3591. /* FIXME: check if the IDs really exist */
  3592. if (sa->assign) {
  3593. ret = btrfs_add_qgroup_relation(trans, root->fs_info,
  3594. sa->src, sa->dst);
  3595. } else {
  3596. ret = btrfs_del_qgroup_relation(trans, root->fs_info,
  3597. sa->src, sa->dst);
  3598. }
  3599. err = btrfs_end_transaction(trans, root);
  3600. if (err && !ret)
  3601. ret = err;
  3602. out:
  3603. kfree(sa);
  3604. drop_write:
  3605. mnt_drop_write_file(file);
  3606. return ret;
  3607. }
  3608. static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  3609. {
  3610. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3611. struct btrfs_ioctl_qgroup_create_args *sa;
  3612. struct btrfs_trans_handle *trans;
  3613. int ret;
  3614. int err;
  3615. if (!capable(CAP_SYS_ADMIN))
  3616. return -EPERM;
  3617. ret = mnt_want_write_file(file);
  3618. if (ret)
  3619. return ret;
  3620. sa = memdup_user(arg, sizeof(*sa));
  3621. if (IS_ERR(sa)) {
  3622. ret = PTR_ERR(sa);
  3623. goto drop_write;
  3624. }
  3625. if (!sa->qgroupid) {
  3626. ret = -EINVAL;
  3627. goto out;
  3628. }
  3629. trans = btrfs_join_transaction(root);
  3630. if (IS_ERR(trans)) {
  3631. ret = PTR_ERR(trans);
  3632. goto out;
  3633. }
  3634. /* FIXME: check if the IDs really exist */
  3635. if (sa->create) {
  3636. ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
  3637. NULL);
  3638. } else {
  3639. ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
  3640. }
  3641. err = btrfs_end_transaction(trans, root);
  3642. if (err && !ret)
  3643. ret = err;
  3644. out:
  3645. kfree(sa);
  3646. drop_write:
  3647. mnt_drop_write_file(file);
  3648. return ret;
  3649. }
  3650. static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  3651. {
  3652. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3653. struct btrfs_ioctl_qgroup_limit_args *sa;
  3654. struct btrfs_trans_handle *trans;
  3655. int ret;
  3656. int err;
  3657. u64 qgroupid;
  3658. if (!capable(CAP_SYS_ADMIN))
  3659. return -EPERM;
  3660. ret = mnt_want_write_file(file);
  3661. if (ret)
  3662. return ret;
  3663. sa = memdup_user(arg, sizeof(*sa));
  3664. if (IS_ERR(sa)) {
  3665. ret = PTR_ERR(sa);
  3666. goto drop_write;
  3667. }
  3668. trans = btrfs_join_transaction(root);
  3669. if (IS_ERR(trans)) {
  3670. ret = PTR_ERR(trans);
  3671. goto out;
  3672. }
  3673. qgroupid = sa->qgroupid;
  3674. if (!qgroupid) {
  3675. /* take the current subvol as qgroup */
  3676. qgroupid = root->root_key.objectid;
  3677. }
  3678. /* FIXME: check if the IDs really exist */
  3679. ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
  3680. err = btrfs_end_transaction(trans, root);
  3681. if (err && !ret)
  3682. ret = err;
  3683. out:
  3684. kfree(sa);
  3685. drop_write:
  3686. mnt_drop_write_file(file);
  3687. return ret;
  3688. }
  3689. static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
  3690. {
  3691. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3692. struct btrfs_ioctl_quota_rescan_args *qsa;
  3693. int ret;
  3694. if (!capable(CAP_SYS_ADMIN))
  3695. return -EPERM;
  3696. ret = mnt_want_write_file(file);
  3697. if (ret)
  3698. return ret;
  3699. qsa = memdup_user(arg, sizeof(*qsa));
  3700. if (IS_ERR(qsa)) {
  3701. ret = PTR_ERR(qsa);
  3702. goto drop_write;
  3703. }
  3704. if (qsa->flags) {
  3705. ret = -EINVAL;
  3706. goto out;
  3707. }
  3708. ret = btrfs_qgroup_rescan(root->fs_info);
  3709. out:
  3710. kfree(qsa);
  3711. drop_write:
  3712. mnt_drop_write_file(file);
  3713. return ret;
  3714. }
  3715. static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
  3716. {
  3717. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3718. struct btrfs_ioctl_quota_rescan_args *qsa;
  3719. int ret = 0;
  3720. if (!capable(CAP_SYS_ADMIN))
  3721. return -EPERM;
  3722. qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
  3723. if (!qsa)
  3724. return -ENOMEM;
  3725. if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  3726. qsa->flags = 1;
  3727. qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
  3728. }
  3729. if (copy_to_user(arg, qsa, sizeof(*qsa)))
  3730. ret = -EFAULT;
  3731. kfree(qsa);
  3732. return ret;
  3733. }
  3734. static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
  3735. {
  3736. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3737. if (!capable(CAP_SYS_ADMIN))
  3738. return -EPERM;
  3739. return btrfs_qgroup_wait_for_completion(root->fs_info);
  3740. }
  3741. static long btrfs_ioctl_set_received_subvol(struct file *file,
  3742. void __user *arg)
  3743. {
  3744. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  3745. struct inode *inode = file_inode(file);
  3746. struct btrfs_root *root = BTRFS_I(inode)->root;
  3747. struct btrfs_root_item *root_item = &root->root_item;
  3748. struct btrfs_trans_handle *trans;
  3749. struct timespec ct = CURRENT_TIME;
  3750. int ret = 0;
  3751. int received_uuid_changed;
  3752. if (!inode_owner_or_capable(inode))
  3753. return -EPERM;
  3754. ret = mnt_want_write_file(file);
  3755. if (ret < 0)
  3756. return ret;
  3757. down_write(&root->fs_info->subvol_sem);
  3758. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  3759. ret = -EINVAL;
  3760. goto out;
  3761. }
  3762. if (btrfs_root_readonly(root)) {
  3763. ret = -EROFS;
  3764. goto out;
  3765. }
  3766. sa = memdup_user(arg, sizeof(*sa));
  3767. if (IS_ERR(sa)) {
  3768. ret = PTR_ERR(sa);
  3769. sa = NULL;
  3770. goto out;
  3771. }
  3772. /*
  3773. * 1 - root item
  3774. * 2 - uuid items (received uuid + subvol uuid)
  3775. */
  3776. trans = btrfs_start_transaction(root, 3);
  3777. if (IS_ERR(trans)) {
  3778. ret = PTR_ERR(trans);
  3779. trans = NULL;
  3780. goto out;
  3781. }
  3782. sa->rtransid = trans->transid;
  3783. sa->rtime.sec = ct.tv_sec;
  3784. sa->rtime.nsec = ct.tv_nsec;
  3785. received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
  3786. BTRFS_UUID_SIZE);
  3787. if (received_uuid_changed &&
  3788. !btrfs_is_empty_uuid(root_item->received_uuid))
  3789. btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
  3790. root_item->received_uuid,
  3791. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  3792. root->root_key.objectid);
  3793. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  3794. btrfs_set_root_stransid(root_item, sa->stransid);
  3795. btrfs_set_root_rtransid(root_item, sa->rtransid);
  3796. btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
  3797. btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
  3798. btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
  3799. btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
  3800. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  3801. &root->root_key, &root->root_item);
  3802. if (ret < 0) {
  3803. btrfs_end_transaction(trans, root);
  3804. goto out;
  3805. }
  3806. if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
  3807. ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
  3808. sa->uuid,
  3809. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  3810. root->root_key.objectid);
  3811. if (ret < 0 && ret != -EEXIST) {
  3812. btrfs_abort_transaction(trans, root, ret);
  3813. goto out;
  3814. }
  3815. }
  3816. ret = btrfs_commit_transaction(trans, root);
  3817. if (ret < 0) {
  3818. btrfs_abort_transaction(trans, root, ret);
  3819. goto out;
  3820. }
  3821. ret = copy_to_user(arg, sa, sizeof(*sa));
  3822. if (ret)
  3823. ret = -EFAULT;
  3824. out:
  3825. kfree(sa);
  3826. up_write(&root->fs_info->subvol_sem);
  3827. mnt_drop_write_file(file);
  3828. return ret;
  3829. }
  3830. static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
  3831. {
  3832. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3833. size_t len;
  3834. int ret;
  3835. char label[BTRFS_LABEL_SIZE];
  3836. spin_lock(&root->fs_info->super_lock);
  3837. memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
  3838. spin_unlock(&root->fs_info->super_lock);
  3839. len = strnlen(label, BTRFS_LABEL_SIZE);
  3840. if (len == BTRFS_LABEL_SIZE) {
  3841. btrfs_warn(root->fs_info,
  3842. "label is too long, return the first %zu bytes", --len);
  3843. }
  3844. ret = copy_to_user(arg, label, len);
  3845. return ret ? -EFAULT : 0;
  3846. }
  3847. static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
  3848. {
  3849. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3850. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  3851. struct btrfs_trans_handle *trans;
  3852. char label[BTRFS_LABEL_SIZE];
  3853. int ret;
  3854. if (!capable(CAP_SYS_ADMIN))
  3855. return -EPERM;
  3856. if (copy_from_user(label, arg, sizeof(label)))
  3857. return -EFAULT;
  3858. if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
  3859. btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
  3860. BTRFS_LABEL_SIZE - 1);
  3861. return -EINVAL;
  3862. }
  3863. ret = mnt_want_write_file(file);
  3864. if (ret)
  3865. return ret;
  3866. trans = btrfs_start_transaction(root, 0);
  3867. if (IS_ERR(trans)) {
  3868. ret = PTR_ERR(trans);
  3869. goto out_unlock;
  3870. }
  3871. spin_lock(&root->fs_info->super_lock);
  3872. strcpy(super_block->label, label);
  3873. spin_unlock(&root->fs_info->super_lock);
  3874. ret = btrfs_end_transaction(trans, root);
  3875. out_unlock:
  3876. mnt_drop_write_file(file);
  3877. return ret;
  3878. }
  3879. #define INIT_FEATURE_FLAGS(suffix) \
  3880. { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
  3881. .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
  3882. .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
  3883. static int btrfs_ioctl_get_supported_features(struct file *file,
  3884. void __user *arg)
  3885. {
  3886. static struct btrfs_ioctl_feature_flags features[3] = {
  3887. INIT_FEATURE_FLAGS(SUPP),
  3888. INIT_FEATURE_FLAGS(SAFE_SET),
  3889. INIT_FEATURE_FLAGS(SAFE_CLEAR)
  3890. };
  3891. if (copy_to_user(arg, &features, sizeof(features)))
  3892. return -EFAULT;
  3893. return 0;
  3894. }
  3895. static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
  3896. {
  3897. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3898. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  3899. struct btrfs_ioctl_feature_flags features;
  3900. features.compat_flags = btrfs_super_compat_flags(super_block);
  3901. features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
  3902. features.incompat_flags = btrfs_super_incompat_flags(super_block);
  3903. if (copy_to_user(arg, &features, sizeof(features)))
  3904. return -EFAULT;
  3905. return 0;
  3906. }
  3907. static int check_feature_bits(struct btrfs_root *root,
  3908. enum btrfs_feature_set set,
  3909. u64 change_mask, u64 flags, u64 supported_flags,
  3910. u64 safe_set, u64 safe_clear)
  3911. {
  3912. const char *type = btrfs_feature_set_names[set];
  3913. char *names;
  3914. u64 disallowed, unsupported;
  3915. u64 set_mask = flags & change_mask;
  3916. u64 clear_mask = ~flags & change_mask;
  3917. unsupported = set_mask & ~supported_flags;
  3918. if (unsupported) {
  3919. names = btrfs_printable_features(set, unsupported);
  3920. if (names) {
  3921. btrfs_warn(root->fs_info,
  3922. "this kernel does not support the %s feature bit%s",
  3923. names, strchr(names, ',') ? "s" : "");
  3924. kfree(names);
  3925. } else
  3926. btrfs_warn(root->fs_info,
  3927. "this kernel does not support %s bits 0x%llx",
  3928. type, unsupported);
  3929. return -EOPNOTSUPP;
  3930. }
  3931. disallowed = set_mask & ~safe_set;
  3932. if (disallowed) {
  3933. names = btrfs_printable_features(set, disallowed);
  3934. if (names) {
  3935. btrfs_warn(root->fs_info,
  3936. "can't set the %s feature bit%s while mounted",
  3937. names, strchr(names, ',') ? "s" : "");
  3938. kfree(names);
  3939. } else
  3940. btrfs_warn(root->fs_info,
  3941. "can't set %s bits 0x%llx while mounted",
  3942. type, disallowed);
  3943. return -EPERM;
  3944. }
  3945. disallowed = clear_mask & ~safe_clear;
  3946. if (disallowed) {
  3947. names = btrfs_printable_features(set, disallowed);
  3948. if (names) {
  3949. btrfs_warn(root->fs_info,
  3950. "can't clear the %s feature bit%s while mounted",
  3951. names, strchr(names, ',') ? "s" : "");
  3952. kfree(names);
  3953. } else
  3954. btrfs_warn(root->fs_info,
  3955. "can't clear %s bits 0x%llx while mounted",
  3956. type, disallowed);
  3957. return -EPERM;
  3958. }
  3959. return 0;
  3960. }
  3961. #define check_feature(root, change_mask, flags, mask_base) \
  3962. check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
  3963. BTRFS_FEATURE_ ## mask_base ## _SUPP, \
  3964. BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
  3965. BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
  3966. static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
  3967. {
  3968. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3969. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  3970. struct btrfs_ioctl_feature_flags flags[2];
  3971. struct btrfs_trans_handle *trans;
  3972. u64 newflags;
  3973. int ret;
  3974. if (!capable(CAP_SYS_ADMIN))
  3975. return -EPERM;
  3976. if (copy_from_user(flags, arg, sizeof(flags)))
  3977. return -EFAULT;
  3978. /* Nothing to do */
  3979. if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
  3980. !flags[0].incompat_flags)
  3981. return 0;
  3982. ret = check_feature(root, flags[0].compat_flags,
  3983. flags[1].compat_flags, COMPAT);
  3984. if (ret)
  3985. return ret;
  3986. ret = check_feature(root, flags[0].compat_ro_flags,
  3987. flags[1].compat_ro_flags, COMPAT_RO);
  3988. if (ret)
  3989. return ret;
  3990. ret = check_feature(root, flags[0].incompat_flags,
  3991. flags[1].incompat_flags, INCOMPAT);
  3992. if (ret)
  3993. return ret;
  3994. trans = btrfs_start_transaction(root, 1);
  3995. if (IS_ERR(trans))
  3996. return PTR_ERR(trans);
  3997. spin_lock(&root->fs_info->super_lock);
  3998. newflags = btrfs_super_compat_flags(super_block);
  3999. newflags |= flags[0].compat_flags & flags[1].compat_flags;
  4000. newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
  4001. btrfs_set_super_compat_flags(super_block, newflags);
  4002. newflags = btrfs_super_compat_ro_flags(super_block);
  4003. newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
  4004. newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
  4005. btrfs_set_super_compat_ro_flags(super_block, newflags);
  4006. newflags = btrfs_super_incompat_flags(super_block);
  4007. newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
  4008. newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
  4009. btrfs_set_super_incompat_flags(super_block, newflags);
  4010. spin_unlock(&root->fs_info->super_lock);
  4011. return btrfs_end_transaction(trans, root);
  4012. }
  4013. long btrfs_ioctl(struct file *file, unsigned int
  4014. cmd, unsigned long arg)
  4015. {
  4016. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4017. void __user *argp = (void __user *)arg;
  4018. switch (cmd) {
  4019. case FS_IOC_GETFLAGS:
  4020. return btrfs_ioctl_getflags(file, argp);
  4021. case FS_IOC_SETFLAGS:
  4022. return btrfs_ioctl_setflags(file, argp);
  4023. case FS_IOC_GETVERSION:
  4024. return btrfs_ioctl_getversion(file, argp);
  4025. case FITRIM:
  4026. return btrfs_ioctl_fitrim(file, argp);
  4027. case BTRFS_IOC_SNAP_CREATE:
  4028. return btrfs_ioctl_snap_create(file, argp, 0);
  4029. case BTRFS_IOC_SNAP_CREATE_V2:
  4030. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  4031. case BTRFS_IOC_SUBVOL_CREATE:
  4032. return btrfs_ioctl_snap_create(file, argp, 1);
  4033. case BTRFS_IOC_SUBVOL_CREATE_V2:
  4034. return btrfs_ioctl_snap_create_v2(file, argp, 1);
  4035. case BTRFS_IOC_SNAP_DESTROY:
  4036. return btrfs_ioctl_snap_destroy(file, argp);
  4037. case BTRFS_IOC_SUBVOL_GETFLAGS:
  4038. return btrfs_ioctl_subvol_getflags(file, argp);
  4039. case BTRFS_IOC_SUBVOL_SETFLAGS:
  4040. return btrfs_ioctl_subvol_setflags(file, argp);
  4041. case BTRFS_IOC_DEFAULT_SUBVOL:
  4042. return btrfs_ioctl_default_subvol(file, argp);
  4043. case BTRFS_IOC_DEFRAG:
  4044. return btrfs_ioctl_defrag(file, NULL);
  4045. case BTRFS_IOC_DEFRAG_RANGE:
  4046. return btrfs_ioctl_defrag(file, argp);
  4047. case BTRFS_IOC_RESIZE:
  4048. return btrfs_ioctl_resize(file, argp);
  4049. case BTRFS_IOC_ADD_DEV:
  4050. return btrfs_ioctl_add_dev(root, argp);
  4051. case BTRFS_IOC_RM_DEV:
  4052. return btrfs_ioctl_rm_dev(file, argp);
  4053. case BTRFS_IOC_FS_INFO:
  4054. return btrfs_ioctl_fs_info(root, argp);
  4055. case BTRFS_IOC_DEV_INFO:
  4056. return btrfs_ioctl_dev_info(root, argp);
  4057. case BTRFS_IOC_BALANCE:
  4058. return btrfs_ioctl_balance(file, NULL);
  4059. case BTRFS_IOC_CLONE:
  4060. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  4061. case BTRFS_IOC_CLONE_RANGE:
  4062. return btrfs_ioctl_clone_range(file, argp);
  4063. case BTRFS_IOC_TRANS_START:
  4064. return btrfs_ioctl_trans_start(file);
  4065. case BTRFS_IOC_TRANS_END:
  4066. return btrfs_ioctl_trans_end(file);
  4067. case BTRFS_IOC_TREE_SEARCH:
  4068. return btrfs_ioctl_tree_search(file, argp);
  4069. case BTRFS_IOC_INO_LOOKUP:
  4070. return btrfs_ioctl_ino_lookup(file, argp);
  4071. case BTRFS_IOC_INO_PATHS:
  4072. return btrfs_ioctl_ino_to_path(root, argp);
  4073. case BTRFS_IOC_LOGICAL_INO:
  4074. return btrfs_ioctl_logical_to_ino(root, argp);
  4075. case BTRFS_IOC_SPACE_INFO:
  4076. return btrfs_ioctl_space_info(root, argp);
  4077. case BTRFS_IOC_GLOBAL_RSV:
  4078. return btrfs_ioctl_global_rsv(root, argp);
  4079. case BTRFS_IOC_SYNC: {
  4080. int ret;
  4081. ret = btrfs_start_delalloc_roots(root->fs_info, 0);
  4082. if (ret)
  4083. return ret;
  4084. ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
  4085. return ret;
  4086. }
  4087. case BTRFS_IOC_START_SYNC:
  4088. return btrfs_ioctl_start_sync(root, argp);
  4089. case BTRFS_IOC_WAIT_SYNC:
  4090. return btrfs_ioctl_wait_sync(root, argp);
  4091. case BTRFS_IOC_SCRUB:
  4092. return btrfs_ioctl_scrub(file, argp);
  4093. case BTRFS_IOC_SCRUB_CANCEL:
  4094. return btrfs_ioctl_scrub_cancel(root, argp);
  4095. case BTRFS_IOC_SCRUB_PROGRESS:
  4096. return btrfs_ioctl_scrub_progress(root, argp);
  4097. case BTRFS_IOC_BALANCE_V2:
  4098. return btrfs_ioctl_balance(file, argp);
  4099. case BTRFS_IOC_BALANCE_CTL:
  4100. return btrfs_ioctl_balance_ctl(root, arg);
  4101. case BTRFS_IOC_BALANCE_PROGRESS:
  4102. return btrfs_ioctl_balance_progress(root, argp);
  4103. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  4104. return btrfs_ioctl_set_received_subvol(file, argp);
  4105. case BTRFS_IOC_SEND:
  4106. return btrfs_ioctl_send(file, argp);
  4107. case BTRFS_IOC_GET_DEV_STATS:
  4108. return btrfs_ioctl_get_dev_stats(root, argp);
  4109. case BTRFS_IOC_QUOTA_CTL:
  4110. return btrfs_ioctl_quota_ctl(file, argp);
  4111. case BTRFS_IOC_QGROUP_ASSIGN:
  4112. return btrfs_ioctl_qgroup_assign(file, argp);
  4113. case BTRFS_IOC_QGROUP_CREATE:
  4114. return btrfs_ioctl_qgroup_create(file, argp);
  4115. case BTRFS_IOC_QGROUP_LIMIT:
  4116. return btrfs_ioctl_qgroup_limit(file, argp);
  4117. case BTRFS_IOC_QUOTA_RESCAN:
  4118. return btrfs_ioctl_quota_rescan(file, argp);
  4119. case BTRFS_IOC_QUOTA_RESCAN_STATUS:
  4120. return btrfs_ioctl_quota_rescan_status(file, argp);
  4121. case BTRFS_IOC_QUOTA_RESCAN_WAIT:
  4122. return btrfs_ioctl_quota_rescan_wait(file, argp);
  4123. case BTRFS_IOC_DEV_REPLACE:
  4124. return btrfs_ioctl_dev_replace(root, argp);
  4125. case BTRFS_IOC_GET_FSLABEL:
  4126. return btrfs_ioctl_get_fslabel(file, argp);
  4127. case BTRFS_IOC_SET_FSLABEL:
  4128. return btrfs_ioctl_set_fslabel(file, argp);
  4129. case BTRFS_IOC_FILE_EXTENT_SAME:
  4130. return btrfs_ioctl_file_extent_same(file, argp);
  4131. case BTRFS_IOC_GET_SUPPORTED_FEATURES:
  4132. return btrfs_ioctl_get_supported_features(file, argp);
  4133. case BTRFS_IOC_GET_FEATURES:
  4134. return btrfs_ioctl_get_features(file, argp);
  4135. case BTRFS_IOC_SET_FEATURES:
  4136. return btrfs_ioctl_set_features(file, argp);
  4137. }
  4138. return -ENOTTY;
  4139. }