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