ioctl.c 61 KB

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