ioctl.c 32 KB

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