ioctl.c 27 KB

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