ioctl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mpage.h>
  31. #include <linux/swap.h>
  32. #include <linux/writeback.h>
  33. #include <linux/statfs.h>
  34. #include <linux/compat.h>
  35. #include <linux/bit_spinlock.h>
  36. #include <linux/version.h>
  37. #include <linux/xattr.h>
  38. #include "ctree.h"
  39. #include "disk-io.h"
  40. #include "transaction.h"
  41. #include "btrfs_inode.h"
  42. #include "ioctl.h"
  43. #include "print-tree.h"
  44. #include "volumes.h"
  45. #include "locking.h"
  46. static noinline int create_subvol(struct btrfs_root *root, char *name,
  47. int namelen)
  48. {
  49. struct btrfs_trans_handle *trans;
  50. struct btrfs_key key;
  51. struct btrfs_root_item root_item;
  52. struct btrfs_inode_item *inode_item;
  53. struct extent_buffer *leaf;
  54. struct btrfs_root *new_root = root;
  55. struct inode *dir;
  56. int ret;
  57. int err;
  58. u64 objectid;
  59. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  60. unsigned long nr = 1;
  61. ret = btrfs_check_free_space(root, 1, 0);
  62. if (ret)
  63. goto fail_commit;
  64. trans = btrfs_start_transaction(root, 1);
  65. BUG_ON(!trans);
  66. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  67. 0, &objectid);
  68. if (ret)
  69. goto fail;
  70. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  71. objectid, trans->transid, 0, 0,
  72. 0, 0);
  73. if (IS_ERR(leaf))
  74. return PTR_ERR(leaf);
  75. btrfs_set_header_nritems(leaf, 0);
  76. btrfs_set_header_level(leaf, 0);
  77. btrfs_set_header_bytenr(leaf, leaf->start);
  78. btrfs_set_header_generation(leaf, trans->transid);
  79. btrfs_set_header_owner(leaf, objectid);
  80. write_extent_buffer(leaf, root->fs_info->fsid,
  81. (unsigned long)btrfs_header_fsid(leaf),
  82. BTRFS_FSID_SIZE);
  83. btrfs_mark_buffer_dirty(leaf);
  84. inode_item = &root_item.inode;
  85. memset(inode_item, 0, sizeof(*inode_item));
  86. inode_item->generation = cpu_to_le64(1);
  87. inode_item->size = cpu_to_le64(3);
  88. inode_item->nlink = cpu_to_le32(1);
  89. inode_item->nblocks = cpu_to_le64(1);
  90. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  91. btrfs_set_root_bytenr(&root_item, leaf->start);
  92. btrfs_set_root_level(&root_item, 0);
  93. btrfs_set_root_refs(&root_item, 1);
  94. btrfs_set_root_used(&root_item, 0);
  95. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  96. root_item.drop_level = 0;
  97. btrfs_tree_unlock(leaf);
  98. free_extent_buffer(leaf);
  99. leaf = NULL;
  100. btrfs_set_root_dirid(&root_item, new_dirid);
  101. key.objectid = objectid;
  102. key.offset = 1;
  103. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  104. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  105. &root_item);
  106. if (ret)
  107. goto fail;
  108. /*
  109. * insert the directory item
  110. */
  111. key.offset = (u64)-1;
  112. dir = root->fs_info->sb->s_root->d_inode;
  113. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  114. name, namelen, dir->i_ino, &key,
  115. BTRFS_FT_DIR);
  116. if (ret)
  117. goto fail;
  118. ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
  119. name, namelen, objectid,
  120. root->fs_info->sb->s_root->d_inode->i_ino);
  121. if (ret)
  122. goto fail;
  123. ret = btrfs_commit_transaction(trans, root);
  124. if (ret)
  125. goto fail_commit;
  126. new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
  127. BUG_ON(!new_root);
  128. trans = btrfs_start_transaction(new_root, 1);
  129. BUG_ON(!trans);
  130. ret = btrfs_create_subvol_root(new_root, trans, new_dirid,
  131. BTRFS_I(dir)->block_group);
  132. if (ret)
  133. goto fail;
  134. /* Invalidate existing dcache entry for new subvolume. */
  135. btrfs_invalidate_dcache_root(root, name, namelen);
  136. fail:
  137. nr = trans->blocks_used;
  138. err = btrfs_commit_transaction(trans, new_root);
  139. if (err && !ret)
  140. ret = err;
  141. fail_commit:
  142. btrfs_btree_balance_dirty(root, nr);
  143. return ret;
  144. }
  145. static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
  146. {
  147. struct btrfs_pending_snapshot *pending_snapshot;
  148. struct btrfs_trans_handle *trans;
  149. int ret;
  150. int err;
  151. unsigned long nr = 0;
  152. if (!root->ref_cows)
  153. return -EINVAL;
  154. ret = btrfs_check_free_space(root, 1, 0);
  155. if (ret)
  156. goto fail_unlock;
  157. pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
  158. if (!pending_snapshot) {
  159. ret = -ENOMEM;
  160. goto fail_unlock;
  161. }
  162. pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
  163. if (!pending_snapshot->name) {
  164. ret = -ENOMEM;
  165. kfree(pending_snapshot);
  166. goto fail_unlock;
  167. }
  168. memcpy(pending_snapshot->name, name, namelen);
  169. pending_snapshot->name[namelen] = '\0';
  170. trans = btrfs_start_transaction(root, 1);
  171. BUG_ON(!trans);
  172. pending_snapshot->root = root;
  173. list_add(&pending_snapshot->list,
  174. &trans->transaction->pending_snapshots);
  175. ret = btrfs_update_inode(trans, root, root->inode);
  176. err = btrfs_commit_transaction(trans, root);
  177. fail_unlock:
  178. btrfs_btree_balance_dirty(root, nr);
  179. return ret;
  180. }
  181. int btrfs_defrag_file(struct file *file)
  182. {
  183. struct inode *inode = fdentry(file)->d_inode;
  184. struct btrfs_root *root = BTRFS_I(inode)->root;
  185. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  186. struct page *page;
  187. unsigned long last_index;
  188. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  189. unsigned long total_read = 0;
  190. u64 page_start;
  191. u64 page_end;
  192. unsigned long i;
  193. int ret;
  194. ret = btrfs_check_free_space(root, inode->i_size, 0);
  195. if (ret)
  196. return -ENOSPC;
  197. mutex_lock(&inode->i_mutex);
  198. last_index = inode->i_size >> PAGE_CACHE_SHIFT;
  199. for (i = 0; i <= last_index; i++) {
  200. if (total_read % ra_pages == 0) {
  201. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  202. min(last_index, i + ra_pages - 1));
  203. }
  204. total_read++;
  205. page = grab_cache_page(inode->i_mapping, i);
  206. if (!page)
  207. goto out_unlock;
  208. if (!PageUptodate(page)) {
  209. btrfs_readpage(NULL, page);
  210. lock_page(page);
  211. if (!PageUptodate(page)) {
  212. unlock_page(page);
  213. page_cache_release(page);
  214. goto out_unlock;
  215. }
  216. }
  217. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  218. ClearPageDirty(page);
  219. #else
  220. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  221. #endif
  222. wait_on_page_writeback(page);
  223. set_page_extent_mapped(page);
  224. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  225. page_end = page_start + PAGE_CACHE_SIZE - 1;
  226. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  227. set_extent_delalloc(io_tree, page_start,
  228. page_end, GFP_NOFS);
  229. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  230. set_page_dirty(page);
  231. unlock_page(page);
  232. page_cache_release(page);
  233. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  234. }
  235. out_unlock:
  236. mutex_unlock(&inode->i_mutex);
  237. return 0;
  238. }
  239. /*
  240. * Called inside transaction, so use GFP_NOFS
  241. */
  242. static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
  243. {
  244. u64 new_size;
  245. u64 old_size;
  246. u64 devid = 1;
  247. struct btrfs_ioctl_vol_args *vol_args;
  248. struct btrfs_trans_handle *trans;
  249. struct btrfs_device *device = NULL;
  250. char *sizestr;
  251. char *devstr = NULL;
  252. int ret = 0;
  253. int namelen;
  254. int mod = 0;
  255. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  256. if (!vol_args)
  257. return -ENOMEM;
  258. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  259. ret = -EFAULT;
  260. goto out;
  261. }
  262. namelen = strlen(vol_args->name);
  263. if (namelen > BTRFS_VOL_NAME_MAX) {
  264. ret = -EINVAL;
  265. goto out;
  266. }
  267. mutex_lock(&root->fs_info->alloc_mutex);
  268. mutex_lock(&root->fs_info->chunk_mutex);
  269. sizestr = vol_args->name;
  270. devstr = strchr(sizestr, ':');
  271. if (devstr) {
  272. char *end;
  273. sizestr = devstr + 1;
  274. *devstr = '\0';
  275. devstr = vol_args->name;
  276. devid = simple_strtoull(devstr, &end, 10);
  277. printk(KERN_INFO "resizing devid %llu\n", devid);
  278. }
  279. device = btrfs_find_device(root, devid, NULL);
  280. if (!device) {
  281. printk(KERN_INFO "resizer unable to find device %llu\n", devid);
  282. ret = -EINVAL;
  283. goto out_unlock;
  284. }
  285. if (!strcmp(sizestr, "max"))
  286. new_size = device->bdev->bd_inode->i_size;
  287. else {
  288. if (sizestr[0] == '-') {
  289. mod = -1;
  290. sizestr++;
  291. } else if (sizestr[0] == '+') {
  292. mod = 1;
  293. sizestr++;
  294. }
  295. new_size = btrfs_parse_size(sizestr);
  296. if (new_size == 0) {
  297. ret = -EINVAL;
  298. goto out_unlock;
  299. }
  300. }
  301. old_size = device->total_bytes;
  302. if (mod < 0) {
  303. if (new_size > old_size) {
  304. ret = -EINVAL;
  305. goto out_unlock;
  306. }
  307. new_size = old_size - new_size;
  308. } else if (mod > 0) {
  309. new_size = old_size + new_size;
  310. }
  311. if (new_size < 256 * 1024 * 1024) {
  312. ret = -EINVAL;
  313. goto out_unlock;
  314. }
  315. if (new_size > device->bdev->bd_inode->i_size) {
  316. ret = -EFBIG;
  317. goto out_unlock;
  318. }
  319. do_div(new_size, root->sectorsize);
  320. new_size *= root->sectorsize;
  321. printk(KERN_INFO "new size for %s is %llu\n",
  322. device->name, (unsigned long long)new_size);
  323. if (new_size > old_size) {
  324. trans = btrfs_start_transaction(root, 1);
  325. ret = btrfs_grow_device(trans, device, new_size);
  326. btrfs_commit_transaction(trans, root);
  327. } else {
  328. ret = btrfs_shrink_device(device, new_size);
  329. }
  330. out_unlock:
  331. mutex_lock(&root->fs_info->alloc_mutex);
  332. mutex_lock(&root->fs_info->chunk_mutex);
  333. out:
  334. kfree(vol_args);
  335. return ret;
  336. }
  337. static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
  338. void __user *arg)
  339. {
  340. struct btrfs_ioctl_vol_args *vol_args;
  341. struct btrfs_dir_item *di;
  342. struct btrfs_path *path;
  343. u64 root_dirid;
  344. int namelen;
  345. int ret;
  346. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  347. if (!vol_args)
  348. return -ENOMEM;
  349. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  350. ret = -EFAULT;
  351. goto out;
  352. }
  353. namelen = strlen(vol_args->name);
  354. if (namelen > BTRFS_VOL_NAME_MAX) {
  355. ret = -EINVAL;
  356. goto out;
  357. }
  358. if (strchr(vol_args->name, '/')) {
  359. ret = -EINVAL;
  360. goto out;
  361. }
  362. path = btrfs_alloc_path();
  363. if (!path) {
  364. ret = -ENOMEM;
  365. goto out;
  366. }
  367. root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
  368. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
  369. path, root_dirid,
  370. vol_args->name, namelen, 0);
  371. btrfs_free_path(path);
  372. if (di && !IS_ERR(di)) {
  373. ret = -EEXIST;
  374. goto out;
  375. }
  376. if (IS_ERR(di)) {
  377. ret = PTR_ERR(di);
  378. goto out;
  379. }
  380. mutex_lock(&root->fs_info->drop_mutex);
  381. if (root == root->fs_info->tree_root)
  382. ret = create_subvol(root, vol_args->name, namelen);
  383. else
  384. ret = create_snapshot(root, vol_args->name, namelen);
  385. mutex_unlock(&root->fs_info->drop_mutex);
  386. out:
  387. kfree(vol_args);
  388. return ret;
  389. }
  390. static int btrfs_ioctl_defrag(struct file *file)
  391. {
  392. struct inode *inode = fdentry(file)->d_inode;
  393. struct btrfs_root *root = BTRFS_I(inode)->root;
  394. switch (inode->i_mode & S_IFMT) {
  395. case S_IFDIR:
  396. btrfs_defrag_root(root, 0);
  397. btrfs_defrag_root(root->fs_info->extent_root, 0);
  398. break;
  399. case S_IFREG:
  400. btrfs_defrag_file(file);
  401. break;
  402. }
  403. return 0;
  404. }
  405. long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  406. {
  407. struct btrfs_ioctl_vol_args *vol_args;
  408. int ret;
  409. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  410. if (!vol_args)
  411. return -ENOMEM;
  412. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  413. ret = -EFAULT;
  414. goto out;
  415. }
  416. ret = btrfs_init_new_device(root, vol_args->name);
  417. out:
  418. kfree(vol_args);
  419. return ret;
  420. }
  421. long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  422. {
  423. struct btrfs_ioctl_vol_args *vol_args;
  424. int ret;
  425. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  426. if (!vol_args)
  427. return -ENOMEM;
  428. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  429. ret = -EFAULT;
  430. goto out;
  431. }
  432. ret = btrfs_rm_device(root, vol_args->name);
  433. out:
  434. kfree(vol_args);
  435. return ret;
  436. }
  437. int dup_item_to_inode(struct btrfs_trans_handle *trans,
  438. struct btrfs_root *root,
  439. struct btrfs_path *path,
  440. struct extent_buffer *leaf,
  441. int slot,
  442. struct btrfs_key *key,
  443. u64 destino)
  444. {
  445. char *dup;
  446. int len = btrfs_item_size_nr(leaf, slot);
  447. struct btrfs_key ckey = *key;
  448. int ret = 0;
  449. dup = kmalloc(len, GFP_NOFS);
  450. if (!dup)
  451. return -ENOMEM;
  452. read_extent_buffer(leaf, dup, btrfs_item_ptr_offset(leaf, slot), len);
  453. btrfs_release_path(root, path);
  454. ckey.objectid = destino;
  455. ret = btrfs_insert_item(trans, root, &ckey, dup, len);
  456. kfree(dup);
  457. return ret;
  458. }
  459. long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
  460. {
  461. struct inode *inode = fdentry(file)->d_inode;
  462. struct btrfs_root *root = BTRFS_I(inode)->root;
  463. struct file *src_file;
  464. struct inode *src;
  465. struct btrfs_trans_handle *trans;
  466. int ret;
  467. u64 pos;
  468. struct btrfs_path *path;
  469. struct btrfs_key key;
  470. struct extent_buffer *leaf;
  471. u32 nritems;
  472. int slot;
  473. src_file = fget(src_fd);
  474. if (!src_file)
  475. return -EBADF;
  476. src = src_file->f_dentry->d_inode;
  477. ret = -EXDEV;
  478. if (src->i_sb != inode->i_sb)
  479. goto out_fput;
  480. if (inode < src) {
  481. mutex_lock(&inode->i_mutex);
  482. mutex_lock(&src->i_mutex);
  483. } else {
  484. mutex_lock(&src->i_mutex);
  485. mutex_lock(&inode->i_mutex);
  486. }
  487. ret = -ENOTEMPTY;
  488. if (inode->i_size)
  489. goto out_unlock;
  490. /* do any pending delalloc/csum calc on src, one way or
  491. another, and lock file content */
  492. while (1) {
  493. filemap_write_and_wait(src->i_mapping);
  494. lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
  495. if (BTRFS_I(src)->delalloc_bytes == 0)
  496. break;
  497. unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
  498. }
  499. trans = btrfs_start_transaction(root, 0);
  500. path = btrfs_alloc_path();
  501. if (!path) {
  502. ret = -ENOMEM;
  503. goto out;
  504. }
  505. key.offset = 0;
  506. key.type = BTRFS_EXTENT_DATA_KEY;
  507. key.objectid = src->i_ino;
  508. pos = 0;
  509. path->reada = 2;
  510. while (1) {
  511. /*
  512. * note the key will change type as we walk through the
  513. * tree.
  514. */
  515. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  516. if (ret < 0)
  517. goto out;
  518. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  519. ret = btrfs_next_leaf(root, path);
  520. if (ret < 0)
  521. goto out;
  522. if (ret > 0)
  523. break;
  524. }
  525. leaf = path->nodes[0];
  526. slot = path->slots[0];
  527. btrfs_item_key_to_cpu(leaf, &key, slot);
  528. nritems = btrfs_header_nritems(leaf);
  529. if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
  530. key.objectid != src->i_ino)
  531. break;
  532. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  533. struct btrfs_file_extent_item *extent;
  534. int found_type;
  535. pos = key.offset;
  536. extent = btrfs_item_ptr(leaf, slot,
  537. struct btrfs_file_extent_item);
  538. found_type = btrfs_file_extent_type(leaf, extent);
  539. if (found_type == BTRFS_FILE_EXTENT_REG) {
  540. u64 len = btrfs_file_extent_num_bytes(leaf,
  541. extent);
  542. u64 ds = btrfs_file_extent_disk_bytenr(leaf,
  543. extent);
  544. u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
  545. extent);
  546. u64 off = btrfs_file_extent_offset(leaf,
  547. extent);
  548. btrfs_insert_file_extent(trans, root,
  549. inode->i_ino, pos,
  550. ds, dl, len, off);
  551. /* ds == 0 means there's a hole */
  552. if (ds != 0) {
  553. btrfs_inc_extent_ref(trans, root,
  554. ds, dl,
  555. root->root_key.objectid,
  556. trans->transid,
  557. inode->i_ino, pos);
  558. }
  559. pos = key.offset + len;
  560. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  561. ret = dup_item_to_inode(trans, root, path,
  562. leaf, slot, &key,
  563. inode->i_ino);
  564. if (ret)
  565. goto out;
  566. pos = key.offset + btrfs_item_size_nr(leaf,
  567. slot);
  568. }
  569. } else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
  570. ret = dup_item_to_inode(trans, root, path, leaf,
  571. slot, &key, inode->i_ino);
  572. if (ret)
  573. goto out;
  574. }
  575. key.offset++;
  576. btrfs_release_path(root, path);
  577. }
  578. ret = 0;
  579. out:
  580. btrfs_free_path(path);
  581. inode->i_blocks = src->i_blocks;
  582. i_size_write(inode, src->i_size);
  583. btrfs_update_inode(trans, root, inode);
  584. unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
  585. btrfs_end_transaction(trans, root);
  586. out_unlock:
  587. mutex_unlock(&src->i_mutex);
  588. mutex_unlock(&inode->i_mutex);
  589. out_fput:
  590. fput(src_file);
  591. return ret;
  592. }
  593. /*
  594. * there are many ways the trans_start and trans_end ioctls can lead
  595. * to deadlocks. They should only be used by applications that
  596. * basically own the machine, and have a very in depth understanding
  597. * of all the possible deadlocks and enospc problems.
  598. */
  599. long btrfs_ioctl_trans_start(struct file *file)
  600. {
  601. struct inode *inode = fdentry(file)->d_inode;
  602. struct btrfs_root *root = BTRFS_I(inode)->root;
  603. struct btrfs_trans_handle *trans;
  604. int ret = 0;
  605. if (!capable(CAP_SYS_ADMIN))
  606. return -EPERM;
  607. if (file->private_data) {
  608. ret = -EINPROGRESS;
  609. goto out;
  610. }
  611. trans = btrfs_start_transaction(root, 0);
  612. if (trans)
  613. file->private_data = trans;
  614. else
  615. ret = -ENOMEM;
  616. /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
  617. out:
  618. return ret;
  619. }
  620. /*
  621. * there are many ways the trans_start and trans_end ioctls can lead
  622. * to deadlocks. They should only be used by applications that
  623. * basically own the machine, and have a very in depth understanding
  624. * of all the possible deadlocks and enospc problems.
  625. */
  626. long btrfs_ioctl_trans_end(struct file *file)
  627. {
  628. struct inode *inode = fdentry(file)->d_inode;
  629. struct btrfs_root *root = BTRFS_I(inode)->root;
  630. struct btrfs_trans_handle *trans;
  631. int ret = 0;
  632. trans = file->private_data;
  633. if (!trans) {
  634. ret = -EINVAL;
  635. goto out;
  636. }
  637. btrfs_end_transaction(trans, root);
  638. file->private_data = 0;
  639. out:
  640. return ret;
  641. }
  642. long btrfs_ioctl(struct file *file, unsigned int
  643. cmd, unsigned long arg)
  644. {
  645. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  646. switch (cmd) {
  647. case BTRFS_IOC_SNAP_CREATE:
  648. return btrfs_ioctl_snap_create(root, (void __user *)arg);
  649. case BTRFS_IOC_DEFRAG:
  650. return btrfs_ioctl_defrag(file);
  651. case BTRFS_IOC_RESIZE:
  652. return btrfs_ioctl_resize(root, (void __user *)arg);
  653. case BTRFS_IOC_ADD_DEV:
  654. return btrfs_ioctl_add_dev(root, (void __user *)arg);
  655. case BTRFS_IOC_RM_DEV:
  656. return btrfs_ioctl_rm_dev(root, (void __user *)arg);
  657. case BTRFS_IOC_BALANCE:
  658. return btrfs_balance(root->fs_info->dev_root);
  659. case BTRFS_IOC_CLONE:
  660. return btrfs_ioctl_clone(file, arg);
  661. case BTRFS_IOC_TRANS_START:
  662. return btrfs_ioctl_trans_start(file);
  663. case BTRFS_IOC_TRANS_END:
  664. return btrfs_ioctl_trans_end(file);
  665. case BTRFS_IOC_SYNC:
  666. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  667. return 0;
  668. }
  669. return -ENOTTY;
  670. }