ioctl.c 71 KB

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