ioctl.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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;
  203. struct inode *dir = dentry->d_parent->d_inode;
  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. return ret;
  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. key.offset = (u64)-1;
  264. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  265. BUG_ON(IS_ERR(new_root));
  266. btrfs_record_root_in_trans(trans, new_root);
  267. ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
  268. BTRFS_I(dir)->block_group);
  269. /*
  270. * insert the directory item
  271. */
  272. ret = btrfs_set_inode_index(dir, &index);
  273. BUG_ON(ret);
  274. ret = btrfs_insert_dir_item(trans, root,
  275. name, namelen, dir->i_ino, &key,
  276. BTRFS_FT_DIR, index);
  277. if (ret)
  278. goto fail;
  279. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  280. ret = btrfs_update_inode(trans, root, dir);
  281. BUG_ON(ret);
  282. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  283. objectid, root->root_key.objectid,
  284. dir->i_ino, index, name, namelen);
  285. BUG_ON(ret);
  286. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  287. fail:
  288. nr = trans->blocks_used;
  289. err = btrfs_commit_transaction(trans, root);
  290. if (err && !ret)
  291. ret = err;
  292. return ret;
  293. }
  294. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  295. char *name, int namelen)
  296. {
  297. struct btrfs_pending_snapshot *pending_snapshot;
  298. struct btrfs_trans_handle *trans;
  299. int ret = 0;
  300. int err;
  301. unsigned long nr = 0;
  302. if (!root->ref_cows)
  303. return -EINVAL;
  304. ret = btrfs_check_metadata_free_space(root);
  305. if (ret)
  306. goto fail_unlock;
  307. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  308. if (!pending_snapshot) {
  309. ret = -ENOMEM;
  310. goto fail_unlock;
  311. }
  312. pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
  313. if (!pending_snapshot->name) {
  314. ret = -ENOMEM;
  315. kfree(pending_snapshot);
  316. goto fail_unlock;
  317. }
  318. memcpy(pending_snapshot->name, name, namelen);
  319. pending_snapshot->name[namelen] = '\0';
  320. pending_snapshot->dentry = dentry;
  321. trans = btrfs_start_transaction(root, 1);
  322. BUG_ON(!trans);
  323. pending_snapshot->root = root;
  324. list_add(&pending_snapshot->list,
  325. &trans->transaction->pending_snapshots);
  326. err = btrfs_commit_transaction(trans, root);
  327. fail_unlock:
  328. btrfs_btree_balance_dirty(root, nr);
  329. return ret;
  330. }
  331. /* copy of may_create in fs/namei.c() */
  332. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  333. {
  334. if (child->d_inode)
  335. return -EEXIST;
  336. if (IS_DEADDIR(dir))
  337. return -ENOENT;
  338. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  339. }
  340. /*
  341. * Create a new subvolume below @parent. This is largely modeled after
  342. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  343. * inside this filesystem so it's quite a bit simpler.
  344. */
  345. static noinline int btrfs_mksubvol(struct path *parent,
  346. char *name, int namelen,
  347. struct btrfs_root *snap_src)
  348. {
  349. struct inode *dir = parent->dentry->d_inode;
  350. struct dentry *dentry;
  351. int error;
  352. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  353. dentry = lookup_one_len(name, parent->dentry, namelen);
  354. error = PTR_ERR(dentry);
  355. if (IS_ERR(dentry))
  356. goto out_unlock;
  357. error = -EEXIST;
  358. if (dentry->d_inode)
  359. goto out_dput;
  360. error = mnt_want_write(parent->mnt);
  361. if (error)
  362. goto out_dput;
  363. error = btrfs_may_create(dir, dentry);
  364. if (error)
  365. goto out_drop_write;
  366. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  367. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  368. goto out_up_read;
  369. if (snap_src) {
  370. error = create_snapshot(snap_src, dentry,
  371. name, namelen);
  372. } else {
  373. error = create_subvol(BTRFS_I(dir)->root, dentry,
  374. name, namelen);
  375. }
  376. if (!error)
  377. fsnotify_mkdir(dir, dentry);
  378. out_up_read:
  379. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  380. out_drop_write:
  381. mnt_drop_write(parent->mnt);
  382. out_dput:
  383. dput(dentry);
  384. out_unlock:
  385. mutex_unlock(&dir->i_mutex);
  386. return error;
  387. }
  388. static int btrfs_defrag_file(struct file *file)
  389. {
  390. struct inode *inode = fdentry(file)->d_inode;
  391. struct btrfs_root *root = BTRFS_I(inode)->root;
  392. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  393. struct btrfs_ordered_extent *ordered;
  394. struct page *page;
  395. unsigned long last_index;
  396. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  397. unsigned long total_read = 0;
  398. u64 page_start;
  399. u64 page_end;
  400. unsigned long i;
  401. int ret;
  402. ret = btrfs_check_data_free_space(root, inode, inode->i_size);
  403. if (ret)
  404. return -ENOSPC;
  405. mutex_lock(&inode->i_mutex);
  406. last_index = inode->i_size >> PAGE_CACHE_SHIFT;
  407. for (i = 0; i <= last_index; i++) {
  408. if (total_read % ra_pages == 0) {
  409. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  410. min(last_index, i + ra_pages - 1));
  411. }
  412. total_read++;
  413. again:
  414. page = grab_cache_page(inode->i_mapping, i);
  415. if (!page)
  416. goto out_unlock;
  417. if (!PageUptodate(page)) {
  418. btrfs_readpage(NULL, page);
  419. lock_page(page);
  420. if (!PageUptodate(page)) {
  421. unlock_page(page);
  422. page_cache_release(page);
  423. goto out_unlock;
  424. }
  425. }
  426. wait_on_page_writeback(page);
  427. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  428. page_end = page_start + PAGE_CACHE_SIZE - 1;
  429. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  430. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  431. if (ordered) {
  432. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  433. unlock_page(page);
  434. page_cache_release(page);
  435. btrfs_start_ordered_extent(inode, ordered, 1);
  436. btrfs_put_ordered_extent(ordered);
  437. goto again;
  438. }
  439. set_page_extent_mapped(page);
  440. /*
  441. * this makes sure page_mkwrite is called on the
  442. * page if it is dirtied again later
  443. */
  444. clear_page_dirty_for_io(page);
  445. btrfs_set_extent_delalloc(inode, page_start, page_end);
  446. set_page_dirty(page);
  447. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  448. unlock_page(page);
  449. page_cache_release(page);
  450. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  451. }
  452. out_unlock:
  453. mutex_unlock(&inode->i_mutex);
  454. return 0;
  455. }
  456. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  457. void __user *arg)
  458. {
  459. u64 new_size;
  460. u64 old_size;
  461. u64 devid = 1;
  462. struct btrfs_ioctl_vol_args *vol_args;
  463. struct btrfs_trans_handle *trans;
  464. struct btrfs_device *device = NULL;
  465. char *sizestr;
  466. char *devstr = NULL;
  467. int ret = 0;
  468. int namelen;
  469. int mod = 0;
  470. if (root->fs_info->sb->s_flags & MS_RDONLY)
  471. return -EROFS;
  472. if (!capable(CAP_SYS_ADMIN))
  473. return -EPERM;
  474. vol_args = memdup_user(arg, sizeof(*vol_args));
  475. if (IS_ERR(vol_args))
  476. return PTR_ERR(vol_args);
  477. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  478. namelen = strlen(vol_args->name);
  479. mutex_lock(&root->fs_info->volume_mutex);
  480. sizestr = vol_args->name;
  481. devstr = strchr(sizestr, ':');
  482. if (devstr) {
  483. char *end;
  484. sizestr = devstr + 1;
  485. *devstr = '\0';
  486. devstr = vol_args->name;
  487. devid = simple_strtoull(devstr, &end, 10);
  488. printk(KERN_INFO "resizing devid %llu\n",
  489. (unsigned long long)devid);
  490. }
  491. device = btrfs_find_device(root, devid, NULL, NULL);
  492. if (!device) {
  493. printk(KERN_INFO "resizer unable to find device %llu\n",
  494. (unsigned long long)devid);
  495. ret = -EINVAL;
  496. goto out_unlock;
  497. }
  498. if (!strcmp(sizestr, "max"))
  499. new_size = device->bdev->bd_inode->i_size;
  500. else {
  501. if (sizestr[0] == '-') {
  502. mod = -1;
  503. sizestr++;
  504. } else if (sizestr[0] == '+') {
  505. mod = 1;
  506. sizestr++;
  507. }
  508. new_size = btrfs_parse_size(sizestr);
  509. if (new_size == 0) {
  510. ret = -EINVAL;
  511. goto out_unlock;
  512. }
  513. }
  514. old_size = device->total_bytes;
  515. if (mod < 0) {
  516. if (new_size > old_size) {
  517. ret = -EINVAL;
  518. goto out_unlock;
  519. }
  520. new_size = old_size - new_size;
  521. } else if (mod > 0) {
  522. new_size = old_size + new_size;
  523. }
  524. if (new_size < 256 * 1024 * 1024) {
  525. ret = -EINVAL;
  526. goto out_unlock;
  527. }
  528. if (new_size > device->bdev->bd_inode->i_size) {
  529. ret = -EFBIG;
  530. goto out_unlock;
  531. }
  532. do_div(new_size, root->sectorsize);
  533. new_size *= root->sectorsize;
  534. printk(KERN_INFO "new size for %s is %llu\n",
  535. device->name, (unsigned long long)new_size);
  536. if (new_size > old_size) {
  537. trans = btrfs_start_transaction(root, 1);
  538. ret = btrfs_grow_device(trans, device, new_size);
  539. btrfs_commit_transaction(trans, root);
  540. } else {
  541. ret = btrfs_shrink_device(device, new_size);
  542. }
  543. out_unlock:
  544. mutex_unlock(&root->fs_info->volume_mutex);
  545. kfree(vol_args);
  546. return ret;
  547. }
  548. static noinline int btrfs_ioctl_snap_create(struct file *file,
  549. void __user *arg, int subvol)
  550. {
  551. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  552. struct btrfs_ioctl_vol_args *vol_args;
  553. struct file *src_file;
  554. int namelen;
  555. int ret = 0;
  556. if (root->fs_info->sb->s_flags & MS_RDONLY)
  557. return -EROFS;
  558. vol_args = memdup_user(arg, sizeof(*vol_args));
  559. if (IS_ERR(vol_args))
  560. return PTR_ERR(vol_args);
  561. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  562. namelen = strlen(vol_args->name);
  563. if (strchr(vol_args->name, '/')) {
  564. ret = -EINVAL;
  565. goto out;
  566. }
  567. if (subvol) {
  568. ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
  569. NULL);
  570. } else {
  571. struct inode *src_inode;
  572. src_file = fget(vol_args->fd);
  573. if (!src_file) {
  574. ret = -EINVAL;
  575. goto out;
  576. }
  577. src_inode = src_file->f_path.dentry->d_inode;
  578. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  579. printk(KERN_INFO "btrfs: Snapshot src from "
  580. "another FS\n");
  581. ret = -EINVAL;
  582. fput(src_file);
  583. goto out;
  584. }
  585. ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
  586. BTRFS_I(src_inode)->root);
  587. fput(src_file);
  588. }
  589. out:
  590. kfree(vol_args);
  591. return ret;
  592. }
  593. /*
  594. * helper to check if the subvolume references other subvolumes
  595. */
  596. static noinline int may_destroy_subvol(struct btrfs_root *root)
  597. {
  598. struct btrfs_path *path;
  599. struct btrfs_key key;
  600. int ret;
  601. path = btrfs_alloc_path();
  602. if (!path)
  603. return -ENOMEM;
  604. key.objectid = root->root_key.objectid;
  605. key.type = BTRFS_ROOT_REF_KEY;
  606. key.offset = (u64)-1;
  607. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  608. &key, path, 0, 0);
  609. if (ret < 0)
  610. goto out;
  611. BUG_ON(ret == 0);
  612. ret = 0;
  613. if (path->slots[0] > 0) {
  614. path->slots[0]--;
  615. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  616. if (key.objectid == root->root_key.objectid &&
  617. key.type == BTRFS_ROOT_REF_KEY)
  618. ret = -ENOTEMPTY;
  619. }
  620. out:
  621. btrfs_free_path(path);
  622. return ret;
  623. }
  624. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  625. void __user *arg)
  626. {
  627. struct dentry *parent = fdentry(file);
  628. struct dentry *dentry;
  629. struct inode *dir = parent->d_inode;
  630. struct inode *inode;
  631. struct btrfs_root *root = BTRFS_I(dir)->root;
  632. struct btrfs_root *dest = NULL;
  633. struct btrfs_ioctl_vol_args *vol_args;
  634. struct btrfs_trans_handle *trans;
  635. int namelen;
  636. int ret;
  637. int err = 0;
  638. if (!capable(CAP_SYS_ADMIN))
  639. return -EPERM;
  640. vol_args = memdup_user(arg, sizeof(*vol_args));
  641. if (IS_ERR(vol_args))
  642. return PTR_ERR(vol_args);
  643. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  644. namelen = strlen(vol_args->name);
  645. if (strchr(vol_args->name, '/') ||
  646. strncmp(vol_args->name, "..", namelen) == 0) {
  647. err = -EINVAL;
  648. goto out;
  649. }
  650. err = mnt_want_write(file->f_path.mnt);
  651. if (err)
  652. goto out;
  653. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  654. dentry = lookup_one_len(vol_args->name, parent, namelen);
  655. if (IS_ERR(dentry)) {
  656. err = PTR_ERR(dentry);
  657. goto out_unlock_dir;
  658. }
  659. if (!dentry->d_inode) {
  660. err = -ENOENT;
  661. goto out_dput;
  662. }
  663. inode = dentry->d_inode;
  664. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  665. err = -EINVAL;
  666. goto out_dput;
  667. }
  668. dest = BTRFS_I(inode)->root;
  669. mutex_lock(&inode->i_mutex);
  670. err = d_invalidate(dentry);
  671. if (err)
  672. goto out_unlock;
  673. down_write(&root->fs_info->subvol_sem);
  674. err = may_destroy_subvol(dest);
  675. if (err)
  676. goto out_up_write;
  677. trans = btrfs_start_transaction(root, 1);
  678. ret = btrfs_unlink_subvol(trans, root, dir,
  679. dest->root_key.objectid,
  680. dentry->d_name.name,
  681. dentry->d_name.len);
  682. BUG_ON(ret);
  683. btrfs_record_root_in_trans(trans, dest);
  684. memset(&dest->root_item.drop_progress, 0,
  685. sizeof(dest->root_item.drop_progress));
  686. dest->root_item.drop_level = 0;
  687. btrfs_set_root_refs(&dest->root_item, 0);
  688. ret = btrfs_insert_orphan_item(trans,
  689. root->fs_info->tree_root,
  690. dest->root_key.objectid);
  691. BUG_ON(ret);
  692. ret = btrfs_commit_transaction(trans, root);
  693. BUG_ON(ret);
  694. inode->i_flags |= S_DEAD;
  695. out_up_write:
  696. up_write(&root->fs_info->subvol_sem);
  697. out_unlock:
  698. mutex_unlock(&inode->i_mutex);
  699. if (!err) {
  700. btrfs_invalidate_inodes(dest);
  701. d_delete(dentry);
  702. }
  703. out_dput:
  704. dput(dentry);
  705. out_unlock_dir:
  706. mutex_unlock(&dir->i_mutex);
  707. mnt_drop_write(file->f_path.mnt);
  708. out:
  709. kfree(vol_args);
  710. return err;
  711. }
  712. static int btrfs_ioctl_defrag(struct file *file)
  713. {
  714. struct inode *inode = fdentry(file)->d_inode;
  715. struct btrfs_root *root = BTRFS_I(inode)->root;
  716. int ret;
  717. ret = mnt_want_write(file->f_path.mnt);
  718. if (ret)
  719. return ret;
  720. switch (inode->i_mode & S_IFMT) {
  721. case S_IFDIR:
  722. if (!capable(CAP_SYS_ADMIN)) {
  723. ret = -EPERM;
  724. goto out;
  725. }
  726. btrfs_defrag_root(root, 0);
  727. btrfs_defrag_root(root->fs_info->extent_root, 0);
  728. break;
  729. case S_IFREG:
  730. if (!(file->f_mode & FMODE_WRITE)) {
  731. ret = -EINVAL;
  732. goto out;
  733. }
  734. btrfs_defrag_file(file);
  735. break;
  736. }
  737. out:
  738. mnt_drop_write(file->f_path.mnt);
  739. return ret;
  740. }
  741. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  742. {
  743. struct btrfs_ioctl_vol_args *vol_args;
  744. int ret;
  745. if (!capable(CAP_SYS_ADMIN))
  746. return -EPERM;
  747. vol_args = memdup_user(arg, sizeof(*vol_args));
  748. if (IS_ERR(vol_args))
  749. return PTR_ERR(vol_args);
  750. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  751. ret = btrfs_init_new_device(root, vol_args->name);
  752. kfree(vol_args);
  753. return ret;
  754. }
  755. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  756. {
  757. struct btrfs_ioctl_vol_args *vol_args;
  758. int ret;
  759. if (!capable(CAP_SYS_ADMIN))
  760. return -EPERM;
  761. if (root->fs_info->sb->s_flags & MS_RDONLY)
  762. return -EROFS;
  763. vol_args = memdup_user(arg, sizeof(*vol_args));
  764. if (IS_ERR(vol_args))
  765. return PTR_ERR(vol_args);
  766. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  767. ret = btrfs_rm_device(root, vol_args->name);
  768. kfree(vol_args);
  769. return ret;
  770. }
  771. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  772. u64 off, u64 olen, u64 destoff)
  773. {
  774. struct inode *inode = fdentry(file)->d_inode;
  775. struct btrfs_root *root = BTRFS_I(inode)->root;
  776. struct file *src_file;
  777. struct inode *src;
  778. struct btrfs_trans_handle *trans;
  779. struct btrfs_path *path;
  780. struct extent_buffer *leaf;
  781. char *buf;
  782. struct btrfs_key key;
  783. u32 nritems;
  784. int slot;
  785. int ret;
  786. u64 len = olen;
  787. u64 bs = root->fs_info->sb->s_blocksize;
  788. u64 hint_byte;
  789. /*
  790. * TODO:
  791. * - split compressed inline extents. annoying: we need to
  792. * decompress into destination's address_space (the file offset
  793. * may change, so source mapping won't do), then recompress (or
  794. * otherwise reinsert) a subrange.
  795. * - allow ranges within the same file to be cloned (provided
  796. * they don't overlap)?
  797. */
  798. /* the destination must be opened for writing */
  799. if (!(file->f_mode & FMODE_WRITE))
  800. return -EINVAL;
  801. ret = mnt_want_write(file->f_path.mnt);
  802. if (ret)
  803. return ret;
  804. src_file = fget(srcfd);
  805. if (!src_file) {
  806. ret = -EBADF;
  807. goto out_drop_write;
  808. }
  809. src = src_file->f_dentry->d_inode;
  810. ret = -EINVAL;
  811. if (src == inode)
  812. goto out_fput;
  813. ret = -EISDIR;
  814. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  815. goto out_fput;
  816. ret = -EXDEV;
  817. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  818. goto out_fput;
  819. ret = -ENOMEM;
  820. buf = vmalloc(btrfs_level_size(root, 0));
  821. if (!buf)
  822. goto out_fput;
  823. path = btrfs_alloc_path();
  824. if (!path) {
  825. vfree(buf);
  826. goto out_fput;
  827. }
  828. path->reada = 2;
  829. if (inode < src) {
  830. mutex_lock(&inode->i_mutex);
  831. mutex_lock(&src->i_mutex);
  832. } else {
  833. mutex_lock(&src->i_mutex);
  834. mutex_lock(&inode->i_mutex);
  835. }
  836. /* determine range to clone */
  837. ret = -EINVAL;
  838. if (off >= src->i_size || off + len > src->i_size)
  839. goto out_unlock;
  840. if (len == 0)
  841. olen = len = src->i_size - off;
  842. /* if we extend to eof, continue to block boundary */
  843. if (off + len == src->i_size)
  844. len = ((src->i_size + bs-1) & ~(bs-1))
  845. - off;
  846. /* verify the end result is block aligned */
  847. if ((off & (bs-1)) ||
  848. ((off + len) & (bs-1)))
  849. goto out_unlock;
  850. /* do any pending delalloc/csum calc on src, one way or
  851. another, and lock file content */
  852. while (1) {
  853. struct btrfs_ordered_extent *ordered;
  854. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  855. ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
  856. if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
  857. break;
  858. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  859. if (ordered)
  860. btrfs_put_ordered_extent(ordered);
  861. btrfs_wait_ordered_range(src, off, off+len);
  862. }
  863. trans = btrfs_start_transaction(root, 1);
  864. BUG_ON(!trans);
  865. /* punch hole in destination first */
  866. btrfs_drop_extents(trans, root, inode, off, off + len,
  867. off + len, 0, &hint_byte, 1);
  868. /* clone data */
  869. key.objectid = src->i_ino;
  870. key.type = BTRFS_EXTENT_DATA_KEY;
  871. key.offset = 0;
  872. while (1) {
  873. /*
  874. * note the key will change type as we walk through the
  875. * tree.
  876. */
  877. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  878. if (ret < 0)
  879. goto out;
  880. nritems = btrfs_header_nritems(path->nodes[0]);
  881. if (path->slots[0] >= nritems) {
  882. ret = btrfs_next_leaf(root, path);
  883. if (ret < 0)
  884. goto out;
  885. if (ret > 0)
  886. break;
  887. nritems = btrfs_header_nritems(path->nodes[0]);
  888. }
  889. leaf = path->nodes[0];
  890. slot = path->slots[0];
  891. btrfs_item_key_to_cpu(leaf, &key, slot);
  892. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  893. key.objectid != src->i_ino)
  894. break;
  895. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  896. struct btrfs_file_extent_item *extent;
  897. int type;
  898. u32 size;
  899. struct btrfs_key new_key;
  900. u64 disko = 0, diskl = 0;
  901. u64 datao = 0, datal = 0;
  902. u8 comp;
  903. size = btrfs_item_size_nr(leaf, slot);
  904. read_extent_buffer(leaf, buf,
  905. btrfs_item_ptr_offset(leaf, slot),
  906. size);
  907. extent = btrfs_item_ptr(leaf, slot,
  908. struct btrfs_file_extent_item);
  909. comp = btrfs_file_extent_compression(leaf, extent);
  910. type = btrfs_file_extent_type(leaf, extent);
  911. if (type == BTRFS_FILE_EXTENT_REG ||
  912. type == BTRFS_FILE_EXTENT_PREALLOC) {
  913. disko = btrfs_file_extent_disk_bytenr(leaf,
  914. extent);
  915. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  916. extent);
  917. datao = btrfs_file_extent_offset(leaf, extent);
  918. datal = btrfs_file_extent_num_bytes(leaf,
  919. extent);
  920. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  921. /* take upper bound, may be compressed */
  922. datal = btrfs_file_extent_ram_bytes(leaf,
  923. extent);
  924. }
  925. btrfs_release_path(root, path);
  926. if (key.offset + datal < off ||
  927. key.offset >= off+len)
  928. goto next;
  929. memcpy(&new_key, &key, sizeof(new_key));
  930. new_key.objectid = inode->i_ino;
  931. new_key.offset = key.offset + destoff - off;
  932. if (type == BTRFS_FILE_EXTENT_REG ||
  933. type == BTRFS_FILE_EXTENT_PREALLOC) {
  934. ret = btrfs_insert_empty_item(trans, root, path,
  935. &new_key, size);
  936. if (ret)
  937. goto out;
  938. leaf = path->nodes[0];
  939. slot = path->slots[0];
  940. write_extent_buffer(leaf, buf,
  941. btrfs_item_ptr_offset(leaf, slot),
  942. size);
  943. extent = btrfs_item_ptr(leaf, slot,
  944. struct btrfs_file_extent_item);
  945. if (off > key.offset) {
  946. datao += off - key.offset;
  947. datal -= off - key.offset;
  948. }
  949. if (key.offset + datao + datal > off + len)
  950. datal = off + len - key.offset - datao;
  951. /* disko == 0 means it's a hole */
  952. if (!disko)
  953. datao = 0;
  954. btrfs_set_file_extent_offset(leaf, extent,
  955. datao);
  956. btrfs_set_file_extent_num_bytes(leaf, extent,
  957. datal);
  958. if (disko) {
  959. inode_add_bytes(inode, datal);
  960. ret = btrfs_inc_extent_ref(trans, root,
  961. disko, diskl, 0,
  962. root->root_key.objectid,
  963. inode->i_ino,
  964. new_key.offset - datao);
  965. BUG_ON(ret);
  966. }
  967. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  968. u64 skip = 0;
  969. u64 trim = 0;
  970. if (off > key.offset) {
  971. skip = off - key.offset;
  972. new_key.offset += skip;
  973. }
  974. if (key.offset + datal > off+len)
  975. trim = key.offset + datal - (off+len);
  976. if (comp && (skip || trim)) {
  977. ret = -EINVAL;
  978. goto out;
  979. }
  980. size -= skip + trim;
  981. datal -= skip + trim;
  982. ret = btrfs_insert_empty_item(trans, root, path,
  983. &new_key, size);
  984. if (ret)
  985. goto out;
  986. if (skip) {
  987. u32 start =
  988. btrfs_file_extent_calc_inline_size(0);
  989. memmove(buf+start, buf+start+skip,
  990. datal);
  991. }
  992. leaf = path->nodes[0];
  993. slot = path->slots[0];
  994. write_extent_buffer(leaf, buf,
  995. btrfs_item_ptr_offset(leaf, slot),
  996. size);
  997. inode_add_bytes(inode, datal);
  998. }
  999. btrfs_mark_buffer_dirty(leaf);
  1000. }
  1001. next:
  1002. btrfs_release_path(root, path);
  1003. key.offset++;
  1004. }
  1005. ret = 0;
  1006. out:
  1007. btrfs_release_path(root, path);
  1008. if (ret == 0) {
  1009. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1010. if (destoff + olen > inode->i_size)
  1011. btrfs_i_size_write(inode, destoff + olen);
  1012. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1013. ret = btrfs_update_inode(trans, root, inode);
  1014. }
  1015. btrfs_end_transaction(trans, root);
  1016. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1017. if (ret)
  1018. vmtruncate(inode, 0);
  1019. out_unlock:
  1020. mutex_unlock(&src->i_mutex);
  1021. mutex_unlock(&inode->i_mutex);
  1022. vfree(buf);
  1023. btrfs_free_path(path);
  1024. out_fput:
  1025. fput(src_file);
  1026. out_drop_write:
  1027. mnt_drop_write(file->f_path.mnt);
  1028. return ret;
  1029. }
  1030. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1031. {
  1032. struct btrfs_ioctl_clone_range_args args;
  1033. if (copy_from_user(&args, argp, sizeof(args)))
  1034. return -EFAULT;
  1035. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1036. args.src_length, args.dest_offset);
  1037. }
  1038. /*
  1039. * there are many ways the trans_start and trans_end ioctls can lead
  1040. * to deadlocks. They should only be used by applications that
  1041. * basically own the machine, and have a very in depth understanding
  1042. * of all the possible deadlocks and enospc problems.
  1043. */
  1044. static long btrfs_ioctl_trans_start(struct file *file)
  1045. {
  1046. struct inode *inode = fdentry(file)->d_inode;
  1047. struct btrfs_root *root = BTRFS_I(inode)->root;
  1048. struct btrfs_trans_handle *trans;
  1049. int ret = 0;
  1050. if (!capable(CAP_SYS_ADMIN))
  1051. return -EPERM;
  1052. if (file->private_data) {
  1053. ret = -EINPROGRESS;
  1054. goto out;
  1055. }
  1056. ret = mnt_want_write(file->f_path.mnt);
  1057. if (ret)
  1058. goto out;
  1059. mutex_lock(&root->fs_info->trans_mutex);
  1060. root->fs_info->open_ioctl_trans++;
  1061. mutex_unlock(&root->fs_info->trans_mutex);
  1062. trans = btrfs_start_ioctl_transaction(root, 0);
  1063. if (trans)
  1064. file->private_data = trans;
  1065. else
  1066. ret = -ENOMEM;
  1067. /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
  1068. out:
  1069. return ret;
  1070. }
  1071. /*
  1072. * there are many ways the trans_start and trans_end ioctls can lead
  1073. * to deadlocks. They should only be used by applications that
  1074. * basically own the machine, and have a very in depth understanding
  1075. * of all the possible deadlocks and enospc problems.
  1076. */
  1077. long btrfs_ioctl_trans_end(struct file *file)
  1078. {
  1079. struct inode *inode = fdentry(file)->d_inode;
  1080. struct btrfs_root *root = BTRFS_I(inode)->root;
  1081. struct btrfs_trans_handle *trans;
  1082. int ret = 0;
  1083. trans = file->private_data;
  1084. if (!trans) {
  1085. ret = -EINVAL;
  1086. goto out;
  1087. }
  1088. btrfs_end_transaction(trans, root);
  1089. file->private_data = NULL;
  1090. mutex_lock(&root->fs_info->trans_mutex);
  1091. root->fs_info->open_ioctl_trans--;
  1092. mutex_unlock(&root->fs_info->trans_mutex);
  1093. mnt_drop_write(file->f_path.mnt);
  1094. out:
  1095. return ret;
  1096. }
  1097. long btrfs_ioctl(struct file *file, unsigned int
  1098. cmd, unsigned long arg)
  1099. {
  1100. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1101. void __user *argp = (void __user *)arg;
  1102. switch (cmd) {
  1103. case FS_IOC_GETFLAGS:
  1104. return btrfs_ioctl_getflags(file, argp);
  1105. case FS_IOC_SETFLAGS:
  1106. return btrfs_ioctl_setflags(file, argp);
  1107. case FS_IOC_GETVERSION:
  1108. return btrfs_ioctl_getversion(file, argp);
  1109. case BTRFS_IOC_SNAP_CREATE:
  1110. return btrfs_ioctl_snap_create(file, argp, 0);
  1111. case BTRFS_IOC_SUBVOL_CREATE:
  1112. return btrfs_ioctl_snap_create(file, argp, 1);
  1113. case BTRFS_IOC_SNAP_DESTROY:
  1114. return btrfs_ioctl_snap_destroy(file, argp);
  1115. case BTRFS_IOC_DEFRAG:
  1116. return btrfs_ioctl_defrag(file);
  1117. case BTRFS_IOC_RESIZE:
  1118. return btrfs_ioctl_resize(root, argp);
  1119. case BTRFS_IOC_ADD_DEV:
  1120. return btrfs_ioctl_add_dev(root, argp);
  1121. case BTRFS_IOC_RM_DEV:
  1122. return btrfs_ioctl_rm_dev(root, argp);
  1123. case BTRFS_IOC_BALANCE:
  1124. return btrfs_balance(root->fs_info->dev_root);
  1125. case BTRFS_IOC_CLONE:
  1126. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  1127. case BTRFS_IOC_CLONE_RANGE:
  1128. return btrfs_ioctl_clone_range(file, argp);
  1129. case BTRFS_IOC_TRANS_START:
  1130. return btrfs_ioctl_trans_start(file);
  1131. case BTRFS_IOC_TRANS_END:
  1132. return btrfs_ioctl_trans_end(file);
  1133. case BTRFS_IOC_SYNC:
  1134. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  1135. return 0;
  1136. }
  1137. return -ENOTTY;
  1138. }