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