ioctl.c 61 KB

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