ioctl.c 27 KB

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