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