ioctl.c 79 KB

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