ioctl.c 74 KB

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