ioctl.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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 "compat.h"
  42. #include "ctree.h"
  43. #include "disk-io.h"
  44. #include "transaction.h"
  45. #include "btrfs_inode.h"
  46. #include "ioctl.h"
  47. #include "print-tree.h"
  48. #include "volumes.h"
  49. #include "locking.h"
  50. /* Mask out flags that are inappropriate for the given type of inode. */
  51. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  52. {
  53. if (S_ISDIR(mode))
  54. return flags;
  55. else if (S_ISREG(mode))
  56. return flags & ~FS_DIRSYNC_FL;
  57. else
  58. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  59. }
  60. /*
  61. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  62. */
  63. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  64. {
  65. unsigned int iflags = 0;
  66. if (flags & BTRFS_INODE_SYNC)
  67. iflags |= FS_SYNC_FL;
  68. if (flags & BTRFS_INODE_IMMUTABLE)
  69. iflags |= FS_IMMUTABLE_FL;
  70. if (flags & BTRFS_INODE_APPEND)
  71. iflags |= FS_APPEND_FL;
  72. if (flags & BTRFS_INODE_NODUMP)
  73. iflags |= FS_NODUMP_FL;
  74. if (flags & BTRFS_INODE_NOATIME)
  75. iflags |= FS_NOATIME_FL;
  76. if (flags & BTRFS_INODE_DIRSYNC)
  77. iflags |= FS_DIRSYNC_FL;
  78. return iflags;
  79. }
  80. /*
  81. * Update inode->i_flags based on the btrfs internal flags.
  82. */
  83. void btrfs_update_iflags(struct inode *inode)
  84. {
  85. struct btrfs_inode *ip = BTRFS_I(inode);
  86. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  87. if (ip->flags & BTRFS_INODE_SYNC)
  88. inode->i_flags |= S_SYNC;
  89. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  90. inode->i_flags |= S_IMMUTABLE;
  91. if (ip->flags & BTRFS_INODE_APPEND)
  92. inode->i_flags |= S_APPEND;
  93. if (ip->flags & BTRFS_INODE_NOATIME)
  94. inode->i_flags |= S_NOATIME;
  95. if (ip->flags & BTRFS_INODE_DIRSYNC)
  96. inode->i_flags |= S_DIRSYNC;
  97. }
  98. /*
  99. * Inherit flags from the parent inode.
  100. *
  101. * Unlike extN we don't have any flags we don't want to inherit currently.
  102. */
  103. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  104. {
  105. unsigned int flags;
  106. if (!dir)
  107. return;
  108. flags = BTRFS_I(dir)->flags;
  109. if (S_ISREG(inode->i_mode))
  110. flags &= ~BTRFS_INODE_DIRSYNC;
  111. else if (!S_ISDIR(inode->i_mode))
  112. flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
  113. BTRFS_I(inode)->flags = flags;
  114. btrfs_update_iflags(inode);
  115. }
  116. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  117. {
  118. struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
  119. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  120. if (copy_to_user(arg, &flags, sizeof(flags)))
  121. return -EFAULT;
  122. return 0;
  123. }
  124. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  125. {
  126. struct inode *inode = file->f_path.dentry->d_inode;
  127. struct btrfs_inode *ip = BTRFS_I(inode);
  128. struct btrfs_root *root = ip->root;
  129. struct btrfs_trans_handle *trans;
  130. unsigned int flags, oldflags;
  131. int ret;
  132. if (copy_from_user(&flags, arg, sizeof(flags)))
  133. return -EFAULT;
  134. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  135. FS_NOATIME_FL | FS_NODUMP_FL | \
  136. FS_SYNC_FL | FS_DIRSYNC_FL))
  137. return -EOPNOTSUPP;
  138. if (!is_owner_or_cap(inode))
  139. return -EACCES;
  140. mutex_lock(&inode->i_mutex);
  141. flags = btrfs_mask_flags(inode->i_mode, flags);
  142. oldflags = btrfs_flags_to_ioctl(ip->flags);
  143. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  144. if (!capable(CAP_LINUX_IMMUTABLE)) {
  145. ret = -EPERM;
  146. goto out_unlock;
  147. }
  148. }
  149. ret = mnt_want_write(file->f_path.mnt);
  150. if (ret)
  151. goto out_unlock;
  152. if (flags & FS_SYNC_FL)
  153. ip->flags |= BTRFS_INODE_SYNC;
  154. else
  155. ip->flags &= ~BTRFS_INODE_SYNC;
  156. if (flags & FS_IMMUTABLE_FL)
  157. ip->flags |= BTRFS_INODE_IMMUTABLE;
  158. else
  159. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  160. if (flags & FS_APPEND_FL)
  161. ip->flags |= BTRFS_INODE_APPEND;
  162. else
  163. ip->flags &= ~BTRFS_INODE_APPEND;
  164. if (flags & FS_NODUMP_FL)
  165. ip->flags |= BTRFS_INODE_NODUMP;
  166. else
  167. ip->flags &= ~BTRFS_INODE_NODUMP;
  168. if (flags & FS_NOATIME_FL)
  169. ip->flags |= BTRFS_INODE_NOATIME;
  170. else
  171. ip->flags &= ~BTRFS_INODE_NOATIME;
  172. if (flags & FS_DIRSYNC_FL)
  173. ip->flags |= BTRFS_INODE_DIRSYNC;
  174. else
  175. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  176. trans = btrfs_join_transaction(root, 1);
  177. BUG_ON(!trans);
  178. ret = btrfs_update_inode(trans, root, inode);
  179. BUG_ON(ret);
  180. btrfs_update_iflags(inode);
  181. inode->i_ctime = CURRENT_TIME;
  182. btrfs_end_transaction(trans, root);
  183. mnt_drop_write(file->f_path.mnt);
  184. out_unlock:
  185. mutex_unlock(&inode->i_mutex);
  186. return 0;
  187. }
  188. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  189. {
  190. struct inode *inode = file->f_path.dentry->d_inode;
  191. return put_user(inode->i_generation, arg);
  192. }
  193. static noinline int create_subvol(struct btrfs_root *root,
  194. struct dentry *dentry,
  195. char *name, int namelen)
  196. {
  197. struct btrfs_trans_handle *trans;
  198. struct btrfs_key key;
  199. struct btrfs_root_item root_item;
  200. struct btrfs_inode_item *inode_item;
  201. struct extent_buffer *leaf;
  202. struct btrfs_root *new_root = root;
  203. struct inode *dir;
  204. int ret;
  205. int err;
  206. u64 objectid;
  207. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  208. u64 index = 0;
  209. unsigned long nr = 1;
  210. ret = btrfs_check_metadata_free_space(root);
  211. if (ret)
  212. goto fail_commit;
  213. trans = btrfs_start_transaction(root, 1);
  214. BUG_ON(!trans);
  215. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  216. 0, &objectid);
  217. if (ret)
  218. goto fail;
  219. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  220. 0, objectid, NULL, 0, 0, 0);
  221. if (IS_ERR(leaf)) {
  222. ret = PTR_ERR(leaf);
  223. goto fail;
  224. }
  225. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  226. btrfs_set_header_bytenr(leaf, leaf->start);
  227. btrfs_set_header_generation(leaf, trans->transid);
  228. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  229. btrfs_set_header_owner(leaf, objectid);
  230. write_extent_buffer(leaf, root->fs_info->fsid,
  231. (unsigned long)btrfs_header_fsid(leaf),
  232. BTRFS_FSID_SIZE);
  233. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  234. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  235. BTRFS_UUID_SIZE);
  236. btrfs_mark_buffer_dirty(leaf);
  237. inode_item = &root_item.inode;
  238. memset(inode_item, 0, sizeof(*inode_item));
  239. inode_item->generation = cpu_to_le64(1);
  240. inode_item->size = cpu_to_le64(3);
  241. inode_item->nlink = cpu_to_le32(1);
  242. inode_item->nbytes = cpu_to_le64(root->leafsize);
  243. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  244. btrfs_set_root_bytenr(&root_item, leaf->start);
  245. btrfs_set_root_generation(&root_item, trans->transid);
  246. btrfs_set_root_level(&root_item, 0);
  247. btrfs_set_root_refs(&root_item, 1);
  248. btrfs_set_root_used(&root_item, 0);
  249. btrfs_set_root_last_snapshot(&root_item, 0);
  250. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  251. root_item.drop_level = 0;
  252. btrfs_tree_unlock(leaf);
  253. free_extent_buffer(leaf);
  254. leaf = NULL;
  255. btrfs_set_root_dirid(&root_item, new_dirid);
  256. key.objectid = objectid;
  257. key.offset = 0;
  258. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  259. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  260. &root_item);
  261. if (ret)
  262. goto fail;
  263. /*
  264. * insert the directory item
  265. */
  266. key.offset = (u64)-1;
  267. dir = dentry->d_parent->d_inode;
  268. ret = btrfs_set_inode_index(dir, &index);
  269. BUG_ON(ret);
  270. ret = btrfs_insert_dir_item(trans, root,
  271. name, namelen, dir->i_ino, &key,
  272. BTRFS_FT_DIR, index);
  273. if (ret)
  274. goto fail;
  275. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  276. ret = btrfs_update_inode(trans, root, dir);
  277. BUG_ON(ret);
  278. /* add the backref first */
  279. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  280. objectid, BTRFS_ROOT_BACKREF_KEY,
  281. root->root_key.objectid,
  282. dir->i_ino, index, name, namelen);
  283. BUG_ON(ret);
  284. /* now add the forward ref */
  285. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  286. root->root_key.objectid, BTRFS_ROOT_REF_KEY,
  287. objectid,
  288. dir->i_ino, index, name, namelen);
  289. BUG_ON(ret);
  290. ret = btrfs_commit_transaction(trans, root);
  291. if (ret)
  292. goto fail_commit;
  293. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  294. BUG_ON(!new_root);
  295. trans = btrfs_start_transaction(new_root, 1);
  296. BUG_ON(!trans);
  297. ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
  298. BTRFS_I(dir)->block_group);
  299. if (ret)
  300. goto fail;
  301. fail:
  302. nr = trans->blocks_used;
  303. err = btrfs_commit_transaction(trans, new_root);
  304. if (err && !ret)
  305. ret = err;
  306. fail_commit:
  307. btrfs_btree_balance_dirty(root, nr);
  308. return ret;
  309. }
  310. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  311. char *name, int namelen)
  312. {
  313. struct btrfs_pending_snapshot *pending_snapshot;
  314. struct btrfs_trans_handle *trans;
  315. int ret = 0;
  316. int err;
  317. unsigned long nr = 0;
  318. if (!root->ref_cows)
  319. return -EINVAL;
  320. ret = btrfs_check_metadata_free_space(root);
  321. if (ret)
  322. goto fail_unlock;
  323. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  324. if (!pending_snapshot) {
  325. ret = -ENOMEM;
  326. goto fail_unlock;
  327. }
  328. pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
  329. if (!pending_snapshot->name) {
  330. ret = -ENOMEM;
  331. kfree(pending_snapshot);
  332. goto fail_unlock;
  333. }
  334. memcpy(pending_snapshot->name, name, namelen);
  335. pending_snapshot->name[namelen] = '\0';
  336. pending_snapshot->dentry = dentry;
  337. trans = btrfs_start_transaction(root, 1);
  338. BUG_ON(!trans);
  339. pending_snapshot->root = root;
  340. list_add(&pending_snapshot->list,
  341. &trans->transaction->pending_snapshots);
  342. err = btrfs_commit_transaction(trans, root);
  343. fail_unlock:
  344. btrfs_btree_balance_dirty(root, nr);
  345. return ret;
  346. }
  347. /* copy of may_create in fs/namei.c() */
  348. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  349. {
  350. if (child->d_inode)
  351. return -EEXIST;
  352. if (IS_DEADDIR(dir))
  353. return -ENOENT;
  354. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  355. }
  356. /*
  357. * Create a new subvolume below @parent. This is largely modeled after
  358. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  359. * inside this filesystem so it's quite a bit simpler.
  360. */
  361. static noinline int btrfs_mksubvol(struct path *parent, char *name,
  362. int mode, int namelen,
  363. struct btrfs_root *snap_src)
  364. {
  365. struct dentry *dentry;
  366. int error;
  367. mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  368. dentry = lookup_one_len(name, parent->dentry, namelen);
  369. error = PTR_ERR(dentry);
  370. if (IS_ERR(dentry))
  371. goto out_unlock;
  372. error = -EEXIST;
  373. if (dentry->d_inode)
  374. goto out_dput;
  375. if (!IS_POSIXACL(parent->dentry->d_inode))
  376. mode &= ~current_umask();
  377. error = mnt_want_write(parent->mnt);
  378. if (error)
  379. goto out_dput;
  380. error = btrfs_may_create(parent->dentry->d_inode, dentry);
  381. if (error)
  382. goto out_drop_write;
  383. /*
  384. * Actually perform the low-level subvolume creation after all
  385. * this VFS fuzz.
  386. *
  387. * Eventually we want to pass in an inode under which we create this
  388. * subvolume, but for now all are under the filesystem root.
  389. *
  390. * Also we should pass on the mode eventually to allow creating new
  391. * subvolume with specific mode bits.
  392. */
  393. if (snap_src) {
  394. struct dentry *dir = dentry->d_parent;
  395. struct dentry *test = dir->d_parent;
  396. struct btrfs_path *path = btrfs_alloc_path();
  397. int ret;
  398. u64 test_oid;
  399. u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
  400. test_oid = snap_src->root_key.objectid;
  401. ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
  402. path, parent_oid, test_oid);
  403. if (ret == 0)
  404. goto create;
  405. btrfs_release_path(snap_src->fs_info->tree_root, path);
  406. /* we need to make sure we aren't creating a directory loop
  407. * by taking a snapshot of something that has our current
  408. * subvol in its directory tree. So, this loops through
  409. * the dentries and checks the forward refs for each subvolume
  410. * to see if is references the subvolume where we are
  411. * placing this new snapshot.
  412. */
  413. while (1) {
  414. if (!test ||
  415. dir == snap_src->fs_info->sb->s_root ||
  416. test == snap_src->fs_info->sb->s_root ||
  417. test->d_inode->i_sb != snap_src->fs_info->sb) {
  418. break;
  419. }
  420. if (S_ISLNK(test->d_inode->i_mode)) {
  421. printk(KERN_INFO "Btrfs symlink in snapshot "
  422. "path, failed\n");
  423. error = -EMLINK;
  424. btrfs_free_path(path);
  425. goto out_drop_write;
  426. }
  427. test_oid =
  428. BTRFS_I(test->d_inode)->root->root_key.objectid;
  429. ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
  430. path, test_oid, parent_oid);
  431. if (ret == 0) {
  432. printk(KERN_INFO "Btrfs snapshot creation "
  433. "failed, looping\n");
  434. error = -EMLINK;
  435. btrfs_free_path(path);
  436. goto out_drop_write;
  437. }
  438. btrfs_release_path(snap_src->fs_info->tree_root, path);
  439. test = test->d_parent;
  440. }
  441. create:
  442. btrfs_free_path(path);
  443. error = create_snapshot(snap_src, dentry, name, namelen);
  444. } else {
  445. error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
  446. dentry, name, namelen);
  447. }
  448. if (error)
  449. goto out_drop_write;
  450. fsnotify_mkdir(parent->dentry->d_inode, dentry);
  451. out_drop_write:
  452. mnt_drop_write(parent->mnt);
  453. out_dput:
  454. dput(dentry);
  455. out_unlock:
  456. mutex_unlock(&parent->dentry->d_inode->i_mutex);
  457. return error;
  458. }
  459. static int btrfs_defrag_file(struct file *file)
  460. {
  461. struct inode *inode = fdentry(file)->d_inode;
  462. struct btrfs_root *root = BTRFS_I(inode)->root;
  463. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  464. struct btrfs_ordered_extent *ordered;
  465. struct page *page;
  466. unsigned long last_index;
  467. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  468. unsigned long total_read = 0;
  469. u64 page_start;
  470. u64 page_end;
  471. unsigned long i;
  472. int ret;
  473. ret = btrfs_check_data_free_space(root, inode, inode->i_size);
  474. if (ret)
  475. return -ENOSPC;
  476. mutex_lock(&inode->i_mutex);
  477. last_index = inode->i_size >> PAGE_CACHE_SHIFT;
  478. for (i = 0; i <= last_index; i++) {
  479. if (total_read % ra_pages == 0) {
  480. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  481. min(last_index, i + ra_pages - 1));
  482. }
  483. total_read++;
  484. again:
  485. page = grab_cache_page(inode->i_mapping, i);
  486. if (!page)
  487. goto out_unlock;
  488. if (!PageUptodate(page)) {
  489. btrfs_readpage(NULL, page);
  490. lock_page(page);
  491. if (!PageUptodate(page)) {
  492. unlock_page(page);
  493. page_cache_release(page);
  494. goto out_unlock;
  495. }
  496. }
  497. wait_on_page_writeback(page);
  498. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  499. page_end = page_start + PAGE_CACHE_SIZE - 1;
  500. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  501. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  502. if (ordered) {
  503. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  504. unlock_page(page);
  505. page_cache_release(page);
  506. btrfs_start_ordered_extent(inode, ordered, 1);
  507. btrfs_put_ordered_extent(ordered);
  508. goto again;
  509. }
  510. set_page_extent_mapped(page);
  511. /*
  512. * this makes sure page_mkwrite is called on the
  513. * page if it is dirtied again later
  514. */
  515. clear_page_dirty_for_io(page);
  516. btrfs_set_extent_delalloc(inode, page_start, page_end);
  517. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  518. set_page_dirty(page);
  519. unlock_page(page);
  520. page_cache_release(page);
  521. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  522. }
  523. out_unlock:
  524. mutex_unlock(&inode->i_mutex);
  525. return 0;
  526. }
  527. static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
  528. {
  529. u64 new_size;
  530. u64 old_size;
  531. u64 devid = 1;
  532. struct btrfs_ioctl_vol_args *vol_args;
  533. struct btrfs_trans_handle *trans;
  534. struct btrfs_device *device = NULL;
  535. char *sizestr;
  536. char *devstr = NULL;
  537. int ret = 0;
  538. int namelen;
  539. int mod = 0;
  540. if (root->fs_info->sb->s_flags & MS_RDONLY)
  541. return -EROFS;
  542. if (!capable(CAP_SYS_ADMIN))
  543. return -EPERM;
  544. vol_args = memdup_user(arg, sizeof(*vol_args));
  545. if (IS_ERR(vol_args))
  546. return PTR_ERR(vol_args);
  547. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  548. namelen = strlen(vol_args->name);
  549. mutex_lock(&root->fs_info->volume_mutex);
  550. sizestr = vol_args->name;
  551. devstr = strchr(sizestr, ':');
  552. if (devstr) {
  553. char *end;
  554. sizestr = devstr + 1;
  555. *devstr = '\0';
  556. devstr = vol_args->name;
  557. devid = simple_strtoull(devstr, &end, 10);
  558. printk(KERN_INFO "resizing devid %llu\n",
  559. (unsigned long long)devid);
  560. }
  561. device = btrfs_find_device(root, devid, NULL, NULL);
  562. if (!device) {
  563. printk(KERN_INFO "resizer unable to find device %llu\n",
  564. (unsigned long long)devid);
  565. ret = -EINVAL;
  566. goto out_unlock;
  567. }
  568. if (!strcmp(sizestr, "max"))
  569. new_size = device->bdev->bd_inode->i_size;
  570. else {
  571. if (sizestr[0] == '-') {
  572. mod = -1;
  573. sizestr++;
  574. } else if (sizestr[0] == '+') {
  575. mod = 1;
  576. sizestr++;
  577. }
  578. new_size = btrfs_parse_size(sizestr);
  579. if (new_size == 0) {
  580. ret = -EINVAL;
  581. goto out_unlock;
  582. }
  583. }
  584. old_size = device->total_bytes;
  585. if (mod < 0) {
  586. if (new_size > old_size) {
  587. ret = -EINVAL;
  588. goto out_unlock;
  589. }
  590. new_size = old_size - new_size;
  591. } else if (mod > 0) {
  592. new_size = old_size + new_size;
  593. }
  594. if (new_size < 256 * 1024 * 1024) {
  595. ret = -EINVAL;
  596. goto out_unlock;
  597. }
  598. if (new_size > device->bdev->bd_inode->i_size) {
  599. ret = -EFBIG;
  600. goto out_unlock;
  601. }
  602. do_div(new_size, root->sectorsize);
  603. new_size *= root->sectorsize;
  604. printk(KERN_INFO "new size for %s is %llu\n",
  605. device->name, (unsigned long long)new_size);
  606. if (new_size > old_size) {
  607. trans = btrfs_start_transaction(root, 1);
  608. ret = btrfs_grow_device(trans, device, new_size);
  609. btrfs_commit_transaction(trans, root);
  610. } else {
  611. ret = btrfs_shrink_device(device, new_size);
  612. }
  613. out_unlock:
  614. mutex_unlock(&root->fs_info->volume_mutex);
  615. kfree(vol_args);
  616. return ret;
  617. }
  618. static noinline int btrfs_ioctl_snap_create(struct file *file,
  619. void __user *arg, int subvol)
  620. {
  621. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  622. struct btrfs_ioctl_vol_args *vol_args;
  623. struct btrfs_dir_item *di;
  624. struct btrfs_path *path;
  625. struct file *src_file;
  626. u64 root_dirid;
  627. int namelen;
  628. int ret = 0;
  629. if (root->fs_info->sb->s_flags & MS_RDONLY)
  630. return -EROFS;
  631. vol_args = memdup_user(arg, sizeof(*vol_args));
  632. if (IS_ERR(vol_args))
  633. return PTR_ERR(vol_args);
  634. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  635. namelen = strlen(vol_args->name);
  636. if (strchr(vol_args->name, '/')) {
  637. ret = -EINVAL;
  638. goto out;
  639. }
  640. path = btrfs_alloc_path();
  641. if (!path) {
  642. ret = -ENOMEM;
  643. goto out;
  644. }
  645. root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
  646. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
  647. path, root_dirid,
  648. vol_args->name, namelen, 0);
  649. btrfs_free_path(path);
  650. if (di && !IS_ERR(di)) {
  651. ret = -EEXIST;
  652. goto out;
  653. }
  654. if (IS_ERR(di)) {
  655. ret = PTR_ERR(di);
  656. goto out;
  657. }
  658. if (subvol) {
  659. ret = btrfs_mksubvol(&file->f_path, vol_args->name,
  660. file->f_path.dentry->d_inode->i_mode,
  661. namelen, NULL);
  662. } else {
  663. struct inode *src_inode;
  664. src_file = fget(vol_args->fd);
  665. if (!src_file) {
  666. ret = -EINVAL;
  667. goto out;
  668. }
  669. src_inode = src_file->f_path.dentry->d_inode;
  670. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  671. printk(KERN_INFO "btrfs: Snapshot src from "
  672. "another FS\n");
  673. ret = -EINVAL;
  674. fput(src_file);
  675. goto out;
  676. }
  677. ret = btrfs_mksubvol(&file->f_path, vol_args->name,
  678. file->f_path.dentry->d_inode->i_mode,
  679. namelen, BTRFS_I(src_inode)->root);
  680. fput(src_file);
  681. }
  682. out:
  683. kfree(vol_args);
  684. return ret;
  685. }
  686. static int btrfs_ioctl_defrag(struct file *file)
  687. {
  688. struct inode *inode = fdentry(file)->d_inode;
  689. struct btrfs_root *root = BTRFS_I(inode)->root;
  690. int ret;
  691. ret = mnt_want_write(file->f_path.mnt);
  692. if (ret)
  693. return ret;
  694. switch (inode->i_mode & S_IFMT) {
  695. case S_IFDIR:
  696. if (!capable(CAP_SYS_ADMIN)) {
  697. ret = -EPERM;
  698. goto out;
  699. }
  700. btrfs_defrag_root(root, 0);
  701. btrfs_defrag_root(root->fs_info->extent_root, 0);
  702. break;
  703. case S_IFREG:
  704. if (!(file->f_mode & FMODE_WRITE)) {
  705. ret = -EINVAL;
  706. goto out;
  707. }
  708. btrfs_defrag_file(file);
  709. break;
  710. }
  711. out:
  712. mnt_drop_write(file->f_path.mnt);
  713. return ret;
  714. }
  715. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  716. {
  717. struct btrfs_ioctl_vol_args *vol_args;
  718. int ret;
  719. if (!capable(CAP_SYS_ADMIN))
  720. return -EPERM;
  721. vol_args = memdup_user(arg, sizeof(*vol_args));
  722. if (IS_ERR(vol_args))
  723. return PTR_ERR(vol_args);
  724. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  725. ret = btrfs_init_new_device(root, vol_args->name);
  726. kfree(vol_args);
  727. return ret;
  728. }
  729. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  730. {
  731. struct btrfs_ioctl_vol_args *vol_args;
  732. int ret;
  733. if (!capable(CAP_SYS_ADMIN))
  734. return -EPERM;
  735. if (root->fs_info->sb->s_flags & MS_RDONLY)
  736. return -EROFS;
  737. vol_args = memdup_user(arg, sizeof(*vol_args));
  738. if (IS_ERR(vol_args))
  739. return PTR_ERR(vol_args);
  740. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  741. ret = btrfs_rm_device(root, vol_args->name);
  742. kfree(vol_args);
  743. return ret;
  744. }
  745. static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  746. u64 off, u64 olen, u64 destoff)
  747. {
  748. struct inode *inode = fdentry(file)->d_inode;
  749. struct btrfs_root *root = BTRFS_I(inode)->root;
  750. struct file *src_file;
  751. struct inode *src;
  752. struct btrfs_trans_handle *trans;
  753. struct btrfs_path *path;
  754. struct extent_buffer *leaf;
  755. char *buf;
  756. struct btrfs_key key;
  757. u32 nritems;
  758. int slot;
  759. int ret;
  760. u64 len = olen;
  761. u64 bs = root->fs_info->sb->s_blocksize;
  762. u64 hint_byte;
  763. /*
  764. * TODO:
  765. * - split compressed inline extents. annoying: we need to
  766. * decompress into destination's address_space (the file offset
  767. * may change, so source mapping won't do), then recompress (or
  768. * otherwise reinsert) a subrange.
  769. * - allow ranges within the same file to be cloned (provided
  770. * they don't overlap)?
  771. */
  772. /* the destination must be opened for writing */
  773. if (!(file->f_mode & FMODE_WRITE))
  774. return -EINVAL;
  775. ret = mnt_want_write(file->f_path.mnt);
  776. if (ret)
  777. return ret;
  778. src_file = fget(srcfd);
  779. if (!src_file) {
  780. ret = -EBADF;
  781. goto out_drop_write;
  782. }
  783. src = src_file->f_dentry->d_inode;
  784. ret = -EINVAL;
  785. if (src == inode)
  786. goto out_fput;
  787. ret = -EISDIR;
  788. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  789. goto out_fput;
  790. ret = -EXDEV;
  791. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  792. goto out_fput;
  793. ret = -ENOMEM;
  794. buf = vmalloc(btrfs_level_size(root, 0));
  795. if (!buf)
  796. goto out_fput;
  797. path = btrfs_alloc_path();
  798. if (!path) {
  799. vfree(buf);
  800. goto out_fput;
  801. }
  802. path->reada = 2;
  803. if (inode < src) {
  804. mutex_lock(&inode->i_mutex);
  805. mutex_lock(&src->i_mutex);
  806. } else {
  807. mutex_lock(&src->i_mutex);
  808. mutex_lock(&inode->i_mutex);
  809. }
  810. /* determine range to clone */
  811. ret = -EINVAL;
  812. if (off >= src->i_size || off + len > src->i_size)
  813. goto out_unlock;
  814. if (len == 0)
  815. olen = len = src->i_size - off;
  816. /* if we extend to eof, continue to block boundary */
  817. if (off + len == src->i_size)
  818. len = ((src->i_size + bs-1) & ~(bs-1))
  819. - off;
  820. /* verify the end result is block aligned */
  821. if ((off & (bs-1)) ||
  822. ((off + len) & (bs-1)))
  823. goto out_unlock;
  824. /* do any pending delalloc/csum calc on src, one way or
  825. another, and lock file content */
  826. while (1) {
  827. struct btrfs_ordered_extent *ordered;
  828. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  829. ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
  830. if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
  831. break;
  832. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  833. if (ordered)
  834. btrfs_put_ordered_extent(ordered);
  835. btrfs_wait_ordered_range(src, off, off+len);
  836. }
  837. trans = btrfs_start_transaction(root, 1);
  838. BUG_ON(!trans);
  839. /* punch hole in destination first */
  840. btrfs_drop_extents(trans, root, inode, off, off + len,
  841. off + len, 0, &hint_byte);
  842. /* clone data */
  843. key.objectid = src->i_ino;
  844. key.type = BTRFS_EXTENT_DATA_KEY;
  845. key.offset = 0;
  846. while (1) {
  847. /*
  848. * note the key will change type as we walk through the
  849. * tree.
  850. */
  851. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  852. if (ret < 0)
  853. goto out;
  854. nritems = btrfs_header_nritems(path->nodes[0]);
  855. if (path->slots[0] >= nritems) {
  856. ret = btrfs_next_leaf(root, path);
  857. if (ret < 0)
  858. goto out;
  859. if (ret > 0)
  860. break;
  861. nritems = btrfs_header_nritems(path->nodes[0]);
  862. }
  863. leaf = path->nodes[0];
  864. slot = path->slots[0];
  865. btrfs_item_key_to_cpu(leaf, &key, slot);
  866. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  867. key.objectid != src->i_ino)
  868. break;
  869. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  870. struct btrfs_file_extent_item *extent;
  871. int type;
  872. u32 size;
  873. struct btrfs_key new_key;
  874. u64 disko = 0, diskl = 0;
  875. u64 datao = 0, datal = 0;
  876. u8 comp;
  877. size = btrfs_item_size_nr(leaf, slot);
  878. read_extent_buffer(leaf, buf,
  879. btrfs_item_ptr_offset(leaf, slot),
  880. size);
  881. extent = btrfs_item_ptr(leaf, slot,
  882. struct btrfs_file_extent_item);
  883. comp = btrfs_file_extent_compression(leaf, extent);
  884. type = btrfs_file_extent_type(leaf, extent);
  885. if (type == BTRFS_FILE_EXTENT_REG ||
  886. type == BTRFS_FILE_EXTENT_PREALLOC) {
  887. disko = btrfs_file_extent_disk_bytenr(leaf,
  888. extent);
  889. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  890. extent);
  891. datao = btrfs_file_extent_offset(leaf, extent);
  892. datal = btrfs_file_extent_num_bytes(leaf,
  893. extent);
  894. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  895. /* take upper bound, may be compressed */
  896. datal = btrfs_file_extent_ram_bytes(leaf,
  897. extent);
  898. }
  899. btrfs_release_path(root, path);
  900. if (key.offset + datal < off ||
  901. key.offset >= off+len)
  902. goto next;
  903. memcpy(&new_key, &key, sizeof(new_key));
  904. new_key.objectid = inode->i_ino;
  905. new_key.offset = key.offset + destoff - off;
  906. if (type == BTRFS_FILE_EXTENT_REG ||
  907. type == BTRFS_FILE_EXTENT_PREALLOC) {
  908. ret = btrfs_insert_empty_item(trans, root, path,
  909. &new_key, size);
  910. if (ret)
  911. goto out;
  912. leaf = path->nodes[0];
  913. slot = path->slots[0];
  914. write_extent_buffer(leaf, buf,
  915. btrfs_item_ptr_offset(leaf, slot),
  916. size);
  917. extent = btrfs_item_ptr(leaf, slot,
  918. struct btrfs_file_extent_item);
  919. if (off > key.offset) {
  920. datao += off - key.offset;
  921. datal -= off - key.offset;
  922. }
  923. if (key.offset + datao + datal + key.offset >
  924. off + len)
  925. datal = off + len - key.offset - datao;
  926. /* disko == 0 means it's a hole */
  927. if (!disko)
  928. datao = 0;
  929. btrfs_set_file_extent_offset(leaf, extent,
  930. datao);
  931. btrfs_set_file_extent_num_bytes(leaf, extent,
  932. datal);
  933. if (disko) {
  934. inode_add_bytes(inode, datal);
  935. ret = btrfs_inc_extent_ref(trans, root,
  936. disko, diskl, 0,
  937. root->root_key.objectid,
  938. inode->i_ino,
  939. new_key.offset - datao);
  940. BUG_ON(ret);
  941. }
  942. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  943. u64 skip = 0;
  944. u64 trim = 0;
  945. if (off > key.offset) {
  946. skip = off - key.offset;
  947. new_key.offset += skip;
  948. }
  949. if (key.offset + datal > off+len)
  950. trim = key.offset + datal - (off+len);
  951. if (comp && (skip || trim)) {
  952. ret = -EINVAL;
  953. goto out;
  954. }
  955. size -= skip + trim;
  956. datal -= skip + trim;
  957. ret = btrfs_insert_empty_item(trans, root, path,
  958. &new_key, size);
  959. if (ret)
  960. goto out;
  961. if (skip) {
  962. u32 start =
  963. btrfs_file_extent_calc_inline_size(0);
  964. memmove(buf+start, buf+start+skip,
  965. datal);
  966. }
  967. leaf = path->nodes[0];
  968. slot = path->slots[0];
  969. write_extent_buffer(leaf, buf,
  970. btrfs_item_ptr_offset(leaf, slot),
  971. size);
  972. inode_add_bytes(inode, datal);
  973. }
  974. btrfs_mark_buffer_dirty(leaf);
  975. }
  976. next:
  977. btrfs_release_path(root, path);
  978. key.offset++;
  979. }
  980. ret = 0;
  981. out:
  982. btrfs_release_path(root, path);
  983. if (ret == 0) {
  984. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  985. if (destoff + olen > inode->i_size)
  986. btrfs_i_size_write(inode, destoff + olen);
  987. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  988. ret = btrfs_update_inode(trans, root, inode);
  989. }
  990. btrfs_end_transaction(trans, root);
  991. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  992. if (ret)
  993. vmtruncate(inode, 0);
  994. out_unlock:
  995. mutex_unlock(&src->i_mutex);
  996. mutex_unlock(&inode->i_mutex);
  997. vfree(buf);
  998. btrfs_free_path(path);
  999. out_fput:
  1000. fput(src_file);
  1001. out_drop_write:
  1002. mnt_drop_write(file->f_path.mnt);
  1003. return ret;
  1004. }
  1005. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1006. {
  1007. struct btrfs_ioctl_clone_range_args args;
  1008. if (copy_from_user(&args, argp, sizeof(args)))
  1009. return -EFAULT;
  1010. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1011. args.src_length, args.dest_offset);
  1012. }
  1013. /*
  1014. * there are many ways the trans_start and trans_end ioctls can lead
  1015. * to deadlocks. They should only be used by applications that
  1016. * basically own the machine, and have a very in depth understanding
  1017. * of all the possible deadlocks and enospc problems.
  1018. */
  1019. static long btrfs_ioctl_trans_start(struct file *file)
  1020. {
  1021. struct inode *inode = fdentry(file)->d_inode;
  1022. struct btrfs_root *root = BTRFS_I(inode)->root;
  1023. struct btrfs_trans_handle *trans;
  1024. int ret = 0;
  1025. if (!capable(CAP_SYS_ADMIN))
  1026. return -EPERM;
  1027. if (file->private_data) {
  1028. ret = -EINPROGRESS;
  1029. goto out;
  1030. }
  1031. ret = mnt_want_write(file->f_path.mnt);
  1032. if (ret)
  1033. goto out;
  1034. mutex_lock(&root->fs_info->trans_mutex);
  1035. root->fs_info->open_ioctl_trans++;
  1036. mutex_unlock(&root->fs_info->trans_mutex);
  1037. trans = btrfs_start_ioctl_transaction(root, 0);
  1038. if (trans)
  1039. file->private_data = trans;
  1040. else
  1041. ret = -ENOMEM;
  1042. /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
  1043. out:
  1044. return ret;
  1045. }
  1046. /*
  1047. * there are many ways the trans_start and trans_end ioctls can lead
  1048. * to deadlocks. They should only be used by applications that
  1049. * basically own the machine, and have a very in depth understanding
  1050. * of all the possible deadlocks and enospc problems.
  1051. */
  1052. long btrfs_ioctl_trans_end(struct file *file)
  1053. {
  1054. struct inode *inode = fdentry(file)->d_inode;
  1055. struct btrfs_root *root = BTRFS_I(inode)->root;
  1056. struct btrfs_trans_handle *trans;
  1057. int ret = 0;
  1058. trans = file->private_data;
  1059. if (!trans) {
  1060. ret = -EINVAL;
  1061. goto out;
  1062. }
  1063. btrfs_end_transaction(trans, root);
  1064. file->private_data = NULL;
  1065. mutex_lock(&root->fs_info->trans_mutex);
  1066. root->fs_info->open_ioctl_trans--;
  1067. mutex_unlock(&root->fs_info->trans_mutex);
  1068. mnt_drop_write(file->f_path.mnt);
  1069. out:
  1070. return ret;
  1071. }
  1072. long btrfs_ioctl(struct file *file, unsigned int
  1073. cmd, unsigned long arg)
  1074. {
  1075. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1076. void __user *argp = (void __user *)arg;
  1077. switch (cmd) {
  1078. case FS_IOC_GETFLAGS:
  1079. return btrfs_ioctl_getflags(file, argp);
  1080. case FS_IOC_SETFLAGS:
  1081. return btrfs_ioctl_setflags(file, argp);
  1082. case FS_IOC_GETVERSION:
  1083. return btrfs_ioctl_getversion(file, argp);
  1084. case BTRFS_IOC_SNAP_CREATE:
  1085. return btrfs_ioctl_snap_create(file, argp, 0);
  1086. case BTRFS_IOC_SUBVOL_CREATE:
  1087. return btrfs_ioctl_snap_create(file, argp, 1);
  1088. case BTRFS_IOC_DEFRAG:
  1089. return btrfs_ioctl_defrag(file);
  1090. case BTRFS_IOC_RESIZE:
  1091. return btrfs_ioctl_resize(root, argp);
  1092. case BTRFS_IOC_ADD_DEV:
  1093. return btrfs_ioctl_add_dev(root, argp);
  1094. case BTRFS_IOC_RM_DEV:
  1095. return btrfs_ioctl_rm_dev(root, argp);
  1096. case BTRFS_IOC_BALANCE:
  1097. return btrfs_balance(root->fs_info->dev_root);
  1098. case BTRFS_IOC_CLONE:
  1099. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  1100. case BTRFS_IOC_CLONE_RANGE:
  1101. return btrfs_ioctl_clone_range(file, argp);
  1102. case BTRFS_IOC_TRANS_START:
  1103. return btrfs_ioctl_trans_start(file);
  1104. case BTRFS_IOC_TRANS_END:
  1105. return btrfs_ioctl_trans_end(file);
  1106. case BTRFS_IOC_SYNC:
  1107. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  1108. return 0;
  1109. }
  1110. return -ENOTTY;
  1111. }