ioctl.c 82 KB

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