ioctl.c 19 KB

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