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