ioctl.c 97 KB

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