ioctl.c 61 KB

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