ioctl.c 28 KB

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