ioctl.c 89 KB

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