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