ioctl.c 32 KB

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