ioctl.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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 + key.offset >
  950. off + len)
  951. datal = off + len - key.offset - datao;
  952. /* disko == 0 means it's a hole */
  953. if (!disko)
  954. datao = 0;
  955. btrfs_set_file_extent_offset(leaf, extent,
  956. datao);
  957. btrfs_set_file_extent_num_bytes(leaf, extent,
  958. datal);
  959. if (disko) {
  960. inode_add_bytes(inode, datal);
  961. ret = btrfs_inc_extent_ref(trans, root,
  962. disko, diskl, 0,
  963. root->root_key.objectid,
  964. inode->i_ino,
  965. new_key.offset - datao);
  966. BUG_ON(ret);
  967. }
  968. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  969. u64 skip = 0;
  970. u64 trim = 0;
  971. if (off > key.offset) {
  972. skip = off - key.offset;
  973. new_key.offset += skip;
  974. }
  975. if (key.offset + datal > off+len)
  976. trim = key.offset + datal - (off+len);
  977. if (comp && (skip || trim)) {
  978. ret = -EINVAL;
  979. goto out;
  980. }
  981. size -= skip + trim;
  982. datal -= skip + trim;
  983. ret = btrfs_insert_empty_item(trans, root, path,
  984. &new_key, size);
  985. if (ret)
  986. goto out;
  987. if (skip) {
  988. u32 start =
  989. btrfs_file_extent_calc_inline_size(0);
  990. memmove(buf+start, buf+start+skip,
  991. datal);
  992. }
  993. leaf = path->nodes[0];
  994. slot = path->slots[0];
  995. write_extent_buffer(leaf, buf,
  996. btrfs_item_ptr_offset(leaf, slot),
  997. size);
  998. inode_add_bytes(inode, datal);
  999. }
  1000. btrfs_mark_buffer_dirty(leaf);
  1001. }
  1002. next:
  1003. btrfs_release_path(root, path);
  1004. key.offset++;
  1005. }
  1006. ret = 0;
  1007. out:
  1008. btrfs_release_path(root, path);
  1009. if (ret == 0) {
  1010. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1011. if (destoff + olen > inode->i_size)
  1012. btrfs_i_size_write(inode, destoff + olen);
  1013. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1014. ret = btrfs_update_inode(trans, root, inode);
  1015. }
  1016. btrfs_end_transaction(trans, root);
  1017. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1018. if (ret)
  1019. vmtruncate(inode, 0);
  1020. out_unlock:
  1021. mutex_unlock(&src->i_mutex);
  1022. mutex_unlock(&inode->i_mutex);
  1023. vfree(buf);
  1024. btrfs_free_path(path);
  1025. out_fput:
  1026. fput(src_file);
  1027. out_drop_write:
  1028. mnt_drop_write(file->f_path.mnt);
  1029. return ret;
  1030. }
  1031. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1032. {
  1033. struct btrfs_ioctl_clone_range_args args;
  1034. if (copy_from_user(&args, argp, sizeof(args)))
  1035. return -EFAULT;
  1036. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1037. args.src_length, args.dest_offset);
  1038. }
  1039. /*
  1040. * there are many ways the trans_start and trans_end ioctls can lead
  1041. * to deadlocks. They should only be used by applications that
  1042. * basically own the machine, and have a very in depth understanding
  1043. * of all the possible deadlocks and enospc problems.
  1044. */
  1045. static long btrfs_ioctl_trans_start(struct file *file)
  1046. {
  1047. struct inode *inode = fdentry(file)->d_inode;
  1048. struct btrfs_root *root = BTRFS_I(inode)->root;
  1049. struct btrfs_trans_handle *trans;
  1050. int ret = 0;
  1051. if (!capable(CAP_SYS_ADMIN))
  1052. return -EPERM;
  1053. if (file->private_data) {
  1054. ret = -EINPROGRESS;
  1055. goto out;
  1056. }
  1057. ret = mnt_want_write(file->f_path.mnt);
  1058. if (ret)
  1059. goto out;
  1060. mutex_lock(&root->fs_info->trans_mutex);
  1061. root->fs_info->open_ioctl_trans++;
  1062. mutex_unlock(&root->fs_info->trans_mutex);
  1063. trans = btrfs_start_ioctl_transaction(root, 0);
  1064. if (trans)
  1065. file->private_data = trans;
  1066. else
  1067. ret = -ENOMEM;
  1068. /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
  1069. out:
  1070. return ret;
  1071. }
  1072. /*
  1073. * there are many ways the trans_start and trans_end ioctls can lead
  1074. * to deadlocks. They should only be used by applications that
  1075. * basically own the machine, and have a very in depth understanding
  1076. * of all the possible deadlocks and enospc problems.
  1077. */
  1078. long btrfs_ioctl_trans_end(struct file *file)
  1079. {
  1080. struct inode *inode = fdentry(file)->d_inode;
  1081. struct btrfs_root *root = BTRFS_I(inode)->root;
  1082. struct btrfs_trans_handle *trans;
  1083. int ret = 0;
  1084. trans = file->private_data;
  1085. if (!trans) {
  1086. ret = -EINVAL;
  1087. goto out;
  1088. }
  1089. btrfs_end_transaction(trans, root);
  1090. file->private_data = NULL;
  1091. mutex_lock(&root->fs_info->trans_mutex);
  1092. root->fs_info->open_ioctl_trans--;
  1093. mutex_unlock(&root->fs_info->trans_mutex);
  1094. mnt_drop_write(file->f_path.mnt);
  1095. out:
  1096. return ret;
  1097. }
  1098. long btrfs_ioctl(struct file *file, unsigned int
  1099. cmd, unsigned long arg)
  1100. {
  1101. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1102. void __user *argp = (void __user *)arg;
  1103. switch (cmd) {
  1104. case FS_IOC_GETFLAGS:
  1105. return btrfs_ioctl_getflags(file, argp);
  1106. case FS_IOC_SETFLAGS:
  1107. return btrfs_ioctl_setflags(file, argp);
  1108. case FS_IOC_GETVERSION:
  1109. return btrfs_ioctl_getversion(file, argp);
  1110. case BTRFS_IOC_SNAP_CREATE:
  1111. return btrfs_ioctl_snap_create(file, argp, 0);
  1112. case BTRFS_IOC_SUBVOL_CREATE:
  1113. return btrfs_ioctl_snap_create(file, argp, 1);
  1114. case BTRFS_IOC_SNAP_DESTROY:
  1115. return btrfs_ioctl_snap_destroy(file, argp);
  1116. case BTRFS_IOC_DEFRAG:
  1117. return btrfs_ioctl_defrag(file);
  1118. case BTRFS_IOC_RESIZE:
  1119. return btrfs_ioctl_resize(root, argp);
  1120. case BTRFS_IOC_ADD_DEV:
  1121. return btrfs_ioctl_add_dev(root, argp);
  1122. case BTRFS_IOC_RM_DEV:
  1123. return btrfs_ioctl_rm_dev(root, argp);
  1124. case BTRFS_IOC_BALANCE:
  1125. return btrfs_balance(root->fs_info->dev_root);
  1126. case BTRFS_IOC_CLONE:
  1127. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  1128. case BTRFS_IOC_CLONE_RANGE:
  1129. return btrfs_ioctl_clone_range(file, argp);
  1130. case BTRFS_IOC_TRANS_START:
  1131. return btrfs_ioctl_trans_start(file);
  1132. case BTRFS_IOC_TRANS_END:
  1133. return btrfs_ioctl_trans_end(file);
  1134. case BTRFS_IOC_SYNC:
  1135. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  1136. return 0;
  1137. }
  1138. return -ENOTTY;
  1139. }