ioctl.c 27 KB

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