ioctl.c 28 KB

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