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