ioctl.c 32 KB

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