ioctl.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790
  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 "compat.h"
  45. #include "ctree.h"
  46. #include "disk-io.h"
  47. #include "transaction.h"
  48. #include "btrfs_inode.h"
  49. #include "ioctl.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. /* Mask out flags that are inappropriate for the given type of inode. */
  58. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  59. {
  60. if (S_ISDIR(mode))
  61. return flags;
  62. else if (S_ISREG(mode))
  63. return flags & ~FS_DIRSYNC_FL;
  64. else
  65. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  66. }
  67. /*
  68. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  69. */
  70. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  71. {
  72. unsigned int iflags = 0;
  73. if (flags & BTRFS_INODE_SYNC)
  74. iflags |= FS_SYNC_FL;
  75. if (flags & BTRFS_INODE_IMMUTABLE)
  76. iflags |= FS_IMMUTABLE_FL;
  77. if (flags & BTRFS_INODE_APPEND)
  78. iflags |= FS_APPEND_FL;
  79. if (flags & BTRFS_INODE_NODUMP)
  80. iflags |= FS_NODUMP_FL;
  81. if (flags & BTRFS_INODE_NOATIME)
  82. iflags |= FS_NOATIME_FL;
  83. if (flags & BTRFS_INODE_DIRSYNC)
  84. iflags |= FS_DIRSYNC_FL;
  85. if (flags & BTRFS_INODE_NODATACOW)
  86. iflags |= FS_NOCOW_FL;
  87. if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
  88. iflags |= FS_COMPR_FL;
  89. else if (flags & BTRFS_INODE_NOCOMPRESS)
  90. iflags |= FS_NOCOMP_FL;
  91. return iflags;
  92. }
  93. /*
  94. * Update inode->i_flags based on the btrfs internal flags.
  95. */
  96. void btrfs_update_iflags(struct inode *inode)
  97. {
  98. struct btrfs_inode *ip = BTRFS_I(inode);
  99. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  100. if (ip->flags & BTRFS_INODE_SYNC)
  101. inode->i_flags |= S_SYNC;
  102. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  103. inode->i_flags |= S_IMMUTABLE;
  104. if (ip->flags & BTRFS_INODE_APPEND)
  105. inode->i_flags |= S_APPEND;
  106. if (ip->flags & BTRFS_INODE_NOATIME)
  107. inode->i_flags |= S_NOATIME;
  108. if (ip->flags & BTRFS_INODE_DIRSYNC)
  109. inode->i_flags |= S_DIRSYNC;
  110. }
  111. /*
  112. * Inherit flags from the parent inode.
  113. *
  114. * Currently only the compression flags and the cow flags are inherited.
  115. */
  116. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  117. {
  118. unsigned int flags;
  119. if (!dir)
  120. return;
  121. flags = BTRFS_I(dir)->flags;
  122. if (flags & BTRFS_INODE_NOCOMPRESS) {
  123. BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
  124. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  125. } else if (flags & BTRFS_INODE_COMPRESS) {
  126. BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
  127. BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
  128. }
  129. if (flags & BTRFS_INODE_NODATACOW)
  130. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  131. btrfs_update_iflags(inode);
  132. }
  133. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  134. {
  135. struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
  136. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  137. if (copy_to_user(arg, &flags, sizeof(flags)))
  138. return -EFAULT;
  139. return 0;
  140. }
  141. static int check_flags(unsigned int flags)
  142. {
  143. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  144. FS_NOATIME_FL | FS_NODUMP_FL | \
  145. FS_SYNC_FL | FS_DIRSYNC_FL | \
  146. FS_NOCOMP_FL | FS_COMPR_FL |
  147. FS_NOCOW_FL))
  148. return -EOPNOTSUPP;
  149. if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
  150. return -EINVAL;
  151. return 0;
  152. }
  153. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  154. {
  155. struct inode *inode = file->f_path.dentry->d_inode;
  156. struct btrfs_inode *ip = BTRFS_I(inode);
  157. struct btrfs_root *root = ip->root;
  158. struct btrfs_trans_handle *trans;
  159. unsigned int flags, oldflags;
  160. int ret;
  161. u64 ip_oldflags;
  162. unsigned int i_oldflags;
  163. if (btrfs_root_readonly(root))
  164. return -EROFS;
  165. if (copy_from_user(&flags, arg, sizeof(flags)))
  166. return -EFAULT;
  167. ret = check_flags(flags);
  168. if (ret)
  169. return ret;
  170. if (!inode_owner_or_capable(inode))
  171. return -EACCES;
  172. ret = mnt_want_write_file(file);
  173. if (ret)
  174. return ret;
  175. mutex_lock(&inode->i_mutex);
  176. ip_oldflags = ip->flags;
  177. i_oldflags = inode->i_flags;
  178. flags = btrfs_mask_flags(inode->i_mode, flags);
  179. oldflags = btrfs_flags_to_ioctl(ip->flags);
  180. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  181. if (!capable(CAP_LINUX_IMMUTABLE)) {
  182. ret = -EPERM;
  183. goto out_unlock;
  184. }
  185. }
  186. if (flags & FS_SYNC_FL)
  187. ip->flags |= BTRFS_INODE_SYNC;
  188. else
  189. ip->flags &= ~BTRFS_INODE_SYNC;
  190. if (flags & FS_IMMUTABLE_FL)
  191. ip->flags |= BTRFS_INODE_IMMUTABLE;
  192. else
  193. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  194. if (flags & FS_APPEND_FL)
  195. ip->flags |= BTRFS_INODE_APPEND;
  196. else
  197. ip->flags &= ~BTRFS_INODE_APPEND;
  198. if (flags & FS_NODUMP_FL)
  199. ip->flags |= BTRFS_INODE_NODUMP;
  200. else
  201. ip->flags &= ~BTRFS_INODE_NODUMP;
  202. if (flags & FS_NOATIME_FL)
  203. ip->flags |= BTRFS_INODE_NOATIME;
  204. else
  205. ip->flags &= ~BTRFS_INODE_NOATIME;
  206. if (flags & FS_DIRSYNC_FL)
  207. ip->flags |= BTRFS_INODE_DIRSYNC;
  208. else
  209. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  210. if (flags & FS_NOCOW_FL)
  211. ip->flags |= BTRFS_INODE_NODATACOW;
  212. else
  213. ip->flags &= ~BTRFS_INODE_NODATACOW;
  214. /*
  215. * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
  216. * flag may be changed automatically if compression code won't make
  217. * things smaller.
  218. */
  219. if (flags & FS_NOCOMP_FL) {
  220. ip->flags &= ~BTRFS_INODE_COMPRESS;
  221. ip->flags |= BTRFS_INODE_NOCOMPRESS;
  222. } else if (flags & FS_COMPR_FL) {
  223. ip->flags |= BTRFS_INODE_COMPRESS;
  224. ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
  225. } else {
  226. ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
  227. }
  228. trans = btrfs_start_transaction(root, 1);
  229. if (IS_ERR(trans)) {
  230. ret = PTR_ERR(trans);
  231. goto out_drop;
  232. }
  233. btrfs_update_iflags(inode);
  234. inode_inc_iversion(inode);
  235. inode->i_ctime = CURRENT_TIME;
  236. ret = btrfs_update_inode(trans, root, inode);
  237. btrfs_end_transaction(trans, root);
  238. out_drop:
  239. if (ret) {
  240. ip->flags = ip_oldflags;
  241. inode->i_flags = i_oldflags;
  242. }
  243. out_unlock:
  244. mutex_unlock(&inode->i_mutex);
  245. mnt_drop_write_file(file);
  246. return ret;
  247. }
  248. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  249. {
  250. struct inode *inode = file->f_path.dentry->d_inode;
  251. return put_user(inode->i_generation, arg);
  252. }
  253. static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
  254. {
  255. struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
  256. struct btrfs_device *device;
  257. struct request_queue *q;
  258. struct fstrim_range range;
  259. u64 minlen = ULLONG_MAX;
  260. u64 num_devices = 0;
  261. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  262. int ret;
  263. if (!capable(CAP_SYS_ADMIN))
  264. return -EPERM;
  265. rcu_read_lock();
  266. list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
  267. dev_list) {
  268. if (!device->bdev)
  269. continue;
  270. q = bdev_get_queue(device->bdev);
  271. if (blk_queue_discard(q)) {
  272. num_devices++;
  273. minlen = min((u64)q->limits.discard_granularity,
  274. minlen);
  275. }
  276. }
  277. rcu_read_unlock();
  278. if (!num_devices)
  279. return -EOPNOTSUPP;
  280. if (copy_from_user(&range, arg, sizeof(range)))
  281. return -EFAULT;
  282. if (range.start > total_bytes)
  283. return -EINVAL;
  284. range.len = min(range.len, total_bytes - range.start);
  285. range.minlen = max(range.minlen, minlen);
  286. ret = btrfs_trim_fs(fs_info->tree_root, &range);
  287. if (ret < 0)
  288. return ret;
  289. if (copy_to_user(arg, &range, sizeof(range)))
  290. return -EFAULT;
  291. return 0;
  292. }
  293. static noinline int create_subvol(struct btrfs_root *root,
  294. struct dentry *dentry,
  295. char *name, int namelen,
  296. u64 *async_transid,
  297. struct btrfs_qgroup_inherit **inherit)
  298. {
  299. struct btrfs_trans_handle *trans;
  300. struct btrfs_key key;
  301. struct btrfs_root_item root_item;
  302. struct btrfs_inode_item *inode_item;
  303. struct extent_buffer *leaf;
  304. struct btrfs_root *new_root;
  305. struct dentry *parent = dentry->d_parent;
  306. struct inode *dir;
  307. struct timespec cur_time = CURRENT_TIME;
  308. int ret;
  309. int err;
  310. u64 objectid;
  311. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  312. u64 index = 0;
  313. uuid_le new_uuid;
  314. ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
  315. if (ret)
  316. return ret;
  317. dir = parent->d_inode;
  318. /*
  319. * 1 - inode item
  320. * 2 - refs
  321. * 1 - root item
  322. * 2 - dir items
  323. */
  324. trans = btrfs_start_transaction(root, 6);
  325. if (IS_ERR(trans))
  326. return PTR_ERR(trans);
  327. ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
  328. inherit ? *inherit : NULL);
  329. if (ret)
  330. goto fail;
  331. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  332. 0, objectid, NULL, 0, 0, 0);
  333. if (IS_ERR(leaf)) {
  334. ret = PTR_ERR(leaf);
  335. goto fail;
  336. }
  337. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  338. btrfs_set_header_bytenr(leaf, leaf->start);
  339. btrfs_set_header_generation(leaf, trans->transid);
  340. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  341. btrfs_set_header_owner(leaf, objectid);
  342. write_extent_buffer(leaf, root->fs_info->fsid,
  343. (unsigned long)btrfs_header_fsid(leaf),
  344. BTRFS_FSID_SIZE);
  345. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  346. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  347. BTRFS_UUID_SIZE);
  348. btrfs_mark_buffer_dirty(leaf);
  349. memset(&root_item, 0, sizeof(root_item));
  350. inode_item = &root_item.inode;
  351. inode_item->generation = cpu_to_le64(1);
  352. inode_item->size = cpu_to_le64(3);
  353. inode_item->nlink = cpu_to_le32(1);
  354. inode_item->nbytes = cpu_to_le64(root->leafsize);
  355. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  356. root_item.flags = 0;
  357. root_item.byte_limit = 0;
  358. inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
  359. btrfs_set_root_bytenr(&root_item, leaf->start);
  360. btrfs_set_root_generation(&root_item, trans->transid);
  361. btrfs_set_root_level(&root_item, 0);
  362. btrfs_set_root_refs(&root_item, 1);
  363. btrfs_set_root_used(&root_item, leaf->len);
  364. btrfs_set_root_last_snapshot(&root_item, 0);
  365. btrfs_set_root_generation_v2(&root_item,
  366. btrfs_root_generation(&root_item));
  367. uuid_le_gen(&new_uuid);
  368. memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
  369. root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
  370. root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
  371. root_item.ctime = root_item.otime;
  372. btrfs_set_root_ctransid(&root_item, trans->transid);
  373. btrfs_set_root_otransid(&root_item, trans->transid);
  374. btrfs_tree_unlock(leaf);
  375. free_extent_buffer(leaf);
  376. leaf = NULL;
  377. btrfs_set_root_dirid(&root_item, new_dirid);
  378. key.objectid = objectid;
  379. key.offset = 0;
  380. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  381. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  382. &root_item);
  383. if (ret)
  384. goto fail;
  385. key.offset = (u64)-1;
  386. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  387. if (IS_ERR(new_root)) {
  388. btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
  389. ret = PTR_ERR(new_root);
  390. goto fail;
  391. }
  392. btrfs_record_root_in_trans(trans, new_root);
  393. ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
  394. if (ret) {
  395. /* We potentially lose an unused inode item here */
  396. btrfs_abort_transaction(trans, root, ret);
  397. goto fail;
  398. }
  399. /*
  400. * insert the directory item
  401. */
  402. ret = btrfs_set_inode_index(dir, &index);
  403. if (ret) {
  404. btrfs_abort_transaction(trans, root, ret);
  405. goto fail;
  406. }
  407. ret = btrfs_insert_dir_item(trans, root,
  408. name, namelen, dir, &key,
  409. BTRFS_FT_DIR, index);
  410. if (ret) {
  411. btrfs_abort_transaction(trans, root, ret);
  412. goto fail;
  413. }
  414. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  415. ret = btrfs_update_inode(trans, root, dir);
  416. BUG_ON(ret);
  417. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  418. objectid, root->root_key.objectid,
  419. btrfs_ino(dir), index, name, namelen);
  420. BUG_ON(ret);
  421. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  422. fail:
  423. if (async_transid) {
  424. *async_transid = trans->transid;
  425. err = btrfs_commit_transaction_async(trans, root, 1);
  426. } else {
  427. err = btrfs_commit_transaction(trans, root);
  428. }
  429. if (err && !ret)
  430. ret = err;
  431. return ret;
  432. }
  433. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  434. char *name, int namelen, u64 *async_transid,
  435. bool readonly, struct btrfs_qgroup_inherit **inherit)
  436. {
  437. struct inode *inode;
  438. struct btrfs_pending_snapshot *pending_snapshot;
  439. struct btrfs_trans_handle *trans;
  440. int ret;
  441. if (!root->ref_cows)
  442. return -EINVAL;
  443. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  444. if (!pending_snapshot)
  445. return -ENOMEM;
  446. btrfs_init_block_rsv(&pending_snapshot->block_rsv);
  447. pending_snapshot->dentry = dentry;
  448. pending_snapshot->root = root;
  449. pending_snapshot->readonly = readonly;
  450. if (inherit) {
  451. pending_snapshot->inherit = *inherit;
  452. *inherit = NULL; /* take responsibility to free it */
  453. }
  454. trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
  455. if (IS_ERR(trans)) {
  456. ret = PTR_ERR(trans);
  457. goto fail;
  458. }
  459. ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
  460. BUG_ON(ret);
  461. spin_lock(&root->fs_info->trans_lock);
  462. list_add(&pending_snapshot->list,
  463. &trans->transaction->pending_snapshots);
  464. spin_unlock(&root->fs_info->trans_lock);
  465. if (async_transid) {
  466. *async_transid = trans->transid;
  467. ret = btrfs_commit_transaction_async(trans,
  468. root->fs_info->extent_root, 1);
  469. } else {
  470. ret = btrfs_commit_transaction(trans,
  471. root->fs_info->extent_root);
  472. }
  473. BUG_ON(ret);
  474. ret = pending_snapshot->error;
  475. if (ret)
  476. goto fail;
  477. ret = btrfs_orphan_cleanup(pending_snapshot->snap);
  478. if (ret)
  479. goto fail;
  480. inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
  481. if (IS_ERR(inode)) {
  482. ret = PTR_ERR(inode);
  483. goto fail;
  484. }
  485. BUG_ON(!inode);
  486. d_instantiate(dentry, inode);
  487. ret = 0;
  488. fail:
  489. kfree(pending_snapshot);
  490. return ret;
  491. }
  492. /* copy of check_sticky in fs/namei.c()
  493. * It's inline, so penalty for filesystems that don't use sticky bit is
  494. * minimal.
  495. */
  496. static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
  497. {
  498. uid_t fsuid = current_fsuid();
  499. if (!(dir->i_mode & S_ISVTX))
  500. return 0;
  501. if (inode->i_uid == fsuid)
  502. return 0;
  503. if (dir->i_uid == fsuid)
  504. return 0;
  505. return !capable(CAP_FOWNER);
  506. }
  507. /* copy of may_delete in fs/namei.c()
  508. * Check whether we can remove a link victim from directory dir, check
  509. * whether the type of victim is right.
  510. * 1. We can't do it if dir is read-only (done in permission())
  511. * 2. We should have write and exec permissions on dir
  512. * 3. We can't remove anything from append-only dir
  513. * 4. We can't do anything with immutable dir (done in permission())
  514. * 5. If the sticky bit on dir is set we should either
  515. * a. be owner of dir, or
  516. * b. be owner of victim, or
  517. * c. have CAP_FOWNER capability
  518. * 6. If the victim is append-only or immutable we can't do antyhing with
  519. * links pointing to it.
  520. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  521. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  522. * 9. We can't remove a root or mountpoint.
  523. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  524. * nfs_async_unlink().
  525. */
  526. static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
  527. {
  528. int error;
  529. if (!victim->d_inode)
  530. return -ENOENT;
  531. BUG_ON(victim->d_parent->d_inode != dir);
  532. audit_inode_child(victim, dir);
  533. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  534. if (error)
  535. return error;
  536. if (IS_APPEND(dir))
  537. return -EPERM;
  538. if (btrfs_check_sticky(dir, victim->d_inode)||
  539. IS_APPEND(victim->d_inode)||
  540. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  541. return -EPERM;
  542. if (isdir) {
  543. if (!S_ISDIR(victim->d_inode->i_mode))
  544. return -ENOTDIR;
  545. if (IS_ROOT(victim))
  546. return -EBUSY;
  547. } else if (S_ISDIR(victim->d_inode->i_mode))
  548. return -EISDIR;
  549. if (IS_DEADDIR(dir))
  550. return -ENOENT;
  551. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  552. return -EBUSY;
  553. return 0;
  554. }
  555. /* copy of may_create in fs/namei.c() */
  556. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  557. {
  558. if (child->d_inode)
  559. return -EEXIST;
  560. if (IS_DEADDIR(dir))
  561. return -ENOENT;
  562. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  563. }
  564. /*
  565. * Create a new subvolume below @parent. This is largely modeled after
  566. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  567. * inside this filesystem so it's quite a bit simpler.
  568. */
  569. static noinline int btrfs_mksubvol(struct path *parent,
  570. char *name, int namelen,
  571. struct btrfs_root *snap_src,
  572. u64 *async_transid, bool readonly,
  573. struct btrfs_qgroup_inherit **inherit)
  574. {
  575. struct inode *dir = parent->dentry->d_inode;
  576. struct dentry *dentry;
  577. int error;
  578. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  579. dentry = lookup_one_len(name, parent->dentry, namelen);
  580. error = PTR_ERR(dentry);
  581. if (IS_ERR(dentry))
  582. goto out_unlock;
  583. error = -EEXIST;
  584. if (dentry->d_inode)
  585. goto out_dput;
  586. error = btrfs_may_create(dir, dentry);
  587. if (error)
  588. goto out_dput;
  589. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  590. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  591. goto out_up_read;
  592. if (snap_src) {
  593. error = create_snapshot(snap_src, dentry, name, namelen,
  594. async_transid, readonly, inherit);
  595. } else {
  596. error = create_subvol(BTRFS_I(dir)->root, dentry,
  597. name, namelen, async_transid, inherit);
  598. }
  599. if (!error)
  600. fsnotify_mkdir(dir, dentry);
  601. out_up_read:
  602. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  603. out_dput:
  604. dput(dentry);
  605. out_unlock:
  606. mutex_unlock(&dir->i_mutex);
  607. return error;
  608. }
  609. /*
  610. * When we're defragging a range, we don't want to kick it off again
  611. * if it is really just waiting for delalloc to send it down.
  612. * If we find a nice big extent or delalloc range for the bytes in the
  613. * file you want to defrag, we return 0 to let you know to skip this
  614. * part of the file
  615. */
  616. static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
  617. {
  618. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  619. struct extent_map *em = NULL;
  620. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  621. u64 end;
  622. read_lock(&em_tree->lock);
  623. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  624. read_unlock(&em_tree->lock);
  625. if (em) {
  626. end = extent_map_end(em);
  627. free_extent_map(em);
  628. if (end - offset > thresh)
  629. return 0;
  630. }
  631. /* if we already have a nice delalloc here, just stop */
  632. thresh /= 2;
  633. end = count_range_bits(io_tree, &offset, offset + thresh,
  634. thresh, EXTENT_DELALLOC, 1);
  635. if (end >= thresh)
  636. return 0;
  637. return 1;
  638. }
  639. /*
  640. * helper function to walk through a file and find extents
  641. * newer than a specific transid, and smaller than thresh.
  642. *
  643. * This is used by the defragging code to find new and small
  644. * extents
  645. */
  646. static int find_new_extents(struct btrfs_root *root,
  647. struct inode *inode, u64 newer_than,
  648. u64 *off, int thresh)
  649. {
  650. struct btrfs_path *path;
  651. struct btrfs_key min_key;
  652. struct btrfs_key max_key;
  653. struct extent_buffer *leaf;
  654. struct btrfs_file_extent_item *extent;
  655. int type;
  656. int ret;
  657. u64 ino = btrfs_ino(inode);
  658. path = btrfs_alloc_path();
  659. if (!path)
  660. return -ENOMEM;
  661. min_key.objectid = ino;
  662. min_key.type = BTRFS_EXTENT_DATA_KEY;
  663. min_key.offset = *off;
  664. max_key.objectid = ino;
  665. max_key.type = (u8)-1;
  666. max_key.offset = (u64)-1;
  667. path->keep_locks = 1;
  668. while(1) {
  669. ret = btrfs_search_forward(root, &min_key, &max_key,
  670. path, 0, newer_than);
  671. if (ret != 0)
  672. goto none;
  673. if (min_key.objectid != ino)
  674. goto none;
  675. if (min_key.type != BTRFS_EXTENT_DATA_KEY)
  676. goto none;
  677. leaf = path->nodes[0];
  678. extent = btrfs_item_ptr(leaf, path->slots[0],
  679. struct btrfs_file_extent_item);
  680. type = btrfs_file_extent_type(leaf, extent);
  681. if (type == BTRFS_FILE_EXTENT_REG &&
  682. btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
  683. check_defrag_in_cache(inode, min_key.offset, thresh)) {
  684. *off = min_key.offset;
  685. btrfs_free_path(path);
  686. return 0;
  687. }
  688. if (min_key.offset == (u64)-1)
  689. goto none;
  690. min_key.offset++;
  691. btrfs_release_path(path);
  692. }
  693. none:
  694. btrfs_free_path(path);
  695. return -ENOENT;
  696. }
  697. static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
  698. {
  699. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  700. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  701. struct extent_map *em;
  702. u64 len = PAGE_CACHE_SIZE;
  703. /*
  704. * hopefully we have this extent in the tree already, try without
  705. * the full extent lock
  706. */
  707. read_lock(&em_tree->lock);
  708. em = lookup_extent_mapping(em_tree, start, len);
  709. read_unlock(&em_tree->lock);
  710. if (!em) {
  711. /* get the big lock and read metadata off disk */
  712. lock_extent(io_tree, start, start + len - 1);
  713. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  714. unlock_extent(io_tree, start, start + len - 1);
  715. if (IS_ERR(em))
  716. return NULL;
  717. }
  718. return em;
  719. }
  720. static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
  721. {
  722. struct extent_map *next;
  723. bool ret = true;
  724. /* this is the last extent */
  725. if (em->start + em->len >= i_size_read(inode))
  726. return false;
  727. next = defrag_lookup_extent(inode, em->start + em->len);
  728. if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
  729. ret = false;
  730. free_extent_map(next);
  731. return ret;
  732. }
  733. static int should_defrag_range(struct inode *inode, u64 start, int thresh,
  734. u64 *last_len, u64 *skip, u64 *defrag_end,
  735. int compress)
  736. {
  737. struct extent_map *em;
  738. int ret = 1;
  739. bool next_mergeable = true;
  740. /*
  741. * make sure that once we start defragging an extent, we keep on
  742. * defragging it
  743. */
  744. if (start < *defrag_end)
  745. return 1;
  746. *skip = 0;
  747. em = defrag_lookup_extent(inode, start);
  748. if (!em)
  749. return 0;
  750. /* this will cover holes, and inline extents */
  751. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  752. ret = 0;
  753. goto out;
  754. }
  755. next_mergeable = defrag_check_next_extent(inode, em);
  756. /*
  757. * we hit a real extent, if it is big or the next extent is not a
  758. * real extent, don't bother defragging it
  759. */
  760. if (!compress && (*last_len == 0 || *last_len >= thresh) &&
  761. (em->len >= thresh || !next_mergeable))
  762. ret = 0;
  763. out:
  764. /*
  765. * last_len ends up being a counter of how many bytes we've defragged.
  766. * every time we choose not to defrag an extent, we reset *last_len
  767. * so that the next tiny extent will force a defrag.
  768. *
  769. * The end result of this is that tiny extents before a single big
  770. * extent will force at least part of that big extent to be defragged.
  771. */
  772. if (ret) {
  773. *defrag_end = extent_map_end(em);
  774. } else {
  775. *last_len = 0;
  776. *skip = extent_map_end(em);
  777. *defrag_end = 0;
  778. }
  779. free_extent_map(em);
  780. return ret;
  781. }
  782. /*
  783. * it doesn't do much good to defrag one or two pages
  784. * at a time. This pulls in a nice chunk of pages
  785. * to COW and defrag.
  786. *
  787. * It also makes sure the delalloc code has enough
  788. * dirty data to avoid making new small extents as part
  789. * of the defrag
  790. *
  791. * It's a good idea to start RA on this range
  792. * before calling this.
  793. */
  794. static int cluster_pages_for_defrag(struct inode *inode,
  795. struct page **pages,
  796. unsigned long start_index,
  797. int num_pages)
  798. {
  799. unsigned long file_end;
  800. u64 isize = i_size_read(inode);
  801. u64 page_start;
  802. u64 page_end;
  803. u64 page_cnt;
  804. int ret;
  805. int i;
  806. int i_done;
  807. struct btrfs_ordered_extent *ordered;
  808. struct extent_state *cached_state = NULL;
  809. struct extent_io_tree *tree;
  810. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  811. file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
  812. if (!isize || start_index > file_end)
  813. return 0;
  814. page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
  815. ret = btrfs_delalloc_reserve_space(inode,
  816. page_cnt << PAGE_CACHE_SHIFT);
  817. if (ret)
  818. return ret;
  819. i_done = 0;
  820. tree = &BTRFS_I(inode)->io_tree;
  821. /* step one, lock all the pages */
  822. for (i = 0; i < page_cnt; i++) {
  823. struct page *page;
  824. again:
  825. page = find_or_create_page(inode->i_mapping,
  826. start_index + i, mask);
  827. if (!page)
  828. break;
  829. page_start = page_offset(page);
  830. page_end = page_start + PAGE_CACHE_SIZE - 1;
  831. while (1) {
  832. lock_extent(tree, page_start, page_end);
  833. ordered = btrfs_lookup_ordered_extent(inode,
  834. page_start);
  835. unlock_extent(tree, page_start, page_end);
  836. if (!ordered)
  837. break;
  838. unlock_page(page);
  839. btrfs_start_ordered_extent(inode, ordered, 1);
  840. btrfs_put_ordered_extent(ordered);
  841. lock_page(page);
  842. /*
  843. * we unlocked the page above, so we need check if
  844. * it was released or not.
  845. */
  846. if (page->mapping != inode->i_mapping) {
  847. unlock_page(page);
  848. page_cache_release(page);
  849. goto again;
  850. }
  851. }
  852. if (!PageUptodate(page)) {
  853. btrfs_readpage(NULL, page);
  854. lock_page(page);
  855. if (!PageUptodate(page)) {
  856. unlock_page(page);
  857. page_cache_release(page);
  858. ret = -EIO;
  859. break;
  860. }
  861. }
  862. if (page->mapping != inode->i_mapping) {
  863. unlock_page(page);
  864. page_cache_release(page);
  865. goto again;
  866. }
  867. pages[i] = page;
  868. i_done++;
  869. }
  870. if (!i_done || ret)
  871. goto out;
  872. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  873. goto out;
  874. /*
  875. * so now we have a nice long stream of locked
  876. * and up to date pages, lets wait on them
  877. */
  878. for (i = 0; i < i_done; i++)
  879. wait_on_page_writeback(pages[i]);
  880. page_start = page_offset(pages[0]);
  881. page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
  882. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  883. page_start, page_end - 1, 0, &cached_state);
  884. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
  885. page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  886. EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
  887. GFP_NOFS);
  888. if (i_done != page_cnt) {
  889. spin_lock(&BTRFS_I(inode)->lock);
  890. BTRFS_I(inode)->outstanding_extents++;
  891. spin_unlock(&BTRFS_I(inode)->lock);
  892. btrfs_delalloc_release_space(inode,
  893. (page_cnt - i_done) << PAGE_CACHE_SHIFT);
  894. }
  895. btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
  896. &cached_state);
  897. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  898. page_start, page_end - 1, &cached_state,
  899. GFP_NOFS);
  900. for (i = 0; i < i_done; i++) {
  901. clear_page_dirty_for_io(pages[i]);
  902. ClearPageChecked(pages[i]);
  903. set_page_extent_mapped(pages[i]);
  904. set_page_dirty(pages[i]);
  905. unlock_page(pages[i]);
  906. page_cache_release(pages[i]);
  907. }
  908. return i_done;
  909. out:
  910. for (i = 0; i < i_done; i++) {
  911. unlock_page(pages[i]);
  912. page_cache_release(pages[i]);
  913. }
  914. btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
  915. return ret;
  916. }
  917. int btrfs_defrag_file(struct inode *inode, struct file *file,
  918. struct btrfs_ioctl_defrag_range_args *range,
  919. u64 newer_than, unsigned long max_to_defrag)
  920. {
  921. struct btrfs_root *root = BTRFS_I(inode)->root;
  922. struct file_ra_state *ra = NULL;
  923. unsigned long last_index;
  924. u64 isize = i_size_read(inode);
  925. u64 last_len = 0;
  926. u64 skip = 0;
  927. u64 defrag_end = 0;
  928. u64 newer_off = range->start;
  929. unsigned long i;
  930. unsigned long ra_index = 0;
  931. int ret;
  932. int defrag_count = 0;
  933. int compress_type = BTRFS_COMPRESS_ZLIB;
  934. int extent_thresh = range->extent_thresh;
  935. int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
  936. int cluster = max_cluster;
  937. u64 new_align = ~((u64)128 * 1024 - 1);
  938. struct page **pages = NULL;
  939. if (extent_thresh == 0)
  940. extent_thresh = 256 * 1024;
  941. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  942. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  943. return -EINVAL;
  944. if (range->compress_type)
  945. compress_type = range->compress_type;
  946. }
  947. if (isize == 0)
  948. return 0;
  949. /*
  950. * if we were not given a file, allocate a readahead
  951. * context
  952. */
  953. if (!file) {
  954. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  955. if (!ra)
  956. return -ENOMEM;
  957. file_ra_state_init(ra, inode->i_mapping);
  958. } else {
  959. ra = &file->f_ra;
  960. }
  961. pages = kmalloc(sizeof(struct page *) * max_cluster,
  962. GFP_NOFS);
  963. if (!pages) {
  964. ret = -ENOMEM;
  965. goto out_ra;
  966. }
  967. /* find the last page to defrag */
  968. if (range->start + range->len > range->start) {
  969. last_index = min_t(u64, isize - 1,
  970. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  971. } else {
  972. last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  973. }
  974. if (newer_than) {
  975. ret = find_new_extents(root, inode, newer_than,
  976. &newer_off, 64 * 1024);
  977. if (!ret) {
  978. range->start = newer_off;
  979. /*
  980. * we always align our defrag to help keep
  981. * the extents in the file evenly spaced
  982. */
  983. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  984. } else
  985. goto out_ra;
  986. } else {
  987. i = range->start >> PAGE_CACHE_SHIFT;
  988. }
  989. if (!max_to_defrag)
  990. max_to_defrag = last_index + 1;
  991. /*
  992. * make writeback starts from i, so the defrag range can be
  993. * written sequentially.
  994. */
  995. if (i < inode->i_mapping->writeback_index)
  996. inode->i_mapping->writeback_index = i;
  997. while (i <= last_index && defrag_count < max_to_defrag &&
  998. (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
  999. PAGE_CACHE_SHIFT)) {
  1000. /*
  1001. * make sure we stop running if someone unmounts
  1002. * the FS
  1003. */
  1004. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  1005. break;
  1006. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  1007. extent_thresh, &last_len, &skip,
  1008. &defrag_end, range->flags &
  1009. BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1010. unsigned long next;
  1011. /*
  1012. * the should_defrag function tells us how much to skip
  1013. * bump our counter by the suggested amount
  1014. */
  1015. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1016. i = max(i + 1, next);
  1017. continue;
  1018. }
  1019. if (!newer_than) {
  1020. cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
  1021. PAGE_CACHE_SHIFT) - i;
  1022. cluster = min(cluster, max_cluster);
  1023. } else {
  1024. cluster = max_cluster;
  1025. }
  1026. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  1027. BTRFS_I(inode)->force_compress = compress_type;
  1028. if (i + cluster > ra_index) {
  1029. ra_index = max(i, ra_index);
  1030. btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
  1031. cluster);
  1032. ra_index += max_cluster;
  1033. }
  1034. mutex_lock(&inode->i_mutex);
  1035. ret = cluster_pages_for_defrag(inode, pages, i, cluster);
  1036. if (ret < 0) {
  1037. mutex_unlock(&inode->i_mutex);
  1038. goto out_ra;
  1039. }
  1040. defrag_count += ret;
  1041. balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
  1042. mutex_unlock(&inode->i_mutex);
  1043. if (newer_than) {
  1044. if (newer_off == (u64)-1)
  1045. break;
  1046. if (ret > 0)
  1047. i += ret;
  1048. newer_off = max(newer_off + 1,
  1049. (u64)i << PAGE_CACHE_SHIFT);
  1050. ret = find_new_extents(root, inode,
  1051. newer_than, &newer_off,
  1052. 64 * 1024);
  1053. if (!ret) {
  1054. range->start = newer_off;
  1055. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  1056. } else {
  1057. break;
  1058. }
  1059. } else {
  1060. if (ret > 0) {
  1061. i += ret;
  1062. last_len += ret << PAGE_CACHE_SHIFT;
  1063. } else {
  1064. i++;
  1065. last_len = 0;
  1066. }
  1067. }
  1068. }
  1069. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  1070. filemap_flush(inode->i_mapping);
  1071. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1072. /* the filemap_flush will queue IO into the worker threads, but
  1073. * we have to make sure the IO is actually started and that
  1074. * ordered extents get created before we return
  1075. */
  1076. atomic_inc(&root->fs_info->async_submit_draining);
  1077. while (atomic_read(&root->fs_info->nr_async_submits) ||
  1078. atomic_read(&root->fs_info->async_delalloc_pages)) {
  1079. wait_event(root->fs_info->async_submit_wait,
  1080. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  1081. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  1082. }
  1083. atomic_dec(&root->fs_info->async_submit_draining);
  1084. mutex_lock(&inode->i_mutex);
  1085. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  1086. mutex_unlock(&inode->i_mutex);
  1087. }
  1088. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  1089. btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
  1090. }
  1091. ret = defrag_count;
  1092. out_ra:
  1093. if (!file)
  1094. kfree(ra);
  1095. kfree(pages);
  1096. return ret;
  1097. }
  1098. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  1099. void __user *arg)
  1100. {
  1101. u64 new_size;
  1102. u64 old_size;
  1103. u64 devid = 1;
  1104. struct btrfs_ioctl_vol_args *vol_args;
  1105. struct btrfs_trans_handle *trans;
  1106. struct btrfs_device *device = NULL;
  1107. char *sizestr;
  1108. char *devstr = NULL;
  1109. int ret = 0;
  1110. int mod = 0;
  1111. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1112. return -EROFS;
  1113. if (!capable(CAP_SYS_ADMIN))
  1114. return -EPERM;
  1115. mutex_lock(&root->fs_info->volume_mutex);
  1116. if (root->fs_info->balance_ctl) {
  1117. printk(KERN_INFO "btrfs: balance in progress\n");
  1118. ret = -EINVAL;
  1119. goto out;
  1120. }
  1121. vol_args = memdup_user(arg, sizeof(*vol_args));
  1122. if (IS_ERR(vol_args)) {
  1123. ret = PTR_ERR(vol_args);
  1124. goto out;
  1125. }
  1126. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1127. sizestr = vol_args->name;
  1128. devstr = strchr(sizestr, ':');
  1129. if (devstr) {
  1130. char *end;
  1131. sizestr = devstr + 1;
  1132. *devstr = '\0';
  1133. devstr = vol_args->name;
  1134. devid = simple_strtoull(devstr, &end, 10);
  1135. printk(KERN_INFO "btrfs: resizing devid %llu\n",
  1136. (unsigned long long)devid);
  1137. }
  1138. device = btrfs_find_device(root, devid, NULL, NULL);
  1139. if (!device) {
  1140. printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
  1141. (unsigned long long)devid);
  1142. ret = -EINVAL;
  1143. goto out_free;
  1144. }
  1145. if (device->fs_devices && device->fs_devices->seeding) {
  1146. printk(KERN_INFO "btrfs: resizer unable to apply on "
  1147. "seeding device %llu\n",
  1148. (unsigned long long)devid);
  1149. ret = -EINVAL;
  1150. goto out_free;
  1151. }
  1152. if (!strcmp(sizestr, "max"))
  1153. new_size = device->bdev->bd_inode->i_size;
  1154. else {
  1155. if (sizestr[0] == '-') {
  1156. mod = -1;
  1157. sizestr++;
  1158. } else if (sizestr[0] == '+') {
  1159. mod = 1;
  1160. sizestr++;
  1161. }
  1162. new_size = memparse(sizestr, NULL);
  1163. if (new_size == 0) {
  1164. ret = -EINVAL;
  1165. goto out_free;
  1166. }
  1167. }
  1168. old_size = device->total_bytes;
  1169. if (mod < 0) {
  1170. if (new_size > old_size) {
  1171. ret = -EINVAL;
  1172. goto out_free;
  1173. }
  1174. new_size = old_size - new_size;
  1175. } else if (mod > 0) {
  1176. new_size = old_size + new_size;
  1177. }
  1178. if (new_size < 256 * 1024 * 1024) {
  1179. ret = -EINVAL;
  1180. goto out_free;
  1181. }
  1182. if (new_size > device->bdev->bd_inode->i_size) {
  1183. ret = -EFBIG;
  1184. goto out_free;
  1185. }
  1186. do_div(new_size, root->sectorsize);
  1187. new_size *= root->sectorsize;
  1188. printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
  1189. rcu_str_deref(device->name),
  1190. (unsigned long long)new_size);
  1191. if (new_size > old_size) {
  1192. trans = btrfs_start_transaction(root, 0);
  1193. if (IS_ERR(trans)) {
  1194. ret = PTR_ERR(trans);
  1195. goto out_free;
  1196. }
  1197. ret = btrfs_grow_device(trans, device, new_size);
  1198. btrfs_commit_transaction(trans, root);
  1199. } else if (new_size < old_size) {
  1200. ret = btrfs_shrink_device(device, new_size);
  1201. }
  1202. out_free:
  1203. kfree(vol_args);
  1204. out:
  1205. mutex_unlock(&root->fs_info->volume_mutex);
  1206. return ret;
  1207. }
  1208. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1209. char *name, unsigned long fd, int subvol,
  1210. u64 *transid, bool readonly,
  1211. struct btrfs_qgroup_inherit **inherit)
  1212. {
  1213. struct file *src_file;
  1214. int namelen;
  1215. int ret = 0;
  1216. ret = mnt_want_write_file(file);
  1217. if (ret)
  1218. goto out;
  1219. namelen = strlen(name);
  1220. if (strchr(name, '/')) {
  1221. ret = -EINVAL;
  1222. goto out_drop_write;
  1223. }
  1224. if (name[0] == '.' &&
  1225. (namelen == 1 || (name[1] == '.' && namelen == 2))) {
  1226. ret = -EEXIST;
  1227. goto out_drop_write;
  1228. }
  1229. if (subvol) {
  1230. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1231. NULL, transid, readonly, inherit);
  1232. } else {
  1233. struct inode *src_inode;
  1234. src_file = fget(fd);
  1235. if (!src_file) {
  1236. ret = -EINVAL;
  1237. goto out_drop_write;
  1238. }
  1239. src_inode = src_file->f_path.dentry->d_inode;
  1240. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  1241. printk(KERN_INFO "btrfs: Snapshot src from "
  1242. "another FS\n");
  1243. ret = -EINVAL;
  1244. fput(src_file);
  1245. goto out_drop_write;
  1246. }
  1247. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1248. BTRFS_I(src_inode)->root,
  1249. transid, readonly, inherit);
  1250. fput(src_file);
  1251. }
  1252. out_drop_write:
  1253. mnt_drop_write_file(file);
  1254. out:
  1255. return ret;
  1256. }
  1257. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1258. void __user *arg, int subvol)
  1259. {
  1260. struct btrfs_ioctl_vol_args *vol_args;
  1261. int ret;
  1262. vol_args = memdup_user(arg, sizeof(*vol_args));
  1263. if (IS_ERR(vol_args))
  1264. return PTR_ERR(vol_args);
  1265. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1266. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1267. vol_args->fd, subvol,
  1268. NULL, false, NULL);
  1269. kfree(vol_args);
  1270. return ret;
  1271. }
  1272. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1273. void __user *arg, int subvol)
  1274. {
  1275. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1276. int ret;
  1277. u64 transid = 0;
  1278. u64 *ptr = NULL;
  1279. bool readonly = false;
  1280. struct btrfs_qgroup_inherit *inherit = NULL;
  1281. vol_args = memdup_user(arg, sizeof(*vol_args));
  1282. if (IS_ERR(vol_args))
  1283. return PTR_ERR(vol_args);
  1284. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1285. if (vol_args->flags &
  1286. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
  1287. BTRFS_SUBVOL_QGROUP_INHERIT)) {
  1288. ret = -EOPNOTSUPP;
  1289. goto out;
  1290. }
  1291. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1292. ptr = &transid;
  1293. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1294. readonly = true;
  1295. if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
  1296. if (vol_args->size > PAGE_CACHE_SIZE) {
  1297. ret = -EINVAL;
  1298. goto out;
  1299. }
  1300. inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
  1301. if (IS_ERR(inherit)) {
  1302. ret = PTR_ERR(inherit);
  1303. goto out;
  1304. }
  1305. }
  1306. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1307. vol_args->fd, subvol, ptr,
  1308. readonly, &inherit);
  1309. if (ret == 0 && ptr &&
  1310. copy_to_user(arg +
  1311. offsetof(struct btrfs_ioctl_vol_args_v2,
  1312. transid), ptr, sizeof(*ptr)))
  1313. ret = -EFAULT;
  1314. out:
  1315. kfree(vol_args);
  1316. kfree(inherit);
  1317. return ret;
  1318. }
  1319. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1320. void __user *arg)
  1321. {
  1322. struct inode *inode = fdentry(file)->d_inode;
  1323. struct btrfs_root *root = BTRFS_I(inode)->root;
  1324. int ret = 0;
  1325. u64 flags = 0;
  1326. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1327. return -EINVAL;
  1328. down_read(&root->fs_info->subvol_sem);
  1329. if (btrfs_root_readonly(root))
  1330. flags |= BTRFS_SUBVOL_RDONLY;
  1331. up_read(&root->fs_info->subvol_sem);
  1332. if (copy_to_user(arg, &flags, sizeof(flags)))
  1333. ret = -EFAULT;
  1334. return ret;
  1335. }
  1336. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1337. void __user *arg)
  1338. {
  1339. struct inode *inode = fdentry(file)->d_inode;
  1340. struct btrfs_root *root = BTRFS_I(inode)->root;
  1341. struct btrfs_trans_handle *trans;
  1342. u64 root_flags;
  1343. u64 flags;
  1344. int ret = 0;
  1345. ret = mnt_want_write_file(file);
  1346. if (ret)
  1347. goto out;
  1348. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1349. ret = -EINVAL;
  1350. goto out_drop_write;
  1351. }
  1352. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1353. ret = -EFAULT;
  1354. goto out_drop_write;
  1355. }
  1356. if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
  1357. ret = -EINVAL;
  1358. goto out_drop_write;
  1359. }
  1360. if (flags & ~BTRFS_SUBVOL_RDONLY) {
  1361. ret = -EOPNOTSUPP;
  1362. goto out_drop_write;
  1363. }
  1364. if (!inode_owner_or_capable(inode)) {
  1365. ret = -EACCES;
  1366. goto out_drop_write;
  1367. }
  1368. down_write(&root->fs_info->subvol_sem);
  1369. /* nothing to do */
  1370. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1371. goto out_drop_sem;
  1372. root_flags = btrfs_root_flags(&root->root_item);
  1373. if (flags & BTRFS_SUBVOL_RDONLY)
  1374. btrfs_set_root_flags(&root->root_item,
  1375. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1376. else
  1377. btrfs_set_root_flags(&root->root_item,
  1378. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1379. trans = btrfs_start_transaction(root, 1);
  1380. if (IS_ERR(trans)) {
  1381. ret = PTR_ERR(trans);
  1382. goto out_reset;
  1383. }
  1384. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1385. &root->root_key, &root->root_item);
  1386. btrfs_commit_transaction(trans, root);
  1387. out_reset:
  1388. if (ret)
  1389. btrfs_set_root_flags(&root->root_item, root_flags);
  1390. out_drop_sem:
  1391. up_write(&root->fs_info->subvol_sem);
  1392. out_drop_write:
  1393. mnt_drop_write_file(file);
  1394. out:
  1395. return ret;
  1396. }
  1397. /*
  1398. * helper to check if the subvolume references other subvolumes
  1399. */
  1400. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1401. {
  1402. struct btrfs_path *path;
  1403. struct btrfs_key key;
  1404. int ret;
  1405. path = btrfs_alloc_path();
  1406. if (!path)
  1407. return -ENOMEM;
  1408. key.objectid = root->root_key.objectid;
  1409. key.type = BTRFS_ROOT_REF_KEY;
  1410. key.offset = (u64)-1;
  1411. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1412. &key, path, 0, 0);
  1413. if (ret < 0)
  1414. goto out;
  1415. BUG_ON(ret == 0);
  1416. ret = 0;
  1417. if (path->slots[0] > 0) {
  1418. path->slots[0]--;
  1419. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1420. if (key.objectid == root->root_key.objectid &&
  1421. key.type == BTRFS_ROOT_REF_KEY)
  1422. ret = -ENOTEMPTY;
  1423. }
  1424. out:
  1425. btrfs_free_path(path);
  1426. return ret;
  1427. }
  1428. static noinline int key_in_sk(struct btrfs_key *key,
  1429. struct btrfs_ioctl_search_key *sk)
  1430. {
  1431. struct btrfs_key test;
  1432. int ret;
  1433. test.objectid = sk->min_objectid;
  1434. test.type = sk->min_type;
  1435. test.offset = sk->min_offset;
  1436. ret = btrfs_comp_cpu_keys(key, &test);
  1437. if (ret < 0)
  1438. return 0;
  1439. test.objectid = sk->max_objectid;
  1440. test.type = sk->max_type;
  1441. test.offset = sk->max_offset;
  1442. ret = btrfs_comp_cpu_keys(key, &test);
  1443. if (ret > 0)
  1444. return 0;
  1445. return 1;
  1446. }
  1447. static noinline int copy_to_sk(struct btrfs_root *root,
  1448. struct btrfs_path *path,
  1449. struct btrfs_key *key,
  1450. struct btrfs_ioctl_search_key *sk,
  1451. char *buf,
  1452. unsigned long *sk_offset,
  1453. int *num_found)
  1454. {
  1455. u64 found_transid;
  1456. struct extent_buffer *leaf;
  1457. struct btrfs_ioctl_search_header sh;
  1458. unsigned long item_off;
  1459. unsigned long item_len;
  1460. int nritems;
  1461. int i;
  1462. int slot;
  1463. int ret = 0;
  1464. leaf = path->nodes[0];
  1465. slot = path->slots[0];
  1466. nritems = btrfs_header_nritems(leaf);
  1467. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1468. i = nritems;
  1469. goto advance_key;
  1470. }
  1471. found_transid = btrfs_header_generation(leaf);
  1472. for (i = slot; i < nritems; i++) {
  1473. item_off = btrfs_item_ptr_offset(leaf, i);
  1474. item_len = btrfs_item_size_nr(leaf, i);
  1475. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1476. item_len = 0;
  1477. if (sizeof(sh) + item_len + *sk_offset >
  1478. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1479. ret = 1;
  1480. goto overflow;
  1481. }
  1482. btrfs_item_key_to_cpu(leaf, key, i);
  1483. if (!key_in_sk(key, sk))
  1484. continue;
  1485. sh.objectid = key->objectid;
  1486. sh.offset = key->offset;
  1487. sh.type = key->type;
  1488. sh.len = item_len;
  1489. sh.transid = found_transid;
  1490. /* copy search result header */
  1491. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1492. *sk_offset += sizeof(sh);
  1493. if (item_len) {
  1494. char *p = buf + *sk_offset;
  1495. /* copy the item */
  1496. read_extent_buffer(leaf, p,
  1497. item_off, item_len);
  1498. *sk_offset += item_len;
  1499. }
  1500. (*num_found)++;
  1501. if (*num_found >= sk->nr_items)
  1502. break;
  1503. }
  1504. advance_key:
  1505. ret = 0;
  1506. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1507. key->offset++;
  1508. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1509. key->offset = 0;
  1510. key->type++;
  1511. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1512. key->offset = 0;
  1513. key->type = 0;
  1514. key->objectid++;
  1515. } else
  1516. ret = 1;
  1517. overflow:
  1518. return ret;
  1519. }
  1520. static noinline int search_ioctl(struct inode *inode,
  1521. struct btrfs_ioctl_search_args *args)
  1522. {
  1523. struct btrfs_root *root;
  1524. struct btrfs_key key;
  1525. struct btrfs_key max_key;
  1526. struct btrfs_path *path;
  1527. struct btrfs_ioctl_search_key *sk = &args->key;
  1528. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1529. int ret;
  1530. int num_found = 0;
  1531. unsigned long sk_offset = 0;
  1532. path = btrfs_alloc_path();
  1533. if (!path)
  1534. return -ENOMEM;
  1535. if (sk->tree_id == 0) {
  1536. /* search the root of the inode that was passed */
  1537. root = BTRFS_I(inode)->root;
  1538. } else {
  1539. key.objectid = sk->tree_id;
  1540. key.type = BTRFS_ROOT_ITEM_KEY;
  1541. key.offset = (u64)-1;
  1542. root = btrfs_read_fs_root_no_name(info, &key);
  1543. if (IS_ERR(root)) {
  1544. printk(KERN_ERR "could not find root %llu\n",
  1545. sk->tree_id);
  1546. btrfs_free_path(path);
  1547. return -ENOENT;
  1548. }
  1549. }
  1550. key.objectid = sk->min_objectid;
  1551. key.type = sk->min_type;
  1552. key.offset = sk->min_offset;
  1553. max_key.objectid = sk->max_objectid;
  1554. max_key.type = sk->max_type;
  1555. max_key.offset = sk->max_offset;
  1556. path->keep_locks = 1;
  1557. while(1) {
  1558. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1559. sk->min_transid);
  1560. if (ret != 0) {
  1561. if (ret > 0)
  1562. ret = 0;
  1563. goto err;
  1564. }
  1565. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1566. &sk_offset, &num_found);
  1567. btrfs_release_path(path);
  1568. if (ret || num_found >= sk->nr_items)
  1569. break;
  1570. }
  1571. ret = 0;
  1572. err:
  1573. sk->nr_items = num_found;
  1574. btrfs_free_path(path);
  1575. return ret;
  1576. }
  1577. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1578. void __user *argp)
  1579. {
  1580. struct btrfs_ioctl_search_args *args;
  1581. struct inode *inode;
  1582. int ret;
  1583. if (!capable(CAP_SYS_ADMIN))
  1584. return -EPERM;
  1585. args = memdup_user(argp, sizeof(*args));
  1586. if (IS_ERR(args))
  1587. return PTR_ERR(args);
  1588. inode = fdentry(file)->d_inode;
  1589. ret = search_ioctl(inode, args);
  1590. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1591. ret = -EFAULT;
  1592. kfree(args);
  1593. return ret;
  1594. }
  1595. /*
  1596. * Search INODE_REFs to identify path name of 'dirid' directory
  1597. * in a 'tree_id' tree. and sets path name to 'name'.
  1598. */
  1599. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1600. u64 tree_id, u64 dirid, char *name)
  1601. {
  1602. struct btrfs_root *root;
  1603. struct btrfs_key key;
  1604. char *ptr;
  1605. int ret = -1;
  1606. int slot;
  1607. int len;
  1608. int total_len = 0;
  1609. struct btrfs_inode_ref *iref;
  1610. struct extent_buffer *l;
  1611. struct btrfs_path *path;
  1612. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1613. name[0]='\0';
  1614. return 0;
  1615. }
  1616. path = btrfs_alloc_path();
  1617. if (!path)
  1618. return -ENOMEM;
  1619. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1620. key.objectid = tree_id;
  1621. key.type = BTRFS_ROOT_ITEM_KEY;
  1622. key.offset = (u64)-1;
  1623. root = btrfs_read_fs_root_no_name(info, &key);
  1624. if (IS_ERR(root)) {
  1625. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1626. ret = -ENOENT;
  1627. goto out;
  1628. }
  1629. key.objectid = dirid;
  1630. key.type = BTRFS_INODE_REF_KEY;
  1631. key.offset = (u64)-1;
  1632. while(1) {
  1633. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1634. if (ret < 0)
  1635. goto out;
  1636. l = path->nodes[0];
  1637. slot = path->slots[0];
  1638. if (ret > 0 && slot > 0)
  1639. slot--;
  1640. btrfs_item_key_to_cpu(l, &key, slot);
  1641. if (ret > 0 && (key.objectid != dirid ||
  1642. key.type != BTRFS_INODE_REF_KEY)) {
  1643. ret = -ENOENT;
  1644. goto out;
  1645. }
  1646. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1647. len = btrfs_inode_ref_name_len(l, iref);
  1648. ptr -= len + 1;
  1649. total_len += len + 1;
  1650. if (ptr < name)
  1651. goto out;
  1652. *(ptr + len) = '/';
  1653. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1654. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1655. break;
  1656. btrfs_release_path(path);
  1657. key.objectid = key.offset;
  1658. key.offset = (u64)-1;
  1659. dirid = key.objectid;
  1660. }
  1661. if (ptr < name)
  1662. goto out;
  1663. memmove(name, ptr, total_len);
  1664. name[total_len]='\0';
  1665. ret = 0;
  1666. out:
  1667. btrfs_free_path(path);
  1668. return ret;
  1669. }
  1670. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1671. void __user *argp)
  1672. {
  1673. struct btrfs_ioctl_ino_lookup_args *args;
  1674. struct inode *inode;
  1675. int ret;
  1676. if (!capable(CAP_SYS_ADMIN))
  1677. return -EPERM;
  1678. args = memdup_user(argp, sizeof(*args));
  1679. if (IS_ERR(args))
  1680. return PTR_ERR(args);
  1681. inode = fdentry(file)->d_inode;
  1682. if (args->treeid == 0)
  1683. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1684. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1685. args->treeid, args->objectid,
  1686. args->name);
  1687. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1688. ret = -EFAULT;
  1689. kfree(args);
  1690. return ret;
  1691. }
  1692. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1693. void __user *arg)
  1694. {
  1695. struct dentry *parent = fdentry(file);
  1696. struct dentry *dentry;
  1697. struct inode *dir = parent->d_inode;
  1698. struct inode *inode;
  1699. struct btrfs_root *root = BTRFS_I(dir)->root;
  1700. struct btrfs_root *dest = NULL;
  1701. struct btrfs_ioctl_vol_args *vol_args;
  1702. struct btrfs_trans_handle *trans;
  1703. int namelen;
  1704. int ret;
  1705. int err = 0;
  1706. vol_args = memdup_user(arg, sizeof(*vol_args));
  1707. if (IS_ERR(vol_args))
  1708. return PTR_ERR(vol_args);
  1709. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1710. namelen = strlen(vol_args->name);
  1711. if (strchr(vol_args->name, '/') ||
  1712. strncmp(vol_args->name, "..", namelen) == 0) {
  1713. err = -EINVAL;
  1714. goto out;
  1715. }
  1716. err = mnt_want_write_file(file);
  1717. if (err)
  1718. goto out;
  1719. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1720. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1721. if (IS_ERR(dentry)) {
  1722. err = PTR_ERR(dentry);
  1723. goto out_unlock_dir;
  1724. }
  1725. if (!dentry->d_inode) {
  1726. err = -ENOENT;
  1727. goto out_dput;
  1728. }
  1729. inode = dentry->d_inode;
  1730. dest = BTRFS_I(inode)->root;
  1731. if (!capable(CAP_SYS_ADMIN)){
  1732. /*
  1733. * Regular user. Only allow this with a special mount
  1734. * option, when the user has write+exec access to the
  1735. * subvol root, and when rmdir(2) would have been
  1736. * allowed.
  1737. *
  1738. * Note that this is _not_ check that the subvol is
  1739. * empty or doesn't contain data that we wouldn't
  1740. * otherwise be able to delete.
  1741. *
  1742. * Users who want to delete empty subvols should try
  1743. * rmdir(2).
  1744. */
  1745. err = -EPERM;
  1746. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1747. goto out_dput;
  1748. /*
  1749. * Do not allow deletion if the parent dir is the same
  1750. * as the dir to be deleted. That means the ioctl
  1751. * must be called on the dentry referencing the root
  1752. * of the subvol, not a random directory contained
  1753. * within it.
  1754. */
  1755. err = -EINVAL;
  1756. if (root == dest)
  1757. goto out_dput;
  1758. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1759. if (err)
  1760. goto out_dput;
  1761. /* check if subvolume may be deleted by a non-root user */
  1762. err = btrfs_may_delete(dir, dentry, 1);
  1763. if (err)
  1764. goto out_dput;
  1765. }
  1766. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1767. err = -EINVAL;
  1768. goto out_dput;
  1769. }
  1770. mutex_lock(&inode->i_mutex);
  1771. err = d_invalidate(dentry);
  1772. if (err)
  1773. goto out_unlock;
  1774. down_write(&root->fs_info->subvol_sem);
  1775. err = may_destroy_subvol(dest);
  1776. if (err)
  1777. goto out_up_write;
  1778. trans = btrfs_start_transaction(root, 0);
  1779. if (IS_ERR(trans)) {
  1780. err = PTR_ERR(trans);
  1781. goto out_up_write;
  1782. }
  1783. trans->block_rsv = &root->fs_info->global_block_rsv;
  1784. ret = btrfs_unlink_subvol(trans, root, dir,
  1785. dest->root_key.objectid,
  1786. dentry->d_name.name,
  1787. dentry->d_name.len);
  1788. if (ret) {
  1789. err = ret;
  1790. btrfs_abort_transaction(trans, root, ret);
  1791. goto out_end_trans;
  1792. }
  1793. btrfs_record_root_in_trans(trans, dest);
  1794. memset(&dest->root_item.drop_progress, 0,
  1795. sizeof(dest->root_item.drop_progress));
  1796. dest->root_item.drop_level = 0;
  1797. btrfs_set_root_refs(&dest->root_item, 0);
  1798. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1799. ret = btrfs_insert_orphan_item(trans,
  1800. root->fs_info->tree_root,
  1801. dest->root_key.objectid);
  1802. if (ret) {
  1803. btrfs_abort_transaction(trans, root, ret);
  1804. err = ret;
  1805. goto out_end_trans;
  1806. }
  1807. }
  1808. out_end_trans:
  1809. ret = btrfs_end_transaction(trans, root);
  1810. if (ret && !err)
  1811. err = ret;
  1812. inode->i_flags |= S_DEAD;
  1813. out_up_write:
  1814. up_write(&root->fs_info->subvol_sem);
  1815. out_unlock:
  1816. mutex_unlock(&inode->i_mutex);
  1817. if (!err) {
  1818. shrink_dcache_sb(root->fs_info->sb);
  1819. btrfs_invalidate_inodes(dest);
  1820. d_delete(dentry);
  1821. }
  1822. out_dput:
  1823. dput(dentry);
  1824. out_unlock_dir:
  1825. mutex_unlock(&dir->i_mutex);
  1826. mnt_drop_write_file(file);
  1827. out:
  1828. kfree(vol_args);
  1829. return err;
  1830. }
  1831. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1832. {
  1833. struct inode *inode = fdentry(file)->d_inode;
  1834. struct btrfs_root *root = BTRFS_I(inode)->root;
  1835. struct btrfs_ioctl_defrag_range_args *range;
  1836. int ret;
  1837. if (btrfs_root_readonly(root))
  1838. return -EROFS;
  1839. ret = mnt_want_write_file(file);
  1840. if (ret)
  1841. return ret;
  1842. switch (inode->i_mode & S_IFMT) {
  1843. case S_IFDIR:
  1844. if (!capable(CAP_SYS_ADMIN)) {
  1845. ret = -EPERM;
  1846. goto out;
  1847. }
  1848. ret = btrfs_defrag_root(root, 0);
  1849. if (ret)
  1850. goto out;
  1851. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1852. break;
  1853. case S_IFREG:
  1854. if (!(file->f_mode & FMODE_WRITE)) {
  1855. ret = -EINVAL;
  1856. goto out;
  1857. }
  1858. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1859. if (!range) {
  1860. ret = -ENOMEM;
  1861. goto out;
  1862. }
  1863. if (argp) {
  1864. if (copy_from_user(range, argp,
  1865. sizeof(*range))) {
  1866. ret = -EFAULT;
  1867. kfree(range);
  1868. goto out;
  1869. }
  1870. /* compression requires us to start the IO */
  1871. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1872. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1873. range->extent_thresh = (u32)-1;
  1874. }
  1875. } else {
  1876. /* the rest are all set to zero by kzalloc */
  1877. range->len = (u64)-1;
  1878. }
  1879. ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
  1880. range, 0, 0);
  1881. if (ret > 0)
  1882. ret = 0;
  1883. kfree(range);
  1884. break;
  1885. default:
  1886. ret = -EINVAL;
  1887. }
  1888. out:
  1889. mnt_drop_write_file(file);
  1890. return ret;
  1891. }
  1892. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1893. {
  1894. struct btrfs_ioctl_vol_args *vol_args;
  1895. int ret;
  1896. if (!capable(CAP_SYS_ADMIN))
  1897. return -EPERM;
  1898. mutex_lock(&root->fs_info->volume_mutex);
  1899. if (root->fs_info->balance_ctl) {
  1900. printk(KERN_INFO "btrfs: balance in progress\n");
  1901. ret = -EINVAL;
  1902. goto out;
  1903. }
  1904. vol_args = memdup_user(arg, sizeof(*vol_args));
  1905. if (IS_ERR(vol_args)) {
  1906. ret = PTR_ERR(vol_args);
  1907. goto out;
  1908. }
  1909. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1910. ret = btrfs_init_new_device(root, vol_args->name);
  1911. kfree(vol_args);
  1912. out:
  1913. mutex_unlock(&root->fs_info->volume_mutex);
  1914. return ret;
  1915. }
  1916. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1917. {
  1918. struct btrfs_ioctl_vol_args *vol_args;
  1919. int ret;
  1920. if (!capable(CAP_SYS_ADMIN))
  1921. return -EPERM;
  1922. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1923. return -EROFS;
  1924. mutex_lock(&root->fs_info->volume_mutex);
  1925. if (root->fs_info->balance_ctl) {
  1926. printk(KERN_INFO "btrfs: balance in progress\n");
  1927. ret = -EINVAL;
  1928. goto out;
  1929. }
  1930. vol_args = memdup_user(arg, sizeof(*vol_args));
  1931. if (IS_ERR(vol_args)) {
  1932. ret = PTR_ERR(vol_args);
  1933. goto out;
  1934. }
  1935. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1936. ret = btrfs_rm_device(root, vol_args->name);
  1937. kfree(vol_args);
  1938. out:
  1939. mutex_unlock(&root->fs_info->volume_mutex);
  1940. return ret;
  1941. }
  1942. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  1943. {
  1944. struct btrfs_ioctl_fs_info_args *fi_args;
  1945. struct btrfs_device *device;
  1946. struct btrfs_device *next;
  1947. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1948. int ret = 0;
  1949. if (!capable(CAP_SYS_ADMIN))
  1950. return -EPERM;
  1951. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  1952. if (!fi_args)
  1953. return -ENOMEM;
  1954. fi_args->num_devices = fs_devices->num_devices;
  1955. memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
  1956. mutex_lock(&fs_devices->device_list_mutex);
  1957. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  1958. if (device->devid > fi_args->max_id)
  1959. fi_args->max_id = device->devid;
  1960. }
  1961. mutex_unlock(&fs_devices->device_list_mutex);
  1962. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  1963. ret = -EFAULT;
  1964. kfree(fi_args);
  1965. return ret;
  1966. }
  1967. static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
  1968. {
  1969. struct btrfs_ioctl_dev_info_args *di_args;
  1970. struct btrfs_device *dev;
  1971. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1972. int ret = 0;
  1973. char *s_uuid = NULL;
  1974. char empty_uuid[BTRFS_UUID_SIZE] = {0};
  1975. if (!capable(CAP_SYS_ADMIN))
  1976. return -EPERM;
  1977. di_args = memdup_user(arg, sizeof(*di_args));
  1978. if (IS_ERR(di_args))
  1979. return PTR_ERR(di_args);
  1980. if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
  1981. s_uuid = di_args->uuid;
  1982. mutex_lock(&fs_devices->device_list_mutex);
  1983. dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
  1984. mutex_unlock(&fs_devices->device_list_mutex);
  1985. if (!dev) {
  1986. ret = -ENODEV;
  1987. goto out;
  1988. }
  1989. di_args->devid = dev->devid;
  1990. di_args->bytes_used = dev->bytes_used;
  1991. di_args->total_bytes = dev->total_bytes;
  1992. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  1993. if (dev->name) {
  1994. struct rcu_string *name;
  1995. rcu_read_lock();
  1996. name = rcu_dereference(dev->name);
  1997. strncpy(di_args->path, name->str, sizeof(di_args->path));
  1998. rcu_read_unlock();
  1999. di_args->path[sizeof(di_args->path) - 1] = 0;
  2000. } else {
  2001. di_args->path[0] = '\0';
  2002. }
  2003. out:
  2004. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  2005. ret = -EFAULT;
  2006. kfree(di_args);
  2007. return ret;
  2008. }
  2009. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  2010. u64 off, u64 olen, u64 destoff)
  2011. {
  2012. struct inode *inode = fdentry(file)->d_inode;
  2013. struct btrfs_root *root = BTRFS_I(inode)->root;
  2014. struct file *src_file;
  2015. struct inode *src;
  2016. struct btrfs_trans_handle *trans;
  2017. struct btrfs_path *path;
  2018. struct extent_buffer *leaf;
  2019. char *buf;
  2020. struct btrfs_key key;
  2021. u32 nritems;
  2022. int slot;
  2023. int ret;
  2024. u64 len = olen;
  2025. u64 bs = root->fs_info->sb->s_blocksize;
  2026. /*
  2027. * TODO:
  2028. * - split compressed inline extents. annoying: we need to
  2029. * decompress into destination's address_space (the file offset
  2030. * may change, so source mapping won't do), then recompress (or
  2031. * otherwise reinsert) a subrange.
  2032. * - allow ranges within the same file to be cloned (provided
  2033. * they don't overlap)?
  2034. */
  2035. /* the destination must be opened for writing */
  2036. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  2037. return -EINVAL;
  2038. if (btrfs_root_readonly(root))
  2039. return -EROFS;
  2040. ret = mnt_want_write_file(file);
  2041. if (ret)
  2042. return ret;
  2043. src_file = fget(srcfd);
  2044. if (!src_file) {
  2045. ret = -EBADF;
  2046. goto out_drop_write;
  2047. }
  2048. ret = -EXDEV;
  2049. if (src_file->f_path.mnt != file->f_path.mnt)
  2050. goto out_fput;
  2051. src = src_file->f_dentry->d_inode;
  2052. ret = -EINVAL;
  2053. if (src == inode)
  2054. goto out_fput;
  2055. /* the src must be open for reading */
  2056. if (!(src_file->f_mode & FMODE_READ))
  2057. goto out_fput;
  2058. /* don't make the dst file partly checksummed */
  2059. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  2060. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
  2061. goto out_fput;
  2062. ret = -EISDIR;
  2063. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  2064. goto out_fput;
  2065. ret = -EXDEV;
  2066. if (src->i_sb != inode->i_sb)
  2067. goto out_fput;
  2068. ret = -ENOMEM;
  2069. buf = vmalloc(btrfs_level_size(root, 0));
  2070. if (!buf)
  2071. goto out_fput;
  2072. path = btrfs_alloc_path();
  2073. if (!path) {
  2074. vfree(buf);
  2075. goto out_fput;
  2076. }
  2077. path->reada = 2;
  2078. if (inode < src) {
  2079. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  2080. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  2081. } else {
  2082. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  2083. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  2084. }
  2085. /* determine range to clone */
  2086. ret = -EINVAL;
  2087. if (off + len > src->i_size || off + len < off)
  2088. goto out_unlock;
  2089. if (len == 0)
  2090. olen = len = src->i_size - off;
  2091. /* if we extend to eof, continue to block boundary */
  2092. if (off + len == src->i_size)
  2093. len = ALIGN(src->i_size, bs) - off;
  2094. /* verify the end result is block aligned */
  2095. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  2096. !IS_ALIGNED(destoff, bs))
  2097. goto out_unlock;
  2098. if (destoff > inode->i_size) {
  2099. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  2100. if (ret)
  2101. goto out_unlock;
  2102. }
  2103. /* truncate page cache pages from target inode range */
  2104. truncate_inode_pages_range(&inode->i_data, destoff,
  2105. PAGE_CACHE_ALIGN(destoff + len) - 1);
  2106. /* do any pending delalloc/csum calc on src, one way or
  2107. another, and lock file content */
  2108. while (1) {
  2109. struct btrfs_ordered_extent *ordered;
  2110. lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
  2111. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  2112. if (!ordered &&
  2113. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  2114. EXTENT_DELALLOC, 0, NULL))
  2115. break;
  2116. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
  2117. if (ordered)
  2118. btrfs_put_ordered_extent(ordered);
  2119. btrfs_wait_ordered_range(src, off, len);
  2120. }
  2121. /* clone data */
  2122. key.objectid = btrfs_ino(src);
  2123. key.type = BTRFS_EXTENT_DATA_KEY;
  2124. key.offset = 0;
  2125. while (1) {
  2126. /*
  2127. * note the key will change type as we walk through the
  2128. * tree.
  2129. */
  2130. ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
  2131. 0, 0);
  2132. if (ret < 0)
  2133. goto out;
  2134. nritems = btrfs_header_nritems(path->nodes[0]);
  2135. if (path->slots[0] >= nritems) {
  2136. ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
  2137. if (ret < 0)
  2138. goto out;
  2139. if (ret > 0)
  2140. break;
  2141. nritems = btrfs_header_nritems(path->nodes[0]);
  2142. }
  2143. leaf = path->nodes[0];
  2144. slot = path->slots[0];
  2145. btrfs_item_key_to_cpu(leaf, &key, slot);
  2146. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  2147. key.objectid != btrfs_ino(src))
  2148. break;
  2149. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  2150. struct btrfs_file_extent_item *extent;
  2151. int type;
  2152. u32 size;
  2153. struct btrfs_key new_key;
  2154. u64 disko = 0, diskl = 0;
  2155. u64 datao = 0, datal = 0;
  2156. u8 comp;
  2157. u64 endoff;
  2158. size = btrfs_item_size_nr(leaf, slot);
  2159. read_extent_buffer(leaf, buf,
  2160. btrfs_item_ptr_offset(leaf, slot),
  2161. size);
  2162. extent = btrfs_item_ptr(leaf, slot,
  2163. struct btrfs_file_extent_item);
  2164. comp = btrfs_file_extent_compression(leaf, extent);
  2165. type = btrfs_file_extent_type(leaf, extent);
  2166. if (type == BTRFS_FILE_EXTENT_REG ||
  2167. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2168. disko = btrfs_file_extent_disk_bytenr(leaf,
  2169. extent);
  2170. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  2171. extent);
  2172. datao = btrfs_file_extent_offset(leaf, extent);
  2173. datal = btrfs_file_extent_num_bytes(leaf,
  2174. extent);
  2175. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2176. /* take upper bound, may be compressed */
  2177. datal = btrfs_file_extent_ram_bytes(leaf,
  2178. extent);
  2179. }
  2180. btrfs_release_path(path);
  2181. if (key.offset + datal <= off ||
  2182. key.offset >= off+len)
  2183. goto next;
  2184. memcpy(&new_key, &key, sizeof(new_key));
  2185. new_key.objectid = btrfs_ino(inode);
  2186. if (off <= key.offset)
  2187. new_key.offset = key.offset + destoff - off;
  2188. else
  2189. new_key.offset = destoff;
  2190. /*
  2191. * 1 - adjusting old extent (we may have to split it)
  2192. * 1 - add new extent
  2193. * 1 - inode update
  2194. */
  2195. trans = btrfs_start_transaction(root, 3);
  2196. if (IS_ERR(trans)) {
  2197. ret = PTR_ERR(trans);
  2198. goto out;
  2199. }
  2200. if (type == BTRFS_FILE_EXTENT_REG ||
  2201. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2202. /*
  2203. * a | --- range to clone ---| b
  2204. * | ------------- extent ------------- |
  2205. */
  2206. /* substract range b */
  2207. if (key.offset + datal > off + len)
  2208. datal = off + len - key.offset;
  2209. /* substract range a */
  2210. if (off > key.offset) {
  2211. datao += off - key.offset;
  2212. datal -= off - key.offset;
  2213. }
  2214. ret = btrfs_drop_extents(trans, root, inode,
  2215. new_key.offset,
  2216. new_key.offset + datal,
  2217. 1);
  2218. if (ret) {
  2219. btrfs_abort_transaction(trans, root,
  2220. ret);
  2221. btrfs_end_transaction(trans, root);
  2222. goto out;
  2223. }
  2224. ret = btrfs_insert_empty_item(trans, root, path,
  2225. &new_key, size);
  2226. if (ret) {
  2227. btrfs_abort_transaction(trans, root,
  2228. ret);
  2229. btrfs_end_transaction(trans, root);
  2230. goto out;
  2231. }
  2232. leaf = path->nodes[0];
  2233. slot = path->slots[0];
  2234. write_extent_buffer(leaf, buf,
  2235. btrfs_item_ptr_offset(leaf, slot),
  2236. size);
  2237. extent = btrfs_item_ptr(leaf, slot,
  2238. struct btrfs_file_extent_item);
  2239. /* disko == 0 means it's a hole */
  2240. if (!disko)
  2241. datao = 0;
  2242. btrfs_set_file_extent_offset(leaf, extent,
  2243. datao);
  2244. btrfs_set_file_extent_num_bytes(leaf, extent,
  2245. datal);
  2246. if (disko) {
  2247. inode_add_bytes(inode, datal);
  2248. ret = btrfs_inc_extent_ref(trans, root,
  2249. disko, diskl, 0,
  2250. root->root_key.objectid,
  2251. btrfs_ino(inode),
  2252. new_key.offset - datao,
  2253. 0);
  2254. if (ret) {
  2255. btrfs_abort_transaction(trans,
  2256. root,
  2257. ret);
  2258. btrfs_end_transaction(trans,
  2259. root);
  2260. goto out;
  2261. }
  2262. }
  2263. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2264. u64 skip = 0;
  2265. u64 trim = 0;
  2266. if (off > key.offset) {
  2267. skip = off - key.offset;
  2268. new_key.offset += skip;
  2269. }
  2270. if (key.offset + datal > off+len)
  2271. trim = key.offset + datal - (off+len);
  2272. if (comp && (skip || trim)) {
  2273. ret = -EINVAL;
  2274. btrfs_end_transaction(trans, root);
  2275. goto out;
  2276. }
  2277. size -= skip + trim;
  2278. datal -= skip + trim;
  2279. ret = btrfs_drop_extents(trans, root, inode,
  2280. new_key.offset,
  2281. new_key.offset + datal,
  2282. 1);
  2283. if (ret) {
  2284. btrfs_abort_transaction(trans, root,
  2285. ret);
  2286. btrfs_end_transaction(trans, root);
  2287. goto out;
  2288. }
  2289. ret = btrfs_insert_empty_item(trans, root, path,
  2290. &new_key, size);
  2291. if (ret) {
  2292. btrfs_abort_transaction(trans, root,
  2293. ret);
  2294. btrfs_end_transaction(trans, root);
  2295. goto out;
  2296. }
  2297. if (skip) {
  2298. u32 start =
  2299. btrfs_file_extent_calc_inline_size(0);
  2300. memmove(buf+start, buf+start+skip,
  2301. datal);
  2302. }
  2303. leaf = path->nodes[0];
  2304. slot = path->slots[0];
  2305. write_extent_buffer(leaf, buf,
  2306. btrfs_item_ptr_offset(leaf, slot),
  2307. size);
  2308. inode_add_bytes(inode, datal);
  2309. }
  2310. btrfs_mark_buffer_dirty(leaf);
  2311. btrfs_release_path(path);
  2312. inode_inc_iversion(inode);
  2313. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2314. /*
  2315. * we round up to the block size at eof when
  2316. * determining which extents to clone above,
  2317. * but shouldn't round up the file size
  2318. */
  2319. endoff = new_key.offset + datal;
  2320. if (endoff > destoff+olen)
  2321. endoff = destoff+olen;
  2322. if (endoff > inode->i_size)
  2323. btrfs_i_size_write(inode, endoff);
  2324. ret = btrfs_update_inode(trans, root, inode);
  2325. if (ret) {
  2326. btrfs_abort_transaction(trans, root, ret);
  2327. btrfs_end_transaction(trans, root);
  2328. goto out;
  2329. }
  2330. ret = btrfs_end_transaction(trans, root);
  2331. }
  2332. next:
  2333. btrfs_release_path(path);
  2334. key.offset++;
  2335. }
  2336. ret = 0;
  2337. out:
  2338. btrfs_release_path(path);
  2339. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
  2340. out_unlock:
  2341. mutex_unlock(&src->i_mutex);
  2342. mutex_unlock(&inode->i_mutex);
  2343. vfree(buf);
  2344. btrfs_free_path(path);
  2345. out_fput:
  2346. fput(src_file);
  2347. out_drop_write:
  2348. mnt_drop_write_file(file);
  2349. return ret;
  2350. }
  2351. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  2352. {
  2353. struct btrfs_ioctl_clone_range_args args;
  2354. if (copy_from_user(&args, argp, sizeof(args)))
  2355. return -EFAULT;
  2356. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  2357. args.src_length, args.dest_offset);
  2358. }
  2359. /*
  2360. * there are many ways the trans_start and trans_end ioctls can lead
  2361. * to deadlocks. They should only be used by applications that
  2362. * basically own the machine, and have a very in depth understanding
  2363. * of all the possible deadlocks and enospc problems.
  2364. */
  2365. static long btrfs_ioctl_trans_start(struct file *file)
  2366. {
  2367. struct inode *inode = fdentry(file)->d_inode;
  2368. struct btrfs_root *root = BTRFS_I(inode)->root;
  2369. struct btrfs_trans_handle *trans;
  2370. int ret;
  2371. ret = -EPERM;
  2372. if (!capable(CAP_SYS_ADMIN))
  2373. goto out;
  2374. ret = -EINPROGRESS;
  2375. if (file->private_data)
  2376. goto out;
  2377. ret = -EROFS;
  2378. if (btrfs_root_readonly(root))
  2379. goto out;
  2380. ret = mnt_want_write_file(file);
  2381. if (ret)
  2382. goto out;
  2383. atomic_inc(&root->fs_info->open_ioctl_trans);
  2384. ret = -ENOMEM;
  2385. trans = btrfs_start_ioctl_transaction(root);
  2386. if (IS_ERR(trans))
  2387. goto out_drop;
  2388. file->private_data = trans;
  2389. return 0;
  2390. out_drop:
  2391. atomic_dec(&root->fs_info->open_ioctl_trans);
  2392. mnt_drop_write_file(file);
  2393. out:
  2394. return ret;
  2395. }
  2396. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  2397. {
  2398. struct inode *inode = fdentry(file)->d_inode;
  2399. struct btrfs_root *root = BTRFS_I(inode)->root;
  2400. struct btrfs_root *new_root;
  2401. struct btrfs_dir_item *di;
  2402. struct btrfs_trans_handle *trans;
  2403. struct btrfs_path *path;
  2404. struct btrfs_key location;
  2405. struct btrfs_disk_key disk_key;
  2406. u64 objectid = 0;
  2407. u64 dir_id;
  2408. if (!capable(CAP_SYS_ADMIN))
  2409. return -EPERM;
  2410. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  2411. return -EFAULT;
  2412. if (!objectid)
  2413. objectid = root->root_key.objectid;
  2414. location.objectid = objectid;
  2415. location.type = BTRFS_ROOT_ITEM_KEY;
  2416. location.offset = (u64)-1;
  2417. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  2418. if (IS_ERR(new_root))
  2419. return PTR_ERR(new_root);
  2420. if (btrfs_root_refs(&new_root->root_item) == 0)
  2421. return -ENOENT;
  2422. path = btrfs_alloc_path();
  2423. if (!path)
  2424. return -ENOMEM;
  2425. path->leave_spinning = 1;
  2426. trans = btrfs_start_transaction(root, 1);
  2427. if (IS_ERR(trans)) {
  2428. btrfs_free_path(path);
  2429. return PTR_ERR(trans);
  2430. }
  2431. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  2432. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  2433. dir_id, "default", 7, 1);
  2434. if (IS_ERR_OR_NULL(di)) {
  2435. btrfs_free_path(path);
  2436. btrfs_end_transaction(trans, root);
  2437. printk(KERN_ERR "Umm, you don't have the default dir item, "
  2438. "this isn't going to work\n");
  2439. return -ENOENT;
  2440. }
  2441. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  2442. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  2443. btrfs_mark_buffer_dirty(path->nodes[0]);
  2444. btrfs_free_path(path);
  2445. btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
  2446. btrfs_end_transaction(trans, root);
  2447. return 0;
  2448. }
  2449. static void get_block_group_info(struct list_head *groups_list,
  2450. struct btrfs_ioctl_space_info *space)
  2451. {
  2452. struct btrfs_block_group_cache *block_group;
  2453. space->total_bytes = 0;
  2454. space->used_bytes = 0;
  2455. space->flags = 0;
  2456. list_for_each_entry(block_group, groups_list, list) {
  2457. space->flags = block_group->flags;
  2458. space->total_bytes += block_group->key.offset;
  2459. space->used_bytes +=
  2460. btrfs_block_group_used(&block_group->item);
  2461. }
  2462. }
  2463. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  2464. {
  2465. struct btrfs_ioctl_space_args space_args;
  2466. struct btrfs_ioctl_space_info space;
  2467. struct btrfs_ioctl_space_info *dest;
  2468. struct btrfs_ioctl_space_info *dest_orig;
  2469. struct btrfs_ioctl_space_info __user *user_dest;
  2470. struct btrfs_space_info *info;
  2471. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  2472. BTRFS_BLOCK_GROUP_SYSTEM,
  2473. BTRFS_BLOCK_GROUP_METADATA,
  2474. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  2475. int num_types = 4;
  2476. int alloc_size;
  2477. int ret = 0;
  2478. u64 slot_count = 0;
  2479. int i, c;
  2480. if (copy_from_user(&space_args,
  2481. (struct btrfs_ioctl_space_args __user *)arg,
  2482. sizeof(space_args)))
  2483. return -EFAULT;
  2484. for (i = 0; i < num_types; i++) {
  2485. struct btrfs_space_info *tmp;
  2486. info = NULL;
  2487. rcu_read_lock();
  2488. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2489. list) {
  2490. if (tmp->flags == types[i]) {
  2491. info = tmp;
  2492. break;
  2493. }
  2494. }
  2495. rcu_read_unlock();
  2496. if (!info)
  2497. continue;
  2498. down_read(&info->groups_sem);
  2499. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2500. if (!list_empty(&info->block_groups[c]))
  2501. slot_count++;
  2502. }
  2503. up_read(&info->groups_sem);
  2504. }
  2505. /* space_slots == 0 means they are asking for a count */
  2506. if (space_args.space_slots == 0) {
  2507. space_args.total_spaces = slot_count;
  2508. goto out;
  2509. }
  2510. slot_count = min_t(u64, space_args.space_slots, slot_count);
  2511. alloc_size = sizeof(*dest) * slot_count;
  2512. /* we generally have at most 6 or so space infos, one for each raid
  2513. * level. So, a whole page should be more than enough for everyone
  2514. */
  2515. if (alloc_size > PAGE_CACHE_SIZE)
  2516. return -ENOMEM;
  2517. space_args.total_spaces = 0;
  2518. dest = kmalloc(alloc_size, GFP_NOFS);
  2519. if (!dest)
  2520. return -ENOMEM;
  2521. dest_orig = dest;
  2522. /* now we have a buffer to copy into */
  2523. for (i = 0; i < num_types; i++) {
  2524. struct btrfs_space_info *tmp;
  2525. if (!slot_count)
  2526. break;
  2527. info = NULL;
  2528. rcu_read_lock();
  2529. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2530. list) {
  2531. if (tmp->flags == types[i]) {
  2532. info = tmp;
  2533. break;
  2534. }
  2535. }
  2536. rcu_read_unlock();
  2537. if (!info)
  2538. continue;
  2539. down_read(&info->groups_sem);
  2540. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2541. if (!list_empty(&info->block_groups[c])) {
  2542. get_block_group_info(&info->block_groups[c],
  2543. &space);
  2544. memcpy(dest, &space, sizeof(space));
  2545. dest++;
  2546. space_args.total_spaces++;
  2547. slot_count--;
  2548. }
  2549. if (!slot_count)
  2550. break;
  2551. }
  2552. up_read(&info->groups_sem);
  2553. }
  2554. user_dest = (struct btrfs_ioctl_space_info __user *)
  2555. (arg + sizeof(struct btrfs_ioctl_space_args));
  2556. if (copy_to_user(user_dest, dest_orig, alloc_size))
  2557. ret = -EFAULT;
  2558. kfree(dest_orig);
  2559. out:
  2560. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  2561. ret = -EFAULT;
  2562. return ret;
  2563. }
  2564. /*
  2565. * there are many ways the trans_start and trans_end ioctls can lead
  2566. * to deadlocks. They should only be used by applications that
  2567. * basically own the machine, and have a very in depth understanding
  2568. * of all the possible deadlocks and enospc problems.
  2569. */
  2570. long btrfs_ioctl_trans_end(struct file *file)
  2571. {
  2572. struct inode *inode = fdentry(file)->d_inode;
  2573. struct btrfs_root *root = BTRFS_I(inode)->root;
  2574. struct btrfs_trans_handle *trans;
  2575. trans = file->private_data;
  2576. if (!trans)
  2577. return -EINVAL;
  2578. file->private_data = NULL;
  2579. btrfs_end_transaction(trans, root);
  2580. atomic_dec(&root->fs_info->open_ioctl_trans);
  2581. mnt_drop_write_file(file);
  2582. return 0;
  2583. }
  2584. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  2585. {
  2586. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2587. struct btrfs_trans_handle *trans;
  2588. u64 transid;
  2589. int ret;
  2590. trans = btrfs_start_transaction(root, 0);
  2591. if (IS_ERR(trans))
  2592. return PTR_ERR(trans);
  2593. transid = trans->transid;
  2594. ret = btrfs_commit_transaction_async(trans, root, 0);
  2595. if (ret) {
  2596. btrfs_end_transaction(trans, root);
  2597. return ret;
  2598. }
  2599. if (argp)
  2600. if (copy_to_user(argp, &transid, sizeof(transid)))
  2601. return -EFAULT;
  2602. return 0;
  2603. }
  2604. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  2605. {
  2606. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2607. u64 transid;
  2608. if (argp) {
  2609. if (copy_from_user(&transid, argp, sizeof(transid)))
  2610. return -EFAULT;
  2611. } else {
  2612. transid = 0; /* current trans */
  2613. }
  2614. return btrfs_wait_for_commit(root, transid);
  2615. }
  2616. static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
  2617. {
  2618. int ret;
  2619. struct btrfs_ioctl_scrub_args *sa;
  2620. if (!capable(CAP_SYS_ADMIN))
  2621. return -EPERM;
  2622. sa = memdup_user(arg, sizeof(*sa));
  2623. if (IS_ERR(sa))
  2624. return PTR_ERR(sa);
  2625. ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
  2626. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
  2627. if (copy_to_user(arg, sa, sizeof(*sa)))
  2628. ret = -EFAULT;
  2629. kfree(sa);
  2630. return ret;
  2631. }
  2632. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  2633. {
  2634. if (!capable(CAP_SYS_ADMIN))
  2635. return -EPERM;
  2636. return btrfs_scrub_cancel(root);
  2637. }
  2638. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  2639. void __user *arg)
  2640. {
  2641. struct btrfs_ioctl_scrub_args *sa;
  2642. int ret;
  2643. if (!capable(CAP_SYS_ADMIN))
  2644. return -EPERM;
  2645. sa = memdup_user(arg, sizeof(*sa));
  2646. if (IS_ERR(sa))
  2647. return PTR_ERR(sa);
  2648. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  2649. if (copy_to_user(arg, sa, sizeof(*sa)))
  2650. ret = -EFAULT;
  2651. kfree(sa);
  2652. return ret;
  2653. }
  2654. static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
  2655. void __user *arg)
  2656. {
  2657. struct btrfs_ioctl_get_dev_stats *sa;
  2658. int ret;
  2659. sa = memdup_user(arg, sizeof(*sa));
  2660. if (IS_ERR(sa))
  2661. return PTR_ERR(sa);
  2662. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  2663. kfree(sa);
  2664. return -EPERM;
  2665. }
  2666. ret = btrfs_get_dev_stats(root, sa);
  2667. if (copy_to_user(arg, sa, sizeof(*sa)))
  2668. ret = -EFAULT;
  2669. kfree(sa);
  2670. return ret;
  2671. }
  2672. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  2673. {
  2674. int ret = 0;
  2675. int i;
  2676. u64 rel_ptr;
  2677. int size;
  2678. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  2679. struct inode_fs_paths *ipath = NULL;
  2680. struct btrfs_path *path;
  2681. if (!capable(CAP_SYS_ADMIN))
  2682. return -EPERM;
  2683. path = btrfs_alloc_path();
  2684. if (!path) {
  2685. ret = -ENOMEM;
  2686. goto out;
  2687. }
  2688. ipa = memdup_user(arg, sizeof(*ipa));
  2689. if (IS_ERR(ipa)) {
  2690. ret = PTR_ERR(ipa);
  2691. ipa = NULL;
  2692. goto out;
  2693. }
  2694. size = min_t(u32, ipa->size, 4096);
  2695. ipath = init_ipath(size, root, path);
  2696. if (IS_ERR(ipath)) {
  2697. ret = PTR_ERR(ipath);
  2698. ipath = NULL;
  2699. goto out;
  2700. }
  2701. ret = paths_from_inode(ipa->inum, ipath);
  2702. if (ret < 0)
  2703. goto out;
  2704. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  2705. rel_ptr = ipath->fspath->val[i] -
  2706. (u64)(unsigned long)ipath->fspath->val;
  2707. ipath->fspath->val[i] = rel_ptr;
  2708. }
  2709. ret = copy_to_user((void *)(unsigned long)ipa->fspath,
  2710. (void *)(unsigned long)ipath->fspath, size);
  2711. if (ret) {
  2712. ret = -EFAULT;
  2713. goto out;
  2714. }
  2715. out:
  2716. btrfs_free_path(path);
  2717. free_ipath(ipath);
  2718. kfree(ipa);
  2719. return ret;
  2720. }
  2721. static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
  2722. {
  2723. struct btrfs_data_container *inodes = ctx;
  2724. const size_t c = 3 * sizeof(u64);
  2725. if (inodes->bytes_left >= c) {
  2726. inodes->bytes_left -= c;
  2727. inodes->val[inodes->elem_cnt] = inum;
  2728. inodes->val[inodes->elem_cnt + 1] = offset;
  2729. inodes->val[inodes->elem_cnt + 2] = root;
  2730. inodes->elem_cnt += 3;
  2731. } else {
  2732. inodes->bytes_missing += c - inodes->bytes_left;
  2733. inodes->bytes_left = 0;
  2734. inodes->elem_missed += 3;
  2735. }
  2736. return 0;
  2737. }
  2738. static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
  2739. void __user *arg)
  2740. {
  2741. int ret = 0;
  2742. int size;
  2743. u64 extent_item_pos;
  2744. struct btrfs_ioctl_logical_ino_args *loi;
  2745. struct btrfs_data_container *inodes = NULL;
  2746. struct btrfs_path *path = NULL;
  2747. struct btrfs_key key;
  2748. if (!capable(CAP_SYS_ADMIN))
  2749. return -EPERM;
  2750. loi = memdup_user(arg, sizeof(*loi));
  2751. if (IS_ERR(loi)) {
  2752. ret = PTR_ERR(loi);
  2753. loi = NULL;
  2754. goto out;
  2755. }
  2756. path = btrfs_alloc_path();
  2757. if (!path) {
  2758. ret = -ENOMEM;
  2759. goto out;
  2760. }
  2761. size = min_t(u32, loi->size, 4096);
  2762. inodes = init_data_container(size);
  2763. if (IS_ERR(inodes)) {
  2764. ret = PTR_ERR(inodes);
  2765. inodes = NULL;
  2766. goto out;
  2767. }
  2768. ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
  2769. btrfs_release_path(path);
  2770. if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
  2771. ret = -ENOENT;
  2772. if (ret < 0)
  2773. goto out;
  2774. extent_item_pos = loi->logical - key.objectid;
  2775. ret = iterate_extent_inodes(root->fs_info, key.objectid,
  2776. extent_item_pos, 0, build_ino_list,
  2777. inodes);
  2778. if (ret < 0)
  2779. goto out;
  2780. ret = copy_to_user((void *)(unsigned long)loi->inodes,
  2781. (void *)(unsigned long)inodes, size);
  2782. if (ret)
  2783. ret = -EFAULT;
  2784. out:
  2785. btrfs_free_path(path);
  2786. kfree(inodes);
  2787. kfree(loi);
  2788. return ret;
  2789. }
  2790. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  2791. struct btrfs_ioctl_balance_args *bargs)
  2792. {
  2793. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2794. bargs->flags = bctl->flags;
  2795. if (atomic_read(&fs_info->balance_running))
  2796. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  2797. if (atomic_read(&fs_info->balance_pause_req))
  2798. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  2799. if (atomic_read(&fs_info->balance_cancel_req))
  2800. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  2801. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  2802. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  2803. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  2804. if (lock) {
  2805. spin_lock(&fs_info->balance_lock);
  2806. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  2807. spin_unlock(&fs_info->balance_lock);
  2808. } else {
  2809. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  2810. }
  2811. }
  2812. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  2813. {
  2814. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2815. struct btrfs_fs_info *fs_info = root->fs_info;
  2816. struct btrfs_ioctl_balance_args *bargs;
  2817. struct btrfs_balance_control *bctl;
  2818. int ret;
  2819. if (!capable(CAP_SYS_ADMIN))
  2820. return -EPERM;
  2821. ret = mnt_want_write_file(file);
  2822. if (ret)
  2823. return ret;
  2824. mutex_lock(&fs_info->volume_mutex);
  2825. mutex_lock(&fs_info->balance_mutex);
  2826. if (arg) {
  2827. bargs = memdup_user(arg, sizeof(*bargs));
  2828. if (IS_ERR(bargs)) {
  2829. ret = PTR_ERR(bargs);
  2830. goto out;
  2831. }
  2832. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  2833. if (!fs_info->balance_ctl) {
  2834. ret = -ENOTCONN;
  2835. goto out_bargs;
  2836. }
  2837. bctl = fs_info->balance_ctl;
  2838. spin_lock(&fs_info->balance_lock);
  2839. bctl->flags |= BTRFS_BALANCE_RESUME;
  2840. spin_unlock(&fs_info->balance_lock);
  2841. goto do_balance;
  2842. }
  2843. } else {
  2844. bargs = NULL;
  2845. }
  2846. if (fs_info->balance_ctl) {
  2847. ret = -EINPROGRESS;
  2848. goto out_bargs;
  2849. }
  2850. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  2851. if (!bctl) {
  2852. ret = -ENOMEM;
  2853. goto out_bargs;
  2854. }
  2855. bctl->fs_info = fs_info;
  2856. if (arg) {
  2857. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  2858. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  2859. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  2860. bctl->flags = bargs->flags;
  2861. } else {
  2862. /* balance everything - no filters */
  2863. bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
  2864. }
  2865. do_balance:
  2866. ret = btrfs_balance(bctl, bargs);
  2867. /*
  2868. * bctl is freed in __cancel_balance or in free_fs_info if
  2869. * restriper was paused all the way until unmount
  2870. */
  2871. if (arg) {
  2872. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  2873. ret = -EFAULT;
  2874. }
  2875. out_bargs:
  2876. kfree(bargs);
  2877. out:
  2878. mutex_unlock(&fs_info->balance_mutex);
  2879. mutex_unlock(&fs_info->volume_mutex);
  2880. mnt_drop_write_file(file);
  2881. return ret;
  2882. }
  2883. static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
  2884. {
  2885. if (!capable(CAP_SYS_ADMIN))
  2886. return -EPERM;
  2887. switch (cmd) {
  2888. case BTRFS_BALANCE_CTL_PAUSE:
  2889. return btrfs_pause_balance(root->fs_info);
  2890. case BTRFS_BALANCE_CTL_CANCEL:
  2891. return btrfs_cancel_balance(root->fs_info);
  2892. }
  2893. return -EINVAL;
  2894. }
  2895. static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
  2896. void __user *arg)
  2897. {
  2898. struct btrfs_fs_info *fs_info = root->fs_info;
  2899. struct btrfs_ioctl_balance_args *bargs;
  2900. int ret = 0;
  2901. if (!capable(CAP_SYS_ADMIN))
  2902. return -EPERM;
  2903. mutex_lock(&fs_info->balance_mutex);
  2904. if (!fs_info->balance_ctl) {
  2905. ret = -ENOTCONN;
  2906. goto out;
  2907. }
  2908. bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
  2909. if (!bargs) {
  2910. ret = -ENOMEM;
  2911. goto out;
  2912. }
  2913. update_ioctl_balance_args(fs_info, 1, bargs);
  2914. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  2915. ret = -EFAULT;
  2916. kfree(bargs);
  2917. out:
  2918. mutex_unlock(&fs_info->balance_mutex);
  2919. return ret;
  2920. }
  2921. static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
  2922. {
  2923. struct btrfs_ioctl_quota_ctl_args *sa;
  2924. struct btrfs_trans_handle *trans = NULL;
  2925. int ret;
  2926. int err;
  2927. if (!capable(CAP_SYS_ADMIN))
  2928. return -EPERM;
  2929. if (root->fs_info->sb->s_flags & MS_RDONLY)
  2930. return -EROFS;
  2931. sa = memdup_user(arg, sizeof(*sa));
  2932. if (IS_ERR(sa))
  2933. return PTR_ERR(sa);
  2934. if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
  2935. trans = btrfs_start_transaction(root, 2);
  2936. if (IS_ERR(trans)) {
  2937. ret = PTR_ERR(trans);
  2938. goto out;
  2939. }
  2940. }
  2941. switch (sa->cmd) {
  2942. case BTRFS_QUOTA_CTL_ENABLE:
  2943. ret = btrfs_quota_enable(trans, root->fs_info);
  2944. break;
  2945. case BTRFS_QUOTA_CTL_DISABLE:
  2946. ret = btrfs_quota_disable(trans, root->fs_info);
  2947. break;
  2948. case BTRFS_QUOTA_CTL_RESCAN:
  2949. ret = btrfs_quota_rescan(root->fs_info);
  2950. break;
  2951. default:
  2952. ret = -EINVAL;
  2953. break;
  2954. }
  2955. if (copy_to_user(arg, sa, sizeof(*sa)))
  2956. ret = -EFAULT;
  2957. if (trans) {
  2958. err = btrfs_commit_transaction(trans, root);
  2959. if (err && !ret)
  2960. ret = err;
  2961. }
  2962. out:
  2963. kfree(sa);
  2964. return ret;
  2965. }
  2966. static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
  2967. {
  2968. struct btrfs_ioctl_qgroup_assign_args *sa;
  2969. struct btrfs_trans_handle *trans;
  2970. int ret;
  2971. int err;
  2972. if (!capable(CAP_SYS_ADMIN))
  2973. return -EPERM;
  2974. if (root->fs_info->sb->s_flags & MS_RDONLY)
  2975. return -EROFS;
  2976. sa = memdup_user(arg, sizeof(*sa));
  2977. if (IS_ERR(sa))
  2978. return PTR_ERR(sa);
  2979. trans = btrfs_join_transaction(root);
  2980. if (IS_ERR(trans)) {
  2981. ret = PTR_ERR(trans);
  2982. goto out;
  2983. }
  2984. /* FIXME: check if the IDs really exist */
  2985. if (sa->assign) {
  2986. ret = btrfs_add_qgroup_relation(trans, root->fs_info,
  2987. sa->src, sa->dst);
  2988. } else {
  2989. ret = btrfs_del_qgroup_relation(trans, root->fs_info,
  2990. sa->src, sa->dst);
  2991. }
  2992. err = btrfs_end_transaction(trans, root);
  2993. if (err && !ret)
  2994. ret = err;
  2995. out:
  2996. kfree(sa);
  2997. return ret;
  2998. }
  2999. static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
  3000. {
  3001. struct btrfs_ioctl_qgroup_create_args *sa;
  3002. struct btrfs_trans_handle *trans;
  3003. int ret;
  3004. int err;
  3005. if (!capable(CAP_SYS_ADMIN))
  3006. return -EPERM;
  3007. if (root->fs_info->sb->s_flags & MS_RDONLY)
  3008. return -EROFS;
  3009. sa = memdup_user(arg, sizeof(*sa));
  3010. if (IS_ERR(sa))
  3011. return PTR_ERR(sa);
  3012. trans = btrfs_join_transaction(root);
  3013. if (IS_ERR(trans)) {
  3014. ret = PTR_ERR(trans);
  3015. goto out;
  3016. }
  3017. /* FIXME: check if the IDs really exist */
  3018. if (sa->create) {
  3019. ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
  3020. NULL);
  3021. } else {
  3022. ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
  3023. }
  3024. err = btrfs_end_transaction(trans, root);
  3025. if (err && !ret)
  3026. ret = err;
  3027. out:
  3028. kfree(sa);
  3029. return ret;
  3030. }
  3031. static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
  3032. {
  3033. struct btrfs_ioctl_qgroup_limit_args *sa;
  3034. struct btrfs_trans_handle *trans;
  3035. int ret;
  3036. int err;
  3037. u64 qgroupid;
  3038. if (!capable(CAP_SYS_ADMIN))
  3039. return -EPERM;
  3040. if (root->fs_info->sb->s_flags & MS_RDONLY)
  3041. return -EROFS;
  3042. sa = memdup_user(arg, sizeof(*sa));
  3043. if (IS_ERR(sa))
  3044. return PTR_ERR(sa);
  3045. trans = btrfs_join_transaction(root);
  3046. if (IS_ERR(trans)) {
  3047. ret = PTR_ERR(trans);
  3048. goto out;
  3049. }
  3050. qgroupid = sa->qgroupid;
  3051. if (!qgroupid) {
  3052. /* take the current subvol as qgroup */
  3053. qgroupid = root->root_key.objectid;
  3054. }
  3055. /* FIXME: check if the IDs really exist */
  3056. ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
  3057. err = btrfs_end_transaction(trans, root);
  3058. if (err && !ret)
  3059. ret = err;
  3060. out:
  3061. kfree(sa);
  3062. return ret;
  3063. }
  3064. static long btrfs_ioctl_set_received_subvol(struct file *file,
  3065. void __user *arg)
  3066. {
  3067. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  3068. struct inode *inode = fdentry(file)->d_inode;
  3069. struct btrfs_root *root = BTRFS_I(inode)->root;
  3070. struct btrfs_root_item *root_item = &root->root_item;
  3071. struct btrfs_trans_handle *trans;
  3072. struct timespec ct = CURRENT_TIME;
  3073. int ret = 0;
  3074. ret = mnt_want_write_file(file);
  3075. if (ret < 0)
  3076. return ret;
  3077. down_write(&root->fs_info->subvol_sem);
  3078. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  3079. ret = -EINVAL;
  3080. goto out;
  3081. }
  3082. if (btrfs_root_readonly(root)) {
  3083. ret = -EROFS;
  3084. goto out;
  3085. }
  3086. if (!inode_owner_or_capable(inode)) {
  3087. ret = -EACCES;
  3088. goto out;
  3089. }
  3090. sa = memdup_user(arg, sizeof(*sa));
  3091. if (IS_ERR(sa)) {
  3092. ret = PTR_ERR(sa);
  3093. sa = NULL;
  3094. goto out;
  3095. }
  3096. trans = btrfs_start_transaction(root, 1);
  3097. if (IS_ERR(trans)) {
  3098. ret = PTR_ERR(trans);
  3099. trans = NULL;
  3100. goto out;
  3101. }
  3102. sa->rtransid = trans->transid;
  3103. sa->rtime.sec = ct.tv_sec;
  3104. sa->rtime.nsec = ct.tv_nsec;
  3105. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  3106. btrfs_set_root_stransid(root_item, sa->stransid);
  3107. btrfs_set_root_rtransid(root_item, sa->rtransid);
  3108. root_item->stime.sec = cpu_to_le64(sa->stime.sec);
  3109. root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
  3110. root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
  3111. root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
  3112. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  3113. &root->root_key, &root->root_item);
  3114. if (ret < 0) {
  3115. btrfs_end_transaction(trans, root);
  3116. trans = NULL;
  3117. goto out;
  3118. } else {
  3119. ret = btrfs_commit_transaction(trans, root);
  3120. if (ret < 0)
  3121. goto out;
  3122. }
  3123. ret = copy_to_user(arg, sa, sizeof(*sa));
  3124. if (ret)
  3125. ret = -EFAULT;
  3126. out:
  3127. kfree(sa);
  3128. up_write(&root->fs_info->subvol_sem);
  3129. mnt_drop_write_file(file);
  3130. return ret;
  3131. }
  3132. long btrfs_ioctl(struct file *file, unsigned int
  3133. cmd, unsigned long arg)
  3134. {
  3135. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3136. void __user *argp = (void __user *)arg;
  3137. switch (cmd) {
  3138. case FS_IOC_GETFLAGS:
  3139. return btrfs_ioctl_getflags(file, argp);
  3140. case FS_IOC_SETFLAGS:
  3141. return btrfs_ioctl_setflags(file, argp);
  3142. case FS_IOC_GETVERSION:
  3143. return btrfs_ioctl_getversion(file, argp);
  3144. case FITRIM:
  3145. return btrfs_ioctl_fitrim(file, argp);
  3146. case BTRFS_IOC_SNAP_CREATE:
  3147. return btrfs_ioctl_snap_create(file, argp, 0);
  3148. case BTRFS_IOC_SNAP_CREATE_V2:
  3149. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  3150. case BTRFS_IOC_SUBVOL_CREATE:
  3151. return btrfs_ioctl_snap_create(file, argp, 1);
  3152. case BTRFS_IOC_SUBVOL_CREATE_V2:
  3153. return btrfs_ioctl_snap_create_v2(file, argp, 1);
  3154. case BTRFS_IOC_SNAP_DESTROY:
  3155. return btrfs_ioctl_snap_destroy(file, argp);
  3156. case BTRFS_IOC_SUBVOL_GETFLAGS:
  3157. return btrfs_ioctl_subvol_getflags(file, argp);
  3158. case BTRFS_IOC_SUBVOL_SETFLAGS:
  3159. return btrfs_ioctl_subvol_setflags(file, argp);
  3160. case BTRFS_IOC_DEFAULT_SUBVOL:
  3161. return btrfs_ioctl_default_subvol(file, argp);
  3162. case BTRFS_IOC_DEFRAG:
  3163. return btrfs_ioctl_defrag(file, NULL);
  3164. case BTRFS_IOC_DEFRAG_RANGE:
  3165. return btrfs_ioctl_defrag(file, argp);
  3166. case BTRFS_IOC_RESIZE:
  3167. return btrfs_ioctl_resize(root, argp);
  3168. case BTRFS_IOC_ADD_DEV:
  3169. return btrfs_ioctl_add_dev(root, argp);
  3170. case BTRFS_IOC_RM_DEV:
  3171. return btrfs_ioctl_rm_dev(root, argp);
  3172. case BTRFS_IOC_FS_INFO:
  3173. return btrfs_ioctl_fs_info(root, argp);
  3174. case BTRFS_IOC_DEV_INFO:
  3175. return btrfs_ioctl_dev_info(root, argp);
  3176. case BTRFS_IOC_BALANCE:
  3177. return btrfs_ioctl_balance(file, NULL);
  3178. case BTRFS_IOC_CLONE:
  3179. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  3180. case BTRFS_IOC_CLONE_RANGE:
  3181. return btrfs_ioctl_clone_range(file, argp);
  3182. case BTRFS_IOC_TRANS_START:
  3183. return btrfs_ioctl_trans_start(file);
  3184. case BTRFS_IOC_TRANS_END:
  3185. return btrfs_ioctl_trans_end(file);
  3186. case BTRFS_IOC_TREE_SEARCH:
  3187. return btrfs_ioctl_tree_search(file, argp);
  3188. case BTRFS_IOC_INO_LOOKUP:
  3189. return btrfs_ioctl_ino_lookup(file, argp);
  3190. case BTRFS_IOC_INO_PATHS:
  3191. return btrfs_ioctl_ino_to_path(root, argp);
  3192. case BTRFS_IOC_LOGICAL_INO:
  3193. return btrfs_ioctl_logical_to_ino(root, argp);
  3194. case BTRFS_IOC_SPACE_INFO:
  3195. return btrfs_ioctl_space_info(root, argp);
  3196. case BTRFS_IOC_SYNC:
  3197. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  3198. return 0;
  3199. case BTRFS_IOC_START_SYNC:
  3200. return btrfs_ioctl_start_sync(file, argp);
  3201. case BTRFS_IOC_WAIT_SYNC:
  3202. return btrfs_ioctl_wait_sync(file, argp);
  3203. case BTRFS_IOC_SCRUB:
  3204. return btrfs_ioctl_scrub(root, argp);
  3205. case BTRFS_IOC_SCRUB_CANCEL:
  3206. return btrfs_ioctl_scrub_cancel(root, argp);
  3207. case BTRFS_IOC_SCRUB_PROGRESS:
  3208. return btrfs_ioctl_scrub_progress(root, argp);
  3209. case BTRFS_IOC_BALANCE_V2:
  3210. return btrfs_ioctl_balance(file, argp);
  3211. case BTRFS_IOC_BALANCE_CTL:
  3212. return btrfs_ioctl_balance_ctl(root, arg);
  3213. case BTRFS_IOC_BALANCE_PROGRESS:
  3214. return btrfs_ioctl_balance_progress(root, argp);
  3215. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  3216. return btrfs_ioctl_set_received_subvol(file, argp);
  3217. case BTRFS_IOC_SEND:
  3218. return btrfs_ioctl_send(file, argp);
  3219. case BTRFS_IOC_GET_DEV_STATS:
  3220. return btrfs_ioctl_get_dev_stats(root, argp);
  3221. case BTRFS_IOC_QUOTA_CTL:
  3222. return btrfs_ioctl_quota_ctl(root, argp);
  3223. case BTRFS_IOC_QGROUP_ASSIGN:
  3224. return btrfs_ioctl_qgroup_assign(root, argp);
  3225. case BTRFS_IOC_QGROUP_CREATE:
  3226. return btrfs_ioctl_qgroup_create(root, argp);
  3227. case BTRFS_IOC_QGROUP_LIMIT:
  3228. return btrfs_ioctl_qgroup_limit(root, argp);
  3229. }
  3230. return -ENOTTY;
  3231. }