ioctl.c 81 KB

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