ioctl.c 71 KB

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