ioctl.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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",
  421. (unsigned long long)devid);
  422. }
  423. device = btrfs_find_device(root, devid, NULL, NULL);
  424. if (!device) {
  425. printk(KERN_INFO "resizer unable to find device %llu\n",
  426. (unsigned long long)devid);
  427. ret = -EINVAL;
  428. goto out_unlock;
  429. }
  430. if (!strcmp(sizestr, "max"))
  431. new_size = device->bdev->bd_inode->i_size;
  432. else {
  433. if (sizestr[0] == '-') {
  434. mod = -1;
  435. sizestr++;
  436. } else if (sizestr[0] == '+') {
  437. mod = 1;
  438. sizestr++;
  439. }
  440. new_size = btrfs_parse_size(sizestr);
  441. if (new_size == 0) {
  442. ret = -EINVAL;
  443. goto out_unlock;
  444. }
  445. }
  446. old_size = device->total_bytes;
  447. if (mod < 0) {
  448. if (new_size > old_size) {
  449. ret = -EINVAL;
  450. goto out_unlock;
  451. }
  452. new_size = old_size - new_size;
  453. } else if (mod > 0) {
  454. new_size = old_size + new_size;
  455. }
  456. if (new_size < 256 * 1024 * 1024) {
  457. ret = -EINVAL;
  458. goto out_unlock;
  459. }
  460. if (new_size > device->bdev->bd_inode->i_size) {
  461. ret = -EFBIG;
  462. goto out_unlock;
  463. }
  464. do_div(new_size, root->sectorsize);
  465. new_size *= root->sectorsize;
  466. printk(KERN_INFO "new size for %s is %llu\n",
  467. device->name, (unsigned long long)new_size);
  468. if (new_size > old_size) {
  469. trans = btrfs_start_transaction(root, 1);
  470. ret = btrfs_grow_device(trans, device, new_size);
  471. btrfs_commit_transaction(trans, root);
  472. } else {
  473. ret = btrfs_shrink_device(device, new_size);
  474. }
  475. out_unlock:
  476. mutex_unlock(&root->fs_info->volume_mutex);
  477. out:
  478. kfree(vol_args);
  479. return ret;
  480. }
  481. static noinline int btrfs_ioctl_snap_create(struct file *file,
  482. void __user *arg, int subvol)
  483. {
  484. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  485. struct btrfs_ioctl_vol_args *vol_args;
  486. struct btrfs_dir_item *di;
  487. struct btrfs_path *path;
  488. struct file *src_file;
  489. u64 root_dirid;
  490. int namelen;
  491. int ret = 0;
  492. if (root->fs_info->sb->s_flags & MS_RDONLY)
  493. return -EROFS;
  494. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  495. if (!vol_args)
  496. return -ENOMEM;
  497. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  498. ret = -EFAULT;
  499. goto out;
  500. }
  501. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  502. namelen = strlen(vol_args->name);
  503. if (strchr(vol_args->name, '/')) {
  504. ret = -EINVAL;
  505. goto out;
  506. }
  507. path = btrfs_alloc_path();
  508. if (!path) {
  509. ret = -ENOMEM;
  510. goto out;
  511. }
  512. root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
  513. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
  514. path, root_dirid,
  515. vol_args->name, namelen, 0);
  516. btrfs_free_path(path);
  517. if (di && !IS_ERR(di)) {
  518. ret = -EEXIST;
  519. goto out;
  520. }
  521. if (IS_ERR(di)) {
  522. ret = PTR_ERR(di);
  523. goto out;
  524. }
  525. if (subvol) {
  526. ret = btrfs_mksubvol(&file->f_path, vol_args->name,
  527. file->f_path.dentry->d_inode->i_mode,
  528. namelen, NULL);
  529. } else {
  530. struct inode *src_inode;
  531. src_file = fget(vol_args->fd);
  532. if (!src_file) {
  533. ret = -EINVAL;
  534. goto out;
  535. }
  536. src_inode = src_file->f_path.dentry->d_inode;
  537. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  538. printk(KERN_INFO "btrfs: Snapshot src from "
  539. "another FS\n");
  540. ret = -EINVAL;
  541. fput(src_file);
  542. goto out;
  543. }
  544. ret = btrfs_mksubvol(&file->f_path, vol_args->name,
  545. file->f_path.dentry->d_inode->i_mode,
  546. namelen, BTRFS_I(src_inode)->root);
  547. fput(src_file);
  548. }
  549. out:
  550. kfree(vol_args);
  551. return ret;
  552. }
  553. static int btrfs_ioctl_defrag(struct file *file)
  554. {
  555. struct inode *inode = fdentry(file)->d_inode;
  556. struct btrfs_root *root = BTRFS_I(inode)->root;
  557. int ret;
  558. ret = mnt_want_write(file->f_path.mnt);
  559. if (ret)
  560. return ret;
  561. switch (inode->i_mode & S_IFMT) {
  562. case S_IFDIR:
  563. if (!capable(CAP_SYS_ADMIN)) {
  564. ret = -EPERM;
  565. goto out;
  566. }
  567. btrfs_defrag_root(root, 0);
  568. btrfs_defrag_root(root->fs_info->extent_root, 0);
  569. break;
  570. case S_IFREG:
  571. if (!(file->f_mode & FMODE_WRITE)) {
  572. ret = -EINVAL;
  573. goto out;
  574. }
  575. btrfs_defrag_file(file);
  576. break;
  577. }
  578. out:
  579. mnt_drop_write(file->f_path.mnt);
  580. return ret;
  581. }
  582. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  583. {
  584. struct btrfs_ioctl_vol_args *vol_args;
  585. int ret;
  586. if (!capable(CAP_SYS_ADMIN))
  587. return -EPERM;
  588. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  589. if (!vol_args)
  590. return -ENOMEM;
  591. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  592. ret = -EFAULT;
  593. goto out;
  594. }
  595. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  596. ret = btrfs_init_new_device(root, vol_args->name);
  597. out:
  598. kfree(vol_args);
  599. return ret;
  600. }
  601. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  602. {
  603. struct btrfs_ioctl_vol_args *vol_args;
  604. int ret;
  605. if (!capable(CAP_SYS_ADMIN))
  606. return -EPERM;
  607. if (root->fs_info->sb->s_flags & MS_RDONLY)
  608. return -EROFS;
  609. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  610. if (!vol_args)
  611. return -ENOMEM;
  612. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  613. ret = -EFAULT;
  614. goto out;
  615. }
  616. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  617. ret = btrfs_rm_device(root, vol_args->name);
  618. out:
  619. kfree(vol_args);
  620. return ret;
  621. }
  622. static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  623. u64 off, u64 olen, u64 destoff)
  624. {
  625. struct inode *inode = fdentry(file)->d_inode;
  626. struct btrfs_root *root = BTRFS_I(inode)->root;
  627. struct file *src_file;
  628. struct inode *src;
  629. struct btrfs_trans_handle *trans;
  630. struct btrfs_path *path;
  631. struct extent_buffer *leaf;
  632. char *buf;
  633. struct btrfs_key key;
  634. u32 nritems;
  635. int slot;
  636. int ret;
  637. u64 len = olen;
  638. u64 bs = root->fs_info->sb->s_blocksize;
  639. u64 hint_byte;
  640. /*
  641. * TODO:
  642. * - split compressed inline extents. annoying: we need to
  643. * decompress into destination's address_space (the file offset
  644. * may change, so source mapping won't do), then recompress (or
  645. * otherwise reinsert) a subrange.
  646. * - allow ranges within the same file to be cloned (provided
  647. * they don't overlap)?
  648. */
  649. /* the destination must be opened for writing */
  650. if (!(file->f_mode & FMODE_WRITE))
  651. return -EINVAL;
  652. ret = mnt_want_write(file->f_path.mnt);
  653. if (ret)
  654. return ret;
  655. src_file = fget(srcfd);
  656. if (!src_file) {
  657. ret = -EBADF;
  658. goto out_drop_write;
  659. }
  660. src = src_file->f_dentry->d_inode;
  661. ret = -EINVAL;
  662. if (src == inode)
  663. goto out_fput;
  664. ret = -EISDIR;
  665. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  666. goto out_fput;
  667. ret = -EXDEV;
  668. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  669. goto out_fput;
  670. ret = -ENOMEM;
  671. buf = vmalloc(btrfs_level_size(root, 0));
  672. if (!buf)
  673. goto out_fput;
  674. path = btrfs_alloc_path();
  675. if (!path) {
  676. vfree(buf);
  677. goto out_fput;
  678. }
  679. path->reada = 2;
  680. if (inode < src) {
  681. mutex_lock(&inode->i_mutex);
  682. mutex_lock(&src->i_mutex);
  683. } else {
  684. mutex_lock(&src->i_mutex);
  685. mutex_lock(&inode->i_mutex);
  686. }
  687. /* determine range to clone */
  688. ret = -EINVAL;
  689. if (off >= src->i_size || off + len > src->i_size)
  690. goto out_unlock;
  691. if (len == 0)
  692. olen = len = src->i_size - off;
  693. /* if we extend to eof, continue to block boundary */
  694. if (off + len == src->i_size)
  695. len = ((src->i_size + bs-1) & ~(bs-1))
  696. - off;
  697. /* verify the end result is block aligned */
  698. if ((off & (bs-1)) ||
  699. ((off + len) & (bs-1)))
  700. goto out_unlock;
  701. /* do any pending delalloc/csum calc on src, one way or
  702. another, and lock file content */
  703. while (1) {
  704. struct btrfs_ordered_extent *ordered;
  705. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  706. ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
  707. if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
  708. break;
  709. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  710. if (ordered)
  711. btrfs_put_ordered_extent(ordered);
  712. btrfs_wait_ordered_range(src, off, off+len);
  713. }
  714. trans = btrfs_start_transaction(root, 1);
  715. BUG_ON(!trans);
  716. /* punch hole in destination first */
  717. btrfs_drop_extents(trans, root, inode, off, off + len,
  718. off + len, 0, &hint_byte);
  719. /* clone data */
  720. key.objectid = src->i_ino;
  721. key.type = BTRFS_EXTENT_DATA_KEY;
  722. key.offset = 0;
  723. while (1) {
  724. /*
  725. * note the key will change type as we walk through the
  726. * tree.
  727. */
  728. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  729. if (ret < 0)
  730. goto out;
  731. nritems = btrfs_header_nritems(path->nodes[0]);
  732. if (path->slots[0] >= nritems) {
  733. ret = btrfs_next_leaf(root, path);
  734. if (ret < 0)
  735. goto out;
  736. if (ret > 0)
  737. break;
  738. nritems = btrfs_header_nritems(path->nodes[0]);
  739. }
  740. leaf = path->nodes[0];
  741. slot = path->slots[0];
  742. btrfs_item_key_to_cpu(leaf, &key, slot);
  743. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  744. key.objectid != src->i_ino)
  745. break;
  746. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  747. struct btrfs_file_extent_item *extent;
  748. int type;
  749. u32 size;
  750. struct btrfs_key new_key;
  751. u64 disko = 0, diskl = 0;
  752. u64 datao = 0, datal = 0;
  753. u8 comp;
  754. size = btrfs_item_size_nr(leaf, slot);
  755. read_extent_buffer(leaf, buf,
  756. btrfs_item_ptr_offset(leaf, slot),
  757. size);
  758. extent = btrfs_item_ptr(leaf, slot,
  759. struct btrfs_file_extent_item);
  760. comp = btrfs_file_extent_compression(leaf, extent);
  761. type = btrfs_file_extent_type(leaf, extent);
  762. if (type == BTRFS_FILE_EXTENT_REG) {
  763. disko = btrfs_file_extent_disk_bytenr(leaf,
  764. extent);
  765. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  766. extent);
  767. datao = btrfs_file_extent_offset(leaf, extent);
  768. datal = btrfs_file_extent_num_bytes(leaf,
  769. extent);
  770. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  771. /* take upper bound, may be compressed */
  772. datal = btrfs_file_extent_ram_bytes(leaf,
  773. extent);
  774. }
  775. btrfs_release_path(root, path);
  776. if (key.offset + datal < off ||
  777. key.offset >= off+len)
  778. goto next;
  779. memcpy(&new_key, &key, sizeof(new_key));
  780. new_key.objectid = inode->i_ino;
  781. new_key.offset = key.offset + destoff - off;
  782. if (type == BTRFS_FILE_EXTENT_REG) {
  783. ret = btrfs_insert_empty_item(trans, root, path,
  784. &new_key, size);
  785. if (ret)
  786. goto out;
  787. leaf = path->nodes[0];
  788. slot = path->slots[0];
  789. write_extent_buffer(leaf, buf,
  790. btrfs_item_ptr_offset(leaf, slot),
  791. size);
  792. extent = btrfs_item_ptr(leaf, slot,
  793. struct btrfs_file_extent_item);
  794. if (off > key.offset) {
  795. datao += off - key.offset;
  796. datal -= off - key.offset;
  797. }
  798. if (key.offset + datao + datal + key.offset >
  799. off + len)
  800. datal = off + len - key.offset - datao;
  801. /* disko == 0 means it's a hole */
  802. if (!disko)
  803. datao = 0;
  804. btrfs_set_file_extent_offset(leaf, extent,
  805. datao);
  806. btrfs_set_file_extent_num_bytes(leaf, extent,
  807. datal);
  808. if (disko) {
  809. inode_add_bytes(inode, datal);
  810. ret = btrfs_inc_extent_ref(trans, root,
  811. disko, diskl, leaf->start,
  812. root->root_key.objectid,
  813. trans->transid,
  814. inode->i_ino);
  815. BUG_ON(ret);
  816. }
  817. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  818. u64 skip = 0;
  819. u64 trim = 0;
  820. if (off > key.offset) {
  821. skip = off - key.offset;
  822. new_key.offset += skip;
  823. }
  824. if (key.offset + datal > off+len)
  825. trim = key.offset + datal - (off+len);
  826. if (comp && (skip || trim)) {
  827. ret = -EINVAL;
  828. goto out;
  829. }
  830. size -= skip + trim;
  831. datal -= skip + trim;
  832. ret = btrfs_insert_empty_item(trans, root, path,
  833. &new_key, size);
  834. if (ret)
  835. goto out;
  836. if (skip) {
  837. u32 start =
  838. btrfs_file_extent_calc_inline_size(0);
  839. memmove(buf+start, buf+start+skip,
  840. datal);
  841. }
  842. leaf = path->nodes[0];
  843. slot = path->slots[0];
  844. write_extent_buffer(leaf, buf,
  845. btrfs_item_ptr_offset(leaf, slot),
  846. size);
  847. inode_add_bytes(inode, datal);
  848. }
  849. btrfs_mark_buffer_dirty(leaf);
  850. }
  851. next:
  852. btrfs_release_path(root, path);
  853. key.offset++;
  854. }
  855. ret = 0;
  856. out:
  857. btrfs_release_path(root, path);
  858. if (ret == 0) {
  859. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  860. if (destoff + olen > inode->i_size)
  861. btrfs_i_size_write(inode, destoff + olen);
  862. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  863. ret = btrfs_update_inode(trans, root, inode);
  864. }
  865. btrfs_end_transaction(trans, root);
  866. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  867. if (ret)
  868. vmtruncate(inode, 0);
  869. out_unlock:
  870. mutex_unlock(&src->i_mutex);
  871. mutex_unlock(&inode->i_mutex);
  872. vfree(buf);
  873. btrfs_free_path(path);
  874. out_fput:
  875. fput(src_file);
  876. out_drop_write:
  877. mnt_drop_write(file->f_path.mnt);
  878. return ret;
  879. }
  880. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  881. {
  882. struct btrfs_ioctl_clone_range_args args;
  883. if (copy_from_user(&args, argp, sizeof(args)))
  884. return -EFAULT;
  885. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  886. args.src_length, args.dest_offset);
  887. }
  888. /*
  889. * there are many ways the trans_start and trans_end ioctls can lead
  890. * to deadlocks. They should only be used by applications that
  891. * basically own the machine, and have a very in depth understanding
  892. * of all the possible deadlocks and enospc problems.
  893. */
  894. static long btrfs_ioctl_trans_start(struct file *file)
  895. {
  896. struct inode *inode = fdentry(file)->d_inode;
  897. struct btrfs_root *root = BTRFS_I(inode)->root;
  898. struct btrfs_trans_handle *trans;
  899. int ret = 0;
  900. if (!capable(CAP_SYS_ADMIN))
  901. return -EPERM;
  902. if (file->private_data) {
  903. ret = -EINPROGRESS;
  904. goto out;
  905. }
  906. ret = mnt_want_write(file->f_path.mnt);
  907. if (ret)
  908. goto out;
  909. mutex_lock(&root->fs_info->trans_mutex);
  910. root->fs_info->open_ioctl_trans++;
  911. mutex_unlock(&root->fs_info->trans_mutex);
  912. trans = btrfs_start_ioctl_transaction(root, 0);
  913. if (trans)
  914. file->private_data = trans;
  915. else
  916. ret = -ENOMEM;
  917. /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
  918. out:
  919. return ret;
  920. }
  921. /*
  922. * there are many ways the trans_start and trans_end ioctls can lead
  923. * to deadlocks. They should only be used by applications that
  924. * basically own the machine, and have a very in depth understanding
  925. * of all the possible deadlocks and enospc problems.
  926. */
  927. long btrfs_ioctl_trans_end(struct file *file)
  928. {
  929. struct inode *inode = fdentry(file)->d_inode;
  930. struct btrfs_root *root = BTRFS_I(inode)->root;
  931. struct btrfs_trans_handle *trans;
  932. int ret = 0;
  933. trans = file->private_data;
  934. if (!trans) {
  935. ret = -EINVAL;
  936. goto out;
  937. }
  938. btrfs_end_transaction(trans, root);
  939. file->private_data = NULL;
  940. mutex_lock(&root->fs_info->trans_mutex);
  941. root->fs_info->open_ioctl_trans--;
  942. mutex_unlock(&root->fs_info->trans_mutex);
  943. mnt_drop_write(file->f_path.mnt);
  944. out:
  945. return ret;
  946. }
  947. long btrfs_ioctl(struct file *file, unsigned int
  948. cmd, unsigned long arg)
  949. {
  950. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  951. void __user *argp = (void __user *)arg;
  952. switch (cmd) {
  953. case BTRFS_IOC_SNAP_CREATE:
  954. return btrfs_ioctl_snap_create(file, argp, 0);
  955. case BTRFS_IOC_SUBVOL_CREATE:
  956. return btrfs_ioctl_snap_create(file, argp, 1);
  957. case BTRFS_IOC_DEFRAG:
  958. return btrfs_ioctl_defrag(file);
  959. case BTRFS_IOC_RESIZE:
  960. return btrfs_ioctl_resize(root, argp);
  961. case BTRFS_IOC_ADD_DEV:
  962. return btrfs_ioctl_add_dev(root, argp);
  963. case BTRFS_IOC_RM_DEV:
  964. return btrfs_ioctl_rm_dev(root, argp);
  965. case BTRFS_IOC_BALANCE:
  966. return btrfs_balance(root->fs_info->dev_root);
  967. case BTRFS_IOC_CLONE:
  968. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  969. case BTRFS_IOC_CLONE_RANGE:
  970. return btrfs_ioctl_clone_range(file, argp);
  971. case BTRFS_IOC_TRANS_START:
  972. return btrfs_ioctl_trans_start(file);
  973. case BTRFS_IOC_TRANS_END:
  974. return btrfs_ioctl_trans_end(file);
  975. case BTRFS_IOC_SYNC:
  976. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  977. return 0;
  978. }
  979. return -ENOTTY;
  980. }