ioctl.c 28 KB

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