ioctl.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. if (root->fs_info->sb->s_flags & MS_RDONLY)
  331. return -EROFS;
  332. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  333. if (!vol_args)
  334. return -ENOMEM;
  335. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  336. ret = -EFAULT;
  337. goto out;
  338. }
  339. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  340. namelen = strlen(vol_args->name);
  341. mutex_lock(&root->fs_info->volume_mutex);
  342. sizestr = vol_args->name;
  343. devstr = strchr(sizestr, ':');
  344. if (devstr) {
  345. char *end;
  346. sizestr = devstr + 1;
  347. *devstr = '\0';
  348. devstr = vol_args->name;
  349. devid = simple_strtoull(devstr, &end, 10);
  350. printk(KERN_INFO "resizing devid %llu\n", devid);
  351. }
  352. device = btrfs_find_device(root, devid, NULL);
  353. if (!device) {
  354. printk(KERN_INFO "resizer unable to find device %llu\n", devid);
  355. ret = -EINVAL;
  356. goto out_unlock;
  357. }
  358. if (!strcmp(sizestr, "max"))
  359. new_size = device->bdev->bd_inode->i_size;
  360. else {
  361. if (sizestr[0] == '-') {
  362. mod = -1;
  363. sizestr++;
  364. } else if (sizestr[0] == '+') {
  365. mod = 1;
  366. sizestr++;
  367. }
  368. new_size = btrfs_parse_size(sizestr);
  369. if (new_size == 0) {
  370. ret = -EINVAL;
  371. goto out_unlock;
  372. }
  373. }
  374. old_size = device->total_bytes;
  375. if (mod < 0) {
  376. if (new_size > old_size) {
  377. ret = -EINVAL;
  378. goto out_unlock;
  379. }
  380. new_size = old_size - new_size;
  381. } else if (mod > 0) {
  382. new_size = old_size + new_size;
  383. }
  384. if (new_size < 256 * 1024 * 1024) {
  385. ret = -EINVAL;
  386. goto out_unlock;
  387. }
  388. if (new_size > device->bdev->bd_inode->i_size) {
  389. ret = -EFBIG;
  390. goto out_unlock;
  391. }
  392. do_div(new_size, root->sectorsize);
  393. new_size *= root->sectorsize;
  394. printk(KERN_INFO "new size for %s is %llu\n",
  395. device->name, (unsigned long long)new_size);
  396. if (new_size > old_size) {
  397. trans = btrfs_start_transaction(root, 1);
  398. ret = btrfs_grow_device(trans, device, new_size);
  399. btrfs_commit_transaction(trans, root);
  400. } else {
  401. ret = btrfs_shrink_device(device, new_size);
  402. }
  403. out_unlock:
  404. mutex_unlock(&root->fs_info->volume_mutex);
  405. out:
  406. kfree(vol_args);
  407. return ret;
  408. }
  409. static noinline int btrfs_ioctl_snap_create(struct file *file,
  410. void __user *arg)
  411. {
  412. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  413. struct btrfs_ioctl_vol_args *vol_args;
  414. struct btrfs_dir_item *di;
  415. struct btrfs_path *path;
  416. u64 root_dirid;
  417. int namelen;
  418. int ret;
  419. if (root->fs_info->sb->s_flags & MS_RDONLY)
  420. return -EROFS;
  421. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  422. if (!vol_args)
  423. return -ENOMEM;
  424. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  425. ret = -EFAULT;
  426. goto out;
  427. }
  428. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  429. namelen = strlen(vol_args->name);
  430. if (strchr(vol_args->name, '/')) {
  431. ret = -EINVAL;
  432. goto out;
  433. }
  434. path = btrfs_alloc_path();
  435. if (!path) {
  436. ret = -ENOMEM;
  437. goto out;
  438. }
  439. root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
  440. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
  441. path, root_dirid,
  442. vol_args->name, namelen, 0);
  443. btrfs_free_path(path);
  444. if (di && !IS_ERR(di)) {
  445. ret = -EEXIST;
  446. goto out;
  447. }
  448. if (IS_ERR(di)) {
  449. ret = PTR_ERR(di);
  450. goto out;
  451. }
  452. if (root == root->fs_info->tree_root) {
  453. ret = btrfs_mksubvol(&file->f_path, vol_args->name,
  454. file->f_path.dentry->d_inode->i_mode,
  455. namelen);
  456. } else {
  457. ret = create_snapshot(root, vol_args->name, namelen);
  458. }
  459. out:
  460. kfree(vol_args);
  461. return ret;
  462. }
  463. static int btrfs_ioctl_defrag(struct file *file)
  464. {
  465. struct inode *inode = fdentry(file)->d_inode;
  466. struct btrfs_root *root = BTRFS_I(inode)->root;
  467. int ret;
  468. ret = mnt_want_write(file->f_path.mnt);
  469. if (ret)
  470. return ret;
  471. switch (inode->i_mode & S_IFMT) {
  472. case S_IFDIR:
  473. btrfs_defrag_root(root, 0);
  474. btrfs_defrag_root(root->fs_info->extent_root, 0);
  475. break;
  476. case S_IFREG:
  477. btrfs_defrag_file(file);
  478. break;
  479. }
  480. return 0;
  481. }
  482. long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  483. {
  484. struct btrfs_ioctl_vol_args *vol_args;
  485. int ret;
  486. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  487. if (!vol_args)
  488. return -ENOMEM;
  489. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  490. ret = -EFAULT;
  491. goto out;
  492. }
  493. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  494. ret = btrfs_init_new_device(root, vol_args->name);
  495. out:
  496. kfree(vol_args);
  497. return ret;
  498. }
  499. long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  500. {
  501. struct btrfs_ioctl_vol_args *vol_args;
  502. int ret;
  503. if (root->fs_info->sb->s_flags & MS_RDONLY)
  504. return -EROFS;
  505. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  506. if (!vol_args)
  507. return -ENOMEM;
  508. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  509. ret = -EFAULT;
  510. goto out;
  511. }
  512. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  513. ret = btrfs_rm_device(root, vol_args->name);
  514. out:
  515. kfree(vol_args);
  516. return ret;
  517. }
  518. long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, u64 off,
  519. u64 olen, u64 destoff)
  520. {
  521. struct inode *inode = fdentry(file)->d_inode;
  522. struct btrfs_root *root = BTRFS_I(inode)->root;
  523. struct file *src_file;
  524. struct inode *src;
  525. struct btrfs_trans_handle *trans;
  526. struct btrfs_path *path;
  527. struct extent_buffer *leaf;
  528. char *buf;
  529. struct btrfs_key key;
  530. u32 nritems;
  531. int slot;
  532. int ret;
  533. u64 len = olen;
  534. u64 bs = root->fs_info->sb->s_blocksize;
  535. u64 hint_byte;
  536. /*
  537. * TODO:
  538. * - split compressed inline extents. annoying: we need to
  539. * decompress into destination's address_space (the file offset
  540. * may change, so source mapping won't do), then recompress (or
  541. * otherwise reinsert) a subrange.
  542. * - allow ranges within the same file to be cloned (provided
  543. * they don't overlap)?
  544. */
  545. ret = mnt_want_write(file->f_path.mnt);
  546. if (ret)
  547. return ret;
  548. src_file = fget(srcfd);
  549. if (!src_file)
  550. return -EBADF;
  551. src = src_file->f_dentry->d_inode;
  552. ret = -EINVAL;
  553. if (src == inode)
  554. goto out_fput;
  555. ret = -EISDIR;
  556. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  557. goto out_fput;
  558. ret = -EXDEV;
  559. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  560. goto out_fput;
  561. ret = -ENOMEM;
  562. buf = vmalloc(btrfs_level_size(root, 0));
  563. if (!buf)
  564. goto out_fput;
  565. path = btrfs_alloc_path();
  566. if (!path) {
  567. vfree(buf);
  568. goto out_fput;
  569. }
  570. path->reada = 2;
  571. if (inode < src) {
  572. mutex_lock(&inode->i_mutex);
  573. mutex_lock(&src->i_mutex);
  574. } else {
  575. mutex_lock(&src->i_mutex);
  576. mutex_lock(&inode->i_mutex);
  577. }
  578. /* determine range to clone */
  579. ret = -EINVAL;
  580. if (off >= src->i_size || off + len > src->i_size)
  581. goto out_unlock;
  582. if (len == 0)
  583. olen = len = src->i_size - off;
  584. /* if we extend to eof, continue to block boundary */
  585. if (off + len == src->i_size)
  586. len = ((src->i_size + bs-1) & ~(bs-1))
  587. - off;
  588. /* verify the end result is block aligned */
  589. if ((off & (bs-1)) ||
  590. ((off + len) & (bs-1)))
  591. goto out_unlock;
  592. printk("final src extent is %llu~%llu\n", off, len);
  593. printk("final dst extent is %llu~%llu\n", destoff, len);
  594. /* do any pending delalloc/csum calc on src, one way or
  595. another, and lock file content */
  596. while (1) {
  597. struct btrfs_ordered_extent *ordered;
  598. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  599. ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
  600. if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
  601. break;
  602. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  603. if (ordered)
  604. btrfs_put_ordered_extent(ordered);
  605. btrfs_wait_ordered_range(src, off, off+len);
  606. }
  607. trans = btrfs_start_transaction(root, 1);
  608. BUG_ON(!trans);
  609. /* punch hole in destination first */
  610. btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte);
  611. /* clone data */
  612. key.objectid = src->i_ino;
  613. key.type = BTRFS_EXTENT_DATA_KEY;
  614. key.offset = 0;
  615. while (1) {
  616. /*
  617. * note the key will change type as we walk through the
  618. * tree.
  619. */
  620. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  621. if (ret < 0)
  622. goto out;
  623. nritems = btrfs_header_nritems(path->nodes[0]);
  624. if (path->slots[0] >= nritems) {
  625. ret = btrfs_next_leaf(root, path);
  626. if (ret < 0)
  627. goto out;
  628. if (ret > 0)
  629. break;
  630. nritems = btrfs_header_nritems(path->nodes[0]);
  631. }
  632. leaf = path->nodes[0];
  633. slot = path->slots[0];
  634. btrfs_item_key_to_cpu(leaf, &key, slot);
  635. if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
  636. key.objectid != src->i_ino)
  637. break;
  638. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  639. struct btrfs_file_extent_item *extent;
  640. int type;
  641. u32 size;
  642. struct btrfs_key new_key;
  643. u64 disko = 0, diskl = 0;
  644. u64 datao = 0, datal = 0;
  645. u8 comp;
  646. size = btrfs_item_size_nr(leaf, slot);
  647. read_extent_buffer(leaf, buf,
  648. btrfs_item_ptr_offset(leaf, slot),
  649. size);
  650. extent = btrfs_item_ptr(leaf, slot,
  651. struct btrfs_file_extent_item);
  652. comp = btrfs_file_extent_compression(leaf, extent);
  653. type = btrfs_file_extent_type(leaf, extent);
  654. if (type == BTRFS_FILE_EXTENT_REG) {
  655. disko = btrfs_file_extent_disk_bytenr(leaf, extent);
  656. diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
  657. datao = btrfs_file_extent_offset(leaf, extent);
  658. datal = btrfs_file_extent_num_bytes(leaf, extent);
  659. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  660. /* take upper bound, may be compressed */
  661. datal = btrfs_file_extent_ram_bytes(leaf,
  662. extent);
  663. }
  664. btrfs_release_path(root, path);
  665. if (key.offset + datal < off ||
  666. key.offset >= off+len)
  667. goto next;
  668. memcpy(&new_key, &key, sizeof(new_key));
  669. new_key.objectid = inode->i_ino;
  670. new_key.offset = key.offset + destoff - off;
  671. if (type == BTRFS_FILE_EXTENT_REG) {
  672. ret = btrfs_insert_empty_item(trans, root, path,
  673. &new_key, size);
  674. if (ret)
  675. goto out;
  676. leaf = path->nodes[0];
  677. slot = path->slots[0];
  678. write_extent_buffer(leaf, buf,
  679. btrfs_item_ptr_offset(leaf, slot),
  680. size);
  681. extent = btrfs_item_ptr(leaf, slot,
  682. struct btrfs_file_extent_item);
  683. printk(" orig disk %llu~%llu data %llu~%llu\n",
  684. disko, diskl, datao, datal);
  685. if (off > key.offset) {
  686. datao += off - key.offset;
  687. datal -= off - key.offset;
  688. }
  689. if (key.offset + datao + datal + key.offset >
  690. off + len)
  691. datal = off + len - key.offset - datao;
  692. /* disko == 0 means it's a hole */
  693. if (!disko)
  694. datao = 0;
  695. printk(" final disk %llu~%llu data %llu~%llu\n",
  696. disko, diskl, datao, datal);
  697. btrfs_set_file_extent_offset(leaf, extent,
  698. datao);
  699. btrfs_set_file_extent_num_bytes(leaf, extent,
  700. datal);
  701. if (disko) {
  702. inode_add_bytes(inode, datal);
  703. ret = btrfs_inc_extent_ref(trans, root,
  704. disko, diskl, leaf->start,
  705. root->root_key.objectid,
  706. trans->transid,
  707. inode->i_ino);
  708. BUG_ON(ret);
  709. }
  710. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  711. u64 skip = 0;
  712. u64 trim = 0;
  713. if (off > key.offset) {
  714. skip = off - key.offset;
  715. new_key.offset += skip;
  716. }
  717. if (key.offset + datal > off+len)
  718. trim = key.offset + datal - (off+len);
  719. printk("len %lld skip %lld trim %lld\n",
  720. datal, skip, trim);
  721. if (comp && (skip || trim)) {
  722. printk("btrfs clone_range can't split compressed inline extents yet\n");
  723. ret = -EINVAL;
  724. goto out;
  725. }
  726. size -= skip + trim;
  727. datal -= skip + trim;
  728. ret = btrfs_insert_empty_item(trans, root, path,
  729. &new_key, size);
  730. if (ret)
  731. goto out;
  732. if (skip) {
  733. u32 start = btrfs_file_extent_calc_inline_size(0);
  734. memmove(buf+start, buf+start+skip,
  735. datal);
  736. }
  737. leaf = path->nodes[0];
  738. slot = path->slots[0];
  739. write_extent_buffer(leaf, buf,
  740. btrfs_item_ptr_offset(leaf, slot),
  741. size);
  742. inode_add_bytes(inode, datal);
  743. }
  744. btrfs_mark_buffer_dirty(leaf);
  745. }
  746. if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
  747. u32 size;
  748. struct btrfs_key new_key;
  749. u64 coverslen;
  750. int coff, clen;
  751. size = btrfs_item_size_nr(leaf, slot);
  752. coverslen = (size / BTRFS_CRC32_SIZE) <<
  753. root->fs_info->sb->s_blocksize_bits;
  754. printk("csums for %llu~%llu\n",
  755. key.offset, coverslen);
  756. if (key.offset + coverslen < off ||
  757. key.offset >= off+len)
  758. goto next;
  759. read_extent_buffer(leaf, buf,
  760. btrfs_item_ptr_offset(leaf, slot),
  761. size);
  762. btrfs_release_path(root, path);
  763. coff = 0;
  764. if (off > key.offset)
  765. coff = ((off - key.offset) >>
  766. root->fs_info->sb->s_blocksize_bits) *
  767. BTRFS_CRC32_SIZE;
  768. clen = size - coff;
  769. if (key.offset + coverslen > off+len)
  770. clen -= ((key.offset+coverslen-off-len) >>
  771. root->fs_info->sb->s_blocksize_bits) *
  772. BTRFS_CRC32_SIZE;
  773. printk(" will dup %d~%d of %d\n",
  774. coff, clen, size);
  775. memcpy(&new_key, &key, sizeof(new_key));
  776. new_key.objectid = inode->i_ino;
  777. new_key.offset = key.offset + destoff - off;
  778. ret = btrfs_insert_empty_item(trans, root, path,
  779. &new_key, clen);
  780. if (ret)
  781. goto out;
  782. leaf = path->nodes[0];
  783. slot = path->slots[0];
  784. write_extent_buffer(leaf, buf + coff,
  785. btrfs_item_ptr_offset(leaf, slot),
  786. clen);
  787. btrfs_mark_buffer_dirty(leaf);
  788. }
  789. next:
  790. btrfs_release_path(root, path);
  791. key.offset++;
  792. }
  793. ret = 0;
  794. out:
  795. btrfs_release_path(root, path);
  796. if (ret == 0) {
  797. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  798. if (destoff + olen > inode->i_size)
  799. btrfs_i_size_write(inode, destoff + olen);
  800. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  801. ret = btrfs_update_inode(trans, root, inode);
  802. }
  803. btrfs_end_transaction(trans, root);
  804. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  805. if (ret)
  806. vmtruncate(inode, 0);
  807. out_unlock:
  808. mutex_unlock(&src->i_mutex);
  809. mutex_unlock(&inode->i_mutex);
  810. vfree(buf);
  811. btrfs_free_path(path);
  812. out_fput:
  813. fput(src_file);
  814. return ret;
  815. }
  816. long btrfs_ioctl_clone_range(struct file *file, unsigned long argptr)
  817. {
  818. struct btrfs_ioctl_clone_range_args args;
  819. if (copy_from_user(&args, (void *)argptr, sizeof(args)))
  820. return -EFAULT;
  821. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  822. args.src_length, args.dest_offset);
  823. }
  824. /*
  825. * there are many ways the trans_start and trans_end ioctls can lead
  826. * to deadlocks. They should only be used by applications that
  827. * basically own the machine, and have a very in depth understanding
  828. * of all the possible deadlocks and enospc problems.
  829. */
  830. long btrfs_ioctl_trans_start(struct file *file)
  831. {
  832. struct inode *inode = fdentry(file)->d_inode;
  833. struct btrfs_root *root = BTRFS_I(inode)->root;
  834. struct btrfs_trans_handle *trans;
  835. int ret = 0;
  836. if (!capable(CAP_SYS_ADMIN))
  837. return -EPERM;
  838. if (file->private_data) {
  839. ret = -EINPROGRESS;
  840. goto out;
  841. }
  842. ret = mnt_want_write(file->f_path.mnt);
  843. if (ret)
  844. goto out;
  845. mutex_lock(&root->fs_info->trans_mutex);
  846. root->fs_info->open_ioctl_trans++;
  847. mutex_unlock(&root->fs_info->trans_mutex);
  848. trans = btrfs_start_ioctl_transaction(root, 0);
  849. if (trans)
  850. file->private_data = trans;
  851. else
  852. ret = -ENOMEM;
  853. /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
  854. out:
  855. return ret;
  856. }
  857. /*
  858. * there are many ways the trans_start and trans_end ioctls can lead
  859. * to deadlocks. They should only be used by applications that
  860. * basically own the machine, and have a very in depth understanding
  861. * of all the possible deadlocks and enospc problems.
  862. */
  863. long btrfs_ioctl_trans_end(struct file *file)
  864. {
  865. struct inode *inode = fdentry(file)->d_inode;
  866. struct btrfs_root *root = BTRFS_I(inode)->root;
  867. struct btrfs_trans_handle *trans;
  868. int ret = 0;
  869. trans = file->private_data;
  870. if (!trans) {
  871. ret = -EINVAL;
  872. goto out;
  873. }
  874. btrfs_end_transaction(trans, root);
  875. file->private_data = NULL;
  876. mutex_lock(&root->fs_info->trans_mutex);
  877. root->fs_info->open_ioctl_trans--;
  878. mutex_unlock(&root->fs_info->trans_mutex);
  879. out:
  880. return ret;
  881. }
  882. long btrfs_ioctl(struct file *file, unsigned int
  883. cmd, unsigned long arg)
  884. {
  885. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  886. switch (cmd) {
  887. case BTRFS_IOC_SNAP_CREATE:
  888. return btrfs_ioctl_snap_create(file, (void __user *)arg);
  889. case BTRFS_IOC_DEFRAG:
  890. return btrfs_ioctl_defrag(file);
  891. case BTRFS_IOC_RESIZE:
  892. return btrfs_ioctl_resize(root, (void __user *)arg);
  893. case BTRFS_IOC_ADD_DEV:
  894. return btrfs_ioctl_add_dev(root, (void __user *)arg);
  895. case BTRFS_IOC_RM_DEV:
  896. return btrfs_ioctl_rm_dev(root, (void __user *)arg);
  897. case BTRFS_IOC_BALANCE:
  898. return btrfs_balance(root->fs_info->dev_root);
  899. case BTRFS_IOC_CLONE:
  900. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  901. case BTRFS_IOC_CLONE_RANGE:
  902. return btrfs_ioctl_clone_range(file, arg);
  903. case BTRFS_IOC_TRANS_START:
  904. return btrfs_ioctl_trans_start(file);
  905. case BTRFS_IOC_TRANS_END:
  906. return btrfs_ioctl_trans_end(file);
  907. case BTRFS_IOC_SYNC:
  908. btrfs_start_delalloc_inodes(root);
  909. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  910. return 0;
  911. }
  912. return -ENOTTY;
  913. }