xref: /openbmc/linux/fs/btrfs/ioctl.c (revision c1d45424)
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 
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
23 #include <linux/fs.h>
24 #include <linux/fsnotify.h>
25 #include <linux/pagemap.h>
26 #include <linux/highmem.h>
27 #include <linux/time.h>
28 #include <linux/init.h>
29 #include <linux/string.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 <linux/slab.h>
43 #include <linux/blkdev.h>
44 #include <linux/uuid.h>
45 #include <linux/btrfs.h>
46 #include "compat.h"
47 #include "ctree.h"
48 #include "disk-io.h"
49 #include "transaction.h"
50 #include "btrfs_inode.h"
51 #include "print-tree.h"
52 #include "volumes.h"
53 #include "locking.h"
54 #include "inode-map.h"
55 #include "backref.h"
56 #include "rcu-string.h"
57 #include "send.h"
58 #include "dev-replace.h"
59 
60 /* Mask out flags that are inappropriate for the given type of inode. */
61 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
62 {
63 	if (S_ISDIR(mode))
64 		return flags;
65 	else if (S_ISREG(mode))
66 		return flags & ~FS_DIRSYNC_FL;
67 	else
68 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
69 }
70 
71 /*
72  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
73  */
74 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
75 {
76 	unsigned int iflags = 0;
77 
78 	if (flags & BTRFS_INODE_SYNC)
79 		iflags |= FS_SYNC_FL;
80 	if (flags & BTRFS_INODE_IMMUTABLE)
81 		iflags |= FS_IMMUTABLE_FL;
82 	if (flags & BTRFS_INODE_APPEND)
83 		iflags |= FS_APPEND_FL;
84 	if (flags & BTRFS_INODE_NODUMP)
85 		iflags |= FS_NODUMP_FL;
86 	if (flags & BTRFS_INODE_NOATIME)
87 		iflags |= FS_NOATIME_FL;
88 	if (flags & BTRFS_INODE_DIRSYNC)
89 		iflags |= FS_DIRSYNC_FL;
90 	if (flags & BTRFS_INODE_NODATACOW)
91 		iflags |= FS_NOCOW_FL;
92 
93 	if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
94 		iflags |= FS_COMPR_FL;
95 	else if (flags & BTRFS_INODE_NOCOMPRESS)
96 		iflags |= FS_NOCOMP_FL;
97 
98 	return iflags;
99 }
100 
101 /*
102  * Update inode->i_flags based on the btrfs internal flags.
103  */
104 void btrfs_update_iflags(struct inode *inode)
105 {
106 	struct btrfs_inode *ip = BTRFS_I(inode);
107 
108 	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
109 
110 	if (ip->flags & BTRFS_INODE_SYNC)
111 		inode->i_flags |= S_SYNC;
112 	if (ip->flags & BTRFS_INODE_IMMUTABLE)
113 		inode->i_flags |= S_IMMUTABLE;
114 	if (ip->flags & BTRFS_INODE_APPEND)
115 		inode->i_flags |= S_APPEND;
116 	if (ip->flags & BTRFS_INODE_NOATIME)
117 		inode->i_flags |= S_NOATIME;
118 	if (ip->flags & BTRFS_INODE_DIRSYNC)
119 		inode->i_flags |= S_DIRSYNC;
120 }
121 
122 /*
123  * Inherit flags from the parent inode.
124  *
125  * Currently only the compression flags and the cow flags are inherited.
126  */
127 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
128 {
129 	unsigned int flags;
130 
131 	if (!dir)
132 		return;
133 
134 	flags = BTRFS_I(dir)->flags;
135 
136 	if (flags & BTRFS_INODE_NOCOMPRESS) {
137 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
138 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
139 	} else if (flags & BTRFS_INODE_COMPRESS) {
140 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
141 		BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
142 	}
143 
144 	if (flags & BTRFS_INODE_NODATACOW) {
145 		BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
146 		if (S_ISREG(inode->i_mode))
147 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
148 	}
149 
150 	btrfs_update_iflags(inode);
151 }
152 
153 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
154 {
155 	struct btrfs_inode *ip = BTRFS_I(file_inode(file));
156 	unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
157 
158 	if (copy_to_user(arg, &flags, sizeof(flags)))
159 		return -EFAULT;
160 	return 0;
161 }
162 
163 static int check_flags(unsigned int flags)
164 {
165 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
166 		      FS_NOATIME_FL | FS_NODUMP_FL | \
167 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
168 		      FS_NOCOMP_FL | FS_COMPR_FL |
169 		      FS_NOCOW_FL))
170 		return -EOPNOTSUPP;
171 
172 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
173 		return -EINVAL;
174 
175 	return 0;
176 }
177 
178 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
179 {
180 	struct inode *inode = file_inode(file);
181 	struct btrfs_inode *ip = BTRFS_I(inode);
182 	struct btrfs_root *root = ip->root;
183 	struct btrfs_trans_handle *trans;
184 	unsigned int flags, oldflags;
185 	int ret;
186 	u64 ip_oldflags;
187 	unsigned int i_oldflags;
188 	umode_t mode;
189 
190 	if (btrfs_root_readonly(root))
191 		return -EROFS;
192 
193 	if (copy_from_user(&flags, arg, sizeof(flags)))
194 		return -EFAULT;
195 
196 	ret = check_flags(flags);
197 	if (ret)
198 		return ret;
199 
200 	if (!inode_owner_or_capable(inode))
201 		return -EACCES;
202 
203 	ret = mnt_want_write_file(file);
204 	if (ret)
205 		return ret;
206 
207 	mutex_lock(&inode->i_mutex);
208 
209 	ip_oldflags = ip->flags;
210 	i_oldflags = inode->i_flags;
211 	mode = inode->i_mode;
212 
213 	flags = btrfs_mask_flags(inode->i_mode, flags);
214 	oldflags = btrfs_flags_to_ioctl(ip->flags);
215 	if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
216 		if (!capable(CAP_LINUX_IMMUTABLE)) {
217 			ret = -EPERM;
218 			goto out_unlock;
219 		}
220 	}
221 
222 	if (flags & FS_SYNC_FL)
223 		ip->flags |= BTRFS_INODE_SYNC;
224 	else
225 		ip->flags &= ~BTRFS_INODE_SYNC;
226 	if (flags & FS_IMMUTABLE_FL)
227 		ip->flags |= BTRFS_INODE_IMMUTABLE;
228 	else
229 		ip->flags &= ~BTRFS_INODE_IMMUTABLE;
230 	if (flags & FS_APPEND_FL)
231 		ip->flags |= BTRFS_INODE_APPEND;
232 	else
233 		ip->flags &= ~BTRFS_INODE_APPEND;
234 	if (flags & FS_NODUMP_FL)
235 		ip->flags |= BTRFS_INODE_NODUMP;
236 	else
237 		ip->flags &= ~BTRFS_INODE_NODUMP;
238 	if (flags & FS_NOATIME_FL)
239 		ip->flags |= BTRFS_INODE_NOATIME;
240 	else
241 		ip->flags &= ~BTRFS_INODE_NOATIME;
242 	if (flags & FS_DIRSYNC_FL)
243 		ip->flags |= BTRFS_INODE_DIRSYNC;
244 	else
245 		ip->flags &= ~BTRFS_INODE_DIRSYNC;
246 	if (flags & FS_NOCOW_FL) {
247 		if (S_ISREG(mode)) {
248 			/*
249 			 * It's safe to turn csums off here, no extents exist.
250 			 * Otherwise we want the flag to reflect the real COW
251 			 * status of the file and will not set it.
252 			 */
253 			if (inode->i_size == 0)
254 				ip->flags |= BTRFS_INODE_NODATACOW
255 					   | BTRFS_INODE_NODATASUM;
256 		} else {
257 			ip->flags |= BTRFS_INODE_NODATACOW;
258 		}
259 	} else {
260 		/*
261 		 * Revert back under same assuptions as above
262 		 */
263 		if (S_ISREG(mode)) {
264 			if (inode->i_size == 0)
265 				ip->flags &= ~(BTRFS_INODE_NODATACOW
266 				             | BTRFS_INODE_NODATASUM);
267 		} else {
268 			ip->flags &= ~BTRFS_INODE_NODATACOW;
269 		}
270 	}
271 
272 	/*
273 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
274 	 * flag may be changed automatically if compression code won't make
275 	 * things smaller.
276 	 */
277 	if (flags & FS_NOCOMP_FL) {
278 		ip->flags &= ~BTRFS_INODE_COMPRESS;
279 		ip->flags |= BTRFS_INODE_NOCOMPRESS;
280 	} else if (flags & FS_COMPR_FL) {
281 		ip->flags |= BTRFS_INODE_COMPRESS;
282 		ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
283 	} else {
284 		ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
285 	}
286 
287 	trans = btrfs_start_transaction(root, 1);
288 	if (IS_ERR(trans)) {
289 		ret = PTR_ERR(trans);
290 		goto out_drop;
291 	}
292 
293 	btrfs_update_iflags(inode);
294 	inode_inc_iversion(inode);
295 	inode->i_ctime = CURRENT_TIME;
296 	ret = btrfs_update_inode(trans, root, inode);
297 
298 	btrfs_end_transaction(trans, root);
299  out_drop:
300 	if (ret) {
301 		ip->flags = ip_oldflags;
302 		inode->i_flags = i_oldflags;
303 	}
304 
305  out_unlock:
306 	mutex_unlock(&inode->i_mutex);
307 	mnt_drop_write_file(file);
308 	return ret;
309 }
310 
311 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
312 {
313 	struct inode *inode = file_inode(file);
314 
315 	return put_user(inode->i_generation, arg);
316 }
317 
318 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
319 {
320 	struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
321 	struct btrfs_device *device;
322 	struct request_queue *q;
323 	struct fstrim_range range;
324 	u64 minlen = ULLONG_MAX;
325 	u64 num_devices = 0;
326 	u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
327 	int ret;
328 
329 	if (!capable(CAP_SYS_ADMIN))
330 		return -EPERM;
331 
332 	rcu_read_lock();
333 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
334 				dev_list) {
335 		if (!device->bdev)
336 			continue;
337 		q = bdev_get_queue(device->bdev);
338 		if (blk_queue_discard(q)) {
339 			num_devices++;
340 			minlen = min((u64)q->limits.discard_granularity,
341 				     minlen);
342 		}
343 	}
344 	rcu_read_unlock();
345 
346 	if (!num_devices)
347 		return -EOPNOTSUPP;
348 	if (copy_from_user(&range, arg, sizeof(range)))
349 		return -EFAULT;
350 	if (range.start > total_bytes ||
351 	    range.len < fs_info->sb->s_blocksize)
352 		return -EINVAL;
353 
354 	range.len = min(range.len, total_bytes - range.start);
355 	range.minlen = max(range.minlen, minlen);
356 	ret = btrfs_trim_fs(fs_info->tree_root, &range);
357 	if (ret < 0)
358 		return ret;
359 
360 	if (copy_to_user(arg, &range, sizeof(range)))
361 		return -EFAULT;
362 
363 	return 0;
364 }
365 
366 static noinline int create_subvol(struct inode *dir,
367 				  struct dentry *dentry,
368 				  char *name, int namelen,
369 				  u64 *async_transid,
370 				  struct btrfs_qgroup_inherit *inherit)
371 {
372 	struct btrfs_trans_handle *trans;
373 	struct btrfs_key key;
374 	struct btrfs_root_item root_item;
375 	struct btrfs_inode_item *inode_item;
376 	struct extent_buffer *leaf;
377 	struct btrfs_root *root = BTRFS_I(dir)->root;
378 	struct btrfs_root *new_root;
379 	struct btrfs_block_rsv block_rsv;
380 	struct timespec cur_time = CURRENT_TIME;
381 	int ret;
382 	int err;
383 	u64 objectid;
384 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
385 	u64 index = 0;
386 	u64 qgroup_reserved;
387 	uuid_le new_uuid;
388 
389 	ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
390 	if (ret)
391 		return ret;
392 
393 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
394 	/*
395 	 * The same as the snapshot creation, please see the comment
396 	 * of create_snapshot().
397 	 */
398 	ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
399 					       7, &qgroup_reserved);
400 	if (ret)
401 		return ret;
402 
403 	trans = btrfs_start_transaction(root, 0);
404 	if (IS_ERR(trans)) {
405 		ret = PTR_ERR(trans);
406 		goto out;
407 	}
408 	trans->block_rsv = &block_rsv;
409 	trans->bytes_reserved = block_rsv.size;
410 
411 	ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
412 	if (ret)
413 		goto fail;
414 
415 	leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
416 				      0, objectid, NULL, 0, 0, 0);
417 	if (IS_ERR(leaf)) {
418 		ret = PTR_ERR(leaf);
419 		goto fail;
420 	}
421 
422 	memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
423 	btrfs_set_header_bytenr(leaf, leaf->start);
424 	btrfs_set_header_generation(leaf, trans->transid);
425 	btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
426 	btrfs_set_header_owner(leaf, objectid);
427 
428 	write_extent_buffer(leaf, root->fs_info->fsid,
429 			    (unsigned long)btrfs_header_fsid(leaf),
430 			    BTRFS_FSID_SIZE);
431 	write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
432 			    (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
433 			    BTRFS_UUID_SIZE);
434 	btrfs_mark_buffer_dirty(leaf);
435 
436 	memset(&root_item, 0, sizeof(root_item));
437 
438 	inode_item = &root_item.inode;
439 	inode_item->generation = cpu_to_le64(1);
440 	inode_item->size = cpu_to_le64(3);
441 	inode_item->nlink = cpu_to_le32(1);
442 	inode_item->nbytes = cpu_to_le64(root->leafsize);
443 	inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
444 
445 	root_item.flags = 0;
446 	root_item.byte_limit = 0;
447 	inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
448 
449 	btrfs_set_root_bytenr(&root_item, leaf->start);
450 	btrfs_set_root_generation(&root_item, trans->transid);
451 	btrfs_set_root_level(&root_item, 0);
452 	btrfs_set_root_refs(&root_item, 1);
453 	btrfs_set_root_used(&root_item, leaf->len);
454 	btrfs_set_root_last_snapshot(&root_item, 0);
455 
456 	btrfs_set_root_generation_v2(&root_item,
457 			btrfs_root_generation(&root_item));
458 	uuid_le_gen(&new_uuid);
459 	memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
460 	root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
461 	root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
462 	root_item.ctime = root_item.otime;
463 	btrfs_set_root_ctransid(&root_item, trans->transid);
464 	btrfs_set_root_otransid(&root_item, trans->transid);
465 
466 	btrfs_tree_unlock(leaf);
467 	free_extent_buffer(leaf);
468 	leaf = NULL;
469 
470 	btrfs_set_root_dirid(&root_item, new_dirid);
471 
472 	key.objectid = objectid;
473 	key.offset = 0;
474 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
475 	ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
476 				&root_item);
477 	if (ret)
478 		goto fail;
479 
480 	key.offset = (u64)-1;
481 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
482 	if (IS_ERR(new_root)) {
483 		btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
484 		ret = PTR_ERR(new_root);
485 		goto fail;
486 	}
487 
488 	btrfs_record_root_in_trans(trans, new_root);
489 
490 	ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
491 	if (ret) {
492 		/* We potentially lose an unused inode item here */
493 		btrfs_abort_transaction(trans, root, ret);
494 		goto fail;
495 	}
496 
497 	/*
498 	 * insert the directory item
499 	 */
500 	ret = btrfs_set_inode_index(dir, &index);
501 	if (ret) {
502 		btrfs_abort_transaction(trans, root, ret);
503 		goto fail;
504 	}
505 
506 	ret = btrfs_insert_dir_item(trans, root,
507 				    name, namelen, dir, &key,
508 				    BTRFS_FT_DIR, index);
509 	if (ret) {
510 		btrfs_abort_transaction(trans, root, ret);
511 		goto fail;
512 	}
513 
514 	btrfs_i_size_write(dir, dir->i_size + namelen * 2);
515 	ret = btrfs_update_inode(trans, root, dir);
516 	BUG_ON(ret);
517 
518 	ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
519 				 objectid, root->root_key.objectid,
520 				 btrfs_ino(dir), index, name, namelen);
521 
522 	BUG_ON(ret);
523 
524 fail:
525 	trans->block_rsv = NULL;
526 	trans->bytes_reserved = 0;
527 	if (async_transid) {
528 		*async_transid = trans->transid;
529 		err = btrfs_commit_transaction_async(trans, root, 1);
530 		if (err)
531 			err = btrfs_commit_transaction(trans, root);
532 	} else {
533 		err = btrfs_commit_transaction(trans, root);
534 	}
535 	if (err && !ret)
536 		ret = err;
537 
538 	if (!ret)
539 		d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
540 out:
541 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
542 	return ret;
543 }
544 
545 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
546 			   struct dentry *dentry, char *name, int namelen,
547 			   u64 *async_transid, bool readonly,
548 			   struct btrfs_qgroup_inherit *inherit)
549 {
550 	struct inode *inode;
551 	struct btrfs_pending_snapshot *pending_snapshot;
552 	struct btrfs_trans_handle *trans;
553 	int ret;
554 
555 	if (!root->ref_cows)
556 		return -EINVAL;
557 
558 	ret = btrfs_start_delalloc_inodes(root, 0);
559 	if (ret)
560 		return ret;
561 
562 	btrfs_wait_ordered_extents(root, 0);
563 
564 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
565 	if (!pending_snapshot)
566 		return -ENOMEM;
567 
568 	btrfs_init_block_rsv(&pending_snapshot->block_rsv,
569 			     BTRFS_BLOCK_RSV_TEMP);
570 	/*
571 	 * 1 - parent dir inode
572 	 * 2 - dir entries
573 	 * 1 - root item
574 	 * 2 - root ref/backref
575 	 * 1 - root of snapshot
576 	 */
577 	ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
578 					&pending_snapshot->block_rsv, 7,
579 					&pending_snapshot->qgroup_reserved);
580 	if (ret)
581 		goto out;
582 
583 	pending_snapshot->dentry = dentry;
584 	pending_snapshot->root = root;
585 	pending_snapshot->readonly = readonly;
586 	pending_snapshot->dir = dir;
587 	pending_snapshot->inherit = inherit;
588 
589 	trans = btrfs_start_transaction(root, 0);
590 	if (IS_ERR(trans)) {
591 		ret = PTR_ERR(trans);
592 		goto fail;
593 	}
594 
595 	spin_lock(&root->fs_info->trans_lock);
596 	list_add(&pending_snapshot->list,
597 		 &trans->transaction->pending_snapshots);
598 	spin_unlock(&root->fs_info->trans_lock);
599 	if (async_transid) {
600 		*async_transid = trans->transid;
601 		ret = btrfs_commit_transaction_async(trans,
602 				     root->fs_info->extent_root, 1);
603 		if (ret)
604 			ret = btrfs_commit_transaction(trans, root);
605 	} else {
606 		ret = btrfs_commit_transaction(trans,
607 					       root->fs_info->extent_root);
608 	}
609 	if (ret)
610 		goto fail;
611 
612 	ret = pending_snapshot->error;
613 	if (ret)
614 		goto fail;
615 
616 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
617 	if (ret)
618 		goto fail;
619 
620 	inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
621 	if (IS_ERR(inode)) {
622 		ret = PTR_ERR(inode);
623 		goto fail;
624 	}
625 	BUG_ON(!inode);
626 	d_instantiate(dentry, inode);
627 	ret = 0;
628 fail:
629 	btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
630 					 &pending_snapshot->block_rsv,
631 					 pending_snapshot->qgroup_reserved);
632 out:
633 	kfree(pending_snapshot);
634 	return ret;
635 }
636 
637 /*  copy of check_sticky in fs/namei.c()
638 * It's inline, so penalty for filesystems that don't use sticky bit is
639 * minimal.
640 */
641 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
642 {
643 	kuid_t fsuid = current_fsuid();
644 
645 	if (!(dir->i_mode & S_ISVTX))
646 		return 0;
647 	if (uid_eq(inode->i_uid, fsuid))
648 		return 0;
649 	if (uid_eq(dir->i_uid, fsuid))
650 		return 0;
651 	return !capable(CAP_FOWNER);
652 }
653 
654 /*  copy of may_delete in fs/namei.c()
655  *	Check whether we can remove a link victim from directory dir, check
656  *  whether the type of victim is right.
657  *  1. We can't do it if dir is read-only (done in permission())
658  *  2. We should have write and exec permissions on dir
659  *  3. We can't remove anything from append-only dir
660  *  4. We can't do anything with immutable dir (done in permission())
661  *  5. If the sticky bit on dir is set we should either
662  *	a. be owner of dir, or
663  *	b. be owner of victim, or
664  *	c. have CAP_FOWNER capability
665  *  6. If the victim is append-only or immutable we can't do antyhing with
666  *     links pointing to it.
667  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
668  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
669  *  9. We can't remove a root or mountpoint.
670  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
671  *     nfs_async_unlink().
672  */
673 
674 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
675 {
676 	int error;
677 
678 	if (!victim->d_inode)
679 		return -ENOENT;
680 
681 	BUG_ON(victim->d_parent->d_inode != dir);
682 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
683 
684 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
685 	if (error)
686 		return error;
687 	if (IS_APPEND(dir))
688 		return -EPERM;
689 	if (btrfs_check_sticky(dir, victim->d_inode)||
690 		IS_APPEND(victim->d_inode)||
691 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
692 		return -EPERM;
693 	if (isdir) {
694 		if (!S_ISDIR(victim->d_inode->i_mode))
695 			return -ENOTDIR;
696 		if (IS_ROOT(victim))
697 			return -EBUSY;
698 	} else if (S_ISDIR(victim->d_inode->i_mode))
699 		return -EISDIR;
700 	if (IS_DEADDIR(dir))
701 		return -ENOENT;
702 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
703 		return -EBUSY;
704 	return 0;
705 }
706 
707 /* copy of may_create in fs/namei.c() */
708 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
709 {
710 	if (child->d_inode)
711 		return -EEXIST;
712 	if (IS_DEADDIR(dir))
713 		return -ENOENT;
714 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
715 }
716 
717 /*
718  * Create a new subvolume below @parent.  This is largely modeled after
719  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
720  * inside this filesystem so it's quite a bit simpler.
721  */
722 static noinline int btrfs_mksubvol(struct path *parent,
723 				   char *name, int namelen,
724 				   struct btrfs_root *snap_src,
725 				   u64 *async_transid, bool readonly,
726 				   struct btrfs_qgroup_inherit *inherit)
727 {
728 	struct inode *dir  = parent->dentry->d_inode;
729 	struct dentry *dentry;
730 	int error;
731 
732 	error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
733 	if (error == -EINTR)
734 		return error;
735 
736 	dentry = lookup_one_len(name, parent->dentry, namelen);
737 	error = PTR_ERR(dentry);
738 	if (IS_ERR(dentry))
739 		goto out_unlock;
740 
741 	error = -EEXIST;
742 	if (dentry->d_inode)
743 		goto out_dput;
744 
745 	error = btrfs_may_create(dir, dentry);
746 	if (error)
747 		goto out_dput;
748 
749 	/*
750 	 * even if this name doesn't exist, we may get hash collisions.
751 	 * check for them now when we can safely fail
752 	 */
753 	error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
754 					       dir->i_ino, name,
755 					       namelen);
756 	if (error)
757 		goto out_dput;
758 
759 	down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
760 
761 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
762 		goto out_up_read;
763 
764 	if (snap_src) {
765 		error = create_snapshot(snap_src, dir, dentry, name, namelen,
766 					async_transid, readonly, inherit);
767 	} else {
768 		error = create_subvol(dir, dentry, name, namelen,
769 				      async_transid, inherit);
770 	}
771 	if (!error)
772 		fsnotify_mkdir(dir, dentry);
773 out_up_read:
774 	up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
775 out_dput:
776 	dput(dentry);
777 out_unlock:
778 	mutex_unlock(&dir->i_mutex);
779 	return error;
780 }
781 
782 /*
783  * When we're defragging a range, we don't want to kick it off again
784  * if it is really just waiting for delalloc to send it down.
785  * If we find a nice big extent or delalloc range for the bytes in the
786  * file you want to defrag, we return 0 to let you know to skip this
787  * part of the file
788  */
789 static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
790 {
791 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
792 	struct extent_map *em = NULL;
793 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
794 	u64 end;
795 
796 	read_lock(&em_tree->lock);
797 	em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
798 	read_unlock(&em_tree->lock);
799 
800 	if (em) {
801 		end = extent_map_end(em);
802 		free_extent_map(em);
803 		if (end - offset > thresh)
804 			return 0;
805 	}
806 	/* if we already have a nice delalloc here, just stop */
807 	thresh /= 2;
808 	end = count_range_bits(io_tree, &offset, offset + thresh,
809 			       thresh, EXTENT_DELALLOC, 1);
810 	if (end >= thresh)
811 		return 0;
812 	return 1;
813 }
814 
815 /*
816  * helper function to walk through a file and find extents
817  * newer than a specific transid, and smaller than thresh.
818  *
819  * This is used by the defragging code to find new and small
820  * extents
821  */
822 static int find_new_extents(struct btrfs_root *root,
823 			    struct inode *inode, u64 newer_than,
824 			    u64 *off, int thresh)
825 {
826 	struct btrfs_path *path;
827 	struct btrfs_key min_key;
828 	struct btrfs_key max_key;
829 	struct extent_buffer *leaf;
830 	struct btrfs_file_extent_item *extent;
831 	int type;
832 	int ret;
833 	u64 ino = btrfs_ino(inode);
834 
835 	path = btrfs_alloc_path();
836 	if (!path)
837 		return -ENOMEM;
838 
839 	min_key.objectid = ino;
840 	min_key.type = BTRFS_EXTENT_DATA_KEY;
841 	min_key.offset = *off;
842 
843 	max_key.objectid = ino;
844 	max_key.type = (u8)-1;
845 	max_key.offset = (u64)-1;
846 
847 	path->keep_locks = 1;
848 
849 	while(1) {
850 		ret = btrfs_search_forward(root, &min_key, &max_key,
851 					   path, newer_than);
852 		if (ret != 0)
853 			goto none;
854 		if (min_key.objectid != ino)
855 			goto none;
856 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
857 			goto none;
858 
859 		leaf = path->nodes[0];
860 		extent = btrfs_item_ptr(leaf, path->slots[0],
861 					struct btrfs_file_extent_item);
862 
863 		type = btrfs_file_extent_type(leaf, extent);
864 		if (type == BTRFS_FILE_EXTENT_REG &&
865 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
866 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
867 			*off = min_key.offset;
868 			btrfs_free_path(path);
869 			return 0;
870 		}
871 
872 		if (min_key.offset == (u64)-1)
873 			goto none;
874 
875 		min_key.offset++;
876 		btrfs_release_path(path);
877 	}
878 none:
879 	btrfs_free_path(path);
880 	return -ENOENT;
881 }
882 
883 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
884 {
885 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
886 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
887 	struct extent_map *em;
888 	u64 len = PAGE_CACHE_SIZE;
889 
890 	/*
891 	 * hopefully we have this extent in the tree already, try without
892 	 * the full extent lock
893 	 */
894 	read_lock(&em_tree->lock);
895 	em = lookup_extent_mapping(em_tree, start, len);
896 	read_unlock(&em_tree->lock);
897 
898 	if (!em) {
899 		/* get the big lock and read metadata off disk */
900 		lock_extent(io_tree, start, start + len - 1);
901 		em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
902 		unlock_extent(io_tree, start, start + len - 1);
903 
904 		if (IS_ERR(em))
905 			return NULL;
906 	}
907 
908 	return em;
909 }
910 
911 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
912 {
913 	struct extent_map *next;
914 	bool ret = true;
915 
916 	/* this is the last extent */
917 	if (em->start + em->len >= i_size_read(inode))
918 		return false;
919 
920 	next = defrag_lookup_extent(inode, em->start + em->len);
921 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
922 		ret = false;
923 
924 	free_extent_map(next);
925 	return ret;
926 }
927 
928 static int should_defrag_range(struct inode *inode, u64 start, int thresh,
929 			       u64 *last_len, u64 *skip, u64 *defrag_end,
930 			       int compress)
931 {
932 	struct extent_map *em;
933 	int ret = 1;
934 	bool next_mergeable = true;
935 
936 	/*
937 	 * make sure that once we start defragging an extent, we keep on
938 	 * defragging it
939 	 */
940 	if (start < *defrag_end)
941 		return 1;
942 
943 	*skip = 0;
944 
945 	em = defrag_lookup_extent(inode, start);
946 	if (!em)
947 		return 0;
948 
949 	/* this will cover holes, and inline extents */
950 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
951 		ret = 0;
952 		goto out;
953 	}
954 
955 	next_mergeable = defrag_check_next_extent(inode, em);
956 
957 	/*
958 	 * we hit a real extent, if it is big or the next extent is not a
959 	 * real extent, don't bother defragging it
960 	 */
961 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
962 	    (em->len >= thresh || !next_mergeable))
963 		ret = 0;
964 out:
965 	/*
966 	 * last_len ends up being a counter of how many bytes we've defragged.
967 	 * every time we choose not to defrag an extent, we reset *last_len
968 	 * so that the next tiny extent will force a defrag.
969 	 *
970 	 * The end result of this is that tiny extents before a single big
971 	 * extent will force at least part of that big extent to be defragged.
972 	 */
973 	if (ret) {
974 		*defrag_end = extent_map_end(em);
975 	} else {
976 		*last_len = 0;
977 		*skip = extent_map_end(em);
978 		*defrag_end = 0;
979 	}
980 
981 	free_extent_map(em);
982 	return ret;
983 }
984 
985 /*
986  * it doesn't do much good to defrag one or two pages
987  * at a time.  This pulls in a nice chunk of pages
988  * to COW and defrag.
989  *
990  * It also makes sure the delalloc code has enough
991  * dirty data to avoid making new small extents as part
992  * of the defrag
993  *
994  * It's a good idea to start RA on this range
995  * before calling this.
996  */
997 static int cluster_pages_for_defrag(struct inode *inode,
998 				    struct page **pages,
999 				    unsigned long start_index,
1000 				    int num_pages)
1001 {
1002 	unsigned long file_end;
1003 	u64 isize = i_size_read(inode);
1004 	u64 page_start;
1005 	u64 page_end;
1006 	u64 page_cnt;
1007 	int ret;
1008 	int i;
1009 	int i_done;
1010 	struct btrfs_ordered_extent *ordered;
1011 	struct extent_state *cached_state = NULL;
1012 	struct extent_io_tree *tree;
1013 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1014 
1015 	file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
1016 	if (!isize || start_index > file_end)
1017 		return 0;
1018 
1019 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1020 
1021 	ret = btrfs_delalloc_reserve_space(inode,
1022 					   page_cnt << PAGE_CACHE_SHIFT);
1023 	if (ret)
1024 		return ret;
1025 	i_done = 0;
1026 	tree = &BTRFS_I(inode)->io_tree;
1027 
1028 	/* step one, lock all the pages */
1029 	for (i = 0; i < page_cnt; i++) {
1030 		struct page *page;
1031 again:
1032 		page = find_or_create_page(inode->i_mapping,
1033 					   start_index + i, mask);
1034 		if (!page)
1035 			break;
1036 
1037 		page_start = page_offset(page);
1038 		page_end = page_start + PAGE_CACHE_SIZE - 1;
1039 		while (1) {
1040 			lock_extent(tree, page_start, page_end);
1041 			ordered = btrfs_lookup_ordered_extent(inode,
1042 							      page_start);
1043 			unlock_extent(tree, page_start, page_end);
1044 			if (!ordered)
1045 				break;
1046 
1047 			unlock_page(page);
1048 			btrfs_start_ordered_extent(inode, ordered, 1);
1049 			btrfs_put_ordered_extent(ordered);
1050 			lock_page(page);
1051 			/*
1052 			 * we unlocked the page above, so we need check if
1053 			 * it was released or not.
1054 			 */
1055 			if (page->mapping != inode->i_mapping) {
1056 				unlock_page(page);
1057 				page_cache_release(page);
1058 				goto again;
1059 			}
1060 		}
1061 
1062 		if (!PageUptodate(page)) {
1063 			btrfs_readpage(NULL, page);
1064 			lock_page(page);
1065 			if (!PageUptodate(page)) {
1066 				unlock_page(page);
1067 				page_cache_release(page);
1068 				ret = -EIO;
1069 				break;
1070 			}
1071 		}
1072 
1073 		if (page->mapping != inode->i_mapping) {
1074 			unlock_page(page);
1075 			page_cache_release(page);
1076 			goto again;
1077 		}
1078 
1079 		pages[i] = page;
1080 		i_done++;
1081 	}
1082 	if (!i_done || ret)
1083 		goto out;
1084 
1085 	if (!(inode->i_sb->s_flags & MS_ACTIVE))
1086 		goto out;
1087 
1088 	/*
1089 	 * so now we have a nice long stream of locked
1090 	 * and up to date pages, lets wait on them
1091 	 */
1092 	for (i = 0; i < i_done; i++)
1093 		wait_on_page_writeback(pages[i]);
1094 
1095 	page_start = page_offset(pages[0]);
1096 	page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1097 
1098 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
1099 			 page_start, page_end - 1, 0, &cached_state);
1100 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1101 			  page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1102 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1103 			  &cached_state, GFP_NOFS);
1104 
1105 	if (i_done != page_cnt) {
1106 		spin_lock(&BTRFS_I(inode)->lock);
1107 		BTRFS_I(inode)->outstanding_extents++;
1108 		spin_unlock(&BTRFS_I(inode)->lock);
1109 		btrfs_delalloc_release_space(inode,
1110 				     (page_cnt - i_done) << PAGE_CACHE_SHIFT);
1111 	}
1112 
1113 
1114 	set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1115 			  &cached_state, GFP_NOFS);
1116 
1117 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1118 			     page_start, page_end - 1, &cached_state,
1119 			     GFP_NOFS);
1120 
1121 	for (i = 0; i < i_done; i++) {
1122 		clear_page_dirty_for_io(pages[i]);
1123 		ClearPageChecked(pages[i]);
1124 		set_page_extent_mapped(pages[i]);
1125 		set_page_dirty(pages[i]);
1126 		unlock_page(pages[i]);
1127 		page_cache_release(pages[i]);
1128 	}
1129 	return i_done;
1130 out:
1131 	for (i = 0; i < i_done; i++) {
1132 		unlock_page(pages[i]);
1133 		page_cache_release(pages[i]);
1134 	}
1135 	btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
1136 	return ret;
1137 
1138 }
1139 
1140 int btrfs_defrag_file(struct inode *inode, struct file *file,
1141 		      struct btrfs_ioctl_defrag_range_args *range,
1142 		      u64 newer_than, unsigned long max_to_defrag)
1143 {
1144 	struct btrfs_root *root = BTRFS_I(inode)->root;
1145 	struct file_ra_state *ra = NULL;
1146 	unsigned long last_index;
1147 	u64 isize = i_size_read(inode);
1148 	u64 last_len = 0;
1149 	u64 skip = 0;
1150 	u64 defrag_end = 0;
1151 	u64 newer_off = range->start;
1152 	unsigned long i;
1153 	unsigned long ra_index = 0;
1154 	int ret;
1155 	int defrag_count = 0;
1156 	int compress_type = BTRFS_COMPRESS_ZLIB;
1157 	int extent_thresh = range->extent_thresh;
1158 	int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1159 	int cluster = max_cluster;
1160 	u64 new_align = ~((u64)128 * 1024 - 1);
1161 	struct page **pages = NULL;
1162 
1163 	if (isize == 0)
1164 		return 0;
1165 
1166 	if (range->start >= isize)
1167 		return -EINVAL;
1168 
1169 	if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1170 		if (range->compress_type > BTRFS_COMPRESS_TYPES)
1171 			return -EINVAL;
1172 		if (range->compress_type)
1173 			compress_type = range->compress_type;
1174 	}
1175 
1176 	if (extent_thresh == 0)
1177 		extent_thresh = 256 * 1024;
1178 
1179 	/*
1180 	 * if we were not given a file, allocate a readahead
1181 	 * context
1182 	 */
1183 	if (!file) {
1184 		ra = kzalloc(sizeof(*ra), GFP_NOFS);
1185 		if (!ra)
1186 			return -ENOMEM;
1187 		file_ra_state_init(ra, inode->i_mapping);
1188 	} else {
1189 		ra = &file->f_ra;
1190 	}
1191 
1192 	pages = kmalloc(sizeof(struct page *) * max_cluster,
1193 			GFP_NOFS);
1194 	if (!pages) {
1195 		ret = -ENOMEM;
1196 		goto out_ra;
1197 	}
1198 
1199 	/* find the last page to defrag */
1200 	if (range->start + range->len > range->start) {
1201 		last_index = min_t(u64, isize - 1,
1202 			 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1203 	} else {
1204 		last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1205 	}
1206 
1207 	if (newer_than) {
1208 		ret = find_new_extents(root, inode, newer_than,
1209 				       &newer_off, 64 * 1024);
1210 		if (!ret) {
1211 			range->start = newer_off;
1212 			/*
1213 			 * we always align our defrag to help keep
1214 			 * the extents in the file evenly spaced
1215 			 */
1216 			i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1217 		} else
1218 			goto out_ra;
1219 	} else {
1220 		i = range->start >> PAGE_CACHE_SHIFT;
1221 	}
1222 	if (!max_to_defrag)
1223 		max_to_defrag = last_index + 1;
1224 
1225 	/*
1226 	 * make writeback starts from i, so the defrag range can be
1227 	 * written sequentially.
1228 	 */
1229 	if (i < inode->i_mapping->writeback_index)
1230 		inode->i_mapping->writeback_index = i;
1231 
1232 	while (i <= last_index && defrag_count < max_to_defrag &&
1233 	       (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1234 		PAGE_CACHE_SHIFT)) {
1235 		/*
1236 		 * make sure we stop running if someone unmounts
1237 		 * the FS
1238 		 */
1239 		if (!(inode->i_sb->s_flags & MS_ACTIVE))
1240 			break;
1241 
1242 		if (btrfs_defrag_cancelled(root->fs_info)) {
1243 			printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1244 			ret = -EAGAIN;
1245 			break;
1246 		}
1247 
1248 		if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1249 					 extent_thresh, &last_len, &skip,
1250 					 &defrag_end, range->flags &
1251 					 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1252 			unsigned long next;
1253 			/*
1254 			 * the should_defrag function tells us how much to skip
1255 			 * bump our counter by the suggested amount
1256 			 */
1257 			next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1258 			i = max(i + 1, next);
1259 			continue;
1260 		}
1261 
1262 		if (!newer_than) {
1263 			cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1264 				   PAGE_CACHE_SHIFT) - i;
1265 			cluster = min(cluster, max_cluster);
1266 		} else {
1267 			cluster = max_cluster;
1268 		}
1269 
1270 		if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1271 			BTRFS_I(inode)->force_compress = compress_type;
1272 
1273 		if (i + cluster > ra_index) {
1274 			ra_index = max(i, ra_index);
1275 			btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1276 				       cluster);
1277 			ra_index += max_cluster;
1278 		}
1279 
1280 		mutex_lock(&inode->i_mutex);
1281 		ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1282 		if (ret < 0) {
1283 			mutex_unlock(&inode->i_mutex);
1284 			goto out_ra;
1285 		}
1286 
1287 		defrag_count += ret;
1288 		balance_dirty_pages_ratelimited(inode->i_mapping);
1289 		mutex_unlock(&inode->i_mutex);
1290 
1291 		if (newer_than) {
1292 			if (newer_off == (u64)-1)
1293 				break;
1294 
1295 			if (ret > 0)
1296 				i += ret;
1297 
1298 			newer_off = max(newer_off + 1,
1299 					(u64)i << PAGE_CACHE_SHIFT);
1300 
1301 			ret = find_new_extents(root, inode,
1302 					       newer_than, &newer_off,
1303 					       64 * 1024);
1304 			if (!ret) {
1305 				range->start = newer_off;
1306 				i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1307 			} else {
1308 				break;
1309 			}
1310 		} else {
1311 			if (ret > 0) {
1312 				i += ret;
1313 				last_len += ret << PAGE_CACHE_SHIFT;
1314 			} else {
1315 				i++;
1316 				last_len = 0;
1317 			}
1318 		}
1319 	}
1320 
1321 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1322 		filemap_flush(inode->i_mapping);
1323 
1324 	if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1325 		/* the filemap_flush will queue IO into the worker threads, but
1326 		 * we have to make sure the IO is actually started and that
1327 		 * ordered extents get created before we return
1328 		 */
1329 		atomic_inc(&root->fs_info->async_submit_draining);
1330 		while (atomic_read(&root->fs_info->nr_async_submits) ||
1331 		      atomic_read(&root->fs_info->async_delalloc_pages)) {
1332 			wait_event(root->fs_info->async_submit_wait,
1333 			   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1334 			    atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1335 		}
1336 		atomic_dec(&root->fs_info->async_submit_draining);
1337 
1338 		mutex_lock(&inode->i_mutex);
1339 		BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1340 		mutex_unlock(&inode->i_mutex);
1341 	}
1342 
1343 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
1344 		btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
1345 	}
1346 
1347 	ret = defrag_count;
1348 
1349 out_ra:
1350 	if (!file)
1351 		kfree(ra);
1352 	kfree(pages);
1353 	return ret;
1354 }
1355 
1356 static noinline int btrfs_ioctl_resize(struct file *file,
1357 					void __user *arg)
1358 {
1359 	u64 new_size;
1360 	u64 old_size;
1361 	u64 devid = 1;
1362 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1363 	struct btrfs_ioctl_vol_args *vol_args;
1364 	struct btrfs_trans_handle *trans;
1365 	struct btrfs_device *device = NULL;
1366 	char *sizestr;
1367 	char *devstr = NULL;
1368 	int ret = 0;
1369 	int mod = 0;
1370 
1371 	if (!capable(CAP_SYS_ADMIN))
1372 		return -EPERM;
1373 
1374 	ret = mnt_want_write_file(file);
1375 	if (ret)
1376 		return ret;
1377 
1378 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1379 			1)) {
1380 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
1381 		mnt_drop_write_file(file);
1382 		return -EINVAL;
1383 	}
1384 
1385 	mutex_lock(&root->fs_info->volume_mutex);
1386 	vol_args = memdup_user(arg, sizeof(*vol_args));
1387 	if (IS_ERR(vol_args)) {
1388 		ret = PTR_ERR(vol_args);
1389 		goto out;
1390 	}
1391 
1392 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1393 
1394 	sizestr = vol_args->name;
1395 	devstr = strchr(sizestr, ':');
1396 	if (devstr) {
1397 		char *end;
1398 		sizestr = devstr + 1;
1399 		*devstr = '\0';
1400 		devstr = vol_args->name;
1401 		devid = simple_strtoull(devstr, &end, 10);
1402 		if (!devid) {
1403 			ret = -EINVAL;
1404 			goto out_free;
1405 		}
1406 		printk(KERN_INFO "btrfs: resizing devid %llu\n",
1407 		       (unsigned long long)devid);
1408 	}
1409 
1410 	device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1411 	if (!device) {
1412 		printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
1413 		       (unsigned long long)devid);
1414 		ret = -ENODEV;
1415 		goto out_free;
1416 	}
1417 
1418 	if (!device->writeable) {
1419 		printk(KERN_INFO "btrfs: resizer unable to apply on "
1420 		       "readonly device %llu\n",
1421 		       (unsigned long long)devid);
1422 		ret = -EPERM;
1423 		goto out_free;
1424 	}
1425 
1426 	if (!strcmp(sizestr, "max"))
1427 		new_size = device->bdev->bd_inode->i_size;
1428 	else {
1429 		if (sizestr[0] == '-') {
1430 			mod = -1;
1431 			sizestr++;
1432 		} else if (sizestr[0] == '+') {
1433 			mod = 1;
1434 			sizestr++;
1435 		}
1436 		new_size = memparse(sizestr, NULL);
1437 		if (new_size == 0) {
1438 			ret = -EINVAL;
1439 			goto out_free;
1440 		}
1441 	}
1442 
1443 	if (device->is_tgtdev_for_dev_replace) {
1444 		ret = -EPERM;
1445 		goto out_free;
1446 	}
1447 
1448 	old_size = device->total_bytes;
1449 
1450 	if (mod < 0) {
1451 		if (new_size > old_size) {
1452 			ret = -EINVAL;
1453 			goto out_free;
1454 		}
1455 		new_size = old_size - new_size;
1456 	} else if (mod > 0) {
1457 		new_size = old_size + new_size;
1458 	}
1459 
1460 	if (new_size < 256 * 1024 * 1024) {
1461 		ret = -EINVAL;
1462 		goto out_free;
1463 	}
1464 	if (new_size > device->bdev->bd_inode->i_size) {
1465 		ret = -EFBIG;
1466 		goto out_free;
1467 	}
1468 
1469 	do_div(new_size, root->sectorsize);
1470 	new_size *= root->sectorsize;
1471 
1472 	printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1473 		      rcu_str_deref(device->name),
1474 		      (unsigned long long)new_size);
1475 
1476 	if (new_size > old_size) {
1477 		trans = btrfs_start_transaction(root, 0);
1478 		if (IS_ERR(trans)) {
1479 			ret = PTR_ERR(trans);
1480 			goto out_free;
1481 		}
1482 		ret = btrfs_grow_device(trans, device, new_size);
1483 		btrfs_commit_transaction(trans, root);
1484 	} else if (new_size < old_size) {
1485 		ret = btrfs_shrink_device(device, new_size);
1486 	} /* equal, nothing need to do */
1487 
1488 out_free:
1489 	kfree(vol_args);
1490 out:
1491 	mutex_unlock(&root->fs_info->volume_mutex);
1492 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
1493 	mnt_drop_write_file(file);
1494 	return ret;
1495 }
1496 
1497 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1498 				char *name, unsigned long fd, int subvol,
1499 				u64 *transid, bool readonly,
1500 				struct btrfs_qgroup_inherit *inherit)
1501 {
1502 	int namelen;
1503 	int ret = 0;
1504 
1505 	ret = mnt_want_write_file(file);
1506 	if (ret)
1507 		goto out;
1508 
1509 	namelen = strlen(name);
1510 	if (strchr(name, '/')) {
1511 		ret = -EINVAL;
1512 		goto out_drop_write;
1513 	}
1514 
1515 	if (name[0] == '.' &&
1516 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1517 		ret = -EEXIST;
1518 		goto out_drop_write;
1519 	}
1520 
1521 	if (subvol) {
1522 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
1523 				     NULL, transid, readonly, inherit);
1524 	} else {
1525 		struct fd src = fdget(fd);
1526 		struct inode *src_inode;
1527 		if (!src.file) {
1528 			ret = -EINVAL;
1529 			goto out_drop_write;
1530 		}
1531 
1532 		src_inode = file_inode(src.file);
1533 		if (src_inode->i_sb != file_inode(file)->i_sb) {
1534 			printk(KERN_INFO "btrfs: Snapshot src from "
1535 			       "another FS\n");
1536 			ret = -EINVAL;
1537 		} else {
1538 			ret = btrfs_mksubvol(&file->f_path, name, namelen,
1539 					     BTRFS_I(src_inode)->root,
1540 					     transid, readonly, inherit);
1541 		}
1542 		fdput(src);
1543 	}
1544 out_drop_write:
1545 	mnt_drop_write_file(file);
1546 out:
1547 	return ret;
1548 }
1549 
1550 static noinline int btrfs_ioctl_snap_create(struct file *file,
1551 					    void __user *arg, int subvol)
1552 {
1553 	struct btrfs_ioctl_vol_args *vol_args;
1554 	int ret;
1555 
1556 	vol_args = memdup_user(arg, sizeof(*vol_args));
1557 	if (IS_ERR(vol_args))
1558 		return PTR_ERR(vol_args);
1559 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1560 
1561 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1562 					      vol_args->fd, subvol,
1563 					      NULL, false, NULL);
1564 
1565 	kfree(vol_args);
1566 	return ret;
1567 }
1568 
1569 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1570 					       void __user *arg, int subvol)
1571 {
1572 	struct btrfs_ioctl_vol_args_v2 *vol_args;
1573 	int ret;
1574 	u64 transid = 0;
1575 	u64 *ptr = NULL;
1576 	bool readonly = false;
1577 	struct btrfs_qgroup_inherit *inherit = NULL;
1578 
1579 	vol_args = memdup_user(arg, sizeof(*vol_args));
1580 	if (IS_ERR(vol_args))
1581 		return PTR_ERR(vol_args);
1582 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1583 
1584 	if (vol_args->flags &
1585 	    ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1586 	      BTRFS_SUBVOL_QGROUP_INHERIT)) {
1587 		ret = -EOPNOTSUPP;
1588 		goto out;
1589 	}
1590 
1591 	if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1592 		ptr = &transid;
1593 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1594 		readonly = true;
1595 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1596 		if (vol_args->size > PAGE_CACHE_SIZE) {
1597 			ret = -EINVAL;
1598 			goto out;
1599 		}
1600 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1601 		if (IS_ERR(inherit)) {
1602 			ret = PTR_ERR(inherit);
1603 			goto out;
1604 		}
1605 	}
1606 
1607 	ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1608 					      vol_args->fd, subvol, ptr,
1609 					      readonly, inherit);
1610 
1611 	if (ret == 0 && ptr &&
1612 	    copy_to_user(arg +
1613 			 offsetof(struct btrfs_ioctl_vol_args_v2,
1614 				  transid), ptr, sizeof(*ptr)))
1615 		ret = -EFAULT;
1616 out:
1617 	kfree(vol_args);
1618 	kfree(inherit);
1619 	return ret;
1620 }
1621 
1622 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1623 						void __user *arg)
1624 {
1625 	struct inode *inode = file_inode(file);
1626 	struct btrfs_root *root = BTRFS_I(inode)->root;
1627 	int ret = 0;
1628 	u64 flags = 0;
1629 
1630 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1631 		return -EINVAL;
1632 
1633 	down_read(&root->fs_info->subvol_sem);
1634 	if (btrfs_root_readonly(root))
1635 		flags |= BTRFS_SUBVOL_RDONLY;
1636 	up_read(&root->fs_info->subvol_sem);
1637 
1638 	if (copy_to_user(arg, &flags, sizeof(flags)))
1639 		ret = -EFAULT;
1640 
1641 	return ret;
1642 }
1643 
1644 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1645 					      void __user *arg)
1646 {
1647 	struct inode *inode = file_inode(file);
1648 	struct btrfs_root *root = BTRFS_I(inode)->root;
1649 	struct btrfs_trans_handle *trans;
1650 	u64 root_flags;
1651 	u64 flags;
1652 	int ret = 0;
1653 
1654 	ret = mnt_want_write_file(file);
1655 	if (ret)
1656 		goto out;
1657 
1658 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1659 		ret = -EINVAL;
1660 		goto out_drop_write;
1661 	}
1662 
1663 	if (copy_from_user(&flags, arg, sizeof(flags))) {
1664 		ret = -EFAULT;
1665 		goto out_drop_write;
1666 	}
1667 
1668 	if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1669 		ret = -EINVAL;
1670 		goto out_drop_write;
1671 	}
1672 
1673 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
1674 		ret = -EOPNOTSUPP;
1675 		goto out_drop_write;
1676 	}
1677 
1678 	if (!inode_owner_or_capable(inode)) {
1679 		ret = -EACCES;
1680 		goto out_drop_write;
1681 	}
1682 
1683 	down_write(&root->fs_info->subvol_sem);
1684 
1685 	/* nothing to do */
1686 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1687 		goto out_drop_sem;
1688 
1689 	root_flags = btrfs_root_flags(&root->root_item);
1690 	if (flags & BTRFS_SUBVOL_RDONLY)
1691 		btrfs_set_root_flags(&root->root_item,
1692 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1693 	else
1694 		btrfs_set_root_flags(&root->root_item,
1695 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1696 
1697 	trans = btrfs_start_transaction(root, 1);
1698 	if (IS_ERR(trans)) {
1699 		ret = PTR_ERR(trans);
1700 		goto out_reset;
1701 	}
1702 
1703 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
1704 				&root->root_key, &root->root_item);
1705 
1706 	btrfs_commit_transaction(trans, root);
1707 out_reset:
1708 	if (ret)
1709 		btrfs_set_root_flags(&root->root_item, root_flags);
1710 out_drop_sem:
1711 	up_write(&root->fs_info->subvol_sem);
1712 out_drop_write:
1713 	mnt_drop_write_file(file);
1714 out:
1715 	return ret;
1716 }
1717 
1718 /*
1719  * helper to check if the subvolume references other subvolumes
1720  */
1721 static noinline int may_destroy_subvol(struct btrfs_root *root)
1722 {
1723 	struct btrfs_path *path;
1724 	struct btrfs_key key;
1725 	int ret;
1726 
1727 	path = btrfs_alloc_path();
1728 	if (!path)
1729 		return -ENOMEM;
1730 
1731 	key.objectid = root->root_key.objectid;
1732 	key.type = BTRFS_ROOT_REF_KEY;
1733 	key.offset = (u64)-1;
1734 
1735 	ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1736 				&key, path, 0, 0);
1737 	if (ret < 0)
1738 		goto out;
1739 	BUG_ON(ret == 0);
1740 
1741 	ret = 0;
1742 	if (path->slots[0] > 0) {
1743 		path->slots[0]--;
1744 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1745 		if (key.objectid == root->root_key.objectid &&
1746 		    key.type == BTRFS_ROOT_REF_KEY)
1747 			ret = -ENOTEMPTY;
1748 	}
1749 out:
1750 	btrfs_free_path(path);
1751 	return ret;
1752 }
1753 
1754 static noinline int key_in_sk(struct btrfs_key *key,
1755 			      struct btrfs_ioctl_search_key *sk)
1756 {
1757 	struct btrfs_key test;
1758 	int ret;
1759 
1760 	test.objectid = sk->min_objectid;
1761 	test.type = sk->min_type;
1762 	test.offset = sk->min_offset;
1763 
1764 	ret = btrfs_comp_cpu_keys(key, &test);
1765 	if (ret < 0)
1766 		return 0;
1767 
1768 	test.objectid = sk->max_objectid;
1769 	test.type = sk->max_type;
1770 	test.offset = sk->max_offset;
1771 
1772 	ret = btrfs_comp_cpu_keys(key, &test);
1773 	if (ret > 0)
1774 		return 0;
1775 	return 1;
1776 }
1777 
1778 static noinline int copy_to_sk(struct btrfs_root *root,
1779 			       struct btrfs_path *path,
1780 			       struct btrfs_key *key,
1781 			       struct btrfs_ioctl_search_key *sk,
1782 			       char *buf,
1783 			       unsigned long *sk_offset,
1784 			       int *num_found)
1785 {
1786 	u64 found_transid;
1787 	struct extent_buffer *leaf;
1788 	struct btrfs_ioctl_search_header sh;
1789 	unsigned long item_off;
1790 	unsigned long item_len;
1791 	int nritems;
1792 	int i;
1793 	int slot;
1794 	int ret = 0;
1795 
1796 	leaf = path->nodes[0];
1797 	slot = path->slots[0];
1798 	nritems = btrfs_header_nritems(leaf);
1799 
1800 	if (btrfs_header_generation(leaf) > sk->max_transid) {
1801 		i = nritems;
1802 		goto advance_key;
1803 	}
1804 	found_transid = btrfs_header_generation(leaf);
1805 
1806 	for (i = slot; i < nritems; i++) {
1807 		item_off = btrfs_item_ptr_offset(leaf, i);
1808 		item_len = btrfs_item_size_nr(leaf, i);
1809 
1810 		btrfs_item_key_to_cpu(leaf, key, i);
1811 		if (!key_in_sk(key, sk))
1812 			continue;
1813 
1814 		if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1815 			item_len = 0;
1816 
1817 		if (sizeof(sh) + item_len + *sk_offset >
1818 		    BTRFS_SEARCH_ARGS_BUFSIZE) {
1819 			ret = 1;
1820 			goto overflow;
1821 		}
1822 
1823 		sh.objectid = key->objectid;
1824 		sh.offset = key->offset;
1825 		sh.type = key->type;
1826 		sh.len = item_len;
1827 		sh.transid = found_transid;
1828 
1829 		/* copy search result header */
1830 		memcpy(buf + *sk_offset, &sh, sizeof(sh));
1831 		*sk_offset += sizeof(sh);
1832 
1833 		if (item_len) {
1834 			char *p = buf + *sk_offset;
1835 			/* copy the item */
1836 			read_extent_buffer(leaf, p,
1837 					   item_off, item_len);
1838 			*sk_offset += item_len;
1839 		}
1840 		(*num_found)++;
1841 
1842 		if (*num_found >= sk->nr_items)
1843 			break;
1844 	}
1845 advance_key:
1846 	ret = 0;
1847 	if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1848 		key->offset++;
1849 	else if (key->type < (u8)-1 && key->type < sk->max_type) {
1850 		key->offset = 0;
1851 		key->type++;
1852 	} else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1853 		key->offset = 0;
1854 		key->type = 0;
1855 		key->objectid++;
1856 	} else
1857 		ret = 1;
1858 overflow:
1859 	return ret;
1860 }
1861 
1862 static noinline int search_ioctl(struct inode *inode,
1863 				 struct btrfs_ioctl_search_args *args)
1864 {
1865 	struct btrfs_root *root;
1866 	struct btrfs_key key;
1867 	struct btrfs_key max_key;
1868 	struct btrfs_path *path;
1869 	struct btrfs_ioctl_search_key *sk = &args->key;
1870 	struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1871 	int ret;
1872 	int num_found = 0;
1873 	unsigned long sk_offset = 0;
1874 
1875 	path = btrfs_alloc_path();
1876 	if (!path)
1877 		return -ENOMEM;
1878 
1879 	if (sk->tree_id == 0) {
1880 		/* search the root of the inode that was passed */
1881 		root = BTRFS_I(inode)->root;
1882 	} else {
1883 		key.objectid = sk->tree_id;
1884 		key.type = BTRFS_ROOT_ITEM_KEY;
1885 		key.offset = (u64)-1;
1886 		root = btrfs_read_fs_root_no_name(info, &key);
1887 		if (IS_ERR(root)) {
1888 			printk(KERN_ERR "could not find root %llu\n",
1889 			       sk->tree_id);
1890 			btrfs_free_path(path);
1891 			return -ENOENT;
1892 		}
1893 	}
1894 
1895 	key.objectid = sk->min_objectid;
1896 	key.type = sk->min_type;
1897 	key.offset = sk->min_offset;
1898 
1899 	max_key.objectid = sk->max_objectid;
1900 	max_key.type = sk->max_type;
1901 	max_key.offset = sk->max_offset;
1902 
1903 	path->keep_locks = 1;
1904 
1905 	while(1) {
1906 		ret = btrfs_search_forward(root, &key, &max_key, path,
1907 					   sk->min_transid);
1908 		if (ret != 0) {
1909 			if (ret > 0)
1910 				ret = 0;
1911 			goto err;
1912 		}
1913 		ret = copy_to_sk(root, path, &key, sk, args->buf,
1914 				 &sk_offset, &num_found);
1915 		btrfs_release_path(path);
1916 		if (ret || num_found >= sk->nr_items)
1917 			break;
1918 
1919 	}
1920 	ret = 0;
1921 err:
1922 	sk->nr_items = num_found;
1923 	btrfs_free_path(path);
1924 	return ret;
1925 }
1926 
1927 static noinline int btrfs_ioctl_tree_search(struct file *file,
1928 					   void __user *argp)
1929 {
1930 	 struct btrfs_ioctl_search_args *args;
1931 	 struct inode *inode;
1932 	 int ret;
1933 
1934 	if (!capable(CAP_SYS_ADMIN))
1935 		return -EPERM;
1936 
1937 	args = memdup_user(argp, sizeof(*args));
1938 	if (IS_ERR(args))
1939 		return PTR_ERR(args);
1940 
1941 	inode = file_inode(file);
1942 	ret = search_ioctl(inode, args);
1943 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1944 		ret = -EFAULT;
1945 	kfree(args);
1946 	return ret;
1947 }
1948 
1949 /*
1950  * Search INODE_REFs to identify path name of 'dirid' directory
1951  * in a 'tree_id' tree. and sets path name to 'name'.
1952  */
1953 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1954 				u64 tree_id, u64 dirid, char *name)
1955 {
1956 	struct btrfs_root *root;
1957 	struct btrfs_key key;
1958 	char *ptr;
1959 	int ret = -1;
1960 	int slot;
1961 	int len;
1962 	int total_len = 0;
1963 	struct btrfs_inode_ref *iref;
1964 	struct extent_buffer *l;
1965 	struct btrfs_path *path;
1966 
1967 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1968 		name[0]='\0';
1969 		return 0;
1970 	}
1971 
1972 	path = btrfs_alloc_path();
1973 	if (!path)
1974 		return -ENOMEM;
1975 
1976 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1977 
1978 	key.objectid = tree_id;
1979 	key.type = BTRFS_ROOT_ITEM_KEY;
1980 	key.offset = (u64)-1;
1981 	root = btrfs_read_fs_root_no_name(info, &key);
1982 	if (IS_ERR(root)) {
1983 		printk(KERN_ERR "could not find root %llu\n", tree_id);
1984 		ret = -ENOENT;
1985 		goto out;
1986 	}
1987 
1988 	key.objectid = dirid;
1989 	key.type = BTRFS_INODE_REF_KEY;
1990 	key.offset = (u64)-1;
1991 
1992 	while(1) {
1993 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1994 		if (ret < 0)
1995 			goto out;
1996 
1997 		l = path->nodes[0];
1998 		slot = path->slots[0];
1999 		if (ret > 0 && slot > 0)
2000 			slot--;
2001 		btrfs_item_key_to_cpu(l, &key, slot);
2002 
2003 		if (ret > 0 && (key.objectid != dirid ||
2004 				key.type != BTRFS_INODE_REF_KEY)) {
2005 			ret = -ENOENT;
2006 			goto out;
2007 		}
2008 
2009 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2010 		len = btrfs_inode_ref_name_len(l, iref);
2011 		ptr -= len + 1;
2012 		total_len += len + 1;
2013 		if (ptr < name)
2014 			goto out;
2015 
2016 		*(ptr + len) = '/';
2017 		read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
2018 
2019 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2020 			break;
2021 
2022 		btrfs_release_path(path);
2023 		key.objectid = key.offset;
2024 		key.offset = (u64)-1;
2025 		dirid = key.objectid;
2026 	}
2027 	if (ptr < name)
2028 		goto out;
2029 	memmove(name, ptr, total_len);
2030 	name[total_len]='\0';
2031 	ret = 0;
2032 out:
2033 	btrfs_free_path(path);
2034 	return ret;
2035 }
2036 
2037 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2038 					   void __user *argp)
2039 {
2040 	 struct btrfs_ioctl_ino_lookup_args *args;
2041 	 struct inode *inode;
2042 	 int ret;
2043 
2044 	if (!capable(CAP_SYS_ADMIN))
2045 		return -EPERM;
2046 
2047 	args = memdup_user(argp, sizeof(*args));
2048 	if (IS_ERR(args))
2049 		return PTR_ERR(args);
2050 
2051 	inode = file_inode(file);
2052 
2053 	if (args->treeid == 0)
2054 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2055 
2056 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2057 					args->treeid, args->objectid,
2058 					args->name);
2059 
2060 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2061 		ret = -EFAULT;
2062 
2063 	kfree(args);
2064 	return ret;
2065 }
2066 
2067 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2068 					     void __user *arg)
2069 {
2070 	struct dentry *parent = fdentry(file);
2071 	struct dentry *dentry;
2072 	struct inode *dir = parent->d_inode;
2073 	struct inode *inode;
2074 	struct btrfs_root *root = BTRFS_I(dir)->root;
2075 	struct btrfs_root *dest = NULL;
2076 	struct btrfs_ioctl_vol_args *vol_args;
2077 	struct btrfs_trans_handle *trans;
2078 	struct btrfs_block_rsv block_rsv;
2079 	u64 qgroup_reserved;
2080 	int namelen;
2081 	int ret;
2082 	int err = 0;
2083 
2084 	vol_args = memdup_user(arg, sizeof(*vol_args));
2085 	if (IS_ERR(vol_args))
2086 		return PTR_ERR(vol_args);
2087 
2088 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2089 	namelen = strlen(vol_args->name);
2090 	if (strchr(vol_args->name, '/') ||
2091 	    strncmp(vol_args->name, "..", namelen) == 0) {
2092 		err = -EINVAL;
2093 		goto out;
2094 	}
2095 
2096 	err = mnt_want_write_file(file);
2097 	if (err)
2098 		goto out;
2099 
2100 	err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2101 	if (err == -EINTR)
2102 		goto out;
2103 	dentry = lookup_one_len(vol_args->name, parent, namelen);
2104 	if (IS_ERR(dentry)) {
2105 		err = PTR_ERR(dentry);
2106 		goto out_unlock_dir;
2107 	}
2108 
2109 	if (!dentry->d_inode) {
2110 		err = -ENOENT;
2111 		goto out_dput;
2112 	}
2113 
2114 	inode = dentry->d_inode;
2115 	dest = BTRFS_I(inode)->root;
2116 	if (!capable(CAP_SYS_ADMIN)){
2117 		/*
2118 		 * Regular user.  Only allow this with a special mount
2119 		 * option, when the user has write+exec access to the
2120 		 * subvol root, and when rmdir(2) would have been
2121 		 * allowed.
2122 		 *
2123 		 * Note that this is _not_ check that the subvol is
2124 		 * empty or doesn't contain data that we wouldn't
2125 		 * otherwise be able to delete.
2126 		 *
2127 		 * Users who want to delete empty subvols should try
2128 		 * rmdir(2).
2129 		 */
2130 		err = -EPERM;
2131 		if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2132 			goto out_dput;
2133 
2134 		/*
2135 		 * Do not allow deletion if the parent dir is the same
2136 		 * as the dir to be deleted.  That means the ioctl
2137 		 * must be called on the dentry referencing the root
2138 		 * of the subvol, not a random directory contained
2139 		 * within it.
2140 		 */
2141 		err = -EINVAL;
2142 		if (root == dest)
2143 			goto out_dput;
2144 
2145 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2146 		if (err)
2147 			goto out_dput;
2148 	}
2149 
2150 	/* check if subvolume may be deleted by a user */
2151 	err = btrfs_may_delete(dir, dentry, 1);
2152 	if (err)
2153 		goto out_dput;
2154 
2155 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
2156 		err = -EINVAL;
2157 		goto out_dput;
2158 	}
2159 
2160 	mutex_lock(&inode->i_mutex);
2161 	err = d_invalidate(dentry);
2162 	if (err)
2163 		goto out_unlock;
2164 
2165 	down_write(&root->fs_info->subvol_sem);
2166 
2167 	err = may_destroy_subvol(dest);
2168 	if (err)
2169 		goto out_up_write;
2170 
2171 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2172 	/*
2173 	 * One for dir inode, two for dir entries, two for root
2174 	 * ref/backref.
2175 	 */
2176 	err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2177 					       5, &qgroup_reserved);
2178 	if (err)
2179 		goto out_up_write;
2180 
2181 	trans = btrfs_start_transaction(root, 0);
2182 	if (IS_ERR(trans)) {
2183 		err = PTR_ERR(trans);
2184 		goto out_release;
2185 	}
2186 	trans->block_rsv = &block_rsv;
2187 	trans->bytes_reserved = block_rsv.size;
2188 
2189 	ret = btrfs_unlink_subvol(trans, root, dir,
2190 				dest->root_key.objectid,
2191 				dentry->d_name.name,
2192 				dentry->d_name.len);
2193 	if (ret) {
2194 		err = ret;
2195 		btrfs_abort_transaction(trans, root, ret);
2196 		goto out_end_trans;
2197 	}
2198 
2199 	btrfs_record_root_in_trans(trans, dest);
2200 
2201 	memset(&dest->root_item.drop_progress, 0,
2202 		sizeof(dest->root_item.drop_progress));
2203 	dest->root_item.drop_level = 0;
2204 	btrfs_set_root_refs(&dest->root_item, 0);
2205 
2206 	if (!xchg(&dest->orphan_item_inserted, 1)) {
2207 		ret = btrfs_insert_orphan_item(trans,
2208 					root->fs_info->tree_root,
2209 					dest->root_key.objectid);
2210 		if (ret) {
2211 			btrfs_abort_transaction(trans, root, ret);
2212 			err = ret;
2213 			goto out_end_trans;
2214 		}
2215 	}
2216 out_end_trans:
2217 	trans->block_rsv = NULL;
2218 	trans->bytes_reserved = 0;
2219 	ret = btrfs_end_transaction(trans, root);
2220 	if (ret && !err)
2221 		err = ret;
2222 	inode->i_flags |= S_DEAD;
2223 out_release:
2224 	btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
2225 out_up_write:
2226 	up_write(&root->fs_info->subvol_sem);
2227 out_unlock:
2228 	mutex_unlock(&inode->i_mutex);
2229 	if (!err) {
2230 		shrink_dcache_sb(root->fs_info->sb);
2231 		btrfs_invalidate_inodes(dest);
2232 		d_delete(dentry);
2233 
2234 		/* the last ref */
2235 		if (dest->cache_inode) {
2236 			iput(dest->cache_inode);
2237 			dest->cache_inode = NULL;
2238 		}
2239 	}
2240 out_dput:
2241 	dput(dentry);
2242 out_unlock_dir:
2243 	mutex_unlock(&dir->i_mutex);
2244 	mnt_drop_write_file(file);
2245 out:
2246 	kfree(vol_args);
2247 	return err;
2248 }
2249 
2250 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2251 {
2252 	struct inode *inode = file_inode(file);
2253 	struct btrfs_root *root = BTRFS_I(inode)->root;
2254 	struct btrfs_ioctl_defrag_range_args *range;
2255 	int ret;
2256 
2257 	ret = mnt_want_write_file(file);
2258 	if (ret)
2259 		return ret;
2260 
2261 	if (btrfs_root_readonly(root)) {
2262 		ret = -EROFS;
2263 		goto out;
2264 	}
2265 
2266 	switch (inode->i_mode & S_IFMT) {
2267 	case S_IFDIR:
2268 		if (!capable(CAP_SYS_ADMIN)) {
2269 			ret = -EPERM;
2270 			goto out;
2271 		}
2272 		ret = btrfs_defrag_root(root);
2273 		if (ret)
2274 			goto out;
2275 		ret = btrfs_defrag_root(root->fs_info->extent_root);
2276 		break;
2277 	case S_IFREG:
2278 		if (!(file->f_mode & FMODE_WRITE)) {
2279 			ret = -EINVAL;
2280 			goto out;
2281 		}
2282 
2283 		range = kzalloc(sizeof(*range), GFP_KERNEL);
2284 		if (!range) {
2285 			ret = -ENOMEM;
2286 			goto out;
2287 		}
2288 
2289 		if (argp) {
2290 			if (copy_from_user(range, argp,
2291 					   sizeof(*range))) {
2292 				ret = -EFAULT;
2293 				kfree(range);
2294 				goto out;
2295 			}
2296 			/* compression requires us to start the IO */
2297 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2298 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2299 				range->extent_thresh = (u32)-1;
2300 			}
2301 		} else {
2302 			/* the rest are all set to zero by kzalloc */
2303 			range->len = (u64)-1;
2304 		}
2305 		ret = btrfs_defrag_file(file_inode(file), file,
2306 					range, 0, 0);
2307 		if (ret > 0)
2308 			ret = 0;
2309 		kfree(range);
2310 		break;
2311 	default:
2312 		ret = -EINVAL;
2313 	}
2314 out:
2315 	mnt_drop_write_file(file);
2316 	return ret;
2317 }
2318 
2319 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2320 {
2321 	struct btrfs_ioctl_vol_args *vol_args;
2322 	int ret;
2323 
2324 	if (!capable(CAP_SYS_ADMIN))
2325 		return -EPERM;
2326 
2327 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2328 			1)) {
2329 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2330 		return -EINVAL;
2331 	}
2332 
2333 	mutex_lock(&root->fs_info->volume_mutex);
2334 	vol_args = memdup_user(arg, sizeof(*vol_args));
2335 	if (IS_ERR(vol_args)) {
2336 		ret = PTR_ERR(vol_args);
2337 		goto out;
2338 	}
2339 
2340 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2341 	ret = btrfs_init_new_device(root, vol_args->name);
2342 
2343 	kfree(vol_args);
2344 out:
2345 	mutex_unlock(&root->fs_info->volume_mutex);
2346 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2347 	return ret;
2348 }
2349 
2350 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2351 {
2352 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2353 	struct btrfs_ioctl_vol_args *vol_args;
2354 	int ret;
2355 
2356 	if (!capable(CAP_SYS_ADMIN))
2357 		return -EPERM;
2358 
2359 	ret = mnt_want_write_file(file);
2360 	if (ret)
2361 		return ret;
2362 
2363 	vol_args = memdup_user(arg, sizeof(*vol_args));
2364 	if (IS_ERR(vol_args)) {
2365 		ret = PTR_ERR(vol_args);
2366 		goto out;
2367 	}
2368 
2369 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2370 
2371 	if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2372 			1)) {
2373 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2374 		goto out;
2375 	}
2376 
2377 	mutex_lock(&root->fs_info->volume_mutex);
2378 	ret = btrfs_rm_device(root, vol_args->name);
2379 	mutex_unlock(&root->fs_info->volume_mutex);
2380 	atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2381 
2382 out:
2383 	kfree(vol_args);
2384 	mnt_drop_write_file(file);
2385 	return ret;
2386 }
2387 
2388 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2389 {
2390 	struct btrfs_ioctl_fs_info_args *fi_args;
2391 	struct btrfs_device *device;
2392 	struct btrfs_device *next;
2393 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2394 	int ret = 0;
2395 
2396 	if (!capable(CAP_SYS_ADMIN))
2397 		return -EPERM;
2398 
2399 	fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2400 	if (!fi_args)
2401 		return -ENOMEM;
2402 
2403 	fi_args->num_devices = fs_devices->num_devices;
2404 	memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2405 
2406 	mutex_lock(&fs_devices->device_list_mutex);
2407 	list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
2408 		if (device->devid > fi_args->max_id)
2409 			fi_args->max_id = device->devid;
2410 	}
2411 	mutex_unlock(&fs_devices->device_list_mutex);
2412 
2413 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2414 		ret = -EFAULT;
2415 
2416 	kfree(fi_args);
2417 	return ret;
2418 }
2419 
2420 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2421 {
2422 	struct btrfs_ioctl_dev_info_args *di_args;
2423 	struct btrfs_device *dev;
2424 	struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2425 	int ret = 0;
2426 	char *s_uuid = NULL;
2427 	char empty_uuid[BTRFS_UUID_SIZE] = {0};
2428 
2429 	if (!capable(CAP_SYS_ADMIN))
2430 		return -EPERM;
2431 
2432 	di_args = memdup_user(arg, sizeof(*di_args));
2433 	if (IS_ERR(di_args))
2434 		return PTR_ERR(di_args);
2435 
2436 	if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2437 		s_uuid = di_args->uuid;
2438 
2439 	mutex_lock(&fs_devices->device_list_mutex);
2440 	dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2441 
2442 	if (!dev) {
2443 		ret = -ENODEV;
2444 		goto out;
2445 	}
2446 
2447 	di_args->devid = dev->devid;
2448 	di_args->bytes_used = dev->bytes_used;
2449 	di_args->total_bytes = dev->total_bytes;
2450 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2451 	if (dev->name) {
2452 		struct rcu_string *name;
2453 
2454 		rcu_read_lock();
2455 		name = rcu_dereference(dev->name);
2456 		strncpy(di_args->path, name->str, sizeof(di_args->path));
2457 		rcu_read_unlock();
2458 		di_args->path[sizeof(di_args->path) - 1] = 0;
2459 	} else {
2460 		di_args->path[0] = '\0';
2461 	}
2462 
2463 out:
2464 	mutex_unlock(&fs_devices->device_list_mutex);
2465 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2466 		ret = -EFAULT;
2467 
2468 	kfree(di_args);
2469 	return ret;
2470 }
2471 
2472 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2473 				       u64 off, u64 olen, u64 destoff)
2474 {
2475 	struct inode *inode = file_inode(file);
2476 	struct btrfs_root *root = BTRFS_I(inode)->root;
2477 	struct fd src_file;
2478 	struct inode *src;
2479 	struct btrfs_trans_handle *trans;
2480 	struct btrfs_path *path;
2481 	struct extent_buffer *leaf;
2482 	char *buf;
2483 	struct btrfs_key key;
2484 	u32 nritems;
2485 	int slot;
2486 	int ret;
2487 	u64 len = olen;
2488 	u64 bs = root->fs_info->sb->s_blocksize;
2489 	int same_inode = 0;
2490 
2491 	/*
2492 	 * TODO:
2493 	 * - split compressed inline extents.  annoying: we need to
2494 	 *   decompress into destination's address_space (the file offset
2495 	 *   may change, so source mapping won't do), then recompress (or
2496 	 *   otherwise reinsert) a subrange.
2497 	 * - allow ranges within the same file to be cloned (provided
2498 	 *   they don't overlap)?
2499 	 */
2500 
2501 	/* the destination must be opened for writing */
2502 	if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
2503 		return -EINVAL;
2504 
2505 	if (btrfs_root_readonly(root))
2506 		return -EROFS;
2507 
2508 	ret = mnt_want_write_file(file);
2509 	if (ret)
2510 		return ret;
2511 
2512 	src_file = fdget(srcfd);
2513 	if (!src_file.file) {
2514 		ret = -EBADF;
2515 		goto out_drop_write;
2516 	}
2517 
2518 	ret = -EXDEV;
2519 	if (src_file.file->f_path.mnt != file->f_path.mnt)
2520 		goto out_fput;
2521 
2522 	src = file_inode(src_file.file);
2523 
2524 	ret = -EINVAL;
2525 	if (src == inode)
2526 		same_inode = 1;
2527 
2528 	/* the src must be open for reading */
2529 	if (!(src_file.file->f_mode & FMODE_READ))
2530 		goto out_fput;
2531 
2532 	/* don't make the dst file partly checksummed */
2533 	if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2534 	    (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2535 		goto out_fput;
2536 
2537 	ret = -EISDIR;
2538 	if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2539 		goto out_fput;
2540 
2541 	ret = -EXDEV;
2542 	if (src->i_sb != inode->i_sb)
2543 		goto out_fput;
2544 
2545 	ret = -ENOMEM;
2546 	buf = vmalloc(btrfs_level_size(root, 0));
2547 	if (!buf)
2548 		goto out_fput;
2549 
2550 	path = btrfs_alloc_path();
2551 	if (!path) {
2552 		vfree(buf);
2553 		goto out_fput;
2554 	}
2555 	path->reada = 2;
2556 
2557 	if (!same_inode) {
2558 		if (inode < src) {
2559 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2560 			mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2561 		} else {
2562 			mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2563 			mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2564 		}
2565 	} else {
2566 		mutex_lock(&src->i_mutex);
2567 	}
2568 
2569 	/* determine range to clone */
2570 	ret = -EINVAL;
2571 	if (off + len > src->i_size || off + len < off)
2572 		goto out_unlock;
2573 	if (len == 0)
2574 		olen = len = src->i_size - off;
2575 	/* if we extend to eof, continue to block boundary */
2576 	if (off + len == src->i_size)
2577 		len = ALIGN(src->i_size, bs) - off;
2578 
2579 	/* verify the end result is block aligned */
2580 	if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2581 	    !IS_ALIGNED(destoff, bs))
2582 		goto out_unlock;
2583 
2584 	/* verify if ranges are overlapped within the same file */
2585 	if (same_inode) {
2586 		if (destoff + len > off && destoff < off + len)
2587 			goto out_unlock;
2588 	}
2589 
2590 	if (destoff > inode->i_size) {
2591 		ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2592 		if (ret)
2593 			goto out_unlock;
2594 	}
2595 
2596 	/* truncate page cache pages from target inode range */
2597 	truncate_inode_pages_range(&inode->i_data, destoff,
2598 				   PAGE_CACHE_ALIGN(destoff + len) - 1);
2599 
2600 	/* do any pending delalloc/csum calc on src, one way or
2601 	   another, and lock file content */
2602 	while (1) {
2603 		struct btrfs_ordered_extent *ordered;
2604 		lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2605 		ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
2606 		if (!ordered &&
2607 		    !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
2608 				    EXTENT_DELALLOC, 0, NULL))
2609 			break;
2610 		unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2611 		if (ordered)
2612 			btrfs_put_ordered_extent(ordered);
2613 		btrfs_wait_ordered_range(src, off, len);
2614 	}
2615 
2616 	/* clone data */
2617 	key.objectid = btrfs_ino(src);
2618 	key.type = BTRFS_EXTENT_DATA_KEY;
2619 	key.offset = 0;
2620 
2621 	while (1) {
2622 		/*
2623 		 * note the key will change type as we walk through the
2624 		 * tree.
2625 		 */
2626 		ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2627 				0, 0);
2628 		if (ret < 0)
2629 			goto out;
2630 
2631 		nritems = btrfs_header_nritems(path->nodes[0]);
2632 		if (path->slots[0] >= nritems) {
2633 			ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2634 			if (ret < 0)
2635 				goto out;
2636 			if (ret > 0)
2637 				break;
2638 			nritems = btrfs_header_nritems(path->nodes[0]);
2639 		}
2640 		leaf = path->nodes[0];
2641 		slot = path->slots[0];
2642 
2643 		btrfs_item_key_to_cpu(leaf, &key, slot);
2644 		if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
2645 		    key.objectid != btrfs_ino(src))
2646 			break;
2647 
2648 		if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2649 			struct btrfs_file_extent_item *extent;
2650 			int type;
2651 			u32 size;
2652 			struct btrfs_key new_key;
2653 			u64 disko = 0, diskl = 0;
2654 			u64 datao = 0, datal = 0;
2655 			u8 comp;
2656 			u64 endoff;
2657 
2658 			size = btrfs_item_size_nr(leaf, slot);
2659 			read_extent_buffer(leaf, buf,
2660 					   btrfs_item_ptr_offset(leaf, slot),
2661 					   size);
2662 
2663 			extent = btrfs_item_ptr(leaf, slot,
2664 						struct btrfs_file_extent_item);
2665 			comp = btrfs_file_extent_compression(leaf, extent);
2666 			type = btrfs_file_extent_type(leaf, extent);
2667 			if (type == BTRFS_FILE_EXTENT_REG ||
2668 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2669 				disko = btrfs_file_extent_disk_bytenr(leaf,
2670 								      extent);
2671 				diskl = btrfs_file_extent_disk_num_bytes(leaf,
2672 								 extent);
2673 				datao = btrfs_file_extent_offset(leaf, extent);
2674 				datal = btrfs_file_extent_num_bytes(leaf,
2675 								    extent);
2676 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2677 				/* take upper bound, may be compressed */
2678 				datal = btrfs_file_extent_ram_bytes(leaf,
2679 								    extent);
2680 			}
2681 			btrfs_release_path(path);
2682 
2683 			if (key.offset + datal <= off ||
2684 			    key.offset >= off + len - 1)
2685 				goto next;
2686 
2687 			memcpy(&new_key, &key, sizeof(new_key));
2688 			new_key.objectid = btrfs_ino(inode);
2689 			if (off <= key.offset)
2690 				new_key.offset = key.offset + destoff - off;
2691 			else
2692 				new_key.offset = destoff;
2693 
2694 			/*
2695 			 * 1 - adjusting old extent (we may have to split it)
2696 			 * 1 - add new extent
2697 			 * 1 - inode update
2698 			 */
2699 			trans = btrfs_start_transaction(root, 3);
2700 			if (IS_ERR(trans)) {
2701 				ret = PTR_ERR(trans);
2702 				goto out;
2703 			}
2704 
2705 			if (type == BTRFS_FILE_EXTENT_REG ||
2706 			    type == BTRFS_FILE_EXTENT_PREALLOC) {
2707 				/*
2708 				 *    a  | --- range to clone ---|  b
2709 				 * | ------------- extent ------------- |
2710 				 */
2711 
2712 				/* substract range b */
2713 				if (key.offset + datal > off + len)
2714 					datal = off + len - key.offset;
2715 
2716 				/* substract range a */
2717 				if (off > key.offset) {
2718 					datao += off - key.offset;
2719 					datal -= off - key.offset;
2720 				}
2721 
2722 				ret = btrfs_drop_extents(trans, root, inode,
2723 							 new_key.offset,
2724 							 new_key.offset + datal,
2725 							 1);
2726 				if (ret) {
2727 					btrfs_abort_transaction(trans, root,
2728 								ret);
2729 					btrfs_end_transaction(trans, root);
2730 					goto out;
2731 				}
2732 
2733 				ret = btrfs_insert_empty_item(trans, root, path,
2734 							      &new_key, size);
2735 				if (ret) {
2736 					btrfs_abort_transaction(trans, root,
2737 								ret);
2738 					btrfs_end_transaction(trans, root);
2739 					goto out;
2740 				}
2741 
2742 				leaf = path->nodes[0];
2743 				slot = path->slots[0];
2744 				write_extent_buffer(leaf, buf,
2745 					    btrfs_item_ptr_offset(leaf, slot),
2746 					    size);
2747 
2748 				extent = btrfs_item_ptr(leaf, slot,
2749 						struct btrfs_file_extent_item);
2750 
2751 				/* disko == 0 means it's a hole */
2752 				if (!disko)
2753 					datao = 0;
2754 
2755 				btrfs_set_file_extent_offset(leaf, extent,
2756 							     datao);
2757 				btrfs_set_file_extent_num_bytes(leaf, extent,
2758 								datal);
2759 				if (disko) {
2760 					inode_add_bytes(inode, datal);
2761 					ret = btrfs_inc_extent_ref(trans, root,
2762 							disko, diskl, 0,
2763 							root->root_key.objectid,
2764 							btrfs_ino(inode),
2765 							new_key.offset - datao,
2766 							0);
2767 					if (ret) {
2768 						btrfs_abort_transaction(trans,
2769 									root,
2770 									ret);
2771 						btrfs_end_transaction(trans,
2772 								      root);
2773 						goto out;
2774 
2775 					}
2776 				}
2777 			} else if (type == BTRFS_FILE_EXTENT_INLINE) {
2778 				u64 skip = 0;
2779 				u64 trim = 0;
2780 				if (off > key.offset) {
2781 					skip = off - key.offset;
2782 					new_key.offset += skip;
2783 				}
2784 
2785 				if (key.offset + datal > off + len)
2786 					trim = key.offset + datal - (off + len);
2787 
2788 				if (comp && (skip || trim)) {
2789 					ret = -EINVAL;
2790 					btrfs_end_transaction(trans, root);
2791 					goto out;
2792 				}
2793 				size -= skip + trim;
2794 				datal -= skip + trim;
2795 
2796 				ret = btrfs_drop_extents(trans, root, inode,
2797 							 new_key.offset,
2798 							 new_key.offset + datal,
2799 							 1);
2800 				if (ret) {
2801 					btrfs_abort_transaction(trans, root,
2802 								ret);
2803 					btrfs_end_transaction(trans, root);
2804 					goto out;
2805 				}
2806 
2807 				ret = btrfs_insert_empty_item(trans, root, path,
2808 							      &new_key, size);
2809 				if (ret) {
2810 					btrfs_abort_transaction(trans, root,
2811 								ret);
2812 					btrfs_end_transaction(trans, root);
2813 					goto out;
2814 				}
2815 
2816 				if (skip) {
2817 					u32 start =
2818 					  btrfs_file_extent_calc_inline_size(0);
2819 					memmove(buf+start, buf+start+skip,
2820 						datal);
2821 				}
2822 
2823 				leaf = path->nodes[0];
2824 				slot = path->slots[0];
2825 				write_extent_buffer(leaf, buf,
2826 					    btrfs_item_ptr_offset(leaf, slot),
2827 					    size);
2828 				inode_add_bytes(inode, datal);
2829 			}
2830 
2831 			btrfs_mark_buffer_dirty(leaf);
2832 			btrfs_release_path(path);
2833 
2834 			inode_inc_iversion(inode);
2835 			inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2836 
2837 			/*
2838 			 * we round up to the block size at eof when
2839 			 * determining which extents to clone above,
2840 			 * but shouldn't round up the file size
2841 			 */
2842 			endoff = new_key.offset + datal;
2843 			if (endoff > destoff+olen)
2844 				endoff = destoff+olen;
2845 			if (endoff > inode->i_size)
2846 				btrfs_i_size_write(inode, endoff);
2847 
2848 			ret = btrfs_update_inode(trans, root, inode);
2849 			if (ret) {
2850 				btrfs_abort_transaction(trans, root, ret);
2851 				btrfs_end_transaction(trans, root);
2852 				goto out;
2853 			}
2854 			ret = btrfs_end_transaction(trans, root);
2855 		}
2856 next:
2857 		btrfs_release_path(path);
2858 		key.offset++;
2859 	}
2860 	ret = 0;
2861 out:
2862 	btrfs_release_path(path);
2863 	unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2864 out_unlock:
2865 	mutex_unlock(&src->i_mutex);
2866 	if (!same_inode)
2867 		mutex_unlock(&inode->i_mutex);
2868 	vfree(buf);
2869 	btrfs_free_path(path);
2870 out_fput:
2871 	fdput(src_file);
2872 out_drop_write:
2873 	mnt_drop_write_file(file);
2874 	return ret;
2875 }
2876 
2877 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2878 {
2879 	struct btrfs_ioctl_clone_range_args args;
2880 
2881 	if (copy_from_user(&args, argp, sizeof(args)))
2882 		return -EFAULT;
2883 	return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2884 				 args.src_length, args.dest_offset);
2885 }
2886 
2887 /*
2888  * there are many ways the trans_start and trans_end ioctls can lead
2889  * to deadlocks.  They should only be used by applications that
2890  * basically own the machine, and have a very in depth understanding
2891  * of all the possible deadlocks and enospc problems.
2892  */
2893 static long btrfs_ioctl_trans_start(struct file *file)
2894 {
2895 	struct inode *inode = file_inode(file);
2896 	struct btrfs_root *root = BTRFS_I(inode)->root;
2897 	struct btrfs_trans_handle *trans;
2898 	int ret;
2899 
2900 	ret = -EPERM;
2901 	if (!capable(CAP_SYS_ADMIN))
2902 		goto out;
2903 
2904 	ret = -EINPROGRESS;
2905 	if (file->private_data)
2906 		goto out;
2907 
2908 	ret = -EROFS;
2909 	if (btrfs_root_readonly(root))
2910 		goto out;
2911 
2912 	ret = mnt_want_write_file(file);
2913 	if (ret)
2914 		goto out;
2915 
2916 	atomic_inc(&root->fs_info->open_ioctl_trans);
2917 
2918 	ret = -ENOMEM;
2919 	trans = btrfs_start_ioctl_transaction(root);
2920 	if (IS_ERR(trans))
2921 		goto out_drop;
2922 
2923 	file->private_data = trans;
2924 	return 0;
2925 
2926 out_drop:
2927 	atomic_dec(&root->fs_info->open_ioctl_trans);
2928 	mnt_drop_write_file(file);
2929 out:
2930 	return ret;
2931 }
2932 
2933 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2934 {
2935 	struct inode *inode = file_inode(file);
2936 	struct btrfs_root *root = BTRFS_I(inode)->root;
2937 	struct btrfs_root *new_root;
2938 	struct btrfs_dir_item *di;
2939 	struct btrfs_trans_handle *trans;
2940 	struct btrfs_path *path;
2941 	struct btrfs_key location;
2942 	struct btrfs_disk_key disk_key;
2943 	u64 objectid = 0;
2944 	u64 dir_id;
2945 	int ret;
2946 
2947 	if (!capable(CAP_SYS_ADMIN))
2948 		return -EPERM;
2949 
2950 	ret = mnt_want_write_file(file);
2951 	if (ret)
2952 		return ret;
2953 
2954 	if (copy_from_user(&objectid, argp, sizeof(objectid))) {
2955 		ret = -EFAULT;
2956 		goto out;
2957 	}
2958 
2959 	if (!objectid)
2960 		objectid = root->root_key.objectid;
2961 
2962 	location.objectid = objectid;
2963 	location.type = BTRFS_ROOT_ITEM_KEY;
2964 	location.offset = (u64)-1;
2965 
2966 	new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2967 	if (IS_ERR(new_root)) {
2968 		ret = PTR_ERR(new_root);
2969 		goto out;
2970 	}
2971 
2972 	path = btrfs_alloc_path();
2973 	if (!path) {
2974 		ret = -ENOMEM;
2975 		goto out;
2976 	}
2977 	path->leave_spinning = 1;
2978 
2979 	trans = btrfs_start_transaction(root, 1);
2980 	if (IS_ERR(trans)) {
2981 		btrfs_free_path(path);
2982 		ret = PTR_ERR(trans);
2983 		goto out;
2984 	}
2985 
2986 	dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
2987 	di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2988 				   dir_id, "default", 7, 1);
2989 	if (IS_ERR_OR_NULL(di)) {
2990 		btrfs_free_path(path);
2991 		btrfs_end_transaction(trans, root);
2992 		printk(KERN_ERR "Umm, you don't have the default dir item, "
2993 		       "this isn't going to work\n");
2994 		ret = -ENOENT;
2995 		goto out;
2996 	}
2997 
2998 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2999 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3000 	btrfs_mark_buffer_dirty(path->nodes[0]);
3001 	btrfs_free_path(path);
3002 
3003 	btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
3004 	btrfs_end_transaction(trans, root);
3005 out:
3006 	mnt_drop_write_file(file);
3007 	return ret;
3008 }
3009 
3010 void btrfs_get_block_group_info(struct list_head *groups_list,
3011 				struct btrfs_ioctl_space_info *space)
3012 {
3013 	struct btrfs_block_group_cache *block_group;
3014 
3015 	space->total_bytes = 0;
3016 	space->used_bytes = 0;
3017 	space->flags = 0;
3018 	list_for_each_entry(block_group, groups_list, list) {
3019 		space->flags = block_group->flags;
3020 		space->total_bytes += block_group->key.offset;
3021 		space->used_bytes +=
3022 			btrfs_block_group_used(&block_group->item);
3023 	}
3024 }
3025 
3026 static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
3027 {
3028 	struct btrfs_ioctl_space_args space_args;
3029 	struct btrfs_ioctl_space_info space;
3030 	struct btrfs_ioctl_space_info *dest;
3031 	struct btrfs_ioctl_space_info *dest_orig;
3032 	struct btrfs_ioctl_space_info __user *user_dest;
3033 	struct btrfs_space_info *info;
3034 	u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3035 		       BTRFS_BLOCK_GROUP_SYSTEM,
3036 		       BTRFS_BLOCK_GROUP_METADATA,
3037 		       BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3038 	int num_types = 4;
3039 	int alloc_size;
3040 	int ret = 0;
3041 	u64 slot_count = 0;
3042 	int i, c;
3043 
3044 	if (copy_from_user(&space_args,
3045 			   (struct btrfs_ioctl_space_args __user *)arg,
3046 			   sizeof(space_args)))
3047 		return -EFAULT;
3048 
3049 	for (i = 0; i < num_types; i++) {
3050 		struct btrfs_space_info *tmp;
3051 
3052 		info = NULL;
3053 		rcu_read_lock();
3054 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3055 					list) {
3056 			if (tmp->flags == types[i]) {
3057 				info = tmp;
3058 				break;
3059 			}
3060 		}
3061 		rcu_read_unlock();
3062 
3063 		if (!info)
3064 			continue;
3065 
3066 		down_read(&info->groups_sem);
3067 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3068 			if (!list_empty(&info->block_groups[c]))
3069 				slot_count++;
3070 		}
3071 		up_read(&info->groups_sem);
3072 	}
3073 
3074 	/* space_slots == 0 means they are asking for a count */
3075 	if (space_args.space_slots == 0) {
3076 		space_args.total_spaces = slot_count;
3077 		goto out;
3078 	}
3079 
3080 	slot_count = min_t(u64, space_args.space_slots, slot_count);
3081 
3082 	alloc_size = sizeof(*dest) * slot_count;
3083 
3084 	/* we generally have at most 6 or so space infos, one for each raid
3085 	 * level.  So, a whole page should be more than enough for everyone
3086 	 */
3087 	if (alloc_size > PAGE_CACHE_SIZE)
3088 		return -ENOMEM;
3089 
3090 	space_args.total_spaces = 0;
3091 	dest = kmalloc(alloc_size, GFP_NOFS);
3092 	if (!dest)
3093 		return -ENOMEM;
3094 	dest_orig = dest;
3095 
3096 	/* now we have a buffer to copy into */
3097 	for (i = 0; i < num_types; i++) {
3098 		struct btrfs_space_info *tmp;
3099 
3100 		if (!slot_count)
3101 			break;
3102 
3103 		info = NULL;
3104 		rcu_read_lock();
3105 		list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3106 					list) {
3107 			if (tmp->flags == types[i]) {
3108 				info = tmp;
3109 				break;
3110 			}
3111 		}
3112 		rcu_read_unlock();
3113 
3114 		if (!info)
3115 			continue;
3116 		down_read(&info->groups_sem);
3117 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3118 			if (!list_empty(&info->block_groups[c])) {
3119 				btrfs_get_block_group_info(
3120 					&info->block_groups[c], &space);
3121 				memcpy(dest, &space, sizeof(space));
3122 				dest++;
3123 				space_args.total_spaces++;
3124 				slot_count--;
3125 			}
3126 			if (!slot_count)
3127 				break;
3128 		}
3129 		up_read(&info->groups_sem);
3130 	}
3131 
3132 	user_dest = (struct btrfs_ioctl_space_info __user *)
3133 		(arg + sizeof(struct btrfs_ioctl_space_args));
3134 
3135 	if (copy_to_user(user_dest, dest_orig, alloc_size))
3136 		ret = -EFAULT;
3137 
3138 	kfree(dest_orig);
3139 out:
3140 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3141 		ret = -EFAULT;
3142 
3143 	return ret;
3144 }
3145 
3146 /*
3147  * there are many ways the trans_start and trans_end ioctls can lead
3148  * to deadlocks.  They should only be used by applications that
3149  * basically own the machine, and have a very in depth understanding
3150  * of all the possible deadlocks and enospc problems.
3151  */
3152 long btrfs_ioctl_trans_end(struct file *file)
3153 {
3154 	struct inode *inode = file_inode(file);
3155 	struct btrfs_root *root = BTRFS_I(inode)->root;
3156 	struct btrfs_trans_handle *trans;
3157 
3158 	trans = file->private_data;
3159 	if (!trans)
3160 		return -EINVAL;
3161 	file->private_data = NULL;
3162 
3163 	btrfs_end_transaction(trans, root);
3164 
3165 	atomic_dec(&root->fs_info->open_ioctl_trans);
3166 
3167 	mnt_drop_write_file(file);
3168 	return 0;
3169 }
3170 
3171 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3172 					    void __user *argp)
3173 {
3174 	struct btrfs_trans_handle *trans;
3175 	u64 transid;
3176 	int ret;
3177 
3178 	trans = btrfs_attach_transaction_barrier(root);
3179 	if (IS_ERR(trans)) {
3180 		if (PTR_ERR(trans) != -ENOENT)
3181 			return PTR_ERR(trans);
3182 
3183 		/* No running transaction, don't bother */
3184 		transid = root->fs_info->last_trans_committed;
3185 		goto out;
3186 	}
3187 	transid = trans->transid;
3188 	ret = btrfs_commit_transaction_async(trans, root, 0);
3189 	if (ret) {
3190 		btrfs_end_transaction(trans, root);
3191 		return ret;
3192 	}
3193 out:
3194 	if (argp)
3195 		if (copy_to_user(argp, &transid, sizeof(transid)))
3196 			return -EFAULT;
3197 	return 0;
3198 }
3199 
3200 static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3201 					   void __user *argp)
3202 {
3203 	u64 transid;
3204 
3205 	if (argp) {
3206 		if (copy_from_user(&transid, argp, sizeof(transid)))
3207 			return -EFAULT;
3208 	} else {
3209 		transid = 0;  /* current trans */
3210 	}
3211 	return btrfs_wait_for_commit(root, transid);
3212 }
3213 
3214 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3215 {
3216 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3217 	struct btrfs_ioctl_scrub_args *sa;
3218 	int ret;
3219 
3220 	if (!capable(CAP_SYS_ADMIN))
3221 		return -EPERM;
3222 
3223 	sa = memdup_user(arg, sizeof(*sa));
3224 	if (IS_ERR(sa))
3225 		return PTR_ERR(sa);
3226 
3227 	if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3228 		ret = mnt_want_write_file(file);
3229 		if (ret)
3230 			goto out;
3231 	}
3232 
3233 	ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
3234 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3235 			      0);
3236 
3237 	if (copy_to_user(arg, sa, sizeof(*sa)))
3238 		ret = -EFAULT;
3239 
3240 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
3241 		mnt_drop_write_file(file);
3242 out:
3243 	kfree(sa);
3244 	return ret;
3245 }
3246 
3247 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3248 {
3249 	if (!capable(CAP_SYS_ADMIN))
3250 		return -EPERM;
3251 
3252 	return btrfs_scrub_cancel(root->fs_info);
3253 }
3254 
3255 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3256 				       void __user *arg)
3257 {
3258 	struct btrfs_ioctl_scrub_args *sa;
3259 	int ret;
3260 
3261 	if (!capable(CAP_SYS_ADMIN))
3262 		return -EPERM;
3263 
3264 	sa = memdup_user(arg, sizeof(*sa));
3265 	if (IS_ERR(sa))
3266 		return PTR_ERR(sa);
3267 
3268 	ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3269 
3270 	if (copy_to_user(arg, sa, sizeof(*sa)))
3271 		ret = -EFAULT;
3272 
3273 	kfree(sa);
3274 	return ret;
3275 }
3276 
3277 static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3278 				      void __user *arg)
3279 {
3280 	struct btrfs_ioctl_get_dev_stats *sa;
3281 	int ret;
3282 
3283 	sa = memdup_user(arg, sizeof(*sa));
3284 	if (IS_ERR(sa))
3285 		return PTR_ERR(sa);
3286 
3287 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3288 		kfree(sa);
3289 		return -EPERM;
3290 	}
3291 
3292 	ret = btrfs_get_dev_stats(root, sa);
3293 
3294 	if (copy_to_user(arg, sa, sizeof(*sa)))
3295 		ret = -EFAULT;
3296 
3297 	kfree(sa);
3298 	return ret;
3299 }
3300 
3301 static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3302 {
3303 	struct btrfs_ioctl_dev_replace_args *p;
3304 	int ret;
3305 
3306 	if (!capable(CAP_SYS_ADMIN))
3307 		return -EPERM;
3308 
3309 	p = memdup_user(arg, sizeof(*p));
3310 	if (IS_ERR(p))
3311 		return PTR_ERR(p);
3312 
3313 	switch (p->cmd) {
3314 	case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3315 		if (atomic_xchg(
3316 			&root->fs_info->mutually_exclusive_operation_running,
3317 			1)) {
3318 			pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3319 			ret = -EINPROGRESS;
3320 		} else {
3321 			ret = btrfs_dev_replace_start(root, p);
3322 			atomic_set(
3323 			 &root->fs_info->mutually_exclusive_operation_running,
3324 			 0);
3325 		}
3326 		break;
3327 	case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3328 		btrfs_dev_replace_status(root->fs_info, p);
3329 		ret = 0;
3330 		break;
3331 	case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3332 		ret = btrfs_dev_replace_cancel(root->fs_info, p);
3333 		break;
3334 	default:
3335 		ret = -EINVAL;
3336 		break;
3337 	}
3338 
3339 	if (copy_to_user(arg, p, sizeof(*p)))
3340 		ret = -EFAULT;
3341 
3342 	kfree(p);
3343 	return ret;
3344 }
3345 
3346 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3347 {
3348 	int ret = 0;
3349 	int i;
3350 	u64 rel_ptr;
3351 	int size;
3352 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
3353 	struct inode_fs_paths *ipath = NULL;
3354 	struct btrfs_path *path;
3355 
3356 	if (!capable(CAP_DAC_READ_SEARCH))
3357 		return -EPERM;
3358 
3359 	path = btrfs_alloc_path();
3360 	if (!path) {
3361 		ret = -ENOMEM;
3362 		goto out;
3363 	}
3364 
3365 	ipa = memdup_user(arg, sizeof(*ipa));
3366 	if (IS_ERR(ipa)) {
3367 		ret = PTR_ERR(ipa);
3368 		ipa = NULL;
3369 		goto out;
3370 	}
3371 
3372 	size = min_t(u32, ipa->size, 4096);
3373 	ipath = init_ipath(size, root, path);
3374 	if (IS_ERR(ipath)) {
3375 		ret = PTR_ERR(ipath);
3376 		ipath = NULL;
3377 		goto out;
3378 	}
3379 
3380 	ret = paths_from_inode(ipa->inum, ipath);
3381 	if (ret < 0)
3382 		goto out;
3383 
3384 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3385 		rel_ptr = ipath->fspath->val[i] -
3386 			  (u64)(unsigned long)ipath->fspath->val;
3387 		ipath->fspath->val[i] = rel_ptr;
3388 	}
3389 
3390 	ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3391 			   (void *)(unsigned long)ipath->fspath, size);
3392 	if (ret) {
3393 		ret = -EFAULT;
3394 		goto out;
3395 	}
3396 
3397 out:
3398 	btrfs_free_path(path);
3399 	free_ipath(ipath);
3400 	kfree(ipa);
3401 
3402 	return ret;
3403 }
3404 
3405 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3406 {
3407 	struct btrfs_data_container *inodes = ctx;
3408 	const size_t c = 3 * sizeof(u64);
3409 
3410 	if (inodes->bytes_left >= c) {
3411 		inodes->bytes_left -= c;
3412 		inodes->val[inodes->elem_cnt] = inum;
3413 		inodes->val[inodes->elem_cnt + 1] = offset;
3414 		inodes->val[inodes->elem_cnt + 2] = root;
3415 		inodes->elem_cnt += 3;
3416 	} else {
3417 		inodes->bytes_missing += c - inodes->bytes_left;
3418 		inodes->bytes_left = 0;
3419 		inodes->elem_missed += 3;
3420 	}
3421 
3422 	return 0;
3423 }
3424 
3425 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3426 					void __user *arg)
3427 {
3428 	int ret = 0;
3429 	int size;
3430 	struct btrfs_ioctl_logical_ino_args *loi;
3431 	struct btrfs_data_container *inodes = NULL;
3432 	struct btrfs_path *path = NULL;
3433 
3434 	if (!capable(CAP_SYS_ADMIN))
3435 		return -EPERM;
3436 
3437 	loi = memdup_user(arg, sizeof(*loi));
3438 	if (IS_ERR(loi)) {
3439 		ret = PTR_ERR(loi);
3440 		loi = NULL;
3441 		goto out;
3442 	}
3443 
3444 	path = btrfs_alloc_path();
3445 	if (!path) {
3446 		ret = -ENOMEM;
3447 		goto out;
3448 	}
3449 
3450 	size = min_t(u32, loi->size, 64 * 1024);
3451 	inodes = init_data_container(size);
3452 	if (IS_ERR(inodes)) {
3453 		ret = PTR_ERR(inodes);
3454 		inodes = NULL;
3455 		goto out;
3456 	}
3457 
3458 	ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3459 					  build_ino_list, inodes);
3460 	if (ret == -EINVAL)
3461 		ret = -ENOENT;
3462 	if (ret < 0)
3463 		goto out;
3464 
3465 	ret = copy_to_user((void *)(unsigned long)loi->inodes,
3466 			   (void *)(unsigned long)inodes, size);
3467 	if (ret)
3468 		ret = -EFAULT;
3469 
3470 out:
3471 	btrfs_free_path(path);
3472 	vfree(inodes);
3473 	kfree(loi);
3474 
3475 	return ret;
3476 }
3477 
3478 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
3479 			       struct btrfs_ioctl_balance_args *bargs)
3480 {
3481 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3482 
3483 	bargs->flags = bctl->flags;
3484 
3485 	if (atomic_read(&fs_info->balance_running))
3486 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3487 	if (atomic_read(&fs_info->balance_pause_req))
3488 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3489 	if (atomic_read(&fs_info->balance_cancel_req))
3490 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3491 
3492 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3493 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3494 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3495 
3496 	if (lock) {
3497 		spin_lock(&fs_info->balance_lock);
3498 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3499 		spin_unlock(&fs_info->balance_lock);
3500 	} else {
3501 		memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3502 	}
3503 }
3504 
3505 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3506 {
3507 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3508 	struct btrfs_fs_info *fs_info = root->fs_info;
3509 	struct btrfs_ioctl_balance_args *bargs;
3510 	struct btrfs_balance_control *bctl;
3511 	bool need_unlock; /* for mut. excl. ops lock */
3512 	int ret;
3513 
3514 	if (!capable(CAP_SYS_ADMIN))
3515 		return -EPERM;
3516 
3517 	ret = mnt_want_write_file(file);
3518 	if (ret)
3519 		return ret;
3520 
3521 again:
3522 	if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3523 		mutex_lock(&fs_info->volume_mutex);
3524 		mutex_lock(&fs_info->balance_mutex);
3525 		need_unlock = true;
3526 		goto locked;
3527 	}
3528 
3529 	/*
3530 	 * mut. excl. ops lock is locked.  Three possibilites:
3531 	 *   (1) some other op is running
3532 	 *   (2) balance is running
3533 	 *   (3) balance is paused -- special case (think resume)
3534 	 */
3535 	mutex_lock(&fs_info->balance_mutex);
3536 	if (fs_info->balance_ctl) {
3537 		/* this is either (2) or (3) */
3538 		if (!atomic_read(&fs_info->balance_running)) {
3539 			mutex_unlock(&fs_info->balance_mutex);
3540 			if (!mutex_trylock(&fs_info->volume_mutex))
3541 				goto again;
3542 			mutex_lock(&fs_info->balance_mutex);
3543 
3544 			if (fs_info->balance_ctl &&
3545 			    !atomic_read(&fs_info->balance_running)) {
3546 				/* this is (3) */
3547 				need_unlock = false;
3548 				goto locked;
3549 			}
3550 
3551 			mutex_unlock(&fs_info->balance_mutex);
3552 			mutex_unlock(&fs_info->volume_mutex);
3553 			goto again;
3554 		} else {
3555 			/* this is (2) */
3556 			mutex_unlock(&fs_info->balance_mutex);
3557 			ret = -EINPROGRESS;
3558 			goto out;
3559 		}
3560 	} else {
3561 		/* this is (1) */
3562 		mutex_unlock(&fs_info->balance_mutex);
3563 		pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3564 		ret = -EINVAL;
3565 		goto out;
3566 	}
3567 
3568 locked:
3569 	BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
3570 
3571 	if (arg) {
3572 		bargs = memdup_user(arg, sizeof(*bargs));
3573 		if (IS_ERR(bargs)) {
3574 			ret = PTR_ERR(bargs);
3575 			goto out_unlock;
3576 		}
3577 
3578 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
3579 			if (!fs_info->balance_ctl) {
3580 				ret = -ENOTCONN;
3581 				goto out_bargs;
3582 			}
3583 
3584 			bctl = fs_info->balance_ctl;
3585 			spin_lock(&fs_info->balance_lock);
3586 			bctl->flags |= BTRFS_BALANCE_RESUME;
3587 			spin_unlock(&fs_info->balance_lock);
3588 
3589 			goto do_balance;
3590 		}
3591 	} else {
3592 		bargs = NULL;
3593 	}
3594 
3595 	if (fs_info->balance_ctl) {
3596 		ret = -EINPROGRESS;
3597 		goto out_bargs;
3598 	}
3599 
3600 	bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3601 	if (!bctl) {
3602 		ret = -ENOMEM;
3603 		goto out_bargs;
3604 	}
3605 
3606 	bctl->fs_info = fs_info;
3607 	if (arg) {
3608 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3609 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3610 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3611 
3612 		bctl->flags = bargs->flags;
3613 	} else {
3614 		/* balance everything - no filters */
3615 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
3616 	}
3617 
3618 do_balance:
3619 	/*
3620 	 * Ownership of bctl and mutually_exclusive_operation_running
3621 	 * goes to to btrfs_balance.  bctl is freed in __cancel_balance,
3622 	 * or, if restriper was paused all the way until unmount, in
3623 	 * free_fs_info.  mutually_exclusive_operation_running is
3624 	 * cleared in __cancel_balance.
3625 	 */
3626 	need_unlock = false;
3627 
3628 	ret = btrfs_balance(bctl, bargs);
3629 
3630 	if (arg) {
3631 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
3632 			ret = -EFAULT;
3633 	}
3634 
3635 out_bargs:
3636 	kfree(bargs);
3637 out_unlock:
3638 	mutex_unlock(&fs_info->balance_mutex);
3639 	mutex_unlock(&fs_info->volume_mutex);
3640 	if (need_unlock)
3641 		atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3642 out:
3643 	mnt_drop_write_file(file);
3644 	return ret;
3645 }
3646 
3647 static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3648 {
3649 	if (!capable(CAP_SYS_ADMIN))
3650 		return -EPERM;
3651 
3652 	switch (cmd) {
3653 	case BTRFS_BALANCE_CTL_PAUSE:
3654 		return btrfs_pause_balance(root->fs_info);
3655 	case BTRFS_BALANCE_CTL_CANCEL:
3656 		return btrfs_cancel_balance(root->fs_info);
3657 	}
3658 
3659 	return -EINVAL;
3660 }
3661 
3662 static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3663 					 void __user *arg)
3664 {
3665 	struct btrfs_fs_info *fs_info = root->fs_info;
3666 	struct btrfs_ioctl_balance_args *bargs;
3667 	int ret = 0;
3668 
3669 	if (!capable(CAP_SYS_ADMIN))
3670 		return -EPERM;
3671 
3672 	mutex_lock(&fs_info->balance_mutex);
3673 	if (!fs_info->balance_ctl) {
3674 		ret = -ENOTCONN;
3675 		goto out;
3676 	}
3677 
3678 	bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3679 	if (!bargs) {
3680 		ret = -ENOMEM;
3681 		goto out;
3682 	}
3683 
3684 	update_ioctl_balance_args(fs_info, 1, bargs);
3685 
3686 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
3687 		ret = -EFAULT;
3688 
3689 	kfree(bargs);
3690 out:
3691 	mutex_unlock(&fs_info->balance_mutex);
3692 	return ret;
3693 }
3694 
3695 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
3696 {
3697 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3698 	struct btrfs_ioctl_quota_ctl_args *sa;
3699 	struct btrfs_trans_handle *trans = NULL;
3700 	int ret;
3701 	int err;
3702 
3703 	if (!capable(CAP_SYS_ADMIN))
3704 		return -EPERM;
3705 
3706 	ret = mnt_want_write_file(file);
3707 	if (ret)
3708 		return ret;
3709 
3710 	sa = memdup_user(arg, sizeof(*sa));
3711 	if (IS_ERR(sa)) {
3712 		ret = PTR_ERR(sa);
3713 		goto drop_write;
3714 	}
3715 
3716 	down_write(&root->fs_info->subvol_sem);
3717 	trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
3718 	if (IS_ERR(trans)) {
3719 		ret = PTR_ERR(trans);
3720 		goto out;
3721 	}
3722 
3723 	switch (sa->cmd) {
3724 	case BTRFS_QUOTA_CTL_ENABLE:
3725 		ret = btrfs_quota_enable(trans, root->fs_info);
3726 		break;
3727 	case BTRFS_QUOTA_CTL_DISABLE:
3728 		ret = btrfs_quota_disable(trans, root->fs_info);
3729 		break;
3730 	default:
3731 		ret = -EINVAL;
3732 		break;
3733 	}
3734 
3735 	err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
3736 	if (err && !ret)
3737 		ret = err;
3738 out:
3739 	kfree(sa);
3740 	up_write(&root->fs_info->subvol_sem);
3741 drop_write:
3742 	mnt_drop_write_file(file);
3743 	return ret;
3744 }
3745 
3746 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
3747 {
3748 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3749 	struct btrfs_ioctl_qgroup_assign_args *sa;
3750 	struct btrfs_trans_handle *trans;
3751 	int ret;
3752 	int err;
3753 
3754 	if (!capable(CAP_SYS_ADMIN))
3755 		return -EPERM;
3756 
3757 	ret = mnt_want_write_file(file);
3758 	if (ret)
3759 		return ret;
3760 
3761 	sa = memdup_user(arg, sizeof(*sa));
3762 	if (IS_ERR(sa)) {
3763 		ret = PTR_ERR(sa);
3764 		goto drop_write;
3765 	}
3766 
3767 	trans = btrfs_join_transaction(root);
3768 	if (IS_ERR(trans)) {
3769 		ret = PTR_ERR(trans);
3770 		goto out;
3771 	}
3772 
3773 	/* FIXME: check if the IDs really exist */
3774 	if (sa->assign) {
3775 		ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3776 						sa->src, sa->dst);
3777 	} else {
3778 		ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3779 						sa->src, sa->dst);
3780 	}
3781 
3782 	err = btrfs_end_transaction(trans, root);
3783 	if (err && !ret)
3784 		ret = err;
3785 
3786 out:
3787 	kfree(sa);
3788 drop_write:
3789 	mnt_drop_write_file(file);
3790 	return ret;
3791 }
3792 
3793 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
3794 {
3795 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3796 	struct btrfs_ioctl_qgroup_create_args *sa;
3797 	struct btrfs_trans_handle *trans;
3798 	int ret;
3799 	int err;
3800 
3801 	if (!capable(CAP_SYS_ADMIN))
3802 		return -EPERM;
3803 
3804 	ret = mnt_want_write_file(file);
3805 	if (ret)
3806 		return ret;
3807 
3808 	sa = memdup_user(arg, sizeof(*sa));
3809 	if (IS_ERR(sa)) {
3810 		ret = PTR_ERR(sa);
3811 		goto drop_write;
3812 	}
3813 
3814 	if (!sa->qgroupid) {
3815 		ret = -EINVAL;
3816 		goto out;
3817 	}
3818 
3819 	trans = btrfs_join_transaction(root);
3820 	if (IS_ERR(trans)) {
3821 		ret = PTR_ERR(trans);
3822 		goto out;
3823 	}
3824 
3825 	/* FIXME: check if the IDs really exist */
3826 	if (sa->create) {
3827 		ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3828 					  NULL);
3829 	} else {
3830 		ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3831 	}
3832 
3833 	err = btrfs_end_transaction(trans, root);
3834 	if (err && !ret)
3835 		ret = err;
3836 
3837 out:
3838 	kfree(sa);
3839 drop_write:
3840 	mnt_drop_write_file(file);
3841 	return ret;
3842 }
3843 
3844 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
3845 {
3846 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3847 	struct btrfs_ioctl_qgroup_limit_args *sa;
3848 	struct btrfs_trans_handle *trans;
3849 	int ret;
3850 	int err;
3851 	u64 qgroupid;
3852 
3853 	if (!capable(CAP_SYS_ADMIN))
3854 		return -EPERM;
3855 
3856 	ret = mnt_want_write_file(file);
3857 	if (ret)
3858 		return ret;
3859 
3860 	sa = memdup_user(arg, sizeof(*sa));
3861 	if (IS_ERR(sa)) {
3862 		ret = PTR_ERR(sa);
3863 		goto drop_write;
3864 	}
3865 
3866 	trans = btrfs_join_transaction(root);
3867 	if (IS_ERR(trans)) {
3868 		ret = PTR_ERR(trans);
3869 		goto out;
3870 	}
3871 
3872 	qgroupid = sa->qgroupid;
3873 	if (!qgroupid) {
3874 		/* take the current subvol as qgroup */
3875 		qgroupid = root->root_key.objectid;
3876 	}
3877 
3878 	/* FIXME: check if the IDs really exist */
3879 	ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3880 
3881 	err = btrfs_end_transaction(trans, root);
3882 	if (err && !ret)
3883 		ret = err;
3884 
3885 out:
3886 	kfree(sa);
3887 drop_write:
3888 	mnt_drop_write_file(file);
3889 	return ret;
3890 }
3891 
3892 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
3893 {
3894 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3895 	struct btrfs_ioctl_quota_rescan_args *qsa;
3896 	int ret;
3897 
3898 	if (!capable(CAP_SYS_ADMIN))
3899 		return -EPERM;
3900 
3901 	ret = mnt_want_write_file(file);
3902 	if (ret)
3903 		return ret;
3904 
3905 	qsa = memdup_user(arg, sizeof(*qsa));
3906 	if (IS_ERR(qsa)) {
3907 		ret = PTR_ERR(qsa);
3908 		goto drop_write;
3909 	}
3910 
3911 	if (qsa->flags) {
3912 		ret = -EINVAL;
3913 		goto out;
3914 	}
3915 
3916 	ret = btrfs_qgroup_rescan(root->fs_info);
3917 
3918 out:
3919 	kfree(qsa);
3920 drop_write:
3921 	mnt_drop_write_file(file);
3922 	return ret;
3923 }
3924 
3925 static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
3926 {
3927 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3928 	struct btrfs_ioctl_quota_rescan_args *qsa;
3929 	int ret = 0;
3930 
3931 	if (!capable(CAP_SYS_ADMIN))
3932 		return -EPERM;
3933 
3934 	qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
3935 	if (!qsa)
3936 		return -ENOMEM;
3937 
3938 	if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3939 		qsa->flags = 1;
3940 		qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
3941 	}
3942 
3943 	if (copy_to_user(arg, qsa, sizeof(*qsa)))
3944 		ret = -EFAULT;
3945 
3946 	kfree(qsa);
3947 	return ret;
3948 }
3949 
3950 static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
3951 {
3952 	struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3953 
3954 	if (!capable(CAP_SYS_ADMIN))
3955 		return -EPERM;
3956 
3957 	return btrfs_qgroup_wait_for_completion(root->fs_info);
3958 }
3959 
3960 static long btrfs_ioctl_set_received_subvol(struct file *file,
3961 					    void __user *arg)
3962 {
3963 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
3964 	struct inode *inode = file_inode(file);
3965 	struct btrfs_root *root = BTRFS_I(inode)->root;
3966 	struct btrfs_root_item *root_item = &root->root_item;
3967 	struct btrfs_trans_handle *trans;
3968 	struct timespec ct = CURRENT_TIME;
3969 	int ret = 0;
3970 
3971 	ret = mnt_want_write_file(file);
3972 	if (ret < 0)
3973 		return ret;
3974 
3975 	down_write(&root->fs_info->subvol_sem);
3976 
3977 	if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3978 		ret = -EINVAL;
3979 		goto out;
3980 	}
3981 
3982 	if (btrfs_root_readonly(root)) {
3983 		ret = -EROFS;
3984 		goto out;
3985 	}
3986 
3987 	if (!inode_owner_or_capable(inode)) {
3988 		ret = -EACCES;
3989 		goto out;
3990 	}
3991 
3992 	sa = memdup_user(arg, sizeof(*sa));
3993 	if (IS_ERR(sa)) {
3994 		ret = PTR_ERR(sa);
3995 		sa = NULL;
3996 		goto out;
3997 	}
3998 
3999 	trans = btrfs_start_transaction(root, 1);
4000 	if (IS_ERR(trans)) {
4001 		ret = PTR_ERR(trans);
4002 		trans = NULL;
4003 		goto out;
4004 	}
4005 
4006 	sa->rtransid = trans->transid;
4007 	sa->rtime.sec = ct.tv_sec;
4008 	sa->rtime.nsec = ct.tv_nsec;
4009 
4010 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4011 	btrfs_set_root_stransid(root_item, sa->stransid);
4012 	btrfs_set_root_rtransid(root_item, sa->rtransid);
4013 	root_item->stime.sec = cpu_to_le64(sa->stime.sec);
4014 	root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
4015 	root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
4016 	root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
4017 
4018 	ret = btrfs_update_root(trans, root->fs_info->tree_root,
4019 				&root->root_key, &root->root_item);
4020 	if (ret < 0) {
4021 		btrfs_end_transaction(trans, root);
4022 		trans = NULL;
4023 		goto out;
4024 	} else {
4025 		ret = btrfs_commit_transaction(trans, root);
4026 		if (ret < 0)
4027 			goto out;
4028 	}
4029 
4030 	ret = copy_to_user(arg, sa, sizeof(*sa));
4031 	if (ret)
4032 		ret = -EFAULT;
4033 
4034 out:
4035 	kfree(sa);
4036 	up_write(&root->fs_info->subvol_sem);
4037 	mnt_drop_write_file(file);
4038 	return ret;
4039 }
4040 
4041 static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4042 {
4043 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4044 	const char *label = root->fs_info->super_copy->label;
4045 	size_t len = strnlen(label, BTRFS_LABEL_SIZE);
4046 	int ret;
4047 
4048 	if (len == BTRFS_LABEL_SIZE) {
4049 		pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4050 			--len);
4051 	}
4052 
4053 	mutex_lock(&root->fs_info->volume_mutex);
4054 	ret = copy_to_user(arg, label, len);
4055 	mutex_unlock(&root->fs_info->volume_mutex);
4056 
4057 	return ret ? -EFAULT : 0;
4058 }
4059 
4060 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4061 {
4062 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4063 	struct btrfs_super_block *super_block = root->fs_info->super_copy;
4064 	struct btrfs_trans_handle *trans;
4065 	char label[BTRFS_LABEL_SIZE];
4066 	int ret;
4067 
4068 	if (!capable(CAP_SYS_ADMIN))
4069 		return -EPERM;
4070 
4071 	if (copy_from_user(label, arg, sizeof(label)))
4072 		return -EFAULT;
4073 
4074 	if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4075 		pr_err("btrfs: unable to set label with more than %d bytes\n",
4076 		       BTRFS_LABEL_SIZE - 1);
4077 		return -EINVAL;
4078 	}
4079 
4080 	ret = mnt_want_write_file(file);
4081 	if (ret)
4082 		return ret;
4083 
4084 	mutex_lock(&root->fs_info->volume_mutex);
4085 	trans = btrfs_start_transaction(root, 0);
4086 	if (IS_ERR(trans)) {
4087 		ret = PTR_ERR(trans);
4088 		goto out_unlock;
4089 	}
4090 
4091 	strcpy(super_block->label, label);
4092 	ret = btrfs_end_transaction(trans, root);
4093 
4094 out_unlock:
4095 	mutex_unlock(&root->fs_info->volume_mutex);
4096 	mnt_drop_write_file(file);
4097 	return ret;
4098 }
4099 
4100 long btrfs_ioctl(struct file *file, unsigned int
4101 		cmd, unsigned long arg)
4102 {
4103 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4104 	void __user *argp = (void __user *)arg;
4105 
4106 	switch (cmd) {
4107 	case FS_IOC_GETFLAGS:
4108 		return btrfs_ioctl_getflags(file, argp);
4109 	case FS_IOC_SETFLAGS:
4110 		return btrfs_ioctl_setflags(file, argp);
4111 	case FS_IOC_GETVERSION:
4112 		return btrfs_ioctl_getversion(file, argp);
4113 	case FITRIM:
4114 		return btrfs_ioctl_fitrim(file, argp);
4115 	case BTRFS_IOC_SNAP_CREATE:
4116 		return btrfs_ioctl_snap_create(file, argp, 0);
4117 	case BTRFS_IOC_SNAP_CREATE_V2:
4118 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
4119 	case BTRFS_IOC_SUBVOL_CREATE:
4120 		return btrfs_ioctl_snap_create(file, argp, 1);
4121 	case BTRFS_IOC_SUBVOL_CREATE_V2:
4122 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
4123 	case BTRFS_IOC_SNAP_DESTROY:
4124 		return btrfs_ioctl_snap_destroy(file, argp);
4125 	case BTRFS_IOC_SUBVOL_GETFLAGS:
4126 		return btrfs_ioctl_subvol_getflags(file, argp);
4127 	case BTRFS_IOC_SUBVOL_SETFLAGS:
4128 		return btrfs_ioctl_subvol_setflags(file, argp);
4129 	case BTRFS_IOC_DEFAULT_SUBVOL:
4130 		return btrfs_ioctl_default_subvol(file, argp);
4131 	case BTRFS_IOC_DEFRAG:
4132 		return btrfs_ioctl_defrag(file, NULL);
4133 	case BTRFS_IOC_DEFRAG_RANGE:
4134 		return btrfs_ioctl_defrag(file, argp);
4135 	case BTRFS_IOC_RESIZE:
4136 		return btrfs_ioctl_resize(file, argp);
4137 	case BTRFS_IOC_ADD_DEV:
4138 		return btrfs_ioctl_add_dev(root, argp);
4139 	case BTRFS_IOC_RM_DEV:
4140 		return btrfs_ioctl_rm_dev(file, argp);
4141 	case BTRFS_IOC_FS_INFO:
4142 		return btrfs_ioctl_fs_info(root, argp);
4143 	case BTRFS_IOC_DEV_INFO:
4144 		return btrfs_ioctl_dev_info(root, argp);
4145 	case BTRFS_IOC_BALANCE:
4146 		return btrfs_ioctl_balance(file, NULL);
4147 	case BTRFS_IOC_CLONE:
4148 		return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4149 	case BTRFS_IOC_CLONE_RANGE:
4150 		return btrfs_ioctl_clone_range(file, argp);
4151 	case BTRFS_IOC_TRANS_START:
4152 		return btrfs_ioctl_trans_start(file);
4153 	case BTRFS_IOC_TRANS_END:
4154 		return btrfs_ioctl_trans_end(file);
4155 	case BTRFS_IOC_TREE_SEARCH:
4156 		return btrfs_ioctl_tree_search(file, argp);
4157 	case BTRFS_IOC_INO_LOOKUP:
4158 		return btrfs_ioctl_ino_lookup(file, argp);
4159 	case BTRFS_IOC_INO_PATHS:
4160 		return btrfs_ioctl_ino_to_path(root, argp);
4161 	case BTRFS_IOC_LOGICAL_INO:
4162 		return btrfs_ioctl_logical_to_ino(root, argp);
4163 	case BTRFS_IOC_SPACE_INFO:
4164 		return btrfs_ioctl_space_info(root, argp);
4165 	case BTRFS_IOC_SYNC:
4166 		btrfs_sync_fs(file->f_dentry->d_sb, 1);
4167 		return 0;
4168 	case BTRFS_IOC_START_SYNC:
4169 		return btrfs_ioctl_start_sync(root, argp);
4170 	case BTRFS_IOC_WAIT_SYNC:
4171 		return btrfs_ioctl_wait_sync(root, argp);
4172 	case BTRFS_IOC_SCRUB:
4173 		return btrfs_ioctl_scrub(file, argp);
4174 	case BTRFS_IOC_SCRUB_CANCEL:
4175 		return btrfs_ioctl_scrub_cancel(root, argp);
4176 	case BTRFS_IOC_SCRUB_PROGRESS:
4177 		return btrfs_ioctl_scrub_progress(root, argp);
4178 	case BTRFS_IOC_BALANCE_V2:
4179 		return btrfs_ioctl_balance(file, argp);
4180 	case BTRFS_IOC_BALANCE_CTL:
4181 		return btrfs_ioctl_balance_ctl(root, arg);
4182 	case BTRFS_IOC_BALANCE_PROGRESS:
4183 		return btrfs_ioctl_balance_progress(root, argp);
4184 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4185 		return btrfs_ioctl_set_received_subvol(file, argp);
4186 	case BTRFS_IOC_SEND:
4187 		return btrfs_ioctl_send(file, argp);
4188 	case BTRFS_IOC_GET_DEV_STATS:
4189 		return btrfs_ioctl_get_dev_stats(root, argp);
4190 	case BTRFS_IOC_QUOTA_CTL:
4191 		return btrfs_ioctl_quota_ctl(file, argp);
4192 	case BTRFS_IOC_QGROUP_ASSIGN:
4193 		return btrfs_ioctl_qgroup_assign(file, argp);
4194 	case BTRFS_IOC_QGROUP_CREATE:
4195 		return btrfs_ioctl_qgroup_create(file, argp);
4196 	case BTRFS_IOC_QGROUP_LIMIT:
4197 		return btrfs_ioctl_qgroup_limit(file, argp);
4198 	case BTRFS_IOC_QUOTA_RESCAN:
4199 		return btrfs_ioctl_quota_rescan(file, argp);
4200 	case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4201 		return btrfs_ioctl_quota_rescan_status(file, argp);
4202 	case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4203 		return btrfs_ioctl_quota_rescan_wait(file, argp);
4204 	case BTRFS_IOC_DEV_REPLACE:
4205 		return btrfs_ioctl_dev_replace(root, argp);
4206 	case BTRFS_IOC_GET_FSLABEL:
4207 		return btrfs_ioctl_get_fslabel(file, argp);
4208 	case BTRFS_IOC_SET_FSLABEL:
4209 		return btrfs_ioctl_set_fslabel(file, argp);
4210 	}
4211 
4212 	return -ENOTTY;
4213 }
4214