ioctl.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213
  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. vol_args = memdup_user(arg, sizeof(*vol_args));
  2044. if (IS_ERR(vol_args)) {
  2045. ret = PTR_ERR(vol_args);
  2046. goto out;
  2047. }
  2048. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2049. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2050. 1)) {
  2051. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2052. goto out;
  2053. }
  2054. mutex_lock(&root->fs_info->volume_mutex);
  2055. ret = btrfs_rm_device(root, vol_args->name);
  2056. mutex_unlock(&root->fs_info->volume_mutex);
  2057. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2058. out:
  2059. kfree(vol_args);
  2060. mnt_drop_write_file(file);
  2061. return ret;
  2062. }
  2063. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  2064. {
  2065. struct btrfs_ioctl_fs_info_args *fi_args;
  2066. struct btrfs_device *device;
  2067. struct btrfs_device *next;
  2068. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2069. int ret = 0;
  2070. if (!capable(CAP_SYS_ADMIN))
  2071. return -EPERM;
  2072. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  2073. if (!fi_args)
  2074. return -ENOMEM;
  2075. fi_args->num_devices = fs_devices->num_devices;
  2076. memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
  2077. mutex_lock(&fs_devices->device_list_mutex);
  2078. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  2079. if (device->devid > fi_args->max_id)
  2080. fi_args->max_id = device->devid;
  2081. }
  2082. mutex_unlock(&fs_devices->device_list_mutex);
  2083. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  2084. ret = -EFAULT;
  2085. kfree(fi_args);
  2086. return ret;
  2087. }
  2088. static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
  2089. {
  2090. struct btrfs_ioctl_dev_info_args *di_args;
  2091. struct btrfs_device *dev;
  2092. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2093. int ret = 0;
  2094. char *s_uuid = NULL;
  2095. char empty_uuid[BTRFS_UUID_SIZE] = {0};
  2096. if (!capable(CAP_SYS_ADMIN))
  2097. return -EPERM;
  2098. di_args = memdup_user(arg, sizeof(*di_args));
  2099. if (IS_ERR(di_args))
  2100. return PTR_ERR(di_args);
  2101. if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
  2102. s_uuid = di_args->uuid;
  2103. mutex_lock(&fs_devices->device_list_mutex);
  2104. dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
  2105. if (!dev) {
  2106. ret = -ENODEV;
  2107. goto out;
  2108. }
  2109. di_args->devid = dev->devid;
  2110. di_args->bytes_used = dev->bytes_used;
  2111. di_args->total_bytes = dev->total_bytes;
  2112. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  2113. if (dev->name) {
  2114. struct rcu_string *name;
  2115. rcu_read_lock();
  2116. name = rcu_dereference(dev->name);
  2117. strncpy(di_args->path, name->str, sizeof(di_args->path));
  2118. rcu_read_unlock();
  2119. di_args->path[sizeof(di_args->path) - 1] = 0;
  2120. } else {
  2121. di_args->path[0] = '\0';
  2122. }
  2123. out:
  2124. mutex_unlock(&fs_devices->device_list_mutex);
  2125. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  2126. ret = -EFAULT;
  2127. kfree(di_args);
  2128. return ret;
  2129. }
  2130. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  2131. u64 off, u64 olen, u64 destoff)
  2132. {
  2133. struct inode *inode = file_inode(file);
  2134. struct btrfs_root *root = BTRFS_I(inode)->root;
  2135. struct fd src_file;
  2136. struct inode *src;
  2137. struct btrfs_trans_handle *trans;
  2138. struct btrfs_path *path;
  2139. struct extent_buffer *leaf;
  2140. char *buf;
  2141. struct btrfs_key key;
  2142. u32 nritems;
  2143. int slot;
  2144. int ret;
  2145. u64 len = olen;
  2146. u64 bs = root->fs_info->sb->s_blocksize;
  2147. int same_inode = 0;
  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. same_inode = 1;
  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 (!same_inode) {
  2201. if (inode < src) {
  2202. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  2203. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  2204. } else {
  2205. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  2206. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  2207. }
  2208. } else {
  2209. mutex_lock(&src->i_mutex);
  2210. }
  2211. /* determine range to clone */
  2212. ret = -EINVAL;
  2213. if (off + len > src->i_size || off + len < off)
  2214. goto out_unlock;
  2215. if (len == 0)
  2216. olen = len = src->i_size - off;
  2217. /* if we extend to eof, continue to block boundary */
  2218. if (off + len == src->i_size)
  2219. len = ALIGN(src->i_size, bs) - off;
  2220. /* verify the end result is block aligned */
  2221. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  2222. !IS_ALIGNED(destoff, bs))
  2223. goto out_unlock;
  2224. /* verify if ranges are overlapped within the same file */
  2225. if (same_inode) {
  2226. if (destoff + len > off && destoff < off + len)
  2227. goto out_unlock;
  2228. }
  2229. if (destoff > inode->i_size) {
  2230. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  2231. if (ret)
  2232. goto out_unlock;
  2233. }
  2234. /* truncate page cache pages from target inode range */
  2235. truncate_inode_pages_range(&inode->i_data, destoff,
  2236. PAGE_CACHE_ALIGN(destoff + len) - 1);
  2237. /* do any pending delalloc/csum calc on src, one way or
  2238. another, and lock file content */
  2239. while (1) {
  2240. struct btrfs_ordered_extent *ordered;
  2241. lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  2242. ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
  2243. if (!ordered &&
  2244. !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
  2245. EXTENT_DELALLOC, 0, NULL))
  2246. break;
  2247. unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  2248. if (ordered)
  2249. btrfs_put_ordered_extent(ordered);
  2250. btrfs_wait_ordered_range(src, off, len);
  2251. }
  2252. /* clone data */
  2253. key.objectid = btrfs_ino(src);
  2254. key.type = BTRFS_EXTENT_DATA_KEY;
  2255. key.offset = 0;
  2256. while (1) {
  2257. /*
  2258. * note the key will change type as we walk through the
  2259. * tree.
  2260. */
  2261. ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
  2262. 0, 0);
  2263. if (ret < 0)
  2264. goto out;
  2265. nritems = btrfs_header_nritems(path->nodes[0]);
  2266. if (path->slots[0] >= nritems) {
  2267. ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
  2268. if (ret < 0)
  2269. goto out;
  2270. if (ret > 0)
  2271. break;
  2272. nritems = btrfs_header_nritems(path->nodes[0]);
  2273. }
  2274. leaf = path->nodes[0];
  2275. slot = path->slots[0];
  2276. btrfs_item_key_to_cpu(leaf, &key, slot);
  2277. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  2278. key.objectid != btrfs_ino(src))
  2279. break;
  2280. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  2281. struct btrfs_file_extent_item *extent;
  2282. int type;
  2283. u32 size;
  2284. struct btrfs_key new_key;
  2285. u64 disko = 0, diskl = 0;
  2286. u64 datao = 0, datal = 0;
  2287. u8 comp;
  2288. u64 endoff;
  2289. size = btrfs_item_size_nr(leaf, slot);
  2290. read_extent_buffer(leaf, buf,
  2291. btrfs_item_ptr_offset(leaf, slot),
  2292. size);
  2293. extent = btrfs_item_ptr(leaf, slot,
  2294. struct btrfs_file_extent_item);
  2295. comp = btrfs_file_extent_compression(leaf, extent);
  2296. type = btrfs_file_extent_type(leaf, extent);
  2297. if (type == BTRFS_FILE_EXTENT_REG ||
  2298. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2299. disko = btrfs_file_extent_disk_bytenr(leaf,
  2300. extent);
  2301. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  2302. extent);
  2303. datao = btrfs_file_extent_offset(leaf, extent);
  2304. datal = btrfs_file_extent_num_bytes(leaf,
  2305. extent);
  2306. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2307. /* take upper bound, may be compressed */
  2308. datal = btrfs_file_extent_ram_bytes(leaf,
  2309. extent);
  2310. }
  2311. btrfs_release_path(path);
  2312. if (key.offset + datal <= off ||
  2313. key.offset >= off + len - 1)
  2314. goto next;
  2315. memcpy(&new_key, &key, sizeof(new_key));
  2316. new_key.objectid = btrfs_ino(inode);
  2317. if (off <= key.offset)
  2318. new_key.offset = key.offset + destoff - off;
  2319. else
  2320. new_key.offset = destoff;
  2321. /*
  2322. * 1 - adjusting old extent (we may have to split it)
  2323. * 1 - add new extent
  2324. * 1 - inode update
  2325. */
  2326. trans = btrfs_start_transaction(root, 3);
  2327. if (IS_ERR(trans)) {
  2328. ret = PTR_ERR(trans);
  2329. goto out;
  2330. }
  2331. if (type == BTRFS_FILE_EXTENT_REG ||
  2332. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2333. /*
  2334. * a | --- range to clone ---| b
  2335. * | ------------- extent ------------- |
  2336. */
  2337. /* substract range b */
  2338. if (key.offset + datal > off + len)
  2339. datal = off + len - key.offset;
  2340. /* substract range a */
  2341. if (off > key.offset) {
  2342. datao += off - key.offset;
  2343. datal -= off - key.offset;
  2344. }
  2345. ret = btrfs_drop_extents(trans, root, inode,
  2346. new_key.offset,
  2347. new_key.offset + datal,
  2348. 1);
  2349. if (ret) {
  2350. btrfs_abort_transaction(trans, root,
  2351. ret);
  2352. btrfs_end_transaction(trans, root);
  2353. goto out;
  2354. }
  2355. ret = btrfs_insert_empty_item(trans, root, path,
  2356. &new_key, size);
  2357. if (ret) {
  2358. btrfs_abort_transaction(trans, root,
  2359. ret);
  2360. btrfs_end_transaction(trans, root);
  2361. goto out;
  2362. }
  2363. leaf = path->nodes[0];
  2364. slot = path->slots[0];
  2365. write_extent_buffer(leaf, buf,
  2366. btrfs_item_ptr_offset(leaf, slot),
  2367. size);
  2368. extent = btrfs_item_ptr(leaf, slot,
  2369. struct btrfs_file_extent_item);
  2370. /* disko == 0 means it's a hole */
  2371. if (!disko)
  2372. datao = 0;
  2373. btrfs_set_file_extent_offset(leaf, extent,
  2374. datao);
  2375. btrfs_set_file_extent_num_bytes(leaf, extent,
  2376. datal);
  2377. if (disko) {
  2378. inode_add_bytes(inode, datal);
  2379. ret = btrfs_inc_extent_ref(trans, root,
  2380. disko, diskl, 0,
  2381. root->root_key.objectid,
  2382. btrfs_ino(inode),
  2383. new_key.offset - datao,
  2384. 0);
  2385. if (ret) {
  2386. btrfs_abort_transaction(trans,
  2387. root,
  2388. ret);
  2389. btrfs_end_transaction(trans,
  2390. root);
  2391. goto out;
  2392. }
  2393. }
  2394. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2395. u64 skip = 0;
  2396. u64 trim = 0;
  2397. if (off > key.offset) {
  2398. skip = off - key.offset;
  2399. new_key.offset += skip;
  2400. }
  2401. if (key.offset + datal > off + len)
  2402. trim = key.offset + datal - (off + len);
  2403. if (comp && (skip || trim)) {
  2404. ret = -EINVAL;
  2405. btrfs_end_transaction(trans, root);
  2406. goto out;
  2407. }
  2408. size -= skip + trim;
  2409. datal -= skip + trim;
  2410. ret = btrfs_drop_extents(trans, root, inode,
  2411. new_key.offset,
  2412. new_key.offset + datal,
  2413. 1);
  2414. if (ret) {
  2415. btrfs_abort_transaction(trans, root,
  2416. ret);
  2417. btrfs_end_transaction(trans, root);
  2418. goto out;
  2419. }
  2420. ret = btrfs_insert_empty_item(trans, root, path,
  2421. &new_key, size);
  2422. if (ret) {
  2423. btrfs_abort_transaction(trans, root,
  2424. ret);
  2425. btrfs_end_transaction(trans, root);
  2426. goto out;
  2427. }
  2428. if (skip) {
  2429. u32 start =
  2430. btrfs_file_extent_calc_inline_size(0);
  2431. memmove(buf+start, buf+start+skip,
  2432. datal);
  2433. }
  2434. leaf = path->nodes[0];
  2435. slot = path->slots[0];
  2436. write_extent_buffer(leaf, buf,
  2437. btrfs_item_ptr_offset(leaf, slot),
  2438. size);
  2439. inode_add_bytes(inode, datal);
  2440. }
  2441. btrfs_mark_buffer_dirty(leaf);
  2442. btrfs_release_path(path);
  2443. inode_inc_iversion(inode);
  2444. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2445. /*
  2446. * we round up to the block size at eof when
  2447. * determining which extents to clone above,
  2448. * but shouldn't round up the file size
  2449. */
  2450. endoff = new_key.offset + datal;
  2451. if (endoff > destoff+olen)
  2452. endoff = destoff+olen;
  2453. if (endoff > inode->i_size)
  2454. btrfs_i_size_write(inode, endoff);
  2455. ret = btrfs_update_inode(trans, root, inode);
  2456. if (ret) {
  2457. btrfs_abort_transaction(trans, root, ret);
  2458. btrfs_end_transaction(trans, root);
  2459. goto out;
  2460. }
  2461. ret = btrfs_end_transaction(trans, root);
  2462. }
  2463. next:
  2464. btrfs_release_path(path);
  2465. key.offset++;
  2466. }
  2467. ret = 0;
  2468. out:
  2469. btrfs_release_path(path);
  2470. unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  2471. out_unlock:
  2472. mutex_unlock(&src->i_mutex);
  2473. if (!same_inode)
  2474. mutex_unlock(&inode->i_mutex);
  2475. vfree(buf);
  2476. btrfs_free_path(path);
  2477. out_fput:
  2478. fdput(src_file);
  2479. out_drop_write:
  2480. mnt_drop_write_file(file);
  2481. return ret;
  2482. }
  2483. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  2484. {
  2485. struct btrfs_ioctl_clone_range_args args;
  2486. if (copy_from_user(&args, argp, sizeof(args)))
  2487. return -EFAULT;
  2488. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  2489. args.src_length, args.dest_offset);
  2490. }
  2491. /*
  2492. * there are many ways the trans_start and trans_end ioctls can lead
  2493. * to deadlocks. They should only be used by applications that
  2494. * basically own the machine, and have a very in depth understanding
  2495. * of all the possible deadlocks and enospc problems.
  2496. */
  2497. static long btrfs_ioctl_trans_start(struct file *file)
  2498. {
  2499. struct inode *inode = file_inode(file);
  2500. struct btrfs_root *root = BTRFS_I(inode)->root;
  2501. struct btrfs_trans_handle *trans;
  2502. int ret;
  2503. ret = -EPERM;
  2504. if (!capable(CAP_SYS_ADMIN))
  2505. goto out;
  2506. ret = -EINPROGRESS;
  2507. if (file->private_data)
  2508. goto out;
  2509. ret = -EROFS;
  2510. if (btrfs_root_readonly(root))
  2511. goto out;
  2512. ret = mnt_want_write_file(file);
  2513. if (ret)
  2514. goto out;
  2515. atomic_inc(&root->fs_info->open_ioctl_trans);
  2516. ret = -ENOMEM;
  2517. trans = btrfs_start_ioctl_transaction(root);
  2518. if (IS_ERR(trans))
  2519. goto out_drop;
  2520. file->private_data = trans;
  2521. return 0;
  2522. out_drop:
  2523. atomic_dec(&root->fs_info->open_ioctl_trans);
  2524. mnt_drop_write_file(file);
  2525. out:
  2526. return ret;
  2527. }
  2528. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  2529. {
  2530. struct inode *inode = file_inode(file);
  2531. struct btrfs_root *root = BTRFS_I(inode)->root;
  2532. struct btrfs_root *new_root;
  2533. struct btrfs_dir_item *di;
  2534. struct btrfs_trans_handle *trans;
  2535. struct btrfs_path *path;
  2536. struct btrfs_key location;
  2537. struct btrfs_disk_key disk_key;
  2538. u64 objectid = 0;
  2539. u64 dir_id;
  2540. int ret;
  2541. if (!capable(CAP_SYS_ADMIN))
  2542. return -EPERM;
  2543. ret = mnt_want_write_file(file);
  2544. if (ret)
  2545. return ret;
  2546. if (copy_from_user(&objectid, argp, sizeof(objectid))) {
  2547. ret = -EFAULT;
  2548. goto out;
  2549. }
  2550. if (!objectid)
  2551. objectid = root->root_key.objectid;
  2552. location.objectid = objectid;
  2553. location.type = BTRFS_ROOT_ITEM_KEY;
  2554. location.offset = (u64)-1;
  2555. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  2556. if (IS_ERR(new_root)) {
  2557. ret = PTR_ERR(new_root);
  2558. goto out;
  2559. }
  2560. path = btrfs_alloc_path();
  2561. if (!path) {
  2562. ret = -ENOMEM;
  2563. goto out;
  2564. }
  2565. path->leave_spinning = 1;
  2566. trans = btrfs_start_transaction(root, 1);
  2567. if (IS_ERR(trans)) {
  2568. btrfs_free_path(path);
  2569. ret = PTR_ERR(trans);
  2570. goto out;
  2571. }
  2572. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  2573. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  2574. dir_id, "default", 7, 1);
  2575. if (IS_ERR_OR_NULL(di)) {
  2576. btrfs_free_path(path);
  2577. btrfs_end_transaction(trans, root);
  2578. printk(KERN_ERR "Umm, you don't have the default dir item, "
  2579. "this isn't going to work\n");
  2580. ret = -ENOENT;
  2581. goto out;
  2582. }
  2583. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  2584. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  2585. btrfs_mark_buffer_dirty(path->nodes[0]);
  2586. btrfs_free_path(path);
  2587. btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
  2588. btrfs_end_transaction(trans, root);
  2589. out:
  2590. mnt_drop_write_file(file);
  2591. return ret;
  2592. }
  2593. void btrfs_get_block_group_info(struct list_head *groups_list,
  2594. struct btrfs_ioctl_space_info *space)
  2595. {
  2596. struct btrfs_block_group_cache *block_group;
  2597. space->total_bytes = 0;
  2598. space->used_bytes = 0;
  2599. space->flags = 0;
  2600. list_for_each_entry(block_group, groups_list, list) {
  2601. space->flags = block_group->flags;
  2602. space->total_bytes += block_group->key.offset;
  2603. space->used_bytes +=
  2604. btrfs_block_group_used(&block_group->item);
  2605. }
  2606. }
  2607. static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  2608. {
  2609. struct btrfs_ioctl_space_args space_args;
  2610. struct btrfs_ioctl_space_info space;
  2611. struct btrfs_ioctl_space_info *dest;
  2612. struct btrfs_ioctl_space_info *dest_orig;
  2613. struct btrfs_ioctl_space_info __user *user_dest;
  2614. struct btrfs_space_info *info;
  2615. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  2616. BTRFS_BLOCK_GROUP_SYSTEM,
  2617. BTRFS_BLOCK_GROUP_METADATA,
  2618. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  2619. int num_types = 4;
  2620. int alloc_size;
  2621. int ret = 0;
  2622. u64 slot_count = 0;
  2623. int i, c;
  2624. if (copy_from_user(&space_args,
  2625. (struct btrfs_ioctl_space_args __user *)arg,
  2626. sizeof(space_args)))
  2627. return -EFAULT;
  2628. for (i = 0; i < num_types; i++) {
  2629. struct btrfs_space_info *tmp;
  2630. info = NULL;
  2631. rcu_read_lock();
  2632. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2633. list) {
  2634. if (tmp->flags == types[i]) {
  2635. info = tmp;
  2636. break;
  2637. }
  2638. }
  2639. rcu_read_unlock();
  2640. if (!info)
  2641. continue;
  2642. down_read(&info->groups_sem);
  2643. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2644. if (!list_empty(&info->block_groups[c]))
  2645. slot_count++;
  2646. }
  2647. up_read(&info->groups_sem);
  2648. }
  2649. /* space_slots == 0 means they are asking for a count */
  2650. if (space_args.space_slots == 0) {
  2651. space_args.total_spaces = slot_count;
  2652. goto out;
  2653. }
  2654. slot_count = min_t(u64, space_args.space_slots, slot_count);
  2655. alloc_size = sizeof(*dest) * slot_count;
  2656. /* we generally have at most 6 or so space infos, one for each raid
  2657. * level. So, a whole page should be more than enough for everyone
  2658. */
  2659. if (alloc_size > PAGE_CACHE_SIZE)
  2660. return -ENOMEM;
  2661. space_args.total_spaces = 0;
  2662. dest = kmalloc(alloc_size, GFP_NOFS);
  2663. if (!dest)
  2664. return -ENOMEM;
  2665. dest_orig = dest;
  2666. /* now we have a buffer to copy into */
  2667. for (i = 0; i < num_types; i++) {
  2668. struct btrfs_space_info *tmp;
  2669. if (!slot_count)
  2670. break;
  2671. info = NULL;
  2672. rcu_read_lock();
  2673. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2674. list) {
  2675. if (tmp->flags == types[i]) {
  2676. info = tmp;
  2677. break;
  2678. }
  2679. }
  2680. rcu_read_unlock();
  2681. if (!info)
  2682. continue;
  2683. down_read(&info->groups_sem);
  2684. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2685. if (!list_empty(&info->block_groups[c])) {
  2686. btrfs_get_block_group_info(
  2687. &info->block_groups[c], &space);
  2688. memcpy(dest, &space, sizeof(space));
  2689. dest++;
  2690. space_args.total_spaces++;
  2691. slot_count--;
  2692. }
  2693. if (!slot_count)
  2694. break;
  2695. }
  2696. up_read(&info->groups_sem);
  2697. }
  2698. user_dest = (struct btrfs_ioctl_space_info __user *)
  2699. (arg + sizeof(struct btrfs_ioctl_space_args));
  2700. if (copy_to_user(user_dest, dest_orig, alloc_size))
  2701. ret = -EFAULT;
  2702. kfree(dest_orig);
  2703. out:
  2704. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  2705. ret = -EFAULT;
  2706. return ret;
  2707. }
  2708. /*
  2709. * there are many ways the trans_start and trans_end ioctls can lead
  2710. * to deadlocks. They should only be used by applications that
  2711. * basically own the machine, and have a very in depth understanding
  2712. * of all the possible deadlocks and enospc problems.
  2713. */
  2714. long btrfs_ioctl_trans_end(struct file *file)
  2715. {
  2716. struct inode *inode = file_inode(file);
  2717. struct btrfs_root *root = BTRFS_I(inode)->root;
  2718. struct btrfs_trans_handle *trans;
  2719. trans = file->private_data;
  2720. if (!trans)
  2721. return -EINVAL;
  2722. file->private_data = NULL;
  2723. btrfs_end_transaction(trans, root);
  2724. atomic_dec(&root->fs_info->open_ioctl_trans);
  2725. mnt_drop_write_file(file);
  2726. return 0;
  2727. }
  2728. static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  2729. void __user *argp)
  2730. {
  2731. struct btrfs_trans_handle *trans;
  2732. u64 transid;
  2733. int ret;
  2734. trans = btrfs_attach_transaction_barrier(root);
  2735. if (IS_ERR(trans)) {
  2736. if (PTR_ERR(trans) != -ENOENT)
  2737. return PTR_ERR(trans);
  2738. /* No running transaction, don't bother */
  2739. transid = root->fs_info->last_trans_committed;
  2740. goto out;
  2741. }
  2742. transid = trans->transid;
  2743. ret = btrfs_commit_transaction_async(trans, root, 0);
  2744. if (ret) {
  2745. btrfs_end_transaction(trans, root);
  2746. return ret;
  2747. }
  2748. out:
  2749. if (argp)
  2750. if (copy_to_user(argp, &transid, sizeof(transid)))
  2751. return -EFAULT;
  2752. return 0;
  2753. }
  2754. static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
  2755. void __user *argp)
  2756. {
  2757. u64 transid;
  2758. if (argp) {
  2759. if (copy_from_user(&transid, argp, sizeof(transid)))
  2760. return -EFAULT;
  2761. } else {
  2762. transid = 0; /* current trans */
  2763. }
  2764. return btrfs_wait_for_commit(root, transid);
  2765. }
  2766. static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
  2767. {
  2768. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  2769. struct btrfs_ioctl_scrub_args *sa;
  2770. int ret;
  2771. if (!capable(CAP_SYS_ADMIN))
  2772. return -EPERM;
  2773. sa = memdup_user(arg, sizeof(*sa));
  2774. if (IS_ERR(sa))
  2775. return PTR_ERR(sa);
  2776. if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
  2777. ret = mnt_want_write_file(file);
  2778. if (ret)
  2779. goto out;
  2780. }
  2781. ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
  2782. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
  2783. 0);
  2784. if (copy_to_user(arg, sa, sizeof(*sa)))
  2785. ret = -EFAULT;
  2786. if (!(sa->flags & BTRFS_SCRUB_READONLY))
  2787. mnt_drop_write_file(file);
  2788. out:
  2789. kfree(sa);
  2790. return ret;
  2791. }
  2792. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  2793. {
  2794. if (!capable(CAP_SYS_ADMIN))
  2795. return -EPERM;
  2796. return btrfs_scrub_cancel(root->fs_info);
  2797. }
  2798. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  2799. void __user *arg)
  2800. {
  2801. struct btrfs_ioctl_scrub_args *sa;
  2802. int ret;
  2803. if (!capable(CAP_SYS_ADMIN))
  2804. return -EPERM;
  2805. sa = memdup_user(arg, sizeof(*sa));
  2806. if (IS_ERR(sa))
  2807. return PTR_ERR(sa);
  2808. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  2809. if (copy_to_user(arg, sa, sizeof(*sa)))
  2810. ret = -EFAULT;
  2811. kfree(sa);
  2812. return ret;
  2813. }
  2814. static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
  2815. void __user *arg)
  2816. {
  2817. struct btrfs_ioctl_get_dev_stats *sa;
  2818. int ret;
  2819. sa = memdup_user(arg, sizeof(*sa));
  2820. if (IS_ERR(sa))
  2821. return PTR_ERR(sa);
  2822. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  2823. kfree(sa);
  2824. return -EPERM;
  2825. }
  2826. ret = btrfs_get_dev_stats(root, sa);
  2827. if (copy_to_user(arg, sa, sizeof(*sa)))
  2828. ret = -EFAULT;
  2829. kfree(sa);
  2830. return ret;
  2831. }
  2832. static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
  2833. {
  2834. struct btrfs_ioctl_dev_replace_args *p;
  2835. int ret;
  2836. if (!capable(CAP_SYS_ADMIN))
  2837. return -EPERM;
  2838. p = memdup_user(arg, sizeof(*p));
  2839. if (IS_ERR(p))
  2840. return PTR_ERR(p);
  2841. switch (p->cmd) {
  2842. case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
  2843. if (atomic_xchg(
  2844. &root->fs_info->mutually_exclusive_operation_running,
  2845. 1)) {
  2846. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  2847. ret = -EINPROGRESS;
  2848. } else {
  2849. ret = btrfs_dev_replace_start(root, p);
  2850. atomic_set(
  2851. &root->fs_info->mutually_exclusive_operation_running,
  2852. 0);
  2853. }
  2854. break;
  2855. case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
  2856. btrfs_dev_replace_status(root->fs_info, p);
  2857. ret = 0;
  2858. break;
  2859. case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
  2860. ret = btrfs_dev_replace_cancel(root->fs_info, p);
  2861. break;
  2862. default:
  2863. ret = -EINVAL;
  2864. break;
  2865. }
  2866. if (copy_to_user(arg, p, sizeof(*p)))
  2867. ret = -EFAULT;
  2868. kfree(p);
  2869. return ret;
  2870. }
  2871. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  2872. {
  2873. int ret = 0;
  2874. int i;
  2875. u64 rel_ptr;
  2876. int size;
  2877. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  2878. struct inode_fs_paths *ipath = NULL;
  2879. struct btrfs_path *path;
  2880. if (!capable(CAP_DAC_READ_SEARCH))
  2881. return -EPERM;
  2882. path = btrfs_alloc_path();
  2883. if (!path) {
  2884. ret = -ENOMEM;
  2885. goto out;
  2886. }
  2887. ipa = memdup_user(arg, sizeof(*ipa));
  2888. if (IS_ERR(ipa)) {
  2889. ret = PTR_ERR(ipa);
  2890. ipa = NULL;
  2891. goto out;
  2892. }
  2893. size = min_t(u32, ipa->size, 4096);
  2894. ipath = init_ipath(size, root, path);
  2895. if (IS_ERR(ipath)) {
  2896. ret = PTR_ERR(ipath);
  2897. ipath = NULL;
  2898. goto out;
  2899. }
  2900. ret = paths_from_inode(ipa->inum, ipath);
  2901. if (ret < 0)
  2902. goto out;
  2903. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  2904. rel_ptr = ipath->fspath->val[i] -
  2905. (u64)(unsigned long)ipath->fspath->val;
  2906. ipath->fspath->val[i] = rel_ptr;
  2907. }
  2908. ret = copy_to_user((void *)(unsigned long)ipa->fspath,
  2909. (void *)(unsigned long)ipath->fspath, size);
  2910. if (ret) {
  2911. ret = -EFAULT;
  2912. goto out;
  2913. }
  2914. out:
  2915. btrfs_free_path(path);
  2916. free_ipath(ipath);
  2917. kfree(ipa);
  2918. return ret;
  2919. }
  2920. static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
  2921. {
  2922. struct btrfs_data_container *inodes = ctx;
  2923. const size_t c = 3 * sizeof(u64);
  2924. if (inodes->bytes_left >= c) {
  2925. inodes->bytes_left -= c;
  2926. inodes->val[inodes->elem_cnt] = inum;
  2927. inodes->val[inodes->elem_cnt + 1] = offset;
  2928. inodes->val[inodes->elem_cnt + 2] = root;
  2929. inodes->elem_cnt += 3;
  2930. } else {
  2931. inodes->bytes_missing += c - inodes->bytes_left;
  2932. inodes->bytes_left = 0;
  2933. inodes->elem_missed += 3;
  2934. }
  2935. return 0;
  2936. }
  2937. static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
  2938. void __user *arg)
  2939. {
  2940. int ret = 0;
  2941. int size;
  2942. struct btrfs_ioctl_logical_ino_args *loi;
  2943. struct btrfs_data_container *inodes = NULL;
  2944. struct btrfs_path *path = NULL;
  2945. if (!capable(CAP_SYS_ADMIN))
  2946. return -EPERM;
  2947. loi = memdup_user(arg, sizeof(*loi));
  2948. if (IS_ERR(loi)) {
  2949. ret = PTR_ERR(loi);
  2950. loi = NULL;
  2951. goto out;
  2952. }
  2953. path = btrfs_alloc_path();
  2954. if (!path) {
  2955. ret = -ENOMEM;
  2956. goto out;
  2957. }
  2958. size = min_t(u32, loi->size, 64 * 1024);
  2959. inodes = init_data_container(size);
  2960. if (IS_ERR(inodes)) {
  2961. ret = PTR_ERR(inodes);
  2962. inodes = NULL;
  2963. goto out;
  2964. }
  2965. ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
  2966. build_ino_list, inodes);
  2967. if (ret == -EINVAL)
  2968. ret = -ENOENT;
  2969. if (ret < 0)
  2970. goto out;
  2971. ret = copy_to_user((void *)(unsigned long)loi->inodes,
  2972. (void *)(unsigned long)inodes, size);
  2973. if (ret)
  2974. ret = -EFAULT;
  2975. out:
  2976. btrfs_free_path(path);
  2977. vfree(inodes);
  2978. kfree(loi);
  2979. return ret;
  2980. }
  2981. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  2982. struct btrfs_ioctl_balance_args *bargs)
  2983. {
  2984. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2985. bargs->flags = bctl->flags;
  2986. if (atomic_read(&fs_info->balance_running))
  2987. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  2988. if (atomic_read(&fs_info->balance_pause_req))
  2989. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  2990. if (atomic_read(&fs_info->balance_cancel_req))
  2991. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  2992. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  2993. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  2994. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  2995. if (lock) {
  2996. spin_lock(&fs_info->balance_lock);
  2997. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  2998. spin_unlock(&fs_info->balance_lock);
  2999. } else {
  3000. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  3001. }
  3002. }
  3003. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  3004. {
  3005. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3006. struct btrfs_fs_info *fs_info = root->fs_info;
  3007. struct btrfs_ioctl_balance_args *bargs;
  3008. struct btrfs_balance_control *bctl;
  3009. bool need_unlock; /* for mut. excl. ops lock */
  3010. int ret;
  3011. if (!capable(CAP_SYS_ADMIN))
  3012. return -EPERM;
  3013. ret = mnt_want_write_file(file);
  3014. if (ret)
  3015. return ret;
  3016. again:
  3017. if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
  3018. mutex_lock(&fs_info->volume_mutex);
  3019. mutex_lock(&fs_info->balance_mutex);
  3020. need_unlock = true;
  3021. goto locked;
  3022. }
  3023. /*
  3024. * mut. excl. ops lock is locked. Three possibilites:
  3025. * (1) some other op is running
  3026. * (2) balance is running
  3027. * (3) balance is paused -- special case (think resume)
  3028. */
  3029. mutex_lock(&fs_info->balance_mutex);
  3030. if (fs_info->balance_ctl) {
  3031. /* this is either (2) or (3) */
  3032. if (!atomic_read(&fs_info->balance_running)) {
  3033. mutex_unlock(&fs_info->balance_mutex);
  3034. if (!mutex_trylock(&fs_info->volume_mutex))
  3035. goto again;
  3036. mutex_lock(&fs_info->balance_mutex);
  3037. if (fs_info->balance_ctl &&
  3038. !atomic_read(&fs_info->balance_running)) {
  3039. /* this is (3) */
  3040. need_unlock = false;
  3041. goto locked;
  3042. }
  3043. mutex_unlock(&fs_info->balance_mutex);
  3044. mutex_unlock(&fs_info->volume_mutex);
  3045. goto again;
  3046. } else {
  3047. /* this is (2) */
  3048. mutex_unlock(&fs_info->balance_mutex);
  3049. ret = -EINPROGRESS;
  3050. goto out;
  3051. }
  3052. } else {
  3053. /* this is (1) */
  3054. mutex_unlock(&fs_info->balance_mutex);
  3055. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  3056. ret = -EINVAL;
  3057. goto out;
  3058. }
  3059. locked:
  3060. BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
  3061. if (arg) {
  3062. bargs = memdup_user(arg, sizeof(*bargs));
  3063. if (IS_ERR(bargs)) {
  3064. ret = PTR_ERR(bargs);
  3065. goto out_unlock;
  3066. }
  3067. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  3068. if (!fs_info->balance_ctl) {
  3069. ret = -ENOTCONN;
  3070. goto out_bargs;
  3071. }
  3072. bctl = fs_info->balance_ctl;
  3073. spin_lock(&fs_info->balance_lock);
  3074. bctl->flags |= BTRFS_BALANCE_RESUME;
  3075. spin_unlock(&fs_info->balance_lock);
  3076. goto do_balance;
  3077. }
  3078. } else {
  3079. bargs = NULL;
  3080. }
  3081. if (fs_info->balance_ctl) {
  3082. ret = -EINPROGRESS;
  3083. goto out_bargs;
  3084. }
  3085. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  3086. if (!bctl) {
  3087. ret = -ENOMEM;
  3088. goto out_bargs;
  3089. }
  3090. bctl->fs_info = fs_info;
  3091. if (arg) {
  3092. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  3093. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  3094. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  3095. bctl->flags = bargs->flags;
  3096. } else {
  3097. /* balance everything - no filters */
  3098. bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
  3099. }
  3100. do_balance:
  3101. /*
  3102. * Ownership of bctl and mutually_exclusive_operation_running
  3103. * goes to to btrfs_balance. bctl is freed in __cancel_balance,
  3104. * or, if restriper was paused all the way until unmount, in
  3105. * free_fs_info. mutually_exclusive_operation_running is
  3106. * cleared in __cancel_balance.
  3107. */
  3108. need_unlock = false;
  3109. ret = btrfs_balance(bctl, bargs);
  3110. if (arg) {
  3111. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3112. ret = -EFAULT;
  3113. }
  3114. out_bargs:
  3115. kfree(bargs);
  3116. out_unlock:
  3117. mutex_unlock(&fs_info->balance_mutex);
  3118. mutex_unlock(&fs_info->volume_mutex);
  3119. if (need_unlock)
  3120. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  3121. out:
  3122. mnt_drop_write_file(file);
  3123. return ret;
  3124. }
  3125. static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
  3126. {
  3127. if (!capable(CAP_SYS_ADMIN))
  3128. return -EPERM;
  3129. switch (cmd) {
  3130. case BTRFS_BALANCE_CTL_PAUSE:
  3131. return btrfs_pause_balance(root->fs_info);
  3132. case BTRFS_BALANCE_CTL_CANCEL:
  3133. return btrfs_cancel_balance(root->fs_info);
  3134. }
  3135. return -EINVAL;
  3136. }
  3137. static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
  3138. void __user *arg)
  3139. {
  3140. struct btrfs_fs_info *fs_info = root->fs_info;
  3141. struct btrfs_ioctl_balance_args *bargs;
  3142. int ret = 0;
  3143. if (!capable(CAP_SYS_ADMIN))
  3144. return -EPERM;
  3145. mutex_lock(&fs_info->balance_mutex);
  3146. if (!fs_info->balance_ctl) {
  3147. ret = -ENOTCONN;
  3148. goto out;
  3149. }
  3150. bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
  3151. if (!bargs) {
  3152. ret = -ENOMEM;
  3153. goto out;
  3154. }
  3155. update_ioctl_balance_args(fs_info, 1, bargs);
  3156. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3157. ret = -EFAULT;
  3158. kfree(bargs);
  3159. out:
  3160. mutex_unlock(&fs_info->balance_mutex);
  3161. return ret;
  3162. }
  3163. static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  3164. {
  3165. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3166. struct btrfs_ioctl_quota_ctl_args *sa;
  3167. struct btrfs_trans_handle *trans = NULL;
  3168. int ret;
  3169. int err;
  3170. if (!capable(CAP_SYS_ADMIN))
  3171. return -EPERM;
  3172. ret = mnt_want_write_file(file);
  3173. if (ret)
  3174. return ret;
  3175. sa = memdup_user(arg, sizeof(*sa));
  3176. if (IS_ERR(sa)) {
  3177. ret = PTR_ERR(sa);
  3178. goto drop_write;
  3179. }
  3180. down_write(&root->fs_info->subvol_sem);
  3181. trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
  3182. if (IS_ERR(trans)) {
  3183. ret = PTR_ERR(trans);
  3184. goto out;
  3185. }
  3186. switch (sa->cmd) {
  3187. case BTRFS_QUOTA_CTL_ENABLE:
  3188. ret = btrfs_quota_enable(trans, root->fs_info);
  3189. break;
  3190. case BTRFS_QUOTA_CTL_DISABLE:
  3191. ret = btrfs_quota_disable(trans, root->fs_info);
  3192. break;
  3193. default:
  3194. ret = -EINVAL;
  3195. break;
  3196. }
  3197. err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
  3198. if (err && !ret)
  3199. ret = err;
  3200. out:
  3201. kfree(sa);
  3202. up_write(&root->fs_info->subvol_sem);
  3203. drop_write:
  3204. mnt_drop_write_file(file);
  3205. return ret;
  3206. }
  3207. static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  3208. {
  3209. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3210. struct btrfs_ioctl_qgroup_assign_args *sa;
  3211. struct btrfs_trans_handle *trans;
  3212. int ret;
  3213. int err;
  3214. if (!capable(CAP_SYS_ADMIN))
  3215. return -EPERM;
  3216. ret = mnt_want_write_file(file);
  3217. if (ret)
  3218. return ret;
  3219. sa = memdup_user(arg, sizeof(*sa));
  3220. if (IS_ERR(sa)) {
  3221. ret = PTR_ERR(sa);
  3222. goto drop_write;
  3223. }
  3224. trans = btrfs_join_transaction(root);
  3225. if (IS_ERR(trans)) {
  3226. ret = PTR_ERR(trans);
  3227. goto out;
  3228. }
  3229. /* FIXME: check if the IDs really exist */
  3230. if (sa->assign) {
  3231. ret = btrfs_add_qgroup_relation(trans, root->fs_info,
  3232. sa->src, sa->dst);
  3233. } else {
  3234. ret = btrfs_del_qgroup_relation(trans, root->fs_info,
  3235. sa->src, sa->dst);
  3236. }
  3237. err = btrfs_end_transaction(trans, root);
  3238. if (err && !ret)
  3239. ret = err;
  3240. out:
  3241. kfree(sa);
  3242. drop_write:
  3243. mnt_drop_write_file(file);
  3244. return ret;
  3245. }
  3246. static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  3247. {
  3248. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3249. struct btrfs_ioctl_qgroup_create_args *sa;
  3250. struct btrfs_trans_handle *trans;
  3251. int ret;
  3252. int err;
  3253. if (!capable(CAP_SYS_ADMIN))
  3254. return -EPERM;
  3255. ret = mnt_want_write_file(file);
  3256. if (ret)
  3257. return ret;
  3258. sa = memdup_user(arg, sizeof(*sa));
  3259. if (IS_ERR(sa)) {
  3260. ret = PTR_ERR(sa);
  3261. goto drop_write;
  3262. }
  3263. if (!sa->qgroupid) {
  3264. ret = -EINVAL;
  3265. goto out;
  3266. }
  3267. trans = btrfs_join_transaction(root);
  3268. if (IS_ERR(trans)) {
  3269. ret = PTR_ERR(trans);
  3270. goto out;
  3271. }
  3272. /* FIXME: check if the IDs really exist */
  3273. if (sa->create) {
  3274. ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
  3275. NULL);
  3276. } else {
  3277. ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
  3278. }
  3279. err = btrfs_end_transaction(trans, root);
  3280. if (err && !ret)
  3281. ret = err;
  3282. out:
  3283. kfree(sa);
  3284. drop_write:
  3285. mnt_drop_write_file(file);
  3286. return ret;
  3287. }
  3288. static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  3289. {
  3290. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3291. struct btrfs_ioctl_qgroup_limit_args *sa;
  3292. struct btrfs_trans_handle *trans;
  3293. int ret;
  3294. int err;
  3295. u64 qgroupid;
  3296. if (!capable(CAP_SYS_ADMIN))
  3297. return -EPERM;
  3298. ret = mnt_want_write_file(file);
  3299. if (ret)
  3300. return ret;
  3301. sa = memdup_user(arg, sizeof(*sa));
  3302. if (IS_ERR(sa)) {
  3303. ret = PTR_ERR(sa);
  3304. goto drop_write;
  3305. }
  3306. trans = btrfs_join_transaction(root);
  3307. if (IS_ERR(trans)) {
  3308. ret = PTR_ERR(trans);
  3309. goto out;
  3310. }
  3311. qgroupid = sa->qgroupid;
  3312. if (!qgroupid) {
  3313. /* take the current subvol as qgroup */
  3314. qgroupid = root->root_key.objectid;
  3315. }
  3316. /* FIXME: check if the IDs really exist */
  3317. ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
  3318. err = btrfs_end_transaction(trans, root);
  3319. if (err && !ret)
  3320. ret = err;
  3321. out:
  3322. kfree(sa);
  3323. drop_write:
  3324. mnt_drop_write_file(file);
  3325. return ret;
  3326. }
  3327. static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
  3328. {
  3329. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3330. struct btrfs_ioctl_quota_rescan_args *qsa;
  3331. int ret;
  3332. if (!capable(CAP_SYS_ADMIN))
  3333. return -EPERM;
  3334. ret = mnt_want_write_file(file);
  3335. if (ret)
  3336. return ret;
  3337. qsa = memdup_user(arg, sizeof(*qsa));
  3338. if (IS_ERR(qsa)) {
  3339. ret = PTR_ERR(qsa);
  3340. goto drop_write;
  3341. }
  3342. if (qsa->flags) {
  3343. ret = -EINVAL;
  3344. goto out;
  3345. }
  3346. ret = btrfs_qgroup_rescan(root->fs_info);
  3347. out:
  3348. kfree(qsa);
  3349. drop_write:
  3350. mnt_drop_write_file(file);
  3351. return ret;
  3352. }
  3353. static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
  3354. {
  3355. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3356. struct btrfs_ioctl_quota_rescan_args *qsa;
  3357. int ret = 0;
  3358. if (!capable(CAP_SYS_ADMIN))
  3359. return -EPERM;
  3360. qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
  3361. if (!qsa)
  3362. return -ENOMEM;
  3363. if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  3364. qsa->flags = 1;
  3365. qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
  3366. }
  3367. if (copy_to_user(arg, qsa, sizeof(*qsa)))
  3368. ret = -EFAULT;
  3369. kfree(qsa);
  3370. return ret;
  3371. }
  3372. static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
  3373. {
  3374. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3375. if (!capable(CAP_SYS_ADMIN))
  3376. return -EPERM;
  3377. return btrfs_qgroup_wait_for_completion(root->fs_info);
  3378. }
  3379. static long btrfs_ioctl_set_received_subvol(struct file *file,
  3380. void __user *arg)
  3381. {
  3382. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  3383. struct inode *inode = file_inode(file);
  3384. struct btrfs_root *root = BTRFS_I(inode)->root;
  3385. struct btrfs_root_item *root_item = &root->root_item;
  3386. struct btrfs_trans_handle *trans;
  3387. struct timespec ct = CURRENT_TIME;
  3388. int ret = 0;
  3389. ret = mnt_want_write_file(file);
  3390. if (ret < 0)
  3391. return ret;
  3392. down_write(&root->fs_info->subvol_sem);
  3393. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  3394. ret = -EINVAL;
  3395. goto out;
  3396. }
  3397. if (btrfs_root_readonly(root)) {
  3398. ret = -EROFS;
  3399. goto out;
  3400. }
  3401. if (!inode_owner_or_capable(inode)) {
  3402. ret = -EACCES;
  3403. goto out;
  3404. }
  3405. sa = memdup_user(arg, sizeof(*sa));
  3406. if (IS_ERR(sa)) {
  3407. ret = PTR_ERR(sa);
  3408. sa = NULL;
  3409. goto out;
  3410. }
  3411. trans = btrfs_start_transaction(root, 1);
  3412. if (IS_ERR(trans)) {
  3413. ret = PTR_ERR(trans);
  3414. trans = NULL;
  3415. goto out;
  3416. }
  3417. sa->rtransid = trans->transid;
  3418. sa->rtime.sec = ct.tv_sec;
  3419. sa->rtime.nsec = ct.tv_nsec;
  3420. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  3421. btrfs_set_root_stransid(root_item, sa->stransid);
  3422. btrfs_set_root_rtransid(root_item, sa->rtransid);
  3423. root_item->stime.sec = cpu_to_le64(sa->stime.sec);
  3424. root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
  3425. root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
  3426. root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
  3427. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  3428. &root->root_key, &root->root_item);
  3429. if (ret < 0) {
  3430. btrfs_end_transaction(trans, root);
  3431. trans = NULL;
  3432. goto out;
  3433. } else {
  3434. ret = btrfs_commit_transaction(trans, root);
  3435. if (ret < 0)
  3436. goto out;
  3437. }
  3438. ret = copy_to_user(arg, sa, sizeof(*sa));
  3439. if (ret)
  3440. ret = -EFAULT;
  3441. out:
  3442. kfree(sa);
  3443. up_write(&root->fs_info->subvol_sem);
  3444. mnt_drop_write_file(file);
  3445. return ret;
  3446. }
  3447. static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
  3448. {
  3449. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3450. const char *label = root->fs_info->super_copy->label;
  3451. size_t len = strnlen(label, BTRFS_LABEL_SIZE);
  3452. int ret;
  3453. if (len == BTRFS_LABEL_SIZE) {
  3454. pr_warn("btrfs: label is too long, return the first %zu bytes\n",
  3455. --len);
  3456. }
  3457. mutex_lock(&root->fs_info->volume_mutex);
  3458. ret = copy_to_user(arg, label, len);
  3459. mutex_unlock(&root->fs_info->volume_mutex);
  3460. return ret ? -EFAULT : 0;
  3461. }
  3462. static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
  3463. {
  3464. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3465. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  3466. struct btrfs_trans_handle *trans;
  3467. char label[BTRFS_LABEL_SIZE];
  3468. int ret;
  3469. if (!capable(CAP_SYS_ADMIN))
  3470. return -EPERM;
  3471. if (copy_from_user(label, arg, sizeof(label)))
  3472. return -EFAULT;
  3473. if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
  3474. pr_err("btrfs: unable to set label with more than %d bytes\n",
  3475. BTRFS_LABEL_SIZE - 1);
  3476. return -EINVAL;
  3477. }
  3478. ret = mnt_want_write_file(file);
  3479. if (ret)
  3480. return ret;
  3481. mutex_lock(&root->fs_info->volume_mutex);
  3482. trans = btrfs_start_transaction(root, 0);
  3483. if (IS_ERR(trans)) {
  3484. ret = PTR_ERR(trans);
  3485. goto out_unlock;
  3486. }
  3487. strcpy(super_block->label, label);
  3488. ret = btrfs_end_transaction(trans, root);
  3489. out_unlock:
  3490. mutex_unlock(&root->fs_info->volume_mutex);
  3491. mnt_drop_write_file(file);
  3492. return ret;
  3493. }
  3494. long btrfs_ioctl(struct file *file, unsigned int
  3495. cmd, unsigned long arg)
  3496. {
  3497. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3498. void __user *argp = (void __user *)arg;
  3499. switch (cmd) {
  3500. case FS_IOC_GETFLAGS:
  3501. return btrfs_ioctl_getflags(file, argp);
  3502. case FS_IOC_SETFLAGS:
  3503. return btrfs_ioctl_setflags(file, argp);
  3504. case FS_IOC_GETVERSION:
  3505. return btrfs_ioctl_getversion(file, argp);
  3506. case FITRIM:
  3507. return btrfs_ioctl_fitrim(file, argp);
  3508. case BTRFS_IOC_SNAP_CREATE:
  3509. return btrfs_ioctl_snap_create(file, argp, 0);
  3510. case BTRFS_IOC_SNAP_CREATE_V2:
  3511. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  3512. case BTRFS_IOC_SUBVOL_CREATE:
  3513. return btrfs_ioctl_snap_create(file, argp, 1);
  3514. case BTRFS_IOC_SUBVOL_CREATE_V2:
  3515. return btrfs_ioctl_snap_create_v2(file, argp, 1);
  3516. case BTRFS_IOC_SNAP_DESTROY:
  3517. return btrfs_ioctl_snap_destroy(file, argp);
  3518. case BTRFS_IOC_SUBVOL_GETFLAGS:
  3519. return btrfs_ioctl_subvol_getflags(file, argp);
  3520. case BTRFS_IOC_SUBVOL_SETFLAGS:
  3521. return btrfs_ioctl_subvol_setflags(file, argp);
  3522. case BTRFS_IOC_DEFAULT_SUBVOL:
  3523. return btrfs_ioctl_default_subvol(file, argp);
  3524. case BTRFS_IOC_DEFRAG:
  3525. return btrfs_ioctl_defrag(file, NULL);
  3526. case BTRFS_IOC_DEFRAG_RANGE:
  3527. return btrfs_ioctl_defrag(file, argp);
  3528. case BTRFS_IOC_RESIZE:
  3529. return btrfs_ioctl_resize(file, argp);
  3530. case BTRFS_IOC_ADD_DEV:
  3531. return btrfs_ioctl_add_dev(root, argp);
  3532. case BTRFS_IOC_RM_DEV:
  3533. return btrfs_ioctl_rm_dev(file, argp);
  3534. case BTRFS_IOC_FS_INFO:
  3535. return btrfs_ioctl_fs_info(root, argp);
  3536. case BTRFS_IOC_DEV_INFO:
  3537. return btrfs_ioctl_dev_info(root, argp);
  3538. case BTRFS_IOC_BALANCE:
  3539. return btrfs_ioctl_balance(file, NULL);
  3540. case BTRFS_IOC_CLONE:
  3541. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  3542. case BTRFS_IOC_CLONE_RANGE:
  3543. return btrfs_ioctl_clone_range(file, argp);
  3544. case BTRFS_IOC_TRANS_START:
  3545. return btrfs_ioctl_trans_start(file);
  3546. case BTRFS_IOC_TRANS_END:
  3547. return btrfs_ioctl_trans_end(file);
  3548. case BTRFS_IOC_TREE_SEARCH:
  3549. return btrfs_ioctl_tree_search(file, argp);
  3550. case BTRFS_IOC_INO_LOOKUP:
  3551. return btrfs_ioctl_ino_lookup(file, argp);
  3552. case BTRFS_IOC_INO_PATHS:
  3553. return btrfs_ioctl_ino_to_path(root, argp);
  3554. case BTRFS_IOC_LOGICAL_INO:
  3555. return btrfs_ioctl_logical_to_ino(root, argp);
  3556. case BTRFS_IOC_SPACE_INFO:
  3557. return btrfs_ioctl_space_info(root, argp);
  3558. case BTRFS_IOC_SYNC:
  3559. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  3560. return 0;
  3561. case BTRFS_IOC_START_SYNC:
  3562. return btrfs_ioctl_start_sync(root, argp);
  3563. case BTRFS_IOC_WAIT_SYNC:
  3564. return btrfs_ioctl_wait_sync(root, argp);
  3565. case BTRFS_IOC_SCRUB:
  3566. return btrfs_ioctl_scrub(file, argp);
  3567. case BTRFS_IOC_SCRUB_CANCEL:
  3568. return btrfs_ioctl_scrub_cancel(root, argp);
  3569. case BTRFS_IOC_SCRUB_PROGRESS:
  3570. return btrfs_ioctl_scrub_progress(root, argp);
  3571. case BTRFS_IOC_BALANCE_V2:
  3572. return btrfs_ioctl_balance(file, argp);
  3573. case BTRFS_IOC_BALANCE_CTL:
  3574. return btrfs_ioctl_balance_ctl(root, arg);
  3575. case BTRFS_IOC_BALANCE_PROGRESS:
  3576. return btrfs_ioctl_balance_progress(root, argp);
  3577. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  3578. return btrfs_ioctl_set_received_subvol(file, argp);
  3579. case BTRFS_IOC_SEND:
  3580. return btrfs_ioctl_send(file, argp);
  3581. case BTRFS_IOC_GET_DEV_STATS:
  3582. return btrfs_ioctl_get_dev_stats(root, argp);
  3583. case BTRFS_IOC_QUOTA_CTL:
  3584. return btrfs_ioctl_quota_ctl(file, argp);
  3585. case BTRFS_IOC_QGROUP_ASSIGN:
  3586. return btrfs_ioctl_qgroup_assign(file, argp);
  3587. case BTRFS_IOC_QGROUP_CREATE:
  3588. return btrfs_ioctl_qgroup_create(file, argp);
  3589. case BTRFS_IOC_QGROUP_LIMIT:
  3590. return btrfs_ioctl_qgroup_limit(file, argp);
  3591. case BTRFS_IOC_QUOTA_RESCAN:
  3592. return btrfs_ioctl_quota_rescan(file, argp);
  3593. case BTRFS_IOC_QUOTA_RESCAN_STATUS:
  3594. return btrfs_ioctl_quota_rescan_status(file, argp);
  3595. case BTRFS_IOC_QUOTA_RESCAN_WAIT:
  3596. return btrfs_ioctl_quota_rescan_wait(file, argp);
  3597. case BTRFS_IOC_DEV_REPLACE:
  3598. return btrfs_ioctl_dev_replace(root, argp);
  3599. case BTRFS_IOC_GET_FSLABEL:
  3600. return btrfs_ioctl_get_fslabel(file, argp);
  3601. case BTRFS_IOC_SET_FSLABEL:
  3602. return btrfs_ioctl_set_fslabel(file, argp);
  3603. }
  3604. return -ENOTTY;
  3605. }