ioctl.c 89 KB

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