ioctl.c 19 KB

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