ioctl.c 99 KB

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