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