ioctl.c 92 KB

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