ioctl.c 21 KB

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