ioctl.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959
  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. while (i <= last_index && defrag_count < max_to_defrag) {
  920. /*
  921. * make sure we stop running if someone unmounts
  922. * the FS
  923. */
  924. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  925. break;
  926. if (!newer_than &&
  927. !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  928. PAGE_CACHE_SIZE,
  929. extent_thresh,
  930. &last_len, &skip,
  931. &defrag_end)) {
  932. unsigned long next;
  933. /*
  934. * the should_defrag function tells us how much to skip
  935. * bump our counter by the suggested amount
  936. */
  937. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  938. i = max(i + 1, next);
  939. continue;
  940. }
  941. if (!newer_than) {
  942. cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
  943. PAGE_CACHE_SHIFT) - i;
  944. cluster = min(cluster, max_cluster);
  945. } else {
  946. cluster = max_cluster;
  947. }
  948. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  949. BTRFS_I(inode)->force_compress = compress_type;
  950. if (i + cluster > ra_index) {
  951. ra_index = max(i, ra_index);
  952. btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
  953. cluster);
  954. ra_index += max_cluster;
  955. }
  956. ret = cluster_pages_for_defrag(inode, pages, i, cluster);
  957. if (ret < 0)
  958. goto out_ra;
  959. defrag_count += ret;
  960. balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
  961. if (newer_than) {
  962. if (newer_off == (u64)-1)
  963. break;
  964. newer_off = max(newer_off + 1,
  965. (u64)i << PAGE_CACHE_SHIFT);
  966. ret = find_new_extents(root, inode,
  967. newer_than, &newer_off,
  968. 64 * 1024);
  969. if (!ret) {
  970. range->start = newer_off;
  971. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  972. } else {
  973. break;
  974. }
  975. } else {
  976. if (ret > 0) {
  977. i += ret;
  978. last_len += ret << PAGE_CACHE_SHIFT;
  979. } else {
  980. i++;
  981. last_len = 0;
  982. }
  983. }
  984. }
  985. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  986. filemap_flush(inode->i_mapping);
  987. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  988. /* the filemap_flush will queue IO into the worker threads, but
  989. * we have to make sure the IO is actually started and that
  990. * ordered extents get created before we return
  991. */
  992. atomic_inc(&root->fs_info->async_submit_draining);
  993. while (atomic_read(&root->fs_info->nr_async_submits) ||
  994. atomic_read(&root->fs_info->async_delalloc_pages)) {
  995. wait_event(root->fs_info->async_submit_wait,
  996. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  997. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  998. }
  999. atomic_dec(&root->fs_info->async_submit_draining);
  1000. mutex_lock(&inode->i_mutex);
  1001. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  1002. mutex_unlock(&inode->i_mutex);
  1003. }
  1004. disk_super = &root->fs_info->super_copy;
  1005. features = btrfs_super_incompat_flags(disk_super);
  1006. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  1007. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  1008. btrfs_set_super_incompat_flags(disk_super, features);
  1009. }
  1010. ret = defrag_count;
  1011. out_ra:
  1012. if (!file)
  1013. kfree(ra);
  1014. kfree(pages);
  1015. return ret;
  1016. }
  1017. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  1018. void __user *arg)
  1019. {
  1020. u64 new_size;
  1021. u64 old_size;
  1022. u64 devid = 1;
  1023. struct btrfs_ioctl_vol_args *vol_args;
  1024. struct btrfs_trans_handle *trans;
  1025. struct btrfs_device *device = NULL;
  1026. char *sizestr;
  1027. char *devstr = NULL;
  1028. int ret = 0;
  1029. int mod = 0;
  1030. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1031. return -EROFS;
  1032. if (!capable(CAP_SYS_ADMIN))
  1033. return -EPERM;
  1034. vol_args = memdup_user(arg, sizeof(*vol_args));
  1035. if (IS_ERR(vol_args))
  1036. return PTR_ERR(vol_args);
  1037. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1038. mutex_lock(&root->fs_info->volume_mutex);
  1039. sizestr = vol_args->name;
  1040. devstr = strchr(sizestr, ':');
  1041. if (devstr) {
  1042. char *end;
  1043. sizestr = devstr + 1;
  1044. *devstr = '\0';
  1045. devstr = vol_args->name;
  1046. devid = simple_strtoull(devstr, &end, 10);
  1047. printk(KERN_INFO "resizing devid %llu\n",
  1048. (unsigned long long)devid);
  1049. }
  1050. device = btrfs_find_device(root, devid, NULL, NULL);
  1051. if (!device) {
  1052. printk(KERN_INFO "resizer unable to find device %llu\n",
  1053. (unsigned long long)devid);
  1054. ret = -EINVAL;
  1055. goto out_unlock;
  1056. }
  1057. if (!strcmp(sizestr, "max"))
  1058. new_size = device->bdev->bd_inode->i_size;
  1059. else {
  1060. if (sizestr[0] == '-') {
  1061. mod = -1;
  1062. sizestr++;
  1063. } else if (sizestr[0] == '+') {
  1064. mod = 1;
  1065. sizestr++;
  1066. }
  1067. new_size = memparse(sizestr, NULL);
  1068. if (new_size == 0) {
  1069. ret = -EINVAL;
  1070. goto out_unlock;
  1071. }
  1072. }
  1073. old_size = device->total_bytes;
  1074. if (mod < 0) {
  1075. if (new_size > old_size) {
  1076. ret = -EINVAL;
  1077. goto out_unlock;
  1078. }
  1079. new_size = old_size - new_size;
  1080. } else if (mod > 0) {
  1081. new_size = old_size + new_size;
  1082. }
  1083. if (new_size < 256 * 1024 * 1024) {
  1084. ret = -EINVAL;
  1085. goto out_unlock;
  1086. }
  1087. if (new_size > device->bdev->bd_inode->i_size) {
  1088. ret = -EFBIG;
  1089. goto out_unlock;
  1090. }
  1091. do_div(new_size, root->sectorsize);
  1092. new_size *= root->sectorsize;
  1093. printk(KERN_INFO "new size for %s is %llu\n",
  1094. device->name, (unsigned long long)new_size);
  1095. if (new_size > old_size) {
  1096. trans = btrfs_start_transaction(root, 0);
  1097. if (IS_ERR(trans)) {
  1098. ret = PTR_ERR(trans);
  1099. goto out_unlock;
  1100. }
  1101. ret = btrfs_grow_device(trans, device, new_size);
  1102. btrfs_commit_transaction(trans, root);
  1103. } else {
  1104. ret = btrfs_shrink_device(device, new_size);
  1105. }
  1106. out_unlock:
  1107. mutex_unlock(&root->fs_info->volume_mutex);
  1108. kfree(vol_args);
  1109. return ret;
  1110. }
  1111. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1112. char *name,
  1113. unsigned long fd,
  1114. int subvol,
  1115. u64 *transid,
  1116. bool readonly)
  1117. {
  1118. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1119. struct file *src_file;
  1120. int namelen;
  1121. int ret = 0;
  1122. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1123. return -EROFS;
  1124. namelen = strlen(name);
  1125. if (strchr(name, '/')) {
  1126. ret = -EINVAL;
  1127. goto out;
  1128. }
  1129. if (subvol) {
  1130. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1131. NULL, transid, readonly);
  1132. } else {
  1133. struct inode *src_inode;
  1134. src_file = fget(fd);
  1135. if (!src_file) {
  1136. ret = -EINVAL;
  1137. goto out;
  1138. }
  1139. src_inode = src_file->f_path.dentry->d_inode;
  1140. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  1141. printk(KERN_INFO "btrfs: Snapshot src from "
  1142. "another FS\n");
  1143. ret = -EINVAL;
  1144. fput(src_file);
  1145. goto out;
  1146. }
  1147. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1148. BTRFS_I(src_inode)->root,
  1149. transid, readonly);
  1150. fput(src_file);
  1151. }
  1152. out:
  1153. return ret;
  1154. }
  1155. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1156. void __user *arg, int subvol)
  1157. {
  1158. struct btrfs_ioctl_vol_args *vol_args;
  1159. int ret;
  1160. vol_args = memdup_user(arg, sizeof(*vol_args));
  1161. if (IS_ERR(vol_args))
  1162. return PTR_ERR(vol_args);
  1163. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1164. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1165. vol_args->fd, subvol,
  1166. NULL, false);
  1167. kfree(vol_args);
  1168. return ret;
  1169. }
  1170. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1171. void __user *arg, int subvol)
  1172. {
  1173. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1174. int ret;
  1175. u64 transid = 0;
  1176. u64 *ptr = NULL;
  1177. bool readonly = false;
  1178. vol_args = memdup_user(arg, sizeof(*vol_args));
  1179. if (IS_ERR(vol_args))
  1180. return PTR_ERR(vol_args);
  1181. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1182. if (vol_args->flags &
  1183. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
  1184. ret = -EOPNOTSUPP;
  1185. goto out;
  1186. }
  1187. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1188. ptr = &transid;
  1189. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1190. readonly = true;
  1191. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1192. vol_args->fd, subvol,
  1193. ptr, readonly);
  1194. if (ret == 0 && ptr &&
  1195. copy_to_user(arg +
  1196. offsetof(struct btrfs_ioctl_vol_args_v2,
  1197. transid), ptr, sizeof(*ptr)))
  1198. ret = -EFAULT;
  1199. out:
  1200. kfree(vol_args);
  1201. return ret;
  1202. }
  1203. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1204. void __user *arg)
  1205. {
  1206. struct inode *inode = fdentry(file)->d_inode;
  1207. struct btrfs_root *root = BTRFS_I(inode)->root;
  1208. int ret = 0;
  1209. u64 flags = 0;
  1210. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1211. return -EINVAL;
  1212. down_read(&root->fs_info->subvol_sem);
  1213. if (btrfs_root_readonly(root))
  1214. flags |= BTRFS_SUBVOL_RDONLY;
  1215. up_read(&root->fs_info->subvol_sem);
  1216. if (copy_to_user(arg, &flags, sizeof(flags)))
  1217. ret = -EFAULT;
  1218. return ret;
  1219. }
  1220. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1221. void __user *arg)
  1222. {
  1223. struct inode *inode = fdentry(file)->d_inode;
  1224. struct btrfs_root *root = BTRFS_I(inode)->root;
  1225. struct btrfs_trans_handle *trans;
  1226. u64 root_flags;
  1227. u64 flags;
  1228. int ret = 0;
  1229. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1230. return -EROFS;
  1231. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1232. return -EINVAL;
  1233. if (copy_from_user(&flags, arg, sizeof(flags)))
  1234. return -EFAULT;
  1235. if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1236. return -EINVAL;
  1237. if (flags & ~BTRFS_SUBVOL_RDONLY)
  1238. return -EOPNOTSUPP;
  1239. if (!inode_owner_or_capable(inode))
  1240. return -EACCES;
  1241. down_write(&root->fs_info->subvol_sem);
  1242. /* nothing to do */
  1243. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1244. goto out;
  1245. root_flags = btrfs_root_flags(&root->root_item);
  1246. if (flags & BTRFS_SUBVOL_RDONLY)
  1247. btrfs_set_root_flags(&root->root_item,
  1248. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1249. else
  1250. btrfs_set_root_flags(&root->root_item,
  1251. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1252. trans = btrfs_start_transaction(root, 1);
  1253. if (IS_ERR(trans)) {
  1254. ret = PTR_ERR(trans);
  1255. goto out_reset;
  1256. }
  1257. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1258. &root->root_key, &root->root_item);
  1259. btrfs_commit_transaction(trans, root);
  1260. out_reset:
  1261. if (ret)
  1262. btrfs_set_root_flags(&root->root_item, root_flags);
  1263. out:
  1264. up_write(&root->fs_info->subvol_sem);
  1265. return ret;
  1266. }
  1267. /*
  1268. * helper to check if the subvolume references other subvolumes
  1269. */
  1270. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1271. {
  1272. struct btrfs_path *path;
  1273. struct btrfs_key key;
  1274. int ret;
  1275. path = btrfs_alloc_path();
  1276. if (!path)
  1277. return -ENOMEM;
  1278. key.objectid = root->root_key.objectid;
  1279. key.type = BTRFS_ROOT_REF_KEY;
  1280. key.offset = (u64)-1;
  1281. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1282. &key, path, 0, 0);
  1283. if (ret < 0)
  1284. goto out;
  1285. BUG_ON(ret == 0);
  1286. ret = 0;
  1287. if (path->slots[0] > 0) {
  1288. path->slots[0]--;
  1289. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1290. if (key.objectid == root->root_key.objectid &&
  1291. key.type == BTRFS_ROOT_REF_KEY)
  1292. ret = -ENOTEMPTY;
  1293. }
  1294. out:
  1295. btrfs_free_path(path);
  1296. return ret;
  1297. }
  1298. static noinline int key_in_sk(struct btrfs_key *key,
  1299. struct btrfs_ioctl_search_key *sk)
  1300. {
  1301. struct btrfs_key test;
  1302. int ret;
  1303. test.objectid = sk->min_objectid;
  1304. test.type = sk->min_type;
  1305. test.offset = sk->min_offset;
  1306. ret = btrfs_comp_cpu_keys(key, &test);
  1307. if (ret < 0)
  1308. return 0;
  1309. test.objectid = sk->max_objectid;
  1310. test.type = sk->max_type;
  1311. test.offset = sk->max_offset;
  1312. ret = btrfs_comp_cpu_keys(key, &test);
  1313. if (ret > 0)
  1314. return 0;
  1315. return 1;
  1316. }
  1317. static noinline int copy_to_sk(struct btrfs_root *root,
  1318. struct btrfs_path *path,
  1319. struct btrfs_key *key,
  1320. struct btrfs_ioctl_search_key *sk,
  1321. char *buf,
  1322. unsigned long *sk_offset,
  1323. int *num_found)
  1324. {
  1325. u64 found_transid;
  1326. struct extent_buffer *leaf;
  1327. struct btrfs_ioctl_search_header sh;
  1328. unsigned long item_off;
  1329. unsigned long item_len;
  1330. int nritems;
  1331. int i;
  1332. int slot;
  1333. int ret = 0;
  1334. leaf = path->nodes[0];
  1335. slot = path->slots[0];
  1336. nritems = btrfs_header_nritems(leaf);
  1337. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1338. i = nritems;
  1339. goto advance_key;
  1340. }
  1341. found_transid = btrfs_header_generation(leaf);
  1342. for (i = slot; i < nritems; i++) {
  1343. item_off = btrfs_item_ptr_offset(leaf, i);
  1344. item_len = btrfs_item_size_nr(leaf, i);
  1345. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1346. item_len = 0;
  1347. if (sizeof(sh) + item_len + *sk_offset >
  1348. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1349. ret = 1;
  1350. goto overflow;
  1351. }
  1352. btrfs_item_key_to_cpu(leaf, key, i);
  1353. if (!key_in_sk(key, sk))
  1354. continue;
  1355. sh.objectid = key->objectid;
  1356. sh.offset = key->offset;
  1357. sh.type = key->type;
  1358. sh.len = item_len;
  1359. sh.transid = found_transid;
  1360. /* copy search result header */
  1361. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1362. *sk_offset += sizeof(sh);
  1363. if (item_len) {
  1364. char *p = buf + *sk_offset;
  1365. /* copy the item */
  1366. read_extent_buffer(leaf, p,
  1367. item_off, item_len);
  1368. *sk_offset += item_len;
  1369. }
  1370. (*num_found)++;
  1371. if (*num_found >= sk->nr_items)
  1372. break;
  1373. }
  1374. advance_key:
  1375. ret = 0;
  1376. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1377. key->offset++;
  1378. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1379. key->offset = 0;
  1380. key->type++;
  1381. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1382. key->offset = 0;
  1383. key->type = 0;
  1384. key->objectid++;
  1385. } else
  1386. ret = 1;
  1387. overflow:
  1388. return ret;
  1389. }
  1390. static noinline int search_ioctl(struct inode *inode,
  1391. struct btrfs_ioctl_search_args *args)
  1392. {
  1393. struct btrfs_root *root;
  1394. struct btrfs_key key;
  1395. struct btrfs_key max_key;
  1396. struct btrfs_path *path;
  1397. struct btrfs_ioctl_search_key *sk = &args->key;
  1398. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1399. int ret;
  1400. int num_found = 0;
  1401. unsigned long sk_offset = 0;
  1402. path = btrfs_alloc_path();
  1403. if (!path)
  1404. return -ENOMEM;
  1405. if (sk->tree_id == 0) {
  1406. /* search the root of the inode that was passed */
  1407. root = BTRFS_I(inode)->root;
  1408. } else {
  1409. key.objectid = sk->tree_id;
  1410. key.type = BTRFS_ROOT_ITEM_KEY;
  1411. key.offset = (u64)-1;
  1412. root = btrfs_read_fs_root_no_name(info, &key);
  1413. if (IS_ERR(root)) {
  1414. printk(KERN_ERR "could not find root %llu\n",
  1415. sk->tree_id);
  1416. btrfs_free_path(path);
  1417. return -ENOENT;
  1418. }
  1419. }
  1420. key.objectid = sk->min_objectid;
  1421. key.type = sk->min_type;
  1422. key.offset = sk->min_offset;
  1423. max_key.objectid = sk->max_objectid;
  1424. max_key.type = sk->max_type;
  1425. max_key.offset = sk->max_offset;
  1426. path->keep_locks = 1;
  1427. while(1) {
  1428. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1429. sk->min_transid);
  1430. if (ret != 0) {
  1431. if (ret > 0)
  1432. ret = 0;
  1433. goto err;
  1434. }
  1435. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1436. &sk_offset, &num_found);
  1437. btrfs_release_path(path);
  1438. if (ret || num_found >= sk->nr_items)
  1439. break;
  1440. }
  1441. ret = 0;
  1442. err:
  1443. sk->nr_items = num_found;
  1444. btrfs_free_path(path);
  1445. return ret;
  1446. }
  1447. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1448. void __user *argp)
  1449. {
  1450. struct btrfs_ioctl_search_args *args;
  1451. struct inode *inode;
  1452. int ret;
  1453. if (!capable(CAP_SYS_ADMIN))
  1454. return -EPERM;
  1455. args = memdup_user(argp, sizeof(*args));
  1456. if (IS_ERR(args))
  1457. return PTR_ERR(args);
  1458. inode = fdentry(file)->d_inode;
  1459. ret = search_ioctl(inode, args);
  1460. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1461. ret = -EFAULT;
  1462. kfree(args);
  1463. return ret;
  1464. }
  1465. /*
  1466. * Search INODE_REFs to identify path name of 'dirid' directory
  1467. * in a 'tree_id' tree. and sets path name to 'name'.
  1468. */
  1469. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1470. u64 tree_id, u64 dirid, char *name)
  1471. {
  1472. struct btrfs_root *root;
  1473. struct btrfs_key key;
  1474. char *ptr;
  1475. int ret = -1;
  1476. int slot;
  1477. int len;
  1478. int total_len = 0;
  1479. struct btrfs_inode_ref *iref;
  1480. struct extent_buffer *l;
  1481. struct btrfs_path *path;
  1482. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1483. name[0]='\0';
  1484. return 0;
  1485. }
  1486. path = btrfs_alloc_path();
  1487. if (!path)
  1488. return -ENOMEM;
  1489. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1490. key.objectid = tree_id;
  1491. key.type = BTRFS_ROOT_ITEM_KEY;
  1492. key.offset = (u64)-1;
  1493. root = btrfs_read_fs_root_no_name(info, &key);
  1494. if (IS_ERR(root)) {
  1495. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1496. ret = -ENOENT;
  1497. goto out;
  1498. }
  1499. key.objectid = dirid;
  1500. key.type = BTRFS_INODE_REF_KEY;
  1501. key.offset = (u64)-1;
  1502. while(1) {
  1503. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1504. if (ret < 0)
  1505. goto out;
  1506. l = path->nodes[0];
  1507. slot = path->slots[0];
  1508. if (ret > 0 && slot > 0)
  1509. slot--;
  1510. btrfs_item_key_to_cpu(l, &key, slot);
  1511. if (ret > 0 && (key.objectid != dirid ||
  1512. key.type != BTRFS_INODE_REF_KEY)) {
  1513. ret = -ENOENT;
  1514. goto out;
  1515. }
  1516. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1517. len = btrfs_inode_ref_name_len(l, iref);
  1518. ptr -= len + 1;
  1519. total_len += len + 1;
  1520. if (ptr < name)
  1521. goto out;
  1522. *(ptr + len) = '/';
  1523. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1524. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1525. break;
  1526. btrfs_release_path(path);
  1527. key.objectid = key.offset;
  1528. key.offset = (u64)-1;
  1529. dirid = key.objectid;
  1530. }
  1531. if (ptr < name)
  1532. goto out;
  1533. memmove(name, ptr, total_len);
  1534. name[total_len]='\0';
  1535. ret = 0;
  1536. out:
  1537. btrfs_free_path(path);
  1538. return ret;
  1539. }
  1540. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1541. void __user *argp)
  1542. {
  1543. struct btrfs_ioctl_ino_lookup_args *args;
  1544. struct inode *inode;
  1545. int ret;
  1546. if (!capable(CAP_SYS_ADMIN))
  1547. return -EPERM;
  1548. args = memdup_user(argp, sizeof(*args));
  1549. if (IS_ERR(args))
  1550. return PTR_ERR(args);
  1551. inode = fdentry(file)->d_inode;
  1552. if (args->treeid == 0)
  1553. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1554. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1555. args->treeid, args->objectid,
  1556. args->name);
  1557. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1558. ret = -EFAULT;
  1559. kfree(args);
  1560. return ret;
  1561. }
  1562. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1563. void __user *arg)
  1564. {
  1565. struct dentry *parent = fdentry(file);
  1566. struct dentry *dentry;
  1567. struct inode *dir = parent->d_inode;
  1568. struct inode *inode;
  1569. struct btrfs_root *root = BTRFS_I(dir)->root;
  1570. struct btrfs_root *dest = NULL;
  1571. struct btrfs_ioctl_vol_args *vol_args;
  1572. struct btrfs_trans_handle *trans;
  1573. int namelen;
  1574. int ret;
  1575. int err = 0;
  1576. vol_args = memdup_user(arg, sizeof(*vol_args));
  1577. if (IS_ERR(vol_args))
  1578. return PTR_ERR(vol_args);
  1579. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1580. namelen = strlen(vol_args->name);
  1581. if (strchr(vol_args->name, '/') ||
  1582. strncmp(vol_args->name, "..", namelen) == 0) {
  1583. err = -EINVAL;
  1584. goto out;
  1585. }
  1586. err = mnt_want_write(file->f_path.mnt);
  1587. if (err)
  1588. goto out;
  1589. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1590. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1591. if (IS_ERR(dentry)) {
  1592. err = PTR_ERR(dentry);
  1593. goto out_unlock_dir;
  1594. }
  1595. if (!dentry->d_inode) {
  1596. err = -ENOENT;
  1597. goto out_dput;
  1598. }
  1599. inode = dentry->d_inode;
  1600. dest = BTRFS_I(inode)->root;
  1601. if (!capable(CAP_SYS_ADMIN)){
  1602. /*
  1603. * Regular user. Only allow this with a special mount
  1604. * option, when the user has write+exec access to the
  1605. * subvol root, and when rmdir(2) would have been
  1606. * allowed.
  1607. *
  1608. * Note that this is _not_ check that the subvol is
  1609. * empty or doesn't contain data that we wouldn't
  1610. * otherwise be able to delete.
  1611. *
  1612. * Users who want to delete empty subvols should try
  1613. * rmdir(2).
  1614. */
  1615. err = -EPERM;
  1616. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1617. goto out_dput;
  1618. /*
  1619. * Do not allow deletion if the parent dir is the same
  1620. * as the dir to be deleted. That means the ioctl
  1621. * must be called on the dentry referencing the root
  1622. * of the subvol, not a random directory contained
  1623. * within it.
  1624. */
  1625. err = -EINVAL;
  1626. if (root == dest)
  1627. goto out_dput;
  1628. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1629. if (err)
  1630. goto out_dput;
  1631. /* check if subvolume may be deleted by a non-root user */
  1632. err = btrfs_may_delete(dir, dentry, 1);
  1633. if (err)
  1634. goto out_dput;
  1635. }
  1636. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1637. err = -EINVAL;
  1638. goto out_dput;
  1639. }
  1640. mutex_lock(&inode->i_mutex);
  1641. err = d_invalidate(dentry);
  1642. if (err)
  1643. goto out_unlock;
  1644. down_write(&root->fs_info->subvol_sem);
  1645. err = may_destroy_subvol(dest);
  1646. if (err)
  1647. goto out_up_write;
  1648. trans = btrfs_start_transaction(root, 0);
  1649. if (IS_ERR(trans)) {
  1650. err = PTR_ERR(trans);
  1651. goto out_up_write;
  1652. }
  1653. trans->block_rsv = &root->fs_info->global_block_rsv;
  1654. ret = btrfs_unlink_subvol(trans, root, dir,
  1655. dest->root_key.objectid,
  1656. dentry->d_name.name,
  1657. dentry->d_name.len);
  1658. BUG_ON(ret);
  1659. btrfs_record_root_in_trans(trans, dest);
  1660. memset(&dest->root_item.drop_progress, 0,
  1661. sizeof(dest->root_item.drop_progress));
  1662. dest->root_item.drop_level = 0;
  1663. btrfs_set_root_refs(&dest->root_item, 0);
  1664. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1665. ret = btrfs_insert_orphan_item(trans,
  1666. root->fs_info->tree_root,
  1667. dest->root_key.objectid);
  1668. BUG_ON(ret);
  1669. }
  1670. ret = btrfs_end_transaction(trans, root);
  1671. BUG_ON(ret);
  1672. inode->i_flags |= S_DEAD;
  1673. out_up_write:
  1674. up_write(&root->fs_info->subvol_sem);
  1675. out_unlock:
  1676. mutex_unlock(&inode->i_mutex);
  1677. if (!err) {
  1678. shrink_dcache_sb(root->fs_info->sb);
  1679. btrfs_invalidate_inodes(dest);
  1680. d_delete(dentry);
  1681. }
  1682. out_dput:
  1683. dput(dentry);
  1684. out_unlock_dir:
  1685. mutex_unlock(&dir->i_mutex);
  1686. mnt_drop_write(file->f_path.mnt);
  1687. out:
  1688. kfree(vol_args);
  1689. return err;
  1690. }
  1691. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1692. {
  1693. struct inode *inode = fdentry(file)->d_inode;
  1694. struct btrfs_root *root = BTRFS_I(inode)->root;
  1695. struct btrfs_ioctl_defrag_range_args *range;
  1696. int ret;
  1697. if (btrfs_root_readonly(root))
  1698. return -EROFS;
  1699. ret = mnt_want_write(file->f_path.mnt);
  1700. if (ret)
  1701. return ret;
  1702. switch (inode->i_mode & S_IFMT) {
  1703. case S_IFDIR:
  1704. if (!capable(CAP_SYS_ADMIN)) {
  1705. ret = -EPERM;
  1706. goto out;
  1707. }
  1708. ret = btrfs_defrag_root(root, 0);
  1709. if (ret)
  1710. goto out;
  1711. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1712. break;
  1713. case S_IFREG:
  1714. if (!(file->f_mode & FMODE_WRITE)) {
  1715. ret = -EINVAL;
  1716. goto out;
  1717. }
  1718. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1719. if (!range) {
  1720. ret = -ENOMEM;
  1721. goto out;
  1722. }
  1723. if (argp) {
  1724. if (copy_from_user(range, argp,
  1725. sizeof(*range))) {
  1726. ret = -EFAULT;
  1727. kfree(range);
  1728. goto out;
  1729. }
  1730. /* compression requires us to start the IO */
  1731. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1732. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1733. range->extent_thresh = (u32)-1;
  1734. }
  1735. } else {
  1736. /* the rest are all set to zero by kzalloc */
  1737. range->len = (u64)-1;
  1738. }
  1739. ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
  1740. range, 0, 0);
  1741. if (ret > 0)
  1742. ret = 0;
  1743. kfree(range);
  1744. break;
  1745. default:
  1746. ret = -EINVAL;
  1747. }
  1748. out:
  1749. mnt_drop_write(file->f_path.mnt);
  1750. return ret;
  1751. }
  1752. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1753. {
  1754. struct btrfs_ioctl_vol_args *vol_args;
  1755. int ret;
  1756. if (!capable(CAP_SYS_ADMIN))
  1757. return -EPERM;
  1758. vol_args = memdup_user(arg, sizeof(*vol_args));
  1759. if (IS_ERR(vol_args))
  1760. return PTR_ERR(vol_args);
  1761. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1762. ret = btrfs_init_new_device(root, vol_args->name);
  1763. kfree(vol_args);
  1764. return ret;
  1765. }
  1766. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1767. {
  1768. struct btrfs_ioctl_vol_args *vol_args;
  1769. int ret;
  1770. if (!capable(CAP_SYS_ADMIN))
  1771. return -EPERM;
  1772. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1773. return -EROFS;
  1774. vol_args = memdup_user(arg, sizeof(*vol_args));
  1775. if (IS_ERR(vol_args))
  1776. return PTR_ERR(vol_args);
  1777. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1778. ret = btrfs_rm_device(root, vol_args->name);
  1779. kfree(vol_args);
  1780. return ret;
  1781. }
  1782. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  1783. {
  1784. struct btrfs_ioctl_fs_info_args *fi_args;
  1785. struct btrfs_device *device;
  1786. struct btrfs_device *next;
  1787. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1788. int ret = 0;
  1789. if (!capable(CAP_SYS_ADMIN))
  1790. return -EPERM;
  1791. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  1792. if (!fi_args)
  1793. return -ENOMEM;
  1794. fi_args->num_devices = fs_devices->num_devices;
  1795. memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
  1796. mutex_lock(&fs_devices->device_list_mutex);
  1797. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  1798. if (device->devid > fi_args->max_id)
  1799. fi_args->max_id = device->devid;
  1800. }
  1801. mutex_unlock(&fs_devices->device_list_mutex);
  1802. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  1803. ret = -EFAULT;
  1804. kfree(fi_args);
  1805. return ret;
  1806. }
  1807. static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
  1808. {
  1809. struct btrfs_ioctl_dev_info_args *di_args;
  1810. struct btrfs_device *dev;
  1811. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1812. int ret = 0;
  1813. char *s_uuid = NULL;
  1814. char empty_uuid[BTRFS_UUID_SIZE] = {0};
  1815. if (!capable(CAP_SYS_ADMIN))
  1816. return -EPERM;
  1817. di_args = memdup_user(arg, sizeof(*di_args));
  1818. if (IS_ERR(di_args))
  1819. return PTR_ERR(di_args);
  1820. if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
  1821. s_uuid = di_args->uuid;
  1822. mutex_lock(&fs_devices->device_list_mutex);
  1823. dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
  1824. mutex_unlock(&fs_devices->device_list_mutex);
  1825. if (!dev) {
  1826. ret = -ENODEV;
  1827. goto out;
  1828. }
  1829. di_args->devid = dev->devid;
  1830. di_args->bytes_used = dev->bytes_used;
  1831. di_args->total_bytes = dev->total_bytes;
  1832. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  1833. strncpy(di_args->path, dev->name, sizeof(di_args->path));
  1834. out:
  1835. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  1836. ret = -EFAULT;
  1837. kfree(di_args);
  1838. return ret;
  1839. }
  1840. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1841. u64 off, u64 olen, u64 destoff)
  1842. {
  1843. struct inode *inode = fdentry(file)->d_inode;
  1844. struct btrfs_root *root = BTRFS_I(inode)->root;
  1845. struct file *src_file;
  1846. struct inode *src;
  1847. struct btrfs_trans_handle *trans;
  1848. struct btrfs_path *path;
  1849. struct extent_buffer *leaf;
  1850. char *buf;
  1851. struct btrfs_key key;
  1852. u32 nritems;
  1853. int slot;
  1854. int ret;
  1855. u64 len = olen;
  1856. u64 bs = root->fs_info->sb->s_blocksize;
  1857. u64 hint_byte;
  1858. /*
  1859. * TODO:
  1860. * - split compressed inline extents. annoying: we need to
  1861. * decompress into destination's address_space (the file offset
  1862. * may change, so source mapping won't do), then recompress (or
  1863. * otherwise reinsert) a subrange.
  1864. * - allow ranges within the same file to be cloned (provided
  1865. * they don't overlap)?
  1866. */
  1867. /* the destination must be opened for writing */
  1868. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1869. return -EINVAL;
  1870. if (btrfs_root_readonly(root))
  1871. return -EROFS;
  1872. ret = mnt_want_write(file->f_path.mnt);
  1873. if (ret)
  1874. return ret;
  1875. src_file = fget(srcfd);
  1876. if (!src_file) {
  1877. ret = -EBADF;
  1878. goto out_drop_write;
  1879. }
  1880. src = src_file->f_dentry->d_inode;
  1881. ret = -EINVAL;
  1882. if (src == inode)
  1883. goto out_fput;
  1884. /* the src must be open for reading */
  1885. if (!(src_file->f_mode & FMODE_READ))
  1886. goto out_fput;
  1887. /* don't make the dst file partly checksummed */
  1888. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  1889. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
  1890. goto out_fput;
  1891. ret = -EISDIR;
  1892. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1893. goto out_fput;
  1894. ret = -EXDEV;
  1895. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1896. goto out_fput;
  1897. ret = -ENOMEM;
  1898. buf = vmalloc(btrfs_level_size(root, 0));
  1899. if (!buf)
  1900. goto out_fput;
  1901. path = btrfs_alloc_path();
  1902. if (!path) {
  1903. vfree(buf);
  1904. goto out_fput;
  1905. }
  1906. path->reada = 2;
  1907. if (inode < src) {
  1908. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  1909. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  1910. } else {
  1911. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  1912. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1913. }
  1914. /* determine range to clone */
  1915. ret = -EINVAL;
  1916. if (off + len > src->i_size || off + len < off)
  1917. goto out_unlock;
  1918. if (len == 0)
  1919. olen = len = src->i_size - off;
  1920. /* if we extend to eof, continue to block boundary */
  1921. if (off + len == src->i_size)
  1922. len = ALIGN(src->i_size, bs) - off;
  1923. /* verify the end result is block aligned */
  1924. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  1925. !IS_ALIGNED(destoff, bs))
  1926. goto out_unlock;
  1927. if (destoff > inode->i_size) {
  1928. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  1929. if (ret)
  1930. goto out_unlock;
  1931. }
  1932. /* truncate page cache pages from target inode range */
  1933. truncate_inode_pages_range(&inode->i_data, destoff,
  1934. PAGE_CACHE_ALIGN(destoff + len) - 1);
  1935. /* do any pending delalloc/csum calc on src, one way or
  1936. another, and lock file content */
  1937. while (1) {
  1938. struct btrfs_ordered_extent *ordered;
  1939. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1940. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  1941. if (!ordered &&
  1942. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  1943. EXTENT_DELALLOC, 0, NULL))
  1944. break;
  1945. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1946. if (ordered)
  1947. btrfs_put_ordered_extent(ordered);
  1948. btrfs_wait_ordered_range(src, off, len);
  1949. }
  1950. /* clone data */
  1951. key.objectid = btrfs_ino(src);
  1952. key.type = BTRFS_EXTENT_DATA_KEY;
  1953. key.offset = 0;
  1954. while (1) {
  1955. /*
  1956. * note the key will change type as we walk through the
  1957. * tree.
  1958. */
  1959. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1960. if (ret < 0)
  1961. goto out;
  1962. nritems = btrfs_header_nritems(path->nodes[0]);
  1963. if (path->slots[0] >= nritems) {
  1964. ret = btrfs_next_leaf(root, path);
  1965. if (ret < 0)
  1966. goto out;
  1967. if (ret > 0)
  1968. break;
  1969. nritems = btrfs_header_nritems(path->nodes[0]);
  1970. }
  1971. leaf = path->nodes[0];
  1972. slot = path->slots[0];
  1973. btrfs_item_key_to_cpu(leaf, &key, slot);
  1974. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1975. key.objectid != btrfs_ino(src))
  1976. break;
  1977. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1978. struct btrfs_file_extent_item *extent;
  1979. int type;
  1980. u32 size;
  1981. struct btrfs_key new_key;
  1982. u64 disko = 0, diskl = 0;
  1983. u64 datao = 0, datal = 0;
  1984. u8 comp;
  1985. u64 endoff;
  1986. size = btrfs_item_size_nr(leaf, slot);
  1987. read_extent_buffer(leaf, buf,
  1988. btrfs_item_ptr_offset(leaf, slot),
  1989. size);
  1990. extent = btrfs_item_ptr(leaf, slot,
  1991. struct btrfs_file_extent_item);
  1992. comp = btrfs_file_extent_compression(leaf, extent);
  1993. type = btrfs_file_extent_type(leaf, extent);
  1994. if (type == BTRFS_FILE_EXTENT_REG ||
  1995. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1996. disko = btrfs_file_extent_disk_bytenr(leaf,
  1997. extent);
  1998. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1999. extent);
  2000. datao = btrfs_file_extent_offset(leaf, extent);
  2001. datal = btrfs_file_extent_num_bytes(leaf,
  2002. extent);
  2003. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2004. /* take upper bound, may be compressed */
  2005. datal = btrfs_file_extent_ram_bytes(leaf,
  2006. extent);
  2007. }
  2008. btrfs_release_path(path);
  2009. if (key.offset + datal <= off ||
  2010. key.offset >= off+len)
  2011. goto next;
  2012. memcpy(&new_key, &key, sizeof(new_key));
  2013. new_key.objectid = btrfs_ino(inode);
  2014. if (off <= key.offset)
  2015. new_key.offset = key.offset + destoff - off;
  2016. else
  2017. new_key.offset = destoff;
  2018. /*
  2019. * 1 - adjusting old extent (we may have to split it)
  2020. * 1 - add new extent
  2021. * 1 - inode update
  2022. */
  2023. trans = btrfs_start_transaction(root, 3);
  2024. if (IS_ERR(trans)) {
  2025. ret = PTR_ERR(trans);
  2026. goto out;
  2027. }
  2028. if (type == BTRFS_FILE_EXTENT_REG ||
  2029. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2030. /*
  2031. * a | --- range to clone ---| b
  2032. * | ------------- extent ------------- |
  2033. */
  2034. /* substract range b */
  2035. if (key.offset + datal > off + len)
  2036. datal = off + len - key.offset;
  2037. /* substract range a */
  2038. if (off > key.offset) {
  2039. datao += off - key.offset;
  2040. datal -= off - key.offset;
  2041. }
  2042. ret = btrfs_drop_extents(trans, inode,
  2043. new_key.offset,
  2044. new_key.offset + datal,
  2045. &hint_byte, 1);
  2046. BUG_ON(ret);
  2047. ret = btrfs_insert_empty_item(trans, root, path,
  2048. &new_key, size);
  2049. BUG_ON(ret);
  2050. leaf = path->nodes[0];
  2051. slot = path->slots[0];
  2052. write_extent_buffer(leaf, buf,
  2053. btrfs_item_ptr_offset(leaf, slot),
  2054. size);
  2055. extent = btrfs_item_ptr(leaf, slot,
  2056. struct btrfs_file_extent_item);
  2057. /* disko == 0 means it's a hole */
  2058. if (!disko)
  2059. datao = 0;
  2060. btrfs_set_file_extent_offset(leaf, extent,
  2061. datao);
  2062. btrfs_set_file_extent_num_bytes(leaf, extent,
  2063. datal);
  2064. if (disko) {
  2065. inode_add_bytes(inode, datal);
  2066. ret = btrfs_inc_extent_ref(trans, root,
  2067. disko, diskl, 0,
  2068. root->root_key.objectid,
  2069. btrfs_ino(inode),
  2070. new_key.offset - datao);
  2071. BUG_ON(ret);
  2072. }
  2073. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2074. u64 skip = 0;
  2075. u64 trim = 0;
  2076. if (off > key.offset) {
  2077. skip = off - key.offset;
  2078. new_key.offset += skip;
  2079. }
  2080. if (key.offset + datal > off+len)
  2081. trim = key.offset + datal - (off+len);
  2082. if (comp && (skip || trim)) {
  2083. ret = -EINVAL;
  2084. btrfs_end_transaction(trans, root);
  2085. goto out;
  2086. }
  2087. size -= skip + trim;
  2088. datal -= skip + trim;
  2089. ret = btrfs_drop_extents(trans, inode,
  2090. new_key.offset,
  2091. new_key.offset + datal,
  2092. &hint_byte, 1);
  2093. BUG_ON(ret);
  2094. ret = btrfs_insert_empty_item(trans, root, path,
  2095. &new_key, size);
  2096. BUG_ON(ret);
  2097. if (skip) {
  2098. u32 start =
  2099. btrfs_file_extent_calc_inline_size(0);
  2100. memmove(buf+start, buf+start+skip,
  2101. datal);
  2102. }
  2103. leaf = path->nodes[0];
  2104. slot = path->slots[0];
  2105. write_extent_buffer(leaf, buf,
  2106. btrfs_item_ptr_offset(leaf, slot),
  2107. size);
  2108. inode_add_bytes(inode, datal);
  2109. }
  2110. btrfs_mark_buffer_dirty(leaf);
  2111. btrfs_release_path(path);
  2112. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2113. /*
  2114. * we round up to the block size at eof when
  2115. * determining which extents to clone above,
  2116. * but shouldn't round up the file size
  2117. */
  2118. endoff = new_key.offset + datal;
  2119. if (endoff > destoff+olen)
  2120. endoff = destoff+olen;
  2121. if (endoff > inode->i_size)
  2122. btrfs_i_size_write(inode, endoff);
  2123. ret = btrfs_update_inode(trans, root, inode);
  2124. BUG_ON(ret);
  2125. btrfs_end_transaction(trans, root);
  2126. }
  2127. next:
  2128. btrfs_release_path(path);
  2129. key.offset++;
  2130. }
  2131. ret = 0;
  2132. out:
  2133. btrfs_release_path(path);
  2134. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  2135. out_unlock:
  2136. mutex_unlock(&src->i_mutex);
  2137. mutex_unlock(&inode->i_mutex);
  2138. vfree(buf);
  2139. btrfs_free_path(path);
  2140. out_fput:
  2141. fput(src_file);
  2142. out_drop_write:
  2143. mnt_drop_write(file->f_path.mnt);
  2144. return ret;
  2145. }
  2146. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  2147. {
  2148. struct btrfs_ioctl_clone_range_args args;
  2149. if (copy_from_user(&args, argp, sizeof(args)))
  2150. return -EFAULT;
  2151. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  2152. args.src_length, args.dest_offset);
  2153. }
  2154. /*
  2155. * there are many ways the trans_start and trans_end ioctls can lead
  2156. * to deadlocks. They should only be used by applications that
  2157. * basically own the machine, and have a very in depth understanding
  2158. * of all the possible deadlocks and enospc problems.
  2159. */
  2160. static long btrfs_ioctl_trans_start(struct file *file)
  2161. {
  2162. struct inode *inode = fdentry(file)->d_inode;
  2163. struct btrfs_root *root = BTRFS_I(inode)->root;
  2164. struct btrfs_trans_handle *trans;
  2165. int ret;
  2166. ret = -EPERM;
  2167. if (!capable(CAP_SYS_ADMIN))
  2168. goto out;
  2169. ret = -EINPROGRESS;
  2170. if (file->private_data)
  2171. goto out;
  2172. ret = -EROFS;
  2173. if (btrfs_root_readonly(root))
  2174. goto out;
  2175. ret = mnt_want_write(file->f_path.mnt);
  2176. if (ret)
  2177. goto out;
  2178. atomic_inc(&root->fs_info->open_ioctl_trans);
  2179. ret = -ENOMEM;
  2180. trans = btrfs_start_ioctl_transaction(root);
  2181. if (IS_ERR(trans))
  2182. goto out_drop;
  2183. file->private_data = trans;
  2184. return 0;
  2185. out_drop:
  2186. atomic_dec(&root->fs_info->open_ioctl_trans);
  2187. mnt_drop_write(file->f_path.mnt);
  2188. out:
  2189. return ret;
  2190. }
  2191. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  2192. {
  2193. struct inode *inode = fdentry(file)->d_inode;
  2194. struct btrfs_root *root = BTRFS_I(inode)->root;
  2195. struct btrfs_root *new_root;
  2196. struct btrfs_dir_item *di;
  2197. struct btrfs_trans_handle *trans;
  2198. struct btrfs_path *path;
  2199. struct btrfs_key location;
  2200. struct btrfs_disk_key disk_key;
  2201. struct btrfs_super_block *disk_super;
  2202. u64 features;
  2203. u64 objectid = 0;
  2204. u64 dir_id;
  2205. if (!capable(CAP_SYS_ADMIN))
  2206. return -EPERM;
  2207. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  2208. return -EFAULT;
  2209. if (!objectid)
  2210. objectid = root->root_key.objectid;
  2211. location.objectid = objectid;
  2212. location.type = BTRFS_ROOT_ITEM_KEY;
  2213. location.offset = (u64)-1;
  2214. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  2215. if (IS_ERR(new_root))
  2216. return PTR_ERR(new_root);
  2217. if (btrfs_root_refs(&new_root->root_item) == 0)
  2218. return -ENOENT;
  2219. path = btrfs_alloc_path();
  2220. if (!path)
  2221. return -ENOMEM;
  2222. path->leave_spinning = 1;
  2223. trans = btrfs_start_transaction(root, 1);
  2224. if (IS_ERR(trans)) {
  2225. btrfs_free_path(path);
  2226. return PTR_ERR(trans);
  2227. }
  2228. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  2229. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  2230. dir_id, "default", 7, 1);
  2231. if (IS_ERR_OR_NULL(di)) {
  2232. btrfs_free_path(path);
  2233. btrfs_end_transaction(trans, root);
  2234. printk(KERN_ERR "Umm, you don't have the default dir item, "
  2235. "this isn't going to work\n");
  2236. return -ENOENT;
  2237. }
  2238. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  2239. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  2240. btrfs_mark_buffer_dirty(path->nodes[0]);
  2241. btrfs_free_path(path);
  2242. disk_super = &root->fs_info->super_copy;
  2243. features = btrfs_super_incompat_flags(disk_super);
  2244. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  2245. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  2246. btrfs_set_super_incompat_flags(disk_super, features);
  2247. }
  2248. btrfs_end_transaction(trans, root);
  2249. return 0;
  2250. }
  2251. static void get_block_group_info(struct list_head *groups_list,
  2252. struct btrfs_ioctl_space_info *space)
  2253. {
  2254. struct btrfs_block_group_cache *block_group;
  2255. space->total_bytes = 0;
  2256. space->used_bytes = 0;
  2257. space->flags = 0;
  2258. list_for_each_entry(block_group, groups_list, list) {
  2259. space->flags = block_group->flags;
  2260. space->total_bytes += block_group->key.offset;
  2261. space->used_bytes +=
  2262. btrfs_block_group_used(&block_group->item);
  2263. }
  2264. }
  2265. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  2266. {
  2267. struct btrfs_ioctl_space_args space_args;
  2268. struct btrfs_ioctl_space_info space;
  2269. struct btrfs_ioctl_space_info *dest;
  2270. struct btrfs_ioctl_space_info *dest_orig;
  2271. struct btrfs_ioctl_space_info __user *user_dest;
  2272. struct btrfs_space_info *info;
  2273. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  2274. BTRFS_BLOCK_GROUP_SYSTEM,
  2275. BTRFS_BLOCK_GROUP_METADATA,
  2276. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  2277. int num_types = 4;
  2278. int alloc_size;
  2279. int ret = 0;
  2280. u64 slot_count = 0;
  2281. int i, c;
  2282. if (copy_from_user(&space_args,
  2283. (struct btrfs_ioctl_space_args __user *)arg,
  2284. sizeof(space_args)))
  2285. return -EFAULT;
  2286. for (i = 0; i < num_types; i++) {
  2287. struct btrfs_space_info *tmp;
  2288. info = NULL;
  2289. rcu_read_lock();
  2290. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2291. list) {
  2292. if (tmp->flags == types[i]) {
  2293. info = tmp;
  2294. break;
  2295. }
  2296. }
  2297. rcu_read_unlock();
  2298. if (!info)
  2299. continue;
  2300. down_read(&info->groups_sem);
  2301. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2302. if (!list_empty(&info->block_groups[c]))
  2303. slot_count++;
  2304. }
  2305. up_read(&info->groups_sem);
  2306. }
  2307. /* space_slots == 0 means they are asking for a count */
  2308. if (space_args.space_slots == 0) {
  2309. space_args.total_spaces = slot_count;
  2310. goto out;
  2311. }
  2312. slot_count = min_t(u64, space_args.space_slots, slot_count);
  2313. alloc_size = sizeof(*dest) * slot_count;
  2314. /* we generally have at most 6 or so space infos, one for each raid
  2315. * level. So, a whole page should be more than enough for everyone
  2316. */
  2317. if (alloc_size > PAGE_CACHE_SIZE)
  2318. return -ENOMEM;
  2319. space_args.total_spaces = 0;
  2320. dest = kmalloc(alloc_size, GFP_NOFS);
  2321. if (!dest)
  2322. return -ENOMEM;
  2323. dest_orig = dest;
  2324. /* now we have a buffer to copy into */
  2325. for (i = 0; i < num_types; i++) {
  2326. struct btrfs_space_info *tmp;
  2327. if (!slot_count)
  2328. break;
  2329. info = NULL;
  2330. rcu_read_lock();
  2331. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2332. list) {
  2333. if (tmp->flags == types[i]) {
  2334. info = tmp;
  2335. break;
  2336. }
  2337. }
  2338. rcu_read_unlock();
  2339. if (!info)
  2340. continue;
  2341. down_read(&info->groups_sem);
  2342. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2343. if (!list_empty(&info->block_groups[c])) {
  2344. get_block_group_info(&info->block_groups[c],
  2345. &space);
  2346. memcpy(dest, &space, sizeof(space));
  2347. dest++;
  2348. space_args.total_spaces++;
  2349. slot_count--;
  2350. }
  2351. if (!slot_count)
  2352. break;
  2353. }
  2354. up_read(&info->groups_sem);
  2355. }
  2356. user_dest = (struct btrfs_ioctl_space_info *)
  2357. (arg + sizeof(struct btrfs_ioctl_space_args));
  2358. if (copy_to_user(user_dest, dest_orig, alloc_size))
  2359. ret = -EFAULT;
  2360. kfree(dest_orig);
  2361. out:
  2362. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  2363. ret = -EFAULT;
  2364. return ret;
  2365. }
  2366. /*
  2367. * there are many ways the trans_start and trans_end ioctls can lead
  2368. * to deadlocks. They should only be used by applications that
  2369. * basically own the machine, and have a very in depth understanding
  2370. * of all the possible deadlocks and enospc problems.
  2371. */
  2372. long btrfs_ioctl_trans_end(struct file *file)
  2373. {
  2374. struct inode *inode = fdentry(file)->d_inode;
  2375. struct btrfs_root *root = BTRFS_I(inode)->root;
  2376. struct btrfs_trans_handle *trans;
  2377. trans = file->private_data;
  2378. if (!trans)
  2379. return -EINVAL;
  2380. file->private_data = NULL;
  2381. btrfs_end_transaction(trans, root);
  2382. atomic_dec(&root->fs_info->open_ioctl_trans);
  2383. mnt_drop_write(file->f_path.mnt);
  2384. return 0;
  2385. }
  2386. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  2387. {
  2388. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2389. struct btrfs_trans_handle *trans;
  2390. u64 transid;
  2391. int ret;
  2392. trans = btrfs_start_transaction(root, 0);
  2393. if (IS_ERR(trans))
  2394. return PTR_ERR(trans);
  2395. transid = trans->transid;
  2396. ret = btrfs_commit_transaction_async(trans, root, 0);
  2397. if (ret) {
  2398. btrfs_end_transaction(trans, root);
  2399. return ret;
  2400. }
  2401. if (argp)
  2402. if (copy_to_user(argp, &transid, sizeof(transid)))
  2403. return -EFAULT;
  2404. return 0;
  2405. }
  2406. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  2407. {
  2408. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2409. u64 transid;
  2410. if (argp) {
  2411. if (copy_from_user(&transid, argp, sizeof(transid)))
  2412. return -EFAULT;
  2413. } else {
  2414. transid = 0; /* current trans */
  2415. }
  2416. return btrfs_wait_for_commit(root, transid);
  2417. }
  2418. static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
  2419. {
  2420. int ret;
  2421. struct btrfs_ioctl_scrub_args *sa;
  2422. if (!capable(CAP_SYS_ADMIN))
  2423. return -EPERM;
  2424. sa = memdup_user(arg, sizeof(*sa));
  2425. if (IS_ERR(sa))
  2426. return PTR_ERR(sa);
  2427. ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
  2428. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
  2429. if (copy_to_user(arg, sa, sizeof(*sa)))
  2430. ret = -EFAULT;
  2431. kfree(sa);
  2432. return ret;
  2433. }
  2434. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  2435. {
  2436. if (!capable(CAP_SYS_ADMIN))
  2437. return -EPERM;
  2438. return btrfs_scrub_cancel(root);
  2439. }
  2440. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  2441. void __user *arg)
  2442. {
  2443. struct btrfs_ioctl_scrub_args *sa;
  2444. int ret;
  2445. if (!capable(CAP_SYS_ADMIN))
  2446. return -EPERM;
  2447. sa = memdup_user(arg, sizeof(*sa));
  2448. if (IS_ERR(sa))
  2449. return PTR_ERR(sa);
  2450. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  2451. if (copy_to_user(arg, sa, sizeof(*sa)))
  2452. ret = -EFAULT;
  2453. kfree(sa);
  2454. return ret;
  2455. }
  2456. long btrfs_ioctl(struct file *file, unsigned int
  2457. cmd, unsigned long arg)
  2458. {
  2459. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2460. void __user *argp = (void __user *)arg;
  2461. switch (cmd) {
  2462. case FS_IOC_GETFLAGS:
  2463. return btrfs_ioctl_getflags(file, argp);
  2464. case FS_IOC_SETFLAGS:
  2465. return btrfs_ioctl_setflags(file, argp);
  2466. case FS_IOC_GETVERSION:
  2467. return btrfs_ioctl_getversion(file, argp);
  2468. case FITRIM:
  2469. return btrfs_ioctl_fitrim(file, argp);
  2470. case BTRFS_IOC_SNAP_CREATE:
  2471. return btrfs_ioctl_snap_create(file, argp, 0);
  2472. case BTRFS_IOC_SNAP_CREATE_V2:
  2473. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  2474. case BTRFS_IOC_SUBVOL_CREATE:
  2475. return btrfs_ioctl_snap_create(file, argp, 1);
  2476. case BTRFS_IOC_SNAP_DESTROY:
  2477. return btrfs_ioctl_snap_destroy(file, argp);
  2478. case BTRFS_IOC_SUBVOL_GETFLAGS:
  2479. return btrfs_ioctl_subvol_getflags(file, argp);
  2480. case BTRFS_IOC_SUBVOL_SETFLAGS:
  2481. return btrfs_ioctl_subvol_setflags(file, argp);
  2482. case BTRFS_IOC_DEFAULT_SUBVOL:
  2483. return btrfs_ioctl_default_subvol(file, argp);
  2484. case BTRFS_IOC_DEFRAG:
  2485. return btrfs_ioctl_defrag(file, NULL);
  2486. case BTRFS_IOC_DEFRAG_RANGE:
  2487. return btrfs_ioctl_defrag(file, argp);
  2488. case BTRFS_IOC_RESIZE:
  2489. return btrfs_ioctl_resize(root, argp);
  2490. case BTRFS_IOC_ADD_DEV:
  2491. return btrfs_ioctl_add_dev(root, argp);
  2492. case BTRFS_IOC_RM_DEV:
  2493. return btrfs_ioctl_rm_dev(root, argp);
  2494. case BTRFS_IOC_FS_INFO:
  2495. return btrfs_ioctl_fs_info(root, argp);
  2496. case BTRFS_IOC_DEV_INFO:
  2497. return btrfs_ioctl_dev_info(root, argp);
  2498. case BTRFS_IOC_BALANCE:
  2499. return btrfs_balance(root->fs_info->dev_root);
  2500. case BTRFS_IOC_CLONE:
  2501. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  2502. case BTRFS_IOC_CLONE_RANGE:
  2503. return btrfs_ioctl_clone_range(file, argp);
  2504. case BTRFS_IOC_TRANS_START:
  2505. return btrfs_ioctl_trans_start(file);
  2506. case BTRFS_IOC_TRANS_END:
  2507. return btrfs_ioctl_trans_end(file);
  2508. case BTRFS_IOC_TREE_SEARCH:
  2509. return btrfs_ioctl_tree_search(file, argp);
  2510. case BTRFS_IOC_INO_LOOKUP:
  2511. return btrfs_ioctl_ino_lookup(file, argp);
  2512. case BTRFS_IOC_SPACE_INFO:
  2513. return btrfs_ioctl_space_info(root, argp);
  2514. case BTRFS_IOC_SYNC:
  2515. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  2516. return 0;
  2517. case BTRFS_IOC_START_SYNC:
  2518. return btrfs_ioctl_start_sync(file, argp);
  2519. case BTRFS_IOC_WAIT_SYNC:
  2520. return btrfs_ioctl_wait_sync(file, argp);
  2521. case BTRFS_IOC_SCRUB:
  2522. return btrfs_ioctl_scrub(root, argp);
  2523. case BTRFS_IOC_SCRUB_CANCEL:
  2524. return btrfs_ioctl_scrub_cancel(root, argp);
  2525. case BTRFS_IOC_SCRUB_PROGRESS:
  2526. return btrfs_ioctl_scrub_progress(root, argp);
  2527. }
  2528. return -ENOTTY;
  2529. }