ioctl.c 61 KB

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