ioctl.c 21 KB

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