ioctl.c 94 KB

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