ioctl.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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/xattr.h>
  41. #include <linux/vmalloc.h>
  42. #include "compat.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. u64 index = 0;
  67. unsigned long nr = 1;
  68. ret = btrfs_check_metadata_free_space(root);
  69. if (ret)
  70. goto fail_commit;
  71. trans = btrfs_start_transaction(root, 1);
  72. BUG_ON(!trans);
  73. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  74. 0, &objectid);
  75. if (ret)
  76. goto fail;
  77. leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
  78. objectid, trans->transid, 0, 0, 0);
  79. if (IS_ERR(leaf)) {
  80. ret = PTR_ERR(leaf);
  81. goto fail;
  82. }
  83. btrfs_set_header_nritems(leaf, 0);
  84. btrfs_set_header_level(leaf, 0);
  85. btrfs_set_header_bytenr(leaf, leaf->start);
  86. btrfs_set_header_generation(leaf, trans->transid);
  87. btrfs_set_header_owner(leaf, objectid);
  88. write_extent_buffer(leaf, root->fs_info->fsid,
  89. (unsigned long)btrfs_header_fsid(leaf),
  90. BTRFS_FSID_SIZE);
  91. btrfs_mark_buffer_dirty(leaf);
  92. inode_item = &root_item.inode;
  93. memset(inode_item, 0, sizeof(*inode_item));
  94. inode_item->generation = cpu_to_le64(1);
  95. inode_item->size = cpu_to_le64(3);
  96. inode_item->nlink = cpu_to_le32(1);
  97. inode_item->nbytes = cpu_to_le64(root->leafsize);
  98. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  99. btrfs_set_root_bytenr(&root_item, leaf->start);
  100. btrfs_set_root_generation(&root_item, trans->transid);
  101. btrfs_set_root_level(&root_item, 0);
  102. btrfs_set_root_refs(&root_item, 1);
  103. btrfs_set_root_used(&root_item, 0);
  104. btrfs_set_root_last_snapshot(&root_item, 0);
  105. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  106. root_item.drop_level = 0;
  107. btrfs_tree_unlock(leaf);
  108. free_extent_buffer(leaf);
  109. leaf = NULL;
  110. btrfs_set_root_dirid(&root_item, new_dirid);
  111. key.objectid = objectid;
  112. key.offset = 1;
  113. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  114. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  115. &root_item);
  116. if (ret)
  117. goto fail;
  118. /*
  119. * insert the directory item
  120. */
  121. key.offset = (u64)-1;
  122. dir = dentry->d_parent->d_inode;
  123. ret = btrfs_set_inode_index(dir, &index);
  124. BUG_ON(ret);
  125. ret = btrfs_insert_dir_item(trans, root,
  126. name, namelen, dir->i_ino, &key,
  127. BTRFS_FT_DIR, index);
  128. if (ret)
  129. goto fail;
  130. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  131. ret = btrfs_update_inode(trans, root, dir);
  132. BUG_ON(ret);
  133. /* add the backref first */
  134. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  135. objectid, BTRFS_ROOT_BACKREF_KEY,
  136. root->root_key.objectid,
  137. dir->i_ino, index, name, namelen);
  138. BUG_ON(ret);
  139. /* now add the forward ref */
  140. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  141. root->root_key.objectid, BTRFS_ROOT_REF_KEY,
  142. objectid,
  143. dir->i_ino, index, name, namelen);
  144. BUG_ON(ret);
  145. ret = btrfs_commit_transaction(trans, root);
  146. if (ret)
  147. goto fail_commit;
  148. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  149. BUG_ON(!new_root);
  150. trans = btrfs_start_transaction(new_root, 1);
  151. BUG_ON(!trans);
  152. ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
  153. BTRFS_I(dir)->block_group);
  154. if (ret)
  155. goto fail;
  156. fail:
  157. nr = trans->blocks_used;
  158. err = btrfs_commit_transaction(trans, new_root);
  159. if (err && !ret)
  160. ret = err;
  161. fail_commit:
  162. btrfs_btree_balance_dirty(root, nr);
  163. return ret;
  164. }
  165. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  166. char *name, int namelen)
  167. {
  168. struct btrfs_pending_snapshot *pending_snapshot;
  169. struct btrfs_trans_handle *trans;
  170. int ret = 0;
  171. int err;
  172. unsigned long nr = 0;
  173. if (!root->ref_cows)
  174. return -EINVAL;
  175. ret = btrfs_check_metadata_free_space(root);
  176. if (ret)
  177. goto fail_unlock;
  178. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  179. if (!pending_snapshot) {
  180. ret = -ENOMEM;
  181. goto fail_unlock;
  182. }
  183. pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
  184. if (!pending_snapshot->name) {
  185. ret = -ENOMEM;
  186. kfree(pending_snapshot);
  187. goto fail_unlock;
  188. }
  189. memcpy(pending_snapshot->name, name, namelen);
  190. pending_snapshot->name[namelen] = '\0';
  191. pending_snapshot->dentry = dentry;
  192. trans = btrfs_start_transaction(root, 1);
  193. BUG_ON(!trans);
  194. pending_snapshot->root = root;
  195. list_add(&pending_snapshot->list,
  196. &trans->transaction->pending_snapshots);
  197. err = btrfs_commit_transaction(trans, root);
  198. fail_unlock:
  199. btrfs_btree_balance_dirty(root, nr);
  200. return ret;
  201. }
  202. /* copy of may_create in fs/namei.c() */
  203. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  204. {
  205. if (child->d_inode)
  206. return -EEXIST;
  207. if (IS_DEADDIR(dir))
  208. return -ENOENT;
  209. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  210. }
  211. /*
  212. * Create a new subvolume below @parent. This is largely modeled after
  213. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  214. * inside this filesystem so it's quite a bit simpler.
  215. */
  216. static noinline int btrfs_mksubvol(struct path *parent, char *name,
  217. int mode, int namelen,
  218. struct btrfs_root *snap_src)
  219. {
  220. struct dentry *dentry;
  221. int error;
  222. mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  223. dentry = lookup_one_len(name, parent->dentry, namelen);
  224. error = PTR_ERR(dentry);
  225. if (IS_ERR(dentry))
  226. goto out_unlock;
  227. error = -EEXIST;
  228. if (dentry->d_inode)
  229. goto out_dput;
  230. if (!IS_POSIXACL(parent->dentry->d_inode))
  231. mode &= ~current_umask();
  232. error = mnt_want_write(parent->mnt);
  233. if (error)
  234. goto out_dput;
  235. error = btrfs_may_create(parent->dentry->d_inode, dentry);
  236. if (error)
  237. goto out_drop_write;
  238. /*
  239. * Actually perform the low-level subvolume creation after all
  240. * this VFS fuzz.
  241. *
  242. * Eventually we want to pass in an inode under which we create this
  243. * subvolume, but for now all are under the filesystem root.
  244. *
  245. * Also we should pass on the mode eventually to allow creating new
  246. * subvolume with specific mode bits.
  247. */
  248. if (snap_src) {
  249. struct dentry *dir = dentry->d_parent;
  250. struct dentry *test = dir->d_parent;
  251. struct btrfs_path *path = btrfs_alloc_path();
  252. int ret;
  253. u64 test_oid;
  254. u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
  255. test_oid = snap_src->root_key.objectid;
  256. ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
  257. path, parent_oid, test_oid);
  258. if (ret == 0)
  259. goto create;
  260. btrfs_release_path(snap_src->fs_info->tree_root, path);
  261. /* we need to make sure we aren't creating a directory loop
  262. * by taking a snapshot of something that has our current
  263. * subvol in its directory tree. So, this loops through
  264. * the dentries and checks the forward refs for each subvolume
  265. * to see if is references the subvolume where we are
  266. * placing this new snapshot.
  267. */
  268. while (1) {
  269. if (!test ||
  270. dir == snap_src->fs_info->sb->s_root ||
  271. test == snap_src->fs_info->sb->s_root ||
  272. test->d_inode->i_sb != snap_src->fs_info->sb) {
  273. break;
  274. }
  275. if (S_ISLNK(test->d_inode->i_mode)) {
  276. printk(KERN_INFO "Btrfs symlink in snapshot "
  277. "path, failed\n");
  278. error = -EMLINK;
  279. btrfs_free_path(path);
  280. goto out_drop_write;
  281. }
  282. test_oid =
  283. BTRFS_I(test->d_inode)->root->root_key.objectid;
  284. ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
  285. path, test_oid, parent_oid);
  286. if (ret == 0) {
  287. printk(KERN_INFO "Btrfs snapshot creation "
  288. "failed, looping\n");
  289. error = -EMLINK;
  290. btrfs_free_path(path);
  291. goto out_drop_write;
  292. }
  293. btrfs_release_path(snap_src->fs_info->tree_root, path);
  294. test = test->d_parent;
  295. }
  296. create:
  297. btrfs_free_path(path);
  298. error = create_snapshot(snap_src, dentry, name, namelen);
  299. } else {
  300. error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
  301. dentry, name, namelen);
  302. }
  303. if (error)
  304. goto out_drop_write;
  305. fsnotify_mkdir(parent->dentry->d_inode, dentry);
  306. out_drop_write:
  307. mnt_drop_write(parent->mnt);
  308. out_dput:
  309. dput(dentry);
  310. out_unlock:
  311. mutex_unlock(&parent->dentry->d_inode->i_mutex);
  312. return error;
  313. }
  314. static int btrfs_defrag_file(struct file *file)
  315. {
  316. struct inode *inode = fdentry(file)->d_inode;
  317. struct btrfs_root *root = BTRFS_I(inode)->root;
  318. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  319. struct btrfs_ordered_extent *ordered;
  320. struct page *page;
  321. unsigned long last_index;
  322. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  323. unsigned long total_read = 0;
  324. u64 page_start;
  325. u64 page_end;
  326. unsigned long i;
  327. int ret;
  328. ret = btrfs_check_data_free_space(root, inode, inode->i_size);
  329. if (ret)
  330. return -ENOSPC;
  331. mutex_lock(&inode->i_mutex);
  332. last_index = inode->i_size >> PAGE_CACHE_SHIFT;
  333. for (i = 0; i <= last_index; i++) {
  334. if (total_read % ra_pages == 0) {
  335. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  336. min(last_index, i + ra_pages - 1));
  337. }
  338. total_read++;
  339. again:
  340. page = grab_cache_page(inode->i_mapping, i);
  341. if (!page)
  342. goto out_unlock;
  343. if (!PageUptodate(page)) {
  344. btrfs_readpage(NULL, page);
  345. lock_page(page);
  346. if (!PageUptodate(page)) {
  347. unlock_page(page);
  348. page_cache_release(page);
  349. goto out_unlock;
  350. }
  351. }
  352. wait_on_page_writeback(page);
  353. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  354. page_end = page_start + PAGE_CACHE_SIZE - 1;
  355. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  356. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  357. if (ordered) {
  358. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  359. unlock_page(page);
  360. page_cache_release(page);
  361. btrfs_start_ordered_extent(inode, ordered, 1);
  362. btrfs_put_ordered_extent(ordered);
  363. goto again;
  364. }
  365. set_page_extent_mapped(page);
  366. /*
  367. * this makes sure page_mkwrite is called on the
  368. * page if it is dirtied again later
  369. */
  370. clear_page_dirty_for_io(page);
  371. btrfs_set_extent_delalloc(inode, page_start, page_end);
  372. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  373. set_page_dirty(page);
  374. unlock_page(page);
  375. page_cache_release(page);
  376. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  377. }
  378. out_unlock:
  379. mutex_unlock(&inode->i_mutex);
  380. return 0;
  381. }
  382. static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
  383. {
  384. u64 new_size;
  385. u64 old_size;
  386. u64 devid = 1;
  387. struct btrfs_ioctl_vol_args *vol_args;
  388. struct btrfs_trans_handle *trans;
  389. struct btrfs_device *device = NULL;
  390. char *sizestr;
  391. char *devstr = NULL;
  392. int ret = 0;
  393. int namelen;
  394. int mod = 0;
  395. if (root->fs_info->sb->s_flags & MS_RDONLY)
  396. return -EROFS;
  397. if (!capable(CAP_SYS_ADMIN))
  398. return -EPERM;
  399. vol_args = memdup_user(arg, sizeof(*vol_args));
  400. if (IS_ERR(vol_args))
  401. return PTR_ERR(vol_args);
  402. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  403. namelen = strlen(vol_args->name);
  404. mutex_lock(&root->fs_info->volume_mutex);
  405. sizestr = vol_args->name;
  406. devstr = strchr(sizestr, ':');
  407. if (devstr) {
  408. char *end;
  409. sizestr = devstr + 1;
  410. *devstr = '\0';
  411. devstr = vol_args->name;
  412. devid = simple_strtoull(devstr, &end, 10);
  413. printk(KERN_INFO "resizing devid %llu\n",
  414. (unsigned long long)devid);
  415. }
  416. device = btrfs_find_device(root, devid, NULL, NULL);
  417. if (!device) {
  418. printk(KERN_INFO "resizer unable to find device %llu\n",
  419. (unsigned long long)devid);
  420. ret = -EINVAL;
  421. goto out_unlock;
  422. }
  423. if (!strcmp(sizestr, "max"))
  424. new_size = device->bdev->bd_inode->i_size;
  425. else {
  426. if (sizestr[0] == '-') {
  427. mod = -1;
  428. sizestr++;
  429. } else if (sizestr[0] == '+') {
  430. mod = 1;
  431. sizestr++;
  432. }
  433. new_size = btrfs_parse_size(sizestr);
  434. if (new_size == 0) {
  435. ret = -EINVAL;
  436. goto out_unlock;
  437. }
  438. }
  439. old_size = device->total_bytes;
  440. if (mod < 0) {
  441. if (new_size > old_size) {
  442. ret = -EINVAL;
  443. goto out_unlock;
  444. }
  445. new_size = old_size - new_size;
  446. } else if (mod > 0) {
  447. new_size = old_size + new_size;
  448. }
  449. if (new_size < 256 * 1024 * 1024) {
  450. ret = -EINVAL;
  451. goto out_unlock;
  452. }
  453. if (new_size > device->bdev->bd_inode->i_size) {
  454. ret = -EFBIG;
  455. goto out_unlock;
  456. }
  457. do_div(new_size, root->sectorsize);
  458. new_size *= root->sectorsize;
  459. printk(KERN_INFO "new size for %s is %llu\n",
  460. device->name, (unsigned long long)new_size);
  461. if (new_size > old_size) {
  462. trans = btrfs_start_transaction(root, 1);
  463. ret = btrfs_grow_device(trans, device, new_size);
  464. btrfs_commit_transaction(trans, root);
  465. } else {
  466. ret = btrfs_shrink_device(device, new_size);
  467. }
  468. out_unlock:
  469. mutex_unlock(&root->fs_info->volume_mutex);
  470. kfree(vol_args);
  471. return ret;
  472. }
  473. static noinline int btrfs_ioctl_snap_create(struct file *file,
  474. void __user *arg, int subvol)
  475. {
  476. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  477. struct btrfs_ioctl_vol_args *vol_args;
  478. struct btrfs_dir_item *di;
  479. struct btrfs_path *path;
  480. struct file *src_file;
  481. u64 root_dirid;
  482. int namelen;
  483. int ret = 0;
  484. if (root->fs_info->sb->s_flags & MS_RDONLY)
  485. return -EROFS;
  486. vol_args = memdup_user(arg, sizeof(*vol_args));
  487. if (IS_ERR(vol_args))
  488. return PTR_ERR(vol_args);
  489. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  490. namelen = strlen(vol_args->name);
  491. if (strchr(vol_args->name, '/')) {
  492. ret = -EINVAL;
  493. goto out;
  494. }
  495. path = btrfs_alloc_path();
  496. if (!path) {
  497. ret = -ENOMEM;
  498. goto out;
  499. }
  500. root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
  501. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
  502. path, root_dirid,
  503. vol_args->name, namelen, 0);
  504. btrfs_free_path(path);
  505. if (di && !IS_ERR(di)) {
  506. ret = -EEXIST;
  507. goto out;
  508. }
  509. if (IS_ERR(di)) {
  510. ret = PTR_ERR(di);
  511. goto out;
  512. }
  513. if (subvol) {
  514. ret = btrfs_mksubvol(&file->f_path, vol_args->name,
  515. file->f_path.dentry->d_inode->i_mode,
  516. namelen, NULL);
  517. } else {
  518. struct inode *src_inode;
  519. src_file = fget(vol_args->fd);
  520. if (!src_file) {
  521. ret = -EINVAL;
  522. goto out;
  523. }
  524. src_inode = src_file->f_path.dentry->d_inode;
  525. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  526. printk(KERN_INFO "btrfs: Snapshot src from "
  527. "another FS\n");
  528. ret = -EINVAL;
  529. fput(src_file);
  530. goto out;
  531. }
  532. ret = btrfs_mksubvol(&file->f_path, vol_args->name,
  533. file->f_path.dentry->d_inode->i_mode,
  534. namelen, BTRFS_I(src_inode)->root);
  535. fput(src_file);
  536. }
  537. out:
  538. kfree(vol_args);
  539. return ret;
  540. }
  541. static int btrfs_ioctl_defrag(struct file *file)
  542. {
  543. struct inode *inode = fdentry(file)->d_inode;
  544. struct btrfs_root *root = BTRFS_I(inode)->root;
  545. int ret;
  546. ret = mnt_want_write(file->f_path.mnt);
  547. if (ret)
  548. return ret;
  549. switch (inode->i_mode & S_IFMT) {
  550. case S_IFDIR:
  551. if (!capable(CAP_SYS_ADMIN)) {
  552. ret = -EPERM;
  553. goto out;
  554. }
  555. btrfs_defrag_root(root, 0);
  556. btrfs_defrag_root(root->fs_info->extent_root, 0);
  557. break;
  558. case S_IFREG:
  559. if (!(file->f_mode & FMODE_WRITE)) {
  560. ret = -EINVAL;
  561. goto out;
  562. }
  563. btrfs_defrag_file(file);
  564. break;
  565. }
  566. out:
  567. mnt_drop_write(file->f_path.mnt);
  568. return ret;
  569. }
  570. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  571. {
  572. struct btrfs_ioctl_vol_args *vol_args;
  573. int ret;
  574. if (!capable(CAP_SYS_ADMIN))
  575. return -EPERM;
  576. vol_args = memdup_user(arg, sizeof(*vol_args));
  577. if (IS_ERR(vol_args))
  578. return PTR_ERR(vol_args);
  579. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  580. ret = btrfs_init_new_device(root, vol_args->name);
  581. kfree(vol_args);
  582. return ret;
  583. }
  584. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  585. {
  586. struct btrfs_ioctl_vol_args *vol_args;
  587. int ret;
  588. if (!capable(CAP_SYS_ADMIN))
  589. return -EPERM;
  590. if (root->fs_info->sb->s_flags & MS_RDONLY)
  591. return -EROFS;
  592. vol_args = memdup_user(arg, sizeof(*vol_args));
  593. if (IS_ERR(vol_args))
  594. return PTR_ERR(vol_args);
  595. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  596. ret = btrfs_rm_device(root, vol_args->name);
  597. kfree(vol_args);
  598. return ret;
  599. }
  600. static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  601. u64 off, u64 olen, u64 destoff)
  602. {
  603. struct inode *inode = fdentry(file)->d_inode;
  604. struct btrfs_root *root = BTRFS_I(inode)->root;
  605. struct file *src_file;
  606. struct inode *src;
  607. struct btrfs_trans_handle *trans;
  608. struct btrfs_path *path;
  609. struct extent_buffer *leaf;
  610. char *buf;
  611. struct btrfs_key key;
  612. u32 nritems;
  613. int slot;
  614. int ret;
  615. u64 len = olen;
  616. u64 bs = root->fs_info->sb->s_blocksize;
  617. u64 hint_byte;
  618. /*
  619. * TODO:
  620. * - split compressed inline extents. annoying: we need to
  621. * decompress into destination's address_space (the file offset
  622. * may change, so source mapping won't do), then recompress (or
  623. * otherwise reinsert) a subrange.
  624. * - allow ranges within the same file to be cloned (provided
  625. * they don't overlap)?
  626. */
  627. /* the destination must be opened for writing */
  628. if (!(file->f_mode & FMODE_WRITE))
  629. return -EINVAL;
  630. ret = mnt_want_write(file->f_path.mnt);
  631. if (ret)
  632. return ret;
  633. src_file = fget(srcfd);
  634. if (!src_file) {
  635. ret = -EBADF;
  636. goto out_drop_write;
  637. }
  638. src = src_file->f_dentry->d_inode;
  639. ret = -EINVAL;
  640. if (src == inode)
  641. goto out_fput;
  642. ret = -EISDIR;
  643. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  644. goto out_fput;
  645. ret = -EXDEV;
  646. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  647. goto out_fput;
  648. ret = -ENOMEM;
  649. buf = vmalloc(btrfs_level_size(root, 0));
  650. if (!buf)
  651. goto out_fput;
  652. path = btrfs_alloc_path();
  653. if (!path) {
  654. vfree(buf);
  655. goto out_fput;
  656. }
  657. path->reada = 2;
  658. if (inode < src) {
  659. mutex_lock(&inode->i_mutex);
  660. mutex_lock(&src->i_mutex);
  661. } else {
  662. mutex_lock(&src->i_mutex);
  663. mutex_lock(&inode->i_mutex);
  664. }
  665. /* determine range to clone */
  666. ret = -EINVAL;
  667. if (off >= src->i_size || off + len > src->i_size)
  668. goto out_unlock;
  669. if (len == 0)
  670. olen = len = src->i_size - off;
  671. /* if we extend to eof, continue to block boundary */
  672. if (off + len == src->i_size)
  673. len = ((src->i_size + bs-1) & ~(bs-1))
  674. - off;
  675. /* verify the end result is block aligned */
  676. if ((off & (bs-1)) ||
  677. ((off + len) & (bs-1)))
  678. goto out_unlock;
  679. /* do any pending delalloc/csum calc on src, one way or
  680. another, and lock file content */
  681. while (1) {
  682. struct btrfs_ordered_extent *ordered;
  683. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  684. ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
  685. if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
  686. break;
  687. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  688. if (ordered)
  689. btrfs_put_ordered_extent(ordered);
  690. btrfs_wait_ordered_range(src, off, off+len);
  691. }
  692. trans = btrfs_start_transaction(root, 1);
  693. BUG_ON(!trans);
  694. /* punch hole in destination first */
  695. btrfs_drop_extents(trans, root, inode, off, off + len,
  696. off + len, 0, &hint_byte);
  697. /* clone data */
  698. key.objectid = src->i_ino;
  699. key.type = BTRFS_EXTENT_DATA_KEY;
  700. key.offset = 0;
  701. while (1) {
  702. /*
  703. * note the key will change type as we walk through the
  704. * tree.
  705. */
  706. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  707. if (ret < 0)
  708. goto out;
  709. nritems = btrfs_header_nritems(path->nodes[0]);
  710. if (path->slots[0] >= nritems) {
  711. ret = btrfs_next_leaf(root, path);
  712. if (ret < 0)
  713. goto out;
  714. if (ret > 0)
  715. break;
  716. nritems = btrfs_header_nritems(path->nodes[0]);
  717. }
  718. leaf = path->nodes[0];
  719. slot = path->slots[0];
  720. btrfs_item_key_to_cpu(leaf, &key, slot);
  721. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  722. key.objectid != src->i_ino)
  723. break;
  724. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  725. struct btrfs_file_extent_item *extent;
  726. int type;
  727. u32 size;
  728. struct btrfs_key new_key;
  729. u64 disko = 0, diskl = 0;
  730. u64 datao = 0, datal = 0;
  731. u8 comp;
  732. size = btrfs_item_size_nr(leaf, slot);
  733. read_extent_buffer(leaf, buf,
  734. btrfs_item_ptr_offset(leaf, slot),
  735. size);
  736. extent = btrfs_item_ptr(leaf, slot,
  737. struct btrfs_file_extent_item);
  738. comp = btrfs_file_extent_compression(leaf, extent);
  739. type = btrfs_file_extent_type(leaf, extent);
  740. if (type == BTRFS_FILE_EXTENT_REG) {
  741. disko = btrfs_file_extent_disk_bytenr(leaf,
  742. extent);
  743. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  744. extent);
  745. datao = btrfs_file_extent_offset(leaf, extent);
  746. datal = btrfs_file_extent_num_bytes(leaf,
  747. extent);
  748. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  749. /* take upper bound, may be compressed */
  750. datal = btrfs_file_extent_ram_bytes(leaf,
  751. extent);
  752. }
  753. btrfs_release_path(root, path);
  754. if (key.offset + datal < off ||
  755. key.offset >= off+len)
  756. goto next;
  757. memcpy(&new_key, &key, sizeof(new_key));
  758. new_key.objectid = inode->i_ino;
  759. new_key.offset = key.offset + destoff - off;
  760. if (type == BTRFS_FILE_EXTENT_REG) {
  761. ret = btrfs_insert_empty_item(trans, root, path,
  762. &new_key, size);
  763. if (ret)
  764. goto out;
  765. leaf = path->nodes[0];
  766. slot = path->slots[0];
  767. write_extent_buffer(leaf, buf,
  768. btrfs_item_ptr_offset(leaf, slot),
  769. size);
  770. extent = btrfs_item_ptr(leaf, slot,
  771. struct btrfs_file_extent_item);
  772. if (off > key.offset) {
  773. datao += off - key.offset;
  774. datal -= off - key.offset;
  775. }
  776. if (key.offset + datao + datal + key.offset >
  777. off + len)
  778. datal = off + len - key.offset - datao;
  779. /* disko == 0 means it's a hole */
  780. if (!disko)
  781. datao = 0;
  782. btrfs_set_file_extent_offset(leaf, extent,
  783. datao);
  784. btrfs_set_file_extent_num_bytes(leaf, extent,
  785. datal);
  786. if (disko) {
  787. inode_add_bytes(inode, datal);
  788. ret = btrfs_inc_extent_ref(trans, root,
  789. disko, diskl, leaf->start,
  790. root->root_key.objectid,
  791. trans->transid,
  792. inode->i_ino);
  793. BUG_ON(ret);
  794. }
  795. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  796. u64 skip = 0;
  797. u64 trim = 0;
  798. if (off > key.offset) {
  799. skip = off - key.offset;
  800. new_key.offset += skip;
  801. }
  802. if (key.offset + datal > off+len)
  803. trim = key.offset + datal - (off+len);
  804. if (comp && (skip || trim)) {
  805. ret = -EINVAL;
  806. goto out;
  807. }
  808. size -= skip + trim;
  809. datal -= skip + trim;
  810. ret = btrfs_insert_empty_item(trans, root, path,
  811. &new_key, size);
  812. if (ret)
  813. goto out;
  814. if (skip) {
  815. u32 start =
  816. btrfs_file_extent_calc_inline_size(0);
  817. memmove(buf+start, buf+start+skip,
  818. datal);
  819. }
  820. leaf = path->nodes[0];
  821. slot = path->slots[0];
  822. write_extent_buffer(leaf, buf,
  823. btrfs_item_ptr_offset(leaf, slot),
  824. size);
  825. inode_add_bytes(inode, datal);
  826. }
  827. btrfs_mark_buffer_dirty(leaf);
  828. }
  829. next:
  830. btrfs_release_path(root, path);
  831. key.offset++;
  832. }
  833. ret = 0;
  834. out:
  835. btrfs_release_path(root, path);
  836. if (ret == 0) {
  837. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  838. if (destoff + olen > inode->i_size)
  839. btrfs_i_size_write(inode, destoff + olen);
  840. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  841. ret = btrfs_update_inode(trans, root, inode);
  842. }
  843. btrfs_end_transaction(trans, root);
  844. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  845. if (ret)
  846. vmtruncate(inode, 0);
  847. out_unlock:
  848. mutex_unlock(&src->i_mutex);
  849. mutex_unlock(&inode->i_mutex);
  850. vfree(buf);
  851. btrfs_free_path(path);
  852. out_fput:
  853. fput(src_file);
  854. out_drop_write:
  855. mnt_drop_write(file->f_path.mnt);
  856. return ret;
  857. }
  858. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  859. {
  860. struct btrfs_ioctl_clone_range_args args;
  861. if (copy_from_user(&args, argp, sizeof(args)))
  862. return -EFAULT;
  863. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  864. args.src_length, args.dest_offset);
  865. }
  866. /*
  867. * there are many ways the trans_start and trans_end ioctls can lead
  868. * to deadlocks. They should only be used by applications that
  869. * basically own the machine, and have a very in depth understanding
  870. * of all the possible deadlocks and enospc problems.
  871. */
  872. static long btrfs_ioctl_trans_start(struct file *file)
  873. {
  874. struct inode *inode = fdentry(file)->d_inode;
  875. struct btrfs_root *root = BTRFS_I(inode)->root;
  876. struct btrfs_trans_handle *trans;
  877. int ret = 0;
  878. if (!capable(CAP_SYS_ADMIN))
  879. return -EPERM;
  880. if (file->private_data) {
  881. ret = -EINPROGRESS;
  882. goto out;
  883. }
  884. ret = mnt_want_write(file->f_path.mnt);
  885. if (ret)
  886. goto out;
  887. mutex_lock(&root->fs_info->trans_mutex);
  888. root->fs_info->open_ioctl_trans++;
  889. mutex_unlock(&root->fs_info->trans_mutex);
  890. trans = btrfs_start_ioctl_transaction(root, 0);
  891. if (trans)
  892. file->private_data = trans;
  893. else
  894. ret = -ENOMEM;
  895. /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
  896. out:
  897. return ret;
  898. }
  899. /*
  900. * there are many ways the trans_start and trans_end ioctls can lead
  901. * to deadlocks. They should only be used by applications that
  902. * basically own the machine, and have a very in depth understanding
  903. * of all the possible deadlocks and enospc problems.
  904. */
  905. long btrfs_ioctl_trans_end(struct file *file)
  906. {
  907. struct inode *inode = fdentry(file)->d_inode;
  908. struct btrfs_root *root = BTRFS_I(inode)->root;
  909. struct btrfs_trans_handle *trans;
  910. int ret = 0;
  911. trans = file->private_data;
  912. if (!trans) {
  913. ret = -EINVAL;
  914. goto out;
  915. }
  916. btrfs_end_transaction(trans, root);
  917. file->private_data = NULL;
  918. mutex_lock(&root->fs_info->trans_mutex);
  919. root->fs_info->open_ioctl_trans--;
  920. mutex_unlock(&root->fs_info->trans_mutex);
  921. mnt_drop_write(file->f_path.mnt);
  922. out:
  923. return ret;
  924. }
  925. long btrfs_ioctl(struct file *file, unsigned int
  926. cmd, unsigned long arg)
  927. {
  928. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  929. void __user *argp = (void __user *)arg;
  930. switch (cmd) {
  931. case BTRFS_IOC_SNAP_CREATE:
  932. return btrfs_ioctl_snap_create(file, argp, 0);
  933. case BTRFS_IOC_SUBVOL_CREATE:
  934. return btrfs_ioctl_snap_create(file, argp, 1);
  935. case BTRFS_IOC_DEFRAG:
  936. return btrfs_ioctl_defrag(file);
  937. case BTRFS_IOC_RESIZE:
  938. return btrfs_ioctl_resize(root, argp);
  939. case BTRFS_IOC_ADD_DEV:
  940. return btrfs_ioctl_add_dev(root, argp);
  941. case BTRFS_IOC_RM_DEV:
  942. return btrfs_ioctl_rm_dev(root, argp);
  943. case BTRFS_IOC_BALANCE:
  944. return btrfs_balance(root->fs_info->dev_root);
  945. case BTRFS_IOC_CLONE:
  946. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  947. case BTRFS_IOC_CLONE_RANGE:
  948. return btrfs_ioctl_clone_range(file, argp);
  949. case BTRFS_IOC_TRANS_START:
  950. return btrfs_ioctl_trans_start(file);
  951. case BTRFS_IOC_TRANS_END:
  952. return btrfs_ioctl_trans_end(file);
  953. case BTRFS_IOC_SYNC:
  954. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  955. return 0;
  956. }
  957. return -ENOTTY;
  958. }