Merge branch 'fix/fst-sysfs' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[deliverable/linux.git] / fs / btrfs / ioctl.c
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 <linux/uaccess.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 #include "props.h"
60 #include "sysfs.h"
61 #include "qgroup.h"
62
63 #ifdef CONFIG_64BIT
64 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
65 * structures are incorrect, as the timespec structure from userspace
66 * is 4 bytes too small. We define these alternatives here to teach
67 * the kernel about the 32-bit struct packing.
68 */
69 struct btrfs_ioctl_timespec_32 {
70 __u64 sec;
71 __u32 nsec;
72 } __attribute__ ((__packed__));
73
74 struct btrfs_ioctl_received_subvol_args_32 {
75 char uuid[BTRFS_UUID_SIZE]; /* in */
76 __u64 stransid; /* in */
77 __u64 rtransid; /* out */
78 struct btrfs_ioctl_timespec_32 stime; /* in */
79 struct btrfs_ioctl_timespec_32 rtime; /* out */
80 __u64 flags; /* in */
81 __u64 reserved[16]; /* in */
82 } __attribute__ ((__packed__));
83
84 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
85 struct btrfs_ioctl_received_subvol_args_32)
86 #endif
87
88
89 static int btrfs_clone(struct inode *src, struct inode *inode,
90 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
91 int no_time_update);
92
93 /* Mask out flags that are inappropriate for the given type of inode. */
94 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
95 {
96 if (S_ISDIR(mode))
97 return flags;
98 else if (S_ISREG(mode))
99 return flags & ~FS_DIRSYNC_FL;
100 else
101 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
102 }
103
104 /*
105 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
106 */
107 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
108 {
109 unsigned int iflags = 0;
110
111 if (flags & BTRFS_INODE_SYNC)
112 iflags |= FS_SYNC_FL;
113 if (flags & BTRFS_INODE_IMMUTABLE)
114 iflags |= FS_IMMUTABLE_FL;
115 if (flags & BTRFS_INODE_APPEND)
116 iflags |= FS_APPEND_FL;
117 if (flags & BTRFS_INODE_NODUMP)
118 iflags |= FS_NODUMP_FL;
119 if (flags & BTRFS_INODE_NOATIME)
120 iflags |= FS_NOATIME_FL;
121 if (flags & BTRFS_INODE_DIRSYNC)
122 iflags |= FS_DIRSYNC_FL;
123 if (flags & BTRFS_INODE_NODATACOW)
124 iflags |= FS_NOCOW_FL;
125
126 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
127 iflags |= FS_COMPR_FL;
128 else if (flags & BTRFS_INODE_NOCOMPRESS)
129 iflags |= FS_NOCOMP_FL;
130
131 return iflags;
132 }
133
134 /*
135 * Update inode->i_flags based on the btrfs internal flags.
136 */
137 void btrfs_update_iflags(struct inode *inode)
138 {
139 struct btrfs_inode *ip = BTRFS_I(inode);
140 unsigned int new_fl = 0;
141
142 if (ip->flags & BTRFS_INODE_SYNC)
143 new_fl |= S_SYNC;
144 if (ip->flags & BTRFS_INODE_IMMUTABLE)
145 new_fl |= S_IMMUTABLE;
146 if (ip->flags & BTRFS_INODE_APPEND)
147 new_fl |= S_APPEND;
148 if (ip->flags & BTRFS_INODE_NOATIME)
149 new_fl |= S_NOATIME;
150 if (ip->flags & BTRFS_INODE_DIRSYNC)
151 new_fl |= S_DIRSYNC;
152
153 set_mask_bits(&inode->i_flags,
154 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
155 new_fl);
156 }
157
158 /*
159 * Inherit flags from the parent inode.
160 *
161 * Currently only the compression flags and the cow flags are inherited.
162 */
163 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
164 {
165 unsigned int flags;
166
167 if (!dir)
168 return;
169
170 flags = BTRFS_I(dir)->flags;
171
172 if (flags & BTRFS_INODE_NOCOMPRESS) {
173 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
174 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
175 } else if (flags & BTRFS_INODE_COMPRESS) {
176 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
177 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
178 }
179
180 if (flags & BTRFS_INODE_NODATACOW) {
181 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
182 if (S_ISREG(inode->i_mode))
183 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
184 }
185
186 btrfs_update_iflags(inode);
187 }
188
189 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
190 {
191 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
192 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
193
194 if (copy_to_user(arg, &flags, sizeof(flags)))
195 return -EFAULT;
196 return 0;
197 }
198
199 static int check_flags(unsigned int flags)
200 {
201 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
202 FS_NOATIME_FL | FS_NODUMP_FL | \
203 FS_SYNC_FL | FS_DIRSYNC_FL | \
204 FS_NOCOMP_FL | FS_COMPR_FL |
205 FS_NOCOW_FL))
206 return -EOPNOTSUPP;
207
208 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
209 return -EINVAL;
210
211 return 0;
212 }
213
214 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
215 {
216 struct inode *inode = file_inode(file);
217 struct btrfs_inode *ip = BTRFS_I(inode);
218 struct btrfs_root *root = ip->root;
219 struct btrfs_trans_handle *trans;
220 unsigned int flags, oldflags;
221 int ret;
222 u64 ip_oldflags;
223 unsigned int i_oldflags;
224 umode_t mode;
225
226 if (!inode_owner_or_capable(inode))
227 return -EPERM;
228
229 if (btrfs_root_readonly(root))
230 return -EROFS;
231
232 if (copy_from_user(&flags, arg, sizeof(flags)))
233 return -EFAULT;
234
235 ret = check_flags(flags);
236 if (ret)
237 return ret;
238
239 ret = mnt_want_write_file(file);
240 if (ret)
241 return ret;
242
243 mutex_lock(&inode->i_mutex);
244
245 ip_oldflags = ip->flags;
246 i_oldflags = inode->i_flags;
247 mode = inode->i_mode;
248
249 flags = btrfs_mask_flags(inode->i_mode, flags);
250 oldflags = btrfs_flags_to_ioctl(ip->flags);
251 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
252 if (!capable(CAP_LINUX_IMMUTABLE)) {
253 ret = -EPERM;
254 goto out_unlock;
255 }
256 }
257
258 if (flags & FS_SYNC_FL)
259 ip->flags |= BTRFS_INODE_SYNC;
260 else
261 ip->flags &= ~BTRFS_INODE_SYNC;
262 if (flags & FS_IMMUTABLE_FL)
263 ip->flags |= BTRFS_INODE_IMMUTABLE;
264 else
265 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
266 if (flags & FS_APPEND_FL)
267 ip->flags |= BTRFS_INODE_APPEND;
268 else
269 ip->flags &= ~BTRFS_INODE_APPEND;
270 if (flags & FS_NODUMP_FL)
271 ip->flags |= BTRFS_INODE_NODUMP;
272 else
273 ip->flags &= ~BTRFS_INODE_NODUMP;
274 if (flags & FS_NOATIME_FL)
275 ip->flags |= BTRFS_INODE_NOATIME;
276 else
277 ip->flags &= ~BTRFS_INODE_NOATIME;
278 if (flags & FS_DIRSYNC_FL)
279 ip->flags |= BTRFS_INODE_DIRSYNC;
280 else
281 ip->flags &= ~BTRFS_INODE_DIRSYNC;
282 if (flags & FS_NOCOW_FL) {
283 if (S_ISREG(mode)) {
284 /*
285 * It's safe to turn csums off here, no extents exist.
286 * Otherwise we want the flag to reflect the real COW
287 * status of the file and will not set it.
288 */
289 if (inode->i_size == 0)
290 ip->flags |= BTRFS_INODE_NODATACOW
291 | BTRFS_INODE_NODATASUM;
292 } else {
293 ip->flags |= BTRFS_INODE_NODATACOW;
294 }
295 } else {
296 /*
297 * Revert back under same assuptions as above
298 */
299 if (S_ISREG(mode)) {
300 if (inode->i_size == 0)
301 ip->flags &= ~(BTRFS_INODE_NODATACOW
302 | BTRFS_INODE_NODATASUM);
303 } else {
304 ip->flags &= ~BTRFS_INODE_NODATACOW;
305 }
306 }
307
308 /*
309 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
310 * flag may be changed automatically if compression code won't make
311 * things smaller.
312 */
313 if (flags & FS_NOCOMP_FL) {
314 ip->flags &= ~BTRFS_INODE_COMPRESS;
315 ip->flags |= BTRFS_INODE_NOCOMPRESS;
316
317 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
318 if (ret && ret != -ENODATA)
319 goto out_drop;
320 } else if (flags & FS_COMPR_FL) {
321 const char *comp;
322
323 ip->flags |= BTRFS_INODE_COMPRESS;
324 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
325
326 if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
327 comp = "lzo";
328 else
329 comp = "zlib";
330 ret = btrfs_set_prop(inode, "btrfs.compression",
331 comp, strlen(comp), 0);
332 if (ret)
333 goto out_drop;
334
335 } else {
336 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
337 if (ret && ret != -ENODATA)
338 goto out_drop;
339 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
340 }
341
342 trans = btrfs_start_transaction(root, 1);
343 if (IS_ERR(trans)) {
344 ret = PTR_ERR(trans);
345 goto out_drop;
346 }
347
348 btrfs_update_iflags(inode);
349 inode_inc_iversion(inode);
350 inode->i_ctime = CURRENT_TIME;
351 ret = btrfs_update_inode(trans, root, inode);
352
353 btrfs_end_transaction(trans, root);
354 out_drop:
355 if (ret) {
356 ip->flags = ip_oldflags;
357 inode->i_flags = i_oldflags;
358 }
359
360 out_unlock:
361 mutex_unlock(&inode->i_mutex);
362 mnt_drop_write_file(file);
363 return ret;
364 }
365
366 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
367 {
368 struct inode *inode = file_inode(file);
369
370 return put_user(inode->i_generation, arg);
371 }
372
373 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
374 {
375 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
376 struct btrfs_device *device;
377 struct request_queue *q;
378 struct fstrim_range range;
379 u64 minlen = ULLONG_MAX;
380 u64 num_devices = 0;
381 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
382 int ret;
383
384 if (!capable(CAP_SYS_ADMIN))
385 return -EPERM;
386
387 rcu_read_lock();
388 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
389 dev_list) {
390 if (!device->bdev)
391 continue;
392 q = bdev_get_queue(device->bdev);
393 if (blk_queue_discard(q)) {
394 num_devices++;
395 minlen = min((u64)q->limits.discard_granularity,
396 minlen);
397 }
398 }
399 rcu_read_unlock();
400
401 if (!num_devices)
402 return -EOPNOTSUPP;
403 if (copy_from_user(&range, arg, sizeof(range)))
404 return -EFAULT;
405 if (range.start > total_bytes ||
406 range.len < fs_info->sb->s_blocksize)
407 return -EINVAL;
408
409 range.len = min(range.len, total_bytes - range.start);
410 range.minlen = max(range.minlen, minlen);
411 ret = btrfs_trim_fs(fs_info->tree_root, &range);
412 if (ret < 0)
413 return ret;
414
415 if (copy_to_user(arg, &range, sizeof(range)))
416 return -EFAULT;
417
418 return 0;
419 }
420
421 int btrfs_is_empty_uuid(u8 *uuid)
422 {
423 int i;
424
425 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
426 if (uuid[i])
427 return 0;
428 }
429 return 1;
430 }
431
432 static noinline int create_subvol(struct inode *dir,
433 struct dentry *dentry,
434 char *name, int namelen,
435 u64 *async_transid,
436 struct btrfs_qgroup_inherit *inherit)
437 {
438 struct btrfs_trans_handle *trans;
439 struct btrfs_key key;
440 struct btrfs_root_item root_item;
441 struct btrfs_inode_item *inode_item;
442 struct extent_buffer *leaf;
443 struct btrfs_root *root = BTRFS_I(dir)->root;
444 struct btrfs_root *new_root;
445 struct btrfs_block_rsv block_rsv;
446 struct timespec cur_time = CURRENT_TIME;
447 struct inode *inode;
448 int ret;
449 int err;
450 u64 objectid;
451 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
452 u64 index = 0;
453 u64 qgroup_reserved;
454 uuid_le new_uuid;
455
456 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
457 if (ret)
458 return ret;
459
460 /*
461 * Don't create subvolume whose level is not zero. Or qgroup will be
462 * screwed up since it assume subvolme qgroup's level to be 0.
463 */
464 if (btrfs_qgroup_level(objectid))
465 return -ENOSPC;
466
467 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
468 /*
469 * The same as the snapshot creation, please see the comment
470 * of create_snapshot().
471 */
472 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
473 8, &qgroup_reserved, false);
474 if (ret)
475 return ret;
476
477 trans = btrfs_start_transaction(root, 0);
478 if (IS_ERR(trans)) {
479 ret = PTR_ERR(trans);
480 btrfs_subvolume_release_metadata(root, &block_rsv,
481 qgroup_reserved);
482 return ret;
483 }
484 trans->block_rsv = &block_rsv;
485 trans->bytes_reserved = block_rsv.size;
486
487 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
488 if (ret)
489 goto fail;
490
491 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
492 if (IS_ERR(leaf)) {
493 ret = PTR_ERR(leaf);
494 goto fail;
495 }
496
497 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
498 btrfs_set_header_bytenr(leaf, leaf->start);
499 btrfs_set_header_generation(leaf, trans->transid);
500 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
501 btrfs_set_header_owner(leaf, objectid);
502
503 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
504 BTRFS_FSID_SIZE);
505 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
506 btrfs_header_chunk_tree_uuid(leaf),
507 BTRFS_UUID_SIZE);
508 btrfs_mark_buffer_dirty(leaf);
509
510 memset(&root_item, 0, sizeof(root_item));
511
512 inode_item = &root_item.inode;
513 btrfs_set_stack_inode_generation(inode_item, 1);
514 btrfs_set_stack_inode_size(inode_item, 3);
515 btrfs_set_stack_inode_nlink(inode_item, 1);
516 btrfs_set_stack_inode_nbytes(inode_item, root->nodesize);
517 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
518
519 btrfs_set_root_flags(&root_item, 0);
520 btrfs_set_root_limit(&root_item, 0);
521 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
522
523 btrfs_set_root_bytenr(&root_item, leaf->start);
524 btrfs_set_root_generation(&root_item, trans->transid);
525 btrfs_set_root_level(&root_item, 0);
526 btrfs_set_root_refs(&root_item, 1);
527 btrfs_set_root_used(&root_item, leaf->len);
528 btrfs_set_root_last_snapshot(&root_item, 0);
529
530 btrfs_set_root_generation_v2(&root_item,
531 btrfs_root_generation(&root_item));
532 uuid_le_gen(&new_uuid);
533 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
534 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
535 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
536 root_item.ctime = root_item.otime;
537 btrfs_set_root_ctransid(&root_item, trans->transid);
538 btrfs_set_root_otransid(&root_item, trans->transid);
539
540 btrfs_tree_unlock(leaf);
541 free_extent_buffer(leaf);
542 leaf = NULL;
543
544 btrfs_set_root_dirid(&root_item, new_dirid);
545
546 key.objectid = objectid;
547 key.offset = 0;
548 key.type = BTRFS_ROOT_ITEM_KEY;
549 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
550 &root_item);
551 if (ret)
552 goto fail;
553
554 key.offset = (u64)-1;
555 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
556 if (IS_ERR(new_root)) {
557 ret = PTR_ERR(new_root);
558 btrfs_abort_transaction(trans, root, ret);
559 goto fail;
560 }
561
562 btrfs_record_root_in_trans(trans, new_root);
563
564 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
565 if (ret) {
566 /* We potentially lose an unused inode item here */
567 btrfs_abort_transaction(trans, root, ret);
568 goto fail;
569 }
570
571 mutex_lock(&new_root->objectid_mutex);
572 new_root->highest_objectid = new_dirid;
573 mutex_unlock(&new_root->objectid_mutex);
574
575 /*
576 * insert the directory item
577 */
578 ret = btrfs_set_inode_index(dir, &index);
579 if (ret) {
580 btrfs_abort_transaction(trans, root, ret);
581 goto fail;
582 }
583
584 ret = btrfs_insert_dir_item(trans, root,
585 name, namelen, dir, &key,
586 BTRFS_FT_DIR, index);
587 if (ret) {
588 btrfs_abort_transaction(trans, root, ret);
589 goto fail;
590 }
591
592 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
593 ret = btrfs_update_inode(trans, root, dir);
594 BUG_ON(ret);
595
596 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
597 objectid, root->root_key.objectid,
598 btrfs_ino(dir), index, name, namelen);
599 BUG_ON(ret);
600
601 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
602 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
603 objectid);
604 if (ret)
605 btrfs_abort_transaction(trans, root, ret);
606
607 fail:
608 trans->block_rsv = NULL;
609 trans->bytes_reserved = 0;
610 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
611
612 if (async_transid) {
613 *async_transid = trans->transid;
614 err = btrfs_commit_transaction_async(trans, root, 1);
615 if (err)
616 err = btrfs_commit_transaction(trans, root);
617 } else {
618 err = btrfs_commit_transaction(trans, root);
619 }
620 if (err && !ret)
621 ret = err;
622
623 if (!ret) {
624 inode = btrfs_lookup_dentry(dir, dentry);
625 if (IS_ERR(inode))
626 return PTR_ERR(inode);
627 d_instantiate(dentry, inode);
628 }
629 return ret;
630 }
631
632 static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root *root)
633 {
634 s64 writers;
635 DEFINE_WAIT(wait);
636
637 do {
638 prepare_to_wait(&root->subv_writers->wait, &wait,
639 TASK_UNINTERRUPTIBLE);
640
641 writers = percpu_counter_sum(&root->subv_writers->counter);
642 if (writers)
643 schedule();
644
645 finish_wait(&root->subv_writers->wait, &wait);
646 } while (writers);
647 }
648
649 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
650 struct dentry *dentry, char *name, int namelen,
651 u64 *async_transid, bool readonly,
652 struct btrfs_qgroup_inherit *inherit)
653 {
654 struct inode *inode;
655 struct btrfs_pending_snapshot *pending_snapshot;
656 struct btrfs_trans_handle *trans;
657 int ret;
658
659 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
660 return -EINVAL;
661
662 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
663 if (!pending_snapshot)
664 return -ENOMEM;
665
666 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
667 GFP_NOFS);
668 pending_snapshot->path = btrfs_alloc_path();
669 if (!pending_snapshot->root_item || !pending_snapshot->path) {
670 ret = -ENOMEM;
671 goto free_pending;
672 }
673
674 atomic_inc(&root->will_be_snapshoted);
675 smp_mb__after_atomic();
676 btrfs_wait_for_no_snapshoting_writes(root);
677
678 ret = btrfs_start_delalloc_inodes(root, 0);
679 if (ret)
680 goto dec_and_free;
681
682 btrfs_wait_ordered_extents(root, -1);
683
684 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
685 BTRFS_BLOCK_RSV_TEMP);
686 /*
687 * 1 - parent dir inode
688 * 2 - dir entries
689 * 1 - root item
690 * 2 - root ref/backref
691 * 1 - root of snapshot
692 * 1 - UUID item
693 */
694 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
695 &pending_snapshot->block_rsv, 8,
696 &pending_snapshot->qgroup_reserved,
697 false);
698 if (ret)
699 goto dec_and_free;
700
701 pending_snapshot->dentry = dentry;
702 pending_snapshot->root = root;
703 pending_snapshot->readonly = readonly;
704 pending_snapshot->dir = dir;
705 pending_snapshot->inherit = inherit;
706
707 trans = btrfs_start_transaction(root, 0);
708 if (IS_ERR(trans)) {
709 ret = PTR_ERR(trans);
710 goto fail;
711 }
712
713 spin_lock(&root->fs_info->trans_lock);
714 list_add(&pending_snapshot->list,
715 &trans->transaction->pending_snapshots);
716 spin_unlock(&root->fs_info->trans_lock);
717 if (async_transid) {
718 *async_transid = trans->transid;
719 ret = btrfs_commit_transaction_async(trans,
720 root->fs_info->extent_root, 1);
721 if (ret)
722 ret = btrfs_commit_transaction(trans, root);
723 } else {
724 ret = btrfs_commit_transaction(trans,
725 root->fs_info->extent_root);
726 }
727 if (ret)
728 goto fail;
729
730 ret = pending_snapshot->error;
731 if (ret)
732 goto fail;
733
734 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
735 if (ret)
736 goto fail;
737
738 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
739 if (IS_ERR(inode)) {
740 ret = PTR_ERR(inode);
741 goto fail;
742 }
743
744 d_instantiate(dentry, inode);
745 ret = 0;
746 fail:
747 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
748 &pending_snapshot->block_rsv,
749 pending_snapshot->qgroup_reserved);
750 dec_and_free:
751 if (atomic_dec_and_test(&root->will_be_snapshoted))
752 wake_up_atomic_t(&root->will_be_snapshoted);
753 free_pending:
754 kfree(pending_snapshot->root_item);
755 btrfs_free_path(pending_snapshot->path);
756 kfree(pending_snapshot);
757
758 return ret;
759 }
760
761 /* copy of may_delete in fs/namei.c()
762 * Check whether we can remove a link victim from directory dir, check
763 * whether the type of victim is right.
764 * 1. We can't do it if dir is read-only (done in permission())
765 * 2. We should have write and exec permissions on dir
766 * 3. We can't remove anything from append-only dir
767 * 4. We can't do anything with immutable dir (done in permission())
768 * 5. If the sticky bit on dir is set we should either
769 * a. be owner of dir, or
770 * b. be owner of victim, or
771 * c. have CAP_FOWNER capability
772 * 6. If the victim is append-only or immutable we can't do antyhing with
773 * links pointing to it.
774 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
775 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
776 * 9. We can't remove a root or mountpoint.
777 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
778 * nfs_async_unlink().
779 */
780
781 static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
782 {
783 int error;
784
785 if (d_really_is_negative(victim))
786 return -ENOENT;
787
788 BUG_ON(d_inode(victim->d_parent) != dir);
789 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
790
791 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
792 if (error)
793 return error;
794 if (IS_APPEND(dir))
795 return -EPERM;
796 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
797 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
798 return -EPERM;
799 if (isdir) {
800 if (!d_is_dir(victim))
801 return -ENOTDIR;
802 if (IS_ROOT(victim))
803 return -EBUSY;
804 } else if (d_is_dir(victim))
805 return -EISDIR;
806 if (IS_DEADDIR(dir))
807 return -ENOENT;
808 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
809 return -EBUSY;
810 return 0;
811 }
812
813 /* copy of may_create in fs/namei.c() */
814 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
815 {
816 if (d_really_is_positive(child))
817 return -EEXIST;
818 if (IS_DEADDIR(dir))
819 return -ENOENT;
820 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
821 }
822
823 /*
824 * Create a new subvolume below @parent. This is largely modeled after
825 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
826 * inside this filesystem so it's quite a bit simpler.
827 */
828 static noinline int btrfs_mksubvol(struct path *parent,
829 char *name, int namelen,
830 struct btrfs_root *snap_src,
831 u64 *async_transid, bool readonly,
832 struct btrfs_qgroup_inherit *inherit)
833 {
834 struct inode *dir = d_inode(parent->dentry);
835 struct dentry *dentry;
836 int error;
837
838 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
839 if (error == -EINTR)
840 return error;
841
842 dentry = lookup_one_len(name, parent->dentry, namelen);
843 error = PTR_ERR(dentry);
844 if (IS_ERR(dentry))
845 goto out_unlock;
846
847 error = -EEXIST;
848 if (d_really_is_positive(dentry))
849 goto out_dput;
850
851 error = btrfs_may_create(dir, dentry);
852 if (error)
853 goto out_dput;
854
855 /*
856 * even if this name doesn't exist, we may get hash collisions.
857 * check for them now when we can safely fail
858 */
859 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
860 dir->i_ino, name,
861 namelen);
862 if (error)
863 goto out_dput;
864
865 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
866
867 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
868 goto out_up_read;
869
870 if (snap_src) {
871 error = create_snapshot(snap_src, dir, dentry, name, namelen,
872 async_transid, readonly, inherit);
873 } else {
874 error = create_subvol(dir, dentry, name, namelen,
875 async_transid, inherit);
876 }
877 if (!error)
878 fsnotify_mkdir(dir, dentry);
879 out_up_read:
880 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
881 out_dput:
882 dput(dentry);
883 out_unlock:
884 mutex_unlock(&dir->i_mutex);
885 return error;
886 }
887
888 /*
889 * When we're defragging a range, we don't want to kick it off again
890 * if it is really just waiting for delalloc to send it down.
891 * If we find a nice big extent or delalloc range for the bytes in the
892 * file you want to defrag, we return 0 to let you know to skip this
893 * part of the file
894 */
895 static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
896 {
897 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
898 struct extent_map *em = NULL;
899 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
900 u64 end;
901
902 read_lock(&em_tree->lock);
903 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
904 read_unlock(&em_tree->lock);
905
906 if (em) {
907 end = extent_map_end(em);
908 free_extent_map(em);
909 if (end - offset > thresh)
910 return 0;
911 }
912 /* if we already have a nice delalloc here, just stop */
913 thresh /= 2;
914 end = count_range_bits(io_tree, &offset, offset + thresh,
915 thresh, EXTENT_DELALLOC, 1);
916 if (end >= thresh)
917 return 0;
918 return 1;
919 }
920
921 /*
922 * helper function to walk through a file and find extents
923 * newer than a specific transid, and smaller than thresh.
924 *
925 * This is used by the defragging code to find new and small
926 * extents
927 */
928 static int find_new_extents(struct btrfs_root *root,
929 struct inode *inode, u64 newer_than,
930 u64 *off, u32 thresh)
931 {
932 struct btrfs_path *path;
933 struct btrfs_key min_key;
934 struct extent_buffer *leaf;
935 struct btrfs_file_extent_item *extent;
936 int type;
937 int ret;
938 u64 ino = btrfs_ino(inode);
939
940 path = btrfs_alloc_path();
941 if (!path)
942 return -ENOMEM;
943
944 min_key.objectid = ino;
945 min_key.type = BTRFS_EXTENT_DATA_KEY;
946 min_key.offset = *off;
947
948 while (1) {
949 ret = btrfs_search_forward(root, &min_key, path, newer_than);
950 if (ret != 0)
951 goto none;
952 process_slot:
953 if (min_key.objectid != ino)
954 goto none;
955 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
956 goto none;
957
958 leaf = path->nodes[0];
959 extent = btrfs_item_ptr(leaf, path->slots[0],
960 struct btrfs_file_extent_item);
961
962 type = btrfs_file_extent_type(leaf, extent);
963 if (type == BTRFS_FILE_EXTENT_REG &&
964 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
965 check_defrag_in_cache(inode, min_key.offset, thresh)) {
966 *off = min_key.offset;
967 btrfs_free_path(path);
968 return 0;
969 }
970
971 path->slots[0]++;
972 if (path->slots[0] < btrfs_header_nritems(leaf)) {
973 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
974 goto process_slot;
975 }
976
977 if (min_key.offset == (u64)-1)
978 goto none;
979
980 min_key.offset++;
981 btrfs_release_path(path);
982 }
983 none:
984 btrfs_free_path(path);
985 return -ENOENT;
986 }
987
988 static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
989 {
990 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
991 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
992 struct extent_map *em;
993 u64 len = PAGE_CACHE_SIZE;
994
995 /*
996 * hopefully we have this extent in the tree already, try without
997 * the full extent lock
998 */
999 read_lock(&em_tree->lock);
1000 em = lookup_extent_mapping(em_tree, start, len);
1001 read_unlock(&em_tree->lock);
1002
1003 if (!em) {
1004 struct extent_state *cached = NULL;
1005 u64 end = start + len - 1;
1006
1007 /* get the big lock and read metadata off disk */
1008 lock_extent_bits(io_tree, start, end, &cached);
1009 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
1010 unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
1011
1012 if (IS_ERR(em))
1013 return NULL;
1014 }
1015
1016 return em;
1017 }
1018
1019 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1020 {
1021 struct extent_map *next;
1022 bool ret = true;
1023
1024 /* this is the last extent */
1025 if (em->start + em->len >= i_size_read(inode))
1026 return false;
1027
1028 next = defrag_lookup_extent(inode, em->start + em->len);
1029 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1030 ret = false;
1031 else if ((em->block_start + em->block_len == next->block_start) &&
1032 (em->block_len > SZ_128K && next->block_len > SZ_128K))
1033 ret = false;
1034
1035 free_extent_map(next);
1036 return ret;
1037 }
1038
1039 static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
1040 u64 *last_len, u64 *skip, u64 *defrag_end,
1041 int compress)
1042 {
1043 struct extent_map *em;
1044 int ret = 1;
1045 bool next_mergeable = true;
1046 bool prev_mergeable = true;
1047
1048 /*
1049 * make sure that once we start defragging an extent, we keep on
1050 * defragging it
1051 */
1052 if (start < *defrag_end)
1053 return 1;
1054
1055 *skip = 0;
1056
1057 em = defrag_lookup_extent(inode, start);
1058 if (!em)
1059 return 0;
1060
1061 /* this will cover holes, and inline extents */
1062 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1063 ret = 0;
1064 goto out;
1065 }
1066
1067 if (!*defrag_end)
1068 prev_mergeable = false;
1069
1070 next_mergeable = defrag_check_next_extent(inode, em);
1071 /*
1072 * we hit a real extent, if it is big or the next extent is not a
1073 * real extent, don't bother defragging it
1074 */
1075 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
1076 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
1077 ret = 0;
1078 out:
1079 /*
1080 * last_len ends up being a counter of how many bytes we've defragged.
1081 * every time we choose not to defrag an extent, we reset *last_len
1082 * so that the next tiny extent will force a defrag.
1083 *
1084 * The end result of this is that tiny extents before a single big
1085 * extent will force at least part of that big extent to be defragged.
1086 */
1087 if (ret) {
1088 *defrag_end = extent_map_end(em);
1089 } else {
1090 *last_len = 0;
1091 *skip = extent_map_end(em);
1092 *defrag_end = 0;
1093 }
1094
1095 free_extent_map(em);
1096 return ret;
1097 }
1098
1099 /*
1100 * it doesn't do much good to defrag one or two pages
1101 * at a time. This pulls in a nice chunk of pages
1102 * to COW and defrag.
1103 *
1104 * It also makes sure the delalloc code has enough
1105 * dirty data to avoid making new small extents as part
1106 * of the defrag
1107 *
1108 * It's a good idea to start RA on this range
1109 * before calling this.
1110 */
1111 static int cluster_pages_for_defrag(struct inode *inode,
1112 struct page **pages,
1113 unsigned long start_index,
1114 unsigned long num_pages)
1115 {
1116 unsigned long file_end;
1117 u64 isize = i_size_read(inode);
1118 u64 page_start;
1119 u64 page_end;
1120 u64 page_cnt;
1121 int ret;
1122 int i;
1123 int i_done;
1124 struct btrfs_ordered_extent *ordered;
1125 struct extent_state *cached_state = NULL;
1126 struct extent_io_tree *tree;
1127 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1128
1129 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
1130 if (!isize || start_index > file_end)
1131 return 0;
1132
1133 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1134
1135 ret = btrfs_delalloc_reserve_space(inode,
1136 start_index << PAGE_CACHE_SHIFT,
1137 page_cnt << PAGE_CACHE_SHIFT);
1138 if (ret)
1139 return ret;
1140 i_done = 0;
1141 tree = &BTRFS_I(inode)->io_tree;
1142
1143 /* step one, lock all the pages */
1144 for (i = 0; i < page_cnt; i++) {
1145 struct page *page;
1146 again:
1147 page = find_or_create_page(inode->i_mapping,
1148 start_index + i, mask);
1149 if (!page)
1150 break;
1151
1152 page_start = page_offset(page);
1153 page_end = page_start + PAGE_CACHE_SIZE - 1;
1154 while (1) {
1155 lock_extent_bits(tree, page_start, page_end,
1156 &cached_state);
1157 ordered = btrfs_lookup_ordered_extent(inode,
1158 page_start);
1159 unlock_extent_cached(tree, page_start, page_end,
1160 &cached_state, GFP_NOFS);
1161 if (!ordered)
1162 break;
1163
1164 unlock_page(page);
1165 btrfs_start_ordered_extent(inode, ordered, 1);
1166 btrfs_put_ordered_extent(ordered);
1167 lock_page(page);
1168 /*
1169 * we unlocked the page above, so we need check if
1170 * it was released or not.
1171 */
1172 if (page->mapping != inode->i_mapping) {
1173 unlock_page(page);
1174 page_cache_release(page);
1175 goto again;
1176 }
1177 }
1178
1179 if (!PageUptodate(page)) {
1180 btrfs_readpage(NULL, page);
1181 lock_page(page);
1182 if (!PageUptodate(page)) {
1183 unlock_page(page);
1184 page_cache_release(page);
1185 ret = -EIO;
1186 break;
1187 }
1188 }
1189
1190 if (page->mapping != inode->i_mapping) {
1191 unlock_page(page);
1192 page_cache_release(page);
1193 goto again;
1194 }
1195
1196 pages[i] = page;
1197 i_done++;
1198 }
1199 if (!i_done || ret)
1200 goto out;
1201
1202 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1203 goto out;
1204
1205 /*
1206 * so now we have a nice long stream of locked
1207 * and up to date pages, lets wait on them
1208 */
1209 for (i = 0; i < i_done; i++)
1210 wait_on_page_writeback(pages[i]);
1211
1212 page_start = page_offset(pages[0]);
1213 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1214
1215 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1216 page_start, page_end - 1, &cached_state);
1217 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1218 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1219 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1220 &cached_state, GFP_NOFS);
1221
1222 if (i_done != page_cnt) {
1223 spin_lock(&BTRFS_I(inode)->lock);
1224 BTRFS_I(inode)->outstanding_extents++;
1225 spin_unlock(&BTRFS_I(inode)->lock);
1226 btrfs_delalloc_release_space(inode,
1227 start_index << PAGE_CACHE_SHIFT,
1228 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
1229 }
1230
1231
1232 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1233 &cached_state, GFP_NOFS);
1234
1235 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1236 page_start, page_end - 1, &cached_state,
1237 GFP_NOFS);
1238
1239 for (i = 0; i < i_done; i++) {
1240 clear_page_dirty_for_io(pages[i]);
1241 ClearPageChecked(pages[i]);
1242 set_page_extent_mapped(pages[i]);
1243 set_page_dirty(pages[i]);
1244 unlock_page(pages[i]);
1245 page_cache_release(pages[i]);
1246 }
1247 return i_done;
1248 out:
1249 for (i = 0; i < i_done; i++) {
1250 unlock_page(pages[i]);
1251 page_cache_release(pages[i]);
1252 }
1253 btrfs_delalloc_release_space(inode,
1254 start_index << PAGE_CACHE_SHIFT,
1255 page_cnt << PAGE_CACHE_SHIFT);
1256 return ret;
1257
1258 }
1259
1260 int btrfs_defrag_file(struct inode *inode, struct file *file,
1261 struct btrfs_ioctl_defrag_range_args *range,
1262 u64 newer_than, unsigned long max_to_defrag)
1263 {
1264 struct btrfs_root *root = BTRFS_I(inode)->root;
1265 struct file_ra_state *ra = NULL;
1266 unsigned long last_index;
1267 u64 isize = i_size_read(inode);
1268 u64 last_len = 0;
1269 u64 skip = 0;
1270 u64 defrag_end = 0;
1271 u64 newer_off = range->start;
1272 unsigned long i;
1273 unsigned long ra_index = 0;
1274 int ret;
1275 int defrag_count = 0;
1276 int compress_type = BTRFS_COMPRESS_ZLIB;
1277 u32 extent_thresh = range->extent_thresh;
1278 unsigned long max_cluster = SZ_256K >> PAGE_CACHE_SHIFT;
1279 unsigned long cluster = max_cluster;
1280 u64 new_align = ~((u64)SZ_128K - 1);
1281 struct page **pages = NULL;
1282
1283 if (isize == 0)
1284 return 0;
1285
1286 if (range->start >= isize)
1287 return -EINVAL;
1288
1289 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1290 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1291 return -EINVAL;
1292 if (range->compress_type)
1293 compress_type = range->compress_type;
1294 }
1295
1296 if (extent_thresh == 0)
1297 extent_thresh = SZ_256K;
1298
1299 /*
1300 * if we were not given a file, allocate a readahead
1301 * context
1302 */
1303 if (!file) {
1304 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1305 if (!ra)
1306 return -ENOMEM;
1307 file_ra_state_init(ra, inode->i_mapping);
1308 } else {
1309 ra = &file->f_ra;
1310 }
1311
1312 pages = kmalloc_array(max_cluster, sizeof(struct page *),
1313 GFP_NOFS);
1314 if (!pages) {
1315 ret = -ENOMEM;
1316 goto out_ra;
1317 }
1318
1319 /* find the last page to defrag */
1320 if (range->start + range->len > range->start) {
1321 last_index = min_t(u64, isize - 1,
1322 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1323 } else {
1324 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1325 }
1326
1327 if (newer_than) {
1328 ret = find_new_extents(root, inode, newer_than,
1329 &newer_off, SZ_64K);
1330 if (!ret) {
1331 range->start = newer_off;
1332 /*
1333 * we always align our defrag to help keep
1334 * the extents in the file evenly spaced
1335 */
1336 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1337 } else
1338 goto out_ra;
1339 } else {
1340 i = range->start >> PAGE_CACHE_SHIFT;
1341 }
1342 if (!max_to_defrag)
1343 max_to_defrag = last_index - i + 1;
1344
1345 /*
1346 * make writeback starts from i, so the defrag range can be
1347 * written sequentially.
1348 */
1349 if (i < inode->i_mapping->writeback_index)
1350 inode->i_mapping->writeback_index = i;
1351
1352 while (i <= last_index && defrag_count < max_to_defrag &&
1353 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE))) {
1354 /*
1355 * make sure we stop running if someone unmounts
1356 * the FS
1357 */
1358 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1359 break;
1360
1361 if (btrfs_defrag_cancelled(root->fs_info)) {
1362 btrfs_debug(root->fs_info, "defrag_file cancelled");
1363 ret = -EAGAIN;
1364 break;
1365 }
1366
1367 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1368 extent_thresh, &last_len, &skip,
1369 &defrag_end, range->flags &
1370 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1371 unsigned long next;
1372 /*
1373 * the should_defrag function tells us how much to skip
1374 * bump our counter by the suggested amount
1375 */
1376 next = DIV_ROUND_UP(skip, PAGE_CACHE_SIZE);
1377 i = max(i + 1, next);
1378 continue;
1379 }
1380
1381 if (!newer_than) {
1382 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1383 PAGE_CACHE_SHIFT) - i;
1384 cluster = min(cluster, max_cluster);
1385 } else {
1386 cluster = max_cluster;
1387 }
1388
1389 if (i + cluster > ra_index) {
1390 ra_index = max(i, ra_index);
1391 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1392 cluster);
1393 ra_index += cluster;
1394 }
1395
1396 mutex_lock(&inode->i_mutex);
1397 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1398 BTRFS_I(inode)->force_compress = compress_type;
1399 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1400 if (ret < 0) {
1401 mutex_unlock(&inode->i_mutex);
1402 goto out_ra;
1403 }
1404
1405 defrag_count += ret;
1406 balance_dirty_pages_ratelimited(inode->i_mapping);
1407 mutex_unlock(&inode->i_mutex);
1408
1409 if (newer_than) {
1410 if (newer_off == (u64)-1)
1411 break;
1412
1413 if (ret > 0)
1414 i += ret;
1415
1416 newer_off = max(newer_off + 1,
1417 (u64)i << PAGE_CACHE_SHIFT);
1418
1419 ret = find_new_extents(root, inode, newer_than,
1420 &newer_off, SZ_64K);
1421 if (!ret) {
1422 range->start = newer_off;
1423 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1424 } else {
1425 break;
1426 }
1427 } else {
1428 if (ret > 0) {
1429 i += ret;
1430 last_len += ret << PAGE_CACHE_SHIFT;
1431 } else {
1432 i++;
1433 last_len = 0;
1434 }
1435 }
1436 }
1437
1438 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
1439 filemap_flush(inode->i_mapping);
1440 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1441 &BTRFS_I(inode)->runtime_flags))
1442 filemap_flush(inode->i_mapping);
1443 }
1444
1445 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1446 /* the filemap_flush will queue IO into the worker threads, but
1447 * we have to make sure the IO is actually started and that
1448 * ordered extents get created before we return
1449 */
1450 atomic_inc(&root->fs_info->async_submit_draining);
1451 while (atomic_read(&root->fs_info->nr_async_submits) ||
1452 atomic_read(&root->fs_info->async_delalloc_pages)) {
1453 wait_event(root->fs_info->async_submit_wait,
1454 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1455 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1456 }
1457 atomic_dec(&root->fs_info->async_submit_draining);
1458 }
1459
1460 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1461 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
1462 btrfs_sysfs_feature_update(root->fs_info,
1463 BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO, FEAT_INCOMPAT);
1464 }
1465
1466 ret = defrag_count;
1467
1468 out_ra:
1469 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1470 mutex_lock(&inode->i_mutex);
1471 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1472 mutex_unlock(&inode->i_mutex);
1473 }
1474 if (!file)
1475 kfree(ra);
1476 kfree(pages);
1477 return ret;
1478 }
1479
1480 static noinline int btrfs_ioctl_resize(struct file *file,
1481 void __user *arg)
1482 {
1483 u64 new_size;
1484 u64 old_size;
1485 u64 devid = 1;
1486 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
1487 struct btrfs_ioctl_vol_args *vol_args;
1488 struct btrfs_trans_handle *trans;
1489 struct btrfs_device *device = NULL;
1490 char *sizestr;
1491 char *retptr;
1492 char *devstr = NULL;
1493 int ret = 0;
1494 int mod = 0;
1495
1496 if (!capable(CAP_SYS_ADMIN))
1497 return -EPERM;
1498
1499 ret = mnt_want_write_file(file);
1500 if (ret)
1501 return ret;
1502
1503 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1504 1)) {
1505 mnt_drop_write_file(file);
1506 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1507 }
1508
1509 mutex_lock(&root->fs_info->volume_mutex);
1510 vol_args = memdup_user(arg, sizeof(*vol_args));
1511 if (IS_ERR(vol_args)) {
1512 ret = PTR_ERR(vol_args);
1513 goto out;
1514 }
1515
1516 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1517
1518 sizestr = vol_args->name;
1519 devstr = strchr(sizestr, ':');
1520 if (devstr) {
1521 sizestr = devstr + 1;
1522 *devstr = '\0';
1523 devstr = vol_args->name;
1524 ret = kstrtoull(devstr, 10, &devid);
1525 if (ret)
1526 goto out_free;
1527 if (!devid) {
1528 ret = -EINVAL;
1529 goto out_free;
1530 }
1531 btrfs_info(root->fs_info, "resizing devid %llu", devid);
1532 }
1533
1534 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1535 if (!device) {
1536 btrfs_info(root->fs_info, "resizer unable to find device %llu",
1537 devid);
1538 ret = -ENODEV;
1539 goto out_free;
1540 }
1541
1542 if (!device->writeable) {
1543 btrfs_info(root->fs_info,
1544 "resizer unable to apply on readonly device %llu",
1545 devid);
1546 ret = -EPERM;
1547 goto out_free;
1548 }
1549
1550 if (!strcmp(sizestr, "max"))
1551 new_size = device->bdev->bd_inode->i_size;
1552 else {
1553 if (sizestr[0] == '-') {
1554 mod = -1;
1555 sizestr++;
1556 } else if (sizestr[0] == '+') {
1557 mod = 1;
1558 sizestr++;
1559 }
1560 new_size = memparse(sizestr, &retptr);
1561 if (*retptr != '\0' || new_size == 0) {
1562 ret = -EINVAL;
1563 goto out_free;
1564 }
1565 }
1566
1567 if (device->is_tgtdev_for_dev_replace) {
1568 ret = -EPERM;
1569 goto out_free;
1570 }
1571
1572 old_size = btrfs_device_get_total_bytes(device);
1573
1574 if (mod < 0) {
1575 if (new_size > old_size) {
1576 ret = -EINVAL;
1577 goto out_free;
1578 }
1579 new_size = old_size - new_size;
1580 } else if (mod > 0) {
1581 if (new_size > ULLONG_MAX - old_size) {
1582 ret = -ERANGE;
1583 goto out_free;
1584 }
1585 new_size = old_size + new_size;
1586 }
1587
1588 if (new_size < SZ_256M) {
1589 ret = -EINVAL;
1590 goto out_free;
1591 }
1592 if (new_size > device->bdev->bd_inode->i_size) {
1593 ret = -EFBIG;
1594 goto out_free;
1595 }
1596
1597 new_size = div_u64(new_size, root->sectorsize);
1598 new_size *= root->sectorsize;
1599
1600 btrfs_info_in_rcu(root->fs_info, "new size for %s is %llu",
1601 rcu_str_deref(device->name), new_size);
1602
1603 if (new_size > old_size) {
1604 trans = btrfs_start_transaction(root, 0);
1605 if (IS_ERR(trans)) {
1606 ret = PTR_ERR(trans);
1607 goto out_free;
1608 }
1609 ret = btrfs_grow_device(trans, device, new_size);
1610 btrfs_commit_transaction(trans, root);
1611 } else if (new_size < old_size) {
1612 ret = btrfs_shrink_device(device, new_size);
1613 } /* equal, nothing need to do */
1614
1615 out_free:
1616 kfree(vol_args);
1617 out:
1618 mutex_unlock(&root->fs_info->volume_mutex);
1619 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
1620 mnt_drop_write_file(file);
1621 return ret;
1622 }
1623
1624 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1625 char *name, unsigned long fd, int subvol,
1626 u64 *transid, bool readonly,
1627 struct btrfs_qgroup_inherit *inherit)
1628 {
1629 int namelen;
1630 int ret = 0;
1631
1632 ret = mnt_want_write_file(file);
1633 if (ret)
1634 goto out;
1635
1636 namelen = strlen(name);
1637 if (strchr(name, '/')) {
1638 ret = -EINVAL;
1639 goto out_drop_write;
1640 }
1641
1642 if (name[0] == '.' &&
1643 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1644 ret = -EEXIST;
1645 goto out_drop_write;
1646 }
1647
1648 if (subvol) {
1649 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1650 NULL, transid, readonly, inherit);
1651 } else {
1652 struct fd src = fdget(fd);
1653 struct inode *src_inode;
1654 if (!src.file) {
1655 ret = -EINVAL;
1656 goto out_drop_write;
1657 }
1658
1659 src_inode = file_inode(src.file);
1660 if (src_inode->i_sb != file_inode(file)->i_sb) {
1661 btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1662 "Snapshot src from another FS");
1663 ret = -EXDEV;
1664 } else if (!inode_owner_or_capable(src_inode)) {
1665 /*
1666 * Subvolume creation is not restricted, but snapshots
1667 * are limited to own subvolumes only
1668 */
1669 ret = -EPERM;
1670 } else {
1671 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1672 BTRFS_I(src_inode)->root,
1673 transid, readonly, inherit);
1674 }
1675 fdput(src);
1676 }
1677 out_drop_write:
1678 mnt_drop_write_file(file);
1679 out:
1680 return ret;
1681 }
1682
1683 static noinline int btrfs_ioctl_snap_create(struct file *file,
1684 void __user *arg, int subvol)
1685 {
1686 struct btrfs_ioctl_vol_args *vol_args;
1687 int ret;
1688
1689 vol_args = memdup_user(arg, sizeof(*vol_args));
1690 if (IS_ERR(vol_args))
1691 return PTR_ERR(vol_args);
1692 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1693
1694 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1695 vol_args->fd, subvol,
1696 NULL, false, NULL);
1697
1698 kfree(vol_args);
1699 return ret;
1700 }
1701
1702 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1703 void __user *arg, int subvol)
1704 {
1705 struct btrfs_ioctl_vol_args_v2 *vol_args;
1706 int ret;
1707 u64 transid = 0;
1708 u64 *ptr = NULL;
1709 bool readonly = false;
1710 struct btrfs_qgroup_inherit *inherit = NULL;
1711
1712 vol_args = memdup_user(arg, sizeof(*vol_args));
1713 if (IS_ERR(vol_args))
1714 return PTR_ERR(vol_args);
1715 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1716
1717 if (vol_args->flags &
1718 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1719 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1720 ret = -EOPNOTSUPP;
1721 goto free_args;
1722 }
1723
1724 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1725 ptr = &transid;
1726 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1727 readonly = true;
1728 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1729 if (vol_args->size > PAGE_CACHE_SIZE) {
1730 ret = -EINVAL;
1731 goto free_args;
1732 }
1733 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1734 if (IS_ERR(inherit)) {
1735 ret = PTR_ERR(inherit);
1736 goto free_args;
1737 }
1738 }
1739
1740 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1741 vol_args->fd, subvol, ptr,
1742 readonly, inherit);
1743 if (ret)
1744 goto free_inherit;
1745
1746 if (ptr && copy_to_user(arg +
1747 offsetof(struct btrfs_ioctl_vol_args_v2,
1748 transid),
1749 ptr, sizeof(*ptr)))
1750 ret = -EFAULT;
1751
1752 free_inherit:
1753 kfree(inherit);
1754 free_args:
1755 kfree(vol_args);
1756 return ret;
1757 }
1758
1759 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1760 void __user *arg)
1761 {
1762 struct inode *inode = file_inode(file);
1763 struct btrfs_root *root = BTRFS_I(inode)->root;
1764 int ret = 0;
1765 u64 flags = 0;
1766
1767 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1768 return -EINVAL;
1769
1770 down_read(&root->fs_info->subvol_sem);
1771 if (btrfs_root_readonly(root))
1772 flags |= BTRFS_SUBVOL_RDONLY;
1773 up_read(&root->fs_info->subvol_sem);
1774
1775 if (copy_to_user(arg, &flags, sizeof(flags)))
1776 ret = -EFAULT;
1777
1778 return ret;
1779 }
1780
1781 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1782 void __user *arg)
1783 {
1784 struct inode *inode = file_inode(file);
1785 struct btrfs_root *root = BTRFS_I(inode)->root;
1786 struct btrfs_trans_handle *trans;
1787 u64 root_flags;
1788 u64 flags;
1789 int ret = 0;
1790
1791 if (!inode_owner_or_capable(inode))
1792 return -EPERM;
1793
1794 ret = mnt_want_write_file(file);
1795 if (ret)
1796 goto out;
1797
1798 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1799 ret = -EINVAL;
1800 goto out_drop_write;
1801 }
1802
1803 if (copy_from_user(&flags, arg, sizeof(flags))) {
1804 ret = -EFAULT;
1805 goto out_drop_write;
1806 }
1807
1808 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1809 ret = -EINVAL;
1810 goto out_drop_write;
1811 }
1812
1813 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1814 ret = -EOPNOTSUPP;
1815 goto out_drop_write;
1816 }
1817
1818 down_write(&root->fs_info->subvol_sem);
1819
1820 /* nothing to do */
1821 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1822 goto out_drop_sem;
1823
1824 root_flags = btrfs_root_flags(&root->root_item);
1825 if (flags & BTRFS_SUBVOL_RDONLY) {
1826 btrfs_set_root_flags(&root->root_item,
1827 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1828 } else {
1829 /*
1830 * Block RO -> RW transition if this subvolume is involved in
1831 * send
1832 */
1833 spin_lock(&root->root_item_lock);
1834 if (root->send_in_progress == 0) {
1835 btrfs_set_root_flags(&root->root_item,
1836 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1837 spin_unlock(&root->root_item_lock);
1838 } else {
1839 spin_unlock(&root->root_item_lock);
1840 btrfs_warn(root->fs_info,
1841 "Attempt to set subvolume %llu read-write during send",
1842 root->root_key.objectid);
1843 ret = -EPERM;
1844 goto out_drop_sem;
1845 }
1846 }
1847
1848 trans = btrfs_start_transaction(root, 1);
1849 if (IS_ERR(trans)) {
1850 ret = PTR_ERR(trans);
1851 goto out_reset;
1852 }
1853
1854 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1855 &root->root_key, &root->root_item);
1856
1857 btrfs_commit_transaction(trans, root);
1858 out_reset:
1859 if (ret)
1860 btrfs_set_root_flags(&root->root_item, root_flags);
1861 out_drop_sem:
1862 up_write(&root->fs_info->subvol_sem);
1863 out_drop_write:
1864 mnt_drop_write_file(file);
1865 out:
1866 return ret;
1867 }
1868
1869 /*
1870 * helper to check if the subvolume references other subvolumes
1871 */
1872 static noinline int may_destroy_subvol(struct btrfs_root *root)
1873 {
1874 struct btrfs_path *path;
1875 struct btrfs_dir_item *di;
1876 struct btrfs_key key;
1877 u64 dir_id;
1878 int ret;
1879
1880 path = btrfs_alloc_path();
1881 if (!path)
1882 return -ENOMEM;
1883
1884 /* Make sure this root isn't set as the default subvol */
1885 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1886 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1887 dir_id, "default", 7, 0);
1888 if (di && !IS_ERR(di)) {
1889 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1890 if (key.objectid == root->root_key.objectid) {
1891 ret = -EPERM;
1892 btrfs_err(root->fs_info, "deleting default subvolume "
1893 "%llu is not allowed", key.objectid);
1894 goto out;
1895 }
1896 btrfs_release_path(path);
1897 }
1898
1899 key.objectid = root->root_key.objectid;
1900 key.type = BTRFS_ROOT_REF_KEY;
1901 key.offset = (u64)-1;
1902
1903 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1904 &key, path, 0, 0);
1905 if (ret < 0)
1906 goto out;
1907 BUG_ON(ret == 0);
1908
1909 ret = 0;
1910 if (path->slots[0] > 0) {
1911 path->slots[0]--;
1912 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1913 if (key.objectid == root->root_key.objectid &&
1914 key.type == BTRFS_ROOT_REF_KEY)
1915 ret = -ENOTEMPTY;
1916 }
1917 out:
1918 btrfs_free_path(path);
1919 return ret;
1920 }
1921
1922 static noinline int key_in_sk(struct btrfs_key *key,
1923 struct btrfs_ioctl_search_key *sk)
1924 {
1925 struct btrfs_key test;
1926 int ret;
1927
1928 test.objectid = sk->min_objectid;
1929 test.type = sk->min_type;
1930 test.offset = sk->min_offset;
1931
1932 ret = btrfs_comp_cpu_keys(key, &test);
1933 if (ret < 0)
1934 return 0;
1935
1936 test.objectid = sk->max_objectid;
1937 test.type = sk->max_type;
1938 test.offset = sk->max_offset;
1939
1940 ret = btrfs_comp_cpu_keys(key, &test);
1941 if (ret > 0)
1942 return 0;
1943 return 1;
1944 }
1945
1946 static noinline int copy_to_sk(struct btrfs_root *root,
1947 struct btrfs_path *path,
1948 struct btrfs_key *key,
1949 struct btrfs_ioctl_search_key *sk,
1950 size_t *buf_size,
1951 char __user *ubuf,
1952 unsigned long *sk_offset,
1953 int *num_found)
1954 {
1955 u64 found_transid;
1956 struct extent_buffer *leaf;
1957 struct btrfs_ioctl_search_header sh;
1958 struct btrfs_key test;
1959 unsigned long item_off;
1960 unsigned long item_len;
1961 int nritems;
1962 int i;
1963 int slot;
1964 int ret = 0;
1965
1966 leaf = path->nodes[0];
1967 slot = path->slots[0];
1968 nritems = btrfs_header_nritems(leaf);
1969
1970 if (btrfs_header_generation(leaf) > sk->max_transid) {
1971 i = nritems;
1972 goto advance_key;
1973 }
1974 found_transid = btrfs_header_generation(leaf);
1975
1976 for (i = slot; i < nritems; i++) {
1977 item_off = btrfs_item_ptr_offset(leaf, i);
1978 item_len = btrfs_item_size_nr(leaf, i);
1979
1980 btrfs_item_key_to_cpu(leaf, key, i);
1981 if (!key_in_sk(key, sk))
1982 continue;
1983
1984 if (sizeof(sh) + item_len > *buf_size) {
1985 if (*num_found) {
1986 ret = 1;
1987 goto out;
1988 }
1989
1990 /*
1991 * return one empty item back for v1, which does not
1992 * handle -EOVERFLOW
1993 */
1994
1995 *buf_size = sizeof(sh) + item_len;
1996 item_len = 0;
1997 ret = -EOVERFLOW;
1998 }
1999
2000 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2001 ret = 1;
2002 goto out;
2003 }
2004
2005 sh.objectid = key->objectid;
2006 sh.offset = key->offset;
2007 sh.type = key->type;
2008 sh.len = item_len;
2009 sh.transid = found_transid;
2010
2011 /* copy search result header */
2012 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2013 ret = -EFAULT;
2014 goto out;
2015 }
2016
2017 *sk_offset += sizeof(sh);
2018
2019 if (item_len) {
2020 char __user *up = ubuf + *sk_offset;
2021 /* copy the item */
2022 if (read_extent_buffer_to_user(leaf, up,
2023 item_off, item_len)) {
2024 ret = -EFAULT;
2025 goto out;
2026 }
2027
2028 *sk_offset += item_len;
2029 }
2030 (*num_found)++;
2031
2032 if (ret) /* -EOVERFLOW from above */
2033 goto out;
2034
2035 if (*num_found >= sk->nr_items) {
2036 ret = 1;
2037 goto out;
2038 }
2039 }
2040 advance_key:
2041 ret = 0;
2042 test.objectid = sk->max_objectid;
2043 test.type = sk->max_type;
2044 test.offset = sk->max_offset;
2045 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2046 ret = 1;
2047 else if (key->offset < (u64)-1)
2048 key->offset++;
2049 else if (key->type < (u8)-1) {
2050 key->offset = 0;
2051 key->type++;
2052 } else if (key->objectid < (u64)-1) {
2053 key->offset = 0;
2054 key->type = 0;
2055 key->objectid++;
2056 } else
2057 ret = 1;
2058 out:
2059 /*
2060 * 0: all items from this leaf copied, continue with next
2061 * 1: * more items can be copied, but unused buffer is too small
2062 * * all items were found
2063 * Either way, it will stops the loop which iterates to the next
2064 * leaf
2065 * -EOVERFLOW: item was to large for buffer
2066 * -EFAULT: could not copy extent buffer back to userspace
2067 */
2068 return ret;
2069 }
2070
2071 static noinline int search_ioctl(struct inode *inode,
2072 struct btrfs_ioctl_search_key *sk,
2073 size_t *buf_size,
2074 char __user *ubuf)
2075 {
2076 struct btrfs_root *root;
2077 struct btrfs_key key;
2078 struct btrfs_path *path;
2079 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
2080 int ret;
2081 int num_found = 0;
2082 unsigned long sk_offset = 0;
2083
2084 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2085 *buf_size = sizeof(struct btrfs_ioctl_search_header);
2086 return -EOVERFLOW;
2087 }
2088
2089 path = btrfs_alloc_path();
2090 if (!path)
2091 return -ENOMEM;
2092
2093 if (sk->tree_id == 0) {
2094 /* search the root of the inode that was passed */
2095 root = BTRFS_I(inode)->root;
2096 } else {
2097 key.objectid = sk->tree_id;
2098 key.type = BTRFS_ROOT_ITEM_KEY;
2099 key.offset = (u64)-1;
2100 root = btrfs_read_fs_root_no_name(info, &key);
2101 if (IS_ERR(root)) {
2102 btrfs_err(info, "could not find root %llu",
2103 sk->tree_id);
2104 btrfs_free_path(path);
2105 return -ENOENT;
2106 }
2107 }
2108
2109 key.objectid = sk->min_objectid;
2110 key.type = sk->min_type;
2111 key.offset = sk->min_offset;
2112
2113 while (1) {
2114 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2115 if (ret != 0) {
2116 if (ret > 0)
2117 ret = 0;
2118 goto err;
2119 }
2120 ret = copy_to_sk(root, path, &key, sk, buf_size, ubuf,
2121 &sk_offset, &num_found);
2122 btrfs_release_path(path);
2123 if (ret)
2124 break;
2125
2126 }
2127 if (ret > 0)
2128 ret = 0;
2129 err:
2130 sk->nr_items = num_found;
2131 btrfs_free_path(path);
2132 return ret;
2133 }
2134
2135 static noinline int btrfs_ioctl_tree_search(struct file *file,
2136 void __user *argp)
2137 {
2138 struct btrfs_ioctl_search_args __user *uargs;
2139 struct btrfs_ioctl_search_key sk;
2140 struct inode *inode;
2141 int ret;
2142 size_t buf_size;
2143
2144 if (!capable(CAP_SYS_ADMIN))
2145 return -EPERM;
2146
2147 uargs = (struct btrfs_ioctl_search_args __user *)argp;
2148
2149 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2150 return -EFAULT;
2151
2152 buf_size = sizeof(uargs->buf);
2153
2154 inode = file_inode(file);
2155 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
2156
2157 /*
2158 * In the origin implementation an overflow is handled by returning a
2159 * search header with a len of zero, so reset ret.
2160 */
2161 if (ret == -EOVERFLOW)
2162 ret = 0;
2163
2164 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2165 ret = -EFAULT;
2166 return ret;
2167 }
2168
2169 static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2170 void __user *argp)
2171 {
2172 struct btrfs_ioctl_search_args_v2 __user *uarg;
2173 struct btrfs_ioctl_search_args_v2 args;
2174 struct inode *inode;
2175 int ret;
2176 size_t buf_size;
2177 const size_t buf_limit = SZ_16M;
2178
2179 if (!capable(CAP_SYS_ADMIN))
2180 return -EPERM;
2181
2182 /* copy search header and buffer size */
2183 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2184 if (copy_from_user(&args, uarg, sizeof(args)))
2185 return -EFAULT;
2186
2187 buf_size = args.buf_size;
2188
2189 if (buf_size < sizeof(struct btrfs_ioctl_search_header))
2190 return -EOVERFLOW;
2191
2192 /* limit result size to 16MB */
2193 if (buf_size > buf_limit)
2194 buf_size = buf_limit;
2195
2196 inode = file_inode(file);
2197 ret = search_ioctl(inode, &args.key, &buf_size,
2198 (char *)(&uarg->buf[0]));
2199 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2200 ret = -EFAULT;
2201 else if (ret == -EOVERFLOW &&
2202 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2203 ret = -EFAULT;
2204
2205 return ret;
2206 }
2207
2208 /*
2209 * Search INODE_REFs to identify path name of 'dirid' directory
2210 * in a 'tree_id' tree. and sets path name to 'name'.
2211 */
2212 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2213 u64 tree_id, u64 dirid, char *name)
2214 {
2215 struct btrfs_root *root;
2216 struct btrfs_key key;
2217 char *ptr;
2218 int ret = -1;
2219 int slot;
2220 int len;
2221 int total_len = 0;
2222 struct btrfs_inode_ref *iref;
2223 struct extent_buffer *l;
2224 struct btrfs_path *path;
2225
2226 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2227 name[0]='\0';
2228 return 0;
2229 }
2230
2231 path = btrfs_alloc_path();
2232 if (!path)
2233 return -ENOMEM;
2234
2235 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
2236
2237 key.objectid = tree_id;
2238 key.type = BTRFS_ROOT_ITEM_KEY;
2239 key.offset = (u64)-1;
2240 root = btrfs_read_fs_root_no_name(info, &key);
2241 if (IS_ERR(root)) {
2242 btrfs_err(info, "could not find root %llu", tree_id);
2243 ret = -ENOENT;
2244 goto out;
2245 }
2246
2247 key.objectid = dirid;
2248 key.type = BTRFS_INODE_REF_KEY;
2249 key.offset = (u64)-1;
2250
2251 while (1) {
2252 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2253 if (ret < 0)
2254 goto out;
2255 else if (ret > 0) {
2256 ret = btrfs_previous_item(root, path, dirid,
2257 BTRFS_INODE_REF_KEY);
2258 if (ret < 0)
2259 goto out;
2260 else if (ret > 0) {
2261 ret = -ENOENT;
2262 goto out;
2263 }
2264 }
2265
2266 l = path->nodes[0];
2267 slot = path->slots[0];
2268 btrfs_item_key_to_cpu(l, &key, slot);
2269
2270 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2271 len = btrfs_inode_ref_name_len(l, iref);
2272 ptr -= len + 1;
2273 total_len += len + 1;
2274 if (ptr < name) {
2275 ret = -ENAMETOOLONG;
2276 goto out;
2277 }
2278
2279 *(ptr + len) = '/';
2280 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2281
2282 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2283 break;
2284
2285 btrfs_release_path(path);
2286 key.objectid = key.offset;
2287 key.offset = (u64)-1;
2288 dirid = key.objectid;
2289 }
2290 memmove(name, ptr, total_len);
2291 name[total_len] = '\0';
2292 ret = 0;
2293 out:
2294 btrfs_free_path(path);
2295 return ret;
2296 }
2297
2298 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2299 void __user *argp)
2300 {
2301 struct btrfs_ioctl_ino_lookup_args *args;
2302 struct inode *inode;
2303 int ret = 0;
2304
2305 args = memdup_user(argp, sizeof(*args));
2306 if (IS_ERR(args))
2307 return PTR_ERR(args);
2308
2309 inode = file_inode(file);
2310
2311 /*
2312 * Unprivileged query to obtain the containing subvolume root id. The
2313 * path is reset so it's consistent with btrfs_search_path_in_tree.
2314 */
2315 if (args->treeid == 0)
2316 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2317
2318 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2319 args->name[0] = 0;
2320 goto out;
2321 }
2322
2323 if (!capable(CAP_SYS_ADMIN)) {
2324 ret = -EPERM;
2325 goto out;
2326 }
2327
2328 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2329 args->treeid, args->objectid,
2330 args->name);
2331
2332 out:
2333 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2334 ret = -EFAULT;
2335
2336 kfree(args);
2337 return ret;
2338 }
2339
2340 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2341 void __user *arg)
2342 {
2343 struct dentry *parent = file->f_path.dentry;
2344 struct dentry *dentry;
2345 struct inode *dir = d_inode(parent);
2346 struct inode *inode;
2347 struct btrfs_root *root = BTRFS_I(dir)->root;
2348 struct btrfs_root *dest = NULL;
2349 struct btrfs_ioctl_vol_args *vol_args;
2350 struct btrfs_trans_handle *trans;
2351 struct btrfs_block_rsv block_rsv;
2352 u64 root_flags;
2353 u64 qgroup_reserved;
2354 int namelen;
2355 int ret;
2356 int err = 0;
2357
2358 vol_args = memdup_user(arg, sizeof(*vol_args));
2359 if (IS_ERR(vol_args))
2360 return PTR_ERR(vol_args);
2361
2362 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2363 namelen = strlen(vol_args->name);
2364 if (strchr(vol_args->name, '/') ||
2365 strncmp(vol_args->name, "..", namelen) == 0) {
2366 err = -EINVAL;
2367 goto out;
2368 }
2369
2370 err = mnt_want_write_file(file);
2371 if (err)
2372 goto out;
2373
2374
2375 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2376 if (err == -EINTR)
2377 goto out_drop_write;
2378 dentry = lookup_one_len(vol_args->name, parent, namelen);
2379 if (IS_ERR(dentry)) {
2380 err = PTR_ERR(dentry);
2381 goto out_unlock_dir;
2382 }
2383
2384 if (d_really_is_negative(dentry)) {
2385 err = -ENOENT;
2386 goto out_dput;
2387 }
2388
2389 inode = d_inode(dentry);
2390 dest = BTRFS_I(inode)->root;
2391 if (!capable(CAP_SYS_ADMIN)) {
2392 /*
2393 * Regular user. Only allow this with a special mount
2394 * option, when the user has write+exec access to the
2395 * subvol root, and when rmdir(2) would have been
2396 * allowed.
2397 *
2398 * Note that this is _not_ check that the subvol is
2399 * empty or doesn't contain data that we wouldn't
2400 * otherwise be able to delete.
2401 *
2402 * Users who want to delete empty subvols should try
2403 * rmdir(2).
2404 */
2405 err = -EPERM;
2406 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2407 goto out_dput;
2408
2409 /*
2410 * Do not allow deletion if the parent dir is the same
2411 * as the dir to be deleted. That means the ioctl
2412 * must be called on the dentry referencing the root
2413 * of the subvol, not a random directory contained
2414 * within it.
2415 */
2416 err = -EINVAL;
2417 if (root == dest)
2418 goto out_dput;
2419
2420 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2421 if (err)
2422 goto out_dput;
2423 }
2424
2425 /* check if subvolume may be deleted by a user */
2426 err = btrfs_may_delete(dir, dentry, 1);
2427 if (err)
2428 goto out_dput;
2429
2430 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
2431 err = -EINVAL;
2432 goto out_dput;
2433 }
2434
2435 mutex_lock(&inode->i_mutex);
2436
2437 /*
2438 * Don't allow to delete a subvolume with send in progress. This is
2439 * inside the i_mutex so the error handling that has to drop the bit
2440 * again is not run concurrently.
2441 */
2442 spin_lock(&dest->root_item_lock);
2443 root_flags = btrfs_root_flags(&dest->root_item);
2444 if (dest->send_in_progress == 0) {
2445 btrfs_set_root_flags(&dest->root_item,
2446 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
2447 spin_unlock(&dest->root_item_lock);
2448 } else {
2449 spin_unlock(&dest->root_item_lock);
2450 btrfs_warn(root->fs_info,
2451 "Attempt to delete subvolume %llu during send",
2452 dest->root_key.objectid);
2453 err = -EPERM;
2454 goto out_unlock_inode;
2455 }
2456
2457 down_write(&root->fs_info->subvol_sem);
2458
2459 err = may_destroy_subvol(dest);
2460 if (err)
2461 goto out_up_write;
2462
2463 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2464 /*
2465 * One for dir inode, two for dir entries, two for root
2466 * ref/backref.
2467 */
2468 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
2469 5, &qgroup_reserved, true);
2470 if (err)
2471 goto out_up_write;
2472
2473 trans = btrfs_start_transaction(root, 0);
2474 if (IS_ERR(trans)) {
2475 err = PTR_ERR(trans);
2476 goto out_release;
2477 }
2478 trans->block_rsv = &block_rsv;
2479 trans->bytes_reserved = block_rsv.size;
2480
2481 ret = btrfs_unlink_subvol(trans, root, dir,
2482 dest->root_key.objectid,
2483 dentry->d_name.name,
2484 dentry->d_name.len);
2485 if (ret) {
2486 err = ret;
2487 btrfs_abort_transaction(trans, root, ret);
2488 goto out_end_trans;
2489 }
2490
2491 btrfs_record_root_in_trans(trans, dest);
2492
2493 memset(&dest->root_item.drop_progress, 0,
2494 sizeof(dest->root_item.drop_progress));
2495 dest->root_item.drop_level = 0;
2496 btrfs_set_root_refs(&dest->root_item, 0);
2497
2498 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
2499 ret = btrfs_insert_orphan_item(trans,
2500 root->fs_info->tree_root,
2501 dest->root_key.objectid);
2502 if (ret) {
2503 btrfs_abort_transaction(trans, root, ret);
2504 err = ret;
2505 goto out_end_trans;
2506 }
2507 }
2508
2509 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2510 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2511 dest->root_key.objectid);
2512 if (ret && ret != -ENOENT) {
2513 btrfs_abort_transaction(trans, root, ret);
2514 err = ret;
2515 goto out_end_trans;
2516 }
2517 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2518 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2519 dest->root_item.received_uuid,
2520 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2521 dest->root_key.objectid);
2522 if (ret && ret != -ENOENT) {
2523 btrfs_abort_transaction(trans, root, ret);
2524 err = ret;
2525 goto out_end_trans;
2526 }
2527 }
2528
2529 out_end_trans:
2530 trans->block_rsv = NULL;
2531 trans->bytes_reserved = 0;
2532 ret = btrfs_end_transaction(trans, root);
2533 if (ret && !err)
2534 err = ret;
2535 inode->i_flags |= S_DEAD;
2536 out_release:
2537 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
2538 out_up_write:
2539 up_write(&root->fs_info->subvol_sem);
2540 if (err) {
2541 spin_lock(&dest->root_item_lock);
2542 root_flags = btrfs_root_flags(&dest->root_item);
2543 btrfs_set_root_flags(&dest->root_item,
2544 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
2545 spin_unlock(&dest->root_item_lock);
2546 }
2547 out_unlock_inode:
2548 mutex_unlock(&inode->i_mutex);
2549 if (!err) {
2550 d_invalidate(dentry);
2551 btrfs_invalidate_inodes(dest);
2552 d_delete(dentry);
2553 ASSERT(dest->send_in_progress == 0);
2554
2555 /* the last ref */
2556 if (dest->ino_cache_inode) {
2557 iput(dest->ino_cache_inode);
2558 dest->ino_cache_inode = NULL;
2559 }
2560 }
2561 out_dput:
2562 dput(dentry);
2563 out_unlock_dir:
2564 mutex_unlock(&dir->i_mutex);
2565 out_drop_write:
2566 mnt_drop_write_file(file);
2567 out:
2568 kfree(vol_args);
2569 return err;
2570 }
2571
2572 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2573 {
2574 struct inode *inode = file_inode(file);
2575 struct btrfs_root *root = BTRFS_I(inode)->root;
2576 struct btrfs_ioctl_defrag_range_args *range;
2577 int ret;
2578
2579 ret = mnt_want_write_file(file);
2580 if (ret)
2581 return ret;
2582
2583 if (btrfs_root_readonly(root)) {
2584 ret = -EROFS;
2585 goto out;
2586 }
2587
2588 switch (inode->i_mode & S_IFMT) {
2589 case S_IFDIR:
2590 if (!capable(CAP_SYS_ADMIN)) {
2591 ret = -EPERM;
2592 goto out;
2593 }
2594 ret = btrfs_defrag_root(root);
2595 if (ret)
2596 goto out;
2597 ret = btrfs_defrag_root(root->fs_info->extent_root);
2598 break;
2599 case S_IFREG:
2600 if (!(file->f_mode & FMODE_WRITE)) {
2601 ret = -EINVAL;
2602 goto out;
2603 }
2604
2605 range = kzalloc(sizeof(*range), GFP_KERNEL);
2606 if (!range) {
2607 ret = -ENOMEM;
2608 goto out;
2609 }
2610
2611 if (argp) {
2612 if (copy_from_user(range, argp,
2613 sizeof(*range))) {
2614 ret = -EFAULT;
2615 kfree(range);
2616 goto out;
2617 }
2618 /* compression requires us to start the IO */
2619 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2620 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2621 range->extent_thresh = (u32)-1;
2622 }
2623 } else {
2624 /* the rest are all set to zero by kzalloc */
2625 range->len = (u64)-1;
2626 }
2627 ret = btrfs_defrag_file(file_inode(file), file,
2628 range, 0, 0);
2629 if (ret > 0)
2630 ret = 0;
2631 kfree(range);
2632 break;
2633 default:
2634 ret = -EINVAL;
2635 }
2636 out:
2637 mnt_drop_write_file(file);
2638 return ret;
2639 }
2640
2641 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2642 {
2643 struct btrfs_ioctl_vol_args *vol_args;
2644 int ret;
2645
2646 if (!capable(CAP_SYS_ADMIN))
2647 return -EPERM;
2648
2649 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2650 1)) {
2651 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2652 }
2653
2654 mutex_lock(&root->fs_info->volume_mutex);
2655 vol_args = memdup_user(arg, sizeof(*vol_args));
2656 if (IS_ERR(vol_args)) {
2657 ret = PTR_ERR(vol_args);
2658 goto out;
2659 }
2660
2661 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2662 ret = btrfs_init_new_device(root, vol_args->name);
2663
2664 if (!ret)
2665 btrfs_info(root->fs_info, "disk added %s",vol_args->name);
2666
2667 kfree(vol_args);
2668 out:
2669 mutex_unlock(&root->fs_info->volume_mutex);
2670 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2671 return ret;
2672 }
2673
2674 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2675 {
2676 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2677 struct btrfs_ioctl_vol_args *vol_args;
2678 int ret;
2679
2680 if (!capable(CAP_SYS_ADMIN))
2681 return -EPERM;
2682
2683 ret = mnt_want_write_file(file);
2684 if (ret)
2685 return ret;
2686
2687 vol_args = memdup_user(arg, sizeof(*vol_args));
2688 if (IS_ERR(vol_args)) {
2689 ret = PTR_ERR(vol_args);
2690 goto err_drop;
2691 }
2692
2693 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2694
2695 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2696 1)) {
2697 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2698 goto out;
2699 }
2700
2701 mutex_lock(&root->fs_info->volume_mutex);
2702 ret = btrfs_rm_device(root, vol_args->name);
2703 mutex_unlock(&root->fs_info->volume_mutex);
2704 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2705
2706 if (!ret)
2707 btrfs_info(root->fs_info, "disk deleted %s",vol_args->name);
2708
2709 out:
2710 kfree(vol_args);
2711 err_drop:
2712 mnt_drop_write_file(file);
2713 return ret;
2714 }
2715
2716 static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2717 {
2718 struct btrfs_ioctl_fs_info_args *fi_args;
2719 struct btrfs_device *device;
2720 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2721 int ret = 0;
2722
2723 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2724 if (!fi_args)
2725 return -ENOMEM;
2726
2727 mutex_lock(&fs_devices->device_list_mutex);
2728 fi_args->num_devices = fs_devices->num_devices;
2729 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
2730
2731 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2732 if (device->devid > fi_args->max_id)
2733 fi_args->max_id = device->devid;
2734 }
2735 mutex_unlock(&fs_devices->device_list_mutex);
2736
2737 fi_args->nodesize = root->fs_info->super_copy->nodesize;
2738 fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
2739 fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
2740
2741 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2742 ret = -EFAULT;
2743
2744 kfree(fi_args);
2745 return ret;
2746 }
2747
2748 static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2749 {
2750 struct btrfs_ioctl_dev_info_args *di_args;
2751 struct btrfs_device *dev;
2752 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2753 int ret = 0;
2754 char *s_uuid = NULL;
2755
2756 di_args = memdup_user(arg, sizeof(*di_args));
2757 if (IS_ERR(di_args))
2758 return PTR_ERR(di_args);
2759
2760 if (!btrfs_is_empty_uuid(di_args->uuid))
2761 s_uuid = di_args->uuid;
2762
2763 mutex_lock(&fs_devices->device_list_mutex);
2764 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
2765
2766 if (!dev) {
2767 ret = -ENODEV;
2768 goto out;
2769 }
2770
2771 di_args->devid = dev->devid;
2772 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2773 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
2774 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2775 if (dev->name) {
2776 struct rcu_string *name;
2777
2778 rcu_read_lock();
2779 name = rcu_dereference(dev->name);
2780 strncpy(di_args->path, name->str, sizeof(di_args->path));
2781 rcu_read_unlock();
2782 di_args->path[sizeof(di_args->path) - 1] = 0;
2783 } else {
2784 di_args->path[0] = '\0';
2785 }
2786
2787 out:
2788 mutex_unlock(&fs_devices->device_list_mutex);
2789 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2790 ret = -EFAULT;
2791
2792 kfree(di_args);
2793 return ret;
2794 }
2795
2796 static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
2797 {
2798 struct page *page;
2799 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2800
2801 page = grab_cache_page(inode->i_mapping, index);
2802 if (!page)
2803 return NULL;
2804
2805 if (!PageUptodate(page)) {
2806 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2807 0))
2808 return NULL;
2809 lock_page(page);
2810 if (!PageUptodate(page)) {
2811 unlock_page(page);
2812 page_cache_release(page);
2813 return NULL;
2814 }
2815 }
2816 unlock_page(page);
2817
2818 return page;
2819 }
2820
2821 static int gather_extent_pages(struct inode *inode, struct page **pages,
2822 int num_pages, u64 off)
2823 {
2824 int i;
2825 pgoff_t index = off >> PAGE_CACHE_SHIFT;
2826
2827 for (i = 0; i < num_pages; i++) {
2828 pages[i] = extent_same_get_page(inode, index + i);
2829 if (!pages[i])
2830 return -ENOMEM;
2831 }
2832 return 0;
2833 }
2834
2835 static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2836 {
2837 /* do any pending delalloc/csum calc on src, one way or
2838 another, and lock file content */
2839 while (1) {
2840 struct btrfs_ordered_extent *ordered;
2841 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2842 ordered = btrfs_lookup_first_ordered_extent(inode,
2843 off + len - 1);
2844 if ((!ordered ||
2845 ordered->file_offset + ordered->len <= off ||
2846 ordered->file_offset >= off + len) &&
2847 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2848 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2849 if (ordered)
2850 btrfs_put_ordered_extent(ordered);
2851 break;
2852 }
2853 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2854 if (ordered)
2855 btrfs_put_ordered_extent(ordered);
2856 btrfs_wait_ordered_range(inode, off, len);
2857 }
2858 }
2859
2860 static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
2861 {
2862 mutex_unlock(&inode1->i_mutex);
2863 mutex_unlock(&inode2->i_mutex);
2864 }
2865
2866 static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2867 {
2868 if (inode1 < inode2)
2869 swap(inode1, inode2);
2870
2871 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2872 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2873 }
2874
2875 static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2876 struct inode *inode2, u64 loff2, u64 len)
2877 {
2878 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2879 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2880 }
2881
2882 static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2883 struct inode *inode2, u64 loff2, u64 len)
2884 {
2885 if (inode1 < inode2) {
2886 swap(inode1, inode2);
2887 swap(loff1, loff2);
2888 }
2889 lock_extent_range(inode1, loff1, len);
2890 lock_extent_range(inode2, loff2, len);
2891 }
2892
2893 struct cmp_pages {
2894 int num_pages;
2895 struct page **src_pages;
2896 struct page **dst_pages;
2897 };
2898
2899 static void btrfs_cmp_data_free(struct cmp_pages *cmp)
2900 {
2901 int i;
2902 struct page *pg;
2903
2904 for (i = 0; i < cmp->num_pages; i++) {
2905 pg = cmp->src_pages[i];
2906 if (pg)
2907 page_cache_release(pg);
2908 pg = cmp->dst_pages[i];
2909 if (pg)
2910 page_cache_release(pg);
2911 }
2912 kfree(cmp->src_pages);
2913 kfree(cmp->dst_pages);
2914 }
2915
2916 static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2917 struct inode *dst, u64 dst_loff,
2918 u64 len, struct cmp_pages *cmp)
2919 {
2920 int ret;
2921 int num_pages = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
2922 struct page **src_pgarr, **dst_pgarr;
2923
2924 /*
2925 * We must gather up all the pages before we initiate our
2926 * extent locking. We use an array for the page pointers. Size
2927 * of the array is bounded by len, which is in turn bounded by
2928 * BTRFS_MAX_DEDUPE_LEN.
2929 */
2930 src_pgarr = kzalloc(num_pages * sizeof(struct page *), GFP_NOFS);
2931 dst_pgarr = kzalloc(num_pages * sizeof(struct page *), GFP_NOFS);
2932 if (!src_pgarr || !dst_pgarr) {
2933 kfree(src_pgarr);
2934 kfree(dst_pgarr);
2935 return -ENOMEM;
2936 }
2937 cmp->num_pages = num_pages;
2938 cmp->src_pages = src_pgarr;
2939 cmp->dst_pages = dst_pgarr;
2940
2941 ret = gather_extent_pages(src, cmp->src_pages, cmp->num_pages, loff);
2942 if (ret)
2943 goto out;
2944
2945 ret = gather_extent_pages(dst, cmp->dst_pages, cmp->num_pages, dst_loff);
2946
2947 out:
2948 if (ret)
2949 btrfs_cmp_data_free(cmp);
2950 return 0;
2951 }
2952
2953 static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2954 u64 dst_loff, u64 len, struct cmp_pages *cmp)
2955 {
2956 int ret = 0;
2957 int i;
2958 struct page *src_page, *dst_page;
2959 unsigned int cmp_len = PAGE_CACHE_SIZE;
2960 void *addr, *dst_addr;
2961
2962 i = 0;
2963 while (len) {
2964 if (len < PAGE_CACHE_SIZE)
2965 cmp_len = len;
2966
2967 BUG_ON(i >= cmp->num_pages);
2968
2969 src_page = cmp->src_pages[i];
2970 dst_page = cmp->dst_pages[i];
2971
2972 addr = kmap_atomic(src_page);
2973 dst_addr = kmap_atomic(dst_page);
2974
2975 flush_dcache_page(src_page);
2976 flush_dcache_page(dst_page);
2977
2978 if (memcmp(addr, dst_addr, cmp_len))
2979 ret = BTRFS_SAME_DATA_DIFFERS;
2980
2981 kunmap_atomic(addr);
2982 kunmap_atomic(dst_addr);
2983
2984 if (ret)
2985 break;
2986
2987 len -= cmp_len;
2988 i++;
2989 }
2990
2991 return ret;
2992 }
2993
2994 static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
2995 u64 olen)
2996 {
2997 u64 len = *plen;
2998 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2999
3000 if (off + olen > inode->i_size || off + olen < off)
3001 return -EINVAL;
3002
3003 /* if we extend to eof, continue to block boundary */
3004 if (off + len == inode->i_size)
3005 *plen = len = ALIGN(inode->i_size, bs) - off;
3006
3007 /* Check that we are block aligned - btrfs_clone() requires this */
3008 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3009 return -EINVAL;
3010
3011 return 0;
3012 }
3013
3014 static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3015 struct inode *dst, u64 dst_loff)
3016 {
3017 int ret;
3018 u64 len = olen;
3019 struct cmp_pages cmp;
3020 int same_inode = 0;
3021 u64 same_lock_start = 0;
3022 u64 same_lock_len = 0;
3023
3024 if (src == dst)
3025 same_inode = 1;
3026
3027 if (len == 0)
3028 return 0;
3029
3030 if (same_inode) {
3031 mutex_lock(&src->i_mutex);
3032
3033 ret = extent_same_check_offsets(src, loff, &len, olen);
3034 if (ret)
3035 goto out_unlock;
3036
3037 /*
3038 * Single inode case wants the same checks, except we
3039 * don't want our length pushed out past i_size as
3040 * comparing that data range makes no sense.
3041 *
3042 * extent_same_check_offsets() will do this for an
3043 * unaligned length at i_size, so catch it here and
3044 * reject the request.
3045 *
3046 * This effectively means we require aligned extents
3047 * for the single-inode case, whereas the other cases
3048 * allow an unaligned length so long as it ends at
3049 * i_size.
3050 */
3051 if (len != olen) {
3052 ret = -EINVAL;
3053 goto out_unlock;
3054 }
3055
3056 /* Check for overlapping ranges */
3057 if (dst_loff + len > loff && dst_loff < loff + len) {
3058 ret = -EINVAL;
3059 goto out_unlock;
3060 }
3061
3062 same_lock_start = min_t(u64, loff, dst_loff);
3063 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
3064 } else {
3065 btrfs_double_inode_lock(src, dst);
3066
3067 ret = extent_same_check_offsets(src, loff, &len, olen);
3068 if (ret)
3069 goto out_unlock;
3070
3071 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3072 if (ret)
3073 goto out_unlock;
3074 }
3075
3076 /* don't make the dst file partly checksummed */
3077 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3078 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3079 ret = -EINVAL;
3080 goto out_unlock;
3081 }
3082
3083 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3084 if (ret)
3085 goto out_unlock;
3086
3087 if (same_inode)
3088 lock_extent_range(src, same_lock_start, same_lock_len);
3089 else
3090 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
3091
3092 /* pass original length for comparison so we stay within i_size */
3093 ret = btrfs_cmp_data(src, loff, dst, dst_loff, olen, &cmp);
3094 if (ret == 0)
3095 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
3096
3097 if (same_inode)
3098 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3099 same_lock_start + same_lock_len - 1);
3100 else
3101 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
3102
3103 btrfs_cmp_data_free(&cmp);
3104 out_unlock:
3105 if (same_inode)
3106 mutex_unlock(&src->i_mutex);
3107 else
3108 btrfs_double_inode_unlock(src, dst);
3109
3110 return ret;
3111 }
3112
3113 #define BTRFS_MAX_DEDUPE_LEN SZ_16M
3114
3115 static long btrfs_ioctl_file_extent_same(struct file *file,
3116 struct btrfs_ioctl_same_args __user *argp)
3117 {
3118 struct btrfs_ioctl_same_args *same = NULL;
3119 struct btrfs_ioctl_same_extent_info *info;
3120 struct inode *src = file_inode(file);
3121 u64 off;
3122 u64 len;
3123 int i;
3124 int ret;
3125 unsigned long size;
3126 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
3127 bool is_admin = capable(CAP_SYS_ADMIN);
3128 u16 count;
3129
3130 if (!(file->f_mode & FMODE_READ))
3131 return -EINVAL;
3132
3133 ret = mnt_want_write_file(file);
3134 if (ret)
3135 return ret;
3136
3137 if (get_user(count, &argp->dest_count)) {
3138 ret = -EFAULT;
3139 goto out;
3140 }
3141
3142 size = offsetof(struct btrfs_ioctl_same_args __user, info[count]);
3143
3144 same = memdup_user(argp, size);
3145
3146 if (IS_ERR(same)) {
3147 ret = PTR_ERR(same);
3148 same = NULL;
3149 goto out;
3150 }
3151
3152 off = same->logical_offset;
3153 len = same->length;
3154
3155 /*
3156 * Limit the total length we will dedupe for each operation.
3157 * This is intended to bound the total time spent in this
3158 * ioctl to something sane.
3159 */
3160 if (len > BTRFS_MAX_DEDUPE_LEN)
3161 len = BTRFS_MAX_DEDUPE_LEN;
3162
3163 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
3164 /*
3165 * Btrfs does not support blocksize < page_size. As a
3166 * result, btrfs_cmp_data() won't correctly handle
3167 * this situation without an update.
3168 */
3169 ret = -EINVAL;
3170 goto out;
3171 }
3172
3173 ret = -EISDIR;
3174 if (S_ISDIR(src->i_mode))
3175 goto out;
3176
3177 ret = -EACCES;
3178 if (!S_ISREG(src->i_mode))
3179 goto out;
3180
3181 /* pre-format output fields to sane values */
3182 for (i = 0; i < count; i++) {
3183 same->info[i].bytes_deduped = 0ULL;
3184 same->info[i].status = 0;
3185 }
3186
3187 for (i = 0, info = same->info; i < count; i++, info++) {
3188 struct inode *dst;
3189 struct fd dst_file = fdget(info->fd);
3190 if (!dst_file.file) {
3191 info->status = -EBADF;
3192 continue;
3193 }
3194 dst = file_inode(dst_file.file);
3195
3196 if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) {
3197 info->status = -EINVAL;
3198 } else if (file->f_path.mnt != dst_file.file->f_path.mnt) {
3199 info->status = -EXDEV;
3200 } else if (S_ISDIR(dst->i_mode)) {
3201 info->status = -EISDIR;
3202 } else if (!S_ISREG(dst->i_mode)) {
3203 info->status = -EACCES;
3204 } else {
3205 info->status = btrfs_extent_same(src, off, len, dst,
3206 info->logical_offset);
3207 if (info->status == 0)
3208 info->bytes_deduped += len;
3209 }
3210 fdput(dst_file);
3211 }
3212
3213 ret = copy_to_user(argp, same, size);
3214 if (ret)
3215 ret = -EFAULT;
3216
3217 out:
3218 mnt_drop_write_file(file);
3219 kfree(same);
3220 return ret;
3221 }
3222
3223 static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3224 struct inode *inode,
3225 u64 endoff,
3226 const u64 destoff,
3227 const u64 olen,
3228 int no_time_update)
3229 {
3230 struct btrfs_root *root = BTRFS_I(inode)->root;
3231 int ret;
3232
3233 inode_inc_iversion(inode);
3234 if (!no_time_update)
3235 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3236 /*
3237 * We round up to the block size at eof when determining which
3238 * extents to clone above, but shouldn't round up the file size.
3239 */
3240 if (endoff > destoff + olen)
3241 endoff = destoff + olen;
3242 if (endoff > inode->i_size)
3243 btrfs_i_size_write(inode, endoff);
3244
3245 ret = btrfs_update_inode(trans, root, inode);
3246 if (ret) {
3247 btrfs_abort_transaction(trans, root, ret);
3248 btrfs_end_transaction(trans, root);
3249 goto out;
3250 }
3251 ret = btrfs_end_transaction(trans, root);
3252 out:
3253 return ret;
3254 }
3255
3256 static void clone_update_extent_map(struct inode *inode,
3257 const struct btrfs_trans_handle *trans,
3258 const struct btrfs_path *path,
3259 const u64 hole_offset,
3260 const u64 hole_len)
3261 {
3262 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3263 struct extent_map *em;
3264 int ret;
3265
3266 em = alloc_extent_map();
3267 if (!em) {
3268 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3269 &BTRFS_I(inode)->runtime_flags);
3270 return;
3271 }
3272
3273 if (path) {
3274 struct btrfs_file_extent_item *fi;
3275
3276 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3277 struct btrfs_file_extent_item);
3278 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
3279 em->generation = -1;
3280 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3281 BTRFS_FILE_EXTENT_INLINE)
3282 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3283 &BTRFS_I(inode)->runtime_flags);
3284 } else {
3285 em->start = hole_offset;
3286 em->len = hole_len;
3287 em->ram_bytes = em->len;
3288 em->orig_start = hole_offset;
3289 em->block_start = EXTENT_MAP_HOLE;
3290 em->block_len = 0;
3291 em->orig_block_len = 0;
3292 em->compress_type = BTRFS_COMPRESS_NONE;
3293 em->generation = trans->transid;
3294 }
3295
3296 while (1) {
3297 write_lock(&em_tree->lock);
3298 ret = add_extent_mapping(em_tree, em, 1);
3299 write_unlock(&em_tree->lock);
3300 if (ret != -EEXIST) {
3301 free_extent_map(em);
3302 break;
3303 }
3304 btrfs_drop_extent_cache(inode, em->start,
3305 em->start + em->len - 1, 0);
3306 }
3307
3308 if (ret)
3309 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3310 &BTRFS_I(inode)->runtime_flags);
3311 }
3312
3313 /*
3314 * Make sure we do not end up inserting an inline extent into a file that has
3315 * already other (non-inline) extents. If a file has an inline extent it can
3316 * not have any other extents and the (single) inline extent must start at the
3317 * file offset 0. Failing to respect these rules will lead to file corruption,
3318 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3319 *
3320 * We can have extents that have been already written to disk or we can have
3321 * dirty ranges still in delalloc, in which case the extent maps and items are
3322 * created only when we run delalloc, and the delalloc ranges might fall outside
3323 * the range we are currently locking in the inode's io tree. So we check the
3324 * inode's i_size because of that (i_size updates are done while holding the
3325 * i_mutex, which we are holding here).
3326 * We also check to see if the inode has a size not greater than "datal" but has
3327 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3328 * protected against such concurrent fallocate calls by the i_mutex).
3329 *
3330 * If the file has no extents but a size greater than datal, do not allow the
3331 * copy because we would need turn the inline extent into a non-inline one (even
3332 * with NO_HOLES enabled). If we find our destination inode only has one inline
3333 * extent, just overwrite it with the source inline extent if its size is less
3334 * than the source extent's size, or we could copy the source inline extent's
3335 * data into the destination inode's inline extent if the later is greater then
3336 * the former.
3337 */
3338 static int clone_copy_inline_extent(struct inode *src,
3339 struct inode *dst,
3340 struct btrfs_trans_handle *trans,
3341 struct btrfs_path *path,
3342 struct btrfs_key *new_key,
3343 const u64 drop_start,
3344 const u64 datal,
3345 const u64 skip,
3346 const u64 size,
3347 char *inline_data)
3348 {
3349 struct btrfs_root *root = BTRFS_I(dst)->root;
3350 const u64 aligned_end = ALIGN(new_key->offset + datal,
3351 root->sectorsize);
3352 int ret;
3353 struct btrfs_key key;
3354
3355 if (new_key->offset > 0)
3356 return -EOPNOTSUPP;
3357
3358 key.objectid = btrfs_ino(dst);
3359 key.type = BTRFS_EXTENT_DATA_KEY;
3360 key.offset = 0;
3361 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3362 if (ret < 0) {
3363 return ret;
3364 } else if (ret > 0) {
3365 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3366 ret = btrfs_next_leaf(root, path);
3367 if (ret < 0)
3368 return ret;
3369 else if (ret > 0)
3370 goto copy_inline_extent;
3371 }
3372 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3373 if (key.objectid == btrfs_ino(dst) &&
3374 key.type == BTRFS_EXTENT_DATA_KEY) {
3375 ASSERT(key.offset > 0);
3376 return -EOPNOTSUPP;
3377 }
3378 } else if (i_size_read(dst) <= datal) {
3379 struct btrfs_file_extent_item *ei;
3380 u64 ext_len;
3381
3382 /*
3383 * If the file size is <= datal, make sure there are no other
3384 * extents following (can happen do to an fallocate call with
3385 * the flag FALLOC_FL_KEEP_SIZE).
3386 */
3387 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3388 struct btrfs_file_extent_item);
3389 /*
3390 * If it's an inline extent, it can not have other extents
3391 * following it.
3392 */
3393 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3394 BTRFS_FILE_EXTENT_INLINE)
3395 goto copy_inline_extent;
3396
3397 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3398 if (ext_len > aligned_end)
3399 return -EOPNOTSUPP;
3400
3401 ret = btrfs_next_item(root, path);
3402 if (ret < 0) {
3403 return ret;
3404 } else if (ret == 0) {
3405 btrfs_item_key_to_cpu(path->nodes[0], &key,
3406 path->slots[0]);
3407 if (key.objectid == btrfs_ino(dst) &&
3408 key.type == BTRFS_EXTENT_DATA_KEY)
3409 return -EOPNOTSUPP;
3410 }
3411 }
3412
3413 copy_inline_extent:
3414 /*
3415 * We have no extent items, or we have an extent at offset 0 which may
3416 * or may not be inlined. All these cases are dealt the same way.
3417 */
3418 if (i_size_read(dst) > datal) {
3419 /*
3420 * If the destination inode has an inline extent...
3421 * This would require copying the data from the source inline
3422 * extent into the beginning of the destination's inline extent.
3423 * But this is really complex, both extents can be compressed
3424 * or just one of them, which would require decompressing and
3425 * re-compressing data (which could increase the new compressed
3426 * size, not allowing the compressed data to fit anymore in an
3427 * inline extent).
3428 * So just don't support this case for now (it should be rare,
3429 * we are not really saving space when cloning inline extents).
3430 */
3431 return -EOPNOTSUPP;
3432 }
3433
3434 btrfs_release_path(path);
3435 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3436 if (ret)
3437 return ret;
3438 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3439 if (ret)
3440 return ret;
3441
3442 if (skip) {
3443 const u32 start = btrfs_file_extent_calc_inline_size(0);
3444
3445 memmove(inline_data + start, inline_data + start + skip, datal);
3446 }
3447
3448 write_extent_buffer(path->nodes[0], inline_data,
3449 btrfs_item_ptr_offset(path->nodes[0],
3450 path->slots[0]),
3451 size);
3452 inode_add_bytes(dst, datal);
3453
3454 return 0;
3455 }
3456
3457 /**
3458 * btrfs_clone() - clone a range from inode file to another
3459 *
3460 * @src: Inode to clone from
3461 * @inode: Inode to clone to
3462 * @off: Offset within source to start clone from
3463 * @olen: Original length, passed by user, of range to clone
3464 * @olen_aligned: Block-aligned value of olen
3465 * @destoff: Offset within @inode to start clone
3466 * @no_time_update: Whether to update mtime/ctime on the target inode
3467 */
3468 static int btrfs_clone(struct inode *src, struct inode *inode,
3469 const u64 off, const u64 olen, const u64 olen_aligned,
3470 const u64 destoff, int no_time_update)
3471 {
3472 struct btrfs_root *root = BTRFS_I(inode)->root;
3473 struct btrfs_path *path = NULL;
3474 struct extent_buffer *leaf;
3475 struct btrfs_trans_handle *trans;
3476 char *buf = NULL;
3477 struct btrfs_key key;
3478 u32 nritems;
3479 int slot;
3480 int ret;
3481 const u64 len = olen_aligned;
3482 u64 last_dest_end = destoff;
3483
3484 ret = -ENOMEM;
3485 buf = vmalloc(root->nodesize);
3486 if (!buf)
3487 return ret;
3488
3489 path = btrfs_alloc_path();
3490 if (!path) {
3491 vfree(buf);
3492 return ret;
3493 }
3494
3495 path->reada = READA_FORWARD;
3496 /* clone data */
3497 key.objectid = btrfs_ino(src);
3498 key.type = BTRFS_EXTENT_DATA_KEY;
3499 key.offset = off;
3500
3501 while (1) {
3502 u64 next_key_min_offset = key.offset + 1;
3503
3504 /*
3505 * note the key will change type as we walk through the
3506 * tree.
3507 */
3508 path->leave_spinning = 1;
3509 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3510 0, 0);
3511 if (ret < 0)
3512 goto out;
3513 /*
3514 * First search, if no extent item that starts at offset off was
3515 * found but the previous item is an extent item, it's possible
3516 * it might overlap our target range, therefore process it.
3517 */
3518 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3519 btrfs_item_key_to_cpu(path->nodes[0], &key,
3520 path->slots[0] - 1);
3521 if (key.type == BTRFS_EXTENT_DATA_KEY)
3522 path->slots[0]--;
3523 }
3524
3525 nritems = btrfs_header_nritems(path->nodes[0]);
3526 process_slot:
3527 if (path->slots[0] >= nritems) {
3528 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
3529 if (ret < 0)
3530 goto out;
3531 if (ret > 0)
3532 break;
3533 nritems = btrfs_header_nritems(path->nodes[0]);
3534 }
3535 leaf = path->nodes[0];
3536 slot = path->slots[0];
3537
3538 btrfs_item_key_to_cpu(leaf, &key, slot);
3539 if (key.type > BTRFS_EXTENT_DATA_KEY ||
3540 key.objectid != btrfs_ino(src))
3541 break;
3542
3543 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3544 struct btrfs_file_extent_item *extent;
3545 int type;
3546 u32 size;
3547 struct btrfs_key new_key;
3548 u64 disko = 0, diskl = 0;
3549 u64 datao = 0, datal = 0;
3550 u8 comp;
3551 u64 drop_start;
3552
3553 extent = btrfs_item_ptr(leaf, slot,
3554 struct btrfs_file_extent_item);
3555 comp = btrfs_file_extent_compression(leaf, extent);
3556 type = btrfs_file_extent_type(leaf, extent);
3557 if (type == BTRFS_FILE_EXTENT_REG ||
3558 type == BTRFS_FILE_EXTENT_PREALLOC) {
3559 disko = btrfs_file_extent_disk_bytenr(leaf,
3560 extent);
3561 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3562 extent);
3563 datao = btrfs_file_extent_offset(leaf, extent);
3564 datal = btrfs_file_extent_num_bytes(leaf,
3565 extent);
3566 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3567 /* take upper bound, may be compressed */
3568 datal = btrfs_file_extent_ram_bytes(leaf,
3569 extent);
3570 }
3571
3572 /*
3573 * The first search might have left us at an extent
3574 * item that ends before our target range's start, can
3575 * happen if we have holes and NO_HOLES feature enabled.
3576 */
3577 if (key.offset + datal <= off) {
3578 path->slots[0]++;
3579 goto process_slot;
3580 } else if (key.offset >= off + len) {
3581 break;
3582 }
3583 next_key_min_offset = key.offset + datal;
3584 size = btrfs_item_size_nr(leaf, slot);
3585 read_extent_buffer(leaf, buf,
3586 btrfs_item_ptr_offset(leaf, slot),
3587 size);
3588
3589 btrfs_release_path(path);
3590 path->leave_spinning = 0;
3591
3592 memcpy(&new_key, &key, sizeof(new_key));
3593 new_key.objectid = btrfs_ino(inode);
3594 if (off <= key.offset)
3595 new_key.offset = key.offset + destoff - off;
3596 else
3597 new_key.offset = destoff;
3598
3599 /*
3600 * Deal with a hole that doesn't have an extent item
3601 * that represents it (NO_HOLES feature enabled).
3602 * This hole is either in the middle of the cloning
3603 * range or at the beginning (fully overlaps it or
3604 * partially overlaps it).
3605 */
3606 if (new_key.offset != last_dest_end)
3607 drop_start = last_dest_end;
3608 else
3609 drop_start = new_key.offset;
3610
3611 /*
3612 * 1 - adjusting old extent (we may have to split it)
3613 * 1 - add new extent
3614 * 1 - inode update
3615 */
3616 trans = btrfs_start_transaction(root, 3);
3617 if (IS_ERR(trans)) {
3618 ret = PTR_ERR(trans);
3619 goto out;
3620 }
3621
3622 if (type == BTRFS_FILE_EXTENT_REG ||
3623 type == BTRFS_FILE_EXTENT_PREALLOC) {
3624 /*
3625 * a | --- range to clone ---| b
3626 * | ------------- extent ------------- |
3627 */
3628
3629 /* subtract range b */
3630 if (key.offset + datal > off + len)
3631 datal = off + len - key.offset;
3632
3633 /* subtract range a */
3634 if (off > key.offset) {
3635 datao += off - key.offset;
3636 datal -= off - key.offset;
3637 }
3638
3639 ret = btrfs_drop_extents(trans, root, inode,
3640 drop_start,
3641 new_key.offset + datal,
3642 1);
3643 if (ret) {
3644 if (ret != -EOPNOTSUPP)
3645 btrfs_abort_transaction(trans,
3646 root, ret);
3647 btrfs_end_transaction(trans, root);
3648 goto out;
3649 }
3650
3651 ret = btrfs_insert_empty_item(trans, root, path,
3652 &new_key, size);
3653 if (ret) {
3654 btrfs_abort_transaction(trans, root,
3655 ret);
3656 btrfs_end_transaction(trans, root);
3657 goto out;
3658 }
3659
3660 leaf = path->nodes[0];
3661 slot = path->slots[0];
3662 write_extent_buffer(leaf, buf,
3663 btrfs_item_ptr_offset(leaf, slot),
3664 size);
3665
3666 extent = btrfs_item_ptr(leaf, slot,
3667 struct btrfs_file_extent_item);
3668
3669 /* disko == 0 means it's a hole */
3670 if (!disko)
3671 datao = 0;
3672
3673 btrfs_set_file_extent_offset(leaf, extent,
3674 datao);
3675 btrfs_set_file_extent_num_bytes(leaf, extent,
3676 datal);
3677
3678 if (disko) {
3679 inode_add_bytes(inode, datal);
3680 ret = btrfs_inc_extent_ref(trans, root,
3681 disko, diskl, 0,
3682 root->root_key.objectid,
3683 btrfs_ino(inode),
3684 new_key.offset - datao);
3685 if (ret) {
3686 btrfs_abort_transaction(trans,
3687 root,
3688 ret);
3689 btrfs_end_transaction(trans,
3690 root);
3691 goto out;
3692
3693 }
3694 }
3695 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3696 u64 skip = 0;
3697 u64 trim = 0;
3698
3699 if (off > key.offset) {
3700 skip = off - key.offset;
3701 new_key.offset += skip;
3702 }
3703
3704 if (key.offset + datal > off + len)
3705 trim = key.offset + datal - (off + len);
3706
3707 if (comp && (skip || trim)) {
3708 ret = -EINVAL;
3709 btrfs_end_transaction(trans, root);
3710 goto out;
3711 }
3712 size -= skip + trim;
3713 datal -= skip + trim;
3714
3715 ret = clone_copy_inline_extent(src, inode,
3716 trans, path,
3717 &new_key,
3718 drop_start,
3719 datal,
3720 skip, size, buf);
3721 if (ret) {
3722 if (ret != -EOPNOTSUPP)
3723 btrfs_abort_transaction(trans,
3724 root,
3725 ret);
3726 btrfs_end_transaction(trans, root);
3727 goto out;
3728 }
3729 leaf = path->nodes[0];
3730 slot = path->slots[0];
3731 }
3732
3733 /* If we have an implicit hole (NO_HOLES feature). */
3734 if (drop_start < new_key.offset)
3735 clone_update_extent_map(inode, trans,
3736 NULL, drop_start,
3737 new_key.offset - drop_start);
3738
3739 clone_update_extent_map(inode, trans, path, 0, 0);
3740
3741 btrfs_mark_buffer_dirty(leaf);
3742 btrfs_release_path(path);
3743
3744 last_dest_end = ALIGN(new_key.offset + datal,
3745 root->sectorsize);
3746 ret = clone_finish_inode_update(trans, inode,
3747 last_dest_end,
3748 destoff, olen,
3749 no_time_update);
3750 if (ret)
3751 goto out;
3752 if (new_key.offset + datal >= destoff + len)
3753 break;
3754 }
3755 btrfs_release_path(path);
3756 key.offset = next_key_min_offset;
3757 }
3758 ret = 0;
3759
3760 if (last_dest_end < destoff + len) {
3761 /*
3762 * We have an implicit hole (NO_HOLES feature is enabled) that
3763 * fully or partially overlaps our cloning range at its end.
3764 */
3765 btrfs_release_path(path);
3766
3767 /*
3768 * 1 - remove extent(s)
3769 * 1 - inode update
3770 */
3771 trans = btrfs_start_transaction(root, 2);
3772 if (IS_ERR(trans)) {
3773 ret = PTR_ERR(trans);
3774 goto out;
3775 }
3776 ret = btrfs_drop_extents(trans, root, inode,
3777 last_dest_end, destoff + len, 1);
3778 if (ret) {
3779 if (ret != -EOPNOTSUPP)
3780 btrfs_abort_transaction(trans, root, ret);
3781 btrfs_end_transaction(trans, root);
3782 goto out;
3783 }
3784 clone_update_extent_map(inode, trans, NULL, last_dest_end,
3785 destoff + len - last_dest_end);
3786 ret = clone_finish_inode_update(trans, inode, destoff + len,
3787 destoff, olen, no_time_update);
3788 }
3789
3790 out:
3791 btrfs_free_path(path);
3792 vfree(buf);
3793 return ret;
3794 }
3795
3796 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3797 u64 off, u64 olen, u64 destoff)
3798 {
3799 struct inode *inode = file_inode(file);
3800 struct btrfs_root *root = BTRFS_I(inode)->root;
3801 struct fd src_file;
3802 struct inode *src;
3803 int ret;
3804 u64 len = olen;
3805 u64 bs = root->fs_info->sb->s_blocksize;
3806 int same_inode = 0;
3807
3808 /*
3809 * TODO:
3810 * - split compressed inline extents. annoying: we need to
3811 * decompress into destination's address_space (the file offset
3812 * may change, so source mapping won't do), then recompress (or
3813 * otherwise reinsert) a subrange.
3814 *
3815 * - split destination inode's inline extents. The inline extents can
3816 * be either compressed or non-compressed.
3817 */
3818
3819 /* the destination must be opened for writing */
3820 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3821 return -EINVAL;
3822
3823 if (btrfs_root_readonly(root))
3824 return -EROFS;
3825
3826 ret = mnt_want_write_file(file);
3827 if (ret)
3828 return ret;
3829
3830 src_file = fdget(srcfd);
3831 if (!src_file.file) {
3832 ret = -EBADF;
3833 goto out_drop_write;
3834 }
3835
3836 ret = -EXDEV;
3837 if (src_file.file->f_path.mnt != file->f_path.mnt)
3838 goto out_fput;
3839
3840 src = file_inode(src_file.file);
3841
3842 ret = -EINVAL;
3843 if (src == inode)
3844 same_inode = 1;
3845
3846 /* the src must be open for reading */
3847 if (!(src_file.file->f_mode & FMODE_READ))
3848 goto out_fput;
3849
3850 /* don't make the dst file partly checksummed */
3851 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3852 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3853 goto out_fput;
3854
3855 ret = -EISDIR;
3856 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3857 goto out_fput;
3858
3859 ret = -EXDEV;
3860 if (src->i_sb != inode->i_sb)
3861 goto out_fput;
3862
3863 if (!same_inode) {
3864 btrfs_double_inode_lock(src, inode);
3865 } else {
3866 mutex_lock(&src->i_mutex);
3867 }
3868
3869 /* determine range to clone */
3870 ret = -EINVAL;
3871 if (off + len > src->i_size || off + len < off)
3872 goto out_unlock;
3873 if (len == 0)
3874 olen = len = src->i_size - off;
3875 /* if we extend to eof, continue to block boundary */
3876 if (off + len == src->i_size)
3877 len = ALIGN(src->i_size, bs) - off;
3878
3879 if (len == 0) {
3880 ret = 0;
3881 goto out_unlock;
3882 }
3883
3884 /* verify the end result is block aligned */
3885 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3886 !IS_ALIGNED(destoff, bs))
3887 goto out_unlock;
3888
3889 /* verify if ranges are overlapped within the same file */
3890 if (same_inode) {
3891 if (destoff + len > off && destoff < off + len)
3892 goto out_unlock;
3893 }
3894
3895 if (destoff > inode->i_size) {
3896 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3897 if (ret)
3898 goto out_unlock;
3899 }
3900
3901 /*
3902 * Lock the target range too. Right after we replace the file extent
3903 * items in the fs tree (which now point to the cloned data), we might
3904 * have a worker replace them with extent items relative to a write
3905 * operation that was issued before this clone operation (i.e. confront
3906 * with inode.c:btrfs_finish_ordered_io).
3907 */
3908 if (same_inode) {
3909 u64 lock_start = min_t(u64, off, destoff);
3910 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
3911
3912 lock_extent_range(src, lock_start, lock_len);
3913 } else {
3914 btrfs_double_extent_lock(src, off, inode, destoff, len);
3915 }
3916
3917 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
3918
3919 if (same_inode) {
3920 u64 lock_start = min_t(u64, off, destoff);
3921 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3922
3923 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3924 } else {
3925 btrfs_double_extent_unlock(src, off, inode, destoff, len);
3926 }
3927 /*
3928 * Truncate page cache pages so that future reads will see the cloned
3929 * data immediately and not the previous data.
3930 */
3931 truncate_inode_pages_range(&inode->i_data, destoff,
3932 PAGE_CACHE_ALIGN(destoff + len) - 1);
3933 out_unlock:
3934 if (!same_inode)
3935 btrfs_double_inode_unlock(src, inode);
3936 else
3937 mutex_unlock(&src->i_mutex);
3938 out_fput:
3939 fdput(src_file);
3940 out_drop_write:
3941 mnt_drop_write_file(file);
3942 return ret;
3943 }
3944
3945 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
3946 {
3947 struct btrfs_ioctl_clone_range_args args;
3948
3949 if (copy_from_user(&args, argp, sizeof(args)))
3950 return -EFAULT;
3951 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3952 args.src_length, args.dest_offset);
3953 }
3954
3955 /*
3956 * there are many ways the trans_start and trans_end ioctls can lead
3957 * to deadlocks. They should only be used by applications that
3958 * basically own the machine, and have a very in depth understanding
3959 * of all the possible deadlocks and enospc problems.
3960 */
3961 static long btrfs_ioctl_trans_start(struct file *file)
3962 {
3963 struct inode *inode = file_inode(file);
3964 struct btrfs_root *root = BTRFS_I(inode)->root;
3965 struct btrfs_trans_handle *trans;
3966 int ret;
3967
3968 ret = -EPERM;
3969 if (!capable(CAP_SYS_ADMIN))
3970 goto out;
3971
3972 ret = -EINPROGRESS;
3973 if (file->private_data)
3974 goto out;
3975
3976 ret = -EROFS;
3977 if (btrfs_root_readonly(root))
3978 goto out;
3979
3980 ret = mnt_want_write_file(file);
3981 if (ret)
3982 goto out;
3983
3984 atomic_inc(&root->fs_info->open_ioctl_trans);
3985
3986 ret = -ENOMEM;
3987 trans = btrfs_start_ioctl_transaction(root);
3988 if (IS_ERR(trans))
3989 goto out_drop;
3990
3991 file->private_data = trans;
3992 return 0;
3993
3994 out_drop:
3995 atomic_dec(&root->fs_info->open_ioctl_trans);
3996 mnt_drop_write_file(file);
3997 out:
3998 return ret;
3999 }
4000
4001 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4002 {
4003 struct inode *inode = file_inode(file);
4004 struct btrfs_root *root = BTRFS_I(inode)->root;
4005 struct btrfs_root *new_root;
4006 struct btrfs_dir_item *di;
4007 struct btrfs_trans_handle *trans;
4008 struct btrfs_path *path;
4009 struct btrfs_key location;
4010 struct btrfs_disk_key disk_key;
4011 u64 objectid = 0;
4012 u64 dir_id;
4013 int ret;
4014
4015 if (!capable(CAP_SYS_ADMIN))
4016 return -EPERM;
4017
4018 ret = mnt_want_write_file(file);
4019 if (ret)
4020 return ret;
4021
4022 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4023 ret = -EFAULT;
4024 goto out;
4025 }
4026
4027 if (!objectid)
4028 objectid = BTRFS_FS_TREE_OBJECTID;
4029
4030 location.objectid = objectid;
4031 location.type = BTRFS_ROOT_ITEM_KEY;
4032 location.offset = (u64)-1;
4033
4034 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4035 if (IS_ERR(new_root)) {
4036 ret = PTR_ERR(new_root);
4037 goto out;
4038 }
4039
4040 path = btrfs_alloc_path();
4041 if (!path) {
4042 ret = -ENOMEM;
4043 goto out;
4044 }
4045 path->leave_spinning = 1;
4046
4047 trans = btrfs_start_transaction(root, 1);
4048 if (IS_ERR(trans)) {
4049 btrfs_free_path(path);
4050 ret = PTR_ERR(trans);
4051 goto out;
4052 }
4053
4054 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
4055 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
4056 dir_id, "default", 7, 1);
4057 if (IS_ERR_OR_NULL(di)) {
4058 btrfs_free_path(path);
4059 btrfs_end_transaction(trans, root);
4060 btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
4061 "item, this isn't going to work");
4062 ret = -ENOENT;
4063 goto out;
4064 }
4065
4066 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4067 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4068 btrfs_mark_buffer_dirty(path->nodes[0]);
4069 btrfs_free_path(path);
4070
4071 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
4072 btrfs_sysfs_feature_update(root->fs_info,
4073 BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL, FEAT_INCOMPAT);
4074 btrfs_end_transaction(trans, root);
4075 out:
4076 mnt_drop_write_file(file);
4077 return ret;
4078 }
4079
4080 void btrfs_get_block_group_info(struct list_head *groups_list,
4081 struct btrfs_ioctl_space_info *space)
4082 {
4083 struct btrfs_block_group_cache *block_group;
4084
4085 space->total_bytes = 0;
4086 space->used_bytes = 0;
4087 space->flags = 0;
4088 list_for_each_entry(block_group, groups_list, list) {
4089 space->flags = block_group->flags;
4090 space->total_bytes += block_group->key.offset;
4091 space->used_bytes +=
4092 btrfs_block_group_used(&block_group->item);
4093 }
4094 }
4095
4096 static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
4097 {
4098 struct btrfs_ioctl_space_args space_args;
4099 struct btrfs_ioctl_space_info space;
4100 struct btrfs_ioctl_space_info *dest;
4101 struct btrfs_ioctl_space_info *dest_orig;
4102 struct btrfs_ioctl_space_info __user *user_dest;
4103 struct btrfs_space_info *info;
4104 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
4105 BTRFS_BLOCK_GROUP_SYSTEM,
4106 BTRFS_BLOCK_GROUP_METADATA,
4107 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
4108 int num_types = 4;
4109 int alloc_size;
4110 int ret = 0;
4111 u64 slot_count = 0;
4112 int i, c;
4113
4114 if (copy_from_user(&space_args,
4115 (struct btrfs_ioctl_space_args __user *)arg,
4116 sizeof(space_args)))
4117 return -EFAULT;
4118
4119 for (i = 0; i < num_types; i++) {
4120 struct btrfs_space_info *tmp;
4121
4122 info = NULL;
4123 rcu_read_lock();
4124 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4125 list) {
4126 if (tmp->flags == types[i]) {
4127 info = tmp;
4128 break;
4129 }
4130 }
4131 rcu_read_unlock();
4132
4133 if (!info)
4134 continue;
4135
4136 down_read(&info->groups_sem);
4137 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4138 if (!list_empty(&info->block_groups[c]))
4139 slot_count++;
4140 }
4141 up_read(&info->groups_sem);
4142 }
4143
4144 /*
4145 * Global block reserve, exported as a space_info
4146 */
4147 slot_count++;
4148
4149 /* space_slots == 0 means they are asking for a count */
4150 if (space_args.space_slots == 0) {
4151 space_args.total_spaces = slot_count;
4152 goto out;
4153 }
4154
4155 slot_count = min_t(u64, space_args.space_slots, slot_count);
4156
4157 alloc_size = sizeof(*dest) * slot_count;
4158
4159 /* we generally have at most 6 or so space infos, one for each raid
4160 * level. So, a whole page should be more than enough for everyone
4161 */
4162 if (alloc_size > PAGE_CACHE_SIZE)
4163 return -ENOMEM;
4164
4165 space_args.total_spaces = 0;
4166 dest = kmalloc(alloc_size, GFP_KERNEL);
4167 if (!dest)
4168 return -ENOMEM;
4169 dest_orig = dest;
4170
4171 /* now we have a buffer to copy into */
4172 for (i = 0; i < num_types; i++) {
4173 struct btrfs_space_info *tmp;
4174
4175 if (!slot_count)
4176 break;
4177
4178 info = NULL;
4179 rcu_read_lock();
4180 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
4181 list) {
4182 if (tmp->flags == types[i]) {
4183 info = tmp;
4184 break;
4185 }
4186 }
4187 rcu_read_unlock();
4188
4189 if (!info)
4190 continue;
4191 down_read(&info->groups_sem);
4192 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4193 if (!list_empty(&info->block_groups[c])) {
4194 btrfs_get_block_group_info(
4195 &info->block_groups[c], &space);
4196 memcpy(dest, &space, sizeof(space));
4197 dest++;
4198 space_args.total_spaces++;
4199 slot_count--;
4200 }
4201 if (!slot_count)
4202 break;
4203 }
4204 up_read(&info->groups_sem);
4205 }
4206
4207 /*
4208 * Add global block reserve
4209 */
4210 if (slot_count) {
4211 struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
4212
4213 spin_lock(&block_rsv->lock);
4214 space.total_bytes = block_rsv->size;
4215 space.used_bytes = block_rsv->size - block_rsv->reserved;
4216 spin_unlock(&block_rsv->lock);
4217 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4218 memcpy(dest, &space, sizeof(space));
4219 space_args.total_spaces++;
4220 }
4221
4222 user_dest = (struct btrfs_ioctl_space_info __user *)
4223 (arg + sizeof(struct btrfs_ioctl_space_args));
4224
4225 if (copy_to_user(user_dest, dest_orig, alloc_size))
4226 ret = -EFAULT;
4227
4228 kfree(dest_orig);
4229 out:
4230 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
4231 ret = -EFAULT;
4232
4233 return ret;
4234 }
4235
4236 /*
4237 * there are many ways the trans_start and trans_end ioctls can lead
4238 * to deadlocks. They should only be used by applications that
4239 * basically own the machine, and have a very in depth understanding
4240 * of all the possible deadlocks and enospc problems.
4241 */
4242 long btrfs_ioctl_trans_end(struct file *file)
4243 {
4244 struct inode *inode = file_inode(file);
4245 struct btrfs_root *root = BTRFS_I(inode)->root;
4246 struct btrfs_trans_handle *trans;
4247
4248 trans = file->private_data;
4249 if (!trans)
4250 return -EINVAL;
4251 file->private_data = NULL;
4252
4253 btrfs_end_transaction(trans, root);
4254
4255 atomic_dec(&root->fs_info->open_ioctl_trans);
4256
4257 mnt_drop_write_file(file);
4258 return 0;
4259 }
4260
4261 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4262 void __user *argp)
4263 {
4264 struct btrfs_trans_handle *trans;
4265 u64 transid;
4266 int ret;
4267
4268 trans = btrfs_attach_transaction_barrier(root);
4269 if (IS_ERR(trans)) {
4270 if (PTR_ERR(trans) != -ENOENT)
4271 return PTR_ERR(trans);
4272
4273 /* No running transaction, don't bother */
4274 transid = root->fs_info->last_trans_committed;
4275 goto out;
4276 }
4277 transid = trans->transid;
4278 ret = btrfs_commit_transaction_async(trans, root, 0);
4279 if (ret) {
4280 btrfs_end_transaction(trans, root);
4281 return ret;
4282 }
4283 out:
4284 if (argp)
4285 if (copy_to_user(argp, &transid, sizeof(transid)))
4286 return -EFAULT;
4287 return 0;
4288 }
4289
4290 static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
4291 void __user *argp)
4292 {
4293 u64 transid;
4294
4295 if (argp) {
4296 if (copy_from_user(&transid, argp, sizeof(transid)))
4297 return -EFAULT;
4298 } else {
4299 transid = 0; /* current trans */
4300 }
4301 return btrfs_wait_for_commit(root, transid);
4302 }
4303
4304 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
4305 {
4306 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4307 struct btrfs_ioctl_scrub_args *sa;
4308 int ret;
4309
4310 if (!capable(CAP_SYS_ADMIN))
4311 return -EPERM;
4312
4313 sa = memdup_user(arg, sizeof(*sa));
4314 if (IS_ERR(sa))
4315 return PTR_ERR(sa);
4316
4317 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4318 ret = mnt_want_write_file(file);
4319 if (ret)
4320 goto out;
4321 }
4322
4323 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
4324 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4325 0);
4326
4327 if (copy_to_user(arg, sa, sizeof(*sa)))
4328 ret = -EFAULT;
4329
4330 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4331 mnt_drop_write_file(file);
4332 out:
4333 kfree(sa);
4334 return ret;
4335 }
4336
4337 static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
4338 {
4339 if (!capable(CAP_SYS_ADMIN))
4340 return -EPERM;
4341
4342 return btrfs_scrub_cancel(root->fs_info);
4343 }
4344
4345 static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
4346 void __user *arg)
4347 {
4348 struct btrfs_ioctl_scrub_args *sa;
4349 int ret;
4350
4351 if (!capable(CAP_SYS_ADMIN))
4352 return -EPERM;
4353
4354 sa = memdup_user(arg, sizeof(*sa));
4355 if (IS_ERR(sa))
4356 return PTR_ERR(sa);
4357
4358 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
4359
4360 if (copy_to_user(arg, sa, sizeof(*sa)))
4361 ret = -EFAULT;
4362
4363 kfree(sa);
4364 return ret;
4365 }
4366
4367 static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
4368 void __user *arg)
4369 {
4370 struct btrfs_ioctl_get_dev_stats *sa;
4371 int ret;
4372
4373 sa = memdup_user(arg, sizeof(*sa));
4374 if (IS_ERR(sa))
4375 return PTR_ERR(sa);
4376
4377 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4378 kfree(sa);
4379 return -EPERM;
4380 }
4381
4382 ret = btrfs_get_dev_stats(root, sa);
4383
4384 if (copy_to_user(arg, sa, sizeof(*sa)))
4385 ret = -EFAULT;
4386
4387 kfree(sa);
4388 return ret;
4389 }
4390
4391 static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
4392 {
4393 struct btrfs_ioctl_dev_replace_args *p;
4394 int ret;
4395
4396 if (!capable(CAP_SYS_ADMIN))
4397 return -EPERM;
4398
4399 p = memdup_user(arg, sizeof(*p));
4400 if (IS_ERR(p))
4401 return PTR_ERR(p);
4402
4403 switch (p->cmd) {
4404 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
4405 if (root->fs_info->sb->s_flags & MS_RDONLY) {
4406 ret = -EROFS;
4407 goto out;
4408 }
4409 if (atomic_xchg(
4410 &root->fs_info->mutually_exclusive_operation_running,
4411 1)) {
4412 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4413 } else {
4414 ret = btrfs_dev_replace_start(root, p);
4415 atomic_set(
4416 &root->fs_info->mutually_exclusive_operation_running,
4417 0);
4418 }
4419 break;
4420 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4421 btrfs_dev_replace_status(root->fs_info, p);
4422 ret = 0;
4423 break;
4424 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4425 ret = btrfs_dev_replace_cancel(root->fs_info, p);
4426 break;
4427 default:
4428 ret = -EINVAL;
4429 break;
4430 }
4431
4432 if (copy_to_user(arg, p, sizeof(*p)))
4433 ret = -EFAULT;
4434 out:
4435 kfree(p);
4436 return ret;
4437 }
4438
4439 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4440 {
4441 int ret = 0;
4442 int i;
4443 u64 rel_ptr;
4444 int size;
4445 struct btrfs_ioctl_ino_path_args *ipa = NULL;
4446 struct inode_fs_paths *ipath = NULL;
4447 struct btrfs_path *path;
4448
4449 if (!capable(CAP_DAC_READ_SEARCH))
4450 return -EPERM;
4451
4452 path = btrfs_alloc_path();
4453 if (!path) {
4454 ret = -ENOMEM;
4455 goto out;
4456 }
4457
4458 ipa = memdup_user(arg, sizeof(*ipa));
4459 if (IS_ERR(ipa)) {
4460 ret = PTR_ERR(ipa);
4461 ipa = NULL;
4462 goto out;
4463 }
4464
4465 size = min_t(u32, ipa->size, 4096);
4466 ipath = init_ipath(size, root, path);
4467 if (IS_ERR(ipath)) {
4468 ret = PTR_ERR(ipath);
4469 ipath = NULL;
4470 goto out;
4471 }
4472
4473 ret = paths_from_inode(ipa->inum, ipath);
4474 if (ret < 0)
4475 goto out;
4476
4477 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
4478 rel_ptr = ipath->fspath->val[i] -
4479 (u64)(unsigned long)ipath->fspath->val;
4480 ipath->fspath->val[i] = rel_ptr;
4481 }
4482
4483 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
4484 (void *)(unsigned long)ipath->fspath, size);
4485 if (ret) {
4486 ret = -EFAULT;
4487 goto out;
4488 }
4489
4490 out:
4491 btrfs_free_path(path);
4492 free_ipath(ipath);
4493 kfree(ipa);
4494
4495 return ret;
4496 }
4497
4498 static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4499 {
4500 struct btrfs_data_container *inodes = ctx;
4501 const size_t c = 3 * sizeof(u64);
4502
4503 if (inodes->bytes_left >= c) {
4504 inodes->bytes_left -= c;
4505 inodes->val[inodes->elem_cnt] = inum;
4506 inodes->val[inodes->elem_cnt + 1] = offset;
4507 inodes->val[inodes->elem_cnt + 2] = root;
4508 inodes->elem_cnt += 3;
4509 } else {
4510 inodes->bytes_missing += c - inodes->bytes_left;
4511 inodes->bytes_left = 0;
4512 inodes->elem_missed += 3;
4513 }
4514
4515 return 0;
4516 }
4517
4518 static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
4519 void __user *arg)
4520 {
4521 int ret = 0;
4522 int size;
4523 struct btrfs_ioctl_logical_ino_args *loi;
4524 struct btrfs_data_container *inodes = NULL;
4525 struct btrfs_path *path = NULL;
4526
4527 if (!capable(CAP_SYS_ADMIN))
4528 return -EPERM;
4529
4530 loi = memdup_user(arg, sizeof(*loi));
4531 if (IS_ERR(loi)) {
4532 ret = PTR_ERR(loi);
4533 loi = NULL;
4534 goto out;
4535 }
4536
4537 path = btrfs_alloc_path();
4538 if (!path) {
4539 ret = -ENOMEM;
4540 goto out;
4541 }
4542
4543 size = min_t(u32, loi->size, SZ_64K);
4544 inodes = init_data_container(size);
4545 if (IS_ERR(inodes)) {
4546 ret = PTR_ERR(inodes);
4547 inodes = NULL;
4548 goto out;
4549 }
4550
4551 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
4552 build_ino_list, inodes);
4553 if (ret == -EINVAL)
4554 ret = -ENOENT;
4555 if (ret < 0)
4556 goto out;
4557
4558 ret = copy_to_user((void *)(unsigned long)loi->inodes,
4559 (void *)(unsigned long)inodes, size);
4560 if (ret)
4561 ret = -EFAULT;
4562
4563 out:
4564 btrfs_free_path(path);
4565 vfree(inodes);
4566 kfree(loi);
4567
4568 return ret;
4569 }
4570
4571 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
4572 struct btrfs_ioctl_balance_args *bargs)
4573 {
4574 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4575
4576 bargs->flags = bctl->flags;
4577
4578 if (atomic_read(&fs_info->balance_running))
4579 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4580 if (atomic_read(&fs_info->balance_pause_req))
4581 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
4582 if (atomic_read(&fs_info->balance_cancel_req))
4583 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
4584
4585 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4586 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4587 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
4588
4589 if (lock) {
4590 spin_lock(&fs_info->balance_lock);
4591 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4592 spin_unlock(&fs_info->balance_lock);
4593 } else {
4594 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4595 }
4596 }
4597
4598 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
4599 {
4600 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4601 struct btrfs_fs_info *fs_info = root->fs_info;
4602 struct btrfs_ioctl_balance_args *bargs;
4603 struct btrfs_balance_control *bctl;
4604 bool need_unlock; /* for mut. excl. ops lock */
4605 int ret;
4606
4607 if (!capable(CAP_SYS_ADMIN))
4608 return -EPERM;
4609
4610 ret = mnt_want_write_file(file);
4611 if (ret)
4612 return ret;
4613
4614 again:
4615 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
4616 mutex_lock(&fs_info->volume_mutex);
4617 mutex_lock(&fs_info->balance_mutex);
4618 need_unlock = true;
4619 goto locked;
4620 }
4621
4622 /*
4623 * mut. excl. ops lock is locked. Three possibilites:
4624 * (1) some other op is running
4625 * (2) balance is running
4626 * (3) balance is paused -- special case (think resume)
4627 */
4628 mutex_lock(&fs_info->balance_mutex);
4629 if (fs_info->balance_ctl) {
4630 /* this is either (2) or (3) */
4631 if (!atomic_read(&fs_info->balance_running)) {
4632 mutex_unlock(&fs_info->balance_mutex);
4633 if (!mutex_trylock(&fs_info->volume_mutex))
4634 goto again;
4635 mutex_lock(&fs_info->balance_mutex);
4636
4637 if (fs_info->balance_ctl &&
4638 !atomic_read(&fs_info->balance_running)) {
4639 /* this is (3) */
4640 need_unlock = false;
4641 goto locked;
4642 }
4643
4644 mutex_unlock(&fs_info->balance_mutex);
4645 mutex_unlock(&fs_info->volume_mutex);
4646 goto again;
4647 } else {
4648 /* this is (2) */
4649 mutex_unlock(&fs_info->balance_mutex);
4650 ret = -EINPROGRESS;
4651 goto out;
4652 }
4653 } else {
4654 /* this is (1) */
4655 mutex_unlock(&fs_info->balance_mutex);
4656 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4657 goto out;
4658 }
4659
4660 locked:
4661 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
4662
4663 if (arg) {
4664 bargs = memdup_user(arg, sizeof(*bargs));
4665 if (IS_ERR(bargs)) {
4666 ret = PTR_ERR(bargs);
4667 goto out_unlock;
4668 }
4669
4670 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4671 if (!fs_info->balance_ctl) {
4672 ret = -ENOTCONN;
4673 goto out_bargs;
4674 }
4675
4676 bctl = fs_info->balance_ctl;
4677 spin_lock(&fs_info->balance_lock);
4678 bctl->flags |= BTRFS_BALANCE_RESUME;
4679 spin_unlock(&fs_info->balance_lock);
4680
4681 goto do_balance;
4682 }
4683 } else {
4684 bargs = NULL;
4685 }
4686
4687 if (fs_info->balance_ctl) {
4688 ret = -EINPROGRESS;
4689 goto out_bargs;
4690 }
4691
4692 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
4693 if (!bctl) {
4694 ret = -ENOMEM;
4695 goto out_bargs;
4696 }
4697
4698 bctl->fs_info = fs_info;
4699 if (arg) {
4700 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4701 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4702 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4703
4704 bctl->flags = bargs->flags;
4705 } else {
4706 /* balance everything - no filters */
4707 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4708 }
4709
4710 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4711 ret = -EINVAL;
4712 goto out_bctl;
4713 }
4714
4715 do_balance:
4716 /*
4717 * Ownership of bctl and mutually_exclusive_operation_running
4718 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4719 * or, if restriper was paused all the way until unmount, in
4720 * free_fs_info. mutually_exclusive_operation_running is
4721 * cleared in __cancel_balance.
4722 */
4723 need_unlock = false;
4724
4725 ret = btrfs_balance(bctl, bargs);
4726 bctl = NULL;
4727
4728 if (arg) {
4729 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4730 ret = -EFAULT;
4731 }
4732
4733 out_bctl:
4734 kfree(bctl);
4735 out_bargs:
4736 kfree(bargs);
4737 out_unlock:
4738 mutex_unlock(&fs_info->balance_mutex);
4739 mutex_unlock(&fs_info->volume_mutex);
4740 if (need_unlock)
4741 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4742 out:
4743 mnt_drop_write_file(file);
4744 return ret;
4745 }
4746
4747 static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4748 {
4749 if (!capable(CAP_SYS_ADMIN))
4750 return -EPERM;
4751
4752 switch (cmd) {
4753 case BTRFS_BALANCE_CTL_PAUSE:
4754 return btrfs_pause_balance(root->fs_info);
4755 case BTRFS_BALANCE_CTL_CANCEL:
4756 return btrfs_cancel_balance(root->fs_info);
4757 }
4758
4759 return -EINVAL;
4760 }
4761
4762 static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4763 void __user *arg)
4764 {
4765 struct btrfs_fs_info *fs_info = root->fs_info;
4766 struct btrfs_ioctl_balance_args *bargs;
4767 int ret = 0;
4768
4769 if (!capable(CAP_SYS_ADMIN))
4770 return -EPERM;
4771
4772 mutex_lock(&fs_info->balance_mutex);
4773 if (!fs_info->balance_ctl) {
4774 ret = -ENOTCONN;
4775 goto out;
4776 }
4777
4778 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
4779 if (!bargs) {
4780 ret = -ENOMEM;
4781 goto out;
4782 }
4783
4784 update_ioctl_balance_args(fs_info, 1, bargs);
4785
4786 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4787 ret = -EFAULT;
4788
4789 kfree(bargs);
4790 out:
4791 mutex_unlock(&fs_info->balance_mutex);
4792 return ret;
4793 }
4794
4795 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4796 {
4797 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4798 struct btrfs_ioctl_quota_ctl_args *sa;
4799 struct btrfs_trans_handle *trans = NULL;
4800 int ret;
4801 int err;
4802
4803 if (!capable(CAP_SYS_ADMIN))
4804 return -EPERM;
4805
4806 ret = mnt_want_write_file(file);
4807 if (ret)
4808 return ret;
4809
4810 sa = memdup_user(arg, sizeof(*sa));
4811 if (IS_ERR(sa)) {
4812 ret = PTR_ERR(sa);
4813 goto drop_write;
4814 }
4815
4816 down_write(&root->fs_info->subvol_sem);
4817 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4818 if (IS_ERR(trans)) {
4819 ret = PTR_ERR(trans);
4820 goto out;
4821 }
4822
4823 switch (sa->cmd) {
4824 case BTRFS_QUOTA_CTL_ENABLE:
4825 ret = btrfs_quota_enable(trans, root->fs_info);
4826 break;
4827 case BTRFS_QUOTA_CTL_DISABLE:
4828 ret = btrfs_quota_disable(trans, root->fs_info);
4829 break;
4830 default:
4831 ret = -EINVAL;
4832 break;
4833 }
4834
4835 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4836 if (err && !ret)
4837 ret = err;
4838 out:
4839 kfree(sa);
4840 up_write(&root->fs_info->subvol_sem);
4841 drop_write:
4842 mnt_drop_write_file(file);
4843 return ret;
4844 }
4845
4846 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4847 {
4848 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4849 struct btrfs_ioctl_qgroup_assign_args *sa;
4850 struct btrfs_trans_handle *trans;
4851 int ret;
4852 int err;
4853
4854 if (!capable(CAP_SYS_ADMIN))
4855 return -EPERM;
4856
4857 ret = mnt_want_write_file(file);
4858 if (ret)
4859 return ret;
4860
4861 sa = memdup_user(arg, sizeof(*sa));
4862 if (IS_ERR(sa)) {
4863 ret = PTR_ERR(sa);
4864 goto drop_write;
4865 }
4866
4867 trans = btrfs_join_transaction(root);
4868 if (IS_ERR(trans)) {
4869 ret = PTR_ERR(trans);
4870 goto out;
4871 }
4872
4873 /* FIXME: check if the IDs really exist */
4874 if (sa->assign) {
4875 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4876 sa->src, sa->dst);
4877 } else {
4878 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4879 sa->src, sa->dst);
4880 }
4881
4882 /* update qgroup status and info */
4883 err = btrfs_run_qgroups(trans, root->fs_info);
4884 if (err < 0)
4885 btrfs_std_error(root->fs_info, ret,
4886 "failed to update qgroup status and info\n");
4887 err = btrfs_end_transaction(trans, root);
4888 if (err && !ret)
4889 ret = err;
4890
4891 out:
4892 kfree(sa);
4893 drop_write:
4894 mnt_drop_write_file(file);
4895 return ret;
4896 }
4897
4898 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4899 {
4900 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4901 struct btrfs_ioctl_qgroup_create_args *sa;
4902 struct btrfs_trans_handle *trans;
4903 int ret;
4904 int err;
4905
4906 if (!capable(CAP_SYS_ADMIN))
4907 return -EPERM;
4908
4909 ret = mnt_want_write_file(file);
4910 if (ret)
4911 return ret;
4912
4913 sa = memdup_user(arg, sizeof(*sa));
4914 if (IS_ERR(sa)) {
4915 ret = PTR_ERR(sa);
4916 goto drop_write;
4917 }
4918
4919 if (!sa->qgroupid) {
4920 ret = -EINVAL;
4921 goto out;
4922 }
4923
4924 trans = btrfs_join_transaction(root);
4925 if (IS_ERR(trans)) {
4926 ret = PTR_ERR(trans);
4927 goto out;
4928 }
4929
4930 /* FIXME: check if the IDs really exist */
4931 if (sa->create) {
4932 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid);
4933 } else {
4934 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4935 }
4936
4937 err = btrfs_end_transaction(trans, root);
4938 if (err && !ret)
4939 ret = err;
4940
4941 out:
4942 kfree(sa);
4943 drop_write:
4944 mnt_drop_write_file(file);
4945 return ret;
4946 }
4947
4948 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4949 {
4950 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4951 struct btrfs_ioctl_qgroup_limit_args *sa;
4952 struct btrfs_trans_handle *trans;
4953 int ret;
4954 int err;
4955 u64 qgroupid;
4956
4957 if (!capable(CAP_SYS_ADMIN))
4958 return -EPERM;
4959
4960 ret = mnt_want_write_file(file);
4961 if (ret)
4962 return ret;
4963
4964 sa = memdup_user(arg, sizeof(*sa));
4965 if (IS_ERR(sa)) {
4966 ret = PTR_ERR(sa);
4967 goto drop_write;
4968 }
4969
4970 trans = btrfs_join_transaction(root);
4971 if (IS_ERR(trans)) {
4972 ret = PTR_ERR(trans);
4973 goto out;
4974 }
4975
4976 qgroupid = sa->qgroupid;
4977 if (!qgroupid) {
4978 /* take the current subvol as qgroup */
4979 qgroupid = root->root_key.objectid;
4980 }
4981
4982 /* FIXME: check if the IDs really exist */
4983 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4984
4985 err = btrfs_end_transaction(trans, root);
4986 if (err && !ret)
4987 ret = err;
4988
4989 out:
4990 kfree(sa);
4991 drop_write:
4992 mnt_drop_write_file(file);
4993 return ret;
4994 }
4995
4996 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4997 {
4998 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4999 struct btrfs_ioctl_quota_rescan_args *qsa;
5000 int ret;
5001
5002 if (!capable(CAP_SYS_ADMIN))
5003 return -EPERM;
5004
5005 ret = mnt_want_write_file(file);
5006 if (ret)
5007 return ret;
5008
5009 qsa = memdup_user(arg, sizeof(*qsa));
5010 if (IS_ERR(qsa)) {
5011 ret = PTR_ERR(qsa);
5012 goto drop_write;
5013 }
5014
5015 if (qsa->flags) {
5016 ret = -EINVAL;
5017 goto out;
5018 }
5019
5020 ret = btrfs_qgroup_rescan(root->fs_info);
5021
5022 out:
5023 kfree(qsa);
5024 drop_write:
5025 mnt_drop_write_file(file);
5026 return ret;
5027 }
5028
5029 static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5030 {
5031 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5032 struct btrfs_ioctl_quota_rescan_args *qsa;
5033 int ret = 0;
5034
5035 if (!capable(CAP_SYS_ADMIN))
5036 return -EPERM;
5037
5038 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
5039 if (!qsa)
5040 return -ENOMEM;
5041
5042 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
5043 qsa->flags = 1;
5044 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
5045 }
5046
5047 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5048 ret = -EFAULT;
5049
5050 kfree(qsa);
5051 return ret;
5052 }
5053
5054 static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5055 {
5056 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5057
5058 if (!capable(CAP_SYS_ADMIN))
5059 return -EPERM;
5060
5061 return btrfs_qgroup_wait_for_completion(root->fs_info);
5062 }
5063
5064 static long _btrfs_ioctl_set_received_subvol(struct file *file,
5065 struct btrfs_ioctl_received_subvol_args *sa)
5066 {
5067 struct inode *inode = file_inode(file);
5068 struct btrfs_root *root = BTRFS_I(inode)->root;
5069 struct btrfs_root_item *root_item = &root->root_item;
5070 struct btrfs_trans_handle *trans;
5071 struct timespec ct = CURRENT_TIME;
5072 int ret = 0;
5073 int received_uuid_changed;
5074
5075 if (!inode_owner_or_capable(inode))
5076 return -EPERM;
5077
5078 ret = mnt_want_write_file(file);
5079 if (ret < 0)
5080 return ret;
5081
5082 down_write(&root->fs_info->subvol_sem);
5083
5084 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
5085 ret = -EINVAL;
5086 goto out;
5087 }
5088
5089 if (btrfs_root_readonly(root)) {
5090 ret = -EROFS;
5091 goto out;
5092 }
5093
5094 /*
5095 * 1 - root item
5096 * 2 - uuid items (received uuid + subvol uuid)
5097 */
5098 trans = btrfs_start_transaction(root, 3);
5099 if (IS_ERR(trans)) {
5100 ret = PTR_ERR(trans);
5101 trans = NULL;
5102 goto out;
5103 }
5104
5105 sa->rtransid = trans->transid;
5106 sa->rtime.sec = ct.tv_sec;
5107 sa->rtime.nsec = ct.tv_nsec;
5108
5109 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5110 BTRFS_UUID_SIZE);
5111 if (received_uuid_changed &&
5112 !btrfs_is_empty_uuid(root_item->received_uuid))
5113 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
5114 root_item->received_uuid,
5115 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5116 root->root_key.objectid);
5117 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5118 btrfs_set_root_stransid(root_item, sa->stransid);
5119 btrfs_set_root_rtransid(root_item, sa->rtransid);
5120 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5121 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5122 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5123 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
5124
5125 ret = btrfs_update_root(trans, root->fs_info->tree_root,
5126 &root->root_key, &root->root_item);
5127 if (ret < 0) {
5128 btrfs_end_transaction(trans, root);
5129 goto out;
5130 }
5131 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
5132 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
5133 sa->uuid,
5134 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5135 root->root_key.objectid);
5136 if (ret < 0 && ret != -EEXIST) {
5137 btrfs_abort_transaction(trans, root, ret);
5138 goto out;
5139 }
5140 }
5141 ret = btrfs_commit_transaction(trans, root);
5142 if (ret < 0) {
5143 btrfs_abort_transaction(trans, root, ret);
5144 goto out;
5145 }
5146
5147 out:
5148 up_write(&root->fs_info->subvol_sem);
5149 mnt_drop_write_file(file);
5150 return ret;
5151 }
5152
5153 #ifdef CONFIG_64BIT
5154 static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5155 void __user *arg)
5156 {
5157 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5158 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5159 int ret = 0;
5160
5161 args32 = memdup_user(arg, sizeof(*args32));
5162 if (IS_ERR(args32)) {
5163 ret = PTR_ERR(args32);
5164 args32 = NULL;
5165 goto out;
5166 }
5167
5168 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
5169 if (!args64) {
5170 ret = -ENOMEM;
5171 goto out;
5172 }
5173
5174 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5175 args64->stransid = args32->stransid;
5176 args64->rtransid = args32->rtransid;
5177 args64->stime.sec = args32->stime.sec;
5178 args64->stime.nsec = args32->stime.nsec;
5179 args64->rtime.sec = args32->rtime.sec;
5180 args64->rtime.nsec = args32->rtime.nsec;
5181 args64->flags = args32->flags;
5182
5183 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5184 if (ret)
5185 goto out;
5186
5187 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5188 args32->stransid = args64->stransid;
5189 args32->rtransid = args64->rtransid;
5190 args32->stime.sec = args64->stime.sec;
5191 args32->stime.nsec = args64->stime.nsec;
5192 args32->rtime.sec = args64->rtime.sec;
5193 args32->rtime.nsec = args64->rtime.nsec;
5194 args32->flags = args64->flags;
5195
5196 ret = copy_to_user(arg, args32, sizeof(*args32));
5197 if (ret)
5198 ret = -EFAULT;
5199
5200 out:
5201 kfree(args32);
5202 kfree(args64);
5203 return ret;
5204 }
5205 #endif
5206
5207 static long btrfs_ioctl_set_received_subvol(struct file *file,
5208 void __user *arg)
5209 {
5210 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5211 int ret = 0;
5212
5213 sa = memdup_user(arg, sizeof(*sa));
5214 if (IS_ERR(sa)) {
5215 ret = PTR_ERR(sa);
5216 sa = NULL;
5217 goto out;
5218 }
5219
5220 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5221
5222 if (ret)
5223 goto out;
5224
5225 ret = copy_to_user(arg, sa, sizeof(*sa));
5226 if (ret)
5227 ret = -EFAULT;
5228
5229 out:
5230 kfree(sa);
5231 return ret;
5232 }
5233
5234 static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5235 {
5236 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5237 size_t len;
5238 int ret;
5239 char label[BTRFS_LABEL_SIZE];
5240
5241 spin_lock(&root->fs_info->super_lock);
5242 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5243 spin_unlock(&root->fs_info->super_lock);
5244
5245 len = strnlen(label, BTRFS_LABEL_SIZE);
5246
5247 if (len == BTRFS_LABEL_SIZE) {
5248 btrfs_warn(root->fs_info,
5249 "label is too long, return the first %zu bytes", --len);
5250 }
5251
5252 ret = copy_to_user(arg, label, len);
5253
5254 return ret ? -EFAULT : 0;
5255 }
5256
5257 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5258 {
5259 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5260 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5261 struct btrfs_trans_handle *trans;
5262 char label[BTRFS_LABEL_SIZE];
5263 int ret;
5264
5265 if (!capable(CAP_SYS_ADMIN))
5266 return -EPERM;
5267
5268 if (copy_from_user(label, arg, sizeof(label)))
5269 return -EFAULT;
5270
5271 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
5272 btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
5273 BTRFS_LABEL_SIZE - 1);
5274 return -EINVAL;
5275 }
5276
5277 ret = mnt_want_write_file(file);
5278 if (ret)
5279 return ret;
5280
5281 trans = btrfs_start_transaction(root, 0);
5282 if (IS_ERR(trans)) {
5283 ret = PTR_ERR(trans);
5284 goto out_unlock;
5285 }
5286
5287 spin_lock(&root->fs_info->super_lock);
5288 strcpy(super_block->label, label);
5289 spin_unlock(&root->fs_info->super_lock);
5290 ret = btrfs_commit_transaction(trans, root);
5291
5292 out_unlock:
5293 mnt_drop_write_file(file);
5294 return ret;
5295 }
5296
5297 #define INIT_FEATURE_FLAGS(suffix) \
5298 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5299 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5300 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5301
5302 static int btrfs_ioctl_get_supported_features(struct file *file,
5303 void __user *arg)
5304 {
5305 static const struct btrfs_ioctl_feature_flags features[3] = {
5306 INIT_FEATURE_FLAGS(SUPP),
5307 INIT_FEATURE_FLAGS(SAFE_SET),
5308 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5309 };
5310
5311 if (copy_to_user(arg, &features, sizeof(features)))
5312 return -EFAULT;
5313
5314 return 0;
5315 }
5316
5317 static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5318 {
5319 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5320 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5321 struct btrfs_ioctl_feature_flags features;
5322
5323 features.compat_flags = btrfs_super_compat_flags(super_block);
5324 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5325 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5326
5327 if (copy_to_user(arg, &features, sizeof(features)))
5328 return -EFAULT;
5329
5330 return 0;
5331 }
5332
5333 static int check_feature_bits(struct btrfs_root *root,
5334 enum btrfs_feature_set set,
5335 u64 change_mask, u64 flags, u64 supported_flags,
5336 u64 safe_set, u64 safe_clear)
5337 {
5338 const char *type = btrfs_feature_set_names[set];
5339 char *names;
5340 u64 disallowed, unsupported;
5341 u64 set_mask = flags & change_mask;
5342 u64 clear_mask = ~flags & change_mask;
5343
5344 unsupported = set_mask & ~supported_flags;
5345 if (unsupported) {
5346 names = btrfs_printable_features(set, unsupported);
5347 if (names) {
5348 btrfs_warn(root->fs_info,
5349 "this kernel does not support the %s feature bit%s",
5350 names, strchr(names, ',') ? "s" : "");
5351 kfree(names);
5352 } else
5353 btrfs_warn(root->fs_info,
5354 "this kernel does not support %s bits 0x%llx",
5355 type, unsupported);
5356 return -EOPNOTSUPP;
5357 }
5358
5359 disallowed = set_mask & ~safe_set;
5360 if (disallowed) {
5361 names = btrfs_printable_features(set, disallowed);
5362 if (names) {
5363 btrfs_warn(root->fs_info,
5364 "can't set the %s feature bit%s while mounted",
5365 names, strchr(names, ',') ? "s" : "");
5366 kfree(names);
5367 } else
5368 btrfs_warn(root->fs_info,
5369 "can't set %s bits 0x%llx while mounted",
5370 type, disallowed);
5371 return -EPERM;
5372 }
5373
5374 disallowed = clear_mask & ~safe_clear;
5375 if (disallowed) {
5376 names = btrfs_printable_features(set, disallowed);
5377 if (names) {
5378 btrfs_warn(root->fs_info,
5379 "can't clear the %s feature bit%s while mounted",
5380 names, strchr(names, ',') ? "s" : "");
5381 kfree(names);
5382 } else
5383 btrfs_warn(root->fs_info,
5384 "can't clear %s bits 0x%llx while mounted",
5385 type, disallowed);
5386 return -EPERM;
5387 }
5388
5389 return 0;
5390 }
5391
5392 #define check_feature(root, change_mask, flags, mask_base) \
5393 check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
5394 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5395 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5396 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5397
5398 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5399 {
5400 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5401 struct btrfs_super_block *super_block = root->fs_info->super_copy;
5402 struct btrfs_ioctl_feature_flags flags[2];
5403 struct btrfs_trans_handle *trans;
5404 u64 newflags;
5405 int ret;
5406
5407 if (!capable(CAP_SYS_ADMIN))
5408 return -EPERM;
5409
5410 if (copy_from_user(flags, arg, sizeof(flags)))
5411 return -EFAULT;
5412
5413 /* Nothing to do */
5414 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5415 !flags[0].incompat_flags)
5416 return 0;
5417
5418 ret = check_feature(root, flags[0].compat_flags,
5419 flags[1].compat_flags, COMPAT);
5420 if (ret)
5421 return ret;
5422
5423 ret = check_feature(root, flags[0].compat_ro_flags,
5424 flags[1].compat_ro_flags, COMPAT_RO);
5425 if (ret)
5426 return ret;
5427
5428 ret = check_feature(root, flags[0].incompat_flags,
5429 flags[1].incompat_flags, INCOMPAT);
5430 if (ret)
5431 return ret;
5432
5433 trans = btrfs_start_transaction(root, 0);
5434 if (IS_ERR(trans))
5435 return PTR_ERR(trans);
5436
5437 spin_lock(&root->fs_info->super_lock);
5438 newflags = btrfs_super_compat_flags(super_block);
5439 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5440 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5441 btrfs_set_super_compat_flags(super_block, newflags);
5442
5443 newflags = btrfs_super_compat_ro_flags(super_block);
5444 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5445 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5446 btrfs_set_super_compat_ro_flags(super_block, newflags);
5447
5448 newflags = btrfs_super_incompat_flags(super_block);
5449 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5450 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5451 btrfs_set_super_incompat_flags(super_block, newflags);
5452 spin_unlock(&root->fs_info->super_lock);
5453
5454 return btrfs_commit_transaction(trans, root);
5455 }
5456
5457 long btrfs_ioctl(struct file *file, unsigned int
5458 cmd, unsigned long arg)
5459 {
5460 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5461 void __user *argp = (void __user *)arg;
5462
5463 switch (cmd) {
5464 case FS_IOC_GETFLAGS:
5465 return btrfs_ioctl_getflags(file, argp);
5466 case FS_IOC_SETFLAGS:
5467 return btrfs_ioctl_setflags(file, argp);
5468 case FS_IOC_GETVERSION:
5469 return btrfs_ioctl_getversion(file, argp);
5470 case FITRIM:
5471 return btrfs_ioctl_fitrim(file, argp);
5472 case BTRFS_IOC_SNAP_CREATE:
5473 return btrfs_ioctl_snap_create(file, argp, 0);
5474 case BTRFS_IOC_SNAP_CREATE_V2:
5475 return btrfs_ioctl_snap_create_v2(file, argp, 0);
5476 case BTRFS_IOC_SUBVOL_CREATE:
5477 return btrfs_ioctl_snap_create(file, argp, 1);
5478 case BTRFS_IOC_SUBVOL_CREATE_V2:
5479 return btrfs_ioctl_snap_create_v2(file, argp, 1);
5480 case BTRFS_IOC_SNAP_DESTROY:
5481 return btrfs_ioctl_snap_destroy(file, argp);
5482 case BTRFS_IOC_SUBVOL_GETFLAGS:
5483 return btrfs_ioctl_subvol_getflags(file, argp);
5484 case BTRFS_IOC_SUBVOL_SETFLAGS:
5485 return btrfs_ioctl_subvol_setflags(file, argp);
5486 case BTRFS_IOC_DEFAULT_SUBVOL:
5487 return btrfs_ioctl_default_subvol(file, argp);
5488 case BTRFS_IOC_DEFRAG:
5489 return btrfs_ioctl_defrag(file, NULL);
5490 case BTRFS_IOC_DEFRAG_RANGE:
5491 return btrfs_ioctl_defrag(file, argp);
5492 case BTRFS_IOC_RESIZE:
5493 return btrfs_ioctl_resize(file, argp);
5494 case BTRFS_IOC_ADD_DEV:
5495 return btrfs_ioctl_add_dev(root, argp);
5496 case BTRFS_IOC_RM_DEV:
5497 return btrfs_ioctl_rm_dev(file, argp);
5498 case BTRFS_IOC_FS_INFO:
5499 return btrfs_ioctl_fs_info(root, argp);
5500 case BTRFS_IOC_DEV_INFO:
5501 return btrfs_ioctl_dev_info(root, argp);
5502 case BTRFS_IOC_BALANCE:
5503 return btrfs_ioctl_balance(file, NULL);
5504 case BTRFS_IOC_CLONE:
5505 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
5506 case BTRFS_IOC_CLONE_RANGE:
5507 return btrfs_ioctl_clone_range(file, argp);
5508 case BTRFS_IOC_TRANS_START:
5509 return btrfs_ioctl_trans_start(file);
5510 case BTRFS_IOC_TRANS_END:
5511 return btrfs_ioctl_trans_end(file);
5512 case BTRFS_IOC_TREE_SEARCH:
5513 return btrfs_ioctl_tree_search(file, argp);
5514 case BTRFS_IOC_TREE_SEARCH_V2:
5515 return btrfs_ioctl_tree_search_v2(file, argp);
5516 case BTRFS_IOC_INO_LOOKUP:
5517 return btrfs_ioctl_ino_lookup(file, argp);
5518 case BTRFS_IOC_INO_PATHS:
5519 return btrfs_ioctl_ino_to_path(root, argp);
5520 case BTRFS_IOC_LOGICAL_INO:
5521 return btrfs_ioctl_logical_to_ino(root, argp);
5522 case BTRFS_IOC_SPACE_INFO:
5523 return btrfs_ioctl_space_info(root, argp);
5524 case BTRFS_IOC_SYNC: {
5525 int ret;
5526
5527 ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
5528 if (ret)
5529 return ret;
5530 ret = btrfs_sync_fs(file_inode(file)->i_sb, 1);
5531 /*
5532 * The transaction thread may want to do more work,
5533 * namely it pokes the cleaner ktread that will start
5534 * processing uncleaned subvols.
5535 */
5536 wake_up_process(root->fs_info->transaction_kthread);
5537 return ret;
5538 }
5539 case BTRFS_IOC_START_SYNC:
5540 return btrfs_ioctl_start_sync(root, argp);
5541 case BTRFS_IOC_WAIT_SYNC:
5542 return btrfs_ioctl_wait_sync(root, argp);
5543 case BTRFS_IOC_SCRUB:
5544 return btrfs_ioctl_scrub(file, argp);
5545 case BTRFS_IOC_SCRUB_CANCEL:
5546 return btrfs_ioctl_scrub_cancel(root, argp);
5547 case BTRFS_IOC_SCRUB_PROGRESS:
5548 return btrfs_ioctl_scrub_progress(root, argp);
5549 case BTRFS_IOC_BALANCE_V2:
5550 return btrfs_ioctl_balance(file, argp);
5551 case BTRFS_IOC_BALANCE_CTL:
5552 return btrfs_ioctl_balance_ctl(root, arg);
5553 case BTRFS_IOC_BALANCE_PROGRESS:
5554 return btrfs_ioctl_balance_progress(root, argp);
5555 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5556 return btrfs_ioctl_set_received_subvol(file, argp);
5557 #ifdef CONFIG_64BIT
5558 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5559 return btrfs_ioctl_set_received_subvol_32(file, argp);
5560 #endif
5561 case BTRFS_IOC_SEND:
5562 return btrfs_ioctl_send(file, argp);
5563 case BTRFS_IOC_GET_DEV_STATS:
5564 return btrfs_ioctl_get_dev_stats(root, argp);
5565 case BTRFS_IOC_QUOTA_CTL:
5566 return btrfs_ioctl_quota_ctl(file, argp);
5567 case BTRFS_IOC_QGROUP_ASSIGN:
5568 return btrfs_ioctl_qgroup_assign(file, argp);
5569 case BTRFS_IOC_QGROUP_CREATE:
5570 return btrfs_ioctl_qgroup_create(file, argp);
5571 case BTRFS_IOC_QGROUP_LIMIT:
5572 return btrfs_ioctl_qgroup_limit(file, argp);
5573 case BTRFS_IOC_QUOTA_RESCAN:
5574 return btrfs_ioctl_quota_rescan(file, argp);
5575 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5576 return btrfs_ioctl_quota_rescan_status(file, argp);
5577 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5578 return btrfs_ioctl_quota_rescan_wait(file, argp);
5579 case BTRFS_IOC_DEV_REPLACE:
5580 return btrfs_ioctl_dev_replace(root, argp);
5581 case BTRFS_IOC_GET_FSLABEL:
5582 return btrfs_ioctl_get_fslabel(file, argp);
5583 case BTRFS_IOC_SET_FSLABEL:
5584 return btrfs_ioctl_set_fslabel(file, argp);
5585 case BTRFS_IOC_FILE_EXTENT_SAME:
5586 return btrfs_ioctl_file_extent_same(file, argp);
5587 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5588 return btrfs_ioctl_get_supported_features(file, argp);
5589 case BTRFS_IOC_GET_FEATURES:
5590 return btrfs_ioctl_get_features(file, argp);
5591 case BTRFS_IOC_SET_FEATURES:
5592 return btrfs_ioctl_set_features(file, argp);
5593 }
5594
5595 return -ENOTTY;
5596 }
This page took 0.178148 seconds and 5 git commands to generate.