ioctl.c 28 KB

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