ioctl.c 32 KB

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