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