ioctl.c 82 KB

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