ioctl.c 26 KB

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